ac2fa193072bae8fb6eb853288a3e3f5db8fcbd4
[pandora-kernel.git] / drivers / gpu / drm / drm_crtc.c
1 /*
2  * Copyright (c) 2006-2008 Intel Corporation
3  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4  * Copyright (c) 2008 Red Hat Inc.
5  *
6  * DRM core CRTC related functions
7  *
8  * Permission to use, copy, modify, distribute, and sell this software and its
9  * documentation for any purpose is hereby granted without fee, provided that
10  * the above copyright notice appear in all copies and that both that copyright
11  * notice and this permission notice appear in supporting documentation, and
12  * that the name of the copyright holders not be used in advertising or
13  * publicity pertaining to distribution of the software without specific,
14  * written prior permission.  The copyright holders make no representations
15  * about the suitability of this software for any purpose.  It is provided "as
16  * is" without express or implied warranty.
17  *
18  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24  * OF THIS SOFTWARE.
25  *
26  * Authors:
27  *      Keith Packard
28  *      Eric Anholt <eric@anholt.net>
29  *      Dave Airlie <airlied@linux.ie>
30  *      Jesse Barnes <jesse.barnes@intel.com>
31  */
32 #include <linux/list.h>
33 #include "drm.h"
34 #include "drmP.h"
35 #include "drm_crtc.h"
36
37 struct drm_prop_enum_list {
38         int type;
39         char *name;
40 };
41
42 /* Avoid boilerplate.  I'm tired of typing. */
43 #define DRM_ENUM_NAME_FN(fnname, list)                          \
44         char *fnname(int val)                                   \
45         {                                                       \
46                 int i;                                          \
47                 for (i = 0; i < ARRAY_SIZE(list); i++) {        \
48                         if (list[i].type == val)                \
49                                 return list[i].name;            \
50                 }                                               \
51                 return "(unknown)";                             \
52         }
53
54 /*
55  * Global properties
56  */
57 static struct drm_prop_enum_list drm_dpms_enum_list[] =
58 {       { DRM_MODE_DPMS_ON, "On" },
59         { DRM_MODE_DPMS_STANDBY, "Standby" },
60         { DRM_MODE_DPMS_SUSPEND, "Suspend" },
61         { DRM_MODE_DPMS_OFF, "Off" }
62 };
63
64 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
65
66 /*
67  * Optional properties
68  */
69 static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
70 {
71         { DRM_MODE_SCALE_NONE, "None" },
72         { DRM_MODE_SCALE_FULLSCREEN, "Full" },
73         { DRM_MODE_SCALE_CENTER, "Center" },
74         { DRM_MODE_SCALE_ASPECT, "Full aspect" },
75 };
76
77 static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
78 {
79         { DRM_MODE_DITHERING_OFF, "Off" },
80         { DRM_MODE_DITHERING_ON, "On" },
81 };
82
83 /*
84  * Non-global properties, but "required" for certain connectors.
85  */
86 static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
87 {
88         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
89         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
90         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
91 };
92
93 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
94
95 static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
96 {
97         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
98         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
99         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
100 };
101
102 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
103                  drm_dvi_i_subconnector_enum_list)
104
105 static struct drm_prop_enum_list drm_tv_select_enum_list[] =
106 {
107         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
108         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
109         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
110         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
111         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
112 };
113
114 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
115
116 static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
117 {
118         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
119         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
120         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
121         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
122         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
123 };
124
125 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
126                  drm_tv_subconnector_enum_list)
127
128 struct drm_conn_prop_enum_list {
129         int type;
130         char *name;
131         int count;
132 };
133
134 /*
135  * Connector and encoder types.
136  */
137 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
138 {       { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
139         { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
140         { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
141         { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
142         { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
143         { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
144         { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
145         { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
146         { DRM_MODE_CONNECTOR_Component, "Component", 0 },
147         { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN", 0 },
148         { DRM_MODE_CONNECTOR_DisplayPort, "DisplayPort", 0 },
149         { DRM_MODE_CONNECTOR_HDMIA, "HDMI Type A", 0 },
150         { DRM_MODE_CONNECTOR_HDMIB, "HDMI Type B", 0 },
151         { DRM_MODE_CONNECTOR_TV, "TV", 0 },
152 };
153
154 static struct drm_prop_enum_list drm_encoder_enum_list[] =
155 {       { DRM_MODE_ENCODER_NONE, "None" },
156         { DRM_MODE_ENCODER_DAC, "DAC" },
157         { DRM_MODE_ENCODER_TMDS, "TMDS" },
158         { DRM_MODE_ENCODER_LVDS, "LVDS" },
159         { DRM_MODE_ENCODER_TVDAC, "TV" },
160 };
161
162 char *drm_get_encoder_name(struct drm_encoder *encoder)
163 {
164         static char buf[32];
165
166         snprintf(buf, 32, "%s-%d",
167                  drm_encoder_enum_list[encoder->encoder_type].name,
168                  encoder->base.id);
169         return buf;
170 }
171 EXPORT_SYMBOL(drm_get_encoder_name);
172
173 char *drm_get_connector_name(struct drm_connector *connector)
174 {
175         static char buf[32];
176
177         snprintf(buf, 32, "%s-%d",
178                  drm_connector_enum_list[connector->connector_type].name,
179                  connector->connector_type_id);
180         return buf;
181 }
182 EXPORT_SYMBOL(drm_get_connector_name);
183
184 char *drm_get_connector_status_name(enum drm_connector_status status)
185 {
186         if (status == connector_status_connected)
187                 return "connected";
188         else if (status == connector_status_disconnected)
189                 return "disconnected";
190         else
191                 return "unknown";
192 }
193
194 /**
195  * drm_mode_object_get - allocate a new identifier
196  * @dev: DRM device
197  * @ptr: object pointer, used to generate unique ID
198  * @type: object type
199  *
200  * LOCKING:
201  *
202  * Create a unique identifier based on @ptr in @dev's identifier space.  Used
203  * for tracking modes, CRTCs and connectors.
204  *
205  * RETURNS:
206  * New unique (relative to other objects in @dev) integer identifier for the
207  * object.
208  */
209 static int drm_mode_object_get(struct drm_device *dev,
210                                struct drm_mode_object *obj, uint32_t obj_type)
211 {
212         int new_id = 0;
213         int ret;
214
215 again:
216         if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
217                 DRM_ERROR("Ran out memory getting a mode number\n");
218                 return -EINVAL;
219         }
220
221         mutex_lock(&dev->mode_config.idr_mutex);
222         ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
223         mutex_unlock(&dev->mode_config.idr_mutex);
224         if (ret == -EAGAIN)
225                 goto again;
226
227         obj->id = new_id;
228         obj->type = obj_type;
229         return 0;
230 }
231
232 /**
233  * drm_mode_object_put - free an identifer
234  * @dev: DRM device
235  * @id: ID to free
236  *
237  * LOCKING:
238  * Caller must hold DRM mode_config lock.
239  *
240  * Free @id from @dev's unique identifier pool.
241  */
242 static void drm_mode_object_put(struct drm_device *dev,
243                                 struct drm_mode_object *object)
244 {
245         mutex_lock(&dev->mode_config.idr_mutex);
246         idr_remove(&dev->mode_config.crtc_idr, object->id);
247         mutex_unlock(&dev->mode_config.idr_mutex);
248 }
249
250 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
251                 uint32_t id, uint32_t type)
252 {
253         struct drm_mode_object *obj = NULL;
254
255         mutex_lock(&dev->mode_config.idr_mutex);
256         obj = idr_find(&dev->mode_config.crtc_idr, id);
257         if (!obj || (obj->type != type) || (obj->id != id))
258                 obj = NULL;
259         mutex_unlock(&dev->mode_config.idr_mutex);
260
261         return obj;
262 }
263 EXPORT_SYMBOL(drm_mode_object_find);
264
265 /**
266  * drm_framebuffer_init - initialize a framebuffer
267  * @dev: DRM device
268  *
269  * LOCKING:
270  * Caller must hold mode config lock.
271  *
272  * Allocates an ID for the framebuffer's parent mode object, sets its mode
273  * functions & device file and adds it to the master fd list.
274  *
275  * RETURNS:
276  * Zero on success, error code on falure.
277  */
278 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
279                          const struct drm_framebuffer_funcs *funcs)
280 {
281         int ret;
282
283         ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
284         if (ret) {
285                 return ret;
286         }
287
288         fb->dev = dev;
289         fb->funcs = funcs;
290         dev->mode_config.num_fb++;
291         list_add(&fb->head, &dev->mode_config.fb_list);
292
293         return 0;
294 }
295 EXPORT_SYMBOL(drm_framebuffer_init);
296
297 /**
298  * drm_framebuffer_cleanup - remove a framebuffer object
299  * @fb: framebuffer to remove
300  *
301  * LOCKING:
302  * Caller must hold mode config lock.
303  *
304  * Scans all the CRTCs in @dev's mode_config.  If they're using @fb, removes
305  * it, setting it to NULL.
306  */
307 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
308 {
309         struct drm_device *dev = fb->dev;
310         struct drm_crtc *crtc;
311         struct drm_mode_set set;
312         int ret;
313
314         /* remove from any CRTC */
315         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
316                 if (crtc->fb == fb) {
317                         /* should turn off the crtc */
318                         memset(&set, 0, sizeof(struct drm_mode_set));
319                         set.crtc = crtc;
320                         set.fb = NULL;
321                         ret = crtc->funcs->set_config(&set);
322                         if (ret)
323                                 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
324                 }
325         }
326
327         drm_mode_object_put(dev, &fb->base);
328         list_del(&fb->head);
329         dev->mode_config.num_fb--;
330 }
331 EXPORT_SYMBOL(drm_framebuffer_cleanup);
332
333 /**
334  * drm_crtc_init - Initialise a new CRTC object
335  * @dev: DRM device
336  * @crtc: CRTC object to init
337  * @funcs: callbacks for the new CRTC
338  *
339  * LOCKING:
340  * Caller must hold mode config lock.
341  *
342  * Inits a new object created as base part of an driver crtc object.
343  */
344 void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
345                    const struct drm_crtc_funcs *funcs)
346 {
347         crtc->dev = dev;
348         crtc->funcs = funcs;
349
350         mutex_lock(&dev->mode_config.mutex);
351         drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
352
353         list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
354         dev->mode_config.num_crtc++;
355         mutex_unlock(&dev->mode_config.mutex);
356 }
357 EXPORT_SYMBOL(drm_crtc_init);
358
359 /**
360  * drm_crtc_cleanup - Cleans up the core crtc usage.
361  * @crtc: CRTC to cleanup
362  *
363  * LOCKING:
364  * Caller must hold mode config lock.
365  *
366  * Cleanup @crtc. Removes from drm modesetting space
367  * does NOT free object, caller does that.
368  */
369 void drm_crtc_cleanup(struct drm_crtc *crtc)
370 {
371         struct drm_device *dev = crtc->dev;
372
373         if (crtc->gamma_store) {
374                 kfree(crtc->gamma_store);
375                 crtc->gamma_store = NULL;
376         }
377
378         drm_mode_object_put(dev, &crtc->base);
379         list_del(&crtc->head);
380         dev->mode_config.num_crtc--;
381 }
382 EXPORT_SYMBOL(drm_crtc_cleanup);
383
384 /**
385  * drm_mode_probed_add - add a mode to a connector's probed mode list
386  * @connector: connector the new mode
387  * @mode: mode data
388  *
389  * LOCKING:
390  * Caller must hold mode config lock.
391  *
392  * Add @mode to @connector's mode list for later use.
393  */
394 void drm_mode_probed_add(struct drm_connector *connector,
395                          struct drm_display_mode *mode)
396 {
397         list_add(&mode->head, &connector->probed_modes);
398 }
399 EXPORT_SYMBOL(drm_mode_probed_add);
400
401 /**
402  * drm_mode_remove - remove and free a mode
403  * @connector: connector list to modify
404  * @mode: mode to remove
405  *
406  * LOCKING:
407  * Caller must hold mode config lock.
408  *
409  * Remove @mode from @connector's mode list, then free it.
410  */
411 void drm_mode_remove(struct drm_connector *connector,
412                      struct drm_display_mode *mode)
413 {
414         list_del(&mode->head);
415         kfree(mode);
416 }
417 EXPORT_SYMBOL(drm_mode_remove);
418
419 /**
420  * drm_connector_init - Init a preallocated connector
421  * @dev: DRM device
422  * @connector: the connector to init
423  * @funcs: callbacks for this connector
424  * @name: user visible name of the connector
425  *
426  * LOCKING:
427  * Caller must hold @dev's mode_config lock.
428  *
429  * Initialises a preallocated connector. Connectors should be
430  * subclassed as part of driver connector objects.
431  */
432 void drm_connector_init(struct drm_device *dev,
433                      struct drm_connector *connector,
434                      const struct drm_connector_funcs *funcs,
435                      int connector_type)
436 {
437         mutex_lock(&dev->mode_config.mutex);
438
439         connector->dev = dev;
440         connector->funcs = funcs;
441         drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
442         connector->connector_type = connector_type;
443         connector->connector_type_id =
444                 ++drm_connector_enum_list[connector_type].count; /* TODO */
445         INIT_LIST_HEAD(&connector->user_modes);
446         INIT_LIST_HEAD(&connector->probed_modes);
447         INIT_LIST_HEAD(&connector->modes);
448         connector->edid_blob_ptr = NULL;
449
450         list_add_tail(&connector->head, &dev->mode_config.connector_list);
451         dev->mode_config.num_connector++;
452
453         drm_connector_attach_property(connector,
454                                       dev->mode_config.edid_property, 0);
455
456         drm_connector_attach_property(connector,
457                                       dev->mode_config.dpms_property, 0);
458
459         mutex_unlock(&dev->mode_config.mutex);
460 }
461 EXPORT_SYMBOL(drm_connector_init);
462
463 /**
464  * drm_connector_cleanup - cleans up an initialised connector
465  * @connector: connector to cleanup
466  *
467  * LOCKING:
468  * Caller must hold @dev's mode_config lock.
469  *
470  * Cleans up the connector but doesn't free the object.
471  */
472 void drm_connector_cleanup(struct drm_connector *connector)
473 {
474         struct drm_device *dev = connector->dev;
475         struct drm_display_mode *mode, *t;
476
477         list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
478                 drm_mode_remove(connector, mode);
479
480         list_for_each_entry_safe(mode, t, &connector->modes, head)
481                 drm_mode_remove(connector, mode);
482
483         list_for_each_entry_safe(mode, t, &connector->user_modes, head)
484                 drm_mode_remove(connector, mode);
485
486         kfree(connector->fb_helper_private);
487         mutex_lock(&dev->mode_config.mutex);
488         drm_mode_object_put(dev, &connector->base);
489         list_del(&connector->head);
490         mutex_unlock(&dev->mode_config.mutex);
491 }
492 EXPORT_SYMBOL(drm_connector_cleanup);
493
494 void drm_encoder_init(struct drm_device *dev,
495                       struct drm_encoder *encoder,
496                       const struct drm_encoder_funcs *funcs,
497                       int encoder_type)
498 {
499         mutex_lock(&dev->mode_config.mutex);
500
501         encoder->dev = dev;
502
503         drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
504         encoder->encoder_type = encoder_type;
505         encoder->funcs = funcs;
506
507         list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
508         dev->mode_config.num_encoder++;
509
510         mutex_unlock(&dev->mode_config.mutex);
511 }
512 EXPORT_SYMBOL(drm_encoder_init);
513
514 void drm_encoder_cleanup(struct drm_encoder *encoder)
515 {
516         struct drm_device *dev = encoder->dev;
517         mutex_lock(&dev->mode_config.mutex);
518         drm_mode_object_put(dev, &encoder->base);
519         list_del(&encoder->head);
520         mutex_unlock(&dev->mode_config.mutex);
521 }
522 EXPORT_SYMBOL(drm_encoder_cleanup);
523
524 /**
525  * drm_mode_create - create a new display mode
526  * @dev: DRM device
527  *
528  * LOCKING:
529  * Caller must hold DRM mode_config lock.
530  *
531  * Create a new drm_display_mode, give it an ID, and return it.
532  *
533  * RETURNS:
534  * Pointer to new mode on success, NULL on error.
535  */
536 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
537 {
538         struct drm_display_mode *nmode;
539
540         nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
541         if (!nmode)
542                 return NULL;
543
544         drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
545         return nmode;
546 }
547 EXPORT_SYMBOL(drm_mode_create);
548
549 /**
550  * drm_mode_destroy - remove a mode
551  * @dev: DRM device
552  * @mode: mode to remove
553  *
554  * LOCKING:
555  * Caller must hold mode config lock.
556  *
557  * Free @mode's unique identifier, then free it.
558  */
559 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
560 {
561         drm_mode_object_put(dev, &mode->base);
562
563         kfree(mode);
564 }
565 EXPORT_SYMBOL(drm_mode_destroy);
566
567 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
568 {
569         struct drm_property *edid;
570         struct drm_property *dpms;
571         int i;
572
573         /*
574          * Standard properties (apply to all connectors)
575          */
576         edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
577                                    DRM_MODE_PROP_IMMUTABLE,
578                                    "EDID", 0);
579         dev->mode_config.edid_property = edid;
580
581         dpms = drm_property_create(dev, DRM_MODE_PROP_ENUM,
582                                    "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
583         for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
584                 drm_property_add_enum(dpms, i, drm_dpms_enum_list[i].type,
585                                       drm_dpms_enum_list[i].name);
586         dev->mode_config.dpms_property = dpms;
587
588         return 0;
589 }
590
591 /**
592  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
593  * @dev: DRM device
594  *
595  * Called by a driver the first time a DVI-I connector is made.
596  */
597 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
598 {
599         struct drm_property *dvi_i_selector;
600         struct drm_property *dvi_i_subconnector;
601         int i;
602
603         if (dev->mode_config.dvi_i_select_subconnector_property)
604                 return 0;
605
606         dvi_i_selector =
607                 drm_property_create(dev, DRM_MODE_PROP_ENUM,
608                                     "select subconnector",
609                                     ARRAY_SIZE(drm_dvi_i_select_enum_list));
610         for (i = 0; i < ARRAY_SIZE(drm_dvi_i_select_enum_list); i++)
611                 drm_property_add_enum(dvi_i_selector, i,
612                                       drm_dvi_i_select_enum_list[i].type,
613                                       drm_dvi_i_select_enum_list[i].name);
614         dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
615
616         dvi_i_subconnector =
617                 drm_property_create(dev, DRM_MODE_PROP_ENUM |
618                                     DRM_MODE_PROP_IMMUTABLE,
619                                     "subconnector",
620                                     ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
621         for (i = 0; i < ARRAY_SIZE(drm_dvi_i_subconnector_enum_list); i++)
622                 drm_property_add_enum(dvi_i_subconnector, i,
623                                       drm_dvi_i_subconnector_enum_list[i].type,
624                                       drm_dvi_i_subconnector_enum_list[i].name);
625         dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
626
627         return 0;
628 }
629 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
630
631 /**
632  * drm_create_tv_properties - create TV specific connector properties
633  * @dev: DRM device
634  * @num_modes: number of different TV formats (modes) supported
635  * @modes: array of pointers to strings containing name of each format
636  *
637  * Called by a driver's TV initialization routine, this function creates
638  * the TV specific connector properties for a given device.  Caller is
639  * responsible for allocating a list of format names and passing them to
640  * this routine.
641  */
642 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
643                                   char *modes[])
644 {
645         struct drm_property *tv_selector;
646         struct drm_property *tv_subconnector;
647         int i;
648
649         if (dev->mode_config.tv_select_subconnector_property)
650                 return 0;
651
652         /*
653          * Basic connector properties
654          */
655         tv_selector = drm_property_create(dev, DRM_MODE_PROP_ENUM,
656                                           "select subconnector",
657                                           ARRAY_SIZE(drm_tv_select_enum_list));
658         for (i = 0; i < ARRAY_SIZE(drm_tv_select_enum_list); i++)
659                 drm_property_add_enum(tv_selector, i,
660                                       drm_tv_select_enum_list[i].type,
661                                       drm_tv_select_enum_list[i].name);
662         dev->mode_config.tv_select_subconnector_property = tv_selector;
663
664         tv_subconnector =
665                 drm_property_create(dev, DRM_MODE_PROP_ENUM |
666                                     DRM_MODE_PROP_IMMUTABLE, "subconnector",
667                                     ARRAY_SIZE(drm_tv_subconnector_enum_list));
668         for (i = 0; i < ARRAY_SIZE(drm_tv_subconnector_enum_list); i++)
669                 drm_property_add_enum(tv_subconnector, i,
670                                       drm_tv_subconnector_enum_list[i].type,
671                                       drm_tv_subconnector_enum_list[i].name);
672         dev->mode_config.tv_subconnector_property = tv_subconnector;
673
674         /*
675          * Other, TV specific properties: margins & TV modes.
676          */
677         dev->mode_config.tv_left_margin_property =
678                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
679                                     "left margin", 2);
680         dev->mode_config.tv_left_margin_property->values[0] = 0;
681         dev->mode_config.tv_left_margin_property->values[1] = 100;
682
683         dev->mode_config.tv_right_margin_property =
684                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
685                                     "right margin", 2);
686         dev->mode_config.tv_right_margin_property->values[0] = 0;
687         dev->mode_config.tv_right_margin_property->values[1] = 100;
688
689         dev->mode_config.tv_top_margin_property =
690                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
691                                     "top margin", 2);
692         dev->mode_config.tv_top_margin_property->values[0] = 0;
693         dev->mode_config.tv_top_margin_property->values[1] = 100;
694
695         dev->mode_config.tv_bottom_margin_property =
696                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
697                                     "bottom margin", 2);
698         dev->mode_config.tv_bottom_margin_property->values[0] = 0;
699         dev->mode_config.tv_bottom_margin_property->values[1] = 100;
700
701         dev->mode_config.tv_mode_property =
702                 drm_property_create(dev, DRM_MODE_PROP_ENUM,
703                                     "mode", num_modes);
704         for (i = 0; i < num_modes; i++)
705                 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
706                                       i, modes[i]);
707
708         dev->mode_config.tv_brightness_property =
709                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
710                                     "brightness", 2);
711         dev->mode_config.tv_brightness_property->values[0] = 0;
712         dev->mode_config.tv_brightness_property->values[1] = 100;
713
714         dev->mode_config.tv_contrast_property =
715                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
716                                     "contrast", 2);
717         dev->mode_config.tv_contrast_property->values[0] = 0;
718         dev->mode_config.tv_contrast_property->values[1] = 100;
719
720         dev->mode_config.tv_flicker_reduction_property =
721                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
722                                     "flicker reduction", 2);
723         dev->mode_config.tv_flicker_reduction_property->values[0] = 0;
724         dev->mode_config.tv_flicker_reduction_property->values[1] = 100;
725
726         dev->mode_config.tv_overscan_property =
727                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
728                                     "overscan", 2);
729         dev->mode_config.tv_overscan_property->values[0] = 0;
730         dev->mode_config.tv_overscan_property->values[1] = 100;
731
732         dev->mode_config.tv_saturation_property =
733                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
734                                     "saturation", 2);
735         dev->mode_config.tv_saturation_property->values[0] = 0;
736         dev->mode_config.tv_saturation_property->values[1] = 100;
737
738         dev->mode_config.tv_hue_property =
739                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
740                                     "hue", 2);
741         dev->mode_config.tv_hue_property->values[0] = 0;
742         dev->mode_config.tv_hue_property->values[1] = 100;
743
744         return 0;
745 }
746 EXPORT_SYMBOL(drm_mode_create_tv_properties);
747
748 /**
749  * drm_mode_create_scaling_mode_property - create scaling mode property
750  * @dev: DRM device
751  *
752  * Called by a driver the first time it's needed, must be attached to desired
753  * connectors.
754  */
755 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
756 {
757         struct drm_property *scaling_mode;
758         int i;
759
760         if (dev->mode_config.scaling_mode_property)
761                 return 0;
762
763         scaling_mode =
764                 drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
765                                     ARRAY_SIZE(drm_scaling_mode_enum_list));
766         for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++)
767                 drm_property_add_enum(scaling_mode, i,
768                                       drm_scaling_mode_enum_list[i].type,
769                                       drm_scaling_mode_enum_list[i].name);
770
771         dev->mode_config.scaling_mode_property = scaling_mode;
772
773         return 0;
774 }
775 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
776
777 /**
778  * drm_mode_create_dithering_property - create dithering property
779  * @dev: DRM device
780  *
781  * Called by a driver the first time it's needed, must be attached to desired
782  * connectors.
783  */
784 int drm_mode_create_dithering_property(struct drm_device *dev)
785 {
786         struct drm_property *dithering_mode;
787         int i;
788
789         if (dev->mode_config.dithering_mode_property)
790                 return 0;
791
792         dithering_mode =
793                 drm_property_create(dev, DRM_MODE_PROP_ENUM, "dithering",
794                                     ARRAY_SIZE(drm_dithering_mode_enum_list));
795         for (i = 0; i < ARRAY_SIZE(drm_dithering_mode_enum_list); i++)
796                 drm_property_add_enum(dithering_mode, i,
797                                       drm_dithering_mode_enum_list[i].type,
798                                       drm_dithering_mode_enum_list[i].name);
799         dev->mode_config.dithering_mode_property = dithering_mode;
800
801         return 0;
802 }
803 EXPORT_SYMBOL(drm_mode_create_dithering_property);
804
805 /**
806  * drm_mode_config_init - initialize DRM mode_configuration structure
807  * @dev: DRM device
808  *
809  * LOCKING:
810  * None, should happen single threaded at init time.
811  *
812  * Initialize @dev's mode_config structure, used for tracking the graphics
813  * configuration of @dev.
814  */
815 void drm_mode_config_init(struct drm_device *dev)
816 {
817         mutex_init(&dev->mode_config.mutex);
818         mutex_init(&dev->mode_config.idr_mutex);
819         INIT_LIST_HEAD(&dev->mode_config.fb_list);
820         INIT_LIST_HEAD(&dev->mode_config.fb_kernel_list);
821         INIT_LIST_HEAD(&dev->mode_config.crtc_list);
822         INIT_LIST_HEAD(&dev->mode_config.connector_list);
823         INIT_LIST_HEAD(&dev->mode_config.encoder_list);
824         INIT_LIST_HEAD(&dev->mode_config.property_list);
825         INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
826         idr_init(&dev->mode_config.crtc_idr);
827
828         mutex_lock(&dev->mode_config.mutex);
829         drm_mode_create_standard_connector_properties(dev);
830         mutex_unlock(&dev->mode_config.mutex);
831
832         /* Just to be sure */
833         dev->mode_config.num_fb = 0;
834         dev->mode_config.num_connector = 0;
835         dev->mode_config.num_crtc = 0;
836         dev->mode_config.num_encoder = 0;
837 }
838 EXPORT_SYMBOL(drm_mode_config_init);
839
840 int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
841 {
842         uint32_t total_objects = 0;
843
844         total_objects += dev->mode_config.num_crtc;
845         total_objects += dev->mode_config.num_connector;
846         total_objects += dev->mode_config.num_encoder;
847
848         if (total_objects == 0)
849                 return -EINVAL;
850
851         group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
852         if (!group->id_list)
853                 return -ENOMEM;
854
855         group->num_crtcs = 0;
856         group->num_connectors = 0;
857         group->num_encoders = 0;
858         return 0;
859 }
860
861 int drm_mode_group_init_legacy_group(struct drm_device *dev,
862                                      struct drm_mode_group *group)
863 {
864         struct drm_crtc *crtc;
865         struct drm_encoder *encoder;
866         struct drm_connector *connector;
867         int ret;
868
869         if ((ret = drm_mode_group_init(dev, group)))
870                 return ret;
871
872         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
873                 group->id_list[group->num_crtcs++] = crtc->base.id;
874
875         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
876                 group->id_list[group->num_crtcs + group->num_encoders++] =
877                 encoder->base.id;
878
879         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
880                 group->id_list[group->num_crtcs + group->num_encoders +
881                                group->num_connectors++] = connector->base.id;
882
883         return 0;
884 }
885
886 /**
887  * drm_mode_config_cleanup - free up DRM mode_config info
888  * @dev: DRM device
889  *
890  * LOCKING:
891  * Caller must hold mode config lock.
892  *
893  * Free up all the connectors and CRTCs associated with this DRM device, then
894  * free up the framebuffers and associated buffer objects.
895  *
896  * FIXME: cleanup any dangling user buffer objects too
897  */
898 void drm_mode_config_cleanup(struct drm_device *dev)
899 {
900         struct drm_connector *connector, *ot;
901         struct drm_crtc *crtc, *ct;
902         struct drm_encoder *encoder, *enct;
903         struct drm_framebuffer *fb, *fbt;
904         struct drm_property *property, *pt;
905
906         list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
907                                  head) {
908                 encoder->funcs->destroy(encoder);
909         }
910
911         list_for_each_entry_safe(connector, ot,
912                                  &dev->mode_config.connector_list, head) {
913                 connector->funcs->destroy(connector);
914         }
915
916         list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
917                                  head) {
918                 drm_property_destroy(dev, property);
919         }
920
921         list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
922                 fb->funcs->destroy(fb);
923         }
924
925         list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
926                 crtc->funcs->destroy(crtc);
927         }
928
929 }
930 EXPORT_SYMBOL(drm_mode_config_cleanup);
931
932 /**
933  * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
934  * @out: drm_mode_modeinfo struct to return to the user
935  * @in: drm_display_mode to use
936  *
937  * LOCKING:
938  * None.
939  *
940  * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
941  * the user.
942  */
943 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
944                                struct drm_display_mode *in)
945 {
946         out->clock = in->clock;
947         out->hdisplay = in->hdisplay;
948         out->hsync_start = in->hsync_start;
949         out->hsync_end = in->hsync_end;
950         out->htotal = in->htotal;
951         out->hskew = in->hskew;
952         out->vdisplay = in->vdisplay;
953         out->vsync_start = in->vsync_start;
954         out->vsync_end = in->vsync_end;
955         out->vtotal = in->vtotal;
956         out->vscan = in->vscan;
957         out->vrefresh = in->vrefresh;
958         out->flags = in->flags;
959         out->type = in->type;
960         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
961         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
962 }
963
964 /**
965  * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
966  * @out: drm_display_mode to return to the user
967  * @in: drm_mode_modeinfo to use
968  *
969  * LOCKING:
970  * None.
971  *
972  * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
973  * the caller.
974  */
975 void drm_crtc_convert_umode(struct drm_display_mode *out,
976                             struct drm_mode_modeinfo *in)
977 {
978         out->clock = in->clock;
979         out->hdisplay = in->hdisplay;
980         out->hsync_start = in->hsync_start;
981         out->hsync_end = in->hsync_end;
982         out->htotal = in->htotal;
983         out->hskew = in->hskew;
984         out->vdisplay = in->vdisplay;
985         out->vsync_start = in->vsync_start;
986         out->vsync_end = in->vsync_end;
987         out->vtotal = in->vtotal;
988         out->vscan = in->vscan;
989         out->vrefresh = in->vrefresh;
990         out->flags = in->flags;
991         out->type = in->type;
992         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
993         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
994 }
995
996 /**
997  * drm_mode_getresources - get graphics configuration
998  * @inode: inode from the ioctl
999  * @filp: file * from the ioctl
1000  * @cmd: cmd from ioctl
1001  * @arg: arg from ioctl
1002  *
1003  * LOCKING:
1004  * Takes mode config lock.
1005  *
1006  * Construct a set of configuration description structures and return
1007  * them to the user, including CRTC, connector and framebuffer configuration.
1008  *
1009  * Called by the user via ioctl.
1010  *
1011  * RETURNS:
1012  * Zero on success, errno on failure.
1013  */
1014 int drm_mode_getresources(struct drm_device *dev, void *data,
1015                           struct drm_file *file_priv)
1016 {
1017         struct drm_mode_card_res *card_res = data;
1018         struct list_head *lh;
1019         struct drm_framebuffer *fb;
1020         struct drm_connector *connector;
1021         struct drm_crtc *crtc;
1022         struct drm_encoder *encoder;
1023         int ret = 0;
1024         int connector_count = 0;
1025         int crtc_count = 0;
1026         int fb_count = 0;
1027         int encoder_count = 0;
1028         int copied = 0, i;
1029         uint32_t __user *fb_id;
1030         uint32_t __user *crtc_id;
1031         uint32_t __user *connector_id;
1032         uint32_t __user *encoder_id;
1033         struct drm_mode_group *mode_group;
1034
1035         mutex_lock(&dev->mode_config.mutex);
1036
1037         /*
1038          * For the non-control nodes we need to limit the list of resources
1039          * by IDs in the group list for this node
1040          */
1041         list_for_each(lh, &file_priv->fbs)
1042                 fb_count++;
1043
1044         mode_group = &file_priv->master->minor->mode_group;
1045         if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1046
1047                 list_for_each(lh, &dev->mode_config.crtc_list)
1048                         crtc_count++;
1049
1050                 list_for_each(lh, &dev->mode_config.connector_list)
1051                         connector_count++;
1052
1053                 list_for_each(lh, &dev->mode_config.encoder_list)
1054                         encoder_count++;
1055         } else {
1056
1057                 crtc_count = mode_group->num_crtcs;
1058                 connector_count = mode_group->num_connectors;
1059                 encoder_count = mode_group->num_encoders;
1060         }
1061
1062         card_res->max_height = dev->mode_config.max_height;
1063         card_res->min_height = dev->mode_config.min_height;
1064         card_res->max_width = dev->mode_config.max_width;
1065         card_res->min_width = dev->mode_config.min_width;
1066
1067         /* handle this in 4 parts */
1068         /* FBs */
1069         if (card_res->count_fbs >= fb_count) {
1070                 copied = 0;
1071                 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1072                 list_for_each_entry(fb, &file_priv->fbs, head) {
1073                         if (put_user(fb->base.id, fb_id + copied)) {
1074                                 ret = -EFAULT;
1075                                 goto out;
1076                         }
1077                         copied++;
1078                 }
1079         }
1080         card_res->count_fbs = fb_count;
1081
1082         /* CRTCs */
1083         if (card_res->count_crtcs >= crtc_count) {
1084                 copied = 0;
1085                 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1086                 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1087                         list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1088                                             head) {
1089                                 DRM_DEBUG_KMS("CRTC ID is %d\n", crtc->base.id);
1090                                 if (put_user(crtc->base.id, crtc_id + copied)) {
1091                                         ret = -EFAULT;
1092                                         goto out;
1093                                 }
1094                                 copied++;
1095                         }
1096                 } else {
1097                         for (i = 0; i < mode_group->num_crtcs; i++) {
1098                                 if (put_user(mode_group->id_list[i],
1099                                              crtc_id + copied)) {
1100                                         ret = -EFAULT;
1101                                         goto out;
1102                                 }
1103                                 copied++;
1104                         }
1105                 }
1106         }
1107         card_res->count_crtcs = crtc_count;
1108
1109         /* Encoders */
1110         if (card_res->count_encoders >= encoder_count) {
1111                 copied = 0;
1112                 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1113                 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1114                         list_for_each_entry(encoder,
1115                                             &dev->mode_config.encoder_list,
1116                                             head) {
1117                                 DRM_DEBUG_KMS("ENCODER ID is %d\n",
1118                                           encoder->base.id);
1119                                 if (put_user(encoder->base.id, encoder_id +
1120                                              copied)) {
1121                                         ret = -EFAULT;
1122                                         goto out;
1123                                 }
1124                                 copied++;
1125                         }
1126                 } else {
1127                         for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1128                                 if (put_user(mode_group->id_list[i],
1129                                              encoder_id + copied)) {
1130                                         ret = -EFAULT;
1131                                         goto out;
1132                                 }
1133                                 copied++;
1134                         }
1135
1136                 }
1137         }
1138         card_res->count_encoders = encoder_count;
1139
1140         /* Connectors */
1141         if (card_res->count_connectors >= connector_count) {
1142                 copied = 0;
1143                 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1144                 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1145                         list_for_each_entry(connector,
1146                                             &dev->mode_config.connector_list,
1147                                             head) {
1148                                 DRM_DEBUG_KMS("CONNECTOR ID is %d\n",
1149                                           connector->base.id);
1150                                 if (put_user(connector->base.id,
1151                                              connector_id + copied)) {
1152                                         ret = -EFAULT;
1153                                         goto out;
1154                                 }
1155                                 copied++;
1156                         }
1157                 } else {
1158                         int start = mode_group->num_crtcs +
1159                                 mode_group->num_encoders;
1160                         for (i = start; i < start + mode_group->num_connectors; i++) {
1161                                 if (put_user(mode_group->id_list[i],
1162                                              connector_id + copied)) {
1163                                         ret = -EFAULT;
1164                                         goto out;
1165                                 }
1166                                 copied++;
1167                         }
1168                 }
1169         }
1170         card_res->count_connectors = connector_count;
1171
1172         DRM_DEBUG_KMS("Counted %d %d %d\n", card_res->count_crtcs,
1173                   card_res->count_connectors, card_res->count_encoders);
1174
1175 out:
1176         mutex_unlock(&dev->mode_config.mutex);
1177         return ret;
1178 }
1179
1180 /**
1181  * drm_mode_getcrtc - get CRTC configuration
1182  * @inode: inode from the ioctl
1183  * @filp: file * from the ioctl
1184  * @cmd: cmd from ioctl
1185  * @arg: arg from ioctl
1186  *
1187  * LOCKING:
1188  * Caller? (FIXME)
1189  *
1190  * Construct a CRTC configuration structure to return to the user.
1191  *
1192  * Called by the user via ioctl.
1193  *
1194  * RETURNS:
1195  * Zero on success, errno on failure.
1196  */
1197 int drm_mode_getcrtc(struct drm_device *dev,
1198                      void *data, struct drm_file *file_priv)
1199 {
1200         struct drm_mode_crtc *crtc_resp = data;
1201         struct drm_crtc *crtc;
1202         struct drm_mode_object *obj;
1203         int ret = 0;
1204
1205         mutex_lock(&dev->mode_config.mutex);
1206
1207         obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1208                                    DRM_MODE_OBJECT_CRTC);
1209         if (!obj) {
1210                 ret = -EINVAL;
1211                 goto out;
1212         }
1213         crtc = obj_to_crtc(obj);
1214
1215         crtc_resp->x = crtc->x;
1216         crtc_resp->y = crtc->y;
1217         crtc_resp->gamma_size = crtc->gamma_size;
1218         if (crtc->fb)
1219                 crtc_resp->fb_id = crtc->fb->base.id;
1220         else
1221                 crtc_resp->fb_id = 0;
1222
1223         if (crtc->enabled) {
1224
1225                 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1226                 crtc_resp->mode_valid = 1;
1227
1228         } else {
1229                 crtc_resp->mode_valid = 0;
1230         }
1231
1232 out:
1233         mutex_unlock(&dev->mode_config.mutex);
1234         return ret;
1235 }
1236
1237 /**
1238  * drm_mode_getconnector - get connector configuration
1239  * @inode: inode from the ioctl
1240  * @filp: file * from the ioctl
1241  * @cmd: cmd from ioctl
1242  * @arg: arg from ioctl
1243  *
1244  * LOCKING:
1245  * Caller? (FIXME)
1246  *
1247  * Construct a connector configuration structure to return to the user.
1248  *
1249  * Called by the user via ioctl.
1250  *
1251  * RETURNS:
1252  * Zero on success, errno on failure.
1253  */
1254 int drm_mode_getconnector(struct drm_device *dev, void *data,
1255                           struct drm_file *file_priv)
1256 {
1257         struct drm_mode_get_connector *out_resp = data;
1258         struct drm_mode_object *obj;
1259         struct drm_connector *connector;
1260         struct drm_display_mode *mode;
1261         int mode_count = 0;
1262         int props_count = 0;
1263         int encoders_count = 0;
1264         int ret = 0;
1265         int copied = 0;
1266         int i;
1267         struct drm_mode_modeinfo u_mode;
1268         struct drm_mode_modeinfo __user *mode_ptr;
1269         uint32_t __user *prop_ptr;
1270         uint64_t __user *prop_values;
1271         uint32_t __user *encoder_ptr;
1272
1273         memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1274
1275         DRM_DEBUG_KMS("connector id %d:\n", out_resp->connector_id);
1276
1277         mutex_lock(&dev->mode_config.mutex);
1278
1279         obj = drm_mode_object_find(dev, out_resp->connector_id,
1280                                    DRM_MODE_OBJECT_CONNECTOR);
1281         if (!obj) {
1282                 ret = -EINVAL;
1283                 goto out;
1284         }
1285         connector = obj_to_connector(obj);
1286
1287         for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1288                 if (connector->property_ids[i] != 0) {
1289                         props_count++;
1290                 }
1291         }
1292
1293         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1294                 if (connector->encoder_ids[i] != 0) {
1295                         encoders_count++;
1296                 }
1297         }
1298
1299         if (out_resp->count_modes == 0) {
1300                 connector->funcs->fill_modes(connector,
1301                                              dev->mode_config.max_width,
1302                                              dev->mode_config.max_height);
1303         }
1304
1305         /* delayed so we get modes regardless of pre-fill_modes state */
1306         list_for_each_entry(mode, &connector->modes, head)
1307                 mode_count++;
1308
1309         out_resp->connector_id = connector->base.id;
1310         out_resp->connector_type = connector->connector_type;
1311         out_resp->connector_type_id = connector->connector_type_id;
1312         out_resp->mm_width = connector->display_info.width_mm;
1313         out_resp->mm_height = connector->display_info.height_mm;
1314         out_resp->subpixel = connector->display_info.subpixel_order;
1315         out_resp->connection = connector->status;
1316         if (connector->encoder)
1317                 out_resp->encoder_id = connector->encoder->base.id;
1318         else
1319                 out_resp->encoder_id = 0;
1320
1321         /*
1322          * This ioctl is called twice, once to determine how much space is
1323          * needed, and the 2nd time to fill it.
1324          */
1325         if ((out_resp->count_modes >= mode_count) && mode_count) {
1326                 copied = 0;
1327                 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1328                 list_for_each_entry(mode, &connector->modes, head) {
1329                         drm_crtc_convert_to_umode(&u_mode, mode);
1330                         if (copy_to_user(mode_ptr + copied,
1331                                          &u_mode, sizeof(u_mode))) {
1332                                 ret = -EFAULT;
1333                                 goto out;
1334                         }
1335                         copied++;
1336                 }
1337         }
1338         out_resp->count_modes = mode_count;
1339
1340         if ((out_resp->count_props >= props_count) && props_count) {
1341                 copied = 0;
1342                 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1343                 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1344                 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1345                         if (connector->property_ids[i] != 0) {
1346                                 if (put_user(connector->property_ids[i],
1347                                              prop_ptr + copied)) {
1348                                         ret = -EFAULT;
1349                                         goto out;
1350                                 }
1351
1352                                 if (put_user(connector->property_values[i],
1353                                              prop_values + copied)) {
1354                                         ret = -EFAULT;
1355                                         goto out;
1356                                 }
1357                                 copied++;
1358                         }
1359                 }
1360         }
1361         out_resp->count_props = props_count;
1362
1363         if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1364                 copied = 0;
1365                 encoder_ptr = (uint32_t *)(unsigned long)(out_resp->encoders_ptr);
1366                 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1367                         if (connector->encoder_ids[i] != 0) {
1368                                 if (put_user(connector->encoder_ids[i],
1369                                              encoder_ptr + copied)) {
1370                                         ret = -EFAULT;
1371                                         goto out;
1372                                 }
1373                                 copied++;
1374                         }
1375                 }
1376         }
1377         out_resp->count_encoders = encoders_count;
1378
1379 out:
1380         mutex_unlock(&dev->mode_config.mutex);
1381         return ret;
1382 }
1383
1384 int drm_mode_getencoder(struct drm_device *dev, void *data,
1385                         struct drm_file *file_priv)
1386 {
1387         struct drm_mode_get_encoder *enc_resp = data;
1388         struct drm_mode_object *obj;
1389         struct drm_encoder *encoder;
1390         int ret = 0;
1391
1392         mutex_lock(&dev->mode_config.mutex);
1393         obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1394                                    DRM_MODE_OBJECT_ENCODER);
1395         if (!obj) {
1396                 ret = -EINVAL;
1397                 goto out;
1398         }
1399         encoder = obj_to_encoder(obj);
1400
1401         if (encoder->crtc)
1402                 enc_resp->crtc_id = encoder->crtc->base.id;
1403         else
1404                 enc_resp->crtc_id = 0;
1405         enc_resp->encoder_type = encoder->encoder_type;
1406         enc_resp->encoder_id = encoder->base.id;
1407         enc_resp->possible_crtcs = encoder->possible_crtcs;
1408         enc_resp->possible_clones = encoder->possible_clones;
1409
1410 out:
1411         mutex_unlock(&dev->mode_config.mutex);
1412         return ret;
1413 }
1414
1415 /**
1416  * drm_mode_setcrtc - set CRTC configuration
1417  * @inode: inode from the ioctl
1418  * @filp: file * from the ioctl
1419  * @cmd: cmd from ioctl
1420  * @arg: arg from ioctl
1421  *
1422  * LOCKING:
1423  * Caller? (FIXME)
1424  *
1425  * Build a new CRTC configuration based on user request.
1426  *
1427  * Called by the user via ioctl.
1428  *
1429  * RETURNS:
1430  * Zero on success, errno on failure.
1431  */
1432 int drm_mode_setcrtc(struct drm_device *dev, void *data,
1433                      struct drm_file *file_priv)
1434 {
1435         struct drm_mode_config *config = &dev->mode_config;
1436         struct drm_mode_crtc *crtc_req = data;
1437         struct drm_mode_object *obj;
1438         struct drm_crtc *crtc, *crtcfb;
1439         struct drm_connector **connector_set = NULL, *connector;
1440         struct drm_framebuffer *fb = NULL;
1441         struct drm_display_mode *mode = NULL;
1442         struct drm_mode_set set;
1443         uint32_t __user *set_connectors_ptr;
1444         int ret = 0;
1445         int i;
1446
1447         mutex_lock(&dev->mode_config.mutex);
1448         obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1449                                    DRM_MODE_OBJECT_CRTC);
1450         if (!obj) {
1451                 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1452                 ret = -EINVAL;
1453                 goto out;
1454         }
1455         crtc = obj_to_crtc(obj);
1456
1457         if (crtc_req->mode_valid) {
1458                 /* If we have a mode we need a framebuffer. */
1459                 /* If we pass -1, set the mode with the currently bound fb */
1460                 if (crtc_req->fb_id == -1) {
1461                         list_for_each_entry(crtcfb,
1462                                             &dev->mode_config.crtc_list, head) {
1463                                 if (crtcfb == crtc) {
1464                                         DRM_DEBUG_KMS("Using current fb for "
1465                                                         "setmode\n");
1466                                         fb = crtc->fb;
1467                                 }
1468                         }
1469                 } else {
1470                         obj = drm_mode_object_find(dev, crtc_req->fb_id,
1471                                                    DRM_MODE_OBJECT_FB);
1472                         if (!obj) {
1473                                 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1474                                                 crtc_req->fb_id);
1475                                 ret = -EINVAL;
1476                                 goto out;
1477                         }
1478                         fb = obj_to_fb(obj);
1479                 }
1480
1481                 mode = drm_mode_create(dev);
1482                 drm_crtc_convert_umode(mode, &crtc_req->mode);
1483                 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1484         }
1485
1486         if (crtc_req->count_connectors == 0 && mode) {
1487                 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1488                 ret = -EINVAL;
1489                 goto out;
1490         }
1491
1492         if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
1493                 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1494                           crtc_req->count_connectors);
1495                 ret = -EINVAL;
1496                 goto out;
1497         }
1498
1499         if (crtc_req->count_connectors > 0) {
1500                 u32 out_id;
1501
1502                 /* Avoid unbounded kernel memory allocation */
1503                 if (crtc_req->count_connectors > config->num_connector) {
1504                         ret = -EINVAL;
1505                         goto out;
1506                 }
1507
1508                 connector_set = kmalloc(crtc_req->count_connectors *
1509                                         sizeof(struct drm_connector *),
1510                                         GFP_KERNEL);
1511                 if (!connector_set) {
1512                         ret = -ENOMEM;
1513                         goto out;
1514                 }
1515
1516                 for (i = 0; i < crtc_req->count_connectors; i++) {
1517                         set_connectors_ptr = (uint32_t *)(unsigned long)crtc_req->set_connectors_ptr;
1518                         if (get_user(out_id, &set_connectors_ptr[i])) {
1519                                 ret = -EFAULT;
1520                                 goto out;
1521                         }
1522
1523                         obj = drm_mode_object_find(dev, out_id,
1524                                                    DRM_MODE_OBJECT_CONNECTOR);
1525                         if (!obj) {
1526                                 DRM_DEBUG_KMS("Connector id %d unknown\n",
1527                                                 out_id);
1528                                 ret = -EINVAL;
1529                                 goto out;
1530                         }
1531                         connector = obj_to_connector(obj);
1532
1533                         connector_set[i] = connector;
1534                 }
1535         }
1536
1537         set.crtc = crtc;
1538         set.x = crtc_req->x;
1539         set.y = crtc_req->y;
1540         set.mode = mode;
1541         set.connectors = connector_set;
1542         set.num_connectors = crtc_req->count_connectors;
1543         set.fb = fb;
1544         ret = crtc->funcs->set_config(&set);
1545
1546 out:
1547         kfree(connector_set);
1548         mutex_unlock(&dev->mode_config.mutex);
1549         return ret;
1550 }
1551
1552 int drm_mode_cursor_ioctl(struct drm_device *dev,
1553                         void *data, struct drm_file *file_priv)
1554 {
1555         struct drm_mode_cursor *req = data;
1556         struct drm_mode_object *obj;
1557         struct drm_crtc *crtc;
1558         int ret = 0;
1559
1560         if (!req->flags) {
1561                 DRM_ERROR("no operation set\n");
1562                 return -EINVAL;
1563         }
1564
1565         mutex_lock(&dev->mode_config.mutex);
1566         obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
1567         if (!obj) {
1568                 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
1569                 ret = -EINVAL;
1570                 goto out;
1571         }
1572         crtc = obj_to_crtc(obj);
1573
1574         if (req->flags & DRM_MODE_CURSOR_BO) {
1575                 if (!crtc->funcs->cursor_set) {
1576                         DRM_ERROR("crtc does not support cursor\n");
1577                         ret = -ENXIO;
1578                         goto out;
1579                 }
1580                 /* Turns off the cursor if handle is 0 */
1581                 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1582                                               req->width, req->height);
1583         }
1584
1585         if (req->flags & DRM_MODE_CURSOR_MOVE) {
1586                 if (crtc->funcs->cursor_move) {
1587                         ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1588                 } else {
1589                         DRM_ERROR("crtc does not support cursor\n");
1590                         ret = -EFAULT;
1591                         goto out;
1592                 }
1593         }
1594 out:
1595         mutex_unlock(&dev->mode_config.mutex);
1596         return ret;
1597 }
1598
1599 /**
1600  * drm_mode_addfb - add an FB to the graphics configuration
1601  * @inode: inode from the ioctl
1602  * @filp: file * from the ioctl
1603  * @cmd: cmd from ioctl
1604  * @arg: arg from ioctl
1605  *
1606  * LOCKING:
1607  * Takes mode config lock.
1608  *
1609  * Add a new FB to the specified CRTC, given a user request.
1610  *
1611  * Called by the user via ioctl.
1612  *
1613  * RETURNS:
1614  * Zero on success, errno on failure.
1615  */
1616 int drm_mode_addfb(struct drm_device *dev,
1617                    void *data, struct drm_file *file_priv)
1618 {
1619         struct drm_mode_fb_cmd *r = data;
1620         struct drm_mode_config *config = &dev->mode_config;
1621         struct drm_framebuffer *fb;
1622         int ret = 0;
1623
1624         if ((config->min_width > r->width) || (r->width > config->max_width)) {
1625                 DRM_ERROR("mode new framebuffer width not within limits\n");
1626                 return -EINVAL;
1627         }
1628         if ((config->min_height > r->height) || (r->height > config->max_height)) {
1629                 DRM_ERROR("mode new framebuffer height not within limits\n");
1630                 return -EINVAL;
1631         }
1632
1633         mutex_lock(&dev->mode_config.mutex);
1634
1635         /* TODO check buffer is sufficently large */
1636         /* TODO setup destructor callback */
1637
1638         fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
1639         if (!fb) {
1640                 DRM_ERROR("could not create framebuffer\n");
1641                 ret = -EINVAL;
1642                 goto out;
1643         }
1644
1645         r->fb_id = fb->base.id;
1646         list_add(&fb->filp_head, &file_priv->fbs);
1647
1648 out:
1649         mutex_unlock(&dev->mode_config.mutex);
1650         return ret;
1651 }
1652
1653 /**
1654  * drm_mode_rmfb - remove an FB from the configuration
1655  * @inode: inode from the ioctl
1656  * @filp: file * from the ioctl
1657  * @cmd: cmd from ioctl
1658  * @arg: arg from ioctl
1659  *
1660  * LOCKING:
1661  * Takes mode config lock.
1662  *
1663  * Remove the FB specified by the user.
1664  *
1665  * Called by the user via ioctl.
1666  *
1667  * RETURNS:
1668  * Zero on success, errno on failure.
1669  */
1670 int drm_mode_rmfb(struct drm_device *dev,
1671                    void *data, struct drm_file *file_priv)
1672 {
1673         struct drm_mode_object *obj;
1674         struct drm_framebuffer *fb = NULL;
1675         struct drm_framebuffer *fbl = NULL;
1676         uint32_t *id = data;
1677         int ret = 0;
1678         int found = 0;
1679
1680         mutex_lock(&dev->mode_config.mutex);
1681         obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
1682         /* TODO check that we realy get a framebuffer back. */
1683         if (!obj) {
1684                 DRM_ERROR("mode invalid framebuffer id\n");
1685                 ret = -EINVAL;
1686                 goto out;
1687         }
1688         fb = obj_to_fb(obj);
1689
1690         list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1691                 if (fb == fbl)
1692                         found = 1;
1693
1694         if (!found) {
1695                 DRM_ERROR("tried to remove a fb that we didn't own\n");
1696                 ret = -EINVAL;
1697                 goto out;
1698         }
1699
1700         /* TODO release all crtc connected to the framebuffer */
1701         /* TODO unhock the destructor from the buffer object */
1702
1703         list_del(&fb->filp_head);
1704         fb->funcs->destroy(fb);
1705
1706 out:
1707         mutex_unlock(&dev->mode_config.mutex);
1708         return ret;
1709 }
1710
1711 /**
1712  * drm_mode_getfb - get FB info
1713  * @inode: inode from the ioctl
1714  * @filp: file * from the ioctl
1715  * @cmd: cmd from ioctl
1716  * @arg: arg from ioctl
1717  *
1718  * LOCKING:
1719  * Caller? (FIXME)
1720  *
1721  * Lookup the FB given its ID and return info about it.
1722  *
1723  * Called by the user via ioctl.
1724  *
1725  * RETURNS:
1726  * Zero on success, errno on failure.
1727  */
1728 int drm_mode_getfb(struct drm_device *dev,
1729                    void *data, struct drm_file *file_priv)
1730 {
1731         struct drm_mode_fb_cmd *r = data;
1732         struct drm_mode_object *obj;
1733         struct drm_framebuffer *fb;
1734         int ret = 0;
1735
1736         mutex_lock(&dev->mode_config.mutex);
1737         obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
1738         if (!obj) {
1739                 DRM_ERROR("invalid framebuffer id\n");
1740                 ret = -EINVAL;
1741                 goto out;
1742         }
1743         fb = obj_to_fb(obj);
1744
1745         r->height = fb->height;
1746         r->width = fb->width;
1747         r->depth = fb->depth;
1748         r->bpp = fb->bits_per_pixel;
1749         r->pitch = fb->pitch;
1750         fb->funcs->create_handle(fb, file_priv, &r->handle);
1751
1752 out:
1753         mutex_unlock(&dev->mode_config.mutex);
1754         return ret;
1755 }
1756
1757 /**
1758  * drm_fb_release - remove and free the FBs on this file
1759  * @filp: file * from the ioctl
1760  *
1761  * LOCKING:
1762  * Takes mode config lock.
1763  *
1764  * Destroy all the FBs associated with @filp.
1765  *
1766  * Called by the user via ioctl.
1767  *
1768  * RETURNS:
1769  * Zero on success, errno on failure.
1770  */
1771 void drm_fb_release(struct drm_file *priv)
1772 {
1773         struct drm_device *dev = priv->minor->dev;
1774         struct drm_framebuffer *fb, *tfb;
1775
1776         mutex_lock(&dev->mode_config.mutex);
1777         list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1778                 list_del(&fb->filp_head);
1779                 fb->funcs->destroy(fb);
1780         }
1781         mutex_unlock(&dev->mode_config.mutex);
1782 }
1783
1784 /**
1785  * drm_mode_attachmode - add a mode to the user mode list
1786  * @dev: DRM device
1787  * @connector: connector to add the mode to
1788  * @mode: mode to add
1789  *
1790  * Add @mode to @connector's user mode list.
1791  */
1792 static int drm_mode_attachmode(struct drm_device *dev,
1793                                struct drm_connector *connector,
1794                                struct drm_display_mode *mode)
1795 {
1796         int ret = 0;
1797
1798         list_add_tail(&mode->head, &connector->user_modes);
1799         return ret;
1800 }
1801
1802 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1803                              struct drm_display_mode *mode)
1804 {
1805         struct drm_connector *connector;
1806         int ret = 0;
1807         struct drm_display_mode *dup_mode;
1808         int need_dup = 0;
1809         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1810                 if (!connector->encoder)
1811                         break;
1812                 if (connector->encoder->crtc == crtc) {
1813                         if (need_dup)
1814                                 dup_mode = drm_mode_duplicate(dev, mode);
1815                         else
1816                                 dup_mode = mode;
1817                         ret = drm_mode_attachmode(dev, connector, dup_mode);
1818                         if (ret)
1819                                 return ret;
1820                         need_dup = 1;
1821                 }
1822         }
1823         return 0;
1824 }
1825 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1826
1827 static int drm_mode_detachmode(struct drm_device *dev,
1828                                struct drm_connector *connector,
1829                                struct drm_display_mode *mode)
1830 {
1831         int found = 0;
1832         int ret = 0;
1833         struct drm_display_mode *match_mode, *t;
1834
1835         list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
1836                 if (drm_mode_equal(match_mode, mode)) {
1837                         list_del(&match_mode->head);
1838                         drm_mode_destroy(dev, match_mode);
1839                         found = 1;
1840                         break;
1841                 }
1842         }
1843
1844         if (!found)
1845                 ret = -EINVAL;
1846
1847         return ret;
1848 }
1849
1850 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
1851 {
1852         struct drm_connector *connector;
1853
1854         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1855                 drm_mode_detachmode(dev, connector, mode);
1856         }
1857         return 0;
1858 }
1859 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
1860
1861 /**
1862  * drm_fb_attachmode - Attach a user mode to an connector
1863  * @inode: inode from the ioctl
1864  * @filp: file * from the ioctl
1865  * @cmd: cmd from ioctl
1866  * @arg: arg from ioctl
1867  *
1868  * This attaches a user specified mode to an connector.
1869  * Called by the user via ioctl.
1870  *
1871  * RETURNS:
1872  * Zero on success, errno on failure.
1873  */
1874 int drm_mode_attachmode_ioctl(struct drm_device *dev,
1875                               void *data, struct drm_file *file_priv)
1876 {
1877         struct drm_mode_mode_cmd *mode_cmd = data;
1878         struct drm_connector *connector;
1879         struct drm_display_mode *mode;
1880         struct drm_mode_object *obj;
1881         struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1882         int ret = 0;
1883
1884         mutex_lock(&dev->mode_config.mutex);
1885
1886         obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1887         if (!obj) {
1888                 ret = -EINVAL;
1889                 goto out;
1890         }
1891         connector = obj_to_connector(obj);
1892
1893         mode = drm_mode_create(dev);
1894         if (!mode) {
1895                 ret = -ENOMEM;
1896                 goto out;
1897         }
1898
1899         drm_crtc_convert_umode(mode, umode);
1900
1901         ret = drm_mode_attachmode(dev, connector, mode);
1902 out:
1903         mutex_unlock(&dev->mode_config.mutex);
1904         return ret;
1905 }
1906
1907
1908 /**
1909  * drm_fb_detachmode - Detach a user specified mode from an connector
1910  * @inode: inode from the ioctl
1911  * @filp: file * from the ioctl
1912  * @cmd: cmd from ioctl
1913  * @arg: arg from ioctl
1914  *
1915  * Called by the user via ioctl.
1916  *
1917  * RETURNS:
1918  * Zero on success, errno on failure.
1919  */
1920 int drm_mode_detachmode_ioctl(struct drm_device *dev,
1921                               void *data, struct drm_file *file_priv)
1922 {
1923         struct drm_mode_object *obj;
1924         struct drm_mode_mode_cmd *mode_cmd = data;
1925         struct drm_connector *connector;
1926         struct drm_display_mode mode;
1927         struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1928         int ret = 0;
1929
1930         mutex_lock(&dev->mode_config.mutex);
1931
1932         obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1933         if (!obj) {
1934                 ret = -EINVAL;
1935                 goto out;
1936         }
1937         connector = obj_to_connector(obj);
1938
1939         drm_crtc_convert_umode(&mode, umode);
1940         ret = drm_mode_detachmode(dev, connector, &mode);
1941 out:
1942         mutex_unlock(&dev->mode_config.mutex);
1943         return ret;
1944 }
1945
1946 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
1947                                          const char *name, int num_values)
1948 {
1949         struct drm_property *property = NULL;
1950
1951         property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
1952         if (!property)
1953                 return NULL;
1954
1955         if (num_values) {
1956                 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
1957                 if (!property->values)
1958                         goto fail;
1959         }
1960
1961         drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
1962         property->flags = flags;
1963         property->num_values = num_values;
1964         INIT_LIST_HEAD(&property->enum_blob_list);
1965
1966         if (name)
1967                 strncpy(property->name, name, DRM_PROP_NAME_LEN);
1968
1969         list_add_tail(&property->head, &dev->mode_config.property_list);
1970         return property;
1971 fail:
1972         kfree(property);
1973         return NULL;
1974 }
1975 EXPORT_SYMBOL(drm_property_create);
1976
1977 int drm_property_add_enum(struct drm_property *property, int index,
1978                           uint64_t value, const char *name)
1979 {
1980         struct drm_property_enum *prop_enum;
1981
1982         if (!(property->flags & DRM_MODE_PROP_ENUM))
1983                 return -EINVAL;
1984
1985         if (!list_empty(&property->enum_blob_list)) {
1986                 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
1987                         if (prop_enum->value == value) {
1988                                 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1989                                 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1990                                 return 0;
1991                         }
1992                 }
1993         }
1994
1995         prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
1996         if (!prop_enum)
1997                 return -ENOMEM;
1998
1999         strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2000         prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2001         prop_enum->value = value;
2002
2003         property->values[index] = value;
2004         list_add_tail(&prop_enum->head, &property->enum_blob_list);
2005         return 0;
2006 }
2007 EXPORT_SYMBOL(drm_property_add_enum);
2008
2009 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2010 {
2011         struct drm_property_enum *prop_enum, *pt;
2012
2013         list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2014                 list_del(&prop_enum->head);
2015                 kfree(prop_enum);
2016         }
2017
2018         if (property->num_values)
2019                 kfree(property->values);
2020         drm_mode_object_put(dev, &property->base);
2021         list_del(&property->head);
2022         kfree(property);
2023 }
2024 EXPORT_SYMBOL(drm_property_destroy);
2025
2026 int drm_connector_attach_property(struct drm_connector *connector,
2027                                struct drm_property *property, uint64_t init_val)
2028 {
2029         int i;
2030
2031         for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2032                 if (connector->property_ids[i] == 0) {
2033                         connector->property_ids[i] = property->base.id;
2034                         connector->property_values[i] = init_val;
2035                         break;
2036                 }
2037         }
2038
2039         if (i == DRM_CONNECTOR_MAX_PROPERTY)
2040                 return -EINVAL;
2041         return 0;
2042 }
2043 EXPORT_SYMBOL(drm_connector_attach_property);
2044
2045 int drm_connector_property_set_value(struct drm_connector *connector,
2046                                   struct drm_property *property, uint64_t value)
2047 {
2048         int i;
2049
2050         for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2051                 if (connector->property_ids[i] == property->base.id) {
2052                         connector->property_values[i] = value;
2053                         break;
2054                 }
2055         }
2056
2057         if (i == DRM_CONNECTOR_MAX_PROPERTY)
2058                 return -EINVAL;
2059         return 0;
2060 }
2061 EXPORT_SYMBOL(drm_connector_property_set_value);
2062
2063 int drm_connector_property_get_value(struct drm_connector *connector,
2064                                   struct drm_property *property, uint64_t *val)
2065 {
2066         int i;
2067
2068         for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2069                 if (connector->property_ids[i] == property->base.id) {
2070                         *val = connector->property_values[i];
2071                         break;
2072                 }
2073         }
2074
2075         if (i == DRM_CONNECTOR_MAX_PROPERTY)
2076                 return -EINVAL;
2077         return 0;
2078 }
2079 EXPORT_SYMBOL(drm_connector_property_get_value);
2080
2081 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2082                                void *data, struct drm_file *file_priv)
2083 {
2084         struct drm_mode_object *obj;
2085         struct drm_mode_get_property *out_resp = data;
2086         struct drm_property *property;
2087         int enum_count = 0;
2088         int blob_count = 0;
2089         int value_count = 0;
2090         int ret = 0, i;
2091         int copied;
2092         struct drm_property_enum *prop_enum;
2093         struct drm_mode_property_enum __user *enum_ptr;
2094         struct drm_property_blob *prop_blob;
2095         uint32_t *blob_id_ptr;
2096         uint64_t __user *values_ptr;
2097         uint32_t __user *blob_length_ptr;
2098
2099         mutex_lock(&dev->mode_config.mutex);
2100         obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2101         if (!obj) {
2102                 ret = -EINVAL;
2103                 goto done;
2104         }
2105         property = obj_to_property(obj);
2106
2107         if (property->flags & DRM_MODE_PROP_ENUM) {
2108                 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2109                         enum_count++;
2110         } else if (property->flags & DRM_MODE_PROP_BLOB) {
2111                 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2112                         blob_count++;
2113         }
2114
2115         value_count = property->num_values;
2116
2117         strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2118         out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2119         out_resp->flags = property->flags;
2120
2121         if ((out_resp->count_values >= value_count) && value_count) {
2122                 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2123                 for (i = 0; i < value_count; i++) {
2124                         if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2125                                 ret = -EFAULT;
2126                                 goto done;
2127                         }
2128                 }
2129         }
2130         out_resp->count_values = value_count;
2131
2132         if (property->flags & DRM_MODE_PROP_ENUM) {
2133                 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2134                         copied = 0;
2135                         enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2136                         list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2137
2138                                 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2139                                         ret = -EFAULT;
2140                                         goto done;
2141                                 }
2142
2143                                 if (copy_to_user(&enum_ptr[copied].name,
2144                                                  &prop_enum->name, DRM_PROP_NAME_LEN)) {
2145                                         ret = -EFAULT;
2146                                         goto done;
2147                                 }
2148                                 copied++;
2149                         }
2150                 }
2151                 out_resp->count_enum_blobs = enum_count;
2152         }
2153
2154         if (property->flags & DRM_MODE_PROP_BLOB) {
2155                 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2156                         copied = 0;
2157                         blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2158                         blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2159
2160                         list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2161                                 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2162                                         ret = -EFAULT;
2163                                         goto done;
2164                                 }
2165
2166                                 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2167                                         ret = -EFAULT;
2168                                         goto done;
2169                                 }
2170
2171                                 copied++;
2172                         }
2173                 }
2174                 out_resp->count_enum_blobs = blob_count;
2175         }
2176 done:
2177         mutex_unlock(&dev->mode_config.mutex);
2178         return ret;
2179 }
2180
2181 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2182                                                           void *data)
2183 {
2184         struct drm_property_blob *blob;
2185
2186         if (!length || !data)
2187                 return NULL;
2188
2189         blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2190         if (!blob)
2191                 return NULL;
2192
2193         blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2194         blob->length = length;
2195
2196         memcpy(blob->data, data, length);
2197
2198         drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2199
2200         list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2201         return blob;
2202 }
2203
2204 static void drm_property_destroy_blob(struct drm_device *dev,
2205                                struct drm_property_blob *blob)
2206 {
2207         drm_mode_object_put(dev, &blob->base);
2208         list_del(&blob->head);
2209         kfree(blob);
2210 }
2211
2212 int drm_mode_getblob_ioctl(struct drm_device *dev,
2213                            void *data, struct drm_file *file_priv)
2214 {
2215         struct drm_mode_object *obj;
2216         struct drm_mode_get_blob *out_resp = data;
2217         struct drm_property_blob *blob;
2218         int ret = 0;
2219         void *blob_ptr;
2220
2221         mutex_lock(&dev->mode_config.mutex);
2222         obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2223         if (!obj) {
2224                 ret = -EINVAL;
2225                 goto done;
2226         }
2227         blob = obj_to_blob(obj);
2228
2229         if (out_resp->length == blob->length) {
2230                 blob_ptr = (void *)(unsigned long)out_resp->data;
2231                 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2232                         ret = -EFAULT;
2233                         goto done;
2234                 }
2235         }
2236         out_resp->length = blob->length;
2237
2238 done:
2239         mutex_unlock(&dev->mode_config.mutex);
2240         return ret;
2241 }
2242
2243 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
2244                                             struct edid *edid)
2245 {
2246         struct drm_device *dev = connector->dev;
2247         int ret = 0;
2248
2249         if (connector->edid_blob_ptr)
2250                 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
2251
2252         /* Delete edid, when there is none. */
2253         if (!edid) {
2254                 connector->edid_blob_ptr = NULL;
2255                 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
2256                 return ret;
2257         }
2258
2259         connector->edid_blob_ptr = drm_property_create_blob(connector->dev, 128, edid);
2260
2261         ret = drm_connector_property_set_value(connector,
2262                                                dev->mode_config.edid_property,
2263                                                connector->edid_blob_ptr->base.id);
2264
2265         return ret;
2266 }
2267 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
2268
2269 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2270                                        void *data, struct drm_file *file_priv)
2271 {
2272         struct drm_mode_connector_set_property *out_resp = data;
2273         struct drm_mode_object *obj;
2274         struct drm_property *property;
2275         struct drm_connector *connector;
2276         int ret = -EINVAL;
2277         int i;
2278
2279         mutex_lock(&dev->mode_config.mutex);
2280
2281         obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2282         if (!obj) {
2283                 goto out;
2284         }
2285         connector = obj_to_connector(obj);
2286
2287         for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2288                 if (connector->property_ids[i] == out_resp->prop_id)
2289                         break;
2290         }
2291
2292         if (i == DRM_CONNECTOR_MAX_PROPERTY) {
2293                 goto out;
2294         }
2295
2296         obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2297         if (!obj) {
2298                 goto out;
2299         }
2300         property = obj_to_property(obj);
2301
2302         if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2303                 goto out;
2304
2305         if (property->flags & DRM_MODE_PROP_RANGE) {
2306                 if (out_resp->value < property->values[0])
2307                         goto out;
2308
2309                 if (out_resp->value > property->values[1])
2310                         goto out;
2311         } else {
2312                 int found = 0;
2313                 for (i = 0; i < property->num_values; i++) {
2314                         if (property->values[i] == out_resp->value) {
2315                                 found = 1;
2316                                 break;
2317                         }
2318                 }
2319                 if (!found) {
2320                         goto out;
2321                 }
2322         }
2323
2324         /* Do DPMS ourselves */
2325         if (property == connector->dev->mode_config.dpms_property) {
2326                 if (connector->funcs->dpms)
2327                         (*connector->funcs->dpms)(connector, (int) out_resp->value);
2328                 ret = 0;
2329         } else if (connector->funcs->set_property)
2330                 ret = connector->funcs->set_property(connector, property, out_resp->value);
2331
2332         /* store the property value if succesful */
2333         if (!ret)
2334                 drm_connector_property_set_value(connector, property, out_resp->value);
2335 out:
2336         mutex_unlock(&dev->mode_config.mutex);
2337         return ret;
2338 }
2339
2340 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2341                                       struct drm_encoder *encoder)
2342 {
2343         int i;
2344
2345         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2346                 if (connector->encoder_ids[i] == 0) {
2347                         connector->encoder_ids[i] = encoder->base.id;
2348                         return 0;
2349                 }
2350         }
2351         return -ENOMEM;
2352 }
2353 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
2354
2355 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
2356                                     struct drm_encoder *encoder)
2357 {
2358         int i;
2359         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2360                 if (connector->encoder_ids[i] == encoder->base.id) {
2361                         connector->encoder_ids[i] = 0;
2362                         if (connector->encoder == encoder)
2363                                 connector->encoder = NULL;
2364                         break;
2365                 }
2366         }
2367 }
2368 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
2369
2370 bool drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
2371                                   int gamma_size)
2372 {
2373         crtc->gamma_size = gamma_size;
2374
2375         crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
2376         if (!crtc->gamma_store) {
2377                 crtc->gamma_size = 0;
2378                 return false;
2379         }
2380
2381         return true;
2382 }
2383 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
2384
2385 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
2386                              void *data, struct drm_file *file_priv)
2387 {
2388         struct drm_mode_crtc_lut *crtc_lut = data;
2389         struct drm_mode_object *obj;
2390         struct drm_crtc *crtc;
2391         void *r_base, *g_base, *b_base;
2392         int size;
2393         int ret = 0;
2394
2395         mutex_lock(&dev->mode_config.mutex);
2396         obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2397         if (!obj) {
2398                 ret = -EINVAL;
2399                 goto out;
2400         }
2401         crtc = obj_to_crtc(obj);
2402
2403         /* memcpy into gamma store */
2404         if (crtc_lut->gamma_size != crtc->gamma_size) {
2405                 ret = -EINVAL;
2406                 goto out;
2407         }
2408
2409         size = crtc_lut->gamma_size * (sizeof(uint16_t));
2410         r_base = crtc->gamma_store;
2411         if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
2412                 ret = -EFAULT;
2413                 goto out;
2414         }
2415
2416         g_base = r_base + size;
2417         if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
2418                 ret = -EFAULT;
2419                 goto out;
2420         }
2421
2422         b_base = g_base + size;
2423         if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
2424                 ret = -EFAULT;
2425                 goto out;
2426         }
2427
2428         crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
2429
2430 out:
2431         mutex_unlock(&dev->mode_config.mutex);
2432         return ret;
2433
2434 }
2435
2436 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
2437                              void *data, struct drm_file *file_priv)
2438 {
2439         struct drm_mode_crtc_lut *crtc_lut = data;
2440         struct drm_mode_object *obj;
2441         struct drm_crtc *crtc;
2442         void *r_base, *g_base, *b_base;
2443         int size;
2444         int ret = 0;
2445
2446         mutex_lock(&dev->mode_config.mutex);
2447         obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2448         if (!obj) {
2449                 ret = -EINVAL;
2450                 goto out;
2451         }
2452         crtc = obj_to_crtc(obj);
2453
2454         /* memcpy into gamma store */
2455         if (crtc_lut->gamma_size != crtc->gamma_size) {
2456                 ret = -EINVAL;
2457                 goto out;
2458         }
2459
2460         size = crtc_lut->gamma_size * (sizeof(uint16_t));
2461         r_base = crtc->gamma_store;
2462         if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
2463                 ret = -EFAULT;
2464                 goto out;
2465         }
2466
2467         g_base = r_base + size;
2468         if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
2469                 ret = -EFAULT;
2470                 goto out;
2471         }
2472
2473         b_base = g_base + size;
2474         if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
2475                 ret = -EFAULT;
2476                 goto out;
2477         }
2478 out:
2479         mutex_unlock(&dev->mode_config.mutex);
2480         return ret;
2481 }
2482
2483 int drm_mode_page_flip_ioctl(struct drm_device *dev,
2484                              void *data, struct drm_file *file_priv)
2485 {
2486         struct drm_mode_crtc_page_flip *page_flip = data;
2487         struct drm_mode_object *obj;
2488         struct drm_crtc *crtc;
2489         struct drm_framebuffer *fb;
2490         struct drm_pending_vblank_event *e = NULL;
2491         unsigned long flags;
2492         int ret = -EINVAL;
2493
2494         if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
2495             page_flip->reserved != 0)
2496                 return -EINVAL;
2497
2498         mutex_lock(&dev->mode_config.mutex);
2499         obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
2500         if (!obj)
2501                 goto out;
2502         crtc = obj_to_crtc(obj);
2503
2504         if (crtc->funcs->page_flip == NULL)
2505                 goto out;
2506
2507         obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
2508         if (!obj)
2509                 goto out;
2510         fb = obj_to_fb(obj);
2511
2512         if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
2513                 ret = -ENOMEM;
2514                 spin_lock_irqsave(&dev->event_lock, flags);
2515                 if (file_priv->event_space < sizeof e->event) {
2516                         spin_unlock_irqrestore(&dev->event_lock, flags);
2517                         goto out;
2518                 }
2519                 file_priv->event_space -= sizeof e->event;
2520                 spin_unlock_irqrestore(&dev->event_lock, flags);
2521
2522                 e = kzalloc(sizeof *e, GFP_KERNEL);
2523                 if (e == NULL) {
2524                         spin_lock_irqsave(&dev->event_lock, flags);
2525                         file_priv->event_space += sizeof e->event;
2526                         spin_unlock_irqrestore(&dev->event_lock, flags);
2527                         goto out;
2528                 }
2529
2530                 e->event.base.type = DRM_EVENT_VBLANK;
2531                 e->event.base.length = sizeof e->event;
2532                 e->event.user_data = page_flip->user_data;
2533                 e->base.event = &e->event.base;
2534                 e->base.file_priv = file_priv;
2535                 e->base.destroy =
2536                         (void (*) (struct drm_pending_event *)) kfree;
2537         }
2538
2539         ret = crtc->funcs->page_flip(crtc, fb, e);
2540         if (ret) {
2541                 spin_lock_irqsave(&dev->event_lock, flags);
2542                 file_priv->event_space += sizeof e->event;
2543                 spin_unlock_irqrestore(&dev->event_lock, flags);
2544                 kfree(e);
2545         }
2546
2547 out:
2548         mutex_unlock(&dev->mode_config.mutex);
2549         return ret;
2550 }