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