blob: 255658906fcf0796c3c0ee0a32eb543907b6a0a7 [file] [log] [blame]
Dave Airlief453ba02008-11-07 14:05:41 -08001/*
2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
5 *
6 * DRM core CRTC related functions
7 *
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
17 *
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 *
26 * Authors:
27 * Keith Packard
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
31 */
32#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040034#include <linux/export.h>
Dave Airlief453ba02008-11-07 14:05:41 -080035#include "drm.h"
36#include "drmP.h"
37#include "drm_crtc.h"
Adam Jackson7466f4c2010-03-29 21:43:23 +000038#include "drm_edid.h"
Jesse Barnes308e5bc2011-11-14 14:51:28 -080039#include "drm_fourcc.h"
Dave Airlief453ba02008-11-07 14:05:41 -080040
41struct drm_prop_enum_list {
42 int type;
43 char *name;
44};
45
46/* Avoid boilerplate. I'm tired of typing. */
47#define DRM_ENUM_NAME_FN(fnname, list) \
48 char *fnname(int val) \
49 { \
50 int i; \
51 for (i = 0; i < ARRAY_SIZE(list); i++) { \
52 if (list[i].type == val) \
53 return list[i].name; \
54 } \
55 return "(unknown)"; \
56 }
57
58/*
59 * Global properties
60 */
61static struct drm_prop_enum_list drm_dpms_enum_list[] =
62{ { DRM_MODE_DPMS_ON, "On" },
63 { DRM_MODE_DPMS_STANDBY, "Standby" },
64 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
65 { DRM_MODE_DPMS_OFF, "Off" }
66};
67
68DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
69
70/*
71 * Optional properties
72 */
73static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
74{
Jesse Barnes53bd8382009-07-01 10:04:40 -070075 { DRM_MODE_SCALE_NONE, "None" },
76 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
77 { DRM_MODE_SCALE_CENTER, "Center" },
78 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -080079};
80
81static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
82{
83 { DRM_MODE_DITHERING_OFF, "Off" },
84 { DRM_MODE_DITHERING_ON, "On" },
Ben Skeggs92897b52010-07-16 15:09:17 +100085 { DRM_MODE_DITHERING_AUTO, "Automatic" },
Dave Airlief453ba02008-11-07 14:05:41 -080086};
87
88/*
89 * Non-global properties, but "required" for certain connectors.
90 */
91static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
92{
93 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
94 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
95 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
96};
97
98DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
99
100static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
101{
102 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
103 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
104 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
105};
106
107DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
108 drm_dvi_i_subconnector_enum_list)
109
110static struct drm_prop_enum_list drm_tv_select_enum_list[] =
111{
112 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
113 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
114 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
115 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200116 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800117};
118
119DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
120
121static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
122{
123 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
124 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
125 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
126 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200127 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800128};
129
130DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
131 drm_tv_subconnector_enum_list)
132
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000133static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
134 { DRM_MODE_DIRTY_OFF, "Off" },
135 { DRM_MODE_DIRTY_ON, "On" },
136 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
137};
138
139DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
140 drm_dirty_info_enum_list)
141
Dave Airlief453ba02008-11-07 14:05:41 -0800142struct drm_conn_prop_enum_list {
143 int type;
144 char *name;
145 int count;
146};
147
148/*
149 * Connector and encoder types.
150 */
151static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
152{ { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
153 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
154 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
155 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
156 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
157 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
158 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
159 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
160 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500161 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
162 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
163 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
164 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
Francisco Jerez74bd3c22009-08-02 04:19:18 +0200165 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500166 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200167 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
Dave Airlief453ba02008-11-07 14:05:41 -0800168};
169
170static struct drm_prop_enum_list drm_encoder_enum_list[] =
171{ { DRM_MODE_ENCODER_NONE, "None" },
172 { DRM_MODE_ENCODER_DAC, "DAC" },
173 { DRM_MODE_ENCODER_TMDS, "TMDS" },
174 { DRM_MODE_ENCODER_LVDS, "LVDS" },
175 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200176 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Dave Airlief453ba02008-11-07 14:05:41 -0800177};
178
179char *drm_get_encoder_name(struct drm_encoder *encoder)
180{
181 static char buf[32];
182
183 snprintf(buf, 32, "%s-%d",
184 drm_encoder_enum_list[encoder->encoder_type].name,
185 encoder->base.id);
186 return buf;
187}
Dave Airlie13a81952009-09-07 15:45:33 +1000188EXPORT_SYMBOL(drm_get_encoder_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800189
190char *drm_get_connector_name(struct drm_connector *connector)
191{
192 static char buf[32];
193
194 snprintf(buf, 32, "%s-%d",
195 drm_connector_enum_list[connector->connector_type].name,
196 connector->connector_type_id);
197 return buf;
198}
199EXPORT_SYMBOL(drm_get_connector_name);
200
201char *drm_get_connector_status_name(enum drm_connector_status status)
202{
203 if (status == connector_status_connected)
204 return "connected";
205 else if (status == connector_status_disconnected)
206 return "disconnected";
207 else
208 return "unknown";
209}
210
211/**
212 * drm_mode_object_get - allocate a new identifier
213 * @dev: DRM device
214 * @ptr: object pointer, used to generate unique ID
215 * @type: object type
216 *
217 * LOCKING:
Dave Airlief453ba02008-11-07 14:05:41 -0800218 *
219 * Create a unique identifier based on @ptr in @dev's identifier space. Used
220 * for tracking modes, CRTCs and connectors.
221 *
222 * RETURNS:
223 * New unique (relative to other objects in @dev) integer identifier for the
224 * object.
225 */
226static int drm_mode_object_get(struct drm_device *dev,
227 struct drm_mode_object *obj, uint32_t obj_type)
228{
229 int new_id = 0;
230 int ret;
231
Dave Airlief453ba02008-11-07 14:05:41 -0800232again:
233 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
234 DRM_ERROR("Ran out memory getting a mode number\n");
235 return -EINVAL;
236 }
237
Jesse Barnesad2563c2009-01-19 17:21:45 +1000238 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800239 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000240 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800241 if (ret == -EAGAIN)
242 goto again;
243
244 obj->id = new_id;
245 obj->type = obj_type;
246 return 0;
247}
248
249/**
250 * drm_mode_object_put - free an identifer
251 * @dev: DRM device
252 * @id: ID to free
253 *
254 * LOCKING:
255 * Caller must hold DRM mode_config lock.
256 *
257 * Free @id from @dev's unique identifier pool.
258 */
259static void drm_mode_object_put(struct drm_device *dev,
260 struct drm_mode_object *object)
261{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000262 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800263 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000264 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800265}
266
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200267struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
268 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800269{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000270 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800271
Jesse Barnesad2563c2009-01-19 17:21:45 +1000272 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800273 obj = idr_find(&dev->mode_config.crtc_idr, id);
274 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000275 obj = NULL;
276 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800277
278 return obj;
279}
280EXPORT_SYMBOL(drm_mode_object_find);
281
282/**
Dave Airlief453ba02008-11-07 14:05:41 -0800283 * drm_framebuffer_init - initialize a framebuffer
284 * @dev: DRM device
285 *
286 * LOCKING:
287 * Caller must hold mode config lock.
288 *
289 * Allocates an ID for the framebuffer's parent mode object, sets its mode
290 * functions & device file and adds it to the master fd list.
291 *
292 * RETURNS:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200293 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800294 */
295int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
296 const struct drm_framebuffer_funcs *funcs)
297{
298 int ret;
299
300 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
301 if (ret) {
302 return ret;
303 }
304
305 fb->dev = dev;
306 fb->funcs = funcs;
307 dev->mode_config.num_fb++;
308 list_add(&fb->head, &dev->mode_config.fb_list);
309
310 return 0;
311}
312EXPORT_SYMBOL(drm_framebuffer_init);
313
314/**
315 * drm_framebuffer_cleanup - remove a framebuffer object
316 * @fb: framebuffer to remove
317 *
318 * LOCKING:
319 * Caller must hold mode config lock.
320 *
321 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
322 * it, setting it to NULL.
323 */
324void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
325{
326 struct drm_device *dev = fb->dev;
327 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800328 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000329 struct drm_mode_set set;
330 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800331
332 /* remove from any CRTC */
333 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie5ef5f722009-08-17 13:11:23 +1000334 if (crtc->fb == fb) {
335 /* should turn off the crtc */
336 memset(&set, 0, sizeof(struct drm_mode_set));
337 set.crtc = crtc;
338 set.fb = NULL;
339 ret = crtc->funcs->set_config(&set);
340 if (ret)
341 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
342 }
Dave Airlief453ba02008-11-07 14:05:41 -0800343 }
344
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800345 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
346 if (plane->fb == fb) {
347 /* should turn off the crtc */
348 ret = plane->funcs->disable_plane(plane);
349 if (ret)
350 DRM_ERROR("failed to disable plane with busy fb\n");
351 }
352 }
353
Dave Airlief453ba02008-11-07 14:05:41 -0800354 drm_mode_object_put(dev, &fb->base);
355 list_del(&fb->head);
356 dev->mode_config.num_fb--;
357}
358EXPORT_SYMBOL(drm_framebuffer_cleanup);
359
360/**
361 * drm_crtc_init - Initialise a new CRTC object
362 * @dev: DRM device
363 * @crtc: CRTC object to init
364 * @funcs: callbacks for the new CRTC
365 *
366 * LOCKING:
367 * Caller must hold mode config lock.
368 *
369 * Inits a new object created as base part of an driver crtc object.
370 */
371void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
372 const struct drm_crtc_funcs *funcs)
373{
374 crtc->dev = dev;
375 crtc->funcs = funcs;
376
377 mutex_lock(&dev->mode_config.mutex);
378 drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
379
380 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
381 dev->mode_config.num_crtc++;
382 mutex_unlock(&dev->mode_config.mutex);
383}
384EXPORT_SYMBOL(drm_crtc_init);
385
386/**
387 * drm_crtc_cleanup - Cleans up the core crtc usage.
388 * @crtc: CRTC to cleanup
389 *
390 * LOCKING:
391 * Caller must hold mode config lock.
392 *
393 * Cleanup @crtc. Removes from drm modesetting space
394 * does NOT free object, caller does that.
395 */
396void drm_crtc_cleanup(struct drm_crtc *crtc)
397{
398 struct drm_device *dev = crtc->dev;
399
400 if (crtc->gamma_store) {
401 kfree(crtc->gamma_store);
402 crtc->gamma_store = NULL;
403 }
404
405 drm_mode_object_put(dev, &crtc->base);
406 list_del(&crtc->head);
407 dev->mode_config.num_crtc--;
408}
409EXPORT_SYMBOL(drm_crtc_cleanup);
410
411/**
412 * drm_mode_probed_add - add a mode to a connector's probed mode list
413 * @connector: connector the new mode
414 * @mode: mode data
415 *
416 * LOCKING:
417 * Caller must hold mode config lock.
418 *
419 * Add @mode to @connector's mode list for later use.
420 */
421void drm_mode_probed_add(struct drm_connector *connector,
422 struct drm_display_mode *mode)
423{
424 list_add(&mode->head, &connector->probed_modes);
425}
426EXPORT_SYMBOL(drm_mode_probed_add);
427
428/**
429 * drm_mode_remove - remove and free a mode
430 * @connector: connector list to modify
431 * @mode: mode to remove
432 *
433 * LOCKING:
434 * Caller must hold mode config lock.
435 *
436 * Remove @mode from @connector's mode list, then free it.
437 */
438void drm_mode_remove(struct drm_connector *connector,
439 struct drm_display_mode *mode)
440{
441 list_del(&mode->head);
442 kfree(mode);
443}
444EXPORT_SYMBOL(drm_mode_remove);
445
446/**
447 * drm_connector_init - Init a preallocated connector
448 * @dev: DRM device
449 * @connector: the connector to init
450 * @funcs: callbacks for this connector
451 * @name: user visible name of the connector
452 *
453 * LOCKING:
454 * Caller must hold @dev's mode_config lock.
455 *
456 * Initialises a preallocated connector. Connectors should be
457 * subclassed as part of driver connector objects.
458 */
459void drm_connector_init(struct drm_device *dev,
460 struct drm_connector *connector,
461 const struct drm_connector_funcs *funcs,
462 int connector_type)
463{
464 mutex_lock(&dev->mode_config.mutex);
465
466 connector->dev = dev;
467 connector->funcs = funcs;
468 drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
469 connector->connector_type = connector_type;
470 connector->connector_type_id =
471 ++drm_connector_enum_list[connector_type].count; /* TODO */
472 INIT_LIST_HEAD(&connector->user_modes);
473 INIT_LIST_HEAD(&connector->probed_modes);
474 INIT_LIST_HEAD(&connector->modes);
475 connector->edid_blob_ptr = NULL;
476
477 list_add_tail(&connector->head, &dev->mode_config.connector_list);
478 dev->mode_config.num_connector++;
479
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200480 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
481 drm_connector_attach_property(connector,
482 dev->mode_config.edid_property,
483 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800484
485 drm_connector_attach_property(connector,
486 dev->mode_config.dpms_property, 0);
487
488 mutex_unlock(&dev->mode_config.mutex);
489}
490EXPORT_SYMBOL(drm_connector_init);
491
492/**
493 * drm_connector_cleanup - cleans up an initialised connector
494 * @connector: connector to cleanup
495 *
496 * LOCKING:
497 * Caller must hold @dev's mode_config lock.
498 *
499 * Cleans up the connector but doesn't free the object.
500 */
501void drm_connector_cleanup(struct drm_connector *connector)
502{
503 struct drm_device *dev = connector->dev;
504 struct drm_display_mode *mode, *t;
505
506 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
507 drm_mode_remove(connector, mode);
508
509 list_for_each_entry_safe(mode, t, &connector->modes, head)
510 drm_mode_remove(connector, mode);
511
512 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
513 drm_mode_remove(connector, mode);
514
515 mutex_lock(&dev->mode_config.mutex);
516 drm_mode_object_put(dev, &connector->base);
517 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000518 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800519 mutex_unlock(&dev->mode_config.mutex);
520}
521EXPORT_SYMBOL(drm_connector_cleanup);
522
523void drm_encoder_init(struct drm_device *dev,
524 struct drm_encoder *encoder,
525 const struct drm_encoder_funcs *funcs,
526 int encoder_type)
527{
528 mutex_lock(&dev->mode_config.mutex);
529
530 encoder->dev = dev;
531
532 drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
533 encoder->encoder_type = encoder_type;
534 encoder->funcs = funcs;
535
536 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
537 dev->mode_config.num_encoder++;
538
539 mutex_unlock(&dev->mode_config.mutex);
540}
541EXPORT_SYMBOL(drm_encoder_init);
542
543void drm_encoder_cleanup(struct drm_encoder *encoder)
544{
545 struct drm_device *dev = encoder->dev;
546 mutex_lock(&dev->mode_config.mutex);
547 drm_mode_object_put(dev, &encoder->base);
548 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000549 dev->mode_config.num_encoder--;
Dave Airlief453ba02008-11-07 14:05:41 -0800550 mutex_unlock(&dev->mode_config.mutex);
551}
552EXPORT_SYMBOL(drm_encoder_cleanup);
553
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800554int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
555 unsigned long possible_crtcs,
556 const struct drm_plane_funcs *funcs,
557 uint32_t *formats, uint32_t format_count)
558{
559 mutex_lock(&dev->mode_config.mutex);
560
561 plane->dev = dev;
562 drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
563 plane->funcs = funcs;
564 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
565 GFP_KERNEL);
566 if (!plane->format_types) {
567 DRM_DEBUG_KMS("out of memory when allocating plane\n");
568 drm_mode_object_put(dev, &plane->base);
569 return -ENOMEM;
570 }
571
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800572 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800573 plane->format_count = format_count;
574 plane->possible_crtcs = possible_crtcs;
575
576 list_add_tail(&plane->head, &dev->mode_config.plane_list);
577 dev->mode_config.num_plane++;
578
579 mutex_unlock(&dev->mode_config.mutex);
580
581 return 0;
582}
583EXPORT_SYMBOL(drm_plane_init);
584
585void drm_plane_cleanup(struct drm_plane *plane)
586{
587 struct drm_device *dev = plane->dev;
588
589 mutex_lock(&dev->mode_config.mutex);
590 kfree(plane->format_types);
591 drm_mode_object_put(dev, &plane->base);
592 list_del(&plane->head);
593 dev->mode_config.num_plane--;
594 mutex_unlock(&dev->mode_config.mutex);
595}
596EXPORT_SYMBOL(drm_plane_cleanup);
597
Dave Airlief453ba02008-11-07 14:05:41 -0800598/**
599 * drm_mode_create - create a new display mode
600 * @dev: DRM device
601 *
602 * LOCKING:
603 * Caller must hold DRM mode_config lock.
604 *
605 * Create a new drm_display_mode, give it an ID, and return it.
606 *
607 * RETURNS:
608 * Pointer to new mode on success, NULL on error.
609 */
610struct drm_display_mode *drm_mode_create(struct drm_device *dev)
611{
612 struct drm_display_mode *nmode;
613
614 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
615 if (!nmode)
616 return NULL;
617
618 drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
619 return nmode;
620}
621EXPORT_SYMBOL(drm_mode_create);
622
623/**
624 * drm_mode_destroy - remove a mode
625 * @dev: DRM device
626 * @mode: mode to remove
627 *
628 * LOCKING:
629 * Caller must hold mode config lock.
630 *
631 * Free @mode's unique identifier, then free it.
632 */
633void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
634{
635 drm_mode_object_put(dev, &mode->base);
636
637 kfree(mode);
638}
639EXPORT_SYMBOL(drm_mode_destroy);
640
641static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
642{
643 struct drm_property *edid;
644 struct drm_property *dpms;
645 int i;
646
647 /*
648 * Standard properties (apply to all connectors)
649 */
650 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
651 DRM_MODE_PROP_IMMUTABLE,
652 "EDID", 0);
653 dev->mode_config.edid_property = edid;
654
655 dpms = drm_property_create(dev, DRM_MODE_PROP_ENUM,
656 "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
657 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
658 drm_property_add_enum(dpms, i, drm_dpms_enum_list[i].type,
659 drm_dpms_enum_list[i].name);
660 dev->mode_config.dpms_property = dpms;
661
662 return 0;
663}
664
665/**
666 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
667 * @dev: DRM device
668 *
669 * Called by a driver the first time a DVI-I connector is made.
670 */
671int drm_mode_create_dvi_i_properties(struct drm_device *dev)
672{
673 struct drm_property *dvi_i_selector;
674 struct drm_property *dvi_i_subconnector;
675 int i;
676
677 if (dev->mode_config.dvi_i_select_subconnector_property)
678 return 0;
679
680 dvi_i_selector =
681 drm_property_create(dev, DRM_MODE_PROP_ENUM,
682 "select subconnector",
683 ARRAY_SIZE(drm_dvi_i_select_enum_list));
684 for (i = 0; i < ARRAY_SIZE(drm_dvi_i_select_enum_list); i++)
685 drm_property_add_enum(dvi_i_selector, i,
686 drm_dvi_i_select_enum_list[i].type,
687 drm_dvi_i_select_enum_list[i].name);
688 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
689
690 dvi_i_subconnector =
691 drm_property_create(dev, DRM_MODE_PROP_ENUM |
692 DRM_MODE_PROP_IMMUTABLE,
693 "subconnector",
694 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
695 for (i = 0; i < ARRAY_SIZE(drm_dvi_i_subconnector_enum_list); i++)
696 drm_property_add_enum(dvi_i_subconnector, i,
697 drm_dvi_i_subconnector_enum_list[i].type,
698 drm_dvi_i_subconnector_enum_list[i].name);
699 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
700
701 return 0;
702}
703EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
704
705/**
706 * drm_create_tv_properties - create TV specific connector properties
707 * @dev: DRM device
708 * @num_modes: number of different TV formats (modes) supported
709 * @modes: array of pointers to strings containing name of each format
710 *
711 * Called by a driver's TV initialization routine, this function creates
712 * the TV specific connector properties for a given device. Caller is
713 * responsible for allocating a list of format names and passing them to
714 * this routine.
715 */
716int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
717 char *modes[])
718{
719 struct drm_property *tv_selector;
720 struct drm_property *tv_subconnector;
721 int i;
722
723 if (dev->mode_config.tv_select_subconnector_property)
724 return 0;
725
726 /*
727 * Basic connector properties
728 */
729 tv_selector = drm_property_create(dev, DRM_MODE_PROP_ENUM,
730 "select subconnector",
731 ARRAY_SIZE(drm_tv_select_enum_list));
732 for (i = 0; i < ARRAY_SIZE(drm_tv_select_enum_list); i++)
733 drm_property_add_enum(tv_selector, i,
734 drm_tv_select_enum_list[i].type,
735 drm_tv_select_enum_list[i].name);
736 dev->mode_config.tv_select_subconnector_property = tv_selector;
737
738 tv_subconnector =
739 drm_property_create(dev, DRM_MODE_PROP_ENUM |
740 DRM_MODE_PROP_IMMUTABLE, "subconnector",
741 ARRAY_SIZE(drm_tv_subconnector_enum_list));
742 for (i = 0; i < ARRAY_SIZE(drm_tv_subconnector_enum_list); i++)
743 drm_property_add_enum(tv_subconnector, i,
744 drm_tv_subconnector_enum_list[i].type,
745 drm_tv_subconnector_enum_list[i].name);
746 dev->mode_config.tv_subconnector_property = tv_subconnector;
747
748 /*
749 * Other, TV specific properties: margins & TV modes.
750 */
751 dev->mode_config.tv_left_margin_property =
752 drm_property_create(dev, DRM_MODE_PROP_RANGE,
753 "left margin", 2);
754 dev->mode_config.tv_left_margin_property->values[0] = 0;
755 dev->mode_config.tv_left_margin_property->values[1] = 100;
756
757 dev->mode_config.tv_right_margin_property =
758 drm_property_create(dev, DRM_MODE_PROP_RANGE,
759 "right margin", 2);
760 dev->mode_config.tv_right_margin_property->values[0] = 0;
761 dev->mode_config.tv_right_margin_property->values[1] = 100;
762
763 dev->mode_config.tv_top_margin_property =
764 drm_property_create(dev, DRM_MODE_PROP_RANGE,
765 "top margin", 2);
766 dev->mode_config.tv_top_margin_property->values[0] = 0;
767 dev->mode_config.tv_top_margin_property->values[1] = 100;
768
769 dev->mode_config.tv_bottom_margin_property =
770 drm_property_create(dev, DRM_MODE_PROP_RANGE,
771 "bottom margin", 2);
772 dev->mode_config.tv_bottom_margin_property->values[0] = 0;
773 dev->mode_config.tv_bottom_margin_property->values[1] = 100;
774
775 dev->mode_config.tv_mode_property =
776 drm_property_create(dev, DRM_MODE_PROP_ENUM,
777 "mode", num_modes);
778 for (i = 0; i < num_modes; i++)
779 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
780 i, modes[i]);
781
Francisco Jerezb6b79022009-08-02 04:19:20 +0200782 dev->mode_config.tv_brightness_property =
783 drm_property_create(dev, DRM_MODE_PROP_RANGE,
784 "brightness", 2);
785 dev->mode_config.tv_brightness_property->values[0] = 0;
786 dev->mode_config.tv_brightness_property->values[1] = 100;
787
788 dev->mode_config.tv_contrast_property =
789 drm_property_create(dev, DRM_MODE_PROP_RANGE,
790 "contrast", 2);
791 dev->mode_config.tv_contrast_property->values[0] = 0;
792 dev->mode_config.tv_contrast_property->values[1] = 100;
793
794 dev->mode_config.tv_flicker_reduction_property =
795 drm_property_create(dev, DRM_MODE_PROP_RANGE,
796 "flicker reduction", 2);
797 dev->mode_config.tv_flicker_reduction_property->values[0] = 0;
798 dev->mode_config.tv_flicker_reduction_property->values[1] = 100;
799
Francisco Jereza75f0232009-08-12 02:30:10 +0200800 dev->mode_config.tv_overscan_property =
801 drm_property_create(dev, DRM_MODE_PROP_RANGE,
802 "overscan", 2);
803 dev->mode_config.tv_overscan_property->values[0] = 0;
804 dev->mode_config.tv_overscan_property->values[1] = 100;
805
806 dev->mode_config.tv_saturation_property =
807 drm_property_create(dev, DRM_MODE_PROP_RANGE,
808 "saturation", 2);
809 dev->mode_config.tv_saturation_property->values[0] = 0;
810 dev->mode_config.tv_saturation_property->values[1] = 100;
811
812 dev->mode_config.tv_hue_property =
813 drm_property_create(dev, DRM_MODE_PROP_RANGE,
814 "hue", 2);
815 dev->mode_config.tv_hue_property->values[0] = 0;
816 dev->mode_config.tv_hue_property->values[1] = 100;
817
Dave Airlief453ba02008-11-07 14:05:41 -0800818 return 0;
819}
820EXPORT_SYMBOL(drm_mode_create_tv_properties);
821
822/**
823 * drm_mode_create_scaling_mode_property - create scaling mode property
824 * @dev: DRM device
825 *
826 * Called by a driver the first time it's needed, must be attached to desired
827 * connectors.
828 */
829int drm_mode_create_scaling_mode_property(struct drm_device *dev)
830{
831 struct drm_property *scaling_mode;
832 int i;
833
834 if (dev->mode_config.scaling_mode_property)
835 return 0;
836
837 scaling_mode =
838 drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
839 ARRAY_SIZE(drm_scaling_mode_enum_list));
840 for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++)
841 drm_property_add_enum(scaling_mode, i,
842 drm_scaling_mode_enum_list[i].type,
843 drm_scaling_mode_enum_list[i].name);
844
845 dev->mode_config.scaling_mode_property = scaling_mode;
846
847 return 0;
848}
849EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
850
851/**
852 * drm_mode_create_dithering_property - create dithering property
853 * @dev: DRM device
854 *
855 * Called by a driver the first time it's needed, must be attached to desired
856 * connectors.
857 */
858int drm_mode_create_dithering_property(struct drm_device *dev)
859{
860 struct drm_property *dithering_mode;
861 int i;
862
863 if (dev->mode_config.dithering_mode_property)
864 return 0;
865
866 dithering_mode =
867 drm_property_create(dev, DRM_MODE_PROP_ENUM, "dithering",
868 ARRAY_SIZE(drm_dithering_mode_enum_list));
869 for (i = 0; i < ARRAY_SIZE(drm_dithering_mode_enum_list); i++)
870 drm_property_add_enum(dithering_mode, i,
871 drm_dithering_mode_enum_list[i].type,
872 drm_dithering_mode_enum_list[i].name);
873 dev->mode_config.dithering_mode_property = dithering_mode;
874
875 return 0;
876}
877EXPORT_SYMBOL(drm_mode_create_dithering_property);
878
879/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000880 * drm_mode_create_dirty_property - create dirty property
881 * @dev: DRM device
882 *
883 * Called by a driver the first time it's needed, must be attached to desired
884 * connectors.
885 */
886int drm_mode_create_dirty_info_property(struct drm_device *dev)
887{
888 struct drm_property *dirty_info;
889 int i;
890
891 if (dev->mode_config.dirty_info_property)
892 return 0;
893
894 dirty_info =
895 drm_property_create(dev, DRM_MODE_PROP_ENUM |
896 DRM_MODE_PROP_IMMUTABLE,
897 "dirty",
898 ARRAY_SIZE(drm_dirty_info_enum_list));
899 for (i = 0; i < ARRAY_SIZE(drm_dirty_info_enum_list); i++)
900 drm_property_add_enum(dirty_info, i,
901 drm_dirty_info_enum_list[i].type,
902 drm_dirty_info_enum_list[i].name);
903 dev->mode_config.dirty_info_property = dirty_info;
904
905 return 0;
906}
907EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
908
909/**
Dave Airlief453ba02008-11-07 14:05:41 -0800910 * drm_mode_config_init - initialize DRM mode_configuration structure
911 * @dev: DRM device
912 *
913 * LOCKING:
914 * None, should happen single threaded at init time.
915 *
916 * Initialize @dev's mode_config structure, used for tracking the graphics
917 * configuration of @dev.
918 */
919void drm_mode_config_init(struct drm_device *dev)
920{
921 mutex_init(&dev->mode_config.mutex);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000922 mutex_init(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800923 INIT_LIST_HEAD(&dev->mode_config.fb_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800924 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
925 INIT_LIST_HEAD(&dev->mode_config.connector_list);
926 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
927 INIT_LIST_HEAD(&dev->mode_config.property_list);
928 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800929 INIT_LIST_HEAD(&dev->mode_config.plane_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800930 idr_init(&dev->mode_config.crtc_idr);
931
932 mutex_lock(&dev->mode_config.mutex);
933 drm_mode_create_standard_connector_properties(dev);
934 mutex_unlock(&dev->mode_config.mutex);
935
936 /* Just to be sure */
937 dev->mode_config.num_fb = 0;
938 dev->mode_config.num_connector = 0;
939 dev->mode_config.num_crtc = 0;
940 dev->mode_config.num_encoder = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800941}
942EXPORT_SYMBOL(drm_mode_config_init);
943
944int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
945{
946 uint32_t total_objects = 0;
947
948 total_objects += dev->mode_config.num_crtc;
949 total_objects += dev->mode_config.num_connector;
950 total_objects += dev->mode_config.num_encoder;
951
Dave Airlief453ba02008-11-07 14:05:41 -0800952 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
953 if (!group->id_list)
954 return -ENOMEM;
955
956 group->num_crtcs = 0;
957 group->num_connectors = 0;
958 group->num_encoders = 0;
959 return 0;
960}
961
962int drm_mode_group_init_legacy_group(struct drm_device *dev,
963 struct drm_mode_group *group)
964{
965 struct drm_crtc *crtc;
966 struct drm_encoder *encoder;
967 struct drm_connector *connector;
968 int ret;
969
970 if ((ret = drm_mode_group_init(dev, group)))
971 return ret;
972
973 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
974 group->id_list[group->num_crtcs++] = crtc->base.id;
975
976 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
977 group->id_list[group->num_crtcs + group->num_encoders++] =
978 encoder->base.id;
979
980 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
981 group->id_list[group->num_crtcs + group->num_encoders +
982 group->num_connectors++] = connector->base.id;
983
984 return 0;
985}
986
987/**
988 * drm_mode_config_cleanup - free up DRM mode_config info
989 * @dev: DRM device
990 *
991 * LOCKING:
992 * Caller must hold mode config lock.
993 *
994 * Free up all the connectors and CRTCs associated with this DRM device, then
995 * free up the framebuffers and associated buffer objects.
996 *
997 * FIXME: cleanup any dangling user buffer objects too
998 */
999void drm_mode_config_cleanup(struct drm_device *dev)
1000{
1001 struct drm_connector *connector, *ot;
1002 struct drm_crtc *crtc, *ct;
1003 struct drm_encoder *encoder, *enct;
1004 struct drm_framebuffer *fb, *fbt;
1005 struct drm_property *property, *pt;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001006 struct drm_plane *plane, *plt;
Dave Airlief453ba02008-11-07 14:05:41 -08001007
1008 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1009 head) {
1010 encoder->funcs->destroy(encoder);
1011 }
1012
1013 list_for_each_entry_safe(connector, ot,
1014 &dev->mode_config.connector_list, head) {
1015 connector->funcs->destroy(connector);
1016 }
1017
1018 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1019 head) {
1020 drm_property_destroy(dev, property);
1021 }
1022
1023 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1024 fb->funcs->destroy(fb);
1025 }
1026
1027 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1028 crtc->funcs->destroy(crtc);
1029 }
1030
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001031 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1032 head) {
1033 plane->funcs->destroy(plane);
1034 }
Dave Airlief453ba02008-11-07 14:05:41 -08001035}
1036EXPORT_SYMBOL(drm_mode_config_cleanup);
1037
Dave Airlief453ba02008-11-07 14:05:41 -08001038/**
1039 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1040 * @out: drm_mode_modeinfo struct to return to the user
1041 * @in: drm_display_mode to use
1042 *
1043 * LOCKING:
1044 * None.
1045 *
1046 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1047 * the user.
1048 */
1049void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1050 struct drm_display_mode *in)
1051{
1052 out->clock = in->clock;
1053 out->hdisplay = in->hdisplay;
1054 out->hsync_start = in->hsync_start;
1055 out->hsync_end = in->hsync_end;
1056 out->htotal = in->htotal;
1057 out->hskew = in->hskew;
1058 out->vdisplay = in->vdisplay;
1059 out->vsync_start = in->vsync_start;
1060 out->vsync_end = in->vsync_end;
1061 out->vtotal = in->vtotal;
1062 out->vscan = in->vscan;
1063 out->vrefresh = in->vrefresh;
1064 out->flags = in->flags;
1065 out->type = in->type;
1066 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1067 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1068}
1069
1070/**
1071 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1072 * @out: drm_display_mode to return to the user
1073 * @in: drm_mode_modeinfo to use
1074 *
1075 * LOCKING:
1076 * None.
1077 *
1078 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1079 * the caller.
1080 */
1081void drm_crtc_convert_umode(struct drm_display_mode *out,
1082 struct drm_mode_modeinfo *in)
1083{
1084 out->clock = in->clock;
1085 out->hdisplay = in->hdisplay;
1086 out->hsync_start = in->hsync_start;
1087 out->hsync_end = in->hsync_end;
1088 out->htotal = in->htotal;
1089 out->hskew = in->hskew;
1090 out->vdisplay = in->vdisplay;
1091 out->vsync_start = in->vsync_start;
1092 out->vsync_end = in->vsync_end;
1093 out->vtotal = in->vtotal;
1094 out->vscan = in->vscan;
1095 out->vrefresh = in->vrefresh;
1096 out->flags = in->flags;
1097 out->type = in->type;
1098 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1099 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1100}
1101
1102/**
1103 * drm_mode_getresources - get graphics configuration
1104 * @inode: inode from the ioctl
1105 * @filp: file * from the ioctl
1106 * @cmd: cmd from ioctl
1107 * @arg: arg from ioctl
1108 *
1109 * LOCKING:
1110 * Takes mode config lock.
1111 *
1112 * Construct a set of configuration description structures and return
1113 * them to the user, including CRTC, connector and framebuffer configuration.
1114 *
1115 * Called by the user via ioctl.
1116 *
1117 * RETURNS:
1118 * Zero on success, errno on failure.
1119 */
1120int drm_mode_getresources(struct drm_device *dev, void *data,
1121 struct drm_file *file_priv)
1122{
1123 struct drm_mode_card_res *card_res = data;
1124 struct list_head *lh;
1125 struct drm_framebuffer *fb;
1126 struct drm_connector *connector;
1127 struct drm_crtc *crtc;
1128 struct drm_encoder *encoder;
1129 int ret = 0;
1130 int connector_count = 0;
1131 int crtc_count = 0;
1132 int fb_count = 0;
1133 int encoder_count = 0;
1134 int copied = 0, i;
1135 uint32_t __user *fb_id;
1136 uint32_t __user *crtc_id;
1137 uint32_t __user *connector_id;
1138 uint32_t __user *encoder_id;
1139 struct drm_mode_group *mode_group;
1140
Dave Airliefb3b06c2011-02-08 13:55:21 +10001141 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1142 return -EINVAL;
1143
Dave Airlief453ba02008-11-07 14:05:41 -08001144 mutex_lock(&dev->mode_config.mutex);
1145
1146 /*
1147 * For the non-control nodes we need to limit the list of resources
1148 * by IDs in the group list for this node
1149 */
1150 list_for_each(lh, &file_priv->fbs)
1151 fb_count++;
1152
1153 mode_group = &file_priv->master->minor->mode_group;
1154 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1155
1156 list_for_each(lh, &dev->mode_config.crtc_list)
1157 crtc_count++;
1158
1159 list_for_each(lh, &dev->mode_config.connector_list)
1160 connector_count++;
1161
1162 list_for_each(lh, &dev->mode_config.encoder_list)
1163 encoder_count++;
1164 } else {
1165
1166 crtc_count = mode_group->num_crtcs;
1167 connector_count = mode_group->num_connectors;
1168 encoder_count = mode_group->num_encoders;
1169 }
1170
1171 card_res->max_height = dev->mode_config.max_height;
1172 card_res->min_height = dev->mode_config.min_height;
1173 card_res->max_width = dev->mode_config.max_width;
1174 card_res->min_width = dev->mode_config.min_width;
1175
1176 /* handle this in 4 parts */
1177 /* FBs */
1178 if (card_res->count_fbs >= fb_count) {
1179 copied = 0;
1180 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
Sascha Hauer618c75e2011-06-03 12:54:14 +02001181 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
Dave Airlief453ba02008-11-07 14:05:41 -08001182 if (put_user(fb->base.id, fb_id + copied)) {
1183 ret = -EFAULT;
1184 goto out;
1185 }
1186 copied++;
1187 }
1188 }
1189 card_res->count_fbs = fb_count;
1190
1191 /* CRTCs */
1192 if (card_res->count_crtcs >= crtc_count) {
1193 copied = 0;
1194 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1195 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1196 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1197 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001198 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001199 if (put_user(crtc->base.id, crtc_id + copied)) {
1200 ret = -EFAULT;
1201 goto out;
1202 }
1203 copied++;
1204 }
1205 } else {
1206 for (i = 0; i < mode_group->num_crtcs; i++) {
1207 if (put_user(mode_group->id_list[i],
1208 crtc_id + copied)) {
1209 ret = -EFAULT;
1210 goto out;
1211 }
1212 copied++;
1213 }
1214 }
1215 }
1216 card_res->count_crtcs = crtc_count;
1217
1218 /* Encoders */
1219 if (card_res->count_encoders >= encoder_count) {
1220 copied = 0;
1221 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1222 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1223 list_for_each_entry(encoder,
1224 &dev->mode_config.encoder_list,
1225 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001226 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1227 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001228 if (put_user(encoder->base.id, encoder_id +
1229 copied)) {
1230 ret = -EFAULT;
1231 goto out;
1232 }
1233 copied++;
1234 }
1235 } else {
1236 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1237 if (put_user(mode_group->id_list[i],
1238 encoder_id + copied)) {
1239 ret = -EFAULT;
1240 goto out;
1241 }
1242 copied++;
1243 }
1244
1245 }
1246 }
1247 card_res->count_encoders = encoder_count;
1248
1249 /* Connectors */
1250 if (card_res->count_connectors >= connector_count) {
1251 copied = 0;
1252 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1253 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1254 list_for_each_entry(connector,
1255 &dev->mode_config.connector_list,
1256 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001257 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1258 connector->base.id,
1259 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001260 if (put_user(connector->base.id,
1261 connector_id + copied)) {
1262 ret = -EFAULT;
1263 goto out;
1264 }
1265 copied++;
1266 }
1267 } else {
1268 int start = mode_group->num_crtcs +
1269 mode_group->num_encoders;
1270 for (i = start; i < start + mode_group->num_connectors; i++) {
1271 if (put_user(mode_group->id_list[i],
1272 connector_id + copied)) {
1273 ret = -EFAULT;
1274 goto out;
1275 }
1276 copied++;
1277 }
1278 }
1279 }
1280 card_res->count_connectors = connector_count;
1281
Jerome Glisse94401062010-07-15 15:43:25 -04001282 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001283 card_res->count_connectors, card_res->count_encoders);
1284
1285out:
1286 mutex_unlock(&dev->mode_config.mutex);
1287 return ret;
1288}
1289
1290/**
1291 * drm_mode_getcrtc - get CRTC configuration
1292 * @inode: inode from the ioctl
1293 * @filp: file * from the ioctl
1294 * @cmd: cmd from ioctl
1295 * @arg: arg from ioctl
1296 *
1297 * LOCKING:
1298 * Caller? (FIXME)
1299 *
1300 * Construct a CRTC configuration structure to return to the user.
1301 *
1302 * Called by the user via ioctl.
1303 *
1304 * RETURNS:
1305 * Zero on success, errno on failure.
1306 */
1307int drm_mode_getcrtc(struct drm_device *dev,
1308 void *data, struct drm_file *file_priv)
1309{
1310 struct drm_mode_crtc *crtc_resp = data;
1311 struct drm_crtc *crtc;
1312 struct drm_mode_object *obj;
1313 int ret = 0;
1314
Dave Airliefb3b06c2011-02-08 13:55:21 +10001315 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1316 return -EINVAL;
1317
Dave Airlief453ba02008-11-07 14:05:41 -08001318 mutex_lock(&dev->mode_config.mutex);
1319
1320 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1321 DRM_MODE_OBJECT_CRTC);
1322 if (!obj) {
1323 ret = -EINVAL;
1324 goto out;
1325 }
1326 crtc = obj_to_crtc(obj);
1327
1328 crtc_resp->x = crtc->x;
1329 crtc_resp->y = crtc->y;
1330 crtc_resp->gamma_size = crtc->gamma_size;
1331 if (crtc->fb)
1332 crtc_resp->fb_id = crtc->fb->base.id;
1333 else
1334 crtc_resp->fb_id = 0;
1335
1336 if (crtc->enabled) {
1337
1338 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1339 crtc_resp->mode_valid = 1;
1340
1341 } else {
1342 crtc_resp->mode_valid = 0;
1343 }
1344
1345out:
1346 mutex_unlock(&dev->mode_config.mutex);
1347 return ret;
1348}
1349
1350/**
1351 * drm_mode_getconnector - get connector configuration
1352 * @inode: inode from the ioctl
1353 * @filp: file * from the ioctl
1354 * @cmd: cmd from ioctl
1355 * @arg: arg from ioctl
1356 *
1357 * LOCKING:
1358 * Caller? (FIXME)
1359 *
1360 * Construct a connector configuration structure to return to the user.
1361 *
1362 * Called by the user via ioctl.
1363 *
1364 * RETURNS:
1365 * Zero on success, errno on failure.
1366 */
1367int drm_mode_getconnector(struct drm_device *dev, void *data,
1368 struct drm_file *file_priv)
1369{
1370 struct drm_mode_get_connector *out_resp = data;
1371 struct drm_mode_object *obj;
1372 struct drm_connector *connector;
1373 struct drm_display_mode *mode;
1374 int mode_count = 0;
1375 int props_count = 0;
1376 int encoders_count = 0;
1377 int ret = 0;
1378 int copied = 0;
1379 int i;
1380 struct drm_mode_modeinfo u_mode;
1381 struct drm_mode_modeinfo __user *mode_ptr;
1382 uint32_t __user *prop_ptr;
1383 uint64_t __user *prop_values;
1384 uint32_t __user *encoder_ptr;
1385
Dave Airliefb3b06c2011-02-08 13:55:21 +10001386 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1387 return -EINVAL;
1388
Dave Airlief453ba02008-11-07 14:05:41 -08001389 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1390
Jerome Glisse94401062010-07-15 15:43:25 -04001391 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001392
1393 mutex_lock(&dev->mode_config.mutex);
1394
1395 obj = drm_mode_object_find(dev, out_resp->connector_id,
1396 DRM_MODE_OBJECT_CONNECTOR);
1397 if (!obj) {
1398 ret = -EINVAL;
1399 goto out;
1400 }
1401 connector = obj_to_connector(obj);
1402
1403 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1404 if (connector->property_ids[i] != 0) {
1405 props_count++;
1406 }
1407 }
1408
1409 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1410 if (connector->encoder_ids[i] != 0) {
1411 encoders_count++;
1412 }
1413 }
1414
1415 if (out_resp->count_modes == 0) {
1416 connector->funcs->fill_modes(connector,
1417 dev->mode_config.max_width,
1418 dev->mode_config.max_height);
1419 }
1420
1421 /* delayed so we get modes regardless of pre-fill_modes state */
1422 list_for_each_entry(mode, &connector->modes, head)
1423 mode_count++;
1424
1425 out_resp->connector_id = connector->base.id;
1426 out_resp->connector_type = connector->connector_type;
1427 out_resp->connector_type_id = connector->connector_type_id;
1428 out_resp->mm_width = connector->display_info.width_mm;
1429 out_resp->mm_height = connector->display_info.height_mm;
1430 out_resp->subpixel = connector->display_info.subpixel_order;
1431 out_resp->connection = connector->status;
1432 if (connector->encoder)
1433 out_resp->encoder_id = connector->encoder->base.id;
1434 else
1435 out_resp->encoder_id = 0;
1436
1437 /*
1438 * This ioctl is called twice, once to determine how much space is
1439 * needed, and the 2nd time to fill it.
1440 */
1441 if ((out_resp->count_modes >= mode_count) && mode_count) {
1442 copied = 0;
1443 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1444 list_for_each_entry(mode, &connector->modes, head) {
1445 drm_crtc_convert_to_umode(&u_mode, mode);
1446 if (copy_to_user(mode_ptr + copied,
1447 &u_mode, sizeof(u_mode))) {
1448 ret = -EFAULT;
1449 goto out;
1450 }
1451 copied++;
1452 }
1453 }
1454 out_resp->count_modes = mode_count;
1455
1456 if ((out_resp->count_props >= props_count) && props_count) {
1457 copied = 0;
1458 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1459 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1460 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1461 if (connector->property_ids[i] != 0) {
1462 if (put_user(connector->property_ids[i],
1463 prop_ptr + copied)) {
1464 ret = -EFAULT;
1465 goto out;
1466 }
1467
1468 if (put_user(connector->property_values[i],
1469 prop_values + copied)) {
1470 ret = -EFAULT;
1471 goto out;
1472 }
1473 copied++;
1474 }
1475 }
1476 }
1477 out_resp->count_props = props_count;
1478
1479 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1480 copied = 0;
1481 encoder_ptr = (uint32_t *)(unsigned long)(out_resp->encoders_ptr);
1482 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1483 if (connector->encoder_ids[i] != 0) {
1484 if (put_user(connector->encoder_ids[i],
1485 encoder_ptr + copied)) {
1486 ret = -EFAULT;
1487 goto out;
1488 }
1489 copied++;
1490 }
1491 }
1492 }
1493 out_resp->count_encoders = encoders_count;
1494
1495out:
1496 mutex_unlock(&dev->mode_config.mutex);
1497 return ret;
1498}
1499
1500int drm_mode_getencoder(struct drm_device *dev, void *data,
1501 struct drm_file *file_priv)
1502{
1503 struct drm_mode_get_encoder *enc_resp = data;
1504 struct drm_mode_object *obj;
1505 struct drm_encoder *encoder;
1506 int ret = 0;
1507
Dave Airliefb3b06c2011-02-08 13:55:21 +10001508 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1509 return -EINVAL;
1510
Dave Airlief453ba02008-11-07 14:05:41 -08001511 mutex_lock(&dev->mode_config.mutex);
1512 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1513 DRM_MODE_OBJECT_ENCODER);
1514 if (!obj) {
1515 ret = -EINVAL;
1516 goto out;
1517 }
1518 encoder = obj_to_encoder(obj);
1519
1520 if (encoder->crtc)
1521 enc_resp->crtc_id = encoder->crtc->base.id;
1522 else
1523 enc_resp->crtc_id = 0;
1524 enc_resp->encoder_type = encoder->encoder_type;
1525 enc_resp->encoder_id = encoder->base.id;
1526 enc_resp->possible_crtcs = encoder->possible_crtcs;
1527 enc_resp->possible_clones = encoder->possible_clones;
1528
1529out:
1530 mutex_unlock(&dev->mode_config.mutex);
1531 return ret;
1532}
1533
1534/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001535 * drm_mode_getplane_res - get plane info
1536 * @dev: DRM device
1537 * @data: ioctl data
1538 * @file_priv: DRM file info
1539 *
1540 * Return an plane count and set of IDs.
1541 */
1542int drm_mode_getplane_res(struct drm_device *dev, void *data,
1543 struct drm_file *file_priv)
1544{
1545 struct drm_mode_get_plane_res *plane_resp = data;
1546 struct drm_mode_config *config;
1547 struct drm_plane *plane;
1548 uint32_t __user *plane_ptr;
1549 int copied = 0, ret = 0;
1550
1551 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1552 return -EINVAL;
1553
1554 mutex_lock(&dev->mode_config.mutex);
1555 config = &dev->mode_config;
1556
1557 /*
1558 * This ioctl is called twice, once to determine how much space is
1559 * needed, and the 2nd time to fill it.
1560 */
1561 if (config->num_plane &&
1562 (plane_resp->count_planes >= config->num_plane)) {
1563 plane_ptr = (uint32_t *)(unsigned long)plane_resp->plane_id_ptr;
1564
1565 list_for_each_entry(plane, &config->plane_list, head) {
1566 if (put_user(plane->base.id, plane_ptr + copied)) {
1567 ret = -EFAULT;
1568 goto out;
1569 }
1570 copied++;
1571 }
1572 }
1573 plane_resp->count_planes = config->num_plane;
1574
1575out:
1576 mutex_unlock(&dev->mode_config.mutex);
1577 return ret;
1578}
1579
1580/**
1581 * drm_mode_getplane - get plane info
1582 * @dev: DRM device
1583 * @data: ioctl data
1584 * @file_priv: DRM file info
1585 *
1586 * Return plane info, including formats supported, gamma size, any
1587 * current fb, etc.
1588 */
1589int drm_mode_getplane(struct drm_device *dev, void *data,
1590 struct drm_file *file_priv)
1591{
1592 struct drm_mode_get_plane *plane_resp = data;
1593 struct drm_mode_object *obj;
1594 struct drm_plane *plane;
1595 uint32_t __user *format_ptr;
1596 int ret = 0;
1597
1598 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1599 return -EINVAL;
1600
1601 mutex_lock(&dev->mode_config.mutex);
1602 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1603 DRM_MODE_OBJECT_PLANE);
1604 if (!obj) {
1605 ret = -ENOENT;
1606 goto out;
1607 }
1608 plane = obj_to_plane(obj);
1609
1610 if (plane->crtc)
1611 plane_resp->crtc_id = plane->crtc->base.id;
1612 else
1613 plane_resp->crtc_id = 0;
1614
1615 if (plane->fb)
1616 plane_resp->fb_id = plane->fb->base.id;
1617 else
1618 plane_resp->fb_id = 0;
1619
1620 plane_resp->plane_id = plane->base.id;
1621 plane_resp->possible_crtcs = plane->possible_crtcs;
1622 plane_resp->gamma_size = plane->gamma_size;
1623
1624 /*
1625 * This ioctl is called twice, once to determine how much space is
1626 * needed, and the 2nd time to fill it.
1627 */
1628 if (plane->format_count &&
1629 (plane_resp->count_format_types >= plane->format_count)) {
1630 format_ptr = (uint32_t *)(unsigned long)plane_resp->format_type_ptr;
1631 if (copy_to_user(format_ptr,
1632 plane->format_types,
1633 sizeof(uint32_t) * plane->format_count)) {
1634 ret = -EFAULT;
1635 goto out;
1636 }
1637 }
1638 plane_resp->count_format_types = plane->format_count;
1639
1640out:
1641 mutex_unlock(&dev->mode_config.mutex);
1642 return ret;
1643}
1644
1645/**
1646 * drm_mode_setplane - set up or tear down an plane
1647 * @dev: DRM device
1648 * @data: ioctl data*
1649 * @file_prive: DRM file info
1650 *
1651 * Set plane info, including placement, fb, scaling, and other factors.
1652 * Or pass a NULL fb to disable.
1653 */
1654int drm_mode_setplane(struct drm_device *dev, void *data,
1655 struct drm_file *file_priv)
1656{
1657 struct drm_mode_set_plane *plane_req = data;
1658 struct drm_mode_object *obj;
1659 struct drm_plane *plane;
1660 struct drm_crtc *crtc;
1661 struct drm_framebuffer *fb;
1662 int ret = 0;
1663
1664 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1665 return -EINVAL;
1666
1667 mutex_lock(&dev->mode_config.mutex);
1668
1669 /*
1670 * First, find the plane, crtc, and fb objects. If not available,
1671 * we don't bother to call the driver.
1672 */
1673 obj = drm_mode_object_find(dev, plane_req->plane_id,
1674 DRM_MODE_OBJECT_PLANE);
1675 if (!obj) {
1676 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1677 plane_req->plane_id);
1678 ret = -ENOENT;
1679 goto out;
1680 }
1681 plane = obj_to_plane(obj);
1682
1683 /* No fb means shut it down */
1684 if (!plane_req->fb_id) {
1685 plane->funcs->disable_plane(plane);
1686 goto out;
1687 }
1688
1689 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1690 DRM_MODE_OBJECT_CRTC);
1691 if (!obj) {
1692 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1693 plane_req->crtc_id);
1694 ret = -ENOENT;
1695 goto out;
1696 }
1697 crtc = obj_to_crtc(obj);
1698
1699 obj = drm_mode_object_find(dev, plane_req->fb_id,
1700 DRM_MODE_OBJECT_FB);
1701 if (!obj) {
1702 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1703 plane_req->fb_id);
1704 ret = -ENOENT;
1705 goto out;
1706 }
1707 fb = obj_to_fb(obj);
1708
1709 ret = plane->funcs->update_plane(plane, crtc, fb,
1710 plane_req->crtc_x, plane_req->crtc_y,
1711 plane_req->crtc_w, plane_req->crtc_h,
1712 plane_req->src_x, plane_req->src_y,
1713 plane_req->src_w, plane_req->src_h);
1714 if (!ret) {
1715 plane->crtc = crtc;
1716 plane->fb = fb;
1717 }
1718
1719out:
1720 mutex_unlock(&dev->mode_config.mutex);
1721
1722 return ret;
1723}
1724
1725/**
Dave Airlief453ba02008-11-07 14:05:41 -08001726 * drm_mode_setcrtc - set CRTC configuration
1727 * @inode: inode from the ioctl
1728 * @filp: file * from the ioctl
1729 * @cmd: cmd from ioctl
1730 * @arg: arg from ioctl
1731 *
1732 * LOCKING:
1733 * Caller? (FIXME)
1734 *
1735 * Build a new CRTC configuration based on user request.
1736 *
1737 * Called by the user via ioctl.
1738 *
1739 * RETURNS:
1740 * Zero on success, errno on failure.
1741 */
1742int drm_mode_setcrtc(struct drm_device *dev, void *data,
1743 struct drm_file *file_priv)
1744{
1745 struct drm_mode_config *config = &dev->mode_config;
1746 struct drm_mode_crtc *crtc_req = data;
1747 struct drm_mode_object *obj;
1748 struct drm_crtc *crtc, *crtcfb;
1749 struct drm_connector **connector_set = NULL, *connector;
1750 struct drm_framebuffer *fb = NULL;
1751 struct drm_display_mode *mode = NULL;
1752 struct drm_mode_set set;
1753 uint32_t __user *set_connectors_ptr;
1754 int ret = 0;
1755 int i;
1756
Dave Airliefb3b06c2011-02-08 13:55:21 +10001757 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1758 return -EINVAL;
1759
Dave Airlief453ba02008-11-07 14:05:41 -08001760 mutex_lock(&dev->mode_config.mutex);
1761 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1762 DRM_MODE_OBJECT_CRTC);
1763 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001764 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001765 ret = -EINVAL;
1766 goto out;
1767 }
1768 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001769 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001770
1771 if (crtc_req->mode_valid) {
1772 /* If we have a mode we need a framebuffer. */
1773 /* If we pass -1, set the mode with the currently bound fb */
1774 if (crtc_req->fb_id == -1) {
1775 list_for_each_entry(crtcfb,
1776 &dev->mode_config.crtc_list, head) {
1777 if (crtcfb == crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001778 DRM_DEBUG_KMS("Using current fb for "
1779 "setmode\n");
Dave Airlief453ba02008-11-07 14:05:41 -08001780 fb = crtc->fb;
1781 }
1782 }
1783 } else {
1784 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1785 DRM_MODE_OBJECT_FB);
1786 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001787 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1788 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001789 ret = -EINVAL;
1790 goto out;
1791 }
1792 fb = obj_to_fb(obj);
1793 }
1794
1795 mode = drm_mode_create(dev);
1796 drm_crtc_convert_umode(mode, &crtc_req->mode);
1797 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1798 }
1799
1800 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001801 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08001802 ret = -EINVAL;
1803 goto out;
1804 }
1805
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01001806 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001807 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08001808 crtc_req->count_connectors);
1809 ret = -EINVAL;
1810 goto out;
1811 }
1812
1813 if (crtc_req->count_connectors > 0) {
1814 u32 out_id;
1815
1816 /* Avoid unbounded kernel memory allocation */
1817 if (crtc_req->count_connectors > config->num_connector) {
1818 ret = -EINVAL;
1819 goto out;
1820 }
1821
1822 connector_set = kmalloc(crtc_req->count_connectors *
1823 sizeof(struct drm_connector *),
1824 GFP_KERNEL);
1825 if (!connector_set) {
1826 ret = -ENOMEM;
1827 goto out;
1828 }
1829
1830 for (i = 0; i < crtc_req->count_connectors; i++) {
1831 set_connectors_ptr = (uint32_t *)(unsigned long)crtc_req->set_connectors_ptr;
1832 if (get_user(out_id, &set_connectors_ptr[i])) {
1833 ret = -EFAULT;
1834 goto out;
1835 }
1836
1837 obj = drm_mode_object_find(dev, out_id,
1838 DRM_MODE_OBJECT_CONNECTOR);
1839 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001840 DRM_DEBUG_KMS("Connector id %d unknown\n",
1841 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001842 ret = -EINVAL;
1843 goto out;
1844 }
1845 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001846 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1847 connector->base.id,
1848 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001849
1850 connector_set[i] = connector;
1851 }
1852 }
1853
1854 set.crtc = crtc;
1855 set.x = crtc_req->x;
1856 set.y = crtc_req->y;
1857 set.mode = mode;
1858 set.connectors = connector_set;
1859 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10001860 set.fb = fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001861 ret = crtc->funcs->set_config(&set);
1862
1863out:
1864 kfree(connector_set);
1865 mutex_unlock(&dev->mode_config.mutex);
1866 return ret;
1867}
1868
1869int drm_mode_cursor_ioctl(struct drm_device *dev,
1870 void *data, struct drm_file *file_priv)
1871{
1872 struct drm_mode_cursor *req = data;
1873 struct drm_mode_object *obj;
1874 struct drm_crtc *crtc;
1875 int ret = 0;
1876
Dave Airliefb3b06c2011-02-08 13:55:21 +10001877 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1878 return -EINVAL;
1879
Jesse Barnesacb4b992011-11-07 12:03:23 -08001880 if (!req->flags)
Dave Airlief453ba02008-11-07 14:05:41 -08001881 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08001882
1883 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10001884 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08001885 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001886 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001887 ret = -EINVAL;
1888 goto out;
1889 }
1890 crtc = obj_to_crtc(obj);
1891
1892 if (req->flags & DRM_MODE_CURSOR_BO) {
1893 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08001894 ret = -ENXIO;
1895 goto out;
1896 }
1897 /* Turns off the cursor if handle is 0 */
1898 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1899 req->width, req->height);
1900 }
1901
1902 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1903 if (crtc->funcs->cursor_move) {
1904 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1905 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08001906 ret = -EFAULT;
1907 goto out;
1908 }
1909 }
1910out:
1911 mutex_unlock(&dev->mode_config.mutex);
1912 return ret;
1913}
1914
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001915/* Original addfb only supported RGB formats, so figure out which one */
1916uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
1917{
1918 uint32_t fmt;
1919
1920 switch (bpp) {
1921 case 8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02001922 fmt = DRM_FORMAT_RGB332;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001923 break;
1924 case 16:
1925 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02001926 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001927 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02001928 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001929 break;
1930 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02001931 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001932 break;
1933 case 32:
1934 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02001935 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001936 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02001937 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001938 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02001939 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001940 break;
1941 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02001942 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
1943 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001944 break;
1945 }
1946
1947 return fmt;
1948}
1949EXPORT_SYMBOL(drm_mode_legacy_fb_format);
1950
Dave Airlief453ba02008-11-07 14:05:41 -08001951/**
1952 * drm_mode_addfb - add an FB to the graphics configuration
1953 * @inode: inode from the ioctl
1954 * @filp: file * from the ioctl
1955 * @cmd: cmd from ioctl
1956 * @arg: arg from ioctl
1957 *
1958 * LOCKING:
1959 * Takes mode config lock.
1960 *
1961 * Add a new FB to the specified CRTC, given a user request.
1962 *
1963 * Called by the user via ioctl.
1964 *
1965 * RETURNS:
1966 * Zero on success, errno on failure.
1967 */
1968int drm_mode_addfb(struct drm_device *dev,
1969 void *data, struct drm_file *file_priv)
1970{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001971 struct drm_mode_fb_cmd *or = data;
1972 struct drm_mode_fb_cmd2 r = {};
1973 struct drm_mode_config *config = &dev->mode_config;
1974 struct drm_framebuffer *fb;
1975 int ret = 0;
1976
1977 /* Use new struct with format internally */
1978 r.fb_id = or->fb_id;
1979 r.width = or->width;
1980 r.height = or->height;
1981 r.pitches[0] = or->pitch;
1982 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
1983 r.handles[0] = or->handle;
1984
1985 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1986 return -EINVAL;
1987
Jesse Barnesacb4b992011-11-07 12:03:23 -08001988 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001989 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08001990
1991 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001992 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001993
1994 mutex_lock(&dev->mode_config.mutex);
1995
1996 /* TODO check buffer is sufficiently large */
1997 /* TODO setup destructor callback */
1998
1999 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2000 if (IS_ERR(fb)) {
2001 DRM_ERROR("could not create framebuffer\n");
2002 ret = PTR_ERR(fb);
2003 goto out;
2004 }
2005
2006 or->fb_id = fb->base.id;
2007 list_add(&fb->filp_head, &file_priv->fbs);
2008 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2009
2010out:
2011 mutex_unlock(&dev->mode_config.mutex);
2012 return ret;
2013}
2014
2015/**
2016 * drm_mode_addfb2 - add an FB to the graphics configuration
2017 * @inode: inode from the ioctl
2018 * @filp: file * from the ioctl
2019 * @cmd: cmd from ioctl
2020 * @arg: arg from ioctl
2021 *
2022 * LOCKING:
2023 * Takes mode config lock.
2024 *
2025 * Add a new FB to the specified CRTC, given a user request with format.
2026 *
2027 * Called by the user via ioctl.
2028 *
2029 * RETURNS:
2030 * Zero on success, errno on failure.
2031 */
2032int drm_mode_addfb2(struct drm_device *dev,
2033 void *data, struct drm_file *file_priv)
2034{
2035 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002036 struct drm_mode_config *config = &dev->mode_config;
2037 struct drm_framebuffer *fb;
2038 int ret = 0;
2039
Dave Airliefb3b06c2011-02-08 13:55:21 +10002040 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2041 return -EINVAL;
2042
Dave Airlief453ba02008-11-07 14:05:41 -08002043 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002044 DRM_ERROR("bad framebuffer width %d, should be >= %d && <= %d\n",
2045 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002046 return -EINVAL;
2047 }
2048 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002049 DRM_ERROR("bad framebuffer height %d, should be >= %d && <= %d\n",
2050 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002051 return -EINVAL;
2052 }
2053
2054 mutex_lock(&dev->mode_config.mutex);
2055
Dave Airlief453ba02008-11-07 14:05:41 -08002056 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002057 if (IS_ERR(fb)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002058 DRM_ERROR("could not create framebuffer\n");
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002059 ret = PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002060 goto out;
2061 }
2062
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002063 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002064 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002065 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002066
2067out:
2068 mutex_unlock(&dev->mode_config.mutex);
2069 return ret;
2070}
2071
2072/**
2073 * drm_mode_rmfb - remove an FB from the configuration
2074 * @inode: inode from the ioctl
2075 * @filp: file * from the ioctl
2076 * @cmd: cmd from ioctl
2077 * @arg: arg from ioctl
2078 *
2079 * LOCKING:
2080 * Takes mode config lock.
2081 *
2082 * Remove the FB specified by the user.
2083 *
2084 * Called by the user via ioctl.
2085 *
2086 * RETURNS:
2087 * Zero on success, errno on failure.
2088 */
2089int drm_mode_rmfb(struct drm_device *dev,
2090 void *data, struct drm_file *file_priv)
2091{
2092 struct drm_mode_object *obj;
2093 struct drm_framebuffer *fb = NULL;
2094 struct drm_framebuffer *fbl = NULL;
2095 uint32_t *id = data;
2096 int ret = 0;
2097 int found = 0;
2098
Dave Airliefb3b06c2011-02-08 13:55:21 +10002099 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2100 return -EINVAL;
2101
Dave Airlief453ba02008-11-07 14:05:41 -08002102 mutex_lock(&dev->mode_config.mutex);
2103 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002104 /* TODO check that we really get a framebuffer back. */
Dave Airlief453ba02008-11-07 14:05:41 -08002105 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002106 ret = -EINVAL;
2107 goto out;
2108 }
2109 fb = obj_to_fb(obj);
2110
2111 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2112 if (fb == fbl)
2113 found = 1;
2114
2115 if (!found) {
Dave Airlief453ba02008-11-07 14:05:41 -08002116 ret = -EINVAL;
2117 goto out;
2118 }
2119
2120 /* TODO release all crtc connected to the framebuffer */
2121 /* TODO unhock the destructor from the buffer object */
2122
2123 list_del(&fb->filp_head);
2124 fb->funcs->destroy(fb);
2125
2126out:
2127 mutex_unlock(&dev->mode_config.mutex);
2128 return ret;
2129}
2130
2131/**
2132 * drm_mode_getfb - get FB info
2133 * @inode: inode from the ioctl
2134 * @filp: file * from the ioctl
2135 * @cmd: cmd from ioctl
2136 * @arg: arg from ioctl
2137 *
2138 * LOCKING:
2139 * Caller? (FIXME)
2140 *
2141 * Lookup the FB given its ID and return info about it.
2142 *
2143 * Called by the user via ioctl.
2144 *
2145 * RETURNS:
2146 * Zero on success, errno on failure.
2147 */
2148int drm_mode_getfb(struct drm_device *dev,
2149 void *data, struct drm_file *file_priv)
2150{
2151 struct drm_mode_fb_cmd *r = data;
2152 struct drm_mode_object *obj;
2153 struct drm_framebuffer *fb;
2154 int ret = 0;
2155
Dave Airliefb3b06c2011-02-08 13:55:21 +10002156 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2157 return -EINVAL;
2158
Dave Airlief453ba02008-11-07 14:05:41 -08002159 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002160 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
Dave Airlief453ba02008-11-07 14:05:41 -08002161 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002162 ret = -EINVAL;
2163 goto out;
2164 }
2165 fb = obj_to_fb(obj);
2166
2167 r->height = fb->height;
2168 r->width = fb->width;
2169 r->depth = fb->depth;
2170 r->bpp = fb->bits_per_pixel;
2171 r->pitch = fb->pitch;
2172 fb->funcs->create_handle(fb, file_priv, &r->handle);
2173
2174out:
2175 mutex_unlock(&dev->mode_config.mutex);
2176 return ret;
2177}
2178
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002179int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2180 void *data, struct drm_file *file_priv)
2181{
2182 struct drm_clip_rect __user *clips_ptr;
2183 struct drm_clip_rect *clips = NULL;
2184 struct drm_mode_fb_dirty_cmd *r = data;
2185 struct drm_mode_object *obj;
2186 struct drm_framebuffer *fb;
2187 unsigned flags;
2188 int num_clips;
2189 int ret = 0;
2190
Dave Airliefb3b06c2011-02-08 13:55:21 +10002191 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2192 return -EINVAL;
2193
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002194 mutex_lock(&dev->mode_config.mutex);
2195 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2196 if (!obj) {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002197 ret = -EINVAL;
2198 goto out_err1;
2199 }
2200 fb = obj_to_fb(obj);
2201
2202 num_clips = r->num_clips;
2203 clips_ptr = (struct drm_clip_rect *)(unsigned long)r->clips_ptr;
2204
2205 if (!num_clips != !clips_ptr) {
2206 ret = -EINVAL;
2207 goto out_err1;
2208 }
2209
2210 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2211
2212 /* If userspace annotates copy, clips must come in pairs */
2213 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2214 ret = -EINVAL;
2215 goto out_err1;
2216 }
2217
2218 if (num_clips && clips_ptr) {
2219 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2220 if (!clips) {
2221 ret = -ENOMEM;
2222 goto out_err1;
2223 }
2224
2225 ret = copy_from_user(clips, clips_ptr,
2226 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002227 if (ret) {
2228 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002229 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002230 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002231 }
2232
2233 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002234 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2235 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002236 } else {
2237 ret = -ENOSYS;
2238 goto out_err2;
2239 }
2240
2241out_err2:
2242 kfree(clips);
2243out_err1:
2244 mutex_unlock(&dev->mode_config.mutex);
2245 return ret;
2246}
2247
2248
Dave Airlief453ba02008-11-07 14:05:41 -08002249/**
2250 * drm_fb_release - remove and free the FBs on this file
2251 * @filp: file * from the ioctl
2252 *
2253 * LOCKING:
2254 * Takes mode config lock.
2255 *
2256 * Destroy all the FBs associated with @filp.
2257 *
2258 * Called by the user via ioctl.
2259 *
2260 * RETURNS:
2261 * Zero on success, errno on failure.
2262 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002263void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002264{
Dave Airlief453ba02008-11-07 14:05:41 -08002265 struct drm_device *dev = priv->minor->dev;
2266 struct drm_framebuffer *fb, *tfb;
2267
2268 mutex_lock(&dev->mode_config.mutex);
2269 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2270 list_del(&fb->filp_head);
2271 fb->funcs->destroy(fb);
2272 }
2273 mutex_unlock(&dev->mode_config.mutex);
2274}
2275
2276/**
2277 * drm_mode_attachmode - add a mode to the user mode list
2278 * @dev: DRM device
2279 * @connector: connector to add the mode to
2280 * @mode: mode to add
2281 *
2282 * Add @mode to @connector's user mode list.
2283 */
2284static int drm_mode_attachmode(struct drm_device *dev,
2285 struct drm_connector *connector,
2286 struct drm_display_mode *mode)
2287{
2288 int ret = 0;
2289
2290 list_add_tail(&mode->head, &connector->user_modes);
2291 return ret;
2292}
2293
2294int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
2295 struct drm_display_mode *mode)
2296{
2297 struct drm_connector *connector;
2298 int ret = 0;
2299 struct drm_display_mode *dup_mode;
2300 int need_dup = 0;
2301 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2302 if (!connector->encoder)
2303 break;
2304 if (connector->encoder->crtc == crtc) {
2305 if (need_dup)
2306 dup_mode = drm_mode_duplicate(dev, mode);
2307 else
2308 dup_mode = mode;
2309 ret = drm_mode_attachmode(dev, connector, dup_mode);
2310 if (ret)
2311 return ret;
2312 need_dup = 1;
2313 }
2314 }
2315 return 0;
2316}
2317EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2318
2319static int drm_mode_detachmode(struct drm_device *dev,
2320 struct drm_connector *connector,
2321 struct drm_display_mode *mode)
2322{
2323 int found = 0;
2324 int ret = 0;
2325 struct drm_display_mode *match_mode, *t;
2326
2327 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2328 if (drm_mode_equal(match_mode, mode)) {
2329 list_del(&match_mode->head);
2330 drm_mode_destroy(dev, match_mode);
2331 found = 1;
2332 break;
2333 }
2334 }
2335
2336 if (!found)
2337 ret = -EINVAL;
2338
2339 return ret;
2340}
2341
2342int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2343{
2344 struct drm_connector *connector;
2345
2346 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2347 drm_mode_detachmode(dev, connector, mode);
2348 }
2349 return 0;
2350}
2351EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2352
2353/**
2354 * drm_fb_attachmode - Attach a user mode to an connector
2355 * @inode: inode from the ioctl
2356 * @filp: file * from the ioctl
2357 * @cmd: cmd from ioctl
2358 * @arg: arg from ioctl
2359 *
2360 * This attaches a user specified mode to an connector.
2361 * Called by the user via ioctl.
2362 *
2363 * RETURNS:
2364 * Zero on success, errno on failure.
2365 */
2366int drm_mode_attachmode_ioctl(struct drm_device *dev,
2367 void *data, struct drm_file *file_priv)
2368{
2369 struct drm_mode_mode_cmd *mode_cmd = data;
2370 struct drm_connector *connector;
2371 struct drm_display_mode *mode;
2372 struct drm_mode_object *obj;
2373 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2374 int ret = 0;
2375
Dave Airliefb3b06c2011-02-08 13:55:21 +10002376 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2377 return -EINVAL;
2378
Dave Airlief453ba02008-11-07 14:05:41 -08002379 mutex_lock(&dev->mode_config.mutex);
2380
2381 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2382 if (!obj) {
2383 ret = -EINVAL;
2384 goto out;
2385 }
2386 connector = obj_to_connector(obj);
2387
2388 mode = drm_mode_create(dev);
2389 if (!mode) {
2390 ret = -ENOMEM;
2391 goto out;
2392 }
2393
2394 drm_crtc_convert_umode(mode, umode);
2395
2396 ret = drm_mode_attachmode(dev, connector, mode);
2397out:
2398 mutex_unlock(&dev->mode_config.mutex);
2399 return ret;
2400}
2401
2402
2403/**
2404 * drm_fb_detachmode - Detach a user specified mode from an connector
2405 * @inode: inode from the ioctl
2406 * @filp: file * from the ioctl
2407 * @cmd: cmd from ioctl
2408 * @arg: arg from ioctl
2409 *
2410 * Called by the user via ioctl.
2411 *
2412 * RETURNS:
2413 * Zero on success, errno on failure.
2414 */
2415int drm_mode_detachmode_ioctl(struct drm_device *dev,
2416 void *data, struct drm_file *file_priv)
2417{
2418 struct drm_mode_object *obj;
2419 struct drm_mode_mode_cmd *mode_cmd = data;
2420 struct drm_connector *connector;
2421 struct drm_display_mode mode;
2422 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2423 int ret = 0;
2424
Dave Airliefb3b06c2011-02-08 13:55:21 +10002425 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2426 return -EINVAL;
2427
Dave Airlief453ba02008-11-07 14:05:41 -08002428 mutex_lock(&dev->mode_config.mutex);
2429
2430 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2431 if (!obj) {
2432 ret = -EINVAL;
2433 goto out;
2434 }
2435 connector = obj_to_connector(obj);
2436
2437 drm_crtc_convert_umode(&mode, umode);
2438 ret = drm_mode_detachmode(dev, connector, &mode);
2439out:
2440 mutex_unlock(&dev->mode_config.mutex);
2441 return ret;
2442}
2443
2444struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2445 const char *name, int num_values)
2446{
2447 struct drm_property *property = NULL;
2448
2449 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2450 if (!property)
2451 return NULL;
2452
2453 if (num_values) {
2454 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2455 if (!property->values)
2456 goto fail;
2457 }
2458
2459 drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2460 property->flags = flags;
2461 property->num_values = num_values;
2462 INIT_LIST_HEAD(&property->enum_blob_list);
2463
2464 if (name)
2465 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2466
2467 list_add_tail(&property->head, &dev->mode_config.property_list);
2468 return property;
2469fail:
2470 kfree(property);
2471 return NULL;
2472}
2473EXPORT_SYMBOL(drm_property_create);
2474
2475int drm_property_add_enum(struct drm_property *property, int index,
2476 uint64_t value, const char *name)
2477{
2478 struct drm_property_enum *prop_enum;
2479
2480 if (!(property->flags & DRM_MODE_PROP_ENUM))
2481 return -EINVAL;
2482
2483 if (!list_empty(&property->enum_blob_list)) {
2484 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2485 if (prop_enum->value == value) {
2486 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2487 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2488 return 0;
2489 }
2490 }
2491 }
2492
2493 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2494 if (!prop_enum)
2495 return -ENOMEM;
2496
2497 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2498 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2499 prop_enum->value = value;
2500
2501 property->values[index] = value;
2502 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2503 return 0;
2504}
2505EXPORT_SYMBOL(drm_property_add_enum);
2506
2507void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2508{
2509 struct drm_property_enum *prop_enum, *pt;
2510
2511 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2512 list_del(&prop_enum->head);
2513 kfree(prop_enum);
2514 }
2515
2516 if (property->num_values)
2517 kfree(property->values);
2518 drm_mode_object_put(dev, &property->base);
2519 list_del(&property->head);
2520 kfree(property);
2521}
2522EXPORT_SYMBOL(drm_property_destroy);
2523
2524int drm_connector_attach_property(struct drm_connector *connector,
2525 struct drm_property *property, uint64_t init_val)
2526{
2527 int i;
2528
2529 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2530 if (connector->property_ids[i] == 0) {
2531 connector->property_ids[i] = property->base.id;
2532 connector->property_values[i] = init_val;
2533 break;
2534 }
2535 }
2536
2537 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2538 return -EINVAL;
2539 return 0;
2540}
2541EXPORT_SYMBOL(drm_connector_attach_property);
2542
2543int drm_connector_property_set_value(struct drm_connector *connector,
2544 struct drm_property *property, uint64_t value)
2545{
2546 int i;
2547
2548 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2549 if (connector->property_ids[i] == property->base.id) {
2550 connector->property_values[i] = value;
2551 break;
2552 }
2553 }
2554
2555 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2556 return -EINVAL;
2557 return 0;
2558}
2559EXPORT_SYMBOL(drm_connector_property_set_value);
2560
2561int drm_connector_property_get_value(struct drm_connector *connector,
2562 struct drm_property *property, uint64_t *val)
2563{
2564 int i;
2565
2566 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2567 if (connector->property_ids[i] == property->base.id) {
2568 *val = connector->property_values[i];
2569 break;
2570 }
2571 }
2572
2573 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2574 return -EINVAL;
2575 return 0;
2576}
2577EXPORT_SYMBOL(drm_connector_property_get_value);
2578
2579int drm_mode_getproperty_ioctl(struct drm_device *dev,
2580 void *data, struct drm_file *file_priv)
2581{
2582 struct drm_mode_object *obj;
2583 struct drm_mode_get_property *out_resp = data;
2584 struct drm_property *property;
2585 int enum_count = 0;
2586 int blob_count = 0;
2587 int value_count = 0;
2588 int ret = 0, i;
2589 int copied;
2590 struct drm_property_enum *prop_enum;
2591 struct drm_mode_property_enum __user *enum_ptr;
2592 struct drm_property_blob *prop_blob;
2593 uint32_t *blob_id_ptr;
2594 uint64_t __user *values_ptr;
2595 uint32_t __user *blob_length_ptr;
2596
Dave Airliefb3b06c2011-02-08 13:55:21 +10002597 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2598 return -EINVAL;
2599
Dave Airlief453ba02008-11-07 14:05:41 -08002600 mutex_lock(&dev->mode_config.mutex);
2601 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2602 if (!obj) {
2603 ret = -EINVAL;
2604 goto done;
2605 }
2606 property = obj_to_property(obj);
2607
2608 if (property->flags & DRM_MODE_PROP_ENUM) {
2609 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2610 enum_count++;
2611 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2612 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2613 blob_count++;
2614 }
2615
2616 value_count = property->num_values;
2617
2618 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2619 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2620 out_resp->flags = property->flags;
2621
2622 if ((out_resp->count_values >= value_count) && value_count) {
2623 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2624 for (i = 0; i < value_count; i++) {
2625 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2626 ret = -EFAULT;
2627 goto done;
2628 }
2629 }
2630 }
2631 out_resp->count_values = value_count;
2632
2633 if (property->flags & DRM_MODE_PROP_ENUM) {
2634 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2635 copied = 0;
2636 enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2637 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2638
2639 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2640 ret = -EFAULT;
2641 goto done;
2642 }
2643
2644 if (copy_to_user(&enum_ptr[copied].name,
2645 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2646 ret = -EFAULT;
2647 goto done;
2648 }
2649 copied++;
2650 }
2651 }
2652 out_resp->count_enum_blobs = enum_count;
2653 }
2654
2655 if (property->flags & DRM_MODE_PROP_BLOB) {
2656 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2657 copied = 0;
2658 blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2659 blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2660
2661 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2662 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2663 ret = -EFAULT;
2664 goto done;
2665 }
2666
2667 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2668 ret = -EFAULT;
2669 goto done;
2670 }
2671
2672 copied++;
2673 }
2674 }
2675 out_resp->count_enum_blobs = blob_count;
2676 }
2677done:
2678 mutex_unlock(&dev->mode_config.mutex);
2679 return ret;
2680}
2681
2682static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2683 void *data)
2684{
2685 struct drm_property_blob *blob;
2686
2687 if (!length || !data)
2688 return NULL;
2689
2690 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2691 if (!blob)
2692 return NULL;
2693
2694 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2695 blob->length = length;
2696
2697 memcpy(blob->data, data, length);
2698
2699 drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2700
2701 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2702 return blob;
2703}
2704
2705static void drm_property_destroy_blob(struct drm_device *dev,
2706 struct drm_property_blob *blob)
2707{
2708 drm_mode_object_put(dev, &blob->base);
2709 list_del(&blob->head);
2710 kfree(blob);
2711}
2712
2713int drm_mode_getblob_ioctl(struct drm_device *dev,
2714 void *data, struct drm_file *file_priv)
2715{
2716 struct drm_mode_object *obj;
2717 struct drm_mode_get_blob *out_resp = data;
2718 struct drm_property_blob *blob;
2719 int ret = 0;
2720 void *blob_ptr;
2721
Dave Airliefb3b06c2011-02-08 13:55:21 +10002722 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2723 return -EINVAL;
2724
Dave Airlief453ba02008-11-07 14:05:41 -08002725 mutex_lock(&dev->mode_config.mutex);
2726 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2727 if (!obj) {
2728 ret = -EINVAL;
2729 goto done;
2730 }
2731 blob = obj_to_blob(obj);
2732
2733 if (out_resp->length == blob->length) {
2734 blob_ptr = (void *)(unsigned long)out_resp->data;
2735 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2736 ret = -EFAULT;
2737 goto done;
2738 }
2739 }
2740 out_resp->length = blob->length;
2741
2742done:
2743 mutex_unlock(&dev->mode_config.mutex);
2744 return ret;
2745}
2746
2747int drm_mode_connector_update_edid_property(struct drm_connector *connector,
2748 struct edid *edid)
2749{
2750 struct drm_device *dev = connector->dev;
Adam Jackson7466f4c2010-03-29 21:43:23 +00002751 int ret = 0, size;
Dave Airlief453ba02008-11-07 14:05:41 -08002752
2753 if (connector->edid_blob_ptr)
2754 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
2755
2756 /* Delete edid, when there is none. */
2757 if (!edid) {
2758 connector->edid_blob_ptr = NULL;
2759 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
2760 return ret;
2761 }
2762
Adam Jackson7466f4c2010-03-29 21:43:23 +00002763 size = EDID_LENGTH * (1 + edid->extensions);
2764 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
2765 size, edid);
Dave Airlief453ba02008-11-07 14:05:41 -08002766
2767 ret = drm_connector_property_set_value(connector,
2768 dev->mode_config.edid_property,
2769 connector->edid_blob_ptr->base.id);
2770
2771 return ret;
2772}
2773EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
2774
2775int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2776 void *data, struct drm_file *file_priv)
2777{
2778 struct drm_mode_connector_set_property *out_resp = data;
2779 struct drm_mode_object *obj;
2780 struct drm_property *property;
2781 struct drm_connector *connector;
2782 int ret = -EINVAL;
2783 int i;
2784
Dave Airliefb3b06c2011-02-08 13:55:21 +10002785 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2786 return -EINVAL;
2787
Dave Airlief453ba02008-11-07 14:05:41 -08002788 mutex_lock(&dev->mode_config.mutex);
2789
2790 obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2791 if (!obj) {
2792 goto out;
2793 }
2794 connector = obj_to_connector(obj);
2795
2796 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2797 if (connector->property_ids[i] == out_resp->prop_id)
2798 break;
2799 }
2800
2801 if (i == DRM_CONNECTOR_MAX_PROPERTY) {
2802 goto out;
2803 }
2804
2805 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2806 if (!obj) {
2807 goto out;
2808 }
2809 property = obj_to_property(obj);
2810
2811 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2812 goto out;
2813
2814 if (property->flags & DRM_MODE_PROP_RANGE) {
2815 if (out_resp->value < property->values[0])
2816 goto out;
2817
2818 if (out_resp->value > property->values[1])
2819 goto out;
2820 } else {
2821 int found = 0;
2822 for (i = 0; i < property->num_values; i++) {
2823 if (property->values[i] == out_resp->value) {
2824 found = 1;
2825 break;
2826 }
2827 }
2828 if (!found) {
2829 goto out;
2830 }
2831 }
2832
Keith Packardc9fb15f2009-05-30 20:42:28 -07002833 /* Do DPMS ourselves */
2834 if (property == connector->dev->mode_config.dpms_property) {
2835 if (connector->funcs->dpms)
2836 (*connector->funcs->dpms)(connector, (int) out_resp->value);
2837 ret = 0;
2838 } else if (connector->funcs->set_property)
Dave Airlief453ba02008-11-07 14:05:41 -08002839 ret = connector->funcs->set_property(connector, property, out_resp->value);
2840
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002841 /* store the property value if successful */
Dave Airlief453ba02008-11-07 14:05:41 -08002842 if (!ret)
2843 drm_connector_property_set_value(connector, property, out_resp->value);
2844out:
2845 mutex_unlock(&dev->mode_config.mutex);
2846 return ret;
2847}
2848
Dave Airlief453ba02008-11-07 14:05:41 -08002849int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2850 struct drm_encoder *encoder)
2851{
2852 int i;
2853
2854 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2855 if (connector->encoder_ids[i] == 0) {
2856 connector->encoder_ids[i] = encoder->base.id;
2857 return 0;
2858 }
2859 }
2860 return -ENOMEM;
2861}
2862EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
2863
2864void drm_mode_connector_detach_encoder(struct drm_connector *connector,
2865 struct drm_encoder *encoder)
2866{
2867 int i;
2868 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2869 if (connector->encoder_ids[i] == encoder->base.id) {
2870 connector->encoder_ids[i] = 0;
2871 if (connector->encoder == encoder)
2872 connector->encoder = NULL;
2873 break;
2874 }
2875 }
2876}
2877EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
2878
2879bool drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
2880 int gamma_size)
2881{
2882 crtc->gamma_size = gamma_size;
2883
2884 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
2885 if (!crtc->gamma_store) {
2886 crtc->gamma_size = 0;
2887 return false;
2888 }
2889
2890 return true;
2891}
2892EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
2893
2894int drm_mode_gamma_set_ioctl(struct drm_device *dev,
2895 void *data, struct drm_file *file_priv)
2896{
2897 struct drm_mode_crtc_lut *crtc_lut = data;
2898 struct drm_mode_object *obj;
2899 struct drm_crtc *crtc;
2900 void *r_base, *g_base, *b_base;
2901 int size;
2902 int ret = 0;
2903
Dave Airliefb3b06c2011-02-08 13:55:21 +10002904 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2905 return -EINVAL;
2906
Dave Airlief453ba02008-11-07 14:05:41 -08002907 mutex_lock(&dev->mode_config.mutex);
2908 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2909 if (!obj) {
2910 ret = -EINVAL;
2911 goto out;
2912 }
2913 crtc = obj_to_crtc(obj);
2914
2915 /* memcpy into gamma store */
2916 if (crtc_lut->gamma_size != crtc->gamma_size) {
2917 ret = -EINVAL;
2918 goto out;
2919 }
2920
2921 size = crtc_lut->gamma_size * (sizeof(uint16_t));
2922 r_base = crtc->gamma_store;
2923 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
2924 ret = -EFAULT;
2925 goto out;
2926 }
2927
2928 g_base = r_base + size;
2929 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
2930 ret = -EFAULT;
2931 goto out;
2932 }
2933
2934 b_base = g_base + size;
2935 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
2936 ret = -EFAULT;
2937 goto out;
2938 }
2939
James Simmons72034252010-08-03 01:33:19 +01002940 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08002941
2942out:
2943 mutex_unlock(&dev->mode_config.mutex);
2944 return ret;
2945
2946}
2947
2948int drm_mode_gamma_get_ioctl(struct drm_device *dev,
2949 void *data, struct drm_file *file_priv)
2950{
2951 struct drm_mode_crtc_lut *crtc_lut = data;
2952 struct drm_mode_object *obj;
2953 struct drm_crtc *crtc;
2954 void *r_base, *g_base, *b_base;
2955 int size;
2956 int ret = 0;
2957
Dave Airliefb3b06c2011-02-08 13:55:21 +10002958 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2959 return -EINVAL;
2960
Dave Airlief453ba02008-11-07 14:05:41 -08002961 mutex_lock(&dev->mode_config.mutex);
2962 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2963 if (!obj) {
2964 ret = -EINVAL;
2965 goto out;
2966 }
2967 crtc = obj_to_crtc(obj);
2968
2969 /* memcpy into gamma store */
2970 if (crtc_lut->gamma_size != crtc->gamma_size) {
2971 ret = -EINVAL;
2972 goto out;
2973 }
2974
2975 size = crtc_lut->gamma_size * (sizeof(uint16_t));
2976 r_base = crtc->gamma_store;
2977 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
2978 ret = -EFAULT;
2979 goto out;
2980 }
2981
2982 g_base = r_base + size;
2983 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
2984 ret = -EFAULT;
2985 goto out;
2986 }
2987
2988 b_base = g_base + size;
2989 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
2990 ret = -EFAULT;
2991 goto out;
2992 }
2993out:
2994 mutex_unlock(&dev->mode_config.mutex);
2995 return ret;
2996}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05002997
2998int drm_mode_page_flip_ioctl(struct drm_device *dev,
2999 void *data, struct drm_file *file_priv)
3000{
3001 struct drm_mode_crtc_page_flip *page_flip = data;
3002 struct drm_mode_object *obj;
3003 struct drm_crtc *crtc;
3004 struct drm_framebuffer *fb;
3005 struct drm_pending_vblank_event *e = NULL;
3006 unsigned long flags;
3007 int ret = -EINVAL;
3008
3009 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3010 page_flip->reserved != 0)
3011 return -EINVAL;
3012
3013 mutex_lock(&dev->mode_config.mutex);
3014 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3015 if (!obj)
3016 goto out;
3017 crtc = obj_to_crtc(obj);
3018
Chris Wilson90c1efd2010-07-17 20:23:26 +01003019 if (crtc->fb == NULL) {
3020 /* The framebuffer is currently unbound, presumably
3021 * due to a hotplug event, that userspace has not
3022 * yet discovered.
3023 */
3024 ret = -EBUSY;
3025 goto out;
3026 }
3027
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003028 if (crtc->funcs->page_flip == NULL)
3029 goto out;
3030
3031 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3032 if (!obj)
3033 goto out;
3034 fb = obj_to_fb(obj);
3035
3036 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3037 ret = -ENOMEM;
3038 spin_lock_irqsave(&dev->event_lock, flags);
3039 if (file_priv->event_space < sizeof e->event) {
3040 spin_unlock_irqrestore(&dev->event_lock, flags);
3041 goto out;
3042 }
3043 file_priv->event_space -= sizeof e->event;
3044 spin_unlock_irqrestore(&dev->event_lock, flags);
3045
3046 e = kzalloc(sizeof *e, GFP_KERNEL);
3047 if (e == NULL) {
3048 spin_lock_irqsave(&dev->event_lock, flags);
3049 file_priv->event_space += sizeof e->event;
3050 spin_unlock_irqrestore(&dev->event_lock, flags);
3051 goto out;
3052 }
3053
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003054 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003055 e->event.base.length = sizeof e->event;
3056 e->event.user_data = page_flip->user_data;
3057 e->base.event = &e->event.base;
3058 e->base.file_priv = file_priv;
3059 e->base.destroy =
3060 (void (*) (struct drm_pending_event *)) kfree;
3061 }
3062
3063 ret = crtc->funcs->page_flip(crtc, fb, e);
3064 if (ret) {
3065 spin_lock_irqsave(&dev->event_lock, flags);
3066 file_priv->event_space += sizeof e->event;
3067 spin_unlock_irqrestore(&dev->event_lock, flags);
3068 kfree(e);
3069 }
3070
3071out:
3072 mutex_unlock(&dev->mode_config.mutex);
3073 return ret;
3074}
Chris Wilsoneb033552011-01-24 15:11:08 +00003075
3076void drm_mode_config_reset(struct drm_device *dev)
3077{
3078 struct drm_crtc *crtc;
3079 struct drm_encoder *encoder;
3080 struct drm_connector *connector;
3081
3082 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3083 if (crtc->funcs->reset)
3084 crtc->funcs->reset(crtc);
3085
3086 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3087 if (encoder->funcs->reset)
3088 encoder->funcs->reset(encoder);
3089
3090 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
3091 if (connector->funcs->reset)
3092 connector->funcs->reset(connector);
3093}
3094EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003095
3096int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3097 void *data, struct drm_file *file_priv)
3098{
3099 struct drm_mode_create_dumb *args = data;
3100
3101 if (!dev->driver->dumb_create)
3102 return -ENOSYS;
3103 return dev->driver->dumb_create(file_priv, dev, args);
3104}
3105
3106int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3107 void *data, struct drm_file *file_priv)
3108{
3109 struct drm_mode_map_dumb *args = data;
3110
3111 /* call driver ioctl to get mmap offset */
3112 if (!dev->driver->dumb_map_offset)
3113 return -ENOSYS;
3114
3115 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3116}
3117
3118int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3119 void *data, struct drm_file *file_priv)
3120{
3121 struct drm_mode_destroy_dumb *args = data;
3122
3123 if (!dev->driver->dumb_destroy)
3124 return -ENOSYS;
3125
3126 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3127}
Dave Airlie248dbc22011-11-29 20:02:54 +00003128
3129/*
3130 * Just need to support RGB formats here for compat with code that doesn't
3131 * use pixel formats directly yet.
3132 */
3133void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3134 int *bpp)
3135{
3136 switch (format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02003137 case DRM_FORMAT_RGB332:
3138 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003139 *depth = 8;
3140 *bpp = 8;
3141 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003142 case DRM_FORMAT_XRGB1555:
3143 case DRM_FORMAT_XBGR1555:
3144 case DRM_FORMAT_RGBX5551:
3145 case DRM_FORMAT_BGRX5551:
3146 case DRM_FORMAT_ARGB1555:
3147 case DRM_FORMAT_ABGR1555:
3148 case DRM_FORMAT_RGBA5551:
3149 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003150 *depth = 15;
3151 *bpp = 16;
3152 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003153 case DRM_FORMAT_RGB565:
3154 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003155 *depth = 16;
3156 *bpp = 16;
3157 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003158 case DRM_FORMAT_RGB888:
3159 case DRM_FORMAT_BGR888:
3160 *depth = 24;
3161 *bpp = 24;
3162 break;
3163 case DRM_FORMAT_XRGB8888:
3164 case DRM_FORMAT_XBGR8888:
3165 case DRM_FORMAT_RGBX8888:
3166 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003167 *depth = 24;
3168 *bpp = 32;
3169 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003170 case DRM_FORMAT_XRGB2101010:
3171 case DRM_FORMAT_XBGR2101010:
3172 case DRM_FORMAT_RGBX1010102:
3173 case DRM_FORMAT_BGRX1010102:
3174 case DRM_FORMAT_ARGB2101010:
3175 case DRM_FORMAT_ABGR2101010:
3176 case DRM_FORMAT_RGBA1010102:
3177 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003178 *depth = 30;
3179 *bpp = 32;
3180 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003181 case DRM_FORMAT_ARGB8888:
3182 case DRM_FORMAT_ABGR8888:
3183 case DRM_FORMAT_RGBA8888:
3184 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003185 *depth = 32;
3186 *bpp = 32;
3187 break;
3188 default:
3189 DRM_DEBUG_KMS("unsupported pixel format\n");
3190 *depth = 0;
3191 *bpp = 0;
3192 break;
3193 }
3194}
3195EXPORT_SYMBOL(drm_fb_get_bpp_depth);