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