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>
21 #include <linux/videodev2.h>
24 #include <linux/wait.h>
138 interval = min + (interval - min + step/2) / step * step;
151 struct uvc_frame *frame =
NULL;
153 unsigned int d, maxd;
165 fcc[0], fcc[1], fcc[2], fcc[3],
171 if (format->
fcc == fmt->
fmt.
pix.pixelformat)
175 if (format ==
NULL || format->
fcc != fmt->
fmt.
pix.pixelformat) {
177 fmt->
fmt.
pix.pixelformat);
187 maxd = (
unsigned int)-1;
189 for (i = 0; i < format->
nframes; ++
i) {
193 d =
min(w, rw) *
min(h, rh);
194 d = w*h + rw*rh - 2*
d;
197 frame = &format->
frame[
i];
213 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
214 (100000000/interval)%10);
217 memset(probe, 0,
sizeof *probe);
237 stream->
ctrl.dwMaxVideoFrameSize;
253 if (uvc_format !=
NULL)
255 if (uvc_frame !=
NULL)
265 struct uvc_format *
format;
266 struct uvc_frame *
frame;
276 if (format ==
NULL || frame ==
NULL) {
286 fmt->
fmt.
pix.sizeimage = stream->
ctrl.dwMaxVideoFrameSize;
299 struct uvc_format *
format;
300 struct uvc_frame *
frame;
306 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
326 static int uvc_v4l2_get_streamparm(
struct uvc_streaming *stream,
335 numerator = stream->
ctrl.dwFrameInterval;
338 denominator = 10000000;
341 memset(parm, 0,
sizeof *parm);
347 parm->
parm.
capture.timeperframe.numerator = numerator;
348 parm->
parm.
capture.timeperframe.denominator = denominator;
354 parm->
parm.
output.timeperframe.numerator = numerator;
355 parm->
parm.
output.timeperframe.denominator = denominator;
361 static int uvc_v4l2_set_streamparm(
struct uvc_streaming *stream,
375 timeperframe = parm->
parm.
output.timeperframe;
378 timeperframe.denominator);
380 timeperframe.numerator, timeperframe.denominator, interval);
384 if (uvc_queue_streaming(&stream->
queue)) {
391 uvc_try_frame_interval(stream->
cur_frame, interval);
405 timeperframe.denominator = 10000000;
407 &timeperframe.denominator, 8, 333);
412 parm->
parm.
output.timeperframe = timeperframe;
444 static int uvc_acquire_privileges(
struct uvc_fh *
handle)
460 static void uvc_dismiss_privileges(
struct uvc_fh *handle)
468 static int uvc_has_privileges(
struct uvc_fh *handle)
477 static int uvc_v4l2_open(
struct file *
file)
484 stream = video_drvdata(file);
489 ret = usb_autopm_get_interface(stream->
dev->intf);
495 if (handle ==
NULL) {
496 usb_autopm_put_interface(stream->
dev->intf);
503 usb_autopm_put_interface(stream->
dev->intf);
520 static int uvc_v4l2_release(
struct file *file)
528 if (uvc_has_privileges(handle)) {
534 uvc_dismiss_privileges(handle);
543 usb_autopm_put_interface(stream->
dev->intf);
547 static long uvc_v4l2_do_ioctl(
struct file *file,
unsigned int cmd,
void *
arg)
561 memset(cap, 0,
sizeof *cap);
564 usb_make_path(stream->
dev->udev,
566 cap->
version = LINUX_VERSION_CODE;
585 memset(&xctrl, 0,
sizeof xctrl);
593 uvc_ctrl_rollback(handle);
595 ctrl->
value = xctrl.value;
604 memset(&xctrl, 0,
sizeof xctrl);
606 xctrl.value = ctrl->
value;
614 uvc_ctrl_rollback(handle);
617 ret = uvc_ctrl_commit(handle, &xctrl, 1);
619 ctrl->
value = xctrl.value;
639 uvc_ctrl_rollback(handle);
645 ret = uvc_ctrl_rollback(handle);
663 uvc_ctrl_rollback(handle);
672 ret = uvc_ctrl_commit(handle,
675 ret = uvc_ctrl_rollback(handle);
688 if (selector ==
NULL ||
697 }
else if (index < selector->
bNrInPins) {
702 if (iterm->
id == pin)
707 if (iterm ==
NULL || iterm->
id != pin)
710 memset(input, 0,
sizeof *input);
734 *(
int *)arg = input - 1;
740 u32 input = *(
u32 *)arg + 1;
742 if ((ret = uvc_acquire_privileges(handle)) < 0)
752 if (input == 0 || input > chain->
selector->bNrInPins)
764 struct uvc_format *
format;
772 memset(fmt, 0,
sizeof(*fmt));
791 return uvc_v4l2_try_format(stream, arg, &probe,
NULL,
NULL);
795 if ((ret = uvc_acquire_privileges(handle)) < 0)
798 return uvc_v4l2_set_format(stream, arg);
801 return uvc_v4l2_get_format(stream, arg);
807 struct uvc_format *format =
NULL;
808 struct uvc_frame *
frame;
812 for (i = 0; i < stream->
nformats; i++) {
813 if (stream->
format[i].fcc ==
836 struct uvc_format *format =
NULL;
837 struct uvc_frame *frame =
NULL;
841 for (i = 0; i < stream->
nformats; i++) {
842 if (stream->
format[i].fcc ==
851 for (i = 0; i < format->
nframes; i++) {
852 if (format->
frame[i].wWidth == fival->
width &&
854 frame = &format->
frame[
i];
868 fival->
discrete.denominator = 10000000;
870 &fival->
discrete.denominator, 8, 333);
875 fival->
stepwise.min.denominator = 10000000;
878 fival->
stepwise.max.denominator = 10000000;
881 fival->
stepwise.step.denominator = 10000000;
883 &fival->
stepwise.min.denominator, 8, 333);
885 &fival->
stepwise.max.denominator, 8, 333);
887 &fival->
stepwise.step.denominator, 8, 333);
894 return uvc_v4l2_get_streamparm(stream, arg);
897 if ((ret = uvc_acquire_privileges(handle)) < 0)
900 return uvc_v4l2_set_streamparm(stream, arg);
931 if ((ret = uvc_acquire_privileges(handle)) < 0)
941 uvc_dismiss_privileges(handle);
950 if (!uvc_has_privileges(handle))
957 if (!uvc_has_privileges(handle))
963 if (!uvc_has_privileges(handle))
973 if (*type != stream->
type)
976 if (!uvc_has_privileges(handle))
991 if (*type != stream->
type)
994 if (!uvc_has_privileges(handle))
1004 switch (sub->
type) {
1036 return uvc_ioctl_ctrl_map(chain, arg);
1049 static long uvc_v4l2_ioctl(
struct file *file,
1050 unsigned int cmd,
unsigned long arg)
1061 #ifdef CONFIG_COMPAT
1062 struct uvc_xu_control_mapping32 {
1080 const struct uvc_xu_control_mapping32 __user *
up)
1100 umenus = compat_ptr(p);
1116 struct uvc_xu_control_mapping32 __user *up)
1135 umenus = compat_ptr(p);
1143 struct uvc_xu_control_query32 {
1152 const struct uvc_xu_control_query32 __user *up)
1162 if (kp->
size == 0) {
1169 udata = compat_ptr(p);
1185 struct uvc_xu_control_query32 __user *up)
1200 udata = compat_ptr(p);
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)
1213 static long uvc_v4l2_compat_ioctl32(
struct file *file,
1214 unsigned int cmd,
unsigned long arg)
1220 void __user *up = compat_ptr(arg);
1225 case UVCIOC_CTRL_MAP32:
1227 ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
1230 case UVCIOC_CTRL_QUERY32:
1232 ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
1241 ret = uvc_v4l2_ioctl(file, cmd, (
unsigned long)&karg);
1249 ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
1253 ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
1261 static ssize_t uvc_v4l2_read(
struct file *file,
char __user *
data,
1262 size_t count, loff_t *ppos)
1268 static int uvc_v4l2_mmap(
struct file *file,
struct vm_area_struct *vma)
1278 static unsigned int uvc_v4l2_poll(
struct file *file,
poll_table *
wait)
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)
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,
1310 .read = uvc_v4l2_read,
1311 .mmap = uvc_v4l2_mmap,
1312 .poll = uvc_v4l2_poll,
1314 .get_unmapped_area = uvc_v4l2_get_unmapped_area,