OSDN Git Service

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