blob: 4fe321dc900ca69a710a6ffd2113b8ddf0af91c1 [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>
33#include "drm.h"
34#include "drmP.h"
35#include "drm_crtc.h"
36
37struct drm_prop_enum_list {
38 int type;
39 char *name;
40};
41
42/* Avoid boilerplate. I'm tired of typing. */
43#define DRM_ENUM_NAME_FN(fnname, list) \
44 char *fnname(int val) \
45 { \
46 int i; \
47 for (i = 0; i < ARRAY_SIZE(list); i++) { \
48 if (list[i].type == val) \
49 return list[i].name; \
50 } \
51 return "(unknown)"; \
52 }
53
54/*
55 * Global properties
56 */
57static struct drm_prop_enum_list drm_dpms_enum_list[] =
58{ { DRM_MODE_DPMS_ON, "On" },
59 { DRM_MODE_DPMS_STANDBY, "Standby" },
60 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
61 { DRM_MODE_DPMS_OFF, "Off" }
62};
63
64DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
65
66/*
67 * Optional properties
68 */
69static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
70{
Jesse Barnes53bd8382009-07-01 10:04:40 -070071 { DRM_MODE_SCALE_NONE, "None" },
72 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
73 { DRM_MODE_SCALE_CENTER, "Center" },
74 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -080075};
76
77static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
78{
79 { DRM_MODE_DITHERING_OFF, "Off" },
80 { DRM_MODE_DITHERING_ON, "On" },
81};
82
83/*
84 * Non-global properties, but "required" for certain connectors.
85 */
86static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
87{
88 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
89 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
90 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
91};
92
93DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
94
95static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
96{
97 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
98 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
99 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
100};
101
102DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
103 drm_dvi_i_subconnector_enum_list)
104
105static struct drm_prop_enum_list drm_tv_select_enum_list[] =
106{
107 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
108 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
109 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
110 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200111 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800112};
113
114DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
115
116static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
117{
118 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
119 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
120 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
121 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200122 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800123};
124
125DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
126 drm_tv_subconnector_enum_list)
127
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000128static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
129 { DRM_MODE_DIRTY_OFF, "Off" },
130 { DRM_MODE_DIRTY_ON, "On" },
131 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
132};
133
134DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
135 drm_dirty_info_enum_list)
136
Dave Airlief453ba02008-11-07 14:05:41 -0800137struct drm_conn_prop_enum_list {
138 int type;
139 char *name;
140 int count;
141};
142
143/*
144 * Connector and encoder types.
145 */
146static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
147{ { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
148 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
149 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
150 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
151 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
152 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
153 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
154 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
155 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
156 { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN", 0 },
157 { DRM_MODE_CONNECTOR_DisplayPort, "DisplayPort", 0 },
158 { DRM_MODE_CONNECTOR_HDMIA, "HDMI Type A", 0 },
159 { DRM_MODE_CONNECTOR_HDMIB, "HDMI Type B", 0 },
Francisco Jerez74bd3c22009-08-02 04:19:18 +0200160 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
Dave Airlief453ba02008-11-07 14:05:41 -0800161};
162
163static struct drm_prop_enum_list drm_encoder_enum_list[] =
164{ { DRM_MODE_ENCODER_NONE, "None" },
165 { DRM_MODE_ENCODER_DAC, "DAC" },
166 { DRM_MODE_ENCODER_TMDS, "TMDS" },
167 { DRM_MODE_ENCODER_LVDS, "LVDS" },
168 { DRM_MODE_ENCODER_TVDAC, "TV" },
169};
170
171char *drm_get_encoder_name(struct drm_encoder *encoder)
172{
173 static char buf[32];
174
175 snprintf(buf, 32, "%s-%d",
176 drm_encoder_enum_list[encoder->encoder_type].name,
177 encoder->base.id);
178 return buf;
179}
Dave Airlie13a81952009-09-07 15:45:33 +1000180EXPORT_SYMBOL(drm_get_encoder_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800181
182char *drm_get_connector_name(struct drm_connector *connector)
183{
184 static char buf[32];
185
186 snprintf(buf, 32, "%s-%d",
187 drm_connector_enum_list[connector->connector_type].name,
188 connector->connector_type_id);
189 return buf;
190}
191EXPORT_SYMBOL(drm_get_connector_name);
192
193char *drm_get_connector_status_name(enum drm_connector_status status)
194{
195 if (status == connector_status_connected)
196 return "connected";
197 else if (status == connector_status_disconnected)
198 return "disconnected";
199 else
200 return "unknown";
201}
202
203/**
204 * drm_mode_object_get - allocate a new identifier
205 * @dev: DRM device
206 * @ptr: object pointer, used to generate unique ID
207 * @type: object type
208 *
209 * LOCKING:
Dave Airlief453ba02008-11-07 14:05:41 -0800210 *
211 * Create a unique identifier based on @ptr in @dev's identifier space. Used
212 * for tracking modes, CRTCs and connectors.
213 *
214 * RETURNS:
215 * New unique (relative to other objects in @dev) integer identifier for the
216 * object.
217 */
218static int drm_mode_object_get(struct drm_device *dev,
219 struct drm_mode_object *obj, uint32_t obj_type)
220{
221 int new_id = 0;
222 int ret;
223
Dave Airlief453ba02008-11-07 14:05:41 -0800224again:
225 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
226 DRM_ERROR("Ran out memory getting a mode number\n");
227 return -EINVAL;
228 }
229
Jesse Barnesad2563c2009-01-19 17:21:45 +1000230 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800231 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000232 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800233 if (ret == -EAGAIN)
234 goto again;
235
236 obj->id = new_id;
237 obj->type = obj_type;
238 return 0;
239}
240
241/**
242 * drm_mode_object_put - free an identifer
243 * @dev: DRM device
244 * @id: ID to free
245 *
246 * LOCKING:
247 * Caller must hold DRM mode_config lock.
248 *
249 * Free @id from @dev's unique identifier pool.
250 */
251static void drm_mode_object_put(struct drm_device *dev,
252 struct drm_mode_object *object)
253{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000254 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800255 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000256 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800257}
258
259void *drm_mode_object_find(struct drm_device *dev, uint32_t id, uint32_t type)
260{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000261 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800262
Jesse Barnesad2563c2009-01-19 17:21:45 +1000263 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800264 obj = idr_find(&dev->mode_config.crtc_idr, id);
265 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000266 obj = NULL;
267 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800268
269 return obj;
270}
271EXPORT_SYMBOL(drm_mode_object_find);
272
273/**
Dave Airlief453ba02008-11-07 14:05:41 -0800274 * drm_framebuffer_init - initialize a framebuffer
275 * @dev: DRM device
276 *
277 * LOCKING:
278 * Caller must hold mode config lock.
279 *
280 * Allocates an ID for the framebuffer's parent mode object, sets its mode
281 * functions & device file and adds it to the master fd list.
282 *
283 * RETURNS:
284 * Zero on success, error code on falure.
285 */
286int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
287 const struct drm_framebuffer_funcs *funcs)
288{
289 int ret;
290
291 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
292 if (ret) {
293 return ret;
294 }
295
296 fb->dev = dev;
297 fb->funcs = funcs;
298 dev->mode_config.num_fb++;
299 list_add(&fb->head, &dev->mode_config.fb_list);
300
301 return 0;
302}
303EXPORT_SYMBOL(drm_framebuffer_init);
304
305/**
306 * drm_framebuffer_cleanup - remove a framebuffer object
307 * @fb: framebuffer to remove
308 *
309 * LOCKING:
310 * Caller must hold mode config lock.
311 *
312 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
313 * it, setting it to NULL.
314 */
315void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
316{
317 struct drm_device *dev = fb->dev;
318 struct drm_crtc *crtc;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000319 struct drm_mode_set set;
320 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800321
322 /* remove from any CRTC */
323 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie5ef5f722009-08-17 13:11:23 +1000324 if (crtc->fb == fb) {
325 /* should turn off the crtc */
326 memset(&set, 0, sizeof(struct drm_mode_set));
327 set.crtc = crtc;
328 set.fb = NULL;
329 ret = crtc->funcs->set_config(&set);
330 if (ret)
331 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
332 }
Dave Airlief453ba02008-11-07 14:05:41 -0800333 }
334
335 drm_mode_object_put(dev, &fb->base);
336 list_del(&fb->head);
337 dev->mode_config.num_fb--;
338}
339EXPORT_SYMBOL(drm_framebuffer_cleanup);
340
341/**
342 * drm_crtc_init - Initialise a new CRTC object
343 * @dev: DRM device
344 * @crtc: CRTC object to init
345 * @funcs: callbacks for the new CRTC
346 *
347 * LOCKING:
348 * Caller must hold mode config lock.
349 *
350 * Inits a new object created as base part of an driver crtc object.
351 */
352void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
353 const struct drm_crtc_funcs *funcs)
354{
355 crtc->dev = dev;
356 crtc->funcs = funcs;
357
358 mutex_lock(&dev->mode_config.mutex);
359 drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
360
361 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
362 dev->mode_config.num_crtc++;
363 mutex_unlock(&dev->mode_config.mutex);
364}
365EXPORT_SYMBOL(drm_crtc_init);
366
367/**
368 * drm_crtc_cleanup - Cleans up the core crtc usage.
369 * @crtc: CRTC to cleanup
370 *
371 * LOCKING:
372 * Caller must hold mode config lock.
373 *
374 * Cleanup @crtc. Removes from drm modesetting space
375 * does NOT free object, caller does that.
376 */
377void drm_crtc_cleanup(struct drm_crtc *crtc)
378{
379 struct drm_device *dev = crtc->dev;
380
381 if (crtc->gamma_store) {
382 kfree(crtc->gamma_store);
383 crtc->gamma_store = NULL;
384 }
385
386 drm_mode_object_put(dev, &crtc->base);
387 list_del(&crtc->head);
388 dev->mode_config.num_crtc--;
389}
390EXPORT_SYMBOL(drm_crtc_cleanup);
391
392/**
393 * drm_mode_probed_add - add a mode to a connector's probed mode list
394 * @connector: connector the new mode
395 * @mode: mode data
396 *
397 * LOCKING:
398 * Caller must hold mode config lock.
399 *
400 * Add @mode to @connector's mode list for later use.
401 */
402void drm_mode_probed_add(struct drm_connector *connector,
403 struct drm_display_mode *mode)
404{
405 list_add(&mode->head, &connector->probed_modes);
406}
407EXPORT_SYMBOL(drm_mode_probed_add);
408
409/**
410 * drm_mode_remove - remove and free a mode
411 * @connector: connector list to modify
412 * @mode: mode to remove
413 *
414 * LOCKING:
415 * Caller must hold mode config lock.
416 *
417 * Remove @mode from @connector's mode list, then free it.
418 */
419void drm_mode_remove(struct drm_connector *connector,
420 struct drm_display_mode *mode)
421{
422 list_del(&mode->head);
423 kfree(mode);
424}
425EXPORT_SYMBOL(drm_mode_remove);
426
427/**
428 * drm_connector_init - Init a preallocated connector
429 * @dev: DRM device
430 * @connector: the connector to init
431 * @funcs: callbacks for this connector
432 * @name: user visible name of the connector
433 *
434 * LOCKING:
435 * Caller must hold @dev's mode_config lock.
436 *
437 * Initialises a preallocated connector. Connectors should be
438 * subclassed as part of driver connector objects.
439 */
440void drm_connector_init(struct drm_device *dev,
441 struct drm_connector *connector,
442 const struct drm_connector_funcs *funcs,
443 int connector_type)
444{
445 mutex_lock(&dev->mode_config.mutex);
446
447 connector->dev = dev;
448 connector->funcs = funcs;
449 drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
450 connector->connector_type = connector_type;
451 connector->connector_type_id =
452 ++drm_connector_enum_list[connector_type].count; /* TODO */
453 INIT_LIST_HEAD(&connector->user_modes);
454 INIT_LIST_HEAD(&connector->probed_modes);
455 INIT_LIST_HEAD(&connector->modes);
456 connector->edid_blob_ptr = NULL;
457
458 list_add_tail(&connector->head, &dev->mode_config.connector_list);
459 dev->mode_config.num_connector++;
460
461 drm_connector_attach_property(connector,
462 dev->mode_config.edid_property, 0);
463
464 drm_connector_attach_property(connector,
465 dev->mode_config.dpms_property, 0);
466
467 mutex_unlock(&dev->mode_config.mutex);
468}
469EXPORT_SYMBOL(drm_connector_init);
470
471/**
472 * drm_connector_cleanup - cleans up an initialised connector
473 * @connector: connector to cleanup
474 *
475 * LOCKING:
476 * Caller must hold @dev's mode_config lock.
477 *
478 * Cleans up the connector but doesn't free the object.
479 */
480void drm_connector_cleanup(struct drm_connector *connector)
481{
482 struct drm_device *dev = connector->dev;
483 struct drm_display_mode *mode, *t;
484
485 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
486 drm_mode_remove(connector, mode);
487
488 list_for_each_entry_safe(mode, t, &connector->modes, head)
489 drm_mode_remove(connector, mode);
490
491 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
492 drm_mode_remove(connector, mode);
493
Dave Airlied50ba252009-09-23 14:44:08 +1000494 kfree(connector->fb_helper_private);
Dave Airlief453ba02008-11-07 14:05:41 -0800495 mutex_lock(&dev->mode_config.mutex);
496 drm_mode_object_put(dev, &connector->base);
497 list_del(&connector->head);
498 mutex_unlock(&dev->mode_config.mutex);
499}
500EXPORT_SYMBOL(drm_connector_cleanup);
501
502void drm_encoder_init(struct drm_device *dev,
503 struct drm_encoder *encoder,
504 const struct drm_encoder_funcs *funcs,
505 int encoder_type)
506{
507 mutex_lock(&dev->mode_config.mutex);
508
509 encoder->dev = dev;
510
511 drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
512 encoder->encoder_type = encoder_type;
513 encoder->funcs = funcs;
514
515 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
516 dev->mode_config.num_encoder++;
517
518 mutex_unlock(&dev->mode_config.mutex);
519}
520EXPORT_SYMBOL(drm_encoder_init);
521
522void drm_encoder_cleanup(struct drm_encoder *encoder)
523{
524 struct drm_device *dev = encoder->dev;
525 mutex_lock(&dev->mode_config.mutex);
526 drm_mode_object_put(dev, &encoder->base);
527 list_del(&encoder->head);
528 mutex_unlock(&dev->mode_config.mutex);
529}
530EXPORT_SYMBOL(drm_encoder_cleanup);
531
532/**
533 * drm_mode_create - create a new display mode
534 * @dev: DRM device
535 *
536 * LOCKING:
537 * Caller must hold DRM mode_config lock.
538 *
539 * Create a new drm_display_mode, give it an ID, and return it.
540 *
541 * RETURNS:
542 * Pointer to new mode on success, NULL on error.
543 */
544struct drm_display_mode *drm_mode_create(struct drm_device *dev)
545{
546 struct drm_display_mode *nmode;
547
548 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
549 if (!nmode)
550 return NULL;
551
552 drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
553 return nmode;
554}
555EXPORT_SYMBOL(drm_mode_create);
556
557/**
558 * drm_mode_destroy - remove a mode
559 * @dev: DRM device
560 * @mode: mode to remove
561 *
562 * LOCKING:
563 * Caller must hold mode config lock.
564 *
565 * Free @mode's unique identifier, then free it.
566 */
567void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
568{
569 drm_mode_object_put(dev, &mode->base);
570
571 kfree(mode);
572}
573EXPORT_SYMBOL(drm_mode_destroy);
574
575static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
576{
577 struct drm_property *edid;
578 struct drm_property *dpms;
579 int i;
580
581 /*
582 * Standard properties (apply to all connectors)
583 */
584 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
585 DRM_MODE_PROP_IMMUTABLE,
586 "EDID", 0);
587 dev->mode_config.edid_property = edid;
588
589 dpms = drm_property_create(dev, DRM_MODE_PROP_ENUM,
590 "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
591 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
592 drm_property_add_enum(dpms, i, drm_dpms_enum_list[i].type,
593 drm_dpms_enum_list[i].name);
594 dev->mode_config.dpms_property = dpms;
595
596 return 0;
597}
598
599/**
600 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
601 * @dev: DRM device
602 *
603 * Called by a driver the first time a DVI-I connector is made.
604 */
605int drm_mode_create_dvi_i_properties(struct drm_device *dev)
606{
607 struct drm_property *dvi_i_selector;
608 struct drm_property *dvi_i_subconnector;
609 int i;
610
611 if (dev->mode_config.dvi_i_select_subconnector_property)
612 return 0;
613
614 dvi_i_selector =
615 drm_property_create(dev, DRM_MODE_PROP_ENUM,
616 "select subconnector",
617 ARRAY_SIZE(drm_dvi_i_select_enum_list));
618 for (i = 0; i < ARRAY_SIZE(drm_dvi_i_select_enum_list); i++)
619 drm_property_add_enum(dvi_i_selector, i,
620 drm_dvi_i_select_enum_list[i].type,
621 drm_dvi_i_select_enum_list[i].name);
622 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
623
624 dvi_i_subconnector =
625 drm_property_create(dev, DRM_MODE_PROP_ENUM |
626 DRM_MODE_PROP_IMMUTABLE,
627 "subconnector",
628 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
629 for (i = 0; i < ARRAY_SIZE(drm_dvi_i_subconnector_enum_list); i++)
630 drm_property_add_enum(dvi_i_subconnector, i,
631 drm_dvi_i_subconnector_enum_list[i].type,
632 drm_dvi_i_subconnector_enum_list[i].name);
633 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
634
635 return 0;
636}
637EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
638
639/**
640 * drm_create_tv_properties - create TV specific connector properties
641 * @dev: DRM device
642 * @num_modes: number of different TV formats (modes) supported
643 * @modes: array of pointers to strings containing name of each format
644 *
645 * Called by a driver's TV initialization routine, this function creates
646 * the TV specific connector properties for a given device. Caller is
647 * responsible for allocating a list of format names and passing them to
648 * this routine.
649 */
650int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
651 char *modes[])
652{
653 struct drm_property *tv_selector;
654 struct drm_property *tv_subconnector;
655 int i;
656
657 if (dev->mode_config.tv_select_subconnector_property)
658 return 0;
659
660 /*
661 * Basic connector properties
662 */
663 tv_selector = drm_property_create(dev, DRM_MODE_PROP_ENUM,
664 "select subconnector",
665 ARRAY_SIZE(drm_tv_select_enum_list));
666 for (i = 0; i < ARRAY_SIZE(drm_tv_select_enum_list); i++)
667 drm_property_add_enum(tv_selector, i,
668 drm_tv_select_enum_list[i].type,
669 drm_tv_select_enum_list[i].name);
670 dev->mode_config.tv_select_subconnector_property = tv_selector;
671
672 tv_subconnector =
673 drm_property_create(dev, DRM_MODE_PROP_ENUM |
674 DRM_MODE_PROP_IMMUTABLE, "subconnector",
675 ARRAY_SIZE(drm_tv_subconnector_enum_list));
676 for (i = 0; i < ARRAY_SIZE(drm_tv_subconnector_enum_list); i++)
677 drm_property_add_enum(tv_subconnector, i,
678 drm_tv_subconnector_enum_list[i].type,
679 drm_tv_subconnector_enum_list[i].name);
680 dev->mode_config.tv_subconnector_property = tv_subconnector;
681
682 /*
683 * Other, TV specific properties: margins & TV modes.
684 */
685 dev->mode_config.tv_left_margin_property =
686 drm_property_create(dev, DRM_MODE_PROP_RANGE,
687 "left margin", 2);
688 dev->mode_config.tv_left_margin_property->values[0] = 0;
689 dev->mode_config.tv_left_margin_property->values[1] = 100;
690
691 dev->mode_config.tv_right_margin_property =
692 drm_property_create(dev, DRM_MODE_PROP_RANGE,
693 "right margin", 2);
694 dev->mode_config.tv_right_margin_property->values[0] = 0;
695 dev->mode_config.tv_right_margin_property->values[1] = 100;
696
697 dev->mode_config.tv_top_margin_property =
698 drm_property_create(dev, DRM_MODE_PROP_RANGE,
699 "top margin", 2);
700 dev->mode_config.tv_top_margin_property->values[0] = 0;
701 dev->mode_config.tv_top_margin_property->values[1] = 100;
702
703 dev->mode_config.tv_bottom_margin_property =
704 drm_property_create(dev, DRM_MODE_PROP_RANGE,
705 "bottom margin", 2);
706 dev->mode_config.tv_bottom_margin_property->values[0] = 0;
707 dev->mode_config.tv_bottom_margin_property->values[1] = 100;
708
709 dev->mode_config.tv_mode_property =
710 drm_property_create(dev, DRM_MODE_PROP_ENUM,
711 "mode", num_modes);
712 for (i = 0; i < num_modes; i++)
713 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
714 i, modes[i]);
715
Francisco Jerezb6b79022009-08-02 04:19:20 +0200716 dev->mode_config.tv_brightness_property =
717 drm_property_create(dev, DRM_MODE_PROP_RANGE,
718 "brightness", 2);
719 dev->mode_config.tv_brightness_property->values[0] = 0;
720 dev->mode_config.tv_brightness_property->values[1] = 100;
721
722 dev->mode_config.tv_contrast_property =
723 drm_property_create(dev, DRM_MODE_PROP_RANGE,
724 "contrast", 2);
725 dev->mode_config.tv_contrast_property->values[0] = 0;
726 dev->mode_config.tv_contrast_property->values[1] = 100;
727
728 dev->mode_config.tv_flicker_reduction_property =
729 drm_property_create(dev, DRM_MODE_PROP_RANGE,
730 "flicker reduction", 2);
731 dev->mode_config.tv_flicker_reduction_property->values[0] = 0;
732 dev->mode_config.tv_flicker_reduction_property->values[1] = 100;
733
Francisco Jereza75f0232009-08-12 02:30:10 +0200734 dev->mode_config.tv_overscan_property =
735 drm_property_create(dev, DRM_MODE_PROP_RANGE,
736 "overscan", 2);
737 dev->mode_config.tv_overscan_property->values[0] = 0;
738 dev->mode_config.tv_overscan_property->values[1] = 100;
739
740 dev->mode_config.tv_saturation_property =
741 drm_property_create(dev, DRM_MODE_PROP_RANGE,
742 "saturation", 2);
743 dev->mode_config.tv_saturation_property->values[0] = 0;
744 dev->mode_config.tv_saturation_property->values[1] = 100;
745
746 dev->mode_config.tv_hue_property =
747 drm_property_create(dev, DRM_MODE_PROP_RANGE,
748 "hue", 2);
749 dev->mode_config.tv_hue_property->values[0] = 0;
750 dev->mode_config.tv_hue_property->values[1] = 100;
751
Dave Airlief453ba02008-11-07 14:05:41 -0800752 return 0;
753}
754EXPORT_SYMBOL(drm_mode_create_tv_properties);
755
756/**
757 * drm_mode_create_scaling_mode_property - create scaling mode property
758 * @dev: DRM device
759 *
760 * Called by a driver the first time it's needed, must be attached to desired
761 * connectors.
762 */
763int drm_mode_create_scaling_mode_property(struct drm_device *dev)
764{
765 struct drm_property *scaling_mode;
766 int i;
767
768 if (dev->mode_config.scaling_mode_property)
769 return 0;
770
771 scaling_mode =
772 drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
773 ARRAY_SIZE(drm_scaling_mode_enum_list));
774 for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++)
775 drm_property_add_enum(scaling_mode, i,
776 drm_scaling_mode_enum_list[i].type,
777 drm_scaling_mode_enum_list[i].name);
778
779 dev->mode_config.scaling_mode_property = scaling_mode;
780
781 return 0;
782}
783EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
784
785/**
786 * drm_mode_create_dithering_property - create dithering property
787 * @dev: DRM device
788 *
789 * Called by a driver the first time it's needed, must be attached to desired
790 * connectors.
791 */
792int drm_mode_create_dithering_property(struct drm_device *dev)
793{
794 struct drm_property *dithering_mode;
795 int i;
796
797 if (dev->mode_config.dithering_mode_property)
798 return 0;
799
800 dithering_mode =
801 drm_property_create(dev, DRM_MODE_PROP_ENUM, "dithering",
802 ARRAY_SIZE(drm_dithering_mode_enum_list));
803 for (i = 0; i < ARRAY_SIZE(drm_dithering_mode_enum_list); i++)
804 drm_property_add_enum(dithering_mode, i,
805 drm_dithering_mode_enum_list[i].type,
806 drm_dithering_mode_enum_list[i].name);
807 dev->mode_config.dithering_mode_property = dithering_mode;
808
809 return 0;
810}
811EXPORT_SYMBOL(drm_mode_create_dithering_property);
812
813/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000814 * drm_mode_create_dirty_property - create dirty property
815 * @dev: DRM device
816 *
817 * Called by a driver the first time it's needed, must be attached to desired
818 * connectors.
819 */
820int drm_mode_create_dirty_info_property(struct drm_device *dev)
821{
822 struct drm_property *dirty_info;
823 int i;
824
825 if (dev->mode_config.dirty_info_property)
826 return 0;
827
828 dirty_info =
829 drm_property_create(dev, DRM_MODE_PROP_ENUM |
830 DRM_MODE_PROP_IMMUTABLE,
831 "dirty",
832 ARRAY_SIZE(drm_dirty_info_enum_list));
833 for (i = 0; i < ARRAY_SIZE(drm_dirty_info_enum_list); i++)
834 drm_property_add_enum(dirty_info, i,
835 drm_dirty_info_enum_list[i].type,
836 drm_dirty_info_enum_list[i].name);
837 dev->mode_config.dirty_info_property = dirty_info;
838
839 return 0;
840}
841EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
842
843/**
Dave Airlief453ba02008-11-07 14:05:41 -0800844 * drm_mode_config_init - initialize DRM mode_configuration structure
845 * @dev: DRM device
846 *
847 * LOCKING:
848 * None, should happen single threaded at init time.
849 *
850 * Initialize @dev's mode_config structure, used for tracking the graphics
851 * configuration of @dev.
852 */
853void drm_mode_config_init(struct drm_device *dev)
854{
855 mutex_init(&dev->mode_config.mutex);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000856 mutex_init(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800857 INIT_LIST_HEAD(&dev->mode_config.fb_list);
858 INIT_LIST_HEAD(&dev->mode_config.fb_kernel_list);
859 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
860 INIT_LIST_HEAD(&dev->mode_config.connector_list);
861 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
862 INIT_LIST_HEAD(&dev->mode_config.property_list);
863 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
864 idr_init(&dev->mode_config.crtc_idr);
865
866 mutex_lock(&dev->mode_config.mutex);
867 drm_mode_create_standard_connector_properties(dev);
868 mutex_unlock(&dev->mode_config.mutex);
869
870 /* Just to be sure */
871 dev->mode_config.num_fb = 0;
872 dev->mode_config.num_connector = 0;
873 dev->mode_config.num_crtc = 0;
874 dev->mode_config.num_encoder = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800875}
876EXPORT_SYMBOL(drm_mode_config_init);
877
878int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
879{
880 uint32_t total_objects = 0;
881
882 total_objects += dev->mode_config.num_crtc;
883 total_objects += dev->mode_config.num_connector;
884 total_objects += dev->mode_config.num_encoder;
885
886 if (total_objects == 0)
887 return -EINVAL;
888
889 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
890 if (!group->id_list)
891 return -ENOMEM;
892
893 group->num_crtcs = 0;
894 group->num_connectors = 0;
895 group->num_encoders = 0;
896 return 0;
897}
898
899int drm_mode_group_init_legacy_group(struct drm_device *dev,
900 struct drm_mode_group *group)
901{
902 struct drm_crtc *crtc;
903 struct drm_encoder *encoder;
904 struct drm_connector *connector;
905 int ret;
906
907 if ((ret = drm_mode_group_init(dev, group)))
908 return ret;
909
910 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
911 group->id_list[group->num_crtcs++] = crtc->base.id;
912
913 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
914 group->id_list[group->num_crtcs + group->num_encoders++] =
915 encoder->base.id;
916
917 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
918 group->id_list[group->num_crtcs + group->num_encoders +
919 group->num_connectors++] = connector->base.id;
920
921 return 0;
922}
923
924/**
925 * drm_mode_config_cleanup - free up DRM mode_config info
926 * @dev: DRM device
927 *
928 * LOCKING:
929 * Caller must hold mode config lock.
930 *
931 * Free up all the connectors and CRTCs associated with this DRM device, then
932 * free up the framebuffers and associated buffer objects.
933 *
934 * FIXME: cleanup any dangling user buffer objects too
935 */
936void drm_mode_config_cleanup(struct drm_device *dev)
937{
938 struct drm_connector *connector, *ot;
939 struct drm_crtc *crtc, *ct;
940 struct drm_encoder *encoder, *enct;
941 struct drm_framebuffer *fb, *fbt;
942 struct drm_property *property, *pt;
943
944 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
945 head) {
946 encoder->funcs->destroy(encoder);
947 }
948
949 list_for_each_entry_safe(connector, ot,
950 &dev->mode_config.connector_list, head) {
951 connector->funcs->destroy(connector);
952 }
953
954 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
955 head) {
956 drm_property_destroy(dev, property);
957 }
958
959 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
960 fb->funcs->destroy(fb);
961 }
962
963 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
964 crtc->funcs->destroy(crtc);
965 }
966
967}
968EXPORT_SYMBOL(drm_mode_config_cleanup);
969
Dave Airlief453ba02008-11-07 14:05:41 -0800970/**
971 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
972 * @out: drm_mode_modeinfo struct to return to the user
973 * @in: drm_display_mode to use
974 *
975 * LOCKING:
976 * None.
977 *
978 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
979 * the user.
980 */
981void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
982 struct drm_display_mode *in)
983{
984 out->clock = in->clock;
985 out->hdisplay = in->hdisplay;
986 out->hsync_start = in->hsync_start;
987 out->hsync_end = in->hsync_end;
988 out->htotal = in->htotal;
989 out->hskew = in->hskew;
990 out->vdisplay = in->vdisplay;
991 out->vsync_start = in->vsync_start;
992 out->vsync_end = in->vsync_end;
993 out->vtotal = in->vtotal;
994 out->vscan = in->vscan;
995 out->vrefresh = in->vrefresh;
996 out->flags = in->flags;
997 out->type = in->type;
998 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
999 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1000}
1001
1002/**
1003 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1004 * @out: drm_display_mode to return to the user
1005 * @in: drm_mode_modeinfo to use
1006 *
1007 * LOCKING:
1008 * None.
1009 *
1010 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1011 * the caller.
1012 */
1013void drm_crtc_convert_umode(struct drm_display_mode *out,
1014 struct drm_mode_modeinfo *in)
1015{
1016 out->clock = in->clock;
1017 out->hdisplay = in->hdisplay;
1018 out->hsync_start = in->hsync_start;
1019 out->hsync_end = in->hsync_end;
1020 out->htotal = in->htotal;
1021 out->hskew = in->hskew;
1022 out->vdisplay = in->vdisplay;
1023 out->vsync_start = in->vsync_start;
1024 out->vsync_end = in->vsync_end;
1025 out->vtotal = in->vtotal;
1026 out->vscan = in->vscan;
1027 out->vrefresh = in->vrefresh;
1028 out->flags = in->flags;
1029 out->type = in->type;
1030 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1031 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1032}
1033
1034/**
1035 * drm_mode_getresources - get graphics configuration
1036 * @inode: inode from the ioctl
1037 * @filp: file * from the ioctl
1038 * @cmd: cmd from ioctl
1039 * @arg: arg from ioctl
1040 *
1041 * LOCKING:
1042 * Takes mode config lock.
1043 *
1044 * Construct a set of configuration description structures and return
1045 * them to the user, including CRTC, connector and framebuffer configuration.
1046 *
1047 * Called by the user via ioctl.
1048 *
1049 * RETURNS:
1050 * Zero on success, errno on failure.
1051 */
1052int drm_mode_getresources(struct drm_device *dev, void *data,
1053 struct drm_file *file_priv)
1054{
1055 struct drm_mode_card_res *card_res = data;
1056 struct list_head *lh;
1057 struct drm_framebuffer *fb;
1058 struct drm_connector *connector;
1059 struct drm_crtc *crtc;
1060 struct drm_encoder *encoder;
1061 int ret = 0;
1062 int connector_count = 0;
1063 int crtc_count = 0;
1064 int fb_count = 0;
1065 int encoder_count = 0;
1066 int copied = 0, i;
1067 uint32_t __user *fb_id;
1068 uint32_t __user *crtc_id;
1069 uint32_t __user *connector_id;
1070 uint32_t __user *encoder_id;
1071 struct drm_mode_group *mode_group;
1072
1073 mutex_lock(&dev->mode_config.mutex);
1074
1075 /*
1076 * For the non-control nodes we need to limit the list of resources
1077 * by IDs in the group list for this node
1078 */
1079 list_for_each(lh, &file_priv->fbs)
1080 fb_count++;
1081
1082 mode_group = &file_priv->master->minor->mode_group;
1083 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1084
1085 list_for_each(lh, &dev->mode_config.crtc_list)
1086 crtc_count++;
1087
1088 list_for_each(lh, &dev->mode_config.connector_list)
1089 connector_count++;
1090
1091 list_for_each(lh, &dev->mode_config.encoder_list)
1092 encoder_count++;
1093 } else {
1094
1095 crtc_count = mode_group->num_crtcs;
1096 connector_count = mode_group->num_connectors;
1097 encoder_count = mode_group->num_encoders;
1098 }
1099
1100 card_res->max_height = dev->mode_config.max_height;
1101 card_res->min_height = dev->mode_config.min_height;
1102 card_res->max_width = dev->mode_config.max_width;
1103 card_res->min_width = dev->mode_config.min_width;
1104
1105 /* handle this in 4 parts */
1106 /* FBs */
1107 if (card_res->count_fbs >= fb_count) {
1108 copied = 0;
1109 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1110 list_for_each_entry(fb, &file_priv->fbs, head) {
1111 if (put_user(fb->base.id, fb_id + copied)) {
1112 ret = -EFAULT;
1113 goto out;
1114 }
1115 copied++;
1116 }
1117 }
1118 card_res->count_fbs = fb_count;
1119
1120 /* CRTCs */
1121 if (card_res->count_crtcs >= crtc_count) {
1122 copied = 0;
1123 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1124 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1125 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1126 head) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001127 DRM_DEBUG_KMS("CRTC ID is %d\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001128 if (put_user(crtc->base.id, crtc_id + copied)) {
1129 ret = -EFAULT;
1130 goto out;
1131 }
1132 copied++;
1133 }
1134 } else {
1135 for (i = 0; i < mode_group->num_crtcs; i++) {
1136 if (put_user(mode_group->id_list[i],
1137 crtc_id + copied)) {
1138 ret = -EFAULT;
1139 goto out;
1140 }
1141 copied++;
1142 }
1143 }
1144 }
1145 card_res->count_crtcs = crtc_count;
1146
1147 /* Encoders */
1148 if (card_res->count_encoders >= encoder_count) {
1149 copied = 0;
1150 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1151 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1152 list_for_each_entry(encoder,
1153 &dev->mode_config.encoder_list,
1154 head) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001155 DRM_DEBUG_KMS("ENCODER ID is %d\n",
Dave Airlief453ba02008-11-07 14:05:41 -08001156 encoder->base.id);
1157 if (put_user(encoder->base.id, encoder_id +
1158 copied)) {
1159 ret = -EFAULT;
1160 goto out;
1161 }
1162 copied++;
1163 }
1164 } else {
1165 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1166 if (put_user(mode_group->id_list[i],
1167 encoder_id + copied)) {
1168 ret = -EFAULT;
1169 goto out;
1170 }
1171 copied++;
1172 }
1173
1174 }
1175 }
1176 card_res->count_encoders = encoder_count;
1177
1178 /* Connectors */
1179 if (card_res->count_connectors >= connector_count) {
1180 copied = 0;
1181 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1182 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1183 list_for_each_entry(connector,
1184 &dev->mode_config.connector_list,
1185 head) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001186 DRM_DEBUG_KMS("CONNECTOR ID is %d\n",
Dave Airlief453ba02008-11-07 14:05:41 -08001187 connector->base.id);
1188 if (put_user(connector->base.id,
1189 connector_id + copied)) {
1190 ret = -EFAULT;
1191 goto out;
1192 }
1193 copied++;
1194 }
1195 } else {
1196 int start = mode_group->num_crtcs +
1197 mode_group->num_encoders;
1198 for (i = start; i < start + mode_group->num_connectors; i++) {
1199 if (put_user(mode_group->id_list[i],
1200 connector_id + copied)) {
1201 ret = -EFAULT;
1202 goto out;
1203 }
1204 copied++;
1205 }
1206 }
1207 }
1208 card_res->count_connectors = connector_count;
1209
Zhao Yakui58367ed2009-07-20 13:48:07 +08001210 DRM_DEBUG_KMS("Counted %d %d %d\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001211 card_res->count_connectors, card_res->count_encoders);
1212
1213out:
1214 mutex_unlock(&dev->mode_config.mutex);
1215 return ret;
1216}
1217
1218/**
1219 * drm_mode_getcrtc - get CRTC configuration
1220 * @inode: inode from the ioctl
1221 * @filp: file * from the ioctl
1222 * @cmd: cmd from ioctl
1223 * @arg: arg from ioctl
1224 *
1225 * LOCKING:
1226 * Caller? (FIXME)
1227 *
1228 * Construct a CRTC configuration structure to return to the user.
1229 *
1230 * Called by the user via ioctl.
1231 *
1232 * RETURNS:
1233 * Zero on success, errno on failure.
1234 */
1235int drm_mode_getcrtc(struct drm_device *dev,
1236 void *data, struct drm_file *file_priv)
1237{
1238 struct drm_mode_crtc *crtc_resp = data;
1239 struct drm_crtc *crtc;
1240 struct drm_mode_object *obj;
1241 int ret = 0;
1242
1243 mutex_lock(&dev->mode_config.mutex);
1244
1245 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1246 DRM_MODE_OBJECT_CRTC);
1247 if (!obj) {
1248 ret = -EINVAL;
1249 goto out;
1250 }
1251 crtc = obj_to_crtc(obj);
1252
1253 crtc_resp->x = crtc->x;
1254 crtc_resp->y = crtc->y;
1255 crtc_resp->gamma_size = crtc->gamma_size;
1256 if (crtc->fb)
1257 crtc_resp->fb_id = crtc->fb->base.id;
1258 else
1259 crtc_resp->fb_id = 0;
1260
1261 if (crtc->enabled) {
1262
1263 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1264 crtc_resp->mode_valid = 1;
1265
1266 } else {
1267 crtc_resp->mode_valid = 0;
1268 }
1269
1270out:
1271 mutex_unlock(&dev->mode_config.mutex);
1272 return ret;
1273}
1274
1275/**
1276 * drm_mode_getconnector - get connector configuration
1277 * @inode: inode from the ioctl
1278 * @filp: file * from the ioctl
1279 * @cmd: cmd from ioctl
1280 * @arg: arg from ioctl
1281 *
1282 * LOCKING:
1283 * Caller? (FIXME)
1284 *
1285 * Construct a connector configuration structure to return to the user.
1286 *
1287 * Called by the user via ioctl.
1288 *
1289 * RETURNS:
1290 * Zero on success, errno on failure.
1291 */
1292int drm_mode_getconnector(struct drm_device *dev, void *data,
1293 struct drm_file *file_priv)
1294{
1295 struct drm_mode_get_connector *out_resp = data;
1296 struct drm_mode_object *obj;
1297 struct drm_connector *connector;
1298 struct drm_display_mode *mode;
1299 int mode_count = 0;
1300 int props_count = 0;
1301 int encoders_count = 0;
1302 int ret = 0;
1303 int copied = 0;
1304 int i;
1305 struct drm_mode_modeinfo u_mode;
1306 struct drm_mode_modeinfo __user *mode_ptr;
1307 uint32_t __user *prop_ptr;
1308 uint64_t __user *prop_values;
1309 uint32_t __user *encoder_ptr;
1310
1311 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1312
Zhao Yakui58367ed2009-07-20 13:48:07 +08001313 DRM_DEBUG_KMS("connector id %d:\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001314
1315 mutex_lock(&dev->mode_config.mutex);
1316
1317 obj = drm_mode_object_find(dev, out_resp->connector_id,
1318 DRM_MODE_OBJECT_CONNECTOR);
1319 if (!obj) {
1320 ret = -EINVAL;
1321 goto out;
1322 }
1323 connector = obj_to_connector(obj);
1324
1325 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1326 if (connector->property_ids[i] != 0) {
1327 props_count++;
1328 }
1329 }
1330
1331 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1332 if (connector->encoder_ids[i] != 0) {
1333 encoders_count++;
1334 }
1335 }
1336
1337 if (out_resp->count_modes == 0) {
1338 connector->funcs->fill_modes(connector,
1339 dev->mode_config.max_width,
1340 dev->mode_config.max_height);
1341 }
1342
1343 /* delayed so we get modes regardless of pre-fill_modes state */
1344 list_for_each_entry(mode, &connector->modes, head)
1345 mode_count++;
1346
1347 out_resp->connector_id = connector->base.id;
1348 out_resp->connector_type = connector->connector_type;
1349 out_resp->connector_type_id = connector->connector_type_id;
1350 out_resp->mm_width = connector->display_info.width_mm;
1351 out_resp->mm_height = connector->display_info.height_mm;
1352 out_resp->subpixel = connector->display_info.subpixel_order;
1353 out_resp->connection = connector->status;
1354 if (connector->encoder)
1355 out_resp->encoder_id = connector->encoder->base.id;
1356 else
1357 out_resp->encoder_id = 0;
1358
1359 /*
1360 * This ioctl is called twice, once to determine how much space is
1361 * needed, and the 2nd time to fill it.
1362 */
1363 if ((out_resp->count_modes >= mode_count) && mode_count) {
1364 copied = 0;
1365 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1366 list_for_each_entry(mode, &connector->modes, head) {
1367 drm_crtc_convert_to_umode(&u_mode, mode);
1368 if (copy_to_user(mode_ptr + copied,
1369 &u_mode, sizeof(u_mode))) {
1370 ret = -EFAULT;
1371 goto out;
1372 }
1373 copied++;
1374 }
1375 }
1376 out_resp->count_modes = mode_count;
1377
1378 if ((out_resp->count_props >= props_count) && props_count) {
1379 copied = 0;
1380 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1381 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1382 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1383 if (connector->property_ids[i] != 0) {
1384 if (put_user(connector->property_ids[i],
1385 prop_ptr + copied)) {
1386 ret = -EFAULT;
1387 goto out;
1388 }
1389
1390 if (put_user(connector->property_values[i],
1391 prop_values + copied)) {
1392 ret = -EFAULT;
1393 goto out;
1394 }
1395 copied++;
1396 }
1397 }
1398 }
1399 out_resp->count_props = props_count;
1400
1401 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1402 copied = 0;
1403 encoder_ptr = (uint32_t *)(unsigned long)(out_resp->encoders_ptr);
1404 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1405 if (connector->encoder_ids[i] != 0) {
1406 if (put_user(connector->encoder_ids[i],
1407 encoder_ptr + copied)) {
1408 ret = -EFAULT;
1409 goto out;
1410 }
1411 copied++;
1412 }
1413 }
1414 }
1415 out_resp->count_encoders = encoders_count;
1416
1417out:
1418 mutex_unlock(&dev->mode_config.mutex);
1419 return ret;
1420}
1421
1422int drm_mode_getencoder(struct drm_device *dev, void *data,
1423 struct drm_file *file_priv)
1424{
1425 struct drm_mode_get_encoder *enc_resp = data;
1426 struct drm_mode_object *obj;
1427 struct drm_encoder *encoder;
1428 int ret = 0;
1429
1430 mutex_lock(&dev->mode_config.mutex);
1431 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1432 DRM_MODE_OBJECT_ENCODER);
1433 if (!obj) {
1434 ret = -EINVAL;
1435 goto out;
1436 }
1437 encoder = obj_to_encoder(obj);
1438
1439 if (encoder->crtc)
1440 enc_resp->crtc_id = encoder->crtc->base.id;
1441 else
1442 enc_resp->crtc_id = 0;
1443 enc_resp->encoder_type = encoder->encoder_type;
1444 enc_resp->encoder_id = encoder->base.id;
1445 enc_resp->possible_crtcs = encoder->possible_crtcs;
1446 enc_resp->possible_clones = encoder->possible_clones;
1447
1448out:
1449 mutex_unlock(&dev->mode_config.mutex);
1450 return ret;
1451}
1452
1453/**
1454 * drm_mode_setcrtc - set CRTC configuration
1455 * @inode: inode from the ioctl
1456 * @filp: file * from the ioctl
1457 * @cmd: cmd from ioctl
1458 * @arg: arg from ioctl
1459 *
1460 * LOCKING:
1461 * Caller? (FIXME)
1462 *
1463 * Build a new CRTC configuration based on user request.
1464 *
1465 * Called by the user via ioctl.
1466 *
1467 * RETURNS:
1468 * Zero on success, errno on failure.
1469 */
1470int drm_mode_setcrtc(struct drm_device *dev, void *data,
1471 struct drm_file *file_priv)
1472{
1473 struct drm_mode_config *config = &dev->mode_config;
1474 struct drm_mode_crtc *crtc_req = data;
1475 struct drm_mode_object *obj;
1476 struct drm_crtc *crtc, *crtcfb;
1477 struct drm_connector **connector_set = NULL, *connector;
1478 struct drm_framebuffer *fb = NULL;
1479 struct drm_display_mode *mode = NULL;
1480 struct drm_mode_set set;
1481 uint32_t __user *set_connectors_ptr;
1482 int ret = 0;
1483 int i;
1484
1485 mutex_lock(&dev->mode_config.mutex);
1486 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1487 DRM_MODE_OBJECT_CRTC);
1488 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001489 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001490 ret = -EINVAL;
1491 goto out;
1492 }
1493 crtc = obj_to_crtc(obj);
1494
1495 if (crtc_req->mode_valid) {
1496 /* If we have a mode we need a framebuffer. */
1497 /* If we pass -1, set the mode with the currently bound fb */
1498 if (crtc_req->fb_id == -1) {
1499 list_for_each_entry(crtcfb,
1500 &dev->mode_config.crtc_list, head) {
1501 if (crtcfb == crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001502 DRM_DEBUG_KMS("Using current fb for "
1503 "setmode\n");
Dave Airlief453ba02008-11-07 14:05:41 -08001504 fb = crtc->fb;
1505 }
1506 }
1507 } else {
1508 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1509 DRM_MODE_OBJECT_FB);
1510 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001511 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1512 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001513 ret = -EINVAL;
1514 goto out;
1515 }
1516 fb = obj_to_fb(obj);
1517 }
1518
1519 mode = drm_mode_create(dev);
1520 drm_crtc_convert_umode(mode, &crtc_req->mode);
1521 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1522 }
1523
1524 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001525 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08001526 ret = -EINVAL;
1527 goto out;
1528 }
1529
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01001530 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001531 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08001532 crtc_req->count_connectors);
1533 ret = -EINVAL;
1534 goto out;
1535 }
1536
1537 if (crtc_req->count_connectors > 0) {
1538 u32 out_id;
1539
1540 /* Avoid unbounded kernel memory allocation */
1541 if (crtc_req->count_connectors > config->num_connector) {
1542 ret = -EINVAL;
1543 goto out;
1544 }
1545
1546 connector_set = kmalloc(crtc_req->count_connectors *
1547 sizeof(struct drm_connector *),
1548 GFP_KERNEL);
1549 if (!connector_set) {
1550 ret = -ENOMEM;
1551 goto out;
1552 }
1553
1554 for (i = 0; i < crtc_req->count_connectors; i++) {
1555 set_connectors_ptr = (uint32_t *)(unsigned long)crtc_req->set_connectors_ptr;
1556 if (get_user(out_id, &set_connectors_ptr[i])) {
1557 ret = -EFAULT;
1558 goto out;
1559 }
1560
1561 obj = drm_mode_object_find(dev, out_id,
1562 DRM_MODE_OBJECT_CONNECTOR);
1563 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001564 DRM_DEBUG_KMS("Connector id %d unknown\n",
1565 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001566 ret = -EINVAL;
1567 goto out;
1568 }
1569 connector = obj_to_connector(obj);
1570
1571 connector_set[i] = connector;
1572 }
1573 }
1574
1575 set.crtc = crtc;
1576 set.x = crtc_req->x;
1577 set.y = crtc_req->y;
1578 set.mode = mode;
1579 set.connectors = connector_set;
1580 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10001581 set.fb = fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001582 ret = crtc->funcs->set_config(&set);
1583
1584out:
1585 kfree(connector_set);
1586 mutex_unlock(&dev->mode_config.mutex);
1587 return ret;
1588}
1589
1590int drm_mode_cursor_ioctl(struct drm_device *dev,
1591 void *data, struct drm_file *file_priv)
1592{
1593 struct drm_mode_cursor *req = data;
1594 struct drm_mode_object *obj;
1595 struct drm_crtc *crtc;
1596 int ret = 0;
1597
Dave Airlief453ba02008-11-07 14:05:41 -08001598 if (!req->flags) {
1599 DRM_ERROR("no operation set\n");
1600 return -EINVAL;
1601 }
1602
1603 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10001604 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08001605 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001606 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001607 ret = -EINVAL;
1608 goto out;
1609 }
1610 crtc = obj_to_crtc(obj);
1611
1612 if (req->flags & DRM_MODE_CURSOR_BO) {
1613 if (!crtc->funcs->cursor_set) {
1614 DRM_ERROR("crtc does not support cursor\n");
1615 ret = -ENXIO;
1616 goto out;
1617 }
1618 /* Turns off the cursor if handle is 0 */
1619 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1620 req->width, req->height);
1621 }
1622
1623 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1624 if (crtc->funcs->cursor_move) {
1625 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1626 } else {
1627 DRM_ERROR("crtc does not support cursor\n");
1628 ret = -EFAULT;
1629 goto out;
1630 }
1631 }
1632out:
1633 mutex_unlock(&dev->mode_config.mutex);
1634 return ret;
1635}
1636
1637/**
1638 * drm_mode_addfb - add an FB to the graphics configuration
1639 * @inode: inode from the ioctl
1640 * @filp: file * from the ioctl
1641 * @cmd: cmd from ioctl
1642 * @arg: arg from ioctl
1643 *
1644 * LOCKING:
1645 * Takes mode config lock.
1646 *
1647 * Add a new FB to the specified CRTC, given a user request.
1648 *
1649 * Called by the user via ioctl.
1650 *
1651 * RETURNS:
1652 * Zero on success, errno on failure.
1653 */
1654int drm_mode_addfb(struct drm_device *dev,
1655 void *data, struct drm_file *file_priv)
1656{
1657 struct drm_mode_fb_cmd *r = data;
1658 struct drm_mode_config *config = &dev->mode_config;
1659 struct drm_framebuffer *fb;
1660 int ret = 0;
1661
1662 if ((config->min_width > r->width) || (r->width > config->max_width)) {
1663 DRM_ERROR("mode new framebuffer width not within limits\n");
1664 return -EINVAL;
1665 }
1666 if ((config->min_height > r->height) || (r->height > config->max_height)) {
1667 DRM_ERROR("mode new framebuffer height not within limits\n");
1668 return -EINVAL;
1669 }
1670
1671 mutex_lock(&dev->mode_config.mutex);
1672
1673 /* TODO check buffer is sufficently large */
1674 /* TODO setup destructor callback */
1675
1676 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
1677 if (!fb) {
1678 DRM_ERROR("could not create framebuffer\n");
1679 ret = -EINVAL;
1680 goto out;
1681 }
1682
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10001683 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08001684 list_add(&fb->filp_head, &file_priv->fbs);
1685
1686out:
1687 mutex_unlock(&dev->mode_config.mutex);
1688 return ret;
1689}
1690
1691/**
1692 * drm_mode_rmfb - remove an FB from the configuration
1693 * @inode: inode from the ioctl
1694 * @filp: file * from the ioctl
1695 * @cmd: cmd from ioctl
1696 * @arg: arg from ioctl
1697 *
1698 * LOCKING:
1699 * Takes mode config lock.
1700 *
1701 * Remove the FB specified by the user.
1702 *
1703 * Called by the user via ioctl.
1704 *
1705 * RETURNS:
1706 * Zero on success, errno on failure.
1707 */
1708int drm_mode_rmfb(struct drm_device *dev,
1709 void *data, struct drm_file *file_priv)
1710{
1711 struct drm_mode_object *obj;
1712 struct drm_framebuffer *fb = NULL;
1713 struct drm_framebuffer *fbl = NULL;
1714 uint32_t *id = data;
1715 int ret = 0;
1716 int found = 0;
1717
1718 mutex_lock(&dev->mode_config.mutex);
1719 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
1720 /* TODO check that we realy get a framebuffer back. */
1721 if (!obj) {
1722 DRM_ERROR("mode invalid framebuffer id\n");
1723 ret = -EINVAL;
1724 goto out;
1725 }
1726 fb = obj_to_fb(obj);
1727
1728 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1729 if (fb == fbl)
1730 found = 1;
1731
1732 if (!found) {
1733 DRM_ERROR("tried to remove a fb that we didn't own\n");
1734 ret = -EINVAL;
1735 goto out;
1736 }
1737
1738 /* TODO release all crtc connected to the framebuffer */
1739 /* TODO unhock the destructor from the buffer object */
1740
1741 list_del(&fb->filp_head);
1742 fb->funcs->destroy(fb);
1743
1744out:
1745 mutex_unlock(&dev->mode_config.mutex);
1746 return ret;
1747}
1748
1749/**
1750 * drm_mode_getfb - get FB info
1751 * @inode: inode from the ioctl
1752 * @filp: file * from the ioctl
1753 * @cmd: cmd from ioctl
1754 * @arg: arg from ioctl
1755 *
1756 * LOCKING:
1757 * Caller? (FIXME)
1758 *
1759 * Lookup the FB given its ID and return info about it.
1760 *
1761 * Called by the user via ioctl.
1762 *
1763 * RETURNS:
1764 * Zero on success, errno on failure.
1765 */
1766int drm_mode_getfb(struct drm_device *dev,
1767 void *data, struct drm_file *file_priv)
1768{
1769 struct drm_mode_fb_cmd *r = data;
1770 struct drm_mode_object *obj;
1771 struct drm_framebuffer *fb;
1772 int ret = 0;
1773
1774 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10001775 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
Dave Airlief453ba02008-11-07 14:05:41 -08001776 if (!obj) {
1777 DRM_ERROR("invalid framebuffer id\n");
1778 ret = -EINVAL;
1779 goto out;
1780 }
1781 fb = obj_to_fb(obj);
1782
1783 r->height = fb->height;
1784 r->width = fb->width;
1785 r->depth = fb->depth;
1786 r->bpp = fb->bits_per_pixel;
1787 r->pitch = fb->pitch;
1788 fb->funcs->create_handle(fb, file_priv, &r->handle);
1789
1790out:
1791 mutex_unlock(&dev->mode_config.mutex);
1792 return ret;
1793}
1794
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001795int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
1796 void *data, struct drm_file *file_priv)
1797{
1798 struct drm_clip_rect __user *clips_ptr;
1799 struct drm_clip_rect *clips = NULL;
1800 struct drm_mode_fb_dirty_cmd *r = data;
1801 struct drm_mode_object *obj;
1802 struct drm_framebuffer *fb;
1803 unsigned flags;
1804 int num_clips;
1805 int ret = 0;
1806
1807 mutex_lock(&dev->mode_config.mutex);
1808 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
1809 if (!obj) {
1810 DRM_ERROR("invalid framebuffer id\n");
1811 ret = -EINVAL;
1812 goto out_err1;
1813 }
1814 fb = obj_to_fb(obj);
1815
1816 num_clips = r->num_clips;
1817 clips_ptr = (struct drm_clip_rect *)(unsigned long)r->clips_ptr;
1818
1819 if (!num_clips != !clips_ptr) {
1820 ret = -EINVAL;
1821 goto out_err1;
1822 }
1823
1824 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
1825
1826 /* If userspace annotates copy, clips must come in pairs */
1827 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
1828 ret = -EINVAL;
1829 goto out_err1;
1830 }
1831
1832 if (num_clips && clips_ptr) {
1833 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
1834 if (!clips) {
1835 ret = -ENOMEM;
1836 goto out_err1;
1837 }
1838
1839 ret = copy_from_user(clips, clips_ptr,
1840 num_clips * sizeof(*clips));
1841 if (ret)
1842 goto out_err2;
1843 }
1844
1845 if (fb->funcs->dirty) {
1846 ret = fb->funcs->dirty(fb, flags, r->color, clips, num_clips);
1847 } else {
1848 ret = -ENOSYS;
1849 goto out_err2;
1850 }
1851
1852out_err2:
1853 kfree(clips);
1854out_err1:
1855 mutex_unlock(&dev->mode_config.mutex);
1856 return ret;
1857}
1858
1859
Dave Airlief453ba02008-11-07 14:05:41 -08001860/**
1861 * drm_fb_release - remove and free the FBs on this file
1862 * @filp: file * from the ioctl
1863 *
1864 * LOCKING:
1865 * Takes mode config lock.
1866 *
1867 * Destroy all the FBs associated with @filp.
1868 *
1869 * Called by the user via ioctl.
1870 *
1871 * RETURNS:
1872 * Zero on success, errno on failure.
1873 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05001874void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08001875{
Dave Airlief453ba02008-11-07 14:05:41 -08001876 struct drm_device *dev = priv->minor->dev;
1877 struct drm_framebuffer *fb, *tfb;
1878
1879 mutex_lock(&dev->mode_config.mutex);
1880 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1881 list_del(&fb->filp_head);
1882 fb->funcs->destroy(fb);
1883 }
1884 mutex_unlock(&dev->mode_config.mutex);
1885}
1886
1887/**
1888 * drm_mode_attachmode - add a mode to the user mode list
1889 * @dev: DRM device
1890 * @connector: connector to add the mode to
1891 * @mode: mode to add
1892 *
1893 * Add @mode to @connector's user mode list.
1894 */
1895static int drm_mode_attachmode(struct drm_device *dev,
1896 struct drm_connector *connector,
1897 struct drm_display_mode *mode)
1898{
1899 int ret = 0;
1900
1901 list_add_tail(&mode->head, &connector->user_modes);
1902 return ret;
1903}
1904
1905int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1906 struct drm_display_mode *mode)
1907{
1908 struct drm_connector *connector;
1909 int ret = 0;
1910 struct drm_display_mode *dup_mode;
1911 int need_dup = 0;
1912 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1913 if (!connector->encoder)
1914 break;
1915 if (connector->encoder->crtc == crtc) {
1916 if (need_dup)
1917 dup_mode = drm_mode_duplicate(dev, mode);
1918 else
1919 dup_mode = mode;
1920 ret = drm_mode_attachmode(dev, connector, dup_mode);
1921 if (ret)
1922 return ret;
1923 need_dup = 1;
1924 }
1925 }
1926 return 0;
1927}
1928EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1929
1930static int drm_mode_detachmode(struct drm_device *dev,
1931 struct drm_connector *connector,
1932 struct drm_display_mode *mode)
1933{
1934 int found = 0;
1935 int ret = 0;
1936 struct drm_display_mode *match_mode, *t;
1937
1938 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
1939 if (drm_mode_equal(match_mode, mode)) {
1940 list_del(&match_mode->head);
1941 drm_mode_destroy(dev, match_mode);
1942 found = 1;
1943 break;
1944 }
1945 }
1946
1947 if (!found)
1948 ret = -EINVAL;
1949
1950 return ret;
1951}
1952
1953int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
1954{
1955 struct drm_connector *connector;
1956
1957 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1958 drm_mode_detachmode(dev, connector, mode);
1959 }
1960 return 0;
1961}
1962EXPORT_SYMBOL(drm_mode_detachmode_crtc);
1963
1964/**
1965 * drm_fb_attachmode - Attach a user mode to an connector
1966 * @inode: inode from the ioctl
1967 * @filp: file * from the ioctl
1968 * @cmd: cmd from ioctl
1969 * @arg: arg from ioctl
1970 *
1971 * This attaches a user specified mode to an connector.
1972 * Called by the user via ioctl.
1973 *
1974 * RETURNS:
1975 * Zero on success, errno on failure.
1976 */
1977int drm_mode_attachmode_ioctl(struct drm_device *dev,
1978 void *data, struct drm_file *file_priv)
1979{
1980 struct drm_mode_mode_cmd *mode_cmd = data;
1981 struct drm_connector *connector;
1982 struct drm_display_mode *mode;
1983 struct drm_mode_object *obj;
1984 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1985 int ret = 0;
1986
1987 mutex_lock(&dev->mode_config.mutex);
1988
1989 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1990 if (!obj) {
1991 ret = -EINVAL;
1992 goto out;
1993 }
1994 connector = obj_to_connector(obj);
1995
1996 mode = drm_mode_create(dev);
1997 if (!mode) {
1998 ret = -ENOMEM;
1999 goto out;
2000 }
2001
2002 drm_crtc_convert_umode(mode, umode);
2003
2004 ret = drm_mode_attachmode(dev, connector, mode);
2005out:
2006 mutex_unlock(&dev->mode_config.mutex);
2007 return ret;
2008}
2009
2010
2011/**
2012 * drm_fb_detachmode - Detach a user specified mode from an connector
2013 * @inode: inode from the ioctl
2014 * @filp: file * from the ioctl
2015 * @cmd: cmd from ioctl
2016 * @arg: arg from ioctl
2017 *
2018 * Called by the user via ioctl.
2019 *
2020 * RETURNS:
2021 * Zero on success, errno on failure.
2022 */
2023int drm_mode_detachmode_ioctl(struct drm_device *dev,
2024 void *data, struct drm_file *file_priv)
2025{
2026 struct drm_mode_object *obj;
2027 struct drm_mode_mode_cmd *mode_cmd = data;
2028 struct drm_connector *connector;
2029 struct drm_display_mode mode;
2030 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2031 int ret = 0;
2032
2033 mutex_lock(&dev->mode_config.mutex);
2034
2035 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2036 if (!obj) {
2037 ret = -EINVAL;
2038 goto out;
2039 }
2040 connector = obj_to_connector(obj);
2041
2042 drm_crtc_convert_umode(&mode, umode);
2043 ret = drm_mode_detachmode(dev, connector, &mode);
2044out:
2045 mutex_unlock(&dev->mode_config.mutex);
2046 return ret;
2047}
2048
2049struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2050 const char *name, int num_values)
2051{
2052 struct drm_property *property = NULL;
2053
2054 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2055 if (!property)
2056 return NULL;
2057
2058 if (num_values) {
2059 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2060 if (!property->values)
2061 goto fail;
2062 }
2063
2064 drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2065 property->flags = flags;
2066 property->num_values = num_values;
2067 INIT_LIST_HEAD(&property->enum_blob_list);
2068
2069 if (name)
2070 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2071
2072 list_add_tail(&property->head, &dev->mode_config.property_list);
2073 return property;
2074fail:
2075 kfree(property);
2076 return NULL;
2077}
2078EXPORT_SYMBOL(drm_property_create);
2079
2080int drm_property_add_enum(struct drm_property *property, int index,
2081 uint64_t value, const char *name)
2082{
2083 struct drm_property_enum *prop_enum;
2084
2085 if (!(property->flags & DRM_MODE_PROP_ENUM))
2086 return -EINVAL;
2087
2088 if (!list_empty(&property->enum_blob_list)) {
2089 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2090 if (prop_enum->value == value) {
2091 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2092 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2093 return 0;
2094 }
2095 }
2096 }
2097
2098 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2099 if (!prop_enum)
2100 return -ENOMEM;
2101
2102 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2103 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2104 prop_enum->value = value;
2105
2106 property->values[index] = value;
2107 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2108 return 0;
2109}
2110EXPORT_SYMBOL(drm_property_add_enum);
2111
2112void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2113{
2114 struct drm_property_enum *prop_enum, *pt;
2115
2116 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2117 list_del(&prop_enum->head);
2118 kfree(prop_enum);
2119 }
2120
2121 if (property->num_values)
2122 kfree(property->values);
2123 drm_mode_object_put(dev, &property->base);
2124 list_del(&property->head);
2125 kfree(property);
2126}
2127EXPORT_SYMBOL(drm_property_destroy);
2128
2129int drm_connector_attach_property(struct drm_connector *connector,
2130 struct drm_property *property, uint64_t init_val)
2131{
2132 int i;
2133
2134 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2135 if (connector->property_ids[i] == 0) {
2136 connector->property_ids[i] = property->base.id;
2137 connector->property_values[i] = init_val;
2138 break;
2139 }
2140 }
2141
2142 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2143 return -EINVAL;
2144 return 0;
2145}
2146EXPORT_SYMBOL(drm_connector_attach_property);
2147
2148int drm_connector_property_set_value(struct drm_connector *connector,
2149 struct drm_property *property, uint64_t value)
2150{
2151 int i;
2152
2153 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2154 if (connector->property_ids[i] == property->base.id) {
2155 connector->property_values[i] = value;
2156 break;
2157 }
2158 }
2159
2160 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2161 return -EINVAL;
2162 return 0;
2163}
2164EXPORT_SYMBOL(drm_connector_property_set_value);
2165
2166int drm_connector_property_get_value(struct drm_connector *connector,
2167 struct drm_property *property, uint64_t *val)
2168{
2169 int i;
2170
2171 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2172 if (connector->property_ids[i] == property->base.id) {
2173 *val = connector->property_values[i];
2174 break;
2175 }
2176 }
2177
2178 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2179 return -EINVAL;
2180 return 0;
2181}
2182EXPORT_SYMBOL(drm_connector_property_get_value);
2183
2184int drm_mode_getproperty_ioctl(struct drm_device *dev,
2185 void *data, struct drm_file *file_priv)
2186{
2187 struct drm_mode_object *obj;
2188 struct drm_mode_get_property *out_resp = data;
2189 struct drm_property *property;
2190 int enum_count = 0;
2191 int blob_count = 0;
2192 int value_count = 0;
2193 int ret = 0, i;
2194 int copied;
2195 struct drm_property_enum *prop_enum;
2196 struct drm_mode_property_enum __user *enum_ptr;
2197 struct drm_property_blob *prop_blob;
2198 uint32_t *blob_id_ptr;
2199 uint64_t __user *values_ptr;
2200 uint32_t __user *blob_length_ptr;
2201
2202 mutex_lock(&dev->mode_config.mutex);
2203 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2204 if (!obj) {
2205 ret = -EINVAL;
2206 goto done;
2207 }
2208 property = obj_to_property(obj);
2209
2210 if (property->flags & DRM_MODE_PROP_ENUM) {
2211 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2212 enum_count++;
2213 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2214 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2215 blob_count++;
2216 }
2217
2218 value_count = property->num_values;
2219
2220 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2221 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2222 out_resp->flags = property->flags;
2223
2224 if ((out_resp->count_values >= value_count) && value_count) {
2225 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2226 for (i = 0; i < value_count; i++) {
2227 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2228 ret = -EFAULT;
2229 goto done;
2230 }
2231 }
2232 }
2233 out_resp->count_values = value_count;
2234
2235 if (property->flags & DRM_MODE_PROP_ENUM) {
2236 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2237 copied = 0;
2238 enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2239 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2240
2241 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2242 ret = -EFAULT;
2243 goto done;
2244 }
2245
2246 if (copy_to_user(&enum_ptr[copied].name,
2247 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2248 ret = -EFAULT;
2249 goto done;
2250 }
2251 copied++;
2252 }
2253 }
2254 out_resp->count_enum_blobs = enum_count;
2255 }
2256
2257 if (property->flags & DRM_MODE_PROP_BLOB) {
2258 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2259 copied = 0;
2260 blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2261 blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2262
2263 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2264 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2265 ret = -EFAULT;
2266 goto done;
2267 }
2268
2269 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2270 ret = -EFAULT;
2271 goto done;
2272 }
2273
2274 copied++;
2275 }
2276 }
2277 out_resp->count_enum_blobs = blob_count;
2278 }
2279done:
2280 mutex_unlock(&dev->mode_config.mutex);
2281 return ret;
2282}
2283
2284static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2285 void *data)
2286{
2287 struct drm_property_blob *blob;
2288
2289 if (!length || !data)
2290 return NULL;
2291
2292 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2293 if (!blob)
2294 return NULL;
2295
2296 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2297 blob->length = length;
2298
2299 memcpy(blob->data, data, length);
2300
2301 drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2302
2303 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2304 return blob;
2305}
2306
2307static void drm_property_destroy_blob(struct drm_device *dev,
2308 struct drm_property_blob *blob)
2309{
2310 drm_mode_object_put(dev, &blob->base);
2311 list_del(&blob->head);
2312 kfree(blob);
2313}
2314
2315int drm_mode_getblob_ioctl(struct drm_device *dev,
2316 void *data, struct drm_file *file_priv)
2317{
2318 struct drm_mode_object *obj;
2319 struct drm_mode_get_blob *out_resp = data;
2320 struct drm_property_blob *blob;
2321 int ret = 0;
2322 void *blob_ptr;
2323
2324 mutex_lock(&dev->mode_config.mutex);
2325 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2326 if (!obj) {
2327 ret = -EINVAL;
2328 goto done;
2329 }
2330 blob = obj_to_blob(obj);
2331
2332 if (out_resp->length == blob->length) {
2333 blob_ptr = (void *)(unsigned long)out_resp->data;
2334 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2335 ret = -EFAULT;
2336 goto done;
2337 }
2338 }
2339 out_resp->length = blob->length;
2340
2341done:
2342 mutex_unlock(&dev->mode_config.mutex);
2343 return ret;
2344}
2345
2346int drm_mode_connector_update_edid_property(struct drm_connector *connector,
2347 struct edid *edid)
2348{
2349 struct drm_device *dev = connector->dev;
2350 int ret = 0;
2351
2352 if (connector->edid_blob_ptr)
2353 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
2354
2355 /* Delete edid, when there is none. */
2356 if (!edid) {
2357 connector->edid_blob_ptr = NULL;
2358 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
2359 return ret;
2360 }
2361
2362 connector->edid_blob_ptr = drm_property_create_blob(connector->dev, 128, edid);
2363
2364 ret = drm_connector_property_set_value(connector,
2365 dev->mode_config.edid_property,
2366 connector->edid_blob_ptr->base.id);
2367
2368 return ret;
2369}
2370EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
2371
2372int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2373 void *data, struct drm_file *file_priv)
2374{
2375 struct drm_mode_connector_set_property *out_resp = data;
2376 struct drm_mode_object *obj;
2377 struct drm_property *property;
2378 struct drm_connector *connector;
2379 int ret = -EINVAL;
2380 int i;
2381
2382 mutex_lock(&dev->mode_config.mutex);
2383
2384 obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2385 if (!obj) {
2386 goto out;
2387 }
2388 connector = obj_to_connector(obj);
2389
2390 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2391 if (connector->property_ids[i] == out_resp->prop_id)
2392 break;
2393 }
2394
2395 if (i == DRM_CONNECTOR_MAX_PROPERTY) {
2396 goto out;
2397 }
2398
2399 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2400 if (!obj) {
2401 goto out;
2402 }
2403 property = obj_to_property(obj);
2404
2405 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2406 goto out;
2407
2408 if (property->flags & DRM_MODE_PROP_RANGE) {
2409 if (out_resp->value < property->values[0])
2410 goto out;
2411
2412 if (out_resp->value > property->values[1])
2413 goto out;
2414 } else {
2415 int found = 0;
2416 for (i = 0; i < property->num_values; i++) {
2417 if (property->values[i] == out_resp->value) {
2418 found = 1;
2419 break;
2420 }
2421 }
2422 if (!found) {
2423 goto out;
2424 }
2425 }
2426
Keith Packardc9fb15f2009-05-30 20:42:28 -07002427 /* Do DPMS ourselves */
2428 if (property == connector->dev->mode_config.dpms_property) {
2429 if (connector->funcs->dpms)
2430 (*connector->funcs->dpms)(connector, (int) out_resp->value);
2431 ret = 0;
2432 } else if (connector->funcs->set_property)
Dave Airlief453ba02008-11-07 14:05:41 -08002433 ret = connector->funcs->set_property(connector, property, out_resp->value);
2434
2435 /* store the property value if succesful */
2436 if (!ret)
2437 drm_connector_property_set_value(connector, property, out_resp->value);
2438out:
2439 mutex_unlock(&dev->mode_config.mutex);
2440 return ret;
2441}
2442
Dave Airlief453ba02008-11-07 14:05:41 -08002443int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2444 struct drm_encoder *encoder)
2445{
2446 int i;
2447
2448 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2449 if (connector->encoder_ids[i] == 0) {
2450 connector->encoder_ids[i] = encoder->base.id;
2451 return 0;
2452 }
2453 }
2454 return -ENOMEM;
2455}
2456EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
2457
2458void drm_mode_connector_detach_encoder(struct drm_connector *connector,
2459 struct drm_encoder *encoder)
2460{
2461 int i;
2462 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2463 if (connector->encoder_ids[i] == encoder->base.id) {
2464 connector->encoder_ids[i] = 0;
2465 if (connector->encoder == encoder)
2466 connector->encoder = NULL;
2467 break;
2468 }
2469 }
2470}
2471EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
2472
2473bool drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
2474 int gamma_size)
2475{
2476 crtc->gamma_size = gamma_size;
2477
2478 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
2479 if (!crtc->gamma_store) {
2480 crtc->gamma_size = 0;
2481 return false;
2482 }
2483
2484 return true;
2485}
2486EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
2487
2488int drm_mode_gamma_set_ioctl(struct drm_device *dev,
2489 void *data, struct drm_file *file_priv)
2490{
2491 struct drm_mode_crtc_lut *crtc_lut = data;
2492 struct drm_mode_object *obj;
2493 struct drm_crtc *crtc;
2494 void *r_base, *g_base, *b_base;
2495 int size;
2496 int ret = 0;
2497
2498 mutex_lock(&dev->mode_config.mutex);
2499 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2500 if (!obj) {
2501 ret = -EINVAL;
2502 goto out;
2503 }
2504 crtc = obj_to_crtc(obj);
2505
2506 /* memcpy into gamma store */
2507 if (crtc_lut->gamma_size != crtc->gamma_size) {
2508 ret = -EINVAL;
2509 goto out;
2510 }
2511
2512 size = crtc_lut->gamma_size * (sizeof(uint16_t));
2513 r_base = crtc->gamma_store;
2514 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
2515 ret = -EFAULT;
2516 goto out;
2517 }
2518
2519 g_base = r_base + size;
2520 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
2521 ret = -EFAULT;
2522 goto out;
2523 }
2524
2525 b_base = g_base + size;
2526 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
2527 ret = -EFAULT;
2528 goto out;
2529 }
2530
2531 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
2532
2533out:
2534 mutex_unlock(&dev->mode_config.mutex);
2535 return ret;
2536
2537}
2538
2539int drm_mode_gamma_get_ioctl(struct drm_device *dev,
2540 void *data, struct drm_file *file_priv)
2541{
2542 struct drm_mode_crtc_lut *crtc_lut = data;
2543 struct drm_mode_object *obj;
2544 struct drm_crtc *crtc;
2545 void *r_base, *g_base, *b_base;
2546 int size;
2547 int ret = 0;
2548
2549 mutex_lock(&dev->mode_config.mutex);
2550 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2551 if (!obj) {
2552 ret = -EINVAL;
2553 goto out;
2554 }
2555 crtc = obj_to_crtc(obj);
2556
2557 /* memcpy into gamma store */
2558 if (crtc_lut->gamma_size != crtc->gamma_size) {
2559 ret = -EINVAL;
2560 goto out;
2561 }
2562
2563 size = crtc_lut->gamma_size * (sizeof(uint16_t));
2564 r_base = crtc->gamma_store;
2565 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
2566 ret = -EFAULT;
2567 goto out;
2568 }
2569
2570 g_base = r_base + size;
2571 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
2572 ret = -EFAULT;
2573 goto out;
2574 }
2575
2576 b_base = g_base + size;
2577 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
2578 ret = -EFAULT;
2579 goto out;
2580 }
2581out:
2582 mutex_unlock(&dev->mode_config.mutex);
2583 return ret;
2584}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05002585
2586int drm_mode_page_flip_ioctl(struct drm_device *dev,
2587 void *data, struct drm_file *file_priv)
2588{
2589 struct drm_mode_crtc_page_flip *page_flip = data;
2590 struct drm_mode_object *obj;
2591 struct drm_crtc *crtc;
2592 struct drm_framebuffer *fb;
2593 struct drm_pending_vblank_event *e = NULL;
2594 unsigned long flags;
2595 int ret = -EINVAL;
2596
2597 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
2598 page_flip->reserved != 0)
2599 return -EINVAL;
2600
2601 mutex_lock(&dev->mode_config.mutex);
2602 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
2603 if (!obj)
2604 goto out;
2605 crtc = obj_to_crtc(obj);
2606
2607 if (crtc->funcs->page_flip == NULL)
2608 goto out;
2609
2610 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
2611 if (!obj)
2612 goto out;
2613 fb = obj_to_fb(obj);
2614
2615 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
2616 ret = -ENOMEM;
2617 spin_lock_irqsave(&dev->event_lock, flags);
2618 if (file_priv->event_space < sizeof e->event) {
2619 spin_unlock_irqrestore(&dev->event_lock, flags);
2620 goto out;
2621 }
2622 file_priv->event_space -= sizeof e->event;
2623 spin_unlock_irqrestore(&dev->event_lock, flags);
2624
2625 e = kzalloc(sizeof *e, GFP_KERNEL);
2626 if (e == NULL) {
2627 spin_lock_irqsave(&dev->event_lock, flags);
2628 file_priv->event_space += sizeof e->event;
2629 spin_unlock_irqrestore(&dev->event_lock, flags);
2630 goto out;
2631 }
2632
2633 e->event.base.type = DRM_EVENT_VBLANK;
2634 e->event.base.length = sizeof e->event;
2635 e->event.user_data = page_flip->user_data;
2636 e->base.event = &e->event.base;
2637 e->base.file_priv = file_priv;
2638 e->base.destroy =
2639 (void (*) (struct drm_pending_event *)) kfree;
2640 }
2641
2642 ret = crtc->funcs->page_flip(crtc, fb, e);
2643 if (ret) {
2644 spin_lock_irqsave(&dev->event_lock, flags);
2645 file_priv->event_space += sizeof e->event;
2646 spin_unlock_irqrestore(&dev->event_lock, flags);
2647 kfree(e);
2648 }
2649
2650out:
2651 mutex_unlock(&dev->mode_config.mutex);
2652 return ret;
2653}