blob: b98e49112081e8c2b4362c79452a27ff14ab6096 [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);
Ville Syrjälä10bf5732011-12-20 00:06:41 +0200569 mutex_unlock(&dev->mode_config.mutex);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800570 return -ENOMEM;
571 }
572
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800573 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800574 plane->format_count = format_count;
575 plane->possible_crtcs = possible_crtcs;
576
577 list_add_tail(&plane->head, &dev->mode_config.plane_list);
578 dev->mode_config.num_plane++;
579
580 mutex_unlock(&dev->mode_config.mutex);
581
582 return 0;
583}
584EXPORT_SYMBOL(drm_plane_init);
585
586void drm_plane_cleanup(struct drm_plane *plane)
587{
588 struct drm_device *dev = plane->dev;
589
590 mutex_lock(&dev->mode_config.mutex);
591 kfree(plane->format_types);
592 drm_mode_object_put(dev, &plane->base);
593 list_del(&plane->head);
594 dev->mode_config.num_plane--;
595 mutex_unlock(&dev->mode_config.mutex);
596}
597EXPORT_SYMBOL(drm_plane_cleanup);
598
Dave Airlief453ba02008-11-07 14:05:41 -0800599/**
600 * drm_mode_create - create a new display mode
601 * @dev: DRM device
602 *
603 * LOCKING:
604 * Caller must hold DRM mode_config lock.
605 *
606 * Create a new drm_display_mode, give it an ID, and return it.
607 *
608 * RETURNS:
609 * Pointer to new mode on success, NULL on error.
610 */
611struct drm_display_mode *drm_mode_create(struct drm_device *dev)
612{
613 struct drm_display_mode *nmode;
614
615 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
616 if (!nmode)
617 return NULL;
618
619 drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
620 return nmode;
621}
622EXPORT_SYMBOL(drm_mode_create);
623
624/**
625 * drm_mode_destroy - remove a mode
626 * @dev: DRM device
627 * @mode: mode to remove
628 *
629 * LOCKING:
630 * Caller must hold mode config lock.
631 *
632 * Free @mode's unique identifier, then free it.
633 */
634void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
635{
636 drm_mode_object_put(dev, &mode->base);
637
638 kfree(mode);
639}
640EXPORT_SYMBOL(drm_mode_destroy);
641
642static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
643{
644 struct drm_property *edid;
645 struct drm_property *dpms;
646 int i;
647
648 /*
649 * Standard properties (apply to all connectors)
650 */
651 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
652 DRM_MODE_PROP_IMMUTABLE,
653 "EDID", 0);
654 dev->mode_config.edid_property = edid;
655
656 dpms = drm_property_create(dev, DRM_MODE_PROP_ENUM,
657 "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
658 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
659 drm_property_add_enum(dpms, i, drm_dpms_enum_list[i].type,
660 drm_dpms_enum_list[i].name);
661 dev->mode_config.dpms_property = dpms;
662
663 return 0;
664}
665
666/**
667 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
668 * @dev: DRM device
669 *
670 * Called by a driver the first time a DVI-I connector is made.
671 */
672int drm_mode_create_dvi_i_properties(struct drm_device *dev)
673{
674 struct drm_property *dvi_i_selector;
675 struct drm_property *dvi_i_subconnector;
676 int i;
677
678 if (dev->mode_config.dvi_i_select_subconnector_property)
679 return 0;
680
681 dvi_i_selector =
682 drm_property_create(dev, DRM_MODE_PROP_ENUM,
683 "select subconnector",
684 ARRAY_SIZE(drm_dvi_i_select_enum_list));
685 for (i = 0; i < ARRAY_SIZE(drm_dvi_i_select_enum_list); i++)
686 drm_property_add_enum(dvi_i_selector, i,
687 drm_dvi_i_select_enum_list[i].type,
688 drm_dvi_i_select_enum_list[i].name);
689 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
690
691 dvi_i_subconnector =
692 drm_property_create(dev, DRM_MODE_PROP_ENUM |
693 DRM_MODE_PROP_IMMUTABLE,
694 "subconnector",
695 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
696 for (i = 0; i < ARRAY_SIZE(drm_dvi_i_subconnector_enum_list); i++)
697 drm_property_add_enum(dvi_i_subconnector, i,
698 drm_dvi_i_subconnector_enum_list[i].type,
699 drm_dvi_i_subconnector_enum_list[i].name);
700 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
701
702 return 0;
703}
704EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
705
706/**
707 * drm_create_tv_properties - create TV specific connector properties
708 * @dev: DRM device
709 * @num_modes: number of different TV formats (modes) supported
710 * @modes: array of pointers to strings containing name of each format
711 *
712 * Called by a driver's TV initialization routine, this function creates
713 * the TV specific connector properties for a given device. Caller is
714 * responsible for allocating a list of format names and passing them to
715 * this routine.
716 */
717int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
718 char *modes[])
719{
720 struct drm_property *tv_selector;
721 struct drm_property *tv_subconnector;
722 int i;
723
724 if (dev->mode_config.tv_select_subconnector_property)
725 return 0;
726
727 /*
728 * Basic connector properties
729 */
730 tv_selector = drm_property_create(dev, DRM_MODE_PROP_ENUM,
731 "select subconnector",
732 ARRAY_SIZE(drm_tv_select_enum_list));
733 for (i = 0; i < ARRAY_SIZE(drm_tv_select_enum_list); i++)
734 drm_property_add_enum(tv_selector, i,
735 drm_tv_select_enum_list[i].type,
736 drm_tv_select_enum_list[i].name);
737 dev->mode_config.tv_select_subconnector_property = tv_selector;
738
739 tv_subconnector =
740 drm_property_create(dev, DRM_MODE_PROP_ENUM |
741 DRM_MODE_PROP_IMMUTABLE, "subconnector",
742 ARRAY_SIZE(drm_tv_subconnector_enum_list));
743 for (i = 0; i < ARRAY_SIZE(drm_tv_subconnector_enum_list); i++)
744 drm_property_add_enum(tv_subconnector, i,
745 drm_tv_subconnector_enum_list[i].type,
746 drm_tv_subconnector_enum_list[i].name);
747 dev->mode_config.tv_subconnector_property = tv_subconnector;
748
749 /*
750 * Other, TV specific properties: margins & TV modes.
751 */
752 dev->mode_config.tv_left_margin_property =
753 drm_property_create(dev, DRM_MODE_PROP_RANGE,
754 "left margin", 2);
755 dev->mode_config.tv_left_margin_property->values[0] = 0;
756 dev->mode_config.tv_left_margin_property->values[1] = 100;
757
758 dev->mode_config.tv_right_margin_property =
759 drm_property_create(dev, DRM_MODE_PROP_RANGE,
760 "right margin", 2);
761 dev->mode_config.tv_right_margin_property->values[0] = 0;
762 dev->mode_config.tv_right_margin_property->values[1] = 100;
763
764 dev->mode_config.tv_top_margin_property =
765 drm_property_create(dev, DRM_MODE_PROP_RANGE,
766 "top margin", 2);
767 dev->mode_config.tv_top_margin_property->values[0] = 0;
768 dev->mode_config.tv_top_margin_property->values[1] = 100;
769
770 dev->mode_config.tv_bottom_margin_property =
771 drm_property_create(dev, DRM_MODE_PROP_RANGE,
772 "bottom margin", 2);
773 dev->mode_config.tv_bottom_margin_property->values[0] = 0;
774 dev->mode_config.tv_bottom_margin_property->values[1] = 100;
775
776 dev->mode_config.tv_mode_property =
777 drm_property_create(dev, DRM_MODE_PROP_ENUM,
778 "mode", num_modes);
779 for (i = 0; i < num_modes; i++)
780 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
781 i, modes[i]);
782
Francisco Jerezb6b79022009-08-02 04:19:20 +0200783 dev->mode_config.tv_brightness_property =
784 drm_property_create(dev, DRM_MODE_PROP_RANGE,
785 "brightness", 2);
786 dev->mode_config.tv_brightness_property->values[0] = 0;
787 dev->mode_config.tv_brightness_property->values[1] = 100;
788
789 dev->mode_config.tv_contrast_property =
790 drm_property_create(dev, DRM_MODE_PROP_RANGE,
791 "contrast", 2);
792 dev->mode_config.tv_contrast_property->values[0] = 0;
793 dev->mode_config.tv_contrast_property->values[1] = 100;
794
795 dev->mode_config.tv_flicker_reduction_property =
796 drm_property_create(dev, DRM_MODE_PROP_RANGE,
797 "flicker reduction", 2);
798 dev->mode_config.tv_flicker_reduction_property->values[0] = 0;
799 dev->mode_config.tv_flicker_reduction_property->values[1] = 100;
800
Francisco Jereza75f0232009-08-12 02:30:10 +0200801 dev->mode_config.tv_overscan_property =
802 drm_property_create(dev, DRM_MODE_PROP_RANGE,
803 "overscan", 2);
804 dev->mode_config.tv_overscan_property->values[0] = 0;
805 dev->mode_config.tv_overscan_property->values[1] = 100;
806
807 dev->mode_config.tv_saturation_property =
808 drm_property_create(dev, DRM_MODE_PROP_RANGE,
809 "saturation", 2);
810 dev->mode_config.tv_saturation_property->values[0] = 0;
811 dev->mode_config.tv_saturation_property->values[1] = 100;
812
813 dev->mode_config.tv_hue_property =
814 drm_property_create(dev, DRM_MODE_PROP_RANGE,
815 "hue", 2);
816 dev->mode_config.tv_hue_property->values[0] = 0;
817 dev->mode_config.tv_hue_property->values[1] = 100;
818
Dave Airlief453ba02008-11-07 14:05:41 -0800819 return 0;
820}
821EXPORT_SYMBOL(drm_mode_create_tv_properties);
822
823/**
824 * drm_mode_create_scaling_mode_property - create scaling mode property
825 * @dev: DRM device
826 *
827 * Called by a driver the first time it's needed, must be attached to desired
828 * connectors.
829 */
830int drm_mode_create_scaling_mode_property(struct drm_device *dev)
831{
832 struct drm_property *scaling_mode;
833 int i;
834
835 if (dev->mode_config.scaling_mode_property)
836 return 0;
837
838 scaling_mode =
839 drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
840 ARRAY_SIZE(drm_scaling_mode_enum_list));
841 for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++)
842 drm_property_add_enum(scaling_mode, i,
843 drm_scaling_mode_enum_list[i].type,
844 drm_scaling_mode_enum_list[i].name);
845
846 dev->mode_config.scaling_mode_property = scaling_mode;
847
848 return 0;
849}
850EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
851
852/**
853 * drm_mode_create_dithering_property - create dithering property
854 * @dev: DRM device
855 *
856 * Called by a driver the first time it's needed, must be attached to desired
857 * connectors.
858 */
859int drm_mode_create_dithering_property(struct drm_device *dev)
860{
861 struct drm_property *dithering_mode;
862 int i;
863
864 if (dev->mode_config.dithering_mode_property)
865 return 0;
866
867 dithering_mode =
868 drm_property_create(dev, DRM_MODE_PROP_ENUM, "dithering",
869 ARRAY_SIZE(drm_dithering_mode_enum_list));
870 for (i = 0; i < ARRAY_SIZE(drm_dithering_mode_enum_list); i++)
871 drm_property_add_enum(dithering_mode, i,
872 drm_dithering_mode_enum_list[i].type,
873 drm_dithering_mode_enum_list[i].name);
874 dev->mode_config.dithering_mode_property = dithering_mode;
875
876 return 0;
877}
878EXPORT_SYMBOL(drm_mode_create_dithering_property);
879
880/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000881 * drm_mode_create_dirty_property - create dirty property
882 * @dev: DRM device
883 *
884 * Called by a driver the first time it's needed, must be attached to desired
885 * connectors.
886 */
887int drm_mode_create_dirty_info_property(struct drm_device *dev)
888{
889 struct drm_property *dirty_info;
890 int i;
891
892 if (dev->mode_config.dirty_info_property)
893 return 0;
894
895 dirty_info =
896 drm_property_create(dev, DRM_MODE_PROP_ENUM |
897 DRM_MODE_PROP_IMMUTABLE,
898 "dirty",
899 ARRAY_SIZE(drm_dirty_info_enum_list));
900 for (i = 0; i < ARRAY_SIZE(drm_dirty_info_enum_list); i++)
901 drm_property_add_enum(dirty_info, i,
902 drm_dirty_info_enum_list[i].type,
903 drm_dirty_info_enum_list[i].name);
904 dev->mode_config.dirty_info_property = dirty_info;
905
906 return 0;
907}
908EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
909
910/**
Dave Airlief453ba02008-11-07 14:05:41 -0800911 * drm_mode_config_init - initialize DRM mode_configuration structure
912 * @dev: DRM device
913 *
914 * LOCKING:
915 * None, should happen single threaded at init time.
916 *
917 * Initialize @dev's mode_config structure, used for tracking the graphics
918 * configuration of @dev.
919 */
920void drm_mode_config_init(struct drm_device *dev)
921{
922 mutex_init(&dev->mode_config.mutex);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000923 mutex_init(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800924 INIT_LIST_HEAD(&dev->mode_config.fb_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800925 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
926 INIT_LIST_HEAD(&dev->mode_config.connector_list);
927 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
928 INIT_LIST_HEAD(&dev->mode_config.property_list);
929 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800930 INIT_LIST_HEAD(&dev->mode_config.plane_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800931 idr_init(&dev->mode_config.crtc_idr);
932
933 mutex_lock(&dev->mode_config.mutex);
934 drm_mode_create_standard_connector_properties(dev);
935 mutex_unlock(&dev->mode_config.mutex);
936
937 /* Just to be sure */
938 dev->mode_config.num_fb = 0;
939 dev->mode_config.num_connector = 0;
940 dev->mode_config.num_crtc = 0;
941 dev->mode_config.num_encoder = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800942}
943EXPORT_SYMBOL(drm_mode_config_init);
944
945int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
946{
947 uint32_t total_objects = 0;
948
949 total_objects += dev->mode_config.num_crtc;
950 total_objects += dev->mode_config.num_connector;
951 total_objects += dev->mode_config.num_encoder;
952
Dave Airlief453ba02008-11-07 14:05:41 -0800953 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
954 if (!group->id_list)
955 return -ENOMEM;
956
957 group->num_crtcs = 0;
958 group->num_connectors = 0;
959 group->num_encoders = 0;
960 return 0;
961}
962
963int drm_mode_group_init_legacy_group(struct drm_device *dev,
964 struct drm_mode_group *group)
965{
966 struct drm_crtc *crtc;
967 struct drm_encoder *encoder;
968 struct drm_connector *connector;
969 int ret;
970
971 if ((ret = drm_mode_group_init(dev, group)))
972 return ret;
973
974 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
975 group->id_list[group->num_crtcs++] = crtc->base.id;
976
977 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
978 group->id_list[group->num_crtcs + group->num_encoders++] =
979 encoder->base.id;
980
981 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
982 group->id_list[group->num_crtcs + group->num_encoders +
983 group->num_connectors++] = connector->base.id;
984
985 return 0;
986}
987
988/**
989 * drm_mode_config_cleanup - free up DRM mode_config info
990 * @dev: DRM device
991 *
992 * LOCKING:
993 * Caller must hold mode config lock.
994 *
995 * Free up all the connectors and CRTCs associated with this DRM device, then
996 * free up the framebuffers and associated buffer objects.
997 *
998 * FIXME: cleanup any dangling user buffer objects too
999 */
1000void drm_mode_config_cleanup(struct drm_device *dev)
1001{
1002 struct drm_connector *connector, *ot;
1003 struct drm_crtc *crtc, *ct;
1004 struct drm_encoder *encoder, *enct;
1005 struct drm_framebuffer *fb, *fbt;
1006 struct drm_property *property, *pt;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001007 struct drm_plane *plane, *plt;
Dave Airlief453ba02008-11-07 14:05:41 -08001008
1009 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1010 head) {
1011 encoder->funcs->destroy(encoder);
1012 }
1013
1014 list_for_each_entry_safe(connector, ot,
1015 &dev->mode_config.connector_list, head) {
1016 connector->funcs->destroy(connector);
1017 }
1018
1019 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1020 head) {
1021 drm_property_destroy(dev, property);
1022 }
1023
1024 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1025 fb->funcs->destroy(fb);
1026 }
1027
1028 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1029 crtc->funcs->destroy(crtc);
1030 }
1031
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001032 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1033 head) {
1034 plane->funcs->destroy(plane);
1035 }
Dave Airlief453ba02008-11-07 14:05:41 -08001036}
1037EXPORT_SYMBOL(drm_mode_config_cleanup);
1038
Dave Airlief453ba02008-11-07 14:05:41 -08001039/**
1040 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1041 * @out: drm_mode_modeinfo struct to return to the user
1042 * @in: drm_display_mode to use
1043 *
1044 * LOCKING:
1045 * None.
1046 *
1047 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1048 * the user.
1049 */
1050void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1051 struct drm_display_mode *in)
1052{
1053 out->clock = in->clock;
1054 out->hdisplay = in->hdisplay;
1055 out->hsync_start = in->hsync_start;
1056 out->hsync_end = in->hsync_end;
1057 out->htotal = in->htotal;
1058 out->hskew = in->hskew;
1059 out->vdisplay = in->vdisplay;
1060 out->vsync_start = in->vsync_start;
1061 out->vsync_end = in->vsync_end;
1062 out->vtotal = in->vtotal;
1063 out->vscan = in->vscan;
1064 out->vrefresh = in->vrefresh;
1065 out->flags = in->flags;
1066 out->type = in->type;
1067 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1068 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1069}
1070
1071/**
1072 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1073 * @out: drm_display_mode to return to the user
1074 * @in: drm_mode_modeinfo to use
1075 *
1076 * LOCKING:
1077 * None.
1078 *
1079 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1080 * the caller.
1081 */
1082void drm_crtc_convert_umode(struct drm_display_mode *out,
1083 struct drm_mode_modeinfo *in)
1084{
1085 out->clock = in->clock;
1086 out->hdisplay = in->hdisplay;
1087 out->hsync_start = in->hsync_start;
1088 out->hsync_end = in->hsync_end;
1089 out->htotal = in->htotal;
1090 out->hskew = in->hskew;
1091 out->vdisplay = in->vdisplay;
1092 out->vsync_start = in->vsync_start;
1093 out->vsync_end = in->vsync_end;
1094 out->vtotal = in->vtotal;
1095 out->vscan = in->vscan;
1096 out->vrefresh = in->vrefresh;
1097 out->flags = in->flags;
1098 out->type = in->type;
1099 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1100 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1101}
1102
1103/**
1104 * drm_mode_getresources - get graphics configuration
1105 * @inode: inode from the ioctl
1106 * @filp: file * from the ioctl
1107 * @cmd: cmd from ioctl
1108 * @arg: arg from ioctl
1109 *
1110 * LOCKING:
1111 * Takes mode config lock.
1112 *
1113 * Construct a set of configuration description structures and return
1114 * them to the user, including CRTC, connector and framebuffer configuration.
1115 *
1116 * Called by the user via ioctl.
1117 *
1118 * RETURNS:
1119 * Zero on success, errno on failure.
1120 */
1121int drm_mode_getresources(struct drm_device *dev, void *data,
1122 struct drm_file *file_priv)
1123{
1124 struct drm_mode_card_res *card_res = data;
1125 struct list_head *lh;
1126 struct drm_framebuffer *fb;
1127 struct drm_connector *connector;
1128 struct drm_crtc *crtc;
1129 struct drm_encoder *encoder;
1130 int ret = 0;
1131 int connector_count = 0;
1132 int crtc_count = 0;
1133 int fb_count = 0;
1134 int encoder_count = 0;
1135 int copied = 0, i;
1136 uint32_t __user *fb_id;
1137 uint32_t __user *crtc_id;
1138 uint32_t __user *connector_id;
1139 uint32_t __user *encoder_id;
1140 struct drm_mode_group *mode_group;
1141
Dave Airliefb3b06c2011-02-08 13:55:21 +10001142 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1143 return -EINVAL;
1144
Dave Airlief453ba02008-11-07 14:05:41 -08001145 mutex_lock(&dev->mode_config.mutex);
1146
1147 /*
1148 * For the non-control nodes we need to limit the list of resources
1149 * by IDs in the group list for this node
1150 */
1151 list_for_each(lh, &file_priv->fbs)
1152 fb_count++;
1153
1154 mode_group = &file_priv->master->minor->mode_group;
1155 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1156
1157 list_for_each(lh, &dev->mode_config.crtc_list)
1158 crtc_count++;
1159
1160 list_for_each(lh, &dev->mode_config.connector_list)
1161 connector_count++;
1162
1163 list_for_each(lh, &dev->mode_config.encoder_list)
1164 encoder_count++;
1165 } else {
1166
1167 crtc_count = mode_group->num_crtcs;
1168 connector_count = mode_group->num_connectors;
1169 encoder_count = mode_group->num_encoders;
1170 }
1171
1172 card_res->max_height = dev->mode_config.max_height;
1173 card_res->min_height = dev->mode_config.min_height;
1174 card_res->max_width = dev->mode_config.max_width;
1175 card_res->min_width = dev->mode_config.min_width;
1176
1177 /* handle this in 4 parts */
1178 /* FBs */
1179 if (card_res->count_fbs >= fb_count) {
1180 copied = 0;
1181 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
Sascha Hauer618c75e2011-06-03 12:54:14 +02001182 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
Dave Airlief453ba02008-11-07 14:05:41 -08001183 if (put_user(fb->base.id, fb_id + copied)) {
1184 ret = -EFAULT;
1185 goto out;
1186 }
1187 copied++;
1188 }
1189 }
1190 card_res->count_fbs = fb_count;
1191
1192 /* CRTCs */
1193 if (card_res->count_crtcs >= crtc_count) {
1194 copied = 0;
1195 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1196 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1197 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1198 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001199 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001200 if (put_user(crtc->base.id, crtc_id + copied)) {
1201 ret = -EFAULT;
1202 goto out;
1203 }
1204 copied++;
1205 }
1206 } else {
1207 for (i = 0; i < mode_group->num_crtcs; i++) {
1208 if (put_user(mode_group->id_list[i],
1209 crtc_id + copied)) {
1210 ret = -EFAULT;
1211 goto out;
1212 }
1213 copied++;
1214 }
1215 }
1216 }
1217 card_res->count_crtcs = crtc_count;
1218
1219 /* Encoders */
1220 if (card_res->count_encoders >= encoder_count) {
1221 copied = 0;
1222 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1223 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1224 list_for_each_entry(encoder,
1225 &dev->mode_config.encoder_list,
1226 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001227 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1228 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001229 if (put_user(encoder->base.id, encoder_id +
1230 copied)) {
1231 ret = -EFAULT;
1232 goto out;
1233 }
1234 copied++;
1235 }
1236 } else {
1237 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1238 if (put_user(mode_group->id_list[i],
1239 encoder_id + copied)) {
1240 ret = -EFAULT;
1241 goto out;
1242 }
1243 copied++;
1244 }
1245
1246 }
1247 }
1248 card_res->count_encoders = encoder_count;
1249
1250 /* Connectors */
1251 if (card_res->count_connectors >= connector_count) {
1252 copied = 0;
1253 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1254 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1255 list_for_each_entry(connector,
1256 &dev->mode_config.connector_list,
1257 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001258 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1259 connector->base.id,
1260 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001261 if (put_user(connector->base.id,
1262 connector_id + copied)) {
1263 ret = -EFAULT;
1264 goto out;
1265 }
1266 copied++;
1267 }
1268 } else {
1269 int start = mode_group->num_crtcs +
1270 mode_group->num_encoders;
1271 for (i = start; i < start + mode_group->num_connectors; i++) {
1272 if (put_user(mode_group->id_list[i],
1273 connector_id + copied)) {
1274 ret = -EFAULT;
1275 goto out;
1276 }
1277 copied++;
1278 }
1279 }
1280 }
1281 card_res->count_connectors = connector_count;
1282
Jerome Glisse94401062010-07-15 15:43:25 -04001283 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001284 card_res->count_connectors, card_res->count_encoders);
1285
1286out:
1287 mutex_unlock(&dev->mode_config.mutex);
1288 return ret;
1289}
1290
1291/**
1292 * drm_mode_getcrtc - get CRTC configuration
1293 * @inode: inode from the ioctl
1294 * @filp: file * from the ioctl
1295 * @cmd: cmd from ioctl
1296 * @arg: arg from ioctl
1297 *
1298 * LOCKING:
1299 * Caller? (FIXME)
1300 *
1301 * Construct a CRTC configuration structure to return to the user.
1302 *
1303 * Called by the user via ioctl.
1304 *
1305 * RETURNS:
1306 * Zero on success, errno on failure.
1307 */
1308int drm_mode_getcrtc(struct drm_device *dev,
1309 void *data, struct drm_file *file_priv)
1310{
1311 struct drm_mode_crtc *crtc_resp = data;
1312 struct drm_crtc *crtc;
1313 struct drm_mode_object *obj;
1314 int ret = 0;
1315
Dave Airliefb3b06c2011-02-08 13:55:21 +10001316 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1317 return -EINVAL;
1318
Dave Airlief453ba02008-11-07 14:05:41 -08001319 mutex_lock(&dev->mode_config.mutex);
1320
1321 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1322 DRM_MODE_OBJECT_CRTC);
1323 if (!obj) {
1324 ret = -EINVAL;
1325 goto out;
1326 }
1327 crtc = obj_to_crtc(obj);
1328
1329 crtc_resp->x = crtc->x;
1330 crtc_resp->y = crtc->y;
1331 crtc_resp->gamma_size = crtc->gamma_size;
1332 if (crtc->fb)
1333 crtc_resp->fb_id = crtc->fb->base.id;
1334 else
1335 crtc_resp->fb_id = 0;
1336
1337 if (crtc->enabled) {
1338
1339 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1340 crtc_resp->mode_valid = 1;
1341
1342 } else {
1343 crtc_resp->mode_valid = 0;
1344 }
1345
1346out:
1347 mutex_unlock(&dev->mode_config.mutex);
1348 return ret;
1349}
1350
1351/**
1352 * drm_mode_getconnector - get connector configuration
1353 * @inode: inode from the ioctl
1354 * @filp: file * from the ioctl
1355 * @cmd: cmd from ioctl
1356 * @arg: arg from ioctl
1357 *
1358 * LOCKING:
1359 * Caller? (FIXME)
1360 *
1361 * Construct a connector configuration structure to return to the user.
1362 *
1363 * Called by the user via ioctl.
1364 *
1365 * RETURNS:
1366 * Zero on success, errno on failure.
1367 */
1368int drm_mode_getconnector(struct drm_device *dev, void *data,
1369 struct drm_file *file_priv)
1370{
1371 struct drm_mode_get_connector *out_resp = data;
1372 struct drm_mode_object *obj;
1373 struct drm_connector *connector;
1374 struct drm_display_mode *mode;
1375 int mode_count = 0;
1376 int props_count = 0;
1377 int encoders_count = 0;
1378 int ret = 0;
1379 int copied = 0;
1380 int i;
1381 struct drm_mode_modeinfo u_mode;
1382 struct drm_mode_modeinfo __user *mode_ptr;
1383 uint32_t __user *prop_ptr;
1384 uint64_t __user *prop_values;
1385 uint32_t __user *encoder_ptr;
1386
Dave Airliefb3b06c2011-02-08 13:55:21 +10001387 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1388 return -EINVAL;
1389
Dave Airlief453ba02008-11-07 14:05:41 -08001390 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1391
Jerome Glisse94401062010-07-15 15:43:25 -04001392 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001393
1394 mutex_lock(&dev->mode_config.mutex);
1395
1396 obj = drm_mode_object_find(dev, out_resp->connector_id,
1397 DRM_MODE_OBJECT_CONNECTOR);
1398 if (!obj) {
1399 ret = -EINVAL;
1400 goto out;
1401 }
1402 connector = obj_to_connector(obj);
1403
1404 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1405 if (connector->property_ids[i] != 0) {
1406 props_count++;
1407 }
1408 }
1409
1410 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1411 if (connector->encoder_ids[i] != 0) {
1412 encoders_count++;
1413 }
1414 }
1415
1416 if (out_resp->count_modes == 0) {
1417 connector->funcs->fill_modes(connector,
1418 dev->mode_config.max_width,
1419 dev->mode_config.max_height);
1420 }
1421
1422 /* delayed so we get modes regardless of pre-fill_modes state */
1423 list_for_each_entry(mode, &connector->modes, head)
1424 mode_count++;
1425
1426 out_resp->connector_id = connector->base.id;
1427 out_resp->connector_type = connector->connector_type;
1428 out_resp->connector_type_id = connector->connector_type_id;
1429 out_resp->mm_width = connector->display_info.width_mm;
1430 out_resp->mm_height = connector->display_info.height_mm;
1431 out_resp->subpixel = connector->display_info.subpixel_order;
1432 out_resp->connection = connector->status;
1433 if (connector->encoder)
1434 out_resp->encoder_id = connector->encoder->base.id;
1435 else
1436 out_resp->encoder_id = 0;
1437
1438 /*
1439 * This ioctl is called twice, once to determine how much space is
1440 * needed, and the 2nd time to fill it.
1441 */
1442 if ((out_resp->count_modes >= mode_count) && mode_count) {
1443 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001444 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001445 list_for_each_entry(mode, &connector->modes, head) {
1446 drm_crtc_convert_to_umode(&u_mode, mode);
1447 if (copy_to_user(mode_ptr + copied,
1448 &u_mode, sizeof(u_mode))) {
1449 ret = -EFAULT;
1450 goto out;
1451 }
1452 copied++;
1453 }
1454 }
1455 out_resp->count_modes = mode_count;
1456
1457 if ((out_resp->count_props >= props_count) && props_count) {
1458 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001459 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1460 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001461 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1462 if (connector->property_ids[i] != 0) {
1463 if (put_user(connector->property_ids[i],
1464 prop_ptr + copied)) {
1465 ret = -EFAULT;
1466 goto out;
1467 }
1468
1469 if (put_user(connector->property_values[i],
1470 prop_values + copied)) {
1471 ret = -EFAULT;
1472 goto out;
1473 }
1474 copied++;
1475 }
1476 }
1477 }
1478 out_resp->count_props = props_count;
1479
1480 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1481 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001482 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001483 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1484 if (connector->encoder_ids[i] != 0) {
1485 if (put_user(connector->encoder_ids[i],
1486 encoder_ptr + copied)) {
1487 ret = -EFAULT;
1488 goto out;
1489 }
1490 copied++;
1491 }
1492 }
1493 }
1494 out_resp->count_encoders = encoders_count;
1495
1496out:
1497 mutex_unlock(&dev->mode_config.mutex);
1498 return ret;
1499}
1500
1501int drm_mode_getencoder(struct drm_device *dev, void *data,
1502 struct drm_file *file_priv)
1503{
1504 struct drm_mode_get_encoder *enc_resp = data;
1505 struct drm_mode_object *obj;
1506 struct drm_encoder *encoder;
1507 int ret = 0;
1508
Dave Airliefb3b06c2011-02-08 13:55:21 +10001509 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1510 return -EINVAL;
1511
Dave Airlief453ba02008-11-07 14:05:41 -08001512 mutex_lock(&dev->mode_config.mutex);
1513 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1514 DRM_MODE_OBJECT_ENCODER);
1515 if (!obj) {
1516 ret = -EINVAL;
1517 goto out;
1518 }
1519 encoder = obj_to_encoder(obj);
1520
1521 if (encoder->crtc)
1522 enc_resp->crtc_id = encoder->crtc->base.id;
1523 else
1524 enc_resp->crtc_id = 0;
1525 enc_resp->encoder_type = encoder->encoder_type;
1526 enc_resp->encoder_id = encoder->base.id;
1527 enc_resp->possible_crtcs = encoder->possible_crtcs;
1528 enc_resp->possible_clones = encoder->possible_clones;
1529
1530out:
1531 mutex_unlock(&dev->mode_config.mutex);
1532 return ret;
1533}
1534
1535/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001536 * drm_mode_getplane_res - get plane info
1537 * @dev: DRM device
1538 * @data: ioctl data
1539 * @file_priv: DRM file info
1540 *
1541 * Return an plane count and set of IDs.
1542 */
1543int drm_mode_getplane_res(struct drm_device *dev, void *data,
1544 struct drm_file *file_priv)
1545{
1546 struct drm_mode_get_plane_res *plane_resp = data;
1547 struct drm_mode_config *config;
1548 struct drm_plane *plane;
1549 uint32_t __user *plane_ptr;
1550 int copied = 0, ret = 0;
1551
1552 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1553 return -EINVAL;
1554
1555 mutex_lock(&dev->mode_config.mutex);
1556 config = &dev->mode_config;
1557
1558 /*
1559 * This ioctl is called twice, once to determine how much space is
1560 * needed, and the 2nd time to fill it.
1561 */
1562 if (config->num_plane &&
1563 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001564 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001565
1566 list_for_each_entry(plane, &config->plane_list, head) {
1567 if (put_user(plane->base.id, plane_ptr + copied)) {
1568 ret = -EFAULT;
1569 goto out;
1570 }
1571 copied++;
1572 }
1573 }
1574 plane_resp->count_planes = config->num_plane;
1575
1576out:
1577 mutex_unlock(&dev->mode_config.mutex);
1578 return ret;
1579}
1580
1581/**
1582 * drm_mode_getplane - get plane info
1583 * @dev: DRM device
1584 * @data: ioctl data
1585 * @file_priv: DRM file info
1586 *
1587 * Return plane info, including formats supported, gamma size, any
1588 * current fb, etc.
1589 */
1590int drm_mode_getplane(struct drm_device *dev, void *data,
1591 struct drm_file *file_priv)
1592{
1593 struct drm_mode_get_plane *plane_resp = data;
1594 struct drm_mode_object *obj;
1595 struct drm_plane *plane;
1596 uint32_t __user *format_ptr;
1597 int ret = 0;
1598
1599 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1600 return -EINVAL;
1601
1602 mutex_lock(&dev->mode_config.mutex);
1603 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1604 DRM_MODE_OBJECT_PLANE);
1605 if (!obj) {
1606 ret = -ENOENT;
1607 goto out;
1608 }
1609 plane = obj_to_plane(obj);
1610
1611 if (plane->crtc)
1612 plane_resp->crtc_id = plane->crtc->base.id;
1613 else
1614 plane_resp->crtc_id = 0;
1615
1616 if (plane->fb)
1617 plane_resp->fb_id = plane->fb->base.id;
1618 else
1619 plane_resp->fb_id = 0;
1620
1621 plane_resp->plane_id = plane->base.id;
1622 plane_resp->possible_crtcs = plane->possible_crtcs;
1623 plane_resp->gamma_size = plane->gamma_size;
1624
1625 /*
1626 * This ioctl is called twice, once to determine how much space is
1627 * needed, and the 2nd time to fill it.
1628 */
1629 if (plane->format_count &&
1630 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001631 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001632 if (copy_to_user(format_ptr,
1633 plane->format_types,
1634 sizeof(uint32_t) * plane->format_count)) {
1635 ret = -EFAULT;
1636 goto out;
1637 }
1638 }
1639 plane_resp->count_format_types = plane->format_count;
1640
1641out:
1642 mutex_unlock(&dev->mode_config.mutex);
1643 return ret;
1644}
1645
1646/**
1647 * drm_mode_setplane - set up or tear down an plane
1648 * @dev: DRM device
1649 * @data: ioctl data*
1650 * @file_prive: DRM file info
1651 *
1652 * Set plane info, including placement, fb, scaling, and other factors.
1653 * Or pass a NULL fb to disable.
1654 */
1655int drm_mode_setplane(struct drm_device *dev, void *data,
1656 struct drm_file *file_priv)
1657{
1658 struct drm_mode_set_plane *plane_req = data;
1659 struct drm_mode_object *obj;
1660 struct drm_plane *plane;
1661 struct drm_crtc *crtc;
1662 struct drm_framebuffer *fb;
1663 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001664 unsigned int fb_width, fb_height;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001665
1666 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1667 return -EINVAL;
1668
1669 mutex_lock(&dev->mode_config.mutex);
1670
1671 /*
1672 * First, find the plane, crtc, and fb objects. If not available,
1673 * we don't bother to call the driver.
1674 */
1675 obj = drm_mode_object_find(dev, plane_req->plane_id,
1676 DRM_MODE_OBJECT_PLANE);
1677 if (!obj) {
1678 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1679 plane_req->plane_id);
1680 ret = -ENOENT;
1681 goto out;
1682 }
1683 plane = obj_to_plane(obj);
1684
1685 /* No fb means shut it down */
1686 if (!plane_req->fb_id) {
1687 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001688 plane->crtc = NULL;
1689 plane->fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001690 goto out;
1691 }
1692
1693 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1694 DRM_MODE_OBJECT_CRTC);
1695 if (!obj) {
1696 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1697 plane_req->crtc_id);
1698 ret = -ENOENT;
1699 goto out;
1700 }
1701 crtc = obj_to_crtc(obj);
1702
1703 obj = drm_mode_object_find(dev, plane_req->fb_id,
1704 DRM_MODE_OBJECT_FB);
1705 if (!obj) {
1706 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1707 plane_req->fb_id);
1708 ret = -ENOENT;
1709 goto out;
1710 }
1711 fb = obj_to_fb(obj);
1712
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001713 fb_width = fb->width << 16;
1714 fb_height = fb->height << 16;
1715
1716 /* Make sure source coordinates are inside the fb. */
1717 if (plane_req->src_w > fb_width ||
1718 plane_req->src_x > fb_width - plane_req->src_w ||
1719 plane_req->src_h > fb_height ||
1720 plane_req->src_y > fb_height - plane_req->src_h) {
1721 DRM_DEBUG_KMS("Invalid source coordinates "
1722 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1723 plane_req->src_w >> 16,
1724 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1725 plane_req->src_h >> 16,
1726 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1727 plane_req->src_x >> 16,
1728 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1729 plane_req->src_y >> 16,
1730 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1731 ret = -ENOSPC;
1732 goto out;
1733 }
1734
Ville Syrjälä687a0402011-12-20 00:06:45 +02001735 /* Give drivers some help against integer overflows */
1736 if (plane_req->crtc_w > INT_MAX ||
1737 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1738 plane_req->crtc_h > INT_MAX ||
1739 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1740 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1741 plane_req->crtc_w, plane_req->crtc_h,
1742 plane_req->crtc_x, plane_req->crtc_y);
1743 ret = -ERANGE;
1744 goto out;
1745 }
1746
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001747 ret = plane->funcs->update_plane(plane, crtc, fb,
1748 plane_req->crtc_x, plane_req->crtc_y,
1749 plane_req->crtc_w, plane_req->crtc_h,
1750 plane_req->src_x, plane_req->src_y,
1751 plane_req->src_w, plane_req->src_h);
1752 if (!ret) {
1753 plane->crtc = crtc;
1754 plane->fb = fb;
1755 }
1756
1757out:
1758 mutex_unlock(&dev->mode_config.mutex);
1759
1760 return ret;
1761}
1762
1763/**
Dave Airlief453ba02008-11-07 14:05:41 -08001764 * drm_mode_setcrtc - set CRTC configuration
1765 * @inode: inode from the ioctl
1766 * @filp: file * from the ioctl
1767 * @cmd: cmd from ioctl
1768 * @arg: arg from ioctl
1769 *
1770 * LOCKING:
1771 * Caller? (FIXME)
1772 *
1773 * Build a new CRTC configuration based on user request.
1774 *
1775 * Called by the user via ioctl.
1776 *
1777 * RETURNS:
1778 * Zero on success, errno on failure.
1779 */
1780int drm_mode_setcrtc(struct drm_device *dev, void *data,
1781 struct drm_file *file_priv)
1782{
1783 struct drm_mode_config *config = &dev->mode_config;
1784 struct drm_mode_crtc *crtc_req = data;
1785 struct drm_mode_object *obj;
1786 struct drm_crtc *crtc, *crtcfb;
1787 struct drm_connector **connector_set = NULL, *connector;
1788 struct drm_framebuffer *fb = NULL;
1789 struct drm_display_mode *mode = NULL;
1790 struct drm_mode_set set;
1791 uint32_t __user *set_connectors_ptr;
1792 int ret = 0;
1793 int i;
1794
Dave Airliefb3b06c2011-02-08 13:55:21 +10001795 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1796 return -EINVAL;
1797
Dave Airlief453ba02008-11-07 14:05:41 -08001798 mutex_lock(&dev->mode_config.mutex);
1799 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1800 DRM_MODE_OBJECT_CRTC);
1801 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001802 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001803 ret = -EINVAL;
1804 goto out;
1805 }
1806 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001807 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001808
1809 if (crtc_req->mode_valid) {
1810 /* If we have a mode we need a framebuffer. */
1811 /* If we pass -1, set the mode with the currently bound fb */
1812 if (crtc_req->fb_id == -1) {
1813 list_for_each_entry(crtcfb,
1814 &dev->mode_config.crtc_list, head) {
1815 if (crtcfb == crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001816 DRM_DEBUG_KMS("Using current fb for "
1817 "setmode\n");
Dave Airlief453ba02008-11-07 14:05:41 -08001818 fb = crtc->fb;
1819 }
1820 }
1821 } else {
1822 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1823 DRM_MODE_OBJECT_FB);
1824 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001825 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1826 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001827 ret = -EINVAL;
1828 goto out;
1829 }
1830 fb = obj_to_fb(obj);
1831 }
1832
1833 mode = drm_mode_create(dev);
1834 drm_crtc_convert_umode(mode, &crtc_req->mode);
1835 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1836 }
1837
1838 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001839 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08001840 ret = -EINVAL;
1841 goto out;
1842 }
1843
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01001844 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001845 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08001846 crtc_req->count_connectors);
1847 ret = -EINVAL;
1848 goto out;
1849 }
1850
1851 if (crtc_req->count_connectors > 0) {
1852 u32 out_id;
1853
1854 /* Avoid unbounded kernel memory allocation */
1855 if (crtc_req->count_connectors > config->num_connector) {
1856 ret = -EINVAL;
1857 goto out;
1858 }
1859
1860 connector_set = kmalloc(crtc_req->count_connectors *
1861 sizeof(struct drm_connector *),
1862 GFP_KERNEL);
1863 if (!connector_set) {
1864 ret = -ENOMEM;
1865 goto out;
1866 }
1867
1868 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001869 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001870 if (get_user(out_id, &set_connectors_ptr[i])) {
1871 ret = -EFAULT;
1872 goto out;
1873 }
1874
1875 obj = drm_mode_object_find(dev, out_id,
1876 DRM_MODE_OBJECT_CONNECTOR);
1877 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001878 DRM_DEBUG_KMS("Connector id %d unknown\n",
1879 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001880 ret = -EINVAL;
1881 goto out;
1882 }
1883 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001884 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1885 connector->base.id,
1886 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001887
1888 connector_set[i] = connector;
1889 }
1890 }
1891
1892 set.crtc = crtc;
1893 set.x = crtc_req->x;
1894 set.y = crtc_req->y;
1895 set.mode = mode;
1896 set.connectors = connector_set;
1897 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10001898 set.fb = fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001899 ret = crtc->funcs->set_config(&set);
1900
1901out:
1902 kfree(connector_set);
1903 mutex_unlock(&dev->mode_config.mutex);
1904 return ret;
1905}
1906
1907int drm_mode_cursor_ioctl(struct drm_device *dev,
1908 void *data, struct drm_file *file_priv)
1909{
1910 struct drm_mode_cursor *req = data;
1911 struct drm_mode_object *obj;
1912 struct drm_crtc *crtc;
1913 int ret = 0;
1914
Dave Airliefb3b06c2011-02-08 13:55:21 +10001915 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1916 return -EINVAL;
1917
Jesse Barnesacb4b992011-11-07 12:03:23 -08001918 if (!req->flags)
Dave Airlief453ba02008-11-07 14:05:41 -08001919 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08001920
1921 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10001922 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08001923 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001924 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001925 ret = -EINVAL;
1926 goto out;
1927 }
1928 crtc = obj_to_crtc(obj);
1929
1930 if (req->flags & DRM_MODE_CURSOR_BO) {
1931 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08001932 ret = -ENXIO;
1933 goto out;
1934 }
1935 /* Turns off the cursor if handle is 0 */
1936 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1937 req->width, req->height);
1938 }
1939
1940 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1941 if (crtc->funcs->cursor_move) {
1942 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1943 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08001944 ret = -EFAULT;
1945 goto out;
1946 }
1947 }
1948out:
1949 mutex_unlock(&dev->mode_config.mutex);
1950 return ret;
1951}
1952
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001953/* Original addfb only supported RGB formats, so figure out which one */
1954uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
1955{
1956 uint32_t fmt;
1957
1958 switch (bpp) {
1959 case 8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02001960 fmt = DRM_FORMAT_RGB332;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001961 break;
1962 case 16:
1963 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02001964 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001965 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02001966 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001967 break;
1968 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02001969 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001970 break;
1971 case 32:
1972 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02001973 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001974 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02001975 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001976 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02001977 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001978 break;
1979 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02001980 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
1981 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001982 break;
1983 }
1984
1985 return fmt;
1986}
1987EXPORT_SYMBOL(drm_mode_legacy_fb_format);
1988
Dave Airlief453ba02008-11-07 14:05:41 -08001989/**
1990 * drm_mode_addfb - add an FB to the graphics configuration
1991 * @inode: inode from the ioctl
1992 * @filp: file * from the ioctl
1993 * @cmd: cmd from ioctl
1994 * @arg: arg from ioctl
1995 *
1996 * LOCKING:
1997 * Takes mode config lock.
1998 *
1999 * Add a new FB to the specified CRTC, given a user request.
2000 *
2001 * Called by the user via ioctl.
2002 *
2003 * RETURNS:
2004 * Zero on success, errno on failure.
2005 */
2006int drm_mode_addfb(struct drm_device *dev,
2007 void *data, struct drm_file *file_priv)
2008{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002009 struct drm_mode_fb_cmd *or = data;
2010 struct drm_mode_fb_cmd2 r = {};
2011 struct drm_mode_config *config = &dev->mode_config;
2012 struct drm_framebuffer *fb;
2013 int ret = 0;
2014
2015 /* Use new struct with format internally */
2016 r.fb_id = or->fb_id;
2017 r.width = or->width;
2018 r.height = or->height;
2019 r.pitches[0] = or->pitch;
2020 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2021 r.handles[0] = or->handle;
2022
2023 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2024 return -EINVAL;
2025
Jesse Barnesacb4b992011-11-07 12:03:23 -08002026 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002027 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002028
2029 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002030 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002031
2032 mutex_lock(&dev->mode_config.mutex);
2033
2034 /* TODO check buffer is sufficiently large */
2035 /* TODO setup destructor callback */
2036
2037 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2038 if (IS_ERR(fb)) {
2039 DRM_ERROR("could not create framebuffer\n");
2040 ret = PTR_ERR(fb);
2041 goto out;
2042 }
2043
2044 or->fb_id = fb->base.id;
2045 list_add(&fb->filp_head, &file_priv->fbs);
2046 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2047
2048out:
2049 mutex_unlock(&dev->mode_config.mutex);
2050 return ret;
2051}
2052
2053/**
2054 * drm_mode_addfb2 - add an FB to the graphics configuration
2055 * @inode: inode from the ioctl
2056 * @filp: file * from the ioctl
2057 * @cmd: cmd from ioctl
2058 * @arg: arg from ioctl
2059 *
2060 * LOCKING:
2061 * Takes mode config lock.
2062 *
2063 * Add a new FB to the specified CRTC, given a user request with format.
2064 *
2065 * Called by the user via ioctl.
2066 *
2067 * RETURNS:
2068 * Zero on success, errno on failure.
2069 */
2070int drm_mode_addfb2(struct drm_device *dev,
2071 void *data, struct drm_file *file_priv)
2072{
2073 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002074 struct drm_mode_config *config = &dev->mode_config;
2075 struct drm_framebuffer *fb;
2076 int ret = 0;
2077
Dave Airliefb3b06c2011-02-08 13:55:21 +10002078 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2079 return -EINVAL;
2080
Dave Airlief453ba02008-11-07 14:05:41 -08002081 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002082 DRM_ERROR("bad framebuffer width %d, should be >= %d && <= %d\n",
2083 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002084 return -EINVAL;
2085 }
2086 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002087 DRM_ERROR("bad framebuffer height %d, should be >= %d && <= %d\n",
2088 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002089 return -EINVAL;
2090 }
2091
2092 mutex_lock(&dev->mode_config.mutex);
2093
Dave Airlief453ba02008-11-07 14:05:41 -08002094 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002095 if (IS_ERR(fb)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002096 DRM_ERROR("could not create framebuffer\n");
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002097 ret = PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002098 goto out;
2099 }
2100
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002101 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002102 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002103 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002104
2105out:
2106 mutex_unlock(&dev->mode_config.mutex);
2107 return ret;
2108}
2109
2110/**
2111 * drm_mode_rmfb - remove an FB from the configuration
2112 * @inode: inode from the ioctl
2113 * @filp: file * from the ioctl
2114 * @cmd: cmd from ioctl
2115 * @arg: arg from ioctl
2116 *
2117 * LOCKING:
2118 * Takes mode config lock.
2119 *
2120 * Remove the FB specified by the user.
2121 *
2122 * Called by the user via ioctl.
2123 *
2124 * RETURNS:
2125 * Zero on success, errno on failure.
2126 */
2127int drm_mode_rmfb(struct drm_device *dev,
2128 void *data, struct drm_file *file_priv)
2129{
2130 struct drm_mode_object *obj;
2131 struct drm_framebuffer *fb = NULL;
2132 struct drm_framebuffer *fbl = NULL;
2133 uint32_t *id = data;
2134 int ret = 0;
2135 int found = 0;
2136
Dave Airliefb3b06c2011-02-08 13:55:21 +10002137 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2138 return -EINVAL;
2139
Dave Airlief453ba02008-11-07 14:05:41 -08002140 mutex_lock(&dev->mode_config.mutex);
2141 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002142 /* TODO check that we really get a framebuffer back. */
Dave Airlief453ba02008-11-07 14:05:41 -08002143 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002144 ret = -EINVAL;
2145 goto out;
2146 }
2147 fb = obj_to_fb(obj);
2148
2149 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2150 if (fb == fbl)
2151 found = 1;
2152
2153 if (!found) {
Dave Airlief453ba02008-11-07 14:05:41 -08002154 ret = -EINVAL;
2155 goto out;
2156 }
2157
2158 /* TODO release all crtc connected to the framebuffer */
2159 /* TODO unhock the destructor from the buffer object */
2160
2161 list_del(&fb->filp_head);
2162 fb->funcs->destroy(fb);
2163
2164out:
2165 mutex_unlock(&dev->mode_config.mutex);
2166 return ret;
2167}
2168
2169/**
2170 * drm_mode_getfb - get FB info
2171 * @inode: inode from the ioctl
2172 * @filp: file * from the ioctl
2173 * @cmd: cmd from ioctl
2174 * @arg: arg from ioctl
2175 *
2176 * LOCKING:
2177 * Caller? (FIXME)
2178 *
2179 * Lookup the FB given its ID and return info about it.
2180 *
2181 * Called by the user via ioctl.
2182 *
2183 * RETURNS:
2184 * Zero on success, errno on failure.
2185 */
2186int drm_mode_getfb(struct drm_device *dev,
2187 void *data, struct drm_file *file_priv)
2188{
2189 struct drm_mode_fb_cmd *r = data;
2190 struct drm_mode_object *obj;
2191 struct drm_framebuffer *fb;
2192 int ret = 0;
2193
Dave Airliefb3b06c2011-02-08 13:55:21 +10002194 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2195 return -EINVAL;
2196
Dave Airlief453ba02008-11-07 14:05:41 -08002197 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002198 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
Dave Airlief453ba02008-11-07 14:05:41 -08002199 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002200 ret = -EINVAL;
2201 goto out;
2202 }
2203 fb = obj_to_fb(obj);
2204
2205 r->height = fb->height;
2206 r->width = fb->width;
2207 r->depth = fb->depth;
2208 r->bpp = fb->bits_per_pixel;
2209 r->pitch = fb->pitch;
2210 fb->funcs->create_handle(fb, file_priv, &r->handle);
2211
2212out:
2213 mutex_unlock(&dev->mode_config.mutex);
2214 return ret;
2215}
2216
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002217int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2218 void *data, struct drm_file *file_priv)
2219{
2220 struct drm_clip_rect __user *clips_ptr;
2221 struct drm_clip_rect *clips = NULL;
2222 struct drm_mode_fb_dirty_cmd *r = data;
2223 struct drm_mode_object *obj;
2224 struct drm_framebuffer *fb;
2225 unsigned flags;
2226 int num_clips;
2227 int ret = 0;
2228
Dave Airliefb3b06c2011-02-08 13:55:21 +10002229 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2230 return -EINVAL;
2231
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002232 mutex_lock(&dev->mode_config.mutex);
2233 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2234 if (!obj) {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002235 ret = -EINVAL;
2236 goto out_err1;
2237 }
2238 fb = obj_to_fb(obj);
2239
2240 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002241 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002242
2243 if (!num_clips != !clips_ptr) {
2244 ret = -EINVAL;
2245 goto out_err1;
2246 }
2247
2248 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2249
2250 /* If userspace annotates copy, clips must come in pairs */
2251 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2252 ret = -EINVAL;
2253 goto out_err1;
2254 }
2255
2256 if (num_clips && clips_ptr) {
2257 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2258 if (!clips) {
2259 ret = -ENOMEM;
2260 goto out_err1;
2261 }
2262
2263 ret = copy_from_user(clips, clips_ptr,
2264 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002265 if (ret) {
2266 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002267 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002268 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002269 }
2270
2271 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002272 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2273 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002274 } else {
2275 ret = -ENOSYS;
2276 goto out_err2;
2277 }
2278
2279out_err2:
2280 kfree(clips);
2281out_err1:
2282 mutex_unlock(&dev->mode_config.mutex);
2283 return ret;
2284}
2285
2286
Dave Airlief453ba02008-11-07 14:05:41 -08002287/**
2288 * drm_fb_release - remove and free the FBs on this file
2289 * @filp: file * from the ioctl
2290 *
2291 * LOCKING:
2292 * Takes mode config lock.
2293 *
2294 * Destroy all the FBs associated with @filp.
2295 *
2296 * Called by the user via ioctl.
2297 *
2298 * RETURNS:
2299 * Zero on success, errno on failure.
2300 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002301void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002302{
Dave Airlief453ba02008-11-07 14:05:41 -08002303 struct drm_device *dev = priv->minor->dev;
2304 struct drm_framebuffer *fb, *tfb;
2305
2306 mutex_lock(&dev->mode_config.mutex);
2307 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2308 list_del(&fb->filp_head);
2309 fb->funcs->destroy(fb);
2310 }
2311 mutex_unlock(&dev->mode_config.mutex);
2312}
2313
2314/**
2315 * drm_mode_attachmode - add a mode to the user mode list
2316 * @dev: DRM device
2317 * @connector: connector to add the mode to
2318 * @mode: mode to add
2319 *
2320 * Add @mode to @connector's user mode list.
2321 */
2322static int drm_mode_attachmode(struct drm_device *dev,
2323 struct drm_connector *connector,
2324 struct drm_display_mode *mode)
2325{
2326 int ret = 0;
2327
2328 list_add_tail(&mode->head, &connector->user_modes);
2329 return ret;
2330}
2331
2332int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
2333 struct drm_display_mode *mode)
2334{
2335 struct drm_connector *connector;
2336 int ret = 0;
2337 struct drm_display_mode *dup_mode;
2338 int need_dup = 0;
2339 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2340 if (!connector->encoder)
2341 break;
2342 if (connector->encoder->crtc == crtc) {
2343 if (need_dup)
2344 dup_mode = drm_mode_duplicate(dev, mode);
2345 else
2346 dup_mode = mode;
2347 ret = drm_mode_attachmode(dev, connector, dup_mode);
2348 if (ret)
2349 return ret;
2350 need_dup = 1;
2351 }
2352 }
2353 return 0;
2354}
2355EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2356
2357static int drm_mode_detachmode(struct drm_device *dev,
2358 struct drm_connector *connector,
2359 struct drm_display_mode *mode)
2360{
2361 int found = 0;
2362 int ret = 0;
2363 struct drm_display_mode *match_mode, *t;
2364
2365 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2366 if (drm_mode_equal(match_mode, mode)) {
2367 list_del(&match_mode->head);
2368 drm_mode_destroy(dev, match_mode);
2369 found = 1;
2370 break;
2371 }
2372 }
2373
2374 if (!found)
2375 ret = -EINVAL;
2376
2377 return ret;
2378}
2379
2380int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2381{
2382 struct drm_connector *connector;
2383
2384 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2385 drm_mode_detachmode(dev, connector, mode);
2386 }
2387 return 0;
2388}
2389EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2390
2391/**
2392 * drm_fb_attachmode - Attach a user mode to an connector
2393 * @inode: inode from the ioctl
2394 * @filp: file * from the ioctl
2395 * @cmd: cmd from ioctl
2396 * @arg: arg from ioctl
2397 *
2398 * This attaches a user specified mode to an connector.
2399 * Called by the user via ioctl.
2400 *
2401 * RETURNS:
2402 * Zero on success, errno on failure.
2403 */
2404int drm_mode_attachmode_ioctl(struct drm_device *dev,
2405 void *data, struct drm_file *file_priv)
2406{
2407 struct drm_mode_mode_cmd *mode_cmd = data;
2408 struct drm_connector *connector;
2409 struct drm_display_mode *mode;
2410 struct drm_mode_object *obj;
2411 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2412 int ret = 0;
2413
Dave Airliefb3b06c2011-02-08 13:55:21 +10002414 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2415 return -EINVAL;
2416
Dave Airlief453ba02008-11-07 14:05:41 -08002417 mutex_lock(&dev->mode_config.mutex);
2418
2419 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2420 if (!obj) {
2421 ret = -EINVAL;
2422 goto out;
2423 }
2424 connector = obj_to_connector(obj);
2425
2426 mode = drm_mode_create(dev);
2427 if (!mode) {
2428 ret = -ENOMEM;
2429 goto out;
2430 }
2431
2432 drm_crtc_convert_umode(mode, umode);
2433
2434 ret = drm_mode_attachmode(dev, connector, mode);
2435out:
2436 mutex_unlock(&dev->mode_config.mutex);
2437 return ret;
2438}
2439
2440
2441/**
2442 * drm_fb_detachmode - Detach a user specified mode from an connector
2443 * @inode: inode from the ioctl
2444 * @filp: file * from the ioctl
2445 * @cmd: cmd from ioctl
2446 * @arg: arg from ioctl
2447 *
2448 * Called by the user via ioctl.
2449 *
2450 * RETURNS:
2451 * Zero on success, errno on failure.
2452 */
2453int drm_mode_detachmode_ioctl(struct drm_device *dev,
2454 void *data, struct drm_file *file_priv)
2455{
2456 struct drm_mode_object *obj;
2457 struct drm_mode_mode_cmd *mode_cmd = data;
2458 struct drm_connector *connector;
2459 struct drm_display_mode mode;
2460 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2461 int ret = 0;
2462
Dave Airliefb3b06c2011-02-08 13:55:21 +10002463 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2464 return -EINVAL;
2465
Dave Airlief453ba02008-11-07 14:05:41 -08002466 mutex_lock(&dev->mode_config.mutex);
2467
2468 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2469 if (!obj) {
2470 ret = -EINVAL;
2471 goto out;
2472 }
2473 connector = obj_to_connector(obj);
2474
2475 drm_crtc_convert_umode(&mode, umode);
2476 ret = drm_mode_detachmode(dev, connector, &mode);
2477out:
2478 mutex_unlock(&dev->mode_config.mutex);
2479 return ret;
2480}
2481
2482struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2483 const char *name, int num_values)
2484{
2485 struct drm_property *property = NULL;
2486
2487 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2488 if (!property)
2489 return NULL;
2490
2491 if (num_values) {
2492 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2493 if (!property->values)
2494 goto fail;
2495 }
2496
2497 drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2498 property->flags = flags;
2499 property->num_values = num_values;
2500 INIT_LIST_HEAD(&property->enum_blob_list);
2501
2502 if (name)
2503 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2504
2505 list_add_tail(&property->head, &dev->mode_config.property_list);
2506 return property;
2507fail:
2508 kfree(property);
2509 return NULL;
2510}
2511EXPORT_SYMBOL(drm_property_create);
2512
2513int drm_property_add_enum(struct drm_property *property, int index,
2514 uint64_t value, const char *name)
2515{
2516 struct drm_property_enum *prop_enum;
2517
2518 if (!(property->flags & DRM_MODE_PROP_ENUM))
2519 return -EINVAL;
2520
2521 if (!list_empty(&property->enum_blob_list)) {
2522 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2523 if (prop_enum->value == value) {
2524 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2525 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2526 return 0;
2527 }
2528 }
2529 }
2530
2531 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2532 if (!prop_enum)
2533 return -ENOMEM;
2534
2535 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2536 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2537 prop_enum->value = value;
2538
2539 property->values[index] = value;
2540 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2541 return 0;
2542}
2543EXPORT_SYMBOL(drm_property_add_enum);
2544
2545void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2546{
2547 struct drm_property_enum *prop_enum, *pt;
2548
2549 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2550 list_del(&prop_enum->head);
2551 kfree(prop_enum);
2552 }
2553
2554 if (property->num_values)
2555 kfree(property->values);
2556 drm_mode_object_put(dev, &property->base);
2557 list_del(&property->head);
2558 kfree(property);
2559}
2560EXPORT_SYMBOL(drm_property_destroy);
2561
2562int drm_connector_attach_property(struct drm_connector *connector,
2563 struct drm_property *property, uint64_t init_val)
2564{
2565 int i;
2566
2567 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2568 if (connector->property_ids[i] == 0) {
2569 connector->property_ids[i] = property->base.id;
2570 connector->property_values[i] = init_val;
2571 break;
2572 }
2573 }
2574
2575 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2576 return -EINVAL;
2577 return 0;
2578}
2579EXPORT_SYMBOL(drm_connector_attach_property);
2580
2581int drm_connector_property_set_value(struct drm_connector *connector,
2582 struct drm_property *property, uint64_t value)
2583{
2584 int i;
2585
2586 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2587 if (connector->property_ids[i] == property->base.id) {
2588 connector->property_values[i] = value;
2589 break;
2590 }
2591 }
2592
2593 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2594 return -EINVAL;
2595 return 0;
2596}
2597EXPORT_SYMBOL(drm_connector_property_set_value);
2598
2599int drm_connector_property_get_value(struct drm_connector *connector,
2600 struct drm_property *property, uint64_t *val)
2601{
2602 int i;
2603
2604 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2605 if (connector->property_ids[i] == property->base.id) {
2606 *val = connector->property_values[i];
2607 break;
2608 }
2609 }
2610
2611 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2612 return -EINVAL;
2613 return 0;
2614}
2615EXPORT_SYMBOL(drm_connector_property_get_value);
2616
2617int drm_mode_getproperty_ioctl(struct drm_device *dev,
2618 void *data, struct drm_file *file_priv)
2619{
2620 struct drm_mode_object *obj;
2621 struct drm_mode_get_property *out_resp = data;
2622 struct drm_property *property;
2623 int enum_count = 0;
2624 int blob_count = 0;
2625 int value_count = 0;
2626 int ret = 0, i;
2627 int copied;
2628 struct drm_property_enum *prop_enum;
2629 struct drm_mode_property_enum __user *enum_ptr;
2630 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002631 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002632 uint64_t __user *values_ptr;
2633 uint32_t __user *blob_length_ptr;
2634
Dave Airliefb3b06c2011-02-08 13:55:21 +10002635 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2636 return -EINVAL;
2637
Dave Airlief453ba02008-11-07 14:05:41 -08002638 mutex_lock(&dev->mode_config.mutex);
2639 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2640 if (!obj) {
2641 ret = -EINVAL;
2642 goto done;
2643 }
2644 property = obj_to_property(obj);
2645
2646 if (property->flags & DRM_MODE_PROP_ENUM) {
2647 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2648 enum_count++;
2649 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2650 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2651 blob_count++;
2652 }
2653
2654 value_count = property->num_values;
2655
2656 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2657 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2658 out_resp->flags = property->flags;
2659
2660 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002661 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002662 for (i = 0; i < value_count; i++) {
2663 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2664 ret = -EFAULT;
2665 goto done;
2666 }
2667 }
2668 }
2669 out_resp->count_values = value_count;
2670
2671 if (property->flags & DRM_MODE_PROP_ENUM) {
2672 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2673 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002674 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002675 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2676
2677 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2678 ret = -EFAULT;
2679 goto done;
2680 }
2681
2682 if (copy_to_user(&enum_ptr[copied].name,
2683 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2684 ret = -EFAULT;
2685 goto done;
2686 }
2687 copied++;
2688 }
2689 }
2690 out_resp->count_enum_blobs = enum_count;
2691 }
2692
2693 if (property->flags & DRM_MODE_PROP_BLOB) {
2694 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2695 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002696 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
2697 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002698
2699 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2700 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2701 ret = -EFAULT;
2702 goto done;
2703 }
2704
2705 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2706 ret = -EFAULT;
2707 goto done;
2708 }
2709
2710 copied++;
2711 }
2712 }
2713 out_resp->count_enum_blobs = blob_count;
2714 }
2715done:
2716 mutex_unlock(&dev->mode_config.mutex);
2717 return ret;
2718}
2719
2720static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2721 void *data)
2722{
2723 struct drm_property_blob *blob;
2724
2725 if (!length || !data)
2726 return NULL;
2727
2728 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2729 if (!blob)
2730 return NULL;
2731
2732 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2733 blob->length = length;
2734
2735 memcpy(blob->data, data, length);
2736
2737 drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2738
2739 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2740 return blob;
2741}
2742
2743static void drm_property_destroy_blob(struct drm_device *dev,
2744 struct drm_property_blob *blob)
2745{
2746 drm_mode_object_put(dev, &blob->base);
2747 list_del(&blob->head);
2748 kfree(blob);
2749}
2750
2751int drm_mode_getblob_ioctl(struct drm_device *dev,
2752 void *data, struct drm_file *file_priv)
2753{
2754 struct drm_mode_object *obj;
2755 struct drm_mode_get_blob *out_resp = data;
2756 struct drm_property_blob *blob;
2757 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002758 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002759
Dave Airliefb3b06c2011-02-08 13:55:21 +10002760 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2761 return -EINVAL;
2762
Dave Airlief453ba02008-11-07 14:05:41 -08002763 mutex_lock(&dev->mode_config.mutex);
2764 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2765 if (!obj) {
2766 ret = -EINVAL;
2767 goto done;
2768 }
2769 blob = obj_to_blob(obj);
2770
2771 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002772 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08002773 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2774 ret = -EFAULT;
2775 goto done;
2776 }
2777 }
2778 out_resp->length = blob->length;
2779
2780done:
2781 mutex_unlock(&dev->mode_config.mutex);
2782 return ret;
2783}
2784
2785int drm_mode_connector_update_edid_property(struct drm_connector *connector,
2786 struct edid *edid)
2787{
2788 struct drm_device *dev = connector->dev;
Adam Jackson7466f4c2010-03-29 21:43:23 +00002789 int ret = 0, size;
Dave Airlief453ba02008-11-07 14:05:41 -08002790
2791 if (connector->edid_blob_ptr)
2792 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
2793
2794 /* Delete edid, when there is none. */
2795 if (!edid) {
2796 connector->edid_blob_ptr = NULL;
2797 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
2798 return ret;
2799 }
2800
Adam Jackson7466f4c2010-03-29 21:43:23 +00002801 size = EDID_LENGTH * (1 + edid->extensions);
2802 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
2803 size, edid);
Dave Airlief453ba02008-11-07 14:05:41 -08002804
2805 ret = drm_connector_property_set_value(connector,
2806 dev->mode_config.edid_property,
2807 connector->edid_blob_ptr->base.id);
2808
2809 return ret;
2810}
2811EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
2812
2813int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2814 void *data, struct drm_file *file_priv)
2815{
2816 struct drm_mode_connector_set_property *out_resp = data;
2817 struct drm_mode_object *obj;
2818 struct drm_property *property;
2819 struct drm_connector *connector;
2820 int ret = -EINVAL;
2821 int i;
2822
Dave Airliefb3b06c2011-02-08 13:55:21 +10002823 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2824 return -EINVAL;
2825
Dave Airlief453ba02008-11-07 14:05:41 -08002826 mutex_lock(&dev->mode_config.mutex);
2827
2828 obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2829 if (!obj) {
2830 goto out;
2831 }
2832 connector = obj_to_connector(obj);
2833
2834 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2835 if (connector->property_ids[i] == out_resp->prop_id)
2836 break;
2837 }
2838
2839 if (i == DRM_CONNECTOR_MAX_PROPERTY) {
2840 goto out;
2841 }
2842
2843 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2844 if (!obj) {
2845 goto out;
2846 }
2847 property = obj_to_property(obj);
2848
2849 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2850 goto out;
2851
2852 if (property->flags & DRM_MODE_PROP_RANGE) {
2853 if (out_resp->value < property->values[0])
2854 goto out;
2855
2856 if (out_resp->value > property->values[1])
2857 goto out;
2858 } else {
2859 int found = 0;
2860 for (i = 0; i < property->num_values; i++) {
2861 if (property->values[i] == out_resp->value) {
2862 found = 1;
2863 break;
2864 }
2865 }
2866 if (!found) {
2867 goto out;
2868 }
2869 }
2870
Keith Packardc9fb15f2009-05-30 20:42:28 -07002871 /* Do DPMS ourselves */
2872 if (property == connector->dev->mode_config.dpms_property) {
2873 if (connector->funcs->dpms)
2874 (*connector->funcs->dpms)(connector, (int) out_resp->value);
2875 ret = 0;
2876 } else if (connector->funcs->set_property)
Dave Airlief453ba02008-11-07 14:05:41 -08002877 ret = connector->funcs->set_property(connector, property, out_resp->value);
2878
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002879 /* store the property value if successful */
Dave Airlief453ba02008-11-07 14:05:41 -08002880 if (!ret)
2881 drm_connector_property_set_value(connector, property, out_resp->value);
2882out:
2883 mutex_unlock(&dev->mode_config.mutex);
2884 return ret;
2885}
2886
Dave Airlief453ba02008-11-07 14:05:41 -08002887int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2888 struct drm_encoder *encoder)
2889{
2890 int i;
2891
2892 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2893 if (connector->encoder_ids[i] == 0) {
2894 connector->encoder_ids[i] = encoder->base.id;
2895 return 0;
2896 }
2897 }
2898 return -ENOMEM;
2899}
2900EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
2901
2902void drm_mode_connector_detach_encoder(struct drm_connector *connector,
2903 struct drm_encoder *encoder)
2904{
2905 int i;
2906 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2907 if (connector->encoder_ids[i] == encoder->base.id) {
2908 connector->encoder_ids[i] = 0;
2909 if (connector->encoder == encoder)
2910 connector->encoder = NULL;
2911 break;
2912 }
2913 }
2914}
2915EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
2916
2917bool drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
2918 int gamma_size)
2919{
2920 crtc->gamma_size = gamma_size;
2921
2922 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
2923 if (!crtc->gamma_store) {
2924 crtc->gamma_size = 0;
2925 return false;
2926 }
2927
2928 return true;
2929}
2930EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
2931
2932int drm_mode_gamma_set_ioctl(struct drm_device *dev,
2933 void *data, struct drm_file *file_priv)
2934{
2935 struct drm_mode_crtc_lut *crtc_lut = data;
2936 struct drm_mode_object *obj;
2937 struct drm_crtc *crtc;
2938 void *r_base, *g_base, *b_base;
2939 int size;
2940 int ret = 0;
2941
Dave Airliefb3b06c2011-02-08 13:55:21 +10002942 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2943 return -EINVAL;
2944
Dave Airlief453ba02008-11-07 14:05:41 -08002945 mutex_lock(&dev->mode_config.mutex);
2946 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2947 if (!obj) {
2948 ret = -EINVAL;
2949 goto out;
2950 }
2951 crtc = obj_to_crtc(obj);
2952
2953 /* memcpy into gamma store */
2954 if (crtc_lut->gamma_size != crtc->gamma_size) {
2955 ret = -EINVAL;
2956 goto out;
2957 }
2958
2959 size = crtc_lut->gamma_size * (sizeof(uint16_t));
2960 r_base = crtc->gamma_store;
2961 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
2962 ret = -EFAULT;
2963 goto out;
2964 }
2965
2966 g_base = r_base + size;
2967 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
2968 ret = -EFAULT;
2969 goto out;
2970 }
2971
2972 b_base = g_base + size;
2973 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
2974 ret = -EFAULT;
2975 goto out;
2976 }
2977
James Simmons72034252010-08-03 01:33:19 +01002978 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08002979
2980out:
2981 mutex_unlock(&dev->mode_config.mutex);
2982 return ret;
2983
2984}
2985
2986int drm_mode_gamma_get_ioctl(struct drm_device *dev,
2987 void *data, struct drm_file *file_priv)
2988{
2989 struct drm_mode_crtc_lut *crtc_lut = data;
2990 struct drm_mode_object *obj;
2991 struct drm_crtc *crtc;
2992 void *r_base, *g_base, *b_base;
2993 int size;
2994 int ret = 0;
2995
Dave Airliefb3b06c2011-02-08 13:55:21 +10002996 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2997 return -EINVAL;
2998
Dave Airlief453ba02008-11-07 14:05:41 -08002999 mutex_lock(&dev->mode_config.mutex);
3000 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3001 if (!obj) {
3002 ret = -EINVAL;
3003 goto out;
3004 }
3005 crtc = obj_to_crtc(obj);
3006
3007 /* memcpy into gamma store */
3008 if (crtc_lut->gamma_size != crtc->gamma_size) {
3009 ret = -EINVAL;
3010 goto out;
3011 }
3012
3013 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3014 r_base = crtc->gamma_store;
3015 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3016 ret = -EFAULT;
3017 goto out;
3018 }
3019
3020 g_base = r_base + size;
3021 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3022 ret = -EFAULT;
3023 goto out;
3024 }
3025
3026 b_base = g_base + size;
3027 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3028 ret = -EFAULT;
3029 goto out;
3030 }
3031out:
3032 mutex_unlock(&dev->mode_config.mutex);
3033 return ret;
3034}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003035
3036int drm_mode_page_flip_ioctl(struct drm_device *dev,
3037 void *data, struct drm_file *file_priv)
3038{
3039 struct drm_mode_crtc_page_flip *page_flip = data;
3040 struct drm_mode_object *obj;
3041 struct drm_crtc *crtc;
3042 struct drm_framebuffer *fb;
3043 struct drm_pending_vblank_event *e = NULL;
3044 unsigned long flags;
3045 int ret = -EINVAL;
3046
3047 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3048 page_flip->reserved != 0)
3049 return -EINVAL;
3050
3051 mutex_lock(&dev->mode_config.mutex);
3052 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3053 if (!obj)
3054 goto out;
3055 crtc = obj_to_crtc(obj);
3056
Chris Wilson90c1efd2010-07-17 20:23:26 +01003057 if (crtc->fb == NULL) {
3058 /* The framebuffer is currently unbound, presumably
3059 * due to a hotplug event, that userspace has not
3060 * yet discovered.
3061 */
3062 ret = -EBUSY;
3063 goto out;
3064 }
3065
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003066 if (crtc->funcs->page_flip == NULL)
3067 goto out;
3068
3069 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3070 if (!obj)
3071 goto out;
3072 fb = obj_to_fb(obj);
3073
3074 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3075 ret = -ENOMEM;
3076 spin_lock_irqsave(&dev->event_lock, flags);
3077 if (file_priv->event_space < sizeof e->event) {
3078 spin_unlock_irqrestore(&dev->event_lock, flags);
3079 goto out;
3080 }
3081 file_priv->event_space -= sizeof e->event;
3082 spin_unlock_irqrestore(&dev->event_lock, flags);
3083
3084 e = kzalloc(sizeof *e, GFP_KERNEL);
3085 if (e == NULL) {
3086 spin_lock_irqsave(&dev->event_lock, flags);
3087 file_priv->event_space += sizeof e->event;
3088 spin_unlock_irqrestore(&dev->event_lock, flags);
3089 goto out;
3090 }
3091
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003092 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003093 e->event.base.length = sizeof e->event;
3094 e->event.user_data = page_flip->user_data;
3095 e->base.event = &e->event.base;
3096 e->base.file_priv = file_priv;
3097 e->base.destroy =
3098 (void (*) (struct drm_pending_event *)) kfree;
3099 }
3100
3101 ret = crtc->funcs->page_flip(crtc, fb, e);
3102 if (ret) {
3103 spin_lock_irqsave(&dev->event_lock, flags);
3104 file_priv->event_space += sizeof e->event;
3105 spin_unlock_irqrestore(&dev->event_lock, flags);
3106 kfree(e);
3107 }
3108
3109out:
3110 mutex_unlock(&dev->mode_config.mutex);
3111 return ret;
3112}
Chris Wilsoneb033552011-01-24 15:11:08 +00003113
3114void drm_mode_config_reset(struct drm_device *dev)
3115{
3116 struct drm_crtc *crtc;
3117 struct drm_encoder *encoder;
3118 struct drm_connector *connector;
3119
3120 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3121 if (crtc->funcs->reset)
3122 crtc->funcs->reset(crtc);
3123
3124 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3125 if (encoder->funcs->reset)
3126 encoder->funcs->reset(encoder);
3127
3128 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
3129 if (connector->funcs->reset)
3130 connector->funcs->reset(connector);
3131}
3132EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003133
3134int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3135 void *data, struct drm_file *file_priv)
3136{
3137 struct drm_mode_create_dumb *args = data;
3138
3139 if (!dev->driver->dumb_create)
3140 return -ENOSYS;
3141 return dev->driver->dumb_create(file_priv, dev, args);
3142}
3143
3144int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3145 void *data, struct drm_file *file_priv)
3146{
3147 struct drm_mode_map_dumb *args = data;
3148
3149 /* call driver ioctl to get mmap offset */
3150 if (!dev->driver->dumb_map_offset)
3151 return -ENOSYS;
3152
3153 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3154}
3155
3156int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3157 void *data, struct drm_file *file_priv)
3158{
3159 struct drm_mode_destroy_dumb *args = data;
3160
3161 if (!dev->driver->dumb_destroy)
3162 return -ENOSYS;
3163
3164 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3165}
Dave Airlie248dbc22011-11-29 20:02:54 +00003166
3167/*
3168 * Just need to support RGB formats here for compat with code that doesn't
3169 * use pixel formats directly yet.
3170 */
3171void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3172 int *bpp)
3173{
3174 switch (format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02003175 case DRM_FORMAT_RGB332:
3176 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003177 *depth = 8;
3178 *bpp = 8;
3179 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003180 case DRM_FORMAT_XRGB1555:
3181 case DRM_FORMAT_XBGR1555:
3182 case DRM_FORMAT_RGBX5551:
3183 case DRM_FORMAT_BGRX5551:
3184 case DRM_FORMAT_ARGB1555:
3185 case DRM_FORMAT_ABGR1555:
3186 case DRM_FORMAT_RGBA5551:
3187 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003188 *depth = 15;
3189 *bpp = 16;
3190 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003191 case DRM_FORMAT_RGB565:
3192 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003193 *depth = 16;
3194 *bpp = 16;
3195 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003196 case DRM_FORMAT_RGB888:
3197 case DRM_FORMAT_BGR888:
3198 *depth = 24;
3199 *bpp = 24;
3200 break;
3201 case DRM_FORMAT_XRGB8888:
3202 case DRM_FORMAT_XBGR8888:
3203 case DRM_FORMAT_RGBX8888:
3204 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003205 *depth = 24;
3206 *bpp = 32;
3207 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003208 case DRM_FORMAT_XRGB2101010:
3209 case DRM_FORMAT_XBGR2101010:
3210 case DRM_FORMAT_RGBX1010102:
3211 case DRM_FORMAT_BGRX1010102:
3212 case DRM_FORMAT_ARGB2101010:
3213 case DRM_FORMAT_ABGR2101010:
3214 case DRM_FORMAT_RGBA1010102:
3215 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003216 *depth = 30;
3217 *bpp = 32;
3218 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003219 case DRM_FORMAT_ARGB8888:
3220 case DRM_FORMAT_ABGR8888:
3221 case DRM_FORMAT_RGBA8888:
3222 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003223 *depth = 32;
3224 *bpp = 32;
3225 break;
3226 default:
3227 DRM_DEBUG_KMS("unsupported pixel format\n");
3228 *depth = 0;
3229 *bpp = 0;
3230 break;
3231 }
3232}
3233EXPORT_SYMBOL(drm_fb_get_bpp_depth);