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