staging/wlan-ng: Fix 'Branch condition evaluates to a garbage value' in p80211netdev.c
[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/drmP.h>
36 #include <drm/drm_crtc.h>
37 #include <drm/drm_edid.h>
38 #include <drm/drm_fourcc.h>
39
40 /* Avoid boilerplate.  I'm tired of typing. */
41 #define DRM_ENUM_NAME_FN(fnname, list)                          \
42         char *fnname(int val)                                   \
43         {                                                       \
44                 int i;                                          \
45                 for (i = 0; i < ARRAY_SIZE(list); i++) {        \
46                         if (list[i].type == val)                \
47                                 return list[i].name;            \
48                 }                                               \
49                 return "(unknown)";                             \
50         }
51
52 /*
53  * Global properties
54  */
55 static struct drm_prop_enum_list drm_dpms_enum_list[] =
56 {       { DRM_MODE_DPMS_ON, "On" },
57         { DRM_MODE_DPMS_STANDBY, "Standby" },
58         { DRM_MODE_DPMS_SUSPEND, "Suspend" },
59         { DRM_MODE_DPMS_OFF, "Off" }
60 };
61
62 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
63
64 /*
65  * Optional properties
66  */
67 static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
68 {
69         { DRM_MODE_SCALE_NONE, "None" },
70         { DRM_MODE_SCALE_FULLSCREEN, "Full" },
71         { DRM_MODE_SCALE_CENTER, "Center" },
72         { DRM_MODE_SCALE_ASPECT, "Full aspect" },
73 };
74
75 static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
76 {
77         { DRM_MODE_DITHERING_OFF, "Off" },
78         { DRM_MODE_DITHERING_ON, "On" },
79         { DRM_MODE_DITHERING_AUTO, "Automatic" },
80 };
81
82 /*
83  * Non-global properties, but "required" for certain connectors.
84  */
85 static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
86 {
87         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
88         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
89         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
90 };
91
92 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
93
94 static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
95 {
96         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
97         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
98         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
99 };
100
101 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
102                  drm_dvi_i_subconnector_enum_list)
103
104 static struct drm_prop_enum_list drm_tv_select_enum_list[] =
105 {
106         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
107         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
108         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
109         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
110         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
111 };
112
113 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
114
115 static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
116 {
117         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
118         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
119         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
120         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
121         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
122 };
123
124 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
125                  drm_tv_subconnector_enum_list)
126
127 static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
128         { DRM_MODE_DIRTY_OFF,      "Off"      },
129         { DRM_MODE_DIRTY_ON,       "On"       },
130         { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
131 };
132
133 DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
134                  drm_dirty_info_enum_list)
135
136 struct drm_conn_prop_enum_list {
137         int type;
138         char *name;
139         int count;
140 };
141
142 /*
143  * Connector and encoder types.
144  */
145 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
146 {       { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
147         { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
148         { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
149         { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
150         { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
151         { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
152         { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
153         { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
154         { DRM_MODE_CONNECTOR_Component, "Component", 0 },
155         { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
156         { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
157         { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
158         { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
159         { DRM_MODE_CONNECTOR_TV, "TV", 0 },
160         { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
161         { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
162 };
163
164 static struct drm_prop_enum_list drm_encoder_enum_list[] =
165 {       { DRM_MODE_ENCODER_NONE, "None" },
166         { DRM_MODE_ENCODER_DAC, "DAC" },
167         { DRM_MODE_ENCODER_TMDS, "TMDS" },
168         { DRM_MODE_ENCODER_LVDS, "LVDS" },
169         { DRM_MODE_ENCODER_TVDAC, "TV" },
170         { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
171 };
172
173 char *drm_get_encoder_name(struct drm_encoder *encoder)
174 {
175         static char buf[32];
176
177         snprintf(buf, 32, "%s-%d",
178                  drm_encoder_enum_list[encoder->encoder_type].name,
179                  encoder->base.id);
180         return buf;
181 }
182 EXPORT_SYMBOL(drm_get_encoder_name);
183
184 char *drm_get_connector_name(struct drm_connector *connector)
185 {
186         static char buf[32];
187
188         snprintf(buf, 32, "%s-%d",
189                  drm_connector_enum_list[connector->connector_type].name,
190                  connector->connector_type_id);
191         return buf;
192 }
193 EXPORT_SYMBOL(drm_get_connector_name);
194
195 char *drm_get_connector_status_name(enum drm_connector_status status)
196 {
197         if (status == connector_status_connected)
198                 return "connected";
199         else if (status == connector_status_disconnected)
200                 return "disconnected";
201         else
202                 return "unknown";
203 }
204
205 /**
206  * drm_mode_object_get - allocate a new identifier
207  * @dev: DRM device
208  * @ptr: object pointer, used to generate unique ID
209  * @type: object type
210  *
211  * LOCKING:
212  *
213  * Create a unique identifier based on @ptr in @dev's identifier space.  Used
214  * for tracking modes, CRTCs and connectors.
215  *
216  * RETURNS:
217  * New unique (relative to other objects in @dev) integer identifier for the
218  * object.
219  */
220 static int drm_mode_object_get(struct drm_device *dev,
221                                struct drm_mode_object *obj, uint32_t obj_type)
222 {
223         int new_id = 0;
224         int ret;
225
226 again:
227         if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
228                 DRM_ERROR("Ran out memory getting a mode number\n");
229                 return -ENOMEM;
230         }
231
232         mutex_lock(&dev->mode_config.idr_mutex);
233         ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
234         mutex_unlock(&dev->mode_config.idr_mutex);
235         if (ret == -EAGAIN)
236                 goto again;
237         else if (ret)
238                 return ret;
239
240         obj->id = new_id;
241         obj->type = obj_type;
242         return 0;
243 }
244
245 /**
246  * drm_mode_object_put - free an identifer
247  * @dev: DRM device
248  * @id: ID to free
249  *
250  * LOCKING:
251  * Caller must hold DRM mode_config lock.
252  *
253  * Free @id from @dev's unique identifier pool.
254  */
255 static void drm_mode_object_put(struct drm_device *dev,
256                                 struct drm_mode_object *object)
257 {
258         mutex_lock(&dev->mode_config.idr_mutex);
259         idr_remove(&dev->mode_config.crtc_idr, object->id);
260         mutex_unlock(&dev->mode_config.idr_mutex);
261 }
262
263 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
264                 uint32_t id, uint32_t type)
265 {
266         struct drm_mode_object *obj = NULL;
267
268         mutex_lock(&dev->mode_config.idr_mutex);
269         obj = idr_find(&dev->mode_config.crtc_idr, id);
270         if (!obj || (obj->type != type) || (obj->id != id))
271                 obj = NULL;
272         mutex_unlock(&dev->mode_config.idr_mutex);
273
274         return obj;
275 }
276 EXPORT_SYMBOL(drm_mode_object_find);
277
278 /**
279  * drm_framebuffer_init - initialize a framebuffer
280  * @dev: DRM device
281  *
282  * LOCKING:
283  * Caller must hold mode config lock.
284  *
285  * Allocates an ID for the framebuffer's parent mode object, sets its mode
286  * functions & device file and adds it to the master fd list.
287  *
288  * RETURNS:
289  * Zero on success, error code on failure.
290  */
291 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
292                          const struct drm_framebuffer_funcs *funcs)
293 {
294         int ret;
295
296         kref_init(&fb->refcount);
297
298         ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
299         if (ret)
300                 return ret;
301
302         fb->dev = dev;
303         fb->funcs = funcs;
304         dev->mode_config.num_fb++;
305         list_add(&fb->head, &dev->mode_config.fb_list);
306
307         return 0;
308 }
309 EXPORT_SYMBOL(drm_framebuffer_init);
310
311 static void drm_framebuffer_free(struct kref *kref)
312 {
313         struct drm_framebuffer *fb =
314                         container_of(kref, struct drm_framebuffer, refcount);
315         fb->funcs->destroy(fb);
316 }
317
318 /**
319  * drm_framebuffer_unreference - unref a framebuffer
320  *
321  * LOCKING:
322  * Caller must hold mode config lock.
323  */
324 void drm_framebuffer_unreference(struct drm_framebuffer *fb)
325 {
326         struct drm_device *dev = fb->dev;
327         DRM_DEBUG("FB ID: %d\n", fb->base.id);
328         WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
329         kref_put(&fb->refcount, drm_framebuffer_free);
330 }
331 EXPORT_SYMBOL(drm_framebuffer_unreference);
332
333 /**
334  * drm_framebuffer_reference - incr the fb refcnt
335  */
336 void drm_framebuffer_reference(struct drm_framebuffer *fb)
337 {
338         DRM_DEBUG("FB ID: %d\n", fb->base.id);
339         kref_get(&fb->refcount);
340 }
341 EXPORT_SYMBOL(drm_framebuffer_reference);
342
343 /**
344  * drm_framebuffer_cleanup - remove a framebuffer object
345  * @fb: framebuffer to remove
346  *
347  * LOCKING:
348  * Caller must hold mode config lock.
349  *
350  * Scans all the CRTCs in @dev's mode_config.  If they're using @fb, removes
351  * it, setting it to NULL.
352  */
353 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
354 {
355         struct drm_device *dev = fb->dev;
356         /*
357          * This could be moved to drm_framebuffer_remove(), but for
358          * debugging is nice to keep around the list of fb's that are
359          * no longer associated w/ a drm_file but are not unreferenced
360          * yet.  (i915 and omapdrm have debugfs files which will show
361          * this.)
362          */
363         drm_mode_object_put(dev, &fb->base);
364         list_del(&fb->head);
365         dev->mode_config.num_fb--;
366 }
367 EXPORT_SYMBOL(drm_framebuffer_cleanup);
368
369 /**
370  * drm_framebuffer_remove - remove and unreference a framebuffer object
371  * @fb: framebuffer to remove
372  *
373  * LOCKING:
374  * Caller must hold mode config lock.
375  *
376  * Scans all the CRTCs and planes in @dev's mode_config.  If they're
377  * using @fb, removes it, setting it to NULL.
378  */
379 void drm_framebuffer_remove(struct drm_framebuffer *fb)
380 {
381         struct drm_device *dev = fb->dev;
382         struct drm_crtc *crtc;
383         struct drm_plane *plane;
384         struct drm_mode_set set;
385         int ret;
386
387         /* remove from any CRTC */
388         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
389                 if (crtc->fb == fb) {
390                         /* should turn off the crtc */
391                         memset(&set, 0, sizeof(struct drm_mode_set));
392                         set.crtc = crtc;
393                         set.fb = NULL;
394                         ret = crtc->funcs->set_config(&set);
395                         if (ret)
396                                 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
397                 }
398         }
399
400         list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
401                 if (plane->fb == fb) {
402                         /* should turn off the crtc */
403                         ret = plane->funcs->disable_plane(plane);
404                         if (ret)
405                                 DRM_ERROR("failed to disable plane with busy fb\n");
406                         /* disconnect the plane from the fb and crtc: */
407                         plane->fb = NULL;
408                         plane->crtc = NULL;
409                 }
410         }
411
412         list_del(&fb->filp_head);
413
414         drm_framebuffer_unreference(fb);
415 }
416 EXPORT_SYMBOL(drm_framebuffer_remove);
417
418 /**
419  * drm_crtc_init - Initialise a new CRTC object
420  * @dev: DRM device
421  * @crtc: CRTC object to init
422  * @funcs: callbacks for the new CRTC
423  *
424  * LOCKING:
425  * Takes mode_config lock.
426  *
427  * Inits a new object created as base part of an driver crtc object.
428  *
429  * RETURNS:
430  * Zero on success, error code on failure.
431  */
432 int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
433                    const struct drm_crtc_funcs *funcs)
434 {
435         int ret;
436
437         crtc->dev = dev;
438         crtc->funcs = funcs;
439         crtc->invert_dimensions = false;
440
441         mutex_lock(&dev->mode_config.mutex);
442
443         ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
444         if (ret)
445                 goto out;
446
447         crtc->base.properties = &crtc->properties;
448
449         list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
450         dev->mode_config.num_crtc++;
451
452  out:
453         mutex_unlock(&dev->mode_config.mutex);
454
455         return ret;
456 }
457 EXPORT_SYMBOL(drm_crtc_init);
458
459 /**
460  * drm_crtc_cleanup - Cleans up the core crtc usage.
461  * @crtc: CRTC to cleanup
462  *
463  * LOCKING:
464  * Caller must hold mode config lock.
465  *
466  * Cleanup @crtc. Removes from drm modesetting space
467  * does NOT free object, caller does that.
468  */
469 void drm_crtc_cleanup(struct drm_crtc *crtc)
470 {
471         struct drm_device *dev = crtc->dev;
472
473         kfree(crtc->gamma_store);
474         crtc->gamma_store = NULL;
475
476         drm_mode_object_put(dev, &crtc->base);
477         list_del(&crtc->head);
478         dev->mode_config.num_crtc--;
479 }
480 EXPORT_SYMBOL(drm_crtc_cleanup);
481
482 /**
483  * drm_mode_probed_add - add a mode to a connector's probed mode list
484  * @connector: connector the new mode
485  * @mode: mode data
486  *
487  * LOCKING:
488  * Caller must hold mode config lock.
489  *
490  * Add @mode to @connector's mode list for later use.
491  */
492 void drm_mode_probed_add(struct drm_connector *connector,
493                          struct drm_display_mode *mode)
494 {
495         list_add(&mode->head, &connector->probed_modes);
496 }
497 EXPORT_SYMBOL(drm_mode_probed_add);
498
499 /**
500  * drm_mode_remove - remove and free a mode
501  * @connector: connector list to modify
502  * @mode: mode to remove
503  *
504  * LOCKING:
505  * Caller must hold mode config lock.
506  *
507  * Remove @mode from @connector's mode list, then free it.
508  */
509 void drm_mode_remove(struct drm_connector *connector,
510                      struct drm_display_mode *mode)
511 {
512         list_del(&mode->head);
513         drm_mode_destroy(connector->dev, mode);
514 }
515 EXPORT_SYMBOL(drm_mode_remove);
516
517 /**
518  * drm_connector_init - Init a preallocated connector
519  * @dev: DRM device
520  * @connector: the connector to init
521  * @funcs: callbacks for this connector
522  * @name: user visible name of the connector
523  *
524  * LOCKING:
525  * Takes mode config lock.
526  *
527  * Initialises a preallocated connector. Connectors should be
528  * subclassed as part of driver connector objects.
529  *
530  * RETURNS:
531  * Zero on success, error code on failure.
532  */
533 int drm_connector_init(struct drm_device *dev,
534                        struct drm_connector *connector,
535                        const struct drm_connector_funcs *funcs,
536                        int connector_type)
537 {
538         int ret;
539
540         mutex_lock(&dev->mode_config.mutex);
541
542         ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
543         if (ret)
544                 goto out;
545
546         connector->base.properties = &connector->properties;
547         connector->dev = dev;
548         connector->funcs = funcs;
549         connector->connector_type = connector_type;
550         connector->connector_type_id =
551                 ++drm_connector_enum_list[connector_type].count; /* TODO */
552         INIT_LIST_HEAD(&connector->user_modes);
553         INIT_LIST_HEAD(&connector->probed_modes);
554         INIT_LIST_HEAD(&connector->modes);
555         connector->edid_blob_ptr = NULL;
556         connector->status = connector_status_unknown;
557
558         list_add_tail(&connector->head, &dev->mode_config.connector_list);
559         dev->mode_config.num_connector++;
560
561         if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
562                 drm_object_attach_property(&connector->base,
563                                               dev->mode_config.edid_property,
564                                               0);
565
566         drm_object_attach_property(&connector->base,
567                                       dev->mode_config.dpms_property, 0);
568
569  out:
570         mutex_unlock(&dev->mode_config.mutex);
571
572         return ret;
573 }
574 EXPORT_SYMBOL(drm_connector_init);
575
576 /**
577  * drm_connector_cleanup - cleans up an initialised connector
578  * @connector: connector to cleanup
579  *
580  * LOCKING:
581  * Takes mode config lock.
582  *
583  * Cleans up the connector but doesn't free the object.
584  */
585 void drm_connector_cleanup(struct drm_connector *connector)
586 {
587         struct drm_device *dev = connector->dev;
588         struct drm_display_mode *mode, *t;
589
590         list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
591                 drm_mode_remove(connector, mode);
592
593         list_for_each_entry_safe(mode, t, &connector->modes, head)
594                 drm_mode_remove(connector, mode);
595
596         list_for_each_entry_safe(mode, t, &connector->user_modes, head)
597                 drm_mode_remove(connector, mode);
598
599         mutex_lock(&dev->mode_config.mutex);
600         drm_mode_object_put(dev, &connector->base);
601         list_del(&connector->head);
602         dev->mode_config.num_connector--;
603         mutex_unlock(&dev->mode_config.mutex);
604 }
605 EXPORT_SYMBOL(drm_connector_cleanup);
606
607 void drm_connector_unplug_all(struct drm_device *dev)
608 {
609         struct drm_connector *connector;
610
611         /* taking the mode config mutex ends up in a clash with sysfs */
612         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
613                 drm_sysfs_connector_remove(connector);
614
615 }
616 EXPORT_SYMBOL(drm_connector_unplug_all);
617
618 int drm_encoder_init(struct drm_device *dev,
619                       struct drm_encoder *encoder,
620                       const struct drm_encoder_funcs *funcs,
621                       int encoder_type)
622 {
623         int ret;
624
625         mutex_lock(&dev->mode_config.mutex);
626
627         ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
628         if (ret)
629                 goto out;
630
631         encoder->dev = dev;
632         encoder->encoder_type = encoder_type;
633         encoder->funcs = funcs;
634
635         list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
636         dev->mode_config.num_encoder++;
637
638  out:
639         mutex_unlock(&dev->mode_config.mutex);
640
641         return ret;
642 }
643 EXPORT_SYMBOL(drm_encoder_init);
644
645 void drm_encoder_cleanup(struct drm_encoder *encoder)
646 {
647         struct drm_device *dev = encoder->dev;
648         mutex_lock(&dev->mode_config.mutex);
649         drm_mode_object_put(dev, &encoder->base);
650         list_del(&encoder->head);
651         dev->mode_config.num_encoder--;
652         mutex_unlock(&dev->mode_config.mutex);
653 }
654 EXPORT_SYMBOL(drm_encoder_cleanup);
655
656 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
657                    unsigned long possible_crtcs,
658                    const struct drm_plane_funcs *funcs,
659                    const uint32_t *formats, uint32_t format_count,
660                    bool priv)
661 {
662         int ret;
663
664         mutex_lock(&dev->mode_config.mutex);
665
666         ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
667         if (ret)
668                 goto out;
669
670         plane->base.properties = &plane->properties;
671         plane->dev = dev;
672         plane->funcs = funcs;
673         plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
674                                       GFP_KERNEL);
675         if (!plane->format_types) {
676                 DRM_DEBUG_KMS("out of memory when allocating plane\n");
677                 drm_mode_object_put(dev, &plane->base);
678                 ret = -ENOMEM;
679                 goto out;
680         }
681
682         memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
683         plane->format_count = format_count;
684         plane->possible_crtcs = possible_crtcs;
685
686         /* private planes are not exposed to userspace, but depending on
687          * display hardware, might be convenient to allow sharing programming
688          * for the scanout engine with the crtc implementation.
689          */
690         if (!priv) {
691                 list_add_tail(&plane->head, &dev->mode_config.plane_list);
692                 dev->mode_config.num_plane++;
693         } else {
694                 INIT_LIST_HEAD(&plane->head);
695         }
696
697  out:
698         mutex_unlock(&dev->mode_config.mutex);
699
700         return ret;
701 }
702 EXPORT_SYMBOL(drm_plane_init);
703
704 void drm_plane_cleanup(struct drm_plane *plane)
705 {
706         struct drm_device *dev = plane->dev;
707
708         mutex_lock(&dev->mode_config.mutex);
709         kfree(plane->format_types);
710         drm_mode_object_put(dev, &plane->base);
711         /* if not added to a list, it must be a private plane */
712         if (!list_empty(&plane->head)) {
713                 list_del(&plane->head);
714                 dev->mode_config.num_plane--;
715         }
716         mutex_unlock(&dev->mode_config.mutex);
717 }
718 EXPORT_SYMBOL(drm_plane_cleanup);
719
720 /**
721  * drm_mode_create - create a new display mode
722  * @dev: DRM device
723  *
724  * LOCKING:
725  * Caller must hold DRM mode_config lock.
726  *
727  * Create a new drm_display_mode, give it an ID, and return it.
728  *
729  * RETURNS:
730  * Pointer to new mode on success, NULL on error.
731  */
732 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
733 {
734         struct drm_display_mode *nmode;
735
736         nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
737         if (!nmode)
738                 return NULL;
739
740         if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
741                 kfree(nmode);
742                 return NULL;
743         }
744
745         return nmode;
746 }
747 EXPORT_SYMBOL(drm_mode_create);
748
749 /**
750  * drm_mode_destroy - remove a mode
751  * @dev: DRM device
752  * @mode: mode to remove
753  *
754  * LOCKING:
755  * Caller must hold mode config lock.
756  *
757  * Free @mode's unique identifier, then free it.
758  */
759 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
760 {
761         if (!mode)
762                 return;
763
764         drm_mode_object_put(dev, &mode->base);
765
766         kfree(mode);
767 }
768 EXPORT_SYMBOL(drm_mode_destroy);
769
770 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
771 {
772         struct drm_property *edid;
773         struct drm_property *dpms;
774
775         /*
776          * Standard properties (apply to all connectors)
777          */
778         edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
779                                    DRM_MODE_PROP_IMMUTABLE,
780                                    "EDID", 0);
781         dev->mode_config.edid_property = edid;
782
783         dpms = drm_property_create_enum(dev, 0,
784                                    "DPMS", drm_dpms_enum_list,
785                                    ARRAY_SIZE(drm_dpms_enum_list));
786         dev->mode_config.dpms_property = dpms;
787
788         return 0;
789 }
790
791 /**
792  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
793  * @dev: DRM device
794  *
795  * Called by a driver the first time a DVI-I connector is made.
796  */
797 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
798 {
799         struct drm_property *dvi_i_selector;
800         struct drm_property *dvi_i_subconnector;
801
802         if (dev->mode_config.dvi_i_select_subconnector_property)
803                 return 0;
804
805         dvi_i_selector =
806                 drm_property_create_enum(dev, 0,
807                                     "select subconnector",
808                                     drm_dvi_i_select_enum_list,
809                                     ARRAY_SIZE(drm_dvi_i_select_enum_list));
810         dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
811
812         dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
813                                     "subconnector",
814                                     drm_dvi_i_subconnector_enum_list,
815                                     ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
816         dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
817
818         return 0;
819 }
820 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
821
822 /**
823  * drm_create_tv_properties - create TV specific connector properties
824  * @dev: DRM device
825  * @num_modes: number of different TV formats (modes) supported
826  * @modes: array of pointers to strings containing name of each format
827  *
828  * Called by a driver's TV initialization routine, this function creates
829  * the TV specific connector properties for a given device.  Caller is
830  * responsible for allocating a list of format names and passing them to
831  * this routine.
832  */
833 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
834                                   char *modes[])
835 {
836         struct drm_property *tv_selector;
837         struct drm_property *tv_subconnector;
838         int i;
839
840         if (dev->mode_config.tv_select_subconnector_property)
841                 return 0;
842
843         /*
844          * Basic connector properties
845          */
846         tv_selector = drm_property_create_enum(dev, 0,
847                                           "select subconnector",
848                                           drm_tv_select_enum_list,
849                                           ARRAY_SIZE(drm_tv_select_enum_list));
850         dev->mode_config.tv_select_subconnector_property = tv_selector;
851
852         tv_subconnector =
853                 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
854                                     "subconnector",
855                                     drm_tv_subconnector_enum_list,
856                                     ARRAY_SIZE(drm_tv_subconnector_enum_list));
857         dev->mode_config.tv_subconnector_property = tv_subconnector;
858
859         /*
860          * Other, TV specific properties: margins & TV modes.
861          */
862         dev->mode_config.tv_left_margin_property =
863                 drm_property_create_range(dev, 0, "left margin", 0, 100);
864
865         dev->mode_config.tv_right_margin_property =
866                 drm_property_create_range(dev, 0, "right margin", 0, 100);
867
868         dev->mode_config.tv_top_margin_property =
869                 drm_property_create_range(dev, 0, "top margin", 0, 100);
870
871         dev->mode_config.tv_bottom_margin_property =
872                 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
873
874         dev->mode_config.tv_mode_property =
875                 drm_property_create(dev, DRM_MODE_PROP_ENUM,
876                                     "mode", num_modes);
877         for (i = 0; i < num_modes; i++)
878                 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
879                                       i, modes[i]);
880
881         dev->mode_config.tv_brightness_property =
882                 drm_property_create_range(dev, 0, "brightness", 0, 100);
883
884         dev->mode_config.tv_contrast_property =
885                 drm_property_create_range(dev, 0, "contrast", 0, 100);
886
887         dev->mode_config.tv_flicker_reduction_property =
888                 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
889
890         dev->mode_config.tv_overscan_property =
891                 drm_property_create_range(dev, 0, "overscan", 0, 100);
892
893         dev->mode_config.tv_saturation_property =
894                 drm_property_create_range(dev, 0, "saturation", 0, 100);
895
896         dev->mode_config.tv_hue_property =
897                 drm_property_create_range(dev, 0, "hue", 0, 100);
898
899         return 0;
900 }
901 EXPORT_SYMBOL(drm_mode_create_tv_properties);
902
903 /**
904  * drm_mode_create_scaling_mode_property - create scaling mode property
905  * @dev: DRM device
906  *
907  * Called by a driver the first time it's needed, must be attached to desired
908  * connectors.
909  */
910 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
911 {
912         struct drm_property *scaling_mode;
913
914         if (dev->mode_config.scaling_mode_property)
915                 return 0;
916
917         scaling_mode =
918                 drm_property_create_enum(dev, 0, "scaling mode",
919                                 drm_scaling_mode_enum_list,
920                                     ARRAY_SIZE(drm_scaling_mode_enum_list));
921
922         dev->mode_config.scaling_mode_property = scaling_mode;
923
924         return 0;
925 }
926 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
927
928 /**
929  * drm_mode_create_dithering_property - create dithering property
930  * @dev: DRM device
931  *
932  * Called by a driver the first time it's needed, must be attached to desired
933  * connectors.
934  */
935 int drm_mode_create_dithering_property(struct drm_device *dev)
936 {
937         struct drm_property *dithering_mode;
938
939         if (dev->mode_config.dithering_mode_property)
940                 return 0;
941
942         dithering_mode =
943                 drm_property_create_enum(dev, 0, "dithering",
944                                 drm_dithering_mode_enum_list,
945                                     ARRAY_SIZE(drm_dithering_mode_enum_list));
946         dev->mode_config.dithering_mode_property = dithering_mode;
947
948         return 0;
949 }
950 EXPORT_SYMBOL(drm_mode_create_dithering_property);
951
952 /**
953  * drm_mode_create_dirty_property - create dirty property
954  * @dev: DRM device
955  *
956  * Called by a driver the first time it's needed, must be attached to desired
957  * connectors.
958  */
959 int drm_mode_create_dirty_info_property(struct drm_device *dev)
960 {
961         struct drm_property *dirty_info;
962
963         if (dev->mode_config.dirty_info_property)
964                 return 0;
965
966         dirty_info =
967                 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
968                                     "dirty",
969                                     drm_dirty_info_enum_list,
970                                     ARRAY_SIZE(drm_dirty_info_enum_list));
971         dev->mode_config.dirty_info_property = dirty_info;
972
973         return 0;
974 }
975 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
976
977 /**
978  * drm_mode_config_init - initialize DRM mode_configuration structure
979  * @dev: DRM device
980  *
981  * LOCKING:
982  * None, should happen single threaded at init time.
983  *
984  * Initialize @dev's mode_config structure, used for tracking the graphics
985  * configuration of @dev.
986  */
987 void drm_mode_config_init(struct drm_device *dev)
988 {
989         mutex_init(&dev->mode_config.mutex);
990         mutex_init(&dev->mode_config.idr_mutex);
991         INIT_LIST_HEAD(&dev->mode_config.fb_list);
992         INIT_LIST_HEAD(&dev->mode_config.crtc_list);
993         INIT_LIST_HEAD(&dev->mode_config.connector_list);
994         INIT_LIST_HEAD(&dev->mode_config.encoder_list);
995         INIT_LIST_HEAD(&dev->mode_config.property_list);
996         INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
997         INIT_LIST_HEAD(&dev->mode_config.plane_list);
998         idr_init(&dev->mode_config.crtc_idr);
999
1000         mutex_lock(&dev->mode_config.mutex);
1001         drm_mode_create_standard_connector_properties(dev);
1002         mutex_unlock(&dev->mode_config.mutex);
1003
1004         /* Just to be sure */
1005         dev->mode_config.num_fb = 0;
1006         dev->mode_config.num_connector = 0;
1007         dev->mode_config.num_crtc = 0;
1008         dev->mode_config.num_encoder = 0;
1009 }
1010 EXPORT_SYMBOL(drm_mode_config_init);
1011
1012 int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1013 {
1014         uint32_t total_objects = 0;
1015
1016         total_objects += dev->mode_config.num_crtc;
1017         total_objects += dev->mode_config.num_connector;
1018         total_objects += dev->mode_config.num_encoder;
1019
1020         group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1021         if (!group->id_list)
1022                 return -ENOMEM;
1023
1024         group->num_crtcs = 0;
1025         group->num_connectors = 0;
1026         group->num_encoders = 0;
1027         return 0;
1028 }
1029
1030 int drm_mode_group_init_legacy_group(struct drm_device *dev,
1031                                      struct drm_mode_group *group)
1032 {
1033         struct drm_crtc *crtc;
1034         struct drm_encoder *encoder;
1035         struct drm_connector *connector;
1036         int ret;
1037
1038         if ((ret = drm_mode_group_init(dev, group)))
1039                 return ret;
1040
1041         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1042                 group->id_list[group->num_crtcs++] = crtc->base.id;
1043
1044         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1045                 group->id_list[group->num_crtcs + group->num_encoders++] =
1046                 encoder->base.id;
1047
1048         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1049                 group->id_list[group->num_crtcs + group->num_encoders +
1050                                group->num_connectors++] = connector->base.id;
1051
1052         return 0;
1053 }
1054 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1055
1056 /**
1057  * drm_mode_config_cleanup - free up DRM mode_config info
1058  * @dev: DRM device
1059  *
1060  * LOCKING:
1061  * Caller must hold mode config lock.
1062  *
1063  * Free up all the connectors and CRTCs associated with this DRM device, then
1064  * free up the framebuffers and associated buffer objects.
1065  *
1066  * FIXME: cleanup any dangling user buffer objects too
1067  */
1068 void drm_mode_config_cleanup(struct drm_device *dev)
1069 {
1070         struct drm_connector *connector, *ot;
1071         struct drm_crtc *crtc, *ct;
1072         struct drm_encoder *encoder, *enct;
1073         struct drm_framebuffer *fb, *fbt;
1074         struct drm_property *property, *pt;
1075         struct drm_plane *plane, *plt;
1076
1077         list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1078                                  head) {
1079                 encoder->funcs->destroy(encoder);
1080         }
1081
1082         list_for_each_entry_safe(connector, ot,
1083                                  &dev->mode_config.connector_list, head) {
1084                 connector->funcs->destroy(connector);
1085         }
1086
1087         list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1088                                  head) {
1089                 drm_property_destroy(dev, property);
1090         }
1091
1092         list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1093                 drm_framebuffer_remove(fb);
1094         }
1095
1096         list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1097                                  head) {
1098                 plane->funcs->destroy(plane);
1099         }
1100
1101         list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1102                 crtc->funcs->destroy(crtc);
1103         }
1104
1105         idr_remove_all(&dev->mode_config.crtc_idr);
1106         idr_destroy(&dev->mode_config.crtc_idr);
1107 }
1108 EXPORT_SYMBOL(drm_mode_config_cleanup);
1109
1110 /**
1111  * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1112  * @out: drm_mode_modeinfo struct to return to the user
1113  * @in: drm_display_mode to use
1114  *
1115  * LOCKING:
1116  * None.
1117  *
1118  * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1119  * the user.
1120  */
1121 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1122                                       const struct drm_display_mode *in)
1123 {
1124         WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1125              in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1126              in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1127              in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1128              in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1129              "timing values too large for mode info\n");
1130
1131         out->clock = in->clock;
1132         out->hdisplay = in->hdisplay;
1133         out->hsync_start = in->hsync_start;
1134         out->hsync_end = in->hsync_end;
1135         out->htotal = in->htotal;
1136         out->hskew = in->hskew;
1137         out->vdisplay = in->vdisplay;
1138         out->vsync_start = in->vsync_start;
1139         out->vsync_end = in->vsync_end;
1140         out->vtotal = in->vtotal;
1141         out->vscan = in->vscan;
1142         out->vrefresh = in->vrefresh;
1143         out->flags = in->flags;
1144         out->type = in->type;
1145         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1146         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1147 }
1148
1149 /**
1150  * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1151  * @out: drm_display_mode to return to the user
1152  * @in: drm_mode_modeinfo to use
1153  *
1154  * LOCKING:
1155  * None.
1156  *
1157  * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1158  * the caller.
1159  *
1160  * RETURNS:
1161  * Zero on success, errno on failure.
1162  */
1163 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1164                                   const struct drm_mode_modeinfo *in)
1165 {
1166         if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1167                 return -ERANGE;
1168
1169         out->clock = in->clock;
1170         out->hdisplay = in->hdisplay;
1171         out->hsync_start = in->hsync_start;
1172         out->hsync_end = in->hsync_end;
1173         out->htotal = in->htotal;
1174         out->hskew = in->hskew;
1175         out->vdisplay = in->vdisplay;
1176         out->vsync_start = in->vsync_start;
1177         out->vsync_end = in->vsync_end;
1178         out->vtotal = in->vtotal;
1179         out->vscan = in->vscan;
1180         out->vrefresh = in->vrefresh;
1181         out->flags = in->flags;
1182         out->type = in->type;
1183         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1184         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1185
1186         return 0;
1187 }
1188
1189 /**
1190  * drm_mode_getresources - get graphics configuration
1191  * @inode: inode from the ioctl
1192  * @filp: file * from the ioctl
1193  * @cmd: cmd from ioctl
1194  * @arg: arg from ioctl
1195  *
1196  * LOCKING:
1197  * Takes mode config lock.
1198  *
1199  * Construct a set of configuration description structures and return
1200  * them to the user, including CRTC, connector and framebuffer configuration.
1201  *
1202  * Called by the user via ioctl.
1203  *
1204  * RETURNS:
1205  * Zero on success, errno on failure.
1206  */
1207 int drm_mode_getresources(struct drm_device *dev, void *data,
1208                           struct drm_file *file_priv)
1209 {
1210         struct drm_mode_card_res *card_res = data;
1211         struct list_head *lh;
1212         struct drm_framebuffer *fb;
1213         struct drm_connector *connector;
1214         struct drm_crtc *crtc;
1215         struct drm_encoder *encoder;
1216         int ret = 0;
1217         int connector_count = 0;
1218         int crtc_count = 0;
1219         int fb_count = 0;
1220         int encoder_count = 0;
1221         int copied = 0, i;
1222         uint32_t __user *fb_id;
1223         uint32_t __user *crtc_id;
1224         uint32_t __user *connector_id;
1225         uint32_t __user *encoder_id;
1226         struct drm_mode_group *mode_group;
1227
1228         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1229                 return -EINVAL;
1230
1231         mutex_lock(&dev->mode_config.mutex);
1232
1233         /*
1234          * For the non-control nodes we need to limit the list of resources
1235          * by IDs in the group list for this node
1236          */
1237         list_for_each(lh, &file_priv->fbs)
1238                 fb_count++;
1239
1240         mode_group = &file_priv->master->minor->mode_group;
1241         if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1242
1243                 list_for_each(lh, &dev->mode_config.crtc_list)
1244                         crtc_count++;
1245
1246                 list_for_each(lh, &dev->mode_config.connector_list)
1247                         connector_count++;
1248
1249                 list_for_each(lh, &dev->mode_config.encoder_list)
1250                         encoder_count++;
1251         } else {
1252
1253                 crtc_count = mode_group->num_crtcs;
1254                 connector_count = mode_group->num_connectors;
1255                 encoder_count = mode_group->num_encoders;
1256         }
1257
1258         card_res->max_height = dev->mode_config.max_height;
1259         card_res->min_height = dev->mode_config.min_height;
1260         card_res->max_width = dev->mode_config.max_width;
1261         card_res->min_width = dev->mode_config.min_width;
1262
1263         /* handle this in 4 parts */
1264         /* FBs */
1265         if (card_res->count_fbs >= fb_count) {
1266                 copied = 0;
1267                 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1268                 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1269                         if (put_user(fb->base.id, fb_id + copied)) {
1270                                 ret = -EFAULT;
1271                                 goto out;
1272                         }
1273                         copied++;
1274                 }
1275         }
1276         card_res->count_fbs = fb_count;
1277
1278         /* CRTCs */
1279         if (card_res->count_crtcs >= crtc_count) {
1280                 copied = 0;
1281                 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1282                 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1283                         list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1284                                             head) {
1285                                 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1286                                 if (put_user(crtc->base.id, crtc_id + copied)) {
1287                                         ret = -EFAULT;
1288                                         goto out;
1289                                 }
1290                                 copied++;
1291                         }
1292                 } else {
1293                         for (i = 0; i < mode_group->num_crtcs; i++) {
1294                                 if (put_user(mode_group->id_list[i],
1295                                              crtc_id + copied)) {
1296                                         ret = -EFAULT;
1297                                         goto out;
1298                                 }
1299                                 copied++;
1300                         }
1301                 }
1302         }
1303         card_res->count_crtcs = crtc_count;
1304
1305         /* Encoders */
1306         if (card_res->count_encoders >= encoder_count) {
1307                 copied = 0;
1308                 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1309                 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1310                         list_for_each_entry(encoder,
1311                                             &dev->mode_config.encoder_list,
1312                                             head) {
1313                                 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1314                                                 drm_get_encoder_name(encoder));
1315                                 if (put_user(encoder->base.id, encoder_id +
1316                                              copied)) {
1317                                         ret = -EFAULT;
1318                                         goto out;
1319                                 }
1320                                 copied++;
1321                         }
1322                 } else {
1323                         for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1324                                 if (put_user(mode_group->id_list[i],
1325                                              encoder_id + copied)) {
1326                                         ret = -EFAULT;
1327                                         goto out;
1328                                 }
1329                                 copied++;
1330                         }
1331
1332                 }
1333         }
1334         card_res->count_encoders = encoder_count;
1335
1336         /* Connectors */
1337         if (card_res->count_connectors >= connector_count) {
1338                 copied = 0;
1339                 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1340                 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1341                         list_for_each_entry(connector,
1342                                             &dev->mode_config.connector_list,
1343                                             head) {
1344                                 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1345                                         connector->base.id,
1346                                         drm_get_connector_name(connector));
1347                                 if (put_user(connector->base.id,
1348                                              connector_id + copied)) {
1349                                         ret = -EFAULT;
1350                                         goto out;
1351                                 }
1352                                 copied++;
1353                         }
1354                 } else {
1355                         int start = mode_group->num_crtcs +
1356                                 mode_group->num_encoders;
1357                         for (i = start; i < start + mode_group->num_connectors; i++) {
1358                                 if (put_user(mode_group->id_list[i],
1359                                              connector_id + copied)) {
1360                                         ret = -EFAULT;
1361                                         goto out;
1362                                 }
1363                                 copied++;
1364                         }
1365                 }
1366         }
1367         card_res->count_connectors = connector_count;
1368
1369         DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1370                   card_res->count_connectors, card_res->count_encoders);
1371
1372 out:
1373         mutex_unlock(&dev->mode_config.mutex);
1374         return ret;
1375 }
1376
1377 /**
1378  * drm_mode_getcrtc - get CRTC configuration
1379  * @inode: inode from the ioctl
1380  * @filp: file * from the ioctl
1381  * @cmd: cmd from ioctl
1382  * @arg: arg from ioctl
1383  *
1384  * LOCKING:
1385  * Takes mode config lock.
1386  *
1387  * Construct a CRTC configuration structure to return to the user.
1388  *
1389  * Called by the user via ioctl.
1390  *
1391  * RETURNS:
1392  * Zero on success, errno on failure.
1393  */
1394 int drm_mode_getcrtc(struct drm_device *dev,
1395                      void *data, struct drm_file *file_priv)
1396 {
1397         struct drm_mode_crtc *crtc_resp = data;
1398         struct drm_crtc *crtc;
1399         struct drm_mode_object *obj;
1400         int ret = 0;
1401
1402         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1403                 return -EINVAL;
1404
1405         mutex_lock(&dev->mode_config.mutex);
1406
1407         obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1408                                    DRM_MODE_OBJECT_CRTC);
1409         if (!obj) {
1410                 ret = -EINVAL;
1411                 goto out;
1412         }
1413         crtc = obj_to_crtc(obj);
1414
1415         crtc_resp->x = crtc->x;
1416         crtc_resp->y = crtc->y;
1417         crtc_resp->gamma_size = crtc->gamma_size;
1418         if (crtc->fb)
1419                 crtc_resp->fb_id = crtc->fb->base.id;
1420         else
1421                 crtc_resp->fb_id = 0;
1422
1423         if (crtc->enabled) {
1424
1425                 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1426                 crtc_resp->mode_valid = 1;
1427
1428         } else {
1429                 crtc_resp->mode_valid = 0;
1430         }
1431
1432 out:
1433         mutex_unlock(&dev->mode_config.mutex);
1434         return ret;
1435 }
1436
1437 /**
1438  * drm_mode_getconnector - get connector configuration
1439  * @inode: inode from the ioctl
1440  * @filp: file * from the ioctl
1441  * @cmd: cmd from ioctl
1442  * @arg: arg from ioctl
1443  *
1444  * LOCKING:
1445  * Takes mode config lock.
1446  *
1447  * Construct a connector configuration structure to return to the user.
1448  *
1449  * Called by the user via ioctl.
1450  *
1451  * RETURNS:
1452  * Zero on success, errno on failure.
1453  */
1454 int drm_mode_getconnector(struct drm_device *dev, void *data,
1455                           struct drm_file *file_priv)
1456 {
1457         struct drm_mode_get_connector *out_resp = data;
1458         struct drm_mode_object *obj;
1459         struct drm_connector *connector;
1460         struct drm_display_mode *mode;
1461         int mode_count = 0;
1462         int props_count = 0;
1463         int encoders_count = 0;
1464         int ret = 0;
1465         int copied = 0;
1466         int i;
1467         struct drm_mode_modeinfo u_mode;
1468         struct drm_mode_modeinfo __user *mode_ptr;
1469         uint32_t __user *prop_ptr;
1470         uint64_t __user *prop_values;
1471         uint32_t __user *encoder_ptr;
1472
1473         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1474                 return -EINVAL;
1475
1476         memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1477
1478         DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1479
1480         mutex_lock(&dev->mode_config.mutex);
1481
1482         obj = drm_mode_object_find(dev, out_resp->connector_id,
1483                                    DRM_MODE_OBJECT_CONNECTOR);
1484         if (!obj) {
1485                 ret = -EINVAL;
1486                 goto out;
1487         }
1488         connector = obj_to_connector(obj);
1489
1490         props_count = connector->properties.count;
1491
1492         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1493                 if (connector->encoder_ids[i] != 0) {
1494                         encoders_count++;
1495                 }
1496         }
1497
1498         if (out_resp->count_modes == 0) {
1499                 connector->funcs->fill_modes(connector,
1500                                              dev->mode_config.max_width,
1501                                              dev->mode_config.max_height);
1502         }
1503
1504         /* delayed so we get modes regardless of pre-fill_modes state */
1505         list_for_each_entry(mode, &connector->modes, head)
1506                 mode_count++;
1507
1508         out_resp->connector_id = connector->base.id;
1509         out_resp->connector_type = connector->connector_type;
1510         out_resp->connector_type_id = connector->connector_type_id;
1511         out_resp->mm_width = connector->display_info.width_mm;
1512         out_resp->mm_height = connector->display_info.height_mm;
1513         out_resp->subpixel = connector->display_info.subpixel_order;
1514         out_resp->connection = connector->status;
1515         if (connector->encoder)
1516                 out_resp->encoder_id = connector->encoder->base.id;
1517         else
1518                 out_resp->encoder_id = 0;
1519
1520         /*
1521          * This ioctl is called twice, once to determine how much space is
1522          * needed, and the 2nd time to fill it.
1523          */
1524         if ((out_resp->count_modes >= mode_count) && mode_count) {
1525                 copied = 0;
1526                 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1527                 list_for_each_entry(mode, &connector->modes, head) {
1528                         drm_crtc_convert_to_umode(&u_mode, mode);
1529                         if (copy_to_user(mode_ptr + copied,
1530                                          &u_mode, sizeof(u_mode))) {
1531                                 ret = -EFAULT;
1532                                 goto out;
1533                         }
1534                         copied++;
1535                 }
1536         }
1537         out_resp->count_modes = mode_count;
1538
1539         if ((out_resp->count_props >= props_count) && props_count) {
1540                 copied = 0;
1541                 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1542                 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1543                 for (i = 0; i < connector->properties.count; i++) {
1544                         if (put_user(connector->properties.ids[i],
1545                                      prop_ptr + copied)) {
1546                                 ret = -EFAULT;
1547                                 goto out;
1548                         }
1549
1550                         if (put_user(connector->properties.values[i],
1551                                      prop_values + copied)) {
1552                                 ret = -EFAULT;
1553                                 goto out;
1554                         }
1555                         copied++;
1556                 }
1557         }
1558         out_resp->count_props = props_count;
1559
1560         if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1561                 copied = 0;
1562                 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1563                 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1564                         if (connector->encoder_ids[i] != 0) {
1565                                 if (put_user(connector->encoder_ids[i],
1566                                              encoder_ptr + copied)) {
1567                                         ret = -EFAULT;
1568                                         goto out;
1569                                 }
1570                                 copied++;
1571                         }
1572                 }
1573         }
1574         out_resp->count_encoders = encoders_count;
1575
1576 out:
1577         mutex_unlock(&dev->mode_config.mutex);
1578         return ret;
1579 }
1580
1581 int drm_mode_getencoder(struct drm_device *dev, void *data,
1582                         struct drm_file *file_priv)
1583 {
1584         struct drm_mode_get_encoder *enc_resp = data;
1585         struct drm_mode_object *obj;
1586         struct drm_encoder *encoder;
1587         int ret = 0;
1588
1589         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1590                 return -EINVAL;
1591
1592         mutex_lock(&dev->mode_config.mutex);
1593         obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1594                                    DRM_MODE_OBJECT_ENCODER);
1595         if (!obj) {
1596                 ret = -EINVAL;
1597                 goto out;
1598         }
1599         encoder = obj_to_encoder(obj);
1600
1601         if (encoder->crtc)
1602                 enc_resp->crtc_id = encoder->crtc->base.id;
1603         else
1604                 enc_resp->crtc_id = 0;
1605         enc_resp->encoder_type = encoder->encoder_type;
1606         enc_resp->encoder_id = encoder->base.id;
1607         enc_resp->possible_crtcs = encoder->possible_crtcs;
1608         enc_resp->possible_clones = encoder->possible_clones;
1609
1610 out:
1611         mutex_unlock(&dev->mode_config.mutex);
1612         return ret;
1613 }
1614
1615 /**
1616  * drm_mode_getplane_res - get plane info
1617  * @dev: DRM device
1618  * @data: ioctl data
1619  * @file_priv: DRM file info
1620  *
1621  * LOCKING:
1622  * Takes mode config lock.
1623  *
1624  * Return an plane count and set of IDs.
1625  */
1626 int drm_mode_getplane_res(struct drm_device *dev, void *data,
1627                             struct drm_file *file_priv)
1628 {
1629         struct drm_mode_get_plane_res *plane_resp = data;
1630         struct drm_mode_config *config;
1631         struct drm_plane *plane;
1632         uint32_t __user *plane_ptr;
1633         int copied = 0, ret = 0;
1634
1635         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1636                 return -EINVAL;
1637
1638         mutex_lock(&dev->mode_config.mutex);
1639         config = &dev->mode_config;
1640
1641         /*
1642          * This ioctl is called twice, once to determine how much space is
1643          * needed, and the 2nd time to fill it.
1644          */
1645         if (config->num_plane &&
1646             (plane_resp->count_planes >= config->num_plane)) {
1647                 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
1648
1649                 list_for_each_entry(plane, &config->plane_list, head) {
1650                         if (put_user(plane->base.id, plane_ptr + copied)) {
1651                                 ret = -EFAULT;
1652                                 goto out;
1653                         }
1654                         copied++;
1655                 }
1656         }
1657         plane_resp->count_planes = config->num_plane;
1658
1659 out:
1660         mutex_unlock(&dev->mode_config.mutex);
1661         return ret;
1662 }
1663
1664 /**
1665  * drm_mode_getplane - get plane info
1666  * @dev: DRM device
1667  * @data: ioctl data
1668  * @file_priv: DRM file info
1669  *
1670  * LOCKING:
1671  * Takes mode config lock.
1672  *
1673  * Return plane info, including formats supported, gamma size, any
1674  * current fb, etc.
1675  */
1676 int drm_mode_getplane(struct drm_device *dev, void *data,
1677                         struct drm_file *file_priv)
1678 {
1679         struct drm_mode_get_plane *plane_resp = data;
1680         struct drm_mode_object *obj;
1681         struct drm_plane *plane;
1682         uint32_t __user *format_ptr;
1683         int ret = 0;
1684
1685         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1686                 return -EINVAL;
1687
1688         mutex_lock(&dev->mode_config.mutex);
1689         obj = drm_mode_object_find(dev, plane_resp->plane_id,
1690                                    DRM_MODE_OBJECT_PLANE);
1691         if (!obj) {
1692                 ret = -ENOENT;
1693                 goto out;
1694         }
1695         plane = obj_to_plane(obj);
1696
1697         if (plane->crtc)
1698                 plane_resp->crtc_id = plane->crtc->base.id;
1699         else
1700                 plane_resp->crtc_id = 0;
1701
1702         if (plane->fb)
1703                 plane_resp->fb_id = plane->fb->base.id;
1704         else
1705                 plane_resp->fb_id = 0;
1706
1707         plane_resp->plane_id = plane->base.id;
1708         plane_resp->possible_crtcs = plane->possible_crtcs;
1709         plane_resp->gamma_size = plane->gamma_size;
1710
1711         /*
1712          * This ioctl is called twice, once to determine how much space is
1713          * needed, and the 2nd time to fill it.
1714          */
1715         if (plane->format_count &&
1716             (plane_resp->count_format_types >= plane->format_count)) {
1717                 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
1718                 if (copy_to_user(format_ptr,
1719                                  plane->format_types,
1720                                  sizeof(uint32_t) * plane->format_count)) {
1721                         ret = -EFAULT;
1722                         goto out;
1723                 }
1724         }
1725         plane_resp->count_format_types = plane->format_count;
1726
1727 out:
1728         mutex_unlock(&dev->mode_config.mutex);
1729         return ret;
1730 }
1731
1732 /**
1733  * drm_mode_setplane - set up or tear down an plane
1734  * @dev: DRM device
1735  * @data: ioctl data*
1736  * @file_prive: DRM file info
1737  *
1738  * LOCKING:
1739  * Takes mode config lock.
1740  *
1741  * Set plane info, including placement, fb, scaling, and other factors.
1742  * Or pass a NULL fb to disable.
1743  */
1744 int drm_mode_setplane(struct drm_device *dev, void *data,
1745                         struct drm_file *file_priv)
1746 {
1747         struct drm_mode_set_plane *plane_req = data;
1748         struct drm_mode_object *obj;
1749         struct drm_plane *plane;
1750         struct drm_crtc *crtc;
1751         struct drm_framebuffer *fb;
1752         int ret = 0;
1753         unsigned int fb_width, fb_height;
1754         int i;
1755
1756         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1757                 return -EINVAL;
1758
1759         mutex_lock(&dev->mode_config.mutex);
1760
1761         /*
1762          * First, find the plane, crtc, and fb objects.  If not available,
1763          * we don't bother to call the driver.
1764          */
1765         obj = drm_mode_object_find(dev, plane_req->plane_id,
1766                                    DRM_MODE_OBJECT_PLANE);
1767         if (!obj) {
1768                 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1769                               plane_req->plane_id);
1770                 ret = -ENOENT;
1771                 goto out;
1772         }
1773         plane = obj_to_plane(obj);
1774
1775         /* No fb means shut it down */
1776         if (!plane_req->fb_id) {
1777                 plane->funcs->disable_plane(plane);
1778                 plane->crtc = NULL;
1779                 plane->fb = NULL;
1780                 goto out;
1781         }
1782
1783         obj = drm_mode_object_find(dev, plane_req->crtc_id,
1784                                    DRM_MODE_OBJECT_CRTC);
1785         if (!obj) {
1786                 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1787                               plane_req->crtc_id);
1788                 ret = -ENOENT;
1789                 goto out;
1790         }
1791         crtc = obj_to_crtc(obj);
1792
1793         obj = drm_mode_object_find(dev, plane_req->fb_id,
1794                                    DRM_MODE_OBJECT_FB);
1795         if (!obj) {
1796                 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1797                               plane_req->fb_id);
1798                 ret = -ENOENT;
1799                 goto out;
1800         }
1801         fb = obj_to_fb(obj);
1802
1803         /* Check whether this plane supports the fb pixel format. */
1804         for (i = 0; i < plane->format_count; i++)
1805                 if (fb->pixel_format == plane->format_types[i])
1806                         break;
1807         if (i == plane->format_count) {
1808                 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1809                 ret = -EINVAL;
1810                 goto out;
1811         }
1812
1813         fb_width = fb->width << 16;
1814         fb_height = fb->height << 16;
1815
1816         /* Make sure source coordinates are inside the fb. */
1817         if (plane_req->src_w > fb_width ||
1818             plane_req->src_x > fb_width - plane_req->src_w ||
1819             plane_req->src_h > fb_height ||
1820             plane_req->src_y > fb_height - plane_req->src_h) {
1821                 DRM_DEBUG_KMS("Invalid source coordinates "
1822                               "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1823                               plane_req->src_w >> 16,
1824                               ((plane_req->src_w & 0xffff) * 15625) >> 10,
1825                               plane_req->src_h >> 16,
1826                               ((plane_req->src_h & 0xffff) * 15625) >> 10,
1827                               plane_req->src_x >> 16,
1828                               ((plane_req->src_x & 0xffff) * 15625) >> 10,
1829                               plane_req->src_y >> 16,
1830                               ((plane_req->src_y & 0xffff) * 15625) >> 10);
1831                 ret = -ENOSPC;
1832                 goto out;
1833         }
1834
1835         /* Give drivers some help against integer overflows */
1836         if (plane_req->crtc_w > INT_MAX ||
1837             plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1838             plane_req->crtc_h > INT_MAX ||
1839             plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1840                 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1841                               plane_req->crtc_w, plane_req->crtc_h,
1842                               plane_req->crtc_x, plane_req->crtc_y);
1843                 ret = -ERANGE;
1844                 goto out;
1845         }
1846
1847         ret = plane->funcs->update_plane(plane, crtc, fb,
1848                                          plane_req->crtc_x, plane_req->crtc_y,
1849                                          plane_req->crtc_w, plane_req->crtc_h,
1850                                          plane_req->src_x, plane_req->src_y,
1851                                          plane_req->src_w, plane_req->src_h);
1852         if (!ret) {
1853                 plane->crtc = crtc;
1854                 plane->fb = fb;
1855         }
1856
1857 out:
1858         mutex_unlock(&dev->mode_config.mutex);
1859
1860         return ret;
1861 }
1862
1863 /**
1864  * drm_mode_setcrtc - set CRTC configuration
1865  * @inode: inode from the ioctl
1866  * @filp: file * from the ioctl
1867  * @cmd: cmd from ioctl
1868  * @arg: arg from ioctl
1869  *
1870  * LOCKING:
1871  * Takes mode config lock.
1872  *
1873  * Build a new CRTC configuration based on user request.
1874  *
1875  * Called by the user via ioctl.
1876  *
1877  * RETURNS:
1878  * Zero on success, errno on failure.
1879  */
1880 int drm_mode_setcrtc(struct drm_device *dev, void *data,
1881                      struct drm_file *file_priv)
1882 {
1883         struct drm_mode_config *config = &dev->mode_config;
1884         struct drm_mode_crtc *crtc_req = data;
1885         struct drm_mode_object *obj;
1886         struct drm_crtc *crtc;
1887         struct drm_connector **connector_set = NULL, *connector;
1888         struct drm_framebuffer *fb = NULL;
1889         struct drm_display_mode *mode = NULL;
1890         struct drm_mode_set set;
1891         uint32_t __user *set_connectors_ptr;
1892         int ret;
1893         int i;
1894
1895         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1896                 return -EINVAL;
1897
1898         /* For some reason crtc x/y offsets are signed internally. */
1899         if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1900                 return -ERANGE;
1901
1902         mutex_lock(&dev->mode_config.mutex);
1903         obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1904                                    DRM_MODE_OBJECT_CRTC);
1905         if (!obj) {
1906                 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1907                 ret = -EINVAL;
1908                 goto out;
1909         }
1910         crtc = obj_to_crtc(obj);
1911         DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1912
1913         if (crtc_req->mode_valid) {
1914                 int hdisplay, vdisplay;
1915                 /* If we have a mode we need a framebuffer. */
1916                 /* If we pass -1, set the mode with the currently bound fb */
1917                 if (crtc_req->fb_id == -1) {
1918                         if (!crtc->fb) {
1919                                 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1920                                 ret = -EINVAL;
1921                                 goto out;
1922                         }
1923                         fb = crtc->fb;
1924                 } else {
1925                         obj = drm_mode_object_find(dev, crtc_req->fb_id,
1926                                                    DRM_MODE_OBJECT_FB);
1927                         if (!obj) {
1928                                 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1929                                                 crtc_req->fb_id);
1930                                 ret = -EINVAL;
1931                                 goto out;
1932                         }
1933                         fb = obj_to_fb(obj);
1934                 }
1935
1936                 mode = drm_mode_create(dev);
1937                 if (!mode) {
1938                         ret = -ENOMEM;
1939                         goto out;
1940                 }
1941
1942                 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
1943                 if (ret) {
1944                         DRM_DEBUG_KMS("Invalid mode\n");
1945                         goto out;
1946                 }
1947
1948                 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1949
1950                 hdisplay = mode->hdisplay;
1951                 vdisplay = mode->vdisplay;
1952
1953                 if (crtc->invert_dimensions)
1954                         swap(hdisplay, vdisplay);
1955
1956                 if (hdisplay > fb->width ||
1957                     vdisplay > fb->height ||
1958                     crtc_req->x > fb->width - hdisplay ||
1959                     crtc_req->y > fb->height - vdisplay) {
1960                         DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
1961                                       fb->width, fb->height,
1962                                       hdisplay, vdisplay, crtc_req->x, crtc_req->y,
1963                                       crtc->invert_dimensions ? " (inverted)" : "");
1964                         ret = -ENOSPC;
1965                         goto out;
1966                 }
1967         }
1968
1969         if (crtc_req->count_connectors == 0 && mode) {
1970                 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1971                 ret = -EINVAL;
1972                 goto out;
1973         }
1974
1975         if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
1976                 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1977                           crtc_req->count_connectors);
1978                 ret = -EINVAL;
1979                 goto out;
1980         }
1981
1982         if (crtc_req->count_connectors > 0) {
1983                 u32 out_id;
1984
1985                 /* Avoid unbounded kernel memory allocation */
1986                 if (crtc_req->count_connectors > config->num_connector) {
1987                         ret = -EINVAL;
1988                         goto out;
1989                 }
1990
1991                 connector_set = kmalloc(crtc_req->count_connectors *
1992                                         sizeof(struct drm_connector *),
1993                                         GFP_KERNEL);
1994                 if (!connector_set) {
1995                         ret = -ENOMEM;
1996                         goto out;
1997                 }
1998
1999                 for (i = 0; i < crtc_req->count_connectors; i++) {
2000                         set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2001                         if (get_user(out_id, &set_connectors_ptr[i])) {
2002                                 ret = -EFAULT;
2003                                 goto out;
2004                         }
2005
2006                         obj = drm_mode_object_find(dev, out_id,
2007                                                    DRM_MODE_OBJECT_CONNECTOR);
2008                         if (!obj) {
2009                                 DRM_DEBUG_KMS("Connector id %d unknown\n",
2010                                                 out_id);
2011                                 ret = -EINVAL;
2012                                 goto out;
2013                         }
2014                         connector = obj_to_connector(obj);
2015                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2016                                         connector->base.id,
2017                                         drm_get_connector_name(connector));
2018
2019                         connector_set[i] = connector;
2020                 }
2021         }
2022
2023         set.crtc = crtc;
2024         set.x = crtc_req->x;
2025         set.y = crtc_req->y;
2026         set.mode = mode;
2027         set.connectors = connector_set;
2028         set.num_connectors = crtc_req->count_connectors;
2029         set.fb = fb;
2030         ret = crtc->funcs->set_config(&set);
2031
2032 out:
2033         kfree(connector_set);
2034         drm_mode_destroy(dev, mode);
2035         mutex_unlock(&dev->mode_config.mutex);
2036         return ret;
2037 }
2038
2039 int drm_mode_cursor_ioctl(struct drm_device *dev,
2040                         void *data, struct drm_file *file_priv)
2041 {
2042         struct drm_mode_cursor *req = data;
2043         struct drm_mode_object *obj;
2044         struct drm_crtc *crtc;
2045         int ret = 0;
2046
2047         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2048                 return -EINVAL;
2049
2050         if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2051                 return -EINVAL;
2052
2053         mutex_lock(&dev->mode_config.mutex);
2054         obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
2055         if (!obj) {
2056                 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2057                 ret = -EINVAL;
2058                 goto out;
2059         }
2060         crtc = obj_to_crtc(obj);
2061
2062         if (req->flags & DRM_MODE_CURSOR_BO) {
2063                 if (!crtc->funcs->cursor_set) {
2064                         ret = -ENXIO;
2065                         goto out;
2066                 }
2067                 /* Turns off the cursor if handle is 0 */
2068                 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2069                                               req->width, req->height);
2070         }
2071
2072         if (req->flags & DRM_MODE_CURSOR_MOVE) {
2073                 if (crtc->funcs->cursor_move) {
2074                         ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2075                 } else {
2076                         ret = -EFAULT;
2077                         goto out;
2078                 }
2079         }
2080 out:
2081         mutex_unlock(&dev->mode_config.mutex);
2082         return ret;
2083 }
2084
2085 /* Original addfb only supported RGB formats, so figure out which one */
2086 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2087 {
2088         uint32_t fmt;
2089
2090         switch (bpp) {
2091         case 8:
2092                 fmt = DRM_FORMAT_RGB332;
2093                 break;
2094         case 16:
2095                 if (depth == 15)
2096                         fmt = DRM_FORMAT_XRGB1555;
2097                 else
2098                         fmt = DRM_FORMAT_RGB565;
2099                 break;
2100         case 24:
2101                 fmt = DRM_FORMAT_RGB888;
2102                 break;
2103         case 32:
2104                 if (depth == 24)
2105                         fmt = DRM_FORMAT_XRGB8888;
2106                 else if (depth == 30)
2107                         fmt = DRM_FORMAT_XRGB2101010;
2108                 else
2109                         fmt = DRM_FORMAT_ARGB8888;
2110                 break;
2111         default:
2112                 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2113                 fmt = DRM_FORMAT_XRGB8888;
2114                 break;
2115         }
2116
2117         return fmt;
2118 }
2119 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2120
2121 /**
2122  * drm_mode_addfb - add an FB to the graphics configuration
2123  * @inode: inode from the ioctl
2124  * @filp: file * from the ioctl
2125  * @cmd: cmd from ioctl
2126  * @arg: arg from ioctl
2127  *
2128  * LOCKING:
2129  * Takes mode config lock.
2130  *
2131  * Add a new FB to the specified CRTC, given a user request.
2132  *
2133  * Called by the user via ioctl.
2134  *
2135  * RETURNS:
2136  * Zero on success, errno on failure.
2137  */
2138 int drm_mode_addfb(struct drm_device *dev,
2139                    void *data, struct drm_file *file_priv)
2140 {
2141         struct drm_mode_fb_cmd *or = data;
2142         struct drm_mode_fb_cmd2 r = {};
2143         struct drm_mode_config *config = &dev->mode_config;
2144         struct drm_framebuffer *fb;
2145         int ret = 0;
2146
2147         /* Use new struct with format internally */
2148         r.fb_id = or->fb_id;
2149         r.width = or->width;
2150         r.height = or->height;
2151         r.pitches[0] = or->pitch;
2152         r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2153         r.handles[0] = or->handle;
2154
2155         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2156                 return -EINVAL;
2157
2158         if ((config->min_width > r.width) || (r.width > config->max_width))
2159                 return -EINVAL;
2160
2161         if ((config->min_height > r.height) || (r.height > config->max_height))
2162                 return -EINVAL;
2163
2164         mutex_lock(&dev->mode_config.mutex);
2165
2166         /* TODO check buffer is sufficiently large */
2167         /* TODO setup destructor callback */
2168
2169         fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2170         if (IS_ERR(fb)) {
2171                 DRM_DEBUG_KMS("could not create framebuffer\n");
2172                 ret = PTR_ERR(fb);
2173                 goto out;
2174         }
2175
2176         or->fb_id = fb->base.id;
2177         list_add(&fb->filp_head, &file_priv->fbs);
2178         DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2179
2180 out:
2181         mutex_unlock(&dev->mode_config.mutex);
2182         return ret;
2183 }
2184
2185 static int format_check(const struct drm_mode_fb_cmd2 *r)
2186 {
2187         uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2188
2189         switch (format) {
2190         case DRM_FORMAT_C8:
2191         case DRM_FORMAT_RGB332:
2192         case DRM_FORMAT_BGR233:
2193         case DRM_FORMAT_XRGB4444:
2194         case DRM_FORMAT_XBGR4444:
2195         case DRM_FORMAT_RGBX4444:
2196         case DRM_FORMAT_BGRX4444:
2197         case DRM_FORMAT_ARGB4444:
2198         case DRM_FORMAT_ABGR4444:
2199         case DRM_FORMAT_RGBA4444:
2200         case DRM_FORMAT_BGRA4444:
2201         case DRM_FORMAT_XRGB1555:
2202         case DRM_FORMAT_XBGR1555:
2203         case DRM_FORMAT_RGBX5551:
2204         case DRM_FORMAT_BGRX5551:
2205         case DRM_FORMAT_ARGB1555:
2206         case DRM_FORMAT_ABGR1555:
2207         case DRM_FORMAT_RGBA5551:
2208         case DRM_FORMAT_BGRA5551:
2209         case DRM_FORMAT_RGB565:
2210         case DRM_FORMAT_BGR565:
2211         case DRM_FORMAT_RGB888:
2212         case DRM_FORMAT_BGR888:
2213         case DRM_FORMAT_XRGB8888:
2214         case DRM_FORMAT_XBGR8888:
2215         case DRM_FORMAT_RGBX8888:
2216         case DRM_FORMAT_BGRX8888:
2217         case DRM_FORMAT_ARGB8888:
2218         case DRM_FORMAT_ABGR8888:
2219         case DRM_FORMAT_RGBA8888:
2220         case DRM_FORMAT_BGRA8888:
2221         case DRM_FORMAT_XRGB2101010:
2222         case DRM_FORMAT_XBGR2101010:
2223         case DRM_FORMAT_RGBX1010102:
2224         case DRM_FORMAT_BGRX1010102:
2225         case DRM_FORMAT_ARGB2101010:
2226         case DRM_FORMAT_ABGR2101010:
2227         case DRM_FORMAT_RGBA1010102:
2228         case DRM_FORMAT_BGRA1010102:
2229         case DRM_FORMAT_YUYV:
2230         case DRM_FORMAT_YVYU:
2231         case DRM_FORMAT_UYVY:
2232         case DRM_FORMAT_VYUY:
2233         case DRM_FORMAT_AYUV:
2234         case DRM_FORMAT_NV12:
2235         case DRM_FORMAT_NV21:
2236         case DRM_FORMAT_NV16:
2237         case DRM_FORMAT_NV61:
2238         case DRM_FORMAT_NV24:
2239         case DRM_FORMAT_NV42:
2240         case DRM_FORMAT_YUV410:
2241         case DRM_FORMAT_YVU410:
2242         case DRM_FORMAT_YUV411:
2243         case DRM_FORMAT_YVU411:
2244         case DRM_FORMAT_YUV420:
2245         case DRM_FORMAT_YVU420:
2246         case DRM_FORMAT_YUV422:
2247         case DRM_FORMAT_YVU422:
2248         case DRM_FORMAT_YUV444:
2249         case DRM_FORMAT_YVU444:
2250                 return 0;
2251         default:
2252                 return -EINVAL;
2253         }
2254 }
2255
2256 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
2257 {
2258         int ret, hsub, vsub, num_planes, i;
2259
2260         ret = format_check(r);
2261         if (ret) {
2262                 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
2263                 return ret;
2264         }
2265
2266         hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2267         vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2268         num_planes = drm_format_num_planes(r->pixel_format);
2269
2270         if (r->width == 0 || r->width % hsub) {
2271                 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
2272                 return -EINVAL;
2273         }
2274
2275         if (r->height == 0 || r->height % vsub) {
2276                 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
2277                 return -EINVAL;
2278         }
2279
2280         for (i = 0; i < num_planes; i++) {
2281                 unsigned int width = r->width / (i != 0 ? hsub : 1);
2282                 unsigned int height = r->height / (i != 0 ? vsub : 1);
2283                 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
2284
2285                 if (!r->handles[i]) {
2286                         DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
2287                         return -EINVAL;
2288                 }
2289
2290                 if ((uint64_t) width * cpp > UINT_MAX)
2291                         return -ERANGE;
2292
2293                 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2294                         return -ERANGE;
2295
2296                 if (r->pitches[i] < width * cpp) {
2297                         DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
2298                         return -EINVAL;
2299                 }
2300         }
2301
2302         return 0;
2303 }
2304
2305 /**
2306  * drm_mode_addfb2 - add an FB to the graphics configuration
2307  * @inode: inode from the ioctl
2308  * @filp: file * from the ioctl
2309  * @cmd: cmd from ioctl
2310  * @arg: arg from ioctl
2311  *
2312  * LOCKING:
2313  * Takes mode config lock.
2314  *
2315  * Add a new FB to the specified CRTC, given a user request with format.
2316  *
2317  * Called by the user via ioctl.
2318  *
2319  * RETURNS:
2320  * Zero on success, errno on failure.
2321  */
2322 int drm_mode_addfb2(struct drm_device *dev,
2323                     void *data, struct drm_file *file_priv)
2324 {
2325         struct drm_mode_fb_cmd2 *r = data;
2326         struct drm_mode_config *config = &dev->mode_config;
2327         struct drm_framebuffer *fb;
2328         int ret;
2329
2330         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2331                 return -EINVAL;
2332
2333         if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2334                 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2335                 return -EINVAL;
2336         }
2337
2338         if ((config->min_width > r->width) || (r->width > config->max_width)) {
2339                 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2340                           r->width, config->min_width, config->max_width);
2341                 return -EINVAL;
2342         }
2343         if ((config->min_height > r->height) || (r->height > config->max_height)) {
2344                 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2345                           r->height, config->min_height, config->max_height);
2346                 return -EINVAL;
2347         }
2348
2349         ret = framebuffer_check(r);
2350         if (ret)
2351                 return ret;
2352
2353         mutex_lock(&dev->mode_config.mutex);
2354
2355         fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
2356         if (IS_ERR(fb)) {
2357                 DRM_DEBUG_KMS("could not create framebuffer\n");
2358                 ret = PTR_ERR(fb);
2359                 goto out;
2360         }
2361
2362         r->fb_id = fb->base.id;
2363         list_add(&fb->filp_head, &file_priv->fbs);
2364         DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2365
2366 out:
2367         mutex_unlock(&dev->mode_config.mutex);
2368         return ret;
2369 }
2370
2371 /**
2372  * drm_mode_rmfb - remove an FB from the configuration
2373  * @inode: inode from the ioctl
2374  * @filp: file * from the ioctl
2375  * @cmd: cmd from ioctl
2376  * @arg: arg from ioctl
2377  *
2378  * LOCKING:
2379  * Takes mode config lock.
2380  *
2381  * Remove the FB specified by the user.
2382  *
2383  * Called by the user via ioctl.
2384  *
2385  * RETURNS:
2386  * Zero on success, errno on failure.
2387  */
2388 int drm_mode_rmfb(struct drm_device *dev,
2389                    void *data, struct drm_file *file_priv)
2390 {
2391         struct drm_mode_object *obj;
2392         struct drm_framebuffer *fb = NULL;
2393         struct drm_framebuffer *fbl = NULL;
2394         uint32_t *id = data;
2395         int ret = 0;
2396         int found = 0;
2397
2398         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2399                 return -EINVAL;
2400
2401         mutex_lock(&dev->mode_config.mutex);
2402         obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
2403         /* TODO check that we really get a framebuffer back. */
2404         if (!obj) {
2405                 ret = -EINVAL;
2406                 goto out;
2407         }
2408         fb = obj_to_fb(obj);
2409
2410         list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2411                 if (fb == fbl)
2412                         found = 1;
2413
2414         if (!found) {
2415                 ret = -EINVAL;
2416                 goto out;
2417         }
2418
2419         drm_framebuffer_remove(fb);
2420
2421 out:
2422         mutex_unlock(&dev->mode_config.mutex);
2423         return ret;
2424 }
2425
2426 /**
2427  * drm_mode_getfb - get FB info
2428  * @inode: inode from the ioctl
2429  * @filp: file * from the ioctl
2430  * @cmd: cmd from ioctl
2431  * @arg: arg from ioctl
2432  *
2433  * LOCKING:
2434  * Takes mode config lock.
2435  *
2436  * Lookup the FB given its ID and return info about it.
2437  *
2438  * Called by the user via ioctl.
2439  *
2440  * RETURNS:
2441  * Zero on success, errno on failure.
2442  */
2443 int drm_mode_getfb(struct drm_device *dev,
2444                    void *data, struct drm_file *file_priv)
2445 {
2446         struct drm_mode_fb_cmd *r = data;
2447         struct drm_mode_object *obj;
2448         struct drm_framebuffer *fb;
2449         int ret = 0;
2450
2451         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2452                 return -EINVAL;
2453
2454         mutex_lock(&dev->mode_config.mutex);
2455         obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2456         if (!obj) {
2457                 ret = -EINVAL;
2458                 goto out;
2459         }
2460         fb = obj_to_fb(obj);
2461
2462         r->height = fb->height;
2463         r->width = fb->width;
2464         r->depth = fb->depth;
2465         r->bpp = fb->bits_per_pixel;
2466         r->pitch = fb->pitches[0];
2467         fb->funcs->create_handle(fb, file_priv, &r->handle);
2468
2469 out:
2470         mutex_unlock(&dev->mode_config.mutex);
2471         return ret;
2472 }
2473
2474 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2475                            void *data, struct drm_file *file_priv)
2476 {
2477         struct drm_clip_rect __user *clips_ptr;
2478         struct drm_clip_rect *clips = NULL;
2479         struct drm_mode_fb_dirty_cmd *r = data;
2480         struct drm_mode_object *obj;
2481         struct drm_framebuffer *fb;
2482         unsigned flags;
2483         int num_clips;
2484         int ret;
2485
2486         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2487                 return -EINVAL;
2488
2489         mutex_lock(&dev->mode_config.mutex);
2490         obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2491         if (!obj) {
2492                 ret = -EINVAL;
2493                 goto out_err1;
2494         }
2495         fb = obj_to_fb(obj);
2496
2497         num_clips = r->num_clips;
2498         clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
2499
2500         if (!num_clips != !clips_ptr) {
2501                 ret = -EINVAL;
2502                 goto out_err1;
2503         }
2504
2505         flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2506
2507         /* If userspace annotates copy, clips must come in pairs */
2508         if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2509                 ret = -EINVAL;
2510                 goto out_err1;
2511         }
2512
2513         if (num_clips && clips_ptr) {
2514                 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2515                         ret = -EINVAL;
2516                         goto out_err1;
2517                 }
2518                 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2519                 if (!clips) {
2520                         ret = -ENOMEM;
2521                         goto out_err1;
2522                 }
2523
2524                 ret = copy_from_user(clips, clips_ptr,
2525                                      num_clips * sizeof(*clips));
2526                 if (ret) {
2527                         ret = -EFAULT;
2528                         goto out_err2;
2529                 }
2530         }
2531
2532         if (fb->funcs->dirty) {
2533                 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2534                                        clips, num_clips);
2535         } else {
2536                 ret = -ENOSYS;
2537                 goto out_err2;
2538         }
2539
2540 out_err2:
2541         kfree(clips);
2542 out_err1:
2543         mutex_unlock(&dev->mode_config.mutex);
2544         return ret;
2545 }
2546
2547
2548 /**
2549  * drm_fb_release - remove and free the FBs on this file
2550  * @filp: file * from the ioctl
2551  *
2552  * LOCKING:
2553  * Takes mode config lock.
2554  *
2555  * Destroy all the FBs associated with @filp.
2556  *
2557  * Called by the user via ioctl.
2558  *
2559  * RETURNS:
2560  * Zero on success, errno on failure.
2561  */
2562 void drm_fb_release(struct drm_file *priv)
2563 {
2564         struct drm_device *dev = priv->minor->dev;
2565         struct drm_framebuffer *fb, *tfb;
2566
2567         mutex_lock(&dev->mode_config.mutex);
2568         list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2569                 drm_framebuffer_remove(fb);
2570         }
2571         mutex_unlock(&dev->mode_config.mutex);
2572 }
2573
2574 /**
2575  * drm_mode_attachmode - add a mode to the user mode list
2576  * @dev: DRM device
2577  * @connector: connector to add the mode to
2578  * @mode: mode to add
2579  *
2580  * Add @mode to @connector's user mode list.
2581  */
2582 static void drm_mode_attachmode(struct drm_device *dev,
2583                                 struct drm_connector *connector,
2584                                 struct drm_display_mode *mode)
2585 {
2586         list_add_tail(&mode->head, &connector->user_modes);
2587 }
2588
2589 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
2590                              const struct drm_display_mode *mode)
2591 {
2592         struct drm_connector *connector;
2593         int ret = 0;
2594         struct drm_display_mode *dup_mode, *next;
2595         LIST_HEAD(list);
2596
2597         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2598                 if (!connector->encoder)
2599                         continue;
2600                 if (connector->encoder->crtc == crtc) {
2601                         dup_mode = drm_mode_duplicate(dev, mode);
2602                         if (!dup_mode) {
2603                                 ret = -ENOMEM;
2604                                 goto out;
2605                         }
2606                         list_add_tail(&dup_mode->head, &list);
2607                 }
2608         }
2609
2610         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2611                 if (!connector->encoder)
2612                         continue;
2613                 if (connector->encoder->crtc == crtc)
2614                         list_move_tail(list.next, &connector->user_modes);
2615         }
2616
2617         WARN_ON(!list_empty(&list));
2618
2619  out:
2620         list_for_each_entry_safe(dup_mode, next, &list, head)
2621                 drm_mode_destroy(dev, dup_mode);
2622
2623         return ret;
2624 }
2625 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2626
2627 static int drm_mode_detachmode(struct drm_device *dev,
2628                                struct drm_connector *connector,
2629                                struct drm_display_mode *mode)
2630 {
2631         int found = 0;
2632         int ret = 0;
2633         struct drm_display_mode *match_mode, *t;
2634
2635         list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2636                 if (drm_mode_equal(match_mode, mode)) {
2637                         list_del(&match_mode->head);
2638                         drm_mode_destroy(dev, match_mode);
2639                         found = 1;
2640                         break;
2641                 }
2642         }
2643
2644         if (!found)
2645                 ret = -EINVAL;
2646
2647         return ret;
2648 }
2649
2650 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2651 {
2652         struct drm_connector *connector;
2653
2654         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2655                 drm_mode_detachmode(dev, connector, mode);
2656         }
2657         return 0;
2658 }
2659 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2660
2661 /**
2662  * drm_fb_attachmode - Attach a user mode to an connector
2663  * @inode: inode from the ioctl
2664  * @filp: file * from the ioctl
2665  * @cmd: cmd from ioctl
2666  * @arg: arg from ioctl
2667  *
2668  * This attaches a user specified mode to an connector.
2669  * Called by the user via ioctl.
2670  *
2671  * RETURNS:
2672  * Zero on success, errno on failure.
2673  */
2674 int drm_mode_attachmode_ioctl(struct drm_device *dev,
2675                               void *data, struct drm_file *file_priv)
2676 {
2677         struct drm_mode_mode_cmd *mode_cmd = data;
2678         struct drm_connector *connector;
2679         struct drm_display_mode *mode;
2680         struct drm_mode_object *obj;
2681         struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2682         int ret;
2683
2684         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2685                 return -EINVAL;
2686
2687         mutex_lock(&dev->mode_config.mutex);
2688
2689         obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2690         if (!obj) {
2691                 ret = -EINVAL;
2692                 goto out;
2693         }
2694         connector = obj_to_connector(obj);
2695
2696         mode = drm_mode_create(dev);
2697         if (!mode) {
2698                 ret = -ENOMEM;
2699                 goto out;
2700         }
2701
2702         ret = drm_crtc_convert_umode(mode, umode);
2703         if (ret) {
2704                 DRM_DEBUG_KMS("Invalid mode\n");
2705                 drm_mode_destroy(dev, mode);
2706                 goto out;
2707         }
2708
2709         drm_mode_attachmode(dev, connector, mode);
2710 out:
2711         mutex_unlock(&dev->mode_config.mutex);
2712         return ret;
2713 }
2714
2715
2716 /**
2717  * drm_fb_detachmode - Detach a user specified mode from an connector
2718  * @inode: inode from the ioctl
2719  * @filp: file * from the ioctl
2720  * @cmd: cmd from ioctl
2721  * @arg: arg from ioctl
2722  *
2723  * Called by the user via ioctl.
2724  *
2725  * RETURNS:
2726  * Zero on success, errno on failure.
2727  */
2728 int drm_mode_detachmode_ioctl(struct drm_device *dev,
2729                               void *data, struct drm_file *file_priv)
2730 {
2731         struct drm_mode_object *obj;
2732         struct drm_mode_mode_cmd *mode_cmd = data;
2733         struct drm_connector *connector;
2734         struct drm_display_mode mode;
2735         struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2736         int ret;
2737
2738         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2739                 return -EINVAL;
2740
2741         mutex_lock(&dev->mode_config.mutex);
2742
2743         obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2744         if (!obj) {
2745                 ret = -EINVAL;
2746                 goto out;
2747         }
2748         connector = obj_to_connector(obj);
2749
2750         ret = drm_crtc_convert_umode(&mode, umode);
2751         if (ret) {
2752                 DRM_DEBUG_KMS("Invalid mode\n");
2753                 goto out;
2754         }
2755
2756         ret = drm_mode_detachmode(dev, connector, &mode);
2757 out:
2758         mutex_unlock(&dev->mode_config.mutex);
2759         return ret;
2760 }
2761
2762 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2763                                          const char *name, int num_values)
2764 {
2765         struct drm_property *property = NULL;
2766         int ret;
2767
2768         property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2769         if (!property)
2770                 return NULL;
2771
2772         if (num_values) {
2773                 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2774                 if (!property->values)
2775                         goto fail;
2776         }
2777
2778         ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2779         if (ret)
2780                 goto fail;
2781
2782         property->flags = flags;
2783         property->num_values = num_values;
2784         INIT_LIST_HEAD(&property->enum_blob_list);
2785
2786         if (name) {
2787                 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2788                 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2789         }
2790
2791         list_add_tail(&property->head, &dev->mode_config.property_list);
2792         return property;
2793 fail:
2794         kfree(property->values);
2795         kfree(property);
2796         return NULL;
2797 }
2798 EXPORT_SYMBOL(drm_property_create);
2799
2800 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2801                                          const char *name,
2802                                          const struct drm_prop_enum_list *props,
2803                                          int num_values)
2804 {
2805         struct drm_property *property;
2806         int i, ret;
2807
2808         flags |= DRM_MODE_PROP_ENUM;
2809
2810         property = drm_property_create(dev, flags, name, num_values);
2811         if (!property)
2812                 return NULL;
2813
2814         for (i = 0; i < num_values; i++) {
2815                 ret = drm_property_add_enum(property, i,
2816                                       props[i].type,
2817                                       props[i].name);
2818                 if (ret) {
2819                         drm_property_destroy(dev, property);
2820                         return NULL;
2821                 }
2822         }
2823
2824         return property;
2825 }
2826 EXPORT_SYMBOL(drm_property_create_enum);
2827
2828 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2829                                          int flags, const char *name,
2830                                          const struct drm_prop_enum_list *props,
2831                                          int num_values)
2832 {
2833         struct drm_property *property;
2834         int i, ret;
2835
2836         flags |= DRM_MODE_PROP_BITMASK;
2837
2838         property = drm_property_create(dev, flags, name, num_values);
2839         if (!property)
2840                 return NULL;
2841
2842         for (i = 0; i < num_values; i++) {
2843                 ret = drm_property_add_enum(property, i,
2844                                       props[i].type,
2845                                       props[i].name);
2846                 if (ret) {
2847                         drm_property_destroy(dev, property);
2848                         return NULL;
2849                 }
2850         }
2851
2852         return property;
2853 }
2854 EXPORT_SYMBOL(drm_property_create_bitmask);
2855
2856 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2857                                          const char *name,
2858                                          uint64_t min, uint64_t max)
2859 {
2860         struct drm_property *property;
2861
2862         flags |= DRM_MODE_PROP_RANGE;
2863
2864         property = drm_property_create(dev, flags, name, 2);
2865         if (!property)
2866                 return NULL;
2867
2868         property->values[0] = min;
2869         property->values[1] = max;
2870
2871         return property;
2872 }
2873 EXPORT_SYMBOL(drm_property_create_range);
2874
2875 int drm_property_add_enum(struct drm_property *property, int index,
2876                           uint64_t value, const char *name)
2877 {
2878         struct drm_property_enum *prop_enum;
2879
2880         if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2881                 return -EINVAL;
2882
2883         /*
2884          * Bitmask enum properties have the additional constraint of values
2885          * from 0 to 63
2886          */
2887         if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
2888                 return -EINVAL;
2889
2890         if (!list_empty(&property->enum_blob_list)) {
2891                 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2892                         if (prop_enum->value == value) {
2893                                 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2894                                 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2895                                 return 0;
2896                         }
2897                 }
2898         }
2899
2900         prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2901         if (!prop_enum)
2902                 return -ENOMEM;
2903
2904         strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2905         prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2906         prop_enum->value = value;
2907
2908         property->values[index] = value;
2909         list_add_tail(&prop_enum->head, &property->enum_blob_list);
2910         return 0;
2911 }
2912 EXPORT_SYMBOL(drm_property_add_enum);
2913
2914 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2915 {
2916         struct drm_property_enum *prop_enum, *pt;
2917
2918         list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2919                 list_del(&prop_enum->head);
2920                 kfree(prop_enum);
2921         }
2922
2923         if (property->num_values)
2924                 kfree(property->values);
2925         drm_mode_object_put(dev, &property->base);
2926         list_del(&property->head);
2927         kfree(property);
2928 }
2929 EXPORT_SYMBOL(drm_property_destroy);
2930
2931 void drm_object_attach_property(struct drm_mode_object *obj,
2932                                 struct drm_property *property,
2933                                 uint64_t init_val)
2934 {
2935         int count = obj->properties->count;
2936
2937         if (count == DRM_OBJECT_MAX_PROPERTY) {
2938                 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2939                         "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2940                         "you see this message on the same object type.\n",
2941                         obj->type);
2942                 return;
2943         }
2944
2945         obj->properties->ids[count] = property->base.id;
2946         obj->properties->values[count] = init_val;
2947         obj->properties->count++;
2948 }
2949 EXPORT_SYMBOL(drm_object_attach_property);
2950
2951 int drm_object_property_set_value(struct drm_mode_object *obj,
2952                                   struct drm_property *property, uint64_t val)
2953 {
2954         int i;
2955
2956         for (i = 0; i < obj->properties->count; i++) {
2957                 if (obj->properties->ids[i] == property->base.id) {
2958                         obj->properties->values[i] = val;
2959                         return 0;
2960                 }
2961         }
2962
2963         return -EINVAL;
2964 }
2965 EXPORT_SYMBOL(drm_object_property_set_value);
2966
2967 int drm_object_property_get_value(struct drm_mode_object *obj,
2968                                   struct drm_property *property, uint64_t *val)
2969 {
2970         int i;
2971
2972         for (i = 0; i < obj->properties->count; i++) {
2973                 if (obj->properties->ids[i] == property->base.id) {
2974                         *val = obj->properties->values[i];
2975                         return 0;
2976                 }
2977         }
2978
2979         return -EINVAL;
2980 }
2981 EXPORT_SYMBOL(drm_object_property_get_value);
2982
2983 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2984                                void *data, struct drm_file *file_priv)
2985 {
2986         struct drm_mode_object *obj;
2987         struct drm_mode_get_property *out_resp = data;
2988         struct drm_property *property;
2989         int enum_count = 0;
2990         int blob_count = 0;
2991         int value_count = 0;
2992         int ret = 0, i;
2993         int copied;
2994         struct drm_property_enum *prop_enum;
2995         struct drm_mode_property_enum __user *enum_ptr;
2996         struct drm_property_blob *prop_blob;
2997         uint32_t __user *blob_id_ptr;
2998         uint64_t __user *values_ptr;
2999         uint32_t __user *blob_length_ptr;
3000
3001         if (!drm_core_check_feature(dev, DRIVER_MODESET))
3002                 return -EINVAL;
3003
3004         mutex_lock(&dev->mode_config.mutex);
3005         obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3006         if (!obj) {
3007                 ret = -EINVAL;
3008                 goto done;
3009         }
3010         property = obj_to_property(obj);
3011
3012         if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3013                 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3014                         enum_count++;
3015         } else if (property->flags & DRM_MODE_PROP_BLOB) {
3016                 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3017                         blob_count++;
3018         }
3019
3020         value_count = property->num_values;
3021
3022         strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3023         out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3024         out_resp->flags = property->flags;
3025
3026         if ((out_resp->count_values >= value_count) && value_count) {
3027                 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
3028                 for (i = 0; i < value_count; i++) {
3029                         if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3030                                 ret = -EFAULT;
3031                                 goto done;
3032                         }
3033                 }
3034         }
3035         out_resp->count_values = value_count;
3036
3037         if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3038                 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3039                         copied = 0;
3040                         enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
3041                         list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3042
3043                                 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3044                                         ret = -EFAULT;
3045                                         goto done;
3046                                 }
3047
3048                                 if (copy_to_user(&enum_ptr[copied].name,
3049                                                  &prop_enum->name, DRM_PROP_NAME_LEN)) {
3050                                         ret = -EFAULT;
3051                                         goto done;
3052                                 }
3053                                 copied++;
3054                         }
3055                 }
3056                 out_resp->count_enum_blobs = enum_count;
3057         }
3058
3059         if (property->flags & DRM_MODE_PROP_BLOB) {
3060                 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3061                         copied = 0;
3062                         blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3063                         blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
3064
3065                         list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3066                                 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3067                                         ret = -EFAULT;
3068                                         goto done;
3069                                 }
3070
3071                                 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3072                                         ret = -EFAULT;
3073                                         goto done;
3074                                 }
3075
3076                                 copied++;
3077                         }
3078                 }
3079                 out_resp->count_enum_blobs = blob_count;
3080         }
3081 done:
3082         mutex_unlock(&dev->mode_config.mutex);
3083         return ret;
3084 }
3085
3086 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3087                                                           void *data)
3088 {
3089         struct drm_property_blob *blob;
3090         int ret;
3091
3092         if (!length || !data)
3093                 return NULL;
3094
3095         blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3096         if (!blob)
3097                 return NULL;
3098
3099         ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3100         if (ret) {
3101                 kfree(blob);
3102                 return NULL;
3103         }
3104
3105         blob->length = length;
3106
3107         memcpy(blob->data, data, length);
3108
3109         list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3110         return blob;
3111 }
3112
3113 static void drm_property_destroy_blob(struct drm_device *dev,
3114                                struct drm_property_blob *blob)
3115 {
3116         drm_mode_object_put(dev, &blob->base);
3117         list_del(&blob->head);
3118         kfree(blob);
3119 }
3120
3121 int drm_mode_getblob_ioctl(struct drm_device *dev,
3122                            void *data, struct drm_file *file_priv)
3123 {
3124         struct drm_mode_object *obj;
3125         struct drm_mode_get_blob *out_resp = data;
3126         struct drm_property_blob *blob;
3127         int ret = 0;
3128         void __user *blob_ptr;
3129
3130         if (!drm_core_check_feature(dev, DRIVER_MODESET))
3131                 return -EINVAL;
3132
3133         mutex_lock(&dev->mode_config.mutex);
3134         obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3135         if (!obj) {
3136                 ret = -EINVAL;
3137                 goto done;
3138         }
3139         blob = obj_to_blob(obj);
3140
3141         if (out_resp->length == blob->length) {
3142                 blob_ptr = (void __user *)(unsigned long)out_resp->data;
3143                 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3144                         ret = -EFAULT;
3145                         goto done;
3146                 }
3147         }
3148         out_resp->length = blob->length;
3149
3150 done:
3151         mutex_unlock(&dev->mode_config.mutex);
3152         return ret;
3153 }
3154
3155 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3156                                             struct edid *edid)
3157 {
3158         struct drm_device *dev = connector->dev;
3159         int ret, size;
3160
3161         if (connector->edid_blob_ptr)
3162                 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3163
3164         /* Delete edid, when there is none. */
3165         if (!edid) {
3166                 connector->edid_blob_ptr = NULL;
3167                 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
3168                 return ret;
3169         }
3170
3171         size = EDID_LENGTH * (1 + edid->extensions);
3172         connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3173                                                             size, edid);
3174         if (!connector->edid_blob_ptr)
3175                 return -EINVAL;
3176
3177         ret = drm_object_property_set_value(&connector->base,
3178                                                dev->mode_config.edid_property,
3179                                                connector->edid_blob_ptr->base.id);
3180
3181         return ret;
3182 }
3183 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3184
3185 static bool drm_property_change_is_valid(struct drm_property *property,
3186                                          uint64_t value)
3187 {
3188         if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3189                 return false;
3190         if (property->flags & DRM_MODE_PROP_RANGE) {
3191                 if (value < property->values[0] || value > property->values[1])
3192                         return false;
3193                 return true;
3194         } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3195                 int i;
3196                 uint64_t valid_mask = 0;
3197                 for (i = 0; i < property->num_values; i++)
3198                         valid_mask |= (1ULL << property->values[i]);
3199                 return !(value & ~valid_mask);
3200         } else if (property->flags & DRM_MODE_PROP_BLOB) {
3201                 /* Only the driver knows */
3202                 return true;
3203         } else {
3204                 int i;
3205                 for (i = 0; i < property->num_values; i++)
3206                         if (property->values[i] == value)
3207                                 return true;
3208                 return false;
3209         }
3210 }
3211
3212 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3213                                        void *data, struct drm_file *file_priv)
3214 {
3215         struct drm_mode_connector_set_property *conn_set_prop = data;
3216         struct drm_mode_obj_set_property obj_set_prop = {
3217                 .value = conn_set_prop->value,
3218                 .prop_id = conn_set_prop->prop_id,
3219                 .obj_id = conn_set_prop->connector_id,
3220                 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3221         };
3222
3223         /* It does all the locking and checking we need */
3224         return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
3225 }
3226
3227 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3228                                            struct drm_property *property,
3229                                            uint64_t value)
3230 {
3231         int ret = -EINVAL;
3232         struct drm_connector *connector = obj_to_connector(obj);
3233
3234         /* Do DPMS ourselves */
3235         if (property == connector->dev->mode_config.dpms_property) {
3236                 if (connector->funcs->dpms)
3237                         (*connector->funcs->dpms)(connector, (int)value);
3238                 ret = 0;
3239         } else if (connector->funcs->set_property)
3240                 ret = connector->funcs->set_property(connector, property, value);
3241
3242         /* store the property value if successful */
3243         if (!ret)
3244                 drm_object_property_set_value(&connector->base, property, value);
3245         return ret;
3246 }
3247
3248 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3249                                       struct drm_property *property,
3250                                       uint64_t value)
3251 {
3252         int ret = -EINVAL;
3253         struct drm_crtc *crtc = obj_to_crtc(obj);
3254
3255         if (crtc->funcs->set_property)
3256                 ret = crtc->funcs->set_property(crtc, property, value);
3257         if (!ret)
3258                 drm_object_property_set_value(obj, property, value);
3259
3260         return ret;
3261 }
3262
3263 static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3264                                       struct drm_property *property,
3265                                       uint64_t value)
3266 {
3267         int ret = -EINVAL;
3268         struct drm_plane *plane = obj_to_plane(obj);
3269
3270         if (plane->funcs->set_property)
3271                 ret = plane->funcs->set_property(plane, property, value);
3272         if (!ret)
3273                 drm_object_property_set_value(obj, property, value);
3274
3275         return ret;
3276 }
3277
3278 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3279                                       struct drm_file *file_priv)
3280 {
3281         struct drm_mode_obj_get_properties *arg = data;
3282         struct drm_mode_object *obj;
3283         int ret = 0;
3284         int i;
3285         int copied = 0;
3286         int props_count = 0;
3287         uint32_t __user *props_ptr;
3288         uint64_t __user *prop_values_ptr;
3289
3290         if (!drm_core_check_feature(dev, DRIVER_MODESET))
3291                 return -EINVAL;
3292
3293         mutex_lock(&dev->mode_config.mutex);
3294
3295         obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3296         if (!obj) {
3297                 ret = -EINVAL;
3298                 goto out;
3299         }
3300         if (!obj->properties) {
3301                 ret = -EINVAL;
3302                 goto out;
3303         }
3304
3305         props_count = obj->properties->count;
3306
3307         /* This ioctl is called twice, once to determine how much space is
3308          * needed, and the 2nd time to fill it. */
3309         if ((arg->count_props >= props_count) && props_count) {
3310                 copied = 0;
3311                 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3312                 prop_values_ptr = (uint64_t __user *)(unsigned long)
3313                                   (arg->prop_values_ptr);
3314                 for (i = 0; i < props_count; i++) {
3315                         if (put_user(obj->properties->ids[i],
3316                                      props_ptr + copied)) {
3317                                 ret = -EFAULT;
3318                                 goto out;
3319                         }
3320                         if (put_user(obj->properties->values[i],
3321                                      prop_values_ptr + copied)) {
3322                                 ret = -EFAULT;
3323                                 goto out;
3324                         }
3325                         copied++;
3326                 }
3327         }
3328         arg->count_props = props_count;
3329 out:
3330         mutex_unlock(&dev->mode_config.mutex);
3331         return ret;
3332 }
3333
3334 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3335                                     struct drm_file *file_priv)
3336 {
3337         struct drm_mode_obj_set_property *arg = data;
3338         struct drm_mode_object *arg_obj;
3339         struct drm_mode_object *prop_obj;
3340         struct drm_property *property;
3341         int ret = -EINVAL;
3342         int i;
3343
3344         if (!drm_core_check_feature(dev, DRIVER_MODESET))
3345                 return -EINVAL;
3346
3347         mutex_lock(&dev->mode_config.mutex);
3348
3349         arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3350         if (!arg_obj)
3351                 goto out;
3352         if (!arg_obj->properties)
3353                 goto out;
3354
3355         for (i = 0; i < arg_obj->properties->count; i++)
3356                 if (arg_obj->properties->ids[i] == arg->prop_id)
3357                         break;
3358
3359         if (i == arg_obj->properties->count)
3360                 goto out;
3361
3362         prop_obj = drm_mode_object_find(dev, arg->prop_id,
3363                                         DRM_MODE_OBJECT_PROPERTY);
3364         if (!prop_obj)
3365                 goto out;
3366         property = obj_to_property(prop_obj);
3367
3368         if (!drm_property_change_is_valid(property, arg->value))
3369                 goto out;
3370
3371         switch (arg_obj->type) {
3372         case DRM_MODE_OBJECT_CONNECTOR:
3373                 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3374                                                       arg->value);
3375                 break;
3376         case DRM_MODE_OBJECT_CRTC:
3377                 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3378                 break;
3379         case DRM_MODE_OBJECT_PLANE:
3380                 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3381                 break;
3382         }
3383
3384 out:
3385         mutex_unlock(&dev->mode_config.mutex);
3386         return ret;
3387 }
3388
3389 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3390                                       struct drm_encoder *encoder)
3391 {
3392         int i;
3393
3394         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3395                 if (connector->encoder_ids[i] == 0) {
3396                         connector->encoder_ids[i] = encoder->base.id;
3397                         return 0;
3398                 }
3399         }
3400         return -ENOMEM;
3401 }
3402 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3403
3404 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3405                                     struct drm_encoder *encoder)
3406 {
3407         int i;
3408         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3409                 if (connector->encoder_ids[i] == encoder->base.id) {
3410                         connector->encoder_ids[i] = 0;
3411                         if (connector->encoder == encoder)
3412                                 connector->encoder = NULL;
3413                         break;
3414                 }
3415         }
3416 }
3417 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3418
3419 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
3420                                   int gamma_size)
3421 {
3422         crtc->gamma_size = gamma_size;
3423
3424         crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3425         if (!crtc->gamma_store) {
3426                 crtc->gamma_size = 0;
3427                 return -ENOMEM;
3428         }
3429
3430         return 0;
3431 }
3432 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3433
3434 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3435                              void *data, struct drm_file *file_priv)
3436 {
3437         struct drm_mode_crtc_lut *crtc_lut = data;
3438         struct drm_mode_object *obj;
3439         struct drm_crtc *crtc;
3440         void *r_base, *g_base, *b_base;
3441         int size;
3442         int ret = 0;
3443
3444         if (!drm_core_check_feature(dev, DRIVER_MODESET))
3445                 return -EINVAL;
3446
3447         mutex_lock(&dev->mode_config.mutex);
3448         obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3449         if (!obj) {
3450                 ret = -EINVAL;
3451                 goto out;
3452         }
3453         crtc = obj_to_crtc(obj);
3454
3455         if (crtc->funcs->gamma_set == NULL) {
3456                 ret = -ENOSYS;
3457                 goto out;
3458         }
3459
3460         /* memcpy into gamma store */
3461         if (crtc_lut->gamma_size != crtc->gamma_size) {
3462                 ret = -EINVAL;
3463                 goto out;
3464         }
3465
3466         size = crtc_lut->gamma_size * (sizeof(uint16_t));
3467         r_base = crtc->gamma_store;
3468         if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3469                 ret = -EFAULT;
3470                 goto out;
3471         }
3472
3473         g_base = r_base + size;
3474         if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3475                 ret = -EFAULT;
3476                 goto out;
3477         }
3478
3479         b_base = g_base + size;
3480         if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3481                 ret = -EFAULT;
3482                 goto out;
3483         }
3484
3485         crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
3486
3487 out:
3488         mutex_unlock(&dev->mode_config.mutex);
3489         return ret;
3490
3491 }
3492
3493 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3494                              void *data, struct drm_file *file_priv)
3495 {
3496         struct drm_mode_crtc_lut *crtc_lut = data;
3497         struct drm_mode_object *obj;
3498         struct drm_crtc *crtc;
3499         void *r_base, *g_base, *b_base;
3500         int size;
3501         int ret = 0;
3502
3503         if (!drm_core_check_feature(dev, DRIVER_MODESET))
3504                 return -EINVAL;
3505
3506         mutex_lock(&dev->mode_config.mutex);
3507         obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3508         if (!obj) {
3509                 ret = -EINVAL;
3510                 goto out;
3511         }
3512         crtc = obj_to_crtc(obj);
3513
3514         /* memcpy into gamma store */
3515         if (crtc_lut->gamma_size != crtc->gamma_size) {
3516                 ret = -EINVAL;
3517                 goto out;
3518         }
3519
3520         size = crtc_lut->gamma_size * (sizeof(uint16_t));
3521         r_base = crtc->gamma_store;
3522         if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3523                 ret = -EFAULT;
3524                 goto out;
3525         }
3526
3527         g_base = r_base + size;
3528         if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3529                 ret = -EFAULT;
3530                 goto out;
3531         }
3532
3533         b_base = g_base + size;
3534         if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3535                 ret = -EFAULT;
3536                 goto out;
3537         }
3538 out:
3539         mutex_unlock(&dev->mode_config.mutex);
3540         return ret;
3541 }
3542
3543 int drm_mode_page_flip_ioctl(struct drm_device *dev,
3544                              void *data, struct drm_file *file_priv)
3545 {
3546         struct drm_mode_crtc_page_flip *page_flip = data;
3547         struct drm_mode_object *obj;
3548         struct drm_crtc *crtc;
3549         struct drm_framebuffer *fb;
3550         struct drm_pending_vblank_event *e = NULL;
3551         unsigned long flags;
3552         int hdisplay, vdisplay;
3553         int ret = -EINVAL;
3554
3555         if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3556             page_flip->reserved != 0)
3557                 return -EINVAL;
3558
3559         mutex_lock(&dev->mode_config.mutex);
3560         obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3561         if (!obj)
3562                 goto out;
3563         crtc = obj_to_crtc(obj);
3564
3565         if (crtc->fb == NULL) {
3566                 /* The framebuffer is currently unbound, presumably
3567                  * due to a hotplug event, that userspace has not
3568                  * yet discovered.
3569                  */
3570                 ret = -EBUSY;
3571                 goto out;
3572         }
3573
3574         if (crtc->funcs->page_flip == NULL)
3575                 goto out;
3576
3577         obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3578         if (!obj)
3579                 goto out;
3580         fb = obj_to_fb(obj);
3581
3582         hdisplay = crtc->mode.hdisplay;
3583         vdisplay = crtc->mode.vdisplay;
3584
3585         if (crtc->invert_dimensions)
3586                 swap(hdisplay, vdisplay);
3587
3588         if (hdisplay > fb->width ||
3589             vdisplay > fb->height ||
3590             crtc->x > fb->width - hdisplay ||
3591             crtc->y > fb->height - vdisplay) {
3592                 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3593                               fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3594                               crtc->invert_dimensions ? " (inverted)" : "");
3595                 ret = -ENOSPC;
3596                 goto out;
3597         }
3598
3599         if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3600                 ret = -ENOMEM;
3601                 spin_lock_irqsave(&dev->event_lock, flags);
3602                 if (file_priv->event_space < sizeof e->event) {
3603                         spin_unlock_irqrestore(&dev->event_lock, flags);
3604                         goto out;
3605                 }
3606                 file_priv->event_space -= sizeof e->event;
3607                 spin_unlock_irqrestore(&dev->event_lock, flags);
3608
3609                 e = kzalloc(sizeof *e, GFP_KERNEL);
3610                 if (e == NULL) {
3611                         spin_lock_irqsave(&dev->event_lock, flags);
3612                         file_priv->event_space += sizeof e->event;
3613                         spin_unlock_irqrestore(&dev->event_lock, flags);
3614                         goto out;
3615                 }
3616
3617                 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
3618                 e->event.base.length = sizeof e->event;
3619                 e->event.user_data = page_flip->user_data;
3620                 e->base.event = &e->event.base;
3621                 e->base.file_priv = file_priv;
3622                 e->base.destroy =
3623                         (void (*) (struct drm_pending_event *)) kfree;
3624         }
3625
3626         ret = crtc->funcs->page_flip(crtc, fb, e);
3627         if (ret) {
3628                 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3629                         spin_lock_irqsave(&dev->event_lock, flags);
3630                         file_priv->event_space += sizeof e->event;
3631                         spin_unlock_irqrestore(&dev->event_lock, flags);
3632                         kfree(e);
3633                 }
3634         }
3635
3636 out:
3637         mutex_unlock(&dev->mode_config.mutex);
3638         return ret;
3639 }
3640
3641 void drm_mode_config_reset(struct drm_device *dev)
3642 {
3643         struct drm_crtc *crtc;
3644         struct drm_encoder *encoder;
3645         struct drm_connector *connector;
3646
3647         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3648                 if (crtc->funcs->reset)
3649                         crtc->funcs->reset(crtc);
3650
3651         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3652                 if (encoder->funcs->reset)
3653                         encoder->funcs->reset(encoder);
3654
3655         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3656                 connector->status = connector_status_unknown;
3657
3658                 if (connector->funcs->reset)
3659                         connector->funcs->reset(connector);
3660         }
3661 }
3662 EXPORT_SYMBOL(drm_mode_config_reset);
3663
3664 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3665                                void *data, struct drm_file *file_priv)
3666 {
3667         struct drm_mode_create_dumb *args = data;
3668
3669         if (!dev->driver->dumb_create)
3670                 return -ENOSYS;
3671         return dev->driver->dumb_create(file_priv, dev, args);
3672 }
3673
3674 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3675                              void *data, struct drm_file *file_priv)
3676 {
3677         struct drm_mode_map_dumb *args = data;
3678
3679         /* call driver ioctl to get mmap offset */
3680         if (!dev->driver->dumb_map_offset)
3681                 return -ENOSYS;
3682
3683         return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3684 }
3685
3686 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3687                                 void *data, struct drm_file *file_priv)
3688 {
3689         struct drm_mode_destroy_dumb *args = data;
3690
3691         if (!dev->driver->dumb_destroy)
3692                 return -ENOSYS;
3693
3694         return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3695 }
3696
3697 /*
3698  * Just need to support RGB formats here for compat with code that doesn't
3699  * use pixel formats directly yet.
3700  */
3701 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3702                           int *bpp)
3703 {
3704         switch (format) {
3705         case DRM_FORMAT_RGB332:
3706         case DRM_FORMAT_BGR233:
3707                 *depth = 8;
3708                 *bpp = 8;
3709                 break;
3710         case DRM_FORMAT_XRGB1555:
3711         case DRM_FORMAT_XBGR1555:
3712         case DRM_FORMAT_RGBX5551:
3713         case DRM_FORMAT_BGRX5551:
3714         case DRM_FORMAT_ARGB1555:
3715         case DRM_FORMAT_ABGR1555:
3716         case DRM_FORMAT_RGBA5551:
3717         case DRM_FORMAT_BGRA5551:
3718                 *depth = 15;
3719                 *bpp = 16;
3720                 break;
3721         case DRM_FORMAT_RGB565:
3722         case DRM_FORMAT_BGR565:
3723                 *depth = 16;
3724                 *bpp = 16;
3725                 break;
3726         case DRM_FORMAT_RGB888:
3727         case DRM_FORMAT_BGR888:
3728                 *depth = 24;
3729                 *bpp = 24;
3730                 break;
3731         case DRM_FORMAT_XRGB8888:
3732         case DRM_FORMAT_XBGR8888:
3733         case DRM_FORMAT_RGBX8888:
3734         case DRM_FORMAT_BGRX8888:
3735                 *depth = 24;
3736                 *bpp = 32;
3737                 break;
3738         case DRM_FORMAT_XRGB2101010:
3739         case DRM_FORMAT_XBGR2101010:
3740         case DRM_FORMAT_RGBX1010102:
3741         case DRM_FORMAT_BGRX1010102:
3742         case DRM_FORMAT_ARGB2101010:
3743         case DRM_FORMAT_ABGR2101010:
3744         case DRM_FORMAT_RGBA1010102:
3745         case DRM_FORMAT_BGRA1010102:
3746                 *depth = 30;
3747                 *bpp = 32;
3748                 break;
3749         case DRM_FORMAT_ARGB8888:
3750         case DRM_FORMAT_ABGR8888:
3751         case DRM_FORMAT_RGBA8888:
3752         case DRM_FORMAT_BGRA8888:
3753                 *depth = 32;
3754                 *bpp = 32;
3755                 break;
3756         default:
3757                 DRM_DEBUG_KMS("unsupported pixel format\n");
3758                 *depth = 0;
3759                 *bpp = 0;
3760                 break;
3761         }
3762 }
3763 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
3764
3765 /**
3766  * drm_format_num_planes - get the number of planes for format
3767  * @format: pixel format (DRM_FORMAT_*)
3768  *
3769  * RETURNS:
3770  * The number of planes used by the specified pixel format.
3771  */
3772 int drm_format_num_planes(uint32_t format)
3773 {
3774         switch (format) {
3775         case DRM_FORMAT_YUV410:
3776         case DRM_FORMAT_YVU410:
3777         case DRM_FORMAT_YUV411:
3778         case DRM_FORMAT_YVU411:
3779         case DRM_FORMAT_YUV420:
3780         case DRM_FORMAT_YVU420:
3781         case DRM_FORMAT_YUV422:
3782         case DRM_FORMAT_YVU422:
3783         case DRM_FORMAT_YUV444:
3784         case DRM_FORMAT_YVU444:
3785                 return 3;
3786         case DRM_FORMAT_NV12:
3787         case DRM_FORMAT_NV21:
3788         case DRM_FORMAT_NV16:
3789         case DRM_FORMAT_NV61:
3790         case DRM_FORMAT_NV24:
3791         case DRM_FORMAT_NV42:
3792                 return 2;
3793         default:
3794                 return 1;
3795         }
3796 }
3797 EXPORT_SYMBOL(drm_format_num_planes);
3798
3799 /**
3800  * drm_format_plane_cpp - determine the bytes per pixel value
3801  * @format: pixel format (DRM_FORMAT_*)
3802  * @plane: plane index
3803  *
3804  * RETURNS:
3805  * The bytes per pixel value for the specified plane.
3806  */
3807 int drm_format_plane_cpp(uint32_t format, int plane)
3808 {
3809         unsigned int depth;
3810         int bpp;
3811
3812         if (plane >= drm_format_num_planes(format))
3813                 return 0;
3814
3815         switch (format) {
3816         case DRM_FORMAT_YUYV:
3817         case DRM_FORMAT_YVYU:
3818         case DRM_FORMAT_UYVY:
3819         case DRM_FORMAT_VYUY:
3820                 return 2;
3821         case DRM_FORMAT_NV12:
3822         case DRM_FORMAT_NV21:
3823         case DRM_FORMAT_NV16:
3824         case DRM_FORMAT_NV61:
3825         case DRM_FORMAT_NV24:
3826         case DRM_FORMAT_NV42:
3827                 return plane ? 2 : 1;
3828         case DRM_FORMAT_YUV410:
3829         case DRM_FORMAT_YVU410:
3830         case DRM_FORMAT_YUV411:
3831         case DRM_FORMAT_YVU411:
3832         case DRM_FORMAT_YUV420:
3833         case DRM_FORMAT_YVU420:
3834         case DRM_FORMAT_YUV422:
3835         case DRM_FORMAT_YVU422:
3836         case DRM_FORMAT_YUV444:
3837         case DRM_FORMAT_YVU444:
3838                 return 1;
3839         default:
3840                 drm_fb_get_bpp_depth(format, &depth, &bpp);
3841                 return bpp >> 3;
3842         }
3843 }
3844 EXPORT_SYMBOL(drm_format_plane_cpp);
3845
3846 /**
3847  * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3848  * @format: pixel format (DRM_FORMAT_*)
3849  *
3850  * RETURNS:
3851  * The horizontal chroma subsampling factor for the
3852  * specified pixel format.
3853  */
3854 int drm_format_horz_chroma_subsampling(uint32_t format)
3855 {
3856         switch (format) {
3857         case DRM_FORMAT_YUV411:
3858         case DRM_FORMAT_YVU411:
3859         case DRM_FORMAT_YUV410:
3860         case DRM_FORMAT_YVU410:
3861                 return 4;
3862         case DRM_FORMAT_YUYV:
3863         case DRM_FORMAT_YVYU:
3864         case DRM_FORMAT_UYVY:
3865         case DRM_FORMAT_VYUY:
3866         case DRM_FORMAT_NV12:
3867         case DRM_FORMAT_NV21:
3868         case DRM_FORMAT_NV16:
3869         case DRM_FORMAT_NV61:
3870         case DRM_FORMAT_YUV422:
3871         case DRM_FORMAT_YVU422:
3872         case DRM_FORMAT_YUV420:
3873         case DRM_FORMAT_YVU420:
3874                 return 2;
3875         default:
3876                 return 1;
3877         }
3878 }
3879 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3880
3881 /**
3882  * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3883  * @format: pixel format (DRM_FORMAT_*)
3884  *
3885  * RETURNS:
3886  * The vertical chroma subsampling factor for the
3887  * specified pixel format.
3888  */
3889 int drm_format_vert_chroma_subsampling(uint32_t format)
3890 {
3891         switch (format) {
3892         case DRM_FORMAT_YUV410:
3893         case DRM_FORMAT_YVU410:
3894                 return 4;
3895         case DRM_FORMAT_YUV420:
3896         case DRM_FORMAT_YVU420:
3897         case DRM_FORMAT_NV12:
3898         case DRM_FORMAT_NV21:
3899                 return 2;
3900         default:
3901                 return 1;
3902         }
3903 }
3904 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);