oops initialise variable to false
[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 /**
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         mutex_unlock(&dev->mode_config.mutex);
596
597         return output;
598
599 }
600 EXPORT_SYMBOL(drm_output_create);
601
602 /**
603  * drm_output_destroy - remove an output
604  * @output: output to remove
605  *
606  * LOCKING:
607  * Caller must hold @dev's mode_config lock.
608  *
609  * Call @output's cleanup function, then remove the output from the DRM
610  * mode_config after freeing @output's modes.
611  */
612 void drm_output_destroy(struct drm_output *output)
613 {
614         struct drm_device *dev = output->dev;
615         struct drm_display_mode *mode, *t;
616
617         if (*output->funcs->cleanup)
618                 (*output->funcs->cleanup)(output);
619
620         list_for_each_entry_safe(mode, t, &output->probed_modes, head)
621                 drm_mode_remove(output, mode);
622
623         list_for_each_entry_safe(mode, t, &output->modes, head)
624                 drm_mode_remove(output, mode);
625
626         list_for_each_entry_safe(mode, t, &output->user_modes, head)
627                 drm_mode_remove(output, mode);
628
629         mutex_lock(&dev->mode_config.mutex);
630         drm_idr_put(dev, output->id);
631         list_del(&output->head);
632         mutex_unlock(&dev->mode_config.mutex);
633         kfree(output);
634 }
635 EXPORT_SYMBOL(drm_output_destroy);
636
637 /**
638  * drm_output_rename - rename an output
639  * @output: output to rename
640  * @name: new user visible name
641  *
642  * LOCKING:
643  * None.
644  *
645  * Simply stuff a new name into @output's name field, based on @name.
646  *
647  * RETURNS:
648  * True if the name was changed, false otherwise.
649  */
650 bool drm_output_rename(struct drm_output *output, const char *name)
651 {
652         if (!name)
653                 return false;
654
655         strncpy(output->name, name, DRM_OUTPUT_LEN);
656         output->name[DRM_OUTPUT_LEN - 1] = 0;
657
658         DRM_DEBUG("Changed name to %s\n", output->name);
659 //      drm_output_set_monitor(output);
660 //      if (drm_output_ignored(output))
661 //              return FALSE;
662
663         return TRUE;
664 }
665 EXPORT_SYMBOL(drm_output_rename);
666
667 /**
668  * drm_mode_create - create a new display mode
669  * @dev: DRM device
670  *
671  * LOCKING:
672  * None.
673  *
674  * Create a new drm_display_mode, give it an ID, and return it.
675  *
676  * RETURNS:
677  * Pointer to new mode on success, NULL on error.
678  */
679 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
680 {
681         struct drm_display_mode *nmode;
682
683         nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
684         if (!nmode)
685                 return NULL;
686
687         nmode->mode_id = drm_idr_get(dev, nmode);
688         return nmode;
689 }
690 EXPORT_SYMBOL(drm_mode_create);
691
692 /**
693  * drm_mode_destroy - remove a mode
694  * @dev: DRM device
695  * @mode: mode to remove
696  *
697  * LOCKING:
698  * Caller must hold mode config lock.
699  *
700  * Free @mode's unique identifier, then free it.
701  */
702 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
703 {
704         drm_idr_put(dev, mode->mode_id);
705
706         kfree(mode);
707 }
708 EXPORT_SYMBOL(drm_mode_destroy);
709
710 /**
711  * drm_mode_config_init - initialize DRM mode_configuration structure
712  * @dev: DRM device
713  *
714  * LOCKING:
715  * None, should happen single threaded at init time.
716  *
717  * Initialize @dev's mode_config structure, used for tracking the graphics
718  * configuration of @dev.
719  */
720 void drm_mode_config_init(struct drm_device *dev)
721 {
722         mutex_init(&dev->mode_config.mutex);
723         INIT_LIST_HEAD(&dev->mode_config.fb_list);
724         INIT_LIST_HEAD(&dev->mode_config.crtc_list);
725         INIT_LIST_HEAD(&dev->mode_config.output_list);
726         INIT_LIST_HEAD(&dev->mode_config.property_list);
727         INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
728         idr_init(&dev->mode_config.crtc_idr);
729         dev->mode_config.edid_property = drm_property_create(dev,
730                                                              DRM_MODE_PROP_BLOB | DRM_MODE_PROP_IMMUTABLE,
731                                                              "EDID", 0);
732 }
733 EXPORT_SYMBOL(drm_mode_config_init);
734
735 /**
736  * drm_get_buffer_object - find the buffer object for a given handle
737  * @dev: DRM device
738  * @bo: pointer to caller's buffer_object pointer
739  * @handle: handle to lookup
740  *
741  * LOCKING:
742  * Must take @dev's struct_mutex to protect buffer object lookup.
743  *
744  * Given @handle, lookup the buffer object in @dev and put it in the caller's
745  * @bo pointer.
746  *
747  * RETURNS:
748  * Zero on success, -EINVAL if the handle couldn't be found.
749  */
750 static int drm_get_buffer_object(struct drm_device *dev, struct drm_buffer_object **bo, unsigned long handle)
751 {
752         struct drm_user_object *uo;
753         struct drm_hash_item *hash;
754         int ret;
755
756         *bo = NULL;
757
758         mutex_lock(&dev->struct_mutex);
759         ret = drm_ht_find_item(&dev->object_hash, handle, &hash);
760         if (ret) {
761                 DRM_ERROR("Couldn't find handle.\n");
762                 ret = -EINVAL;
763                 goto out_err;
764         }
765
766         uo = drm_hash_entry(hash, struct drm_user_object, hash);
767         if (uo->type != drm_buffer_type) {
768                 ret = -EINVAL;
769                 goto out_err;
770         }
771         
772         *bo = drm_user_object_entry(uo, struct drm_buffer_object, base);
773         ret = 0;
774 out_err:
775         mutex_unlock(&dev->struct_mutex);
776         return ret;
777 }
778
779 /**
780  * drm_pick_crtcs - pick crtcs for output devices
781  * @dev: DRM device
782  *
783  * LOCKING:
784  * Caller must hold mode config lock.
785  */
786 static void drm_pick_crtcs (struct drm_device *dev)
787 {
788         int c, o, assigned;
789         struct drm_output *output, *output_equal;
790         struct drm_crtc   *crtc;
791         struct drm_display_mode *des_mode = NULL, *modes, *modes_equal;
792
793         list_for_each_entry(output, &dev->mode_config.output_list, head) {
794                 output->crtc = NULL;
795     
796                 /* Don't hook up outputs that are disconnected ??
797                  *
798                  * This is debateable. Do we want fixed /dev/fbX or
799                  * dynamic on hotplug (need mode code for that though) ?
800                  *
801                  * If we don't hook up outputs now, then we only create
802                  * /dev/fbX for the output that's enabled, that's good as
803                  * the users console will be on that output.
804                  *
805                  * If we do hook up outputs that are disconnected now, then
806                  * the user may end up having to muck about with the fbcon
807                  * map flags to assign his console to the enabled output. Ugh.
808                  */
809                 if (output->status != output_status_connected)
810                         continue;
811
812                 des_mode = NULL;
813                 list_for_each_entry(des_mode, &output->modes, head) {
814                         if (des_mode->type & DRM_MODE_TYPE_PREFERRED)
815                                 break;
816                 }
817
818                 /* No preferred mode, let's just select the first available */
819                 if (!des_mode || !(des_mode->type & DRM_MODE_TYPE_PREFERRED)) {
820                         list_for_each_entry(des_mode, &output->modes, head) {
821                                 if (des_mode)
822                                         break;
823                         }
824                 }
825
826                 c = -1;
827                 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
828                         assigned = 0;
829
830                         c++;
831                         if ((output->possible_crtcs & (1 << c)) == 0)
832                                 continue;
833         
834                         list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
835                                 if (output->id == output_equal->id)
836                                         continue;
837
838                                 /* Find out if crtc has been assigned before */
839                                 if (output_equal->crtc == crtc)
840                                         assigned = 1;
841                         }
842
843 #if 1 /* continue for now */
844                         if (assigned)
845                                 continue;
846 #endif
847
848                         o = -1;
849                         list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
850                                 o++;
851                                 if (output->id == output_equal->id)
852                                         continue;
853
854                                 list_for_each_entry(modes, &output->modes, head) {
855                                         list_for_each_entry(modes_equal, &output_equal->modes, head) {
856                                                 if (drm_mode_equal (modes, modes_equal)) {
857                                                         if ((output->possible_clones & output_equal->possible_clones) && (output_equal->crtc == crtc)) {
858                                                                 printk("Cloning %s (0x%lx) to %s (0x%lx)\n",output->name,output->possible_clones,output_equal->name,output_equal->possible_clones);
859                                                                 assigned = 0;
860                                                                 goto clone;
861                                                         }
862                                                 }
863                                         }
864                                 }
865                         }
866
867 clone:
868                         /* crtc has been assigned skip it */
869                         if (assigned)
870                                 continue;
871
872                         /* Found a CRTC to attach to, do it ! */
873                         output->crtc = crtc;
874                         output->crtc->desired_mode = des_mode;
875                         output->initial_x = 0;
876                         output->initial_y = 0;
877                         DRM_DEBUG("Desired mode for CRTC %d is 0x%x:%s\n",c,des_mode->mode_id, des_mode->name);
878                         break;
879                 }
880         }
881 }
882
883
884 /**
885  * drm_initial_config - setup a sane initial output configuration
886  * @dev: DRM device
887  * @can_grow: this configuration is growable
888  *
889  * LOCKING:
890  * Called at init time, must take mode config lock.
891  *
892  * Scan the CRTCs and outputs and try to put together an initial setup.
893  * At the moment, this is a cloned configuration across all heads with
894  * a new framebuffer object as the backing store.
895  *
896  * RETURNS:
897  * Zero if everything went ok, nonzero otherwise.
898  */
899 bool drm_initial_config(struct drm_device *dev, bool can_grow)
900 {
901         struct drm_output *output;
902         struct drm_crtc *crtc;
903         int ret = false;
904
905         mutex_lock(&dev->mode_config.mutex);
906
907         drm_crtc_probe_output_modes(dev, 2048, 2048);
908
909         drm_pick_crtcs(dev);
910
911         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
912
913                 /* can't setup the crtc if there's no assigned mode */
914                 if (!crtc->desired_mode)
915                         continue;
916
917                 /* Now setup the fbdev for attached crtcs */
918                 dev->driver->fb_probe(dev, crtc);
919         }
920
921         /* This is a little screwy, as we've already walked the outputs 
922          * above, but it's a little bit of magic too. There's the potential
923          * for things not to get setup above if an existing device gets
924          * re-assigned thus confusing the hardware. By walking the outputs
925          * this fixes up their crtc's.
926          */
927         list_for_each_entry(output, &dev->mode_config.output_list, head) {
928
929                 /* can't setup the output if there's no assigned mode */
930                 if (!output->crtc || !output->crtc->desired_mode)
931                         continue;
932
933                 /* and needs an attached fb */
934                 if (output->crtc->fb)
935                         drm_crtc_set_mode(output->crtc, output->crtc->desired_mode, 0, 0);
936         }
937
938         drm_disable_unused_functions(dev);
939
940         mutex_unlock(&dev->mode_config.mutex);
941         return ret;
942 }
943 EXPORT_SYMBOL(drm_initial_config);
944
945 /**
946  * drm_mode_config_cleanup - free up DRM mode_config info
947  * @dev: DRM device
948  *
949  * LOCKING:
950  * Caller must hold mode config lock.
951  *
952  * Free up all the outputs and CRTCs associated with this DRM device, then
953  * free up the framebuffers and associated buffer objects.
954  *
955  * FIXME: cleanup any dangling user buffer objects too
956  */
957 void drm_mode_config_cleanup(struct drm_device *dev)
958 {
959         struct drm_output *output, *ot;
960         struct drm_crtc *crtc, *ct;
961         struct drm_framebuffer *fb, *fbt;
962         struct drm_property *property, *pt;
963
964         list_for_each_entry_safe(output, ot, &dev->mode_config.output_list, head) {
965                 drm_output_destroy(output);
966         }
967
968         list_for_each_entry_safe(property, pt, &dev->mode_config.property_list, head) {
969                 drm_property_destroy(dev, property);
970         }
971
972         list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
973                 if (fb->bo->type != drm_bo_type_kernel)
974                         drm_framebuffer_destroy(fb);
975                 else
976                         dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
977         }
978
979         list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
980                 drm_crtc_destroy(crtc);
981         }
982
983 }
984 EXPORT_SYMBOL(drm_mode_config_cleanup);
985
986 /**
987  * drm_crtc_set_config - set a new config from userspace
988  * @crtc: CRTC to setup
989  * @crtc_info: user provided configuration
990  * @new_mode: new mode to set
991  * @output_set: set of outputs for the new config
992  * @fb: new framebuffer
993  *
994  * LOCKING:
995  * Caller must hold mode config lock.
996  *
997  * Setup a new configuration, provided by the user in @crtc_info, and enable
998  * it.
999  *
1000  * RETURNS:
1001  * Zero. (FIXME)
1002  */
1003 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)
1004 {
1005         struct drm_device *dev = crtc->dev;
1006         struct drm_crtc **save_crtcs, *new_crtc;
1007         bool save_enabled = crtc->enabled;
1008         bool changed = false;
1009         struct drm_output *output;
1010         int count = 0, ro;
1011
1012         save_crtcs = kzalloc(dev->mode_config.num_crtc * sizeof(struct drm_crtc *), GFP_KERNEL);
1013         if (!save_crtcs)
1014                 return -ENOMEM;
1015
1016         if (crtc->fb != fb)
1017                 changed = true;
1018
1019         if (crtc_info->x != crtc->x || crtc_info->y != crtc->y)
1020                 changed = true;
1021
1022         if (new_mode && !drm_mode_equal(new_mode, &crtc->mode))
1023                 changed = true;
1024
1025         list_for_each_entry(output, &dev->mode_config.output_list, head) {
1026                 save_crtcs[count++] = output->crtc;
1027
1028                 if (output->crtc == crtc)
1029                         new_crtc = NULL;
1030                 else
1031                         new_crtc = output->crtc;
1032
1033                 for (ro = 0; ro < crtc_info->count_outputs; ro++) {
1034                         if (output_set[ro] == output)
1035                                 new_crtc = crtc;
1036                 }
1037                 if (new_crtc != output->crtc) {
1038                         changed = true;
1039                         output->crtc = new_crtc;
1040                 }
1041         }
1042
1043         if (changed) {
1044                 crtc->fb = fb;
1045                 crtc->enabled = (new_mode != NULL);
1046                 if (new_mode != NULL) {
1047                         DRM_DEBUG("attempting to set mode from userspace\n");
1048                         drm_mode_debug_printmodeline(dev, new_mode);
1049                         if (!drm_crtc_set_mode(crtc, new_mode, crtc_info->x,
1050                                                crtc_info->y)) {
1051                                 crtc->enabled = save_enabled;
1052                                 count = 0;
1053                                 list_for_each_entry(output, &dev->mode_config.output_list, head)
1054                                         output->crtc = save_crtcs[count++];
1055                                 kfree(save_crtcs);
1056                                 return -EINVAL;
1057                         }
1058                         crtc->desired_x = crtc_info->x;
1059                         crtc->desired_y = crtc_info->y;
1060                         crtc->desired_mode = new_mode;
1061                 }
1062                 drm_disable_unused_functions(dev);
1063         }
1064         kfree(save_crtcs);
1065         return 0;
1066 }
1067
1068 /**
1069  * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1070  * @out: drm_mode_modeinfo struct to return to the user
1071  * @in: drm_display_mode to use
1072  *
1073  * LOCKING:
1074  * None.
1075  *
1076  * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1077  * the user.
1078  */
1079 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out, struct drm_display_mode *in)
1080 {
1081         out->clock = in->clock;
1082         out->hdisplay = in->hdisplay;
1083         out->hsync_start = in->hsync_start;
1084         out->hsync_end = in->hsync_end;
1085         out->htotal = in->htotal;
1086         out->hskew = in->hskew;
1087         out->vdisplay = in->vdisplay;
1088         out->vsync_start = in->vsync_start;
1089         out->vsync_end = in->vsync_end;
1090         out->vtotal = in->vtotal;
1091         out->vscan = in->vscan;
1092         out->vrefresh = in->vrefresh;
1093         out->flags = in->flags;
1094         out->type = in->type;
1095         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1096         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1097 }
1098
1099 /**
1100  * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1101  * @out: drm_display_mode to return to the user
1102  * @in: drm_mode_modeinfo to use
1103  *
1104  * LOCKING:
1105  * None.
1106  *
1107  * Convert a drmo_mode_modeinfo into a drm_display_mode structure to return to
1108  * the caller.
1109  */
1110 void drm_crtc_convert_umode(struct drm_display_mode *out, struct drm_mode_modeinfo *in)
1111 {
1112         out->clock = in->clock;
1113         out->hdisplay = in->hdisplay;
1114         out->hsync_start = in->hsync_start;
1115         out->hsync_end = in->hsync_end;
1116         out->htotal = in->htotal;
1117         out->hskew = in->hskew;
1118         out->vdisplay = in->vdisplay;
1119         out->vsync_start = in->vsync_start;
1120         out->vsync_end = in->vsync_end;
1121         out->vtotal = in->vtotal;
1122         out->vscan = in->vscan;
1123         out->vrefresh = in->vrefresh;
1124         out->flags = in->flags;
1125         out->type = in->type;
1126         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1127         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1128 }
1129         
1130 /**
1131  * drm_mode_getresources - get graphics configuration
1132  * @inode: inode from the ioctl
1133  * @filp: file * from the ioctl
1134  * @cmd: cmd from ioctl
1135  * @arg: arg from ioctl
1136  *
1137  * LOCKING:
1138  * Takes mode config lock.
1139  *
1140  * Construct a set of configuration description structures and return
1141  * them to the user, including CRTC, output and framebuffer configuration.
1142  *
1143  * Called by the user via ioctl.
1144  *
1145  * RETURNS:
1146  * Zero on success, errno on failure.
1147  */
1148 int drm_mode_getresources(struct drm_device *dev,
1149                           void *data, struct drm_file *file_priv)
1150 {
1151         struct drm_mode_card_res *card_res = data;
1152         struct list_head *lh;
1153         struct drm_framebuffer *fb;
1154         struct drm_output *output;
1155         struct drm_crtc *crtc;
1156         int ret = 0;
1157         int output_count = 0;
1158         int crtc_count = 0;
1159         int fb_count = 0;
1160         int copied = 0;
1161         uint32_t __user *fb_id;
1162         uint32_t __user *crtc_id;
1163         uint32_t __user *output_id;
1164
1165         mutex_lock(&dev->mode_config.mutex);
1166
1167         list_for_each(lh, &dev->mode_config.fb_list)
1168                 fb_count++;
1169
1170         list_for_each(lh, &dev->mode_config.crtc_list)
1171                 crtc_count++;
1172
1173         list_for_each(lh, &dev->mode_config.output_list)
1174                 output_count++;
1175
1176         card_res->max_height = dev->mode_config.max_height;
1177         card_res->min_height = dev->mode_config.min_height;
1178         card_res->max_width = dev->mode_config.max_width;
1179         card_res->min_width = dev->mode_config.min_width;
1180
1181         /* handle this in 4 parts */
1182         /* FBs */
1183         if (card_res->count_fbs >= fb_count) {
1184                 copied = 0;
1185                 fb_id = (uint32_t *)(unsigned long)card_res->fb_id_ptr;
1186                 list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
1187                         if (put_user(fb->id, fb_id + copied)) {
1188                                 ret = -EFAULT;
1189                                 goto out;
1190                         }
1191                         copied++;
1192                 }
1193         }
1194         card_res->count_fbs = fb_count;
1195
1196         /* CRTCs */
1197         if (card_res->count_crtcs >= crtc_count) {
1198                 copied = 0;
1199                 crtc_id = (uint32_t *)(unsigned long)card_res->crtc_id_ptr;
1200                 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head){
1201                         DRM_DEBUG("CRTC ID is %d\n", crtc->id);
1202                         if (put_user(crtc->id, crtc_id + copied)) {
1203                                 ret = -EFAULT;
1204                                 goto out;
1205                         }
1206                         copied++;
1207                 }
1208         }
1209         card_res->count_crtcs = crtc_count;
1210
1211
1212         /* Outputs */
1213         if (card_res->count_outputs >= output_count) {
1214                 copied = 0;
1215                 output_id = (uint32_t *)(unsigned long)card_res->output_id_ptr;
1216                 list_for_each_entry(output, &dev->mode_config.output_list,
1217                                     head) {
1218                         DRM_DEBUG("OUTPUT ID is %d\n", output->id);
1219                         if (put_user(output->id, output_id + copied)) {
1220                                 ret = -EFAULT;
1221                                 goto out;
1222                         }
1223                         copied++;
1224                 }
1225         }
1226         card_res->count_outputs = output_count;
1227         
1228         DRM_DEBUG("Counted %d %d\n", card_res->count_crtcs,
1229                   card_res->count_outputs);
1230
1231 out:    
1232         mutex_unlock(&dev->mode_config.mutex);
1233         return ret;
1234 }
1235
1236 /**
1237  * drm_mode_getcrtc - get CRTC configuration
1238  * @inode: inode from the ioctl
1239  * @filp: file * from the ioctl
1240  * @cmd: cmd from ioctl
1241  * @arg: arg from ioctl
1242  *
1243  * LOCKING:
1244  * Caller? (FIXME)
1245  *
1246  * Construct a CRTC configuration structure to return to the user.
1247  *
1248  * Called by the user via ioctl.
1249  *
1250  * RETURNS:
1251  * Zero on success, errno on failure.
1252  */
1253 int drm_mode_getcrtc(struct drm_device *dev,
1254                      void *data, struct drm_file *file_priv)
1255 {
1256         struct drm_mode_crtc *crtc_resp = data;
1257         struct drm_crtc *crtc;
1258         struct drm_output *output;
1259         int ocount;
1260         int ret = 0;
1261
1262         mutex_lock(&dev->mode_config.mutex);
1263         crtc = idr_find(&dev->mode_config.crtc_idr, crtc_resp->crtc_id);
1264         if (!crtc || (crtc->id != crtc_resp->crtc_id)) {
1265                 ret = -EINVAL;
1266                 goto out;
1267         }
1268
1269         crtc_resp->x = crtc->x;
1270         crtc_resp->y = crtc->y;
1271
1272         if (crtc->fb)
1273                 crtc_resp->fb_id = crtc->fb->id;
1274         else
1275                 crtc_resp->fb_id = 0;
1276
1277         crtc_resp->outputs = 0;
1278         if (crtc->enabled) {
1279
1280                 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1281                 crtc_resp->mode_valid = 1;
1282                 ocount = 0;
1283                 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1284                         if (output->crtc == crtc)
1285                                 crtc_resp->outputs |= 1 << (ocount++);
1286                 }
1287                 
1288         } else {
1289                 crtc_resp->mode_valid = 0;
1290         }
1291
1292 out:
1293         mutex_unlock(&dev->mode_config.mutex);
1294         return ret;
1295 }
1296
1297 /**
1298  * drm_mode_getoutput - get output configuration
1299  * @inode: inode from the ioctl
1300  * @filp: file * from the ioctl
1301  * @cmd: cmd from ioctl
1302  * @arg: arg from ioctl
1303  *
1304  * LOCKING:
1305  * Caller? (FIXME)
1306  *
1307  * Construct a output configuration structure to return to the user.
1308  *
1309  * Called by the user via ioctl.
1310  *
1311  * RETURNS:
1312  * Zero on success, errno on failure.
1313  */
1314 int drm_mode_getoutput(struct drm_device *dev,
1315                        void *data, struct drm_file *file_priv)
1316 {
1317         struct drm_mode_get_output *out_resp = data;
1318         struct drm_output *output;
1319         struct drm_display_mode *mode;
1320         int mode_count = 0;
1321         int props_count = 0;
1322         int ret = 0;
1323         int copied = 0;
1324         int i;
1325         struct drm_mode_modeinfo u_mode;
1326         struct drm_mode_modeinfo __user *mode_ptr;
1327         uint32_t __user *prop_ptr;
1328         uint64_t __user *prop_values;
1329
1330         memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1331
1332         DRM_DEBUG("output id %d:\n", out_resp->output);
1333
1334         mutex_lock(&dev->mode_config.mutex);
1335         output= idr_find(&dev->mode_config.crtc_idr, out_resp->output);
1336         if (!output || (output->id != out_resp->output)) {
1337                 ret = -EINVAL;
1338                 goto out;
1339         }
1340
1341         list_for_each_entry(mode, &output->modes, head)
1342                 mode_count++;
1343         
1344         for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1345                 if (output->property_ids[i] != 0) {
1346                         props_count++;
1347                 }
1348         }
1349
1350         if (out_resp->count_modes == 0) {
1351                 drm_crtc_probe_single_output_modes(output, dev->mode_config.max_width, dev->mode_config.max_height);
1352         }
1353
1354         strncpy(out_resp->name, output->name, DRM_OUTPUT_NAME_LEN);
1355         out_resp->name[DRM_OUTPUT_NAME_LEN-1] = 0;
1356
1357         out_resp->mm_width = output->mm_width;
1358         out_resp->mm_height = output->mm_height;
1359         out_resp->subpixel = output->subpixel_order;
1360         out_resp->connection = output->status;
1361         if (output->crtc)
1362                 out_resp->crtc = output->crtc->id;
1363         else
1364                 out_resp->crtc = 0;
1365
1366         out_resp->crtcs = output->possible_crtcs;
1367         out_resp->clones = output->possible_clones;
1368
1369         if ((out_resp->count_modes >= mode_count) && mode_count) {
1370                 copied = 0;
1371                 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1372                 list_for_each_entry(mode, &output->modes, head) {
1373                         drm_crtc_convert_to_umode(&u_mode, mode);
1374                         if (copy_to_user(mode_ptr + copied,
1375                                          &u_mode, sizeof(u_mode))) {
1376                                 ret = -EFAULT;
1377                                 goto out;
1378                         }
1379                         copied++;
1380                         
1381                 }
1382         }
1383         out_resp->count_modes = mode_count;
1384
1385         if ((out_resp->count_props >= props_count) && props_count) {
1386                 copied = 0;
1387                 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1388                 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1389                 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1390                         if (output->property_ids[i] != 0) {
1391                                 if (put_user(output->property_ids[i], prop_ptr + copied)) {
1392                                         ret = -EFAULT;
1393                                         goto out;
1394                                 }
1395
1396                                 if (put_user(output->property_values[i], prop_values + copied)) {
1397                                         ret = -EFAULT;
1398                                         goto out;
1399                                 }
1400                                 copied++;
1401                         }
1402                 }
1403         }
1404         out_resp->count_props = props_count;
1405
1406 out:
1407         mutex_unlock(&dev->mode_config.mutex);
1408         return ret;
1409 }
1410
1411 /**
1412  * drm_mode_setcrtc - set CRTC configuration
1413  * @inode: inode from the ioctl
1414  * @filp: file * from the ioctl
1415  * @cmd: cmd from ioctl
1416  * @arg: arg from ioctl
1417  *
1418  * LOCKING:
1419  * Caller? (FIXME)
1420  *
1421  * Build a new CRTC configuration based on user request.
1422  *
1423  * Called by the user via ioctl.
1424  *
1425  * RETURNS:
1426  * Zero on success, errno on failure.
1427  */
1428 int drm_mode_setcrtc(struct drm_device *dev,
1429                      void *data, struct drm_file *file_priv)
1430 {
1431         struct drm_mode_crtc *crtc_req = data;
1432         struct drm_crtc *crtc;
1433         struct drm_output **output_set = NULL, *output;
1434         struct drm_framebuffer *fb = NULL;
1435         struct drm_display_mode *mode = NULL;
1436         int ret = 0;
1437         int i;
1438         uint32_t __user *set_outputs_ptr;
1439
1440         mutex_lock(&dev->mode_config.mutex);
1441         crtc = idr_find(&dev->mode_config.crtc_idr, crtc_req->crtc_id);
1442         if (!crtc || (crtc->id != crtc_req->crtc_id)) {
1443                 DRM_DEBUG("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1444                 ret = -EINVAL;
1445                 goto out;
1446         }
1447
1448         if (crtc_req->mode_valid) {
1449                 /* if we have a mode we need a framebuffer */
1450                 if (crtc_req->fb_id) {
1451                         fb = idr_find(&dev->mode_config.crtc_idr, crtc_req->fb_id);
1452                         if (!fb || (fb->id != crtc_req->fb_id)) {
1453                                 DRM_DEBUG("Unknown FB ID%d\n", crtc_req->fb_id);
1454                                 ret = -EINVAL;
1455                                 goto out;
1456                         }
1457                 }
1458
1459                 mode = drm_mode_create(dev);
1460                 drm_crtc_convert_umode(mode, &crtc_req->mode);
1461         }
1462
1463         if (crtc_req->count_outputs == 0 && mode) {
1464                 DRM_DEBUG("Count outputs is 0 but mode set\n");
1465                 ret = -EINVAL;
1466                 goto out;
1467         }
1468
1469         if (crtc_req->count_outputs > 0 && !mode && !fb) {
1470                 DRM_DEBUG("Count outputs is %d but no mode or fb set\n", crtc_req->count_outputs);
1471                 ret = -EINVAL;
1472                 goto out;
1473         }
1474
1475         if (crtc_req->count_outputs > 0) {
1476                 u32 out_id;
1477                 output_set = kmalloc(crtc_req->count_outputs *
1478                                      sizeof(struct drm_output *), GFP_KERNEL);
1479                 if (!output_set) {
1480                         ret = -ENOMEM;
1481                         goto out;
1482                 }
1483
1484                 for (i = 0; i < crtc_req->count_outputs; i++) {
1485                         set_outputs_ptr = (uint32_t *)(unsigned long)crtc_req->set_outputs_ptr;
1486                         if (get_user(out_id, &set_outputs_ptr[i])) {
1487                                 ret = -EFAULT;
1488                                 goto out;
1489                         }
1490
1491                         output = idr_find(&dev->mode_config.crtc_idr, out_id);
1492                         if (!output || (out_id != output->id)) {
1493                                 DRM_DEBUG("Output id %d unknown\n", out_id);
1494                                 ret = -EINVAL;
1495                                 goto out;
1496                         }
1497
1498                         output_set[i] = output;
1499                 }
1500         }
1501
1502         ret = drm_crtc_set_config(crtc, crtc_req, mode, output_set, fb);
1503
1504 out:
1505         mutex_unlock(&dev->mode_config.mutex);
1506         return ret;
1507 }
1508
1509 /**
1510  * drm_mode_addfb - add an FB to the graphics configuration
1511  * @inode: inode from the ioctl
1512  * @filp: file * from the ioctl
1513  * @cmd: cmd from ioctl
1514  * @arg: arg from ioctl
1515  *
1516  * LOCKING:
1517  * Takes mode config lock.
1518  *
1519  * Add a new FB to the specified CRTC, given a user request.
1520  *
1521  * Called by the user via ioctl.
1522  *
1523  * RETURNS:
1524  * Zero on success, errno on failure.
1525  */
1526 int drm_mode_addfb(struct drm_device *dev,
1527                    void *data, struct drm_file *file_priv)
1528 {
1529         struct drm_mode_fb_cmd *r = data;
1530         struct drm_mode_config *config = &dev->mode_config;
1531         struct drm_framebuffer *fb;
1532         struct drm_buffer_object *bo;
1533         struct drm_crtc *crtc;
1534         int ret = 0;
1535
1536         if ((config->min_width > r->width) || (r->width > config->max_width)) {
1537                 DRM_ERROR("mode new framebuffer width not within limits\n");
1538                 return -EINVAL;
1539         }
1540         if ((config->min_height > r->height) || (r->height > config->max_height)) {
1541                 DRM_ERROR("mode new framebuffer height not within limits\n");
1542                 return -EINVAL;
1543         }
1544
1545         mutex_lock(&dev->mode_config.mutex);
1546         /* TODO check limits are okay */
1547         ret = drm_get_buffer_object(dev, &bo, r->handle);
1548         if (ret || !bo) {
1549                 ret = -EINVAL;
1550                 goto out;
1551         }
1552
1553         /* TODO check buffer is sufficently large */
1554         /* TODO setup destructor callback */
1555
1556         fb = drm_framebuffer_create(dev);
1557         if (!fb) {
1558                 ret = -EINVAL;
1559                 goto out;
1560         }
1561
1562         fb->width = r->width;
1563         fb->height = r->height;
1564         fb->pitch = r->pitch;
1565         fb->bits_per_pixel = r->bpp;
1566         fb->depth = r->depth;
1567         fb->offset = bo->offset;
1568         fb->bo = bo;
1569
1570         r->buffer_id = fb->id;
1571
1572         list_add(&fb->filp_head, &file_priv->fbs);
1573
1574         /* FIXME: bind the fb to the right crtc */
1575         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1576                 crtc->fb = fb;
1577                 dev->driver->fb_probe(dev, crtc);
1578         }
1579
1580 out:
1581         mutex_unlock(&dev->mode_config.mutex);
1582         return ret;
1583 }
1584
1585 /**
1586  * drm_mode_rmfb - remove an FB from the configuration
1587  * @inode: inode from the ioctl
1588  * @filp: file * from the ioctl
1589  * @cmd: cmd from ioctl
1590  * @arg: arg from ioctl
1591  *
1592  * LOCKING:
1593  * Takes mode config lock.
1594  *
1595  * Remove the FB specified by the user.
1596  *
1597  * Called by the user via ioctl.
1598  *
1599  * RETURNS:
1600  * Zero on success, errno on failure.
1601  */
1602 int drm_mode_rmfb(struct drm_device *dev,
1603                    void *data, struct drm_file *file_priv)
1604 {
1605         struct drm_framebuffer *fb = 0;
1606         uint32_t *id = data;
1607         int ret = 0;
1608
1609         mutex_lock(&dev->mode_config.mutex);
1610         fb = idr_find(&dev->mode_config.crtc_idr, *id);
1611         /* TODO check that we realy get a framebuffer back. */
1612         if (!fb || (*id != fb->id)) {
1613                 DRM_ERROR("mode invalid framebuffer id\n");
1614                 ret = -EINVAL;
1615                 goto out;
1616         }
1617
1618         /* TODO check if we own the buffer */
1619         /* TODO release all crtc connected to the framebuffer */
1620         /* bind the fb to the crtc for now */
1621         /* TODO unhock the destructor from the buffer object */
1622
1623         if (fb->bo->type != drm_bo_type_kernel)
1624                 drm_framebuffer_destroy(fb);
1625         else
1626                 dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1627
1628 out:
1629         mutex_unlock(&dev->mode_config.mutex);
1630         return ret;
1631 }
1632
1633 /**
1634  * drm_mode_getfb - get FB info
1635  * @inode: inode from the ioctl
1636  * @filp: file * from the ioctl
1637  * @cmd: cmd from ioctl
1638  * @arg: arg from ioctl
1639  *
1640  * LOCKING:
1641  * Caller? (FIXME)
1642  *
1643  * Lookup the FB given its ID and return info about it.
1644  *
1645  * Called by the user via ioctl.
1646  *
1647  * RETURNS:
1648  * Zero on success, errno on failure.
1649  */
1650 int drm_mode_getfb(struct drm_device *dev,
1651                    void *data, struct drm_file *file_priv)
1652 {
1653         struct drm_mode_fb_cmd *r = data;
1654         struct drm_framebuffer *fb;
1655         int ret = 0;
1656
1657         mutex_lock(&dev->mode_config.mutex);
1658         fb = idr_find(&dev->mode_config.crtc_idr, r->buffer_id);
1659         if (!fb || (r->buffer_id != fb->id)) {
1660                 DRM_ERROR("invalid framebuffer id\n");
1661                 ret = -EINVAL;
1662                 goto out;
1663         }
1664
1665         r->height = fb->height;
1666         r->width = fb->width;
1667         r->depth = fb->depth;
1668         r->bpp = fb->bits_per_pixel;
1669         r->handle = fb->bo->base.hash.key;
1670         r->pitch = fb->pitch;
1671
1672 out:
1673         mutex_unlock(&dev->mode_config.mutex);
1674         return ret;
1675 }
1676
1677 /**
1678  * drm_fb_release - remove and free the FBs on this file
1679  * @filp: file * from the ioctl
1680  *
1681  * LOCKING:
1682  * Takes mode config lock.
1683  *
1684  * Destroy all the FBs associated with @filp.
1685  *
1686  * Called by the user via ioctl.
1687  *
1688  * RETURNS:
1689  * Zero on success, errno on failure.
1690  */
1691 void drm_fb_release(struct file *filp)
1692 {
1693         struct drm_file *priv = filp->private_data;
1694         struct drm_device *dev = priv->head->dev;
1695         struct drm_framebuffer *fb, *tfb;
1696
1697         mutex_lock(&dev->mode_config.mutex);
1698         list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1699                 list_del(&fb->filp_head);
1700                 if (fb->bo->type != drm_bo_type_kernel)
1701                         drm_framebuffer_destroy(fb);
1702                 else
1703                         dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1704         }
1705         mutex_unlock(&dev->mode_config.mutex);
1706 }
1707
1708 /*
1709  *
1710  */
1711
1712 static int drm_mode_attachmode(struct drm_device *dev,
1713                                struct drm_output *output,
1714                                struct drm_display_mode *mode)
1715 {
1716         int ret = 0;
1717
1718         list_add_tail(&mode->head, &output->user_modes);
1719         return ret;
1720 }
1721
1722 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1723                              struct drm_display_mode *mode)
1724 {
1725         struct drm_output *output;
1726         int ret = 0;
1727         struct drm_display_mode *dup_mode;
1728         int need_dup = 0;
1729         list_for_each_entry(output, &dev->mode_config.output_list, head) {
1730                 if (output->crtc == crtc) {
1731                         if (need_dup)
1732                                 dup_mode = drm_mode_duplicate(dev, mode);
1733                         else
1734                                 dup_mode = mode;
1735                         ret = drm_mode_attachmode(dev, output, dup_mode); 
1736                         if (ret)
1737                                 return ret;
1738                         need_dup = 1;
1739                 }
1740         }
1741         return 0;
1742 }
1743 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1744
1745 static int drm_mode_detachmode(struct drm_device *dev,
1746                                struct drm_output *output,
1747                                struct drm_display_mode *mode)
1748 {
1749         int found = 0;
1750         int ret = 0;
1751         struct drm_display_mode *match_mode, *t;
1752
1753         list_for_each_entry_safe(match_mode, t, &output->user_modes, head) {
1754                 if (drm_mode_equal(match_mode, mode)) {
1755                         list_del(&match_mode->head);
1756                         drm_mode_destroy(dev, match_mode);
1757                         found = 1;
1758                         break;
1759                 }
1760         }
1761
1762         if (!found)
1763                 ret = -EINVAL;
1764
1765         return ret;
1766 }
1767
1768 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
1769 {
1770         struct drm_output *output;
1771
1772         list_for_each_entry(output, &dev->mode_config.output_list, head) {
1773                 drm_mode_detachmode(dev, output, mode);
1774         }
1775         return 0;
1776 }
1777 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
1778
1779 /**
1780  * drm_fb_attachmode - Attach a user mode to an output
1781  * @inode: inode from the ioctl
1782  * @filp: file * from the ioctl
1783  * @cmd: cmd from ioctl
1784  * @arg: arg from ioctl
1785  *
1786  * This attaches a user specified mode to an output.
1787  * Called by the user via ioctl.
1788  *
1789  * RETURNS:
1790  * Zero on success, errno on failure.
1791  */
1792 int drm_mode_attachmode_ioctl(struct drm_device *dev,
1793                               void *data, struct drm_file *file_priv)
1794 {
1795         struct drm_mode_mode_cmd *mode_cmd = data;
1796         struct drm_output *output;
1797         struct drm_display_mode *mode;
1798         struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1799         int ret = 0;
1800
1801         mutex_lock(&dev->mode_config.mutex);
1802
1803         output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
1804         if (!output || (output->id != mode_cmd->output_id)) {
1805                 ret = -EINVAL;
1806                 goto out;
1807         }
1808
1809         mode = drm_mode_create(dev);
1810         if (!mode) {
1811                 ret = -ENOMEM;
1812                 goto out;
1813         }
1814         
1815         drm_crtc_convert_umode(mode, umode);
1816
1817         ret = drm_mode_attachmode(dev, output, mode);
1818 out:
1819         mutex_unlock(&dev->mode_config.mutex);
1820         return ret;
1821 }
1822
1823
1824 /**
1825  * drm_fb_detachmode - Detach a user specified mode from an output
1826  * @inode: inode from the ioctl
1827  * @filp: file * from the ioctl
1828  * @cmd: cmd from ioctl
1829  * @arg: arg from ioctl
1830  *
1831  * Called by the user via ioctl.
1832  *
1833  * RETURNS:
1834  * Zero on success, errno on failure.
1835  */
1836 int drm_mode_detachmode_ioctl(struct drm_device *dev,
1837                               void *data, struct drm_file *file_priv)
1838 {
1839         struct drm_mode_mode_cmd *mode_cmd = data;
1840         struct drm_output *output;
1841         struct drm_display_mode mode;
1842         struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1843         int ret = 0;
1844
1845         mutex_lock(&dev->mode_config.mutex);
1846
1847         output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
1848         if (!output || (output->id != mode_cmd->output_id)) {
1849                 ret = -EINVAL;
1850                 goto out;
1851         }
1852         
1853         drm_crtc_convert_umode(&mode, umode);
1854         ret = drm_mode_detachmode(dev, output, &mode);
1855 out:           
1856         mutex_unlock(&dev->mode_config.mutex);
1857         return ret;
1858 }
1859
1860 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
1861                                          const char *name, int num_values)
1862 {
1863         struct drm_property *property = NULL;
1864
1865         property = kzalloc(sizeof(struct drm_output), GFP_KERNEL);
1866         if (!property)
1867                 return NULL;
1868
1869         if (num_values) {
1870                 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
1871                 if (!property->values)
1872                         goto fail;
1873         }
1874
1875         property->id = drm_idr_get(dev, property);
1876         property->flags = flags;
1877         property->num_values = num_values;
1878         INIT_LIST_HEAD(&property->enum_blob_list);
1879
1880         if (name)
1881                 strncpy(property->name, name, DRM_PROP_NAME_LEN);
1882
1883         list_add_tail(&property->head, &dev->mode_config.property_list);
1884         return property;
1885 fail:
1886         kfree(property);
1887         return NULL;
1888 }
1889 EXPORT_SYMBOL(drm_property_create);
1890
1891 int drm_property_add_enum(struct drm_property *property, int index,
1892                           uint64_t value, const char *name)
1893 {
1894         struct drm_property_enum *prop_enum;
1895
1896         if (!(property->flags & DRM_MODE_PROP_ENUM))
1897                 return -EINVAL;
1898
1899         if (!list_empty(&property->enum_blob_list)) {
1900                 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
1901                         if (prop_enum->value == value) {
1902                                 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN); 
1903                                 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1904                                 return 0;
1905                         }
1906                 }
1907         }
1908
1909         prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
1910         if (!prop_enum)
1911                 return -ENOMEM;
1912
1913         strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN); 
1914         prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1915         prop_enum->value = value;
1916
1917         property->values[index] = value;
1918         list_add_tail(&prop_enum->head, &property->enum_blob_list);
1919         return 0;
1920 }
1921 EXPORT_SYMBOL(drm_property_add_enum);
1922
1923 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
1924 {
1925         struct drm_property_enum *prop_enum, *pt;
1926
1927         list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
1928                 list_del(&prop_enum->head);
1929                 kfree(prop_enum);
1930         }
1931
1932         if (property->num_values)
1933                 kfree(property->values);
1934         drm_idr_put(dev, property->id);
1935         list_del(&property->head);
1936         kfree(property);        
1937 }
1938 EXPORT_SYMBOL(drm_property_destroy);
1939
1940 int drm_output_attach_property(struct drm_output *output,
1941                                struct drm_property *property, uint64_t init_val)
1942 {
1943         int i;
1944
1945         for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1946                 if (output->property_ids[i] == 0) {
1947                         output->property_ids[i] = property->id;
1948                         output->property_values[i] = init_val;
1949                         break;
1950                 }
1951         }
1952
1953         if (i == DRM_OUTPUT_MAX_PROPERTY)
1954                 return -EINVAL;
1955         return 0;
1956 }
1957 EXPORT_SYMBOL(drm_output_attach_property);
1958
1959 int drm_output_property_set_value(struct drm_output *output,
1960                                   struct drm_property *property, uint64_t value)
1961 {
1962         int i;
1963
1964         for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1965                 if (output->property_ids[i] == property->id) {
1966                         output->property_values[i] = value;
1967                         break;
1968                 }
1969         }
1970
1971         if (i == DRM_OUTPUT_MAX_PROPERTY)
1972                 return -EINVAL;
1973         return 0;
1974 }
1975 EXPORT_SYMBOL(drm_output_property_set_value);
1976
1977 int drm_mode_getproperty_ioctl(struct drm_device *dev,
1978                                void *data, struct drm_file *file_priv)
1979 {
1980         struct drm_mode_get_property *out_resp = data;
1981         struct drm_property *property;
1982         int enum_count = 0;
1983         int blob_count = 0;
1984         int value_count = 0;
1985         int ret = 0, i;
1986         int copied;
1987         struct drm_property_enum *prop_enum;
1988         struct drm_property_enum __user *enum_ptr;
1989         struct drm_property_blob *prop_blob;
1990         uint32_t *blob_id_ptr;
1991         uint64_t __user *values_ptr;
1992         uint32_t __user *blob_length_ptr;
1993
1994         mutex_lock(&dev->mode_config.mutex);
1995         property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
1996         if (!property || (property->id != out_resp->prop_id)) {
1997                 ret = -EINVAL;
1998                 goto done;
1999         }
2000
2001         if (property->flags & DRM_MODE_PROP_ENUM) {
2002                 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2003                         enum_count++;
2004         } else if (property->flags & DRM_MODE_PROP_BLOB) {
2005                 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2006                         blob_count++;
2007         }
2008
2009         value_count = property->num_values;
2010
2011         strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2012         out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2013         out_resp->flags = property->flags;
2014
2015         if ((out_resp->count_values >= value_count) && value_count) {
2016                 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2017                 for (i = 0; i < value_count; i++) {
2018                         if (put_user(property->values[i], values_ptr + i)) {
2019                                 ret = -EFAULT;
2020                                 goto done;
2021                         }
2022                 }
2023         }
2024         out_resp->count_values = value_count;
2025
2026         if (property->flags & DRM_MODE_PROP_ENUM) {
2027                 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2028                         copied = 0;
2029                         enum_ptr = (struct drm_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2030                         list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2031                                 if (put_user(prop_enum->value, &enum_ptr[copied].value)) {
2032                                         ret = -EFAULT;
2033                                         goto done;
2034                                 }
2035                                 
2036                                 if (copy_to_user(&enum_ptr[copied].name,
2037                                                  prop_enum->name, DRM_PROP_NAME_LEN)) {
2038                                 ret = -EFAULT;
2039                                 goto done;
2040                                 }
2041                                 copied++;
2042                         }
2043                 }
2044                 out_resp->count_enum_blobs = enum_count;
2045         }
2046
2047         if (property->flags & DRM_MODE_PROP_BLOB) {
2048                 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2049                         copied = 0;
2050                         blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2051                         blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2052                         
2053                         list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2054                                 if (put_user(prop_blob->id, blob_id_ptr + copied)) {
2055                                         ret = -EFAULT;
2056                                         goto done;
2057                                 }
2058                                 
2059                                 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2060                                         ret = -EFAULT;
2061                                         goto done;
2062                                 }
2063                                 
2064                                 copied++;
2065                         }
2066                 }
2067                 out_resp->count_enum_blobs = enum_count;
2068         }
2069 done:
2070         mutex_unlock(&dev->mode_config.mutex);
2071         return ret;
2072 }
2073
2074 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2075                                                           void *data)
2076 {
2077         struct drm_property_blob *blob;
2078
2079         if (!length || !data)
2080                 return NULL;
2081
2082         blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2083         if (!blob)
2084                 return NULL;
2085
2086         blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2087         blob->length = length;
2088
2089         memcpy(blob->data, data, length);
2090
2091         blob->id = drm_idr_get(dev, blob);
2092         
2093         list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2094         return blob;
2095 }
2096
2097 static void drm_property_destroy_blob(struct drm_device *dev,
2098                                struct drm_property_blob *blob)
2099 {
2100         drm_idr_put(dev, blob->id);
2101         list_del(&blob->head);
2102         kfree(blob);
2103 }
2104
2105 int drm_mode_getblob_ioctl(struct drm_device *dev,
2106                            void *data, struct drm_file *file_priv)
2107 {
2108         struct drm_mode_get_blob *out_resp = data;
2109         struct drm_property_blob *blob;
2110         int ret = 0;
2111         void *blob_ptr;
2112
2113         mutex_lock(&dev->mode_config.mutex);
2114         
2115         blob = idr_find(&dev->mode_config.crtc_idr, out_resp->blob_id);
2116         if (!blob || (blob->id != out_resp->blob_id)) {
2117                 ret = -EINVAL;
2118                 goto done;
2119         }
2120
2121         if (out_resp->length == blob->length) {
2122                 blob_ptr = (void *)(unsigned long)out_resp->data;
2123                 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2124                         ret = -EFAULT;
2125                         goto done;
2126                 }
2127         }
2128         out_resp->length = blob->length;
2129
2130 done:
2131         mutex_unlock(&dev->mode_config.mutex);
2132         return ret;
2133 }
2134
2135 int drm_mode_output_update_edid_property(struct drm_output *output, unsigned char *edid)
2136 {
2137         struct drm_device *dev = output->dev;
2138         int ret = 0;
2139         if (output->edid_blob_ptr)
2140                 drm_property_destroy_blob(dev, output->edid_blob_ptr);
2141
2142         output->edid_blob_ptr = drm_property_create_blob(output->dev, 128, edid);
2143         
2144         ret = drm_output_property_set_value(output, dev->mode_config.edid_property, output->edid_blob_ptr->id);
2145         return ret;
2146 }
2147 EXPORT_SYMBOL(drm_mode_output_update_edid_property);