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