diff options
Diffstat (limited to 'drivers/media/usb/uvc/uvc_v4l2.c')
-rw-r--r-- | drivers/media/usb/uvc/uvc_v4l2.c | 1317 |
1 files changed, 1317 insertions, 0 deletions
diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c new file mode 100644 index 000000000000..f00db3060e0e --- /dev/null +++ b/drivers/media/usb/uvc/uvc_v4l2.c | |||
@@ -0,0 +1,1317 @@ | |||
1 | /* | ||
2 | * uvc_v4l2.c -- USB Video Class driver - V4L2 API | ||
3 | * | ||
4 | * Copyright (C) 2005-2010 | ||
5 | * Laurent Pinchart (laurent.pinchart@ideasonboard.com) | ||
6 | * | ||
7 | * This program is free software; you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU General Public License as published by | ||
9 | * the Free Software Foundation; either version 2 of the License, or | ||
10 | * (at your option) any later version. | ||
11 | * | ||
12 | */ | ||
13 | |||
14 | #include <linux/compat.h> | ||
15 | #include <linux/kernel.h> | ||
16 | #include <linux/version.h> | ||
17 | #include <linux/list.h> | ||
18 | #include <linux/module.h> | ||
19 | #include <linux/slab.h> | ||
20 | #include <linux/usb.h> | ||
21 | #include <linux/videodev2.h> | ||
22 | #include <linux/vmalloc.h> | ||
23 | #include <linux/mm.h> | ||
24 | #include <linux/wait.h> | ||
25 | #include <linux/atomic.h> | ||
26 | |||
27 | #include <media/v4l2-common.h> | ||
28 | #include <media/v4l2-ctrls.h> | ||
29 | #include <media/v4l2-event.h> | ||
30 | #include <media/v4l2-ioctl.h> | ||
31 | |||
32 | #include "uvcvideo.h" | ||
33 | |||
34 | /* ------------------------------------------------------------------------ | ||
35 | * UVC ioctls | ||
36 | */ | ||
37 | static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain, | ||
38 | struct uvc_xu_control_mapping *xmap) | ||
39 | { | ||
40 | struct uvc_control_mapping *map; | ||
41 | unsigned int size; | ||
42 | int ret; | ||
43 | |||
44 | map = kzalloc(sizeof *map, GFP_KERNEL); | ||
45 | if (map == NULL) | ||
46 | return -ENOMEM; | ||
47 | |||
48 | map->id = xmap->id; | ||
49 | memcpy(map->name, xmap->name, sizeof map->name); | ||
50 | memcpy(map->entity, xmap->entity, sizeof map->entity); | ||
51 | map->selector = xmap->selector; | ||
52 | map->size = xmap->size; | ||
53 | map->offset = xmap->offset; | ||
54 | map->v4l2_type = xmap->v4l2_type; | ||
55 | map->data_type = xmap->data_type; | ||
56 | |||
57 | switch (xmap->v4l2_type) { | ||
58 | case V4L2_CTRL_TYPE_INTEGER: | ||
59 | case V4L2_CTRL_TYPE_BOOLEAN: | ||
60 | case V4L2_CTRL_TYPE_BUTTON: | ||
61 | break; | ||
62 | |||
63 | case V4L2_CTRL_TYPE_MENU: | ||
64 | /* Prevent excessive memory consumption, as well as integer | ||
65 | * overflows. | ||
66 | */ | ||
67 | if (xmap->menu_count == 0 || | ||
68 | xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES) { | ||
69 | ret = -EINVAL; | ||
70 | goto done; | ||
71 | } | ||
72 | |||
73 | size = xmap->menu_count * sizeof(*map->menu_info); | ||
74 | map->menu_info = kmalloc(size, GFP_KERNEL); | ||
75 | if (map->menu_info == NULL) { | ||
76 | ret = -ENOMEM; | ||
77 | goto done; | ||
78 | } | ||
79 | |||
80 | if (copy_from_user(map->menu_info, xmap->menu_info, size)) { | ||
81 | ret = -EFAULT; | ||
82 | goto done; | ||
83 | } | ||
84 | |||
85 | map->menu_count = xmap->menu_count; | ||
86 | break; | ||
87 | |||
88 | default: | ||
89 | uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type " | ||
90 | "%u.\n", xmap->v4l2_type); | ||
91 | ret = -ENOTTY; | ||
92 | goto done; | ||
93 | } | ||
94 | |||
95 | ret = uvc_ctrl_add_mapping(chain, map); | ||
96 | |||
97 | done: | ||
98 | kfree(map->menu_info); | ||
99 | kfree(map); | ||
100 | |||
101 | return ret; | ||
102 | } | ||
103 | |||
104 | /* ------------------------------------------------------------------------ | ||
105 | * V4L2 interface | ||
106 | */ | ||
107 | |||
108 | /* | ||
109 | * Find the frame interval closest to the requested frame interval for the | ||
110 | * given frame format and size. This should be done by the device as part of | ||
111 | * the Video Probe and Commit negotiation, but some hardware don't implement | ||
112 | * that feature. | ||
113 | */ | ||
114 | static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval) | ||
115 | { | ||
116 | unsigned int i; | ||
117 | |||
118 | if (frame->bFrameIntervalType) { | ||
119 | __u32 best = -1, dist; | ||
120 | |||
121 | for (i = 0; i < frame->bFrameIntervalType; ++i) { | ||
122 | dist = interval > frame->dwFrameInterval[i] | ||
123 | ? interval - frame->dwFrameInterval[i] | ||
124 | : frame->dwFrameInterval[i] - interval; | ||
125 | |||
126 | if (dist > best) | ||
127 | break; | ||
128 | |||
129 | best = dist; | ||
130 | } | ||
131 | |||
132 | interval = frame->dwFrameInterval[i-1]; | ||
133 | } else { | ||
134 | const __u32 min = frame->dwFrameInterval[0]; | ||
135 | const __u32 max = frame->dwFrameInterval[1]; | ||
136 | const __u32 step = frame->dwFrameInterval[2]; | ||
137 | |||
138 | interval = min + (interval - min + step/2) / step * step; | ||
139 | if (interval > max) | ||
140 | interval = max; | ||
141 | } | ||
142 | |||
143 | return interval; | ||
144 | } | ||
145 | |||
146 | static int uvc_v4l2_try_format(struct uvc_streaming *stream, | ||
147 | struct v4l2_format *fmt, struct uvc_streaming_control *probe, | ||
148 | struct uvc_format **uvc_format, struct uvc_frame **uvc_frame) | ||
149 | { | ||
150 | struct uvc_format *format = NULL; | ||
151 | struct uvc_frame *frame = NULL; | ||
152 | __u16 rw, rh; | ||
153 | unsigned int d, maxd; | ||
154 | unsigned int i; | ||
155 | __u32 interval; | ||
156 | int ret = 0; | ||
157 | __u8 *fcc; | ||
158 | |||
159 | if (fmt->type != stream->type) | ||
160 | return -EINVAL; | ||
161 | |||
162 | fcc = (__u8 *)&fmt->fmt.pix.pixelformat; | ||
163 | uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n", | ||
164 | fmt->fmt.pix.pixelformat, | ||
165 | fcc[0], fcc[1], fcc[2], fcc[3], | ||
166 | fmt->fmt.pix.width, fmt->fmt.pix.height); | ||
167 | |||
168 | /* Check if the hardware supports the requested format. */ | ||
169 | for (i = 0; i < stream->nformats; ++i) { | ||
170 | format = &stream->format[i]; | ||
171 | if (format->fcc == fmt->fmt.pix.pixelformat) | ||
172 | break; | ||
173 | } | ||
174 | |||
175 | if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) { | ||
176 | uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n", | ||
177 | fmt->fmt.pix.pixelformat); | ||
178 | return -EINVAL; | ||
179 | } | ||
180 | |||
181 | /* Find the closest image size. The distance between image sizes is | ||
182 | * the size in pixels of the non-overlapping regions between the | ||
183 | * requested size and the frame-specified size. | ||
184 | */ | ||
185 | rw = fmt->fmt.pix.width; | ||
186 | rh = fmt->fmt.pix.height; | ||
187 | maxd = (unsigned int)-1; | ||
188 | |||
189 | for (i = 0; i < format->nframes; ++i) { | ||
190 | __u16 w = format->frame[i].wWidth; | ||
191 | __u16 h = format->frame[i].wHeight; | ||
192 | |||
193 | d = min(w, rw) * min(h, rh); | ||
194 | d = w*h + rw*rh - 2*d; | ||
195 | if (d < maxd) { | ||
196 | maxd = d; | ||
197 | frame = &format->frame[i]; | ||
198 | } | ||
199 | |||
200 | if (maxd == 0) | ||
201 | break; | ||
202 | } | ||
203 | |||
204 | if (frame == NULL) { | ||
205 | uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n", | ||
206 | fmt->fmt.pix.width, fmt->fmt.pix.height); | ||
207 | return -EINVAL; | ||
208 | } | ||
209 | |||
210 | /* Use the default frame interval. */ | ||
211 | interval = frame->dwDefaultFrameInterval; | ||
212 | uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us " | ||
213 | "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval, | ||
214 | (100000000/interval)%10); | ||
215 | |||
216 | /* Set the format index, frame index and frame interval. */ | ||
217 | memset(probe, 0, sizeof *probe); | ||
218 | probe->bmHint = 1; /* dwFrameInterval */ | ||
219 | probe->bFormatIndex = format->index; | ||
220 | probe->bFrameIndex = frame->bFrameIndex; | ||
221 | probe->dwFrameInterval = uvc_try_frame_interval(frame, interval); | ||
222 | /* Some webcams stall the probe control set request when the | ||
223 | * dwMaxVideoFrameSize field is set to zero. The UVC specification | ||
224 | * clearly states that the field is read-only from the host, so this | ||
225 | * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by | ||
226 | * the webcam to work around the problem. | ||
227 | * | ||
228 | * The workaround could probably be enabled for all webcams, so the | ||
229 | * quirk can be removed if needed. It's currently useful to detect | ||
230 | * webcam bugs and fix them before they hit the market (providing | ||
231 | * developers test their webcams with the Linux driver as well as with | ||
232 | * the Windows driver). | ||
233 | */ | ||
234 | mutex_lock(&stream->mutex); | ||
235 | if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS) | ||
236 | probe->dwMaxVideoFrameSize = | ||
237 | stream->ctrl.dwMaxVideoFrameSize; | ||
238 | |||
239 | /* Probe the device. */ | ||
240 | ret = uvc_probe_video(stream, probe); | ||
241 | mutex_unlock(&stream->mutex); | ||
242 | if (ret < 0) | ||
243 | goto done; | ||
244 | |||
245 | fmt->fmt.pix.width = frame->wWidth; | ||
246 | fmt->fmt.pix.height = frame->wHeight; | ||
247 | fmt->fmt.pix.field = V4L2_FIELD_NONE; | ||
248 | fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8; | ||
249 | fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize; | ||
250 | fmt->fmt.pix.colorspace = format->colorspace; | ||
251 | fmt->fmt.pix.priv = 0; | ||
252 | |||
253 | if (uvc_format != NULL) | ||
254 | *uvc_format = format; | ||
255 | if (uvc_frame != NULL) | ||
256 | *uvc_frame = frame; | ||
257 | |||
258 | done: | ||
259 | return ret; | ||
260 | } | ||
261 | |||
262 | static int uvc_v4l2_get_format(struct uvc_streaming *stream, | ||
263 | struct v4l2_format *fmt) | ||
264 | { | ||
265 | struct uvc_format *format; | ||
266 | struct uvc_frame *frame; | ||
267 | int ret = 0; | ||
268 | |||
269 | if (fmt->type != stream->type) | ||
270 | return -EINVAL; | ||
271 | |||
272 | mutex_lock(&stream->mutex); | ||
273 | format = stream->cur_format; | ||
274 | frame = stream->cur_frame; | ||
275 | |||
276 | if (format == NULL || frame == NULL) { | ||
277 | ret = -EINVAL; | ||
278 | goto done; | ||
279 | } | ||
280 | |||
281 | fmt->fmt.pix.pixelformat = format->fcc; | ||
282 | fmt->fmt.pix.width = frame->wWidth; | ||
283 | fmt->fmt.pix.height = frame->wHeight; | ||
284 | fmt->fmt.pix.field = V4L2_FIELD_NONE; | ||
285 | fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8; | ||
286 | fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize; | ||
287 | fmt->fmt.pix.colorspace = format->colorspace; | ||
288 | fmt->fmt.pix.priv = 0; | ||
289 | |||
290 | done: | ||
291 | mutex_unlock(&stream->mutex); | ||
292 | return ret; | ||
293 | } | ||
294 | |||
295 | static int uvc_v4l2_set_format(struct uvc_streaming *stream, | ||
296 | struct v4l2_format *fmt) | ||
297 | { | ||
298 | struct uvc_streaming_control probe; | ||
299 | struct uvc_format *format; | ||
300 | struct uvc_frame *frame; | ||
301 | int ret; | ||
302 | |||
303 | if (fmt->type != stream->type) | ||
304 | return -EINVAL; | ||
305 | |||
306 | ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame); | ||
307 | if (ret < 0) | ||
308 | return ret; | ||
309 | |||
310 | mutex_lock(&stream->mutex); | ||
311 | |||
312 | if (uvc_queue_allocated(&stream->queue)) { | ||
313 | ret = -EBUSY; | ||
314 | goto done; | ||
315 | } | ||
316 | |||
317 | memcpy(&stream->ctrl, &probe, sizeof probe); | ||
318 | stream->cur_format = format; | ||
319 | stream->cur_frame = frame; | ||
320 | |||
321 | done: | ||
322 | mutex_unlock(&stream->mutex); | ||
323 | return ret; | ||
324 | } | ||
325 | |||
326 | static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream, | ||
327 | struct v4l2_streamparm *parm) | ||
328 | { | ||
329 | uint32_t numerator, denominator; | ||
330 | |||
331 | if (parm->type != stream->type) | ||
332 | return -EINVAL; | ||
333 | |||
334 | mutex_lock(&stream->mutex); | ||
335 | numerator = stream->ctrl.dwFrameInterval; | ||
336 | mutex_unlock(&stream->mutex); | ||
337 | |||
338 | denominator = 10000000; | ||
339 | uvc_simplify_fraction(&numerator, &denominator, 8, 333); | ||
340 | |||
341 | memset(parm, 0, sizeof *parm); | ||
342 | parm->type = stream->type; | ||
343 | |||
344 | if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { | ||
345 | parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; | ||
346 | parm->parm.capture.capturemode = 0; | ||
347 | parm->parm.capture.timeperframe.numerator = numerator; | ||
348 | parm->parm.capture.timeperframe.denominator = denominator; | ||
349 | parm->parm.capture.extendedmode = 0; | ||
350 | parm->parm.capture.readbuffers = 0; | ||
351 | } else { | ||
352 | parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME; | ||
353 | parm->parm.output.outputmode = 0; | ||
354 | parm->parm.output.timeperframe.numerator = numerator; | ||
355 | parm->parm.output.timeperframe.denominator = denominator; | ||
356 | } | ||
357 | |||
358 | return 0; | ||
359 | } | ||
360 | |||
361 | static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream, | ||
362 | struct v4l2_streamparm *parm) | ||
363 | { | ||
364 | struct uvc_streaming_control probe; | ||
365 | struct v4l2_fract timeperframe; | ||
366 | uint32_t interval; | ||
367 | int ret; | ||
368 | |||
369 | if (parm->type != stream->type) | ||
370 | return -EINVAL; | ||
371 | |||
372 | if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) | ||
373 | timeperframe = parm->parm.capture.timeperframe; | ||
374 | else | ||
375 | timeperframe = parm->parm.output.timeperframe; | ||
376 | |||
377 | interval = uvc_fraction_to_interval(timeperframe.numerator, | ||
378 | timeperframe.denominator); | ||
379 | uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n", | ||
380 | timeperframe.numerator, timeperframe.denominator, interval); | ||
381 | |||
382 | mutex_lock(&stream->mutex); | ||
383 | |||
384 | if (uvc_queue_streaming(&stream->queue)) { | ||
385 | mutex_unlock(&stream->mutex); | ||
386 | return -EBUSY; | ||
387 | } | ||
388 | |||
389 | memcpy(&probe, &stream->ctrl, sizeof probe); | ||
390 | probe.dwFrameInterval = | ||
391 | uvc_try_frame_interval(stream->cur_frame, interval); | ||
392 | |||
393 | /* Probe the device with the new settings. */ | ||
394 | ret = uvc_probe_video(stream, &probe); | ||
395 | if (ret < 0) { | ||
396 | mutex_unlock(&stream->mutex); | ||
397 | return ret; | ||
398 | } | ||
399 | |||
400 | memcpy(&stream->ctrl, &probe, sizeof probe); | ||
401 | mutex_unlock(&stream->mutex); | ||
402 | |||
403 | /* Return the actual frame period. */ | ||
404 | timeperframe.numerator = probe.dwFrameInterval; | ||
405 | timeperframe.denominator = 10000000; | ||
406 | uvc_simplify_fraction(&timeperframe.numerator, | ||
407 | &timeperframe.denominator, 8, 333); | ||
408 | |||
409 | if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) | ||
410 | parm->parm.capture.timeperframe = timeperframe; | ||
411 | else | ||
412 | parm->parm.output.timeperframe = timeperframe; | ||
413 | |||
414 | return 0; | ||
415 | } | ||
416 | |||
417 | /* ------------------------------------------------------------------------ | ||
418 | * Privilege management | ||
419 | */ | ||
420 | |||
421 | /* | ||
422 | * Privilege management is the multiple-open implementation basis. The current | ||
423 | * implementation is completely transparent for the end-user and doesn't | ||
424 | * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls. | ||
425 | * Those ioctls enable finer control on the device (by making possible for a | ||
426 | * user to request exclusive access to a device), but are not mature yet. | ||
427 | * Switching to the V4L2 priority mechanism might be considered in the future | ||
428 | * if this situation changes. | ||
429 | * | ||
430 | * Each open instance of a UVC device can either be in a privileged or | ||
431 | * unprivileged state. Only a single instance can be in a privileged state at | ||
432 | * a given time. Trying to perform an operation that requires privileges will | ||
433 | * automatically acquire the required privileges if possible, or return -EBUSY | ||
434 | * otherwise. Privileges are dismissed when closing the instance or when | ||
435 | * freeing the video buffers using VIDIOC_REQBUFS. | ||
436 | * | ||
437 | * Operations that require privileges are: | ||
438 | * | ||
439 | * - VIDIOC_S_INPUT | ||
440 | * - VIDIOC_S_PARM | ||
441 | * - VIDIOC_S_FMT | ||
442 | * - VIDIOC_REQBUFS | ||
443 | */ | ||
444 | static int uvc_acquire_privileges(struct uvc_fh *handle) | ||
445 | { | ||
446 | /* Always succeed if the handle is already privileged. */ | ||
447 | if (handle->state == UVC_HANDLE_ACTIVE) | ||
448 | return 0; | ||
449 | |||
450 | /* Check if the device already has a privileged handle. */ | ||
451 | if (atomic_inc_return(&handle->stream->active) != 1) { | ||
452 | atomic_dec(&handle->stream->active); | ||
453 | return -EBUSY; | ||
454 | } | ||
455 | |||
456 | handle->state = UVC_HANDLE_ACTIVE; | ||
457 | return 0; | ||
458 | } | ||
459 | |||
460 | static void uvc_dismiss_privileges(struct uvc_fh *handle) | ||
461 | { | ||
462 | if (handle->state == UVC_HANDLE_ACTIVE) | ||
463 | atomic_dec(&handle->stream->active); | ||
464 | |||
465 | handle->state = UVC_HANDLE_PASSIVE; | ||
466 | } | ||
467 | |||
468 | static int uvc_has_privileges(struct uvc_fh *handle) | ||
469 | { | ||
470 | return handle->state == UVC_HANDLE_ACTIVE; | ||
471 | } | ||
472 | |||
473 | /* ------------------------------------------------------------------------ | ||
474 | * V4L2 file operations | ||
475 | */ | ||
476 | |||
477 | static int uvc_v4l2_open(struct file *file) | ||
478 | { | ||
479 | struct uvc_streaming *stream; | ||
480 | struct uvc_fh *handle; | ||
481 | int ret = 0; | ||
482 | |||
483 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n"); | ||
484 | stream = video_drvdata(file); | ||
485 | |||
486 | if (stream->dev->state & UVC_DEV_DISCONNECTED) | ||
487 | return -ENODEV; | ||
488 | |||
489 | ret = usb_autopm_get_interface(stream->dev->intf); | ||
490 | if (ret < 0) | ||
491 | return ret; | ||
492 | |||
493 | /* Create the device handle. */ | ||
494 | handle = kzalloc(sizeof *handle, GFP_KERNEL); | ||
495 | if (handle == NULL) { | ||
496 | usb_autopm_put_interface(stream->dev->intf); | ||
497 | return -ENOMEM; | ||
498 | } | ||
499 | |||
500 | if (atomic_inc_return(&stream->dev->users) == 1) { | ||
501 | ret = uvc_status_start(stream->dev); | ||
502 | if (ret < 0) { | ||
503 | usb_autopm_put_interface(stream->dev->intf); | ||
504 | atomic_dec(&stream->dev->users); | ||
505 | kfree(handle); | ||
506 | return ret; | ||
507 | } | ||
508 | } | ||
509 | |||
510 | v4l2_fh_init(&handle->vfh, stream->vdev); | ||
511 | v4l2_fh_add(&handle->vfh); | ||
512 | handle->chain = stream->chain; | ||
513 | handle->stream = stream; | ||
514 | handle->state = UVC_HANDLE_PASSIVE; | ||
515 | file->private_data = handle; | ||
516 | |||
517 | return 0; | ||
518 | } | ||
519 | |||
520 | static int uvc_v4l2_release(struct file *file) | ||
521 | { | ||
522 | struct uvc_fh *handle = file->private_data; | ||
523 | struct uvc_streaming *stream = handle->stream; | ||
524 | |||
525 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n"); | ||
526 | |||
527 | /* Only free resources if this is a privileged handle. */ | ||
528 | if (uvc_has_privileges(handle)) { | ||
529 | uvc_video_enable(stream, 0); | ||
530 | uvc_free_buffers(&stream->queue); | ||
531 | } | ||
532 | |||
533 | /* Release the file handle. */ | ||
534 | uvc_dismiss_privileges(handle); | ||
535 | v4l2_fh_del(&handle->vfh); | ||
536 | v4l2_fh_exit(&handle->vfh); | ||
537 | kfree(handle); | ||
538 | file->private_data = NULL; | ||
539 | |||
540 | if (atomic_dec_return(&stream->dev->users) == 0) | ||
541 | uvc_status_stop(stream->dev); | ||
542 | |||
543 | usb_autopm_put_interface(stream->dev->intf); | ||
544 | return 0; | ||
545 | } | ||
546 | |||
547 | static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg) | ||
548 | { | ||
549 | struct video_device *vdev = video_devdata(file); | ||
550 | struct uvc_fh *handle = file->private_data; | ||
551 | struct uvc_video_chain *chain = handle->chain; | ||
552 | struct uvc_streaming *stream = handle->stream; | ||
553 | long ret = 0; | ||
554 | |||
555 | switch (cmd) { | ||
556 | /* Query capabilities */ | ||
557 | case VIDIOC_QUERYCAP: | ||
558 | { | ||
559 | struct v4l2_capability *cap = arg; | ||
560 | |||
561 | memset(cap, 0, sizeof *cap); | ||
562 | strlcpy(cap->driver, "uvcvideo", sizeof cap->driver); | ||
563 | strlcpy(cap->card, vdev->name, sizeof cap->card); | ||
564 | usb_make_path(stream->dev->udev, | ||
565 | cap->bus_info, sizeof(cap->bus_info)); | ||
566 | cap->version = LINUX_VERSION_CODE; | ||
567 | if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) | ||
568 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | ||
569 | | V4L2_CAP_STREAMING; | ||
570 | else | ||
571 | cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | ||
572 | | V4L2_CAP_STREAMING; | ||
573 | break; | ||
574 | } | ||
575 | |||
576 | /* Get, Set & Query control */ | ||
577 | case VIDIOC_QUERYCTRL: | ||
578 | return uvc_query_v4l2_ctrl(chain, arg); | ||
579 | |||
580 | case VIDIOC_G_CTRL: | ||
581 | { | ||
582 | struct v4l2_control *ctrl = arg; | ||
583 | struct v4l2_ext_control xctrl; | ||
584 | |||
585 | memset(&xctrl, 0, sizeof xctrl); | ||
586 | xctrl.id = ctrl->id; | ||
587 | |||
588 | ret = uvc_ctrl_begin(chain); | ||
589 | if (ret < 0) | ||
590 | return ret; | ||
591 | |||
592 | ret = uvc_ctrl_get(chain, &xctrl); | ||
593 | uvc_ctrl_rollback(handle); | ||
594 | if (ret >= 0) | ||
595 | ctrl->value = xctrl.value; | ||
596 | break; | ||
597 | } | ||
598 | |||
599 | case VIDIOC_S_CTRL: | ||
600 | { | ||
601 | struct v4l2_control *ctrl = arg; | ||
602 | struct v4l2_ext_control xctrl; | ||
603 | |||
604 | memset(&xctrl, 0, sizeof xctrl); | ||
605 | xctrl.id = ctrl->id; | ||
606 | xctrl.value = ctrl->value; | ||
607 | |||
608 | ret = uvc_ctrl_begin(chain); | ||
609 | if (ret < 0) | ||
610 | return ret; | ||
611 | |||
612 | ret = uvc_ctrl_set(chain, &xctrl); | ||
613 | if (ret < 0) { | ||
614 | uvc_ctrl_rollback(handle); | ||
615 | return ret; | ||
616 | } | ||
617 | ret = uvc_ctrl_commit(handle, &xctrl, 1); | ||
618 | if (ret == 0) | ||
619 | ctrl->value = xctrl.value; | ||
620 | break; | ||
621 | } | ||
622 | |||
623 | case VIDIOC_QUERYMENU: | ||
624 | return uvc_query_v4l2_menu(chain, arg); | ||
625 | |||
626 | case VIDIOC_G_EXT_CTRLS: | ||
627 | { | ||
628 | struct v4l2_ext_controls *ctrls = arg; | ||
629 | struct v4l2_ext_control *ctrl = ctrls->controls; | ||
630 | unsigned int i; | ||
631 | |||
632 | ret = uvc_ctrl_begin(chain); | ||
633 | if (ret < 0) | ||
634 | return ret; | ||
635 | |||
636 | for (i = 0; i < ctrls->count; ++ctrl, ++i) { | ||
637 | ret = uvc_ctrl_get(chain, ctrl); | ||
638 | if (ret < 0) { | ||
639 | uvc_ctrl_rollback(handle); | ||
640 | ctrls->error_idx = i; | ||
641 | return ret; | ||
642 | } | ||
643 | } | ||
644 | ctrls->error_idx = 0; | ||
645 | ret = uvc_ctrl_rollback(handle); | ||
646 | break; | ||
647 | } | ||
648 | |||
649 | case VIDIOC_S_EXT_CTRLS: | ||
650 | case VIDIOC_TRY_EXT_CTRLS: | ||
651 | { | ||
652 | struct v4l2_ext_controls *ctrls = arg; | ||
653 | struct v4l2_ext_control *ctrl = ctrls->controls; | ||
654 | unsigned int i; | ||
655 | |||
656 | ret = uvc_ctrl_begin(chain); | ||
657 | if (ret < 0) | ||
658 | return ret; | ||
659 | |||
660 | for (i = 0; i < ctrls->count; ++ctrl, ++i) { | ||
661 | ret = uvc_ctrl_set(chain, ctrl); | ||
662 | if (ret < 0) { | ||
663 | uvc_ctrl_rollback(handle); | ||
664 | ctrls->error_idx = i; | ||
665 | return ret; | ||
666 | } | ||
667 | } | ||
668 | |||
669 | ctrls->error_idx = 0; | ||
670 | |||
671 | if (cmd == VIDIOC_S_EXT_CTRLS) | ||
672 | ret = uvc_ctrl_commit(handle, | ||
673 | ctrls->controls, ctrls->count); | ||
674 | else | ||
675 | ret = uvc_ctrl_rollback(handle); | ||
676 | break; | ||
677 | } | ||
678 | |||
679 | /* Get, Set & Enum input */ | ||
680 | case VIDIOC_ENUMINPUT: | ||
681 | { | ||
682 | const struct uvc_entity *selector = chain->selector; | ||
683 | struct v4l2_input *input = arg; | ||
684 | struct uvc_entity *iterm = NULL; | ||
685 | u32 index = input->index; | ||
686 | int pin = 0; | ||
687 | |||
688 | if (selector == NULL || | ||
689 | (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) { | ||
690 | if (index != 0) | ||
691 | return -EINVAL; | ||
692 | list_for_each_entry(iterm, &chain->entities, chain) { | ||
693 | if (UVC_ENTITY_IS_ITERM(iterm)) | ||
694 | break; | ||
695 | } | ||
696 | pin = iterm->id; | ||
697 | } else if (index < selector->bNrInPins) { | ||
698 | pin = selector->baSourceID[index]; | ||
699 | list_for_each_entry(iterm, &chain->entities, chain) { | ||
700 | if (!UVC_ENTITY_IS_ITERM(iterm)) | ||
701 | continue; | ||
702 | if (iterm->id == pin) | ||
703 | break; | ||
704 | } | ||
705 | } | ||
706 | |||
707 | if (iterm == NULL || iterm->id != pin) | ||
708 | return -EINVAL; | ||
709 | |||
710 | memset(input, 0, sizeof *input); | ||
711 | input->index = index; | ||
712 | strlcpy(input->name, iterm->name, sizeof input->name); | ||
713 | if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA) | ||
714 | input->type = V4L2_INPUT_TYPE_CAMERA; | ||
715 | break; | ||
716 | } | ||
717 | |||
718 | case VIDIOC_G_INPUT: | ||
719 | { | ||
720 | u8 input; | ||
721 | |||
722 | if (chain->selector == NULL || | ||
723 | (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) { | ||
724 | *(int *)arg = 0; | ||
725 | break; | ||
726 | } | ||
727 | |||
728 | ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, | ||
729 | chain->selector->id, chain->dev->intfnum, | ||
730 | UVC_SU_INPUT_SELECT_CONTROL, &input, 1); | ||
731 | if (ret < 0) | ||
732 | return ret; | ||
733 | |||
734 | *(int *)arg = input - 1; | ||
735 | break; | ||
736 | } | ||
737 | |||
738 | case VIDIOC_S_INPUT: | ||
739 | { | ||
740 | u32 input = *(u32 *)arg + 1; | ||
741 | |||
742 | if ((ret = uvc_acquire_privileges(handle)) < 0) | ||
743 | return ret; | ||
744 | |||
745 | if (chain->selector == NULL || | ||
746 | (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) { | ||
747 | if (input != 1) | ||
748 | return -EINVAL; | ||
749 | break; | ||
750 | } | ||
751 | |||
752 | if (input == 0 || input > chain->selector->bNrInPins) | ||
753 | return -EINVAL; | ||
754 | |||
755 | return uvc_query_ctrl(chain->dev, UVC_SET_CUR, | ||
756 | chain->selector->id, chain->dev->intfnum, | ||
757 | UVC_SU_INPUT_SELECT_CONTROL, &input, 1); | ||
758 | } | ||
759 | |||
760 | /* Try, Get, Set & Enum format */ | ||
761 | case VIDIOC_ENUM_FMT: | ||
762 | { | ||
763 | struct v4l2_fmtdesc *fmt = arg; | ||
764 | struct uvc_format *format; | ||
765 | enum v4l2_buf_type type = fmt->type; | ||
766 | __u32 index = fmt->index; | ||
767 | |||
768 | if (fmt->type != stream->type || | ||
769 | fmt->index >= stream->nformats) | ||
770 | return -EINVAL; | ||
771 | |||
772 | memset(fmt, 0, sizeof(*fmt)); | ||
773 | fmt->index = index; | ||
774 | fmt->type = type; | ||
775 | |||
776 | format = &stream->format[fmt->index]; | ||
777 | fmt->flags = 0; | ||
778 | if (format->flags & UVC_FMT_FLAG_COMPRESSED) | ||
779 | fmt->flags |= V4L2_FMT_FLAG_COMPRESSED; | ||
780 | strlcpy(fmt->description, format->name, | ||
781 | sizeof fmt->description); | ||
782 | fmt->description[sizeof fmt->description - 1] = 0; | ||
783 | fmt->pixelformat = format->fcc; | ||
784 | break; | ||
785 | } | ||
786 | |||
787 | case VIDIOC_TRY_FMT: | ||
788 | { | ||
789 | struct uvc_streaming_control probe; | ||
790 | |||
791 | return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL); | ||
792 | } | ||
793 | |||
794 | case VIDIOC_S_FMT: | ||
795 | if ((ret = uvc_acquire_privileges(handle)) < 0) | ||
796 | return ret; | ||
797 | |||
798 | return uvc_v4l2_set_format(stream, arg); | ||
799 | |||
800 | case VIDIOC_G_FMT: | ||
801 | return uvc_v4l2_get_format(stream, arg); | ||
802 | |||
803 | /* Frame size enumeration */ | ||
804 | case VIDIOC_ENUM_FRAMESIZES: | ||
805 | { | ||
806 | struct v4l2_frmsizeenum *fsize = arg; | ||
807 | struct uvc_format *format = NULL; | ||
808 | struct uvc_frame *frame; | ||
809 | int i; | ||
810 | |||
811 | /* Look for the given pixel format */ | ||
812 | for (i = 0; i < stream->nformats; i++) { | ||
813 | if (stream->format[i].fcc == | ||
814 | fsize->pixel_format) { | ||
815 | format = &stream->format[i]; | ||
816 | break; | ||
817 | } | ||
818 | } | ||
819 | if (format == NULL) | ||
820 | return -EINVAL; | ||
821 | |||
822 | if (fsize->index >= format->nframes) | ||
823 | return -EINVAL; | ||
824 | |||
825 | frame = &format->frame[fsize->index]; | ||
826 | fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; | ||
827 | fsize->discrete.width = frame->wWidth; | ||
828 | fsize->discrete.height = frame->wHeight; | ||
829 | break; | ||
830 | } | ||
831 | |||
832 | /* Frame interval enumeration */ | ||
833 | case VIDIOC_ENUM_FRAMEINTERVALS: | ||
834 | { | ||
835 | struct v4l2_frmivalenum *fival = arg; | ||
836 | struct uvc_format *format = NULL; | ||
837 | struct uvc_frame *frame = NULL; | ||
838 | int i; | ||
839 | |||
840 | /* Look for the given pixel format and frame size */ | ||
841 | for (i = 0; i < stream->nformats; i++) { | ||
842 | if (stream->format[i].fcc == | ||
843 | fival->pixel_format) { | ||
844 | format = &stream->format[i]; | ||
845 | break; | ||
846 | } | ||
847 | } | ||
848 | if (format == NULL) | ||
849 | return -EINVAL; | ||
850 | |||
851 | for (i = 0; i < format->nframes; i++) { | ||
852 | if (format->frame[i].wWidth == fival->width && | ||
853 | format->frame[i].wHeight == fival->height) { | ||
854 | frame = &format->frame[i]; | ||
855 | break; | ||
856 | } | ||
857 | } | ||
858 | if (frame == NULL) | ||
859 | return -EINVAL; | ||
860 | |||
861 | if (frame->bFrameIntervalType) { | ||
862 | if (fival->index >= frame->bFrameIntervalType) | ||
863 | return -EINVAL; | ||
864 | |||
865 | fival->type = V4L2_FRMIVAL_TYPE_DISCRETE; | ||
866 | fival->discrete.numerator = | ||
867 | frame->dwFrameInterval[fival->index]; | ||
868 | fival->discrete.denominator = 10000000; | ||
869 | uvc_simplify_fraction(&fival->discrete.numerator, | ||
870 | &fival->discrete.denominator, 8, 333); | ||
871 | } else { | ||
872 | fival->type = V4L2_FRMIVAL_TYPE_STEPWISE; | ||
873 | fival->stepwise.min.numerator = | ||
874 | frame->dwFrameInterval[0]; | ||
875 | fival->stepwise.min.denominator = 10000000; | ||
876 | fival->stepwise.max.numerator = | ||
877 | frame->dwFrameInterval[1]; | ||
878 | fival->stepwise.max.denominator = 10000000; | ||
879 | fival->stepwise.step.numerator = | ||
880 | frame->dwFrameInterval[2]; | ||
881 | fival->stepwise.step.denominator = 10000000; | ||
882 | uvc_simplify_fraction(&fival->stepwise.min.numerator, | ||
883 | &fival->stepwise.min.denominator, 8, 333); | ||
884 | uvc_simplify_fraction(&fival->stepwise.max.numerator, | ||
885 | &fival->stepwise.max.denominator, 8, 333); | ||
886 | uvc_simplify_fraction(&fival->stepwise.step.numerator, | ||
887 | &fival->stepwise.step.denominator, 8, 333); | ||
888 | } | ||
889 | break; | ||
890 | } | ||
891 | |||
892 | /* Get & Set streaming parameters */ | ||
893 | case VIDIOC_G_PARM: | ||
894 | return uvc_v4l2_get_streamparm(stream, arg); | ||
895 | |||
896 | case VIDIOC_S_PARM: | ||
897 | if ((ret = uvc_acquire_privileges(handle)) < 0) | ||
898 | return ret; | ||
899 | |||
900 | return uvc_v4l2_set_streamparm(stream, arg); | ||
901 | |||
902 | /* Cropping and scaling */ | ||
903 | case VIDIOC_CROPCAP: | ||
904 | { | ||
905 | struct v4l2_cropcap *ccap = arg; | ||
906 | |||
907 | if (ccap->type != stream->type) | ||
908 | return -EINVAL; | ||
909 | |||
910 | ccap->bounds.left = 0; | ||
911 | ccap->bounds.top = 0; | ||
912 | |||
913 | mutex_lock(&stream->mutex); | ||
914 | ccap->bounds.width = stream->cur_frame->wWidth; | ||
915 | ccap->bounds.height = stream->cur_frame->wHeight; | ||
916 | mutex_unlock(&stream->mutex); | ||
917 | |||
918 | ccap->defrect = ccap->bounds; | ||
919 | |||
920 | ccap->pixelaspect.numerator = 1; | ||
921 | ccap->pixelaspect.denominator = 1; | ||
922 | break; | ||
923 | } | ||
924 | |||
925 | case VIDIOC_G_CROP: | ||
926 | case VIDIOC_S_CROP: | ||
927 | return -EINVAL; | ||
928 | |||
929 | /* Buffers & streaming */ | ||
930 | case VIDIOC_REQBUFS: | ||
931 | if ((ret = uvc_acquire_privileges(handle)) < 0) | ||
932 | return ret; | ||
933 | |||
934 | mutex_lock(&stream->mutex); | ||
935 | ret = uvc_alloc_buffers(&stream->queue, arg); | ||
936 | mutex_unlock(&stream->mutex); | ||
937 | if (ret < 0) | ||
938 | return ret; | ||
939 | |||
940 | if (ret == 0) | ||
941 | uvc_dismiss_privileges(handle); | ||
942 | |||
943 | ret = 0; | ||
944 | break; | ||
945 | |||
946 | case VIDIOC_QUERYBUF: | ||
947 | { | ||
948 | struct v4l2_buffer *buf = arg; | ||
949 | |||
950 | if (!uvc_has_privileges(handle)) | ||
951 | return -EBUSY; | ||
952 | |||
953 | return uvc_query_buffer(&stream->queue, buf); | ||
954 | } | ||
955 | |||
956 | case VIDIOC_QBUF: | ||
957 | if (!uvc_has_privileges(handle)) | ||
958 | return -EBUSY; | ||
959 | |||
960 | return uvc_queue_buffer(&stream->queue, arg); | ||
961 | |||
962 | case VIDIOC_DQBUF: | ||
963 | if (!uvc_has_privileges(handle)) | ||
964 | return -EBUSY; | ||
965 | |||
966 | return uvc_dequeue_buffer(&stream->queue, arg, | ||
967 | file->f_flags & O_NONBLOCK); | ||
968 | |||
969 | case VIDIOC_STREAMON: | ||
970 | { | ||
971 | int *type = arg; | ||
972 | |||
973 | if (*type != stream->type) | ||
974 | return -EINVAL; | ||
975 | |||
976 | if (!uvc_has_privileges(handle)) | ||
977 | return -EBUSY; | ||
978 | |||
979 | mutex_lock(&stream->mutex); | ||
980 | ret = uvc_video_enable(stream, 1); | ||
981 | mutex_unlock(&stream->mutex); | ||
982 | if (ret < 0) | ||
983 | return ret; | ||
984 | break; | ||
985 | } | ||
986 | |||
987 | case VIDIOC_STREAMOFF: | ||
988 | { | ||
989 | int *type = arg; | ||
990 | |||
991 | if (*type != stream->type) | ||
992 | return -EINVAL; | ||
993 | |||
994 | if (!uvc_has_privileges(handle)) | ||
995 | return -EBUSY; | ||
996 | |||
997 | return uvc_video_enable(stream, 0); | ||
998 | } | ||
999 | |||
1000 | case VIDIOC_SUBSCRIBE_EVENT: | ||
1001 | { | ||
1002 | struct v4l2_event_subscription *sub = arg; | ||
1003 | |||
1004 | switch (sub->type) { | ||
1005 | case V4L2_EVENT_CTRL: | ||
1006 | return v4l2_event_subscribe(&handle->vfh, sub, 0, | ||
1007 | &uvc_ctrl_sub_ev_ops); | ||
1008 | default: | ||
1009 | return -EINVAL; | ||
1010 | } | ||
1011 | } | ||
1012 | |||
1013 | case VIDIOC_UNSUBSCRIBE_EVENT: | ||
1014 | return v4l2_event_unsubscribe(&handle->vfh, arg); | ||
1015 | |||
1016 | case VIDIOC_DQEVENT: | ||
1017 | return v4l2_event_dequeue(&handle->vfh, arg, | ||
1018 | file->f_flags & O_NONBLOCK); | ||
1019 | |||
1020 | /* Analog video standards make no sense for digital cameras. */ | ||
1021 | case VIDIOC_ENUMSTD: | ||
1022 | case VIDIOC_QUERYSTD: | ||
1023 | case VIDIOC_G_STD: | ||
1024 | case VIDIOC_S_STD: | ||
1025 | |||
1026 | case VIDIOC_OVERLAY: | ||
1027 | |||
1028 | case VIDIOC_ENUMAUDIO: | ||
1029 | case VIDIOC_ENUMAUDOUT: | ||
1030 | |||
1031 | case VIDIOC_ENUMOUTPUT: | ||
1032 | uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd); | ||
1033 | return -EINVAL; | ||
1034 | |||
1035 | case UVCIOC_CTRL_MAP: | ||
1036 | return uvc_ioctl_ctrl_map(chain, arg); | ||
1037 | |||
1038 | case UVCIOC_CTRL_QUERY: | ||
1039 | return uvc_xu_ctrl_query(chain, arg); | ||
1040 | |||
1041 | default: | ||
1042 | uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n", cmd); | ||
1043 | return -ENOTTY; | ||
1044 | } | ||
1045 | |||
1046 | return ret; | ||
1047 | } | ||
1048 | |||
1049 | static long uvc_v4l2_ioctl(struct file *file, | ||
1050 | unsigned int cmd, unsigned long arg) | ||
1051 | { | ||
1052 | if (uvc_trace_param & UVC_TRACE_IOCTL) { | ||
1053 | uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl("); | ||
1054 | v4l_printk_ioctl(NULL, cmd); | ||
1055 | printk(")\n"); | ||
1056 | } | ||
1057 | |||
1058 | return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl); | ||
1059 | } | ||
1060 | |||
1061 | #ifdef CONFIG_COMPAT | ||
1062 | struct uvc_xu_control_mapping32 { | ||
1063 | __u32 id; | ||
1064 | __u8 name[32]; | ||
1065 | __u8 entity[16]; | ||
1066 | __u8 selector; | ||
1067 | |||
1068 | __u8 size; | ||
1069 | __u8 offset; | ||
1070 | __u32 v4l2_type; | ||
1071 | __u32 data_type; | ||
1072 | |||
1073 | compat_caddr_t menu_info; | ||
1074 | __u32 menu_count; | ||
1075 | |||
1076 | __u32 reserved[4]; | ||
1077 | }; | ||
1078 | |||
1079 | static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp, | ||
1080 | const struct uvc_xu_control_mapping32 __user *up) | ||
1081 | { | ||
1082 | struct uvc_menu_info __user *umenus; | ||
1083 | struct uvc_menu_info __user *kmenus; | ||
1084 | compat_caddr_t p; | ||
1085 | |||
1086 | if (!access_ok(VERIFY_READ, up, sizeof(*up)) || | ||
1087 | __copy_from_user(kp, up, offsetof(typeof(*up), menu_info)) || | ||
1088 | __get_user(kp->menu_count, &up->menu_count)) | ||
1089 | return -EFAULT; | ||
1090 | |||
1091 | memset(kp->reserved, 0, sizeof(kp->reserved)); | ||
1092 | |||
1093 | if (kp->menu_count == 0) { | ||
1094 | kp->menu_info = NULL; | ||
1095 | return 0; | ||
1096 | } | ||
1097 | |||
1098 | if (__get_user(p, &up->menu_info)) | ||
1099 | return -EFAULT; | ||
1100 | umenus = compat_ptr(p); | ||
1101 | if (!access_ok(VERIFY_READ, umenus, kp->menu_count * sizeof(*umenus))) | ||
1102 | return -EFAULT; | ||
1103 | |||
1104 | kmenus = compat_alloc_user_space(kp->menu_count * sizeof(*kmenus)); | ||
1105 | if (kmenus == NULL) | ||
1106 | return -EFAULT; | ||
1107 | kp->menu_info = kmenus; | ||
1108 | |||
1109 | if (copy_in_user(kmenus, umenus, kp->menu_count * sizeof(*umenus))) | ||
1110 | return -EFAULT; | ||
1111 | |||
1112 | return 0; | ||
1113 | } | ||
1114 | |||
1115 | static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp, | ||
1116 | struct uvc_xu_control_mapping32 __user *up) | ||
1117 | { | ||
1118 | struct uvc_menu_info __user *umenus; | ||
1119 | struct uvc_menu_info __user *kmenus = kp->menu_info; | ||
1120 | compat_caddr_t p; | ||
1121 | |||
1122 | if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) || | ||
1123 | __copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) || | ||
1124 | __put_user(kp->menu_count, &up->menu_count)) | ||
1125 | return -EFAULT; | ||
1126 | |||
1127 | if (__clear_user(up->reserved, sizeof(up->reserved))) | ||
1128 | return -EFAULT; | ||
1129 | |||
1130 | if (kp->menu_count == 0) | ||
1131 | return 0; | ||
1132 | |||
1133 | if (get_user(p, &up->menu_info)) | ||
1134 | return -EFAULT; | ||
1135 | umenus = compat_ptr(p); | ||
1136 | |||
1137 | if (copy_in_user(umenus, kmenus, kp->menu_count * sizeof(*umenus))) | ||
1138 | return -EFAULT; | ||
1139 | |||
1140 | return 0; | ||
1141 | } | ||
1142 | |||
1143 | struct uvc_xu_control_query32 { | ||
1144 | __u8 unit; | ||
1145 | __u8 selector; | ||
1146 | __u8 query; | ||
1147 | __u16 size; | ||
1148 | compat_caddr_t data; | ||
1149 | }; | ||
1150 | |||
1151 | static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp, | ||
1152 | const struct uvc_xu_control_query32 __user *up) | ||
1153 | { | ||
1154 | u8 __user *udata; | ||
1155 | u8 __user *kdata; | ||
1156 | compat_caddr_t p; | ||
1157 | |||
1158 | if (!access_ok(VERIFY_READ, up, sizeof(*up)) || | ||
1159 | __copy_from_user(kp, up, offsetof(typeof(*up), data))) | ||
1160 | return -EFAULT; | ||
1161 | |||
1162 | if (kp->size == 0) { | ||
1163 | kp->data = NULL; | ||
1164 | return 0; | ||
1165 | } | ||
1166 | |||
1167 | if (__get_user(p, &up->data)) | ||
1168 | return -EFAULT; | ||
1169 | udata = compat_ptr(p); | ||
1170 | if (!access_ok(VERIFY_READ, udata, kp->size)) | ||
1171 | return -EFAULT; | ||
1172 | |||
1173 | kdata = compat_alloc_user_space(kp->size); | ||
1174 | if (kdata == NULL) | ||
1175 | return -EFAULT; | ||
1176 | kp->data = kdata; | ||
1177 | |||
1178 | if (copy_in_user(kdata, udata, kp->size)) | ||
1179 | return -EFAULT; | ||
1180 | |||
1181 | return 0; | ||
1182 | } | ||
1183 | |||
1184 | static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp, | ||
1185 | struct uvc_xu_control_query32 __user *up) | ||
1186 | { | ||
1187 | u8 __user *udata; | ||
1188 | u8 __user *kdata = kp->data; | ||
1189 | compat_caddr_t p; | ||
1190 | |||
1191 | if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) || | ||
1192 | __copy_to_user(up, kp, offsetof(typeof(*up), data))) | ||
1193 | return -EFAULT; | ||
1194 | |||
1195 | if (kp->size == 0) | ||
1196 | return 0; | ||
1197 | |||
1198 | if (get_user(p, &up->data)) | ||
1199 | return -EFAULT; | ||
1200 | udata = compat_ptr(p); | ||
1201 | if (!access_ok(VERIFY_READ, udata, kp->size)) | ||
1202 | return -EFAULT; | ||
1203 | |||
1204 | if (copy_in_user(udata, kdata, kp->size)) | ||
1205 | return -EFAULT; | ||
1206 | |||
1207 | return 0; | ||
1208 | } | ||
1209 | |||
1210 | #define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32) | ||
1211 | #define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32) | ||
1212 | |||
1213 | static long uvc_v4l2_compat_ioctl32(struct file *file, | ||
1214 | unsigned int cmd, unsigned long arg) | ||
1215 | { | ||
1216 | union { | ||
1217 | struct uvc_xu_control_mapping xmap; | ||
1218 | struct uvc_xu_control_query xqry; | ||
1219 | } karg; | ||
1220 | void __user *up = compat_ptr(arg); | ||
1221 | mm_segment_t old_fs; | ||
1222 | long ret; | ||
1223 | |||
1224 | switch (cmd) { | ||
1225 | case UVCIOC_CTRL_MAP32: | ||
1226 | cmd = UVCIOC_CTRL_MAP; | ||
1227 | ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up); | ||
1228 | break; | ||
1229 | |||
1230 | case UVCIOC_CTRL_QUERY32: | ||
1231 | cmd = UVCIOC_CTRL_QUERY; | ||
1232 | ret = uvc_v4l2_get_xu_query(&karg.xqry, up); | ||
1233 | break; | ||
1234 | |||
1235 | default: | ||
1236 | return -ENOIOCTLCMD; | ||
1237 | } | ||
1238 | |||
1239 | old_fs = get_fs(); | ||
1240 | set_fs(KERNEL_DS); | ||
1241 | ret = uvc_v4l2_ioctl(file, cmd, (unsigned long)&karg); | ||
1242 | set_fs(old_fs); | ||
1243 | |||
1244 | if (ret < 0) | ||
1245 | return ret; | ||
1246 | |||
1247 | switch (cmd) { | ||
1248 | case UVCIOC_CTRL_MAP: | ||
1249 | ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up); | ||
1250 | break; | ||
1251 | |||
1252 | case UVCIOC_CTRL_QUERY: | ||
1253 | ret = uvc_v4l2_put_xu_query(&karg.xqry, up); | ||
1254 | break; | ||
1255 | } | ||
1256 | |||
1257 | return ret; | ||
1258 | } | ||
1259 | #endif | ||
1260 | |||
1261 | static ssize_t uvc_v4l2_read(struct file *file, char __user *data, | ||
1262 | size_t count, loff_t *ppos) | ||
1263 | { | ||
1264 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n"); | ||
1265 | return -EINVAL; | ||
1266 | } | ||
1267 | |||
1268 | static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma) | ||
1269 | { | ||
1270 | struct uvc_fh *handle = file->private_data; | ||
1271 | struct uvc_streaming *stream = handle->stream; | ||
1272 | |||
1273 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n"); | ||
1274 | |||
1275 | return uvc_queue_mmap(&stream->queue, vma); | ||
1276 | } | ||
1277 | |||
1278 | static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait) | ||
1279 | { | ||
1280 | struct uvc_fh *handle = file->private_data; | ||
1281 | struct uvc_streaming *stream = handle->stream; | ||
1282 | |||
1283 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n"); | ||
1284 | |||
1285 | return uvc_queue_poll(&stream->queue, file, wait); | ||
1286 | } | ||
1287 | |||
1288 | #ifndef CONFIG_MMU | ||
1289 | static unsigned long uvc_v4l2_get_unmapped_area(struct file *file, | ||
1290 | unsigned long addr, unsigned long len, unsigned long pgoff, | ||
1291 | unsigned long flags) | ||
1292 | { | ||
1293 | struct uvc_fh *handle = file->private_data; | ||
1294 | struct uvc_streaming *stream = handle->stream; | ||
1295 | |||
1296 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n"); | ||
1297 | |||
1298 | return uvc_queue_get_unmapped_area(&stream->queue, pgoff); | ||
1299 | } | ||
1300 | #endif | ||
1301 | |||
1302 | const struct v4l2_file_operations uvc_fops = { | ||
1303 | .owner = THIS_MODULE, | ||
1304 | .open = uvc_v4l2_open, | ||
1305 | .release = uvc_v4l2_release, | ||
1306 | .unlocked_ioctl = uvc_v4l2_ioctl, | ||
1307 | #ifdef CONFIG_COMPAT | ||
1308 | .compat_ioctl32 = uvc_v4l2_compat_ioctl32, | ||
1309 | #endif | ||
1310 | .read = uvc_v4l2_read, | ||
1311 | .mmap = uvc_v4l2_mmap, | ||
1312 | .poll = uvc_v4l2_poll, | ||
1313 | #ifndef CONFIG_MMU | ||
1314 | .get_unmapped_area = uvc_v4l2_get_unmapped_area, | ||
1315 | #endif | ||
1316 | }; | ||
1317 | |||