Improved DRM sysfs support
[profile/ivi/libdrm.git] / linux-core / drm_crtc.c
1 /*
2  * Copyright (c) 2006-2007 Intel Corporation
3  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4  *
5  * DRM core CRTC related functions
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and its
8  * documentation for any purpose is hereby granted without fee, provided that
9  * the above copyright notice appear in all copies and that both that copyright
10  * notice and this permission notice appear in supporting documentation, and
11  * that the name of the copyright holders not be used in advertising or
12  * publicity pertaining to distribution of the software without specific,
13  * written prior permission.  The copyright holders make no representations
14  * about the suitability of this software for any purpose.  It is provided "as
15  * is" without express or implied warranty.
16  *
17  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23  * OF THIS SOFTWARE.
24  *
25  * Authors:
26  *      Keith Packard
27  *      Eric Anholt <eric@anholt.net>
28  *      Dave Airlie <airlied@linux.ie>
29  *      Jesse Barnes <jesse.barnes@intel.com>
30  */
31 #include <linux/list.h>
32 #include "drm.h"
33 #include "drmP.h"
34 #include "drm_crtc.h"
35
36 struct drm_prop_enum_list {
37         int type;
38         char *name;
39 };
40
41 /*
42  * Global properties
43  */
44 static struct drm_prop_enum_list drm_dpms_enum_list[] =
45 { { DPMSModeOn, "On" },
46   { DPMSModeStandby, "Standby" },
47   { DPMSModeSuspend, "Suspend" },
48   { DPMSModeOff, "Off" }
49 };
50
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         drm_sysfs_output_add(output);
655
656         mutex_lock(&dev->mode_config.mutex);
657         list_add_tail(&output->head, &dev->mode_config.output_list);
658         dev->mode_config.num_output++;
659
660         drm_output_attach_property(output, dev->mode_config.edid_property, 0);
661
662         drm_output_attach_property(output, dev->mode_config.dpms_property, 0);
663
664         mutex_unlock(&dev->mode_config.mutex);
665
666         return output;
667
668 }
669 EXPORT_SYMBOL(drm_output_create);
670
671 /**
672  * drm_output_destroy - remove an output
673  * @output: output to remove
674  *
675  * LOCKING:
676  * Caller must hold @dev's mode_config lock.
677  *
678  * Call @output's cleanup function, then remove the output from the DRM
679  * mode_config after freeing @output's modes.
680  */
681 void drm_output_destroy(struct drm_output *output)
682 {
683         struct drm_device *dev = output->dev;
684         struct drm_display_mode *mode, *t;
685
686         drm_sysfs_output_remove(output);
687
688         if (*output->funcs->cleanup)
689                 (*output->funcs->cleanup)(output);
690
691         list_for_each_entry_safe(mode, t, &output->probed_modes, head)
692                 drm_mode_remove(output, mode);
693
694         list_for_each_entry_safe(mode, t, &output->modes, head)
695                 drm_mode_remove(output, mode);
696
697         list_for_each_entry_safe(mode, t, &output->user_modes, head)
698                 drm_mode_remove(output, mode);
699
700         mutex_lock(&dev->mode_config.mutex);
701         drm_idr_put(dev, output->id);
702         list_del(&output->head);
703         mutex_unlock(&dev->mode_config.mutex);
704         kfree(output);
705 }
706 EXPORT_SYMBOL(drm_output_destroy);
707
708
709 /**
710  * drm_mode_create - create a new display mode
711  * @dev: DRM device
712  *
713  * LOCKING:
714  * None.
715  *
716  * Create a new drm_display_mode, give it an ID, and return it.
717  *
718  * RETURNS:
719  * Pointer to new mode on success, NULL on error.
720  */
721 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
722 {
723         struct drm_display_mode *nmode;
724
725         nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
726         if (!nmode)
727                 return NULL;
728
729         nmode->mode_id = drm_idr_get(dev, nmode);
730         return nmode;
731 }
732 EXPORT_SYMBOL(drm_mode_create);
733
734 /**
735  * drm_mode_destroy - remove a mode
736  * @dev: DRM device
737  * @mode: mode to remove
738  *
739  * LOCKING:
740  * Caller must hold mode config lock.
741  *
742  * Free @mode's unique identifier, then free it.
743  */
744 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
745 {
746         drm_idr_put(dev, mode->mode_id);
747
748         kfree(mode);
749 }
750 EXPORT_SYMBOL(drm_mode_destroy);
751
752 static int drm_mode_create_standard_output_properties(struct drm_device *dev)
753 {
754         int i;
755
756         /*
757          * Standard properties (apply to all outputs)
758          */
759         dev->mode_config.edid_property =
760                 drm_property_create(dev, DRM_MODE_PROP_BLOB | DRM_MODE_PROP_IMMUTABLE,
761                                     "EDID", 0);
762
763         dev->mode_config.dpms_property =
764                 drm_property_create(dev, DRM_MODE_PROP_ENUM, 
765                         "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
766         for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
767                 drm_property_add_enum(dev->mode_config.dpms_property, i, drm_dpms_enum_list[i].type, drm_dpms_enum_list[i].name);
768
769         dev->mode_config.connector_type_property =
770                 drm_property_create(dev, DRM_MODE_PROP_ENUM | DRM_MODE_PROP_IMMUTABLE,
771                         "Connector Type", ARRAY_SIZE(drm_conn_enum_list));
772         for (i = 0; i < ARRAY_SIZE(drm_conn_enum_list); i++)
773                 drm_property_add_enum(dev->mode_config.connector_type_property, i, drm_conn_enum_list[i].type, drm_conn_enum_list[i].name);
774
775         dev->mode_config.connector_num_property =
776                 drm_property_create(dev, DRM_MODE_PROP_RANGE | DRM_MODE_PROP_IMMUTABLE,
777                         "Connector ID", 2);
778         dev->mode_config.connector_num_property->values[0] = 0;
779         dev->mode_config.connector_num_property->values[1] = 20;
780
781         /*
782          * TV specific properties
783          */
784         dev->mode_config.tv_left_margin_property =
785                 drm_property_create(dev, DRM_MODE_PROP_RANGE |
786                                     DRM_MODE_PROP_IMMUTABLE,
787                                     "left margin", 2);
788         dev->mode_config.tv_left_margin_property->values[0] = 0;
789         dev->mode_config.tv_left_margin_property->values[1] = 100;
790
791         dev->mode_config.tv_right_margin_property =
792                 drm_property_create(dev, DRM_MODE_PROP_RANGE |
793                                     DRM_MODE_PROP_IMMUTABLE,
794                                     "right margin", 2);
795         dev->mode_config.tv_right_margin_property->values[0] = 0;
796         dev->mode_config.tv_right_margin_property->values[1] = 100;
797
798         dev->mode_config.tv_top_margin_property =
799                 drm_property_create(dev, DRM_MODE_PROP_RANGE |
800                                     DRM_MODE_PROP_IMMUTABLE,
801                                     "top margin", 2);
802         dev->mode_config.tv_top_margin_property->values[0] = 0;
803         dev->mode_config.tv_top_margin_property->values[1] = 100;
804
805         dev->mode_config.tv_bottom_margin_property =
806                 drm_property_create(dev, DRM_MODE_PROP_RANGE |
807                                     DRM_MODE_PROP_IMMUTABLE,
808                                     "bottom margin", 2);
809         dev->mode_config.tv_bottom_margin_property->values[0] = 0;
810         dev->mode_config.tv_bottom_margin_property->values[1] = 100;
811
812         return 0;
813 }
814
815 /**
816  * drm_mode_config_init - initialize DRM mode_configuration structure
817  * @dev: DRM device
818  *
819  * LOCKING:
820  * None, should happen single threaded at init time.
821  *
822  * Initialize @dev's mode_config structure, used for tracking the graphics
823  * configuration of @dev.
824  */
825 void drm_mode_config_init(struct drm_device *dev)
826 {
827         mutex_init(&dev->mode_config.mutex);
828         INIT_LIST_HEAD(&dev->mode_config.fb_list);
829         INIT_LIST_HEAD(&dev->mode_config.crtc_list);
830         INIT_LIST_HEAD(&dev->mode_config.output_list);
831         INIT_LIST_HEAD(&dev->mode_config.property_list);
832         INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
833         idr_init(&dev->mode_config.crtc_idr);
834
835         drm_mode_create_standard_output_properties(dev);
836
837         /* Just to be sure */
838         dev->mode_config.num_fb = 0;
839         dev->mode_config.num_output = 0;
840         dev->mode_config.num_crtc = 0;
841         dev->mode_config.hotplug_counter = 0;
842 }
843 EXPORT_SYMBOL(drm_mode_config_init);
844
845 /**
846  * drm_get_buffer_object - find the buffer object for a given handle
847  * @dev: DRM device
848  * @bo: pointer to caller's buffer_object pointer
849  * @handle: handle to lookup
850  *
851  * LOCKING:
852  * Must take @dev's struct_mutex to protect buffer object lookup.
853  *
854  * Given @handle, lookup the buffer object in @dev and put it in the caller's
855  * @bo pointer.
856  *
857  * RETURNS:
858  * Zero on success, -EINVAL if the handle couldn't be found.
859  */
860 static int drm_get_buffer_object(struct drm_device *dev, struct drm_buffer_object **bo, unsigned long handle)
861 {
862         struct drm_user_object *uo;
863         struct drm_hash_item *hash;
864         int ret;
865
866         *bo = NULL;
867
868         mutex_lock(&dev->struct_mutex);
869         ret = drm_ht_find_item(&dev->object_hash, handle, &hash);
870         if (ret) {
871                 DRM_ERROR("Couldn't find handle.\n");
872                 ret = -EINVAL;
873                 goto out_err;
874         }
875
876         uo = drm_hash_entry(hash, struct drm_user_object, hash);
877         if (uo->type != drm_buffer_type) {
878                 ret = -EINVAL;
879                 goto out_err;
880         }
881         
882         *bo = drm_user_object_entry(uo, struct drm_buffer_object, base);
883         ret = 0;
884 out_err:
885         mutex_unlock(&dev->struct_mutex);
886         return ret;
887 }
888
889 /**
890  * drm_pick_crtcs - pick crtcs for output devices
891  * @dev: DRM device
892  *
893  * LOCKING:
894  * Caller must hold mode config lock.
895  */
896 static void drm_pick_crtcs (struct drm_device *dev)
897 {
898         int c, o, assigned;
899         struct drm_output *output, *output_equal;
900         struct drm_crtc   *crtc;
901         struct drm_display_mode *des_mode = NULL, *modes, *modes_equal;
902         int found;
903
904         list_for_each_entry(output, &dev->mode_config.output_list, head) {
905                 output->crtc = NULL;
906     
907                 /* Don't hook up outputs that are disconnected ??
908                  *
909                  * This is debateable. Do we want fixed /dev/fbX or
910                  * dynamic on hotplug (need mode code for that though) ?
911                  *
912                  * If we don't hook up outputs now, then we only create
913                  * /dev/fbX for the output that's enabled, that's good as
914                  * the users console will be on that output.
915                  *
916                  * If we do hook up outputs that are disconnected now, then
917                  * the user may end up having to muck about with the fbcon
918                  * map flags to assign his console to the enabled output. Ugh.
919                  */
920                 if (output->status != output_status_connected)
921                         continue;
922
923                 if (list_empty(&output->modes))
924                         continue;
925
926                 des_mode = NULL;
927                 found = 0;
928                 list_for_each_entry(des_mode, &output->modes, head) {
929                         if (des_mode->type & DRM_MODE_TYPE_PREFERRED) {
930                                 found = 1;
931                                 break;
932                         }
933                 }
934
935                 /* No preferred mode, let's just select the first available */
936                 if (!found) {
937                         des_mode = NULL;
938                         list_for_each_entry(des_mode, &output->modes, head) {
939                                 break;
940                         }
941                 }
942
943                 c = -1;
944                 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
945                         assigned = 0;
946
947                         c++;
948                         if ((output->possible_crtcs & (1 << c)) == 0)
949                                 continue;
950         
951                         list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
952                                 if (output->id == output_equal->id)
953                                         continue;
954
955                                 /* Find out if crtc has been assigned before */
956                                 if (output_equal->crtc == crtc)
957                                         assigned = 1;
958                         }
959
960 #if 1 /* continue for now */
961                         if (assigned)
962                                 continue;
963 #endif
964
965                         o = -1;
966                         list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
967                                 o++;
968                                 if (output->id == output_equal->id)
969                                         continue;
970
971                                 list_for_each_entry(modes, &output->modes, head) {
972                                         list_for_each_entry(modes_equal, &output_equal->modes, head) {
973                                                 if (drm_mode_equal (modes, modes_equal)) {
974                                                         if ((output->possible_clones & output_equal->possible_clones) && (output_equal->crtc == crtc)) {
975                                                                 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);
976                                                                 des_mode = modes;
977                                                                 assigned = 0;
978                                                                 goto clone;
979                                                         }
980                                                 }
981                                         }
982                                 }
983                         }
984
985 clone:
986                         /* crtc has been assigned skip it */
987                         if (assigned)
988                                 continue;
989
990                         /* Found a CRTC to attach to, do it ! */
991                         output->crtc = crtc;
992                         output->crtc->desired_mode = des_mode;
993                         output->initial_x = 0;
994                         output->initial_y = 0;
995                         DRM_DEBUG("Desired mode for CRTC %d is 0x%x:%s\n",c,des_mode->mode_id, des_mode->name);
996                         break;
997                 }
998         }
999 }
1000 EXPORT_SYMBOL(drm_pick_crtcs);
1001
1002 /**
1003  * drm_initial_config - setup a sane initial output configuration
1004  * @dev: DRM device
1005  * @can_grow: this configuration is growable
1006  *
1007  * LOCKING:
1008  * Called at init time, must take mode config lock.
1009  *
1010  * Scan the CRTCs and outputs and try to put together an initial setup.
1011  * At the moment, this is a cloned configuration across all heads with
1012  * a new framebuffer object as the backing store.
1013  *
1014  * RETURNS:
1015  * Zero if everything went ok, nonzero otherwise.
1016  */
1017 bool drm_initial_config(struct drm_device *dev, bool can_grow)
1018 {
1019         struct drm_output *output;
1020         struct drm_crtc *crtc;
1021         int ret = false;
1022
1023         mutex_lock(&dev->mode_config.mutex);
1024
1025         drm_crtc_probe_output_modes(dev, 2048, 2048);
1026
1027         drm_pick_crtcs(dev);
1028
1029         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1030
1031                 /* can't setup the crtc if there's no assigned mode */
1032                 if (!crtc->desired_mode)
1033                         continue;
1034
1035                 /* Now setup the fbdev for attached crtcs */
1036                 dev->driver->fb_probe(dev, crtc);
1037         }
1038
1039         /* This is a little screwy, as we've already walked the outputs 
1040          * above, but it's a little bit of magic too. There's the potential
1041          * for things not to get setup above if an existing device gets
1042          * re-assigned thus confusing the hardware. By walking the outputs
1043          * this fixes up their crtc's.
1044          */
1045         list_for_each_entry(output, &dev->mode_config.output_list, head) {
1046
1047                 /* can't setup the output if there's no assigned mode */
1048                 if (!output->crtc || !output->crtc->desired_mode)
1049                         continue;
1050
1051                 /* and needs an attached fb */
1052                 if (output->crtc->fb)
1053                         drm_crtc_set_mode(output->crtc, output->crtc->desired_mode, 0, 0);
1054         }
1055
1056         drm_disable_unused_functions(dev);
1057
1058         mutex_unlock(&dev->mode_config.mutex);
1059         return ret;
1060 }
1061 EXPORT_SYMBOL(drm_initial_config);
1062
1063 /**
1064  * drm_mode_config_cleanup - free up DRM mode_config info
1065  * @dev: DRM device
1066  *
1067  * LOCKING:
1068  * Caller must hold mode config lock.
1069  *
1070  * Free up all the outputs and CRTCs associated with this DRM device, then
1071  * free up the framebuffers and associated buffer objects.
1072  *
1073  * FIXME: cleanup any dangling user buffer objects too
1074  */
1075 void drm_mode_config_cleanup(struct drm_device *dev)
1076 {
1077         struct drm_output *output, *ot;
1078         struct drm_crtc *crtc, *ct;
1079         struct drm_framebuffer *fb, *fbt;
1080         struct drm_property *property, *pt;
1081
1082         list_for_each_entry_safe(output, ot, &dev->mode_config.output_list, head) {
1083                 drm_output_destroy(output);
1084         }
1085
1086         list_for_each_entry_safe(property, pt, &dev->mode_config.property_list, head) {
1087                 drm_property_destroy(dev, property);
1088         }
1089
1090         list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1091                 /* there should only be bo of kernel type left */
1092                 if (fb->bo->type != drm_bo_type_kernel)
1093                         drm_framebuffer_destroy(fb);
1094                 else
1095                         dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1096         }
1097
1098         list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1099                 drm_crtc_destroy(crtc);
1100         }
1101
1102 }
1103 EXPORT_SYMBOL(drm_mode_config_cleanup);
1104
1105 /**
1106  * drm_crtc_set_config - set a new config from userspace
1107  * @crtc: CRTC to setup
1108  * @crtc_info: user provided configuration
1109  * @new_mode: new mode to set
1110  * @output_set: set of outputs for the new config
1111  * @fb: new framebuffer
1112  *
1113  * LOCKING:
1114  * Caller must hold mode config lock.
1115  *
1116  * Setup a new configuration, provided by the user in @crtc_info, and enable
1117  * it.
1118  *
1119  * RETURNS:
1120  * Zero. (FIXME)
1121  */
1122 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)
1123 {
1124         struct drm_device *dev = crtc->dev;
1125         struct drm_crtc **save_crtcs, *new_crtc;
1126         bool save_enabled = crtc->enabled;
1127         bool changed = false;
1128         bool flip_or_move = false;
1129         struct drm_output *output;
1130         int count = 0, ro;
1131
1132         /* this is meant to be num_output not num_crtc */
1133         save_crtcs = kzalloc(dev->mode_config.num_output * sizeof(struct drm_crtc *), GFP_KERNEL);
1134         if (!save_crtcs)
1135                 return -ENOMEM;
1136
1137         /* We should be able to check here if the fb has the same properties
1138          * and then just flip_or_move it */
1139         if (crtc->fb != fb)
1140                 flip_or_move = true;
1141
1142         if (crtc_info->x != crtc->x || crtc_info->y != crtc->y)
1143                 flip_or_move = true;
1144
1145         if (new_mode && !drm_mode_equal(new_mode, &crtc->mode)) {
1146                 DRM_DEBUG("modes are different\n");
1147                 drm_mode_debug_printmodeline(dev, &crtc->mode);
1148                 drm_mode_debug_printmodeline(dev, new_mode);
1149                 changed = true;
1150         }
1151
1152         list_for_each_entry(output, &dev->mode_config.output_list, head) {
1153                 save_crtcs[count++] = output->crtc;
1154
1155                 if (output->crtc == crtc)
1156                         new_crtc = NULL;
1157                 else
1158                         new_crtc = output->crtc;
1159
1160                 for (ro = 0; ro < crtc_info->count_outputs; ro++) {
1161                         if (output_set[ro] == output)
1162                                 new_crtc = crtc;
1163                 }
1164                 if (new_crtc != output->crtc) {
1165                         changed = true;
1166                         output->crtc = new_crtc;
1167                 }
1168         }
1169
1170         /* mode_set_base is not a required function */
1171         if (flip_or_move && !crtc->funcs->mode_set_base)
1172                 changed = true;
1173
1174         if (changed) {
1175                 crtc->fb = fb;
1176                 crtc->enabled = (new_mode != NULL);
1177                 if (new_mode != NULL) {
1178                         DRM_DEBUG("attempting to set mode from userspace\n");
1179                         drm_mode_debug_printmodeline(dev, new_mode);
1180                         if (!drm_crtc_set_mode(crtc, new_mode, crtc_info->x,
1181                                                crtc_info->y)) {
1182                                 crtc->enabled = save_enabled;
1183                                 count = 0;
1184                                 list_for_each_entry(output, &dev->mode_config.output_list, head)
1185                                         output->crtc = save_crtcs[count++];
1186                                 kfree(save_crtcs);
1187                                 return -EINVAL;
1188                         }
1189                         crtc->desired_x = crtc_info->x;
1190                         crtc->desired_y = crtc_info->y;
1191                         crtc->desired_mode = new_mode;
1192                 }
1193                 drm_disable_unused_functions(dev);
1194         } else if (flip_or_move) {
1195                 if (crtc->fb != fb)
1196                         crtc->fb = fb;
1197                 crtc->funcs->mode_set_base(crtc, crtc_info->x, crtc_info->y);
1198         }
1199
1200         kfree(save_crtcs);
1201         return 0;
1202 }
1203
1204 /**
1205  * drm_hotplug_stage_two
1206  * @dev DRM device
1207  * @output hotpluged output
1208  *
1209  * LOCKING.
1210  * Caller must hold mode config lock, function might grab struct lock.
1211  *
1212  * Stage two of a hotplug.
1213  *
1214  * RETURNS:
1215  * Zero on success, errno on failure.
1216  */
1217 int drm_hotplug_stage_two(struct drm_device *dev, struct drm_output *output,
1218                           bool connected)
1219 {
1220         int has_config = 0;
1221
1222         dev->mode_config.hotplug_counter++;
1223
1224         /* We might want to do something more here */
1225         if (!connected) {
1226                 DRM_DEBUG("not connected\n");
1227                 return 0;
1228         }
1229
1230         if (output->crtc && output->crtc->desired_mode) {
1231                 DRM_DEBUG("drm thinks that the output already has a config\n");
1232                 has_config = 1;
1233         }
1234
1235         drm_crtc_probe_output_modes(dev, 2048, 2048);
1236
1237         if (!has_config)
1238                 drm_pick_crtcs(dev);
1239
1240         if (!output->crtc || !output->crtc->desired_mode) {
1241                 DRM_DEBUG("could not find a desired mode or crtc for output\n");
1242                 return 1;
1243         }
1244
1245         /* We should really check if there is a fb using this crtc */
1246         if (!has_config)
1247                 dev->driver->fb_probe(dev, output->crtc);
1248         else {
1249                 dev->driver->fb_resize(dev, output->crtc);
1250
1251                 if (!drm_crtc_set_mode(output->crtc, output->crtc->desired_mode, 0, 0))
1252                         DRM_ERROR("failed to set mode after hotplug\n");
1253         }
1254
1255         drm_sysfs_hotplug_event(dev);
1256
1257         drm_disable_unused_functions(dev);
1258
1259         return 0;
1260 }
1261 EXPORT_SYMBOL(drm_hotplug_stage_two);
1262
1263 int drm_mode_hotplug_ioctl(struct drm_device *dev,
1264                            void *data, struct drm_file *file_priv)
1265 {
1266         struct drm_mode_hotplug *arg = data;
1267
1268         arg->counter = dev->mode_config.hotplug_counter;
1269
1270         return 0;
1271 }
1272
1273 /**
1274  * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1275  * @out: drm_mode_modeinfo struct to return to the user
1276  * @in: drm_display_mode to use
1277  *
1278  * LOCKING:
1279  * None.
1280  *
1281  * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1282  * the user.
1283  */
1284 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out, struct drm_display_mode *in)
1285 {
1286         out->clock = in->clock;
1287         out->hdisplay = in->hdisplay;
1288         out->hsync_start = in->hsync_start;
1289         out->hsync_end = in->hsync_end;
1290         out->htotal = in->htotal;
1291         out->hskew = in->hskew;
1292         out->vdisplay = in->vdisplay;
1293         out->vsync_start = in->vsync_start;
1294         out->vsync_end = in->vsync_end;
1295         out->vtotal = in->vtotal;
1296         out->vscan = in->vscan;
1297         out->vrefresh = in->vrefresh;
1298         out->flags = in->flags;
1299         out->type = in->type;
1300         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1301         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1302 }
1303
1304 /**
1305  * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1306  * @out: drm_display_mode to return to the user
1307  * @in: drm_mode_modeinfo to use
1308  *
1309  * LOCKING:
1310  * None.
1311  *
1312  * Convert a drmo_mode_modeinfo into a drm_display_mode structure to return to
1313  * the caller.
1314  */
1315 void drm_crtc_convert_umode(struct drm_display_mode *out, struct drm_mode_modeinfo *in)
1316 {
1317         out->clock = in->clock;
1318         out->hdisplay = in->hdisplay;
1319         out->hsync_start = in->hsync_start;
1320         out->hsync_end = in->hsync_end;
1321         out->htotal = in->htotal;
1322         out->hskew = in->hskew;
1323         out->vdisplay = in->vdisplay;
1324         out->vsync_start = in->vsync_start;
1325         out->vsync_end = in->vsync_end;
1326         out->vtotal = in->vtotal;
1327         out->vscan = in->vscan;
1328         out->vrefresh = in->vrefresh;
1329         out->flags = in->flags;
1330         out->type = in->type;
1331         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1332         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1333 }
1334         
1335 /**
1336  * drm_mode_getresources - get graphics configuration
1337  * @inode: inode from the ioctl
1338  * @filp: file * from the ioctl
1339  * @cmd: cmd from ioctl
1340  * @arg: arg from ioctl
1341  *
1342  * LOCKING:
1343  * Takes mode config lock.
1344  *
1345  * Construct a set of configuration description structures and return
1346  * them to the user, including CRTC, output and framebuffer configuration.
1347  *
1348  * Called by the user via ioctl.
1349  *
1350  * RETURNS:
1351  * Zero on success, errno on failure.
1352  */
1353 int drm_mode_getresources(struct drm_device *dev,
1354                           void *data, struct drm_file *file_priv)
1355 {
1356         struct drm_mode_card_res *card_res = data;
1357         struct list_head *lh;
1358         struct drm_framebuffer *fb;
1359         struct drm_output *output;
1360         struct drm_crtc *crtc;
1361         int ret = 0;
1362         int output_count = 0;
1363         int crtc_count = 0;
1364         int fb_count = 0;
1365         int copied = 0;
1366         uint32_t __user *fb_id;
1367         uint32_t __user *crtc_id;
1368         uint32_t __user *output_id;
1369
1370         mutex_lock(&dev->mode_config.mutex);
1371
1372         list_for_each(lh, &dev->mode_config.fb_list)
1373                 fb_count++;
1374
1375         list_for_each(lh, &dev->mode_config.crtc_list)
1376                 crtc_count++;
1377
1378         list_for_each(lh, &dev->mode_config.output_list)
1379                 output_count++;
1380
1381         card_res->max_height = dev->mode_config.max_height;
1382         card_res->min_height = dev->mode_config.min_height;
1383         card_res->max_width = dev->mode_config.max_width;
1384         card_res->min_width = dev->mode_config.min_width;
1385
1386         /* handle this in 4 parts */
1387         /* FBs */
1388         if (card_res->count_fbs >= fb_count) {
1389                 copied = 0;
1390                 fb_id = (uint32_t *)(unsigned long)card_res->fb_id_ptr;
1391                 list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
1392                         if (put_user(fb->id, fb_id + copied)) {
1393                                 ret = -EFAULT;
1394                                 goto out;
1395                         }
1396                         copied++;
1397                 }
1398         }
1399         card_res->count_fbs = fb_count;
1400
1401         /* CRTCs */
1402         if (card_res->count_crtcs >= crtc_count) {
1403                 copied = 0;
1404                 crtc_id = (uint32_t *)(unsigned long)card_res->crtc_id_ptr;
1405                 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head){
1406                         DRM_DEBUG("CRTC ID is %d\n", crtc->id);
1407                         if (put_user(crtc->id, crtc_id + copied)) {
1408                                 ret = -EFAULT;
1409                                 goto out;
1410                         }
1411                         copied++;
1412                 }
1413         }
1414         card_res->count_crtcs = crtc_count;
1415
1416
1417         /* Outputs */
1418         if (card_res->count_outputs >= output_count) {
1419                 copied = 0;
1420                 output_id = (uint32_t *)(unsigned long)card_res->output_id_ptr;
1421                 list_for_each_entry(output, &dev->mode_config.output_list,
1422                                     head) {
1423                         DRM_DEBUG("OUTPUT ID is %d\n", output->id);
1424                         if (put_user(output->id, output_id + copied)) {
1425                                 ret = -EFAULT;
1426                                 goto out;
1427                         }
1428                         copied++;
1429                 }
1430         }
1431         card_res->count_outputs = output_count;
1432         
1433         DRM_DEBUG("Counted %d %d\n", card_res->count_crtcs,
1434                   card_res->count_outputs);
1435
1436 out:    
1437         mutex_unlock(&dev->mode_config.mutex);
1438         return ret;
1439 }
1440
1441 /**
1442  * drm_mode_getcrtc - get CRTC configuration
1443  * @inode: inode from the ioctl
1444  * @filp: file * from the ioctl
1445  * @cmd: cmd from ioctl
1446  * @arg: arg from ioctl
1447  *
1448  * LOCKING:
1449  * Caller? (FIXME)
1450  *
1451  * Construct a CRTC configuration structure to return to the user.
1452  *
1453  * Called by the user via ioctl.
1454  *
1455  * RETURNS:
1456  * Zero on success, errno on failure.
1457  */
1458 int drm_mode_getcrtc(struct drm_device *dev,
1459                      void *data, struct drm_file *file_priv)
1460 {
1461         struct drm_mode_crtc *crtc_resp = data;
1462         struct drm_crtc *crtc;
1463         struct drm_output *output;
1464         int ocount;
1465         int ret = 0;
1466
1467         mutex_lock(&dev->mode_config.mutex);
1468         crtc = idr_find(&dev->mode_config.crtc_idr, crtc_resp->crtc_id);
1469         if (!crtc || (crtc->id != crtc_resp->crtc_id)) {
1470                 ret = -EINVAL;
1471                 goto out;
1472         }
1473
1474         crtc_resp->x = crtc->x;
1475         crtc_resp->y = crtc->y;
1476
1477         if (crtc->fb)
1478                 crtc_resp->fb_id = crtc->fb->id;
1479         else
1480                 crtc_resp->fb_id = 0;
1481
1482         crtc_resp->outputs = 0;
1483         if (crtc->enabled) {
1484
1485                 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1486                 crtc_resp->mode_valid = 1;
1487                 ocount = 0;
1488                 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1489                         if (output->crtc == crtc)
1490                                 crtc_resp->outputs |= 1 << (ocount++);
1491                 }
1492                 
1493         } else {
1494                 crtc_resp->mode_valid = 0;
1495         }
1496
1497 out:
1498         mutex_unlock(&dev->mode_config.mutex);
1499         return ret;
1500 }
1501
1502 /**
1503  * drm_mode_getoutput - get output configuration
1504  * @inode: inode from the ioctl
1505  * @filp: file * from the ioctl
1506  * @cmd: cmd from ioctl
1507  * @arg: arg from ioctl
1508  *
1509  * LOCKING:
1510  * Caller? (FIXME)
1511  *
1512  * Construct a output configuration structure to return to the user.
1513  *
1514  * Called by the user via ioctl.
1515  *
1516  * RETURNS:
1517  * Zero on success, errno on failure.
1518  */
1519 int drm_mode_getoutput(struct drm_device *dev,
1520                        void *data, struct drm_file *file_priv)
1521 {
1522         struct drm_mode_get_output *out_resp = data;
1523         struct drm_output *output;
1524         struct drm_display_mode *mode;
1525         int mode_count = 0;
1526         int props_count = 0;
1527         int ret = 0;
1528         int copied = 0;
1529         int i;
1530         struct drm_mode_modeinfo u_mode;
1531         struct drm_mode_modeinfo __user *mode_ptr;
1532         uint32_t __user *prop_ptr;
1533         uint64_t __user *prop_values;
1534
1535         memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1536
1537         DRM_DEBUG("output id %d:\n", out_resp->output);
1538
1539         mutex_lock(&dev->mode_config.mutex);
1540         output= idr_find(&dev->mode_config.crtc_idr, out_resp->output);
1541         if (!output || (output->id != out_resp->output)) {
1542                 ret = -EINVAL;
1543                 goto out;
1544         }
1545
1546         list_for_each_entry(mode, &output->modes, head)
1547                 mode_count++;
1548         
1549         for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1550                 if (output->property_ids[i] != 0) {
1551                         props_count++;
1552                 }
1553         }
1554
1555         if (out_resp->count_modes == 0) {
1556                 drm_crtc_probe_single_output_modes(output, dev->mode_config.max_width, dev->mode_config.max_height);
1557         }
1558
1559         out_resp->output_type = output->output_type;
1560         out_resp->output_type_id = output->output_type_id;
1561         out_resp->mm_width = output->mm_width;
1562         out_resp->mm_height = output->mm_height;
1563         out_resp->subpixel = output->subpixel_order;
1564         out_resp->connection = output->status;
1565         if (output->crtc)
1566                 out_resp->crtc = output->crtc->id;
1567         else
1568                 out_resp->crtc = 0;
1569
1570         out_resp->crtcs = output->possible_crtcs;
1571         out_resp->clones = output->possible_clones;
1572
1573         if ((out_resp->count_modes >= mode_count) && mode_count) {
1574                 copied = 0;
1575                 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1576                 list_for_each_entry(mode, &output->modes, head) {
1577                         drm_crtc_convert_to_umode(&u_mode, mode);
1578                         if (copy_to_user(mode_ptr + copied,
1579                                          &u_mode, sizeof(u_mode))) {
1580                                 ret = -EFAULT;
1581                                 goto out;
1582                         }
1583                         copied++;
1584                         
1585                 }
1586         }
1587         out_resp->count_modes = mode_count;
1588
1589         if ((out_resp->count_props >= props_count) && props_count) {
1590                 copied = 0;
1591                 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1592                 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1593                 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1594                         if (output->property_ids[i] != 0) {
1595                                 if (put_user(output->property_ids[i], prop_ptr + copied)) {
1596                                         ret = -EFAULT;
1597                                         goto out;
1598                                 }
1599
1600                                 if (put_user(output->property_values[i], prop_values + copied)) {
1601                                         ret = -EFAULT;
1602                                         goto out;
1603                                 }
1604                                 copied++;
1605                         }
1606                 }
1607         }
1608         out_resp->count_props = props_count;
1609
1610 out:
1611         mutex_unlock(&dev->mode_config.mutex);
1612         return ret;
1613 }
1614
1615 /**
1616  * drm_mode_setcrtc - set CRTC configuration
1617  * @inode: inode from the ioctl
1618  * @filp: file * from the ioctl
1619  * @cmd: cmd from ioctl
1620  * @arg: arg from ioctl
1621  *
1622  * LOCKING:
1623  * Caller? (FIXME)
1624  *
1625  * Build a new CRTC configuration based on user request.
1626  *
1627  * Called by the user via ioctl.
1628  *
1629  * RETURNS:
1630  * Zero on success, errno on failure.
1631  */
1632 int drm_mode_setcrtc(struct drm_device *dev,
1633                      void *data, struct drm_file *file_priv)
1634 {
1635         struct drm_mode_crtc *crtc_req = data;
1636         struct drm_crtc *crtc, *crtcfb;
1637         struct drm_output **output_set = NULL, *output;
1638         struct drm_framebuffer *fb = NULL;
1639         struct drm_display_mode *mode = NULL;
1640         uint32_t __user *set_outputs_ptr;
1641         int ret = 0;
1642         int i;
1643
1644         mutex_lock(&dev->mode_config.mutex);
1645         crtc = idr_find(&dev->mode_config.crtc_idr, crtc_req->crtc_id);
1646         if (!crtc || (crtc->id != crtc_req->crtc_id)) {
1647                 DRM_DEBUG("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1648                 ret = -EINVAL;
1649                 goto out;
1650         }
1651
1652         if (crtc_req->mode_valid) {
1653                 /* If we have a mode we need a framebuffer. */
1654                 /* If we pass -1, set the mode with the currently bound fb */
1655                 if (crtc_req->fb_id == -1) {
1656                         list_for_each_entry(crtcfb, &dev->mode_config.crtc_list, head) {
1657                                 if (crtcfb == crtc) {
1658                                         DRM_DEBUG("Using current fb for setmode\n");
1659                                         fb = crtc->fb;          
1660                                 }
1661                         }
1662                 } else {
1663                         fb = idr_find(&dev->mode_config.crtc_idr, crtc_req->fb_id);
1664                         if (!fb || (fb->id != crtc_req->fb_id)) {
1665                                 DRM_DEBUG("Unknown FB ID%d\n", crtc_req->fb_id);
1666                                 ret = -EINVAL;
1667                                 goto out;
1668                         }
1669                 }
1670
1671                 mode = drm_mode_create(dev);
1672                 drm_crtc_convert_umode(mode, &crtc_req->mode);
1673                 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1674         }
1675
1676         if (crtc_req->count_outputs == 0 && mode) {
1677                 DRM_DEBUG("Count outputs is 0 but mode set\n");
1678                 ret = -EINVAL;
1679                 goto out;
1680         }
1681
1682         if (crtc_req->count_outputs > 0 && !mode && !fb) {
1683                 DRM_DEBUG("Count outputs is %d but no mode or fb set\n", crtc_req->count_outputs);
1684                 ret = -EINVAL;
1685                 goto out;
1686         }
1687
1688         if (crtc_req->count_outputs > 0) {
1689                 u32 out_id;
1690                 /* Maybe we should check that count_outputs is a sensible value. */
1691                 output_set = kmalloc(crtc_req->count_outputs *
1692                                      sizeof(struct drm_output *), GFP_KERNEL);
1693                 if (!output_set) {
1694                         ret = -ENOMEM;
1695                         goto out;
1696                 }
1697
1698                 for (i = 0; i < crtc_req->count_outputs; i++) {
1699                         set_outputs_ptr = (uint32_t *)(unsigned long)crtc_req->set_outputs_ptr;
1700                         if (get_user(out_id, &set_outputs_ptr[i])) {
1701                                 ret = -EFAULT;
1702                                 goto out;
1703                         }
1704
1705                         output = idr_find(&dev->mode_config.crtc_idr, out_id);
1706                         if (!output || (out_id != output->id)) {
1707                                 DRM_DEBUG("Output id %d unknown\n", out_id);
1708                                 ret = -EINVAL;
1709                                 goto out;
1710                         }
1711
1712                         output_set[i] = output;
1713                 }
1714         }
1715
1716         ret = drm_crtc_set_config(crtc, crtc_req, mode, output_set, fb);
1717
1718 out:
1719         kfree(output_set);
1720         mutex_unlock(&dev->mode_config.mutex);
1721         return ret;
1722 }
1723
1724 int drm_mode_cursor_ioctl(struct drm_device *dev,
1725                         void *data, struct drm_file *file_priv)
1726 {
1727         struct drm_mode_cursor *req = data;
1728         struct drm_crtc *crtc;
1729         struct drm_buffer_object *bo = NULL; /* must be set */
1730         int ret = 0;
1731
1732         DRM_DEBUG("\n");
1733
1734         if (!req->flags) {
1735                 DRM_ERROR("no operation set\n");
1736                 return -EINVAL;
1737         }
1738
1739         mutex_lock(&dev->mode_config.mutex);
1740         crtc = idr_find(&dev->mode_config.crtc_idr, req->crtc);
1741         if (!crtc || (crtc->id != req->crtc)) {
1742                 DRM_DEBUG("Unknown CRTC ID %d\n", req->crtc);
1743                 ret = -EINVAL;
1744                 goto out;
1745         }
1746
1747         if (req->flags & DRM_MODE_CURSOR_BO) {
1748                 /* Turn of the cursor if handle is 0 */
1749                 if (req->handle)
1750                         ret = drm_get_buffer_object(dev, &bo, req->handle);
1751
1752                 if (ret) {
1753                         DRM_ERROR("invalid buffer id\n");
1754                         ret = -EINVAL;
1755                         goto out;
1756                 }
1757
1758                 if (crtc->funcs->cursor_set) {
1759                         ret = crtc->funcs->cursor_set(crtc, bo, req->width, req->height);
1760                 } else {
1761                         DRM_ERROR("crtc does not support cursor\n");
1762                         ret = -EFAULT;
1763                         goto out;
1764                 }
1765         }
1766
1767         if (req->flags & DRM_MODE_CURSOR_MOVE) {
1768                 if (crtc->funcs->cursor_move) {
1769                         ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1770                 } else {
1771                         DRM_ERROR("crtc does not support cursor\n");
1772                         ret = -EFAULT;
1773                         goto out;
1774                 }
1775         }
1776 out:
1777         mutex_unlock(&dev->mode_config.mutex);
1778         return ret;
1779 }
1780
1781 /**
1782  * drm_mode_addfb - add an FB to the graphics configuration
1783  * @inode: inode from the ioctl
1784  * @filp: file * from the ioctl
1785  * @cmd: cmd from ioctl
1786  * @arg: arg from ioctl
1787  *
1788  * LOCKING:
1789  * Takes mode config lock.
1790  *
1791  * Add a new FB to the specified CRTC, given a user request.
1792  *
1793  * Called by the user via ioctl.
1794  *
1795  * RETURNS:
1796  * Zero on success, errno on failure.
1797  */
1798 int drm_mode_addfb(struct drm_device *dev,
1799                    void *data, struct drm_file *file_priv)
1800 {
1801         struct drm_mode_fb_cmd *r = data;
1802         struct drm_mode_config *config = &dev->mode_config;
1803         struct drm_framebuffer *fb;
1804         struct drm_buffer_object *bo;
1805         int ret = 0;
1806
1807         if ((config->min_width > r->width) || (r->width > config->max_width)) {
1808                 DRM_ERROR("mode new framebuffer width not within limits\n");
1809                 return -EINVAL;
1810         }
1811         if ((config->min_height > r->height) || (r->height > config->max_height)) {
1812                 DRM_ERROR("mode new framebuffer height not within limits\n");
1813                 return -EINVAL;
1814         }
1815
1816         mutex_lock(&dev->mode_config.mutex);
1817         /* TODO check limits are okay */
1818         ret = drm_get_buffer_object(dev, &bo, r->handle);
1819         if (ret || !bo) {
1820                 DRM_ERROR("BO handle not valid\n");
1821                 ret = -EINVAL;
1822                 goto out;
1823         }
1824
1825         /* TODO check buffer is sufficently large */
1826         /* TODO setup destructor callback */
1827
1828         fb = drm_framebuffer_create(dev);
1829         if (!fb) {
1830                 DRM_ERROR("could not create framebuffer\n");
1831                 ret = -EINVAL;
1832                 goto out;
1833         }
1834
1835         fb->width = r->width;
1836         fb->height = r->height;
1837         fb->pitch = r->pitch;
1838         fb->bits_per_pixel = r->bpp;
1839         fb->depth = r->depth;
1840         fb->bo = bo;
1841
1842         r->buffer_id = fb->id;
1843
1844         list_add(&fb->filp_head, &file_priv->fbs);
1845
1846 out:
1847         mutex_unlock(&dev->mode_config.mutex);
1848         return ret;
1849 }
1850
1851 /**
1852  * drm_mode_rmfb - remove an FB from the configuration
1853  * @inode: inode from the ioctl
1854  * @filp: file * from the ioctl
1855  * @cmd: cmd from ioctl
1856  * @arg: arg from ioctl
1857  *
1858  * LOCKING:
1859  * Takes mode config lock.
1860  *
1861  * Remove the FB specified by the user.
1862  *
1863  * Called by the user via ioctl.
1864  *
1865  * RETURNS:
1866  * Zero on success, errno on failure.
1867  */
1868 int drm_mode_rmfb(struct drm_device *dev,
1869                    void *data, struct drm_file *file_priv)
1870 {
1871         struct drm_framebuffer *fb = 0;
1872         struct drm_framebuffer *fbl = 0;
1873         uint32_t *id = data;
1874         int ret = 0;
1875         int found = 0;
1876
1877         mutex_lock(&dev->mode_config.mutex);
1878         fb = idr_find(&dev->mode_config.crtc_idr, *id);
1879         /* TODO check that we realy get a framebuffer back. */
1880         if (!fb || (*id != fb->id)) {
1881                 DRM_ERROR("mode invalid framebuffer id\n");
1882                 ret = -EINVAL;
1883                 goto out;
1884         }
1885
1886         list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1887                 if (fb == fbl)
1888                         found = 1;
1889
1890         if (!found) {
1891                 DRM_ERROR("tried to remove a fb that we didn't own\n");
1892                 ret = -EINVAL;
1893                 goto out;
1894         }
1895
1896         /* TODO release all crtc connected to the framebuffer */
1897         /* TODO unhock the destructor from the buffer object */
1898
1899         if (fb->bo->type == drm_bo_type_kernel)
1900                 DRM_ERROR("the bo type should not be of kernel type\n");
1901
1902         list_del(&fb->filp_head);
1903         drm_framebuffer_destroy(fb);
1904
1905 out:
1906         mutex_unlock(&dev->mode_config.mutex);
1907         return ret;
1908 }
1909
1910 /**
1911  * drm_mode_getfb - get FB info
1912  * @inode: inode from the ioctl
1913  * @filp: file * from the ioctl
1914  * @cmd: cmd from ioctl
1915  * @arg: arg from ioctl
1916  *
1917  * LOCKING:
1918  * Caller? (FIXME)
1919  *
1920  * Lookup the FB given its ID and return info about it.
1921  *
1922  * Called by the user via ioctl.
1923  *
1924  * RETURNS:
1925  * Zero on success, errno on failure.
1926  */
1927 int drm_mode_getfb(struct drm_device *dev,
1928                    void *data, struct drm_file *file_priv)
1929 {
1930         struct drm_mode_fb_cmd *r = data;
1931         struct drm_framebuffer *fb;
1932         int ret = 0;
1933
1934         mutex_lock(&dev->mode_config.mutex);
1935         fb = idr_find(&dev->mode_config.crtc_idr, r->buffer_id);
1936         if (!fb || (r->buffer_id != fb->id)) {
1937                 DRM_ERROR("invalid framebuffer id\n");
1938                 ret = -EINVAL;
1939                 goto out;
1940         }
1941
1942         r->height = fb->height;
1943         r->width = fb->width;
1944         r->depth = fb->depth;
1945         r->bpp = fb->bits_per_pixel;
1946         r->handle = fb->bo->base.hash.key;
1947         r->pitch = fb->pitch;
1948
1949 out:
1950         mutex_unlock(&dev->mode_config.mutex);
1951         return ret;
1952 }
1953
1954 /**
1955  * drm_fb_release - remove and free the FBs on this file
1956  * @filp: file * from the ioctl
1957  *
1958  * LOCKING:
1959  * Takes mode config lock.
1960  *
1961  * Destroy all the FBs associated with @filp.
1962  *
1963  * Called by the user via ioctl.
1964  *
1965  * RETURNS:
1966  * Zero on success, errno on failure.
1967  */
1968 void drm_fb_release(struct file *filp)
1969 {
1970         struct drm_file *priv = filp->private_data;
1971         struct drm_device *dev = priv->minor->dev;
1972         struct drm_framebuffer *fb, *tfb;
1973
1974         mutex_lock(&dev->mode_config.mutex);
1975         list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1976                 list_del(&fb->filp_head);
1977                 if (fb->bo->type == drm_bo_type_kernel)
1978                         DRM_ERROR("the bo type should not be of kernel_type, the kernel will probably explode, why Dave\n");
1979
1980                 drm_framebuffer_destroy(fb);
1981         }
1982         mutex_unlock(&dev->mode_config.mutex);
1983 }
1984
1985 /*
1986  *
1987  */
1988
1989 static int drm_mode_attachmode(struct drm_device *dev,
1990                                struct drm_output *output,
1991                                struct drm_display_mode *mode)
1992 {
1993         int ret = 0;
1994
1995         list_add_tail(&mode->head, &output->user_modes);
1996         return ret;
1997 }
1998
1999 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
2000                              struct drm_display_mode *mode)
2001 {
2002         struct drm_output *output;
2003         int ret = 0;
2004         struct drm_display_mode *dup_mode;
2005         int need_dup = 0;
2006         list_for_each_entry(output, &dev->mode_config.output_list, head) {
2007                 if (output->crtc == crtc) {
2008                         if (need_dup)
2009                                 dup_mode = drm_mode_duplicate(dev, mode);
2010                         else
2011                                 dup_mode = mode;
2012                         ret = drm_mode_attachmode(dev, output, dup_mode); 
2013                         if (ret)
2014                                 return ret;
2015                         need_dup = 1;
2016                 }
2017         }
2018         return 0;
2019 }
2020 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2021
2022 static int drm_mode_detachmode(struct drm_device *dev,
2023                                struct drm_output *output,
2024                                struct drm_display_mode *mode)
2025 {
2026         int found = 0;
2027         int ret = 0;
2028         struct drm_display_mode *match_mode, *t;
2029
2030         list_for_each_entry_safe(match_mode, t, &output->user_modes, head) {
2031                 if (drm_mode_equal(match_mode, mode)) {
2032                         list_del(&match_mode->head);
2033                         drm_mode_destroy(dev, match_mode);
2034                         found = 1;
2035                         break;
2036                 }
2037         }
2038
2039         if (!found)
2040                 ret = -EINVAL;
2041
2042         return ret;
2043 }
2044
2045 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2046 {
2047         struct drm_output *output;
2048
2049         list_for_each_entry(output, &dev->mode_config.output_list, head) {
2050                 drm_mode_detachmode(dev, output, mode);
2051         }
2052         return 0;
2053 }
2054 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2055
2056 /**
2057  * drm_fb_attachmode - Attach a user mode to an output
2058  * @inode: inode from the ioctl
2059  * @filp: file * from the ioctl
2060  * @cmd: cmd from ioctl
2061  * @arg: arg from ioctl
2062  *
2063  * This attaches a user specified mode to an output.
2064  * Called by the user via ioctl.
2065  *
2066  * RETURNS:
2067  * Zero on success, errno on failure.
2068  */
2069 int drm_mode_attachmode_ioctl(struct drm_device *dev,
2070                               void *data, struct drm_file *file_priv)
2071 {
2072         struct drm_mode_mode_cmd *mode_cmd = data;
2073         struct drm_output *output;
2074         struct drm_display_mode *mode;
2075         struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2076         int ret = 0;
2077
2078         mutex_lock(&dev->mode_config.mutex);
2079
2080         output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
2081         if (!output || (output->id != mode_cmd->output_id)) {
2082                 ret = -EINVAL;
2083                 goto out;
2084         }
2085
2086         mode = drm_mode_create(dev);
2087         if (!mode) {
2088                 ret = -ENOMEM;
2089                 goto out;
2090         }
2091         
2092         drm_crtc_convert_umode(mode, umode);
2093
2094         ret = drm_mode_attachmode(dev, output, mode);
2095 out:
2096         mutex_unlock(&dev->mode_config.mutex);
2097         return ret;
2098 }
2099
2100
2101 /**
2102  * drm_fb_detachmode - Detach a user specified mode from an output
2103  * @inode: inode from the ioctl
2104  * @filp: file * from the ioctl
2105  * @cmd: cmd from ioctl
2106  * @arg: arg from ioctl
2107  *
2108  * Called by the user via ioctl.
2109  *
2110  * RETURNS:
2111  * Zero on success, errno on failure.
2112  */
2113 int drm_mode_detachmode_ioctl(struct drm_device *dev,
2114                               void *data, struct drm_file *file_priv)
2115 {
2116         struct drm_mode_mode_cmd *mode_cmd = data;
2117         struct drm_output *output;
2118         struct drm_display_mode mode;
2119         struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2120         int ret = 0;
2121
2122         mutex_lock(&dev->mode_config.mutex);
2123
2124         output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
2125         if (!output || (output->id != mode_cmd->output_id)) {
2126                 ret = -EINVAL;
2127                 goto out;
2128         }
2129         
2130         drm_crtc_convert_umode(&mode, umode);
2131         ret = drm_mode_detachmode(dev, output, &mode);
2132 out:           
2133         mutex_unlock(&dev->mode_config.mutex);
2134         return ret;
2135 }
2136
2137 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2138                                          const char *name, int num_values)
2139 {
2140         struct drm_property *property = NULL;
2141
2142         property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2143         if (!property)
2144                 return NULL;
2145
2146         if (num_values) {
2147                 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2148                 if (!property->values)
2149                         goto fail;
2150         }
2151
2152         property->id = drm_idr_get(dev, property);
2153         property->flags = flags;
2154         property->num_values = num_values;
2155         INIT_LIST_HEAD(&property->enum_blob_list);
2156
2157         if (name)
2158                 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2159
2160         list_add_tail(&property->head, &dev->mode_config.property_list);
2161         return property;
2162 fail:
2163         kfree(property);
2164         return NULL;
2165 }
2166 EXPORT_SYMBOL(drm_property_create);
2167
2168 int drm_property_add_enum(struct drm_property *property, int index,
2169                           uint64_t value, const char *name)
2170 {
2171         struct drm_property_enum *prop_enum;
2172
2173         if (!(property->flags & DRM_MODE_PROP_ENUM))
2174                 return -EINVAL;
2175
2176         if (!list_empty(&property->enum_blob_list)) {
2177                 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2178                         if (prop_enum->value == value) {
2179                                 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN); 
2180                                 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2181                                 return 0;
2182                         }
2183                 }
2184         }
2185
2186         prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2187         if (!prop_enum)
2188                 return -ENOMEM;
2189
2190         strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN); 
2191         prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2192         prop_enum->value = value;
2193
2194         property->values[index] = value;
2195         list_add_tail(&prop_enum->head, &property->enum_blob_list);
2196         return 0;
2197 }
2198 EXPORT_SYMBOL(drm_property_add_enum);
2199
2200 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2201 {
2202         struct drm_property_enum *prop_enum, *pt;
2203
2204         list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2205                 list_del(&prop_enum->head);
2206                 kfree(prop_enum);
2207         }
2208
2209         if (property->num_values)
2210                 kfree(property->values);
2211         drm_idr_put(dev, property->id);
2212         list_del(&property->head);
2213         kfree(property);        
2214 }
2215 EXPORT_SYMBOL(drm_property_destroy);
2216
2217 int drm_output_attach_property(struct drm_output *output,
2218                                struct drm_property *property, uint64_t init_val)
2219 {
2220         int i;
2221
2222         for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2223                 if (output->property_ids[i] == 0) {
2224                         output->property_ids[i] = property->id;
2225                         output->property_values[i] = init_val;
2226                         break;
2227                 }
2228         }
2229
2230         if (i == DRM_OUTPUT_MAX_PROPERTY)
2231                 return -EINVAL;
2232         return 0;
2233 }
2234 EXPORT_SYMBOL(drm_output_attach_property);
2235
2236 int drm_output_property_set_value(struct drm_output *output,
2237                                   struct drm_property *property, uint64_t value)
2238 {
2239         int i;
2240
2241         for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2242                 if (output->property_ids[i] == property->id) {
2243                         output->property_values[i] = value;
2244                         break;
2245                 }
2246         }
2247
2248         if (i == DRM_OUTPUT_MAX_PROPERTY)
2249                 return -EINVAL;
2250         return 0;
2251 }
2252 EXPORT_SYMBOL(drm_output_property_set_value);
2253
2254 int drm_output_property_get_value(struct drm_output *output,
2255                                   struct drm_property *property, uint64_t *val)
2256 {
2257         int i;
2258
2259         for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2260                 if (output->property_ids[i] == property->id) {
2261                         *val = output->property_values[i];
2262                         break;
2263                 }
2264         }
2265
2266         if (i == DRM_OUTPUT_MAX_PROPERTY)
2267                 return -EINVAL;
2268         return 0;
2269 }
2270 EXPORT_SYMBOL(drm_output_property_get_value);
2271
2272 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2273                                void *data, struct drm_file *file_priv)
2274 {
2275         struct drm_mode_get_property *out_resp = data;
2276         struct drm_property *property;
2277         int enum_count = 0;
2278         int blob_count = 0;
2279         int value_count = 0;
2280         int ret = 0, i;
2281         int copied;
2282         struct drm_property_enum *prop_enum;
2283         struct drm_mode_property_enum __user *enum_ptr;
2284         struct drm_property_blob *prop_blob;
2285         uint32_t *blob_id_ptr;
2286         uint64_t __user *values_ptr;
2287         uint32_t __user *blob_length_ptr;
2288
2289         mutex_lock(&dev->mode_config.mutex);
2290         property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
2291         if (!property || (property->id != out_resp->prop_id)) {
2292                 ret = -EINVAL;
2293                 goto done;
2294         }
2295
2296         if (property->flags & DRM_MODE_PROP_ENUM) {
2297                 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2298                         enum_count++;
2299         } else if (property->flags & DRM_MODE_PROP_BLOB) {
2300                 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2301                         blob_count++;
2302         }
2303
2304         value_count = property->num_values;
2305
2306         strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2307         out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2308         out_resp->flags = property->flags;
2309
2310         if ((out_resp->count_values >= value_count) && value_count) {
2311                 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2312                 for (i = 0; i < value_count; i++) {
2313                         if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2314                                 ret = -EFAULT;
2315                                 goto done;
2316                         }
2317                 }
2318         }
2319         out_resp->count_values = value_count;
2320
2321         if (property->flags & DRM_MODE_PROP_ENUM) {
2322
2323                 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2324                         copied = 0;
2325                         enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2326                         list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2327                                 
2328                                 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2329                                         ret = -EFAULT;
2330                                         goto done;
2331                                 }
2332                                 
2333                                 if (copy_to_user(&enum_ptr[copied].name,
2334                                                  &prop_enum->name, DRM_PROP_NAME_LEN)) {
2335                                         ret = -EFAULT;
2336                                         goto done;
2337                                 }
2338                                 copied++;
2339                         }
2340                 }
2341                 out_resp->count_enum_blobs = enum_count;
2342         }
2343
2344         if (property->flags & DRM_MODE_PROP_BLOB) {
2345                 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2346                         copied = 0;
2347                         blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2348                         blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2349                         
2350                         list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2351                                 if (put_user(prop_blob->id, blob_id_ptr + copied)) {
2352                                         ret = -EFAULT;
2353                                         goto done;
2354                                 }
2355                                 
2356                                 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2357                                         ret = -EFAULT;
2358                                         goto done;
2359                                 }
2360                                 
2361                                 copied++;
2362                         }
2363                 }
2364                 out_resp->count_enum_blobs = enum_count;
2365         }
2366 done:
2367         mutex_unlock(&dev->mode_config.mutex);
2368         return ret;
2369 }
2370
2371 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2372                                                           void *data)
2373 {
2374         struct drm_property_blob *blob;
2375
2376         if (!length || !data)
2377                 return NULL;
2378
2379         blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2380         if (!blob)
2381                 return NULL;
2382
2383         blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2384         blob->length = length;
2385
2386         memcpy(blob->data, data, length);
2387
2388         blob->id = drm_idr_get(dev, blob);
2389         
2390         list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2391         return blob;
2392 }
2393
2394 static void drm_property_destroy_blob(struct drm_device *dev,
2395                                struct drm_property_blob *blob)
2396 {
2397         drm_idr_put(dev, blob->id);
2398         list_del(&blob->head);
2399         kfree(blob);
2400 }
2401
2402 int drm_mode_getblob_ioctl(struct drm_device *dev,
2403                            void *data, struct drm_file *file_priv)
2404 {
2405         struct drm_mode_get_blob *out_resp = data;
2406         struct drm_property_blob *blob;
2407         int ret = 0;
2408         void *blob_ptr;
2409
2410         mutex_lock(&dev->mode_config.mutex);
2411         
2412         blob = idr_find(&dev->mode_config.crtc_idr, out_resp->blob_id);
2413         if (!blob || (blob->id != out_resp->blob_id)) {
2414                 ret = -EINVAL;
2415                 goto done;
2416         }
2417
2418         if (out_resp->length == blob->length) {
2419                 blob_ptr = (void *)(unsigned long)out_resp->data;
2420                 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2421                         ret = -EFAULT;
2422                         goto done;
2423                 }
2424         }
2425         out_resp->length = blob->length;
2426
2427 done:
2428         mutex_unlock(&dev->mode_config.mutex);
2429         return ret;
2430 }
2431
2432 int drm_mode_output_update_edid_property(struct drm_output *output, struct edid *edid)
2433 {
2434         struct drm_device *dev = output->dev;
2435         int ret = 0;
2436         if (output->edid_blob_ptr)
2437                 drm_property_destroy_blob(dev, output->edid_blob_ptr);
2438
2439         output->edid_blob_ptr = drm_property_create_blob(output->dev, 128, edid);
2440         
2441         ret = drm_output_property_set_value(output, dev->mode_config.edid_property, output->edid_blob_ptr->id);
2442         return ret;
2443 }
2444 EXPORT_SYMBOL(drm_mode_output_update_edid_property);
2445
2446 int drm_mode_output_property_set_ioctl(struct drm_device *dev,
2447                                        void *data, struct drm_file *file_priv)
2448 {
2449         struct drm_mode_output_set_property *out_resp = data;
2450         struct drm_property *property;
2451         struct drm_output *output;
2452         int ret = -EINVAL;
2453         int i;
2454
2455         mutex_lock(&dev->mode_config.mutex);
2456         output = idr_find(&dev->mode_config.crtc_idr, out_resp->output_id);
2457         if (!output || (output->id != out_resp->output_id)) {
2458                 goto out;
2459         }
2460
2461         for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2462                 if (output->property_ids[i] == out_resp->prop_id)
2463                         break;
2464         }
2465
2466         if (i == DRM_OUTPUT_MAX_PROPERTY) {
2467                 goto out;
2468         }
2469         
2470         property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
2471         if (!property || (property->id != out_resp->prop_id)) {
2472                 goto out;
2473         }
2474
2475         if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2476                 goto out;
2477
2478         if (property->flags & DRM_MODE_PROP_RANGE) {
2479                 if (out_resp->value < property->values[0])
2480                         goto out;
2481
2482                 if (out_resp->value > property->values[1])
2483                         goto out;
2484         } else {
2485                 int found = 0;
2486                 for (i = 0; i < property->num_values; i++) {
2487                         if (property->values[i] == out_resp->value) {
2488                                 found = 1;
2489                                 break;
2490                         }
2491                 }
2492                 if (!found) {
2493                         goto out;
2494                 }
2495         }
2496
2497         if (output->funcs->set_property)
2498                 ret = output->funcs->set_property(output, property, out_resp->value);
2499
2500 out:
2501         mutex_unlock(&dev->mode_config.mutex);
2502         return ret;
2503 }
2504