blob: 8323fc3898401ac957d9a677b947455f5a598b91 [file] [log] [blame]
Dave Airlief453ba02008-11-07 14:05:41 -08001/*
2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
5 *
6 * DRM core CRTC related functions
7 *
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
17 *
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 *
26 * Authors:
27 * Keith Packard
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
31 */
32#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040034#include <linux/export.h>
Dave Airlief453ba02008-11-07 14:05:41 -080035#include "drm.h"
36#include "drmP.h"
37#include "drm_crtc.h"
Adam Jackson7466f4c2010-03-29 21:43:23 +000038#include "drm_edid.h"
Dave Airlief453ba02008-11-07 14:05:41 -080039
40struct drm_prop_enum_list {
41 int type;
42 char *name;
43};
44
45/* Avoid boilerplate. I'm tired of typing. */
46#define DRM_ENUM_NAME_FN(fnname, list) \
47 char *fnname(int val) \
48 { \
49 int i; \
50 for (i = 0; i < ARRAY_SIZE(list); i++) { \
51 if (list[i].type == val) \
52 return list[i].name; \
53 } \
54 return "(unknown)"; \
55 }
56
57/*
58 * Global properties
59 */
60static struct drm_prop_enum_list drm_dpms_enum_list[] =
61{ { DRM_MODE_DPMS_ON, "On" },
62 { DRM_MODE_DPMS_STANDBY, "Standby" },
63 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
64 { DRM_MODE_DPMS_OFF, "Off" }
65};
66
67DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
68
69/*
70 * Optional properties
71 */
72static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
73{
Jesse Barnes53bd8382009-07-01 10:04:40 -070074 { DRM_MODE_SCALE_NONE, "None" },
75 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
76 { DRM_MODE_SCALE_CENTER, "Center" },
77 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -080078};
79
80static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
81{
82 { DRM_MODE_DITHERING_OFF, "Off" },
83 { DRM_MODE_DITHERING_ON, "On" },
Ben Skeggs92897b52010-07-16 15:09:17 +100084 { DRM_MODE_DITHERING_AUTO, "Automatic" },
Dave Airlief453ba02008-11-07 14:05:41 -080085};
86
87/*
88 * Non-global properties, but "required" for certain connectors.
89 */
90static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
91{
92 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
93 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
94 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
95};
96
97DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
98
99static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
100{
101 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
102 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
103 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
104};
105
106DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
107 drm_dvi_i_subconnector_enum_list)
108
109static struct drm_prop_enum_list drm_tv_select_enum_list[] =
110{
111 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
112 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
113 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
114 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200115 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800116};
117
118DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
119
120static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
121{
122 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
123 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
124 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
125 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200126 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800127};
128
129DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
130 drm_tv_subconnector_enum_list)
131
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000132static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
133 { DRM_MODE_DIRTY_OFF, "Off" },
134 { DRM_MODE_DIRTY_ON, "On" },
135 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
136};
137
138DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
139 drm_dirty_info_enum_list)
140
Dave Airlief453ba02008-11-07 14:05:41 -0800141struct drm_conn_prop_enum_list {
142 int type;
143 char *name;
144 int count;
145};
146
147/*
148 * Connector and encoder types.
149 */
150static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
151{ { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
152 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
153 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
154 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
155 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
156 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
157 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
158 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
159 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500160 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
161 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
162 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
163 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
Francisco Jerez74bd3c22009-08-02 04:19:18 +0200164 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500165 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200166 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
Dave Airlief453ba02008-11-07 14:05:41 -0800167};
168
169static struct drm_prop_enum_list drm_encoder_enum_list[] =
170{ { DRM_MODE_ENCODER_NONE, "None" },
171 { DRM_MODE_ENCODER_DAC, "DAC" },
172 { DRM_MODE_ENCODER_TMDS, "TMDS" },
173 { DRM_MODE_ENCODER_LVDS, "LVDS" },
174 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200175 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Dave Airlief453ba02008-11-07 14:05:41 -0800176};
177
178char *drm_get_encoder_name(struct drm_encoder *encoder)
179{
180 static char buf[32];
181
182 snprintf(buf, 32, "%s-%d",
183 drm_encoder_enum_list[encoder->encoder_type].name,
184 encoder->base.id);
185 return buf;
186}
Dave Airlie13a81952009-09-07 15:45:33 +1000187EXPORT_SYMBOL(drm_get_encoder_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800188
189char *drm_get_connector_name(struct drm_connector *connector)
190{
191 static char buf[32];
192
193 snprintf(buf, 32, "%s-%d",
194 drm_connector_enum_list[connector->connector_type].name,
195 connector->connector_type_id);
196 return buf;
197}
198EXPORT_SYMBOL(drm_get_connector_name);
199
200char *drm_get_connector_status_name(enum drm_connector_status status)
201{
202 if (status == connector_status_connected)
203 return "connected";
204 else if (status == connector_status_disconnected)
205 return "disconnected";
206 else
207 return "unknown";
208}
209
210/**
211 * drm_mode_object_get - allocate a new identifier
212 * @dev: DRM device
213 * @ptr: object pointer, used to generate unique ID
214 * @type: object type
215 *
216 * LOCKING:
Dave Airlief453ba02008-11-07 14:05:41 -0800217 *
218 * Create a unique identifier based on @ptr in @dev's identifier space. Used
219 * for tracking modes, CRTCs and connectors.
220 *
221 * RETURNS:
222 * New unique (relative to other objects in @dev) integer identifier for the
223 * object.
224 */
225static int drm_mode_object_get(struct drm_device *dev,
226 struct drm_mode_object *obj, uint32_t obj_type)
227{
228 int new_id = 0;
229 int ret;
230
Dave Airlief453ba02008-11-07 14:05:41 -0800231again:
232 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
233 DRM_ERROR("Ran out memory getting a mode number\n");
234 return -EINVAL;
235 }
236
Jesse Barnesad2563c2009-01-19 17:21:45 +1000237 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800238 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000239 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800240 if (ret == -EAGAIN)
241 goto again;
242
243 obj->id = new_id;
244 obj->type = obj_type;
245 return 0;
246}
247
248/**
249 * drm_mode_object_put - free an identifer
250 * @dev: DRM device
251 * @id: ID to free
252 *
253 * LOCKING:
254 * Caller must hold DRM mode_config lock.
255 *
256 * Free @id from @dev's unique identifier pool.
257 */
258static void drm_mode_object_put(struct drm_device *dev,
259 struct drm_mode_object *object)
260{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000261 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800262 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000263 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800264}
265
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200266struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
267 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800268{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000269 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800270
Jesse Barnesad2563c2009-01-19 17:21:45 +1000271 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800272 obj = idr_find(&dev->mode_config.crtc_idr, id);
273 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000274 obj = NULL;
275 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800276
277 return obj;
278}
279EXPORT_SYMBOL(drm_mode_object_find);
280
281/**
Dave Airlief453ba02008-11-07 14:05:41 -0800282 * drm_framebuffer_init - initialize a framebuffer
283 * @dev: DRM device
284 *
285 * LOCKING:
286 * Caller must hold mode config lock.
287 *
288 * Allocates an ID for the framebuffer's parent mode object, sets its mode
289 * functions & device file and adds it to the master fd list.
290 *
291 * RETURNS:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200292 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800293 */
294int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
295 const struct drm_framebuffer_funcs *funcs)
296{
297 int ret;
298
299 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
300 if (ret) {
301 return ret;
302 }
303
304 fb->dev = dev;
305 fb->funcs = funcs;
306 dev->mode_config.num_fb++;
307 list_add(&fb->head, &dev->mode_config.fb_list);
308
309 return 0;
310}
311EXPORT_SYMBOL(drm_framebuffer_init);
312
313/**
314 * drm_framebuffer_cleanup - remove a framebuffer object
315 * @fb: framebuffer to remove
316 *
317 * LOCKING:
318 * Caller must hold mode config lock.
319 *
320 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
321 * it, setting it to NULL.
322 */
323void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
324{
325 struct drm_device *dev = fb->dev;
326 struct drm_crtc *crtc;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000327 struct drm_mode_set set;
328 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800329
330 /* remove from any CRTC */
331 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie5ef5f722009-08-17 13:11:23 +1000332 if (crtc->fb == fb) {
333 /* should turn off the crtc */
334 memset(&set, 0, sizeof(struct drm_mode_set));
335 set.crtc = crtc;
336 set.fb = NULL;
337 ret = crtc->funcs->set_config(&set);
338 if (ret)
339 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
340 }
Dave Airlief453ba02008-11-07 14:05:41 -0800341 }
342
343 drm_mode_object_put(dev, &fb->base);
344 list_del(&fb->head);
345 dev->mode_config.num_fb--;
346}
347EXPORT_SYMBOL(drm_framebuffer_cleanup);
348
349/**
350 * drm_crtc_init - Initialise a new CRTC object
351 * @dev: DRM device
352 * @crtc: CRTC object to init
353 * @funcs: callbacks for the new CRTC
354 *
355 * LOCKING:
356 * Caller must hold mode config lock.
357 *
358 * Inits a new object created as base part of an driver crtc object.
359 */
360void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
361 const struct drm_crtc_funcs *funcs)
362{
363 crtc->dev = dev;
364 crtc->funcs = funcs;
365
366 mutex_lock(&dev->mode_config.mutex);
367 drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
368
369 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
370 dev->mode_config.num_crtc++;
371 mutex_unlock(&dev->mode_config.mutex);
372}
373EXPORT_SYMBOL(drm_crtc_init);
374
375/**
376 * drm_crtc_cleanup - Cleans up the core crtc usage.
377 * @crtc: CRTC to cleanup
378 *
379 * LOCKING:
380 * Caller must hold mode config lock.
381 *
382 * Cleanup @crtc. Removes from drm modesetting space
383 * does NOT free object, caller does that.
384 */
385void drm_crtc_cleanup(struct drm_crtc *crtc)
386{
387 struct drm_device *dev = crtc->dev;
388
389 if (crtc->gamma_store) {
390 kfree(crtc->gamma_store);
391 crtc->gamma_store = NULL;
392 }
393
394 drm_mode_object_put(dev, &crtc->base);
395 list_del(&crtc->head);
396 dev->mode_config.num_crtc--;
397}
398EXPORT_SYMBOL(drm_crtc_cleanup);
399
400/**
401 * drm_mode_probed_add - add a mode to a connector's probed mode list
402 * @connector: connector the new mode
403 * @mode: mode data
404 *
405 * LOCKING:
406 * Caller must hold mode config lock.
407 *
408 * Add @mode to @connector's mode list for later use.
409 */
410void drm_mode_probed_add(struct drm_connector *connector,
411 struct drm_display_mode *mode)
412{
413 list_add(&mode->head, &connector->probed_modes);
414}
415EXPORT_SYMBOL(drm_mode_probed_add);
416
417/**
418 * drm_mode_remove - remove and free a mode
419 * @connector: connector list to modify
420 * @mode: mode to remove
421 *
422 * LOCKING:
423 * Caller must hold mode config lock.
424 *
425 * Remove @mode from @connector's mode list, then free it.
426 */
427void drm_mode_remove(struct drm_connector *connector,
428 struct drm_display_mode *mode)
429{
430 list_del(&mode->head);
431 kfree(mode);
432}
433EXPORT_SYMBOL(drm_mode_remove);
434
435/**
436 * drm_connector_init - Init a preallocated connector
437 * @dev: DRM device
438 * @connector: the connector to init
439 * @funcs: callbacks for this connector
440 * @name: user visible name of the connector
441 *
442 * LOCKING:
443 * Caller must hold @dev's mode_config lock.
444 *
445 * Initialises a preallocated connector. Connectors should be
446 * subclassed as part of driver connector objects.
447 */
448void drm_connector_init(struct drm_device *dev,
449 struct drm_connector *connector,
450 const struct drm_connector_funcs *funcs,
451 int connector_type)
452{
453 mutex_lock(&dev->mode_config.mutex);
454
455 connector->dev = dev;
456 connector->funcs = funcs;
457 drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
458 connector->connector_type = connector_type;
459 connector->connector_type_id =
460 ++drm_connector_enum_list[connector_type].count; /* TODO */
461 INIT_LIST_HEAD(&connector->user_modes);
462 INIT_LIST_HEAD(&connector->probed_modes);
463 INIT_LIST_HEAD(&connector->modes);
464 connector->edid_blob_ptr = NULL;
465
466 list_add_tail(&connector->head, &dev->mode_config.connector_list);
467 dev->mode_config.num_connector++;
468
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200469 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
470 drm_connector_attach_property(connector,
471 dev->mode_config.edid_property,
472 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800473
474 drm_connector_attach_property(connector,
475 dev->mode_config.dpms_property, 0);
476
477 mutex_unlock(&dev->mode_config.mutex);
478}
479EXPORT_SYMBOL(drm_connector_init);
480
481/**
482 * drm_connector_cleanup - cleans up an initialised connector
483 * @connector: connector to cleanup
484 *
485 * LOCKING:
486 * Caller must hold @dev's mode_config lock.
487 *
488 * Cleans up the connector but doesn't free the object.
489 */
490void drm_connector_cleanup(struct drm_connector *connector)
491{
492 struct drm_device *dev = connector->dev;
493 struct drm_display_mode *mode, *t;
494
495 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
496 drm_mode_remove(connector, mode);
497
498 list_for_each_entry_safe(mode, t, &connector->modes, head)
499 drm_mode_remove(connector, mode);
500
501 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
502 drm_mode_remove(connector, mode);
503
504 mutex_lock(&dev->mode_config.mutex);
505 drm_mode_object_put(dev, &connector->base);
506 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000507 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800508 mutex_unlock(&dev->mode_config.mutex);
509}
510EXPORT_SYMBOL(drm_connector_cleanup);
511
512void drm_encoder_init(struct drm_device *dev,
513 struct drm_encoder *encoder,
514 const struct drm_encoder_funcs *funcs,
515 int encoder_type)
516{
517 mutex_lock(&dev->mode_config.mutex);
518
519 encoder->dev = dev;
520
521 drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
522 encoder->encoder_type = encoder_type;
523 encoder->funcs = funcs;
524
525 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
526 dev->mode_config.num_encoder++;
527
528 mutex_unlock(&dev->mode_config.mutex);
529}
530EXPORT_SYMBOL(drm_encoder_init);
531
532void drm_encoder_cleanup(struct drm_encoder *encoder)
533{
534 struct drm_device *dev = encoder->dev;
535 mutex_lock(&dev->mode_config.mutex);
536 drm_mode_object_put(dev, &encoder->base);
537 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000538 dev->mode_config.num_encoder--;
Dave Airlief453ba02008-11-07 14:05:41 -0800539 mutex_unlock(&dev->mode_config.mutex);
540}
541EXPORT_SYMBOL(drm_encoder_cleanup);
542
543/**
544 * drm_mode_create - create a new display mode
545 * @dev: DRM device
546 *
547 * LOCKING:
548 * Caller must hold DRM mode_config lock.
549 *
550 * Create a new drm_display_mode, give it an ID, and return it.
551 *
552 * RETURNS:
553 * Pointer to new mode on success, NULL on error.
554 */
555struct drm_display_mode *drm_mode_create(struct drm_device *dev)
556{
557 struct drm_display_mode *nmode;
558
559 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
560 if (!nmode)
561 return NULL;
562
563 drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
564 return nmode;
565}
566EXPORT_SYMBOL(drm_mode_create);
567
568/**
569 * drm_mode_destroy - remove a mode
570 * @dev: DRM device
571 * @mode: mode to remove
572 *
573 * LOCKING:
574 * Caller must hold mode config lock.
575 *
576 * Free @mode's unique identifier, then free it.
577 */
578void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
579{
580 drm_mode_object_put(dev, &mode->base);
581
582 kfree(mode);
583}
584EXPORT_SYMBOL(drm_mode_destroy);
585
586static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
587{
588 struct drm_property *edid;
589 struct drm_property *dpms;
590 int i;
591
592 /*
593 * Standard properties (apply to all connectors)
594 */
595 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
596 DRM_MODE_PROP_IMMUTABLE,
597 "EDID", 0);
598 dev->mode_config.edid_property = edid;
599
600 dpms = drm_property_create(dev, DRM_MODE_PROP_ENUM,
601 "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
602 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
603 drm_property_add_enum(dpms, i, drm_dpms_enum_list[i].type,
604 drm_dpms_enum_list[i].name);
605 dev->mode_config.dpms_property = dpms;
606
607 return 0;
608}
609
610/**
611 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
612 * @dev: DRM device
613 *
614 * Called by a driver the first time a DVI-I connector is made.
615 */
616int drm_mode_create_dvi_i_properties(struct drm_device *dev)
617{
618 struct drm_property *dvi_i_selector;
619 struct drm_property *dvi_i_subconnector;
620 int i;
621
622 if (dev->mode_config.dvi_i_select_subconnector_property)
623 return 0;
624
625 dvi_i_selector =
626 drm_property_create(dev, DRM_MODE_PROP_ENUM,
627 "select subconnector",
628 ARRAY_SIZE(drm_dvi_i_select_enum_list));
629 for (i = 0; i < ARRAY_SIZE(drm_dvi_i_select_enum_list); i++)
630 drm_property_add_enum(dvi_i_selector, i,
631 drm_dvi_i_select_enum_list[i].type,
632 drm_dvi_i_select_enum_list[i].name);
633 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
634
635 dvi_i_subconnector =
636 drm_property_create(dev, DRM_MODE_PROP_ENUM |
637 DRM_MODE_PROP_IMMUTABLE,
638 "subconnector",
639 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
640 for (i = 0; i < ARRAY_SIZE(drm_dvi_i_subconnector_enum_list); i++)
641 drm_property_add_enum(dvi_i_subconnector, i,
642 drm_dvi_i_subconnector_enum_list[i].type,
643 drm_dvi_i_subconnector_enum_list[i].name);
644 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
645
646 return 0;
647}
648EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
649
650/**
651 * drm_create_tv_properties - create TV specific connector properties
652 * @dev: DRM device
653 * @num_modes: number of different TV formats (modes) supported
654 * @modes: array of pointers to strings containing name of each format
655 *
656 * Called by a driver's TV initialization routine, this function creates
657 * the TV specific connector properties for a given device. Caller is
658 * responsible for allocating a list of format names and passing them to
659 * this routine.
660 */
661int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
662 char *modes[])
663{
664 struct drm_property *tv_selector;
665 struct drm_property *tv_subconnector;
666 int i;
667
668 if (dev->mode_config.tv_select_subconnector_property)
669 return 0;
670
671 /*
672 * Basic connector properties
673 */
674 tv_selector = drm_property_create(dev, DRM_MODE_PROP_ENUM,
675 "select subconnector",
676 ARRAY_SIZE(drm_tv_select_enum_list));
677 for (i = 0; i < ARRAY_SIZE(drm_tv_select_enum_list); i++)
678 drm_property_add_enum(tv_selector, i,
679 drm_tv_select_enum_list[i].type,
680 drm_tv_select_enum_list[i].name);
681 dev->mode_config.tv_select_subconnector_property = tv_selector;
682
683 tv_subconnector =
684 drm_property_create(dev, DRM_MODE_PROP_ENUM |
685 DRM_MODE_PROP_IMMUTABLE, "subconnector",
686 ARRAY_SIZE(drm_tv_subconnector_enum_list));
687 for (i = 0; i < ARRAY_SIZE(drm_tv_subconnector_enum_list); i++)
688 drm_property_add_enum(tv_subconnector, i,
689 drm_tv_subconnector_enum_list[i].type,
690 drm_tv_subconnector_enum_list[i].name);
691 dev->mode_config.tv_subconnector_property = tv_subconnector;
692
693 /*
694 * Other, TV specific properties: margins & TV modes.
695 */
696 dev->mode_config.tv_left_margin_property =
697 drm_property_create(dev, DRM_MODE_PROP_RANGE,
698 "left margin", 2);
699 dev->mode_config.tv_left_margin_property->values[0] = 0;
700 dev->mode_config.tv_left_margin_property->values[1] = 100;
701
702 dev->mode_config.tv_right_margin_property =
703 drm_property_create(dev, DRM_MODE_PROP_RANGE,
704 "right margin", 2);
705 dev->mode_config.tv_right_margin_property->values[0] = 0;
706 dev->mode_config.tv_right_margin_property->values[1] = 100;
707
708 dev->mode_config.tv_top_margin_property =
709 drm_property_create(dev, DRM_MODE_PROP_RANGE,
710 "top margin", 2);
711 dev->mode_config.tv_top_margin_property->values[0] = 0;
712 dev->mode_config.tv_top_margin_property->values[1] = 100;
713
714 dev->mode_config.tv_bottom_margin_property =
715 drm_property_create(dev, DRM_MODE_PROP_RANGE,
716 "bottom margin", 2);
717 dev->mode_config.tv_bottom_margin_property->values[0] = 0;
718 dev->mode_config.tv_bottom_margin_property->values[1] = 100;
719
720 dev->mode_config.tv_mode_property =
721 drm_property_create(dev, DRM_MODE_PROP_ENUM,
722 "mode", num_modes);
723 for (i = 0; i < num_modes; i++)
724 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
725 i, modes[i]);
726
Francisco Jerezb6b79022009-08-02 04:19:20 +0200727 dev->mode_config.tv_brightness_property =
728 drm_property_create(dev, DRM_MODE_PROP_RANGE,
729 "brightness", 2);
730 dev->mode_config.tv_brightness_property->values[0] = 0;
731 dev->mode_config.tv_brightness_property->values[1] = 100;
732
733 dev->mode_config.tv_contrast_property =
734 drm_property_create(dev, DRM_MODE_PROP_RANGE,
735 "contrast", 2);
736 dev->mode_config.tv_contrast_property->values[0] = 0;
737 dev->mode_config.tv_contrast_property->values[1] = 100;
738
739 dev->mode_config.tv_flicker_reduction_property =
740 drm_property_create(dev, DRM_MODE_PROP_RANGE,
741 "flicker reduction", 2);
742 dev->mode_config.tv_flicker_reduction_property->values[0] = 0;
743 dev->mode_config.tv_flicker_reduction_property->values[1] = 100;
744
Francisco Jereza75f0232009-08-12 02:30:10 +0200745 dev->mode_config.tv_overscan_property =
746 drm_property_create(dev, DRM_MODE_PROP_RANGE,
747 "overscan", 2);
748 dev->mode_config.tv_overscan_property->values[0] = 0;
749 dev->mode_config.tv_overscan_property->values[1] = 100;
750
751 dev->mode_config.tv_saturation_property =
752 drm_property_create(dev, DRM_MODE_PROP_RANGE,
753 "saturation", 2);
754 dev->mode_config.tv_saturation_property->values[0] = 0;
755 dev->mode_config.tv_saturation_property->values[1] = 100;
756
757 dev->mode_config.tv_hue_property =
758 drm_property_create(dev, DRM_MODE_PROP_RANGE,
759 "hue", 2);
760 dev->mode_config.tv_hue_property->values[0] = 0;
761 dev->mode_config.tv_hue_property->values[1] = 100;
762
Dave Airlief453ba02008-11-07 14:05:41 -0800763 return 0;
764}
765EXPORT_SYMBOL(drm_mode_create_tv_properties);
766
767/**
768 * drm_mode_create_scaling_mode_property - create scaling mode property
769 * @dev: DRM device
770 *
771 * Called by a driver the first time it's needed, must be attached to desired
772 * connectors.
773 */
774int drm_mode_create_scaling_mode_property(struct drm_device *dev)
775{
776 struct drm_property *scaling_mode;
777 int i;
778
779 if (dev->mode_config.scaling_mode_property)
780 return 0;
781
782 scaling_mode =
783 drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
784 ARRAY_SIZE(drm_scaling_mode_enum_list));
785 for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++)
786 drm_property_add_enum(scaling_mode, i,
787 drm_scaling_mode_enum_list[i].type,
788 drm_scaling_mode_enum_list[i].name);
789
790 dev->mode_config.scaling_mode_property = scaling_mode;
791
792 return 0;
793}
794EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
795
796/**
797 * drm_mode_create_dithering_property - create dithering property
798 * @dev: DRM device
799 *
800 * Called by a driver the first time it's needed, must be attached to desired
801 * connectors.
802 */
803int drm_mode_create_dithering_property(struct drm_device *dev)
804{
805 struct drm_property *dithering_mode;
806 int i;
807
808 if (dev->mode_config.dithering_mode_property)
809 return 0;
810
811 dithering_mode =
812 drm_property_create(dev, DRM_MODE_PROP_ENUM, "dithering",
813 ARRAY_SIZE(drm_dithering_mode_enum_list));
814 for (i = 0; i < ARRAY_SIZE(drm_dithering_mode_enum_list); i++)
815 drm_property_add_enum(dithering_mode, i,
816 drm_dithering_mode_enum_list[i].type,
817 drm_dithering_mode_enum_list[i].name);
818 dev->mode_config.dithering_mode_property = dithering_mode;
819
820 return 0;
821}
822EXPORT_SYMBOL(drm_mode_create_dithering_property);
823
824/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000825 * drm_mode_create_dirty_property - create dirty property
826 * @dev: DRM device
827 *
828 * Called by a driver the first time it's needed, must be attached to desired
829 * connectors.
830 */
831int drm_mode_create_dirty_info_property(struct drm_device *dev)
832{
833 struct drm_property *dirty_info;
834 int i;
835
836 if (dev->mode_config.dirty_info_property)
837 return 0;
838
839 dirty_info =
840 drm_property_create(dev, DRM_MODE_PROP_ENUM |
841 DRM_MODE_PROP_IMMUTABLE,
842 "dirty",
843 ARRAY_SIZE(drm_dirty_info_enum_list));
844 for (i = 0; i < ARRAY_SIZE(drm_dirty_info_enum_list); i++)
845 drm_property_add_enum(dirty_info, i,
846 drm_dirty_info_enum_list[i].type,
847 drm_dirty_info_enum_list[i].name);
848 dev->mode_config.dirty_info_property = dirty_info;
849
850 return 0;
851}
852EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
853
854/**
Dave Airlief453ba02008-11-07 14:05:41 -0800855 * drm_mode_config_init - initialize DRM mode_configuration structure
856 * @dev: DRM device
857 *
858 * LOCKING:
859 * None, should happen single threaded at init time.
860 *
861 * Initialize @dev's mode_config structure, used for tracking the graphics
862 * configuration of @dev.
863 */
864void drm_mode_config_init(struct drm_device *dev)
865{
866 mutex_init(&dev->mode_config.mutex);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000867 mutex_init(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800868 INIT_LIST_HEAD(&dev->mode_config.fb_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800869 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
870 INIT_LIST_HEAD(&dev->mode_config.connector_list);
871 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
872 INIT_LIST_HEAD(&dev->mode_config.property_list);
873 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
874 idr_init(&dev->mode_config.crtc_idr);
875
876 mutex_lock(&dev->mode_config.mutex);
877 drm_mode_create_standard_connector_properties(dev);
878 mutex_unlock(&dev->mode_config.mutex);
879
880 /* Just to be sure */
881 dev->mode_config.num_fb = 0;
882 dev->mode_config.num_connector = 0;
883 dev->mode_config.num_crtc = 0;
884 dev->mode_config.num_encoder = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800885}
886EXPORT_SYMBOL(drm_mode_config_init);
887
888int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
889{
890 uint32_t total_objects = 0;
891
892 total_objects += dev->mode_config.num_crtc;
893 total_objects += dev->mode_config.num_connector;
894 total_objects += dev->mode_config.num_encoder;
895
Dave Airlief453ba02008-11-07 14:05:41 -0800896 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
897 if (!group->id_list)
898 return -ENOMEM;
899
900 group->num_crtcs = 0;
901 group->num_connectors = 0;
902 group->num_encoders = 0;
903 return 0;
904}
905
906int drm_mode_group_init_legacy_group(struct drm_device *dev,
907 struct drm_mode_group *group)
908{
909 struct drm_crtc *crtc;
910 struct drm_encoder *encoder;
911 struct drm_connector *connector;
912 int ret;
913
914 if ((ret = drm_mode_group_init(dev, group)))
915 return ret;
916
917 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
918 group->id_list[group->num_crtcs++] = crtc->base.id;
919
920 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
921 group->id_list[group->num_crtcs + group->num_encoders++] =
922 encoder->base.id;
923
924 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
925 group->id_list[group->num_crtcs + group->num_encoders +
926 group->num_connectors++] = connector->base.id;
927
928 return 0;
929}
930
931/**
932 * drm_mode_config_cleanup - free up DRM mode_config info
933 * @dev: DRM device
934 *
935 * LOCKING:
936 * Caller must hold mode config lock.
937 *
938 * Free up all the connectors and CRTCs associated with this DRM device, then
939 * free up the framebuffers and associated buffer objects.
940 *
941 * FIXME: cleanup any dangling user buffer objects too
942 */
943void drm_mode_config_cleanup(struct drm_device *dev)
944{
945 struct drm_connector *connector, *ot;
946 struct drm_crtc *crtc, *ct;
947 struct drm_encoder *encoder, *enct;
948 struct drm_framebuffer *fb, *fbt;
949 struct drm_property *property, *pt;
950
951 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
952 head) {
953 encoder->funcs->destroy(encoder);
954 }
955
956 list_for_each_entry_safe(connector, ot,
957 &dev->mode_config.connector_list, head) {
958 connector->funcs->destroy(connector);
959 }
960
961 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
962 head) {
963 drm_property_destroy(dev, property);
964 }
965
966 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
967 fb->funcs->destroy(fb);
968 }
969
970 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
971 crtc->funcs->destroy(crtc);
972 }
973
974}
975EXPORT_SYMBOL(drm_mode_config_cleanup);
976
Dave Airlief453ba02008-11-07 14:05:41 -0800977/**
978 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
979 * @out: drm_mode_modeinfo struct to return to the user
980 * @in: drm_display_mode to use
981 *
982 * LOCKING:
983 * None.
984 *
985 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
986 * the user.
987 */
988void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
989 struct drm_display_mode *in)
990{
991 out->clock = in->clock;
992 out->hdisplay = in->hdisplay;
993 out->hsync_start = in->hsync_start;
994 out->hsync_end = in->hsync_end;
995 out->htotal = in->htotal;
996 out->hskew = in->hskew;
997 out->vdisplay = in->vdisplay;
998 out->vsync_start = in->vsync_start;
999 out->vsync_end = in->vsync_end;
1000 out->vtotal = in->vtotal;
1001 out->vscan = in->vscan;
1002 out->vrefresh = in->vrefresh;
1003 out->flags = in->flags;
1004 out->type = in->type;
1005 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1006 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1007}
1008
1009/**
1010 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1011 * @out: drm_display_mode to return to the user
1012 * @in: drm_mode_modeinfo to use
1013 *
1014 * LOCKING:
1015 * None.
1016 *
1017 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1018 * the caller.
1019 */
1020void drm_crtc_convert_umode(struct drm_display_mode *out,
1021 struct drm_mode_modeinfo *in)
1022{
1023 out->clock = in->clock;
1024 out->hdisplay = in->hdisplay;
1025 out->hsync_start = in->hsync_start;
1026 out->hsync_end = in->hsync_end;
1027 out->htotal = in->htotal;
1028 out->hskew = in->hskew;
1029 out->vdisplay = in->vdisplay;
1030 out->vsync_start = in->vsync_start;
1031 out->vsync_end = in->vsync_end;
1032 out->vtotal = in->vtotal;
1033 out->vscan = in->vscan;
1034 out->vrefresh = in->vrefresh;
1035 out->flags = in->flags;
1036 out->type = in->type;
1037 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1038 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1039}
1040
1041/**
1042 * drm_mode_getresources - get graphics configuration
1043 * @inode: inode from the ioctl
1044 * @filp: file * from the ioctl
1045 * @cmd: cmd from ioctl
1046 * @arg: arg from ioctl
1047 *
1048 * LOCKING:
1049 * Takes mode config lock.
1050 *
1051 * Construct a set of configuration description structures and return
1052 * them to the user, including CRTC, connector and framebuffer configuration.
1053 *
1054 * Called by the user via ioctl.
1055 *
1056 * RETURNS:
1057 * Zero on success, errno on failure.
1058 */
1059int drm_mode_getresources(struct drm_device *dev, void *data,
1060 struct drm_file *file_priv)
1061{
1062 struct drm_mode_card_res *card_res = data;
1063 struct list_head *lh;
1064 struct drm_framebuffer *fb;
1065 struct drm_connector *connector;
1066 struct drm_crtc *crtc;
1067 struct drm_encoder *encoder;
1068 int ret = 0;
1069 int connector_count = 0;
1070 int crtc_count = 0;
1071 int fb_count = 0;
1072 int encoder_count = 0;
1073 int copied = 0, i;
1074 uint32_t __user *fb_id;
1075 uint32_t __user *crtc_id;
1076 uint32_t __user *connector_id;
1077 uint32_t __user *encoder_id;
1078 struct drm_mode_group *mode_group;
1079
Dave Airliefb3b06c2011-02-08 13:55:21 +10001080 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1081 return -EINVAL;
1082
Dave Airlief453ba02008-11-07 14:05:41 -08001083 mutex_lock(&dev->mode_config.mutex);
1084
1085 /*
1086 * For the non-control nodes we need to limit the list of resources
1087 * by IDs in the group list for this node
1088 */
1089 list_for_each(lh, &file_priv->fbs)
1090 fb_count++;
1091
1092 mode_group = &file_priv->master->minor->mode_group;
1093 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1094
1095 list_for_each(lh, &dev->mode_config.crtc_list)
1096 crtc_count++;
1097
1098 list_for_each(lh, &dev->mode_config.connector_list)
1099 connector_count++;
1100
1101 list_for_each(lh, &dev->mode_config.encoder_list)
1102 encoder_count++;
1103 } else {
1104
1105 crtc_count = mode_group->num_crtcs;
1106 connector_count = mode_group->num_connectors;
1107 encoder_count = mode_group->num_encoders;
1108 }
1109
1110 card_res->max_height = dev->mode_config.max_height;
1111 card_res->min_height = dev->mode_config.min_height;
1112 card_res->max_width = dev->mode_config.max_width;
1113 card_res->min_width = dev->mode_config.min_width;
1114
1115 /* handle this in 4 parts */
1116 /* FBs */
1117 if (card_res->count_fbs >= fb_count) {
1118 copied = 0;
1119 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
Sascha Hauer618c75e2011-06-03 12:54:14 +02001120 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
Dave Airlief453ba02008-11-07 14:05:41 -08001121 if (put_user(fb->base.id, fb_id + copied)) {
1122 ret = -EFAULT;
1123 goto out;
1124 }
1125 copied++;
1126 }
1127 }
1128 card_res->count_fbs = fb_count;
1129
1130 /* CRTCs */
1131 if (card_res->count_crtcs >= crtc_count) {
1132 copied = 0;
1133 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1134 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1135 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1136 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001137 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001138 if (put_user(crtc->base.id, crtc_id + copied)) {
1139 ret = -EFAULT;
1140 goto out;
1141 }
1142 copied++;
1143 }
1144 } else {
1145 for (i = 0; i < mode_group->num_crtcs; i++) {
1146 if (put_user(mode_group->id_list[i],
1147 crtc_id + copied)) {
1148 ret = -EFAULT;
1149 goto out;
1150 }
1151 copied++;
1152 }
1153 }
1154 }
1155 card_res->count_crtcs = crtc_count;
1156
1157 /* Encoders */
1158 if (card_res->count_encoders >= encoder_count) {
1159 copied = 0;
1160 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1161 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1162 list_for_each_entry(encoder,
1163 &dev->mode_config.encoder_list,
1164 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001165 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1166 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001167 if (put_user(encoder->base.id, encoder_id +
1168 copied)) {
1169 ret = -EFAULT;
1170 goto out;
1171 }
1172 copied++;
1173 }
1174 } else {
1175 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1176 if (put_user(mode_group->id_list[i],
1177 encoder_id + copied)) {
1178 ret = -EFAULT;
1179 goto out;
1180 }
1181 copied++;
1182 }
1183
1184 }
1185 }
1186 card_res->count_encoders = encoder_count;
1187
1188 /* Connectors */
1189 if (card_res->count_connectors >= connector_count) {
1190 copied = 0;
1191 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1192 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1193 list_for_each_entry(connector,
1194 &dev->mode_config.connector_list,
1195 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001196 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1197 connector->base.id,
1198 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001199 if (put_user(connector->base.id,
1200 connector_id + copied)) {
1201 ret = -EFAULT;
1202 goto out;
1203 }
1204 copied++;
1205 }
1206 } else {
1207 int start = mode_group->num_crtcs +
1208 mode_group->num_encoders;
1209 for (i = start; i < start + mode_group->num_connectors; i++) {
1210 if (put_user(mode_group->id_list[i],
1211 connector_id + copied)) {
1212 ret = -EFAULT;
1213 goto out;
1214 }
1215 copied++;
1216 }
1217 }
1218 }
1219 card_res->count_connectors = connector_count;
1220
Jerome Glisse94401062010-07-15 15:43:25 -04001221 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001222 card_res->count_connectors, card_res->count_encoders);
1223
1224out:
1225 mutex_unlock(&dev->mode_config.mutex);
1226 return ret;
1227}
1228
1229/**
1230 * drm_mode_getcrtc - get CRTC configuration
1231 * @inode: inode from the ioctl
1232 * @filp: file * from the ioctl
1233 * @cmd: cmd from ioctl
1234 * @arg: arg from ioctl
1235 *
1236 * LOCKING:
1237 * Caller? (FIXME)
1238 *
1239 * Construct a CRTC configuration structure to return to the user.
1240 *
1241 * Called by the user via ioctl.
1242 *
1243 * RETURNS:
1244 * Zero on success, errno on failure.
1245 */
1246int drm_mode_getcrtc(struct drm_device *dev,
1247 void *data, struct drm_file *file_priv)
1248{
1249 struct drm_mode_crtc *crtc_resp = data;
1250 struct drm_crtc *crtc;
1251 struct drm_mode_object *obj;
1252 int ret = 0;
1253
Dave Airliefb3b06c2011-02-08 13:55:21 +10001254 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1255 return -EINVAL;
1256
Dave Airlief453ba02008-11-07 14:05:41 -08001257 mutex_lock(&dev->mode_config.mutex);
1258
1259 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1260 DRM_MODE_OBJECT_CRTC);
1261 if (!obj) {
1262 ret = -EINVAL;
1263 goto out;
1264 }
1265 crtc = obj_to_crtc(obj);
1266
1267 crtc_resp->x = crtc->x;
1268 crtc_resp->y = crtc->y;
1269 crtc_resp->gamma_size = crtc->gamma_size;
1270 if (crtc->fb)
1271 crtc_resp->fb_id = crtc->fb->base.id;
1272 else
1273 crtc_resp->fb_id = 0;
1274
1275 if (crtc->enabled) {
1276
1277 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1278 crtc_resp->mode_valid = 1;
1279
1280 } else {
1281 crtc_resp->mode_valid = 0;
1282 }
1283
1284out:
1285 mutex_unlock(&dev->mode_config.mutex);
1286 return ret;
1287}
1288
1289/**
1290 * drm_mode_getconnector - get connector configuration
1291 * @inode: inode from the ioctl
1292 * @filp: file * from the ioctl
1293 * @cmd: cmd from ioctl
1294 * @arg: arg from ioctl
1295 *
1296 * LOCKING:
1297 * Caller? (FIXME)
1298 *
1299 * Construct a connector configuration structure to return to the user.
1300 *
1301 * Called by the user via ioctl.
1302 *
1303 * RETURNS:
1304 * Zero on success, errno on failure.
1305 */
1306int drm_mode_getconnector(struct drm_device *dev, void *data,
1307 struct drm_file *file_priv)
1308{
1309 struct drm_mode_get_connector *out_resp = data;
1310 struct drm_mode_object *obj;
1311 struct drm_connector *connector;
1312 struct drm_display_mode *mode;
1313 int mode_count = 0;
1314 int props_count = 0;
1315 int encoders_count = 0;
1316 int ret = 0;
1317 int copied = 0;
1318 int i;
1319 struct drm_mode_modeinfo u_mode;
1320 struct drm_mode_modeinfo __user *mode_ptr;
1321 uint32_t __user *prop_ptr;
1322 uint64_t __user *prop_values;
1323 uint32_t __user *encoder_ptr;
1324
Dave Airliefb3b06c2011-02-08 13:55:21 +10001325 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1326 return -EINVAL;
1327
Dave Airlief453ba02008-11-07 14:05:41 -08001328 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1329
Jerome Glisse94401062010-07-15 15:43:25 -04001330 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001331
1332 mutex_lock(&dev->mode_config.mutex);
1333
1334 obj = drm_mode_object_find(dev, out_resp->connector_id,
1335 DRM_MODE_OBJECT_CONNECTOR);
1336 if (!obj) {
1337 ret = -EINVAL;
1338 goto out;
1339 }
1340 connector = obj_to_connector(obj);
1341
1342 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1343 if (connector->property_ids[i] != 0) {
1344 props_count++;
1345 }
1346 }
1347
1348 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1349 if (connector->encoder_ids[i] != 0) {
1350 encoders_count++;
1351 }
1352 }
1353
1354 if (out_resp->count_modes == 0) {
1355 connector->funcs->fill_modes(connector,
1356 dev->mode_config.max_width,
1357 dev->mode_config.max_height);
1358 }
1359
1360 /* delayed so we get modes regardless of pre-fill_modes state */
1361 list_for_each_entry(mode, &connector->modes, head)
1362 mode_count++;
1363
1364 out_resp->connector_id = connector->base.id;
1365 out_resp->connector_type = connector->connector_type;
1366 out_resp->connector_type_id = connector->connector_type_id;
1367 out_resp->mm_width = connector->display_info.width_mm;
1368 out_resp->mm_height = connector->display_info.height_mm;
1369 out_resp->subpixel = connector->display_info.subpixel_order;
1370 out_resp->connection = connector->status;
1371 if (connector->encoder)
1372 out_resp->encoder_id = connector->encoder->base.id;
1373 else
1374 out_resp->encoder_id = 0;
1375
1376 /*
1377 * This ioctl is called twice, once to determine how much space is
1378 * needed, and the 2nd time to fill it.
1379 */
1380 if ((out_resp->count_modes >= mode_count) && mode_count) {
1381 copied = 0;
1382 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1383 list_for_each_entry(mode, &connector->modes, head) {
1384 drm_crtc_convert_to_umode(&u_mode, mode);
1385 if (copy_to_user(mode_ptr + copied,
1386 &u_mode, sizeof(u_mode))) {
1387 ret = -EFAULT;
1388 goto out;
1389 }
1390 copied++;
1391 }
1392 }
1393 out_resp->count_modes = mode_count;
1394
1395 if ((out_resp->count_props >= props_count) && props_count) {
1396 copied = 0;
1397 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1398 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1399 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1400 if (connector->property_ids[i] != 0) {
1401 if (put_user(connector->property_ids[i],
1402 prop_ptr + copied)) {
1403 ret = -EFAULT;
1404 goto out;
1405 }
1406
1407 if (put_user(connector->property_values[i],
1408 prop_values + copied)) {
1409 ret = -EFAULT;
1410 goto out;
1411 }
1412 copied++;
1413 }
1414 }
1415 }
1416 out_resp->count_props = props_count;
1417
1418 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1419 copied = 0;
1420 encoder_ptr = (uint32_t *)(unsigned long)(out_resp->encoders_ptr);
1421 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1422 if (connector->encoder_ids[i] != 0) {
1423 if (put_user(connector->encoder_ids[i],
1424 encoder_ptr + copied)) {
1425 ret = -EFAULT;
1426 goto out;
1427 }
1428 copied++;
1429 }
1430 }
1431 }
1432 out_resp->count_encoders = encoders_count;
1433
1434out:
1435 mutex_unlock(&dev->mode_config.mutex);
1436 return ret;
1437}
1438
1439int drm_mode_getencoder(struct drm_device *dev, void *data,
1440 struct drm_file *file_priv)
1441{
1442 struct drm_mode_get_encoder *enc_resp = data;
1443 struct drm_mode_object *obj;
1444 struct drm_encoder *encoder;
1445 int ret = 0;
1446
Dave Airliefb3b06c2011-02-08 13:55:21 +10001447 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1448 return -EINVAL;
1449
Dave Airlief453ba02008-11-07 14:05:41 -08001450 mutex_lock(&dev->mode_config.mutex);
1451 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1452 DRM_MODE_OBJECT_ENCODER);
1453 if (!obj) {
1454 ret = -EINVAL;
1455 goto out;
1456 }
1457 encoder = obj_to_encoder(obj);
1458
1459 if (encoder->crtc)
1460 enc_resp->crtc_id = encoder->crtc->base.id;
1461 else
1462 enc_resp->crtc_id = 0;
1463 enc_resp->encoder_type = encoder->encoder_type;
1464 enc_resp->encoder_id = encoder->base.id;
1465 enc_resp->possible_crtcs = encoder->possible_crtcs;
1466 enc_resp->possible_clones = encoder->possible_clones;
1467
1468out:
1469 mutex_unlock(&dev->mode_config.mutex);
1470 return ret;
1471}
1472
1473/**
1474 * drm_mode_setcrtc - set CRTC configuration
1475 * @inode: inode from the ioctl
1476 * @filp: file * from the ioctl
1477 * @cmd: cmd from ioctl
1478 * @arg: arg from ioctl
1479 *
1480 * LOCKING:
1481 * Caller? (FIXME)
1482 *
1483 * Build a new CRTC configuration based on user request.
1484 *
1485 * Called by the user via ioctl.
1486 *
1487 * RETURNS:
1488 * Zero on success, errno on failure.
1489 */
1490int drm_mode_setcrtc(struct drm_device *dev, void *data,
1491 struct drm_file *file_priv)
1492{
1493 struct drm_mode_config *config = &dev->mode_config;
1494 struct drm_mode_crtc *crtc_req = data;
1495 struct drm_mode_object *obj;
1496 struct drm_crtc *crtc, *crtcfb;
1497 struct drm_connector **connector_set = NULL, *connector;
1498 struct drm_framebuffer *fb = NULL;
1499 struct drm_display_mode *mode = NULL;
1500 struct drm_mode_set set;
1501 uint32_t __user *set_connectors_ptr;
1502 int ret = 0;
1503 int i;
1504
Dave Airliefb3b06c2011-02-08 13:55:21 +10001505 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1506 return -EINVAL;
1507
Dave Airlief453ba02008-11-07 14:05:41 -08001508 mutex_lock(&dev->mode_config.mutex);
1509 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1510 DRM_MODE_OBJECT_CRTC);
1511 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001512 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001513 ret = -EINVAL;
1514 goto out;
1515 }
1516 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001517 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001518
1519 if (crtc_req->mode_valid) {
1520 /* If we have a mode we need a framebuffer. */
1521 /* If we pass -1, set the mode with the currently bound fb */
1522 if (crtc_req->fb_id == -1) {
1523 list_for_each_entry(crtcfb,
1524 &dev->mode_config.crtc_list, head) {
1525 if (crtcfb == crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001526 DRM_DEBUG_KMS("Using current fb for "
1527 "setmode\n");
Dave Airlief453ba02008-11-07 14:05:41 -08001528 fb = crtc->fb;
1529 }
1530 }
1531 } else {
1532 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1533 DRM_MODE_OBJECT_FB);
1534 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001535 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1536 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001537 ret = -EINVAL;
1538 goto out;
1539 }
1540 fb = obj_to_fb(obj);
1541 }
1542
1543 mode = drm_mode_create(dev);
1544 drm_crtc_convert_umode(mode, &crtc_req->mode);
1545 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1546 }
1547
1548 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001549 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08001550 ret = -EINVAL;
1551 goto out;
1552 }
1553
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01001554 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001555 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08001556 crtc_req->count_connectors);
1557 ret = -EINVAL;
1558 goto out;
1559 }
1560
1561 if (crtc_req->count_connectors > 0) {
1562 u32 out_id;
1563
1564 /* Avoid unbounded kernel memory allocation */
1565 if (crtc_req->count_connectors > config->num_connector) {
1566 ret = -EINVAL;
1567 goto out;
1568 }
1569
1570 connector_set = kmalloc(crtc_req->count_connectors *
1571 sizeof(struct drm_connector *),
1572 GFP_KERNEL);
1573 if (!connector_set) {
1574 ret = -ENOMEM;
1575 goto out;
1576 }
1577
1578 for (i = 0; i < crtc_req->count_connectors; i++) {
1579 set_connectors_ptr = (uint32_t *)(unsigned long)crtc_req->set_connectors_ptr;
1580 if (get_user(out_id, &set_connectors_ptr[i])) {
1581 ret = -EFAULT;
1582 goto out;
1583 }
1584
1585 obj = drm_mode_object_find(dev, out_id,
1586 DRM_MODE_OBJECT_CONNECTOR);
1587 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001588 DRM_DEBUG_KMS("Connector id %d unknown\n",
1589 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001590 ret = -EINVAL;
1591 goto out;
1592 }
1593 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001594 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1595 connector->base.id,
1596 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001597
1598 connector_set[i] = connector;
1599 }
1600 }
1601
1602 set.crtc = crtc;
1603 set.x = crtc_req->x;
1604 set.y = crtc_req->y;
1605 set.mode = mode;
1606 set.connectors = connector_set;
1607 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10001608 set.fb = fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001609 ret = crtc->funcs->set_config(&set);
1610
1611out:
1612 kfree(connector_set);
1613 mutex_unlock(&dev->mode_config.mutex);
1614 return ret;
1615}
1616
1617int drm_mode_cursor_ioctl(struct drm_device *dev,
1618 void *data, struct drm_file *file_priv)
1619{
1620 struct drm_mode_cursor *req = data;
1621 struct drm_mode_object *obj;
1622 struct drm_crtc *crtc;
1623 int ret = 0;
1624
Dave Airliefb3b06c2011-02-08 13:55:21 +10001625 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1626 return -EINVAL;
1627
Dave Airlief453ba02008-11-07 14:05:41 -08001628 if (!req->flags) {
1629 DRM_ERROR("no operation set\n");
1630 return -EINVAL;
1631 }
1632
1633 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10001634 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08001635 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001636 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001637 ret = -EINVAL;
1638 goto out;
1639 }
1640 crtc = obj_to_crtc(obj);
1641
1642 if (req->flags & DRM_MODE_CURSOR_BO) {
1643 if (!crtc->funcs->cursor_set) {
1644 DRM_ERROR("crtc does not support cursor\n");
1645 ret = -ENXIO;
1646 goto out;
1647 }
1648 /* Turns off the cursor if handle is 0 */
1649 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1650 req->width, req->height);
1651 }
1652
1653 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1654 if (crtc->funcs->cursor_move) {
1655 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1656 } else {
1657 DRM_ERROR("crtc does not support cursor\n");
1658 ret = -EFAULT;
1659 goto out;
1660 }
1661 }
1662out:
1663 mutex_unlock(&dev->mode_config.mutex);
1664 return ret;
1665}
1666
1667/**
1668 * drm_mode_addfb - add an FB to the graphics configuration
1669 * @inode: inode from the ioctl
1670 * @filp: file * from the ioctl
1671 * @cmd: cmd from ioctl
1672 * @arg: arg from ioctl
1673 *
1674 * LOCKING:
1675 * Takes mode config lock.
1676 *
1677 * Add a new FB to the specified CRTC, given a user request.
1678 *
1679 * Called by the user via ioctl.
1680 *
1681 * RETURNS:
1682 * Zero on success, errno on failure.
1683 */
1684int drm_mode_addfb(struct drm_device *dev,
1685 void *data, struct drm_file *file_priv)
1686{
1687 struct drm_mode_fb_cmd *r = data;
1688 struct drm_mode_config *config = &dev->mode_config;
1689 struct drm_framebuffer *fb;
1690 int ret = 0;
1691
Dave Airliefb3b06c2011-02-08 13:55:21 +10001692 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1693 return -EINVAL;
1694
Dave Airlief453ba02008-11-07 14:05:41 -08001695 if ((config->min_width > r->width) || (r->width > config->max_width)) {
1696 DRM_ERROR("mode new framebuffer width not within limits\n");
1697 return -EINVAL;
1698 }
1699 if ((config->min_height > r->height) || (r->height > config->max_height)) {
1700 DRM_ERROR("mode new framebuffer height not within limits\n");
1701 return -EINVAL;
1702 }
1703
1704 mutex_lock(&dev->mode_config.mutex);
1705
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001706 /* TODO check buffer is sufficiently large */
Dave Airlief453ba02008-11-07 14:05:41 -08001707 /* TODO setup destructor callback */
1708
1709 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001710 if (IS_ERR(fb)) {
Dave Airlief453ba02008-11-07 14:05:41 -08001711 DRM_ERROR("could not create framebuffer\n");
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001712 ret = PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08001713 goto out;
1714 }
1715
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10001716 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08001717 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04001718 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001719
1720out:
1721 mutex_unlock(&dev->mode_config.mutex);
1722 return ret;
1723}
1724
1725/**
1726 * drm_mode_rmfb - remove an FB from the configuration
1727 * @inode: inode from the ioctl
1728 * @filp: file * from the ioctl
1729 * @cmd: cmd from ioctl
1730 * @arg: arg from ioctl
1731 *
1732 * LOCKING:
1733 * Takes mode config lock.
1734 *
1735 * Remove the FB specified by the user.
1736 *
1737 * Called by the user via ioctl.
1738 *
1739 * RETURNS:
1740 * Zero on success, errno on failure.
1741 */
1742int drm_mode_rmfb(struct drm_device *dev,
1743 void *data, struct drm_file *file_priv)
1744{
1745 struct drm_mode_object *obj;
1746 struct drm_framebuffer *fb = NULL;
1747 struct drm_framebuffer *fbl = NULL;
1748 uint32_t *id = data;
1749 int ret = 0;
1750 int found = 0;
1751
Dave Airliefb3b06c2011-02-08 13:55:21 +10001752 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1753 return -EINVAL;
1754
Dave Airlief453ba02008-11-07 14:05:41 -08001755 mutex_lock(&dev->mode_config.mutex);
1756 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001757 /* TODO check that we really get a framebuffer back. */
Dave Airlief453ba02008-11-07 14:05:41 -08001758 if (!obj) {
1759 DRM_ERROR("mode invalid framebuffer id\n");
1760 ret = -EINVAL;
1761 goto out;
1762 }
1763 fb = obj_to_fb(obj);
1764
1765 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1766 if (fb == fbl)
1767 found = 1;
1768
1769 if (!found) {
1770 DRM_ERROR("tried to remove a fb that we didn't own\n");
1771 ret = -EINVAL;
1772 goto out;
1773 }
1774
1775 /* TODO release all crtc connected to the framebuffer */
1776 /* TODO unhock the destructor from the buffer object */
1777
1778 list_del(&fb->filp_head);
1779 fb->funcs->destroy(fb);
1780
1781out:
1782 mutex_unlock(&dev->mode_config.mutex);
1783 return ret;
1784}
1785
1786/**
1787 * drm_mode_getfb - get FB info
1788 * @inode: inode from the ioctl
1789 * @filp: file * from the ioctl
1790 * @cmd: cmd from ioctl
1791 * @arg: arg from ioctl
1792 *
1793 * LOCKING:
1794 * Caller? (FIXME)
1795 *
1796 * Lookup the FB given its ID and return info about it.
1797 *
1798 * Called by the user via ioctl.
1799 *
1800 * RETURNS:
1801 * Zero on success, errno on failure.
1802 */
1803int drm_mode_getfb(struct drm_device *dev,
1804 void *data, struct drm_file *file_priv)
1805{
1806 struct drm_mode_fb_cmd *r = data;
1807 struct drm_mode_object *obj;
1808 struct drm_framebuffer *fb;
1809 int ret = 0;
1810
Dave Airliefb3b06c2011-02-08 13:55:21 +10001811 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1812 return -EINVAL;
1813
Dave Airlief453ba02008-11-07 14:05:41 -08001814 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10001815 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
Dave Airlief453ba02008-11-07 14:05:41 -08001816 if (!obj) {
1817 DRM_ERROR("invalid framebuffer id\n");
1818 ret = -EINVAL;
1819 goto out;
1820 }
1821 fb = obj_to_fb(obj);
1822
1823 r->height = fb->height;
1824 r->width = fb->width;
1825 r->depth = fb->depth;
1826 r->bpp = fb->bits_per_pixel;
1827 r->pitch = fb->pitch;
1828 fb->funcs->create_handle(fb, file_priv, &r->handle);
1829
1830out:
1831 mutex_unlock(&dev->mode_config.mutex);
1832 return ret;
1833}
1834
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001835int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
1836 void *data, struct drm_file *file_priv)
1837{
1838 struct drm_clip_rect __user *clips_ptr;
1839 struct drm_clip_rect *clips = NULL;
1840 struct drm_mode_fb_dirty_cmd *r = data;
1841 struct drm_mode_object *obj;
1842 struct drm_framebuffer *fb;
1843 unsigned flags;
1844 int num_clips;
1845 int ret = 0;
1846
Dave Airliefb3b06c2011-02-08 13:55:21 +10001847 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1848 return -EINVAL;
1849
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001850 mutex_lock(&dev->mode_config.mutex);
1851 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
1852 if (!obj) {
1853 DRM_ERROR("invalid framebuffer id\n");
1854 ret = -EINVAL;
1855 goto out_err1;
1856 }
1857 fb = obj_to_fb(obj);
1858
1859 num_clips = r->num_clips;
1860 clips_ptr = (struct drm_clip_rect *)(unsigned long)r->clips_ptr;
1861
1862 if (!num_clips != !clips_ptr) {
1863 ret = -EINVAL;
1864 goto out_err1;
1865 }
1866
1867 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
1868
1869 /* If userspace annotates copy, clips must come in pairs */
1870 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
1871 ret = -EINVAL;
1872 goto out_err1;
1873 }
1874
1875 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05001876 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
1877 ret = -EINVAL;
1878 goto out_err1;
1879 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001880 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
1881 if (!clips) {
1882 ret = -ENOMEM;
1883 goto out_err1;
1884 }
1885
1886 ret = copy_from_user(clips, clips_ptr,
1887 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02001888 if (ret) {
1889 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001890 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02001891 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001892 }
1893
1894 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02001895 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
1896 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001897 } else {
1898 ret = -ENOSYS;
1899 goto out_err2;
1900 }
1901
1902out_err2:
1903 kfree(clips);
1904out_err1:
1905 mutex_unlock(&dev->mode_config.mutex);
1906 return ret;
1907}
1908
1909
Dave Airlief453ba02008-11-07 14:05:41 -08001910/**
1911 * drm_fb_release - remove and free the FBs on this file
1912 * @filp: file * from the ioctl
1913 *
1914 * LOCKING:
1915 * Takes mode config lock.
1916 *
1917 * Destroy all the FBs associated with @filp.
1918 *
1919 * Called by the user via ioctl.
1920 *
1921 * RETURNS:
1922 * Zero on success, errno on failure.
1923 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05001924void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08001925{
Dave Airlief453ba02008-11-07 14:05:41 -08001926 struct drm_device *dev = priv->minor->dev;
1927 struct drm_framebuffer *fb, *tfb;
1928
1929 mutex_lock(&dev->mode_config.mutex);
1930 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1931 list_del(&fb->filp_head);
1932 fb->funcs->destroy(fb);
1933 }
1934 mutex_unlock(&dev->mode_config.mutex);
1935}
1936
1937/**
1938 * drm_mode_attachmode - add a mode to the user mode list
1939 * @dev: DRM device
1940 * @connector: connector to add the mode to
1941 * @mode: mode to add
1942 *
1943 * Add @mode to @connector's user mode list.
1944 */
1945static int drm_mode_attachmode(struct drm_device *dev,
1946 struct drm_connector *connector,
1947 struct drm_display_mode *mode)
1948{
1949 int ret = 0;
1950
1951 list_add_tail(&mode->head, &connector->user_modes);
1952 return ret;
1953}
1954
1955int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1956 struct drm_display_mode *mode)
1957{
1958 struct drm_connector *connector;
1959 int ret = 0;
1960 struct drm_display_mode *dup_mode;
1961 int need_dup = 0;
1962 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1963 if (!connector->encoder)
1964 break;
1965 if (connector->encoder->crtc == crtc) {
1966 if (need_dup)
1967 dup_mode = drm_mode_duplicate(dev, mode);
1968 else
1969 dup_mode = mode;
1970 ret = drm_mode_attachmode(dev, connector, dup_mode);
1971 if (ret)
1972 return ret;
1973 need_dup = 1;
1974 }
1975 }
1976 return 0;
1977}
1978EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1979
1980static int drm_mode_detachmode(struct drm_device *dev,
1981 struct drm_connector *connector,
1982 struct drm_display_mode *mode)
1983{
1984 int found = 0;
1985 int ret = 0;
1986 struct drm_display_mode *match_mode, *t;
1987
1988 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
1989 if (drm_mode_equal(match_mode, mode)) {
1990 list_del(&match_mode->head);
1991 drm_mode_destroy(dev, match_mode);
1992 found = 1;
1993 break;
1994 }
1995 }
1996
1997 if (!found)
1998 ret = -EINVAL;
1999
2000 return ret;
2001}
2002
2003int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2004{
2005 struct drm_connector *connector;
2006
2007 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2008 drm_mode_detachmode(dev, connector, mode);
2009 }
2010 return 0;
2011}
2012EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2013
2014/**
2015 * drm_fb_attachmode - Attach a user mode to an connector
2016 * @inode: inode from the ioctl
2017 * @filp: file * from the ioctl
2018 * @cmd: cmd from ioctl
2019 * @arg: arg from ioctl
2020 *
2021 * This attaches a user specified mode to an connector.
2022 * Called by the user via ioctl.
2023 *
2024 * RETURNS:
2025 * Zero on success, errno on failure.
2026 */
2027int drm_mode_attachmode_ioctl(struct drm_device *dev,
2028 void *data, struct drm_file *file_priv)
2029{
2030 struct drm_mode_mode_cmd *mode_cmd = data;
2031 struct drm_connector *connector;
2032 struct drm_display_mode *mode;
2033 struct drm_mode_object *obj;
2034 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2035 int ret = 0;
2036
Dave Airliefb3b06c2011-02-08 13:55:21 +10002037 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2038 return -EINVAL;
2039
Dave Airlief453ba02008-11-07 14:05:41 -08002040 mutex_lock(&dev->mode_config.mutex);
2041
2042 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2043 if (!obj) {
2044 ret = -EINVAL;
2045 goto out;
2046 }
2047 connector = obj_to_connector(obj);
2048
2049 mode = drm_mode_create(dev);
2050 if (!mode) {
2051 ret = -ENOMEM;
2052 goto out;
2053 }
2054
2055 drm_crtc_convert_umode(mode, umode);
2056
2057 ret = drm_mode_attachmode(dev, connector, mode);
2058out:
2059 mutex_unlock(&dev->mode_config.mutex);
2060 return ret;
2061}
2062
2063
2064/**
2065 * drm_fb_detachmode - Detach a user specified mode from an connector
2066 * @inode: inode from the ioctl
2067 * @filp: file * from the ioctl
2068 * @cmd: cmd from ioctl
2069 * @arg: arg from ioctl
2070 *
2071 * Called by the user via ioctl.
2072 *
2073 * RETURNS:
2074 * Zero on success, errno on failure.
2075 */
2076int drm_mode_detachmode_ioctl(struct drm_device *dev,
2077 void *data, struct drm_file *file_priv)
2078{
2079 struct drm_mode_object *obj;
2080 struct drm_mode_mode_cmd *mode_cmd = data;
2081 struct drm_connector *connector;
2082 struct drm_display_mode mode;
2083 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2084 int ret = 0;
2085
Dave Airliefb3b06c2011-02-08 13:55:21 +10002086 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2087 return -EINVAL;
2088
Dave Airlief453ba02008-11-07 14:05:41 -08002089 mutex_lock(&dev->mode_config.mutex);
2090
2091 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2092 if (!obj) {
2093 ret = -EINVAL;
2094 goto out;
2095 }
2096 connector = obj_to_connector(obj);
2097
2098 drm_crtc_convert_umode(&mode, umode);
2099 ret = drm_mode_detachmode(dev, connector, &mode);
2100out:
2101 mutex_unlock(&dev->mode_config.mutex);
2102 return ret;
2103}
2104
2105struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2106 const char *name, int num_values)
2107{
2108 struct drm_property *property = NULL;
2109
2110 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2111 if (!property)
2112 return NULL;
2113
2114 if (num_values) {
2115 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2116 if (!property->values)
2117 goto fail;
2118 }
2119
2120 drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2121 property->flags = flags;
2122 property->num_values = num_values;
2123 INIT_LIST_HEAD(&property->enum_blob_list);
2124
Vinson Lee471dd2e2011-11-10 11:55:40 -08002125 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002126 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002127 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2128 }
Dave Airlief453ba02008-11-07 14:05:41 -08002129
2130 list_add_tail(&property->head, &dev->mode_config.property_list);
2131 return property;
2132fail:
2133 kfree(property);
2134 return NULL;
2135}
2136EXPORT_SYMBOL(drm_property_create);
2137
2138int drm_property_add_enum(struct drm_property *property, int index,
2139 uint64_t value, const char *name)
2140{
2141 struct drm_property_enum *prop_enum;
2142
2143 if (!(property->flags & DRM_MODE_PROP_ENUM))
2144 return -EINVAL;
2145
2146 if (!list_empty(&property->enum_blob_list)) {
2147 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2148 if (prop_enum->value == value) {
2149 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2150 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2151 return 0;
2152 }
2153 }
2154 }
2155
2156 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2157 if (!prop_enum)
2158 return -ENOMEM;
2159
2160 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2161 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2162 prop_enum->value = value;
2163
2164 property->values[index] = value;
2165 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2166 return 0;
2167}
2168EXPORT_SYMBOL(drm_property_add_enum);
2169
2170void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2171{
2172 struct drm_property_enum *prop_enum, *pt;
2173
2174 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2175 list_del(&prop_enum->head);
2176 kfree(prop_enum);
2177 }
2178
2179 if (property->num_values)
2180 kfree(property->values);
2181 drm_mode_object_put(dev, &property->base);
2182 list_del(&property->head);
2183 kfree(property);
2184}
2185EXPORT_SYMBOL(drm_property_destroy);
2186
2187int drm_connector_attach_property(struct drm_connector *connector,
2188 struct drm_property *property, uint64_t init_val)
2189{
2190 int i;
2191
2192 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2193 if (connector->property_ids[i] == 0) {
2194 connector->property_ids[i] = property->base.id;
2195 connector->property_values[i] = init_val;
2196 break;
2197 }
2198 }
2199
2200 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2201 return -EINVAL;
2202 return 0;
2203}
2204EXPORT_SYMBOL(drm_connector_attach_property);
2205
2206int drm_connector_property_set_value(struct drm_connector *connector,
2207 struct drm_property *property, uint64_t value)
2208{
2209 int i;
2210
2211 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2212 if (connector->property_ids[i] == property->base.id) {
2213 connector->property_values[i] = value;
2214 break;
2215 }
2216 }
2217
2218 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2219 return -EINVAL;
2220 return 0;
2221}
2222EXPORT_SYMBOL(drm_connector_property_set_value);
2223
2224int drm_connector_property_get_value(struct drm_connector *connector,
2225 struct drm_property *property, uint64_t *val)
2226{
2227 int i;
2228
2229 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2230 if (connector->property_ids[i] == property->base.id) {
2231 *val = connector->property_values[i];
2232 break;
2233 }
2234 }
2235
2236 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2237 return -EINVAL;
2238 return 0;
2239}
2240EXPORT_SYMBOL(drm_connector_property_get_value);
2241
2242int drm_mode_getproperty_ioctl(struct drm_device *dev,
2243 void *data, struct drm_file *file_priv)
2244{
2245 struct drm_mode_object *obj;
2246 struct drm_mode_get_property *out_resp = data;
2247 struct drm_property *property;
2248 int enum_count = 0;
2249 int blob_count = 0;
2250 int value_count = 0;
2251 int ret = 0, i;
2252 int copied;
2253 struct drm_property_enum *prop_enum;
2254 struct drm_mode_property_enum __user *enum_ptr;
2255 struct drm_property_blob *prop_blob;
2256 uint32_t *blob_id_ptr;
2257 uint64_t __user *values_ptr;
2258 uint32_t __user *blob_length_ptr;
2259
Dave Airliefb3b06c2011-02-08 13:55:21 +10002260 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2261 return -EINVAL;
2262
Dave Airlief453ba02008-11-07 14:05:41 -08002263 mutex_lock(&dev->mode_config.mutex);
2264 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2265 if (!obj) {
2266 ret = -EINVAL;
2267 goto done;
2268 }
2269 property = obj_to_property(obj);
2270
2271 if (property->flags & DRM_MODE_PROP_ENUM) {
2272 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2273 enum_count++;
2274 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2275 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2276 blob_count++;
2277 }
2278
2279 value_count = property->num_values;
2280
2281 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2282 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2283 out_resp->flags = property->flags;
2284
2285 if ((out_resp->count_values >= value_count) && value_count) {
2286 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2287 for (i = 0; i < value_count; i++) {
2288 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2289 ret = -EFAULT;
2290 goto done;
2291 }
2292 }
2293 }
2294 out_resp->count_values = value_count;
2295
2296 if (property->flags & DRM_MODE_PROP_ENUM) {
2297 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2298 copied = 0;
2299 enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2300 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2301
2302 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2303 ret = -EFAULT;
2304 goto done;
2305 }
2306
2307 if (copy_to_user(&enum_ptr[copied].name,
2308 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2309 ret = -EFAULT;
2310 goto done;
2311 }
2312 copied++;
2313 }
2314 }
2315 out_resp->count_enum_blobs = enum_count;
2316 }
2317
2318 if (property->flags & DRM_MODE_PROP_BLOB) {
2319 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2320 copied = 0;
2321 blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2322 blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2323
2324 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2325 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2326 ret = -EFAULT;
2327 goto done;
2328 }
2329
2330 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2331 ret = -EFAULT;
2332 goto done;
2333 }
2334
2335 copied++;
2336 }
2337 }
2338 out_resp->count_enum_blobs = blob_count;
2339 }
2340done:
2341 mutex_unlock(&dev->mode_config.mutex);
2342 return ret;
2343}
2344
2345static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2346 void *data)
2347{
2348 struct drm_property_blob *blob;
2349
2350 if (!length || !data)
2351 return NULL;
2352
2353 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2354 if (!blob)
2355 return NULL;
2356
2357 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2358 blob->length = length;
2359
2360 memcpy(blob->data, data, length);
2361
2362 drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2363
2364 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2365 return blob;
2366}
2367
2368static void drm_property_destroy_blob(struct drm_device *dev,
2369 struct drm_property_blob *blob)
2370{
2371 drm_mode_object_put(dev, &blob->base);
2372 list_del(&blob->head);
2373 kfree(blob);
2374}
2375
2376int drm_mode_getblob_ioctl(struct drm_device *dev,
2377 void *data, struct drm_file *file_priv)
2378{
2379 struct drm_mode_object *obj;
2380 struct drm_mode_get_blob *out_resp = data;
2381 struct drm_property_blob *blob;
2382 int ret = 0;
2383 void *blob_ptr;
2384
Dave Airliefb3b06c2011-02-08 13:55:21 +10002385 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2386 return -EINVAL;
2387
Dave Airlief453ba02008-11-07 14:05:41 -08002388 mutex_lock(&dev->mode_config.mutex);
2389 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2390 if (!obj) {
2391 ret = -EINVAL;
2392 goto done;
2393 }
2394 blob = obj_to_blob(obj);
2395
2396 if (out_resp->length == blob->length) {
2397 blob_ptr = (void *)(unsigned long)out_resp->data;
2398 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2399 ret = -EFAULT;
2400 goto done;
2401 }
2402 }
2403 out_resp->length = blob->length;
2404
2405done:
2406 mutex_unlock(&dev->mode_config.mutex);
2407 return ret;
2408}
2409
2410int drm_mode_connector_update_edid_property(struct drm_connector *connector,
2411 struct edid *edid)
2412{
2413 struct drm_device *dev = connector->dev;
Adam Jackson7466f4c2010-03-29 21:43:23 +00002414 int ret = 0, size;
Dave Airlief453ba02008-11-07 14:05:41 -08002415
2416 if (connector->edid_blob_ptr)
2417 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
2418
2419 /* Delete edid, when there is none. */
2420 if (!edid) {
2421 connector->edid_blob_ptr = NULL;
2422 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
2423 return ret;
2424 }
2425
Adam Jackson7466f4c2010-03-29 21:43:23 +00002426 size = EDID_LENGTH * (1 + edid->extensions);
2427 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
2428 size, edid);
Dave Airlief453ba02008-11-07 14:05:41 -08002429
2430 ret = drm_connector_property_set_value(connector,
2431 dev->mode_config.edid_property,
2432 connector->edid_blob_ptr->base.id);
2433
2434 return ret;
2435}
2436EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
2437
2438int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2439 void *data, struct drm_file *file_priv)
2440{
2441 struct drm_mode_connector_set_property *out_resp = data;
2442 struct drm_mode_object *obj;
2443 struct drm_property *property;
2444 struct drm_connector *connector;
2445 int ret = -EINVAL;
2446 int i;
2447
Dave Airliefb3b06c2011-02-08 13:55:21 +10002448 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2449 return -EINVAL;
2450
Dave Airlief453ba02008-11-07 14:05:41 -08002451 mutex_lock(&dev->mode_config.mutex);
2452
2453 obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2454 if (!obj) {
2455 goto out;
2456 }
2457 connector = obj_to_connector(obj);
2458
2459 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2460 if (connector->property_ids[i] == out_resp->prop_id)
2461 break;
2462 }
2463
2464 if (i == DRM_CONNECTOR_MAX_PROPERTY) {
2465 goto out;
2466 }
2467
2468 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2469 if (!obj) {
2470 goto out;
2471 }
2472 property = obj_to_property(obj);
2473
2474 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2475 goto out;
2476
2477 if (property->flags & DRM_MODE_PROP_RANGE) {
2478 if (out_resp->value < property->values[0])
2479 goto out;
2480
2481 if (out_resp->value > property->values[1])
2482 goto out;
2483 } else {
2484 int found = 0;
2485 for (i = 0; i < property->num_values; i++) {
2486 if (property->values[i] == out_resp->value) {
2487 found = 1;
2488 break;
2489 }
2490 }
2491 if (!found) {
2492 goto out;
2493 }
2494 }
2495
Keith Packardc9fb15f2009-05-30 20:42:28 -07002496 /* Do DPMS ourselves */
2497 if (property == connector->dev->mode_config.dpms_property) {
2498 if (connector->funcs->dpms)
2499 (*connector->funcs->dpms)(connector, (int) out_resp->value);
2500 ret = 0;
2501 } else if (connector->funcs->set_property)
Dave Airlief453ba02008-11-07 14:05:41 -08002502 ret = connector->funcs->set_property(connector, property, out_resp->value);
2503
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002504 /* store the property value if successful */
Dave Airlief453ba02008-11-07 14:05:41 -08002505 if (!ret)
2506 drm_connector_property_set_value(connector, property, out_resp->value);
2507out:
2508 mutex_unlock(&dev->mode_config.mutex);
2509 return ret;
2510}
2511
Dave Airlief453ba02008-11-07 14:05:41 -08002512int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2513 struct drm_encoder *encoder)
2514{
2515 int i;
2516
2517 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2518 if (connector->encoder_ids[i] == 0) {
2519 connector->encoder_ids[i] = encoder->base.id;
2520 return 0;
2521 }
2522 }
2523 return -ENOMEM;
2524}
2525EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
2526
2527void drm_mode_connector_detach_encoder(struct drm_connector *connector,
2528 struct drm_encoder *encoder)
2529{
2530 int i;
2531 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2532 if (connector->encoder_ids[i] == encoder->base.id) {
2533 connector->encoder_ids[i] = 0;
2534 if (connector->encoder == encoder)
2535 connector->encoder = NULL;
2536 break;
2537 }
2538 }
2539}
2540EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
2541
2542bool drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
2543 int gamma_size)
2544{
2545 crtc->gamma_size = gamma_size;
2546
2547 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
2548 if (!crtc->gamma_store) {
2549 crtc->gamma_size = 0;
2550 return false;
2551 }
2552
2553 return true;
2554}
2555EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
2556
2557int drm_mode_gamma_set_ioctl(struct drm_device *dev,
2558 void *data, struct drm_file *file_priv)
2559{
2560 struct drm_mode_crtc_lut *crtc_lut = data;
2561 struct drm_mode_object *obj;
2562 struct drm_crtc *crtc;
2563 void *r_base, *g_base, *b_base;
2564 int size;
2565 int ret = 0;
2566
Dave Airliefb3b06c2011-02-08 13:55:21 +10002567 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2568 return -EINVAL;
2569
Dave Airlief453ba02008-11-07 14:05:41 -08002570 mutex_lock(&dev->mode_config.mutex);
2571 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2572 if (!obj) {
2573 ret = -EINVAL;
2574 goto out;
2575 }
2576 crtc = obj_to_crtc(obj);
2577
2578 /* memcpy into gamma store */
2579 if (crtc_lut->gamma_size != crtc->gamma_size) {
2580 ret = -EINVAL;
2581 goto out;
2582 }
2583
2584 size = crtc_lut->gamma_size * (sizeof(uint16_t));
2585 r_base = crtc->gamma_store;
2586 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
2587 ret = -EFAULT;
2588 goto out;
2589 }
2590
2591 g_base = r_base + size;
2592 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
2593 ret = -EFAULT;
2594 goto out;
2595 }
2596
2597 b_base = g_base + size;
2598 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
2599 ret = -EFAULT;
2600 goto out;
2601 }
2602
James Simmons72034252010-08-03 01:33:19 +01002603 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08002604
2605out:
2606 mutex_unlock(&dev->mode_config.mutex);
2607 return ret;
2608
2609}
2610
2611int drm_mode_gamma_get_ioctl(struct drm_device *dev,
2612 void *data, struct drm_file *file_priv)
2613{
2614 struct drm_mode_crtc_lut *crtc_lut = data;
2615 struct drm_mode_object *obj;
2616 struct drm_crtc *crtc;
2617 void *r_base, *g_base, *b_base;
2618 int size;
2619 int ret = 0;
2620
Dave Airliefb3b06c2011-02-08 13:55:21 +10002621 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2622 return -EINVAL;
2623
Dave Airlief453ba02008-11-07 14:05:41 -08002624 mutex_lock(&dev->mode_config.mutex);
2625 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2626 if (!obj) {
2627 ret = -EINVAL;
2628 goto out;
2629 }
2630 crtc = obj_to_crtc(obj);
2631
2632 /* memcpy into gamma store */
2633 if (crtc_lut->gamma_size != crtc->gamma_size) {
2634 ret = -EINVAL;
2635 goto out;
2636 }
2637
2638 size = crtc_lut->gamma_size * (sizeof(uint16_t));
2639 r_base = crtc->gamma_store;
2640 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
2641 ret = -EFAULT;
2642 goto out;
2643 }
2644
2645 g_base = r_base + size;
2646 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
2647 ret = -EFAULT;
2648 goto out;
2649 }
2650
2651 b_base = g_base + size;
2652 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
2653 ret = -EFAULT;
2654 goto out;
2655 }
2656out:
2657 mutex_unlock(&dev->mode_config.mutex);
2658 return ret;
2659}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05002660
2661int drm_mode_page_flip_ioctl(struct drm_device *dev,
2662 void *data, struct drm_file *file_priv)
2663{
2664 struct drm_mode_crtc_page_flip *page_flip = data;
2665 struct drm_mode_object *obj;
2666 struct drm_crtc *crtc;
2667 struct drm_framebuffer *fb;
2668 struct drm_pending_vblank_event *e = NULL;
2669 unsigned long flags;
2670 int ret = -EINVAL;
2671
2672 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
2673 page_flip->reserved != 0)
2674 return -EINVAL;
2675
2676 mutex_lock(&dev->mode_config.mutex);
2677 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
2678 if (!obj)
2679 goto out;
2680 crtc = obj_to_crtc(obj);
2681
Chris Wilson90c1efd2010-07-17 20:23:26 +01002682 if (crtc->fb == NULL) {
2683 /* The framebuffer is currently unbound, presumably
2684 * due to a hotplug event, that userspace has not
2685 * yet discovered.
2686 */
2687 ret = -EBUSY;
2688 goto out;
2689 }
2690
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05002691 if (crtc->funcs->page_flip == NULL)
2692 goto out;
2693
2694 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
2695 if (!obj)
2696 goto out;
2697 fb = obj_to_fb(obj);
2698
2699 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
2700 ret = -ENOMEM;
2701 spin_lock_irqsave(&dev->event_lock, flags);
2702 if (file_priv->event_space < sizeof e->event) {
2703 spin_unlock_irqrestore(&dev->event_lock, flags);
2704 goto out;
2705 }
2706 file_priv->event_space -= sizeof e->event;
2707 spin_unlock_irqrestore(&dev->event_lock, flags);
2708
2709 e = kzalloc(sizeof *e, GFP_KERNEL);
2710 if (e == NULL) {
2711 spin_lock_irqsave(&dev->event_lock, flags);
2712 file_priv->event_space += sizeof e->event;
2713 spin_unlock_irqrestore(&dev->event_lock, flags);
2714 goto out;
2715 }
2716
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08002717 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05002718 e->event.base.length = sizeof e->event;
2719 e->event.user_data = page_flip->user_data;
2720 e->base.event = &e->event.base;
2721 e->base.file_priv = file_priv;
2722 e->base.destroy =
2723 (void (*) (struct drm_pending_event *)) kfree;
2724 }
2725
2726 ret = crtc->funcs->page_flip(crtc, fb, e);
2727 if (ret) {
2728 spin_lock_irqsave(&dev->event_lock, flags);
2729 file_priv->event_space += sizeof e->event;
2730 spin_unlock_irqrestore(&dev->event_lock, flags);
2731 kfree(e);
2732 }
2733
2734out:
2735 mutex_unlock(&dev->mode_config.mutex);
2736 return ret;
2737}
Chris Wilsoneb033552011-01-24 15:11:08 +00002738
2739void drm_mode_config_reset(struct drm_device *dev)
2740{
2741 struct drm_crtc *crtc;
2742 struct drm_encoder *encoder;
2743 struct drm_connector *connector;
2744
2745 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
2746 if (crtc->funcs->reset)
2747 crtc->funcs->reset(crtc);
2748
2749 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
2750 if (encoder->funcs->reset)
2751 encoder->funcs->reset(encoder);
2752
2753 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
2754 if (connector->funcs->reset)
2755 connector->funcs->reset(connector);
2756}
2757EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002758
2759int drm_mode_create_dumb_ioctl(struct drm_device *dev,
2760 void *data, struct drm_file *file_priv)
2761{
2762 struct drm_mode_create_dumb *args = data;
2763
2764 if (!dev->driver->dumb_create)
2765 return -ENOSYS;
2766 return dev->driver->dumb_create(file_priv, dev, args);
2767}
2768
2769int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
2770 void *data, struct drm_file *file_priv)
2771{
2772 struct drm_mode_map_dumb *args = data;
2773
2774 /* call driver ioctl to get mmap offset */
2775 if (!dev->driver->dumb_map_offset)
2776 return -ENOSYS;
2777
2778 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
2779}
2780
2781int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
2782 void *data, struct drm_file *file_priv)
2783{
2784 struct drm_mode_destroy_dumb *args = data;
2785
2786 if (!dev->driver->dumb_destroy)
2787 return -ENOSYS;
2788
2789 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
2790}