NV50: basic fbcon + misc fixes
[platform/upstream/libdrm.git] / linux-core / drm_crtc.c
1 /*
2  * Copyright (c) 2006-2007 Intel Corporation
3  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4  * Copyright (c) 2008 Red Hat Inc.
5  *
6  * DRM core CRTC related functions
7  *
8  * Permission to use, copy, modify, distribute, and sell this software and its
9  * documentation for any purpose is hereby granted without fee, provided that
10  * the above copyright notice appear in all copies and that both that copyright
11  * notice and this permission notice appear in supporting documentation, and
12  * that the name of the copyright holders not be used in advertising or
13  * publicity pertaining to distribution of the software without specific,
14  * written prior permission.  The copyright holders make no representations
15  * about the suitability of this software for any purpose.  It is provided "as
16  * is" without express or implied warranty.
17  *
18  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24  * OF THIS SOFTWARE.
25  *
26  * Authors:
27  *      Keith Packard
28  *      Eric Anholt <eric@anholt.net>
29  *      Dave Airlie <airlied@linux.ie>
30  *      Jesse Barnes <jesse.barnes@intel.com>
31  */
32 #include <linux/list.h>
33 #include "drm.h"
34 #include "drmP.h"
35 #include "drm_crtc.h"
36
37 struct drm_prop_enum_list {
38         int type;
39         char *name;
40 };
41
42 /*
43  * Global properties
44  */
45 static struct drm_prop_enum_list drm_dpms_enum_list[] =
46 {       { DPMSModeOn, "On" },
47         { DPMSModeStandby, "Standby" },
48         { DPMSModeSuspend, "Suspend" },
49         { DPMSModeOff, "Off" }
50 };
51
52 char *drm_get_dpms_name(int val)
53 {
54         int i;
55
56         for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
57                 if (drm_dpms_enum_list[i].type == val)
58                         return drm_dpms_enum_list[i].name;
59
60         return "unknown";
61 }
62
63 static struct drm_prop_enum_list drm_connector_enum_list[] = 
64 {       { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
65         { DRM_MODE_CONNECTOR_VGA, "VGA" },
66         { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
67         { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
68         { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
69         { DRM_MODE_CONNECTOR_Composite, "Composite" },
70         { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
71         { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
72         { DRM_MODE_CONNECTOR_Component, "Component" },
73         { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
74         { DRM_MODE_CONNECTOR_DisplayPort, "DisplayPort" },
75         { DRM_MODE_CONNECTOR_HDMIA, "HDMI Type A" },
76         { DRM_MODE_CONNECTOR_HDMIB, "HDMI Type B" },
77 };
78 static struct drm_prop_enum_list drm_encoder_enum_list[] =
79 {       { DRM_MODE_ENCODER_NONE, "None" },
80         { DRM_MODE_ENCODER_DAC, "DAC" },
81         { DRM_MODE_ENCODER_TMDS, "TMDS" },
82         { DRM_MODE_ENCODER_LVDS, "LVDS" },
83         { DRM_MODE_ENCODER_TVDAC, "TV" },
84 };
85
86 char *drm_get_encoder_name(struct drm_encoder *encoder)
87 {
88         static char buf[32];
89
90         snprintf(buf, 32, "%s-%d", drm_encoder_enum_list[encoder->encoder_type].name,
91                  encoder->base.id);
92         return buf;
93 }
94
95 char *drm_get_connector_name(struct drm_connector *connector)
96 {
97         static char buf[32];
98
99         snprintf(buf, 32, "%s-%d", drm_connector_enum_list[connector->connector_type].name,
100                  connector->connector_type_id);
101         return buf;
102 }
103 EXPORT_SYMBOL(drm_get_connector_name);
104
105 char *drm_get_connector_status_name(enum drm_connector_status status)
106 {
107         if (status == connector_status_connected)
108                 return "connected";
109         else if (status == connector_status_disconnected)
110                 return "disconnected";
111         else
112                 return "unknown";
113 }
114
115 /**
116  * drm_idr_get - allocate a new identifier
117  * @dev: DRM device
118  * @ptr: object pointer, used to generate unique ID
119  *
120  * LOCKING:
121  * Caller must hold DRM mode_config lock.
122  *
123  * Create a unique identifier based on @ptr in @dev's identifier space.  Used
124  * for tracking modes, CRTCs and connectors.
125  *
126  * RETURNS:
127  * New unique (relative to other objects in @dev) integer identifier for the
128  * object.
129  */
130 static int drm_mode_object_get(struct drm_device *dev, struct drm_mode_object *obj, uint32_t obj_type)
131 {
132         int new_id = 0;
133         int ret;
134 again:
135         if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
136                 DRM_ERROR("Ran out memory getting a mode number\n");
137                 return -EINVAL;
138         }
139
140         ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
141         if (ret == -EAGAIN)
142                 goto again;
143
144         obj->id = new_id;
145         obj->type = obj_type;
146         return 0;
147 }
148
149 /**
150  * drm_mode_object_put - free an identifer
151  * @dev: DRM device
152  * @id: ID to free
153  *
154  * LOCKING:
155  * Caller must hold DRM mode_config lock.
156  *
157  * Free @id from @dev's unique identifier pool.
158  */
159 static void drm_mode_object_put(struct drm_device *dev, struct drm_mode_object *object)
160 {
161         idr_remove(&dev->mode_config.crtc_idr, object->id);
162 }
163
164 static void *drm_mode_object_find(struct drm_device *dev, uint32_t id, uint32_t type)
165 {
166         struct drm_mode_object *obj;
167
168         obj = idr_find(&dev->mode_config.crtc_idr, id);
169         if (!obj || (obj->type != type) || (obj->id != id))
170                 return NULL;
171
172         return obj;
173 }
174
175 /**
176  * drm_crtc_from_fb - find the CRTC structure associated with an fb
177  * @dev: DRM device
178  * @fb: framebuffer in question
179  *
180  * LOCKING:
181  * Caller must hold mode_config lock.
182  *
183  * Find CRTC in the mode_config structure that matches @fb.
184  *
185  * RETURNS:
186  * Pointer to the CRTC or NULL if it wasn't found.
187  */
188 struct drm_crtc *drm_crtc_from_fb(struct drm_device *dev,
189                                   struct drm_framebuffer *fb)
190 {
191         struct drm_crtc *crtc;
192
193         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
194                 if (crtc->fb == fb)
195                         return crtc;
196         }
197         return NULL;
198 }
199
200 /**
201  * drm_framebuffer_create - create a new framebuffer object
202  * @dev: DRM device
203  *
204  * LOCKING:
205  * Caller must hold mode config lock.
206  *
207  * Creates a new framebuffer objects and adds it to @dev's DRM mode_config.
208  *
209  * RETURNS:
210  * Pointer to new framebuffer or NULL on error.
211  */
212 struct drm_framebuffer *drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
213                                              const struct drm_framebuffer_funcs *funcs)
214 {
215         drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
216         fb->dev = dev;
217         fb->funcs = funcs;
218         dev->mode_config.num_fb++;
219         list_add(&fb->head, &dev->mode_config.fb_list);
220
221         return fb;
222 }
223 EXPORT_SYMBOL(drm_framebuffer_init);
224
225 /**
226  * drm_framebuffer_cleanup - remove a framebuffer object
227  * @fb: framebuffer to remove
228  *
229  * LOCKING:
230  * Caller must hold mode config lock.
231  *
232  * Scans all the CRTCs in @dev's mode_config.  If they're using @fb, removes
233  * it, setting it to NULL.
234  */
235 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
236 {
237         struct drm_device *dev = fb->dev;
238         struct drm_crtc *crtc;
239
240         /* remove from any CRTC */
241         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
242                 if (crtc->fb == fb)
243                         crtc->fb = NULL;
244         }
245
246         drm_mode_object_put(dev, &fb->base);
247         list_del(&fb->head);
248         dev->mode_config.num_fb--;
249 }
250 EXPORT_SYMBOL(drm_framebuffer_cleanup);
251
252 /**
253  * drm_crtc_init - Initialise a new CRTC object
254  * @dev: DRM device
255  * @crtc: CRTC object to init
256  * @funcs: callbacks for the new CRTC
257  *
258  * LOCKING:
259  * Caller must hold mode config lock.
260  *
261  * Inits a new object created as base part of an driver crtc object.
262  */
263 void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
264                    const struct drm_crtc_funcs *funcs)
265 {
266         crtc->dev = dev;
267         crtc->funcs = funcs;
268
269         drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
270
271         list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
272         dev->mode_config.num_crtc++;
273 }
274 EXPORT_SYMBOL(drm_crtc_init);
275
276 /**
277  * drm_crtc_cleanup - Cleans up the core crtc usage.
278  * @crtc: CRTC to cleanup
279  *
280  * LOCKING:
281  * Caller must hold mode config lock.
282  *
283  * Cleanup @crtc. Removes from drm modesetting space
284  * does NOT free object, caller does that.
285  */
286 void drm_crtc_cleanup(struct drm_crtc *crtc)
287 {
288         struct drm_device *dev = crtc->dev;
289
290         if (crtc->gamma_store) {
291                 kfree(crtc->gamma_store);
292                 crtc->gamma_store = NULL;
293         }
294
295         drm_mode_object_put(dev, &crtc->base);
296         list_del(&crtc->head);
297         dev->mode_config.num_crtc--;
298 }
299 EXPORT_SYMBOL(drm_crtc_cleanup);
300
301 /**
302  * drm_mode_probed_add - add a mode to the specified connector's probed mode list
303  * @connector: connector the new mode
304  * @mode: mode data
305  *
306  * LOCKING:
307  * Caller must hold mode config lock.
308  * 
309  * Add @mode to @connector's mode list for later use.
310  */
311 void drm_mode_probed_add(struct drm_connector *connector,
312                          struct drm_display_mode *mode)
313 {
314         list_add(&mode->head, &connector->probed_modes);
315 }
316 EXPORT_SYMBOL(drm_mode_probed_add);
317
318 /**
319  * drm_mode_remove - remove and free a mode
320  * @connector: connector list to modify
321  * @mode: mode to remove
322  *
323  * LOCKING:
324  * Caller must hold mode config lock.
325  * 
326  * Remove @mode from @connector's mode list, then free it.
327  */
328 void drm_mode_remove(struct drm_connector *connector, struct drm_display_mode *mode)
329 {
330         list_del(&mode->head);
331         kfree(mode);
332 }
333 EXPORT_SYMBOL(drm_mode_remove);
334
335 /**
336  * drm_connector_init - Init a preallocated connector
337  * @dev: DRM device
338  * @connector: the connector to init
339  * @funcs: callbacks for this connector
340  * @name: user visible name of the connector
341  *
342  * LOCKING:
343  * Caller must hold @dev's mode_config lock.
344  *
345  * Initialises a preallocated connector. Connectors should be
346  * subclassed as part of driver connector objects.
347  */
348 void drm_connector_init(struct drm_device *dev,
349                      struct drm_connector *connector,
350                      const struct drm_connector_funcs *funcs,
351                      int connector_type)
352 {
353         connector->dev = dev;
354         connector->funcs = funcs;
355         drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
356         connector->connector_type = connector_type;
357         connector->connector_type_id = 1; /* TODO */
358         INIT_LIST_HEAD(&connector->user_modes);
359         INIT_LIST_HEAD(&connector->probed_modes);
360         INIT_LIST_HEAD(&connector->modes);
361         connector->edid_blob_ptr = NULL;
362         /* randr_connector? */
363         /* connector_set_monitor(connector)? */
364         /* check for connector_ignored(connector)? */
365
366         mutex_lock(&dev->mode_config.mutex);
367         list_add_tail(&connector->head, &dev->mode_config.connector_list);
368         dev->mode_config.num_connector++;
369
370         drm_connector_attach_property(connector, dev->mode_config.edid_property, 0);
371
372         drm_connector_attach_property(connector, dev->mode_config.dpms_property, 0);
373
374         mutex_unlock(&dev->mode_config.mutex);
375 }
376 EXPORT_SYMBOL(drm_connector_init);
377
378 /**
379  * drm_connector_cleanup - cleans up an initialised connector
380  * @connector: connector to cleanup
381  *
382  * LOCKING:
383  * Caller must hold @dev's mode_config lock.
384  *
385  * Cleans up the connector but doesn't free the object.
386  */
387 void drm_connector_cleanup(struct drm_connector *connector)
388 {
389         struct drm_device *dev = connector->dev;
390         struct drm_display_mode *mode, *t;
391
392         list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
393                 drm_mode_remove(connector, mode);
394
395         list_for_each_entry_safe(mode, t, &connector->modes, head)
396                 drm_mode_remove(connector, mode);
397
398         list_for_each_entry_safe(mode, t, &connector->user_modes, head)
399                 drm_mode_remove(connector, mode);
400
401         mutex_lock(&dev->mode_config.mutex);
402         drm_mode_object_put(dev, &connector->base);
403         list_del(&connector->head);
404         mutex_unlock(&dev->mode_config.mutex);
405 }
406 EXPORT_SYMBOL(drm_connector_cleanup);
407
408 void drm_encoder_init(struct drm_device *dev,
409                       struct drm_encoder *encoder,
410                       const struct drm_encoder_funcs *funcs,
411                       int encoder_type)
412 {
413         encoder->dev = dev;
414
415         drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
416         encoder->encoder_type = encoder_type;
417         encoder->funcs = funcs;
418
419         mutex_lock(&dev->mode_config.mutex);
420         list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
421         dev->mode_config.num_encoder++;
422
423         mutex_unlock(&dev->mode_config.mutex);
424 }
425 EXPORT_SYMBOL(drm_encoder_init);
426
427 void drm_encoder_cleanup(struct drm_encoder *encoder)
428 {
429         struct drm_device *dev = encoder->dev;
430         mutex_lock(&dev->mode_config.mutex);
431         drm_mode_object_put(dev, &encoder->base);
432         list_del(&encoder->head);
433         mutex_unlock(&dev->mode_config.mutex);
434 }
435 EXPORT_SYMBOL(drm_encoder_cleanup);
436
437 /**
438  * drm_mode_create - create a new display mode
439  * @dev: DRM device
440  *
441  * LOCKING:
442  * None.
443  *
444  * Create a new drm_display_mode, give it an ID, and return it.
445  *
446  * RETURNS:
447  * Pointer to new mode on success, NULL on error.
448  */
449 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
450 {
451         struct drm_display_mode *nmode;
452
453         nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
454         if (!nmode)
455                 return NULL;
456
457         drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
458         return nmode;
459 }
460 EXPORT_SYMBOL(drm_mode_create);
461
462 /**
463  * drm_mode_destroy - remove a mode
464  * @dev: DRM device
465  * @mode: mode to remove
466  *
467  * LOCKING:
468  * Caller must hold mode config lock.
469  *
470  * Free @mode's unique identifier, then free it.
471  */
472 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
473 {
474         drm_mode_object_put(dev, &mode->base);
475
476         kfree(mode);
477 }
478 EXPORT_SYMBOL(drm_mode_destroy);
479
480 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
481 {
482         int i;
483
484         /*
485          * Standard properties (apply to all connectors)
486          */
487         dev->mode_config.edid_property =
488                 drm_property_create(dev, DRM_MODE_PROP_BLOB | DRM_MODE_PROP_IMMUTABLE,
489                                     "EDID", 0);
490
491         dev->mode_config.dpms_property =
492                 drm_property_create(dev, DRM_MODE_PROP_ENUM, 
493                         "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
494         for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
495                 drm_property_add_enum(dev->mode_config.dpms_property, i, drm_dpms_enum_list[i].type, drm_dpms_enum_list[i].name);
496
497         return 0;
498 }
499
500 /**
501  * drm_create_tv_properties - create TV specific connector properties
502  * @dev: DRM device
503  * @num_modes: number of different TV formats (modes) supported
504  * @modes: array of pointers to strings containing name of each format
505  *
506  * Called by a driver's TV initialization routine, this function creates
507  * the TV specific connector properties for a given device.  Caller is
508  * responsible for allocating a list of format names and passing them to
509  * this routine.
510  */
511 bool drm_create_tv_properties(struct drm_device *dev, int num_modes,
512                               char *modes[])
513 {
514         int i;
515
516         dev->mode_config.tv_left_margin_property =
517                 drm_property_create(dev, DRM_MODE_PROP_RANGE |
518                                     DRM_MODE_PROP_IMMUTABLE,
519                                     "left margin", 2);
520         dev->mode_config.tv_left_margin_property->values[0] = 0;
521         dev->mode_config.tv_left_margin_property->values[1] = 100;
522
523         dev->mode_config.tv_right_margin_property =
524                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
525                                     "right margin", 2);
526         dev->mode_config.tv_right_margin_property->values[0] = 0;
527         dev->mode_config.tv_right_margin_property->values[1] = 100;
528
529         dev->mode_config.tv_top_margin_property =
530                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
531                                     "top margin", 2);
532         dev->mode_config.tv_top_margin_property->values[0] = 0;
533         dev->mode_config.tv_top_margin_property->values[1] = 100;
534
535         dev->mode_config.tv_bottom_margin_property =
536                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
537                                     "bottom margin", 2);
538         dev->mode_config.tv_bottom_margin_property->values[0] = 0;
539         dev->mode_config.tv_bottom_margin_property->values[1] = 100;
540
541         dev->mode_config.tv_mode_property =
542                 drm_property_create(dev, DRM_MODE_PROP_ENUM,
543                                     "mode", num_modes);
544         for (i = 0; i < num_modes; i++)
545                 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
546                                       i, modes[i]);
547
548         return 0;
549 }
550 EXPORT_SYMBOL(drm_create_tv_properties);
551
552 /**
553  * drm_mode_config_init - initialize DRM mode_configuration structure
554  * @dev: DRM device
555  *
556  * LOCKING:
557  * None, should happen single threaded at init time.
558  *
559  * Initialize @dev's mode_config structure, used for tracking the graphics
560  * configuration of @dev.
561  */
562 void drm_mode_config_init(struct drm_device *dev)
563 {
564         mutex_init(&dev->mode_config.mutex);
565         INIT_LIST_HEAD(&dev->mode_config.fb_list);
566         INIT_LIST_HEAD(&dev->mode_config.fb_kernel_list);
567         INIT_LIST_HEAD(&dev->mode_config.crtc_list);
568         INIT_LIST_HEAD(&dev->mode_config.connector_list);
569         INIT_LIST_HEAD(&dev->mode_config.encoder_list);
570         INIT_LIST_HEAD(&dev->mode_config.property_list);
571         INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
572         idr_init(&dev->mode_config.crtc_idr);
573
574         drm_mode_create_standard_connector_properties(dev);
575
576         /* Just to be sure */
577         dev->mode_config.current_generation = 0;
578         dev->mode_config.num_fb = 0;
579         dev->mode_config.num_connector = 0;
580         dev->mode_config.num_crtc = 0;
581         dev->mode_config.num_encoder = 0;
582         dev->mode_config.hotplug_counter = 0;
583 }
584 EXPORT_SYMBOL(drm_mode_config_init);
585
586 int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
587 {
588         uint32_t total_objects = 0;
589
590         total_objects += dev->mode_config.num_crtc;
591         total_objects += dev->mode_config.num_connector;
592         total_objects += dev->mode_config.num_encoder;
593
594         group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
595         if (!group->id_list)
596                 return -ENOMEM;
597
598         group->num_crtcs = 0;
599         group->num_connectors = 0;
600         group->num_encoders = 0;
601         return 0;
602 }
603
604 int drm_mode_group_init_legacy_group(struct drm_device *dev, struct drm_mode_group *group)
605 {
606         struct drm_crtc *crtc;
607         struct drm_encoder *encoder;
608         struct drm_connector *connector;
609
610         if (drm_mode_group_init(dev, group))
611                 return -ENOMEM;
612
613         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
614                 group->id_list[group->num_crtcs++] = crtc->base.id;
615
616         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
617                 group->id_list[group->num_crtcs + group->num_encoders++] = encoder->base.id;
618
619         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
620                 group->id_list[group->num_crtcs + group->num_encoders + group->num_connectors++] = connector->base.id;
621
622         return 0;
623 }
624
625 /**
626  * drm_mode_config_cleanup - free up DRM mode_config info
627  * @dev: DRM device
628  *
629  * LOCKING:
630  * Caller must hold mode config lock.
631  *
632  * Free up all the connectors and CRTCs associated with this DRM device, then
633  * free up the framebuffers and associated buffer objects.
634  *
635  * FIXME: cleanup any dangling user buffer objects too
636  */
637 void drm_mode_config_cleanup(struct drm_device *dev)
638 {
639         struct drm_connector *connector, *ot;
640         struct drm_crtc *crtc, *ct;
641         struct drm_encoder *encoder, *enct;
642         struct drm_framebuffer *fb, *fbt;
643         struct drm_property *property, *pt;
644
645         list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list, head) {
646                 encoder->funcs->destroy(encoder);
647         }
648
649         list_for_each_entry_safe(connector, ot, &dev->mode_config.connector_list, head) {
650                 connector->funcs->destroy(connector);
651         }
652
653         list_for_each_entry_safe(property, pt, &dev->mode_config.property_list, head) {
654                 drm_property_destroy(dev, property);
655         }
656
657         list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
658                 fb->funcs->destroy(fb);
659         }
660
661         list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
662                 crtc->funcs->destroy(crtc);
663         }
664
665 }
666 EXPORT_SYMBOL(drm_mode_config_cleanup);
667
668 int drm_mode_hotplug_ioctl(struct drm_device *dev,
669                            void *data, struct drm_file *file_priv)
670 {
671         struct drm_mode_hotplug *arg = data;
672
673         arg->counter = dev->mode_config.hotplug_counter;
674
675         return 0;
676 }
677
678 /**
679  * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
680  * @out: drm_mode_modeinfo struct to return to the user
681  * @in: drm_display_mode to use
682  *
683  * LOCKING:
684  * None.
685  *
686  * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
687  * the user.
688  */
689 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out, struct drm_display_mode *in)
690 {
691         out->clock = in->clock;
692         out->hdisplay = in->hdisplay;
693         out->hsync_start = in->hsync_start;
694         out->hsync_end = in->hsync_end;
695         out->htotal = in->htotal;
696         out->hskew = in->hskew;
697         out->vdisplay = in->vdisplay;
698         out->vsync_start = in->vsync_start;
699         out->vsync_end = in->vsync_end;
700         out->vtotal = in->vtotal;
701         out->vscan = in->vscan;
702         out->vrefresh = in->vrefresh;
703         out->flags = in->flags;
704         out->type = in->type;
705         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
706         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
707 }
708
709 /**
710  * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
711  * @out: drm_display_mode to return to the user
712  * @in: drm_mode_modeinfo to use
713  *
714  * LOCKING:
715  * None.
716  *
717  * Convert a drmo_mode_modeinfo into a drm_display_mode structure to return to
718  * the caller.
719  */
720 void drm_crtc_convert_umode(struct drm_display_mode *out, struct drm_mode_modeinfo *in)
721 {
722         out->clock = in->clock;
723         out->hdisplay = in->hdisplay;
724         out->hsync_start = in->hsync_start;
725         out->hsync_end = in->hsync_end;
726         out->htotal = in->htotal;
727         out->hskew = in->hskew;
728         out->vdisplay = in->vdisplay;
729         out->vsync_start = in->vsync_start;
730         out->vsync_end = in->vsync_end;
731         out->vtotal = in->vtotal;
732         out->vscan = in->vscan;
733         out->vrefresh = in->vrefresh;
734         out->flags = in->flags;
735         out->type = in->type;
736         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
737         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
738 }
739
740 /**
741  * drm_mode_getresources - get graphics configuration
742  * @inode: inode from the ioctl
743  * @filp: file * from the ioctl
744  * @cmd: cmd from ioctl
745  * @arg: arg from ioctl
746  *
747  * LOCKING:
748  * Takes mode config lock.
749  *
750  * Construct a set of configuration description structures and return
751  * them to the user, including CRTC, connector and framebuffer configuration.
752  *
753  * Called by the user via ioctl.
754  *
755  * RETURNS:
756  * Zero on success, errno on failure.
757  */
758 int drm_mode_getresources(struct drm_device *dev,
759                           void *data, struct drm_file *file_priv)
760 {
761         struct drm_mode_card_res *card_res = data;
762         struct list_head *lh;
763         struct drm_framebuffer *fb;
764         struct drm_connector *connector;
765         struct drm_crtc *crtc;
766         struct drm_encoder *encoder;
767         int ret = 0;
768         int connector_count = 0;
769         int crtc_count = 0;
770         int fb_count = 0;
771         int encoder_count = 0;
772         int copied = 0, i;
773         uint32_t __user *fb_id;
774         uint32_t __user *crtc_id;
775         uint32_t __user *connector_id;
776         uint32_t __user *encoder_id;
777         struct drm_mode_group *mode_group;
778
779         mutex_lock(&dev->mode_config.mutex);
780
781         /* for the non-control nodes we need to limit the list of resources by IDs in the
782            group list for this node */
783         list_for_each(lh, &file_priv->fbs)
784                 fb_count++;
785
786         mode_group = &file_priv->master->minor->mode_group;
787         if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
788
789                 list_for_each(lh, &dev->mode_config.crtc_list)
790                         crtc_count++;
791
792                 list_for_each(lh, &dev->mode_config.connector_list)
793                         connector_count++;
794
795                 list_for_each(lh, &dev->mode_config.encoder_list)
796                         encoder_count++;
797         } else {
798
799                 crtc_count = mode_group->num_crtcs;
800                 connector_count = mode_group->num_connectors;
801                 encoder_count = mode_group->num_encoders;
802         }
803
804         card_res->max_height = dev->mode_config.max_height;
805         card_res->min_height = dev->mode_config.min_height;
806         card_res->max_width = dev->mode_config.max_width;
807         card_res->min_width = dev->mode_config.min_width;
808         card_res->generation = dev->mode_config.current_generation;
809
810         /* handle this in 4 parts */
811         /* FBs */
812         if (card_res->count_fbs >= fb_count) {
813                 copied = 0;
814                 fb_id = (uint32_t *)(unsigned long)card_res->fb_id_ptr;
815                 list_for_each_entry(fb, &file_priv->fbs, head) {
816                         if (put_user(fb->base.id, fb_id + copied)) {
817                                 ret = -EFAULT;
818                                 goto out;
819                         }
820                         copied++;
821                 }
822         }
823         card_res->count_fbs = fb_count;
824
825         /* CRTCs */
826         if (card_res->count_crtcs >= crtc_count) {
827                 copied = 0;
828                 crtc_id = (uint32_t *)(unsigned long)card_res->crtc_id_ptr;
829                 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
830                         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
831                                 DRM_DEBUG("CRTC ID is %d\n", crtc->base.id);
832                                 if (put_user(crtc->base.id, crtc_id + copied)) {
833                                         ret = -EFAULT;
834                                         goto out;
835                                 }
836                                 copied++;
837                         }
838                 } else {
839                         for (i = 0; i < mode_group->num_crtcs; i++) {
840                                 if (put_user(mode_group->id_list[i], crtc_id + copied)) {
841                                         ret = -EFAULT;
842                                         goto out;
843                                 }
844                                 copied++;
845                         }
846                 }
847         }
848         card_res->count_crtcs = crtc_count;
849
850         /* Encoders */
851         if (card_res->count_encoders >= encoder_count) {
852                 copied = 0;
853                 encoder_id = (uint32_t *)(unsigned long)card_res->encoder_id_ptr;
854                 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
855                         list_for_each_entry(encoder, &dev->mode_config.encoder_list,
856                                             head) {
857                                 DRM_DEBUG("ENCODER ID is %d\n", encoder->base.id);
858                                 if (put_user(encoder->base.id, encoder_id + copied)) {
859                                         ret = -EFAULT;
860                                         goto out;
861                                 }
862                                 copied++;
863                         }
864                 } else {
865                         for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
866                                 if (put_user(mode_group->id_list[i], encoder_id + copied)) {
867                                         ret = -EFAULT;
868                                         goto out;
869                                 }
870                                 copied++;
871                         }
872
873                 }
874         }
875         card_res->count_encoders = encoder_count;
876
877         /* Connectors */
878         if (card_res->count_connectors >= connector_count) {
879                 copied = 0;
880                 connector_id = (uint32_t *)(unsigned long)card_res->connector_id_ptr;
881                 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
882                         list_for_each_entry(connector, &dev->mode_config.connector_list,
883                                             head) {
884                                 DRM_DEBUG("CONNECTOR ID is %d\n", connector->base.id);
885                                 if (put_user(connector->base.id, connector_id + copied)) {
886                                         ret = -EFAULT;
887                                         goto out;
888                                 }
889                                 copied++;
890                         }
891                 } else {
892                         int start = mode_group->num_crtcs + mode_group->num_encoders;
893                         for (i = start; i < start + mode_group->num_connectors; i++) {
894                                 if (put_user(mode_group->id_list[i], connector_id + copied)) {
895                                         ret = -EFAULT;
896                                         goto out;
897                                 }
898                                 copied++;
899                         }
900                 }
901         }
902         card_res->count_connectors = connector_count;
903
904         DRM_DEBUG("Counted %d %d %d\n", card_res->count_crtcs,
905                   card_res->count_connectors, card_res->count_encoders);
906
907 out:
908         mutex_unlock(&dev->mode_config.mutex);
909         return ret;
910 }
911
912 /**
913  * drm_mode_getcrtc - get CRTC configuration
914  * @inode: inode from the ioctl
915  * @filp: file * from the ioctl
916  * @cmd: cmd from ioctl
917  * @arg: arg from ioctl
918  *
919  * LOCKING:
920  * Caller? (FIXME)
921  *
922  * Construct a CRTC configuration structure to return to the user.
923  *
924  * Called by the user via ioctl.
925  *
926  * RETURNS:
927  * Zero on success, errno on failure.
928  */
929 int drm_mode_getcrtc(struct drm_device *dev,
930                      void *data, struct drm_file *file_priv)
931 {
932         struct drm_mode_crtc *crtc_resp = data;
933         struct drm_crtc *crtc;
934         struct drm_mode_object *obj;
935         int ret = 0;
936
937         mutex_lock(&dev->mode_config.mutex);
938
939         obj = drm_mode_object_find(dev, crtc_resp->crtc_id, DRM_MODE_OBJECT_CRTC);
940         if (!obj) {
941                 ret = -EINVAL;
942                 goto out;
943         }
944         crtc = obj_to_crtc(obj);
945
946         crtc_resp->x = crtc->x;
947         crtc_resp->y = crtc->y;
948         crtc_resp->gamma_size = crtc->gamma_size;
949         crtc_resp->generation = dev->mode_config.current_generation;
950         if (crtc->fb)
951                 crtc_resp->fb_id = crtc->fb->base.id;
952         else
953                 crtc_resp->fb_id = 0;
954
955         crtc_resp->connectors = 0;
956         if (crtc->enabled) {
957
958                 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
959                 crtc_resp->mode_valid = 1;
960
961         } else {
962                 crtc_resp->mode_valid = 0;
963         }
964
965 out:
966         mutex_unlock(&dev->mode_config.mutex);
967         return ret;
968 }
969
970 /**
971  * drm_mode_getconnector - get connector configuration
972  * @inode: inode from the ioctl
973  * @filp: file * from the ioctl
974  * @cmd: cmd from ioctl
975  * @arg: arg from ioctl
976  *
977  * LOCKING:
978  * Caller? (FIXME)
979  *
980  * Construct a connector configuration structure to return to the user.
981  *
982  * Called by the user via ioctl.
983  *
984  * RETURNS:
985  * Zero on success, errno on failure.
986  */
987 int drm_mode_getconnector(struct drm_device *dev,
988                        void *data, struct drm_file *file_priv)
989 {
990         struct drm_mode_get_connector *out_resp = data;
991         struct drm_mode_object *obj;
992         struct drm_connector *connector;
993         struct drm_display_mode *mode;
994         int mode_count = 0;
995         int props_count = 0;
996         int encoders_count = 0;
997         int ret = 0;
998         int copied = 0;
999         int i;
1000         struct drm_mode_modeinfo u_mode;
1001         struct drm_mode_modeinfo __user *mode_ptr;
1002         uint32_t __user *prop_ptr;
1003         uint64_t __user *prop_values;
1004         uint32_t __user *encoder_ptr;
1005
1006         memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1007
1008         DRM_DEBUG("connector id %d:\n", out_resp->connector);
1009
1010         mutex_lock(&dev->mode_config.mutex);
1011
1012         obj = drm_mode_object_find(dev, out_resp->connector, DRM_MODE_OBJECT_CONNECTOR);
1013         if (!obj) {
1014                 ret = -EINVAL;
1015                 goto out;
1016         }
1017         connector = obj_to_connector(obj);
1018
1019         for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1020                 if (connector->property_ids[i] != 0) {
1021                         props_count++;
1022                 }
1023         }
1024
1025         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1026                 if (connector->encoder_ids[i] != 0) {
1027                         encoders_count++;
1028                 }
1029         }
1030
1031         if (out_resp->count_modes == 0) {
1032                 connector->funcs->fill_modes(connector, dev->mode_config.max_width, dev->mode_config.max_height);
1033         }
1034
1035         /* delayed so we get modes regardless of pre-fill_modes state */
1036         list_for_each_entry(mode, &connector->modes, head)
1037                 mode_count++;
1038
1039         out_resp->generation = dev->mode_config.current_generation;
1040         out_resp->connector_type = connector->connector_type;
1041         out_resp->connector_type_id = connector->connector_type_id;
1042         out_resp->mm_width = connector->display_info.width_mm;
1043         out_resp->mm_height = connector->display_info.height_mm;
1044         out_resp->subpixel = connector->display_info.subpixel_order;
1045         out_resp->connection = connector->status;
1046         if (connector->encoder)
1047                 out_resp->encoder = connector->encoder->base.id;
1048         else
1049                 out_resp->encoder = 0;
1050
1051         /* this ioctl is called twice, once to determine how much space is needed, and the 2nd time to fill it */
1052         if ((out_resp->count_modes >= mode_count) && mode_count) {
1053                 copied = 0;
1054                 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1055                 list_for_each_entry(mode, &connector->modes, head) {
1056                         drm_crtc_convert_to_umode(&u_mode, mode);
1057                         if (copy_to_user(mode_ptr + copied,
1058                                          &u_mode, sizeof(u_mode))) {
1059                                 ret = -EFAULT;
1060                                 goto out;
1061                         }
1062                         copied++;
1063                 }
1064         }
1065         out_resp->count_modes = mode_count;
1066
1067         if ((out_resp->count_props >= props_count) && props_count) {
1068                 copied = 0;
1069                 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1070                 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1071                 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1072                         if (connector->property_ids[i] != 0) {
1073                                 if (put_user(connector->property_ids[i], prop_ptr + copied)) {
1074                                         ret = -EFAULT;
1075                                         goto out;
1076                                 }
1077
1078                                 if (put_user(connector->property_values[i], prop_values + copied)) {
1079                                         ret = -EFAULT;
1080                                         goto out;
1081                                 }
1082                                 copied++;
1083                         }
1084                 }
1085         }
1086         out_resp->count_props = props_count;
1087
1088         if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1089                 copied = 0;
1090                 encoder_ptr = (uint32_t *)(unsigned long)(out_resp->encoders_ptr);
1091                 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1092                         if (connector->encoder_ids[i] != 0) {
1093                                 if (put_user(connector->encoder_ids[i], encoder_ptr + copied)) {
1094                                         ret = -EFAULT;
1095                                         goto out;
1096                                 }
1097                                 copied++;
1098                         }
1099                 }
1100         }
1101         out_resp->count_encoders = encoders_count;
1102
1103 out:
1104         mutex_unlock(&dev->mode_config.mutex);
1105         return ret;
1106 }
1107
1108 int drm_mode_getencoder(struct drm_device *dev,
1109                         void *data, struct drm_file *file_priv)
1110 {
1111         struct drm_mode_get_encoder *enc_resp = data;
1112         struct drm_mode_object *obj;
1113         struct drm_encoder *encoder;
1114         int ret = 0;
1115
1116         mutex_lock(&dev->mode_config.mutex);
1117         obj = drm_mode_object_find(dev, enc_resp->encoder_id, DRM_MODE_OBJECT_ENCODER);
1118         if (!obj) {
1119                 ret = -EINVAL;
1120                 goto out;
1121         }
1122         encoder = obj_to_encoder(obj);
1123
1124         if (encoder->crtc)
1125                 enc_resp->crtc = encoder->crtc->base.id;
1126         else
1127                 enc_resp->crtc = 0;
1128         enc_resp->generation = dev->mode_config.current_generation;
1129         enc_resp->encoder_type = encoder->encoder_type;
1130         enc_resp->encoder_id = encoder->base.id;
1131         enc_resp->crtcs = encoder->possible_crtcs;
1132         enc_resp->clones = encoder->possible_clones;
1133
1134 out:
1135         mutex_unlock(&dev->mode_config.mutex);
1136         return ret;
1137 }
1138
1139 /**
1140  * drm_mode_setcrtc - set CRTC configuration
1141  * @inode: inode from the ioctl
1142  * @filp: file * from the ioctl
1143  * @cmd: cmd from ioctl
1144  * @arg: arg from ioctl
1145  *
1146  * LOCKING:
1147  * Caller? (FIXME)
1148  *
1149  * Build a new CRTC configuration based on user request.
1150  *
1151  * Called by the user via ioctl.
1152  *
1153  * RETURNS:
1154  * Zero on success, errno on failure.
1155  */
1156 int drm_mode_setcrtc(struct drm_device *dev,
1157                      void *data, struct drm_file *file_priv)
1158 {
1159         struct drm_mode_crtc *crtc_req = data;
1160         struct drm_mode_object *obj;
1161         struct drm_crtc *crtc, *crtcfb;
1162         struct drm_connector **connector_set = NULL, *connector;
1163         struct drm_framebuffer *fb = NULL;
1164         struct drm_display_mode *mode = NULL;
1165         struct drm_mode_set set;
1166         uint32_t __user *set_connectors_ptr;
1167         int ret = 0;
1168         int i;
1169
1170         mutex_lock(&dev->mode_config.mutex);
1171         obj = drm_mode_object_find(dev, crtc_req->crtc_id, DRM_MODE_OBJECT_CRTC);
1172         if (!obj) {
1173                 DRM_DEBUG("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1174                 ret = -EINVAL;
1175                 goto out;
1176         }
1177         crtc = obj_to_crtc(obj);
1178
1179         if (crtc_req->mode_valid) {
1180                 /* If we have a mode we need a framebuffer. */
1181                 /* If we pass -1, set the mode with the currently bound fb */
1182                 if (crtc_req->fb_id == -1) {
1183                         list_for_each_entry(crtcfb, &dev->mode_config.crtc_list, head) {
1184                                 if (crtcfb == crtc) {
1185                                         DRM_DEBUG("Using current fb for setmode\n");
1186                                         fb = crtc->fb;
1187                                 }
1188                         }
1189                 } else {
1190                         obj = drm_mode_object_find(dev, crtc_req->fb_id, DRM_MODE_OBJECT_FB);
1191                         if (!obj) {
1192                                 DRM_DEBUG("Unknown FB ID%d\n", crtc_req->fb_id);
1193                                 ret = -EINVAL;
1194                                 goto out;
1195                         }
1196                         fb = obj_to_fb(obj);
1197                 }
1198
1199                 mode = drm_mode_create(dev);
1200                 drm_crtc_convert_umode(mode, &crtc_req->mode);
1201                 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1202         }
1203
1204         if (crtc_req->count_connectors == 0 && mode) {
1205                 DRM_DEBUG("Count connectors is 0 but mode set\n");
1206                 ret = -EINVAL;
1207                 goto out;
1208         }
1209
1210         if (crtc_req->count_connectors > 0 && !mode && !fb) {
1211                 DRM_DEBUG("Count connectors is %d but no mode or fb set\n", crtc_req->count_connectors);
1212                 ret = -EINVAL;
1213                 goto out;
1214         }
1215
1216         if (crtc_req->count_connectors > 0) {
1217                 u32 out_id;
1218                 /* Maybe we should check that count_connectors is a sensible value. */
1219                 connector_set = kmalloc(crtc_req->count_connectors *
1220                                      sizeof(struct drm_connector *), GFP_KERNEL);
1221                 if (!connector_set) {
1222                         ret = -ENOMEM;
1223                         goto out;
1224                 }
1225
1226                 for (i = 0; i < crtc_req->count_connectors; i++) {
1227                         set_connectors_ptr = (uint32_t *)(unsigned long)crtc_req->set_connectors_ptr;
1228                         if (get_user(out_id, &set_connectors_ptr[i])) {
1229                                 ret = -EFAULT;
1230                                 goto out;
1231                         }
1232
1233                         obj = drm_mode_object_find(dev, out_id, DRM_MODE_OBJECT_CONNECTOR);
1234                         if (!obj) {
1235                                 DRM_DEBUG("Connector id %d unknown\n", out_id);
1236                                 ret = -EINVAL;
1237                                 goto out;
1238                         }
1239                         connector = obj_to_connector(obj);
1240
1241                         connector_set[i] = connector;
1242                 }
1243         }
1244
1245         set.crtc = crtc;
1246         set.x = crtc_req->x;
1247         set.y = crtc_req->y;
1248         set.mode = mode;
1249         set.connectors = connector_set;
1250         set.num_connectors = crtc_req->count_connectors;
1251         set.fb =fb;
1252         ret = crtc->funcs->set_config(&set);
1253
1254 out:
1255         kfree(connector_set);
1256         mutex_unlock(&dev->mode_config.mutex);
1257         return ret;
1258 }
1259
1260 int drm_mode_cursor_ioctl(struct drm_device *dev,
1261                         void *data, struct drm_file *file_priv)
1262 {
1263         struct drm_mode_cursor *req = data;
1264         struct drm_mode_object *obj;
1265         struct drm_crtc *crtc;
1266         int ret = 0;
1267
1268         DRM_DEBUG("\n");
1269
1270         if (!req->flags) {
1271                 DRM_ERROR("no operation set\n");
1272                 return -EINVAL;
1273         }
1274
1275         mutex_lock(&dev->mode_config.mutex);
1276         obj = drm_mode_object_find(dev, req->crtc, DRM_MODE_OBJECT_CRTC);
1277         if (!obj) {
1278                 DRM_DEBUG("Unknown CRTC ID %d\n", req->crtc);
1279                 ret = -EINVAL;
1280                 goto out;
1281         }
1282         crtc = obj_to_crtc(obj);
1283
1284         if (req->flags & DRM_MODE_CURSOR_BO) {
1285                 /* Turn of the cursor if handle is 0 */
1286                 if (crtc->funcs->cursor_set) {
1287                         ret = crtc->funcs->cursor_set(crtc, req->handle, req->width, req->height);
1288                 } else {
1289                         DRM_ERROR("crtc does not support cursor\n");
1290                         ret = -EFAULT;
1291                         goto out;
1292                 }
1293         }
1294
1295         if (req->flags & DRM_MODE_CURSOR_MOVE) {
1296                 if (crtc->funcs->cursor_move) {
1297                         ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1298                 } else {
1299                         DRM_ERROR("crtc does not support cursor\n");
1300                         ret = -EFAULT;
1301                         goto out;
1302                 }
1303         }
1304 out:
1305         mutex_unlock(&dev->mode_config.mutex);
1306         return ret;
1307 }
1308
1309 /**
1310  * drm_mode_addfb - add an FB to the graphics configuration
1311  * @inode: inode from the ioctl
1312  * @filp: file * from the ioctl
1313  * @cmd: cmd from ioctl
1314  * @arg: arg from ioctl
1315  *
1316  * LOCKING:
1317  * Takes mode config lock.
1318  *
1319  * Add a new FB to the specified CRTC, given a user request.
1320  *
1321  * Called by the user via ioctl.
1322  *
1323  * RETURNS:
1324  * Zero on success, errno on failure.
1325  */
1326 int drm_mode_addfb(struct drm_device *dev,
1327                    void *data, struct drm_file *file_priv)
1328 {
1329         struct drm_mode_fb_cmd *r = data;
1330         struct drm_mode_config *config = &dev->mode_config;
1331         struct drm_framebuffer *fb;
1332         int ret = 0;
1333
1334         if ((config->min_width > r->width) || (r->width > config->max_width)) {
1335                 DRM_ERROR("mode new framebuffer width not within limits\n");
1336                 return -EINVAL;
1337         }
1338         if ((config->min_height > r->height) || (r->height > config->max_height)) {
1339                 DRM_ERROR("mode new framebuffer height not within limits\n");
1340                 return -EINVAL;
1341         }
1342
1343         mutex_lock(&dev->mode_config.mutex);
1344
1345         /* TODO check buffer is sufficently large */
1346         /* TODO setup destructor callback */
1347
1348         fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
1349         if (!fb) {
1350                 DRM_ERROR("could not create framebuffer\n");
1351                 ret = -EINVAL;
1352                 goto out;
1353         }
1354
1355         r->buffer_id = fb->base.id;
1356         list_add(&fb->filp_head, &file_priv->fbs);
1357
1358 out:
1359         mutex_unlock(&dev->mode_config.mutex);
1360         return ret;
1361 }
1362
1363 /**
1364  * drm_mode_rmfb - remove an FB from the configuration
1365  * @inode: inode from the ioctl
1366  * @filp: file * from the ioctl
1367  * @cmd: cmd from ioctl
1368  * @arg: arg from ioctl
1369  *
1370  * LOCKING:
1371  * Takes mode config lock.
1372  *
1373  * Remove the FB specified by the user.
1374  *
1375  * Called by the user via ioctl.
1376  *
1377  * RETURNS:
1378  * Zero on success, errno on failure.
1379  */
1380 int drm_mode_rmfb(struct drm_device *dev,
1381                    void *data, struct drm_file *file_priv)
1382 {
1383         struct drm_mode_object *obj;
1384         struct drm_framebuffer *fb = NULL;
1385         struct drm_framebuffer *fbl = NULL;
1386         uint32_t *id = data;
1387         int ret = 0;
1388         int found = 0;
1389
1390         mutex_lock(&dev->mode_config.mutex);
1391         obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
1392         /* TODO check that we realy get a framebuffer back. */
1393         if (!obj) {
1394                 DRM_ERROR("mode invalid framebuffer id\n");
1395                 ret = -EINVAL;
1396                 goto out;
1397         }
1398         fb = obj_to_fb(obj);
1399
1400         list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1401                 if (fb == fbl)
1402                         found = 1;
1403
1404         if (!found) {
1405                 DRM_ERROR("tried to remove a fb that we didn't own\n");
1406                 ret = -EINVAL;
1407                 goto out;
1408         }
1409
1410         /* TODO release all crtc connected to the framebuffer */
1411         /* TODO unhock the destructor from the buffer object */
1412
1413         list_del(&fb->filp_head);
1414         fb->funcs->destroy(fb);
1415
1416 out:
1417         mutex_unlock(&dev->mode_config.mutex);
1418         return ret;
1419 }
1420
1421 /**
1422  * drm_mode_getfb - get FB info
1423  * @inode: inode from the ioctl
1424  * @filp: file * from the ioctl
1425  * @cmd: cmd from ioctl
1426  * @arg: arg from ioctl
1427  *
1428  * LOCKING:
1429  * Caller? (FIXME)
1430  *
1431  * Lookup the FB given its ID and return info about it.
1432  *
1433  * Called by the user via ioctl.
1434  *
1435  * RETURNS:
1436  * Zero on success, errno on failure.
1437  */
1438 int drm_mode_getfb(struct drm_device *dev,
1439                    void *data, struct drm_file *file_priv)
1440 {
1441         struct drm_mode_fb_cmd *r = data;
1442         struct drm_mode_object *obj;
1443         struct drm_framebuffer *fb;
1444         int ret = 0;
1445
1446         mutex_lock(&dev->mode_config.mutex);
1447         obj = drm_mode_object_find(dev, r->buffer_id, DRM_MODE_OBJECT_FB);
1448         if (!obj) {
1449                 DRM_ERROR("invalid framebuffer id\n");
1450                 ret = -EINVAL;
1451                 goto out;
1452         }
1453         fb = obj_to_fb(obj);
1454
1455         r->height = fb->height;
1456         r->width = fb->width;
1457         r->depth = fb->depth;
1458         r->bpp = fb->bits_per_pixel;
1459         r->handle = fb->mm_handle;
1460         r->pitch = fb->pitch;
1461
1462 out:
1463         mutex_unlock(&dev->mode_config.mutex);
1464         return ret;
1465 }
1466
1467 /**
1468  * drm_fb_release - remove and free the FBs on this file
1469  * @filp: file * from the ioctl
1470  *
1471  * LOCKING:
1472  * Takes mode config lock.
1473  *
1474  * Destroy all the FBs associated with @filp.
1475  *
1476  * Called by the user via ioctl.
1477  *
1478  * RETURNS:
1479  * Zero on success, errno on failure.
1480  */
1481 void drm_fb_release(struct file *filp)
1482 {
1483         struct drm_file *priv = filp->private_data;
1484         struct drm_device *dev = priv->minor->dev;
1485         struct drm_framebuffer *fb, *tfb;
1486
1487         mutex_lock(&dev->mode_config.mutex);
1488         list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1489                 list_del(&fb->filp_head);
1490                 fb->funcs->destroy(fb);
1491         }
1492         mutex_unlock(&dev->mode_config.mutex);
1493 }
1494
1495 /*
1496  *
1497  */
1498
1499 static int drm_mode_attachmode(struct drm_device *dev,
1500                                struct drm_connector *connector,
1501                                struct drm_display_mode *mode)
1502 {
1503         int ret = 0;
1504
1505         list_add_tail(&mode->head, &connector->user_modes);
1506         return ret;
1507 }
1508
1509 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1510                              struct drm_display_mode *mode)
1511 {
1512         struct drm_connector *connector;
1513         int ret = 0;
1514         struct drm_display_mode *dup_mode;
1515         int need_dup = 0;
1516         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1517                 if (!connector->encoder)
1518                         break;
1519                 if (connector->encoder->crtc == crtc) {
1520                         if (need_dup)
1521                                 dup_mode = drm_mode_duplicate(dev, mode);
1522                         else
1523                                 dup_mode = mode;
1524                         ret = drm_mode_attachmode(dev, connector, dup_mode); 
1525                         if (ret)
1526                                 return ret;
1527                         need_dup = 1;
1528                 }
1529         }
1530         return 0;
1531 }
1532 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1533
1534 static int drm_mode_detachmode(struct drm_device *dev,
1535                                struct drm_connector *connector,
1536                                struct drm_display_mode *mode)
1537 {
1538         int found = 0;
1539         int ret = 0;
1540         struct drm_display_mode *match_mode, *t;
1541
1542         list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
1543                 if (drm_mode_equal(match_mode, mode)) {
1544                         list_del(&match_mode->head);
1545                         drm_mode_destroy(dev, match_mode);
1546                         found = 1;
1547                         break;
1548                 }
1549         }
1550
1551         if (!found)
1552                 ret = -EINVAL;
1553
1554         return ret;
1555 }
1556
1557 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
1558 {
1559         struct drm_connector *connector;
1560
1561         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1562                 drm_mode_detachmode(dev, connector, mode);
1563         }
1564         return 0;
1565 }
1566 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
1567
1568 /**
1569  * drm_fb_attachmode - Attach a user mode to an connector
1570  * @inode: inode from the ioctl
1571  * @filp: file * from the ioctl
1572  * @cmd: cmd from ioctl
1573  * @arg: arg from ioctl
1574  *
1575  * This attaches a user specified mode to an connector.
1576  * Called by the user via ioctl.
1577  *
1578  * RETURNS:
1579  * Zero on success, errno on failure.
1580  */
1581 int drm_mode_attachmode_ioctl(struct drm_device *dev,
1582                               void *data, struct drm_file *file_priv)
1583 {
1584         struct drm_mode_mode_cmd *mode_cmd = data;
1585         struct drm_connector *connector;
1586         struct drm_display_mode *mode;
1587         struct drm_mode_object *obj;
1588         struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1589         int ret = 0;
1590
1591         mutex_lock(&dev->mode_config.mutex);
1592
1593         obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1594         if (!obj) {
1595                 ret = -EINVAL;
1596                 goto out;
1597         }
1598         connector = obj_to_connector(obj);
1599
1600         mode = drm_mode_create(dev);
1601         if (!mode) {
1602                 ret = -ENOMEM;
1603                 goto out;
1604         }
1605
1606         drm_crtc_convert_umode(mode, umode);
1607
1608         ret = drm_mode_attachmode(dev, connector, mode);
1609 out:
1610         mutex_unlock(&dev->mode_config.mutex);
1611         return ret;
1612 }
1613
1614
1615 /**
1616  * drm_fb_detachmode - Detach a user specified mode from an connector
1617  * @inode: inode from the ioctl
1618  * @filp: file * from the ioctl
1619  * @cmd: cmd from ioctl
1620  * @arg: arg from ioctl
1621  *
1622  * Called by the user via ioctl.
1623  *
1624  * RETURNS:
1625  * Zero on success, errno on failure.
1626  */
1627 int drm_mode_detachmode_ioctl(struct drm_device *dev,
1628                               void *data, struct drm_file *file_priv)
1629 {
1630         struct drm_mode_object *obj;
1631         struct drm_mode_mode_cmd *mode_cmd = data;
1632         struct drm_connector *connector;
1633         struct drm_display_mode mode;
1634         struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1635         int ret = 0;
1636
1637         mutex_lock(&dev->mode_config.mutex);
1638
1639         obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1640         if (!obj) {
1641                 ret = -EINVAL;
1642                 goto out;
1643         }
1644         connector = obj_to_connector(obj);
1645
1646         drm_crtc_convert_umode(&mode, umode);
1647         ret = drm_mode_detachmode(dev, connector, &mode);
1648 out:           
1649         mutex_unlock(&dev->mode_config.mutex);
1650         return ret;
1651 }
1652
1653 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
1654                                          const char *name, int num_values)
1655 {
1656         struct drm_property *property = NULL;
1657
1658         property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
1659         if (!property)
1660                 return NULL;
1661
1662         if (num_values) {
1663                 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
1664                 if (!property->values)
1665                         goto fail;
1666         }
1667
1668         drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
1669         property->flags = flags;
1670         property->num_values = num_values;
1671         INIT_LIST_HEAD(&property->enum_blob_list);
1672
1673         if (name)
1674                 strncpy(property->name, name, DRM_PROP_NAME_LEN);
1675
1676         list_add_tail(&property->head, &dev->mode_config.property_list);
1677         return property;
1678 fail:
1679         kfree(property);
1680         return NULL;
1681 }
1682 EXPORT_SYMBOL(drm_property_create);
1683
1684 int drm_property_add_enum(struct drm_property *property, int index,
1685                           uint64_t value, const char *name)
1686 {
1687         struct drm_property_enum *prop_enum;
1688
1689         if (!(property->flags & DRM_MODE_PROP_ENUM))
1690                 return -EINVAL;
1691
1692         if (!list_empty(&property->enum_blob_list)) {
1693                 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
1694                         if (prop_enum->value == value) {
1695                                 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN); 
1696                                 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1697                                 return 0;
1698                         }
1699                 }
1700         }
1701
1702         prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
1703         if (!prop_enum)
1704                 return -ENOMEM;
1705
1706         strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN); 
1707         prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1708         prop_enum->value = value;
1709
1710         property->values[index] = value;
1711         list_add_tail(&prop_enum->head, &property->enum_blob_list);
1712         return 0;
1713 }
1714 EXPORT_SYMBOL(drm_property_add_enum);
1715
1716 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
1717 {
1718         struct drm_property_enum *prop_enum, *pt;
1719
1720         list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
1721                 list_del(&prop_enum->head);
1722                 kfree(prop_enum);
1723         }
1724
1725         if (property->num_values)
1726                 kfree(property->values);
1727         drm_mode_object_put(dev, &property->base);
1728         list_del(&property->head);
1729         kfree(property);
1730 }
1731 EXPORT_SYMBOL(drm_property_destroy);
1732
1733 int drm_connector_attach_property(struct drm_connector *connector,
1734                                struct drm_property *property, uint64_t init_val)
1735 {
1736         int i;
1737
1738         for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1739                 if (connector->property_ids[i] == 0) {
1740                         connector->property_ids[i] = property->base.id;
1741                         connector->property_values[i] = init_val;
1742                         break;
1743                 }
1744         }
1745
1746         if (i == DRM_CONNECTOR_MAX_PROPERTY)
1747                 return -EINVAL;
1748         return 0;
1749 }
1750 EXPORT_SYMBOL(drm_connector_attach_property);
1751
1752 int drm_connector_property_set_value(struct drm_connector *connector,
1753                                   struct drm_property *property, uint64_t value)
1754 {
1755         int i;
1756
1757         for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1758                 if (connector->property_ids[i] == property->base.id) {
1759                         connector->property_values[i] = value;
1760                         break;
1761                 }
1762         }
1763
1764         if (i == DRM_CONNECTOR_MAX_PROPERTY)
1765                 return -EINVAL;
1766         return 0;
1767 }
1768 EXPORT_SYMBOL(drm_connector_property_set_value);
1769
1770 int drm_connector_property_get_value(struct drm_connector *connector,
1771                                   struct drm_property *property, uint64_t *val)
1772 {
1773         int i;
1774
1775         for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1776                 if (connector->property_ids[i] == property->base.id) {
1777                         *val = connector->property_values[i];
1778                         break;
1779                 }
1780         }
1781
1782         if (i == DRM_CONNECTOR_MAX_PROPERTY)
1783                 return -EINVAL;
1784         return 0;
1785 }
1786 EXPORT_SYMBOL(drm_connector_property_get_value);
1787
1788 int drm_mode_getproperty_ioctl(struct drm_device *dev,
1789                                void *data, struct drm_file *file_priv)
1790 {
1791         struct drm_mode_object *obj;
1792         struct drm_mode_get_property *out_resp = data;
1793         struct drm_property *property;
1794         int enum_count = 0;
1795         int blob_count = 0;
1796         int value_count = 0;
1797         int ret = 0, i;
1798         int copied;
1799         struct drm_property_enum *prop_enum;
1800         struct drm_mode_property_enum __user *enum_ptr;
1801         struct drm_property_blob *prop_blob;
1802         uint32_t *blob_id_ptr;
1803         uint64_t __user *values_ptr;
1804         uint32_t __user *blob_length_ptr;
1805
1806         mutex_lock(&dev->mode_config.mutex);
1807         obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
1808         if (!obj) {
1809                 ret = -EINVAL;
1810                 goto done;
1811         }
1812         property = obj_to_property(obj);
1813
1814         if (property->flags & DRM_MODE_PROP_ENUM) {
1815                 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
1816                         enum_count++;
1817         } else if (property->flags & DRM_MODE_PROP_BLOB) {
1818                 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
1819                         blob_count++;
1820         }
1821
1822         value_count = property->num_values;
1823
1824         strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
1825         out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
1826         out_resp->flags = property->flags;
1827
1828         if ((out_resp->count_values >= value_count) && value_count) {
1829                 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
1830                 for (i = 0; i < value_count; i++) {
1831                         if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
1832                                 ret = -EFAULT;
1833                                 goto done;
1834                         }
1835                 }
1836         }
1837         out_resp->count_values = value_count;
1838
1839         if (property->flags & DRM_MODE_PROP_ENUM) {
1840                 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
1841                         copied = 0;
1842                         enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
1843                         list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
1844
1845                                 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
1846                                         ret = -EFAULT;
1847                                         goto done;
1848                                 }
1849
1850                                 if (copy_to_user(&enum_ptr[copied].name,
1851                                                  &prop_enum->name, DRM_PROP_NAME_LEN)) {
1852                                         ret = -EFAULT;
1853                                         goto done;
1854                                 }
1855                                 copied++;
1856                         }
1857                 }
1858                 out_resp->count_enum_blobs = enum_count;
1859         }
1860
1861         if (property->flags & DRM_MODE_PROP_BLOB) {
1862                 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
1863                         copied = 0;
1864                         blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
1865                         blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
1866
1867                         list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
1868                                 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
1869                                         ret = -EFAULT;
1870                                         goto done;
1871                                 }
1872
1873                                 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
1874                                         ret = -EFAULT;
1875                                         goto done;
1876                                 }
1877
1878                                 copied++;
1879                         }
1880                 }
1881                 out_resp->count_enum_blobs = blob_count;
1882         }
1883 done:
1884         mutex_unlock(&dev->mode_config.mutex);
1885         return ret;
1886 }
1887
1888 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
1889                                                           void *data)
1890 {
1891         struct drm_property_blob *blob;
1892
1893         if (!length || !data)
1894                 return NULL;
1895
1896         blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
1897         if (!blob)
1898                 return NULL;
1899
1900         blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
1901         blob->length = length;
1902
1903         memcpy(blob->data, data, length);
1904
1905         drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
1906
1907         list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
1908         return blob;
1909 }
1910
1911 static void drm_property_destroy_blob(struct drm_device *dev,
1912                                struct drm_property_blob *blob)
1913 {
1914         drm_mode_object_put(dev, &blob->base);
1915         list_del(&blob->head);
1916         kfree(blob);
1917 }
1918
1919 int drm_mode_getblob_ioctl(struct drm_device *dev,
1920                            void *data, struct drm_file *file_priv)
1921 {
1922         struct drm_mode_object *obj;
1923         struct drm_mode_get_blob *out_resp = data;
1924         struct drm_property_blob *blob;
1925         int ret = 0;
1926         void *blob_ptr;
1927
1928         mutex_lock(&dev->mode_config.mutex);
1929         obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_CONNECTOR);
1930         if (!obj) {
1931                 ret = -EINVAL;
1932                 goto done;
1933         }
1934         blob = obj_to_blob(obj);
1935
1936         if (out_resp->length == blob->length) {
1937                 blob_ptr = (void *)(unsigned long)out_resp->data;
1938                 if (copy_to_user(blob_ptr, blob->data, blob->length)){
1939                         ret = -EFAULT;
1940                         goto done;
1941                 }
1942         }
1943         out_resp->length = blob->length;
1944
1945 done:
1946         mutex_unlock(&dev->mode_config.mutex);
1947         return ret;
1948 }
1949
1950 int drm_mode_connector_update_edid_property(struct drm_connector *connector, struct edid *edid)
1951 {
1952         struct drm_device *dev = connector->dev;
1953         int ret = 0;
1954         if (connector->edid_blob_ptr)
1955                 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
1956
1957         connector->edid_blob_ptr = drm_property_create_blob(connector->dev, 128, edid);
1958
1959         ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, connector->edid_blob_ptr->base.id);
1960         return ret;
1961 }
1962 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
1963
1964 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
1965                                        void *data, struct drm_file *file_priv)
1966 {
1967         struct drm_mode_connector_set_property *out_resp = data;
1968         struct drm_mode_object *obj;
1969         struct drm_property *property;
1970         struct drm_connector *connector;
1971         int ret = -EINVAL;
1972         int i;
1973
1974         mutex_lock(&dev->mode_config.mutex);
1975
1976         obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1977         if (!obj) {
1978                 goto out;
1979         }
1980         connector = obj_to_connector(obj);
1981
1982         for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1983                 if (connector->property_ids[i] == out_resp->prop_id)
1984                         break;
1985         }
1986
1987         if (i == DRM_CONNECTOR_MAX_PROPERTY) {
1988                 goto out;
1989         }
1990
1991         obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
1992         if (!obj) {
1993                 goto out;
1994         }
1995         property = obj_to_property(obj);
1996
1997         if (property->flags & DRM_MODE_PROP_IMMUTABLE)
1998                 goto out;
1999
2000         if (property->flags & DRM_MODE_PROP_RANGE) {
2001                 if (out_resp->value < property->values[0])
2002                         goto out;
2003
2004                 if (out_resp->value > property->values[1])
2005                         goto out;
2006         } else {
2007                 int found = 0;
2008                 for (i = 0; i < property->num_values; i++) {
2009                         if (property->values[i] == out_resp->value) {
2010                                 found = 1;
2011                                 break;
2012                         }
2013                 }
2014                 if (!found) {
2015                         goto out;
2016                 }
2017         }
2018
2019         /* store the property value */
2020         drm_connector_property_set_value(connector, property, out_resp->value);
2021
2022         if (connector->funcs->set_property)
2023                 ret = connector->funcs->set_property(connector, property, out_resp->value);
2024
2025 out:
2026         mutex_unlock(&dev->mode_config.mutex);
2027         return ret;
2028 }
2029
2030
2031 int drm_mode_replacefb(struct drm_device *dev,
2032                        void *data, struct drm_file *file_priv)
2033 {
2034         struct drm_mode_fb_cmd *r = data;
2035         struct drm_mode_object *obj;
2036         struct drm_framebuffer *fb;
2037         int found = 0;
2038         struct drm_framebuffer *fbl = NULL;
2039         int ret = 0;
2040
2041         /* right replace the current bo attached to this fb with a new bo */
2042         mutex_lock(&dev->mode_config.mutex);
2043         obj = drm_mode_object_find(dev, r->buffer_id, DRM_MODE_OBJECT_FB);
2044         if (!obj) {
2045                 ret = -EINVAL;
2046                 goto out;
2047         }
2048         fb = obj_to_fb(obj);
2049
2050         list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2051                 if (fb == fbl)
2052                         found = 1;
2053
2054         if (!found) {
2055                 DRM_ERROR("tried to replace an fb we didn't own\n");
2056                 ret = -EINVAL;
2057                 goto out;
2058         }
2059
2060         if (dev->mode_config.funcs->resize_fb)
2061                 ret = dev->mode_config.funcs->resize_fb(dev, file_priv, fb, r);
2062         else
2063                 ret = -EINVAL;
2064 out:
2065         mutex_unlock(&dev->mode_config.mutex);
2066         return ret;
2067
2068 }
2069
2070 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2071                                       struct drm_encoder *encoder)
2072 {
2073         int i;
2074
2075         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2076                 if (connector->encoder_ids[i] == 0) {
2077                         connector->encoder_ids[i] = encoder->base.id;
2078                         return 0;
2079                 }
2080         }
2081         return -ENOMEM;
2082 }
2083 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
2084
2085 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
2086                                     struct drm_encoder *encoder)
2087 {
2088         int i;
2089         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2090                 if (connector->encoder_ids[i] == encoder->base.id) {
2091                         connector->encoder_ids[i] = 0;
2092                         if (connector->encoder == encoder)
2093                                 connector->encoder = NULL;
2094                         break;
2095                 }
2096         }
2097 }
2098 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
2099
2100 bool drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
2101                                   int gamma_size)
2102 {
2103         crtc->gamma_size = gamma_size;
2104
2105         crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
2106         if (!crtc->gamma_store) {
2107                 crtc->gamma_size = 0;
2108                 return false;
2109         }
2110
2111         return true;
2112 }
2113 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
2114
2115 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
2116                              void *data, struct drm_file *file_priv)
2117 {
2118         struct drm_mode_crtc_lut *crtc_lut = data;
2119         struct drm_mode_object *obj;
2120         struct drm_crtc *crtc;
2121         void *r_base, *g_base, *b_base;
2122         int size;
2123         int ret = 0;
2124
2125         mutex_lock(&dev->mode_config.mutex);
2126         obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2127         if (!obj) {
2128                 ret = -EINVAL;
2129                 goto out;
2130         }
2131         crtc = obj_to_crtc(obj);
2132
2133         /* memcpy into gamma store */
2134         if (crtc_lut->gamma_size != crtc->gamma_size) {
2135                 ret = -EINVAL;
2136                 goto out;
2137         }
2138
2139         size = crtc_lut->gamma_size * (sizeof(uint16_t));
2140         r_base = crtc->gamma_store;
2141         if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
2142                 ret = -EFAULT;
2143                 goto out;
2144         }
2145
2146         g_base = r_base + size;
2147         if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
2148                 ret = -EFAULT;
2149                 goto out;
2150         }
2151
2152         b_base = g_base + size;
2153         if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
2154                 ret = -EFAULT;
2155                 goto out;
2156         }
2157
2158         crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
2159
2160 out:
2161         mutex_unlock(&dev->mode_config.mutex);
2162         return ret;
2163
2164 }
2165
2166 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
2167                              void *data, struct drm_file *file_priv)
2168 {
2169         struct drm_mode_crtc_lut *crtc_lut = data;
2170         struct drm_mode_object *obj;
2171         struct drm_crtc *crtc;
2172         void *r_base, *g_base, *b_base;
2173         int size;
2174         int ret = 0;
2175
2176         mutex_lock(&dev->mode_config.mutex);
2177         obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2178         if (!obj) {
2179                 ret = -EINVAL;
2180                 goto out;
2181         }
2182         crtc = obj_to_crtc(obj);
2183
2184         /* memcpy into gamma store */
2185         if (crtc_lut->gamma_size != crtc->gamma_size) {
2186                 ret = -EINVAL;
2187                 goto out;
2188         }
2189
2190         size = crtc_lut->gamma_size * (sizeof(uint16_t));
2191         r_base = crtc->gamma_store;
2192         if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
2193                 ret = -EFAULT;
2194                 goto out;
2195         }
2196
2197         g_base = r_base + size;
2198         if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
2199                 ret = -EFAULT;
2200                 goto out;
2201         }
2202
2203         b_base = g_base + size;
2204         if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
2205                 ret = -EFAULT;
2206                 goto out;
2207         }
2208 out:
2209         mutex_unlock(&dev->mode_config.mutex);
2210         return ret;
2211 }