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