Split TV property creation into its own routine
[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_create - create a new CRTC object
236  * @dev: DRM device
237  * @funcs: callbacks for the new CRTC
238  *
239  * LOCKING:
240  * Caller must hold mode config lock.
241  *
242  * Creates a new CRTC object and adds it to @dev's mode_config structure.
243  *
244  * RETURNS:
245  * Pointer to new CRTC object or NULL on error.
246  */
247 struct drm_crtc *drm_crtc_create(struct drm_device *dev,
248                                  const struct drm_crtc_funcs *funcs)
249 {
250         struct drm_crtc *crtc;
251
252         crtc = kzalloc(sizeof(struct drm_crtc), GFP_KERNEL);
253         if (!crtc)
254                 return NULL;
255
256         crtc->dev = dev;
257         crtc->funcs = funcs;
258
259         crtc->id = drm_idr_get(dev, crtc);
260
261         list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
262         dev->mode_config.num_crtc++;
263
264         return crtc;
265 }
266 EXPORT_SYMBOL(drm_crtc_create);
267
268 /**
269  * drm_crtc_destroy - remove a CRTC object
270  * @crtc: CRTC to remove
271  *
272  * LOCKING:
273  * Caller must hold mode config lock.
274  *
275  * Cleanup @crtc.  Calls @crtc's cleanup function, then removes @crtc from
276  * its associated DRM device's mode_config.  Frees it afterwards.
277  */
278 void drm_crtc_destroy(struct drm_crtc *crtc)
279 {
280         struct drm_device *dev = crtc->dev;
281
282         if (crtc->funcs->cleanup)
283                 (*crtc->funcs->cleanup)(crtc);
284
285         drm_idr_put(dev, crtc->id);
286         list_del(&crtc->head);
287         dev->mode_config.num_crtc--;
288         kfree(crtc);
289 }
290 EXPORT_SYMBOL(drm_crtc_destroy);
291
292 /**
293  * drm_crtc_in_use - check if a given CRTC is in a mode_config
294  * @crtc: CRTC to check
295  *
296  * LOCKING:
297  * Caller must hold mode config lock.
298  *
299  * Walk @crtc's DRM device's mode_config and see if it's in use.
300  *
301  * RETURNS:
302  * True if @crtc is part of the mode_config, false otherwise.
303  */
304 bool drm_crtc_in_use(struct drm_crtc *crtc)
305 {
306         struct drm_output *output;
307         struct drm_device *dev = crtc->dev;
308         /* FIXME: Locking around list access? */
309         list_for_each_entry(output, &dev->mode_config.output_list, head)
310                 if (output->crtc == crtc)
311                         return true;
312         return false;
313 }
314 EXPORT_SYMBOL(drm_crtc_in_use);
315
316 /*
317  * Detailed mode info for a standard 640x480@60Hz monitor
318  */
319 static struct drm_display_mode std_mode[] = {
320         { DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 25200, 640, 656,
321                    752, 800, 0, 480, 490, 492, 525, 0,
322                    V_NHSYNC | V_NVSYNC) }, /* 640x480@60Hz */
323 };
324
325 /**
326  * drm_crtc_probe_output_modes - get complete set of display modes
327  * @dev: DRM device
328  * @maxX: max width for modes
329  * @maxY: max height for modes
330  *
331  * LOCKING:
332  * Caller must hold mode config lock.
333  *
334  * Based on @dev's mode_config layout, scan all the outputs and try to detect
335  * modes on them.  Modes will first be added to the output's probed_modes
336  * list, then culled (based on validity and the @maxX, @maxY parameters) and
337  * put into the normal modes list.
338  *
339  * Intended to be used either at bootup time or when major configuration
340  * changes have occurred.
341  *
342  * FIXME: take into account monitor limits
343  */
344 void drm_crtc_probe_single_output_modes(struct drm_output *output, int maxX, int maxY)
345 {
346         struct drm_device *dev = output->dev;
347         struct drm_display_mode *mode, *t;
348         int ret;
349         //if (maxX == 0 || maxY == 0) 
350         // TODO
351
352         /* set all modes to the unverified state */
353         list_for_each_entry_safe(mode, t, &output->modes, head)
354                 mode->status = MODE_UNVERIFIED;
355                 
356         output->status = (*output->funcs->detect)(output);
357         
358         if (output->status == output_status_disconnected) {
359                 DRM_DEBUG("%s is disconnected\n", drm_get_output_name(output));
360                 /* TODO set EDID to NULL */
361                 return;
362         }
363         
364         ret = (*output->funcs->get_modes)(output);
365         
366         if (ret) {
367                 drm_mode_output_list_update(output);
368         }
369         
370         if (maxX && maxY)
371                 drm_mode_validate_size(dev, &output->modes, maxX,
372                                        maxY, 0);
373         list_for_each_entry_safe(mode, t, &output->modes, head) {
374                 if (mode->status == MODE_OK)
375                         mode->status = (*output->funcs->mode_valid)(output,mode);
376         }
377         
378         
379         drm_mode_prune_invalid(dev, &output->modes, TRUE);
380         
381         if (list_empty(&output->modes)) {
382                 struct drm_display_mode *stdmode;
383                 
384                 DRM_DEBUG("No valid modes on %s\n", drm_get_output_name(output));
385                 
386                 /* Should we do this here ???
387                  * When no valid EDID modes are available we end up
388                  * here and bailed in the past, now we add a standard
389                  * 640x480@60Hz mode and carry on.
390                  */
391                 stdmode = drm_mode_duplicate(dev, &std_mode[0]);
392                 drm_mode_probed_add(output, stdmode);
393                 drm_mode_list_concat(&output->probed_modes,
394                                      &output->modes);
395                 
396                 DRM_DEBUG("Adding standard 640x480 @ 60Hz to %s\n",
397                           drm_get_output_name(output));
398         }
399         
400         drm_mode_sort(&output->modes);
401         
402         DRM_DEBUG("Probed modes for %s\n", drm_get_output_name(output));
403         list_for_each_entry_safe(mode, t, &output->modes, head) {
404                 mode->vrefresh = drm_mode_vrefresh(mode);
405                 
406                 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
407                 drm_mode_debug_printmodeline(dev, mode);
408         }
409 }
410
411 void drm_crtc_probe_output_modes(struct drm_device *dev, int maxX, int maxY)
412 {
413         struct drm_output *output;
414
415         list_for_each_entry(output, &dev->mode_config.output_list, head) {
416                 drm_crtc_probe_single_output_modes(output, maxX, maxY);
417         }
418 }
419 EXPORT_SYMBOL(drm_crtc_probe_output_modes);
420
421 /**
422  * drm_crtc_set_mode - set a mode
423  * @crtc: CRTC to program
424  * @mode: mode to use
425  * @x: width of mode
426  * @y: height of mode
427  *
428  * LOCKING:
429  * Caller must hold mode config lock.
430  *
431  * Try to set @mode on @crtc.  Give @crtc and its associated outputs a chance
432  * to fixup or reject the mode prior to trying to set it.
433  *
434  * RETURNS:
435  * True if the mode was set successfully, or false otherwise.
436  */
437 bool drm_crtc_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
438                        int x, int y)
439 {
440         struct drm_device *dev = crtc->dev;
441         struct drm_display_mode *adjusted_mode, saved_mode;
442         int saved_x, saved_y;
443         struct drm_output *output;
444         bool ret = true;
445
446         adjusted_mode = drm_mode_duplicate(dev, mode);
447
448         crtc->enabled = drm_crtc_in_use(crtc);
449
450         if (!crtc->enabled)
451                 return true;
452
453         saved_mode = crtc->mode;
454         saved_x = crtc->x;
455         saved_y = crtc->y;
456         
457         /* Update crtc values up front so the driver can rely on them for mode
458          * setting.
459          */
460         crtc->mode = *mode;
461         crtc->x = x;
462         crtc->y = y;
463
464         if (drm_mode_equal(&saved_mode, &crtc->mode)) {
465                 if (saved_x != crtc->x || saved_y != crtc->y) {
466                         crtc->funcs->mode_set_base(crtc, crtc->x, crtc->y);
467                         goto done;
468                 }
469         }
470
471         /* Pass our mode to the outputs and the CRTC to give them a chance to
472          * adjust it according to limitations or output properties, and also
473          * a chance to reject the mode entirely.
474          */
475         list_for_each_entry(output, &dev->mode_config.output_list, head) {
476                 
477                 if (output->crtc != crtc)
478                         continue;
479                 
480                 if (!(ret = output->funcs->mode_fixup(output, mode, adjusted_mode))) {
481                         goto done;
482                 }
483         }
484         
485         if (!(ret = crtc->funcs->mode_fixup(crtc, mode, adjusted_mode))) {
486                 goto done;
487         }
488
489         /* Prepare the outputs and CRTCs before setting the mode. */
490         list_for_each_entry(output, &dev->mode_config.output_list, head) {
491
492                 if (output->crtc != crtc)
493                         continue;
494                 
495                 /* Disable the output as the first thing we do. */
496                 output->funcs->prepare(output);
497         }
498         
499         crtc->funcs->prepare(crtc);
500         
501         /* Set up the DPLL and any output state that needs to adjust or depend
502          * on the DPLL.
503          */
504         crtc->funcs->mode_set(crtc, mode, adjusted_mode, x, y);
505
506         list_for_each_entry(output, &dev->mode_config.output_list, head) {
507
508                 if (output->crtc != crtc)
509                         continue;
510                 
511                 DRM_INFO("%s: set mode %s %x\n", drm_get_output_name(output), mode->name, mode->mode_id);
512
513                 output->funcs->mode_set(output, mode, adjusted_mode);
514         }
515         
516         /* Now, enable the clocks, plane, pipe, and outputs that we set up. */
517         crtc->funcs->commit(crtc);
518
519         list_for_each_entry(output, &dev->mode_config.output_list, head) {
520
521                 if (output->crtc != crtc)
522                         continue;
523                 
524                 output->funcs->commit(output);
525
526 #if 0 // TODO def RANDR_12_INTERFACE
527                 if (output->randr_output)
528                         RRPostPendingProperties (output->randr_output);
529 #endif
530         }
531         
532         /* XXX free adjustedmode */
533         drm_mode_destroy(dev, adjusted_mode);
534         /* TODO */
535 //      if (scrn->pScreen)
536 //              drm_crtc_set_screen_sub_pixel_order(dev);
537
538 done:
539         if (!ret) { 
540                 crtc->mode = saved_mode;
541                 crtc->x = saved_x;
542                 crtc->y = saved_y;
543         } 
544         
545         return ret;
546 }
547 EXPORT_SYMBOL(drm_crtc_set_mode);
548
549 /**
550  * drm_disable_unused_functions - disable unused objects
551  * @dev: DRM device
552  *
553  * LOCKING:
554  * Caller must hold mode config lock.
555  *
556  * If an output or CRTC isn't part of @dev's mode_config, it can be disabled
557  * by calling its dpms function, which should power it off.
558  */
559 void drm_disable_unused_functions(struct drm_device *dev)
560 {
561         struct drm_output *output;
562         struct drm_crtc *crtc;
563
564         list_for_each_entry(output, &dev->mode_config.output_list, head) {
565                 if (!output->crtc)
566                         (*output->funcs->dpms)(output, DPMSModeOff);
567         }
568
569         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
570                 if (!crtc->enabled)
571                         crtc->funcs->dpms(crtc, DPMSModeOff);
572         }
573 }
574 EXPORT_SYMBOL(drm_disable_unused_functions);
575
576 /**
577  * drm_mode_probed_add - add a mode to the specified output's probed mode list
578  * @output: output the new mode
579  * @mode: mode data
580  *
581  * LOCKING:
582  * Caller must hold mode config lock.
583  * 
584  * Add @mode to @output's mode list for later use.
585  */
586 void drm_mode_probed_add(struct drm_output *output,
587                          struct drm_display_mode *mode)
588 {
589         list_add(&mode->head, &output->probed_modes);
590 }
591 EXPORT_SYMBOL(drm_mode_probed_add);
592
593 /**
594  * drm_mode_remove - remove and free a mode
595  * @output: output list to modify
596  * @mode: mode to remove
597  *
598  * LOCKING:
599  * Caller must hold mode config lock.
600  * 
601  * Remove @mode from @output's mode list, then free it.
602  */
603 void drm_mode_remove(struct drm_output *output, struct drm_display_mode *mode)
604 {
605         list_del(&mode->head);
606         kfree(mode);
607 }
608 EXPORT_SYMBOL(drm_mode_remove);
609
610 /**
611  * drm_output_create - create a new output
612  * @dev: DRM device
613  * @funcs: callbacks for this output
614  * @name: user visible name of the output
615  *
616  * LOCKING:
617  * Caller must hold @dev's mode_config lock.
618  *
619  * Creates a new drm_output structure and adds it to @dev's mode_config
620  * structure.
621  *
622  * RETURNS:
623  * Pointer to the new output or NULL on error.
624  */
625 struct drm_output *drm_output_create(struct drm_device *dev,
626                                      const struct drm_output_funcs *funcs,
627                                      int output_type)
628 {
629         struct drm_output *output = NULL;
630
631         output = kzalloc(sizeof(struct drm_output), GFP_KERNEL);
632         if (!output)
633                 return NULL;
634                 
635         output->dev = dev;
636         output->funcs = funcs;
637         output->id = drm_idr_get(dev, output);
638         output->output_type = output_type;
639         output->output_type_id = 1; /* TODO */
640         INIT_LIST_HEAD(&output->user_modes);
641         INIT_LIST_HEAD(&output->probed_modes);
642         INIT_LIST_HEAD(&output->modes);
643         /* randr_output? */
644         /* output_set_monitor(output)? */
645         /* check for output_ignored(output)? */
646
647         mutex_lock(&dev->mode_config.mutex);
648         list_add_tail(&output->head, &dev->mode_config.output_list);
649         dev->mode_config.num_output++;
650
651         drm_output_attach_property(output, dev->mode_config.edid_property, 0);
652
653         drm_output_attach_property(output, dev->mode_config.dpms_property, 0);
654
655         mutex_unlock(&dev->mode_config.mutex);
656
657         return output;
658
659 }
660 EXPORT_SYMBOL(drm_output_create);
661
662 /**
663  * drm_output_destroy - remove an output
664  * @output: output to remove
665  *
666  * LOCKING:
667  * Caller must hold @dev's mode_config lock.
668  *
669  * Call @output's cleanup function, then remove the output from the DRM
670  * mode_config after freeing @output's modes.
671  */
672 void drm_output_destroy(struct drm_output *output)
673 {
674         struct drm_device *dev = output->dev;
675         struct drm_display_mode *mode, *t;
676
677         if (*output->funcs->cleanup)
678                 (*output->funcs->cleanup)(output);
679
680         list_for_each_entry_safe(mode, t, &output->probed_modes, head)
681                 drm_mode_remove(output, mode);
682
683         list_for_each_entry_safe(mode, t, &output->modes, head)
684                 drm_mode_remove(output, mode);
685
686         list_for_each_entry_safe(mode, t, &output->user_modes, head)
687                 drm_mode_remove(output, mode);
688
689         mutex_lock(&dev->mode_config.mutex);
690         drm_idr_put(dev, output->id);
691         list_del(&output->head);
692         mutex_unlock(&dev->mode_config.mutex);
693         kfree(output);
694 }
695 EXPORT_SYMBOL(drm_output_destroy);
696
697
698 /**
699  * drm_mode_create - create a new display mode
700  * @dev: DRM device
701  *
702  * LOCKING:
703  * None.
704  *
705  * Create a new drm_display_mode, give it an ID, and return it.
706  *
707  * RETURNS:
708  * Pointer to new mode on success, NULL on error.
709  */
710 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
711 {
712         struct drm_display_mode *nmode;
713
714         nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
715         if (!nmode)
716                 return NULL;
717
718         nmode->mode_id = drm_idr_get(dev, nmode);
719         return nmode;
720 }
721 EXPORT_SYMBOL(drm_mode_create);
722
723 /**
724  * drm_mode_destroy - remove a mode
725  * @dev: DRM device
726  * @mode: mode to remove
727  *
728  * LOCKING:
729  * Caller must hold mode config lock.
730  *
731  * Free @mode's unique identifier, then free it.
732  */
733 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
734 {
735         drm_idr_put(dev, mode->mode_id);
736
737         kfree(mode);
738 }
739 EXPORT_SYMBOL(drm_mode_destroy);
740
741 static int drm_mode_create_standard_output_properties(struct drm_device *dev)
742 {
743         int i;
744
745         /*
746          * Standard properties (apply to all outputs)
747          */
748         dev->mode_config.edid_property =
749                 drm_property_create(dev, DRM_MODE_PROP_BLOB | DRM_MODE_PROP_IMMUTABLE,
750                                     "EDID", 0);
751
752         dev->mode_config.dpms_property =
753                 drm_property_create(dev, DRM_MODE_PROP_ENUM, 
754                         "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
755         for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
756                 drm_property_add_enum(dev->mode_config.dpms_property, i, drm_dpms_enum_list[i].type, drm_dpms_enum_list[i].name);
757
758         dev->mode_config.connector_type_property =
759                 drm_property_create(dev, DRM_MODE_PROP_ENUM | DRM_MODE_PROP_IMMUTABLE,
760                         "Connector Type", ARRAY_SIZE(drm_conn_enum_list));
761         for (i = 0; i < ARRAY_SIZE(drm_conn_enum_list); i++)
762                 drm_property_add_enum(dev->mode_config.connector_type_property, i, drm_conn_enum_list[i].type, drm_conn_enum_list[i].name);
763
764         dev->mode_config.connector_num_property =
765                 drm_property_create(dev, DRM_MODE_PROP_RANGE | DRM_MODE_PROP_IMMUTABLE,
766                         "Connector ID", 2);
767         dev->mode_config.connector_num_property->values[0] = 0;
768         dev->mode_config.connector_num_property->values[1] = 20;
769
770         return 0;
771 }
772
773 /**
774  * drm_create_tv_properties - create TV specific output properties
775  * @dev: DRM device
776  * @num_modes: number of different TV formats (modes) supported
777  * @modes: array of pointers to strings containing name of each format
778  *
779  * Called by a driver's TV initialization routine, this function creates
780  * the TV specific output properties for a given device.  Caller is
781  * responsible for allocating a list of format names and passing them to
782  * this routine.
783  */
784 bool drm_create_tv_properties(struct drm_device *dev, int num_modes,
785                               char *modes[])
786 {
787         int i;
788
789         dev->mode_config.tv_left_margin_property =
790                 drm_property_create(dev, DRM_MODE_PROP_RANGE |
791                                     DRM_MODE_PROP_IMMUTABLE,
792                                     "left margin", 2);
793         dev->mode_config.tv_left_margin_property->values[0] = 0;
794         dev->mode_config.tv_left_margin_property->values[1] = 100;
795
796         dev->mode_config.tv_right_margin_property =
797                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
798                                     "right margin", 2);
799         dev->mode_config.tv_right_margin_property->values[0] = 0;
800         dev->mode_config.tv_right_margin_property->values[1] = 100;
801
802         dev->mode_config.tv_top_margin_property =
803                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
804                                     "top margin", 2);
805         dev->mode_config.tv_top_margin_property->values[0] = 0;
806         dev->mode_config.tv_top_margin_property->values[1] = 100;
807
808         dev->mode_config.tv_bottom_margin_property =
809                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
810                                     "bottom margin", 2);
811         dev->mode_config.tv_bottom_margin_property->values[0] = 0;
812         dev->mode_config.tv_bottom_margin_property->values[1] = 100;
813
814         dev->mode_config.tv_mode_property =
815                 drm_property_create(dev, DRM_MODE_PROP_ENUM,
816                                     "mode", num_modes);
817         for (i = 0; i < num_modes; i++)
818                 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
819                                       i, modes[i]);
820
821         return 0;
822 }
823 EXPORT_SYMBOL(drm_create_tv_properties);
824
825 /**
826  * drm_mode_config_init - initialize DRM mode_configuration structure
827  * @dev: DRM device
828  *
829  * LOCKING:
830  * None, should happen single threaded at init time.
831  *
832  * Initialize @dev's mode_config structure, used for tracking the graphics
833  * configuration of @dev.
834  */
835 void drm_mode_config_init(struct drm_device *dev)
836 {
837         mutex_init(&dev->mode_config.mutex);
838         INIT_LIST_HEAD(&dev->mode_config.fb_list);
839         INIT_LIST_HEAD(&dev->mode_config.crtc_list);
840         INIT_LIST_HEAD(&dev->mode_config.output_list);
841         INIT_LIST_HEAD(&dev->mode_config.property_list);
842         INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
843         idr_init(&dev->mode_config.crtc_idr);
844
845         drm_mode_create_standard_output_properties(dev);
846
847         /* Just to be sure */
848         dev->mode_config.num_fb = 0;
849         dev->mode_config.num_output = 0;
850         dev->mode_config.num_crtc = 0;
851         dev->mode_config.hotplug_counter = 0;
852 }
853 EXPORT_SYMBOL(drm_mode_config_init);
854
855 /**
856  * drm_get_buffer_object - find the buffer object for a given handle
857  * @dev: DRM device
858  * @bo: pointer to caller's buffer_object pointer
859  * @handle: handle to lookup
860  *
861  * LOCKING:
862  * Must take @dev's struct_mutex to protect buffer object lookup.
863  *
864  * Given @handle, lookup the buffer object in @dev and put it in the caller's
865  * @bo pointer.
866  *
867  * RETURNS:
868  * Zero on success, -EINVAL if the handle couldn't be found.
869  */
870 static int drm_get_buffer_object(struct drm_device *dev, struct drm_buffer_object **bo, unsigned long handle)
871 {
872         struct drm_user_object *uo;
873         struct drm_hash_item *hash;
874         int ret;
875
876         *bo = NULL;
877
878         mutex_lock(&dev->struct_mutex);
879         ret = drm_ht_find_item(&dev->object_hash, handle, &hash);
880         if (ret) {
881                 DRM_ERROR("Couldn't find handle.\n");
882                 ret = -EINVAL;
883                 goto out_err;
884         }
885
886         uo = drm_hash_entry(hash, struct drm_user_object, hash);
887         if (uo->type != drm_buffer_type) {
888                 ret = -EINVAL;
889                 goto out_err;
890         }
891         
892         *bo = drm_user_object_entry(uo, struct drm_buffer_object, base);
893         ret = 0;
894 out_err:
895         mutex_unlock(&dev->struct_mutex);
896         return ret;
897 }
898
899 /**
900  * drm_pick_crtcs - pick crtcs for output devices
901  * @dev: DRM device
902  *
903  * LOCKING:
904  * Caller must hold mode config lock.
905  */
906 static void drm_pick_crtcs (struct drm_device *dev)
907 {
908         int c, o, assigned;
909         struct drm_output *output, *output_equal;
910         struct drm_crtc   *crtc;
911         struct drm_display_mode *des_mode = NULL, *modes, *modes_equal;
912         int found;
913
914         list_for_each_entry(output, &dev->mode_config.output_list, head) {
915                 output->crtc = NULL;
916     
917                 /* Don't hook up outputs that are disconnected ??
918                  *
919                  * This is debateable. Do we want fixed /dev/fbX or
920                  * dynamic on hotplug (need mode code for that though) ?
921                  *
922                  * If we don't hook up outputs now, then we only create
923                  * /dev/fbX for the output that's enabled, that's good as
924                  * the users console will be on that output.
925                  *
926                  * If we do hook up outputs that are disconnected now, then
927                  * the user may end up having to muck about with the fbcon
928                  * map flags to assign his console to the enabled output. Ugh.
929                  */
930                 if (output->status != output_status_connected)
931                         continue;
932
933                 if (list_empty(&output->modes))
934                         continue;
935
936                 des_mode = NULL;
937                 found = 0;
938                 list_for_each_entry(des_mode, &output->modes, head) {
939                         if (des_mode->type & DRM_MODE_TYPE_PREFERRED) {
940                                 found = 1;
941                                 break;
942                         }
943                 }
944
945                 /* No preferred mode, let's just select the first available */
946                 if (!found) {
947                         des_mode = NULL;
948                         list_for_each_entry(des_mode, &output->modes, head) {
949                                 break;
950                         }
951                 }
952
953                 c = -1;
954                 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
955                         assigned = 0;
956
957                         c++;
958                         if ((output->possible_crtcs & (1 << c)) == 0)
959                                 continue;
960         
961                         list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
962                                 if (output->id == output_equal->id)
963                                         continue;
964
965                                 /* Find out if crtc has been assigned before */
966                                 if (output_equal->crtc == crtc)
967                                         assigned = 1;
968                         }
969
970 #if 1 /* continue for now */
971                         if (assigned)
972                                 continue;
973 #endif
974
975                         o = -1;
976                         list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
977                                 o++;
978                                 if (output->id == output_equal->id)
979                                         continue;
980
981                                 list_for_each_entry(modes, &output->modes, head) {
982                                         list_for_each_entry(modes_equal, &output_equal->modes, head) {
983                                                 if (drm_mode_equal (modes, modes_equal)) {
984                                                         if ((output->possible_clones & output_equal->possible_clones) && (output_equal->crtc == crtc)) {
985                                                                 printk("Cloning %s (0x%lx) to %s (0x%lx)\n",drm_get_output_name(output),output->possible_clones,drm_get_output_name(output_equal),output_equal->possible_clones);
986                                                                 des_mode = modes;
987                                                                 assigned = 0;
988                                                                 goto clone;
989                                                         }
990                                                 }
991                                         }
992                                 }
993                         }
994
995 clone:
996                         /* crtc has been assigned skip it */
997                         if (assigned)
998                                 continue;
999
1000                         /* Found a CRTC to attach to, do it ! */
1001                         output->crtc = crtc;
1002                         output->crtc->desired_mode = des_mode;
1003                         output->initial_x = 0;
1004                         output->initial_y = 0;
1005                         DRM_DEBUG("Desired mode for CRTC %d is 0x%x:%s\n",c,des_mode->mode_id, des_mode->name);
1006                         break;
1007                 }
1008         }
1009 }
1010 EXPORT_SYMBOL(drm_pick_crtcs);
1011
1012 /**
1013  * drm_initial_config - setup a sane initial output configuration
1014  * @dev: DRM device
1015  * @can_grow: this configuration is growable
1016  *
1017  * LOCKING:
1018  * Called at init time, must take mode config lock.
1019  *
1020  * Scan the CRTCs and outputs and try to put together an initial setup.
1021  * At the moment, this is a cloned configuration across all heads with
1022  * a new framebuffer object as the backing store.
1023  *
1024  * RETURNS:
1025  * Zero if everything went ok, nonzero otherwise.
1026  */
1027 bool drm_initial_config(struct drm_device *dev, bool can_grow)
1028 {
1029         struct drm_output *output;
1030         struct drm_crtc *crtc;
1031         int ret = false;
1032
1033         mutex_lock(&dev->mode_config.mutex);
1034
1035         drm_crtc_probe_output_modes(dev, 2048, 2048);
1036
1037         drm_pick_crtcs(dev);
1038
1039         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1040
1041                 /* can't setup the crtc if there's no assigned mode */
1042                 if (!crtc->desired_mode)
1043                         continue;
1044
1045                 /* Now setup the fbdev for attached crtcs */
1046                 dev->driver->fb_probe(dev, crtc);
1047         }
1048
1049         /* This is a little screwy, as we've already walked the outputs 
1050          * above, but it's a little bit of magic too. There's the potential
1051          * for things not to get setup above if an existing device gets
1052          * re-assigned thus confusing the hardware. By walking the outputs
1053          * this fixes up their crtc's.
1054          */
1055         list_for_each_entry(output, &dev->mode_config.output_list, head) {
1056
1057                 /* can't setup the output if there's no assigned mode */
1058                 if (!output->crtc || !output->crtc->desired_mode)
1059                         continue;
1060
1061                 /* and needs an attached fb */
1062                 if (output->crtc->fb)
1063                         drm_crtc_set_mode(output->crtc, output->crtc->desired_mode, 0, 0);
1064         }
1065
1066         drm_disable_unused_functions(dev);
1067
1068         mutex_unlock(&dev->mode_config.mutex);
1069         return ret;
1070 }
1071 EXPORT_SYMBOL(drm_initial_config);
1072
1073 /**
1074  * drm_mode_config_cleanup - free up DRM mode_config info
1075  * @dev: DRM device
1076  *
1077  * LOCKING:
1078  * Caller must hold mode config lock.
1079  *
1080  * Free up all the outputs and CRTCs associated with this DRM device, then
1081  * free up the framebuffers and associated buffer objects.
1082  *
1083  * FIXME: cleanup any dangling user buffer objects too
1084  */
1085 void drm_mode_config_cleanup(struct drm_device *dev)
1086 {
1087         struct drm_output *output, *ot;
1088         struct drm_crtc *crtc, *ct;
1089         struct drm_framebuffer *fb, *fbt;
1090         struct drm_property *property, *pt;
1091
1092         list_for_each_entry_safe(output, ot, &dev->mode_config.output_list, head) {
1093                 drm_sysfs_output_remove(output);
1094                 drm_output_destroy(output);
1095         }
1096
1097         list_for_each_entry_safe(property, pt, &dev->mode_config.property_list, head) {
1098                 drm_property_destroy(dev, property);
1099         }
1100
1101         list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1102                 /* there should only be bo of kernel type left */
1103                 if (fb->bo->type != drm_bo_type_kernel)
1104                         drm_framebuffer_destroy(fb);
1105                 else
1106                         dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1107         }
1108
1109         list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1110                 drm_crtc_destroy(crtc);
1111         }
1112
1113 }
1114 EXPORT_SYMBOL(drm_mode_config_cleanup);
1115
1116 /**
1117  * drm_crtc_set_config - set a new config from userspace
1118  * @crtc: CRTC to setup
1119  * @crtc_info: user provided configuration
1120  * @new_mode: new mode to set
1121  * @output_set: set of outputs for the new config
1122  * @fb: new framebuffer
1123  *
1124  * LOCKING:
1125  * Caller must hold mode config lock.
1126  *
1127  * Setup a new configuration, provided by the user in @crtc_info, and enable
1128  * it.
1129  *
1130  * RETURNS:
1131  * Zero. (FIXME)
1132  */
1133 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)
1134 {
1135         struct drm_device *dev = crtc->dev;
1136         struct drm_crtc **save_crtcs, *new_crtc;
1137         bool save_enabled = crtc->enabled;
1138         bool changed = false;
1139         bool flip_or_move = false;
1140         struct drm_output *output;
1141         int count = 0, ro;
1142
1143         /* this is meant to be num_output not num_crtc */
1144         save_crtcs = kzalloc(dev->mode_config.num_output * sizeof(struct drm_crtc *), GFP_KERNEL);
1145         if (!save_crtcs)
1146                 return -ENOMEM;
1147
1148         /* We should be able to check here if the fb has the same properties
1149          * and then just flip_or_move it */
1150         if (crtc->fb != fb)
1151                 flip_or_move = true;
1152
1153         if (crtc_info->x != crtc->x || crtc_info->y != crtc->y)
1154                 flip_or_move = true;
1155
1156         if (new_mode && !drm_mode_equal(new_mode, &crtc->mode)) {
1157                 DRM_DEBUG("modes are different\n");
1158                 drm_mode_debug_printmodeline(dev, &crtc->mode);
1159                 drm_mode_debug_printmodeline(dev, new_mode);
1160                 changed = true;
1161         }
1162
1163         list_for_each_entry(output, &dev->mode_config.output_list, head) {
1164                 save_crtcs[count++] = output->crtc;
1165
1166                 if (output->crtc == crtc)
1167                         new_crtc = NULL;
1168                 else
1169                         new_crtc = output->crtc;
1170
1171                 for (ro = 0; ro < crtc_info->count_outputs; ro++) {
1172                         if (output_set[ro] == output)
1173                                 new_crtc = crtc;
1174                 }
1175                 if (new_crtc != output->crtc) {
1176                         changed = true;
1177                         output->crtc = new_crtc;
1178                 }
1179         }
1180
1181         /* mode_set_base is not a required function */
1182         if (flip_or_move && !crtc->funcs->mode_set_base)
1183                 changed = true;
1184
1185         if (changed) {
1186                 crtc->fb = fb;
1187                 crtc->enabled = (new_mode != NULL);
1188                 if (new_mode != NULL) {
1189                         DRM_DEBUG("attempting to set mode from userspace\n");
1190                         drm_mode_debug_printmodeline(dev, new_mode);
1191                         if (!drm_crtc_set_mode(crtc, new_mode, crtc_info->x,
1192                                                crtc_info->y)) {
1193                                 crtc->enabled = save_enabled;
1194                                 count = 0;
1195                                 list_for_each_entry(output, &dev->mode_config.output_list, head)
1196                                         output->crtc = save_crtcs[count++];
1197                                 kfree(save_crtcs);
1198                                 return -EINVAL;
1199                         }
1200                         crtc->desired_x = crtc_info->x;
1201                         crtc->desired_y = crtc_info->y;
1202                         crtc->desired_mode = new_mode;
1203                 }
1204                 drm_disable_unused_functions(dev);
1205         } else if (flip_or_move) {
1206                 if (crtc->fb != fb)
1207                         crtc->fb = fb;
1208                 crtc->funcs->mode_set_base(crtc, crtc_info->x, crtc_info->y);
1209         }
1210
1211         kfree(save_crtcs);
1212         return 0;
1213 }
1214
1215 /**
1216  * drm_hotplug_stage_two
1217  * @dev DRM device
1218  * @output hotpluged output
1219  *
1220  * LOCKING.
1221  * Caller must hold mode config lock, function might grab struct lock.
1222  *
1223  * Stage two of a hotplug.
1224  *
1225  * RETURNS:
1226  * Zero on success, errno on failure.
1227  */
1228 int drm_hotplug_stage_two(struct drm_device *dev, struct drm_output *output,
1229                           bool connected)
1230 {
1231         int has_config = 0;
1232
1233         dev->mode_config.hotplug_counter++;
1234
1235         /* We might want to do something more here */
1236         if (!connected) {
1237                 DRM_DEBUG("not connected\n");
1238                 return 0;
1239         }
1240
1241         if (output->crtc && output->crtc->desired_mode) {
1242                 DRM_DEBUG("drm thinks that the output already has a config\n");
1243                 has_config = 1;
1244         }
1245
1246         drm_crtc_probe_output_modes(dev, 2048, 2048);
1247
1248         if (!has_config)
1249                 drm_pick_crtcs(dev);
1250
1251         if (!output->crtc || !output->crtc->desired_mode) {
1252                 DRM_DEBUG("could not find a desired mode or crtc for output\n");
1253                 return 1;
1254         }
1255
1256         /* We should really check if there is a fb using this crtc */
1257         if (!has_config)
1258                 dev->driver->fb_probe(dev, output->crtc);
1259         else {
1260                 dev->driver->fb_resize(dev, output->crtc);
1261
1262                 if (!drm_crtc_set_mode(output->crtc, output->crtc->desired_mode, 0, 0))
1263                         DRM_ERROR("failed to set mode after hotplug\n");
1264         }
1265
1266         drm_sysfs_hotplug_event(dev);
1267
1268         drm_disable_unused_functions(dev);
1269
1270         return 0;
1271 }
1272 EXPORT_SYMBOL(drm_hotplug_stage_two);
1273
1274 int drm_mode_hotplug_ioctl(struct drm_device *dev,
1275                            void *data, struct drm_file *file_priv)
1276 {
1277         struct drm_mode_hotplug *arg = data;
1278
1279         arg->counter = dev->mode_config.hotplug_counter;
1280
1281         return 0;
1282 }
1283
1284 /**
1285  * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1286  * @out: drm_mode_modeinfo struct to return to the user
1287  * @in: drm_display_mode to use
1288  *
1289  * LOCKING:
1290  * None.
1291  *
1292  * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1293  * the user.
1294  */
1295 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out, struct drm_display_mode *in)
1296 {
1297         out->clock = in->clock;
1298         out->hdisplay = in->hdisplay;
1299         out->hsync_start = in->hsync_start;
1300         out->hsync_end = in->hsync_end;
1301         out->htotal = in->htotal;
1302         out->hskew = in->hskew;
1303         out->vdisplay = in->vdisplay;
1304         out->vsync_start = in->vsync_start;
1305         out->vsync_end = in->vsync_end;
1306         out->vtotal = in->vtotal;
1307         out->vscan = in->vscan;
1308         out->vrefresh = in->vrefresh;
1309         out->flags = in->flags;
1310         out->type = in->type;
1311         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1312         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1313 }
1314
1315 /**
1316  * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1317  * @out: drm_display_mode to return to the user
1318  * @in: drm_mode_modeinfo to use
1319  *
1320  * LOCKING:
1321  * None.
1322  *
1323  * Convert a drmo_mode_modeinfo into a drm_display_mode structure to return to
1324  * the caller.
1325  */
1326 void drm_crtc_convert_umode(struct drm_display_mode *out, struct drm_mode_modeinfo *in)
1327 {
1328         out->clock = in->clock;
1329         out->hdisplay = in->hdisplay;
1330         out->hsync_start = in->hsync_start;
1331         out->hsync_end = in->hsync_end;
1332         out->htotal = in->htotal;
1333         out->hskew = in->hskew;
1334         out->vdisplay = in->vdisplay;
1335         out->vsync_start = in->vsync_start;
1336         out->vsync_end = in->vsync_end;
1337         out->vtotal = in->vtotal;
1338         out->vscan = in->vscan;
1339         out->vrefresh = in->vrefresh;
1340         out->flags = in->flags;
1341         out->type = in->type;
1342         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1343         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1344 }
1345         
1346 /**
1347  * drm_mode_getresources - get graphics configuration
1348  * @inode: inode from the ioctl
1349  * @filp: file * from the ioctl
1350  * @cmd: cmd from ioctl
1351  * @arg: arg from ioctl
1352  *
1353  * LOCKING:
1354  * Takes mode config lock.
1355  *
1356  * Construct a set of configuration description structures and return
1357  * them to the user, including CRTC, output and framebuffer configuration.
1358  *
1359  * Called by the user via ioctl.
1360  *
1361  * RETURNS:
1362  * Zero on success, errno on failure.
1363  */
1364 int drm_mode_getresources(struct drm_device *dev,
1365                           void *data, struct drm_file *file_priv)
1366 {
1367         struct drm_mode_card_res *card_res = data;
1368         struct list_head *lh;
1369         struct drm_framebuffer *fb;
1370         struct drm_output *output;
1371         struct drm_crtc *crtc;
1372         int ret = 0;
1373         int output_count = 0;
1374         int crtc_count = 0;
1375         int fb_count = 0;
1376         int copied = 0;
1377         uint32_t __user *fb_id;
1378         uint32_t __user *crtc_id;
1379         uint32_t __user *output_id;
1380
1381         mutex_lock(&dev->mode_config.mutex);
1382
1383         list_for_each(lh, &dev->mode_config.fb_list)
1384                 fb_count++;
1385
1386         list_for_each(lh, &dev->mode_config.crtc_list)
1387                 crtc_count++;
1388
1389         list_for_each(lh, &dev->mode_config.output_list)
1390                 output_count++;
1391
1392         card_res->max_height = dev->mode_config.max_height;
1393         card_res->min_height = dev->mode_config.min_height;
1394         card_res->max_width = dev->mode_config.max_width;
1395         card_res->min_width = dev->mode_config.min_width;
1396
1397         /* handle this in 4 parts */
1398         /* FBs */
1399         if (card_res->count_fbs >= fb_count) {
1400                 copied = 0;
1401                 fb_id = (uint32_t *)(unsigned long)card_res->fb_id_ptr;
1402                 list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
1403                         if (put_user(fb->id, fb_id + copied)) {
1404                                 ret = -EFAULT;
1405                                 goto out;
1406                         }
1407                         copied++;
1408                 }
1409         }
1410         card_res->count_fbs = fb_count;
1411
1412         /* CRTCs */
1413         if (card_res->count_crtcs >= crtc_count) {
1414                 copied = 0;
1415                 crtc_id = (uint32_t *)(unsigned long)card_res->crtc_id_ptr;
1416                 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head){
1417                         DRM_DEBUG("CRTC ID is %d\n", crtc->id);
1418                         if (put_user(crtc->id, crtc_id + copied)) {
1419                                 ret = -EFAULT;
1420                                 goto out;
1421                         }
1422                         copied++;
1423                 }
1424         }
1425         card_res->count_crtcs = crtc_count;
1426
1427
1428         /* Outputs */
1429         if (card_res->count_outputs >= output_count) {
1430                 copied = 0;
1431                 output_id = (uint32_t *)(unsigned long)card_res->output_id_ptr;
1432                 list_for_each_entry(output, &dev->mode_config.output_list,
1433                                     head) {
1434                         DRM_DEBUG("OUTPUT ID is %d\n", output->id);
1435                         if (put_user(output->id, output_id + copied)) {
1436                                 ret = -EFAULT;
1437                                 goto out;
1438                         }
1439                         copied++;
1440                 }
1441         }
1442         card_res->count_outputs = output_count;
1443         
1444         DRM_DEBUG("Counted %d %d\n", card_res->count_crtcs,
1445                   card_res->count_outputs);
1446
1447 out:    
1448         mutex_unlock(&dev->mode_config.mutex);
1449         return ret;
1450 }
1451
1452 /**
1453  * drm_mode_getcrtc - get CRTC configuration
1454  * @inode: inode from the ioctl
1455  * @filp: file * from the ioctl
1456  * @cmd: cmd from ioctl
1457  * @arg: arg from ioctl
1458  *
1459  * LOCKING:
1460  * Caller? (FIXME)
1461  *
1462  * Construct a CRTC configuration structure to return to the user.
1463  *
1464  * Called by the user via ioctl.
1465  *
1466  * RETURNS:
1467  * Zero on success, errno on failure.
1468  */
1469 int drm_mode_getcrtc(struct drm_device *dev,
1470                      void *data, struct drm_file *file_priv)
1471 {
1472         struct drm_mode_crtc *crtc_resp = data;
1473         struct drm_crtc *crtc;
1474         struct drm_output *output;
1475         int ocount;
1476         int ret = 0;
1477
1478         mutex_lock(&dev->mode_config.mutex);
1479         crtc = idr_find(&dev->mode_config.crtc_idr, crtc_resp->crtc_id);
1480         if (!crtc || (crtc->id != crtc_resp->crtc_id)) {
1481                 ret = -EINVAL;
1482                 goto out;
1483         }
1484
1485         crtc_resp->x = crtc->x;
1486         crtc_resp->y = crtc->y;
1487
1488         if (crtc->fb)
1489                 crtc_resp->fb_id = crtc->fb->id;
1490         else
1491                 crtc_resp->fb_id = 0;
1492
1493         crtc_resp->outputs = 0;
1494         if (crtc->enabled) {
1495
1496                 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1497                 crtc_resp->mode_valid = 1;
1498                 ocount = 0;
1499                 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1500                         if (output->crtc == crtc)
1501                                 crtc_resp->outputs |= 1 << (ocount++);
1502                 }
1503                 
1504         } else {
1505                 crtc_resp->mode_valid = 0;
1506         }
1507
1508 out:
1509         mutex_unlock(&dev->mode_config.mutex);
1510         return ret;
1511 }
1512
1513 /**
1514  * drm_mode_getoutput - get output configuration
1515  * @inode: inode from the ioctl
1516  * @filp: file * from the ioctl
1517  * @cmd: cmd from ioctl
1518  * @arg: arg from ioctl
1519  *
1520  * LOCKING:
1521  * Caller? (FIXME)
1522  *
1523  * Construct a output configuration structure to return to the user.
1524  *
1525  * Called by the user via ioctl.
1526  *
1527  * RETURNS:
1528  * Zero on success, errno on failure.
1529  */
1530 int drm_mode_getoutput(struct drm_device *dev,
1531                        void *data, struct drm_file *file_priv)
1532 {
1533         struct drm_mode_get_output *out_resp = data;
1534         struct drm_output *output;
1535         struct drm_display_mode *mode;
1536         int mode_count = 0;
1537         int props_count = 0;
1538         int ret = 0;
1539         int copied = 0;
1540         int i;
1541         struct drm_mode_modeinfo u_mode;
1542         struct drm_mode_modeinfo __user *mode_ptr;
1543         uint32_t __user *prop_ptr;
1544         uint64_t __user *prop_values;
1545
1546         memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1547
1548         DRM_DEBUG("output id %d:\n", out_resp->output);
1549
1550         mutex_lock(&dev->mode_config.mutex);
1551         output= idr_find(&dev->mode_config.crtc_idr, out_resp->output);
1552         if (!output || (output->id != out_resp->output)) {
1553                 ret = -EINVAL;
1554                 goto out;
1555         }
1556
1557         list_for_each_entry(mode, &output->modes, head)
1558                 mode_count++;
1559         
1560         for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1561                 if (output->property_ids[i] != 0) {
1562                         props_count++;
1563                 }
1564         }
1565
1566         if (out_resp->count_modes == 0) {
1567                 drm_crtc_probe_single_output_modes(output, dev->mode_config.max_width, dev->mode_config.max_height);
1568         }
1569
1570         out_resp->output_type = output->output_type;
1571         out_resp->output_type_id = output->output_type_id;
1572         out_resp->mm_width = output->display_info.width_mm;
1573         out_resp->mm_height = output->display_info.height_mm;
1574         out_resp->subpixel = output->display_info.subpixel_order;
1575         out_resp->connection = output->status;
1576         if (output->crtc)
1577                 out_resp->crtc = output->crtc->id;
1578         else
1579                 out_resp->crtc = 0;
1580
1581         out_resp->crtcs = output->possible_crtcs;
1582         out_resp->clones = output->possible_clones;
1583
1584         if ((out_resp->count_modes >= mode_count) && mode_count) {
1585                 copied = 0;
1586                 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1587                 list_for_each_entry(mode, &output->modes, head) {
1588                         drm_crtc_convert_to_umode(&u_mode, mode);
1589                         if (copy_to_user(mode_ptr + copied,
1590                                          &u_mode, sizeof(u_mode))) {
1591                                 ret = -EFAULT;
1592                                 goto out;
1593                         }
1594                         copied++;
1595                         
1596                 }
1597         }
1598         out_resp->count_modes = mode_count;
1599
1600         if ((out_resp->count_props >= props_count) && props_count) {
1601                 copied = 0;
1602                 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1603                 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1604                 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1605                         if (output->property_ids[i] != 0) {
1606                                 if (put_user(output->property_ids[i], prop_ptr + copied)) {
1607                                         ret = -EFAULT;
1608                                         goto out;
1609                                 }
1610
1611                                 if (put_user(output->property_values[i], prop_values + copied)) {
1612                                         ret = -EFAULT;
1613                                         goto out;
1614                                 }
1615                                 copied++;
1616                         }
1617                 }
1618         }
1619         out_resp->count_props = props_count;
1620
1621 out:
1622         mutex_unlock(&dev->mode_config.mutex);
1623         return ret;
1624 }
1625
1626 /**
1627  * drm_mode_setcrtc - set CRTC configuration
1628  * @inode: inode from the ioctl
1629  * @filp: file * from the ioctl
1630  * @cmd: cmd from ioctl
1631  * @arg: arg from ioctl
1632  *
1633  * LOCKING:
1634  * Caller? (FIXME)
1635  *
1636  * Build a new CRTC configuration based on user request.
1637  *
1638  * Called by the user via ioctl.
1639  *
1640  * RETURNS:
1641  * Zero on success, errno on failure.
1642  */
1643 int drm_mode_setcrtc(struct drm_device *dev,
1644                      void *data, struct drm_file *file_priv)
1645 {
1646         struct drm_mode_crtc *crtc_req = data;
1647         struct drm_crtc *crtc, *crtcfb;
1648         struct drm_output **output_set = NULL, *output;
1649         struct drm_framebuffer *fb = NULL;
1650         struct drm_display_mode *mode = NULL;
1651         uint32_t __user *set_outputs_ptr;
1652         int ret = 0;
1653         int i;
1654
1655         mutex_lock(&dev->mode_config.mutex);
1656         crtc = idr_find(&dev->mode_config.crtc_idr, crtc_req->crtc_id);
1657         if (!crtc || (crtc->id != crtc_req->crtc_id)) {
1658                 DRM_DEBUG("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1659                 ret = -EINVAL;
1660                 goto out;
1661         }
1662
1663         if (crtc_req->mode_valid) {
1664                 /* If we have a mode we need a framebuffer. */
1665                 /* If we pass -1, set the mode with the currently bound fb */
1666                 if (crtc_req->fb_id == -1) {
1667                         list_for_each_entry(crtcfb, &dev->mode_config.crtc_list, head) {
1668                                 if (crtcfb == crtc) {
1669                                         DRM_DEBUG("Using current fb for setmode\n");
1670                                         fb = crtc->fb;          
1671                                 }
1672                         }
1673                 } else {
1674                         fb = idr_find(&dev->mode_config.crtc_idr, crtc_req->fb_id);
1675                         if (!fb || (fb->id != crtc_req->fb_id)) {
1676                                 DRM_DEBUG("Unknown FB ID%d\n", crtc_req->fb_id);
1677                                 ret = -EINVAL;
1678                                 goto out;
1679                         }
1680                 }
1681
1682                 mode = drm_mode_create(dev);
1683                 drm_crtc_convert_umode(mode, &crtc_req->mode);
1684                 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1685         }
1686
1687         if (crtc_req->count_outputs == 0 && mode) {
1688                 DRM_DEBUG("Count outputs is 0 but mode set\n");
1689                 ret = -EINVAL;
1690                 goto out;
1691         }
1692
1693         if (crtc_req->count_outputs > 0 && !mode && !fb) {
1694                 DRM_DEBUG("Count outputs is %d but no mode or fb set\n", crtc_req->count_outputs);
1695                 ret = -EINVAL;
1696                 goto out;
1697         }
1698
1699         if (crtc_req->count_outputs > 0) {
1700                 u32 out_id;
1701                 /* Maybe we should check that count_outputs is a sensible value. */
1702                 output_set = kmalloc(crtc_req->count_outputs *
1703                                      sizeof(struct drm_output *), GFP_KERNEL);
1704                 if (!output_set) {
1705                         ret = -ENOMEM;
1706                         goto out;
1707                 }
1708
1709                 for (i = 0; i < crtc_req->count_outputs; i++) {
1710                         set_outputs_ptr = (uint32_t *)(unsigned long)crtc_req->set_outputs_ptr;
1711                         if (get_user(out_id, &set_outputs_ptr[i])) {
1712                                 ret = -EFAULT;
1713                                 goto out;
1714                         }
1715
1716                         output = idr_find(&dev->mode_config.crtc_idr, out_id);
1717                         if (!output || (out_id != output->id)) {
1718                                 DRM_DEBUG("Output id %d unknown\n", out_id);
1719                                 ret = -EINVAL;
1720                                 goto out;
1721                         }
1722
1723                         output_set[i] = output;
1724                 }
1725         }
1726
1727         ret = drm_crtc_set_config(crtc, crtc_req, mode, output_set, fb);
1728
1729 out:
1730         kfree(output_set);
1731         mutex_unlock(&dev->mode_config.mutex);
1732         return ret;
1733 }
1734
1735 int drm_mode_cursor_ioctl(struct drm_device *dev,
1736                         void *data, struct drm_file *file_priv)
1737 {
1738         struct drm_mode_cursor *req = data;
1739         struct drm_crtc *crtc;
1740         struct drm_buffer_object *bo = NULL; /* must be set */
1741         int ret = 0;
1742
1743         DRM_DEBUG("\n");
1744
1745         if (!req->flags) {
1746                 DRM_ERROR("no operation set\n");
1747                 return -EINVAL;
1748         }
1749
1750         mutex_lock(&dev->mode_config.mutex);
1751         crtc = idr_find(&dev->mode_config.crtc_idr, req->crtc);
1752         if (!crtc || (crtc->id != req->crtc)) {
1753                 DRM_DEBUG("Unknown CRTC ID %d\n", req->crtc);
1754                 ret = -EINVAL;
1755                 goto out;
1756         }
1757
1758         if (req->flags & DRM_MODE_CURSOR_BO) {
1759                 /* Turn of the cursor if handle is 0 */
1760                 if (req->handle)
1761                         ret = drm_get_buffer_object(dev, &bo, req->handle);
1762
1763                 if (ret) {
1764                         DRM_ERROR("invalid buffer id\n");
1765                         ret = -EINVAL;
1766                         goto out;
1767                 }
1768
1769                 if (crtc->funcs->cursor_set) {
1770                         ret = crtc->funcs->cursor_set(crtc, bo, req->width, req->height);
1771                 } else {
1772                         DRM_ERROR("crtc does not support cursor\n");
1773                         ret = -EFAULT;
1774                         goto out;
1775                 }
1776         }
1777
1778         if (req->flags & DRM_MODE_CURSOR_MOVE) {
1779                 if (crtc->funcs->cursor_move) {
1780                         ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1781                 } else {
1782                         DRM_ERROR("crtc does not support cursor\n");
1783                         ret = -EFAULT;
1784                         goto out;
1785                 }
1786         }
1787 out:
1788         mutex_unlock(&dev->mode_config.mutex);
1789         return ret;
1790 }
1791
1792 /**
1793  * drm_mode_addfb - add an FB to the graphics configuration
1794  * @inode: inode from the ioctl
1795  * @filp: file * from the ioctl
1796  * @cmd: cmd from ioctl
1797  * @arg: arg from ioctl
1798  *
1799  * LOCKING:
1800  * Takes mode config lock.
1801  *
1802  * Add a new FB to the specified CRTC, given a user request.
1803  *
1804  * Called by the user via ioctl.
1805  *
1806  * RETURNS:
1807  * Zero on success, errno on failure.
1808  */
1809 int drm_mode_addfb(struct drm_device *dev,
1810                    void *data, struct drm_file *file_priv)
1811 {
1812         struct drm_mode_fb_cmd *r = data;
1813         struct drm_mode_config *config = &dev->mode_config;
1814         struct drm_framebuffer *fb;
1815         struct drm_buffer_object *bo;
1816         int ret = 0;
1817
1818         if ((config->min_width > r->width) || (r->width > config->max_width)) {
1819                 DRM_ERROR("mode new framebuffer width not within limits\n");
1820                 return -EINVAL;
1821         }
1822         if ((config->min_height > r->height) || (r->height > config->max_height)) {
1823                 DRM_ERROR("mode new framebuffer height not within limits\n");
1824                 return -EINVAL;
1825         }
1826
1827         mutex_lock(&dev->mode_config.mutex);
1828         /* TODO check limits are okay */
1829         ret = drm_get_buffer_object(dev, &bo, r->handle);
1830         if (ret || !bo) {
1831                 DRM_ERROR("BO handle not valid\n");
1832                 ret = -EINVAL;
1833                 goto out;
1834         }
1835
1836         /* TODO check buffer is sufficently large */
1837         /* TODO setup destructor callback */
1838
1839         fb = drm_framebuffer_create(dev);
1840         if (!fb) {
1841                 DRM_ERROR("could not create framebuffer\n");
1842                 ret = -EINVAL;
1843                 goto out;
1844         }
1845
1846         fb->width = r->width;
1847         fb->height = r->height;
1848         fb->pitch = r->pitch;
1849         fb->bits_per_pixel = r->bpp;
1850         fb->depth = r->depth;
1851         fb->bo = bo;
1852
1853         r->buffer_id = fb->id;
1854
1855         list_add(&fb->filp_head, &file_priv->fbs);
1856
1857 out:
1858         mutex_unlock(&dev->mode_config.mutex);
1859         return ret;
1860 }
1861
1862 /**
1863  * drm_mode_rmfb - remove an FB from the configuration
1864  * @inode: inode from the ioctl
1865  * @filp: file * from the ioctl
1866  * @cmd: cmd from ioctl
1867  * @arg: arg from ioctl
1868  *
1869  * LOCKING:
1870  * Takes mode config lock.
1871  *
1872  * Remove the FB specified by the user.
1873  *
1874  * Called by the user via ioctl.
1875  *
1876  * RETURNS:
1877  * Zero on success, errno on failure.
1878  */
1879 int drm_mode_rmfb(struct drm_device *dev,
1880                    void *data, struct drm_file *file_priv)
1881 {
1882         struct drm_framebuffer *fb = 0;
1883         struct drm_framebuffer *fbl = 0;
1884         uint32_t *id = data;
1885         int ret = 0;
1886         int found = 0;
1887
1888         mutex_lock(&dev->mode_config.mutex);
1889         fb = idr_find(&dev->mode_config.crtc_idr, *id);
1890         /* TODO check that we realy get a framebuffer back. */
1891         if (!fb || (*id != fb->id)) {
1892                 DRM_ERROR("mode invalid framebuffer id\n");
1893                 ret = -EINVAL;
1894                 goto out;
1895         }
1896
1897         list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1898                 if (fb == fbl)
1899                         found = 1;
1900
1901         if (!found) {
1902                 DRM_ERROR("tried to remove a fb that we didn't own\n");
1903                 ret = -EINVAL;
1904                 goto out;
1905         }
1906
1907         /* TODO release all crtc connected to the framebuffer */
1908         /* TODO unhock the destructor from the buffer object */
1909
1910         if (fb->bo->type == drm_bo_type_kernel)
1911                 DRM_ERROR("the bo type should not be of kernel type\n");
1912
1913         list_del(&fb->filp_head);
1914         drm_framebuffer_destroy(fb);
1915
1916 out:
1917         mutex_unlock(&dev->mode_config.mutex);
1918         return ret;
1919 }
1920
1921 /**
1922  * drm_mode_getfb - get FB info
1923  * @inode: inode from the ioctl
1924  * @filp: file * from the ioctl
1925  * @cmd: cmd from ioctl
1926  * @arg: arg from ioctl
1927  *
1928  * LOCKING:
1929  * Caller? (FIXME)
1930  *
1931  * Lookup the FB given its ID and return info about it.
1932  *
1933  * Called by the user via ioctl.
1934  *
1935  * RETURNS:
1936  * Zero on success, errno on failure.
1937  */
1938 int drm_mode_getfb(struct drm_device *dev,
1939                    void *data, struct drm_file *file_priv)
1940 {
1941         struct drm_mode_fb_cmd *r = data;
1942         struct drm_framebuffer *fb;
1943         int ret = 0;
1944
1945         mutex_lock(&dev->mode_config.mutex);
1946         fb = idr_find(&dev->mode_config.crtc_idr, r->buffer_id);
1947         if (!fb || (r->buffer_id != fb->id)) {
1948                 DRM_ERROR("invalid framebuffer id\n");
1949                 ret = -EINVAL;
1950                 goto out;
1951         }
1952
1953         r->height = fb->height;
1954         r->width = fb->width;
1955         r->depth = fb->depth;
1956         r->bpp = fb->bits_per_pixel;
1957         r->handle = fb->bo->base.hash.key;
1958         r->pitch = fb->pitch;
1959
1960 out:
1961         mutex_unlock(&dev->mode_config.mutex);
1962         return ret;
1963 }
1964
1965 /**
1966  * drm_fb_release - remove and free the FBs on this file
1967  * @filp: file * from the ioctl
1968  *
1969  * LOCKING:
1970  * Takes mode config lock.
1971  *
1972  * Destroy all the FBs associated with @filp.
1973  *
1974  * Called by the user via ioctl.
1975  *
1976  * RETURNS:
1977  * Zero on success, errno on failure.
1978  */
1979 void drm_fb_release(struct file *filp)
1980 {
1981         struct drm_file *priv = filp->private_data;
1982         struct drm_device *dev = priv->minor->dev;
1983         struct drm_framebuffer *fb, *tfb;
1984
1985         mutex_lock(&dev->mode_config.mutex);
1986         list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1987                 list_del(&fb->filp_head);
1988                 if (fb->bo->type == drm_bo_type_kernel)
1989                         DRM_ERROR("the bo type should not be of kernel_type, the kernel will probably explode, why Dave\n");
1990
1991                 drm_framebuffer_destroy(fb);
1992         }
1993         mutex_unlock(&dev->mode_config.mutex);
1994 }
1995
1996 /*
1997  *
1998  */
1999
2000 static int drm_mode_attachmode(struct drm_device *dev,
2001                                struct drm_output *output,
2002                                struct drm_display_mode *mode)
2003 {
2004         int ret = 0;
2005
2006         list_add_tail(&mode->head, &output->user_modes);
2007         return ret;
2008 }
2009
2010 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
2011                              struct drm_display_mode *mode)
2012 {
2013         struct drm_output *output;
2014         int ret = 0;
2015         struct drm_display_mode *dup_mode;
2016         int need_dup = 0;
2017         list_for_each_entry(output, &dev->mode_config.output_list, head) {
2018                 if (output->crtc == crtc) {
2019                         if (need_dup)
2020                                 dup_mode = drm_mode_duplicate(dev, mode);
2021                         else
2022                                 dup_mode = mode;
2023                         ret = drm_mode_attachmode(dev, output, dup_mode); 
2024                         if (ret)
2025                                 return ret;
2026                         need_dup = 1;
2027                 }
2028         }
2029         return 0;
2030 }
2031 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2032
2033 static int drm_mode_detachmode(struct drm_device *dev,
2034                                struct drm_output *output,
2035                                struct drm_display_mode *mode)
2036 {
2037         int found = 0;
2038         int ret = 0;
2039         struct drm_display_mode *match_mode, *t;
2040
2041         list_for_each_entry_safe(match_mode, t, &output->user_modes, head) {
2042                 if (drm_mode_equal(match_mode, mode)) {
2043                         list_del(&match_mode->head);
2044                         drm_mode_destroy(dev, match_mode);
2045                         found = 1;
2046                         break;
2047                 }
2048         }
2049
2050         if (!found)
2051                 ret = -EINVAL;
2052
2053         return ret;
2054 }
2055
2056 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2057 {
2058         struct drm_output *output;
2059
2060         list_for_each_entry(output, &dev->mode_config.output_list, head) {
2061                 drm_mode_detachmode(dev, output, mode);
2062         }
2063         return 0;
2064 }
2065 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2066
2067 /**
2068  * drm_fb_attachmode - Attach a user mode to an output
2069  * @inode: inode from the ioctl
2070  * @filp: file * from the ioctl
2071  * @cmd: cmd from ioctl
2072  * @arg: arg from ioctl
2073  *
2074  * This attaches a user specified mode to an output.
2075  * Called by the user via ioctl.
2076  *
2077  * RETURNS:
2078  * Zero on success, errno on failure.
2079  */
2080 int drm_mode_attachmode_ioctl(struct drm_device *dev,
2081                               void *data, struct drm_file *file_priv)
2082 {
2083         struct drm_mode_mode_cmd *mode_cmd = data;
2084         struct drm_output *output;
2085         struct drm_display_mode *mode;
2086         struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2087         int ret = 0;
2088
2089         mutex_lock(&dev->mode_config.mutex);
2090
2091         output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
2092         if (!output || (output->id != mode_cmd->output_id)) {
2093                 ret = -EINVAL;
2094                 goto out;
2095         }
2096
2097         mode = drm_mode_create(dev);
2098         if (!mode) {
2099                 ret = -ENOMEM;
2100                 goto out;
2101         }
2102         
2103         drm_crtc_convert_umode(mode, umode);
2104
2105         ret = drm_mode_attachmode(dev, output, mode);
2106 out:
2107         mutex_unlock(&dev->mode_config.mutex);
2108         return ret;
2109 }
2110
2111
2112 /**
2113  * drm_fb_detachmode - Detach a user specified mode from an output
2114  * @inode: inode from the ioctl
2115  * @filp: file * from the ioctl
2116  * @cmd: cmd from ioctl
2117  * @arg: arg from ioctl
2118  *
2119  * Called by the user via ioctl.
2120  *
2121  * RETURNS:
2122  * Zero on success, errno on failure.
2123  */
2124 int drm_mode_detachmode_ioctl(struct drm_device *dev,
2125                               void *data, struct drm_file *file_priv)
2126 {
2127         struct drm_mode_mode_cmd *mode_cmd = data;
2128         struct drm_output *output;
2129         struct drm_display_mode mode;
2130         struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2131         int ret = 0;
2132
2133         mutex_lock(&dev->mode_config.mutex);
2134
2135         output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
2136         if (!output || (output->id != mode_cmd->output_id)) {
2137                 ret = -EINVAL;
2138                 goto out;
2139         }
2140         
2141         drm_crtc_convert_umode(&mode, umode);
2142         ret = drm_mode_detachmode(dev, output, &mode);
2143 out:           
2144         mutex_unlock(&dev->mode_config.mutex);
2145         return ret;
2146 }
2147
2148 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2149                                          const char *name, int num_values)
2150 {
2151         struct drm_property *property = NULL;
2152
2153         property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2154         if (!property)
2155                 return NULL;
2156
2157         if (num_values) {
2158                 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2159                 if (!property->values)
2160                         goto fail;
2161         }
2162
2163         property->id = drm_idr_get(dev, property);
2164         property->flags = flags;
2165         property->num_values = num_values;
2166         INIT_LIST_HEAD(&property->enum_blob_list);
2167
2168         if (name)
2169                 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2170
2171         list_add_tail(&property->head, &dev->mode_config.property_list);
2172         return property;
2173 fail:
2174         kfree(property);
2175         return NULL;
2176 }
2177 EXPORT_SYMBOL(drm_property_create);
2178
2179 int drm_property_add_enum(struct drm_property *property, int index,
2180                           uint64_t value, const char *name)
2181 {
2182         struct drm_property_enum *prop_enum;
2183
2184         if (!(property->flags & DRM_MODE_PROP_ENUM))
2185                 return -EINVAL;
2186
2187         if (!list_empty(&property->enum_blob_list)) {
2188                 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2189                         if (prop_enum->value == value) {
2190                                 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN); 
2191                                 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2192                                 return 0;
2193                         }
2194                 }
2195         }
2196
2197         prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2198         if (!prop_enum)
2199                 return -ENOMEM;
2200
2201         strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN); 
2202         prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2203         prop_enum->value = value;
2204
2205         property->values[index] = value;
2206         list_add_tail(&prop_enum->head, &property->enum_blob_list);
2207         return 0;
2208 }
2209 EXPORT_SYMBOL(drm_property_add_enum);
2210
2211 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2212 {
2213         struct drm_property_enum *prop_enum, *pt;
2214
2215         list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2216                 list_del(&prop_enum->head);
2217                 kfree(prop_enum);
2218         }
2219
2220         if (property->num_values)
2221                 kfree(property->values);
2222         drm_idr_put(dev, property->id);
2223         list_del(&property->head);
2224         kfree(property);        
2225 }
2226 EXPORT_SYMBOL(drm_property_destroy);
2227
2228 int drm_output_attach_property(struct drm_output *output,
2229                                struct drm_property *property, uint64_t init_val)
2230 {
2231         int i;
2232
2233         for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2234                 if (output->property_ids[i] == 0) {
2235                         output->property_ids[i] = property->id;
2236                         output->property_values[i] = init_val;
2237                         break;
2238                 }
2239         }
2240
2241         if (i == DRM_OUTPUT_MAX_PROPERTY)
2242                 return -EINVAL;
2243         return 0;
2244 }
2245 EXPORT_SYMBOL(drm_output_attach_property);
2246
2247 int drm_output_property_set_value(struct drm_output *output,
2248                                   struct drm_property *property, uint64_t value)
2249 {
2250         int i;
2251
2252         for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2253                 if (output->property_ids[i] == property->id) {
2254                         output->property_values[i] = value;
2255                         break;
2256                 }
2257         }
2258
2259         if (i == DRM_OUTPUT_MAX_PROPERTY)
2260                 return -EINVAL;
2261         return 0;
2262 }
2263 EXPORT_SYMBOL(drm_output_property_set_value);
2264
2265 int drm_output_property_get_value(struct drm_output *output,
2266                                   struct drm_property *property, uint64_t *val)
2267 {
2268         int i;
2269
2270         for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2271                 if (output->property_ids[i] == property->id) {
2272                         *val = output->property_values[i];
2273                         break;
2274                 }
2275         }
2276
2277         if (i == DRM_OUTPUT_MAX_PROPERTY)
2278                 return -EINVAL;
2279         return 0;
2280 }
2281 EXPORT_SYMBOL(drm_output_property_get_value);
2282
2283 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2284                                void *data, struct drm_file *file_priv)
2285 {
2286         struct drm_mode_get_property *out_resp = data;
2287         struct drm_property *property;
2288         int enum_count = 0;
2289         int blob_count = 0;
2290         int value_count = 0;
2291         int ret = 0, i;
2292         int copied;
2293         struct drm_property_enum *prop_enum;
2294         struct drm_mode_property_enum __user *enum_ptr;
2295         struct drm_property_blob *prop_blob;
2296         uint32_t *blob_id_ptr;
2297         uint64_t __user *values_ptr;
2298         uint32_t __user *blob_length_ptr;
2299
2300         mutex_lock(&dev->mode_config.mutex);
2301         property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
2302         if (!property || (property->id != out_resp->prop_id)) {
2303                 ret = -EINVAL;
2304                 goto done;
2305         }
2306
2307         if (property->flags & DRM_MODE_PROP_ENUM) {
2308                 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2309                         enum_count++;
2310         } else if (property->flags & DRM_MODE_PROP_BLOB) {
2311                 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2312                         blob_count++;
2313         }
2314
2315         value_count = property->num_values;
2316
2317         strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2318         out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2319         out_resp->flags = property->flags;
2320
2321         if ((out_resp->count_values >= value_count) && value_count) {
2322                 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2323                 for (i = 0; i < value_count; i++) {
2324                         if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2325                                 ret = -EFAULT;
2326                                 goto done;
2327                         }
2328                 }
2329         }
2330         out_resp->count_values = value_count;
2331
2332         if (property->flags & DRM_MODE_PROP_ENUM) {
2333
2334                 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2335                         copied = 0;
2336                         enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2337                         list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2338                                 
2339                                 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2340                                         ret = -EFAULT;
2341                                         goto done;
2342                                 }
2343                                 
2344                                 if (copy_to_user(&enum_ptr[copied].name,
2345                                                  &prop_enum->name, DRM_PROP_NAME_LEN)) {
2346                                         ret = -EFAULT;
2347                                         goto done;
2348                                 }
2349                                 copied++;
2350                         }
2351                 }
2352                 out_resp->count_enum_blobs = enum_count;
2353         }
2354
2355         if (property->flags & DRM_MODE_PROP_BLOB) {
2356                 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2357                         copied = 0;
2358                         blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2359                         blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2360                         
2361                         list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2362                                 if (put_user(prop_blob->id, blob_id_ptr + copied)) {
2363                                         ret = -EFAULT;
2364                                         goto done;
2365                                 }
2366                                 
2367                                 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2368                                         ret = -EFAULT;
2369                                         goto done;
2370                                 }
2371                                 
2372                                 copied++;
2373                         }
2374                 }
2375                 out_resp->count_enum_blobs = enum_count;
2376         }
2377 done:
2378         mutex_unlock(&dev->mode_config.mutex);
2379         return ret;
2380 }
2381
2382 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2383                                                           void *data)
2384 {
2385         struct drm_property_blob *blob;
2386
2387         if (!length || !data)
2388                 return NULL;
2389
2390         blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2391         if (!blob)
2392                 return NULL;
2393
2394         blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2395         blob->length = length;
2396
2397         memcpy(blob->data, data, length);
2398
2399         blob->id = drm_idr_get(dev, blob);
2400         
2401         list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2402         return blob;
2403 }
2404
2405 static void drm_property_destroy_blob(struct drm_device *dev,
2406                                struct drm_property_blob *blob)
2407 {
2408         drm_idr_put(dev, blob->id);
2409         list_del(&blob->head);
2410         kfree(blob);
2411 }
2412
2413 int drm_mode_getblob_ioctl(struct drm_device *dev,
2414                            void *data, struct drm_file *file_priv)
2415 {
2416         struct drm_mode_get_blob *out_resp = data;
2417         struct drm_property_blob *blob;
2418         int ret = 0;
2419         void *blob_ptr;
2420
2421         mutex_lock(&dev->mode_config.mutex);
2422         
2423         blob = idr_find(&dev->mode_config.crtc_idr, out_resp->blob_id);
2424         if (!blob || (blob->id != out_resp->blob_id)) {
2425                 ret = -EINVAL;
2426                 goto done;
2427         }
2428
2429         if (out_resp->length == blob->length) {
2430                 blob_ptr = (void *)(unsigned long)out_resp->data;
2431                 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2432                         ret = -EFAULT;
2433                         goto done;
2434                 }
2435         }
2436         out_resp->length = blob->length;
2437
2438 done:
2439         mutex_unlock(&dev->mode_config.mutex);
2440         return ret;
2441 }
2442
2443 int drm_mode_output_update_edid_property(struct drm_output *output, struct edid *edid)
2444 {
2445         struct drm_device *dev = output->dev;
2446         int ret = 0;
2447         if (output->edid_blob_ptr)
2448                 drm_property_destroy_blob(dev, output->edid_blob_ptr);
2449
2450         output->edid_blob_ptr = drm_property_create_blob(output->dev, 128, edid);
2451         
2452         ret = drm_output_property_set_value(output, dev->mode_config.edid_property, output->edid_blob_ptr->id);
2453         return ret;
2454 }
2455 EXPORT_SYMBOL(drm_mode_output_update_edid_property);
2456
2457 int drm_mode_output_property_set_ioctl(struct drm_device *dev,
2458                                        void *data, struct drm_file *file_priv)
2459 {
2460         struct drm_mode_output_set_property *out_resp = data;
2461         struct drm_property *property;
2462         struct drm_output *output;
2463         int ret = -EINVAL;
2464         int i;
2465
2466         mutex_lock(&dev->mode_config.mutex);
2467         output = idr_find(&dev->mode_config.crtc_idr, out_resp->output_id);
2468         if (!output || (output->id != out_resp->output_id)) {
2469                 goto out;
2470         }
2471
2472         for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2473                 if (output->property_ids[i] == out_resp->prop_id)
2474                         break;
2475         }
2476
2477         if (i == DRM_OUTPUT_MAX_PROPERTY) {
2478                 goto out;
2479         }
2480         
2481         property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
2482         if (!property || (property->id != out_resp->prop_id)) {
2483                 goto out;
2484         }
2485
2486         if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2487                 goto out;
2488
2489         if (property->flags & DRM_MODE_PROP_RANGE) {
2490                 if (out_resp->value < property->values[0])
2491                         goto out;
2492
2493                 if (out_resp->value > property->values[1])
2494                         goto out;
2495         } else {
2496                 int found = 0;
2497                 for (i = 0; i < property->num_values; i++) {
2498                         if (property->values[i] == out_resp->value) {
2499                                 found = 1;
2500                                 break;
2501                         }
2502                 }
2503                 if (!found) {
2504                         goto out;
2505                 }
2506         }
2507
2508         if (output->funcs->set_property)
2509                 ret = output->funcs->set_property(output, property, out_resp->value);
2510
2511 out:
2512         mutex_unlock(&dev->mode_config.mutex);
2513         return ret;
2514 }
2515