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