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