NV50: Initial import of kernel modesetting.
[profile/ivi/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
905         
906         DRM_DEBUG("Counted %d %d %d\n", card_res->count_crtcs,
907                   card_res->count_connectors, card_res->count_encoders);
908
909 out:    
910         mutex_unlock(&dev->mode_config.mutex);
911         return ret;
912 }
913
914 /**
915  * drm_mode_getcrtc - get CRTC configuration
916  * @inode: inode from the ioctl
917  * @filp: file * from the ioctl
918  * @cmd: cmd from ioctl
919  * @arg: arg from ioctl
920  *
921  * LOCKING:
922  * Caller? (FIXME)
923  *
924  * Construct a CRTC configuration structure to return to the user.
925  *
926  * Called by the user via ioctl.
927  *
928  * RETURNS:
929  * Zero on success, errno on failure.
930  */
931 int drm_mode_getcrtc(struct drm_device *dev,
932                      void *data, struct drm_file *file_priv)
933 {
934         struct drm_mode_crtc *crtc_resp = data;
935         struct drm_crtc *crtc;
936         struct drm_mode_object *obj;
937         int ret = 0;
938
939         mutex_lock(&dev->mode_config.mutex);
940
941         obj = drm_mode_object_find(dev, crtc_resp->crtc_id, DRM_MODE_OBJECT_CRTC);
942         if (!obj) {
943                 ret = -EINVAL;
944                 goto out;
945         }
946         crtc = obj_to_crtc(obj);
947
948         crtc_resp->x = crtc->x;
949         crtc_resp->y = crtc->y;
950         crtc_resp->gamma_size = crtc->gamma_size;
951         crtc_resp->generation = dev->mode_config.current_generation;
952         if (crtc->fb)
953                 crtc_resp->fb_id = crtc->fb->base.id;
954         else
955                 crtc_resp->fb_id = 0;
956
957         crtc_resp->connectors = 0;
958         if (crtc->enabled) {
959
960                 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
961                 crtc_resp->mode_valid = 1;
962                 
963         } else {
964                 crtc_resp->mode_valid = 0;
965         }
966
967 out:
968         mutex_unlock(&dev->mode_config.mutex);
969         return ret;
970 }
971
972 /**
973  * drm_mode_getconnector - get connector configuration
974  * @inode: inode from the ioctl
975  * @filp: file * from the ioctl
976  * @cmd: cmd from ioctl
977  * @arg: arg from ioctl
978  *
979  * LOCKING:
980  * Caller? (FIXME)
981  *
982  * Construct a connector configuration structure to return to the user.
983  *
984  * Called by the user via ioctl.
985  *
986  * RETURNS:
987  * Zero on success, errno on failure.
988  */
989 int drm_mode_getconnector(struct drm_device *dev,
990                        void *data, struct drm_file *file_priv)
991 {
992         struct drm_mode_get_connector *out_resp = data;
993         struct drm_mode_object *obj;
994         struct drm_connector *connector;
995         struct drm_display_mode *mode;
996         int mode_count = 0;
997         int props_count = 0;
998         int encoders_count = 0;
999         int ret = 0;
1000         int copied = 0;
1001         int i;
1002         struct drm_mode_modeinfo u_mode;
1003         struct drm_mode_modeinfo __user *mode_ptr;
1004         uint32_t __user *prop_ptr;
1005         uint64_t __user *prop_values;
1006         uint32_t __user *encoder_ptr;
1007
1008         memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1009
1010         DRM_DEBUG("connector id %d:\n", out_resp->connector);
1011
1012         mutex_lock(&dev->mode_config.mutex);
1013
1014         obj = drm_mode_object_find(dev, out_resp->connector, DRM_MODE_OBJECT_CONNECTOR);
1015         if (!obj) {
1016                 ret = -EINVAL;
1017                 goto out;
1018         }
1019         connector = obj_to_connector(obj);
1020
1021         list_for_each_entry(mode, &connector->modes, head)
1022                 mode_count++;
1023         
1024         for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1025                 if (connector->property_ids[i] != 0) {
1026                         props_count++;
1027                 }
1028         }
1029
1030         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1031                 if (connector->encoder_ids[i] != 0) {
1032                         encoders_count++;
1033                 }
1034         }
1035
1036         if (out_resp->count_modes == 0) {
1037                 connector->funcs->fill_modes(connector, dev->mode_config.max_width, dev->mode_config.max_height);
1038         }
1039
1040         out_resp->generation = dev->mode_config.current_generation;
1041         out_resp->connector_type = connector->connector_type;
1042         out_resp->connector_type_id = connector->connector_type_id;
1043         out_resp->mm_width = connector->display_info.width_mm;
1044         out_resp->mm_height = connector->display_info.height_mm;
1045         out_resp->subpixel = connector->display_info.subpixel_order;
1046         out_resp->connection = connector->status;
1047         if (connector->encoder)
1048                 out_resp->encoder = connector->encoder->base.id;
1049         else
1050                 out_resp->encoder = 0;
1051
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         }
1066         out_resp->count_modes = mode_count;
1067
1068         if ((out_resp->count_props >= props_count) && props_count) {
1069                 copied = 0;
1070                 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1071                 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1072                 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1073                         if (connector->property_ids[i] != 0) {
1074                                 if (put_user(connector->property_ids[i], prop_ptr + copied)) {
1075                                         ret = -EFAULT;
1076                                         goto out;
1077                                 }
1078
1079                                 if (put_user(connector->property_values[i], prop_values + copied)) {
1080                                         ret = -EFAULT;
1081                                         goto out;
1082                                 }
1083                                 copied++;
1084                         }
1085                 }
1086         }
1087         out_resp->count_props = props_count;
1088
1089         if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1090                 copied = 0;
1091                 encoder_ptr = (uint32_t *)(unsigned long)(out_resp->encoders_ptr);
1092                 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1093                         if (connector->encoder_ids[i] != 0) {
1094                                 if (put_user(connector->encoder_ids[i], encoder_ptr + copied)) {
1095                                         ret = -EFAULT;
1096                                         goto out;
1097                                 }
1098                                 copied++;
1099                         }
1100                 }
1101         }
1102         out_resp->count_encoders = encoders_count;
1103
1104 out:
1105         mutex_unlock(&dev->mode_config.mutex);
1106         return ret;
1107 }
1108
1109 int drm_mode_getencoder(struct drm_device *dev,
1110                         void *data, struct drm_file *file_priv)
1111 {
1112         struct drm_mode_get_encoder *enc_resp = data;
1113         struct drm_mode_object *obj;
1114         struct drm_encoder *encoder;
1115         int ret = 0;
1116         
1117         mutex_lock(&dev->mode_config.mutex);
1118         obj = drm_mode_object_find(dev, enc_resp->encoder_id, DRM_MODE_OBJECT_ENCODER);
1119         if (!obj) {
1120                 ret = -EINVAL;
1121                 goto out;
1122         }
1123         encoder = obj_to_encoder(obj);
1124
1125         if (encoder->crtc)
1126                 enc_resp->crtc = encoder->crtc->base.id;
1127         else
1128                 enc_resp->crtc = 0;
1129         enc_resp->generation = dev->mode_config.current_generation;
1130         enc_resp->encoder_type = encoder->encoder_type;
1131         enc_resp->encoder_id = encoder->base.id;
1132         enc_resp->crtcs = encoder->possible_crtcs;
1133         enc_resp->clones = encoder->possible_clones;
1134         
1135 out:
1136         mutex_unlock(&dev->mode_config.mutex);
1137         return ret;
1138 }
1139
1140 /**
1141  * drm_mode_setcrtc - set CRTC configuration
1142  * @inode: inode from the ioctl
1143  * @filp: file * from the ioctl
1144  * @cmd: cmd from ioctl
1145  * @arg: arg from ioctl
1146  *
1147  * LOCKING:
1148  * Caller? (FIXME)
1149  *
1150  * Build a new CRTC configuration based on user request.
1151  *
1152  * Called by the user via ioctl.
1153  *
1154  * RETURNS:
1155  * Zero on success, errno on failure.
1156  */
1157 int drm_mode_setcrtc(struct drm_device *dev,
1158                      void *data, struct drm_file *file_priv)
1159 {
1160         struct drm_mode_crtc *crtc_req = data;
1161         struct drm_mode_object *obj;
1162         struct drm_crtc *crtc, *crtcfb;
1163         struct drm_connector **connector_set = NULL, *connector;
1164         struct drm_framebuffer *fb = NULL;
1165         struct drm_display_mode *mode = NULL;
1166         struct drm_mode_set set;
1167         uint32_t __user *set_connectors_ptr;
1168         int ret = 0;
1169         int i;
1170
1171         mutex_lock(&dev->mode_config.mutex);
1172         obj = drm_mode_object_find(dev, crtc_req->crtc_id, DRM_MODE_OBJECT_CRTC);
1173         if (!obj) {
1174                 DRM_DEBUG("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1175                 ret = -EINVAL;
1176                 goto out;
1177         }
1178         crtc = obj_to_crtc(obj);
1179         
1180         if (crtc_req->mode_valid) {
1181                 /* If we have a mode we need a framebuffer. */
1182                 /* If we pass -1, set the mode with the currently bound fb */
1183                 if (crtc_req->fb_id == -1) {
1184                         list_for_each_entry(crtcfb, &dev->mode_config.crtc_list, head) {
1185                                 if (crtcfb == crtc) {
1186                                         DRM_DEBUG("Using current fb for setmode\n");
1187                                         fb = crtc->fb;          
1188                                 }
1189                         }
1190                 } else {
1191                         obj = drm_mode_object_find(dev, crtc_req->fb_id, DRM_MODE_OBJECT_FB);
1192                         if (!obj) {
1193                                 DRM_DEBUG("Unknown FB ID%d\n", crtc_req->fb_id);
1194                                 ret = -EINVAL;
1195                                 goto out;
1196                         }
1197                         fb = obj_to_fb(obj);
1198                 }
1199
1200                 mode = drm_mode_create(dev);
1201                 drm_crtc_convert_umode(mode, &crtc_req->mode);
1202                 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1203         }
1204
1205         if (crtc_req->count_connectors == 0 && mode) {
1206                 DRM_DEBUG("Count connectors is 0 but mode set\n");
1207                 ret = -EINVAL;
1208                 goto out;
1209         }
1210
1211         if (crtc_req->count_connectors > 0 && !mode && !fb) {
1212                 DRM_DEBUG("Count connectors is %d but no mode or fb set\n", crtc_req->count_connectors);
1213                 ret = -EINVAL;
1214                 goto out;
1215         }
1216
1217         if (crtc_req->count_connectors > 0) {
1218                 u32 out_id;
1219                 /* Maybe we should check that count_connectors is a sensible value. */
1220                 connector_set = kmalloc(crtc_req->count_connectors *
1221                                      sizeof(struct drm_connector *), GFP_KERNEL);
1222                 if (!connector_set) {
1223                         ret = -ENOMEM;
1224                         goto out;
1225                 }
1226
1227                 for (i = 0; i < crtc_req->count_connectors; i++) {
1228                         set_connectors_ptr = (uint32_t *)(unsigned long)crtc_req->set_connectors_ptr;
1229                         if (get_user(out_id, &set_connectors_ptr[i])) {
1230                                 ret = -EFAULT;
1231                                 goto out;
1232                         }
1233
1234                         obj = drm_mode_object_find(dev, out_id, DRM_MODE_OBJECT_CONNECTOR);
1235                         if (!obj) {
1236                                 DRM_DEBUG("Connector id %d unknown\n", out_id);
1237                                 ret = -EINVAL;
1238                                 goto out;
1239                         }
1240                         connector = obj_to_connector(obj);
1241
1242                         connector_set[i] = connector;
1243                 }
1244         }
1245
1246         set.crtc = crtc;
1247         set.x = crtc_req->x;
1248         set.y = crtc_req->y;
1249         set.mode = mode;
1250         set.connectors = connector_set;
1251         set.num_connectors = crtc_req->count_connectors;
1252         set.fb =fb;
1253         ret = crtc->funcs->set_config(&set);
1254
1255 out:
1256         kfree(connector_set);
1257         mutex_unlock(&dev->mode_config.mutex);
1258         return ret;
1259 }
1260
1261 int drm_mode_cursor_ioctl(struct drm_device *dev,
1262                         void *data, struct drm_file *file_priv)
1263 {
1264         struct drm_mode_cursor *req = data;
1265         struct drm_mode_object *obj;
1266         struct drm_crtc *crtc;
1267         int ret = 0;
1268
1269         DRM_DEBUG("\n");
1270
1271         if (!req->flags) {
1272                 DRM_ERROR("no operation set\n");
1273                 return -EINVAL;
1274         }
1275
1276         mutex_lock(&dev->mode_config.mutex);
1277         obj = drm_mode_object_find(dev, req->crtc, DRM_MODE_OBJECT_CRTC);
1278         if (!obj) {
1279                 DRM_DEBUG("Unknown CRTC ID %d\n", req->crtc);
1280                 ret = -EINVAL;
1281                 goto out;
1282         }
1283         crtc = obj_to_crtc(obj);
1284
1285         if (req->flags & DRM_MODE_CURSOR_BO) {
1286                 /* Turn of the cursor if handle is 0 */
1287                 if (crtc->funcs->cursor_set) {
1288                         ret = crtc->funcs->cursor_set(crtc, req->handle, req->width, req->height);
1289                 } else {
1290                         DRM_ERROR("crtc does not support cursor\n");
1291                         ret = -EFAULT;
1292                         goto out;
1293                 }
1294         }
1295
1296         if (req->flags & DRM_MODE_CURSOR_MOVE) {
1297                 if (crtc->funcs->cursor_move) {
1298                         ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1299                 } else {
1300                         DRM_ERROR("crtc does not support cursor\n");
1301                         ret = -EFAULT;
1302                         goto out;
1303                 }
1304         }
1305 out:
1306         mutex_unlock(&dev->mode_config.mutex);
1307         return ret;
1308 }
1309
1310 /**
1311  * drm_mode_addfb - add an FB to the graphics configuration
1312  * @inode: inode from the ioctl
1313  * @filp: file * from the ioctl
1314  * @cmd: cmd from ioctl
1315  * @arg: arg from ioctl
1316  *
1317  * LOCKING:
1318  * Takes mode config lock.
1319  *
1320  * Add a new FB to the specified CRTC, given a user request.
1321  *
1322  * Called by the user via ioctl.
1323  *
1324  * RETURNS:
1325  * Zero on success, errno on failure.
1326  */
1327 int drm_mode_addfb(struct drm_device *dev,
1328                    void *data, struct drm_file *file_priv)
1329 {
1330         struct drm_mode_fb_cmd *r = data;
1331         struct drm_mode_config *config = &dev->mode_config;
1332         struct drm_framebuffer *fb;
1333         int ret = 0;
1334
1335         if ((config->min_width > r->width) || (r->width > config->max_width)) {
1336                 DRM_ERROR("mode new framebuffer width not within limits\n");
1337                 return -EINVAL;
1338         }
1339         if ((config->min_height > r->height) || (r->height > config->max_height)) {
1340                 DRM_ERROR("mode new framebuffer height not within limits\n");
1341                 return -EINVAL;
1342         }
1343
1344         mutex_lock(&dev->mode_config.mutex);
1345
1346         /* TODO check buffer is sufficently large */
1347         /* TODO setup destructor callback */
1348
1349         fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
1350         if (!fb) {
1351                 DRM_ERROR("could not create framebuffer\n");
1352                 ret = -EINVAL;
1353                 goto out;
1354         }
1355
1356         r->buffer_id = fb->base.id;
1357         list_add(&fb->filp_head, &file_priv->fbs);
1358
1359 out:
1360         mutex_unlock(&dev->mode_config.mutex);
1361         return ret;
1362 }
1363
1364 /**
1365  * drm_mode_rmfb - remove an FB from the configuration
1366  * @inode: inode from the ioctl
1367  * @filp: file * from the ioctl
1368  * @cmd: cmd from ioctl
1369  * @arg: arg from ioctl
1370  *
1371  * LOCKING:
1372  * Takes mode config lock.
1373  *
1374  * Remove the FB specified by the user.
1375  *
1376  * Called by the user via ioctl.
1377  *
1378  * RETURNS:
1379  * Zero on success, errno on failure.
1380  */
1381 int drm_mode_rmfb(struct drm_device *dev,
1382                    void *data, struct drm_file *file_priv)
1383 {
1384         struct drm_mode_object *obj;
1385         struct drm_framebuffer *fb = NULL;
1386         struct drm_framebuffer *fbl = NULL;
1387         uint32_t *id = data;
1388         int ret = 0;
1389         int found = 0;
1390
1391         mutex_lock(&dev->mode_config.mutex);
1392         obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
1393         /* TODO check that we realy get a framebuffer back. */
1394         if (!obj) {
1395                 DRM_ERROR("mode invalid framebuffer id\n");
1396                 ret = -EINVAL;
1397                 goto out;
1398         }
1399         fb = obj_to_fb(obj);
1400
1401         list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1402                 if (fb == fbl)
1403                         found = 1;
1404
1405         if (!found) {
1406                 DRM_ERROR("tried to remove a fb that we didn't own\n");
1407                 ret = -EINVAL;
1408                 goto out;
1409         }
1410
1411         /* TODO release all crtc connected to the framebuffer */
1412         /* TODO unhock the destructor from the buffer object */
1413
1414         list_del(&fb->filp_head);
1415         fb->funcs->destroy(fb);
1416
1417 out:
1418         mutex_unlock(&dev->mode_config.mutex);
1419         return ret;
1420 }
1421
1422 /**
1423  * drm_mode_getfb - get FB info
1424  * @inode: inode from the ioctl
1425  * @filp: file * from the ioctl
1426  * @cmd: cmd from ioctl
1427  * @arg: arg from ioctl
1428  *
1429  * LOCKING:
1430  * Caller? (FIXME)
1431  *
1432  * Lookup the FB given its ID and return info about it.
1433  *
1434  * Called by the user via ioctl.
1435  *
1436  * RETURNS:
1437  * Zero on success, errno on failure.
1438  */
1439 int drm_mode_getfb(struct drm_device *dev,
1440                    void *data, struct drm_file *file_priv)
1441 {
1442         struct drm_mode_fb_cmd *r = data;
1443         struct drm_mode_object *obj;
1444         struct drm_framebuffer *fb;
1445         int ret = 0;
1446
1447         mutex_lock(&dev->mode_config.mutex);
1448         obj = drm_mode_object_find(dev, r->buffer_id, DRM_MODE_OBJECT_FB);
1449         if (!obj) {
1450                 DRM_ERROR("invalid framebuffer id\n");
1451                 ret = -EINVAL;
1452                 goto out;
1453         }
1454         fb = obj_to_fb(obj);
1455
1456         r->height = fb->height;
1457         r->width = fb->width;
1458         r->depth = fb->depth;
1459         r->bpp = fb->bits_per_pixel;
1460         r->handle = fb->mm_handle;
1461         r->pitch = fb->pitch;
1462
1463 out:
1464         mutex_unlock(&dev->mode_config.mutex);
1465         return ret;
1466 }
1467
1468 /**
1469  * drm_fb_release - remove and free the FBs on this file
1470  * @filp: file * from the ioctl
1471  *
1472  * LOCKING:
1473  * Takes mode config lock.
1474  *
1475  * Destroy all the FBs associated with @filp.
1476  *
1477  * Called by the user via ioctl.
1478  *
1479  * RETURNS:
1480  * Zero on success, errno on failure.
1481  */
1482 void drm_fb_release(struct file *filp)
1483 {
1484         struct drm_file *priv = filp->private_data;
1485         struct drm_device *dev = priv->minor->dev;
1486         struct drm_framebuffer *fb, *tfb;
1487
1488         mutex_lock(&dev->mode_config.mutex);
1489         list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1490                 list_del(&fb->filp_head);
1491                 fb->funcs->destroy(fb);
1492         }
1493         mutex_unlock(&dev->mode_config.mutex);
1494 }
1495
1496 /*
1497  *
1498  */
1499
1500 static int drm_mode_attachmode(struct drm_device *dev,
1501                                struct drm_connector *connector,
1502                                struct drm_display_mode *mode)
1503 {
1504         int ret = 0;
1505
1506         list_add_tail(&mode->head, &connector->user_modes);
1507         return ret;
1508 }
1509
1510 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1511                              struct drm_display_mode *mode)
1512 {
1513         struct drm_connector *connector;
1514         int ret = 0;
1515         struct drm_display_mode *dup_mode;
1516         int need_dup = 0;
1517         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1518                 if (!connector->encoder)
1519                         break;
1520                 if (connector->encoder->crtc == crtc) {
1521                         if (need_dup)
1522                                 dup_mode = drm_mode_duplicate(dev, mode);
1523                         else
1524                                 dup_mode = mode;
1525                         ret = drm_mode_attachmode(dev, connector, dup_mode); 
1526                         if (ret)
1527                                 return ret;
1528                         need_dup = 1;
1529                 }
1530         }
1531         return 0;
1532 }
1533 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1534
1535 static int drm_mode_detachmode(struct drm_device *dev,
1536                                struct drm_connector *connector,
1537                                struct drm_display_mode *mode)
1538 {
1539         int found = 0;
1540         int ret = 0;
1541         struct drm_display_mode *match_mode, *t;
1542
1543         list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
1544                 if (drm_mode_equal(match_mode, mode)) {
1545                         list_del(&match_mode->head);
1546                         drm_mode_destroy(dev, match_mode);
1547                         found = 1;
1548                         break;
1549                 }
1550         }
1551
1552         if (!found)
1553                 ret = -EINVAL;
1554
1555         return ret;
1556 }
1557
1558 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
1559 {
1560         struct drm_connector *connector;
1561
1562         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1563                 drm_mode_detachmode(dev, connector, mode);
1564         }
1565         return 0;
1566 }
1567 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
1568
1569 /**
1570  * drm_fb_attachmode - Attach a user mode to an connector
1571  * @inode: inode from the ioctl
1572  * @filp: file * from the ioctl
1573  * @cmd: cmd from ioctl
1574  * @arg: arg from ioctl
1575  *
1576  * This attaches a user specified mode to an connector.
1577  * Called by the user via ioctl.
1578  *
1579  * RETURNS:
1580  * Zero on success, errno on failure.
1581  */
1582 int drm_mode_attachmode_ioctl(struct drm_device *dev,
1583                               void *data, struct drm_file *file_priv)
1584 {
1585         struct drm_mode_mode_cmd *mode_cmd = data;
1586         struct drm_connector *connector;
1587         struct drm_display_mode *mode;
1588         struct drm_mode_object *obj;
1589         struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1590         int ret = 0;
1591
1592         mutex_lock(&dev->mode_config.mutex);
1593
1594         obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1595         if (!obj) {
1596                 ret = -EINVAL;
1597                 goto out;
1598         }
1599         connector = obj_to_connector(obj);
1600
1601         mode = drm_mode_create(dev);
1602         if (!mode) {
1603                 ret = -ENOMEM;
1604                 goto out;
1605         }
1606         
1607         drm_crtc_convert_umode(mode, umode);
1608
1609         ret = drm_mode_attachmode(dev, connector, mode);
1610 out:
1611         mutex_unlock(&dev->mode_config.mutex);
1612         return ret;
1613 }
1614
1615
1616 /**
1617  * drm_fb_detachmode - Detach a user specified mode from an connector
1618  * @inode: inode from the ioctl
1619  * @filp: file * from the ioctl
1620  * @cmd: cmd from ioctl
1621  * @arg: arg from ioctl
1622  *
1623  * Called by the user via ioctl.
1624  *
1625  * RETURNS:
1626  * Zero on success, errno on failure.
1627  */
1628 int drm_mode_detachmode_ioctl(struct drm_device *dev,
1629                               void *data, struct drm_file *file_priv)
1630 {
1631         struct drm_mode_object *obj;
1632         struct drm_mode_mode_cmd *mode_cmd = data;
1633         struct drm_connector *connector;
1634         struct drm_display_mode mode;
1635         struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1636         int ret = 0;
1637
1638         mutex_lock(&dev->mode_config.mutex);
1639
1640         obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1641         if (!obj) {
1642                 ret = -EINVAL;
1643                 goto out;
1644         }
1645         connector = obj_to_connector(obj);
1646         
1647         drm_crtc_convert_umode(&mode, umode);
1648         ret = drm_mode_detachmode(dev, connector, &mode);
1649 out:           
1650         mutex_unlock(&dev->mode_config.mutex);
1651         return ret;
1652 }
1653
1654 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
1655                                          const char *name, int num_values)
1656 {
1657         struct drm_property *property = NULL;
1658
1659         property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
1660         if (!property)
1661                 return NULL;
1662
1663         if (num_values) {
1664                 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
1665                 if (!property->values)
1666                         goto fail;
1667         }
1668
1669         drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
1670         property->flags = flags;
1671         property->num_values = num_values;
1672         INIT_LIST_HEAD(&property->enum_blob_list);
1673
1674         if (name)
1675                 strncpy(property->name, name, DRM_PROP_NAME_LEN);
1676
1677         list_add_tail(&property->head, &dev->mode_config.property_list);
1678         return property;
1679 fail:
1680         kfree(property);
1681         return NULL;
1682 }
1683 EXPORT_SYMBOL(drm_property_create);
1684
1685 int drm_property_add_enum(struct drm_property *property, int index,
1686                           uint64_t value, const char *name)
1687 {
1688         struct drm_property_enum *prop_enum;
1689
1690         if (!(property->flags & DRM_MODE_PROP_ENUM))
1691                 return -EINVAL;
1692
1693         if (!list_empty(&property->enum_blob_list)) {
1694                 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
1695                         if (prop_enum->value == value) {
1696                                 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN); 
1697                                 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1698                                 return 0;
1699                         }
1700                 }
1701         }
1702
1703         prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
1704         if (!prop_enum)
1705                 return -ENOMEM;
1706
1707         strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN); 
1708         prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1709         prop_enum->value = value;
1710
1711         property->values[index] = value;
1712         list_add_tail(&prop_enum->head, &property->enum_blob_list);
1713         return 0;
1714 }
1715 EXPORT_SYMBOL(drm_property_add_enum);
1716
1717 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
1718 {
1719         struct drm_property_enum *prop_enum, *pt;
1720
1721         list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
1722                 list_del(&prop_enum->head);
1723                 kfree(prop_enum);
1724         }
1725
1726         if (property->num_values)
1727                 kfree(property->values);
1728         drm_mode_object_put(dev, &property->base);
1729         list_del(&property->head);
1730         kfree(property);        
1731 }
1732 EXPORT_SYMBOL(drm_property_destroy);
1733
1734 int drm_connector_attach_property(struct drm_connector *connector,
1735                                struct drm_property *property, uint64_t init_val)
1736 {
1737         int i;
1738
1739         for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1740                 if (connector->property_ids[i] == 0) {
1741                         connector->property_ids[i] = property->base.id;
1742                         connector->property_values[i] = init_val;
1743                         break;
1744                 }
1745         }
1746
1747         if (i == DRM_CONNECTOR_MAX_PROPERTY)
1748                 return -EINVAL;
1749         return 0;
1750 }
1751 EXPORT_SYMBOL(drm_connector_attach_property);
1752
1753 int drm_connector_property_set_value(struct drm_connector *connector,
1754                                   struct drm_property *property, uint64_t value)
1755 {
1756         int i;
1757
1758         for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1759                 if (connector->property_ids[i] == property->base.id) {
1760                         connector->property_values[i] = value;
1761                         break;
1762                 }
1763         }
1764
1765         if (i == DRM_CONNECTOR_MAX_PROPERTY)
1766                 return -EINVAL;
1767         return 0;
1768 }
1769 EXPORT_SYMBOL(drm_connector_property_set_value);
1770
1771 int drm_connector_property_get_value(struct drm_connector *connector,
1772                                   struct drm_property *property, uint64_t *val)
1773 {
1774         int i;
1775
1776         for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1777                 if (connector->property_ids[i] == property->base.id) {
1778                         *val = connector->property_values[i];
1779                         break;
1780                 }
1781         }
1782
1783         if (i == DRM_CONNECTOR_MAX_PROPERTY)
1784                 return -EINVAL;
1785         return 0;
1786 }
1787 EXPORT_SYMBOL(drm_connector_property_get_value);
1788
1789 int drm_mode_getproperty_ioctl(struct drm_device *dev,
1790                                void *data, struct drm_file *file_priv)
1791 {
1792         struct drm_mode_object *obj;
1793         struct drm_mode_get_property *out_resp = data;
1794         struct drm_property *property;
1795         int enum_count = 0;
1796         int blob_count = 0;
1797         int value_count = 0;
1798         int ret = 0, i;
1799         int copied;
1800         struct drm_property_enum *prop_enum;
1801         struct drm_mode_property_enum __user *enum_ptr;
1802         struct drm_property_blob *prop_blob;
1803         uint32_t *blob_id_ptr;
1804         uint64_t __user *values_ptr;
1805         uint32_t __user *blob_length_ptr;
1806
1807         mutex_lock(&dev->mode_config.mutex);
1808         obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
1809         if (!obj) {
1810                 ret = -EINVAL;
1811                 goto done;
1812         }
1813         property = obj_to_property(obj);
1814
1815         if (property->flags & DRM_MODE_PROP_ENUM) {
1816                 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
1817                         enum_count++;
1818         } else if (property->flags & DRM_MODE_PROP_BLOB) {
1819                 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
1820                         blob_count++;
1821         }
1822
1823         value_count = property->num_values;
1824
1825         strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
1826         out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
1827         out_resp->flags = property->flags;
1828
1829         if ((out_resp->count_values >= value_count) && value_count) {
1830                 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
1831                 for (i = 0; i < value_count; i++) {
1832                         if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
1833                                 ret = -EFAULT;
1834                                 goto done;
1835                         }
1836                 }
1837         }
1838         out_resp->count_values = value_count;
1839
1840         if (property->flags & DRM_MODE_PROP_ENUM) {
1841
1842                 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
1843                         copied = 0;
1844                         enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
1845                         list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
1846                                 
1847                                 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
1848                                         ret = -EFAULT;
1849                                         goto done;
1850                                 }
1851                                 
1852                                 if (copy_to_user(&enum_ptr[copied].name,
1853                                                  &prop_enum->name, DRM_PROP_NAME_LEN)) {
1854                                         ret = -EFAULT;
1855                                         goto done;
1856                                 }
1857                                 copied++;
1858                         }
1859                 }
1860                 out_resp->count_enum_blobs = enum_count;
1861         }
1862
1863         if (property->flags & DRM_MODE_PROP_BLOB) {
1864                 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
1865                         copied = 0;
1866                         blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
1867                         blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
1868                         
1869                         list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
1870                                 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
1871                                         ret = -EFAULT;
1872                                         goto done;
1873                                 }
1874                                 
1875                                 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
1876                                         ret = -EFAULT;
1877                                         goto done;
1878                                 }
1879                                 
1880                                 copied++;
1881                         }
1882                 }
1883                 out_resp->count_enum_blobs = enum_count;
1884         }
1885 done:
1886         mutex_unlock(&dev->mode_config.mutex);
1887         return ret;
1888 }
1889
1890 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
1891                                                           void *data)
1892 {
1893         struct drm_property_blob *blob;
1894
1895         if (!length || !data)
1896                 return NULL;
1897
1898         blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
1899         if (!blob)
1900                 return NULL;
1901
1902         blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
1903         blob->length = length;
1904
1905         memcpy(blob->data, data, length);
1906
1907         drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
1908         
1909         list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
1910         return blob;
1911 }
1912
1913 static void drm_property_destroy_blob(struct drm_device *dev,
1914                                struct drm_property_blob *blob)
1915 {
1916         drm_mode_object_put(dev, &blob->base);
1917         list_del(&blob->head);
1918         kfree(blob);
1919 }
1920
1921 int drm_mode_getblob_ioctl(struct drm_device *dev,
1922                            void *data, struct drm_file *file_priv)
1923 {
1924         struct drm_mode_object *obj;
1925         struct drm_mode_get_blob *out_resp = data;
1926         struct drm_property_blob *blob;
1927         int ret = 0;
1928         void *blob_ptr;
1929
1930         mutex_lock(&dev->mode_config.mutex);
1931         obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_CONNECTOR);
1932         if (!obj) {
1933                 ret = -EINVAL;
1934                 goto done;
1935         }
1936         blob = obj_to_blob(obj);
1937
1938         if (out_resp->length == blob->length) {
1939                 blob_ptr = (void *)(unsigned long)out_resp->data;
1940                 if (copy_to_user(blob_ptr, blob->data, blob->length)){
1941                         ret = -EFAULT;
1942                         goto done;
1943                 }
1944         }
1945         out_resp->length = blob->length;
1946
1947 done:
1948         mutex_unlock(&dev->mode_config.mutex);
1949         return ret;
1950 }
1951
1952 int drm_mode_connector_update_edid_property(struct drm_connector *connector, struct edid *edid)
1953 {
1954         struct drm_device *dev = connector->dev;
1955         int ret = 0;
1956         if (connector->edid_blob_ptr)
1957                 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
1958
1959         connector->edid_blob_ptr = drm_property_create_blob(connector->dev, 128, edid);
1960         
1961         ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, connector->edid_blob_ptr->base.id);
1962         return ret;
1963 }
1964 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
1965
1966 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
1967                                        void *data, struct drm_file *file_priv)
1968 {
1969         struct drm_mode_connector_set_property *out_resp = data;
1970         struct drm_mode_object *obj;
1971         struct drm_property *property;
1972         struct drm_connector *connector;
1973         int ret = -EINVAL;
1974         int i;
1975
1976         mutex_lock(&dev->mode_config.mutex);
1977
1978         obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1979         if (!obj) {
1980                 goto out;
1981         }
1982         connector = obj_to_connector(obj);
1983
1984         for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1985                 if (connector->property_ids[i] == out_resp->prop_id)
1986                         break;
1987         }
1988
1989         if (i == DRM_CONNECTOR_MAX_PROPERTY) {
1990                 goto out;
1991         }
1992         
1993         obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
1994         if (!obj) {
1995                 goto out;
1996         }
1997         property = obj_to_property(obj);
1998
1999         if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2000                 goto out;
2001
2002         if (property->flags & DRM_MODE_PROP_RANGE) {
2003                 if (out_resp->value < property->values[0])
2004                         goto out;
2005
2006                 if (out_resp->value > property->values[1])
2007                         goto out;
2008         } else {
2009                 int found = 0;
2010                 for (i = 0; i < property->num_values; i++) {
2011                         if (property->values[i] == out_resp->value) {
2012                                 found = 1;
2013                                 break;
2014                         }
2015                 }
2016                 if (!found) {
2017                         goto out;
2018                 }
2019         }
2020
2021         if (connector->funcs->set_property)
2022                 ret = connector->funcs->set_property(connector, property, out_resp->value);
2023
2024 out:
2025         mutex_unlock(&dev->mode_config.mutex);
2026         return ret;
2027 }
2028
2029
2030 int drm_mode_replacefb(struct drm_device *dev,
2031                        void *data, struct drm_file *file_priv)
2032 {
2033         struct drm_mode_fb_cmd *r = data;
2034         struct drm_mode_object *obj;
2035         struct drm_framebuffer *fb;
2036         int found = 0;
2037         struct drm_framebuffer *fbl = NULL;
2038         int ret = 0;
2039
2040         /* right replace the current bo attached to this fb with a new bo */
2041         mutex_lock(&dev->mode_config.mutex);
2042         obj = drm_mode_object_find(dev, r->buffer_id, DRM_MODE_OBJECT_FB);
2043         if (!obj) {
2044                 ret = -EINVAL;
2045                 goto out;
2046         }
2047         fb = obj_to_fb(obj);
2048
2049         list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2050                 if (fb == fbl)
2051                         found = 1;
2052
2053         if (!found) {
2054                 DRM_ERROR("tried to replace an fb we didn't own\n");
2055                 ret = -EINVAL;
2056                 goto out;
2057         }
2058
2059         if (dev->mode_config.funcs->resize_fb)
2060                 ret = dev->mode_config.funcs->resize_fb(dev, file_priv, fb, r);
2061         else
2062                 ret = -EINVAL;
2063 out:
2064         mutex_unlock(&dev->mode_config.mutex);
2065         return ret;
2066
2067 }
2068
2069 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2070                                       struct drm_encoder *encoder)
2071 {
2072         int i;
2073
2074         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2075                 if (connector->encoder_ids[i] == 0) {
2076                         connector->encoder_ids[i] = encoder->base.id;
2077                         return 0;
2078                 }
2079         }
2080         return -ENOMEM;
2081 }
2082 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
2083
2084 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
2085                                     struct drm_encoder *encoder)
2086 {
2087         int i;
2088         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2089                 if (connector->encoder_ids[i] == encoder->base.id) {
2090                         connector->encoder_ids[i] = 0;
2091                         if (connector->encoder == encoder)
2092                                 connector->encoder = NULL;
2093                         break;
2094                 }
2095         }
2096 }
2097 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
2098
2099 bool drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
2100                                   int gamma_size)
2101 {
2102         crtc->gamma_size = gamma_size;
2103
2104         crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
2105         if (!crtc->gamma_store) {
2106                 crtc->gamma_size = 0;
2107                 return false;
2108         }
2109
2110         return true;
2111 }
2112 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
2113
2114 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
2115                              void *data, struct drm_file *file_priv)
2116 {
2117         struct drm_mode_crtc_lut *crtc_lut = data;
2118         struct drm_mode_object *obj;
2119         struct drm_crtc *crtc;
2120         void *r_base, *g_base, *b_base;
2121         int size;
2122         int ret = 0;
2123
2124         mutex_lock(&dev->mode_config.mutex);
2125         obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2126         if (!obj) {
2127                 ret = -EINVAL;
2128                 goto out;
2129         }
2130         crtc = obj_to_crtc(obj);
2131         
2132         /* memcpy into gamma store */
2133         if (crtc_lut->gamma_size != crtc->gamma_size) {
2134                 ret = -EINVAL;
2135                 goto out;
2136         }
2137
2138         size = crtc_lut->gamma_size * (sizeof(uint16_t));
2139         r_base = crtc->gamma_store;
2140         if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
2141                 ret = -EFAULT;
2142                 goto out;
2143         }
2144
2145         g_base = r_base + size;
2146         if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
2147                 ret = -EFAULT;
2148                 goto out;
2149         }
2150
2151         b_base = g_base + size;
2152         if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
2153                 ret = -EFAULT;
2154                 goto out;
2155         }
2156
2157         crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
2158
2159 out:
2160         mutex_unlock(&dev->mode_config.mutex);
2161         return ret;
2162
2163 }
2164
2165 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
2166                              void *data, struct drm_file *file_priv)
2167 {
2168         struct drm_mode_crtc_lut *crtc_lut = data;
2169         struct drm_mode_object *obj;
2170         struct drm_crtc *crtc;
2171         void *r_base, *g_base, *b_base;
2172         int size;
2173         int ret = 0;
2174
2175         mutex_lock(&dev->mode_config.mutex);
2176         obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2177         if (!obj) {
2178                 ret = -EINVAL;
2179                 goto out;
2180         }
2181         crtc = obj_to_crtc(obj);
2182         
2183         /* memcpy into gamma store */
2184         if (crtc_lut->gamma_size != crtc->gamma_size) {
2185                 ret = -EINVAL;
2186                 goto out;
2187         }
2188
2189         size = crtc_lut->gamma_size * (sizeof(uint16_t));
2190         r_base = crtc->gamma_store;
2191         if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
2192                 ret = -EFAULT;
2193                 goto out;
2194         }
2195
2196         g_base = r_base + size;
2197         if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
2198                 ret = -EFAULT;
2199                 goto out;
2200         }
2201
2202         b_base = g_base + size;
2203         if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
2204                 ret = -EFAULT;
2205                 goto out;
2206         }
2207 out:
2208         mutex_unlock(&dev->mode_config.mutex);
2209         return ret;
2210 }