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                                                                 des_mode = modes;
951                                                                 assigned = 0;
952                                                                 goto clone;
953                                                         }
954                                                 }
955                                         }
956                                 }
957                         }
958
959 clone:
960                         /* crtc has been assigned skip it */
961                         if (assigned)
962                                 continue;
963
964                         /* Found a CRTC to attach to, do it ! */
965                         output->crtc = crtc;
966                         output->crtc->desired_mode = des_mode;
967                         output->initial_x = 0;
968                         output->initial_y = 0;
969                         DRM_DEBUG("Desired mode for CRTC %d is 0x%x:%s\n",c,des_mode->mode_id, des_mode->name);
970                         break;
971                 }
972         }
973 }
974 EXPORT_SYMBOL(drm_pick_crtcs);
975
976 /**
977  * drm_initial_config - setup a sane initial output configuration
978  * @dev: DRM device
979  * @can_grow: this configuration is growable
980  *
981  * LOCKING:
982  * Called at init time, must take mode config lock.
983  *
984  * Scan the CRTCs and outputs and try to put together an initial setup.
985  * At the moment, this is a cloned configuration across all heads with
986  * a new framebuffer object as the backing store.
987  *
988  * RETURNS:
989  * Zero if everything went ok, nonzero otherwise.
990  */
991 bool drm_initial_config(struct drm_device *dev, bool can_grow)
992 {
993         struct drm_output *output;
994         struct drm_crtc *crtc;
995         int ret = false;
996
997         mutex_lock(&dev->mode_config.mutex);
998
999         drm_crtc_probe_output_modes(dev, 2048, 2048);
1000
1001         drm_pick_crtcs(dev);
1002
1003         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1004
1005                 /* can't setup the crtc if there's no assigned mode */
1006                 if (!crtc->desired_mode)
1007                         continue;
1008
1009                 /* Now setup the fbdev for attached crtcs */
1010                 dev->driver->fb_probe(dev, crtc);
1011         }
1012
1013         /* This is a little screwy, as we've already walked the outputs 
1014          * above, but it's a little bit of magic too. There's the potential
1015          * for things not to get setup above if an existing device gets
1016          * re-assigned thus confusing the hardware. By walking the outputs
1017          * this fixes up their crtc's.
1018          */
1019         list_for_each_entry(output, &dev->mode_config.output_list, head) {
1020
1021                 /* can't setup the output if there's no assigned mode */
1022                 if (!output->crtc || !output->crtc->desired_mode)
1023                         continue;
1024
1025                 /* and needs an attached fb */
1026                 if (output->crtc->fb)
1027                         drm_crtc_set_mode(output->crtc, output->crtc->desired_mode, 0, 0);
1028         }
1029
1030         drm_disable_unused_functions(dev);
1031
1032         mutex_unlock(&dev->mode_config.mutex);
1033         return ret;
1034 }
1035 EXPORT_SYMBOL(drm_initial_config);
1036
1037 /**
1038  * drm_mode_config_cleanup - free up DRM mode_config info
1039  * @dev: DRM device
1040  *
1041  * LOCKING:
1042  * Caller must hold mode config lock.
1043  *
1044  * Free up all the outputs and CRTCs associated with this DRM device, then
1045  * free up the framebuffers and associated buffer objects.
1046  *
1047  * FIXME: cleanup any dangling user buffer objects too
1048  */
1049 void drm_mode_config_cleanup(struct drm_device *dev)
1050 {
1051         struct drm_output *output, *ot;
1052         struct drm_crtc *crtc, *ct;
1053         struct drm_framebuffer *fb, *fbt;
1054         struct drm_property *property, *pt;
1055
1056         list_for_each_entry_safe(output, ot, &dev->mode_config.output_list, head) {
1057                 drm_output_destroy(output);
1058         }
1059
1060         list_for_each_entry_safe(property, pt, &dev->mode_config.property_list, head) {
1061                 drm_property_destroy(dev, property);
1062         }
1063
1064         list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1065                 /* there should only be bo of kernel type left */
1066                 if (fb->bo->type != drm_bo_type_kernel)
1067                         drm_framebuffer_destroy(fb);
1068                 else
1069                         dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1070         }
1071
1072         list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1073                 drm_crtc_destroy(crtc);
1074         }
1075
1076 }
1077 EXPORT_SYMBOL(drm_mode_config_cleanup);
1078
1079 /**
1080  * drm_crtc_set_config - set a new config from userspace
1081  * @crtc: CRTC to setup
1082  * @crtc_info: user provided configuration
1083  * @new_mode: new mode to set
1084  * @output_set: set of outputs for the new config
1085  * @fb: new framebuffer
1086  *
1087  * LOCKING:
1088  * Caller must hold mode config lock.
1089  *
1090  * Setup a new configuration, provided by the user in @crtc_info, and enable
1091  * it.
1092  *
1093  * RETURNS:
1094  * Zero. (FIXME)
1095  */
1096 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)
1097 {
1098         struct drm_device *dev = crtc->dev;
1099         struct drm_crtc **save_crtcs, *new_crtc;
1100         bool save_enabled = crtc->enabled;
1101         bool changed = false;
1102         bool flip_or_move = false;
1103         struct drm_output *output;
1104         int count = 0, ro;
1105
1106         /* this is meant to be num_output not num_crtc */
1107         save_crtcs = kzalloc(dev->mode_config.num_output * sizeof(struct drm_crtc *), GFP_KERNEL);
1108         if (!save_crtcs)
1109                 return -ENOMEM;
1110
1111         /* We should be able to check here if the fb has the same properties
1112          * and then just flip_or_move it */
1113         if (crtc->fb != fb)
1114                 flip_or_move = true;
1115
1116         if (crtc_info->x != crtc->x || crtc_info->y != crtc->y)
1117                 flip_or_move = true;
1118
1119         if (new_mode && !drm_mode_equal(new_mode, &crtc->mode)) {
1120                 DRM_DEBUG("modes are different\n");
1121                 drm_mode_debug_printmodeline(dev, &crtc->mode);
1122                 drm_mode_debug_printmodeline(dev, new_mode);
1123                 changed = true;
1124         }
1125
1126         list_for_each_entry(output, &dev->mode_config.output_list, head) {
1127                 save_crtcs[count++] = output->crtc;
1128
1129                 if (output->crtc == crtc)
1130                         new_crtc = NULL;
1131                 else
1132                         new_crtc = output->crtc;
1133
1134                 for (ro = 0; ro < crtc_info->count_outputs; ro++) {
1135                         if (output_set[ro] == output)
1136                                 new_crtc = crtc;
1137                 }
1138                 if (new_crtc != output->crtc) {
1139                         changed = true;
1140                         output->crtc = new_crtc;
1141                 }
1142         }
1143
1144         /* mode_set_base is not a required function */
1145         if (flip_or_move && !crtc->funcs->mode_set_base)
1146                 changed = true;
1147
1148         if (changed) {
1149                 crtc->fb = fb;
1150                 crtc->enabled = (new_mode != NULL);
1151                 if (new_mode != NULL) {
1152                         DRM_DEBUG("attempting to set mode from userspace\n");
1153                         drm_mode_debug_printmodeline(dev, new_mode);
1154                         if (!drm_crtc_set_mode(crtc, new_mode, crtc_info->x,
1155                                                crtc_info->y)) {
1156                                 crtc->enabled = save_enabled;
1157                                 count = 0;
1158                                 list_for_each_entry(output, &dev->mode_config.output_list, head)
1159                                         output->crtc = save_crtcs[count++];
1160                                 kfree(save_crtcs);
1161                                 return -EINVAL;
1162                         }
1163                         crtc->desired_x = crtc_info->x;
1164                         crtc->desired_y = crtc_info->y;
1165                         crtc->desired_mode = new_mode;
1166                 }
1167                 drm_disable_unused_functions(dev);
1168         } else if (flip_or_move) {
1169                 if (crtc->fb != fb)
1170                         crtc->fb = fb;
1171                 crtc->funcs->mode_set_base(crtc, crtc_info->x, crtc_info->y);
1172         }
1173
1174         kfree(save_crtcs);
1175         return 0;
1176 }
1177
1178 /**
1179  * drm_hotplug_stage_two
1180  * @dev DRM device
1181  * @output hotpluged output
1182  *
1183  * LOCKING.
1184  * Caller must hold mode config lock, function might grab struct lock.
1185  *
1186  * Stage two of a hotplug.
1187  *
1188  * RETURNS:
1189  * Zero on success, errno on failure.
1190  */
1191 int drm_hotplug_stage_two(struct drm_device *dev, struct drm_output *output,
1192                           bool connected)
1193 {
1194         int has_config = 0;
1195
1196         dev->mode_config.hotplug_counter++;
1197
1198         /* We might want to do something more here */
1199         if (!connected) {
1200                 DRM_DEBUG("not connected\n");
1201                 return 0;
1202         }
1203
1204         if (output->crtc && output->crtc->desired_mode) {
1205                 DRM_DEBUG("drm thinks that the output already has a config\n");
1206                 has_config = 1;
1207         }
1208
1209         drm_crtc_probe_output_modes(dev, 2048, 2048);
1210
1211         if (!has_config)
1212                 drm_pick_crtcs(dev);
1213
1214         if (!output->crtc || !output->crtc->desired_mode) {
1215                 DRM_DEBUG("could not find a desired mode or crtc for output\n");
1216                 return 1;
1217         }
1218
1219         /* We should really check if there is a fb using this crtc */
1220         if (!has_config)
1221                 dev->driver->fb_probe(dev, output->crtc);
1222         else {
1223                 dev->driver->fb_resize(dev, output->crtc);
1224
1225                 if (!drm_crtc_set_mode(output->crtc, output->crtc->desired_mode, 0, 0))
1226                         DRM_ERROR("failed to set mode after hotplug\n");
1227         }
1228
1229         drm_disable_unused_functions(dev);
1230
1231         return 0;
1232 }
1233 EXPORT_SYMBOL(drm_hotplug_stage_two);
1234
1235 int drm_mode_hotplug_ioctl(struct drm_device *dev,
1236                            void *data, struct drm_file *file_priv)
1237 {
1238         struct drm_mode_hotplug *arg = data;
1239
1240         arg->counter = dev->mode_config.hotplug_counter;
1241
1242         return 0;
1243 }
1244
1245 /**
1246  * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1247  * @out: drm_mode_modeinfo struct to return to the user
1248  * @in: drm_display_mode to use
1249  *
1250  * LOCKING:
1251  * None.
1252  *
1253  * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1254  * the user.
1255  */
1256 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out, struct drm_display_mode *in)
1257 {
1258         out->clock = in->clock;
1259         out->hdisplay = in->hdisplay;
1260         out->hsync_start = in->hsync_start;
1261         out->hsync_end = in->hsync_end;
1262         out->htotal = in->htotal;
1263         out->hskew = in->hskew;
1264         out->vdisplay = in->vdisplay;
1265         out->vsync_start = in->vsync_start;
1266         out->vsync_end = in->vsync_end;
1267         out->vtotal = in->vtotal;
1268         out->vscan = in->vscan;
1269         out->vrefresh = in->vrefresh;
1270         out->flags = in->flags;
1271         out->type = in->type;
1272         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1273         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1274 }
1275
1276 /**
1277  * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1278  * @out: drm_display_mode to return to the user
1279  * @in: drm_mode_modeinfo to use
1280  *
1281  * LOCKING:
1282  * None.
1283  *
1284  * Convert a drmo_mode_modeinfo into a drm_display_mode structure to return to
1285  * the caller.
1286  */
1287 void drm_crtc_convert_umode(struct drm_display_mode *out, struct drm_mode_modeinfo *in)
1288 {
1289         out->clock = in->clock;
1290         out->hdisplay = in->hdisplay;
1291         out->hsync_start = in->hsync_start;
1292         out->hsync_end = in->hsync_end;
1293         out->htotal = in->htotal;
1294         out->hskew = in->hskew;
1295         out->vdisplay = in->vdisplay;
1296         out->vsync_start = in->vsync_start;
1297         out->vsync_end = in->vsync_end;
1298         out->vtotal = in->vtotal;
1299         out->vscan = in->vscan;
1300         out->vrefresh = in->vrefresh;
1301         out->flags = in->flags;
1302         out->type = in->type;
1303         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1304         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1305 }
1306         
1307 /**
1308  * drm_mode_getresources - get graphics configuration
1309  * @inode: inode from the ioctl
1310  * @filp: file * from the ioctl
1311  * @cmd: cmd from ioctl
1312  * @arg: arg from ioctl
1313  *
1314  * LOCKING:
1315  * Takes mode config lock.
1316  *
1317  * Construct a set of configuration description structures and return
1318  * them to the user, including CRTC, output and framebuffer configuration.
1319  *
1320  * Called by the user via ioctl.
1321  *
1322  * RETURNS:
1323  * Zero on success, errno on failure.
1324  */
1325 int drm_mode_getresources(struct drm_device *dev,
1326                           void *data, struct drm_file *file_priv)
1327 {
1328         struct drm_mode_card_res *card_res = data;
1329         struct list_head *lh;
1330         struct drm_framebuffer *fb;
1331         struct drm_output *output;
1332         struct drm_crtc *crtc;
1333         int ret = 0;
1334         int output_count = 0;
1335         int crtc_count = 0;
1336         int fb_count = 0;
1337         int copied = 0;
1338         uint32_t __user *fb_id;
1339         uint32_t __user *crtc_id;
1340         uint32_t __user *output_id;
1341
1342         mutex_lock(&dev->mode_config.mutex);
1343
1344         list_for_each(lh, &dev->mode_config.fb_list)
1345                 fb_count++;
1346
1347         list_for_each(lh, &dev->mode_config.crtc_list)
1348                 crtc_count++;
1349
1350         list_for_each(lh, &dev->mode_config.output_list)
1351                 output_count++;
1352
1353         card_res->max_height = dev->mode_config.max_height;
1354         card_res->min_height = dev->mode_config.min_height;
1355         card_res->max_width = dev->mode_config.max_width;
1356         card_res->min_width = dev->mode_config.min_width;
1357
1358         /* handle this in 4 parts */
1359         /* FBs */
1360         if (card_res->count_fbs >= fb_count) {
1361                 copied = 0;
1362                 fb_id = (uint32_t *)(unsigned long)card_res->fb_id_ptr;
1363                 list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
1364                         if (put_user(fb->id, fb_id + copied)) {
1365                                 ret = -EFAULT;
1366                                 goto out;
1367                         }
1368                         copied++;
1369                 }
1370         }
1371         card_res->count_fbs = fb_count;
1372
1373         /* CRTCs */
1374         if (card_res->count_crtcs >= crtc_count) {
1375                 copied = 0;
1376                 crtc_id = (uint32_t *)(unsigned long)card_res->crtc_id_ptr;
1377                 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head){
1378                         DRM_DEBUG("CRTC ID is %d\n", crtc->id);
1379                         if (put_user(crtc->id, crtc_id + copied)) {
1380                                 ret = -EFAULT;
1381                                 goto out;
1382                         }
1383                         copied++;
1384                 }
1385         }
1386         card_res->count_crtcs = crtc_count;
1387
1388
1389         /* Outputs */
1390         if (card_res->count_outputs >= output_count) {
1391                 copied = 0;
1392                 output_id = (uint32_t *)(unsigned long)card_res->output_id_ptr;
1393                 list_for_each_entry(output, &dev->mode_config.output_list,
1394                                     head) {
1395                         DRM_DEBUG("OUTPUT ID is %d\n", output->id);
1396                         if (put_user(output->id, output_id + copied)) {
1397                                 ret = -EFAULT;
1398                                 goto out;
1399                         }
1400                         copied++;
1401                 }
1402         }
1403         card_res->count_outputs = output_count;
1404         
1405         DRM_DEBUG("Counted %d %d\n", card_res->count_crtcs,
1406                   card_res->count_outputs);
1407
1408 out:    
1409         mutex_unlock(&dev->mode_config.mutex);
1410         return ret;
1411 }
1412
1413 /**
1414  * drm_mode_getcrtc - get CRTC configuration
1415  * @inode: inode from the ioctl
1416  * @filp: file * from the ioctl
1417  * @cmd: cmd from ioctl
1418  * @arg: arg from ioctl
1419  *
1420  * LOCKING:
1421  * Caller? (FIXME)
1422  *
1423  * Construct a CRTC configuration structure to return to the user.
1424  *
1425  * Called by the user via ioctl.
1426  *
1427  * RETURNS:
1428  * Zero on success, errno on failure.
1429  */
1430 int drm_mode_getcrtc(struct drm_device *dev,
1431                      void *data, struct drm_file *file_priv)
1432 {
1433         struct drm_mode_crtc *crtc_resp = data;
1434         struct drm_crtc *crtc;
1435         struct drm_output *output;
1436         int ocount;
1437         int ret = 0;
1438
1439         mutex_lock(&dev->mode_config.mutex);
1440         crtc = idr_find(&dev->mode_config.crtc_idr, crtc_resp->crtc_id);
1441         if (!crtc || (crtc->id != crtc_resp->crtc_id)) {
1442                 ret = -EINVAL;
1443                 goto out;
1444         }
1445
1446         crtc_resp->x = crtc->x;
1447         crtc_resp->y = crtc->y;
1448
1449         if (crtc->fb)
1450                 crtc_resp->fb_id = crtc->fb->id;
1451         else
1452                 crtc_resp->fb_id = 0;
1453
1454         crtc_resp->outputs = 0;
1455         if (crtc->enabled) {
1456
1457                 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1458                 crtc_resp->mode_valid = 1;
1459                 ocount = 0;
1460                 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1461                         if (output->crtc == crtc)
1462                                 crtc_resp->outputs |= 1 << (ocount++);
1463                 }
1464                 
1465         } else {
1466                 crtc_resp->mode_valid = 0;
1467         }
1468
1469 out:
1470         mutex_unlock(&dev->mode_config.mutex);
1471         return ret;
1472 }
1473
1474 /**
1475  * drm_mode_getoutput - get output configuration
1476  * @inode: inode from the ioctl
1477  * @filp: file * from the ioctl
1478  * @cmd: cmd from ioctl
1479  * @arg: arg from ioctl
1480  *
1481  * LOCKING:
1482  * Caller? (FIXME)
1483  *
1484  * Construct a output configuration structure to return to the user.
1485  *
1486  * Called by the user via ioctl.
1487  *
1488  * RETURNS:
1489  * Zero on success, errno on failure.
1490  */
1491 int drm_mode_getoutput(struct drm_device *dev,
1492                        void *data, struct drm_file *file_priv)
1493 {
1494         struct drm_mode_get_output *out_resp = data;
1495         struct drm_output *output;
1496         struct drm_display_mode *mode;
1497         int mode_count = 0;
1498         int props_count = 0;
1499         int ret = 0;
1500         int copied = 0;
1501         int i;
1502         struct drm_mode_modeinfo u_mode;
1503         struct drm_mode_modeinfo __user *mode_ptr;
1504         uint32_t __user *prop_ptr;
1505         uint64_t __user *prop_values;
1506
1507         memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1508
1509         DRM_DEBUG("output id %d:\n", out_resp->output);
1510
1511         mutex_lock(&dev->mode_config.mutex);
1512         output= idr_find(&dev->mode_config.crtc_idr, out_resp->output);
1513         if (!output || (output->id != out_resp->output)) {
1514                 ret = -EINVAL;
1515                 goto out;
1516         }
1517
1518         list_for_each_entry(mode, &output->modes, head)
1519                 mode_count++;
1520         
1521         for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1522                 if (output->property_ids[i] != 0) {
1523                         props_count++;
1524                 }
1525         }
1526
1527         if (out_resp->count_modes == 0) {
1528                 drm_crtc_probe_single_output_modes(output, dev->mode_config.max_width, dev->mode_config.max_height);
1529         }
1530
1531         out_resp->output_type = output->output_type;
1532         out_resp->output_type_id = output->output_type_id;
1533         out_resp->mm_width = output->mm_width;
1534         out_resp->mm_height = output->mm_height;
1535         out_resp->subpixel = output->subpixel_order;
1536         out_resp->connection = output->status;
1537         if (output->crtc)
1538                 out_resp->crtc = output->crtc->id;
1539         else
1540                 out_resp->crtc = 0;
1541
1542         out_resp->crtcs = output->possible_crtcs;
1543         out_resp->clones = output->possible_clones;
1544
1545         if ((out_resp->count_modes >= mode_count) && mode_count) {
1546                 copied = 0;
1547                 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1548                 list_for_each_entry(mode, &output->modes, head) {
1549                         drm_crtc_convert_to_umode(&u_mode, mode);
1550                         if (copy_to_user(mode_ptr + copied,
1551                                          &u_mode, sizeof(u_mode))) {
1552                                 ret = -EFAULT;
1553                                 goto out;
1554                         }
1555                         copied++;
1556                         
1557                 }
1558         }
1559         out_resp->count_modes = mode_count;
1560
1561         if ((out_resp->count_props >= props_count) && props_count) {
1562                 copied = 0;
1563                 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1564                 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1565                 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1566                         if (output->property_ids[i] != 0) {
1567                                 if (put_user(output->property_ids[i], prop_ptr + copied)) {
1568                                         ret = -EFAULT;
1569                                         goto out;
1570                                 }
1571
1572                                 if (put_user(output->property_values[i], prop_values + copied)) {
1573                                         ret = -EFAULT;
1574                                         goto out;
1575                                 }
1576                                 copied++;
1577                         }
1578                 }
1579         }
1580         out_resp->count_props = props_count;
1581
1582 out:
1583         mutex_unlock(&dev->mode_config.mutex);
1584         return ret;
1585 }
1586
1587 /**
1588  * drm_mode_setcrtc - set CRTC configuration
1589  * @inode: inode from the ioctl
1590  * @filp: file * from the ioctl
1591  * @cmd: cmd from ioctl
1592  * @arg: arg from ioctl
1593  *
1594  * LOCKING:
1595  * Caller? (FIXME)
1596  *
1597  * Build a new CRTC configuration based on user request.
1598  *
1599  * Called by the user via ioctl.
1600  *
1601  * RETURNS:
1602  * Zero on success, errno on failure.
1603  */
1604 int drm_mode_setcrtc(struct drm_device *dev,
1605                      void *data, struct drm_file *file_priv)
1606 {
1607         struct drm_mode_crtc *crtc_req = data;
1608         struct drm_crtc *crtc, *crtcfb;
1609         struct drm_output **output_set = NULL, *output;
1610         struct drm_framebuffer *fb = NULL;
1611         struct drm_display_mode *mode = NULL;
1612         uint32_t __user *set_outputs_ptr;
1613         int ret = 0;
1614         int i;
1615
1616         mutex_lock(&dev->mode_config.mutex);
1617         crtc = idr_find(&dev->mode_config.crtc_idr, crtc_req->crtc_id);
1618         if (!crtc || (crtc->id != crtc_req->crtc_id)) {
1619                 DRM_DEBUG("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1620                 ret = -EINVAL;
1621                 goto out;
1622         }
1623
1624         if (crtc_req->mode_valid) {
1625                 /* If we have a mode we need a framebuffer. */
1626                 /* If we pass -1, set the mode with the currently bound fb */
1627                 if (crtc_req->fb_id == -1) {
1628                         list_for_each_entry(crtcfb, &dev->mode_config.crtc_list, head) {
1629                                 if (crtcfb == crtc) {
1630                                         DRM_DEBUG("Using current fb for setmode\n");
1631                                         fb = crtc->fb;          
1632                                 }
1633                         }
1634                 } else {
1635                         fb = idr_find(&dev->mode_config.crtc_idr, crtc_req->fb_id);
1636                         if (!fb || (fb->id != crtc_req->fb_id)) {
1637                                 DRM_DEBUG("Unknown FB ID%d\n", crtc_req->fb_id);
1638                                 ret = -EINVAL;
1639                                 goto out;
1640                         }
1641                 }
1642
1643                 mode = drm_mode_create(dev);
1644                 drm_crtc_convert_umode(mode, &crtc_req->mode);
1645                 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1646         }
1647
1648         if (crtc_req->count_outputs == 0 && mode) {
1649                 DRM_DEBUG("Count outputs is 0 but mode set\n");
1650                 ret = -EINVAL;
1651                 goto out;
1652         }
1653
1654         if (crtc_req->count_outputs > 0 && !mode && !fb) {
1655                 DRM_DEBUG("Count outputs is %d but no mode or fb set\n", crtc_req->count_outputs);
1656                 ret = -EINVAL;
1657                 goto out;
1658         }
1659
1660         if (crtc_req->count_outputs > 0) {
1661                 u32 out_id;
1662                 /* Maybe we should check that count_outputs is a sensible value. */
1663                 output_set = kmalloc(crtc_req->count_outputs *
1664                                      sizeof(struct drm_output *), GFP_KERNEL);
1665                 if (!output_set) {
1666                         ret = -ENOMEM;
1667                         goto out;
1668                 }
1669
1670                 for (i = 0; i < crtc_req->count_outputs; i++) {
1671                         set_outputs_ptr = (uint32_t *)(unsigned long)crtc_req->set_outputs_ptr;
1672                         if (get_user(out_id, &set_outputs_ptr[i])) {
1673                                 ret = -EFAULT;
1674                                 goto out;
1675                         }
1676
1677                         output = idr_find(&dev->mode_config.crtc_idr, out_id);
1678                         if (!output || (out_id != output->id)) {
1679                                 DRM_DEBUG("Output id %d unknown\n", out_id);
1680                                 ret = -EINVAL;
1681                                 goto out;
1682                         }
1683
1684                         output_set[i] = output;
1685                 }
1686         }
1687
1688         ret = drm_crtc_set_config(crtc, crtc_req, mode, output_set, fb);
1689
1690 out:
1691         kfree(output_set);
1692         mutex_unlock(&dev->mode_config.mutex);
1693         return ret;
1694 }
1695
1696 int drm_mode_cursor_ioctl(struct drm_device *dev,
1697                         void *data, struct drm_file *file_priv)
1698 {
1699         struct drm_mode_cursor *req = data;
1700         struct drm_crtc *crtc;
1701         struct drm_buffer_object *bo = NULL; /* must be set */
1702         int ret = 0;
1703
1704         DRM_DEBUG("\n");
1705
1706         if (!req->flags) {
1707                 DRM_ERROR("no operation set\n");
1708                 return -EINVAL;
1709         }
1710
1711         mutex_lock(&dev->mode_config.mutex);
1712         crtc = idr_find(&dev->mode_config.crtc_idr, req->crtc);
1713         if (!crtc || (crtc->id != req->crtc)) {
1714                 DRM_DEBUG("Unknown CRTC ID %d\n", req->crtc);
1715                 ret = -EINVAL;
1716                 goto out;
1717         }
1718
1719         if (req->flags & DRM_MODE_CURSOR_BO) {
1720                 /* Turn of the cursor if handle is 0 */
1721                 if (req->handle)
1722                         ret = drm_get_buffer_object(dev, &bo, req->handle);
1723
1724                 if (ret) {
1725                         DRM_ERROR("invalid buffer id\n");
1726                         ret = -EINVAL;
1727                         goto out;
1728                 }
1729
1730                 if (crtc->funcs->cursor_set) {
1731                         ret = crtc->funcs->cursor_set(crtc, bo, req->width, req->height);
1732                 } else {
1733                         DRM_ERROR("crtc does not support cursor\n");
1734                         ret = -EFAULT;
1735                         goto out;
1736                 }
1737         }
1738
1739         if (req->flags & DRM_MODE_CURSOR_MOVE) {
1740                 if (crtc->funcs->cursor_move) {
1741                         ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1742                 } else {
1743                         DRM_ERROR("crtc does not support cursor\n");
1744                         ret = -EFAULT;
1745                         goto out;
1746                 }
1747         }
1748 out:
1749         mutex_unlock(&dev->mode_config.mutex);
1750         return ret;
1751 }
1752
1753 /**
1754  * drm_mode_addfb - add an FB to the graphics configuration
1755  * @inode: inode from the ioctl
1756  * @filp: file * from the ioctl
1757  * @cmd: cmd from ioctl
1758  * @arg: arg from ioctl
1759  *
1760  * LOCKING:
1761  * Takes mode config lock.
1762  *
1763  * Add a new FB to the specified CRTC, given a user request.
1764  *
1765  * Called by the user via ioctl.
1766  *
1767  * RETURNS:
1768  * Zero on success, errno on failure.
1769  */
1770 int drm_mode_addfb(struct drm_device *dev,
1771                    void *data, struct drm_file *file_priv)
1772 {
1773         struct drm_mode_fb_cmd *r = data;
1774         struct drm_mode_config *config = &dev->mode_config;
1775         struct drm_framebuffer *fb;
1776         struct drm_buffer_object *bo;
1777         int ret = 0;
1778
1779         if ((config->min_width > r->width) || (r->width > config->max_width)) {
1780                 DRM_ERROR("mode new framebuffer width not within limits\n");
1781                 return -EINVAL;
1782         }
1783         if ((config->min_height > r->height) || (r->height > config->max_height)) {
1784                 DRM_ERROR("mode new framebuffer height not within limits\n");
1785                 return -EINVAL;
1786         }
1787
1788         mutex_lock(&dev->mode_config.mutex);
1789         /* TODO check limits are okay */
1790         ret = drm_get_buffer_object(dev, &bo, r->handle);
1791         if (ret || !bo) {
1792                 DRM_ERROR("BO handle not valid\n");
1793                 ret = -EINVAL;
1794                 goto out;
1795         }
1796
1797         /* TODO check buffer is sufficently large */
1798         /* TODO setup destructor callback */
1799
1800         fb = drm_framebuffer_create(dev);
1801         if (!fb) {
1802                 DRM_ERROR("could not create framebuffer\n");
1803                 ret = -EINVAL;
1804                 goto out;
1805         }
1806
1807         fb->width = r->width;
1808         fb->height = r->height;
1809         fb->pitch = r->pitch;
1810         fb->bits_per_pixel = r->bpp;
1811         fb->depth = r->depth;
1812         fb->bo = bo;
1813
1814         r->buffer_id = fb->id;
1815
1816         list_add(&fb->filp_head, &file_priv->fbs);
1817
1818 out:
1819         mutex_unlock(&dev->mode_config.mutex);
1820         return ret;
1821 }
1822
1823 /**
1824  * drm_mode_rmfb - remove an FB from the configuration
1825  * @inode: inode from the ioctl
1826  * @filp: file * from the ioctl
1827  * @cmd: cmd from ioctl
1828  * @arg: arg from ioctl
1829  *
1830  * LOCKING:
1831  * Takes mode config lock.
1832  *
1833  * Remove the FB specified by the user.
1834  *
1835  * Called by the user via ioctl.
1836  *
1837  * RETURNS:
1838  * Zero on success, errno on failure.
1839  */
1840 int drm_mode_rmfb(struct drm_device *dev,
1841                    void *data, struct drm_file *file_priv)
1842 {
1843         struct drm_framebuffer *fb = 0;
1844         struct drm_framebuffer *fbl = 0;
1845         uint32_t *id = data;
1846         int ret = 0;
1847         int found = 0;
1848
1849         mutex_lock(&dev->mode_config.mutex);
1850         fb = idr_find(&dev->mode_config.crtc_idr, *id);
1851         /* TODO check that we realy get a framebuffer back. */
1852         if (!fb || (*id != fb->id)) {
1853                 DRM_ERROR("mode invalid framebuffer id\n");
1854                 ret = -EINVAL;
1855                 goto out;
1856         }
1857
1858         list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1859                 if (fb == fbl)
1860                         found = 1;
1861
1862         if (!found) {
1863                 DRM_ERROR("tried to remove a fb that we didn't own\n");
1864                 ret = -EINVAL;
1865                 goto out;
1866         }
1867
1868         /* TODO release all crtc connected to the framebuffer */
1869         /* TODO unhock the destructor from the buffer object */
1870
1871         if (fb->bo->type == drm_bo_type_kernel)
1872                 DRM_ERROR("the bo type should not be of kernel type\n");
1873
1874         list_del(&fb->filp_head);
1875         drm_framebuffer_destroy(fb);
1876
1877 out:
1878         mutex_unlock(&dev->mode_config.mutex);
1879         return ret;
1880 }
1881
1882 /**
1883  * drm_mode_getfb - get FB info
1884  * @inode: inode from the ioctl
1885  * @filp: file * from the ioctl
1886  * @cmd: cmd from ioctl
1887  * @arg: arg from ioctl
1888  *
1889  * LOCKING:
1890  * Caller? (FIXME)
1891  *
1892  * Lookup the FB given its ID and return info about it.
1893  *
1894  * Called by the user via ioctl.
1895  *
1896  * RETURNS:
1897  * Zero on success, errno on failure.
1898  */
1899 int drm_mode_getfb(struct drm_device *dev,
1900                    void *data, struct drm_file *file_priv)
1901 {
1902         struct drm_mode_fb_cmd *r = data;
1903         struct drm_framebuffer *fb;
1904         int ret = 0;
1905
1906         mutex_lock(&dev->mode_config.mutex);
1907         fb = idr_find(&dev->mode_config.crtc_idr, r->buffer_id);
1908         if (!fb || (r->buffer_id != fb->id)) {
1909                 DRM_ERROR("invalid framebuffer id\n");
1910                 ret = -EINVAL;
1911                 goto out;
1912         }
1913
1914         r->height = fb->height;
1915         r->width = fb->width;
1916         r->depth = fb->depth;
1917         r->bpp = fb->bits_per_pixel;
1918         r->handle = fb->bo->base.hash.key;
1919         r->pitch = fb->pitch;
1920
1921 out:
1922         mutex_unlock(&dev->mode_config.mutex);
1923         return ret;
1924 }
1925
1926 /**
1927  * drm_fb_release - remove and free the FBs on this file
1928  * @filp: file * from the ioctl
1929  *
1930  * LOCKING:
1931  * Takes mode config lock.
1932  *
1933  * Destroy all the FBs associated with @filp.
1934  *
1935  * Called by the user via ioctl.
1936  *
1937  * RETURNS:
1938  * Zero on success, errno on failure.
1939  */
1940 void drm_fb_release(struct file *filp)
1941 {
1942         struct drm_file *priv = filp->private_data;
1943         struct drm_device *dev = priv->minor->dev;
1944         struct drm_framebuffer *fb, *tfb;
1945
1946         mutex_lock(&dev->mode_config.mutex);
1947         list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1948                 list_del(&fb->filp_head);
1949                 if (fb->bo->type == drm_bo_type_kernel)
1950                         DRM_ERROR("the bo type should not be of kernel_type, the kernel will probably explode, why Dave\n");
1951
1952                 drm_framebuffer_destroy(fb);
1953         }
1954         mutex_unlock(&dev->mode_config.mutex);
1955 }
1956
1957 /*
1958  *
1959  */
1960
1961 static int drm_mode_attachmode(struct drm_device *dev,
1962                                struct drm_output *output,
1963                                struct drm_display_mode *mode)
1964 {
1965         int ret = 0;
1966
1967         list_add_tail(&mode->head, &output->user_modes);
1968         return ret;
1969 }
1970
1971 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1972                              struct drm_display_mode *mode)
1973 {
1974         struct drm_output *output;
1975         int ret = 0;
1976         struct drm_display_mode *dup_mode;
1977         int need_dup = 0;
1978         list_for_each_entry(output, &dev->mode_config.output_list, head) {
1979                 if (output->crtc == crtc) {
1980                         if (need_dup)
1981                                 dup_mode = drm_mode_duplicate(dev, mode);
1982                         else
1983                                 dup_mode = mode;
1984                         ret = drm_mode_attachmode(dev, output, dup_mode); 
1985                         if (ret)
1986                                 return ret;
1987                         need_dup = 1;
1988                 }
1989         }
1990         return 0;
1991 }
1992 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1993
1994 static int drm_mode_detachmode(struct drm_device *dev,
1995                                struct drm_output *output,
1996                                struct drm_display_mode *mode)
1997 {
1998         int found = 0;
1999         int ret = 0;
2000         struct drm_display_mode *match_mode, *t;
2001
2002         list_for_each_entry_safe(match_mode, t, &output->user_modes, head) {
2003                 if (drm_mode_equal(match_mode, mode)) {
2004                         list_del(&match_mode->head);
2005                         drm_mode_destroy(dev, match_mode);
2006                         found = 1;
2007                         break;
2008                 }
2009         }
2010
2011         if (!found)
2012                 ret = -EINVAL;
2013
2014         return ret;
2015 }
2016
2017 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2018 {
2019         struct drm_output *output;
2020
2021         list_for_each_entry(output, &dev->mode_config.output_list, head) {
2022                 drm_mode_detachmode(dev, output, mode);
2023         }
2024         return 0;
2025 }
2026 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2027
2028 /**
2029  * drm_fb_attachmode - Attach a user mode to an output
2030  * @inode: inode from the ioctl
2031  * @filp: file * from the ioctl
2032  * @cmd: cmd from ioctl
2033  * @arg: arg from ioctl
2034  *
2035  * This attaches a user specified mode to an output.
2036  * Called by the user via ioctl.
2037  *
2038  * RETURNS:
2039  * Zero on success, errno on failure.
2040  */
2041 int drm_mode_attachmode_ioctl(struct drm_device *dev,
2042                               void *data, struct drm_file *file_priv)
2043 {
2044         struct drm_mode_mode_cmd *mode_cmd = data;
2045         struct drm_output *output;
2046         struct drm_display_mode *mode;
2047         struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2048         int ret = 0;
2049
2050         mutex_lock(&dev->mode_config.mutex);
2051
2052         output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
2053         if (!output || (output->id != mode_cmd->output_id)) {
2054                 ret = -EINVAL;
2055                 goto out;
2056         }
2057
2058         mode = drm_mode_create(dev);
2059         if (!mode) {
2060                 ret = -ENOMEM;
2061                 goto out;
2062         }
2063         
2064         drm_crtc_convert_umode(mode, umode);
2065
2066         ret = drm_mode_attachmode(dev, output, mode);
2067 out:
2068         mutex_unlock(&dev->mode_config.mutex);
2069         return ret;
2070 }
2071
2072
2073 /**
2074  * drm_fb_detachmode - Detach a user specified mode from an output
2075  * @inode: inode from the ioctl
2076  * @filp: file * from the ioctl
2077  * @cmd: cmd from ioctl
2078  * @arg: arg from ioctl
2079  *
2080  * Called by the user via ioctl.
2081  *
2082  * RETURNS:
2083  * Zero on success, errno on failure.
2084  */
2085 int drm_mode_detachmode_ioctl(struct drm_device *dev,
2086                               void *data, struct drm_file *file_priv)
2087 {
2088         struct drm_mode_mode_cmd *mode_cmd = data;
2089         struct drm_output *output;
2090         struct drm_display_mode mode;
2091         struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2092         int ret = 0;
2093
2094         mutex_lock(&dev->mode_config.mutex);
2095
2096         output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
2097         if (!output || (output->id != mode_cmd->output_id)) {
2098                 ret = -EINVAL;
2099                 goto out;
2100         }
2101         
2102         drm_crtc_convert_umode(&mode, umode);
2103         ret = drm_mode_detachmode(dev, output, &mode);
2104 out:           
2105         mutex_unlock(&dev->mode_config.mutex);
2106         return ret;
2107 }
2108
2109 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2110                                          const char *name, int num_values)
2111 {
2112         struct drm_property *property = NULL;
2113
2114         property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2115         if (!property)
2116                 return NULL;
2117
2118         if (num_values) {
2119                 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2120                 if (!property->values)
2121                         goto fail;
2122         }
2123
2124         property->id = drm_idr_get(dev, property);
2125         property->flags = flags;
2126         property->num_values = num_values;
2127         INIT_LIST_HEAD(&property->enum_blob_list);
2128
2129         if (name)
2130                 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2131
2132         list_add_tail(&property->head, &dev->mode_config.property_list);
2133         return property;
2134 fail:
2135         kfree(property);
2136         return NULL;
2137 }
2138 EXPORT_SYMBOL(drm_property_create);
2139
2140 int drm_property_add_enum(struct drm_property *property, int index,
2141                           uint64_t value, const char *name)
2142 {
2143         struct drm_property_enum *prop_enum;
2144
2145         if (!(property->flags & DRM_MODE_PROP_ENUM))
2146                 return -EINVAL;
2147
2148         if (!list_empty(&property->enum_blob_list)) {
2149                 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2150                         if (prop_enum->value == value) {
2151                                 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN); 
2152                                 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2153                                 return 0;
2154                         }
2155                 }
2156         }
2157
2158         prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2159         if (!prop_enum)
2160                 return -ENOMEM;
2161
2162         strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN); 
2163         prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2164         prop_enum->value = value;
2165
2166         property->values[index] = value;
2167         list_add_tail(&prop_enum->head, &property->enum_blob_list);
2168         return 0;
2169 }
2170 EXPORT_SYMBOL(drm_property_add_enum);
2171
2172 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2173 {
2174         struct drm_property_enum *prop_enum, *pt;
2175
2176         list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2177                 list_del(&prop_enum->head);
2178                 kfree(prop_enum);
2179         }
2180
2181         if (property->num_values)
2182                 kfree(property->values);
2183         drm_idr_put(dev, property->id);
2184         list_del(&property->head);
2185         kfree(property);        
2186 }
2187 EXPORT_SYMBOL(drm_property_destroy);
2188
2189 int drm_output_attach_property(struct drm_output *output,
2190                                struct drm_property *property, uint64_t init_val)
2191 {
2192         int i;
2193
2194         for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2195                 if (output->property_ids[i] == 0) {
2196                         output->property_ids[i] = property->id;
2197                         output->property_values[i] = init_val;
2198                         break;
2199                 }
2200         }
2201
2202         if (i == DRM_OUTPUT_MAX_PROPERTY)
2203                 return -EINVAL;
2204         return 0;
2205 }
2206 EXPORT_SYMBOL(drm_output_attach_property);
2207
2208 int drm_output_property_set_value(struct drm_output *output,
2209                                   struct drm_property *property, uint64_t value)
2210 {
2211         int i;
2212
2213         for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2214                 if (output->property_ids[i] == property->id) {
2215                         output->property_values[i] = value;
2216                         break;
2217                 }
2218         }
2219
2220         if (i == DRM_OUTPUT_MAX_PROPERTY)
2221                 return -EINVAL;
2222         return 0;
2223 }
2224 EXPORT_SYMBOL(drm_output_property_set_value);
2225
2226 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2227                                void *data, struct drm_file *file_priv)
2228 {
2229         struct drm_mode_get_property *out_resp = data;
2230         struct drm_property *property;
2231         int enum_count = 0;
2232         int blob_count = 0;
2233         int value_count = 0;
2234         int ret = 0, i;
2235         int copied;
2236         struct drm_property_enum *prop_enum;
2237         struct drm_mode_property_enum __user *enum_ptr;
2238         struct drm_property_blob *prop_blob;
2239         uint32_t *blob_id_ptr;
2240         uint64_t __user *values_ptr;
2241         uint32_t __user *blob_length_ptr;
2242
2243         mutex_lock(&dev->mode_config.mutex);
2244         property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
2245         if (!property || (property->id != out_resp->prop_id)) {
2246                 ret = -EINVAL;
2247                 goto done;
2248         }
2249
2250         if (property->flags & DRM_MODE_PROP_ENUM) {
2251                 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2252                         enum_count++;
2253         } else if (property->flags & DRM_MODE_PROP_BLOB) {
2254                 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2255                         blob_count++;
2256         }
2257
2258         value_count = property->num_values;
2259
2260         strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2261         out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2262         out_resp->flags = property->flags;
2263
2264         if ((out_resp->count_values >= value_count) && value_count) {
2265                 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2266                 for (i = 0; i < value_count; i++) {
2267                         if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2268                                 ret = -EFAULT;
2269                                 goto done;
2270                         }
2271                 }
2272         }
2273         out_resp->count_values = value_count;
2274
2275         if (property->flags & DRM_MODE_PROP_ENUM) {
2276
2277                 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2278                         copied = 0;
2279                         enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2280                         list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2281                                 
2282                                 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2283                                         ret = -EFAULT;
2284                                         goto done;
2285                                 }
2286                                 
2287                                 if (copy_to_user(&enum_ptr[copied].name,
2288                                                  &prop_enum->name, DRM_PROP_NAME_LEN)) {
2289                                         ret = -EFAULT;
2290                                         goto done;
2291                                 }
2292                                 copied++;
2293                         }
2294                 }
2295                 out_resp->count_enum_blobs = enum_count;
2296         }
2297
2298         if (property->flags & DRM_MODE_PROP_BLOB) {
2299                 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2300                         copied = 0;
2301                         blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2302                         blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2303                         
2304                         list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2305                                 if (put_user(prop_blob->id, blob_id_ptr + copied)) {
2306                                         ret = -EFAULT;
2307                                         goto done;
2308                                 }
2309                                 
2310                                 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2311                                         ret = -EFAULT;
2312                                         goto done;
2313                                 }
2314                                 
2315                                 copied++;
2316                         }
2317                 }
2318                 out_resp->count_enum_blobs = enum_count;
2319         }
2320 done:
2321         mutex_unlock(&dev->mode_config.mutex);
2322         return ret;
2323 }
2324
2325 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2326                                                           void *data)
2327 {
2328         struct drm_property_blob *blob;
2329
2330         if (!length || !data)
2331                 return NULL;
2332
2333         blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2334         if (!blob)
2335                 return NULL;
2336
2337         blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2338         blob->length = length;
2339
2340         memcpy(blob->data, data, length);
2341
2342         blob->id = drm_idr_get(dev, blob);
2343         
2344         list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2345         return blob;
2346 }
2347
2348 static void drm_property_destroy_blob(struct drm_device *dev,
2349                                struct drm_property_blob *blob)
2350 {
2351         drm_idr_put(dev, blob->id);
2352         list_del(&blob->head);
2353         kfree(blob);
2354 }
2355
2356 int drm_mode_getblob_ioctl(struct drm_device *dev,
2357                            void *data, struct drm_file *file_priv)
2358 {
2359         struct drm_mode_get_blob *out_resp = data;
2360         struct drm_property_blob *blob;
2361         int ret = 0;
2362         void *blob_ptr;
2363
2364         mutex_lock(&dev->mode_config.mutex);
2365         
2366         blob = idr_find(&dev->mode_config.crtc_idr, out_resp->blob_id);
2367         if (!blob || (blob->id != out_resp->blob_id)) {
2368                 ret = -EINVAL;
2369                 goto done;
2370         }
2371
2372         if (out_resp->length == blob->length) {
2373                 blob_ptr = (void *)(unsigned long)out_resp->data;
2374                 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2375                         ret = -EFAULT;
2376                         goto done;
2377                 }
2378         }
2379         out_resp->length = blob->length;
2380
2381 done:
2382         mutex_unlock(&dev->mode_config.mutex);
2383         return ret;
2384 }
2385
2386 int drm_mode_output_update_edid_property(struct drm_output *output, struct edid *edid)
2387 {
2388         struct drm_device *dev = output->dev;
2389         int ret = 0;
2390         if (output->edid_blob_ptr)
2391                 drm_property_destroy_blob(dev, output->edid_blob_ptr);
2392
2393         output->edid_blob_ptr = drm_property_create_blob(output->dev, 128, edid);
2394         
2395         ret = drm_output_property_set_value(output, dev->mode_config.edid_property, output->edid_blob_ptr->id);
2396         return ret;
2397 }
2398 EXPORT_SYMBOL(drm_mode_output_update_edid_property);
2399
2400 int drm_mode_output_property_set_ioctl(struct drm_device *dev,
2401                                        void *data, struct drm_file *file_priv)
2402 {
2403         struct drm_mode_output_set_property *out_resp = data;
2404         struct drm_property *property;
2405         struct drm_output *output;
2406         int ret = -EINVAL;
2407         int i;
2408
2409         mutex_lock(&dev->mode_config.mutex);
2410         output = idr_find(&dev->mode_config.crtc_idr, out_resp->output_id);
2411         if (!output || (output->id != out_resp->output_id)) {
2412                 goto out;
2413         }
2414
2415         for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2416                 if (output->property_ids[i] == out_resp->prop_id)
2417                         break;
2418         }
2419
2420         if (i == DRM_OUTPUT_MAX_PROPERTY) {
2421                 goto out;
2422         }
2423         
2424         property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
2425         if (!property || (property->id != out_resp->prop_id)) {
2426                 goto out;
2427         }
2428
2429         if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2430                 goto out;
2431
2432         if (property->flags & DRM_MODE_PROP_RANGE) {
2433                 if (out_resp->value < property->values[0])
2434                         goto out;
2435
2436                 if (out_resp->value > property->values[1])
2437                         goto out;
2438         } else {
2439                 int found = 0;
2440                 for (i = 0; i < property->num_values; i++) {
2441                         if (property->values[i] == out_resp->value) {
2442                                 found = 1;
2443                                 break;
2444                         }
2445                 }
2446                 if (!found) {
2447                         goto out;
2448                 }
2449         }
2450
2451         if (output->funcs->set_property)
2452                 ret = output->funcs->set_property(output, property, out_resp->value);
2453
2454 out:
2455         mutex_unlock(&dev->mode_config.mutex);
2456         return ret;
2457 }
2458