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