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