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