Commit | Line | Data |
---|---|---|
f453ba04 DA |
1 | /* |
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> | |
5a0e3ad6 | 33 | #include <linux/slab.h> |
2d1a8a48 | 34 | #include <linux/export.h> |
f453ba04 DA |
35 | #include "drm.h" |
36 | #include "drmP.h" | |
37 | #include "drm_crtc.h" | |
7466f4cc | 38 | #include "drm_edid.h" |
308e5bcb | 39 | #include "drm_fourcc.h" |
f453ba04 | 40 | |
f453ba04 DA |
41 | /* Avoid boilerplate. I'm tired of typing. */ |
42 | #define DRM_ENUM_NAME_FN(fnname, list) \ | |
43 | char *fnname(int val) \ | |
44 | { \ | |
45 | int i; \ | |
46 | for (i = 0; i < ARRAY_SIZE(list); i++) { \ | |
47 | if (list[i].type == val) \ | |
48 | return list[i].name; \ | |
49 | } \ | |
50 | return "(unknown)"; \ | |
51 | } | |
52 | ||
53 | /* | |
54 | * Global properties | |
55 | */ | |
56 | static struct drm_prop_enum_list drm_dpms_enum_list[] = | |
57 | { { DRM_MODE_DPMS_ON, "On" }, | |
58 | { DRM_MODE_DPMS_STANDBY, "Standby" }, | |
59 | { DRM_MODE_DPMS_SUSPEND, "Suspend" }, | |
60 | { DRM_MODE_DPMS_OFF, "Off" } | |
61 | }; | |
62 | ||
63 | DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list) | |
64 | ||
65 | /* | |
66 | * Optional properties | |
67 | */ | |
68 | static struct drm_prop_enum_list drm_scaling_mode_enum_list[] = | |
69 | { | |
53bd8389 JB |
70 | { DRM_MODE_SCALE_NONE, "None" }, |
71 | { DRM_MODE_SCALE_FULLSCREEN, "Full" }, | |
72 | { DRM_MODE_SCALE_CENTER, "Center" }, | |
73 | { DRM_MODE_SCALE_ASPECT, "Full aspect" }, | |
f453ba04 DA |
74 | }; |
75 | ||
76 | static struct drm_prop_enum_list drm_dithering_mode_enum_list[] = | |
77 | { | |
78 | { DRM_MODE_DITHERING_OFF, "Off" }, | |
79 | { DRM_MODE_DITHERING_ON, "On" }, | |
92897b5c | 80 | { DRM_MODE_DITHERING_AUTO, "Automatic" }, |
f453ba04 DA |
81 | }; |
82 | ||
83 | /* | |
84 | * Non-global properties, but "required" for certain connectors. | |
85 | */ | |
86 | static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = | |
87 | { | |
88 | { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */ | |
89 | { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */ | |
90 | { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */ | |
91 | }; | |
92 | ||
93 | DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list) | |
94 | ||
95 | static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = | |
96 | { | |
97 | { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */ | |
98 | { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */ | |
99 | { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */ | |
100 | }; | |
101 | ||
102 | DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name, | |
103 | drm_dvi_i_subconnector_enum_list) | |
104 | ||
105 | static struct drm_prop_enum_list drm_tv_select_enum_list[] = | |
106 | { | |
107 | { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */ | |
108 | { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */ | |
109 | { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */ | |
110 | { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */ | |
aeaa1ad3 | 111 | { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */ |
f453ba04 DA |
112 | }; |
113 | ||
114 | DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list) | |
115 | ||
116 | static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = | |
117 | { | |
118 | { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */ | |
119 | { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */ | |
120 | { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */ | |
121 | { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */ | |
aeaa1ad3 | 122 | { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */ |
f453ba04 DA |
123 | }; |
124 | ||
125 | DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name, | |
126 | drm_tv_subconnector_enum_list) | |
127 | ||
884840aa JB |
128 | static struct drm_prop_enum_list drm_dirty_info_enum_list[] = { |
129 | { DRM_MODE_DIRTY_OFF, "Off" }, | |
130 | { DRM_MODE_DIRTY_ON, "On" }, | |
131 | { DRM_MODE_DIRTY_ANNOTATE, "Annotate" }, | |
132 | }; | |
133 | ||
134 | DRM_ENUM_NAME_FN(drm_get_dirty_info_name, | |
135 | drm_dirty_info_enum_list) | |
136 | ||
f453ba04 DA |
137 | struct drm_conn_prop_enum_list { |
138 | int type; | |
139 | char *name; | |
140 | int count; | |
141 | }; | |
142 | ||
143 | /* | |
144 | * Connector and encoder types. | |
145 | */ | |
146 | static struct drm_conn_prop_enum_list drm_connector_enum_list[] = | |
147 | { { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 }, | |
148 | { DRM_MODE_CONNECTOR_VGA, "VGA", 0 }, | |
149 | { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 }, | |
150 | { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 }, | |
151 | { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 }, | |
152 | { DRM_MODE_CONNECTOR_Composite, "Composite", 0 }, | |
153 | { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 }, | |
154 | { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 }, | |
155 | { DRM_MODE_CONNECTOR_Component, "Component", 0 }, | |
e76116ca AD |
156 | { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 }, |
157 | { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 }, | |
158 | { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 }, | |
159 | { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 }, | |
74bd3c26 | 160 | { DRM_MODE_CONNECTOR_TV, "TV", 0 }, |
e76116ca | 161 | { DRM_MODE_CONNECTOR_eDP, "eDP", 0 }, |
a7331e5c | 162 | { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0}, |
f453ba04 DA |
163 | }; |
164 | ||
165 | static struct drm_prop_enum_list drm_encoder_enum_list[] = | |
166 | { { DRM_MODE_ENCODER_NONE, "None" }, | |
167 | { DRM_MODE_ENCODER_DAC, "DAC" }, | |
168 | { DRM_MODE_ENCODER_TMDS, "TMDS" }, | |
169 | { DRM_MODE_ENCODER_LVDS, "LVDS" }, | |
170 | { DRM_MODE_ENCODER_TVDAC, "TV" }, | |
a7331e5c | 171 | { DRM_MODE_ENCODER_VIRTUAL, "Virtual" }, |
f453ba04 DA |
172 | }; |
173 | ||
174 | char *drm_get_encoder_name(struct drm_encoder *encoder) | |
175 | { | |
176 | static char buf[32]; | |
177 | ||
178 | snprintf(buf, 32, "%s-%d", | |
179 | drm_encoder_enum_list[encoder->encoder_type].name, | |
180 | encoder->base.id); | |
181 | return buf; | |
182 | } | |
13a8195b | 183 | EXPORT_SYMBOL(drm_get_encoder_name); |
f453ba04 DA |
184 | |
185 | char *drm_get_connector_name(struct drm_connector *connector) | |
186 | { | |
187 | static char buf[32]; | |
188 | ||
189 | snprintf(buf, 32, "%s-%d", | |
190 | drm_connector_enum_list[connector->connector_type].name, | |
191 | connector->connector_type_id); | |
192 | return buf; | |
193 | } | |
194 | EXPORT_SYMBOL(drm_get_connector_name); | |
195 | ||
196 | char *drm_get_connector_status_name(enum drm_connector_status status) | |
197 | { | |
198 | if (status == connector_status_connected) | |
199 | return "connected"; | |
200 | else if (status == connector_status_disconnected) | |
201 | return "disconnected"; | |
202 | else | |
203 | return "unknown"; | |
204 | } | |
205 | ||
206 | /** | |
207 | * drm_mode_object_get - allocate a new identifier | |
208 | * @dev: DRM device | |
209 | * @ptr: object pointer, used to generate unique ID | |
210 | * @type: object type | |
211 | * | |
212 | * LOCKING: | |
f453ba04 DA |
213 | * |
214 | * Create a unique identifier based on @ptr in @dev's identifier space. Used | |
215 | * for tracking modes, CRTCs and connectors. | |
216 | * | |
217 | * RETURNS: | |
218 | * New unique (relative to other objects in @dev) integer identifier for the | |
219 | * object. | |
220 | */ | |
221 | static int drm_mode_object_get(struct drm_device *dev, | |
222 | struct drm_mode_object *obj, uint32_t obj_type) | |
223 | { | |
224 | int new_id = 0; | |
225 | int ret; | |
226 | ||
f453ba04 DA |
227 | again: |
228 | if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) { | |
229 | DRM_ERROR("Ran out memory getting a mode number\n"); | |
f1ae126c | 230 | return -ENOMEM; |
f453ba04 DA |
231 | } |
232 | ||
ad2563c2 | 233 | mutex_lock(&dev->mode_config.idr_mutex); |
f453ba04 | 234 | ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id); |
ad2563c2 | 235 | mutex_unlock(&dev->mode_config.idr_mutex); |
f453ba04 DA |
236 | if (ret == -EAGAIN) |
237 | goto again; | |
f1ae126c VS |
238 | else if (ret) |
239 | return ret; | |
f453ba04 DA |
240 | |
241 | obj->id = new_id; | |
242 | obj->type = obj_type; | |
243 | return 0; | |
244 | } | |
245 | ||
246 | /** | |
247 | * drm_mode_object_put - free an identifer | |
248 | * @dev: DRM device | |
249 | * @id: ID to free | |
250 | * | |
251 | * LOCKING: | |
252 | * Caller must hold DRM mode_config lock. | |
253 | * | |
254 | * Free @id from @dev's unique identifier pool. | |
255 | */ | |
256 | static void drm_mode_object_put(struct drm_device *dev, | |
257 | struct drm_mode_object *object) | |
258 | { | |
ad2563c2 | 259 | mutex_lock(&dev->mode_config.idr_mutex); |
f453ba04 | 260 | idr_remove(&dev->mode_config.crtc_idr, object->id); |
ad2563c2 | 261 | mutex_unlock(&dev->mode_config.idr_mutex); |
f453ba04 DA |
262 | } |
263 | ||
7a9c9060 DV |
264 | struct drm_mode_object *drm_mode_object_find(struct drm_device *dev, |
265 | uint32_t id, uint32_t type) | |
f453ba04 | 266 | { |
ad2563c2 | 267 | struct drm_mode_object *obj = NULL; |
f453ba04 | 268 | |
ad2563c2 | 269 | mutex_lock(&dev->mode_config.idr_mutex); |
f453ba04 DA |
270 | obj = idr_find(&dev->mode_config.crtc_idr, id); |
271 | if (!obj || (obj->type != type) || (obj->id != id)) | |
ad2563c2 JB |
272 | obj = NULL; |
273 | mutex_unlock(&dev->mode_config.idr_mutex); | |
f453ba04 DA |
274 | |
275 | return obj; | |
276 | } | |
277 | EXPORT_SYMBOL(drm_mode_object_find); | |
278 | ||
f453ba04 DA |
279 | /** |
280 | * drm_framebuffer_init - initialize a framebuffer | |
281 | * @dev: DRM device | |
282 | * | |
283 | * LOCKING: | |
284 | * Caller must hold mode config lock. | |
285 | * | |
286 | * Allocates an ID for the framebuffer's parent mode object, sets its mode | |
287 | * functions & device file and adds it to the master fd list. | |
288 | * | |
289 | * RETURNS: | |
af901ca1 | 290 | * Zero on success, error code on failure. |
f453ba04 DA |
291 | */ |
292 | int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb, | |
293 | const struct drm_framebuffer_funcs *funcs) | |
294 | { | |
295 | int ret; | |
296 | ||
297 | ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB); | |
6bfc56aa | 298 | if (ret) |
f453ba04 | 299 | return ret; |
f453ba04 DA |
300 | |
301 | fb->dev = dev; | |
302 | fb->funcs = funcs; | |
303 | dev->mode_config.num_fb++; | |
304 | list_add(&fb->head, &dev->mode_config.fb_list); | |
305 | ||
306 | return 0; | |
307 | } | |
308 | EXPORT_SYMBOL(drm_framebuffer_init); | |
309 | ||
310 | /** | |
311 | * drm_framebuffer_cleanup - remove a framebuffer object | |
312 | * @fb: framebuffer to remove | |
313 | * | |
314 | * LOCKING: | |
315 | * Caller must hold mode config lock. | |
316 | * | |
317 | * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes | |
318 | * it, setting it to NULL. | |
319 | */ | |
320 | void drm_framebuffer_cleanup(struct drm_framebuffer *fb) | |
321 | { | |
322 | struct drm_device *dev = fb->dev; | |
323 | struct drm_crtc *crtc; | |
8cf5c917 | 324 | struct drm_plane *plane; |
5ef5f72f DA |
325 | struct drm_mode_set set; |
326 | int ret; | |
f453ba04 DA |
327 | |
328 | /* remove from any CRTC */ | |
329 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { | |
5ef5f72f DA |
330 | if (crtc->fb == fb) { |
331 | /* should turn off the crtc */ | |
332 | memset(&set, 0, sizeof(struct drm_mode_set)); | |
333 | set.crtc = crtc; | |
334 | set.fb = NULL; | |
335 | ret = crtc->funcs->set_config(&set); | |
336 | if (ret) | |
337 | DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc); | |
338 | } | |
f453ba04 DA |
339 | } |
340 | ||
8cf5c917 JB |
341 | list_for_each_entry(plane, &dev->mode_config.plane_list, head) { |
342 | if (plane->fb == fb) { | |
343 | /* should turn off the crtc */ | |
344 | ret = plane->funcs->disable_plane(plane); | |
345 | if (ret) | |
346 | DRM_ERROR("failed to disable plane with busy fb\n"); | |
a9971157 RC |
347 | /* disconnect the plane from the fb and crtc: */ |
348 | plane->fb = NULL; | |
349 | plane->crtc = NULL; | |
8cf5c917 JB |
350 | } |
351 | } | |
352 | ||
f453ba04 DA |
353 | drm_mode_object_put(dev, &fb->base); |
354 | list_del(&fb->head); | |
355 | dev->mode_config.num_fb--; | |
356 | } | |
357 | EXPORT_SYMBOL(drm_framebuffer_cleanup); | |
358 | ||
359 | /** | |
360 | * drm_crtc_init - Initialise a new CRTC object | |
361 | * @dev: DRM device | |
362 | * @crtc: CRTC object to init | |
363 | * @funcs: callbacks for the new CRTC | |
364 | * | |
365 | * LOCKING: | |
3b02ab88 | 366 | * Takes mode_config lock. |
f453ba04 DA |
367 | * |
368 | * Inits a new object created as base part of an driver crtc object. | |
6bfc56aa VS |
369 | * |
370 | * RETURNS: | |
371 | * Zero on success, error code on failure. | |
f453ba04 | 372 | */ |
6bfc56aa | 373 | int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc, |
f453ba04 DA |
374 | const struct drm_crtc_funcs *funcs) |
375 | { | |
6bfc56aa VS |
376 | int ret; |
377 | ||
f453ba04 DA |
378 | crtc->dev = dev; |
379 | crtc->funcs = funcs; | |
380 | ||
381 | mutex_lock(&dev->mode_config.mutex); | |
6bfc56aa VS |
382 | |
383 | ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC); | |
384 | if (ret) | |
385 | goto out; | |
f453ba04 | 386 | |
bffd9de0 PZ |
387 | crtc->base.properties = &crtc->properties; |
388 | ||
f453ba04 DA |
389 | list_add_tail(&crtc->head, &dev->mode_config.crtc_list); |
390 | dev->mode_config.num_crtc++; | |
6bfc56aa VS |
391 | |
392 | out: | |
f453ba04 | 393 | mutex_unlock(&dev->mode_config.mutex); |
6bfc56aa VS |
394 | |
395 | return ret; | |
f453ba04 DA |
396 | } |
397 | EXPORT_SYMBOL(drm_crtc_init); | |
398 | ||
399 | /** | |
400 | * drm_crtc_cleanup - Cleans up the core crtc usage. | |
401 | * @crtc: CRTC to cleanup | |
402 | * | |
403 | * LOCKING: | |
404 | * Caller must hold mode config lock. | |
405 | * | |
406 | * Cleanup @crtc. Removes from drm modesetting space | |
407 | * does NOT free object, caller does that. | |
408 | */ | |
409 | void drm_crtc_cleanup(struct drm_crtc *crtc) | |
410 | { | |
411 | struct drm_device *dev = crtc->dev; | |
412 | ||
413 | if (crtc->gamma_store) { | |
414 | kfree(crtc->gamma_store); | |
415 | crtc->gamma_store = NULL; | |
416 | } | |
417 | ||
418 | drm_mode_object_put(dev, &crtc->base); | |
419 | list_del(&crtc->head); | |
420 | dev->mode_config.num_crtc--; | |
421 | } | |
422 | EXPORT_SYMBOL(drm_crtc_cleanup); | |
423 | ||
424 | /** | |
425 | * drm_mode_probed_add - add a mode to a connector's probed mode list | |
426 | * @connector: connector the new mode | |
427 | * @mode: mode data | |
428 | * | |
429 | * LOCKING: | |
430 | * Caller must hold mode config lock. | |
431 | * | |
432 | * Add @mode to @connector's mode list for later use. | |
433 | */ | |
434 | void drm_mode_probed_add(struct drm_connector *connector, | |
435 | struct drm_display_mode *mode) | |
436 | { | |
437 | list_add(&mode->head, &connector->probed_modes); | |
438 | } | |
439 | EXPORT_SYMBOL(drm_mode_probed_add); | |
440 | ||
441 | /** | |
442 | * drm_mode_remove - remove and free a mode | |
443 | * @connector: connector list to modify | |
444 | * @mode: mode to remove | |
445 | * | |
446 | * LOCKING: | |
447 | * Caller must hold mode config lock. | |
448 | * | |
449 | * Remove @mode from @connector's mode list, then free it. | |
450 | */ | |
451 | void drm_mode_remove(struct drm_connector *connector, | |
452 | struct drm_display_mode *mode) | |
453 | { | |
454 | list_del(&mode->head); | |
554f1d78 | 455 | drm_mode_destroy(connector->dev, mode); |
f453ba04 DA |
456 | } |
457 | EXPORT_SYMBOL(drm_mode_remove); | |
458 | ||
459 | /** | |
460 | * drm_connector_init - Init a preallocated connector | |
461 | * @dev: DRM device | |
462 | * @connector: the connector to init | |
463 | * @funcs: callbacks for this connector | |
464 | * @name: user visible name of the connector | |
465 | * | |
466 | * LOCKING: | |
b20f3867 | 467 | * Takes mode config lock. |
f453ba04 DA |
468 | * |
469 | * Initialises a preallocated connector. Connectors should be | |
470 | * subclassed as part of driver connector objects. | |
6bfc56aa VS |
471 | * |
472 | * RETURNS: | |
473 | * Zero on success, error code on failure. | |
f453ba04 | 474 | */ |
6bfc56aa VS |
475 | int drm_connector_init(struct drm_device *dev, |
476 | struct drm_connector *connector, | |
477 | const struct drm_connector_funcs *funcs, | |
478 | int connector_type) | |
f453ba04 | 479 | { |
6bfc56aa VS |
480 | int ret; |
481 | ||
f453ba04 DA |
482 | mutex_lock(&dev->mode_config.mutex); |
483 | ||
6bfc56aa VS |
484 | ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR); |
485 | if (ret) | |
486 | goto out; | |
487 | ||
7e3bdf4a | 488 | connector->base.properties = &connector->properties; |
f453ba04 DA |
489 | connector->dev = dev; |
490 | connector->funcs = funcs; | |
f453ba04 DA |
491 | connector->connector_type = connector_type; |
492 | connector->connector_type_id = | |
493 | ++drm_connector_enum_list[connector_type].count; /* TODO */ | |
494 | INIT_LIST_HEAD(&connector->user_modes); | |
495 | INIT_LIST_HEAD(&connector->probed_modes); | |
496 | INIT_LIST_HEAD(&connector->modes); | |
497 | connector->edid_blob_ptr = NULL; | |
498 | ||
499 | list_add_tail(&connector->head, &dev->mode_config.connector_list); | |
500 | dev->mode_config.num_connector++; | |
501 | ||
a7331e5c TH |
502 | if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL) |
503 | drm_connector_attach_property(connector, | |
504 | dev->mode_config.edid_property, | |
505 | 0); | |
f453ba04 DA |
506 | |
507 | drm_connector_attach_property(connector, | |
508 | dev->mode_config.dpms_property, 0); | |
509 | ||
6bfc56aa | 510 | out: |
f453ba04 | 511 | mutex_unlock(&dev->mode_config.mutex); |
6bfc56aa VS |
512 | |
513 | return ret; | |
f453ba04 DA |
514 | } |
515 | EXPORT_SYMBOL(drm_connector_init); | |
516 | ||
517 | /** | |
518 | * drm_connector_cleanup - cleans up an initialised connector | |
519 | * @connector: connector to cleanup | |
520 | * | |
521 | * LOCKING: | |
b20f3867 | 522 | * Takes mode config lock. |
f453ba04 DA |
523 | * |
524 | * Cleans up the connector but doesn't free the object. | |
525 | */ | |
526 | void drm_connector_cleanup(struct drm_connector *connector) | |
527 | { | |
528 | struct drm_device *dev = connector->dev; | |
529 | struct drm_display_mode *mode, *t; | |
530 | ||
531 | list_for_each_entry_safe(mode, t, &connector->probed_modes, head) | |
532 | drm_mode_remove(connector, mode); | |
533 | ||
534 | list_for_each_entry_safe(mode, t, &connector->modes, head) | |
535 | drm_mode_remove(connector, mode); | |
536 | ||
537 | list_for_each_entry_safe(mode, t, &connector->user_modes, head) | |
538 | drm_mode_remove(connector, mode); | |
539 | ||
540 | mutex_lock(&dev->mode_config.mutex); | |
541 | drm_mode_object_put(dev, &connector->base); | |
542 | list_del(&connector->head); | |
6380c509 | 543 | dev->mode_config.num_connector--; |
f453ba04 DA |
544 | mutex_unlock(&dev->mode_config.mutex); |
545 | } | |
546 | EXPORT_SYMBOL(drm_connector_cleanup); | |
547 | ||
cbc7e221 DA |
548 | void drm_connector_unplug_all(struct drm_device *dev) |
549 | { | |
550 | struct drm_connector *connector; | |
551 | ||
552 | /* taking the mode config mutex ends up in a clash with sysfs */ | |
553 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) | |
554 | drm_sysfs_connector_remove(connector); | |
555 | ||
556 | } | |
557 | EXPORT_SYMBOL(drm_connector_unplug_all); | |
558 | ||
6bfc56aa | 559 | int drm_encoder_init(struct drm_device *dev, |
cbc7e221 DA |
560 | struct drm_encoder *encoder, |
561 | const struct drm_encoder_funcs *funcs, | |
562 | int encoder_type) | |
f453ba04 | 563 | { |
6bfc56aa VS |
564 | int ret; |
565 | ||
f453ba04 DA |
566 | mutex_lock(&dev->mode_config.mutex); |
567 | ||
6bfc56aa VS |
568 | ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER); |
569 | if (ret) | |
570 | goto out; | |
f453ba04 | 571 | |
6bfc56aa | 572 | encoder->dev = dev; |
f453ba04 DA |
573 | encoder->encoder_type = encoder_type; |
574 | encoder->funcs = funcs; | |
575 | ||
576 | list_add_tail(&encoder->head, &dev->mode_config.encoder_list); | |
577 | dev->mode_config.num_encoder++; | |
578 | ||
6bfc56aa | 579 | out: |
f453ba04 | 580 | mutex_unlock(&dev->mode_config.mutex); |
6bfc56aa VS |
581 | |
582 | return ret; | |
f453ba04 DA |
583 | } |
584 | EXPORT_SYMBOL(drm_encoder_init); | |
585 | ||
586 | void drm_encoder_cleanup(struct drm_encoder *encoder) | |
587 | { | |
588 | struct drm_device *dev = encoder->dev; | |
589 | mutex_lock(&dev->mode_config.mutex); | |
590 | drm_mode_object_put(dev, &encoder->base); | |
591 | list_del(&encoder->head); | |
6380c509 | 592 | dev->mode_config.num_encoder--; |
f453ba04 DA |
593 | mutex_unlock(&dev->mode_config.mutex); |
594 | } | |
595 | EXPORT_SYMBOL(drm_encoder_cleanup); | |
596 | ||
8cf5c917 JB |
597 | int drm_plane_init(struct drm_device *dev, struct drm_plane *plane, |
598 | unsigned long possible_crtcs, | |
599 | const struct drm_plane_funcs *funcs, | |
0a7eb243 RC |
600 | const uint32_t *formats, uint32_t format_count, |
601 | bool priv) | |
8cf5c917 | 602 | { |
6bfc56aa VS |
603 | int ret; |
604 | ||
8cf5c917 JB |
605 | mutex_lock(&dev->mode_config.mutex); |
606 | ||
6bfc56aa VS |
607 | ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE); |
608 | if (ret) | |
609 | goto out; | |
610 | ||
8cf5c917 | 611 | plane->dev = dev; |
8cf5c917 JB |
612 | plane->funcs = funcs; |
613 | plane->format_types = kmalloc(sizeof(uint32_t) * format_count, | |
614 | GFP_KERNEL); | |
615 | if (!plane->format_types) { | |
616 | DRM_DEBUG_KMS("out of memory when allocating plane\n"); | |
617 | drm_mode_object_put(dev, &plane->base); | |
6bfc56aa VS |
618 | ret = -ENOMEM; |
619 | goto out; | |
8cf5c917 JB |
620 | } |
621 | ||
308e5bcb | 622 | memcpy(plane->format_types, formats, format_count * sizeof(uint32_t)); |
8cf5c917 JB |
623 | plane->format_count = format_count; |
624 | plane->possible_crtcs = possible_crtcs; | |
625 | ||
0a7eb243 RC |
626 | /* private planes are not exposed to userspace, but depending on |
627 | * display hardware, might be convenient to allow sharing programming | |
628 | * for the scanout engine with the crtc implementation. | |
629 | */ | |
630 | if (!priv) { | |
631 | list_add_tail(&plane->head, &dev->mode_config.plane_list); | |
632 | dev->mode_config.num_plane++; | |
633 | } else { | |
634 | INIT_LIST_HEAD(&plane->head); | |
635 | } | |
8cf5c917 | 636 | |
6bfc56aa | 637 | out: |
8cf5c917 JB |
638 | mutex_unlock(&dev->mode_config.mutex); |
639 | ||
6bfc56aa | 640 | return ret; |
8cf5c917 JB |
641 | } |
642 | EXPORT_SYMBOL(drm_plane_init); | |
643 | ||
644 | void drm_plane_cleanup(struct drm_plane *plane) | |
645 | { | |
646 | struct drm_device *dev = plane->dev; | |
647 | ||
648 | mutex_lock(&dev->mode_config.mutex); | |
649 | kfree(plane->format_types); | |
650 | drm_mode_object_put(dev, &plane->base); | |
0a7eb243 RC |
651 | /* if not added to a list, it must be a private plane */ |
652 | if (!list_empty(&plane->head)) { | |
653 | list_del(&plane->head); | |
654 | dev->mode_config.num_plane--; | |
655 | } | |
8cf5c917 JB |
656 | mutex_unlock(&dev->mode_config.mutex); |
657 | } | |
658 | EXPORT_SYMBOL(drm_plane_cleanup); | |
659 | ||
f453ba04 DA |
660 | /** |
661 | * drm_mode_create - create a new display mode | |
662 | * @dev: DRM device | |
663 | * | |
664 | * LOCKING: | |
665 | * Caller must hold DRM mode_config lock. | |
666 | * | |
667 | * Create a new drm_display_mode, give it an ID, and return it. | |
668 | * | |
669 | * RETURNS: | |
670 | * Pointer to new mode on success, NULL on error. | |
671 | */ | |
672 | struct drm_display_mode *drm_mode_create(struct drm_device *dev) | |
673 | { | |
674 | struct drm_display_mode *nmode; | |
675 | ||
676 | nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL); | |
677 | if (!nmode) | |
678 | return NULL; | |
679 | ||
6bfc56aa VS |
680 | if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) { |
681 | kfree(nmode); | |
682 | return NULL; | |
683 | } | |
684 | ||
f453ba04 DA |
685 | return nmode; |
686 | } | |
687 | EXPORT_SYMBOL(drm_mode_create); | |
688 | ||
689 | /** | |
690 | * drm_mode_destroy - remove a mode | |
691 | * @dev: DRM device | |
692 | * @mode: mode to remove | |
693 | * | |
694 | * LOCKING: | |
695 | * Caller must hold mode config lock. | |
696 | * | |
697 | * Free @mode's unique identifier, then free it. | |
698 | */ | |
699 | void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode) | |
700 | { | |
ee34ab5b VS |
701 | if (!mode) |
702 | return; | |
703 | ||
f453ba04 DA |
704 | drm_mode_object_put(dev, &mode->base); |
705 | ||
706 | kfree(mode); | |
707 | } | |
708 | EXPORT_SYMBOL(drm_mode_destroy); | |
709 | ||
710 | static int drm_mode_create_standard_connector_properties(struct drm_device *dev) | |
711 | { | |
712 | struct drm_property *edid; | |
713 | struct drm_property *dpms; | |
f453ba04 DA |
714 | |
715 | /* | |
716 | * Standard properties (apply to all connectors) | |
717 | */ | |
718 | edid = drm_property_create(dev, DRM_MODE_PROP_BLOB | | |
719 | DRM_MODE_PROP_IMMUTABLE, | |
720 | "EDID", 0); | |
721 | dev->mode_config.edid_property = edid; | |
722 | ||
4a67d391 SH |
723 | dpms = drm_property_create_enum(dev, 0, |
724 | "DPMS", drm_dpms_enum_list, | |
725 | ARRAY_SIZE(drm_dpms_enum_list)); | |
f453ba04 DA |
726 | dev->mode_config.dpms_property = dpms; |
727 | ||
728 | return 0; | |
729 | } | |
730 | ||
731 | /** | |
732 | * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties | |
733 | * @dev: DRM device | |
734 | * | |
735 | * Called by a driver the first time a DVI-I connector is made. | |
736 | */ | |
737 | int drm_mode_create_dvi_i_properties(struct drm_device *dev) | |
738 | { | |
739 | struct drm_property *dvi_i_selector; | |
740 | struct drm_property *dvi_i_subconnector; | |
f453ba04 DA |
741 | |
742 | if (dev->mode_config.dvi_i_select_subconnector_property) | |
743 | return 0; | |
744 | ||
745 | dvi_i_selector = | |
4a67d391 | 746 | drm_property_create_enum(dev, 0, |
f453ba04 | 747 | "select subconnector", |
4a67d391 | 748 | drm_dvi_i_select_enum_list, |
f453ba04 | 749 | ARRAY_SIZE(drm_dvi_i_select_enum_list)); |
f453ba04 DA |
750 | dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector; |
751 | ||
4a67d391 | 752 | dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE, |
f453ba04 | 753 | "subconnector", |
4a67d391 | 754 | drm_dvi_i_subconnector_enum_list, |
f453ba04 | 755 | ARRAY_SIZE(drm_dvi_i_subconnector_enum_list)); |
f453ba04 DA |
756 | dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector; |
757 | ||
758 | return 0; | |
759 | } | |
760 | EXPORT_SYMBOL(drm_mode_create_dvi_i_properties); | |
761 | ||
762 | /** | |
763 | * drm_create_tv_properties - create TV specific connector properties | |
764 | * @dev: DRM device | |
765 | * @num_modes: number of different TV formats (modes) supported | |
766 | * @modes: array of pointers to strings containing name of each format | |
767 | * | |
768 | * Called by a driver's TV initialization routine, this function creates | |
769 | * the TV specific connector properties for a given device. Caller is | |
770 | * responsible for allocating a list of format names and passing them to | |
771 | * this routine. | |
772 | */ | |
773 | int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes, | |
774 | char *modes[]) | |
775 | { | |
776 | struct drm_property *tv_selector; | |
777 | struct drm_property *tv_subconnector; | |
778 | int i; | |
779 | ||
780 | if (dev->mode_config.tv_select_subconnector_property) | |
781 | return 0; | |
782 | ||
783 | /* | |
784 | * Basic connector properties | |
785 | */ | |
4a67d391 | 786 | tv_selector = drm_property_create_enum(dev, 0, |
f453ba04 | 787 | "select subconnector", |
4a67d391 | 788 | drm_tv_select_enum_list, |
f453ba04 | 789 | ARRAY_SIZE(drm_tv_select_enum_list)); |
f453ba04 DA |
790 | dev->mode_config.tv_select_subconnector_property = tv_selector; |
791 | ||
792 | tv_subconnector = | |
4a67d391 SH |
793 | drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE, |
794 | "subconnector", | |
795 | drm_tv_subconnector_enum_list, | |
f453ba04 | 796 | ARRAY_SIZE(drm_tv_subconnector_enum_list)); |
f453ba04 DA |
797 | dev->mode_config.tv_subconnector_property = tv_subconnector; |
798 | ||
799 | /* | |
800 | * Other, TV specific properties: margins & TV modes. | |
801 | */ | |
802 | dev->mode_config.tv_left_margin_property = | |
d9bc3c02 | 803 | drm_property_create_range(dev, 0, "left margin", 0, 100); |
f453ba04 DA |
804 | |
805 | dev->mode_config.tv_right_margin_property = | |
d9bc3c02 | 806 | drm_property_create_range(dev, 0, "right margin", 0, 100); |
f453ba04 DA |
807 | |
808 | dev->mode_config.tv_top_margin_property = | |
d9bc3c02 | 809 | drm_property_create_range(dev, 0, "top margin", 0, 100); |
f453ba04 DA |
810 | |
811 | dev->mode_config.tv_bottom_margin_property = | |
d9bc3c02 | 812 | drm_property_create_range(dev, 0, "bottom margin", 0, 100); |
f453ba04 DA |
813 | |
814 | dev->mode_config.tv_mode_property = | |
815 | drm_property_create(dev, DRM_MODE_PROP_ENUM, | |
816 | "mode", num_modes); | |
817 | for (i = 0; i < num_modes; i++) | |
818 | drm_property_add_enum(dev->mode_config.tv_mode_property, i, | |
819 | i, modes[i]); | |
820 | ||
b6b7902e | 821 | dev->mode_config.tv_brightness_property = |
d9bc3c02 | 822 | drm_property_create_range(dev, 0, "brightness", 0, 100); |
b6b7902e FJ |
823 | |
824 | dev->mode_config.tv_contrast_property = | |
d9bc3c02 | 825 | drm_property_create_range(dev, 0, "contrast", 0, 100); |
b6b7902e FJ |
826 | |
827 | dev->mode_config.tv_flicker_reduction_property = | |
d9bc3c02 | 828 | drm_property_create_range(dev, 0, "flicker reduction", 0, 100); |
b6b7902e | 829 | |
a75f0236 | 830 | dev->mode_config.tv_overscan_property = |
d9bc3c02 | 831 | drm_property_create_range(dev, 0, "overscan", 0, 100); |
a75f0236 FJ |
832 | |
833 | dev->mode_config.tv_saturation_property = | |
d9bc3c02 | 834 | drm_property_create_range(dev, 0, "saturation", 0, 100); |
a75f0236 FJ |
835 | |
836 | dev->mode_config.tv_hue_property = | |
d9bc3c02 | 837 | drm_property_create_range(dev, 0, "hue", 0, 100); |
a75f0236 | 838 | |
f453ba04 DA |
839 | return 0; |
840 | } | |
841 | EXPORT_SYMBOL(drm_mode_create_tv_properties); | |
842 | ||
843 | /** | |
844 | * drm_mode_create_scaling_mode_property - create scaling mode property | |
845 | * @dev: DRM device | |
846 | * | |
847 | * Called by a driver the first time it's needed, must be attached to desired | |
848 | * connectors. | |
849 | */ | |
850 | int drm_mode_create_scaling_mode_property(struct drm_device *dev) | |
851 | { | |
852 | struct drm_property *scaling_mode; | |
f453ba04 DA |
853 | |
854 | if (dev->mode_config.scaling_mode_property) | |
855 | return 0; | |
856 | ||
857 | scaling_mode = | |
4a67d391 SH |
858 | drm_property_create_enum(dev, 0, "scaling mode", |
859 | drm_scaling_mode_enum_list, | |
f453ba04 | 860 | ARRAY_SIZE(drm_scaling_mode_enum_list)); |
f453ba04 DA |
861 | |
862 | dev->mode_config.scaling_mode_property = scaling_mode; | |
863 | ||
864 | return 0; | |
865 | } | |
866 | EXPORT_SYMBOL(drm_mode_create_scaling_mode_property); | |
867 | ||
868 | /** | |
869 | * drm_mode_create_dithering_property - create dithering property | |
870 | * @dev: DRM device | |
871 | * | |
872 | * Called by a driver the first time it's needed, must be attached to desired | |
873 | * connectors. | |
874 | */ | |
875 | int drm_mode_create_dithering_property(struct drm_device *dev) | |
876 | { | |
877 | struct drm_property *dithering_mode; | |
f453ba04 DA |
878 | |
879 | if (dev->mode_config.dithering_mode_property) | |
880 | return 0; | |
881 | ||
882 | dithering_mode = | |
4a67d391 SH |
883 | drm_property_create_enum(dev, 0, "dithering", |
884 | drm_dithering_mode_enum_list, | |
f453ba04 | 885 | ARRAY_SIZE(drm_dithering_mode_enum_list)); |
f453ba04 DA |
886 | dev->mode_config.dithering_mode_property = dithering_mode; |
887 | ||
888 | return 0; | |
889 | } | |
890 | EXPORT_SYMBOL(drm_mode_create_dithering_property); | |
891 | ||
884840aa JB |
892 | /** |
893 | * drm_mode_create_dirty_property - create dirty property | |
894 | * @dev: DRM device | |
895 | * | |
896 | * Called by a driver the first time it's needed, must be attached to desired | |
897 | * connectors. | |
898 | */ | |
899 | int drm_mode_create_dirty_info_property(struct drm_device *dev) | |
900 | { | |
901 | struct drm_property *dirty_info; | |
884840aa JB |
902 | |
903 | if (dev->mode_config.dirty_info_property) | |
904 | return 0; | |
905 | ||
906 | dirty_info = | |
4a67d391 | 907 | drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE, |
884840aa | 908 | "dirty", |
4a67d391 | 909 | drm_dirty_info_enum_list, |
884840aa | 910 | ARRAY_SIZE(drm_dirty_info_enum_list)); |
884840aa JB |
911 | dev->mode_config.dirty_info_property = dirty_info; |
912 | ||
913 | return 0; | |
914 | } | |
915 | EXPORT_SYMBOL(drm_mode_create_dirty_info_property); | |
916 | ||
f453ba04 DA |
917 | /** |
918 | * drm_mode_config_init - initialize DRM mode_configuration structure | |
919 | * @dev: DRM device | |
920 | * | |
921 | * LOCKING: | |
922 | * None, should happen single threaded at init time. | |
923 | * | |
924 | * Initialize @dev's mode_config structure, used for tracking the graphics | |
925 | * configuration of @dev. | |
926 | */ | |
927 | void drm_mode_config_init(struct drm_device *dev) | |
928 | { | |
929 | mutex_init(&dev->mode_config.mutex); | |
ad2563c2 | 930 | mutex_init(&dev->mode_config.idr_mutex); |
f453ba04 | 931 | INIT_LIST_HEAD(&dev->mode_config.fb_list); |
f453ba04 DA |
932 | INIT_LIST_HEAD(&dev->mode_config.crtc_list); |
933 | INIT_LIST_HEAD(&dev->mode_config.connector_list); | |
934 | INIT_LIST_HEAD(&dev->mode_config.encoder_list); | |
935 | INIT_LIST_HEAD(&dev->mode_config.property_list); | |
936 | INIT_LIST_HEAD(&dev->mode_config.property_blob_list); | |
8cf5c917 | 937 | INIT_LIST_HEAD(&dev->mode_config.plane_list); |
f453ba04 DA |
938 | idr_init(&dev->mode_config.crtc_idr); |
939 | ||
940 | mutex_lock(&dev->mode_config.mutex); | |
941 | drm_mode_create_standard_connector_properties(dev); | |
942 | mutex_unlock(&dev->mode_config.mutex); | |
943 | ||
944 | /* Just to be sure */ | |
945 | dev->mode_config.num_fb = 0; | |
946 | dev->mode_config.num_connector = 0; | |
947 | dev->mode_config.num_crtc = 0; | |
948 | dev->mode_config.num_encoder = 0; | |
f453ba04 DA |
949 | } |
950 | EXPORT_SYMBOL(drm_mode_config_init); | |
951 | ||
952 | int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group) | |
953 | { | |
954 | uint32_t total_objects = 0; | |
955 | ||
956 | total_objects += dev->mode_config.num_crtc; | |
957 | total_objects += dev->mode_config.num_connector; | |
958 | total_objects += dev->mode_config.num_encoder; | |
959 | ||
f453ba04 DA |
960 | group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL); |
961 | if (!group->id_list) | |
962 | return -ENOMEM; | |
963 | ||
964 | group->num_crtcs = 0; | |
965 | group->num_connectors = 0; | |
966 | group->num_encoders = 0; | |
967 | return 0; | |
968 | } | |
969 | ||
970 | int drm_mode_group_init_legacy_group(struct drm_device *dev, | |
971 | struct drm_mode_group *group) | |
972 | { | |
973 | struct drm_crtc *crtc; | |
974 | struct drm_encoder *encoder; | |
975 | struct drm_connector *connector; | |
976 | int ret; | |
977 | ||
978 | if ((ret = drm_mode_group_init(dev, group))) | |
979 | return ret; | |
980 | ||
981 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) | |
982 | group->id_list[group->num_crtcs++] = crtc->base.id; | |
983 | ||
984 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) | |
985 | group->id_list[group->num_crtcs + group->num_encoders++] = | |
986 | encoder->base.id; | |
987 | ||
988 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) | |
989 | group->id_list[group->num_crtcs + group->num_encoders + | |
990 | group->num_connectors++] = connector->base.id; | |
991 | ||
992 | return 0; | |
993 | } | |
9c1dfc55 | 994 | EXPORT_SYMBOL(drm_mode_group_init_legacy_group); |
f453ba04 DA |
995 | |
996 | /** | |
997 | * drm_mode_config_cleanup - free up DRM mode_config info | |
998 | * @dev: DRM device | |
999 | * | |
1000 | * LOCKING: | |
1001 | * Caller must hold mode config lock. | |
1002 | * | |
1003 | * Free up all the connectors and CRTCs associated with this DRM device, then | |
1004 | * free up the framebuffers and associated buffer objects. | |
1005 | * | |
1006 | * FIXME: cleanup any dangling user buffer objects too | |
1007 | */ | |
1008 | void drm_mode_config_cleanup(struct drm_device *dev) | |
1009 | { | |
1010 | struct drm_connector *connector, *ot; | |
1011 | struct drm_crtc *crtc, *ct; | |
1012 | struct drm_encoder *encoder, *enct; | |
1013 | struct drm_framebuffer *fb, *fbt; | |
1014 | struct drm_property *property, *pt; | |
8cf5c917 | 1015 | struct drm_plane *plane, *plt; |
f453ba04 DA |
1016 | |
1017 | list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list, | |
1018 | head) { | |
1019 | encoder->funcs->destroy(encoder); | |
1020 | } | |
1021 | ||
1022 | list_for_each_entry_safe(connector, ot, | |
1023 | &dev->mode_config.connector_list, head) { | |
1024 | connector->funcs->destroy(connector); | |
1025 | } | |
1026 | ||
1027 | list_for_each_entry_safe(property, pt, &dev->mode_config.property_list, | |
1028 | head) { | |
1029 | drm_property_destroy(dev, property); | |
1030 | } | |
1031 | ||
1032 | list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) { | |
1033 | fb->funcs->destroy(fb); | |
1034 | } | |
1035 | ||
1036 | list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) { | |
1037 | crtc->funcs->destroy(crtc); | |
1038 | } | |
1039 | ||
8cf5c917 JB |
1040 | list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list, |
1041 | head) { | |
1042 | plane->funcs->destroy(plane); | |
1043 | } | |
59ce062e SH |
1044 | |
1045 | idr_remove_all(&dev->mode_config.crtc_idr); | |
1046 | idr_destroy(&dev->mode_config.crtc_idr); | |
f453ba04 DA |
1047 | } |
1048 | EXPORT_SYMBOL(drm_mode_config_cleanup); | |
1049 | ||
f453ba04 DA |
1050 | /** |
1051 | * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo | |
1052 | * @out: drm_mode_modeinfo struct to return to the user | |
1053 | * @in: drm_display_mode to use | |
1054 | * | |
1055 | * LOCKING: | |
1056 | * None. | |
1057 | * | |
1058 | * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to | |
1059 | * the user. | |
1060 | */ | |
93bbf6db VS |
1061 | static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out, |
1062 | const struct drm_display_mode *in) | |
f453ba04 | 1063 | { |
e36fae38 VS |
1064 | WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX || |
1065 | in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX || | |
1066 | in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX || | |
1067 | in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX || | |
1068 | in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX, | |
1069 | "timing values too large for mode info\n"); | |
1070 | ||
f453ba04 DA |
1071 | out->clock = in->clock; |
1072 | out->hdisplay = in->hdisplay; | |
1073 | out->hsync_start = in->hsync_start; | |
1074 | out->hsync_end = in->hsync_end; | |
1075 | out->htotal = in->htotal; | |
1076 | out->hskew = in->hskew; | |
1077 | out->vdisplay = in->vdisplay; | |
1078 | out->vsync_start = in->vsync_start; | |
1079 | out->vsync_end = in->vsync_end; | |
1080 | out->vtotal = in->vtotal; | |
1081 | out->vscan = in->vscan; | |
1082 | out->vrefresh = in->vrefresh; | |
1083 | out->flags = in->flags; | |
1084 | out->type = in->type; | |
1085 | strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN); | |
1086 | out->name[DRM_DISPLAY_MODE_LEN-1] = 0; | |
1087 | } | |
1088 | ||
1089 | /** | |
1090 | * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode | |
1091 | * @out: drm_display_mode to return to the user | |
1092 | * @in: drm_mode_modeinfo to use | |
1093 | * | |
1094 | * LOCKING: | |
1095 | * None. | |
1096 | * | |
1097 | * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to | |
1098 | * the caller. | |
90367bf6 VS |
1099 | * |
1100 | * RETURNS: | |
1101 | * Zero on success, errno on failure. | |
f453ba04 | 1102 | */ |
93bbf6db VS |
1103 | static int drm_crtc_convert_umode(struct drm_display_mode *out, |
1104 | const struct drm_mode_modeinfo *in) | |
f453ba04 | 1105 | { |
90367bf6 VS |
1106 | if (in->clock > INT_MAX || in->vrefresh > INT_MAX) |
1107 | return -ERANGE; | |
1108 | ||
f453ba04 DA |
1109 | out->clock = in->clock; |
1110 | out->hdisplay = in->hdisplay; | |
1111 | out->hsync_start = in->hsync_start; | |
1112 | out->hsync_end = in->hsync_end; | |
1113 | out->htotal = in->htotal; | |
1114 | out->hskew = in->hskew; | |
1115 | out->vdisplay = in->vdisplay; | |
1116 | out->vsync_start = in->vsync_start; | |
1117 | out->vsync_end = in->vsync_end; | |
1118 | out->vtotal = in->vtotal; | |
1119 | out->vscan = in->vscan; | |
1120 | out->vrefresh = in->vrefresh; | |
1121 | out->flags = in->flags; | |
1122 | out->type = in->type; | |
1123 | strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN); | |
1124 | out->name[DRM_DISPLAY_MODE_LEN-1] = 0; | |
90367bf6 VS |
1125 | |
1126 | return 0; | |
f453ba04 DA |
1127 | } |
1128 | ||
1129 | /** | |
1130 | * drm_mode_getresources - get graphics configuration | |
1131 | * @inode: inode from the ioctl | |
1132 | * @filp: file * from the ioctl | |
1133 | * @cmd: cmd from ioctl | |
1134 | * @arg: arg from ioctl | |
1135 | * | |
1136 | * LOCKING: | |
1137 | * Takes mode config lock. | |
1138 | * | |
1139 | * Construct a set of configuration description structures and return | |
1140 | * them to the user, including CRTC, connector and framebuffer configuration. | |
1141 | * | |
1142 | * Called by the user via ioctl. | |
1143 | * | |
1144 | * RETURNS: | |
1145 | * Zero on success, errno on failure. | |
1146 | */ | |
1147 | int drm_mode_getresources(struct drm_device *dev, void *data, | |
1148 | struct drm_file *file_priv) | |
1149 | { | |
1150 | struct drm_mode_card_res *card_res = data; | |
1151 | struct list_head *lh; | |
1152 | struct drm_framebuffer *fb; | |
1153 | struct drm_connector *connector; | |
1154 | struct drm_crtc *crtc; | |
1155 | struct drm_encoder *encoder; | |
1156 | int ret = 0; | |
1157 | int connector_count = 0; | |
1158 | int crtc_count = 0; | |
1159 | int fb_count = 0; | |
1160 | int encoder_count = 0; | |
1161 | int copied = 0, i; | |
1162 | uint32_t __user *fb_id; | |
1163 | uint32_t __user *crtc_id; | |
1164 | uint32_t __user *connector_id; | |
1165 | uint32_t __user *encoder_id; | |
1166 | struct drm_mode_group *mode_group; | |
1167 | ||
fb3b06c8 DA |
1168 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
1169 | return -EINVAL; | |
1170 | ||
f453ba04 DA |
1171 | mutex_lock(&dev->mode_config.mutex); |
1172 | ||
1173 | /* | |
1174 | * For the non-control nodes we need to limit the list of resources | |
1175 | * by IDs in the group list for this node | |
1176 | */ | |
1177 | list_for_each(lh, &file_priv->fbs) | |
1178 | fb_count++; | |
1179 | ||
1180 | mode_group = &file_priv->master->minor->mode_group; | |
1181 | if (file_priv->master->minor->type == DRM_MINOR_CONTROL) { | |
1182 | ||
1183 | list_for_each(lh, &dev->mode_config.crtc_list) | |
1184 | crtc_count++; | |
1185 | ||
1186 | list_for_each(lh, &dev->mode_config.connector_list) | |
1187 | connector_count++; | |
1188 | ||
1189 | list_for_each(lh, &dev->mode_config.encoder_list) | |
1190 | encoder_count++; | |
1191 | } else { | |
1192 | ||
1193 | crtc_count = mode_group->num_crtcs; | |
1194 | connector_count = mode_group->num_connectors; | |
1195 | encoder_count = mode_group->num_encoders; | |
1196 | } | |
1197 | ||
1198 | card_res->max_height = dev->mode_config.max_height; | |
1199 | card_res->min_height = dev->mode_config.min_height; | |
1200 | card_res->max_width = dev->mode_config.max_width; | |
1201 | card_res->min_width = dev->mode_config.min_width; | |
1202 | ||
1203 | /* handle this in 4 parts */ | |
1204 | /* FBs */ | |
1205 | if (card_res->count_fbs >= fb_count) { | |
1206 | copied = 0; | |
1207 | fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr; | |
618c75e4 | 1208 | list_for_each_entry(fb, &file_priv->fbs, filp_head) { |
f453ba04 DA |
1209 | if (put_user(fb->base.id, fb_id + copied)) { |
1210 | ret = -EFAULT; | |
1211 | goto out; | |
1212 | } | |
1213 | copied++; | |
1214 | } | |
1215 | } | |
1216 | card_res->count_fbs = fb_count; | |
1217 | ||
1218 | /* CRTCs */ | |
1219 | if (card_res->count_crtcs >= crtc_count) { | |
1220 | copied = 0; | |
1221 | crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr; | |
1222 | if (file_priv->master->minor->type == DRM_MINOR_CONTROL) { | |
1223 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, | |
1224 | head) { | |
9440106b | 1225 | DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id); |
f453ba04 DA |
1226 | if (put_user(crtc->base.id, crtc_id + copied)) { |
1227 | ret = -EFAULT; | |
1228 | goto out; | |
1229 | } | |
1230 | copied++; | |
1231 | } | |
1232 | } else { | |
1233 | for (i = 0; i < mode_group->num_crtcs; i++) { | |
1234 | if (put_user(mode_group->id_list[i], | |
1235 | crtc_id + copied)) { | |
1236 | ret = -EFAULT; | |
1237 | goto out; | |
1238 | } | |
1239 | copied++; | |
1240 | } | |
1241 | } | |
1242 | } | |
1243 | card_res->count_crtcs = crtc_count; | |
1244 | ||
1245 | /* Encoders */ | |
1246 | if (card_res->count_encoders >= encoder_count) { | |
1247 | copied = 0; | |
1248 | encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr; | |
1249 | if (file_priv->master->minor->type == DRM_MINOR_CONTROL) { | |
1250 | list_for_each_entry(encoder, | |
1251 | &dev->mode_config.encoder_list, | |
1252 | head) { | |
9440106b JG |
1253 | DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id, |
1254 | drm_get_encoder_name(encoder)); | |
f453ba04 DA |
1255 | if (put_user(encoder->base.id, encoder_id + |
1256 | copied)) { | |
1257 | ret = -EFAULT; | |
1258 | goto out; | |
1259 | } | |
1260 | copied++; | |
1261 | } | |
1262 | } else { | |
1263 | for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) { | |
1264 | if (put_user(mode_group->id_list[i], | |
1265 | encoder_id + copied)) { | |
1266 | ret = -EFAULT; | |
1267 | goto out; | |
1268 | } | |
1269 | copied++; | |
1270 | } | |
1271 | ||
1272 | } | |
1273 | } | |
1274 | card_res->count_encoders = encoder_count; | |
1275 | ||
1276 | /* Connectors */ | |
1277 | if (card_res->count_connectors >= connector_count) { | |
1278 | copied = 0; | |
1279 | connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr; | |
1280 | if (file_priv->master->minor->type == DRM_MINOR_CONTROL) { | |
1281 | list_for_each_entry(connector, | |
1282 | &dev->mode_config.connector_list, | |
1283 | head) { | |
9440106b JG |
1284 | DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", |
1285 | connector->base.id, | |
1286 | drm_get_connector_name(connector)); | |
f453ba04 DA |
1287 | if (put_user(connector->base.id, |
1288 | connector_id + copied)) { | |
1289 | ret = -EFAULT; | |
1290 | goto out; | |
1291 | } | |
1292 | copied++; | |
1293 | } | |
1294 | } else { | |
1295 | int start = mode_group->num_crtcs + | |
1296 | mode_group->num_encoders; | |
1297 | for (i = start; i < start + mode_group->num_connectors; i++) { | |
1298 | if (put_user(mode_group->id_list[i], | |
1299 | connector_id + copied)) { | |
1300 | ret = -EFAULT; | |
1301 | goto out; | |
1302 | } | |
1303 | copied++; | |
1304 | } | |
1305 | } | |
1306 | } | |
1307 | card_res->count_connectors = connector_count; | |
1308 | ||
9440106b | 1309 | DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs, |
f453ba04 DA |
1310 | card_res->count_connectors, card_res->count_encoders); |
1311 | ||
1312 | out: | |
1313 | mutex_unlock(&dev->mode_config.mutex); | |
1314 | return ret; | |
1315 | } | |
1316 | ||
1317 | /** | |
1318 | * drm_mode_getcrtc - get CRTC configuration | |
1319 | * @inode: inode from the ioctl | |
1320 | * @filp: file * from the ioctl | |
1321 | * @cmd: cmd from ioctl | |
1322 | * @arg: arg from ioctl | |
1323 | * | |
1324 | * LOCKING: | |
b20f3867 | 1325 | * Takes mode config lock. |
f453ba04 DA |
1326 | * |
1327 | * Construct a CRTC configuration structure to return to the user. | |
1328 | * | |
1329 | * Called by the user via ioctl. | |
1330 | * | |
1331 | * RETURNS: | |
1332 | * Zero on success, errno on failure. | |
1333 | */ | |
1334 | int drm_mode_getcrtc(struct drm_device *dev, | |
1335 | void *data, struct drm_file *file_priv) | |
1336 | { | |
1337 | struct drm_mode_crtc *crtc_resp = data; | |
1338 | struct drm_crtc *crtc; | |
1339 | struct drm_mode_object *obj; | |
1340 | int ret = 0; | |
1341 | ||
fb3b06c8 DA |
1342 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
1343 | return -EINVAL; | |
1344 | ||
f453ba04 DA |
1345 | mutex_lock(&dev->mode_config.mutex); |
1346 | ||
1347 | obj = drm_mode_object_find(dev, crtc_resp->crtc_id, | |
1348 | DRM_MODE_OBJECT_CRTC); | |
1349 | if (!obj) { | |
1350 | ret = -EINVAL; | |
1351 | goto out; | |
1352 | } | |
1353 | crtc = obj_to_crtc(obj); | |
1354 | ||
1355 | crtc_resp->x = crtc->x; | |
1356 | crtc_resp->y = crtc->y; | |
1357 | crtc_resp->gamma_size = crtc->gamma_size; | |
1358 | if (crtc->fb) | |
1359 | crtc_resp->fb_id = crtc->fb->base.id; | |
1360 | else | |
1361 | crtc_resp->fb_id = 0; | |
1362 | ||
1363 | if (crtc->enabled) { | |
1364 | ||
1365 | drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode); | |
1366 | crtc_resp->mode_valid = 1; | |
1367 | ||
1368 | } else { | |
1369 | crtc_resp->mode_valid = 0; | |
1370 | } | |
1371 | ||
1372 | out: | |
1373 | mutex_unlock(&dev->mode_config.mutex); | |
1374 | return ret; | |
1375 | } | |
1376 | ||
1377 | /** | |
1378 | * drm_mode_getconnector - get connector configuration | |
1379 | * @inode: inode from the ioctl | |
1380 | * @filp: file * from the ioctl | |
1381 | * @cmd: cmd from ioctl | |
1382 | * @arg: arg from ioctl | |
1383 | * | |
1384 | * LOCKING: | |
b20f3867 | 1385 | * Takes mode config lock. |
f453ba04 DA |
1386 | * |
1387 | * Construct a connector configuration structure to return to the user. | |
1388 | * | |
1389 | * Called by the user via ioctl. | |
1390 | * | |
1391 | * RETURNS: | |
1392 | * Zero on success, errno on failure. | |
1393 | */ | |
1394 | int drm_mode_getconnector(struct drm_device *dev, void *data, | |
1395 | struct drm_file *file_priv) | |
1396 | { | |
1397 | struct drm_mode_get_connector *out_resp = data; | |
1398 | struct drm_mode_object *obj; | |
1399 | struct drm_connector *connector; | |
1400 | struct drm_display_mode *mode; | |
1401 | int mode_count = 0; | |
1402 | int props_count = 0; | |
1403 | int encoders_count = 0; | |
1404 | int ret = 0; | |
1405 | int copied = 0; | |
1406 | int i; | |
1407 | struct drm_mode_modeinfo u_mode; | |
1408 | struct drm_mode_modeinfo __user *mode_ptr; | |
1409 | uint32_t __user *prop_ptr; | |
1410 | uint64_t __user *prop_values; | |
1411 | uint32_t __user *encoder_ptr; | |
1412 | ||
fb3b06c8 DA |
1413 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
1414 | return -EINVAL; | |
1415 | ||
f453ba04 DA |
1416 | memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo)); |
1417 | ||
9440106b | 1418 | DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id); |
f453ba04 DA |
1419 | |
1420 | mutex_lock(&dev->mode_config.mutex); | |
1421 | ||
1422 | obj = drm_mode_object_find(dev, out_resp->connector_id, | |
1423 | DRM_MODE_OBJECT_CONNECTOR); | |
1424 | if (!obj) { | |
1425 | ret = -EINVAL; | |
1426 | goto out; | |
1427 | } | |
1428 | connector = obj_to_connector(obj); | |
1429 | ||
7f88a9be | 1430 | props_count = connector->properties.count; |
f453ba04 DA |
1431 | |
1432 | for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) { | |
1433 | if (connector->encoder_ids[i] != 0) { | |
1434 | encoders_count++; | |
1435 | } | |
1436 | } | |
1437 | ||
1438 | if (out_resp->count_modes == 0) { | |
1439 | connector->funcs->fill_modes(connector, | |
1440 | dev->mode_config.max_width, | |
1441 | dev->mode_config.max_height); | |
1442 | } | |
1443 | ||
1444 | /* delayed so we get modes regardless of pre-fill_modes state */ | |
1445 | list_for_each_entry(mode, &connector->modes, head) | |
1446 | mode_count++; | |
1447 | ||
1448 | out_resp->connector_id = connector->base.id; | |
1449 | out_resp->connector_type = connector->connector_type; | |
1450 | out_resp->connector_type_id = connector->connector_type_id; | |
1451 | out_resp->mm_width = connector->display_info.width_mm; | |
1452 | out_resp->mm_height = connector->display_info.height_mm; | |
1453 | out_resp->subpixel = connector->display_info.subpixel_order; | |
1454 | out_resp->connection = connector->status; | |
1455 | if (connector->encoder) | |
1456 | out_resp->encoder_id = connector->encoder->base.id; | |
1457 | else | |
1458 | out_resp->encoder_id = 0; | |
1459 | ||
1460 | /* | |
1461 | * This ioctl is called twice, once to determine how much space is | |
1462 | * needed, and the 2nd time to fill it. | |
1463 | */ | |
1464 | if ((out_resp->count_modes >= mode_count) && mode_count) { | |
1465 | copied = 0; | |
81f6c7f8 | 1466 | mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr; |
f453ba04 DA |
1467 | list_for_each_entry(mode, &connector->modes, head) { |
1468 | drm_crtc_convert_to_umode(&u_mode, mode); | |
1469 | if (copy_to_user(mode_ptr + copied, | |
1470 | &u_mode, sizeof(u_mode))) { | |
1471 | ret = -EFAULT; | |
1472 | goto out; | |
1473 | } | |
1474 | copied++; | |
1475 | } | |
1476 | } | |
1477 | out_resp->count_modes = mode_count; | |
1478 | ||
1479 | if ((out_resp->count_props >= props_count) && props_count) { | |
1480 | copied = 0; | |
81f6c7f8 VS |
1481 | prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr); |
1482 | prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr); | |
7f88a9be PZ |
1483 | for (i = 0; i < connector->properties.count; i++) { |
1484 | if (put_user(connector->properties.ids[i], | |
1485 | prop_ptr + copied)) { | |
1486 | ret = -EFAULT; | |
1487 | goto out; | |
1488 | } | |
f453ba04 | 1489 | |
7f88a9be PZ |
1490 | if (put_user(connector->properties.values[i], |
1491 | prop_values + copied)) { | |
1492 | ret = -EFAULT; | |
1493 | goto out; | |
f453ba04 | 1494 | } |
7f88a9be | 1495 | copied++; |
f453ba04 DA |
1496 | } |
1497 | } | |
1498 | out_resp->count_props = props_count; | |
1499 | ||
1500 | if ((out_resp->count_encoders >= encoders_count) && encoders_count) { | |
1501 | copied = 0; | |
81f6c7f8 | 1502 | encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr); |
f453ba04 DA |
1503 | for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) { |
1504 | if (connector->encoder_ids[i] != 0) { | |
1505 | if (put_user(connector->encoder_ids[i], | |
1506 | encoder_ptr + copied)) { | |
1507 | ret = -EFAULT; | |
1508 | goto out; | |
1509 | } | |
1510 | copied++; | |
1511 | } | |
1512 | } | |
1513 | } | |
1514 | out_resp->count_encoders = encoders_count; | |
1515 | ||
1516 | out: | |
1517 | mutex_unlock(&dev->mode_config.mutex); | |
1518 | return ret; | |
1519 | } | |
1520 | ||
1521 | int drm_mode_getencoder(struct drm_device *dev, void *data, | |
1522 | struct drm_file *file_priv) | |
1523 | { | |
1524 | struct drm_mode_get_encoder *enc_resp = data; | |
1525 | struct drm_mode_object *obj; | |
1526 | struct drm_encoder *encoder; | |
1527 | int ret = 0; | |
1528 | ||
fb3b06c8 DA |
1529 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
1530 | return -EINVAL; | |
1531 | ||
f453ba04 DA |
1532 | mutex_lock(&dev->mode_config.mutex); |
1533 | obj = drm_mode_object_find(dev, enc_resp->encoder_id, | |
1534 | DRM_MODE_OBJECT_ENCODER); | |
1535 | if (!obj) { | |
1536 | ret = -EINVAL; | |
1537 | goto out; | |
1538 | } | |
1539 | encoder = obj_to_encoder(obj); | |
1540 | ||
1541 | if (encoder->crtc) | |
1542 | enc_resp->crtc_id = encoder->crtc->base.id; | |
1543 | else | |
1544 | enc_resp->crtc_id = 0; | |
1545 | enc_resp->encoder_type = encoder->encoder_type; | |
1546 | enc_resp->encoder_id = encoder->base.id; | |
1547 | enc_resp->possible_crtcs = encoder->possible_crtcs; | |
1548 | enc_resp->possible_clones = encoder->possible_clones; | |
1549 | ||
1550 | out: | |
1551 | mutex_unlock(&dev->mode_config.mutex); | |
1552 | return ret; | |
1553 | } | |
1554 | ||
8cf5c917 JB |
1555 | /** |
1556 | * drm_mode_getplane_res - get plane info | |
1557 | * @dev: DRM device | |
1558 | * @data: ioctl data | |
1559 | * @file_priv: DRM file info | |
1560 | * | |
b20f3867 SH |
1561 | * LOCKING: |
1562 | * Takes mode config lock. | |
1563 | * | |
8cf5c917 JB |
1564 | * Return an plane count and set of IDs. |
1565 | */ | |
1566 | int drm_mode_getplane_res(struct drm_device *dev, void *data, | |
1567 | struct drm_file *file_priv) | |
1568 | { | |
1569 | struct drm_mode_get_plane_res *plane_resp = data; | |
1570 | struct drm_mode_config *config; | |
1571 | struct drm_plane *plane; | |
1572 | uint32_t __user *plane_ptr; | |
1573 | int copied = 0, ret = 0; | |
1574 | ||
1575 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) | |
1576 | return -EINVAL; | |
1577 | ||
1578 | mutex_lock(&dev->mode_config.mutex); | |
1579 | config = &dev->mode_config; | |
1580 | ||
1581 | /* | |
1582 | * This ioctl is called twice, once to determine how much space is | |
1583 | * needed, and the 2nd time to fill it. | |
1584 | */ | |
1585 | if (config->num_plane && | |
1586 | (plane_resp->count_planes >= config->num_plane)) { | |
81f6c7f8 | 1587 | plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr; |
8cf5c917 JB |
1588 | |
1589 | list_for_each_entry(plane, &config->plane_list, head) { | |
1590 | if (put_user(plane->base.id, plane_ptr + copied)) { | |
1591 | ret = -EFAULT; | |
1592 | goto out; | |
1593 | } | |
1594 | copied++; | |
1595 | } | |
1596 | } | |
1597 | plane_resp->count_planes = config->num_plane; | |
1598 | ||
1599 | out: | |
1600 | mutex_unlock(&dev->mode_config.mutex); | |
1601 | return ret; | |
1602 | } | |
1603 | ||
1604 | /** | |
1605 | * drm_mode_getplane - get plane info | |
1606 | * @dev: DRM device | |
1607 | * @data: ioctl data | |
1608 | * @file_priv: DRM file info | |
1609 | * | |
b20f3867 SH |
1610 | * LOCKING: |
1611 | * Takes mode config lock. | |
1612 | * | |
8cf5c917 JB |
1613 | * Return plane info, including formats supported, gamma size, any |
1614 | * current fb, etc. | |
1615 | */ | |
1616 | int drm_mode_getplane(struct drm_device *dev, void *data, | |
1617 | struct drm_file *file_priv) | |
1618 | { | |
1619 | struct drm_mode_get_plane *plane_resp = data; | |
1620 | struct drm_mode_object *obj; | |
1621 | struct drm_plane *plane; | |
1622 | uint32_t __user *format_ptr; | |
1623 | int ret = 0; | |
1624 | ||
1625 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) | |
1626 | return -EINVAL; | |
1627 | ||
1628 | mutex_lock(&dev->mode_config.mutex); | |
1629 | obj = drm_mode_object_find(dev, plane_resp->plane_id, | |
1630 | DRM_MODE_OBJECT_PLANE); | |
1631 | if (!obj) { | |
1632 | ret = -ENOENT; | |
1633 | goto out; | |
1634 | } | |
1635 | plane = obj_to_plane(obj); | |
1636 | ||
1637 | if (plane->crtc) | |
1638 | plane_resp->crtc_id = plane->crtc->base.id; | |
1639 | else | |
1640 | plane_resp->crtc_id = 0; | |
1641 | ||
1642 | if (plane->fb) | |
1643 | plane_resp->fb_id = plane->fb->base.id; | |
1644 | else | |
1645 | plane_resp->fb_id = 0; | |
1646 | ||
1647 | plane_resp->plane_id = plane->base.id; | |
1648 | plane_resp->possible_crtcs = plane->possible_crtcs; | |
1649 | plane_resp->gamma_size = plane->gamma_size; | |
1650 | ||
1651 | /* | |
1652 | * This ioctl is called twice, once to determine how much space is | |
1653 | * needed, and the 2nd time to fill it. | |
1654 | */ | |
1655 | if (plane->format_count && | |
1656 | (plane_resp->count_format_types >= plane->format_count)) { | |
81f6c7f8 | 1657 | format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr; |
8cf5c917 JB |
1658 | if (copy_to_user(format_ptr, |
1659 | plane->format_types, | |
1660 | sizeof(uint32_t) * plane->format_count)) { | |
1661 | ret = -EFAULT; | |
1662 | goto out; | |
1663 | } | |
1664 | } | |
1665 | plane_resp->count_format_types = plane->format_count; | |
1666 | ||
1667 | out: | |
1668 | mutex_unlock(&dev->mode_config.mutex); | |
1669 | return ret; | |
1670 | } | |
1671 | ||
1672 | /** | |
1673 | * drm_mode_setplane - set up or tear down an plane | |
1674 | * @dev: DRM device | |
1675 | * @data: ioctl data* | |
1676 | * @file_prive: DRM file info | |
1677 | * | |
b20f3867 SH |
1678 | * LOCKING: |
1679 | * Takes mode config lock. | |
1680 | * | |
8cf5c917 JB |
1681 | * Set plane info, including placement, fb, scaling, and other factors. |
1682 | * Or pass a NULL fb to disable. | |
1683 | */ | |
1684 | int drm_mode_setplane(struct drm_device *dev, void *data, | |
1685 | struct drm_file *file_priv) | |
1686 | { | |
1687 | struct drm_mode_set_plane *plane_req = data; | |
1688 | struct drm_mode_object *obj; | |
1689 | struct drm_plane *plane; | |
1690 | struct drm_crtc *crtc; | |
1691 | struct drm_framebuffer *fb; | |
1692 | int ret = 0; | |
42ef8789 | 1693 | unsigned int fb_width, fb_height; |
62443be6 | 1694 | int i; |
8cf5c917 JB |
1695 | |
1696 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) | |
1697 | return -EINVAL; | |
1698 | ||
1699 | mutex_lock(&dev->mode_config.mutex); | |
1700 | ||
1701 | /* | |
1702 | * First, find the plane, crtc, and fb objects. If not available, | |
1703 | * we don't bother to call the driver. | |
1704 | */ | |
1705 | obj = drm_mode_object_find(dev, plane_req->plane_id, | |
1706 | DRM_MODE_OBJECT_PLANE); | |
1707 | if (!obj) { | |
1708 | DRM_DEBUG_KMS("Unknown plane ID %d\n", | |
1709 | plane_req->plane_id); | |
1710 | ret = -ENOENT; | |
1711 | goto out; | |
1712 | } | |
1713 | plane = obj_to_plane(obj); | |
1714 | ||
1715 | /* No fb means shut it down */ | |
1716 | if (!plane_req->fb_id) { | |
1717 | plane->funcs->disable_plane(plane); | |
e5e3b44c VS |
1718 | plane->crtc = NULL; |
1719 | plane->fb = NULL; | |
8cf5c917 JB |
1720 | goto out; |
1721 | } | |
1722 | ||
1723 | obj = drm_mode_object_find(dev, plane_req->crtc_id, | |
1724 | DRM_MODE_OBJECT_CRTC); | |
1725 | if (!obj) { | |
1726 | DRM_DEBUG_KMS("Unknown crtc ID %d\n", | |
1727 | plane_req->crtc_id); | |
1728 | ret = -ENOENT; | |
1729 | goto out; | |
1730 | } | |
1731 | crtc = obj_to_crtc(obj); | |
1732 | ||
1733 | obj = drm_mode_object_find(dev, plane_req->fb_id, | |
1734 | DRM_MODE_OBJECT_FB); | |
1735 | if (!obj) { | |
1736 | DRM_DEBUG_KMS("Unknown framebuffer ID %d\n", | |
1737 | plane_req->fb_id); | |
1738 | ret = -ENOENT; | |
1739 | goto out; | |
1740 | } | |
1741 | fb = obj_to_fb(obj); | |
1742 | ||
62443be6 VS |
1743 | /* Check whether this plane supports the fb pixel format. */ |
1744 | for (i = 0; i < plane->format_count; i++) | |
1745 | if (fb->pixel_format == plane->format_types[i]) | |
1746 | break; | |
1747 | if (i == plane->format_count) { | |
1748 | DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format); | |
1749 | ret = -EINVAL; | |
1750 | goto out; | |
1751 | } | |
1752 | ||
42ef8789 VS |
1753 | fb_width = fb->width << 16; |
1754 | fb_height = fb->height << 16; | |
1755 | ||
1756 | /* Make sure source coordinates are inside the fb. */ | |
1757 | if (plane_req->src_w > fb_width || | |
1758 | plane_req->src_x > fb_width - plane_req->src_w || | |
1759 | plane_req->src_h > fb_height || | |
1760 | plane_req->src_y > fb_height - plane_req->src_h) { | |
1761 | DRM_DEBUG_KMS("Invalid source coordinates " | |
1762 | "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n", | |
1763 | plane_req->src_w >> 16, | |
1764 | ((plane_req->src_w & 0xffff) * 15625) >> 10, | |
1765 | plane_req->src_h >> 16, | |
1766 | ((plane_req->src_h & 0xffff) * 15625) >> 10, | |
1767 | plane_req->src_x >> 16, | |
1768 | ((plane_req->src_x & 0xffff) * 15625) >> 10, | |
1769 | plane_req->src_y >> 16, | |
1770 | ((plane_req->src_y & 0xffff) * 15625) >> 10); | |
1771 | ret = -ENOSPC; | |
1772 | goto out; | |
1773 | } | |
1774 | ||
687a0400 VS |
1775 | /* Give drivers some help against integer overflows */ |
1776 | if (plane_req->crtc_w > INT_MAX || | |
1777 | plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w || | |
1778 | plane_req->crtc_h > INT_MAX || | |
1779 | plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) { | |
1780 | DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n", | |
1781 | plane_req->crtc_w, plane_req->crtc_h, | |
1782 | plane_req->crtc_x, plane_req->crtc_y); | |
1783 | ret = -ERANGE; | |
1784 | goto out; | |
1785 | } | |
1786 | ||
8cf5c917 JB |
1787 | ret = plane->funcs->update_plane(plane, crtc, fb, |
1788 | plane_req->crtc_x, plane_req->crtc_y, | |
1789 | plane_req->crtc_w, plane_req->crtc_h, | |
1790 | plane_req->src_x, plane_req->src_y, | |
1791 | plane_req->src_w, plane_req->src_h); | |
1792 | if (!ret) { | |
1793 | plane->crtc = crtc; | |
1794 | plane->fb = fb; | |
1795 | } | |
1796 | ||
1797 | out: | |
1798 | mutex_unlock(&dev->mode_config.mutex); | |
1799 | ||
1800 | return ret; | |
1801 | } | |
1802 | ||
f453ba04 DA |
1803 | /** |
1804 | * drm_mode_setcrtc - set CRTC configuration | |
1805 | * @inode: inode from the ioctl | |
1806 | * @filp: file * from the ioctl | |
1807 | * @cmd: cmd from ioctl | |
1808 | * @arg: arg from ioctl | |
1809 | * | |
1810 | * LOCKING: | |
b20f3867 | 1811 | * Takes mode config lock. |
f453ba04 DA |
1812 | * |
1813 | * Build a new CRTC configuration based on user request. | |
1814 | * | |
1815 | * Called by the user via ioctl. | |
1816 | * | |
1817 | * RETURNS: | |
1818 | * Zero on success, errno on failure. | |
1819 | */ | |
1820 | int drm_mode_setcrtc(struct drm_device *dev, void *data, | |
1821 | struct drm_file *file_priv) | |
1822 | { | |
1823 | struct drm_mode_config *config = &dev->mode_config; | |
1824 | struct drm_mode_crtc *crtc_req = data; | |
1825 | struct drm_mode_object *obj; | |
6653cc8d | 1826 | struct drm_crtc *crtc; |
f453ba04 DA |
1827 | struct drm_connector **connector_set = NULL, *connector; |
1828 | struct drm_framebuffer *fb = NULL; | |
1829 | struct drm_display_mode *mode = NULL; | |
1830 | struct drm_mode_set set; | |
1831 | uint32_t __user *set_connectors_ptr; | |
4a1b0714 | 1832 | int ret; |
f453ba04 DA |
1833 | int i; |
1834 | ||
fb3b06c8 DA |
1835 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
1836 | return -EINVAL; | |
1837 | ||
1d97e915 VS |
1838 | /* For some reason crtc x/y offsets are signed internally. */ |
1839 | if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX) | |
1840 | return -ERANGE; | |
1841 | ||
f453ba04 DA |
1842 | mutex_lock(&dev->mode_config.mutex); |
1843 | obj = drm_mode_object_find(dev, crtc_req->crtc_id, | |
1844 | DRM_MODE_OBJECT_CRTC); | |
1845 | if (!obj) { | |
58367ed6 | 1846 | DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id); |
f453ba04 DA |
1847 | ret = -EINVAL; |
1848 | goto out; | |
1849 | } | |
1850 | crtc = obj_to_crtc(obj); | |
9440106b | 1851 | DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id); |
f453ba04 DA |
1852 | |
1853 | if (crtc_req->mode_valid) { | |
1854 | /* If we have a mode we need a framebuffer. */ | |
1855 | /* If we pass -1, set the mode with the currently bound fb */ | |
1856 | if (crtc_req->fb_id == -1) { | |
6653cc8d VS |
1857 | if (!crtc->fb) { |
1858 | DRM_DEBUG_KMS("CRTC doesn't have current FB\n"); | |
1859 | ret = -EINVAL; | |
1860 | goto out; | |
f453ba04 | 1861 | } |
6653cc8d | 1862 | fb = crtc->fb; |
f453ba04 DA |
1863 | } else { |
1864 | obj = drm_mode_object_find(dev, crtc_req->fb_id, | |
1865 | DRM_MODE_OBJECT_FB); | |
1866 | if (!obj) { | |
58367ed6 ZY |
1867 | DRM_DEBUG_KMS("Unknown FB ID%d\n", |
1868 | crtc_req->fb_id); | |
f453ba04 DA |
1869 | ret = -EINVAL; |
1870 | goto out; | |
1871 | } | |
1872 | fb = obj_to_fb(obj); | |
1873 | } | |
1874 | ||
1875 | mode = drm_mode_create(dev); | |
ee34ab5b VS |
1876 | if (!mode) { |
1877 | ret = -ENOMEM; | |
1878 | goto out; | |
1879 | } | |
1880 | ||
90367bf6 VS |
1881 | ret = drm_crtc_convert_umode(mode, &crtc_req->mode); |
1882 | if (ret) { | |
1883 | DRM_DEBUG_KMS("Invalid mode\n"); | |
1884 | goto out; | |
1885 | } | |
1886 | ||
f453ba04 | 1887 | drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V); |
5f61bb42 VS |
1888 | |
1889 | if (mode->hdisplay > fb->width || | |
1890 | mode->vdisplay > fb->height || | |
1891 | crtc_req->x > fb->width - mode->hdisplay || | |
1892 | crtc_req->y > fb->height - mode->vdisplay) { | |
1893 | DRM_DEBUG_KMS("Invalid CRTC viewport %ux%u+%u+%u for fb size %ux%u.\n", | |
1894 | mode->hdisplay, mode->vdisplay, | |
1895 | crtc_req->x, crtc_req->y, | |
1896 | fb->width, fb->height); | |
1897 | ret = -ENOSPC; | |
1898 | goto out; | |
1899 | } | |
f453ba04 DA |
1900 | } |
1901 | ||
1902 | if (crtc_req->count_connectors == 0 && mode) { | |
58367ed6 | 1903 | DRM_DEBUG_KMS("Count connectors is 0 but mode set\n"); |
f453ba04 DA |
1904 | ret = -EINVAL; |
1905 | goto out; | |
1906 | } | |
1907 | ||
7781de74 | 1908 | if (crtc_req->count_connectors > 0 && (!mode || !fb)) { |
58367ed6 | 1909 | DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n", |
f453ba04 DA |
1910 | crtc_req->count_connectors); |
1911 | ret = -EINVAL; | |
1912 | goto out; | |
1913 | } | |
1914 | ||
1915 | if (crtc_req->count_connectors > 0) { | |
1916 | u32 out_id; | |
1917 | ||
1918 | /* Avoid unbounded kernel memory allocation */ | |
1919 | if (crtc_req->count_connectors > config->num_connector) { | |
1920 | ret = -EINVAL; | |
1921 | goto out; | |
1922 | } | |
1923 | ||
1924 | connector_set = kmalloc(crtc_req->count_connectors * | |
1925 | sizeof(struct drm_connector *), | |
1926 | GFP_KERNEL); | |
1927 | if (!connector_set) { | |
1928 | ret = -ENOMEM; | |
1929 | goto out; | |
1930 | } | |
1931 | ||
1932 | for (i = 0; i < crtc_req->count_connectors; i++) { | |
81f6c7f8 | 1933 | set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr; |
f453ba04 DA |
1934 | if (get_user(out_id, &set_connectors_ptr[i])) { |
1935 | ret = -EFAULT; | |
1936 | goto out; | |
1937 | } | |
1938 | ||
1939 | obj = drm_mode_object_find(dev, out_id, | |
1940 | DRM_MODE_OBJECT_CONNECTOR); | |
1941 | if (!obj) { | |
58367ed6 ZY |
1942 | DRM_DEBUG_KMS("Connector id %d unknown\n", |
1943 | out_id); | |
f453ba04 DA |
1944 | ret = -EINVAL; |
1945 | goto out; | |
1946 | } | |
1947 | connector = obj_to_connector(obj); | |
9440106b JG |
1948 | DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", |
1949 | connector->base.id, | |
1950 | drm_get_connector_name(connector)); | |
f453ba04 DA |
1951 | |
1952 | connector_set[i] = connector; | |
1953 | } | |
1954 | } | |
1955 | ||
1956 | set.crtc = crtc; | |
1957 | set.x = crtc_req->x; | |
1958 | set.y = crtc_req->y; | |
1959 | set.mode = mode; | |
1960 | set.connectors = connector_set; | |
1961 | set.num_connectors = crtc_req->count_connectors; | |
5ef5f72f | 1962 | set.fb = fb; |
f453ba04 DA |
1963 | ret = crtc->funcs->set_config(&set); |
1964 | ||
1965 | out: | |
1966 | kfree(connector_set); | |
ee34ab5b | 1967 | drm_mode_destroy(dev, mode); |
f453ba04 DA |
1968 | mutex_unlock(&dev->mode_config.mutex); |
1969 | return ret; | |
1970 | } | |
1971 | ||
1972 | int drm_mode_cursor_ioctl(struct drm_device *dev, | |
1973 | void *data, struct drm_file *file_priv) | |
1974 | { | |
1975 | struct drm_mode_cursor *req = data; | |
1976 | struct drm_mode_object *obj; | |
1977 | struct drm_crtc *crtc; | |
1978 | int ret = 0; | |
1979 | ||
fb3b06c8 DA |
1980 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
1981 | return -EINVAL; | |
1982 | ||
acb4b992 | 1983 | if (!req->flags) |
f453ba04 | 1984 | return -EINVAL; |
f453ba04 DA |
1985 | |
1986 | mutex_lock(&dev->mode_config.mutex); | |
e0c8463a | 1987 | obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC); |
f453ba04 | 1988 | if (!obj) { |
58367ed6 | 1989 | DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id); |
f453ba04 DA |
1990 | ret = -EINVAL; |
1991 | goto out; | |
1992 | } | |
1993 | crtc = obj_to_crtc(obj); | |
1994 | ||
1995 | if (req->flags & DRM_MODE_CURSOR_BO) { | |
1996 | if (!crtc->funcs->cursor_set) { | |
f453ba04 DA |
1997 | ret = -ENXIO; |
1998 | goto out; | |
1999 | } | |
2000 | /* Turns off the cursor if handle is 0 */ | |
2001 | ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle, | |
2002 | req->width, req->height); | |
2003 | } | |
2004 | ||
2005 | if (req->flags & DRM_MODE_CURSOR_MOVE) { | |
2006 | if (crtc->funcs->cursor_move) { | |
2007 | ret = crtc->funcs->cursor_move(crtc, req->x, req->y); | |
2008 | } else { | |
f453ba04 DA |
2009 | ret = -EFAULT; |
2010 | goto out; | |
2011 | } | |
2012 | } | |
2013 | out: | |
2014 | mutex_unlock(&dev->mode_config.mutex); | |
2015 | return ret; | |
2016 | } | |
2017 | ||
308e5bcb JB |
2018 | /* Original addfb only supported RGB formats, so figure out which one */ |
2019 | uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth) | |
2020 | { | |
2021 | uint32_t fmt; | |
2022 | ||
2023 | switch (bpp) { | |
2024 | case 8: | |
04b3924d | 2025 | fmt = DRM_FORMAT_RGB332; |
308e5bcb JB |
2026 | break; |
2027 | case 16: | |
2028 | if (depth == 15) | |
04b3924d | 2029 | fmt = DRM_FORMAT_XRGB1555; |
308e5bcb | 2030 | else |
04b3924d | 2031 | fmt = DRM_FORMAT_RGB565; |
308e5bcb JB |
2032 | break; |
2033 | case 24: | |
04b3924d | 2034 | fmt = DRM_FORMAT_RGB888; |
308e5bcb JB |
2035 | break; |
2036 | case 32: | |
2037 | if (depth == 24) | |
04b3924d | 2038 | fmt = DRM_FORMAT_XRGB8888; |
308e5bcb | 2039 | else if (depth == 30) |
04b3924d | 2040 | fmt = DRM_FORMAT_XRGB2101010; |
308e5bcb | 2041 | else |
04b3924d | 2042 | fmt = DRM_FORMAT_ARGB8888; |
308e5bcb JB |
2043 | break; |
2044 | default: | |
04b3924d VS |
2045 | DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n"); |
2046 | fmt = DRM_FORMAT_XRGB8888; | |
308e5bcb JB |
2047 | break; |
2048 | } | |
2049 | ||
2050 | return fmt; | |
2051 | } | |
2052 | EXPORT_SYMBOL(drm_mode_legacy_fb_format); | |
2053 | ||
f453ba04 DA |
2054 | /** |
2055 | * drm_mode_addfb - add an FB to the graphics configuration | |
2056 | * @inode: inode from the ioctl | |
2057 | * @filp: file * from the ioctl | |
2058 | * @cmd: cmd from ioctl | |
2059 | * @arg: arg from ioctl | |
2060 | * | |
2061 | * LOCKING: | |
2062 | * Takes mode config lock. | |
2063 | * | |
2064 | * Add a new FB to the specified CRTC, given a user request. | |
2065 | * | |
2066 | * Called by the user via ioctl. | |
2067 | * | |
2068 | * RETURNS: | |
2069 | * Zero on success, errno on failure. | |
2070 | */ | |
2071 | int drm_mode_addfb(struct drm_device *dev, | |
2072 | void *data, struct drm_file *file_priv) | |
2073 | { | |
308e5bcb JB |
2074 | struct drm_mode_fb_cmd *or = data; |
2075 | struct drm_mode_fb_cmd2 r = {}; | |
2076 | struct drm_mode_config *config = &dev->mode_config; | |
2077 | struct drm_framebuffer *fb; | |
2078 | int ret = 0; | |
2079 | ||
2080 | /* Use new struct with format internally */ | |
2081 | r.fb_id = or->fb_id; | |
2082 | r.width = or->width; | |
2083 | r.height = or->height; | |
2084 | r.pitches[0] = or->pitch; | |
2085 | r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth); | |
2086 | r.handles[0] = or->handle; | |
2087 | ||
2088 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) | |
2089 | return -EINVAL; | |
2090 | ||
acb4b992 | 2091 | if ((config->min_width > r.width) || (r.width > config->max_width)) |
308e5bcb | 2092 | return -EINVAL; |
acb4b992 JB |
2093 | |
2094 | if ((config->min_height > r.height) || (r.height > config->max_height)) | |
308e5bcb | 2095 | return -EINVAL; |
308e5bcb JB |
2096 | |
2097 | mutex_lock(&dev->mode_config.mutex); | |
2098 | ||
2099 | /* TODO check buffer is sufficiently large */ | |
2100 | /* TODO setup destructor callback */ | |
2101 | ||
2102 | fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r); | |
2103 | if (IS_ERR(fb)) { | |
1aa1b11c | 2104 | DRM_DEBUG_KMS("could not create framebuffer\n"); |
308e5bcb JB |
2105 | ret = PTR_ERR(fb); |
2106 | goto out; | |
2107 | } | |
2108 | ||
2109 | or->fb_id = fb->base.id; | |
2110 | list_add(&fb->filp_head, &file_priv->fbs); | |
2111 | DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id); | |
2112 | ||
2113 | out: | |
2114 | mutex_unlock(&dev->mode_config.mutex); | |
2115 | return ret; | |
2116 | } | |
2117 | ||
935b5977 VS |
2118 | static int format_check(struct drm_mode_fb_cmd2 *r) |
2119 | { | |
2120 | uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN; | |
2121 | ||
2122 | switch (format) { | |
2123 | case DRM_FORMAT_C8: | |
2124 | case DRM_FORMAT_RGB332: | |
2125 | case DRM_FORMAT_BGR233: | |
2126 | case DRM_FORMAT_XRGB4444: | |
2127 | case DRM_FORMAT_XBGR4444: | |
2128 | case DRM_FORMAT_RGBX4444: | |
2129 | case DRM_FORMAT_BGRX4444: | |
2130 | case DRM_FORMAT_ARGB4444: | |
2131 | case DRM_FORMAT_ABGR4444: | |
2132 | case DRM_FORMAT_RGBA4444: | |
2133 | case DRM_FORMAT_BGRA4444: | |
2134 | case DRM_FORMAT_XRGB1555: | |
2135 | case DRM_FORMAT_XBGR1555: | |
2136 | case DRM_FORMAT_RGBX5551: | |
2137 | case DRM_FORMAT_BGRX5551: | |
2138 | case DRM_FORMAT_ARGB1555: | |
2139 | case DRM_FORMAT_ABGR1555: | |
2140 | case DRM_FORMAT_RGBA5551: | |
2141 | case DRM_FORMAT_BGRA5551: | |
2142 | case DRM_FORMAT_RGB565: | |
2143 | case DRM_FORMAT_BGR565: | |
2144 | case DRM_FORMAT_RGB888: | |
2145 | case DRM_FORMAT_BGR888: | |
2146 | case DRM_FORMAT_XRGB8888: | |
2147 | case DRM_FORMAT_XBGR8888: | |
2148 | case DRM_FORMAT_RGBX8888: | |
2149 | case DRM_FORMAT_BGRX8888: | |
2150 | case DRM_FORMAT_ARGB8888: | |
2151 | case DRM_FORMAT_ABGR8888: | |
2152 | case DRM_FORMAT_RGBA8888: | |
2153 | case DRM_FORMAT_BGRA8888: | |
2154 | case DRM_FORMAT_XRGB2101010: | |
2155 | case DRM_FORMAT_XBGR2101010: | |
2156 | case DRM_FORMAT_RGBX1010102: | |
2157 | case DRM_FORMAT_BGRX1010102: | |
2158 | case DRM_FORMAT_ARGB2101010: | |
2159 | case DRM_FORMAT_ABGR2101010: | |
2160 | case DRM_FORMAT_RGBA1010102: | |
2161 | case DRM_FORMAT_BGRA1010102: | |
2162 | case DRM_FORMAT_YUYV: | |
2163 | case DRM_FORMAT_YVYU: | |
2164 | case DRM_FORMAT_UYVY: | |
2165 | case DRM_FORMAT_VYUY: | |
2166 | case DRM_FORMAT_AYUV: | |
2167 | case DRM_FORMAT_NV12: | |
2168 | case DRM_FORMAT_NV21: | |
2169 | case DRM_FORMAT_NV16: | |
2170 | case DRM_FORMAT_NV61: | |
2171 | case DRM_FORMAT_YUV410: | |
2172 | case DRM_FORMAT_YVU410: | |
2173 | case DRM_FORMAT_YUV411: | |
2174 | case DRM_FORMAT_YVU411: | |
2175 | case DRM_FORMAT_YUV420: | |
2176 | case DRM_FORMAT_YVU420: | |
2177 | case DRM_FORMAT_YUV422: | |
2178 | case DRM_FORMAT_YVU422: | |
2179 | case DRM_FORMAT_YUV444: | |
2180 | case DRM_FORMAT_YVU444: | |
2181 | return 0; | |
2182 | default: | |
2183 | return -EINVAL; | |
2184 | } | |
2185 | } | |
2186 | ||
d1b45d5f VS |
2187 | static int framebuffer_check(struct drm_mode_fb_cmd2 *r) |
2188 | { | |
2189 | int ret, hsub, vsub, num_planes, i; | |
2190 | ||
2191 | ret = format_check(r); | |
2192 | if (ret) { | |
1aa1b11c | 2193 | DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format); |
d1b45d5f VS |
2194 | return ret; |
2195 | } | |
2196 | ||
2197 | hsub = drm_format_horz_chroma_subsampling(r->pixel_format); | |
2198 | vsub = drm_format_vert_chroma_subsampling(r->pixel_format); | |
2199 | num_planes = drm_format_num_planes(r->pixel_format); | |
2200 | ||
2201 | if (r->width == 0 || r->width % hsub) { | |
1aa1b11c | 2202 | DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height); |
d1b45d5f VS |
2203 | return -EINVAL; |
2204 | } | |
2205 | ||
2206 | if (r->height == 0 || r->height % vsub) { | |
1aa1b11c | 2207 | DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height); |
d1b45d5f VS |
2208 | return -EINVAL; |
2209 | } | |
2210 | ||
2211 | for (i = 0; i < num_planes; i++) { | |
2212 | unsigned int width = r->width / (i != 0 ? hsub : 1); | |
2213 | ||
2214 | if (!r->handles[i]) { | |
1aa1b11c | 2215 | DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i); |
d1b45d5f VS |
2216 | return -EINVAL; |
2217 | } | |
2218 | ||
2219 | if (r->pitches[i] < drm_format_plane_cpp(r->pixel_format, i) * width) { | |
1aa1b11c | 2220 | DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i); |
d1b45d5f VS |
2221 | return -EINVAL; |
2222 | } | |
2223 | } | |
2224 | ||
2225 | return 0; | |
2226 | } | |
2227 | ||
308e5bcb JB |
2228 | /** |
2229 | * drm_mode_addfb2 - add an FB to the graphics configuration | |
2230 | * @inode: inode from the ioctl | |
2231 | * @filp: file * from the ioctl | |
2232 | * @cmd: cmd from ioctl | |
2233 | * @arg: arg from ioctl | |
2234 | * | |
2235 | * LOCKING: | |
2236 | * Takes mode config lock. | |
2237 | * | |
2238 | * Add a new FB to the specified CRTC, given a user request with format. | |
2239 | * | |
2240 | * Called by the user via ioctl. | |
2241 | * | |
2242 | * RETURNS: | |
2243 | * Zero on success, errno on failure. | |
2244 | */ | |
2245 | int drm_mode_addfb2(struct drm_device *dev, | |
2246 | void *data, struct drm_file *file_priv) | |
2247 | { | |
2248 | struct drm_mode_fb_cmd2 *r = data; | |
f453ba04 DA |
2249 | struct drm_mode_config *config = &dev->mode_config; |
2250 | struct drm_framebuffer *fb; | |
4a1b0714 | 2251 | int ret; |
f453ba04 | 2252 | |
fb3b06c8 DA |
2253 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
2254 | return -EINVAL; | |
2255 | ||
f453ba04 | 2256 | if ((config->min_width > r->width) || (r->width > config->max_width)) { |
1aa1b11c | 2257 | DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n", |
8cf5c917 | 2258 | r->width, config->min_width, config->max_width); |
f453ba04 DA |
2259 | return -EINVAL; |
2260 | } | |
2261 | if ((config->min_height > r->height) || (r->height > config->max_height)) { | |
1aa1b11c | 2262 | DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n", |
8cf5c917 | 2263 | r->height, config->min_height, config->max_height); |
f453ba04 DA |
2264 | return -EINVAL; |
2265 | } | |
2266 | ||
d1b45d5f VS |
2267 | ret = framebuffer_check(r); |
2268 | if (ret) | |
935b5977 | 2269 | return ret; |
935b5977 | 2270 | |
f453ba04 DA |
2271 | mutex_lock(&dev->mode_config.mutex); |
2272 | ||
f453ba04 | 2273 | fb = dev->mode_config.funcs->fb_create(dev, file_priv, r); |
cce13ff7 | 2274 | if (IS_ERR(fb)) { |
1aa1b11c | 2275 | DRM_DEBUG_KMS("could not create framebuffer\n"); |
cce13ff7 | 2276 | ret = PTR_ERR(fb); |
f453ba04 DA |
2277 | goto out; |
2278 | } | |
2279 | ||
e0c8463a | 2280 | r->fb_id = fb->base.id; |
f453ba04 | 2281 | list_add(&fb->filp_head, &file_priv->fbs); |
9440106b | 2282 | DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id); |
f453ba04 DA |
2283 | |
2284 | out: | |
2285 | mutex_unlock(&dev->mode_config.mutex); | |
2286 | return ret; | |
2287 | } | |
2288 | ||
2289 | /** | |
2290 | * drm_mode_rmfb - remove an FB from the configuration | |
2291 | * @inode: inode from the ioctl | |
2292 | * @filp: file * from the ioctl | |
2293 | * @cmd: cmd from ioctl | |
2294 | * @arg: arg from ioctl | |
2295 | * | |
2296 | * LOCKING: | |
2297 | * Takes mode config lock. | |
2298 | * | |
2299 | * Remove the FB specified by the user. | |
2300 | * | |
2301 | * Called by the user via ioctl. | |
2302 | * | |
2303 | * RETURNS: | |
2304 | * Zero on success, errno on failure. | |
2305 | */ | |
2306 | int drm_mode_rmfb(struct drm_device *dev, | |
2307 | void *data, struct drm_file *file_priv) | |
2308 | { | |
2309 | struct drm_mode_object *obj; | |
2310 | struct drm_framebuffer *fb = NULL; | |
2311 | struct drm_framebuffer *fbl = NULL; | |
2312 | uint32_t *id = data; | |
2313 | int ret = 0; | |
2314 | int found = 0; | |
2315 | ||
fb3b06c8 DA |
2316 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
2317 | return -EINVAL; | |
2318 | ||
f453ba04 DA |
2319 | mutex_lock(&dev->mode_config.mutex); |
2320 | obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB); | |
25985edc | 2321 | /* TODO check that we really get a framebuffer back. */ |
f453ba04 | 2322 | if (!obj) { |
f453ba04 DA |
2323 | ret = -EINVAL; |
2324 | goto out; | |
2325 | } | |
2326 | fb = obj_to_fb(obj); | |
2327 | ||
2328 | list_for_each_entry(fbl, &file_priv->fbs, filp_head) | |
2329 | if (fb == fbl) | |
2330 | found = 1; | |
2331 | ||
2332 | if (!found) { | |
f453ba04 DA |
2333 | ret = -EINVAL; |
2334 | goto out; | |
2335 | } | |
2336 | ||
2337 | /* TODO release all crtc connected to the framebuffer */ | |
2338 | /* TODO unhock the destructor from the buffer object */ | |
2339 | ||
2340 | list_del(&fb->filp_head); | |
2341 | fb->funcs->destroy(fb); | |
2342 | ||
2343 | out: | |
2344 | mutex_unlock(&dev->mode_config.mutex); | |
2345 | return ret; | |
2346 | } | |
2347 | ||
2348 | /** | |
2349 | * drm_mode_getfb - get FB info | |
2350 | * @inode: inode from the ioctl | |
2351 | * @filp: file * from the ioctl | |
2352 | * @cmd: cmd from ioctl | |
2353 | * @arg: arg from ioctl | |
2354 | * | |
2355 | * LOCKING: | |
b20f3867 | 2356 | * Takes mode config lock. |
f453ba04 DA |
2357 | * |
2358 | * Lookup the FB given its ID and return info about it. | |
2359 | * | |
2360 | * Called by the user via ioctl. | |
2361 | * | |
2362 | * RETURNS: | |
2363 | * Zero on success, errno on failure. | |
2364 | */ | |
2365 | int drm_mode_getfb(struct drm_device *dev, | |
2366 | void *data, struct drm_file *file_priv) | |
2367 | { | |
2368 | struct drm_mode_fb_cmd *r = data; | |
2369 | struct drm_mode_object *obj; | |
2370 | struct drm_framebuffer *fb; | |
2371 | int ret = 0; | |
2372 | ||
fb3b06c8 DA |
2373 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
2374 | return -EINVAL; | |
2375 | ||
f453ba04 | 2376 | mutex_lock(&dev->mode_config.mutex); |
e0c8463a | 2377 | obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB); |
f453ba04 | 2378 | if (!obj) { |
f453ba04 DA |
2379 | ret = -EINVAL; |
2380 | goto out; | |
2381 | } | |
2382 | fb = obj_to_fb(obj); | |
2383 | ||
2384 | r->height = fb->height; | |
2385 | r->width = fb->width; | |
2386 | r->depth = fb->depth; | |
2387 | r->bpp = fb->bits_per_pixel; | |
01f2c773 | 2388 | r->pitch = fb->pitches[0]; |
f453ba04 DA |
2389 | fb->funcs->create_handle(fb, file_priv, &r->handle); |
2390 | ||
2391 | out: | |
2392 | mutex_unlock(&dev->mode_config.mutex); | |
2393 | return ret; | |
2394 | } | |
2395 | ||
884840aa JB |
2396 | int drm_mode_dirtyfb_ioctl(struct drm_device *dev, |
2397 | void *data, struct drm_file *file_priv) | |
2398 | { | |
2399 | struct drm_clip_rect __user *clips_ptr; | |
2400 | struct drm_clip_rect *clips = NULL; | |
2401 | struct drm_mode_fb_dirty_cmd *r = data; | |
2402 | struct drm_mode_object *obj; | |
2403 | struct drm_framebuffer *fb; | |
2404 | unsigned flags; | |
2405 | int num_clips; | |
4a1b0714 | 2406 | int ret; |
884840aa | 2407 | |
fb3b06c8 DA |
2408 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
2409 | return -EINVAL; | |
2410 | ||
884840aa JB |
2411 | mutex_lock(&dev->mode_config.mutex); |
2412 | obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB); | |
2413 | if (!obj) { | |
884840aa JB |
2414 | ret = -EINVAL; |
2415 | goto out_err1; | |
2416 | } | |
2417 | fb = obj_to_fb(obj); | |
2418 | ||
2419 | num_clips = r->num_clips; | |
81f6c7f8 | 2420 | clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr; |
884840aa JB |
2421 | |
2422 | if (!num_clips != !clips_ptr) { | |
2423 | ret = -EINVAL; | |
2424 | goto out_err1; | |
2425 | } | |
2426 | ||
2427 | flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags; | |
2428 | ||
2429 | /* If userspace annotates copy, clips must come in pairs */ | |
2430 | if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) { | |
2431 | ret = -EINVAL; | |
2432 | goto out_err1; | |
2433 | } | |
2434 | ||
2435 | if (num_clips && clips_ptr) { | |
a5cd3351 XW |
2436 | if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) { |
2437 | ret = -EINVAL; | |
2438 | goto out_err1; | |
2439 | } | |
884840aa JB |
2440 | clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL); |
2441 | if (!clips) { | |
2442 | ret = -ENOMEM; | |
2443 | goto out_err1; | |
2444 | } | |
2445 | ||
2446 | ret = copy_from_user(clips, clips_ptr, | |
2447 | num_clips * sizeof(*clips)); | |
e902a358 DC |
2448 | if (ret) { |
2449 | ret = -EFAULT; | |
884840aa | 2450 | goto out_err2; |
e902a358 | 2451 | } |
884840aa JB |
2452 | } |
2453 | ||
2454 | if (fb->funcs->dirty) { | |
02b00162 TH |
2455 | ret = fb->funcs->dirty(fb, file_priv, flags, r->color, |
2456 | clips, num_clips); | |
884840aa JB |
2457 | } else { |
2458 | ret = -ENOSYS; | |
2459 | goto out_err2; | |
2460 | } | |
2461 | ||
2462 | out_err2: | |
2463 | kfree(clips); | |
2464 | out_err1: | |
2465 | mutex_unlock(&dev->mode_config.mutex); | |
2466 | return ret; | |
2467 | } | |
2468 | ||
2469 | ||
f453ba04 DA |
2470 | /** |
2471 | * drm_fb_release - remove and free the FBs on this file | |
2472 | * @filp: file * from the ioctl | |
2473 | * | |
2474 | * LOCKING: | |
2475 | * Takes mode config lock. | |
2476 | * | |
2477 | * Destroy all the FBs associated with @filp. | |
2478 | * | |
2479 | * Called by the user via ioctl. | |
2480 | * | |
2481 | * RETURNS: | |
2482 | * Zero on success, errno on failure. | |
2483 | */ | |
ea39f835 | 2484 | void drm_fb_release(struct drm_file *priv) |
f453ba04 | 2485 | { |
f453ba04 DA |
2486 | struct drm_device *dev = priv->minor->dev; |
2487 | struct drm_framebuffer *fb, *tfb; | |
2488 | ||
2489 | mutex_lock(&dev->mode_config.mutex); | |
2490 | list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) { | |
2491 | list_del(&fb->filp_head); | |
2492 | fb->funcs->destroy(fb); | |
2493 | } | |
2494 | mutex_unlock(&dev->mode_config.mutex); | |
2495 | } | |
2496 | ||
2497 | /** | |
2498 | * drm_mode_attachmode - add a mode to the user mode list | |
2499 | * @dev: DRM device | |
2500 | * @connector: connector to add the mode to | |
2501 | * @mode: mode to add | |
2502 | * | |
2503 | * Add @mode to @connector's user mode list. | |
2504 | */ | |
1dd6c8bd VS |
2505 | static void drm_mode_attachmode(struct drm_device *dev, |
2506 | struct drm_connector *connector, | |
2507 | struct drm_display_mode *mode) | |
f453ba04 | 2508 | { |
f453ba04 | 2509 | list_add_tail(&mode->head, &connector->user_modes); |
f453ba04 DA |
2510 | } |
2511 | ||
2512 | int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc, | |
ac235daf | 2513 | const struct drm_display_mode *mode) |
f453ba04 DA |
2514 | { |
2515 | struct drm_connector *connector; | |
ac235daf VS |
2516 | int ret = 0; |
2517 | struct drm_display_mode *dup_mode, *next; | |
2518 | LIST_HEAD(list); | |
2519 | ||
f453ba04 DA |
2520 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
2521 | if (!connector->encoder) | |
ac235daf | 2522 | continue; |
f453ba04 | 2523 | if (connector->encoder->crtc == crtc) { |
ac235daf VS |
2524 | dup_mode = drm_mode_duplicate(dev, mode); |
2525 | if (!dup_mode) { | |
2526 | ret = -ENOMEM; | |
2527 | goto out; | |
2528 | } | |
2529 | list_add_tail(&dup_mode->head, &list); | |
f453ba04 DA |
2530 | } |
2531 | } | |
ac235daf VS |
2532 | |
2533 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { | |
2534 | if (!connector->encoder) | |
2535 | continue; | |
2536 | if (connector->encoder->crtc == crtc) | |
2537 | list_move_tail(list.next, &connector->user_modes); | |
2538 | } | |
2539 | ||
2540 | WARN_ON(!list_empty(&list)); | |
2541 | ||
2542 | out: | |
2543 | list_for_each_entry_safe(dup_mode, next, &list, head) | |
2544 | drm_mode_destroy(dev, dup_mode); | |
2545 | ||
2546 | return ret; | |
f453ba04 DA |
2547 | } |
2548 | EXPORT_SYMBOL(drm_mode_attachmode_crtc); | |
2549 | ||
2550 | static int drm_mode_detachmode(struct drm_device *dev, | |
2551 | struct drm_connector *connector, | |
2552 | struct drm_display_mode *mode) | |
2553 | { | |
2554 | int found = 0; | |
2555 | int ret = 0; | |
2556 | struct drm_display_mode *match_mode, *t; | |
2557 | ||
2558 | list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) { | |
2559 | if (drm_mode_equal(match_mode, mode)) { | |
2560 | list_del(&match_mode->head); | |
2561 | drm_mode_destroy(dev, match_mode); | |
2562 | found = 1; | |
2563 | break; | |
2564 | } | |
2565 | } | |
2566 | ||
2567 | if (!found) | |
2568 | ret = -EINVAL; | |
2569 | ||
2570 | return ret; | |
2571 | } | |
2572 | ||
2573 | int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode) | |
2574 | { | |
2575 | struct drm_connector *connector; | |
2576 | ||
2577 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { | |
2578 | drm_mode_detachmode(dev, connector, mode); | |
2579 | } | |
2580 | return 0; | |
2581 | } | |
2582 | EXPORT_SYMBOL(drm_mode_detachmode_crtc); | |
2583 | ||
2584 | /** | |
2585 | * drm_fb_attachmode - Attach a user mode to an connector | |
2586 | * @inode: inode from the ioctl | |
2587 | * @filp: file * from the ioctl | |
2588 | * @cmd: cmd from ioctl | |
2589 | * @arg: arg from ioctl | |
2590 | * | |
2591 | * This attaches a user specified mode to an connector. | |
2592 | * Called by the user via ioctl. | |
2593 | * | |
2594 | * RETURNS: | |
2595 | * Zero on success, errno on failure. | |
2596 | */ | |
2597 | int drm_mode_attachmode_ioctl(struct drm_device *dev, | |
2598 | void *data, struct drm_file *file_priv) | |
2599 | { | |
2600 | struct drm_mode_mode_cmd *mode_cmd = data; | |
2601 | struct drm_connector *connector; | |
2602 | struct drm_display_mode *mode; | |
2603 | struct drm_mode_object *obj; | |
2604 | struct drm_mode_modeinfo *umode = &mode_cmd->mode; | |
4a1b0714 | 2605 | int ret; |
f453ba04 | 2606 | |
fb3b06c8 DA |
2607 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
2608 | return -EINVAL; | |
2609 | ||
f453ba04 DA |
2610 | mutex_lock(&dev->mode_config.mutex); |
2611 | ||
2612 | obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR); | |
2613 | if (!obj) { | |
2614 | ret = -EINVAL; | |
2615 | goto out; | |
2616 | } | |
2617 | connector = obj_to_connector(obj); | |
2618 | ||
2619 | mode = drm_mode_create(dev); | |
2620 | if (!mode) { | |
2621 | ret = -ENOMEM; | |
2622 | goto out; | |
2623 | } | |
2624 | ||
90367bf6 VS |
2625 | ret = drm_crtc_convert_umode(mode, umode); |
2626 | if (ret) { | |
2627 | DRM_DEBUG_KMS("Invalid mode\n"); | |
2628 | drm_mode_destroy(dev, mode); | |
2629 | goto out; | |
2630 | } | |
f453ba04 | 2631 | |
1dd6c8bd | 2632 | drm_mode_attachmode(dev, connector, mode); |
f453ba04 DA |
2633 | out: |
2634 | mutex_unlock(&dev->mode_config.mutex); | |
2635 | return ret; | |
2636 | } | |
2637 | ||
2638 | ||
2639 | /** | |
2640 | * drm_fb_detachmode - Detach a user specified mode from an connector | |
2641 | * @inode: inode from the ioctl | |
2642 | * @filp: file * from the ioctl | |
2643 | * @cmd: cmd from ioctl | |
2644 | * @arg: arg from ioctl | |
2645 | * | |
2646 | * Called by the user via ioctl. | |
2647 | * | |
2648 | * RETURNS: | |
2649 | * Zero on success, errno on failure. | |
2650 | */ | |
2651 | int drm_mode_detachmode_ioctl(struct drm_device *dev, | |
2652 | void *data, struct drm_file *file_priv) | |
2653 | { | |
2654 | struct drm_mode_object *obj; | |
2655 | struct drm_mode_mode_cmd *mode_cmd = data; | |
2656 | struct drm_connector *connector; | |
2657 | struct drm_display_mode mode; | |
2658 | struct drm_mode_modeinfo *umode = &mode_cmd->mode; | |
4a1b0714 | 2659 | int ret; |
f453ba04 | 2660 | |
fb3b06c8 DA |
2661 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
2662 | return -EINVAL; | |
2663 | ||
f453ba04 DA |
2664 | mutex_lock(&dev->mode_config.mutex); |
2665 | ||
2666 | obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR); | |
2667 | if (!obj) { | |
2668 | ret = -EINVAL; | |
2669 | goto out; | |
2670 | } | |
2671 | connector = obj_to_connector(obj); | |
2672 | ||
90367bf6 VS |
2673 | ret = drm_crtc_convert_umode(&mode, umode); |
2674 | if (ret) { | |
2675 | DRM_DEBUG_KMS("Invalid mode\n"); | |
2676 | goto out; | |
2677 | } | |
2678 | ||
f453ba04 DA |
2679 | ret = drm_mode_detachmode(dev, connector, &mode); |
2680 | out: | |
2681 | mutex_unlock(&dev->mode_config.mutex); | |
2682 | return ret; | |
2683 | } | |
2684 | ||
2685 | struct drm_property *drm_property_create(struct drm_device *dev, int flags, | |
2686 | const char *name, int num_values) | |
2687 | { | |
2688 | struct drm_property *property = NULL; | |
6bfc56aa | 2689 | int ret; |
f453ba04 DA |
2690 | |
2691 | property = kzalloc(sizeof(struct drm_property), GFP_KERNEL); | |
2692 | if (!property) | |
2693 | return NULL; | |
2694 | ||
2695 | if (num_values) { | |
2696 | property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL); | |
2697 | if (!property->values) | |
2698 | goto fail; | |
2699 | } | |
2700 | ||
6bfc56aa VS |
2701 | ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY); |
2702 | if (ret) | |
2703 | goto fail; | |
2704 | ||
f453ba04 DA |
2705 | property->flags = flags; |
2706 | property->num_values = num_values; | |
2707 | INIT_LIST_HEAD(&property->enum_blob_list); | |
2708 | ||
471dd2ef | 2709 | if (name) { |
f453ba04 | 2710 | strncpy(property->name, name, DRM_PROP_NAME_LEN); |
471dd2ef VL |
2711 | property->name[DRM_PROP_NAME_LEN-1] = '\0'; |
2712 | } | |
f453ba04 DA |
2713 | |
2714 | list_add_tail(&property->head, &dev->mode_config.property_list); | |
2715 | return property; | |
2716 | fail: | |
6bfc56aa | 2717 | kfree(property->values); |
f453ba04 DA |
2718 | kfree(property); |
2719 | return NULL; | |
2720 | } | |
2721 | EXPORT_SYMBOL(drm_property_create); | |
2722 | ||
4a67d391 SH |
2723 | struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags, |
2724 | const char *name, | |
2725 | const struct drm_prop_enum_list *props, | |
2726 | int num_values) | |
2727 | { | |
2728 | struct drm_property *property; | |
2729 | int i, ret; | |
2730 | ||
2731 | flags |= DRM_MODE_PROP_ENUM; | |
2732 | ||
2733 | property = drm_property_create(dev, flags, name, num_values); | |
2734 | if (!property) | |
2735 | return NULL; | |
2736 | ||
2737 | for (i = 0; i < num_values; i++) { | |
2738 | ret = drm_property_add_enum(property, i, | |
2739 | props[i].type, | |
2740 | props[i].name); | |
2741 | if (ret) { | |
2742 | drm_property_destroy(dev, property); | |
2743 | return NULL; | |
2744 | } | |
2745 | } | |
2746 | ||
2747 | return property; | |
2748 | } | |
2749 | EXPORT_SYMBOL(drm_property_create_enum); | |
2750 | ||
d9bc3c02 SH |
2751 | struct drm_property *drm_property_create_range(struct drm_device *dev, int flags, |
2752 | const char *name, | |
2753 | uint64_t min, uint64_t max) | |
2754 | { | |
2755 | struct drm_property *property; | |
2756 | ||
2757 | flags |= DRM_MODE_PROP_RANGE; | |
2758 | ||
2759 | property = drm_property_create(dev, flags, name, 2); | |
2760 | if (!property) | |
2761 | return NULL; | |
2762 | ||
2763 | property->values[0] = min; | |
2764 | property->values[1] = max; | |
2765 | ||
2766 | return property; | |
2767 | } | |
2768 | EXPORT_SYMBOL(drm_property_create_range); | |
2769 | ||
f453ba04 DA |
2770 | int drm_property_add_enum(struct drm_property *property, int index, |
2771 | uint64_t value, const char *name) | |
2772 | { | |
2773 | struct drm_property_enum *prop_enum; | |
2774 | ||
2775 | if (!(property->flags & DRM_MODE_PROP_ENUM)) | |
2776 | return -EINVAL; | |
2777 | ||
2778 | if (!list_empty(&property->enum_blob_list)) { | |
2779 | list_for_each_entry(prop_enum, &property->enum_blob_list, head) { | |
2780 | if (prop_enum->value == value) { | |
2781 | strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN); | |
2782 | prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0'; | |
2783 | return 0; | |
2784 | } | |
2785 | } | |
2786 | } | |
2787 | ||
2788 | prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL); | |
2789 | if (!prop_enum) | |
2790 | return -ENOMEM; | |
2791 | ||
2792 | strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN); | |
2793 | prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0'; | |
2794 | prop_enum->value = value; | |
2795 | ||
2796 | property->values[index] = value; | |
2797 | list_add_tail(&prop_enum->head, &property->enum_blob_list); | |
2798 | return 0; | |
2799 | } | |
2800 | EXPORT_SYMBOL(drm_property_add_enum); | |
2801 | ||
2802 | void drm_property_destroy(struct drm_device *dev, struct drm_property *property) | |
2803 | { | |
2804 | struct drm_property_enum *prop_enum, *pt; | |
2805 | ||
2806 | list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) { | |
2807 | list_del(&prop_enum->head); | |
2808 | kfree(prop_enum); | |
2809 | } | |
2810 | ||
2811 | if (property->num_values) | |
2812 | kfree(property->values); | |
2813 | drm_mode_object_put(dev, &property->base); | |
2814 | list_del(&property->head); | |
2815 | kfree(property); | |
2816 | } | |
2817 | EXPORT_SYMBOL(drm_property_destroy); | |
2818 | ||
afea2ad5 | 2819 | void drm_connector_attach_property(struct drm_connector *connector, |
f453ba04 DA |
2820 | struct drm_property *property, uint64_t init_val) |
2821 | { | |
0057d8dd | 2822 | drm_object_attach_property(&connector->base, property, init_val); |
f453ba04 DA |
2823 | } |
2824 | EXPORT_SYMBOL(drm_connector_attach_property); | |
2825 | ||
2826 | int drm_connector_property_set_value(struct drm_connector *connector, | |
2827 | struct drm_property *property, uint64_t value) | |
2828 | { | |
0057d8dd | 2829 | return drm_object_property_set_value(&connector->base, property, value); |
f453ba04 DA |
2830 | } |
2831 | EXPORT_SYMBOL(drm_connector_property_set_value); | |
2832 | ||
2833 | int drm_connector_property_get_value(struct drm_connector *connector, | |
2834 | struct drm_property *property, uint64_t *val) | |
2835 | { | |
0057d8dd | 2836 | return drm_object_property_get_value(&connector->base, property, val); |
f453ba04 DA |
2837 | } |
2838 | EXPORT_SYMBOL(drm_connector_property_get_value); | |
2839 | ||
c543188a PZ |
2840 | void drm_object_attach_property(struct drm_mode_object *obj, |
2841 | struct drm_property *property, | |
2842 | uint64_t init_val) | |
2843 | { | |
7f88a9be | 2844 | int count = obj->properties->count; |
c543188a | 2845 | |
7f88a9be PZ |
2846 | if (count == DRM_OBJECT_MAX_PROPERTY) { |
2847 | WARN(1, "Failed to attach object property (type: 0x%x). Please " | |
2848 | "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time " | |
2849 | "you see this message on the same object type.\n", | |
2850 | obj->type); | |
2851 | return; | |
c543188a PZ |
2852 | } |
2853 | ||
7f88a9be PZ |
2854 | obj->properties->ids[count] = property->base.id; |
2855 | obj->properties->values[count] = init_val; | |
2856 | obj->properties->count++; | |
c543188a PZ |
2857 | } |
2858 | EXPORT_SYMBOL(drm_object_attach_property); | |
2859 | ||
2860 | int drm_object_property_set_value(struct drm_mode_object *obj, | |
2861 | struct drm_property *property, uint64_t val) | |
2862 | { | |
2863 | int i; | |
2864 | ||
7f88a9be | 2865 | for (i = 0; i < obj->properties->count; i++) { |
c543188a PZ |
2866 | if (obj->properties->ids[i] == property->base.id) { |
2867 | obj->properties->values[i] = val; | |
2868 | return 0; | |
2869 | } | |
2870 | } | |
2871 | ||
2872 | return -EINVAL; | |
2873 | } | |
2874 | EXPORT_SYMBOL(drm_object_property_set_value); | |
2875 | ||
2876 | int drm_object_property_get_value(struct drm_mode_object *obj, | |
2877 | struct drm_property *property, uint64_t *val) | |
2878 | { | |
2879 | int i; | |
2880 | ||
7f88a9be | 2881 | for (i = 0; i < obj->properties->count; i++) { |
c543188a PZ |
2882 | if (obj->properties->ids[i] == property->base.id) { |
2883 | *val = obj->properties->values[i]; | |
2884 | return 0; | |
2885 | } | |
2886 | } | |
2887 | ||
2888 | return -EINVAL; | |
2889 | } | |
2890 | EXPORT_SYMBOL(drm_object_property_get_value); | |
2891 | ||
f453ba04 DA |
2892 | int drm_mode_getproperty_ioctl(struct drm_device *dev, |
2893 | void *data, struct drm_file *file_priv) | |
2894 | { | |
2895 | struct drm_mode_object *obj; | |
2896 | struct drm_mode_get_property *out_resp = data; | |
2897 | struct drm_property *property; | |
2898 | int enum_count = 0; | |
2899 | int blob_count = 0; | |
2900 | int value_count = 0; | |
2901 | int ret = 0, i; | |
2902 | int copied; | |
2903 | struct drm_property_enum *prop_enum; | |
2904 | struct drm_mode_property_enum __user *enum_ptr; | |
2905 | struct drm_property_blob *prop_blob; | |
81f6c7f8 | 2906 | uint32_t __user *blob_id_ptr; |
f453ba04 DA |
2907 | uint64_t __user *values_ptr; |
2908 | uint32_t __user *blob_length_ptr; | |
2909 | ||
fb3b06c8 DA |
2910 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
2911 | return -EINVAL; | |
2912 | ||
f453ba04 DA |
2913 | mutex_lock(&dev->mode_config.mutex); |
2914 | obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY); | |
2915 | if (!obj) { | |
2916 | ret = -EINVAL; | |
2917 | goto done; | |
2918 | } | |
2919 | property = obj_to_property(obj); | |
2920 | ||
2921 | if (property->flags & DRM_MODE_PROP_ENUM) { | |
2922 | list_for_each_entry(prop_enum, &property->enum_blob_list, head) | |
2923 | enum_count++; | |
2924 | } else if (property->flags & DRM_MODE_PROP_BLOB) { | |
2925 | list_for_each_entry(prop_blob, &property->enum_blob_list, head) | |
2926 | blob_count++; | |
2927 | } | |
2928 | ||
2929 | value_count = property->num_values; | |
2930 | ||
2931 | strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN); | |
2932 | out_resp->name[DRM_PROP_NAME_LEN-1] = 0; | |
2933 | out_resp->flags = property->flags; | |
2934 | ||
2935 | if ((out_resp->count_values >= value_count) && value_count) { | |
81f6c7f8 | 2936 | values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr; |
f453ba04 DA |
2937 | for (i = 0; i < value_count; i++) { |
2938 | if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) { | |
2939 | ret = -EFAULT; | |
2940 | goto done; | |
2941 | } | |
2942 | } | |
2943 | } | |
2944 | out_resp->count_values = value_count; | |
2945 | ||
2946 | if (property->flags & DRM_MODE_PROP_ENUM) { | |
2947 | if ((out_resp->count_enum_blobs >= enum_count) && enum_count) { | |
2948 | copied = 0; | |
81f6c7f8 | 2949 | enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr; |
f453ba04 DA |
2950 | list_for_each_entry(prop_enum, &property->enum_blob_list, head) { |
2951 | ||
2952 | if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) { | |
2953 | ret = -EFAULT; | |
2954 | goto done; | |
2955 | } | |
2956 | ||
2957 | if (copy_to_user(&enum_ptr[copied].name, | |
2958 | &prop_enum->name, DRM_PROP_NAME_LEN)) { | |
2959 | ret = -EFAULT; | |
2960 | goto done; | |
2961 | } | |
2962 | copied++; | |
2963 | } | |
2964 | } | |
2965 | out_resp->count_enum_blobs = enum_count; | |
2966 | } | |
2967 | ||
2968 | if (property->flags & DRM_MODE_PROP_BLOB) { | |
2969 | if ((out_resp->count_enum_blobs >= blob_count) && blob_count) { | |
2970 | copied = 0; | |
81f6c7f8 VS |
2971 | blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr; |
2972 | blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr; | |
f453ba04 DA |
2973 | |
2974 | list_for_each_entry(prop_blob, &property->enum_blob_list, head) { | |
2975 | if (put_user(prop_blob->base.id, blob_id_ptr + copied)) { | |
2976 | ret = -EFAULT; | |
2977 | goto done; | |
2978 | } | |
2979 | ||
2980 | if (put_user(prop_blob->length, blob_length_ptr + copied)) { | |
2981 | ret = -EFAULT; | |
2982 | goto done; | |
2983 | } | |
2984 | ||
2985 | copied++; | |
2986 | } | |
2987 | } | |
2988 | out_resp->count_enum_blobs = blob_count; | |
2989 | } | |
2990 | done: | |
2991 | mutex_unlock(&dev->mode_config.mutex); | |
2992 | return ret; | |
2993 | } | |
2994 | ||
2995 | static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length, | |
2996 | void *data) | |
2997 | { | |
2998 | struct drm_property_blob *blob; | |
6bfc56aa | 2999 | int ret; |
f453ba04 DA |
3000 | |
3001 | if (!length || !data) | |
3002 | return NULL; | |
3003 | ||
3004 | blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL); | |
3005 | if (!blob) | |
3006 | return NULL; | |
3007 | ||
6bfc56aa VS |
3008 | ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB); |
3009 | if (ret) { | |
3010 | kfree(blob); | |
3011 | return NULL; | |
3012 | } | |
3013 | ||
f453ba04 DA |
3014 | blob->length = length; |
3015 | ||
3016 | memcpy(blob->data, data, length); | |
3017 | ||
f453ba04 DA |
3018 | list_add_tail(&blob->head, &dev->mode_config.property_blob_list); |
3019 | return blob; | |
3020 | } | |
3021 | ||
3022 | static void drm_property_destroy_blob(struct drm_device *dev, | |
3023 | struct drm_property_blob *blob) | |
3024 | { | |
3025 | drm_mode_object_put(dev, &blob->base); | |
3026 | list_del(&blob->head); | |
3027 | kfree(blob); | |
3028 | } | |
3029 | ||
3030 | int drm_mode_getblob_ioctl(struct drm_device *dev, | |
3031 | void *data, struct drm_file *file_priv) | |
3032 | { | |
3033 | struct drm_mode_object *obj; | |
3034 | struct drm_mode_get_blob *out_resp = data; | |
3035 | struct drm_property_blob *blob; | |
3036 | int ret = 0; | |
81f6c7f8 | 3037 | void __user *blob_ptr; |
f453ba04 | 3038 | |
fb3b06c8 DA |
3039 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
3040 | return -EINVAL; | |
3041 | ||
f453ba04 DA |
3042 | mutex_lock(&dev->mode_config.mutex); |
3043 | obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB); | |
3044 | if (!obj) { | |
3045 | ret = -EINVAL; | |
3046 | goto done; | |
3047 | } | |
3048 | blob = obj_to_blob(obj); | |
3049 | ||
3050 | if (out_resp->length == blob->length) { | |
81f6c7f8 | 3051 | blob_ptr = (void __user *)(unsigned long)out_resp->data; |
f453ba04 DA |
3052 | if (copy_to_user(blob_ptr, blob->data, blob->length)){ |
3053 | ret = -EFAULT; | |
3054 | goto done; | |
3055 | } | |
3056 | } | |
3057 | out_resp->length = blob->length; | |
3058 | ||
3059 | done: | |
3060 | mutex_unlock(&dev->mode_config.mutex); | |
3061 | return ret; | |
3062 | } | |
3063 | ||
3064 | int drm_mode_connector_update_edid_property(struct drm_connector *connector, | |
3065 | struct edid *edid) | |
3066 | { | |
3067 | struct drm_device *dev = connector->dev; | |
4a1b0714 | 3068 | int ret, size; |
f453ba04 DA |
3069 | |
3070 | if (connector->edid_blob_ptr) | |
3071 | drm_property_destroy_blob(dev, connector->edid_blob_ptr); | |
3072 | ||
3073 | /* Delete edid, when there is none. */ | |
3074 | if (!edid) { | |
3075 | connector->edid_blob_ptr = NULL; | |
3076 | ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0); | |
3077 | return ret; | |
3078 | } | |
3079 | ||
7466f4cc AJ |
3080 | size = EDID_LENGTH * (1 + edid->extensions); |
3081 | connector->edid_blob_ptr = drm_property_create_blob(connector->dev, | |
3082 | size, edid); | |
f453ba04 DA |
3083 | |
3084 | ret = drm_connector_property_set_value(connector, | |
3085 | dev->mode_config.edid_property, | |
3086 | connector->edid_blob_ptr->base.id); | |
3087 | ||
3088 | return ret; | |
3089 | } | |
3090 | EXPORT_SYMBOL(drm_mode_connector_update_edid_property); | |
3091 | ||
26a34815 PZ |
3092 | static bool drm_property_change_is_valid(struct drm_property *property, |
3093 | __u64 value) | |
3094 | { | |
3095 | if (property->flags & DRM_MODE_PROP_IMMUTABLE) | |
3096 | return false; | |
3097 | if (property->flags & DRM_MODE_PROP_RANGE) { | |
3098 | if (value < property->values[0] || value > property->values[1]) | |
3099 | return false; | |
3100 | return true; | |
3101 | } else { | |
3102 | int i; | |
3103 | for (i = 0; i < property->num_values; i++) | |
3104 | if (property->values[i] == value) | |
3105 | return true; | |
3106 | return false; | |
3107 | } | |
3108 | } | |
3109 | ||
f453ba04 DA |
3110 | int drm_mode_connector_property_set_ioctl(struct drm_device *dev, |
3111 | void *data, struct drm_file *file_priv) | |
3112 | { | |
0057d8dd PZ |
3113 | struct drm_mode_connector_set_property *conn_set_prop = data; |
3114 | struct drm_mode_obj_set_property obj_set_prop = { | |
3115 | .value = conn_set_prop->value, | |
3116 | .prop_id = conn_set_prop->prop_id, | |
3117 | .obj_id = conn_set_prop->connector_id, | |
3118 | .obj_type = DRM_MODE_OBJECT_CONNECTOR | |
3119 | }; | |
fb3b06c8 | 3120 | |
0057d8dd PZ |
3121 | /* It does all the locking and checking we need */ |
3122 | return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv); | |
f453ba04 DA |
3123 | } |
3124 | ||
c543188a PZ |
3125 | static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj, |
3126 | struct drm_property *property, | |
3127 | uint64_t value) | |
3128 | { | |
3129 | int ret = -EINVAL; | |
3130 | struct drm_connector *connector = obj_to_connector(obj); | |
3131 | ||
3132 | /* Do DPMS ourselves */ | |
3133 | if (property == connector->dev->mode_config.dpms_property) { | |
3134 | if (connector->funcs->dpms) | |
3135 | (*connector->funcs->dpms)(connector, (int)value); | |
3136 | ret = 0; | |
3137 | } else if (connector->funcs->set_property) | |
3138 | ret = connector->funcs->set_property(connector, property, value); | |
3139 | ||
3140 | /* store the property value if successful */ | |
3141 | if (!ret) | |
3142 | drm_connector_property_set_value(connector, property, value); | |
3143 | return ret; | |
3144 | } | |
3145 | ||
bffd9de0 PZ |
3146 | static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj, |
3147 | struct drm_property *property, | |
3148 | uint64_t value) | |
3149 | { | |
3150 | int ret = -EINVAL; | |
3151 | struct drm_crtc *crtc = obj_to_crtc(obj); | |
3152 | ||
3153 | if (crtc->funcs->set_property) | |
3154 | ret = crtc->funcs->set_property(crtc, property, value); | |
3155 | if (!ret) | |
3156 | drm_object_property_set_value(obj, property, value); | |
3157 | ||
3158 | return ret; | |
3159 | } | |
3160 | ||
c543188a PZ |
3161 | int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data, |
3162 | struct drm_file *file_priv) | |
3163 | { | |
3164 | struct drm_mode_obj_get_properties *arg = data; | |
3165 | struct drm_mode_object *obj; | |
3166 | int ret = 0; | |
3167 | int i; | |
3168 | int copied = 0; | |
3169 | int props_count = 0; | |
3170 | uint32_t __user *props_ptr; | |
3171 | uint64_t __user *prop_values_ptr; | |
3172 | ||
3173 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) | |
3174 | return -EINVAL; | |
3175 | ||
3176 | mutex_lock(&dev->mode_config.mutex); | |
3177 | ||
3178 | obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type); | |
3179 | if (!obj) { | |
3180 | ret = -EINVAL; | |
3181 | goto out; | |
3182 | } | |
3183 | if (!obj->properties) { | |
3184 | ret = -EINVAL; | |
3185 | goto out; | |
3186 | } | |
3187 | ||
7f88a9be | 3188 | props_count = obj->properties->count; |
c543188a PZ |
3189 | |
3190 | /* This ioctl is called twice, once to determine how much space is | |
3191 | * needed, and the 2nd time to fill it. */ | |
3192 | if ((arg->count_props >= props_count) && props_count) { | |
3193 | copied = 0; | |
3194 | props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr); | |
3195 | prop_values_ptr = (uint64_t __user *)(unsigned long) | |
3196 | (arg->prop_values_ptr); | |
3197 | for (i = 0; i < props_count; i++) { | |
3198 | if (put_user(obj->properties->ids[i], | |
3199 | props_ptr + copied)) { | |
3200 | ret = -EFAULT; | |
3201 | goto out; | |
3202 | } | |
3203 | if (put_user(obj->properties->values[i], | |
3204 | prop_values_ptr + copied)) { | |
3205 | ret = -EFAULT; | |
3206 | goto out; | |
3207 | } | |
3208 | copied++; | |
3209 | } | |
3210 | } | |
3211 | arg->count_props = props_count; | |
3212 | out: | |
3213 | mutex_unlock(&dev->mode_config.mutex); | |
3214 | return ret; | |
3215 | } | |
3216 | ||
3217 | int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data, | |
3218 | struct drm_file *file_priv) | |
3219 | { | |
3220 | struct drm_mode_obj_set_property *arg = data; | |
3221 | struct drm_mode_object *arg_obj; | |
3222 | struct drm_mode_object *prop_obj; | |
3223 | struct drm_property *property; | |
3224 | int ret = -EINVAL; | |
3225 | int i; | |
3226 | ||
3227 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) | |
3228 | return -EINVAL; | |
3229 | ||
3230 | mutex_lock(&dev->mode_config.mutex); | |
3231 | ||
3232 | arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type); | |
3233 | if (!arg_obj) | |
3234 | goto out; | |
3235 | if (!arg_obj->properties) | |
3236 | goto out; | |
3237 | ||
7f88a9be | 3238 | for (i = 0; i < arg_obj->properties->count; i++) |
c543188a PZ |
3239 | if (arg_obj->properties->ids[i] == arg->prop_id) |
3240 | break; | |
3241 | ||
7f88a9be | 3242 | if (i == arg_obj->properties->count) |
c543188a PZ |
3243 | goto out; |
3244 | ||
3245 | prop_obj = drm_mode_object_find(dev, arg->prop_id, | |
3246 | DRM_MODE_OBJECT_PROPERTY); | |
3247 | if (!prop_obj) | |
3248 | goto out; | |
3249 | property = obj_to_property(prop_obj); | |
3250 | ||
3251 | if (!drm_property_change_is_valid(property, arg->value)) | |
3252 | goto out; | |
3253 | ||
3254 | switch (arg_obj->type) { | |
3255 | case DRM_MODE_OBJECT_CONNECTOR: | |
3256 | ret = drm_mode_connector_set_obj_prop(arg_obj, property, | |
3257 | arg->value); | |
3258 | break; | |
bffd9de0 PZ |
3259 | case DRM_MODE_OBJECT_CRTC: |
3260 | ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value); | |
3261 | break; | |
c543188a PZ |
3262 | } |
3263 | ||
3264 | out: | |
3265 | mutex_unlock(&dev->mode_config.mutex); | |
3266 | return ret; | |
3267 | } | |
3268 | ||
f453ba04 DA |
3269 | int drm_mode_connector_attach_encoder(struct drm_connector *connector, |
3270 | struct drm_encoder *encoder) | |
3271 | { | |
3272 | int i; | |
3273 | ||
3274 | for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) { | |
3275 | if (connector->encoder_ids[i] == 0) { | |
3276 | connector->encoder_ids[i] = encoder->base.id; | |
3277 | return 0; | |
3278 | } | |
3279 | } | |
3280 | return -ENOMEM; | |
3281 | } | |
3282 | EXPORT_SYMBOL(drm_mode_connector_attach_encoder); | |
3283 | ||
3284 | void drm_mode_connector_detach_encoder(struct drm_connector *connector, | |
3285 | struct drm_encoder *encoder) | |
3286 | { | |
3287 | int i; | |
3288 | for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) { | |
3289 | if (connector->encoder_ids[i] == encoder->base.id) { | |
3290 | connector->encoder_ids[i] = 0; | |
3291 | if (connector->encoder == encoder) | |
3292 | connector->encoder = NULL; | |
3293 | break; | |
3294 | } | |
3295 | } | |
3296 | } | |
3297 | EXPORT_SYMBOL(drm_mode_connector_detach_encoder); | |
3298 | ||
4cae5b84 | 3299 | int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc, |
f453ba04 DA |
3300 | int gamma_size) |
3301 | { | |
3302 | crtc->gamma_size = gamma_size; | |
3303 | ||
3304 | crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL); | |
3305 | if (!crtc->gamma_store) { | |
3306 | crtc->gamma_size = 0; | |
4cae5b84 | 3307 | return -ENOMEM; |
f453ba04 DA |
3308 | } |
3309 | ||
4cae5b84 | 3310 | return 0; |
f453ba04 DA |
3311 | } |
3312 | EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size); | |
3313 | ||
3314 | int drm_mode_gamma_set_ioctl(struct drm_device *dev, | |
3315 | void *data, struct drm_file *file_priv) | |
3316 | { | |
3317 | struct drm_mode_crtc_lut *crtc_lut = data; | |
3318 | struct drm_mode_object *obj; | |
3319 | struct drm_crtc *crtc; | |
3320 | void *r_base, *g_base, *b_base; | |
3321 | int size; | |
3322 | int ret = 0; | |
3323 | ||
fb3b06c8 DA |
3324 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
3325 | return -EINVAL; | |
3326 | ||
f453ba04 DA |
3327 | mutex_lock(&dev->mode_config.mutex); |
3328 | obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC); | |
3329 | if (!obj) { | |
3330 | ret = -EINVAL; | |
3331 | goto out; | |
3332 | } | |
3333 | crtc = obj_to_crtc(obj); | |
3334 | ||
ebe0f244 LP |
3335 | if (crtc->funcs->gamma_set == NULL) { |
3336 | ret = -ENOSYS; | |
3337 | goto out; | |
3338 | } | |
3339 | ||
f453ba04 DA |
3340 | /* memcpy into gamma store */ |
3341 | if (crtc_lut->gamma_size != crtc->gamma_size) { | |
3342 | ret = -EINVAL; | |
3343 | goto out; | |
3344 | } | |
3345 | ||
3346 | size = crtc_lut->gamma_size * (sizeof(uint16_t)); | |
3347 | r_base = crtc->gamma_store; | |
3348 | if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) { | |
3349 | ret = -EFAULT; | |
3350 | goto out; | |
3351 | } | |
3352 | ||
3353 | g_base = r_base + size; | |
3354 | if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) { | |
3355 | ret = -EFAULT; | |
3356 | goto out; | |
3357 | } | |
3358 | ||
3359 | b_base = g_base + size; | |
3360 | if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) { | |
3361 | ret = -EFAULT; | |
3362 | goto out; | |
3363 | } | |
3364 | ||
7203425a | 3365 | crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size); |
f453ba04 DA |
3366 | |
3367 | out: | |
3368 | mutex_unlock(&dev->mode_config.mutex); | |
3369 | return ret; | |
3370 | ||
3371 | } | |
3372 | ||
3373 | int drm_mode_gamma_get_ioctl(struct drm_device *dev, | |
3374 | void *data, struct drm_file *file_priv) | |
3375 | { | |
3376 | struct drm_mode_crtc_lut *crtc_lut = data; | |
3377 | struct drm_mode_object *obj; | |
3378 | struct drm_crtc *crtc; | |
3379 | void *r_base, *g_base, *b_base; | |
3380 | int size; | |
3381 | int ret = 0; | |
3382 | ||
fb3b06c8 DA |
3383 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
3384 | return -EINVAL; | |
3385 | ||
f453ba04 DA |
3386 | mutex_lock(&dev->mode_config.mutex); |
3387 | obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC); | |
3388 | if (!obj) { | |
3389 | ret = -EINVAL; | |
3390 | goto out; | |
3391 | } | |
3392 | crtc = obj_to_crtc(obj); | |
3393 | ||
3394 | /* memcpy into gamma store */ | |
3395 | if (crtc_lut->gamma_size != crtc->gamma_size) { | |
3396 | ret = -EINVAL; | |
3397 | goto out; | |
3398 | } | |
3399 | ||
3400 | size = crtc_lut->gamma_size * (sizeof(uint16_t)); | |
3401 | r_base = crtc->gamma_store; | |
3402 | if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) { | |
3403 | ret = -EFAULT; | |
3404 | goto out; | |
3405 | } | |
3406 | ||
3407 | g_base = r_base + size; | |
3408 | if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) { | |
3409 | ret = -EFAULT; | |
3410 | goto out; | |
3411 | } | |
3412 | ||
3413 | b_base = g_base + size; | |
3414 | if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) { | |
3415 | ret = -EFAULT; | |
3416 | goto out; | |
3417 | } | |
3418 | out: | |
3419 | mutex_unlock(&dev->mode_config.mutex); | |
3420 | return ret; | |
3421 | } | |
d91d8a3f KH |
3422 | |
3423 | int drm_mode_page_flip_ioctl(struct drm_device *dev, | |
3424 | void *data, struct drm_file *file_priv) | |
3425 | { | |
3426 | struct drm_mode_crtc_page_flip *page_flip = data; | |
3427 | struct drm_mode_object *obj; | |
3428 | struct drm_crtc *crtc; | |
3429 | struct drm_framebuffer *fb; | |
3430 | struct drm_pending_vblank_event *e = NULL; | |
3431 | unsigned long flags; | |
3432 | int ret = -EINVAL; | |
3433 | ||
3434 | if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS || | |
3435 | page_flip->reserved != 0) | |
3436 | return -EINVAL; | |
3437 | ||
3438 | mutex_lock(&dev->mode_config.mutex); | |
3439 | obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC); | |
3440 | if (!obj) | |
3441 | goto out; | |
3442 | crtc = obj_to_crtc(obj); | |
3443 | ||
90c1efdd CW |
3444 | if (crtc->fb == NULL) { |
3445 | /* The framebuffer is currently unbound, presumably | |
3446 | * due to a hotplug event, that userspace has not | |
3447 | * yet discovered. | |
3448 | */ | |
3449 | ret = -EBUSY; | |
3450 | goto out; | |
3451 | } | |
3452 | ||
d91d8a3f KH |
3453 | if (crtc->funcs->page_flip == NULL) |
3454 | goto out; | |
3455 | ||
3456 | obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB); | |
3457 | if (!obj) | |
3458 | goto out; | |
3459 | fb = obj_to_fb(obj); | |
3460 | ||
5f61bb42 VS |
3461 | if (crtc->mode.hdisplay > fb->width || |
3462 | crtc->mode.vdisplay > fb->height || | |
3463 | crtc->x > fb->width - crtc->mode.hdisplay || | |
3464 | crtc->y > fb->height - crtc->mode.vdisplay) { | |
3465 | DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d.\n", | |
3466 | fb->width, fb->height, | |
3467 | crtc->mode.hdisplay, crtc->mode.vdisplay, | |
3468 | crtc->x, crtc->y); | |
3469 | ret = -ENOSPC; | |
3470 | goto out; | |
3471 | } | |
3472 | ||
d91d8a3f KH |
3473 | if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) { |
3474 | ret = -ENOMEM; | |
3475 | spin_lock_irqsave(&dev->event_lock, flags); | |
3476 | if (file_priv->event_space < sizeof e->event) { | |
3477 | spin_unlock_irqrestore(&dev->event_lock, flags); | |
3478 | goto out; | |
3479 | } | |
3480 | file_priv->event_space -= sizeof e->event; | |
3481 | spin_unlock_irqrestore(&dev->event_lock, flags); | |
3482 | ||
3483 | e = kzalloc(sizeof *e, GFP_KERNEL); | |
3484 | if (e == NULL) { | |
3485 | spin_lock_irqsave(&dev->event_lock, flags); | |
3486 | file_priv->event_space += sizeof e->event; | |
3487 | spin_unlock_irqrestore(&dev->event_lock, flags); | |
3488 | goto out; | |
3489 | } | |
3490 | ||
7bd4d7be | 3491 | e->event.base.type = DRM_EVENT_FLIP_COMPLETE; |
d91d8a3f KH |
3492 | e->event.base.length = sizeof e->event; |
3493 | e->event.user_data = page_flip->user_data; | |
3494 | e->base.event = &e->event.base; | |
3495 | e->base.file_priv = file_priv; | |
3496 | e->base.destroy = | |
3497 | (void (*) (struct drm_pending_event *)) kfree; | |
3498 | } | |
3499 | ||
3500 | ret = crtc->funcs->page_flip(crtc, fb, e); | |
3501 | if (ret) { | |
aef6a7ee JS |
3502 | if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) { |
3503 | spin_lock_irqsave(&dev->event_lock, flags); | |
3504 | file_priv->event_space += sizeof e->event; | |
3505 | spin_unlock_irqrestore(&dev->event_lock, flags); | |
3506 | kfree(e); | |
3507 | } | |
d91d8a3f KH |
3508 | } |
3509 | ||
3510 | out: | |
3511 | mutex_unlock(&dev->mode_config.mutex); | |
3512 | return ret; | |
3513 | } | |
eb033556 CW |
3514 | |
3515 | void drm_mode_config_reset(struct drm_device *dev) | |
3516 | { | |
3517 | struct drm_crtc *crtc; | |
3518 | struct drm_encoder *encoder; | |
3519 | struct drm_connector *connector; | |
3520 | ||
3521 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) | |
3522 | if (crtc->funcs->reset) | |
3523 | crtc->funcs->reset(crtc); | |
3524 | ||
3525 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) | |
3526 | if (encoder->funcs->reset) | |
3527 | encoder->funcs->reset(encoder); | |
3528 | ||
3529 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) | |
3530 | if (connector->funcs->reset) | |
3531 | connector->funcs->reset(connector); | |
3532 | } | |
3533 | EXPORT_SYMBOL(drm_mode_config_reset); | |
ff72145b DA |
3534 | |
3535 | int drm_mode_create_dumb_ioctl(struct drm_device *dev, | |
3536 | void *data, struct drm_file *file_priv) | |
3537 | { | |
3538 | struct drm_mode_create_dumb *args = data; | |
3539 | ||
3540 | if (!dev->driver->dumb_create) | |
3541 | return -ENOSYS; | |
3542 | return dev->driver->dumb_create(file_priv, dev, args); | |
3543 | } | |
3544 | ||
3545 | int drm_mode_mmap_dumb_ioctl(struct drm_device *dev, | |
3546 | void *data, struct drm_file *file_priv) | |
3547 | { | |
3548 | struct drm_mode_map_dumb *args = data; | |
3549 | ||
3550 | /* call driver ioctl to get mmap offset */ | |
3551 | if (!dev->driver->dumb_map_offset) | |
3552 | return -ENOSYS; | |
3553 | ||
3554 | return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset); | |
3555 | } | |
3556 | ||
3557 | int drm_mode_destroy_dumb_ioctl(struct drm_device *dev, | |
3558 | void *data, struct drm_file *file_priv) | |
3559 | { | |
3560 | struct drm_mode_destroy_dumb *args = data; | |
3561 | ||
3562 | if (!dev->driver->dumb_destroy) | |
3563 | return -ENOSYS; | |
3564 | ||
3565 | return dev->driver->dumb_destroy(file_priv, dev, args->handle); | |
3566 | } | |
248dbc23 DA |
3567 | |
3568 | /* | |
3569 | * Just need to support RGB formats here for compat with code that doesn't | |
3570 | * use pixel formats directly yet. | |
3571 | */ | |
3572 | void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth, | |
3573 | int *bpp) | |
3574 | { | |
3575 | switch (format) { | |
04b3924d VS |
3576 | case DRM_FORMAT_RGB332: |
3577 | case DRM_FORMAT_BGR233: | |
248dbc23 DA |
3578 | *depth = 8; |
3579 | *bpp = 8; | |
3580 | break; | |
04b3924d VS |
3581 | case DRM_FORMAT_XRGB1555: |
3582 | case DRM_FORMAT_XBGR1555: | |
3583 | case DRM_FORMAT_RGBX5551: | |
3584 | case DRM_FORMAT_BGRX5551: | |
3585 | case DRM_FORMAT_ARGB1555: | |
3586 | case DRM_FORMAT_ABGR1555: | |
3587 | case DRM_FORMAT_RGBA5551: | |
3588 | case DRM_FORMAT_BGRA5551: | |
248dbc23 DA |
3589 | *depth = 15; |
3590 | *bpp = 16; | |
3591 | break; | |
04b3924d VS |
3592 | case DRM_FORMAT_RGB565: |
3593 | case DRM_FORMAT_BGR565: | |
248dbc23 DA |
3594 | *depth = 16; |
3595 | *bpp = 16; | |
3596 | break; | |
04b3924d VS |
3597 | case DRM_FORMAT_RGB888: |
3598 | case DRM_FORMAT_BGR888: | |
3599 | *depth = 24; | |
3600 | *bpp = 24; | |
3601 | break; | |
3602 | case DRM_FORMAT_XRGB8888: | |
3603 | case DRM_FORMAT_XBGR8888: | |
3604 | case DRM_FORMAT_RGBX8888: | |
3605 | case DRM_FORMAT_BGRX8888: | |
248dbc23 DA |
3606 | *depth = 24; |
3607 | *bpp = 32; | |
3608 | break; | |
04b3924d VS |
3609 | case DRM_FORMAT_XRGB2101010: |
3610 | case DRM_FORMAT_XBGR2101010: | |
3611 | case DRM_FORMAT_RGBX1010102: | |
3612 | case DRM_FORMAT_BGRX1010102: | |
3613 | case DRM_FORMAT_ARGB2101010: | |
3614 | case DRM_FORMAT_ABGR2101010: | |
3615 | case DRM_FORMAT_RGBA1010102: | |
3616 | case DRM_FORMAT_BGRA1010102: | |
248dbc23 DA |
3617 | *depth = 30; |
3618 | *bpp = 32; | |
3619 | break; | |
04b3924d VS |
3620 | case DRM_FORMAT_ARGB8888: |
3621 | case DRM_FORMAT_ABGR8888: | |
3622 | case DRM_FORMAT_RGBA8888: | |
3623 | case DRM_FORMAT_BGRA8888: | |
248dbc23 DA |
3624 | *depth = 32; |
3625 | *bpp = 32; | |
3626 | break; | |
3627 | default: | |
3628 | DRM_DEBUG_KMS("unsupported pixel format\n"); | |
3629 | *depth = 0; | |
3630 | *bpp = 0; | |
3631 | break; | |
3632 | } | |
3633 | } | |
3634 | EXPORT_SYMBOL(drm_fb_get_bpp_depth); | |
141670e9 VS |
3635 | |
3636 | /** | |
3637 | * drm_format_num_planes - get the number of planes for format | |
3638 | * @format: pixel format (DRM_FORMAT_*) | |
3639 | * | |
3640 | * RETURNS: | |
3641 | * The number of planes used by the specified pixel format. | |
3642 | */ | |
3643 | int drm_format_num_planes(uint32_t format) | |
3644 | { | |
3645 | switch (format) { | |
3646 | case DRM_FORMAT_YUV410: | |
3647 | case DRM_FORMAT_YVU410: | |
3648 | case DRM_FORMAT_YUV411: | |
3649 | case DRM_FORMAT_YVU411: | |
3650 | case DRM_FORMAT_YUV420: | |
3651 | case DRM_FORMAT_YVU420: | |
3652 | case DRM_FORMAT_YUV422: | |
3653 | case DRM_FORMAT_YVU422: | |
3654 | case DRM_FORMAT_YUV444: | |
3655 | case DRM_FORMAT_YVU444: | |
3656 | return 3; | |
3657 | case DRM_FORMAT_NV12: | |
3658 | case DRM_FORMAT_NV21: | |
3659 | case DRM_FORMAT_NV16: | |
3660 | case DRM_FORMAT_NV61: | |
3661 | return 2; | |
3662 | default: | |
3663 | return 1; | |
3664 | } | |
3665 | } | |
3666 | EXPORT_SYMBOL(drm_format_num_planes); | |
5a86bd55 VS |
3667 | |
3668 | /** | |
3669 | * drm_format_plane_cpp - determine the bytes per pixel value | |
3670 | * @format: pixel format (DRM_FORMAT_*) | |
3671 | * @plane: plane index | |
3672 | * | |
3673 | * RETURNS: | |
3674 | * The bytes per pixel value for the specified plane. | |
3675 | */ | |
3676 | int drm_format_plane_cpp(uint32_t format, int plane) | |
3677 | { | |
3678 | unsigned int depth; | |
3679 | int bpp; | |
3680 | ||
3681 | if (plane >= drm_format_num_planes(format)) | |
3682 | return 0; | |
3683 | ||
3684 | switch (format) { | |
3685 | case DRM_FORMAT_YUYV: | |
3686 | case DRM_FORMAT_YVYU: | |
3687 | case DRM_FORMAT_UYVY: | |
3688 | case DRM_FORMAT_VYUY: | |
3689 | return 2; | |
3690 | case DRM_FORMAT_NV12: | |
3691 | case DRM_FORMAT_NV21: | |
3692 | case DRM_FORMAT_NV16: | |
3693 | case DRM_FORMAT_NV61: | |
3694 | return plane ? 2 : 1; | |
3695 | case DRM_FORMAT_YUV410: | |
3696 | case DRM_FORMAT_YVU410: | |
3697 | case DRM_FORMAT_YUV411: | |
3698 | case DRM_FORMAT_YVU411: | |
3699 | case DRM_FORMAT_YUV420: | |
3700 | case DRM_FORMAT_YVU420: | |
3701 | case DRM_FORMAT_YUV422: | |
3702 | case DRM_FORMAT_YVU422: | |
3703 | case DRM_FORMAT_YUV444: | |
3704 | case DRM_FORMAT_YVU444: | |
3705 | return 1; | |
3706 | default: | |
3707 | drm_fb_get_bpp_depth(format, &depth, &bpp); | |
3708 | return bpp >> 3; | |
3709 | } | |
3710 | } | |
3711 | EXPORT_SYMBOL(drm_format_plane_cpp); | |
01b68b04 VS |
3712 | |
3713 | /** | |
3714 | * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor | |
3715 | * @format: pixel format (DRM_FORMAT_*) | |
3716 | * | |
3717 | * RETURNS: | |
3718 | * The horizontal chroma subsampling factor for the | |
3719 | * specified pixel format. | |
3720 | */ | |
3721 | int drm_format_horz_chroma_subsampling(uint32_t format) | |
3722 | { | |
3723 | switch (format) { | |
3724 | case DRM_FORMAT_YUV411: | |
3725 | case DRM_FORMAT_YVU411: | |
3726 | case DRM_FORMAT_YUV410: | |
3727 | case DRM_FORMAT_YVU410: | |
3728 | return 4; | |
3729 | case DRM_FORMAT_YUYV: | |
3730 | case DRM_FORMAT_YVYU: | |
3731 | case DRM_FORMAT_UYVY: | |
3732 | case DRM_FORMAT_VYUY: | |
3733 | case DRM_FORMAT_NV12: | |
3734 | case DRM_FORMAT_NV21: | |
3735 | case DRM_FORMAT_NV16: | |
3736 | case DRM_FORMAT_NV61: | |
3737 | case DRM_FORMAT_YUV422: | |
3738 | case DRM_FORMAT_YVU422: | |
3739 | case DRM_FORMAT_YUV420: | |
3740 | case DRM_FORMAT_YVU420: | |
3741 | return 2; | |
3742 | default: | |
3743 | return 1; | |
3744 | } | |
3745 | } | |
3746 | EXPORT_SYMBOL(drm_format_horz_chroma_subsampling); | |
3747 | ||
3748 | /** | |
3749 | * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor | |
3750 | * @format: pixel format (DRM_FORMAT_*) | |
3751 | * | |
3752 | * RETURNS: | |
3753 | * The vertical chroma subsampling factor for the | |
3754 | * specified pixel format. | |
3755 | */ | |
3756 | int drm_format_vert_chroma_subsampling(uint32_t format) | |
3757 | { | |
3758 | switch (format) { | |
3759 | case DRM_FORMAT_YUV410: | |
3760 | case DRM_FORMAT_YVU410: | |
3761 | return 4; | |
3762 | case DRM_FORMAT_YUV420: | |
3763 | case DRM_FORMAT_YVU420: | |
3764 | case DRM_FORMAT_NV12: | |
3765 | case DRM_FORMAT_NV21: | |
3766 | return 2; | |
3767 | default: | |
3768 | return 1; | |
3769 | } | |
3770 | } | |
3771 | EXPORT_SYMBOL(drm_format_vert_chroma_subsampling); |