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