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