2 * stk-webcam.c : Driver for Syntek 1125 USB webcam controller
4 * Copyright (C) 2006 Nicolas VIVIEN
5 * Copyright 2007-2008 Jaime Velasco Juan <jsagarribay@gmail.com>
7 * Some parts are inspired from cafe_ccic.c
8 * Copyright 2006-2007 Jonathan Corbet
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/errno.h>
29 #include <linux/slab.h>
30 #include <linux/kref.h>
32 #include <linux/usb.h>
33 #include <linux/vmalloc.h>
34 #include <linux/videodev2.h>
35 #include <media/v4l2-common.h>
37 #include "stk-webcam.h"
41 module_param(hflip, bool, 0444);
42 MODULE_PARM_DESC(hflip, "Horizontal image flip (mirror). Defaults to 1");
45 module_param(vflip, bool, 0444);
46 MODULE_PARM_DESC(vflip, "Vertical image flip. Defaults to 1");
49 module_param(debug, int, 0444);
50 MODULE_PARM_DESC(debug, "Debug v4l ioctls. Defaults to 0");
52 MODULE_LICENSE("GPL");
53 MODULE_AUTHOR("Jaime Velasco Juan <jsagarribay@gmail.com> and Nicolas VIVIEN");
54 MODULE_DESCRIPTION("Syntek DC1125 webcam driver");
58 /* Some cameras have audio interfaces, we aren't interested in those */
59 static struct usb_device_id stkwebcam_table[] = {
60 { USB_DEVICE_AND_INTERFACE_INFO(0x174f, 0xa311, 0xff, 0xff, 0xff) },
61 { USB_DEVICE_AND_INTERFACE_INFO(0x05e1, 0x0501, 0xff, 0xff, 0xff) },
64 MODULE_DEVICE_TABLE(usb, stkwebcam_table);
66 void stk_camera_cleanup(struct kref *kref)
68 struct stk_camera *dev = to_stk_camera(kref);
70 STK_INFO("Syntek USB2.0 Camera release resources"
71 " video device /dev/video%d\n", dev->vdev.minor);
72 video_unregister_device(&dev->vdev);
73 dev->vdev.priv = NULL;
75 if (dev->sio_bufs != NULL || dev->isobufs != NULL)
76 STK_ERROR("We are leaking memory\n");
77 usb_put_intf(dev->interface);
85 int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
87 struct usb_device *udev = dev->udev;
90 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
92 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
104 int stk_camera_read_reg(struct stk_camera *dev, u16 index, int *value)
106 struct usb_device *udev = dev->udev;
109 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
111 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
123 static int stk_start_stream(struct stk_camera *dev)
127 int value_116, value_117;
129 if (!is_present(dev))
131 if (!is_memallocd(dev) || !is_initialised(dev)) {
132 STK_ERROR("FIXME: Buffers are not allocated\n");
135 ret = usb_set_interface(dev->udev, 0, 5);
138 STK_ERROR("usb_set_interface failed !\n");
139 if (stk_sensor_wakeup(dev))
140 STK_ERROR("error awaking the sensor\n");
142 stk_camera_read_reg(dev, 0x0116, &value_116);
143 stk_camera_read_reg(dev, 0x0117, &value_117);
145 stk_camera_write_reg(dev, 0x0116, 0x0000);
146 stk_camera_write_reg(dev, 0x0117, 0x0000);
148 stk_camera_read_reg(dev, 0x0100, &value);
149 stk_camera_write_reg(dev, 0x0100, value | 0x80);
151 stk_camera_write_reg(dev, 0x0116, value_116);
152 stk_camera_write_reg(dev, 0x0117, value_117);
153 for (i = 0; i < MAX_ISO_BUFS; i++) {
154 if (dev->isobufs[i].urb) {
155 ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL);
156 atomic_inc(&dev->urbs_used);
165 static int stk_stop_stream(struct stk_camera *dev)
169 if (is_present(dev)) {
170 stk_camera_read_reg(dev, 0x0100, &value);
171 stk_camera_write_reg(dev, 0x0100, value & ~0x80);
172 if (dev->isobufs != NULL) {
173 for (i = 0; i < MAX_ISO_BUFS; i++) {
174 if (dev->isobufs[i].urb)
175 usb_kill_urb(dev->isobufs[i].urb);
178 unset_streaming(dev);
180 if (usb_set_interface(dev->udev, 0, 0))
181 STK_ERROR("usb_set_interface failed !\n");
182 if (stk_sensor_sleep(dev))
183 STK_ERROR("error suspending the sensor\n");
189 * This seems to be the shortest init sequence we
190 * must do in order to find the sensor
191 * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
192 * is also reset. Maybe powers down it?
193 * Rest of values don't make a difference
196 static struct regval stk1125_initvals[] = {
197 /*TODO: What means this sequence? */
226 static int stk_initialise(struct stk_camera *dev)
230 if (!is_present(dev))
232 if (is_initialised(dev))
234 rv = stk1125_initvals;
235 while (rv->reg != 0xffff) {
236 ret = stk_camera_write_reg(dev, rv->reg, rv->val);
241 if (stk_sensor_init(dev) == 0) {
242 set_initialised(dev);
248 /* sysfs functions */
249 /*FIXME cleanup this */
251 static ssize_t show_brightness(struct device *class,
252 struct device_attribute *attr, char *buf)
254 struct video_device *vdev = to_video_device(class);
255 struct stk_camera *dev = vdev_to_camera(vdev);
257 return sprintf(buf, "%X\n", dev->vsettings.brightness);
260 static ssize_t store_brightness(struct device *class,
261 struct device_attribute *attr, const char *buf, size_t count)
267 struct video_device *vdev = to_video_device(class);
268 struct stk_camera *dev = vdev_to_camera(vdev);
270 value = simple_strtoul(buf, &endp, 16);
272 dev->vsettings.brightness = (int) value;
274 ret = stk_sensor_set_brightness(dev, value >> 8);
281 static ssize_t show_hflip(struct device *class,
282 struct device_attribute *attr, char *buf)
284 struct video_device *vdev = to_video_device(class);
285 struct stk_camera *dev = vdev_to_camera(vdev);
287 return sprintf(buf, "%d\n", dev->vsettings.hflip);
290 static ssize_t store_hflip(struct device *class,
291 struct device_attribute *attr, const char *buf, size_t count)
293 struct video_device *vdev = to_video_device(class);
294 struct stk_camera *dev = vdev_to_camera(vdev);
296 if (strncmp(buf, "1", 1) == 0)
297 dev->vsettings.hflip = 1;
298 else if (strncmp(buf, "0", 1) == 0)
299 dev->vsettings.hflip = 0;
306 static ssize_t show_vflip(struct device *class,
307 struct device_attribute *attr, char *buf)
309 struct video_device *vdev = to_video_device(class);
310 struct stk_camera *dev = vdev_to_camera(vdev);
312 return sprintf(buf, "%d\n", dev->vsettings.vflip);
315 static ssize_t store_vflip(struct device *class,
316 struct device_attribute *attr, const char *buf, size_t count)
318 struct video_device *vdev = to_video_device(class);
319 struct stk_camera *dev = vdev_to_camera(vdev);
321 if (strncmp(buf, "1", 1) == 0)
322 dev->vsettings.vflip = 1;
323 else if (strncmp(buf, "0", 1) == 0)
324 dev->vsettings.vflip = 0;
331 static DEVICE_ATTR(brightness, S_IRUGO | S_IWUGO,
332 show_brightness, store_brightness);
333 static DEVICE_ATTR(hflip, S_IRUGO | S_IWUGO, show_hflip, store_hflip);
334 static DEVICE_ATTR(vflip, S_IRUGO | S_IWUGO, show_vflip, store_vflip);
336 static int stk_create_sysfs_files(struct video_device *vdev)
340 ret = video_device_create_file(vdev, &dev_attr_brightness);
341 ret += video_device_create_file(vdev, &dev_attr_hflip);
342 ret += video_device_create_file(vdev, &dev_attr_vflip);
346 static void stk_remove_sysfs_files(struct video_device *vdev)
348 video_device_remove_file(vdev, &dev_attr_brightness);
349 video_device_remove_file(vdev, &dev_attr_hflip);
350 video_device_remove_file(vdev, &dev_attr_vflip);
354 /* *********************************************** */
356 * This function is called as an URB transfert is complete (Isochronous pipe).
357 * So, the traitement is done in interrupt time, so it has be fast, not crash,
358 * and not stall. Neat.
360 static void stk_isoc_handler(struct urb *urb)
367 unsigned char *fill = NULL;
368 unsigned char *iso_buf = NULL;
370 struct stk_camera *dev;
371 struct stk_sio_buffer *fb;
373 dev = (struct stk_camera *) urb->context;
376 STK_ERROR("isoc_handler called with NULL device !\n");
380 if (urb->status == -ENOENT || urb->status == -ECONNRESET
381 || urb->status == -ESHUTDOWN) {
382 atomic_dec(&dev->urbs_used);
386 spin_lock_irqsave(&dev->spinlock, flags);
388 if (urb->status != -EINPROGRESS && urb->status != 0) {
389 STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
393 if (list_empty(&dev->sio_avail)) {
394 /*FIXME Stop streaming after a while */
395 (void) (printk_ratelimit() &&
396 STK_ERROR("isoc_handler without available buffer!\n"));
399 fb = list_first_entry(&dev->sio_avail,
400 struct stk_sio_buffer, list);
401 fill = fb->buffer + fb->v4lbuf.bytesused;
403 for (i = 0; i < urb->number_of_packets; i++) {
404 if (urb->iso_frame_desc[i].status != 0) {
405 if (urb->iso_frame_desc[i].status != -EXDEV)
406 STK_ERROR("Frame %d has error %d\n", i,
407 urb->iso_frame_desc[i].status);
410 framelen = urb->iso_frame_desc[i].actual_length;
411 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
414 continue; /* no data */
417 * we found something informational from there
418 * the isoc frames have to type of headers
419 * type1: 00 xx 00 00 or 20 xx 00 00
420 * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
421 * xx is a sequencer which has never been seen over 0x3f
422 * imho data written down looks like bayer, i see similarities
423 * after every 640 bytes
425 if (*iso_buf & 0x80) {
428 /* This marks a new frame */
429 if (fb->v4lbuf.bytesused != 0
430 && fb->v4lbuf.bytesused != dev->frame_size) {
431 (void) (printk_ratelimit() &&
432 STK_ERROR("frame %d, "
433 "bytesused=%d, skipping\n",
434 i, fb->v4lbuf.bytesused));
435 fb->v4lbuf.bytesused = 0;
437 } else if (fb->v4lbuf.bytesused == dev->frame_size) {
438 list_move_tail(dev->sio_avail.next,
440 wake_up(&dev->wait_frame);
441 if (list_empty(&dev->sio_avail)) {
442 (void) (printk_ratelimit() &&
443 STK_ERROR("No buffer available\n"));
446 fb = list_first_entry(&dev->sio_avail,
447 struct stk_sio_buffer, list);
448 fb->v4lbuf.bytesused = 0;
456 /* Our buffer is full !!! */
457 if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
458 (void) (printk_ratelimit() &&
459 STK_ERROR("Frame buffer overflow, lost sync\n"));
460 /*FIXME Do something here? */
463 spin_unlock_irqrestore(&dev->spinlock, flags);
464 memcpy(fill, iso_buf, framelen);
465 spin_lock_irqsave(&dev->spinlock, flags);
468 /* New size of our buffer */
469 fb->v4lbuf.bytesused += framelen;
473 spin_unlock_irqrestore(&dev->spinlock, flags);
474 urb->dev = dev->udev;
475 ret = usb_submit_urb(urb, GFP_ATOMIC);
477 STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
482 /* -------------------------------------------- */
484 static int stk_prepare_iso(struct stk_camera *dev)
489 struct usb_device *udev;
496 STK_ERROR("isobufs already allocated. Bad\n");
498 dev->isobufs = kzalloc(MAX_ISO_BUFS * sizeof(*dev->isobufs),
500 if (dev->isobufs == NULL) {
501 STK_ERROR("Unable to allocate iso buffers\n");
504 for (i = 0; i < MAX_ISO_BUFS; i++) {
505 if (dev->isobufs[i].data == NULL) {
506 kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
508 STK_ERROR("Failed to allocate iso buffer %d\n",
512 dev->isobufs[i].data = kbuf;
514 STK_ERROR("isobuf data already allocated\n");
515 if (dev->isobufs[i].urb == NULL) {
516 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
518 STK_ERROR("Failed to allocate URB %d\n", i);
521 dev->isobufs[i].urb = urb;
523 STK_ERROR("Killing URB\n");
524 usb_kill_urb(dev->isobufs[i].urb);
525 urb = dev->isobufs[i].urb;
529 urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
530 urb->transfer_flags = URB_ISO_ASAP;
531 urb->transfer_buffer = dev->isobufs[i].data;
532 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
533 urb->complete = stk_isoc_handler;
535 urb->start_frame = 0;
536 urb->number_of_packets = ISO_FRAMES_PER_DESC;
538 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
539 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
540 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
547 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
548 kfree(dev->isobufs[i].data);
549 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
550 usb_free_urb(dev->isobufs[i].urb);
556 static void stk_clean_iso(struct stk_camera *dev)
560 if (dev == NULL || dev->isobufs == NULL)
563 for (i = 0; i < MAX_ISO_BUFS; i++) {
566 urb = dev->isobufs[i].urb;
568 if (atomic_read(&dev->urbs_used))
572 kfree(dev->isobufs[i].data);
576 unset_memallocd(dev);
579 static int stk_setup_siobuf(struct stk_camera *dev, int index)
581 struct stk_sio_buffer *buf = dev->sio_bufs + index;
582 INIT_LIST_HEAD(&buf->list);
583 buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
584 buf->buffer = vmalloc_user(buf->v4lbuf.length);
585 if (buf->buffer == NULL)
589 buf->v4lbuf.index = index;
590 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
591 buf->v4lbuf.field = V4L2_FIELD_NONE;
592 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
593 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
597 static int stk_free_sio_buffers(struct stk_camera *dev)
602 if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
605 * If any buffers are mapped, we cannot free them at all.
607 for (i = 0; i < dev->n_sbufs; i++) {
608 if (dev->sio_bufs[i].mapcount > 0)
614 spin_lock_irqsave(&dev->spinlock, flags);
615 INIT_LIST_HEAD(&dev->sio_avail);
616 INIT_LIST_HEAD(&dev->sio_full);
617 nbufs = dev->n_sbufs;
619 spin_unlock_irqrestore(&dev->spinlock, flags);
620 for (i = 0; i < nbufs; i++) {
621 if (dev->sio_bufs[i].buffer != NULL)
622 vfree(dev->sio_bufs[i].buffer);
624 kfree(dev->sio_bufs);
625 dev->sio_bufs = NULL;
629 static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
632 if (dev->sio_bufs != NULL)
633 STK_ERROR("sio_bufs already allocated\n");
635 dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
637 if (dev->sio_bufs == NULL)
639 for (i = 0; i < n_sbufs; i++) {
640 if (stk_setup_siobuf(dev, i))
641 return (dev->n_sbufs > 1)? 0 : -ENOMEM;
648 static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
651 err = stk_prepare_iso(dev);
656 err = stk_prepare_sio_buffers(dev, n_sbufs);
658 stk_free_sio_buffers(dev);
664 static void stk_free_buffers(struct stk_camera *dev)
667 stk_free_sio_buffers(dev);
669 /* -------------------------------------------- */
671 /* v4l file operations */
673 static int v4l_stk_open(struct inode *inode, struct file *fp)
675 struct stk_camera *dev;
676 struct video_device *vdev;
678 vdev = video_devdata(fp);
679 dev = vdev_to_camera(vdev);
681 if (dev == NULL || !is_present(dev))
683 fp->private_data = vdev;
684 kref_get(&dev->kref);
689 static int v4l_stk_release(struct inode *inode, struct file *fp)
691 struct stk_camera *dev;
692 struct video_device *vdev;
694 vdev = video_devdata(fp);
696 STK_ERROR("v4l_release called w/o video devdata\n");
699 dev = vdev_to_camera(vdev);
701 STK_ERROR("v4l_release called on removed device\n");
705 if (dev->owner != fp) {
706 kref_put(&dev->kref, stk_camera_cleanup);
710 stk_stop_stream(dev);
712 stk_free_buffers(dev);
716 kref_put(&dev->kref, stk_camera_cleanup);
721 static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
722 size_t count, loff_t *f_pos)
727 struct stk_camera *dev;
728 struct video_device *vdev;
729 struct stk_sio_buffer *sbuf;
731 vdev = video_devdata(fp);
734 dev = vdev_to_camera(vdev);
739 if (!is_present(dev))
741 if (dev->owner && dev->owner != fp)
744 if (!is_streaming(dev)) {
745 if (stk_initialise(dev)
746 || stk_allocate_buffers(dev, 3)
747 || stk_start_stream(dev))
749 spin_lock_irqsave(&dev->spinlock, flags);
750 for (i = 0; i < dev->n_sbufs; i++) {
751 list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
752 dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
754 spin_unlock_irqrestore(&dev->spinlock, flags);
757 if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
759 ret = wait_event_interruptible(dev->wait_frame,
760 !list_empty(&dev->sio_full) || !is_present(dev));
763 if (!is_present(dev))
766 if (count + *f_pos > dev->frame_size)
767 count = dev->frame_size - *f_pos;
768 spin_lock_irqsave(&dev->spinlock, flags);
769 if (list_empty(&dev->sio_full)) {
770 spin_unlock_irqrestore(&dev->spinlock, flags);
771 STK_ERROR("BUG: No siobufs ready\n");
774 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
775 spin_unlock_irqrestore(&dev->spinlock, flags);
777 if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
782 if (*f_pos >= dev->frame_size) {
784 spin_lock_irqsave(&dev->spinlock, flags);
785 list_move_tail(&sbuf->list, &dev->sio_avail);
786 spin_unlock_irqrestore(&dev->spinlock, flags);
791 static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
793 struct stk_camera *dev;
794 struct video_device *vdev;
796 vdev = video_devdata(fp);
801 dev = vdev_to_camera(vdev);
805 poll_wait(fp, &dev->wait_frame, wait);
807 if (!is_present(dev))
810 if (!list_empty(&dev->sio_full))
811 return (POLLIN | POLLRDNORM);
817 static void stk_v4l_vm_open(struct vm_area_struct *vma)
819 struct stk_sio_buffer *sbuf = vma->vm_private_data;
822 static void stk_v4l_vm_close(struct vm_area_struct *vma)
824 struct stk_sio_buffer *sbuf = vma->vm_private_data;
826 if (sbuf->mapcount == 0)
827 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
829 static struct vm_operations_struct stk_v4l_vm_ops = {
830 .open = stk_v4l_vm_open,
831 .close = stk_v4l_vm_close
834 static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
838 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
839 struct stk_camera *dev;
840 struct video_device *vdev;
841 struct stk_sio_buffer *sbuf = NULL;
843 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
846 vdev = video_devdata(fp);
847 dev = vdev_to_camera(vdev);
849 for (i = 0; i < dev->n_sbufs; i++) {
850 if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
851 sbuf = dev->sio_bufs + i;
857 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
860 vma->vm_flags |= VM_DONTEXPAND;
861 vma->vm_private_data = sbuf;
862 vma->vm_ops = &stk_v4l_vm_ops;
863 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
864 stk_v4l_vm_open(vma);
868 /* v4l ioctl handlers */
870 static int stk_vidioc_querycap(struct file *filp,
871 void *priv, struct v4l2_capability *cap)
873 strcpy(cap->driver, "stk");
874 strcpy(cap->card, "stk");
875 cap->version = DRIVER_VERSION_NUM;
877 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
878 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
882 static int stk_vidioc_enum_input(struct file *filp,
883 void *priv, struct v4l2_input *input)
885 if (input->index != 0)
888 strcpy(input->name, "Syntek USB Camera");
889 input->type = V4L2_INPUT_TYPE_CAMERA;
894 static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
900 static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
909 static int stk_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
914 /* List of all V4Lv2 controls supported by the driver */
915 static struct v4l2_queryctrl stk_controls[] = {
917 .id = V4L2_CID_BRIGHTNESS,
918 .type = V4L2_CTRL_TYPE_INTEGER,
919 .name = "Brightness",
923 .default_value = 0x6000,
925 /*TODO: get more controls to work */
928 static int stk_vidioc_queryctrl(struct file *filp,
929 void *priv, struct v4l2_queryctrl *c)
933 nbr = ARRAY_SIZE(stk_controls);
935 for (i = 0; i < nbr; i++) {
936 if (stk_controls[i].id == c->id) {
937 memcpy(c, &stk_controls[i],
938 sizeof(struct v4l2_queryctrl));
945 static int stk_vidioc_g_ctrl(struct file *filp,
946 void *priv, struct v4l2_control *c)
948 struct stk_camera *dev = priv;
950 case V4L2_CID_BRIGHTNESS:
951 c->value = dev->vsettings.brightness;
959 static int stk_vidioc_s_ctrl(struct file *filp,
960 void *priv, struct v4l2_control *c)
962 struct stk_camera *dev = priv;
964 case V4L2_CID_BRIGHTNESS:
965 dev->vsettings.brightness = c->value;
966 return stk_sensor_set_brightness(dev, c->value >> 8);
974 static int stk_vidioc_enum_fmt_cap(struct file *filp,
975 void *priv, struct v4l2_fmtdesc *fmtd)
979 switch (fmtd->index) {
981 fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
982 strcpy(fmtd->description, "r5g6b5");
985 fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
986 strcpy(fmtd->description, "r5g6b5BE");
989 fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
990 strcpy(fmtd->description, "yuv4:2:2");
993 fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
994 strcpy(fmtd->description, "Raw bayer");
1002 static struct stk_size {
1007 { .w = 1280, .h = 1024, .m = MODE_SXGA, },
1008 { .w = 640, .h = 480, .m = MODE_VGA, },
1009 { .w = 352, .h = 288, .m = MODE_CIF, },
1010 { .w = 320, .h = 240, .m = MODE_QVGA, },
1011 { .w = 176, .h = 144, .m = MODE_QCIF, },
1014 static int stk_vidioc_g_fmt_cap(struct file *filp,
1015 void *priv, struct v4l2_format *f)
1017 struct v4l2_pix_format *pix_format = &f->fmt.pix;
1018 struct stk_camera *dev = priv;
1021 for (i = 0; i < ARRAY_SIZE(stk_sizes)
1022 && stk_sizes[i].m != dev->vsettings.mode;
1024 if (i == ARRAY_SIZE(stk_sizes)) {
1025 STK_ERROR("ERROR: mode invalid\n");
1028 pix_format->width = stk_sizes[i].w;
1029 pix_format->height = stk_sizes[i].h;
1030 pix_format->field = V4L2_FIELD_NONE;
1031 pix_format->colorspace = V4L2_COLORSPACE_SRGB;
1032 pix_format->priv = 0;
1033 pix_format->pixelformat = dev->vsettings.palette;
1034 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
1035 pix_format->bytesperline = pix_format->width;
1037 pix_format->bytesperline = 2 * pix_format->width;
1038 pix_format->sizeimage = pix_format->bytesperline
1039 * pix_format->height;
1043 static int stk_vidioc_try_fmt_cap(struct file *filp,
1044 void *priv, struct v4l2_format *fmtd)
1047 switch (fmtd->fmt.pix.pixelformat) {
1048 case V4L2_PIX_FMT_RGB565:
1049 case V4L2_PIX_FMT_RGB565X:
1050 case V4L2_PIX_FMT_UYVY:
1051 case V4L2_PIX_FMT_SBGGR8:
1056 for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
1057 if (fmtd->fmt.pix.width > stk_sizes[i].w)
1060 if (i == ARRAY_SIZE(stk_sizes)
1061 || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
1062 < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
1063 fmtd->fmt.pix.height = stk_sizes[i-1].h;
1064 fmtd->fmt.pix.width = stk_sizes[i-1].w;
1065 fmtd->fmt.pix.priv = i - 1;
1067 fmtd->fmt.pix.height = stk_sizes[i].h;
1068 fmtd->fmt.pix.width = stk_sizes[i].w;
1069 fmtd->fmt.pix.priv = i;
1072 fmtd->fmt.pix.field = V4L2_FIELD_NONE;
1073 fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
1074 if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
1075 fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
1077 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
1078 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
1079 * fmtd->fmt.pix.height;
1083 static int stk_vidioc_s_fmt_cap(struct file *filp,
1084 void *priv, struct v4l2_format *fmtd)
1087 struct stk_camera *dev = priv;
1091 if (!is_present(dev))
1093 if (is_streaming(dev))
1095 if (dev->owner && dev->owner != filp)
1098 ret = stk_vidioc_try_fmt_cap(filp, priv, fmtd);
1102 dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
1103 stk_free_buffers(dev);
1104 dev->frame_size = fmtd->fmt.pix.sizeimage;
1105 dev->vsettings.mode = stk_sizes[fmtd->fmt.pix.priv].m;
1107 stk_initialise(dev);
1108 /* This registers controls some timings, not sure of what. */
1109 stk_camera_write_reg(dev, 0x001b, 0x0e);
1110 if (dev->vsettings.mode == MODE_SXGA)
1111 stk_camera_write_reg(dev, 0x001c, 0x0e);
1113 stk_camera_write_reg(dev, 0x001c, 0x46);
1115 * Registers 0x0115 0x0114 are the size of each line (bytes),
1116 * regs 0x0117 0x0116 are the heigth of the image.
1118 stk_camera_write_reg(dev, 0x0115,
1119 (fmtd->fmt.pix.bytesperline >> 8) & 0xff);
1120 stk_camera_write_reg(dev, 0x0114,
1121 fmtd->fmt.pix.bytesperline & 0xff);
1122 stk_camera_write_reg(dev, 0x0117,
1123 (fmtd->fmt.pix.height >> 8) & 0xff);
1124 stk_camera_write_reg(dev, 0x0116,
1125 fmtd->fmt.pix.height & 0xff);
1126 return stk_sensor_configure(dev);
1129 static int stk_vidioc_reqbufs(struct file *filp,
1130 void *priv, struct v4l2_requestbuffers *rb)
1132 struct stk_camera *dev = priv;
1136 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1138 if (rb->memory != V4L2_MEMORY_MMAP)
1140 if (is_streaming(dev)
1141 || (dev->owner && dev->owner != filp))
1145 /*FIXME If they ask for zero, we must stop streaming and free */
1148 /* Arbitrary limit */
1149 else if (rb->count > 5)
1152 stk_allocate_buffers(dev, rb->count);
1153 rb->count = dev->n_sbufs;
1157 static int stk_vidioc_querybuf(struct file *filp,
1158 void *priv, struct v4l2_buffer *buf)
1161 struct stk_camera *dev = priv;
1162 struct stk_sio_buffer *sbuf;
1164 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1169 if (index < 0 || index >= dev->n_sbufs)
1171 sbuf = dev->sio_bufs + buf->index;
1172 *buf = sbuf->v4lbuf;
1176 static int stk_vidioc_qbuf(struct file *filp,
1177 void *priv, struct v4l2_buffer *buf)
1179 struct stk_camera *dev = priv;
1180 struct stk_sio_buffer *sbuf;
1181 unsigned long flags;
1182 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1185 if (buf->memory != V4L2_MEMORY_MMAP)
1188 if (buf->index < 0 || buf->index >= dev->n_sbufs)
1190 sbuf = dev->sio_bufs + buf->index;
1191 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
1193 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1194 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1195 spin_lock_irqsave(&dev->spinlock, flags);
1196 list_add_tail(&sbuf->list, &dev->sio_avail);
1197 *buf = sbuf->v4lbuf;
1198 spin_unlock_irqrestore(&dev->spinlock, flags);
1202 static int stk_vidioc_dqbuf(struct file *filp,
1203 void *priv, struct v4l2_buffer *buf)
1205 struct stk_camera *dev = priv;
1206 struct stk_sio_buffer *sbuf;
1207 unsigned long flags;
1210 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1211 || !is_streaming(dev))
1214 if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
1215 return -EWOULDBLOCK;
1216 ret = wait_event_interruptible(dev->wait_frame,
1217 !list_empty(&dev->sio_full) || !is_present(dev));
1220 if (!is_present(dev))
1223 spin_lock_irqsave(&dev->spinlock, flags);
1224 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
1225 list_del_init(&sbuf->list);
1226 spin_unlock_irqrestore(&dev->spinlock, flags);
1227 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1228 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1229 sbuf->v4lbuf.sequence = ++dev->sequence;
1230 do_gettimeofday(&sbuf->v4lbuf.timestamp);
1232 *buf = sbuf->v4lbuf;
1236 static int stk_vidioc_streamon(struct file *filp,
1237 void *priv, enum v4l2_buf_type type)
1239 struct stk_camera *dev = priv;
1240 if (is_streaming(dev))
1242 if (dev->sio_bufs == NULL)
1245 return stk_start_stream(dev);
1248 static int stk_vidioc_streamoff(struct file *filp,
1249 void *priv, enum v4l2_buf_type type)
1251 struct stk_camera *dev = priv;
1252 unsigned long flags;
1254 stk_stop_stream(dev);
1255 spin_lock_irqsave(&dev->spinlock, flags);
1256 INIT_LIST_HEAD(&dev->sio_avail);
1257 INIT_LIST_HEAD(&dev->sio_full);
1258 for (i = 0; i < dev->n_sbufs; i++) {
1259 INIT_LIST_HEAD(&dev->sio_bufs[i].list);
1260 dev->sio_bufs[i].v4lbuf.flags = 0;
1262 spin_unlock_irqrestore(&dev->spinlock, flags);
1267 static int stk_vidioc_g_parm(struct file *filp,
1268 void *priv, struct v4l2_streamparm *sp)
1270 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1273 sp->parm.capture.capability = 0;
1274 sp->parm.capture.capturemode = 0;
1275 /*FIXME This is not correct */
1276 sp->parm.capture.timeperframe.numerator = 1;
1277 sp->parm.capture.timeperframe.denominator = 30;
1278 sp->parm.capture.readbuffers = 2;
1279 sp->parm.capture.extendedmode = 0;
1283 static struct file_operations v4l_stk_fops = {
1284 .owner = THIS_MODULE,
1285 .open = v4l_stk_open,
1286 .release = v4l_stk_release,
1287 .read = v4l_stk_read,
1288 .poll = v4l_stk_poll,
1289 .mmap = v4l_stk_mmap,
1290 .ioctl = video_ioctl2,
1294 static void stk_v4l_dev_release(struct video_device *vd)
1298 static struct video_device stk_v4l_data = {
1299 .name = "stkwebcam",
1300 .type = VFL_TYPE_GRABBER,
1301 .type2 = VID_TYPE_CAPTURE,
1303 .tvnorms = V4L2_STD_UNKNOWN,
1304 .current_norm = V4L2_STD_UNKNOWN,
1305 .fops = &v4l_stk_fops,
1306 .release = stk_v4l_dev_release,
1308 .vidioc_querycap = stk_vidioc_querycap,
1309 .vidioc_enum_fmt_cap = stk_vidioc_enum_fmt_cap,
1310 .vidioc_try_fmt_cap = stk_vidioc_try_fmt_cap,
1311 .vidioc_s_fmt_cap = stk_vidioc_s_fmt_cap,
1312 .vidioc_g_fmt_cap = stk_vidioc_g_fmt_cap,
1313 .vidioc_enum_input = stk_vidioc_enum_input,
1314 .vidioc_s_input = stk_vidioc_s_input,
1315 .vidioc_g_input = stk_vidioc_g_input,
1316 .vidioc_s_std = stk_vidioc_s_std,
1317 .vidioc_reqbufs = stk_vidioc_reqbufs,
1318 .vidioc_querybuf = stk_vidioc_querybuf,
1319 .vidioc_qbuf = stk_vidioc_qbuf,
1320 .vidioc_dqbuf = stk_vidioc_dqbuf,
1321 .vidioc_streamon = stk_vidioc_streamon,
1322 .vidioc_streamoff = stk_vidioc_streamoff,
1323 .vidioc_queryctrl = stk_vidioc_queryctrl,
1324 .vidioc_g_ctrl = stk_vidioc_g_ctrl,
1325 .vidioc_s_ctrl = stk_vidioc_s_ctrl,
1326 .vidioc_g_parm = stk_vidioc_g_parm,
1330 static int stk_register_video_device(struct stk_camera *dev)
1334 dev->vdev = stk_v4l_data;
1335 dev->vdev.debug = debug;
1336 dev->vdev.dev = &dev->interface->dev;
1337 dev->vdev.priv = dev;
1338 err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1340 STK_ERROR("v4l registration failed\n");
1342 STK_INFO("Syntek USB2.0 Camera is now controlling video device"
1343 " /dev/video%d\n", dev->vdev.minor);
1350 static int stk_camera_probe(struct usb_interface *interface,
1351 const struct usb_device_id *id)
1356 struct stk_camera *dev = NULL;
1357 struct usb_device *udev = interface_to_usbdev(interface);
1358 struct usb_host_interface *iface_desc;
1359 struct usb_endpoint_descriptor *endpoint;
1361 dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
1363 STK_ERROR("Out of memory !\n");
1367 kref_init(&dev->kref);
1368 spin_lock_init(&dev->spinlock);
1369 init_waitqueue_head(&dev->wait_frame);
1372 dev->interface = interface;
1373 usb_get_intf(interface);
1375 dev->vsettings.vflip = vflip;
1376 dev->vsettings.hflip = hflip;
1380 /* Set up the endpoint information
1381 * use only the first isoc-in endpoint
1382 * for the current alternate setting */
1383 iface_desc = interface->cur_altsetting;
1385 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1386 endpoint = &iface_desc->endpoint[i].desc;
1389 && ((endpoint->bEndpointAddress
1390 & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
1391 && ((endpoint->bmAttributes
1392 & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_ISOC)) {
1393 /* we found an isoc in endpoint */
1394 dev->isoc_ep = (endpoint->bEndpointAddress & 0xF);
1398 if (!dev->isoc_ep) {
1399 STK_ERROR("Could not find isoc-in endpoint");
1400 kref_put(&dev->kref, stk_camera_cleanup);
1403 dev->vsettings.brightness = 0x7fff;
1404 dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
1405 dev->vsettings.mode = MODE_VGA;
1406 dev->frame_size = 640*480*2;
1408 INIT_LIST_HEAD(&dev->sio_avail);
1409 INIT_LIST_HEAD(&dev->sio_full);
1411 usb_set_intfdata(interface, dev);
1413 err = stk_register_video_device(dev);
1415 kref_put(&dev->kref, stk_camera_cleanup);
1419 stk_create_sysfs_files(&dev->vdev);
1424 static void stk_camera_disconnect(struct usb_interface *interface)
1426 struct stk_camera *dev = usb_get_intfdata(interface);
1428 usb_set_intfdata(interface, NULL);
1431 wake_up_interruptible(&dev->wait_frame);
1432 stk_remove_sysfs_files(&dev->vdev);
1434 kref_put(&dev->kref, stk_camera_cleanup);
1437 static struct usb_driver stk_camera_driver = {
1438 .name = "stkwebcam",
1439 .probe = stk_camera_probe,
1440 .disconnect = stk_camera_disconnect,
1441 .id_table = stkwebcam_table,
1445 static int __init stk_camera_init(void)
1449 result = usb_register(&stk_camera_driver);
1451 STK_ERROR("usb_register failed ! Error number %d\n", result);
1457 static void __exit stk_camera_exit(void)
1459 usb_deregister(&stk_camera_driver);
1462 module_init(stk_camera_init);
1463 module_exit(stk_camera_exit);