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