drm_ioctl.c revision 1.5 1 1.5 riastrad /* $NetBSD: drm_ioctl.c,v 1.5 2018/08/27 04:58:19 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*
4 1.1 riastrad * Created: Fri Jan 8 09:01:26 1999 by faith (at) valinux.com
5 1.1 riastrad *
6 1.1 riastrad * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
7 1.1 riastrad * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
8 1.1 riastrad * All Rights Reserved.
9 1.1 riastrad *
10 1.5 riastrad * Author Rickard E. (Rik) Faith <faith (at) valinux.com>
11 1.5 riastrad * Author Gareth Hughes <gareth (at) valinux.com>
12 1.5 riastrad *
13 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a
14 1.1 riastrad * copy of this software and associated documentation files (the "Software"),
15 1.1 riastrad * to deal in the Software without restriction, including without limitation
16 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the
18 1.1 riastrad * Software is furnished to do so, subject to the following conditions:
19 1.1 riastrad *
20 1.1 riastrad * The above copyright notice and this permission notice (including the next
21 1.1 riastrad * paragraph) shall be included in all copies or substantial portions of the
22 1.1 riastrad * Software.
23 1.1 riastrad *
24 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 1.1 riastrad * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
28 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
29 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30 1.1 riastrad * OTHER DEALINGS IN THE SOFTWARE.
31 1.1 riastrad */
32 1.1 riastrad
33 1.5 riastrad #include <sys/cdefs.h>
34 1.5 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_ioctl.c,v 1.5 2018/08/27 04:58:19 riastradh Exp $");
35 1.5 riastrad
36 1.1 riastrad #include <drm/drmP.h>
37 1.1 riastrad #include <drm/drm_core.h>
38 1.5 riastrad #include "drm_legacy.h"
39 1.5 riastrad #include "drm_internal.h"
40 1.5 riastrad #include "drm_crtc_internal.h"
41 1.1 riastrad
42 1.1 riastrad #include <linux/pci.h>
43 1.1 riastrad #include <linux/export.h>
44 1.1 riastrad
45 1.5 riastrad static int drm_version(struct drm_device *dev, void *data,
46 1.5 riastrad struct drm_file *file_priv);
47 1.5 riastrad
48 1.5 riastrad /*
49 1.1 riastrad * Get the bus id.
50 1.1 riastrad *
51 1.1 riastrad * \param inode device inode.
52 1.1 riastrad * \param file_priv DRM file private.
53 1.1 riastrad * \param cmd command.
54 1.1 riastrad * \param arg user argument, pointing to a drm_unique structure.
55 1.1 riastrad * \return zero on success or a negative number on failure.
56 1.1 riastrad *
57 1.1 riastrad * Copies the bus id from drm_device::unique into user space.
58 1.1 riastrad */
59 1.5 riastrad static int drm_getunique(struct drm_device *dev, void *data,
60 1.1 riastrad struct drm_file *file_priv)
61 1.1 riastrad {
62 1.1 riastrad struct drm_unique *u = data;
63 1.1 riastrad struct drm_master *master = file_priv->master;
64 1.1 riastrad
65 1.1 riastrad if (u->unique_len >= master->unique_len) {
66 1.1 riastrad if (copy_to_user(u->unique, master->unique, master->unique_len))
67 1.1 riastrad return -EFAULT;
68 1.1 riastrad }
69 1.1 riastrad u->unique_len = master->unique_len;
70 1.1 riastrad
71 1.1 riastrad return 0;
72 1.1 riastrad }
73 1.1 riastrad
74 1.1 riastrad static void
75 1.1 riastrad drm_unset_busid(struct drm_device *dev,
76 1.1 riastrad struct drm_master *master)
77 1.1 riastrad {
78 1.1 riastrad kfree(master->unique);
79 1.1 riastrad master->unique = NULL;
80 1.1 riastrad master->unique_len = 0;
81 1.1 riastrad }
82 1.1 riastrad
83 1.5 riastrad /*
84 1.1 riastrad * Set the bus id.
85 1.1 riastrad *
86 1.1 riastrad * \param inode device inode.
87 1.1 riastrad * \param file_priv DRM file private.
88 1.1 riastrad * \param cmd command.
89 1.1 riastrad * \param arg user argument, pointing to a drm_unique structure.
90 1.1 riastrad * \return zero on success or a negative number on failure.
91 1.1 riastrad *
92 1.1 riastrad * Copies the bus id from userspace into drm_device::unique, and verifies that
93 1.1 riastrad * it matches the device this DRM is attached to (EINVAL otherwise). Deprecated
94 1.1 riastrad * in interface version 1.1 and will return EBUSY when setversion has requested
95 1.5 riastrad * version 1.1 or greater. Also note that KMS is all version 1.1 and later and
96 1.5 riastrad * UMS was only ever supported on pci devices.
97 1.1 riastrad */
98 1.5 riastrad static int drm_setunique(struct drm_device *dev, void *data,
99 1.1 riastrad struct drm_file *file_priv)
100 1.1 riastrad {
101 1.1 riastrad struct drm_unique *u = data;
102 1.1 riastrad struct drm_master *master = file_priv->master;
103 1.1 riastrad int ret;
104 1.1 riastrad
105 1.1 riastrad if (master->unique_len || master->unique)
106 1.1 riastrad return -EBUSY;
107 1.1 riastrad
108 1.1 riastrad if (!u->unique_len || u->unique_len > 1024)
109 1.1 riastrad return -EINVAL;
110 1.1 riastrad
111 1.5 riastrad if (drm_core_check_feature(dev, DRIVER_MODESET))
112 1.5 riastrad return 0;
113 1.5 riastrad
114 1.5 riastrad if (WARN_ON(!dev->pdev))
115 1.1 riastrad return -EINVAL;
116 1.1 riastrad
117 1.5 riastrad ret = drm_pci_set_unique(dev, master, u);
118 1.1 riastrad if (ret)
119 1.1 riastrad goto err;
120 1.1 riastrad
121 1.1 riastrad return 0;
122 1.1 riastrad
123 1.1 riastrad err:
124 1.1 riastrad drm_unset_busid(dev, master);
125 1.1 riastrad return ret;
126 1.1 riastrad }
127 1.1 riastrad
128 1.1 riastrad static int drm_set_busid(struct drm_device *dev, struct drm_file *file_priv)
129 1.1 riastrad {
130 1.1 riastrad struct drm_master *master = file_priv->master;
131 1.1 riastrad int ret;
132 1.1 riastrad
133 1.1 riastrad if (master->unique != NULL)
134 1.1 riastrad drm_unset_busid(dev, master);
135 1.1 riastrad
136 1.5 riastrad if (dev->driver->set_busid) {
137 1.5 riastrad ret = dev->driver->set_busid(dev, master);
138 1.5 riastrad if (ret) {
139 1.5 riastrad drm_unset_busid(dev, master);
140 1.5 riastrad return ret;
141 1.5 riastrad }
142 1.5 riastrad } else {
143 1.5 riastrad if (WARN(dev->unique == NULL,
144 1.5 riastrad "No drm_driver.set_busid() implementation provided by "
145 1.5 riastrad "%ps. Use drm_dev_set_unique() to set the unique "
146 1.5 riastrad "name explicitly.", dev->driver))
147 1.5 riastrad return -EINVAL;
148 1.5 riastrad
149 1.5 riastrad master->unique = kstrdup(dev->unique, GFP_KERNEL);
150 1.5 riastrad if (master->unique)
151 1.5 riastrad master->unique_len = strlen(dev->unique);
152 1.5 riastrad }
153 1.5 riastrad
154 1.1 riastrad return 0;
155 1.1 riastrad }
156 1.1 riastrad
157 1.5 riastrad /*
158 1.1 riastrad * Get a mapping information.
159 1.1 riastrad *
160 1.1 riastrad * \param inode device inode.
161 1.1 riastrad * \param file_priv DRM file private.
162 1.1 riastrad * \param cmd command.
163 1.1 riastrad * \param arg user argument, pointing to a drm_map structure.
164 1.1 riastrad *
165 1.1 riastrad * \return zero on success or a negative number on failure.
166 1.1 riastrad *
167 1.1 riastrad * Searches for the mapping with the specified offset and copies its information
168 1.1 riastrad * into userspace
169 1.1 riastrad */
170 1.5 riastrad static int drm_getmap(struct drm_device *dev, void *data,
171 1.1 riastrad struct drm_file *file_priv)
172 1.1 riastrad {
173 1.1 riastrad struct drm_map *map = data;
174 1.1 riastrad struct drm_map_list *r_list = NULL;
175 1.1 riastrad struct list_head *list;
176 1.1 riastrad int idx;
177 1.1 riastrad int i;
178 1.1 riastrad
179 1.1 riastrad idx = map->offset;
180 1.1 riastrad if (idx < 0)
181 1.1 riastrad return -EINVAL;
182 1.1 riastrad
183 1.1 riastrad i = 0;
184 1.1 riastrad mutex_lock(&dev->struct_mutex);
185 1.1 riastrad list_for_each(list, &dev->maplist) {
186 1.1 riastrad if (i == idx) {
187 1.1 riastrad r_list = list_entry(list, struct drm_map_list, head);
188 1.1 riastrad break;
189 1.1 riastrad }
190 1.1 riastrad i++;
191 1.1 riastrad }
192 1.1 riastrad if (!r_list || !r_list->map) {
193 1.1 riastrad mutex_unlock(&dev->struct_mutex);
194 1.1 riastrad return -EINVAL;
195 1.1 riastrad }
196 1.1 riastrad
197 1.1 riastrad map->offset = r_list->map->offset;
198 1.1 riastrad map->size = r_list->map->size;
199 1.1 riastrad map->type = r_list->map->type;
200 1.1 riastrad map->flags = r_list->map->flags;
201 1.1 riastrad map->handle = (void *)(unsigned long) r_list->user_token;
202 1.5 riastrad map->mtrr = arch_phys_wc_index(r_list->map->mtrr);
203 1.3 riastrad
204 1.1 riastrad mutex_unlock(&dev->struct_mutex);
205 1.1 riastrad
206 1.1 riastrad return 0;
207 1.1 riastrad }
208 1.1 riastrad
209 1.5 riastrad /*
210 1.1 riastrad * Get client information.
211 1.1 riastrad *
212 1.1 riastrad * \param inode device inode.
213 1.1 riastrad * \param file_priv DRM file private.
214 1.1 riastrad * \param cmd command.
215 1.1 riastrad * \param arg user argument, pointing to a drm_client structure.
216 1.1 riastrad *
217 1.1 riastrad * \return zero on success or a negative number on failure.
218 1.1 riastrad *
219 1.1 riastrad * Searches for the client with the specified index and copies its information
220 1.1 riastrad * into userspace
221 1.1 riastrad */
222 1.5 riastrad static int drm_getclient(struct drm_device *dev, void *data,
223 1.1 riastrad struct drm_file *file_priv)
224 1.1 riastrad {
225 1.1 riastrad struct drm_client *client = data;
226 1.1 riastrad
227 1.3 riastrad /*
228 1.3 riastrad * Hollowed-out getclient ioctl to keep some dead old drm tests/tools
229 1.3 riastrad * not breaking completely. Userspace tools stop enumerating one they
230 1.3 riastrad * get -EINVAL, hence this is the return value we need to hand back for
231 1.3 riastrad * no clients tracked.
232 1.3 riastrad *
233 1.3 riastrad * Unfortunately some clients (*cough* libva *cough*) use this in a fun
234 1.3 riastrad * attempt to figure out whether they're authenticated or not. Since
235 1.3 riastrad * that's the only thing they care about, give it to the directly
236 1.3 riastrad * instead of walking one giant list.
237 1.3 riastrad */
238 1.3 riastrad if (client->idx == 0) {
239 1.3 riastrad client->auth = file_priv->authenticated;
240 1.2 riastrad #ifdef __NetBSD__ /* XXX Too scary to contemplate. */
241 1.4 riastrad client->pid = curproc->p_pid;
242 1.4 riastrad client->uid = kauth_cred_geteuid(curproc->p_cred);
243 1.2 riastrad #else
244 1.3 riastrad client->pid = pid_vnr(file_priv->pid);
245 1.3 riastrad client->uid = from_kuid_munged(current_user_ns(),
246 1.3 riastrad file_priv->uid);
247 1.2 riastrad #endif
248 1.3 riastrad client->magic = 0;
249 1.3 riastrad client->iocs = 0;
250 1.1 riastrad
251 1.3 riastrad return 0;
252 1.3 riastrad } else {
253 1.3 riastrad return -EINVAL;
254 1.1 riastrad }
255 1.1 riastrad }
256 1.1 riastrad
257 1.5 riastrad /*
258 1.1 riastrad * Get statistics information.
259 1.1 riastrad *
260 1.1 riastrad * \param inode device inode.
261 1.1 riastrad * \param file_priv DRM file private.
262 1.1 riastrad * \param cmd command.
263 1.1 riastrad * \param arg user argument, pointing to a drm_stats structure.
264 1.1 riastrad *
265 1.1 riastrad * \return zero on success or a negative number on failure.
266 1.1 riastrad */
267 1.5 riastrad static int drm_getstats(struct drm_device *dev, void *data,
268 1.1 riastrad struct drm_file *file_priv)
269 1.1 riastrad {
270 1.1 riastrad struct drm_stats *stats = data;
271 1.1 riastrad
272 1.3 riastrad /* Clear stats to prevent userspace from eating its stack garbage. */
273 1.1 riastrad memset(stats, 0, sizeof(*stats));
274 1.1 riastrad
275 1.1 riastrad return 0;
276 1.1 riastrad }
277 1.1 riastrad
278 1.5 riastrad /*
279 1.1 riastrad * Get device/driver capabilities
280 1.1 riastrad */
281 1.5 riastrad static int drm_getcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
282 1.1 riastrad {
283 1.1 riastrad struct drm_get_cap *req = data;
284 1.1 riastrad
285 1.1 riastrad req->value = 0;
286 1.1 riastrad switch (req->capability) {
287 1.1 riastrad case DRM_CAP_DUMB_BUFFER:
288 1.1 riastrad if (dev->driver->dumb_create)
289 1.1 riastrad req->value = 1;
290 1.1 riastrad break;
291 1.1 riastrad case DRM_CAP_VBLANK_HIGH_CRTC:
292 1.1 riastrad req->value = 1;
293 1.1 riastrad break;
294 1.1 riastrad case DRM_CAP_DUMB_PREFERRED_DEPTH:
295 1.1 riastrad req->value = dev->mode_config.preferred_depth;
296 1.1 riastrad break;
297 1.1 riastrad case DRM_CAP_DUMB_PREFER_SHADOW:
298 1.1 riastrad req->value = dev->mode_config.prefer_shadow;
299 1.1 riastrad break;
300 1.1 riastrad case DRM_CAP_PRIME:
301 1.1 riastrad req->value |= dev->driver->prime_fd_to_handle ? DRM_PRIME_CAP_IMPORT : 0;
302 1.1 riastrad req->value |= dev->driver->prime_handle_to_fd ? DRM_PRIME_CAP_EXPORT : 0;
303 1.1 riastrad break;
304 1.1 riastrad case DRM_CAP_TIMESTAMP_MONOTONIC:
305 1.1 riastrad req->value = drm_timestamp_monotonic;
306 1.1 riastrad break;
307 1.3 riastrad case DRM_CAP_ASYNC_PAGE_FLIP:
308 1.3 riastrad req->value = dev->mode_config.async_page_flip;
309 1.3 riastrad break;
310 1.3 riastrad case DRM_CAP_CURSOR_WIDTH:
311 1.3 riastrad if (dev->mode_config.cursor_width)
312 1.3 riastrad req->value = dev->mode_config.cursor_width;
313 1.3 riastrad else
314 1.3 riastrad req->value = 64;
315 1.3 riastrad break;
316 1.3 riastrad case DRM_CAP_CURSOR_HEIGHT:
317 1.3 riastrad if (dev->mode_config.cursor_height)
318 1.3 riastrad req->value = dev->mode_config.cursor_height;
319 1.3 riastrad else
320 1.3 riastrad req->value = 64;
321 1.3 riastrad break;
322 1.5 riastrad case DRM_CAP_ADDFB2_MODIFIERS:
323 1.5 riastrad req->value = dev->mode_config.allow_fb_modifiers;
324 1.5 riastrad break;
325 1.3 riastrad default:
326 1.3 riastrad return -EINVAL;
327 1.3 riastrad }
328 1.3 riastrad return 0;
329 1.3 riastrad }
330 1.3 riastrad
331 1.5 riastrad /*
332 1.3 riastrad * Set device/driver capabilities
333 1.3 riastrad */
334 1.5 riastrad static int
335 1.3 riastrad drm_setclientcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
336 1.3 riastrad {
337 1.3 riastrad struct drm_set_client_cap *req = data;
338 1.3 riastrad
339 1.3 riastrad switch (req->capability) {
340 1.3 riastrad case DRM_CLIENT_CAP_STEREO_3D:
341 1.3 riastrad if (req->value > 1)
342 1.3 riastrad return -EINVAL;
343 1.3 riastrad file_priv->stereo_allowed = req->value;
344 1.3 riastrad break;
345 1.3 riastrad case DRM_CLIENT_CAP_UNIVERSAL_PLANES:
346 1.5 riastrad if (req->value > 1)
347 1.5 riastrad return -EINVAL;
348 1.5 riastrad file_priv->universal_planes = req->value;
349 1.5 riastrad break;
350 1.5 riastrad case DRM_CLIENT_CAP_ATOMIC:
351 1.5 riastrad if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
352 1.3 riastrad return -EINVAL;
353 1.3 riastrad if (req->value > 1)
354 1.3 riastrad return -EINVAL;
355 1.5 riastrad file_priv->atomic = req->value;
356 1.3 riastrad file_priv->universal_planes = req->value;
357 1.3 riastrad break;
358 1.1 riastrad default:
359 1.1 riastrad return -EINVAL;
360 1.1 riastrad }
361 1.3 riastrad
362 1.1 riastrad return 0;
363 1.1 riastrad }
364 1.1 riastrad
365 1.5 riastrad /*
366 1.1 riastrad * Setversion ioctl.
367 1.1 riastrad *
368 1.1 riastrad * \param inode device inode.
369 1.1 riastrad * \param file_priv DRM file private.
370 1.1 riastrad * \param cmd command.
371 1.1 riastrad * \param arg user argument, pointing to a drm_lock structure.
372 1.1 riastrad * \return zero on success or negative number on failure.
373 1.1 riastrad *
374 1.1 riastrad * Sets the requested interface version
375 1.1 riastrad */
376 1.5 riastrad static int drm_setversion(struct drm_device *dev, void *data, struct drm_file *file_priv)
377 1.1 riastrad {
378 1.1 riastrad struct drm_set_version *sv = data;
379 1.1 riastrad int if_version, retcode = 0;
380 1.1 riastrad
381 1.1 riastrad if (sv->drm_di_major != -1) {
382 1.1 riastrad if (sv->drm_di_major != DRM_IF_MAJOR ||
383 1.1 riastrad sv->drm_di_minor < 0 || sv->drm_di_minor > DRM_IF_MINOR) {
384 1.1 riastrad retcode = -EINVAL;
385 1.1 riastrad goto done;
386 1.1 riastrad }
387 1.1 riastrad if_version = DRM_IF_VERSION(sv->drm_di_major,
388 1.1 riastrad sv->drm_di_minor);
389 1.1 riastrad dev->if_version = max(if_version, dev->if_version);
390 1.1 riastrad if (sv->drm_di_minor >= 1) {
391 1.1 riastrad /*
392 1.1 riastrad * Version 1.1 includes tying of DRM to specific device
393 1.1 riastrad * Version 1.4 has proper PCI domain support
394 1.1 riastrad */
395 1.1 riastrad retcode = drm_set_busid(dev, file_priv);
396 1.1 riastrad if (retcode)
397 1.1 riastrad goto done;
398 1.1 riastrad }
399 1.1 riastrad }
400 1.1 riastrad
401 1.1 riastrad if (sv->drm_dd_major != -1) {
402 1.1 riastrad if (sv->drm_dd_major != dev->driver->major ||
403 1.1 riastrad sv->drm_dd_minor < 0 || sv->drm_dd_minor >
404 1.1 riastrad dev->driver->minor) {
405 1.1 riastrad retcode = -EINVAL;
406 1.1 riastrad goto done;
407 1.1 riastrad }
408 1.1 riastrad }
409 1.1 riastrad
410 1.1 riastrad done:
411 1.1 riastrad sv->drm_di_major = DRM_IF_MAJOR;
412 1.1 riastrad sv->drm_di_minor = DRM_IF_MINOR;
413 1.1 riastrad sv->drm_dd_major = dev->driver->major;
414 1.1 riastrad sv->drm_dd_minor = dev->driver->minor;
415 1.1 riastrad
416 1.1 riastrad return retcode;
417 1.1 riastrad }
418 1.1 riastrad
419 1.5 riastrad /**
420 1.5 riastrad * drm_noop - DRM no-op ioctl implemntation
421 1.5 riastrad * @dev: DRM device for the ioctl
422 1.5 riastrad * @data: data pointer for the ioctl
423 1.5 riastrad * @file_priv: DRM file for the ioctl call
424 1.5 riastrad *
425 1.5 riastrad * This no-op implementation for drm ioctls is useful for deprecated
426 1.5 riastrad * functionality where we can't return a failure code because existing userspace
427 1.5 riastrad * checks the result of the ioctl, but doesn't care about the action.
428 1.5 riastrad *
429 1.5 riastrad * Always returns successfully with 0.
430 1.5 riastrad */
431 1.1 riastrad int drm_noop(struct drm_device *dev, void *data,
432 1.1 riastrad struct drm_file *file_priv)
433 1.1 riastrad {
434 1.1 riastrad DRM_DEBUG("\n");
435 1.1 riastrad return 0;
436 1.1 riastrad }
437 1.1 riastrad EXPORT_SYMBOL(drm_noop);
438 1.5 riastrad
439 1.5 riastrad /**
440 1.5 riastrad * drm_invalid_op - DRM invalid ioctl implemntation
441 1.5 riastrad * @dev: DRM device for the ioctl
442 1.5 riastrad * @data: data pointer for the ioctl
443 1.5 riastrad * @file_priv: DRM file for the ioctl call
444 1.5 riastrad *
445 1.5 riastrad * This no-op implementation for drm ioctls is useful for deprecated
446 1.5 riastrad * functionality where we really don't want to allow userspace to call the ioctl
447 1.5 riastrad * any more. This is the case for old ums interfaces for drivers that
448 1.5 riastrad * transitioned to kms gradually and so kept the old legacy tables around. This
449 1.5 riastrad * only applies to radeon and i915 kms drivers, other drivers shouldn't need to
450 1.5 riastrad * use this function.
451 1.5 riastrad *
452 1.5 riastrad * Always fails with a return value of -EINVAL.
453 1.5 riastrad */
454 1.5 riastrad int drm_invalid_op(struct drm_device *dev, void *data,
455 1.5 riastrad struct drm_file *file_priv)
456 1.5 riastrad {
457 1.5 riastrad return -EINVAL;
458 1.5 riastrad }
459 1.5 riastrad EXPORT_SYMBOL(drm_invalid_op);
460 1.5 riastrad
461 1.5 riastrad /*
462 1.5 riastrad * Copy and IOCTL return string to user space
463 1.5 riastrad */
464 1.5 riastrad static int drm_copy_field(char __user *buf, size_t *buf_len, const char *value)
465 1.5 riastrad {
466 1.5 riastrad int len;
467 1.5 riastrad
468 1.5 riastrad /* don't overflow userbuf */
469 1.5 riastrad len = strlen(value);
470 1.5 riastrad if (len > *buf_len)
471 1.5 riastrad len = *buf_len;
472 1.5 riastrad
473 1.5 riastrad /* let userspace know exact length of driver value (which could be
474 1.5 riastrad * larger than the userspace-supplied buffer) */
475 1.5 riastrad *buf_len = strlen(value);
476 1.5 riastrad
477 1.5 riastrad /* finally, try filling in the userbuf */
478 1.5 riastrad if (len && buf)
479 1.5 riastrad if (copy_to_user(buf, value, len))
480 1.5 riastrad return -EFAULT;
481 1.5 riastrad return 0;
482 1.5 riastrad }
483 1.5 riastrad
484 1.5 riastrad /*
485 1.5 riastrad * Get version information
486 1.5 riastrad *
487 1.5 riastrad * \param inode device inode.
488 1.5 riastrad * \param filp file pointer.
489 1.5 riastrad * \param cmd command.
490 1.5 riastrad * \param arg user argument, pointing to a drm_version structure.
491 1.5 riastrad * \return zero on success or negative number on failure.
492 1.5 riastrad *
493 1.5 riastrad * Fills in the version information in \p arg.
494 1.5 riastrad */
495 1.5 riastrad static int drm_version(struct drm_device *dev, void *data,
496 1.5 riastrad struct drm_file *file_priv)
497 1.5 riastrad {
498 1.5 riastrad struct drm_version *version = data;
499 1.5 riastrad int err;
500 1.5 riastrad
501 1.5 riastrad version->version_major = dev->driver->major;
502 1.5 riastrad version->version_minor = dev->driver->minor;
503 1.5 riastrad version->version_patchlevel = dev->driver->patchlevel;
504 1.5 riastrad err = drm_copy_field(version->name, &version->name_len,
505 1.5 riastrad dev->driver->name);
506 1.5 riastrad if (!err)
507 1.5 riastrad err = drm_copy_field(version->date, &version->date_len,
508 1.5 riastrad dev->driver->date);
509 1.5 riastrad if (!err)
510 1.5 riastrad err = drm_copy_field(version->desc, &version->desc_len,
511 1.5 riastrad dev->driver->desc);
512 1.5 riastrad
513 1.5 riastrad return err;
514 1.5 riastrad }
515 1.5 riastrad
516 1.5 riastrad /*
517 1.5 riastrad * drm_ioctl_permit - Check ioctl permissions against caller
518 1.5 riastrad *
519 1.5 riastrad * @flags: ioctl permission flags.
520 1.5 riastrad * @file_priv: Pointer to struct drm_file identifying the caller.
521 1.5 riastrad *
522 1.5 riastrad * Checks whether the caller is allowed to run an ioctl with the
523 1.5 riastrad * indicated permissions. If so, returns zero. Otherwise returns an
524 1.5 riastrad * error code suitable for ioctl return.
525 1.5 riastrad */
526 1.5 riastrad int drm_ioctl_permit(u32 flags, struct drm_file *file_priv)
527 1.5 riastrad {
528 1.5 riastrad /* ROOT_ONLY is only for CAP_SYS_ADMIN */
529 1.5 riastrad if (unlikely((flags & DRM_ROOT_ONLY) && !capable(CAP_SYS_ADMIN)))
530 1.5 riastrad return -EACCES;
531 1.5 riastrad
532 1.5 riastrad /* AUTH is only for authenticated or render client */
533 1.5 riastrad if (unlikely((flags & DRM_AUTH) && !drm_is_render_client(file_priv) &&
534 1.5 riastrad !file_priv->authenticated))
535 1.5 riastrad return -EACCES;
536 1.5 riastrad
537 1.5 riastrad /* MASTER is only for master or control clients */
538 1.5 riastrad if (unlikely((flags & DRM_MASTER) && !file_priv->is_master &&
539 1.5 riastrad !drm_is_control_client(file_priv)))
540 1.5 riastrad return -EACCES;
541 1.5 riastrad
542 1.5 riastrad /* Control clients must be explicitly allowed */
543 1.5 riastrad if (unlikely(!(flags & DRM_CONTROL_ALLOW) &&
544 1.5 riastrad drm_is_control_client(file_priv)))
545 1.5 riastrad return -EACCES;
546 1.5 riastrad
547 1.5 riastrad /* Render clients must be explicitly allowed */
548 1.5 riastrad if (unlikely(!(flags & DRM_RENDER_ALLOW) &&
549 1.5 riastrad drm_is_render_client(file_priv)))
550 1.5 riastrad return -EACCES;
551 1.5 riastrad
552 1.5 riastrad return 0;
553 1.5 riastrad }
554 1.5 riastrad EXPORT_SYMBOL(drm_ioctl_permit);
555 1.5 riastrad
556 1.5 riastrad #define DRM_IOCTL_DEF(ioctl, _func, _flags) \
557 1.5 riastrad [DRM_IOCTL_NR(ioctl)] = { \
558 1.5 riastrad .cmd = ioctl, \
559 1.5 riastrad .func = _func, \
560 1.5 riastrad .flags = _flags, \
561 1.5 riastrad .name = #ioctl \
562 1.5 riastrad }
563 1.5 riastrad
564 1.5 riastrad /* Ioctl table */
565 1.5 riastrad static const struct drm_ioctl_desc drm_ioctls[] = {
566 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_VERSION, drm_version,
567 1.5 riastrad DRM_UNLOCKED|DRM_RENDER_ALLOW|DRM_CONTROL_ALLOW),
568 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GET_UNIQUE, drm_getunique, 0),
569 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GET_MAGIC, drm_getmagic, 0),
570 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_IRQ_BUSID, drm_irq_by_busid, DRM_MASTER|DRM_ROOT_ONLY),
571 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GET_MAP, drm_getmap, DRM_UNLOCKED),
572 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GET_CLIENT, drm_getclient, DRM_UNLOCKED),
573 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GET_STATS, drm_getstats, DRM_UNLOCKED),
574 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GET_CAP, drm_getcap, DRM_UNLOCKED|DRM_RENDER_ALLOW),
575 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SET_CLIENT_CAP, drm_setclientcap, 0),
576 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SET_VERSION, drm_setversion, DRM_MASTER),
577 1.5 riastrad
578 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SET_UNIQUE, drm_setunique, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
579 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_BLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
580 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_UNBLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
581 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AUTH_MAGIC, drm_authmagic, DRM_AUTH|DRM_MASTER),
582 1.5 riastrad
583 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_ADD_MAP, drm_legacy_addmap_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
584 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_RM_MAP, drm_legacy_rmmap_ioctl, DRM_AUTH),
585 1.5 riastrad
586 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SET_SAREA_CTX, drm_legacy_setsareactx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
587 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GET_SAREA_CTX, drm_legacy_getsareactx, DRM_AUTH),
588 1.5 riastrad
589 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SET_MASTER, drm_setmaster_ioctl, DRM_ROOT_ONLY),
590 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_DROP_MASTER, drm_dropmaster_ioctl, DRM_ROOT_ONLY),
591 1.5 riastrad
592 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_ADD_CTX, drm_legacy_addctx, DRM_AUTH|DRM_ROOT_ONLY),
593 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_RM_CTX, drm_legacy_rmctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
594 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MOD_CTX, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
595 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GET_CTX, drm_legacy_getctx, DRM_AUTH),
596 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SWITCH_CTX, drm_legacy_switchctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
597 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_NEW_CTX, drm_legacy_newctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
598 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_RES_CTX, drm_legacy_resctx, DRM_AUTH),
599 1.5 riastrad
600 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_ADD_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
601 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_RM_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
602 1.5 riastrad
603 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_LOCK, drm_legacy_lock, DRM_AUTH),
604 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_UNLOCK, drm_legacy_unlock, DRM_AUTH),
605 1.5 riastrad
606 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_FINISH, drm_noop, DRM_AUTH),
607 1.5 riastrad
608 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_ADD_BUFS, drm_legacy_addbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
609 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MARK_BUFS, drm_legacy_markbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
610 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_INFO_BUFS, drm_legacy_infobufs, DRM_AUTH),
611 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MAP_BUFS, drm_legacy_mapbufs, DRM_AUTH),
612 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_FREE_BUFS, drm_legacy_freebufs, DRM_AUTH),
613 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_DMA, drm_legacy_dma_ioctl, DRM_AUTH),
614 1.5 riastrad
615 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_CONTROL, drm_control, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
616 1.5 riastrad
617 1.5 riastrad #if IS_ENABLED(CONFIG_AGP)
618 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_ACQUIRE, drm_agp_acquire_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
619 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_RELEASE, drm_agp_release_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
620 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_ENABLE, drm_agp_enable_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
621 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_INFO, drm_agp_info_ioctl, DRM_AUTH),
622 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_ALLOC, drm_agp_alloc_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
623 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_FREE, drm_agp_free_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
624 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_BIND, drm_agp_bind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
625 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_AGP_UNBIND, drm_agp_unbind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
626 1.5 riastrad #endif
627 1.5 riastrad
628 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SG_ALLOC, drm_legacy_sg_alloc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
629 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_SG_FREE, drm_legacy_sg_free, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
630 1.5 riastrad
631 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_WAIT_VBLANK, drm_wait_vblank, DRM_UNLOCKED),
632 1.5 riastrad
633 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODESET_CTL, drm_modeset_ctl, 0),
634 1.5 riastrad
635 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_UPDATE_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
636 1.5 riastrad
637 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GEM_CLOSE, drm_gem_close_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
638 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GEM_FLINK, drm_gem_flink_ioctl, DRM_AUTH|DRM_UNLOCKED),
639 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_GEM_OPEN, drm_gem_open_ioctl, DRM_AUTH|DRM_UNLOCKED),
640 1.5 riastrad
641 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETRESOURCES, drm_mode_getresources, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
642 1.5 riastrad
643 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_PRIME_HANDLE_TO_FD, drm_prime_handle_to_fd_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
644 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_PRIME_FD_TO_HANDLE, drm_prime_fd_to_handle_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
645 1.5 riastrad
646 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANERESOURCES, drm_mode_getplane_res, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
647 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCRTC, drm_mode_getcrtc, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
648 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETCRTC, drm_mode_setcrtc, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
649 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANE, drm_mode_getplane, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
650 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPLANE, drm_mode_setplane, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
651 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR, drm_mode_cursor_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
652 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETGAMMA, drm_mode_gamma_get_ioctl, DRM_UNLOCKED),
653 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETGAMMA, drm_mode_gamma_set_ioctl, DRM_MASTER|DRM_UNLOCKED),
654 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETENCODER, drm_mode_getencoder, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
655 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCONNECTOR, drm_mode_getconnector, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
656 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_ATTACHMODE, drm_noop, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
657 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_DETACHMODE, drm_noop, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
658 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPERTY, drm_mode_getproperty_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
659 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPROPERTY, drm_mode_connector_property_set_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
660 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPBLOB, drm_mode_getblob_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
661 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETFB, drm_mode_getfb, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
662 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB, drm_mode_addfb, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
663 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB2, drm_mode_addfb2, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
664 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_RMFB, drm_mode_rmfb, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
665 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_PAGE_FLIP, drm_mode_page_flip_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
666 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_DIRTYFB, drm_mode_dirtyfb_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
667 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATE_DUMB, drm_mode_create_dumb_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
668 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_MAP_DUMB, drm_mode_mmap_dumb_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
669 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_DESTROY_DUMB, drm_mode_destroy_dumb_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
670 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_OBJ_GETPROPERTIES, drm_mode_obj_get_properties_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
671 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_OBJ_SETPROPERTY, drm_mode_obj_set_property_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
672 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR2, drm_mode_cursor2_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
673 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_ATOMIC, drm_mode_atomic_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
674 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATEPROPBLOB, drm_mode_createblob_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
675 1.5 riastrad DRM_IOCTL_DEF(DRM_IOCTL_MODE_DESTROYPROPBLOB, drm_mode_destroyblob_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
676 1.5 riastrad };
677 1.5 riastrad
678 1.5 riastrad #define DRM_CORE_IOCTL_COUNT ARRAY_SIZE( drm_ioctls )
679 1.5 riastrad
680 1.5 riastrad /**
681 1.5 riastrad * drm_ioctl - ioctl callback implementation for DRM drivers
682 1.5 riastrad * @filp: file this ioctl is called on
683 1.5 riastrad * @cmd: ioctl cmd number
684 1.5 riastrad * @arg: user argument
685 1.5 riastrad *
686 1.5 riastrad * Looks up the ioctl function in the ::ioctls table, checking for root
687 1.5 riastrad * previleges if so required, and dispatches to the respective function.
688 1.5 riastrad *
689 1.5 riastrad * Returns:
690 1.5 riastrad * Zero on success, negative error code on failure.
691 1.5 riastrad */
692 1.5 riastrad long drm_ioctl(struct file *filp,
693 1.5 riastrad unsigned int cmd, unsigned long arg)
694 1.5 riastrad {
695 1.5 riastrad struct drm_file *file_priv = filp->private_data;
696 1.5 riastrad struct drm_device *dev;
697 1.5 riastrad const struct drm_ioctl_desc *ioctl = NULL;
698 1.5 riastrad drm_ioctl_t *func;
699 1.5 riastrad unsigned int nr = DRM_IOCTL_NR(cmd);
700 1.5 riastrad int retcode = -EINVAL;
701 1.5 riastrad char stack_kdata[128];
702 1.5 riastrad char *kdata = NULL;
703 1.5 riastrad unsigned int usize, asize, drv_size;
704 1.5 riastrad bool is_driver_ioctl;
705 1.5 riastrad
706 1.5 riastrad dev = file_priv->minor->dev;
707 1.5 riastrad
708 1.5 riastrad if (drm_device_is_unplugged(dev))
709 1.5 riastrad return -ENODEV;
710 1.5 riastrad
711 1.5 riastrad is_driver_ioctl = nr >= DRM_COMMAND_BASE && nr < DRM_COMMAND_END;
712 1.5 riastrad
713 1.5 riastrad if (is_driver_ioctl) {
714 1.5 riastrad /* driver ioctl */
715 1.5 riastrad if (nr - DRM_COMMAND_BASE >= dev->driver->num_ioctls)
716 1.5 riastrad goto err_i1;
717 1.5 riastrad ioctl = &dev->driver->ioctls[nr - DRM_COMMAND_BASE];
718 1.5 riastrad } else {
719 1.5 riastrad /* core ioctl */
720 1.5 riastrad if (nr >= DRM_CORE_IOCTL_COUNT)
721 1.5 riastrad goto err_i1;
722 1.5 riastrad ioctl = &drm_ioctls[nr];
723 1.5 riastrad }
724 1.5 riastrad
725 1.5 riastrad drv_size = _IOC_SIZE(ioctl->cmd);
726 1.5 riastrad usize = _IOC_SIZE(cmd);
727 1.5 riastrad asize = max(usize, drv_size);
728 1.5 riastrad cmd = ioctl->cmd;
729 1.5 riastrad
730 1.5 riastrad DRM_DEBUG("pid=%d, dev=0x%lx, auth=%d, %s\n",
731 1.5 riastrad task_pid_nr(current),
732 1.5 riastrad (long)old_encode_dev(file_priv->minor->kdev->devt),
733 1.5 riastrad file_priv->authenticated, ioctl->name);
734 1.5 riastrad
735 1.5 riastrad /* Do not trust userspace, use our own definition */
736 1.5 riastrad func = ioctl->func;
737 1.5 riastrad
738 1.5 riastrad if (unlikely(!func)) {
739 1.5 riastrad DRM_DEBUG("no function\n");
740 1.5 riastrad retcode = -EINVAL;
741 1.5 riastrad goto err_i1;
742 1.5 riastrad }
743 1.5 riastrad
744 1.5 riastrad retcode = drm_ioctl_permit(ioctl->flags, file_priv);
745 1.5 riastrad if (unlikely(retcode))
746 1.5 riastrad goto err_i1;
747 1.5 riastrad
748 1.5 riastrad if (cmd & (IOC_IN | IOC_OUT)) {
749 1.5 riastrad if (asize <= sizeof(stack_kdata)) {
750 1.5 riastrad kdata = stack_kdata;
751 1.5 riastrad } else {
752 1.5 riastrad kdata = kmalloc(asize, GFP_KERNEL);
753 1.5 riastrad if (!kdata) {
754 1.5 riastrad retcode = -ENOMEM;
755 1.5 riastrad goto err_i1;
756 1.5 riastrad }
757 1.5 riastrad }
758 1.5 riastrad if (asize > usize)
759 1.5 riastrad memset(kdata + usize, 0, asize - usize);
760 1.5 riastrad }
761 1.5 riastrad
762 1.5 riastrad if (cmd & IOC_IN) {
763 1.5 riastrad if (copy_from_user(kdata, (void __user *)arg,
764 1.5 riastrad usize) != 0) {
765 1.5 riastrad retcode = -EFAULT;
766 1.5 riastrad goto err_i1;
767 1.5 riastrad }
768 1.5 riastrad } else if (cmd & IOC_OUT) {
769 1.5 riastrad memset(kdata, 0, usize);
770 1.5 riastrad }
771 1.5 riastrad
772 1.5 riastrad /* Enforce sane locking for kms driver ioctls. Core ioctls are
773 1.5 riastrad * too messy still. */
774 1.5 riastrad if ((drm_core_check_feature(dev, DRIVER_MODESET) && is_driver_ioctl) ||
775 1.5 riastrad (ioctl->flags & DRM_UNLOCKED))
776 1.5 riastrad retcode = func(dev, kdata, file_priv);
777 1.5 riastrad else {
778 1.5 riastrad mutex_lock(&drm_global_mutex);
779 1.5 riastrad retcode = func(dev, kdata, file_priv);
780 1.5 riastrad mutex_unlock(&drm_global_mutex);
781 1.5 riastrad }
782 1.5 riastrad
783 1.5 riastrad if (cmd & IOC_OUT) {
784 1.5 riastrad if (copy_to_user((void __user *)arg, kdata,
785 1.5 riastrad usize) != 0)
786 1.5 riastrad retcode = -EFAULT;
787 1.5 riastrad }
788 1.5 riastrad
789 1.5 riastrad err_i1:
790 1.5 riastrad if (!ioctl)
791 1.5 riastrad DRM_DEBUG("invalid ioctl: pid=%d, dev=0x%lx, auth=%d, cmd=0x%02x, nr=0x%02x\n",
792 1.5 riastrad task_pid_nr(current),
793 1.5 riastrad (long)old_encode_dev(file_priv->minor->kdev->devt),
794 1.5 riastrad file_priv->authenticated, cmd, nr);
795 1.5 riastrad
796 1.5 riastrad if (kdata != stack_kdata)
797 1.5 riastrad kfree(kdata);
798 1.5 riastrad if (retcode)
799 1.5 riastrad DRM_DEBUG("ret = %d\n", retcode);
800 1.5 riastrad return retcode;
801 1.5 riastrad }
802 1.5 riastrad EXPORT_SYMBOL(drm_ioctl);
803 1.5 riastrad
804 1.5 riastrad /**
805 1.5 riastrad * drm_ioctl_flags - Check for core ioctl and return ioctl permission flags
806 1.5 riastrad * @nr: ioctl number
807 1.5 riastrad * @flags: where to return the ioctl permission flags
808 1.5 riastrad *
809 1.5 riastrad * This ioctl is only used by the vmwgfx driver to augment the access checks
810 1.5 riastrad * done by the drm core and insofar a pretty decent layering violation. This
811 1.5 riastrad * shouldn't be used by any drivers.
812 1.5 riastrad *
813 1.5 riastrad * Returns:
814 1.5 riastrad * True if the @nr corresponds to a DRM core ioctl numer, false otherwise.
815 1.5 riastrad */
816 1.5 riastrad bool drm_ioctl_flags(unsigned int nr, unsigned int *flags)
817 1.5 riastrad {
818 1.5 riastrad if (nr >= DRM_COMMAND_BASE && nr < DRM_COMMAND_END)
819 1.5 riastrad return false;
820 1.5 riastrad
821 1.5 riastrad if (nr >= DRM_CORE_IOCTL_COUNT)
822 1.5 riastrad return false;
823 1.5 riastrad
824 1.5 riastrad *flags = drm_ioctls[nr].flags;
825 1.5 riastrad return true;
826 1.5 riastrad }
827 1.5 riastrad EXPORT_SYMBOL(drm_ioctl_flags);
828