Fix some buffer teardown problems.
[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_output_modes(struct drm_device *dev, int maxX, int maxY)
284 {
285         struct drm_output *output;
286         struct drm_display_mode *mode, *t;
287         int ret;
288         //if (maxX == 0 || maxY == 0) 
289         // TODO
290
291         list_for_each_entry(output, &dev->mode_config.output_list, head) {
292
293                 /* set all modes to the unverified state */
294                 list_for_each_entry_safe(mode, t, &output->modes, head)
295                         mode->status = MODE_UNVERIFIED;
296                 
297                 output->status = (*output->funcs->detect)(output);
298
299                 if (output->status == output_status_disconnected) {
300                         DRM_DEBUG("%s is disconnected\n", output->name);
301                         /* TODO set EDID to NULL */
302                         continue;
303                 }
304
305                 ret = (*output->funcs->get_modes)(output);
306
307                 if (ret) {
308                         drm_mode_output_list_update(output);
309                 }
310
311                 if (maxX && maxY)
312                         drm_mode_validate_size(dev, &output->modes, maxX,
313                                                maxY, 0);
314                 list_for_each_entry_safe(mode, t, &output->modes, head) {
315                         if (mode->status == MODE_OK)
316                                 mode->status = (*output->funcs->mode_valid)(output,mode);
317                 }
318                 
319
320                 drm_mode_prune_invalid(dev, &output->modes, TRUE);
321
322                 if (list_empty(&output->modes)) {
323                         struct drm_display_mode *stdmode;
324
325                         DRM_DEBUG("No valid modes on %s\n", output->name);
326
327                         /* Should we do this here ???
328                          * When no valid EDID modes are available we end up
329                          * here and bailed in the past, now we add a standard
330                          * 640x480@60Hz mode and carry on.
331                          */
332                         stdmode = drm_mode_duplicate(dev, &std_mode[0]);
333                         drm_mode_probed_add(output, stdmode);
334                         drm_mode_list_concat(&output->probed_modes,
335                                              &output->modes);
336
337                         DRM_DEBUG("Adding standard 640x480 @ 60Hz to %s\n",
338                                                                 output->name);
339                 }
340
341                 drm_mode_sort(&output->modes);
342
343                 DRM_DEBUG("Probed modes for %s\n", output->name);
344                 list_for_each_entry_safe(mode, t, &output->modes, head) {
345                         mode->vrefresh = drm_mode_vrefresh(mode);
346
347                         drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
348                         drm_mode_debug_printmodeline(dev, mode);
349                 }
350         }
351 }
352
353 /**
354  * drm_crtc_set_mode - set a mode
355  * @crtc: CRTC to program
356  * @mode: mode to use
357  * @x: width of mode
358  * @y: height of mode
359  *
360  * LOCKING:
361  * Caller must hold mode config lock.
362  *
363  * Try to set @mode on @crtc.  Give @crtc and its associated outputs a chance
364  * to fixup or reject the mode prior to trying to set it.
365  *
366  * RETURNS:
367  * True if the mode was set successfully, or false otherwise.
368  */
369 bool drm_crtc_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
370                        int x, int y)
371 {
372         struct drm_device *dev = crtc->dev;
373         struct drm_display_mode *adjusted_mode, saved_mode;
374         int saved_x, saved_y;
375         bool didLock = false;
376         bool ret = false;
377         struct drm_output *output;
378
379         adjusted_mode = drm_mode_duplicate(dev, mode);
380
381         crtc->enabled = drm_crtc_in_use(crtc);
382
383         if (!crtc->enabled) {
384                 return true;
385         }
386
387         didLock = crtc->funcs->lock(crtc);
388
389         saved_mode = crtc->mode;
390         saved_x = crtc->x;
391         saved_y = crtc->y;
392         
393         /* Update crtc values up front so the driver can rely on them for mode
394          * setting.
395          */
396         crtc->mode = *mode;
397         crtc->x = x;
398         crtc->y = y;
399
400         /* XXX short-circuit changes to base location only */
401         
402         /* Pass our mode to the outputs and the CRTC to give them a chance to
403          * adjust it according to limitations or output properties, and also
404          * a chance to reject the mode entirely.
405          */
406         list_for_each_entry(output, &dev->mode_config.output_list, head) {
407                 
408                 if (output->crtc != crtc)
409                         continue;
410                 
411                 if (!output->funcs->mode_fixup(output, mode, adjusted_mode)) {
412                         goto done;
413                 }
414         }
415         
416         if (!crtc->funcs->mode_fixup(crtc, mode, adjusted_mode)) {
417                 goto done;
418         }
419
420         /* Prepare the outputs and CRTCs before setting the mode. */
421         list_for_each_entry(output, &dev->mode_config.output_list, head) {
422
423                 if (output->crtc != crtc)
424                         continue;
425                 
426                 /* Disable the output as the first thing we do. */
427                 output->funcs->prepare(output);
428         }
429         
430         crtc->funcs->prepare(crtc);
431         
432         /* Set up the DPLL and any output state that needs to adjust or depend
433          * on the DPLL.
434          */
435         crtc->funcs->mode_set(crtc, mode, adjusted_mode, x, y);
436
437         list_for_each_entry(output, &dev->mode_config.output_list, head) {
438
439                 if (output->crtc != crtc)
440                         continue;
441                 
442                 DRM_INFO("%s: set mode %s\n", output->name, mode->name);
443
444                 output->funcs->mode_set(output, mode, adjusted_mode);
445         }
446         
447         /* Now, enable the clocks, plane, pipe, and outputs that we set up. */
448         crtc->funcs->commit(crtc);
449
450         list_for_each_entry(output, &dev->mode_config.output_list, head) {
451
452                 if (output->crtc != crtc)
453                         continue;
454                 
455                 output->funcs->commit(output);
456
457 #if 0 // TODO def RANDR_12_INTERFACE
458                 if (output->randr_output)
459                         RRPostPendingProperties (output->randr_output);
460 #endif
461         }
462         
463         /* XXX free adjustedmode */
464         drm_mode_destroy(dev, adjusted_mode);
465         ret = TRUE;
466         /* TODO */
467 //      if (scrn->pScreen)
468 //              drm_crtc_set_screen_sub_pixel_order(dev);
469
470 done:
471         if (!ret) {
472                 crtc->x = saved_x;
473                 crtc->y = saved_y;
474                 crtc->mode = saved_mode;
475         }
476         
477         if (didLock)
478                 crtc->funcs->unlock (crtc);
479         
480         return ret;
481 }
482 EXPORT_SYMBOL(drm_crtc_set_mode);
483
484 /**
485  * drm_disable_unused_functions - disable unused objects
486  * @dev: DRM device
487  *
488  * LOCKING:
489  * Caller must hold mode config lock.
490  *
491  * If an output or CRTC isn't part of @dev's mode_config, it can be disabled
492  * by calling its dpms function, which should power it off.
493  */
494 void drm_disable_unused_functions(struct drm_device *dev)
495 {
496         struct drm_output *output;
497         struct drm_crtc *crtc;
498
499         list_for_each_entry(output, &dev->mode_config.output_list, head) {
500                 if (!output->crtc)
501                         (*output->funcs->dpms)(output, DPMSModeOff);
502         }
503
504         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
505                 if (!crtc->enabled)
506                         crtc->funcs->dpms(crtc, DPMSModeOff);
507         }
508 }
509         
510 /**
511  * drm_mode_probed_add - add a mode to the specified output's probed mode list
512  * @output: output the new mode
513  * @mode: mode data
514  *
515  * LOCKING:
516  * Caller must hold mode config lock.
517  * 
518  * Add @mode to @output's mode list for later use.
519  */
520 void drm_mode_probed_add(struct drm_output *output,
521                          struct drm_display_mode *mode)
522 {
523         list_add(&mode->head, &output->probed_modes);
524 }
525 EXPORT_SYMBOL(drm_mode_probed_add);
526
527 /**
528  * drm_mode_remove - remove and free a mode
529  * @output: output list to modify
530  * @mode: mode to remove
531  *
532  * LOCKING:
533  * Caller must hold mode config lock.
534  * 
535  * Remove @mode from @output's mode list, then free it.
536  */
537 void drm_mode_remove(struct drm_output *output, struct drm_display_mode *mode)
538 {
539         list_del(&mode->head);
540         kfree(mode);
541 }
542 EXPORT_SYMBOL(drm_mode_remove);
543
544 /**
545  * drm_output_create - create a new output
546  * @dev: DRM device
547  * @funcs: callbacks for this output
548  * @name: user visible name of the output
549  *
550  * LOCKING:
551  * Caller must hold @dev's mode_config lock.
552  *
553  * Creates a new drm_output structure and adds it to @dev's mode_config
554  * structure.
555  *
556  * RETURNS:
557  * Pointer to the new output or NULL on error.
558  */
559 struct drm_output *drm_output_create(struct drm_device *dev,
560                                      const struct drm_output_funcs *funcs,
561                                      const char *name)
562 {
563         struct drm_output *output = NULL;
564
565         output = kzalloc(sizeof(struct drm_output), GFP_KERNEL);
566         if (!output)
567                 return NULL;
568                 
569         output->dev = dev;
570         output->funcs = funcs;
571         output->id = drm_idr_get(dev, output);
572         if (name)
573                 strncpy(output->name, name, DRM_OUTPUT_LEN);
574         output->name[DRM_OUTPUT_LEN - 1] = 0;
575         output->subpixel_order = SubPixelUnknown;
576         INIT_LIST_HEAD(&output->probed_modes);
577         INIT_LIST_HEAD(&output->modes);
578         /* randr_output? */
579         /* output_set_monitor(output)? */
580         /* check for output_ignored(output)? */
581
582         mutex_lock(&dev->mode_config.mutex);
583         list_add_tail(&output->head, &dev->mode_config.output_list);
584         dev->mode_config.num_output++;
585
586         mutex_unlock(&dev->mode_config.mutex);
587
588         return output;
589
590 }
591 EXPORT_SYMBOL(drm_output_create);
592
593 /**
594  * drm_output_destroy - remove an output
595  * @output: output to remove
596  *
597  * LOCKING:
598  * Caller must hold @dev's mode_config lock.
599  *
600  * Call @output's cleanup function, then remove the output from the DRM
601  * mode_config after freeing @output's modes.
602  */
603 void drm_output_destroy(struct drm_output *output)
604 {
605         struct drm_device *dev = output->dev;
606         struct drm_display_mode *mode, *t;
607
608         if (*output->funcs->cleanup)
609                 (*output->funcs->cleanup)(output);
610
611         list_for_each_entry_safe(mode, t, &output->probed_modes, head)
612                 drm_mode_remove(output, mode);
613
614         list_for_each_entry_safe(mode, t, &output->modes, head)
615                 drm_mode_remove(output, mode);
616
617         mutex_lock(&dev->mode_config.mutex);
618         drm_idr_put(dev, output->id);
619         list_del(&output->head);
620         mutex_unlock(&dev->mode_config.mutex);
621         kfree(output);
622 }
623 EXPORT_SYMBOL(drm_output_destroy);
624
625 /**
626  * drm_output_rename - rename an output
627  * @output: output to rename
628  * @name: new user visible name
629  *
630  * LOCKING:
631  * None.
632  *
633  * Simply stuff a new name into @output's name field, based on @name.
634  *
635  * RETURNS:
636  * True if the name was changed, false otherwise.
637  */
638 bool drm_output_rename(struct drm_output *output, const char *name)
639 {
640         if (!name)
641                 return false;
642
643         strncpy(output->name, name, DRM_OUTPUT_LEN);
644         output->name[DRM_OUTPUT_LEN - 1] = 0;
645
646         DRM_DEBUG("Changed name to %s\n", output->name);
647 //      drm_output_set_monitor(output);
648 //      if (drm_output_ignored(output))
649 //              return FALSE;
650
651         return TRUE;
652 }
653 EXPORT_SYMBOL(drm_output_rename);
654
655 /**
656  * drm_mode_create - create a new display mode
657  * @dev: DRM device
658  *
659  * LOCKING:
660  * None.
661  *
662  * Create a new drm_display_mode, give it an ID, and return it.
663  *
664  * RETURNS:
665  * Pointer to new mode on success, NULL on error.
666  */
667 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
668 {
669         struct drm_display_mode *nmode;
670
671         nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
672         if (!nmode)
673                 return NULL;
674
675         nmode->mode_id = drm_idr_get(dev, nmode);
676         return nmode;
677 }
678 EXPORT_SYMBOL(drm_mode_create);
679
680 /**
681  * drm_mode_destroy - remove a mode
682  * @dev: DRM device
683  * @mode: mode to remove
684  *
685  * LOCKING:
686  * Caller must hold mode config lock.
687  *
688  * Free @mode's unique identifier, then free it.
689  */
690 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
691 {
692         drm_idr_put(dev, mode->mode_id);
693
694         kfree(mode);
695 }
696 EXPORT_SYMBOL(drm_mode_destroy);
697
698 /**
699  * drm_mode_config_init - initialize DRM mode_configuration structure
700  * @dev: DRM device
701  *
702  * LOCKING:
703  * None, should happen single threaded at init time.
704  *
705  * Initialize @dev's mode_config structure, used for tracking the graphics
706  * configuration of @dev.
707  */
708 void drm_mode_config_init(struct drm_device *dev)
709 {
710         mutex_init(&dev->mode_config.mutex);
711         INIT_LIST_HEAD(&dev->mode_config.fb_list);
712         INIT_LIST_HEAD(&dev->mode_config.crtc_list);
713         INIT_LIST_HEAD(&dev->mode_config.output_list);
714         INIT_LIST_HEAD(&dev->mode_config.usermode_list);
715         idr_init(&dev->mode_config.crtc_idr);
716 }
717 EXPORT_SYMBOL(drm_mode_config_init);
718
719 /**
720  * drm_get_buffer_object - find the buffer object for a given handle
721  * @dev: DRM device
722  * @bo: pointer to caller's buffer_object pointer
723  * @handle: handle to lookup
724  *
725  * LOCKING:
726  * Must take @dev's struct_mutex to protect buffer object lookup.
727  *
728  * Given @handle, lookup the buffer object in @dev and put it in the caller's
729  * @bo pointer.
730  *
731  * RETURNS:
732  * Zero on success, -EINVAL if the handle couldn't be found.
733  */
734 static int drm_get_buffer_object(struct drm_device *dev, struct drm_buffer_object **bo, unsigned long handle)
735 {
736         struct drm_user_object *uo;
737         struct drm_hash_item *hash;
738         int ret;
739
740         *bo = NULL;
741
742         mutex_lock(&dev->struct_mutex);
743         ret = drm_ht_find_item(&dev->object_hash, handle, &hash);
744         if (ret) {
745                 DRM_ERROR("Couldn't find handle.\n");
746                 ret = -EINVAL;
747                 goto out_err;
748         }
749
750         uo = drm_hash_entry(hash, struct drm_user_object, hash);
751         if (uo->type != drm_buffer_type) {
752                 ret = -EINVAL;
753                 goto out_err;
754         }
755         
756         *bo = drm_user_object_entry(uo, struct drm_buffer_object, base);
757         ret = 0;
758 out_err:
759         mutex_unlock(&dev->struct_mutex);
760         return ret;
761 }
762
763 /**
764  * drm_pick_crtcs - pick crtcs for output devices
765  * @dev: DRM device
766  *
767  * LOCKING:
768  * Caller must hold mode config lock.
769  */
770 static void drm_pick_crtcs (struct drm_device *dev)
771 {
772         int c, o, assigned;
773         struct drm_output *output, *output_equal;
774         struct drm_crtc   *crtc;
775         struct drm_display_mode *des_mode = NULL, *modes, *modes_equal;
776
777         list_for_each_entry(output, &dev->mode_config.output_list, head) {
778                 output->crtc = NULL;
779     
780                 /* Don't hook up outputs that are disconnected ??
781                  *
782                  * This is debateable. Do we want fixed /dev/fbX or
783                  * dynamic on hotplug (need mode code for that though) ?
784                  *
785                  * If we don't hook up outputs now, then we only create
786                  * /dev/fbX for the output that's enabled, that's good as
787                  * the users console will be on that output.
788                  *
789                  * If we do hook up outputs that are disconnected now, then
790                  * the user may end up having to muck about with the fbcon
791                  * map flags to assign his console to the enabled output. Ugh.
792                  */
793                 if (output->status != output_status_connected)
794                         continue;
795
796                 des_mode = NULL;
797                 list_for_each_entry(des_mode, &output->modes, head) {
798                         if (des_mode->type & DRM_MODE_TYPE_PREFERRED)
799                                 break;
800                 }
801
802                 /* No preferred mode, let's just select the first available */
803                 if (!des_mode || !(des_mode->type & DRM_MODE_TYPE_PREFERRED)) {
804                         list_for_each_entry(des_mode, &output->modes, head) {
805                                 if (des_mode)
806                                         break;
807                         }
808                 }
809
810                 c = -1;
811                 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
812                         assigned = 0;
813
814                         c++;
815                         if ((output->possible_crtcs & (1 << c)) == 0)
816                                 continue;
817         
818                         list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
819                                 if (output->id == output_equal->id)
820                                         continue;
821
822                                 /* Find out if crtc has been assigned before */
823                                 if (output_equal->crtc == crtc)
824                                         assigned = 1;
825                         }
826
827 #if 1 /* continue for now */
828                         if (assigned)
829                                 continue;
830 #endif
831
832                         o = -1;
833                         list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
834                                 o++;
835                                 if (output->id == output_equal->id)
836                                         continue;
837
838                                 list_for_each_entry(modes, &output->modes, head) {
839                                         list_for_each_entry(modes_equal, &output_equal->modes, head) {
840                                                 if (drm_mode_equal (modes, modes_equal)) {
841                                                         if ((output->possible_clones & output_equal->possible_clones) && (output_equal->crtc == crtc)) {
842                                                                 printk("Cloning %s (0x%lx) to %s (0x%lx)\n",output->name,output->possible_clones,output_equal->name,output_equal->possible_clones);
843                                                                 assigned = 0;
844                                                                 goto clone;
845                                                         }
846                                                 }
847                                         }
848                                 }
849                         }
850
851 clone:
852                         /* crtc has been assigned skip it */
853                         if (assigned)
854                                 continue;
855
856                         /* Found a CRTC to attach to, do it ! */
857                         output->crtc = crtc;
858                         output->crtc->desired_mode = des_mode;
859                         output->initial_x = 0;
860                         output->initial_y = 0;
861                         DRM_DEBUG("Desired mode for CRTC %d is %s\n",c,des_mode->name);
862                         break;
863                 }
864         }
865 }
866
867
868 /**
869  * drm_initial_config - setup a sane initial output configuration
870  * @dev: DRM device
871  * @can_grow: this configuration is growable
872  *
873  * LOCKING:
874  * Called at init time, must take mode config lock.
875  *
876  * Scan the CRTCs and outputs and try to put together an initial setup.
877  * At the moment, this is a cloned configuration across all heads with
878  * a new framebuffer object as the backing store.
879  *
880  * RETURNS:
881  * Zero if everything went ok, nonzero otherwise.
882  */
883 bool drm_initial_config(struct drm_device *dev, bool can_grow)
884 {
885         struct drm_output *output;
886         struct drm_crtc *crtc;
887         int ret = false;
888
889         mutex_lock(&dev->mode_config.mutex);
890
891         drm_crtc_probe_output_modes(dev, 2048, 2048);
892
893         drm_pick_crtcs(dev);
894
895         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
896
897                 /* can't setup the crtc if there's no assigned mode */
898                 if (!crtc->desired_mode)
899                         continue;
900
901                 /* Now setup the fbdev for attached crtcs */
902                 dev->driver->fb_probe(dev, crtc);
903         }
904
905         /* This is a little screwy, as we've already walked the outputs 
906          * above, but it's a little bit of magic too. There's the potential
907          * for things not to get setup above if an existing device gets
908          * re-assigned thus confusing the hardware. By walking the outputs
909          * this fixes up their crtc's.
910          */
911         list_for_each_entry(output, &dev->mode_config.output_list, head) {
912
913                 /* can't setup the output if there's no assigned mode */
914                 if (!output->crtc || !output->crtc->desired_mode)
915                         continue;
916
917                 drm_crtc_set_mode(output->crtc, output->crtc->desired_mode, 0, 0);
918         }
919
920         drm_disable_unused_functions(dev);
921
922         mutex_unlock(&dev->mode_config.mutex);
923         return ret;
924 }
925 EXPORT_SYMBOL(drm_initial_config);
926
927 /**
928  * drm_mode_config_cleanup - free up DRM mode_config info
929  * @dev: DRM device
930  *
931  * LOCKING:
932  * Caller must hold mode config lock.
933  *
934  * Free up all the outputs and CRTCs associated with this DRM device, then
935  * free up the framebuffers and associated buffer objects.
936  *
937  * FIXME: cleanup any dangling user buffer objects too
938  */
939 void drm_mode_config_cleanup(struct drm_device *dev)
940 {
941         struct drm_output *output, *ot;
942         struct drm_crtc *crtc, *ct;
943         struct drm_framebuffer *fb, *fbt;
944         struct drm_display_mode *mode, *mt;
945         list_for_each_entry_safe(output, ot, &dev->mode_config.output_list, head) {
946                 drm_output_destroy(output);
947         }
948
949         list_for_each_entry_safe(mode, mt, &dev->mode_config.usermode_list, head) {
950                 drm_mode_destroy(dev, mode);
951         }
952                 
953         list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
954                 if (fb->bo->type != drm_bo_type_kernel)
955                         drm_framebuffer_destroy(fb);
956                 else
957                         dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
958         }
959
960         list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
961                 drm_crtc_destroy(crtc);
962         }
963
964 }
965 EXPORT_SYMBOL(drm_mode_config_cleanup);
966
967 /**
968  * drm_crtc_set_config - set a new config from userspace
969  * @crtc: CRTC to setup
970  * @crtc_info: user provided configuration
971  * @new_mode: new mode to set
972  * @output_set: set of outputs for the new config
973  * @fb: new framebuffer
974  *
975  * LOCKING:
976  * Caller must hold mode config lock.
977  *
978  * Setup a new configuration, provided by the user in @crtc_info, and enable
979  * it.
980  *
981  * RETURNS:
982  * Zero. (FIXME)
983  */
984 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)
985 {
986         struct drm_device *dev = crtc->dev;
987         struct drm_crtc **save_crtcs, *new_crtc;
988         bool save_enabled = crtc->enabled;
989         bool changed;
990         struct drm_output *output;
991         int count = 0, ro;
992
993         save_crtcs = kzalloc(dev->mode_config.num_crtc * sizeof(struct drm_crtc *), GFP_KERNEL);
994         if (!save_crtcs)
995                 return -ENOMEM;
996
997         if (crtc->fb != fb)
998                 changed = true;
999
1000         if (crtc_info->x != crtc->x || crtc_info->y != crtc->y)
1001                 changed = true;
1002
1003         if (new_mode && (crtc->mode.mode_id != new_mode->mode_id))
1004                 changed = true;
1005
1006         list_for_each_entry(output, &dev->mode_config.output_list, head) {
1007                 save_crtcs[count++] = output->crtc;
1008
1009                 if (output->crtc == crtc)
1010                         new_crtc = NULL;
1011                 else
1012                         new_crtc = output->crtc;
1013
1014                 for (ro = 0; ro < crtc_info->count_outputs; ro++) {
1015                         if (output_set[ro] == output)
1016                                 new_crtc = crtc;
1017                 }
1018                 if (new_crtc != output->crtc) {
1019                         changed = true;
1020                         output->crtc = new_crtc;
1021                 }
1022         }
1023
1024         if (changed) {
1025                 crtc->fb = fb;
1026                 crtc->enabled = (new_mode != NULL);
1027                 if (new_mode != NULL) {
1028                         DRM_DEBUG("attempting to set mode from userspace\n");
1029                         drm_mode_debug_printmodeline(dev, new_mode);
1030                         if (!drm_crtc_set_mode(crtc, new_mode, crtc_info->x,
1031                                                crtc_info->y)) {
1032                                 crtc->enabled = save_enabled;
1033                                 count = 0;
1034                                 list_for_each_entry(output, &dev->mode_config.output_list, head)
1035                                         output->crtc = save_crtcs[count++];
1036                                 kfree(save_crtcs);
1037                                 return -EINVAL;
1038                         }
1039                         crtc->desired_x = crtc_info->x;
1040                         crtc->desired_y = crtc_info->y;
1041                         crtc->desired_mode = new_mode;
1042                 }
1043                 drm_disable_unused_functions(dev);
1044         }
1045         kfree(save_crtcs);
1046         return 0;
1047 }
1048
1049 /**
1050  * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1051  * @out: drm_mode_modeinfo struct to return to the user
1052  * @in: drm_display_mode to use
1053  *
1054  * LOCKING:
1055  * None.
1056  *
1057  * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1058  * the user.
1059  */
1060 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out, struct drm_display_mode *in)
1061 {
1062
1063         out->id = in->mode_id;
1064         out->clock = in->clock;
1065         out->hdisplay = in->hdisplay;
1066         out->hsync_start = in->hsync_start;
1067         out->hsync_end = in->hsync_end;
1068         out->htotal = in->htotal;
1069         out->hskew = in->hskew;
1070         out->vdisplay = in->vdisplay;
1071         out->vsync_start = in->vsync_start;
1072         out->vsync_end = in->vsync_end;
1073         out->vtotal = in->vtotal;
1074         out->vscan = in->vscan;
1075         out->vrefresh = in->vrefresh;
1076         out->flags = in->flags;
1077         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1078         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1079 }
1080
1081 /**
1082  * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1083  * @out: drm_display_mode to return to the user
1084  * @in: drm_mode_modeinfo to use
1085  *
1086  * LOCKING:
1087  * None.
1088  *
1089  * Convert a drmo_mode_modeinfo into a drm_display_mode structure to return to
1090  * the caller.
1091  */
1092 void drm_crtc_convert_umode(struct drm_display_mode *out, struct drm_mode_modeinfo *in)
1093 {
1094         out->clock = in->clock;
1095         out->hdisplay = in->hdisplay;
1096         out->hsync_start = in->hsync_start;
1097         out->hsync_end = in->hsync_end;
1098         out->htotal = in->htotal;
1099         out->hskew = in->hskew;
1100         out->vdisplay = in->vdisplay;
1101         out->vsync_start = in->vsync_start;
1102         out->vsync_end = in->vsync_end;
1103         out->vtotal = in->vtotal;
1104         out->vscan = in->vscan;
1105         out->vrefresh = in->vrefresh;
1106         out->flags = in->flags;
1107         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1108         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1109 }
1110         
1111 /**
1112  * drm_mode_getresources - get graphics configuration
1113  * @inode: inode from the ioctl
1114  * @filp: file * from the ioctl
1115  * @cmd: cmd from ioctl
1116  * @arg: arg from ioctl
1117  *
1118  * LOCKING:
1119  * Takes mode config lock.
1120  *
1121  * Construct a set of configuration description structures and return
1122  * them to the user, including CRTC, output and framebuffer configuration.
1123  *
1124  * Called by the user via ioctl.
1125  *
1126  * RETURNS:
1127  * Zero on success, errno on failure.
1128  */
1129 int drm_mode_getresources(struct drm_device *dev,
1130                           void *data, struct drm_file *file_priv)
1131 {
1132         struct drm_mode_card_res *card_res = data;
1133         struct list_head *lh;
1134         struct drm_framebuffer *fb;
1135         struct drm_output *output;
1136         struct drm_crtc *crtc;
1137         struct drm_mode_modeinfo u_mode;
1138         struct drm_display_mode *mode;
1139         int ret = 0;
1140         int mode_count= 0;
1141         int output_count = 0;
1142         int crtc_count = 0;
1143         int fb_count = 0;
1144         int copied = 0;
1145         
1146         memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1147
1148         mutex_lock(&dev->mode_config.mutex);
1149
1150         list_for_each(lh, &dev->mode_config.fb_list)
1151                 fb_count++;
1152
1153         list_for_each(lh, &dev->mode_config.crtc_list)
1154                 crtc_count++;
1155
1156         list_for_each_entry(output, &dev->mode_config.output_list,
1157                             head) {
1158                 output_count++;
1159                 list_for_each(lh, &output->modes)
1160                         mode_count++;
1161         }
1162         list_for_each(lh, &dev->mode_config.usermode_list)
1163                 mode_count++;
1164
1165         if (card_res->count_modes == 0) {
1166                 DRM_DEBUG("probing modes %dx%d\n", dev->mode_config.max_width, dev->mode_config.max_height);
1167                 drm_crtc_probe_output_modes(dev, dev->mode_config.max_width, dev->mode_config.max_height);
1168                 mode_count = 0;
1169                 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1170                         list_for_each(lh, &output->modes)
1171                                 mode_count++;
1172                 }
1173                 list_for_each(lh, &dev->mode_config.usermode_list)
1174                         mode_count++;
1175         }
1176
1177         /* handle this in 4 parts */
1178         /* FBs */
1179         if (card_res->count_fbs >= fb_count) {
1180                 copied = 0;
1181                 list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
1182                         card_res->fb_id[copied++] = fb->id;
1183                 }
1184         }
1185         card_res->count_fbs = fb_count;
1186
1187         /* CRTCs */
1188         if (card_res->count_crtcs >= crtc_count) {
1189                 copied = 0;
1190                 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head){
1191                         DRM_DEBUG("CRTC ID is %d\n", crtc->id);
1192                         card_res->crtc_id[copied++] = crtc->id;
1193                 }
1194         }
1195         card_res->count_crtcs = crtc_count;
1196
1197
1198         /* Outputs */
1199         if (card_res->count_outputs >= output_count) {
1200                 copied = 0;
1201                 list_for_each_entry(output, &dev->mode_config.output_list,
1202                                     head) {
1203                         DRM_DEBUG("OUTPUT ID is %d\n", output->id);
1204                         card_res->output_id[copied++] = output->id;
1205                 }
1206         }
1207         card_res->count_outputs = output_count;
1208         
1209         /* Modes */
1210         if (card_res->count_modes >= mode_count) {
1211                 copied = 0;
1212                 list_for_each_entry(output, &dev->mode_config.output_list,
1213                                     head) {
1214                         list_for_each_entry(mode, &output->modes, head) {
1215                                 drm_crtc_convert_to_umode(&u_mode, mode);
1216                                 card_res->modes[copied++] = u_mode;
1217                         }
1218                 }
1219                 /* add in user modes */
1220                 list_for_each_entry(mode, &dev->mode_config.usermode_list, head) {
1221                         drm_crtc_convert_to_umode(&u_mode, mode);
1222                         card_res->modes[copied++] = u_mode;
1223                 }
1224         }
1225         card_res->count_modes = mode_count;
1226
1227         DRM_DEBUG("Counted %d %d %d\n", card_res->count_crtcs,
1228                   card_res->count_outputs,
1229                   card_res->count_modes);
1230         
1231         mutex_unlock(&dev->mode_config.mutex);
1232         return ret;
1233 }
1234
1235 /**
1236  * drm_mode_getcrtc - get CRTC configuration
1237  * @inode: inode from the ioctl
1238  * @filp: file * from the ioctl
1239  * @cmd: cmd from ioctl
1240  * @arg: arg from ioctl
1241  *
1242  * LOCKING:
1243  * Caller? (FIXME)
1244  *
1245  * Construct a CRTC configuration structure to return to the user.
1246  *
1247  * Called by the user via ioctl.
1248  *
1249  * RETURNS:
1250  * Zero on success, errno on failure.
1251  */
1252 int drm_mode_getcrtc(struct drm_device *dev,
1253                      void *data, struct drm_file *file_priv)
1254 {
1255         struct drm_mode_crtc *crtc_resp = data;
1256         struct drm_crtc *crtc;
1257         struct drm_output *output;
1258         int ocount;
1259         int ret = 0;
1260
1261         mutex_lock(&dev->mode_config.mutex);
1262         crtc = idr_find(&dev->mode_config.crtc_idr, crtc_resp->crtc_id);
1263         if (!crtc || (crtc->id != crtc_resp->crtc_id)) {
1264                 ret = -EINVAL;
1265                 goto out;
1266         }
1267
1268         crtc_resp->x = crtc->x;
1269         crtc_resp->y = crtc->y;
1270         crtc_resp->fb_id = 1;
1271
1272         crtc_resp->outputs = 0;
1273         if (crtc->enabled) {
1274
1275                 crtc_resp->mode = crtc->mode.mode_id;
1276                 ocount = 0;
1277                 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1278                         if (output->crtc == crtc)
1279                                 crtc_resp->outputs |= 1 << (ocount++);
1280                 }
1281         } else {
1282                 crtc_resp->mode = 0;
1283         }
1284
1285 out:
1286         mutex_unlock(&dev->mode_config.mutex);
1287         return ret;
1288 }
1289
1290 /**
1291  * drm_mode_getoutput - get output configuration
1292  * @inode: inode from the ioctl
1293  * @filp: file * from the ioctl
1294  * @cmd: cmd from ioctl
1295  * @arg: arg from ioctl
1296  *
1297  * LOCKING:
1298  * Caller? (FIXME)
1299  *
1300  * Construct a output configuration structure to return to the user.
1301  *
1302  * Called by the user via ioctl.
1303  *
1304  * RETURNS:
1305  * Zero on success, errno on failure.
1306  */
1307 int drm_mode_getoutput(struct drm_device *dev,
1308                        void *data, struct drm_file *file_priv)
1309 {
1310         struct drm_mode_get_output *out_resp = data;
1311         struct drm_output *output;
1312         struct drm_display_mode *mode;
1313         int mode_count = 0;
1314         int ret = 0;
1315         int copied = 0;
1316         int i;
1317
1318         DRM_DEBUG("output id %d:\n", out_resp->output);
1319
1320         mutex_lock(&dev->mode_config.mutex);
1321         output= idr_find(&dev->mode_config.crtc_idr, out_resp->output);
1322         if (!output || (output->id != out_resp->output)) {
1323                 ret = -EINVAL;
1324                 goto done;
1325         }
1326
1327         list_for_each_entry(mode, &output->modes, head)
1328                 mode_count++;
1329         
1330         for (i = 0; i < DRM_OUTPUT_MAX_UMODES; i++)
1331                 if (output->user_mode_ids[i] != 0)
1332                         mode_count++;
1333
1334         strncpy(out_resp->name, output->name, DRM_OUTPUT_NAME_LEN);
1335         out_resp->name[DRM_OUTPUT_NAME_LEN-1] = 0;
1336
1337         out_resp->mm_width = output->mm_width;
1338         out_resp->mm_height = output->mm_height;
1339         out_resp->subpixel = output->subpixel_order;
1340         out_resp->connection = output->status;
1341         if (output->crtc)
1342                 out_resp->crtc = output->crtc->id;
1343         else
1344                 out_resp->crtc = 0;
1345
1346         if ((out_resp->count_modes >= mode_count) && mode_count) {
1347                 copied = 0;
1348                 list_for_each_entry(mode, &output->modes, head) {
1349                         out_resp->modes[copied++] = mode->mode_id;
1350                 }
1351                 for (i = 0; i < DRM_OUTPUT_MAX_UMODES; i++) {
1352                         if (output->user_mode_ids[i] != 0)
1353                                 out_resp->modes[copied++] = output->user_mode_ids[i];
1354                 }
1355                         
1356         }
1357         out_resp->count_modes = mode_count;
1358
1359 done:
1360         mutex_unlock(&dev->mode_config.mutex);
1361         return ret;
1362 }
1363
1364 /**
1365  * drm_mode_setcrtc - set CRTC configuration
1366  * @inode: inode from the ioctl
1367  * @filp: file * from the ioctl
1368  * @cmd: cmd from ioctl
1369  * @arg: arg from ioctl
1370  *
1371  * LOCKING:
1372  * Caller? (FIXME)
1373  *
1374  * Build a new CRTC configuration based on user request.
1375  *
1376  * Called by the user via ioctl.
1377  *
1378  * RETURNS:
1379  * Zero on success, errno on failure.
1380  */
1381 int drm_mode_setcrtc(struct drm_device *dev,
1382                      void *data, struct drm_file *file_priv)
1383 {
1384         struct drm_mode_crtc *crtc_req = data;
1385         struct drm_crtc *crtc;
1386         struct drm_output **output_set = NULL, *output;
1387         struct drm_display_mode *mode;
1388         struct drm_framebuffer *fb = NULL;
1389         int ret = 0;
1390         int i;
1391
1392         mutex_lock(&dev->mode_config.mutex);
1393         crtc = idr_find(&dev->mode_config.crtc_idr, crtc_req->crtc_id);
1394         if (!crtc || (crtc->id != crtc_req->crtc_id)) {
1395                 DRM_DEBUG("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1396                 ret = -EINVAL;
1397                 goto out;
1398         }
1399
1400         if (crtc_req->mode) {
1401                 /* if we have a mode we need a framebuffer */
1402                 if (crtc_req->fb_id) {
1403                         fb = idr_find(&dev->mode_config.crtc_idr, crtc_req->fb_id);
1404                         if (!fb || (fb->id != crtc_req->fb_id)) {
1405                                 DRM_DEBUG("Unknown FB ID%d\n", crtc_req->fb_id);
1406                                 ret = -EINVAL;
1407                                 goto out;
1408                         }
1409                 }
1410                 mode = idr_find(&dev->mode_config.crtc_idr, crtc_req->mode);
1411                 if (!mode || (mode->mode_id != crtc_req->mode)) {
1412                         struct drm_output *output;
1413                         
1414                         list_for_each_entry(output, 
1415                                             &dev->mode_config.output_list,
1416                                             head) {
1417                                 list_for_each_entry(mode, &output->modes,
1418                                                     head) {
1419                                         drm_mode_debug_printmodeline(dev, 
1420                                                                      mode);
1421                                 }
1422                         }
1423
1424                         DRM_DEBUG("Unknown mode id %d, %p\n", crtc_req->mode, mode);
1425                         ret = -EINVAL;
1426                         goto out;
1427                 }
1428         } else
1429                 mode = NULL;
1430
1431         if (crtc_req->count_outputs == 0 && mode) {
1432                 DRM_DEBUG("Count outputs is 0 but mode set\n");
1433                 ret = -EINVAL;
1434                 goto out;
1435         }
1436
1437         if (crtc_req->count_outputs > 0 && !mode && !fb) {
1438                 DRM_DEBUG("Count outputs is %d but no mode or fb set\n", crtc_req->count_outputs);
1439                 ret = -EINVAL;
1440                 goto out;
1441         }
1442
1443         if (crtc_req->count_outputs > 0) {
1444                 u32 out_id;
1445                 output_set = kmalloc(crtc_req->count_outputs *
1446                                      sizeof(struct drm_output *), GFP_KERNEL);
1447                 if (!output_set) {
1448                         ret = -ENOMEM;
1449                         goto out;
1450                 }
1451
1452                 for (i = 0; i < crtc_req->count_outputs; i++) {
1453                         if (get_user(out_id, &crtc_req->set_outputs[i])) {
1454                                 ret = -EFAULT;
1455                                 goto out;
1456                         }
1457
1458                         output = idr_find(&dev->mode_config.crtc_idr, out_id);
1459                         if (!output || (out_id != output->id)) {
1460                                 DRM_DEBUG("Output id %d unknown\n", out_id);
1461                                 ret = -EINVAL;
1462                                 goto out;
1463                         }
1464
1465                         output_set[i] = output;
1466                 }
1467         }
1468                 
1469         ret = drm_crtc_set_config(crtc, crtc_req, mode, output_set, fb);
1470
1471 out:
1472         mutex_unlock(&dev->mode_config.mutex);
1473         return ret;
1474 }
1475
1476 /**
1477  * drm_mode_addfb - add an FB to the graphics configuration
1478  * @inode: inode from the ioctl
1479  * @filp: file * from the ioctl
1480  * @cmd: cmd from ioctl
1481  * @arg: arg from ioctl
1482  *
1483  * LOCKING:
1484  * Takes mode config lock.
1485  *
1486  * Add a new FB to the specified CRTC, given a user request.
1487  *
1488  * Called by the user via ioctl.
1489  *
1490  * RETURNS:
1491  * Zero on success, errno on failure.
1492  */
1493 int drm_mode_addfb(struct drm_device *dev,
1494                    void *data, struct drm_file *file_priv)
1495 {
1496         struct drm_mode_fb_cmd *r = data;
1497         struct drm_mode_config *config = &dev->mode_config;
1498         struct drm_framebuffer *fb;
1499         struct drm_buffer_object *bo;
1500         struct drm_crtc *crtc;
1501         int ret = 0;
1502
1503         if ((config->min_width > r->width) || (r->width > config->max_width)) {
1504                 DRM_ERROR("mode new framebuffer width not within limits\n");
1505                 return -EINVAL;
1506         }
1507         if ((config->min_height > r->height) || (r->height > config->max_height)) {
1508                 DRM_ERROR("mode new framebuffer height not within limits\n");
1509                 return -EINVAL;
1510         }
1511
1512         mutex_lock(&dev->mode_config.mutex);
1513         /* TODO check limits are okay */
1514         ret = drm_get_buffer_object(dev, &bo, r->handle);
1515         if (ret || !bo) {
1516                 ret = -EINVAL;
1517                 goto out;
1518         }
1519
1520         /* TODO check buffer is sufficently large */
1521         /* TODO setup destructor callback */
1522
1523         fb = drm_framebuffer_create(dev);
1524         if (!fb) {
1525                 ret = -EINVAL;
1526                 goto out;
1527         }
1528
1529         fb->width = r->width;
1530         fb->height = r->height;
1531         fb->pitch = r->pitch;
1532         fb->bits_per_pixel = r->bpp;
1533         fb->depth = r->depth;
1534         fb->offset = bo->offset;
1535         fb->bo = bo;
1536
1537         r->buffer_id = fb->id;
1538
1539         list_add(&fb->filp_head, &file_priv->fbs);
1540
1541         /* FIXME: bind the fb to the right crtc */
1542         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1543                 crtc->fb = fb;
1544                 dev->driver->fb_probe(dev, crtc);
1545         }
1546
1547 out:
1548         mutex_unlock(&dev->mode_config.mutex);
1549         return ret;
1550 }
1551
1552 /**
1553  * drm_mode_rmfb - remove an FB from the configuration
1554  * @inode: inode from the ioctl
1555  * @filp: file * from the ioctl
1556  * @cmd: cmd from ioctl
1557  * @arg: arg from ioctl
1558  *
1559  * LOCKING:
1560  * Takes mode config lock.
1561  *
1562  * Remove the FB specified by the user.
1563  *
1564  * Called by the user via ioctl.
1565  *
1566  * RETURNS:
1567  * Zero on success, errno on failure.
1568  */
1569 int drm_mode_rmfb(struct drm_device *dev,
1570                    void *data, struct drm_file *file_priv)
1571 {
1572         struct drm_framebuffer *fb = 0;
1573         uint32_t *id = data;
1574         int ret = 0;
1575
1576         fb = idr_find(&dev->mode_config.crtc_idr, *id);
1577         /* TODO check that we realy get a framebuffer back. */
1578         if (!fb || (*id != fb->id)) {
1579                 DRM_ERROR("mode invalid framebuffer id\n");
1580                 ret = -EINVAL;
1581                 goto out;
1582         }
1583
1584         /* TODO check if we own the buffer */
1585         /* TODO release all crtc connected to the framebuffer */
1586         /* bind the fb to the crtc for now */
1587         /* TODO unhock the destructor from the buffer object */
1588
1589         if (fb->bo->type != drm_bo_type_kernel)
1590                 drm_framebuffer_destroy(fb);
1591         else
1592                 dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1593
1594 out:
1595         mutex_unlock(&dev->mode_config.mutex);
1596         return ret;
1597 }
1598
1599 /**
1600  * drm_mode_getfb - get FB info
1601  * @inode: inode from the ioctl
1602  * @filp: file * from the ioctl
1603  * @cmd: cmd from ioctl
1604  * @arg: arg from ioctl
1605  *
1606  * LOCKING:
1607  * Caller? (FIXME)
1608  *
1609  * Lookup the FB given its ID and return info about it.
1610  *
1611  * Called by the user via ioctl.
1612  *
1613  * RETURNS:
1614  * Zero on success, errno on failure.
1615  */
1616 int drm_mode_getfb(struct drm_device *dev,
1617                    void *data, struct drm_file *file_priv)
1618 {
1619         struct drm_mode_fb_cmd *r = data;
1620         struct drm_framebuffer *fb;
1621         int ret = 0;
1622
1623         mutex_lock(&dev->mode_config.mutex);
1624         fb = idr_find(&dev->mode_config.crtc_idr, r->buffer_id);
1625         if (!fb || (r->buffer_id != fb->id)) {
1626                 DRM_ERROR("invalid framebuffer id\n");
1627                 ret = -EINVAL;
1628                 goto out;
1629         }
1630
1631         r->height = fb->height;
1632         r->width = fb->width;
1633         r->depth = fb->depth;
1634         r->bpp = fb->bits_per_pixel;
1635         r->handle = fb->bo->base.hash.key;
1636         r->pitch = fb->pitch;
1637
1638 out:
1639         mutex_unlock(&dev->mode_config.mutex);
1640         return ret;
1641 }
1642
1643 /**
1644  * drm_fb_release - remove and free the FBs on this file
1645  * @filp: file * from the ioctl
1646  *
1647  * LOCKING:
1648  * Takes mode config lock.
1649  *
1650  * Destroy all the FBs associated with @filp.
1651  *
1652  * Called by the user via ioctl.
1653  *
1654  * RETURNS:
1655  * Zero on success, errno on failure.
1656  */
1657 void drm_fb_release(struct file *filp)
1658 {
1659         struct drm_file *priv = filp->private_data;
1660         struct drm_device *dev = priv->head->dev;
1661         struct drm_framebuffer *fb, *tfb;
1662
1663         mutex_lock(&dev->mode_config.mutex);
1664         list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1665                 list_del(&fb->filp_head);
1666                 if (fb->bo->type != drm_bo_type_kernel)
1667                         drm_framebuffer_destroy(fb);
1668                 else
1669                         dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1670         }
1671         mutex_unlock(&dev->mode_config.mutex);
1672 }
1673
1674 /**
1675  * drm_fb_newmode - adds a user defined mode
1676  * @inode: inode from the ioctl
1677  * @filp: file * from the ioctl
1678  * @cmd: cmd from ioctl
1679  * @arg: arg from ioctl
1680  *
1681  * Adds a user specified mode to the kernel.
1682  *
1683  * Called by the user via ioctl.
1684  *
1685  * RETURNS:
1686  * writes new mode id into arg.
1687  * Zero on success, errno on failure.
1688  */
1689 int drm_mode_addmode(struct drm_device *dev,
1690                      void *data, struct drm_file *file_priv)
1691 {
1692         struct drm_mode_modeinfo *new_mode = data;
1693         struct drm_display_mode *user_mode;
1694         int ret = 0;
1695
1696         mutex_lock(&dev->mode_config.mutex);
1697         user_mode = drm_mode_create(dev);
1698         if (!user_mode) {
1699                 ret = -ENOMEM;
1700                 goto out;
1701         }
1702
1703         drm_crtc_convert_umode(user_mode, new_mode);
1704         user_mode->type |= DRM_MODE_TYPE_USERDEF;
1705
1706         user_mode->output_count = 0;
1707
1708         list_add(&user_mode->head, &dev->mode_config.usermode_list);
1709
1710         new_mode->id = user_mode->mode_id;
1711
1712 out:
1713         mutex_unlock(&dev->mode_config.mutex);
1714         return ret;
1715 }
1716
1717 /**
1718  * drm_fb_rmmode - removes a user defined mode
1719  * @inode: inode from the ioctl
1720  * @filp: file * from the ioctl
1721  * @cmd: cmd from ioctl
1722  * @arg: arg from ioctl
1723  *
1724  * Remove the user defined mode specified by the user.
1725  *
1726  * Called by the user via ioctl
1727  *
1728  * RETURNS:
1729  * Zero on success, errno on failure.
1730  */
1731 int drm_mode_rmmode(struct drm_device *dev,
1732                     void *data, struct drm_file *file_priv)
1733 {
1734         uint32_t *id = data;
1735         struct drm_display_mode *mode, *t;
1736         int ret = -EINVAL;
1737
1738         mutex_lock(&dev->mode_config.mutex);    
1739         mode = idr_find(&dev->mode_config.crtc_idr, *id);
1740         if (!mode || (*id != mode->mode_id)) {
1741                 ret = -EINVAL;
1742                 goto out;
1743         }
1744
1745         if (!(mode->type & DRM_MODE_TYPE_USERDEF)) {
1746                 ret = -EINVAL;
1747                 goto out;
1748         }
1749
1750         if (mode->output_count) {
1751                 ret = -EINVAL;
1752                 goto out;
1753         }
1754
1755         list_for_each_entry(t, &dev->mode_config.usermode_list, head) {
1756                 if (t == mode) {
1757                         list_del(&mode->head);
1758                         drm_mode_destroy(dev, mode);
1759                         ret = 0;
1760                         break;
1761                 }
1762         }
1763
1764 out:
1765         mutex_unlock(&dev->mode_config.mutex);
1766         return ret;
1767 }
1768
1769 /**
1770  * drm_fb_attachmode - Attach a user mode to an output
1771  * @inode: inode from the ioctl
1772  * @filp: file * from the ioctl
1773  * @cmd: cmd from ioctl
1774  * @arg: arg from ioctl
1775  *
1776  * This attaches a user specified mode to an output.
1777  * Called by the user via ioctl.
1778  *
1779  * RETURNS:
1780  * Zero on success, errno on failure.
1781  */
1782 int drm_mode_attachmode(struct drm_device *dev,
1783                         void *data, struct drm_file *file_priv)
1784 {
1785         struct drm_mode_mode_cmd *mode_cmd = data;
1786         struct drm_output *output;
1787         struct drm_display_mode *mode;
1788         int i, ret = 0;
1789
1790         mutex_lock(&dev->mode_config.mutex);
1791
1792         mode = idr_find(&dev->mode_config.crtc_idr, mode_cmd->mode_id);
1793         if (!mode || (mode->mode_id != mode_cmd->mode_id)) {
1794                 ret = -EINVAL;
1795                 goto out;
1796         }
1797
1798         output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
1799         if (!output || (output->id != mode_cmd->output_id)) {
1800                 ret = -EINVAL;
1801                 goto out;
1802         }
1803
1804         for (i = 0; i < DRM_OUTPUT_MAX_UMODES; i++) {
1805                 if (output->user_mode_ids[i] == 0) {
1806                         output->user_mode_ids[i] = mode->mode_id;
1807                         mode->output_count++;
1808                         break;
1809                 }
1810         }
1811
1812         if (i == DRM_OUTPUT_MAX_UMODES)
1813                 ret = -ENOSPC;
1814
1815 out:
1816         mutex_unlock(&dev->mode_config.mutex);
1817         return ret;
1818 }
1819
1820
1821 /**
1822  * drm_fb_detachmode - Detach a user specified mode from an output
1823  * @inode: inode from the ioctl
1824  * @filp: file * from the ioctl
1825  * @cmd: cmd from ioctl
1826  * @arg: arg from ioctl
1827  *
1828  * Called by the user via ioctl.
1829  *
1830  * RETURNS:
1831  * Zero on success, errno on failure.
1832  */
1833 int drm_mode_detachmode(struct drm_device *dev,
1834                         void *data, struct drm_file *file_priv)
1835 {
1836         struct drm_mode_mode_cmd *mode_cmd = data;
1837         struct drm_output *output;
1838         struct drm_display_mode *mode;
1839         int i, found = 0, ret = 0;
1840
1841         mutex_lock(&dev->mode_config.mutex);
1842
1843         mode = idr_find(&dev->mode_config.crtc_idr, mode_cmd->mode_id);
1844         if (!mode || (mode->mode_id != mode_cmd->mode_id)) {
1845                 ret = -EINVAL;
1846                 goto out;
1847         }
1848
1849         output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
1850         if (!output || (output->id != mode_cmd->output_id)) {
1851                 ret = -EINVAL;
1852                 goto out;
1853         }
1854
1855
1856         for (i = 0; i < DRM_OUTPUT_MAX_UMODES; i++) {
1857                 if (output->user_mode_ids[i] == mode->mode_id) {
1858                         output->user_mode_ids[i] = 0;
1859                         mode->output_count--;
1860                         found = 1;
1861                 }
1862         }
1863
1864         if (!found)
1865                 ret = -EINVAL;
1866
1867 out:           
1868         mutex_unlock(&dev->mode_config.mutex);
1869         return ret;
1870 }