34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/kernel.h>
37 #include <linux/version.h>
38 #include <linux/errno.h>
39 #include <linux/slab.h>
40 #include <linux/kref.h>
41 #include <linux/vmalloc.h>
45 #include <linux/usb.h>
46 #include <media/v4l2-common.h>
47 #include <media/v4l2-ioctl.h>
79 .id = V4L2_CID_BRIGHTNESS,
80 .type = V4L2_CTRL_TYPE_INTEGER,
85 .default_value = 0x7f00,
88 .id = V4L2_CID_WHITENESS,
89 .type = V4L2_CTRL_TYPE_INTEGER,
94 .default_value = 0x7f00,
97 .id = V4L2_CID_SATURATION,
98 .type = V4L2_CTRL_TYPE_INTEGER,
103 .default_value = 0x7f00,
106 .id = V4L2_CID_CONTRAST,
107 .type = V4L2_CTRL_TYPE_INTEGER,
112 .default_value = 0x7f00,
115 .id = V4L2_CID_HFLIP,
116 .type = V4L2_CTRL_TYPE_BOOLEAN,
117 .name =
"Flip Horizontally",
124 .id = V4L2_CID_VFLIP,
125 .type = V4L2_CTRL_TYPE_BOOLEAN,
126 .name =
"Flip Vertically",
188 for (i=0, find=0; i<STK11XX_NBR_SIZES; i++) {
195 for (i=0, find=0; i<STK11XX_NBR_SIZES-3; i++) {
206 dev->resolution = find;
208 STK_DEBUG(
"Set mode %d [%dx%d]\n", dev->resolution,
213 dev->view.
y = height;
217 switch (dev->resolution) {
220 case STK11XX_160x120:
221 case STK11XX_213x160:
222 case STK11XX_320x240:
223 case STK11XX_640x480:
226 dev->frame_size = dev->image.
x * dev->image.
y;
229 case STK11XX_800x600:
230 case STK11XX_1024x768:
231 case STK11XX_1280x1024:
234 dev->frame_size = dev->image.
x * dev->image.
y;
241 case STK11XX_PALETTE_RGB24:
242 case STK11XX_PALETTE_BGR24:
243 dev->view_size = 3 * dev->view.
x * dev->view.
y;
244 dev->image_size = 3 * dev->frame_size;
247 case STK11XX_PALETTE_RGB32:
248 case STK11XX_PALETTE_BGR32:
249 dev->view_size = 3 * dev->view.
x * dev->view.
y;
250 dev->image_size = 4 * dev->frame_size;
253 case STK11XX_PALETTE_UYVY:
254 case STK11XX_PALETTE_YUYV:
255 dev->view_size = 2 * dev->view.
x * dev->view.
y;
256 dev->image_size = 2 * dev->frame_size;
278 struct video_device *vdev;
280 vdev = video_devdata(fp);
281 dev = video_get_drvdata(video_devdata(fp));
284 STK_ERROR(
"Device not initialized !!!\n");
290 STK_DEBUG(
"Device is busy, someone is using the device\n");
300 STK_ERROR(
"Failed to allocate buffer memory !\n");
309 dev->error_status = 0;
329 STK_ERROR(
"Failed to init ISOC stuff !\n");
343 usb_autopm_get_interface(dev->
interface);
346 fp->private_data = vdev;
366 struct video_device *vdev;
368 vdev = video_devdata(fp);
369 dev = video_get_drvdata(video_devdata(fp));
372 STK_ERROR(
"v4l_release called on closed device\n");
389 usb_autopm_put_interface(dev->
interface);
411 size_t count, loff_t *f_pos)
413 int noblock = fp->f_flags & O_NONBLOCK;
416 struct video_device *vdev;
419 void *image_buffer_addr;
421 DECLARE_WAITQUEUE(wait, current);
423 vdev = video_devdata(fp);
424 dev = video_get_drvdata(video_devdata(fp));
426 STK_STREAM(
"Read vdev=0x%p, buf=0x%p, count=%zd\n", vdev, buf, count);
436 if (dev->image_read_pos == 0) {
439 while (dev->full_frames == NULL) {
440 if (dev->error_status) {
442 set_current_state(TASK_RUNNING);
444 return -dev->error_status ;
449 set_current_state(TASK_RUNNING);
454 if (signal_pending(current)) {
456 set_current_state(TASK_RUNNING);
462 set_current_state(TASK_INTERRUPTIBLE);
466 set_current_state(TASK_RUNNING);
474 bytes_to_read = dev->view_size;
476 if (count + dev->image_read_pos > bytes_to_read)
477 count = bytes_to_read - dev->image_read_pos;
479 image_buffer_addr = dev->image_data;
480 image_buffer_addr += dev->images[dev->fill_image].
offset;
481 image_buffer_addr += dev->image_read_pos;
483 if (copy_to_user(buf, image_buffer_addr, count)) {
488 dev->image_read_pos += count;
490 if (dev->image_read_pos >= bytes_to_read) {
491 dev->image_read_pos = 0;
512 struct video_device *vdev;
514 vdev = video_devdata(fp);
515 dev = video_get_drvdata(video_devdata(fp));
527 if (dev->error_status)
530 if (dev->full_frames != NULL)
531 return (POLLIN | POLLRDNORM);
558 struct video_device *vdev;
560 vdev = video_devdata(fp);
561 dev = video_get_drvdata(video_devdata(fp));
565 start = vma->vm_start;
566 size = vma->vm_end - vma->vm_start;
569 for (i=0; i<dev->nbuffers; i++) {
570 pos = dev->images[i].
offset;
572 if ((pos >> PAGE_SHIFT) == vma->vm_pgoff)
583 unsigned long total_size;
585 total_size = dev->nbuffers * dev->len_per_image;
587 if (size != dev->len_per_image && size != total_size) {
588 STK_ERROR(
"Wrong size (%lu) needed to be len_per_image=%d or total_size=%lu\n",
589 size, dev->len_per_image, total_size);
594 else if (size > dev->len_per_image)
597 vma->vm_flags |= VM_IO;
599 pos = (
unsigned long) dev->image_data;
602 page = vmalloc_to_pfn((
void *) pos);
604 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
610 if (size > PAGE_SIZE)
632 unsigned int cmd,
void __user *arg)
635 struct video_device *vdev;
637 DECLARE_WAITQUEUE(wait, current);
639 vdev = video_devdata(fp);
640 dev = video_get_drvdata(video_devdata(fp));
642 #if (CONFIG_STK11XX_DEBUG == 1)
643 v4l_printk_ioctl(cmd);
649 case VIDIOC_QUERYCAP:
651 struct v4l2_capability *cap = arg;
655 memset(cap, 0,
sizeof(*cap));
656 strlcpy(cap->driver,
"stk11xx",
sizeof(cap->driver));
658 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
661 if (usb_make_path(dev->
udev, cap->bus_info,
sizeof(cap->bus_info)) < 0)
662 strlcpy(cap->bus_info, dev->
vdev->name,
sizeof(cap->bus_info));
666 case VIDIOC_ENUMINPUT:
668 struct v4l2_input *i = arg;
670 STK_DEBUG(
"VIDIOC_ENUMINPUT %d\n", i->index);
675 strlcpy(i->name,
"USB",
sizeof(i->name));
676 i->type = V4L2_INPUT_TYPE_CAMERA;
682 struct v4l2_input *i = arg;
693 struct v4l2_input *i = arg;
702 case VIDIOC_QUERYCTRL:
706 struct v4l2_queryctrl *c = arg;
708 STK_DEBUG(
"VIDIOC_QUERYCTRL id = %d\n", c->id);
712 for (i=0; i<nbr; i++) {
718 case V4L2_CID_BRIGHTNESS:
719 c->default_value = dev->
vsettings.default_brightness;
721 case V4L2_CID_WHITENESS:
722 c->default_value = dev->
vsettings.default_whiteness;
724 case V4L2_CID_SATURATION:
725 c->default_value = dev->
vsettings.default_colour;
727 case V4L2_CID_CONTRAST:
728 c->default_value = dev->
vsettings.default_contrast;
731 c->default_value = dev->
vsettings.default_hflip;
734 c->default_value = dev->
vsettings.default_vflip;
748 struct v4l2_control *c = arg;
753 case V4L2_CID_BRIGHTNESS:
757 case V4L2_CID_WHITENESS:
761 case V4L2_CID_SATURATION:
765 case V4L2_CID_CONTRAST:
785 struct v4l2_control *c = arg;
787 STK_DEBUG(
"SET CTRL id=%d value=%d\n", c->id, c->value);
790 case V4L2_CID_BRIGHTNESS:
794 case V4L2_CID_WHITENESS:
798 case V4L2_CID_SATURATION:
802 case V4L2_CID_CONTRAST:
822 case VIDIOC_ENUM_FMT:
825 struct v4l2_fmtdesc *fmtd = arg;
827 STK_DEBUG(
"VIDIOC_ENUM_FMT %d\n", fmtd->index);
829 if (fmtd->index != 0)
834 memset(fmtd, 0,
sizeof(*fmtd));
836 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
842 fmtd->pixelformat = V4L2_PIX_FMT_RGB24;
844 strcpy(fmtd->description,
"rgb24");
849 fmtd->pixelformat = V4L2_PIX_FMT_RGB32;
851 strcpy(fmtd->description,
"rgb32");
856 fmtd->pixelformat = V4L2_PIX_FMT_BGR24;
858 strcpy(fmtd->description,
"bgr24");
863 fmtd->pixelformat = V4L2_PIX_FMT_BGR32;
865 strcpy(fmtd->description,
"bgr32");
870 fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
872 strcpy(fmtd->description,
"uyvy");
877 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
879 strcpy(fmtd->description,
"yuyv");
890 struct v4l2_format *fmtd = arg;
891 struct v4l2_pix_format pix_format;
895 if (fmtd->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
898 pix_format.width = dev->view.
x;
899 pix_format.height = dev->view.
y;
900 pix_format.field = V4L2_FIELD_NONE;
901 pix_format.colorspace = V4L2_COLORSPACE_SRGB;
905 case STK11XX_PALETTE_RGB24:
906 pix_format.pixelformat = V4L2_PIX_FMT_RGB24;
907 pix_format.sizeimage = pix_format.width * pix_format.height * 3;
908 pix_format.bytesperline = 3 * pix_format.width;
911 case STK11XX_PALETTE_RGB32:
912 pix_format.pixelformat = V4L2_PIX_FMT_RGB32;
913 pix_format.sizeimage = pix_format.width * pix_format.height * 4;
914 pix_format.bytesperline = 4 * pix_format.width;
917 case STK11XX_PALETTE_BGR24:
918 pix_format.pixelformat = V4L2_PIX_FMT_BGR24;
919 pix_format.sizeimage = pix_format.width * pix_format.height * 3;
920 pix_format.bytesperline = 3 * pix_format.width;
923 case STK11XX_PALETTE_BGR32:
924 pix_format.pixelformat = V4L2_PIX_FMT_BGR32;
925 pix_format.sizeimage = pix_format.width * pix_format.height * 4;
926 pix_format.bytesperline = 4 * pix_format.width;
929 case STK11XX_PALETTE_UYVY:
930 pix_format.pixelformat = V4L2_PIX_FMT_UYVY;
931 pix_format.sizeimage = pix_format.width * pix_format.height * 2;
932 pix_format.bytesperline = 2 * pix_format.width;
935 case STK11XX_PALETTE_YUYV:
936 pix_format.pixelformat = V4L2_PIX_FMT_YUYV;
937 pix_format.sizeimage = pix_format.width * pix_format.height * 2;
938 pix_format.bytesperline = 2 * pix_format.width;
942 memcpy(&(fmtd->fmt.pix), &pix_format,
sizeof(pix_format));
948 struct v4l2_format *fmtd = arg;
952 if (fmtd->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
981 fmtd->fmt.pix.field = V4L2_FIELD_NONE;
982 fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
983 fmtd->fmt.pix.priv = 0;
984 switch (fmtd->fmt.pix.pixelformat) {
985 case V4L2_PIX_FMT_RGB24:
987 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.width * fmtd->fmt.pix.height * 3;
988 fmtd->fmt.pix.bytesperline = 3 * fmtd->fmt.pix.width;
992 case V4L2_PIX_FMT_RGB32:
994 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.width * fmtd->fmt.pix.height * 4;
995 fmtd->fmt.pix.bytesperline = 4 * fmtd->fmt.pix.width;
999 case V4L2_PIX_FMT_BGR24:
1001 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.width * fmtd->fmt.pix.height * 3;
1002 fmtd->fmt.pix.bytesperline = 3 * fmtd->fmt.pix.width;
1006 case V4L2_PIX_FMT_BGR32:
1008 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.width * fmtd->fmt.pix.height * 4;
1009 fmtd->fmt.pix.bytesperline = 4 * fmtd->fmt.pix.width;
1013 case V4L2_PIX_FMT_UYVY:
1015 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.width * fmtd->fmt.pix.height * 2;
1016 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
1020 case V4L2_PIX_FMT_YUYV:
1022 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.width * fmtd->fmt.pix.height * 2;
1023 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
1036 struct v4l2_format *fmtd = arg;
1038 STK_DEBUG(
"SET FMT %d : %d\n", fmtd->type, fmtd->fmt.pix.pixelformat);
1040 if (fmtd->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1044 fmtd->fmt.pix.field = V4L2_FIELD_NONE;
1045 fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
1046 fmtd->fmt.pix.priv = 0;
1049 switch (fmtd->fmt.pix.pixelformat) {
1050 case V4L2_PIX_FMT_RGB24:
1053 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.width * fmtd->fmt.pix.height * 3;
1054 fmtd->fmt.pix.bytesperline = 3 * fmtd->fmt.pix.width;
1058 case V4L2_PIX_FMT_RGB32:
1061 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.width * fmtd->fmt.pix.height * 4;
1062 fmtd->fmt.pix.bytesperline = 4 * fmtd->fmt.pix.width;
1066 case V4L2_PIX_FMT_BGR24:
1069 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.width * fmtd->fmt.pix.height * 3;
1070 fmtd->fmt.pix.bytesperline = 3 * fmtd->fmt.pix.width;
1074 case V4L2_PIX_FMT_BGR32:
1077 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.width * fmtd->fmt.pix.height * 4;
1078 fmtd->fmt.pix.bytesperline = 4 * fmtd->fmt.pix.width;
1082 case V4L2_PIX_FMT_UYVY:
1085 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.width * fmtd->fmt.pix.height * 2;
1086 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
1090 case V4L2_PIX_FMT_YUYV:
1093 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.width * fmtd->fmt.pix.height * 2;
1094 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
1102 STK_DEBUG(
"Set width=%d, height=%d\n", fmtd->fmt.pix.width, fmtd->fmt.pix.height);
1117 STK_ERROR(
"Select video mode failed !\n");
1140 case VIDIOC_QUERYSTD:
1149 v4l2_std_id *std = arg;
1153 *std = V4L2_STD_UNKNOWN;
1159 v4l2_std_id *std = arg;
1163 if (*std != V4L2_STD_UNKNOWN)
1168 case VIDIOC_ENUMSTD:
1170 struct v4l2_standard *std = arg;
1174 if (std->index != 0)
1177 std->id = V4L2_STD_UNKNOWN;
1178 strncpy(std->name,
"webcam",
sizeof(std->name));
1183 case VIDIOC_REQBUFS:
1186 struct v4l2_requestbuffers *rb = arg;
1188 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1191 if (rb->memory != V4L2_MEMORY_MMAP)
1194 nbuffers = rb->count;
1198 else if (nbuffers > dev->nbuffers)
1199 nbuffers = dev->nbuffers;
1201 rb->count = dev->nbuffers;
1205 case VIDIOC_QUERYBUF:
1208 struct v4l2_buffer *buf = arg;
1210 STK_DEBUG(
"QUERY BUFFERS %d %d\n", buf->index, dev->nbuffers);
1212 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1215 if (buf->memory != V4L2_MEMORY_MMAP)
1220 if (index < 0 || index >= dev->nbuffers)
1223 memset(buf, 0,
sizeof(
struct v4l2_buffer));
1225 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1227 buf->m.offset = index * dev->len_per_image;
1228 buf->bytesused = dev->view_size;
1229 buf->field = V4L2_FIELD_NONE;
1230 buf->memory = V4L2_MEMORY_MMAP;
1231 buf->length = dev->len_per_image;
1237 struct v4l2_buffer *buf = arg;
1241 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1244 if (buf->memory != V4L2_MEMORY_MMAP)
1247 if (buf->index < 0 || buf->index >= dev->nbuffers)
1250 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1251 buf->flags &= ~V4L2_BUF_FLAG_DONE;
1258 struct v4l2_buffer *buf = arg;
1262 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1267 while (dev->full_frames == NULL) {
1268 if (dev->error_status) {
1270 set_current_state(TASK_RUNNING);
1272 return -dev->error_status;
1275 if (signal_pending(current)) {
1277 set_current_state(TASK_RUNNING);
1279 return -ERESTARTSYS;
1283 set_current_state(TASK_INTERRUPTIBLE);
1287 set_current_state(TASK_RUNNING);
1289 STK_DEBUG(
"VIDIOC_DQBUF : frame ready.\n");
1296 buf->index = dev->fill_image;
1297 buf->bytesused = dev->view_size;
1298 buf->flags = V4L2_BUF_FLAG_MAPPED;
1299 buf->field = V4L2_FIELD_NONE;
1300 do_gettimeofday(&buf->timestamp);
1302 buf->memory = V4L2_MEMORY_MMAP;
1303 buf->m.offset = dev->fill_image * dev->len_per_image;
1304 buf->length = dev->len_per_image;
1310 case VIDIOC_STREAMON:
1318 case VIDIOC_STREAMOFF:
1328 struct v4l2_streamparm *sp = arg;
1332 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1335 sp->parm.capture.capability = 0;
1336 sp->parm.capture.capturemode = 0;
1337 sp->parm.capture.timeperframe.numerator = 1;
1338 sp->parm.capture.timeperframe.denominator = 30;
1339 sp->parm.capture.readbuffers = 2;
1340 sp->parm.capture.extendedmode = 0;
1345 case VIDIOC_G_AUDIO:
1350 case VIDIOC_S_AUDIO:
1355 case VIDIOC_S_TUNER:
1362 case VIDIOC_OVERLAY:
1366 case VIDIOC_G_TUNER:
1367 case VIDIOC_G_FREQUENCY:
1368 case VIDIOC_S_FREQUENCY:
1372 case VIDIOC_QUERYMENU:
1398 return -ENOIOCTLCMD;
1417 unsigned int cmd,
unsigned long arg)
1421 struct video_device *vdev;
1423 vdev = video_devdata(fp);
1424 dev = video_get_drvdata(video_devdata(fp));
1426 STK_DEBUG(
"v4l_stk11xx_ioctl %02X\n", (
unsigned char) cmd);
1461 dev->
vdev->release = video_device_release;
1462 dev->
vdev->minor = -1;
1464 video_set_drvdata(dev->
vdev, dev);
1466 err = video_register_device(dev->
vdev, VFL_TYPE_GRABBER, -1);
1471 STK_INFO(
"Syntek USB2.0 Camera is now controlling video device /dev/video%d\n", dev->
vdev->minor);
1488 STK_INFO(
"Syntek USB2.0 Camera release resources video device /dev/video%d\n", dev->
vdev->minor);
1490 video_set_drvdata(dev->
vdev, NULL);
1491 video_unregister_device(dev->
vdev);
1503 .owner = THIS_MODULE,