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