OSDN Git Service

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