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