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