2 * Copyright (c) 2006-2007 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
5 * DRM core CRTC related functions
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.
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
27 * Eric Anholt <eric@anholt.net>
28 * Dave Airlie <airlied@linux.ie>
29 * Jesse Barnes <jesse.barnes@intel.com>
31 #include <linux/list.h>
37 * drm_idr_get - allocate a new identifier
39 * @ptr: object pointer, used to generate unique ID
42 * Caller must hold DRM mode_config lock.
44 * Create a unique identifier based on @ptr in @dev's identifier space. Used
45 * for tracking modes, CRTCs and outputs.
48 * New unique (relative to other objects in @dev) integer identifier for the
51 int drm_idr_get(struct drm_device *dev, void *ptr)
56 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
57 DRM_ERROR("Ran out memory getting a mode number\n");
61 ret = idr_get_new_above(&dev->mode_config.crtc_idr, ptr, 1, &new_id);
63 spin_unlock(&dev->mode_config.config_lock);
71 * drm_idr_put - free an identifer
76 * Caller must hold DRM mode_config lock.
78 * Free @id from @dev's unique identifier pool.
80 void drm_idr_put(struct drm_device *dev, int id)
82 idr_remove(&dev->mode_config.crtc_idr, id);
86 * drm_crtc_from_fb - find the CRTC structure associated with an fb
88 * @fb: framebuffer in question
91 * Caller must hold mode_config lock.
93 * Find CRTC in the mode_config structure that matches @fb.
96 * Pointer to the CRTC or NULL if it wasn't found.
98 struct drm_crtc *drm_crtc_from_fb(struct drm_device *dev,
99 struct drm_framebuffer *fb)
101 struct drm_crtc *crtc;
103 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
111 * drm_framebuffer_create - create a new framebuffer object
115 * Caller must hold mode config lock.
117 * Creates a new framebuffer objects and adds it to @dev's DRM mode_config.
120 * Pointer to new framebuffer or NULL on error.
122 struct drm_framebuffer *drm_framebuffer_create(drm_device_t *dev)
124 struct drm_framebuffer *fb;
126 /* Limit to single framebuffer for now */
127 if (dev->mode_config.num_fb > 1) {
128 spin_unlock(&dev->mode_config.config_lock);
129 DRM_ERROR("Attempt to add multiple framebuffers failed\n");
133 fb = kzalloc(sizeof(struct drm_framebuffer), GFP_KERNEL);
137 fb->id = drm_idr_get(dev, fb);
139 dev->mode_config.num_fb++;
140 list_add(&fb->head, &dev->mode_config.fb_list);
144 EXPORT_SYMBOL(drm_framebuffer_create);
147 * drm_framebuffer_destroy - remove a framebuffer object
148 * @fb: framebuffer to remove
151 * Caller must hold mode config lock.
153 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
154 * it, setting it to NULL.
156 void drm_framebuffer_destroy(struct drm_framebuffer *fb)
158 drm_device_t *dev = fb->dev;
159 struct drm_crtc *crtc;
161 /* remove from any CRTC */
162 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
167 drm_idr_put(dev, fb->id);
169 dev->mode_config.num_fb--;
173 EXPORT_SYMBOL(drm_framebuffer_destroy);
176 * drm_crtc_create - create a new CRTC object
178 * @funcs: callbacks for the new CRTC
181 * Caller must hold mode config lock.
183 * Creates a new CRTC object and adds it to @dev's mode_config structure.
186 * Pointer to new CRTC object or NULL on error.
188 struct drm_crtc *drm_crtc_create(drm_device_t *dev,
189 const struct drm_crtc_funcs *funcs)
191 struct drm_crtc *crtc;
193 crtc = kzalloc(sizeof(struct drm_crtc), GFP_KERNEL);
200 crtc->id = drm_idr_get(dev, crtc);
202 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
203 dev->mode_config.num_crtc++;
207 EXPORT_SYMBOL(drm_crtc_create);
210 * drm_crtc_destroy - remove a CRTC object
211 * @crtc: CRTC to remove
214 * Caller must hold mode config lock.
216 * Cleanup @crtc. Calls @crtc's cleanup function, then removes @crtc from
217 * its associated DRM device's mode_config. Frees it afterwards.
219 void drm_crtc_destroy(struct drm_crtc *crtc)
221 drm_device_t *dev = crtc->dev;
223 if (crtc->funcs->cleanup)
224 (*crtc->funcs->cleanup)(crtc);
226 drm_idr_put(dev, crtc->id);
227 list_del(&crtc->head);
228 dev->mode_config.num_crtc--;
231 EXPORT_SYMBOL(drm_crtc_destroy);
234 * drm_crtc_in_use - check if a given CRTC is in a mode_config
235 * @crtc: CRTC to check
238 * Caller must hold mode config lock.
240 * Walk @crtc's DRM device's mode_config and see if it's in use.
243 * True if @crtc is part of the mode_config, false otherwise.
245 bool drm_crtc_in_use(struct drm_crtc *crtc)
247 struct drm_output *output;
248 drm_device_t *dev = crtc->dev;
249 /* FIXME: Locking around list access? */
250 list_for_each_entry(output, &dev->mode_config.output_list, head)
251 if (output->crtc == crtc)
255 EXPORT_SYMBOL(drm_crtc_in_use);
258 * Detailed mode info for a standard 640x480@60Hz monitor
260 static struct drm_display_mode std_mode[] = {
261 { DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 25200, 640, 656,
262 752, 800, 0, 480, 490, 492, 525, 0,
263 V_NHSYNC | V_NVSYNC) }, /* 640x480@60Hz */
267 * drm_crtc_probe_output_modes - get complete set of display modes
269 * @maxX: max width for modes
270 * @maxY: max height for modes
273 * Caller must hold mode config lock.
275 * Based on @dev's mode_config layout, scan all the outputs and try to detect
276 * modes on them. Modes will first be added to the output's probed_modes
277 * list, then culled (based on validity and the @maxX, @maxY parameters) and
278 * put into the normal modes list.
280 * Intended to be used either at bootup time or when major configuration
281 * changes have occurred.
283 * FIXME: take into account monitor limits
285 void drm_crtc_probe_output_modes(struct drm_device *dev, int maxX, int maxY)
287 struct drm_output *output;
288 struct drm_display_mode *mode, *t;
290 //if (maxX == 0 || maxY == 0)
293 list_for_each_entry(output, &dev->mode_config.output_list, head) {
295 /* set all modes to the unverified state */
296 list_for_each_entry_safe(mode, t, &output->modes, head)
297 mode->status = MODE_UNVERIFIED;
299 output->status = (*output->funcs->detect)(output);
301 if (output->status == output_status_disconnected) {
302 DRM_DEBUG("%s is disconnected\n", output->name);
303 /* TODO set EDID to NULL */
307 ret = (*output->funcs->get_modes)(output);
310 drm_mode_output_list_update(output);
314 drm_mode_validate_size(dev, &output->modes, maxX,
316 list_for_each_entry_safe(mode, t, &output->modes, head) {
317 if (mode->status == MODE_OK)
318 mode->status = (*output->funcs->mode_valid)(output,mode);
322 drm_mode_prune_invalid(dev, &output->modes, TRUE);
324 if (list_empty(&output->modes)) {
325 struct drm_display_mode *stdmode;
327 DRM_DEBUG("No valid modes on %s\n", output->name);
329 /* Should we do this here ???
330 * When no valid EDID modes are available we end up
331 * here and bailed in the past, now we add a standard
332 * 640x480@60Hz mode and carry on.
334 stdmode = drm_mode_duplicate(dev, &std_mode[0]);
335 drm_mode_probed_add(output, stdmode);
336 drm_mode_list_concat(&output->probed_modes,
339 DRM_DEBUG("Adding standard 640x480 @ 60Hz to %s\n",
343 drm_mode_sort(&output->modes);
345 DRM_DEBUG("Probed modes for %s\n", output->name);
346 list_for_each_entry_safe(mode, t, &output->modes, head) {
347 mode->vrefresh = drm_mode_vrefresh(mode);
349 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
350 drm_mode_debug_printmodeline(dev, mode);
356 * drm_crtc_set_mode - set a mode
357 * @crtc: CRTC to program
363 * Caller must hold mode config lock.
365 * Try to set @mode on @crtc. Give @crtc and its associated outputs a chance
366 * to fixup or reject the mode prior to trying to set it.
369 * True if the mode was set successfully, or false otherwise.
371 bool drm_crtc_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
374 drm_device_t *dev = crtc->dev;
375 struct drm_display_mode *adjusted_mode, saved_mode;
376 int saved_x, saved_y;
377 bool didLock = false;
379 struct drm_output *output;
381 adjusted_mode = drm_mode_duplicate(dev, mode);
383 crtc->enabled = drm_crtc_in_use(crtc);
385 if (!crtc->enabled) {
389 didLock = crtc->funcs->lock(crtc);
391 saved_mode = crtc->mode;
395 /* Update crtc values up front so the driver can rely on them for mode
402 /* XXX short-circuit changes to base location only */
404 /* Pass our mode to the outputs and the CRTC to give them a chance to
405 * adjust it according to limitations or output properties, and also
406 * a chance to reject the mode entirely.
408 list_for_each_entry(output, &dev->mode_config.output_list, head) {
410 if (output->crtc != crtc)
413 if (!output->funcs->mode_fixup(output, mode, adjusted_mode)) {
418 if (!crtc->funcs->mode_fixup(crtc, mode, adjusted_mode)) {
422 /* Prepare the outputs and CRTCs before setting the mode. */
423 list_for_each_entry(output, &dev->mode_config.output_list, head) {
425 if (output->crtc != crtc)
428 /* Disable the output as the first thing we do. */
429 output->funcs->prepare(output);
432 crtc->funcs->prepare(crtc);
434 /* Set up the DPLL and any output state that needs to adjust or depend
437 crtc->funcs->mode_set(crtc, mode, adjusted_mode, x, y);
438 list_for_each_entry(output, &dev->mode_config.output_list, head) {
439 if (output->crtc == crtc) {
440 dev_warn(&output->dev->pdev->dev, "%s: set mode %s\n",
441 output->name, mode->name);
442 output->funcs->mode_set(output, mode, adjusted_mode);
446 /* Now, enable the clocks, plane, pipe, and outputs that we set up. */
447 crtc->funcs->commit(crtc);
448 list_for_each_entry(output, &dev->mode_config.output_list, head) {
449 if (output->crtc == crtc) {
450 output->funcs->commit(output);
451 #if 0 // TODO def RANDR_12_INTERFACE
452 if (output->randr_output)
453 RRPostPendingProperties (output->randr_output);
458 /* XXX free adjustedmode */
459 drm_mode_destroy(dev, adjusted_mode);
462 // if (scrn->pScreen)
463 // drm_crtc_set_screen_sub_pixel_order(dev);
469 crtc->mode = saved_mode;
473 crtc->funcs->unlock (crtc);
477 EXPORT_SYMBOL(drm_crtc_set_mode);
480 * drm_disable_unused_functions - disable unused objects
484 * Caller must hold mode config lock.
486 * If an output or CRTC isn't part of @dev's mode_config, it can be disabled
487 * by calling its dpms function, which should power it off.
489 void drm_disable_unused_functions(struct drm_device *dev)
491 struct drm_output *output;
492 struct drm_crtc *crtc;
494 list_for_each_entry(output, &dev->mode_config.output_list, head) {
496 (*output->funcs->dpms)(output, DPMSModeOff);
499 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
501 crtc->funcs->dpms(crtc, DPMSModeOff);
506 * drm_mode_probed_add - add a mode to the specified output's probed mode list
507 * @output: output the new mode
511 * Caller must hold mode config lock.
513 * Add @mode to @output's mode list for later use.
515 void drm_mode_probed_add(struct drm_output *output,
516 struct drm_display_mode *mode)
518 list_add(&mode->head, &output->probed_modes);
520 EXPORT_SYMBOL(drm_mode_probed_add);
523 * drm_mode_remove - remove and free a mode
524 * @output: output list to modify
525 * @mode: mode to remove
528 * Caller must hold mode config lock.
530 * Remove @mode from @output's mode list, then free it.
532 void drm_mode_remove(struct drm_output *output, struct drm_display_mode *mode)
534 list_del(&mode->head);
537 EXPORT_SYMBOL(drm_mode_remove);
540 * drm_output_create - create a new output
542 * @funcs: callbacks for this output
543 * @name: user visible name of the output
546 * Caller must hold @dev's mode_config lock.
548 * Creates a new drm_output structure and adds it to @dev's mode_config
552 * Pointer to the new output or NULL on error.
554 struct drm_output *drm_output_create(drm_device_t *dev,
555 const struct drm_output_funcs *funcs,
558 struct drm_output *output = NULL;
560 output = kzalloc(sizeof(struct drm_output), GFP_KERNEL);
565 output->funcs = funcs;
566 output->id = drm_idr_get(dev, output);
568 strncpy(output->name, name, DRM_OUTPUT_LEN);
569 output->name[DRM_OUTPUT_LEN - 1] = 0;
570 output->subpixel_order = SubPixelUnknown;
571 INIT_LIST_HEAD(&output->probed_modes);
572 INIT_LIST_HEAD(&output->modes);
574 /* output_set_monitor(output)? */
575 /* check for output_ignored(output)? */
577 spin_lock(&dev->mode_config.config_lock);
578 list_add_tail(&output->head, &dev->mode_config.output_list);
579 dev->mode_config.num_output++;
581 spin_unlock(&dev->mode_config.config_lock);
586 EXPORT_SYMBOL(drm_output_create);
589 * drm_output_destroy - remove an output
590 * @output: output to remove
593 * Caller must hold @dev's mode_config lock.
595 * Call @output's cleanup function, then remove the output from the DRM
596 * mode_config after freeing @output's modes.
598 void drm_output_destroy(struct drm_output *output)
600 struct drm_device *dev = output->dev;
601 struct drm_display_mode *mode, *t;
603 if (*output->funcs->cleanup)
604 (*output->funcs->cleanup)(output);
606 list_for_each_entry_safe(mode, t, &output->probed_modes, head)
607 drm_mode_remove(output, mode);
609 list_for_each_entry_safe(mode, t, &output->modes, head)
610 drm_mode_remove(output, mode);
612 spin_lock(&dev->mode_config.config_lock);
613 drm_idr_put(dev, output->id);
614 list_del(&output->head);
615 spin_unlock(&dev->mode_config.config_lock);
618 EXPORT_SYMBOL(drm_output_destroy);
621 * drm_output_rename - rename an output
622 * @output: output to rename
623 * @name: new user visible name
628 * Simply stuff a new name into @output's name field, based on @name.
631 * True if the name was changed, false otherwise.
633 bool drm_output_rename(struct drm_output *output, const char *name)
638 strncpy(output->name, name, DRM_OUTPUT_LEN);
639 output->name[DRM_OUTPUT_LEN - 1] = 0;
641 DRM_DEBUG("Changed name to %s\n", output->name);
642 // drm_output_set_monitor(output);
643 // if (drm_output_ignored(output))
648 EXPORT_SYMBOL(drm_output_rename);
651 * drm_mode_create - create a new display mode
657 * Create a new drm_display_mode, give it an ID, and return it.
660 * Pointer to new mode on success, NULL on error.
662 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
664 struct drm_display_mode *nmode;
666 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
670 nmode->mode_id = drm_idr_get(dev, nmode);
673 EXPORT_SYMBOL(drm_mode_create);
676 * drm_mode_destroy - remove a mode
678 * @mode: mode to remove
681 * Caller must hold mode config lock.
683 * Free @mode's unique identifier, then free it.
685 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
687 drm_idr_put(dev, mode->mode_id);
691 EXPORT_SYMBOL(drm_mode_destroy);
694 * drm_mode_config_init - initialize DRM mode_configuration structure
698 * None, should happen single threaded at init time.
700 * Initialize @dev's mode_config structure, used for tracking the graphics
701 * configuration of @dev.
703 void drm_mode_config_init(drm_device_t *dev)
705 spin_lock_init(&dev->mode_config.config_lock);
706 INIT_LIST_HEAD(&dev->mode_config.fb_list);
707 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
708 INIT_LIST_HEAD(&dev->mode_config.output_list);
709 INIT_LIST_HEAD(&dev->mode_config.usermode_list);
710 idr_init(&dev->mode_config.crtc_idr);
712 EXPORT_SYMBOL(drm_mode_config_init);
715 * drm_get_buffer_object - find the buffer object for a given handle
717 * @bo: pointer to caller's buffer_object pointer
718 * @handle: handle to lookup
721 * Must take @dev's struct_mutex to protect buffer object lookup.
723 * Given @handle, lookup the buffer object in @dev and put it in the caller's
727 * Zero on success, -EINVAL if the handle couldn't be found.
729 static int drm_get_buffer_object(drm_device_t *dev, struct drm_buffer_object **bo, unsigned long handle)
731 drm_user_object_t *uo;
732 drm_hash_item_t *hash;
737 mutex_lock(&dev->struct_mutex);
738 ret = drm_ht_find_item(&dev->object_hash, handle, &hash);
740 DRM_ERROR("Couldn't find handle.\n");
745 uo = drm_hash_entry(hash, drm_user_object_t, hash);
746 if (uo->type != drm_buffer_type) {
751 *bo = drm_user_object_entry(uo, drm_buffer_object_t, base);
754 mutex_unlock(&dev->struct_mutex);
759 * drm_pick_crtcs - pick crtcs for output devices
763 * Caller must hold mode config lock.
765 static void drm_pick_crtcs (drm_device_t *dev)
768 struct drm_output *output, *output_equal;
769 struct drm_crtc *crtc;
770 struct drm_display_mode *des_mode = NULL, *modes, *modes_equal;
772 list_for_each_entry(output, &dev->mode_config.output_list, head) {
775 /* Don't hook up outputs that are disconnected ??
777 * This is debateable. Do we want fixed /dev/fbX or
778 * dynamic on hotplug (need mode code for that though) ?
780 * If we don't hook up outputs now, then we only create
781 * /dev/fbX for the output that's enabled, that's good as
782 * the users console will be on that output.
784 * If we do hook up outputs that are disconnected now, then
785 * the user may end up having to muck about with the fbcon
786 * map flags to assign his console to the enabled output. Ugh.
788 if (output->status != output_status_connected)
792 list_for_each_entry(des_mode, &output->modes, head) {
793 if (des_mode->type & DRM_MODE_TYPE_PREFERRED)
797 /* No preferred mode, let's just select the first available */
798 if (!des_mode || !(des_mode->type & DRM_MODE_TYPE_PREFERRED)) {
799 list_for_each_entry(des_mode, &output->modes, head) {
806 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
808 if ((output->possible_crtcs & (1 << c)) == 0)
811 #if 0 /* should we try and clone ?? - code not tested - FIXME */
813 list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
815 if (output->id == output_equal->id)
818 list_for_each_entry(modes, &output->modes, head) {
819 list_for_each_entry(modes_equal, &output_equal->modes, head) {
820 if (drm_mode_equal (modes, modes_equal)) {
821 if ((output->possible_clones & (1 << o))) {
831 /* Found a CRTC to attach to, do it ! */
833 output->crtc->desired_mode = des_mode;
834 output->initial_x = 0;
835 output->initial_y = 0;
836 DRM_DEBUG("Desired mode for CRTC %d is %s\n",c,des_mode->name);
844 * drm_initial_config - setup a sane initial output configuration
846 * @can_grow: this configuration is growable
849 * Called at init time, must take mode config lock.
851 * Scan the CRTCs and outputs and try to put together an initial setup.
852 * At the moment, this is a cloned configuration across all heads with
853 * a new framebuffer object as the backing store.
856 * Zero if everything went ok, nonzero otherwise.
858 bool drm_initial_config(drm_device_t *dev, bool can_grow)
860 struct drm_output *output;
863 spin_lock(&dev->mode_config.config_lock);
865 drm_crtc_probe_output_modes(dev, 2048, 2048);
869 list_for_each_entry(output, &dev->mode_config.output_list, head) {
871 /* can't setup the output if there's no assigned crtc or mode */
872 if (!output->crtc || !output->crtc->desired_mode)
875 dev->driver->fb_probe(dev, output->crtc);
877 drm_disable_unused_functions(dev);
879 spin_unlock(&dev->mode_config.config_lock);
882 EXPORT_SYMBOL(drm_initial_config);
885 * drm_mode_config_cleanup - free up DRM mode_config info
889 * Caller must hold mode config lock.
891 * Free up all the outputs and CRTCs associated with this DRM device, then
892 * free up the framebuffers and associated buffer objects.
894 * FIXME: cleanup any dangling user buffer objects too
896 void drm_mode_config_cleanup(drm_device_t *dev)
898 struct drm_output *output, *ot;
899 struct drm_crtc *crtc, *ct;
900 struct drm_framebuffer *fb, *fbt;
901 struct drm_display_mode *mode, *mt;
902 list_for_each_entry_safe(output, ot, &dev->mode_config.output_list, head) {
903 drm_output_destroy(output);
906 list_for_each_entry_safe(mode, mt, &dev->mode_config.usermode_list, head) {
907 drm_mode_destroy(dev, mode);
910 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
911 dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
912 /* If this FB was the kernel one, free it */
913 if (fb->bo->type == drm_bo_type_kernel) {
914 mutex_lock(&dev->struct_mutex);
915 drm_bo_usage_deref_locked(fb->bo);
916 mutex_unlock(&dev->struct_mutex);
918 drm_framebuffer_destroy(fb);
921 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
922 drm_crtc_destroy(crtc);
926 EXPORT_SYMBOL(drm_mode_config_cleanup);
929 * drm_crtc_set_config - set a new config from userspace
930 * @crtc: CRTC to setup
931 * @crtc_info: user provided configuration
932 * @new_mode: new mode to set
933 * @output_set: set of outputs for the new config
934 * @fb: new framebuffer
937 * Caller must hold mode config lock.
939 * Setup a new configuration, provided by the user in @crtc_info, and enable
945 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)
947 drm_device_t *dev = crtc->dev;
948 struct drm_crtc **save_crtcs, *new_crtc;
949 bool save_enabled = crtc->enabled;
951 struct drm_output *output;
954 save_crtcs = kzalloc(dev->mode_config.num_crtc * sizeof(struct drm_crtc *), GFP_KERNEL);
961 if (crtc_info->x != crtc->x || crtc_info->y != crtc->y)
964 if (new_mode && (crtc->mode.mode_id != new_mode->mode_id))
967 list_for_each_entry(output, &dev->mode_config.output_list, head) {
968 save_crtcs[count++] = output->crtc;
970 if (output->crtc == crtc)
973 new_crtc = output->crtc;
975 for (ro = 0; ro < crtc_info->count_outputs; ro++) {
976 if (output_set[ro] == output)
979 if (new_crtc != output->crtc) {
981 output->crtc = new_crtc;
987 crtc->enabled = (new_mode != NULL);
988 if (new_mode != NULL) {
989 DRM_DEBUG("attempting to set mode from userspace\n");
990 drm_mode_debug_printmodeline(dev, new_mode);
991 if (!drm_crtc_set_mode(crtc, new_mode, crtc_info->x,
993 crtc->enabled = save_enabled;
995 list_for_each_entry(output, &dev->mode_config.output_list, head)
996 output->crtc = save_crtcs[count++];
1000 crtc->desired_x = crtc_info->x;
1001 crtc->desired_y = crtc_info->y;
1002 crtc->desired_mode = new_mode;
1004 drm_disable_unused_functions(dev);
1011 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1012 * @out: drm_mode_modeinfo struct to return to the user
1013 * @in: drm_display_mode to use
1018 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1021 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out, struct drm_display_mode *in)
1024 out->id = in->mode_id;
1025 out->clock = in->clock;
1026 out->hdisplay = in->hdisplay;
1027 out->hsync_start = in->hsync_start;
1028 out->hsync_end = in->hsync_end;
1029 out->htotal = in->htotal;
1030 out->hskew = in->hskew;
1031 out->vdisplay = in->vdisplay;
1032 out->vsync_start = in->vsync_start;
1033 out->vsync_end = in->vsync_end;
1034 out->vtotal = in->vtotal;
1035 out->vscan = in->vscan;
1036 out->vrefresh = in->vrefresh;
1037 out->flags = in->flags;
1038 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1039 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1043 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1044 * @out: drm_display_mode to return to the user
1045 * @in: drm_mode_modeinfo to use
1050 * Convert a drmo_mode_modeinfo into a drm_display_mode structure to return to
1053 void drm_crtc_convert_umode(struct drm_display_mode *out, struct drm_mode_modeinfo *in)
1055 out->clock = in->clock;
1056 out->hdisplay = in->hdisplay;
1057 out->hsync_start = in->hsync_start;
1058 out->hsync_end = in->hsync_end;
1059 out->htotal = in->htotal;
1060 out->hskew = in->hskew;
1061 out->vdisplay = in->vdisplay;
1062 out->vsync_start = in->vsync_start;
1063 out->vsync_end = in->vsync_end;
1064 out->vtotal = in->vtotal;
1065 out->vscan = in->vscan;
1066 out->vrefresh = in->vrefresh;
1067 out->flags = in->flags;
1068 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1069 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1073 * drm_mode_getresources - get graphics configuration
1074 * @inode: inode from the ioctl
1075 * @filp: file * from the ioctl
1076 * @cmd: cmd from ioctl
1077 * @arg: arg from ioctl
1080 * Takes mode config lock.
1082 * Construct a set of configuration description structures and return
1083 * them to the user, including CRTC, output and framebuffer configuration.
1085 * Called by the user via ioctl.
1088 * Zero on success, errno on failure.
1090 int drm_mode_getresources(struct inode *inode, struct file *filp,
1091 unsigned int cmd, unsigned long arg)
1093 drm_file_t *priv = filp->private_data;
1094 drm_device_t *dev = priv->head->dev;
1095 struct drm_mode_card_res __user *argp = (void __user *)arg;
1096 struct drm_mode_card_res card_res;
1097 struct list_head *lh;
1098 struct drm_framebuffer *fb;
1099 struct drm_output *output;
1100 struct drm_crtc *crtc;
1101 struct drm_mode_modeinfo u_mode;
1102 struct drm_display_mode *mode;
1105 int output_count = 0;
1110 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1112 spin_lock(&dev->mode_config.config_lock);
1114 list_for_each(lh, &dev->mode_config.fb_list)
1117 list_for_each(lh, &dev->mode_config.crtc_list)
1120 list_for_each_entry(output, &dev->mode_config.output_list,
1123 list_for_each(lh, &output->modes)
1126 list_for_each(lh, &dev->mode_config.usermode_list)
1129 if (copy_from_user(&card_res, argp, sizeof(card_res))) {
1134 if (card_res.count_modes == 0) {
1135 DRM_DEBUG("probing modes %dx%d\n", dev->mode_config.max_width, dev->mode_config.max_height);
1136 drm_crtc_probe_output_modes(dev, dev->mode_config.max_width, dev->mode_config.max_height);
1138 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1139 list_for_each(lh, &output->modes)
1142 list_for_each(lh, &dev->mode_config.usermode_list)
1146 /* handle this in 4 parts */
1148 if (card_res.count_fbs >= fb_count) {
1150 list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
1151 if (put_user(fb->id, &card_res.fb_id[copied++])) {
1157 card_res.count_fbs = fb_count;
1160 if (card_res.count_crtcs >= crtc_count) {
1162 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head){
1163 DRM_DEBUG("CRTC ID is %d\n", crtc->id);
1164 if (put_user(crtc->id, &card_res.crtc_id[copied++])) {
1170 card_res.count_crtcs = crtc_count;
1174 if (card_res.count_outputs >= output_count) {
1176 list_for_each_entry(output, &dev->mode_config.output_list,
1178 DRM_DEBUG("OUTPUT ID is %d\n", output->id);
1179 if (put_user(output->id, &card_res.output_id[copied++])) {
1185 card_res.count_outputs = output_count;
1188 if (card_res.count_modes >= mode_count) {
1190 list_for_each_entry(output, &dev->mode_config.output_list,
1192 list_for_each_entry(mode, &output->modes, head) {
1193 drm_crtc_convert_to_umode(&u_mode, mode);
1194 if (copy_to_user(&card_res.modes[copied++], &u_mode, sizeof(struct drm_mode_modeinfo))) {
1200 /* add in user modes */
1201 list_for_each_entry(mode, &dev->mode_config.usermode_list, head) {
1202 drm_crtc_convert_to_umode(&u_mode, mode);
1203 if (copy_to_user(&card_res.modes[copied++], &u_mode, sizeof(struct drm_mode_modeinfo))) {
1209 card_res.count_modes = mode_count;
1212 DRM_DEBUG("Counted %d %d %d\n", card_res.count_crtcs,
1213 card_res.count_outputs,
1214 card_res.count_modes);
1216 if (copy_to_user(argp, &card_res, sizeof(card_res)))
1220 spin_unlock(&dev->mode_config.config_lock);
1225 * drm_mode_getcrtc - get CRTC configuration
1226 * @inode: inode from the ioctl
1227 * @filp: file * from the ioctl
1228 * @cmd: cmd from ioctl
1229 * @arg: arg from ioctl
1234 * Construct a CRTC configuration structure to return to the user.
1236 * Called by the user via ioctl.
1239 * Zero on success, errno on failure.
1241 int drm_mode_getcrtc(struct inode *inode, struct file *filp,
1242 unsigned int cmd, unsigned long arg)
1244 drm_file_t *priv = filp->private_data;
1245 drm_device_t *dev = priv->head->dev;
1246 struct drm_mode_crtc __user *argp = (void __user *)arg;
1247 struct drm_mode_crtc crtc_resp;
1248 struct drm_crtc *crtc;
1249 struct drm_output *output;
1253 if (copy_from_user(&crtc_resp, argp, sizeof(crtc_resp)))
1256 spin_lock(&dev->mode_config.config_lock);
1257 crtc = idr_find(&dev->mode_config.crtc_idr, crtc_resp.crtc_id);
1258 if (!crtc || (crtc->id != crtc_resp.crtc_id)) {
1263 crtc_resp.x = crtc->x;
1264 crtc_resp.y = crtc->y;
1265 crtc_resp.fb_id = 1;
1267 crtc_resp.outputs = 0;
1268 if (crtc->enabled) {
1270 crtc_resp.mode = crtc->mode.mode_id;
1272 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1273 if (output->crtc == crtc)
1274 crtc_resp.outputs |= 1 << (ocount++);
1280 if (copy_to_user(argp, &crtc_resp, sizeof(crtc_resp)))
1284 spin_unlock(&dev->mode_config.config_lock);
1289 * drm_mode_getoutput - get output configuration
1290 * @inode: inode from the ioctl
1291 * @filp: file * from the ioctl
1292 * @cmd: cmd from ioctl
1293 * @arg: arg from ioctl
1298 * Construct a output configuration structure to return to the user.
1300 * Called by the user via ioctl.
1303 * Zero on success, errno on failure.
1305 int drm_mode_getoutput(struct inode *inode, struct file *filp,
1306 unsigned int cmd, unsigned long arg)
1308 drm_file_t *priv = filp->private_data;
1309 drm_device_t *dev = priv->head->dev;
1310 struct drm_mode_get_output __user *argp = (void __user *)arg;
1311 struct drm_mode_get_output out_resp;
1312 struct drm_output *output;
1313 struct drm_display_mode *mode;
1319 if (copy_from_user(&out_resp, argp, sizeof(out_resp)))
1322 DRM_DEBUG("output id %d:\n", out_resp.output);
1324 spin_lock(&dev->mode_config.config_lock);
1325 output= idr_find(&dev->mode_config.crtc_idr, out_resp.output);
1326 if (!output || (output->id != out_resp.output)) {
1331 list_for_each_entry(mode, &output->modes, head)
1334 for (i = 0; i < DRM_OUTPUT_MAX_UMODES; i++)
1335 if (output->user_mode_ids[i] != 0)
1338 strncpy(out_resp.name, output->name, DRM_OUTPUT_NAME_LEN);
1339 out_resp.name[DRM_OUTPUT_NAME_LEN-1] = 0;
1341 out_resp.mm_width = output->mm_width;
1342 out_resp.mm_height = output->mm_height;
1343 out_resp.subpixel = output->subpixel_order;
1344 out_resp.connection = output->status;
1346 out_resp.crtc = output->crtc->id;
1350 if ((out_resp.count_modes >= mode_count) && mode_count) {
1352 list_for_each_entry(mode, &output->modes, head) {
1353 if (put_user(mode->mode_id, &out_resp.modes[copied++])) {
1358 for (i = 0; i < DRM_OUTPUT_MAX_UMODES; i++) {
1359 if (output->user_mode_ids[i] != 0)
1360 if (put_user(output->user_mode_ids[i], &out_resp.modes[copied++])) {
1367 out_resp.count_modes = mode_count;
1370 if (copy_to_user(argp, &out_resp, sizeof(out_resp)))
1374 spin_unlock(&dev->mode_config.config_lock);
1379 * drm_mode_setcrtc - set CRTC configuration
1380 * @inode: inode from the ioctl
1381 * @filp: file * from the ioctl
1382 * @cmd: cmd from ioctl
1383 * @arg: arg from ioctl
1388 * Build a new CRTC configuration based on user request.
1390 * Called by the user via ioctl.
1393 * Zero on success, errno on failure.
1395 int drm_mode_setcrtc(struct inode *inode, struct file *filp,
1396 unsigned int cmd, unsigned long arg)
1398 drm_file_t *priv = filp->private_data;
1399 drm_device_t *dev = priv->head->dev;
1400 struct drm_mode_crtc __user *argp = (void __user *)arg;
1401 struct drm_mode_crtc crtc_req;
1402 struct drm_crtc *crtc;
1403 struct drm_output **output_set = NULL, *output;
1404 struct drm_display_mode *mode;
1405 struct drm_framebuffer *fb = NULL;
1409 if (copy_from_user(&crtc_req, argp, sizeof(crtc_req)))
1412 spin_lock(&dev->mode_config.config_lock);
1413 crtc = idr_find(&dev->mode_config.crtc_idr, crtc_req.crtc_id);
1414 if (!crtc || (crtc->id != crtc_req.crtc_id)) {
1415 DRM_DEBUG("Unknown CRTC ID %d\n", crtc_req.crtc_id);
1420 if (crtc_req.mode) {
1421 /* if we have a mode we need a framebuffer */
1422 if (crtc_req.fb_id) {
1423 fb = idr_find(&dev->mode_config.crtc_idr, crtc_req.fb_id);
1424 if (!fb || (fb->id != crtc_req.fb_id)) {
1425 DRM_DEBUG("Unknown FB ID%d\n", crtc_req.fb_id);
1430 mode = idr_find(&dev->mode_config.crtc_idr, crtc_req.mode);
1431 if (!mode || (mode->mode_id != crtc_req.mode)) {
1432 struct drm_output *output;
1434 list_for_each_entry(output,
1435 &dev->mode_config.output_list,
1437 list_for_each_entry(mode, &output->modes,
1439 drm_mode_debug_printmodeline(dev,
1444 DRM_DEBUG("Unknown mode id %d, %p\n", crtc_req.mode, mode);
1451 if (crtc_req.count_outputs == 0 && mode) {
1452 DRM_DEBUG("Count outputs is 0 but mode set\n");
1457 if (crtc_req.count_outputs > 0 && !mode && !fb) {
1458 DRM_DEBUG("Count outputs is %d but no mode or fb set\n", crtc_req.count_outputs);
1463 if (crtc_req.count_outputs > 0) {
1465 output_set = kmalloc(crtc_req.count_outputs *
1466 sizeof(struct drm_output *), GFP_KERNEL);
1472 for (i = 0; i < crtc_req.count_outputs; i++) {
1473 if (get_user(out_id, &crtc_req.set_outputs[i])) {
1478 output = idr_find(&dev->mode_config.crtc_idr, out_id);
1479 if (!output || (out_id != output->id)) {
1480 DRM_DEBUG("Output id %d unknown\n", out_id);
1485 output_set[i] = output;
1489 ret = drm_crtc_set_config(crtc, &crtc_req, mode, output_set, fb);
1492 spin_unlock(&dev->mode_config.config_lock);
1497 * drm_mode_addfb - add an FB to the graphics configuration
1498 * @inode: inode from the ioctl
1499 * @filp: file * from the ioctl
1500 * @cmd: cmd from ioctl
1501 * @arg: arg from ioctl
1504 * Takes mode config lock.
1506 * Add a new FB to the specified CRTC, given a user request.
1508 * Called by the user via ioctl.
1511 * Zero on success, errno on failure.
1513 int drm_mode_addfb(struct inode *inode, struct file *filp,
1514 unsigned int cmd, unsigned long arg)
1516 struct drm_file *priv = filp->private_data;
1517 struct drm_device *dev = priv->head->dev;
1518 struct drm_mode_fb_cmd __user *argp = (void __user *)arg;
1519 struct drm_mode_fb_cmd r;
1520 struct drm_mode_config *config = &dev->mode_config;
1521 struct drm_framebuffer *fb;
1522 struct drm_buffer_object *bo;
1523 struct drm_crtc *crtc;
1526 if (copy_from_user(&r, argp, sizeof(r)))
1529 if ((config->min_width > r.width) || (r.width > config->max_width)) {
1530 DRM_ERROR("mode new framebuffer width not within limits\n");
1533 if ((config->min_height > r.height) || (r.height > config->max_height)) {
1534 DRM_ERROR("mode new framebuffer height not within limits\n");
1538 spin_lock(&dev->mode_config.config_lock);
1539 /* TODO check limits are okay */
1540 ret = drm_get_buffer_object(dev, &bo, r.handle);
1546 /* TODO check buffer is sufficently large */
1547 /* TODO setup destructor callback */
1549 fb = drm_framebuffer_create(dev);
1555 fb->width = r.width;
1556 fb->height = r.height;
1557 fb->pitch = r.pitch;
1558 fb->bits_per_pixel = r.bpp;
1559 fb->depth = r.depth;
1560 fb->offset = bo->offset;
1563 r.buffer_id = fb->id;
1565 list_add(&fb->filp_head, &priv->fbs);
1567 if (copy_to_user(argp, &r, sizeof(r))) {
1572 /* FIXME: bind the fb to the right crtc */
1573 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1575 dev->driver->fb_probe(dev, crtc);
1579 spin_unlock(&dev->mode_config.config_lock);
1584 * drm_mode_rmfb - remove an FB from the configuration
1585 * @inode: inode from the ioctl
1586 * @filp: file * from the ioctl
1587 * @cmd: cmd from ioctl
1588 * @arg: arg from ioctl
1591 * Takes mode config lock.
1593 * Remove the FB specified by the user.
1595 * Called by the user via ioctl.
1598 * Zero on success, errno on failure.
1600 int drm_mode_rmfb(struct inode *inode, struct file *filp,
1601 unsigned int cmd, unsigned long arg)
1603 drm_file_t *priv = filp->private_data;
1604 drm_device_t *dev = priv->head->dev;
1605 struct drm_framebuffer *fb = 0;
1609 spin_lock(&dev->mode_config.config_lock);
1610 fb = idr_find(&dev->mode_config.crtc_idr, id);
1611 /* TODO check that we realy get a framebuffer back. */
1612 if (!fb || (id != fb->id)) {
1613 DRM_ERROR("mode invalid framebuffer id\n");
1618 dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1620 /* TODO check if we own the buffer */
1621 /* TODO release all crtc connected to the framebuffer */
1622 /* bind the fb to the crtc for now */
1623 /* TODO unhock the destructor from the buffer object */
1625 drm_framebuffer_destroy(fb);
1628 spin_unlock(&dev->mode_config.config_lock);
1633 * drm_mode_getfb - get FB info
1634 * @inode: inode from the ioctl
1635 * @filp: file * from the ioctl
1636 * @cmd: cmd from ioctl
1637 * @arg: arg from ioctl
1642 * Lookup the FB given its ID and return info about it.
1644 * Called by the user via ioctl.
1647 * Zero on success, errno on failure.
1649 int drm_mode_getfb(struct inode *inode, struct file *filp,
1650 unsigned int cmd, unsigned long arg)
1652 drm_file_t *priv = filp->private_data;
1653 drm_device_t *dev = priv->head->dev;
1654 struct drm_mode_fb_cmd __user *argp = (void __user *)arg;
1655 struct drm_mode_fb_cmd r;
1656 struct drm_framebuffer *fb;
1659 if (copy_from_user(&r, argp, sizeof(r)))
1662 spin_lock(&dev->mode_config.config_lock);
1663 fb = idr_find(&dev->mode_config.crtc_idr, r.buffer_id);
1664 if (!fb || (r.buffer_id != fb->id)) {
1665 DRM_ERROR("invalid framebuffer id\n");
1670 r.height = fb->height;
1671 r.width = fb->width;
1672 r.depth = fb->depth;
1673 r.bpp = fb->bits_per_pixel;
1674 r.handle = fb->bo->base.hash.key;
1675 r.pitch = fb->pitch;
1677 if (copy_to_user(argp, &r, sizeof(r)))
1681 spin_unlock(&dev->mode_config.config_lock);
1686 * drm_fb_release - remove and free the FBs on this file
1687 * @filp: file * from the ioctl
1690 * Takes mode config lock.
1692 * Destroy all the FBs associated with @filp.
1694 * Called by the user via ioctl.
1697 * Zero on success, errno on failure.
1699 void drm_fb_release(struct file *filp)
1701 drm_file_t *priv = filp->private_data;
1702 drm_device_t *dev = priv->head->dev;
1703 struct drm_framebuffer *fb, *tfb;
1705 spin_lock(&dev->mode_config.config_lock);
1706 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1707 list_del(&fb->filp_head);
1708 dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1709 drm_framebuffer_destroy(fb);
1711 spin_unlock(&dev->mode_config.config_lock);
1715 * drm_fb_newmode - adds a user defined mode
1716 * @inode: inode from the ioctl
1717 * @filp: file * from the ioctl
1718 * @cmd: cmd from ioctl
1719 * @arg: arg from ioctl
1721 * Adds a user specified mode to the kernel.
1723 * Called by the user via ioctl.
1726 * writes new mode id into arg.
1727 * Zero on success, errno on failure.
1729 int drm_mode_addmode(struct inode *inode, struct file *filp,
1730 unsigned int cmd, unsigned long arg)
1732 drm_file_t *priv = filp->private_data;
1733 drm_device_t *dev = priv->head->dev;
1734 struct drm_mode_modeinfo __user *argp = (void __user *)arg;
1735 struct drm_mode_modeinfo new_mode;
1736 struct drm_display_mode *user_mode;
1739 if (copy_from_user(&new_mode, argp, sizeof(new_mode)))
1742 spin_lock(&dev->mode_config.config_lock);
1743 user_mode = drm_mode_create(dev);
1749 drm_crtc_convert_umode(user_mode, &new_mode);
1750 user_mode->type |= DRM_MODE_TYPE_USERDEF;
1752 user_mode->output_count = 0;
1754 list_add(&user_mode->head, &dev->mode_config.usermode_list);
1756 new_mode.id = user_mode->mode_id;
1757 if (copy_to_user(argp, &new_mode, sizeof(new_mode)))
1761 spin_unlock(&dev->mode_config.config_lock);
1766 * drm_fb_rmmode - removes a user defined mode
1767 * @inode: inode from the ioctl
1768 * @filp: file * from the ioctl
1769 * @cmd: cmd from ioctl
1770 * @arg: arg from ioctl
1772 * Remove the user defined mode specified by the user.
1774 * Called by the user via ioctl
1777 * Zero on success, errno on failure.
1779 int drm_mode_rmmode(struct inode *inode, struct file *filp,
1780 unsigned int cmd, unsigned long arg)
1782 drm_file_t *priv = filp->private_data;
1783 drm_device_t *dev = priv->head->dev;
1785 struct drm_display_mode *mode, *t;
1788 spin_lock(&dev->mode_config.config_lock);
1789 mode = idr_find(&dev->mode_config.crtc_idr, id);
1790 if (!mode || (id != mode->mode_id)) {
1795 if (!(mode->type & DRM_MODE_TYPE_USERDEF)) {
1800 if (mode->output_count) {
1805 list_for_each_entry(t, &dev->mode_config.usermode_list, head) {
1807 list_del(&mode->head);
1808 drm_mode_destroy(dev, mode);
1815 spin_unlock(&dev->mode_config.config_lock);
1820 * drm_fb_attachmode - Attach a user mode to an output
1821 * @inode: inode from the ioctl
1822 * @filp: file * from the ioctl
1823 * @cmd: cmd from ioctl
1824 * @arg: arg from ioctl
1826 * This attaches a user specified mode to an output.
1827 * Called by the user via ioctl.
1830 * Zero on success, errno on failure.
1832 int drm_mode_attachmode(struct inode *inode, struct file *filp,
1833 unsigned int cmd, unsigned long arg)
1835 drm_file_t *priv = filp->private_data;
1836 drm_device_t *dev = priv->head->dev;
1837 struct drm_mode_mode_cmd __user *argp = (void __user *)arg;
1838 struct drm_mode_mode_cmd mode_cmd;
1839 struct drm_output *output;
1840 struct drm_display_mode *mode;
1843 if (copy_from_user(&mode_cmd, argp, sizeof(mode_cmd)))
1846 spin_lock(&dev->mode_config.config_lock);
1848 mode = idr_find(&dev->mode_config.crtc_idr, mode_cmd.mode_id);
1849 if (!mode || (mode->mode_id != mode_cmd.mode_id)) {
1854 output = idr_find(&dev->mode_config.crtc_idr, mode_cmd.output_id);
1855 if (!output || (output->id != mode_cmd.output_id)) {
1860 for (i = 0; i < DRM_OUTPUT_MAX_UMODES; i++) {
1861 if (output->user_mode_ids[i] == 0) {
1862 output->user_mode_ids[i] = mode->mode_id;
1863 mode->output_count++;
1868 if (i == DRM_OUTPUT_MAX_UMODES)
1872 spin_unlock(&dev->mode_config.config_lock);
1878 * drm_fb_detachmode - Detach a user specified mode from an output
1879 * @inode: inode from the ioctl
1880 * @filp: file * from the ioctl
1881 * @cmd: cmd from ioctl
1882 * @arg: arg from ioctl
1884 * Called by the user via ioctl.
1887 * Zero on success, errno on failure.
1889 int drm_mode_detachmode(struct inode *inode, struct file *filp,
1890 unsigned int cmd, unsigned long arg)
1892 drm_file_t *priv = filp->private_data;
1893 drm_device_t *dev = priv->head->dev;
1894 struct drm_mode_mode_cmd __user *argp = (void __user *)arg;
1895 struct drm_mode_mode_cmd mode_cmd;
1896 struct drm_output *output;
1897 struct drm_display_mode *mode;
1898 int i, found = 0, ret = 0;
1900 if (copy_from_user(&mode_cmd, argp, sizeof(mode_cmd)))
1903 spin_lock(&dev->mode_config.config_lock);
1905 mode = idr_find(&dev->mode_config.crtc_idr, mode_cmd.mode_id);
1906 if (!mode || (mode->mode_id != mode_cmd.mode_id)) {
1911 output = idr_find(&dev->mode_config.crtc_idr, mode_cmd.output_id);
1912 if (!output || (output->id != mode_cmd.output_id)) {
1918 for (i = 0; i < DRM_OUTPUT_MAX_UMODES; i++) {
1919 if (output->user_mode_ids[i] == mode->mode_id) {
1920 output->user_mode_ids[i] = 0;
1921 mode->output_count--;
1930 spin_unlock(&dev->mode_config.config_lock);