blob: e8cd6832f08ee42daaad742ac2b24da83cf3ccde [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 },
Alex Deucher7970e672010-01-07 13:47:47 -0500161 { DRM_MODE_CONNECTOR_eDP, "Embedded DisplayPort", 0 },
Dave Airlief453ba02008-11-07 14:05:41 -0800162};
163
164static struct drm_prop_enum_list drm_encoder_enum_list[] =
165{ { DRM_MODE_ENCODER_NONE, "None" },
166 { DRM_MODE_ENCODER_DAC, "DAC" },
167 { DRM_MODE_ENCODER_TMDS, "TMDS" },
168 { DRM_MODE_ENCODER_LVDS, "LVDS" },
169 { DRM_MODE_ENCODER_TVDAC, "TV" },
170};
171
172char *drm_get_encoder_name(struct drm_encoder *encoder)
173{
174 static char buf[32];
175
176 snprintf(buf, 32, "%s-%d",
177 drm_encoder_enum_list[encoder->encoder_type].name,
178 encoder->base.id);
179 return buf;
180}
Dave Airlie13a81952009-09-07 15:45:33 +1000181EXPORT_SYMBOL(drm_get_encoder_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800182
183char *drm_get_connector_name(struct drm_connector *connector)
184{
185 static char buf[32];
186
187 snprintf(buf, 32, "%s-%d",
188 drm_connector_enum_list[connector->connector_type].name,
189 connector->connector_type_id);
190 return buf;
191}
192EXPORT_SYMBOL(drm_get_connector_name);
193
194char *drm_get_connector_status_name(enum drm_connector_status status)
195{
196 if (status == connector_status_connected)
197 return "connected";
198 else if (status == connector_status_disconnected)
199 return "disconnected";
200 else
201 return "unknown";
202}
203
204/**
205 * drm_mode_object_get - allocate a new identifier
206 * @dev: DRM device
207 * @ptr: object pointer, used to generate unique ID
208 * @type: object type
209 *
210 * LOCKING:
Dave Airlief453ba02008-11-07 14:05:41 -0800211 *
212 * Create a unique identifier based on @ptr in @dev's identifier space. Used
213 * for tracking modes, CRTCs and connectors.
214 *
215 * RETURNS:
216 * New unique (relative to other objects in @dev) integer identifier for the
217 * object.
218 */
219static int drm_mode_object_get(struct drm_device *dev,
220 struct drm_mode_object *obj, uint32_t obj_type)
221{
222 int new_id = 0;
223 int ret;
224
Dave Airlief453ba02008-11-07 14:05:41 -0800225again:
226 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
227 DRM_ERROR("Ran out memory getting a mode number\n");
228 return -EINVAL;
229 }
230
Jesse Barnesad2563c2009-01-19 17:21:45 +1000231 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800232 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000233 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800234 if (ret == -EAGAIN)
235 goto again;
236
237 obj->id = new_id;
238 obj->type = obj_type;
239 return 0;
240}
241
242/**
243 * drm_mode_object_put - free an identifer
244 * @dev: DRM device
245 * @id: ID to free
246 *
247 * LOCKING:
248 * Caller must hold DRM mode_config lock.
249 *
250 * Free @id from @dev's unique identifier pool.
251 */
252static void drm_mode_object_put(struct drm_device *dev,
253 struct drm_mode_object *object)
254{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000255 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800256 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000257 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800258}
259
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200260struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
261 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800262{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000263 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800264
Jesse Barnesad2563c2009-01-19 17:21:45 +1000265 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800266 obj = idr_find(&dev->mode_config.crtc_idr, id);
267 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000268 obj = NULL;
269 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800270
271 return obj;
272}
273EXPORT_SYMBOL(drm_mode_object_find);
274
275/**
Dave Airlief453ba02008-11-07 14:05:41 -0800276 * drm_framebuffer_init - initialize a framebuffer
277 * @dev: DRM device
278 *
279 * LOCKING:
280 * Caller must hold mode config lock.
281 *
282 * Allocates an ID for the framebuffer's parent mode object, sets its mode
283 * functions & device file and adds it to the master fd list.
284 *
285 * RETURNS:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200286 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800287 */
288int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
289 const struct drm_framebuffer_funcs *funcs)
290{
291 int ret;
292
293 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
294 if (ret) {
295 return ret;
296 }
297
298 fb->dev = dev;
299 fb->funcs = funcs;
300 dev->mode_config.num_fb++;
301 list_add(&fb->head, &dev->mode_config.fb_list);
302
303 return 0;
304}
305EXPORT_SYMBOL(drm_framebuffer_init);
306
307/**
308 * drm_framebuffer_cleanup - remove a framebuffer object
309 * @fb: framebuffer to remove
310 *
311 * LOCKING:
312 * Caller must hold mode config lock.
313 *
314 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
315 * it, setting it to NULL.
316 */
317void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
318{
319 struct drm_device *dev = fb->dev;
320 struct drm_crtc *crtc;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000321 struct drm_mode_set set;
322 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800323
324 /* remove from any CRTC */
325 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie5ef5f722009-08-17 13:11:23 +1000326 if (crtc->fb == fb) {
327 /* should turn off the crtc */
328 memset(&set, 0, sizeof(struct drm_mode_set));
329 set.crtc = crtc;
330 set.fb = NULL;
331 ret = crtc->funcs->set_config(&set);
332 if (ret)
333 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
334 }
Dave Airlief453ba02008-11-07 14:05:41 -0800335 }
336
337 drm_mode_object_put(dev, &fb->base);
338 list_del(&fb->head);
339 dev->mode_config.num_fb--;
340}
341EXPORT_SYMBOL(drm_framebuffer_cleanup);
342
343/**
344 * drm_crtc_init - Initialise a new CRTC object
345 * @dev: DRM device
346 * @crtc: CRTC object to init
347 * @funcs: callbacks for the new CRTC
348 *
349 * LOCKING:
350 * Caller must hold mode config lock.
351 *
352 * Inits a new object created as base part of an driver crtc object.
353 */
354void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
355 const struct drm_crtc_funcs *funcs)
356{
357 crtc->dev = dev;
358 crtc->funcs = funcs;
359
360 mutex_lock(&dev->mode_config.mutex);
361 drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
362
363 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
364 dev->mode_config.num_crtc++;
365 mutex_unlock(&dev->mode_config.mutex);
366}
367EXPORT_SYMBOL(drm_crtc_init);
368
369/**
370 * drm_crtc_cleanup - Cleans up the core crtc usage.
371 * @crtc: CRTC to cleanup
372 *
373 * LOCKING:
374 * Caller must hold mode config lock.
375 *
376 * Cleanup @crtc. Removes from drm modesetting space
377 * does NOT free object, caller does that.
378 */
379void drm_crtc_cleanup(struct drm_crtc *crtc)
380{
381 struct drm_device *dev = crtc->dev;
382
383 if (crtc->gamma_store) {
384 kfree(crtc->gamma_store);
385 crtc->gamma_store = NULL;
386 }
387
388 drm_mode_object_put(dev, &crtc->base);
389 list_del(&crtc->head);
390 dev->mode_config.num_crtc--;
391}
392EXPORT_SYMBOL(drm_crtc_cleanup);
393
394/**
395 * drm_mode_probed_add - add a mode to a connector's probed mode list
396 * @connector: connector the new mode
397 * @mode: mode data
398 *
399 * LOCKING:
400 * Caller must hold mode config lock.
401 *
402 * Add @mode to @connector's mode list for later use.
403 */
404void drm_mode_probed_add(struct drm_connector *connector,
405 struct drm_display_mode *mode)
406{
407 list_add(&mode->head, &connector->probed_modes);
408}
409EXPORT_SYMBOL(drm_mode_probed_add);
410
411/**
412 * drm_mode_remove - remove and free a mode
413 * @connector: connector list to modify
414 * @mode: mode to remove
415 *
416 * LOCKING:
417 * Caller must hold mode config lock.
418 *
419 * Remove @mode from @connector's mode list, then free it.
420 */
421void drm_mode_remove(struct drm_connector *connector,
422 struct drm_display_mode *mode)
423{
424 list_del(&mode->head);
425 kfree(mode);
426}
427EXPORT_SYMBOL(drm_mode_remove);
428
429/**
430 * drm_connector_init - Init a preallocated connector
431 * @dev: DRM device
432 * @connector: the connector to init
433 * @funcs: callbacks for this connector
434 * @name: user visible name of the connector
435 *
436 * LOCKING:
437 * Caller must hold @dev's mode_config lock.
438 *
439 * Initialises a preallocated connector. Connectors should be
440 * subclassed as part of driver connector objects.
441 */
442void drm_connector_init(struct drm_device *dev,
443 struct drm_connector *connector,
444 const struct drm_connector_funcs *funcs,
445 int connector_type)
446{
447 mutex_lock(&dev->mode_config.mutex);
448
449 connector->dev = dev;
450 connector->funcs = funcs;
451 drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
452 connector->connector_type = connector_type;
453 connector->connector_type_id =
454 ++drm_connector_enum_list[connector_type].count; /* TODO */
455 INIT_LIST_HEAD(&connector->user_modes);
456 INIT_LIST_HEAD(&connector->probed_modes);
457 INIT_LIST_HEAD(&connector->modes);
458 connector->edid_blob_ptr = NULL;
459
460 list_add_tail(&connector->head, &dev->mode_config.connector_list);
461 dev->mode_config.num_connector++;
462
463 drm_connector_attach_property(connector,
464 dev->mode_config.edid_property, 0);
465
466 drm_connector_attach_property(connector,
467 dev->mode_config.dpms_property, 0);
468
469 mutex_unlock(&dev->mode_config.mutex);
470}
471EXPORT_SYMBOL(drm_connector_init);
472
473/**
474 * drm_connector_cleanup - cleans up an initialised connector
475 * @connector: connector to cleanup
476 *
477 * LOCKING:
478 * Caller must hold @dev's mode_config lock.
479 *
480 * Cleans up the connector but doesn't free the object.
481 */
482void drm_connector_cleanup(struct drm_connector *connector)
483{
484 struct drm_device *dev = connector->dev;
485 struct drm_display_mode *mode, *t;
486
487 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
488 drm_mode_remove(connector, mode);
489
490 list_for_each_entry_safe(mode, t, &connector->modes, head)
491 drm_mode_remove(connector, mode);
492
493 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
494 drm_mode_remove(connector, mode);
495
496 mutex_lock(&dev->mode_config.mutex);
497 drm_mode_object_put(dev, &connector->base);
498 list_del(&connector->head);
499 mutex_unlock(&dev->mode_config.mutex);
500}
501EXPORT_SYMBOL(drm_connector_cleanup);
502
503void drm_encoder_init(struct drm_device *dev,
504 struct drm_encoder *encoder,
505 const struct drm_encoder_funcs *funcs,
506 int encoder_type)
507{
508 mutex_lock(&dev->mode_config.mutex);
509
510 encoder->dev = dev;
511
512 drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
513 encoder->encoder_type = encoder_type;
514 encoder->funcs = funcs;
515
516 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
517 dev->mode_config.num_encoder++;
518
519 mutex_unlock(&dev->mode_config.mutex);
520}
521EXPORT_SYMBOL(drm_encoder_init);
522
523void drm_encoder_cleanup(struct drm_encoder *encoder)
524{
525 struct drm_device *dev = encoder->dev;
526 mutex_lock(&dev->mode_config.mutex);
527 drm_mode_object_put(dev, &encoder->base);
528 list_del(&encoder->head);
529 mutex_unlock(&dev->mode_config.mutex);
530}
531EXPORT_SYMBOL(drm_encoder_cleanup);
532
533/**
534 * drm_mode_create - create a new display mode
535 * @dev: DRM device
536 *
537 * LOCKING:
538 * Caller must hold DRM mode_config lock.
539 *
540 * Create a new drm_display_mode, give it an ID, and return it.
541 *
542 * RETURNS:
543 * Pointer to new mode on success, NULL on error.
544 */
545struct drm_display_mode *drm_mode_create(struct drm_device *dev)
546{
547 struct drm_display_mode *nmode;
548
549 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
550 if (!nmode)
551 return NULL;
552
553 drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
554 return nmode;
555}
556EXPORT_SYMBOL(drm_mode_create);
557
558/**
559 * drm_mode_destroy - remove a mode
560 * @dev: DRM device
561 * @mode: mode to remove
562 *
563 * LOCKING:
564 * Caller must hold mode config lock.
565 *
566 * Free @mode's unique identifier, then free it.
567 */
568void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
569{
570 drm_mode_object_put(dev, &mode->base);
571
572 kfree(mode);
573}
574EXPORT_SYMBOL(drm_mode_destroy);
575
576static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
577{
578 struct drm_property *edid;
579 struct drm_property *dpms;
580 int i;
581
582 /*
583 * Standard properties (apply to all connectors)
584 */
585 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
586 DRM_MODE_PROP_IMMUTABLE,
587 "EDID", 0);
588 dev->mode_config.edid_property = edid;
589
590 dpms = drm_property_create(dev, DRM_MODE_PROP_ENUM,
591 "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
592 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
593 drm_property_add_enum(dpms, i, drm_dpms_enum_list[i].type,
594 drm_dpms_enum_list[i].name);
595 dev->mode_config.dpms_property = dpms;
596
597 return 0;
598}
599
600/**
601 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
602 * @dev: DRM device
603 *
604 * Called by a driver the first time a DVI-I connector is made.
605 */
606int drm_mode_create_dvi_i_properties(struct drm_device *dev)
607{
608 struct drm_property *dvi_i_selector;
609 struct drm_property *dvi_i_subconnector;
610 int i;
611
612 if (dev->mode_config.dvi_i_select_subconnector_property)
613 return 0;
614
615 dvi_i_selector =
616 drm_property_create(dev, DRM_MODE_PROP_ENUM,
617 "select subconnector",
618 ARRAY_SIZE(drm_dvi_i_select_enum_list));
619 for (i = 0; i < ARRAY_SIZE(drm_dvi_i_select_enum_list); i++)
620 drm_property_add_enum(dvi_i_selector, i,
621 drm_dvi_i_select_enum_list[i].type,
622 drm_dvi_i_select_enum_list[i].name);
623 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
624
625 dvi_i_subconnector =
626 drm_property_create(dev, DRM_MODE_PROP_ENUM |
627 DRM_MODE_PROP_IMMUTABLE,
628 "subconnector",
629 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
630 for (i = 0; i < ARRAY_SIZE(drm_dvi_i_subconnector_enum_list); i++)
631 drm_property_add_enum(dvi_i_subconnector, i,
632 drm_dvi_i_subconnector_enum_list[i].type,
633 drm_dvi_i_subconnector_enum_list[i].name);
634 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
635
636 return 0;
637}
638EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
639
640/**
641 * drm_create_tv_properties - create TV specific connector properties
642 * @dev: DRM device
643 * @num_modes: number of different TV formats (modes) supported
644 * @modes: array of pointers to strings containing name of each format
645 *
646 * Called by a driver's TV initialization routine, this function creates
647 * the TV specific connector properties for a given device. Caller is
648 * responsible for allocating a list of format names and passing them to
649 * this routine.
650 */
651int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
652 char *modes[])
653{
654 struct drm_property *tv_selector;
655 struct drm_property *tv_subconnector;
656 int i;
657
658 if (dev->mode_config.tv_select_subconnector_property)
659 return 0;
660
661 /*
662 * Basic connector properties
663 */
664 tv_selector = drm_property_create(dev, DRM_MODE_PROP_ENUM,
665 "select subconnector",
666 ARRAY_SIZE(drm_tv_select_enum_list));
667 for (i = 0; i < ARRAY_SIZE(drm_tv_select_enum_list); i++)
668 drm_property_add_enum(tv_selector, i,
669 drm_tv_select_enum_list[i].type,
670 drm_tv_select_enum_list[i].name);
671 dev->mode_config.tv_select_subconnector_property = tv_selector;
672
673 tv_subconnector =
674 drm_property_create(dev, DRM_MODE_PROP_ENUM |
675 DRM_MODE_PROP_IMMUTABLE, "subconnector",
676 ARRAY_SIZE(drm_tv_subconnector_enum_list));
677 for (i = 0; i < ARRAY_SIZE(drm_tv_subconnector_enum_list); i++)
678 drm_property_add_enum(tv_subconnector, i,
679 drm_tv_subconnector_enum_list[i].type,
680 drm_tv_subconnector_enum_list[i].name);
681 dev->mode_config.tv_subconnector_property = tv_subconnector;
682
683 /*
684 * Other, TV specific properties: margins & TV modes.
685 */
686 dev->mode_config.tv_left_margin_property =
687 drm_property_create(dev, DRM_MODE_PROP_RANGE,
688 "left margin", 2);
689 dev->mode_config.tv_left_margin_property->values[0] = 0;
690 dev->mode_config.tv_left_margin_property->values[1] = 100;
691
692 dev->mode_config.tv_right_margin_property =
693 drm_property_create(dev, DRM_MODE_PROP_RANGE,
694 "right margin", 2);
695 dev->mode_config.tv_right_margin_property->values[0] = 0;
696 dev->mode_config.tv_right_margin_property->values[1] = 100;
697
698 dev->mode_config.tv_top_margin_property =
699 drm_property_create(dev, DRM_MODE_PROP_RANGE,
700 "top margin", 2);
701 dev->mode_config.tv_top_margin_property->values[0] = 0;
702 dev->mode_config.tv_top_margin_property->values[1] = 100;
703
704 dev->mode_config.tv_bottom_margin_property =
705 drm_property_create(dev, DRM_MODE_PROP_RANGE,
706 "bottom margin", 2);
707 dev->mode_config.tv_bottom_margin_property->values[0] = 0;
708 dev->mode_config.tv_bottom_margin_property->values[1] = 100;
709
710 dev->mode_config.tv_mode_property =
711 drm_property_create(dev, DRM_MODE_PROP_ENUM,
712 "mode", num_modes);
713 for (i = 0; i < num_modes; i++)
714 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
715 i, modes[i]);
716
Francisco Jerezb6b79022009-08-02 04:19:20 +0200717 dev->mode_config.tv_brightness_property =
718 drm_property_create(dev, DRM_MODE_PROP_RANGE,
719 "brightness", 2);
720 dev->mode_config.tv_brightness_property->values[0] = 0;
721 dev->mode_config.tv_brightness_property->values[1] = 100;
722
723 dev->mode_config.tv_contrast_property =
724 drm_property_create(dev, DRM_MODE_PROP_RANGE,
725 "contrast", 2);
726 dev->mode_config.tv_contrast_property->values[0] = 0;
727 dev->mode_config.tv_contrast_property->values[1] = 100;
728
729 dev->mode_config.tv_flicker_reduction_property =
730 drm_property_create(dev, DRM_MODE_PROP_RANGE,
731 "flicker reduction", 2);
732 dev->mode_config.tv_flicker_reduction_property->values[0] = 0;
733 dev->mode_config.tv_flicker_reduction_property->values[1] = 100;
734
Francisco Jereza75f0232009-08-12 02:30:10 +0200735 dev->mode_config.tv_overscan_property =
736 drm_property_create(dev, DRM_MODE_PROP_RANGE,
737 "overscan", 2);
738 dev->mode_config.tv_overscan_property->values[0] = 0;
739 dev->mode_config.tv_overscan_property->values[1] = 100;
740
741 dev->mode_config.tv_saturation_property =
742 drm_property_create(dev, DRM_MODE_PROP_RANGE,
743 "saturation", 2);
744 dev->mode_config.tv_saturation_property->values[0] = 0;
745 dev->mode_config.tv_saturation_property->values[1] = 100;
746
747 dev->mode_config.tv_hue_property =
748 drm_property_create(dev, DRM_MODE_PROP_RANGE,
749 "hue", 2);
750 dev->mode_config.tv_hue_property->values[0] = 0;
751 dev->mode_config.tv_hue_property->values[1] = 100;
752
Dave Airlief453ba02008-11-07 14:05:41 -0800753 return 0;
754}
755EXPORT_SYMBOL(drm_mode_create_tv_properties);
756
757/**
758 * drm_mode_create_scaling_mode_property - create scaling mode property
759 * @dev: DRM device
760 *
761 * Called by a driver the first time it's needed, must be attached to desired
762 * connectors.
763 */
764int drm_mode_create_scaling_mode_property(struct drm_device *dev)
765{
766 struct drm_property *scaling_mode;
767 int i;
768
769 if (dev->mode_config.scaling_mode_property)
770 return 0;
771
772 scaling_mode =
773 drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
774 ARRAY_SIZE(drm_scaling_mode_enum_list));
775 for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++)
776 drm_property_add_enum(scaling_mode, i,
777 drm_scaling_mode_enum_list[i].type,
778 drm_scaling_mode_enum_list[i].name);
779
780 dev->mode_config.scaling_mode_property = scaling_mode;
781
782 return 0;
783}
784EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
785
786/**
787 * drm_mode_create_dithering_property - create dithering property
788 * @dev: DRM device
789 *
790 * Called by a driver the first time it's needed, must be attached to desired
791 * connectors.
792 */
793int drm_mode_create_dithering_property(struct drm_device *dev)
794{
795 struct drm_property *dithering_mode;
796 int i;
797
798 if (dev->mode_config.dithering_mode_property)
799 return 0;
800
801 dithering_mode =
802 drm_property_create(dev, DRM_MODE_PROP_ENUM, "dithering",
803 ARRAY_SIZE(drm_dithering_mode_enum_list));
804 for (i = 0; i < ARRAY_SIZE(drm_dithering_mode_enum_list); i++)
805 drm_property_add_enum(dithering_mode, i,
806 drm_dithering_mode_enum_list[i].type,
807 drm_dithering_mode_enum_list[i].name);
808 dev->mode_config.dithering_mode_property = dithering_mode;
809
810 return 0;
811}
812EXPORT_SYMBOL(drm_mode_create_dithering_property);
813
814/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000815 * drm_mode_create_dirty_property - create dirty property
816 * @dev: DRM device
817 *
818 * Called by a driver the first time it's needed, must be attached to desired
819 * connectors.
820 */
821int drm_mode_create_dirty_info_property(struct drm_device *dev)
822{
823 struct drm_property *dirty_info;
824 int i;
825
826 if (dev->mode_config.dirty_info_property)
827 return 0;
828
829 dirty_info =
830 drm_property_create(dev, DRM_MODE_PROP_ENUM |
831 DRM_MODE_PROP_IMMUTABLE,
832 "dirty",
833 ARRAY_SIZE(drm_dirty_info_enum_list));
834 for (i = 0; i < ARRAY_SIZE(drm_dirty_info_enum_list); i++)
835 drm_property_add_enum(dirty_info, i,
836 drm_dirty_info_enum_list[i].type,
837 drm_dirty_info_enum_list[i].name);
838 dev->mode_config.dirty_info_property = dirty_info;
839
840 return 0;
841}
842EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
843
844/**
Dave Airlief453ba02008-11-07 14:05:41 -0800845 * drm_mode_config_init - initialize DRM mode_configuration structure
846 * @dev: DRM device
847 *
848 * LOCKING:
849 * None, should happen single threaded at init time.
850 *
851 * Initialize @dev's mode_config structure, used for tracking the graphics
852 * configuration of @dev.
853 */
854void drm_mode_config_init(struct drm_device *dev)
855{
856 mutex_init(&dev->mode_config.mutex);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000857 mutex_init(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800858 INIT_LIST_HEAD(&dev->mode_config.fb_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800859 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
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002435 /* store the property value if successful */
Dave Airlief453ba02008-11-07 14:05:41 -08002436 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
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08002633 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05002634 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}