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>
34 #include <linux/vmalloc.h>
35 #include <linux/videodev2.h>
36 #include <media/v4l2-common.h>
38 #include "stk-webcam.h"
42 module_param(hflip, bool, 0444);
43 MODULE_PARM_DESC(hflip, "Horizontal image flip (mirror). Defaults to 1");
46 module_param(vflip, bool, 0444);
47 MODULE_PARM_DESC(vflip, "Vertical image flip. Defaults to 1");
50 module_param(debug, int, 0444);
51 MODULE_PARM_DESC(debug, "Debug v4l ioctls. Defaults to 0");
53 MODULE_LICENSE("GPL");
54 MODULE_AUTHOR("Jaime Velasco Juan <jsagarribay@gmail.com> and Nicolas VIVIEN");
55 MODULE_DESCRIPTION("Syntek DC1125 webcam driver");
59 /* Some cameras have audio interfaces, we aren't interested in those */
60 static struct usb_device_id stkwebcam_table[] = {
61 { USB_DEVICE_AND_INTERFACE_INFO(0x174f, 0xa311, 0xff, 0xff, 0xff) },
62 { USB_DEVICE_AND_INTERFACE_INFO(0x05e1, 0x0501, 0xff, 0xff, 0xff) },
65 MODULE_DEVICE_TABLE(usb, stkwebcam_table);
67 static void stk_camera_cleanup(struct kref *kref)
69 struct stk_camera *dev = to_stk_camera(kref);
71 STK_INFO("Syntek USB2.0 Camera release resources"
72 " video device /dev/video%d\n", dev->vdev.minor);
73 video_unregister_device(&dev->vdev);
74 dev->vdev.priv = NULL;
76 if (dev->sio_bufs != NULL || dev->isobufs != NULL)
77 STK_ERROR("We are leaking memory\n");
78 usb_put_intf(dev->interface);
86 int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
88 struct usb_device *udev = dev->udev;
91 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
93 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
105 int stk_camera_read_reg(struct stk_camera *dev, u16 index, int *value)
107 struct usb_device *udev = dev->udev;
110 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
112 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
124 static int stk_start_stream(struct stk_camera *dev)
128 int value_116, value_117;
130 if (!is_present(dev))
132 if (!is_memallocd(dev) || !is_initialised(dev)) {
133 STK_ERROR("FIXME: Buffers are not allocated\n");
136 ret = usb_set_interface(dev->udev, 0, 5);
139 STK_ERROR("usb_set_interface failed !\n");
140 if (stk_sensor_wakeup(dev))
141 STK_ERROR("error awaking the sensor\n");
143 stk_camera_read_reg(dev, 0x0116, &value_116);
144 stk_camera_read_reg(dev, 0x0117, &value_117);
146 stk_camera_write_reg(dev, 0x0116, 0x0000);
147 stk_camera_write_reg(dev, 0x0117, 0x0000);
149 stk_camera_read_reg(dev, 0x0100, &value);
150 stk_camera_write_reg(dev, 0x0100, value | 0x80);
152 stk_camera_write_reg(dev, 0x0116, value_116);
153 stk_camera_write_reg(dev, 0x0117, value_117);
154 for (i = 0; i < MAX_ISO_BUFS; i++) {
155 if (dev->isobufs[i].urb) {
156 ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL);
157 atomic_inc(&dev->urbs_used);
166 static int stk_stop_stream(struct stk_camera *dev)
170 if (is_present(dev)) {
171 stk_camera_read_reg(dev, 0x0100, &value);
172 stk_camera_write_reg(dev, 0x0100, value & ~0x80);
173 if (dev->isobufs != NULL) {
174 for (i = 0; i < MAX_ISO_BUFS; i++) {
175 if (dev->isobufs[i].urb)
176 usb_kill_urb(dev->isobufs[i].urb);
179 unset_streaming(dev);
181 if (usb_set_interface(dev->udev, 0, 0))
182 STK_ERROR("usb_set_interface failed !\n");
183 if (stk_sensor_sleep(dev))
184 STK_ERROR("error suspending the sensor\n");
190 * This seems to be the shortest init sequence we
191 * must do in order to find the sensor
192 * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
193 * is also reset. Maybe powers down it?
194 * Rest of values don't make a difference
197 static struct regval stk1125_initvals[] = {
198 /*TODO: What means this sequence? */
227 static int stk_initialise(struct stk_camera *dev)
231 if (!is_present(dev))
233 if (is_initialised(dev))
235 rv = stk1125_initvals;
236 while (rv->reg != 0xffff) {
237 ret = stk_camera_write_reg(dev, rv->reg, rv->val);
242 if (stk_sensor_init(dev) == 0) {
243 set_initialised(dev);
249 #ifdef CONFIG_VIDEO_V4L1_COMPAT
251 /* sysfs functions */
252 /*FIXME cleanup this */
254 static ssize_t show_brightness(struct device *class,
255 struct device_attribute *attr, char *buf)
257 struct video_device *vdev = to_video_device(class);
258 struct stk_camera *dev = vdev_to_camera(vdev);
260 return sprintf(buf, "%X\n", dev->vsettings.brightness);
263 static ssize_t store_brightness(struct device *class,
264 struct device_attribute *attr, const char *buf, size_t count)
270 struct video_device *vdev = to_video_device(class);
271 struct stk_camera *dev = vdev_to_camera(vdev);
273 value = simple_strtoul(buf, &endp, 16);
275 dev->vsettings.brightness = (int) value;
277 ret = stk_sensor_set_brightness(dev, value >> 8);
284 static ssize_t show_hflip(struct device *class,
285 struct device_attribute *attr, char *buf)
287 struct video_device *vdev = to_video_device(class);
288 struct stk_camera *dev = vdev_to_camera(vdev);
290 return sprintf(buf, "%d\n", dev->vsettings.hflip);
293 static ssize_t store_hflip(struct device *class,
294 struct device_attribute *attr, const char *buf, size_t count)
296 struct video_device *vdev = to_video_device(class);
297 struct stk_camera *dev = vdev_to_camera(vdev);
299 if (strncmp(buf, "1", 1) == 0)
300 dev->vsettings.hflip = 1;
301 else if (strncmp(buf, "0", 1) == 0)
302 dev->vsettings.hflip = 0;
309 static ssize_t show_vflip(struct device *class,
310 struct device_attribute *attr, char *buf)
312 struct video_device *vdev = to_video_device(class);
313 struct stk_camera *dev = vdev_to_camera(vdev);
315 return sprintf(buf, "%d\n", dev->vsettings.vflip);
318 static ssize_t store_vflip(struct device *class,
319 struct device_attribute *attr, const char *buf, size_t count)
321 struct video_device *vdev = to_video_device(class);
322 struct stk_camera *dev = vdev_to_camera(vdev);
324 if (strncmp(buf, "1", 1) == 0)
325 dev->vsettings.vflip = 1;
326 else if (strncmp(buf, "0", 1) == 0)
327 dev->vsettings.vflip = 0;
334 static DEVICE_ATTR(brightness, S_IRUGO | S_IWUGO,
335 show_brightness, store_brightness);
336 static DEVICE_ATTR(hflip, S_IRUGO | S_IWUGO, show_hflip, store_hflip);
337 static DEVICE_ATTR(vflip, S_IRUGO | S_IWUGO, show_vflip, store_vflip);
339 static int stk_create_sysfs_files(struct video_device *vdev)
343 ret = video_device_create_file(vdev, &dev_attr_brightness);
344 ret += video_device_create_file(vdev, &dev_attr_hflip);
345 ret += video_device_create_file(vdev, &dev_attr_vflip);
349 static void stk_remove_sysfs_files(struct video_device *vdev)
351 video_device_remove_file(vdev, &dev_attr_brightness);
352 video_device_remove_file(vdev, &dev_attr_hflip);
353 video_device_remove_file(vdev, &dev_attr_vflip);
357 #define stk_create_sysfs_files(a)
358 #define stk_remove_sysfs_files(a)
361 /* *********************************************** */
363 * This function is called as an URB transfert is complete (Isochronous pipe).
364 * So, the traitement is done in interrupt time, so it has be fast, not crash,
365 * and not stall. Neat.
367 static void stk_isoc_handler(struct urb *urb)
374 unsigned char *fill = NULL;
375 unsigned char *iso_buf = NULL;
377 struct stk_camera *dev;
378 struct stk_sio_buffer *fb;
380 dev = (struct stk_camera *) urb->context;
383 STK_ERROR("isoc_handler called with NULL device !\n");
387 if (urb->status == -ENOENT || urb->status == -ECONNRESET
388 || urb->status == -ESHUTDOWN) {
389 atomic_dec(&dev->urbs_used);
393 spin_lock_irqsave(&dev->spinlock, flags);
395 if (urb->status != -EINPROGRESS && urb->status != 0) {
396 STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
400 if (list_empty(&dev->sio_avail)) {
401 /*FIXME Stop streaming after a while */
402 (void) (printk_ratelimit() &&
403 STK_ERROR("isoc_handler without available buffer!\n"));
406 fb = list_first_entry(&dev->sio_avail,
407 struct stk_sio_buffer, list);
408 fill = fb->buffer + fb->v4lbuf.bytesused;
410 for (i = 0; i < urb->number_of_packets; i++) {
411 if (urb->iso_frame_desc[i].status != 0) {
412 if (urb->iso_frame_desc[i].status != -EXDEV)
413 STK_ERROR("Frame %d has error %d\n", i,
414 urb->iso_frame_desc[i].status);
417 framelen = urb->iso_frame_desc[i].actual_length;
418 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
421 continue; /* no data */
424 * we found something informational from there
425 * the isoc frames have to type of headers
426 * type1: 00 xx 00 00 or 20 xx 00 00
427 * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
428 * xx is a sequencer which has never been seen over 0x3f
429 * imho data written down looks like bayer, i see similarities
430 * after every 640 bytes
432 if (*iso_buf & 0x80) {
435 /* This marks a new frame */
436 if (fb->v4lbuf.bytesused != 0
437 && fb->v4lbuf.bytesused != dev->frame_size) {
438 (void) (printk_ratelimit() &&
439 STK_ERROR("frame %d, "
440 "bytesused=%d, skipping\n",
441 i, fb->v4lbuf.bytesused));
442 fb->v4lbuf.bytesused = 0;
444 } else if (fb->v4lbuf.bytesused == dev->frame_size) {
445 list_move_tail(dev->sio_avail.next,
447 wake_up(&dev->wait_frame);
448 if (list_empty(&dev->sio_avail)) {
449 (void) (printk_ratelimit() &&
450 STK_ERROR("No buffer available\n"));
453 fb = list_first_entry(&dev->sio_avail,
454 struct stk_sio_buffer, list);
455 fb->v4lbuf.bytesused = 0;
463 /* Our buffer is full !!! */
464 if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
465 (void) (printk_ratelimit() &&
466 STK_ERROR("Frame buffer overflow, lost sync\n"));
467 /*FIXME Do something here? */
470 spin_unlock_irqrestore(&dev->spinlock, flags);
471 memcpy(fill, iso_buf, framelen);
472 spin_lock_irqsave(&dev->spinlock, flags);
475 /* New size of our buffer */
476 fb->v4lbuf.bytesused += framelen;
480 spin_unlock_irqrestore(&dev->spinlock, flags);
481 urb->dev = dev->udev;
482 ret = usb_submit_urb(urb, GFP_ATOMIC);
484 STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
489 /* -------------------------------------------- */
491 static int stk_prepare_iso(struct stk_camera *dev)
496 struct usb_device *udev;
503 STK_ERROR("isobufs already allocated. Bad\n");
505 dev->isobufs = kzalloc(MAX_ISO_BUFS * sizeof(*dev->isobufs),
507 if (dev->isobufs == NULL) {
508 STK_ERROR("Unable to allocate iso buffers\n");
511 for (i = 0; i < MAX_ISO_BUFS; i++) {
512 if (dev->isobufs[i].data == NULL) {
513 kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
515 STK_ERROR("Failed to allocate iso buffer %d\n",
519 dev->isobufs[i].data = kbuf;
521 STK_ERROR("isobuf data already allocated\n");
522 if (dev->isobufs[i].urb == NULL) {
523 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
525 STK_ERROR("Failed to allocate URB %d\n", i);
528 dev->isobufs[i].urb = urb;
530 STK_ERROR("Killing URB\n");
531 usb_kill_urb(dev->isobufs[i].urb);
532 urb = dev->isobufs[i].urb;
536 urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
537 urb->transfer_flags = URB_ISO_ASAP;
538 urb->transfer_buffer = dev->isobufs[i].data;
539 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
540 urb->complete = stk_isoc_handler;
542 urb->start_frame = 0;
543 urb->number_of_packets = ISO_FRAMES_PER_DESC;
545 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
546 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
547 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
554 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
555 kfree(dev->isobufs[i].data);
556 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
557 usb_free_urb(dev->isobufs[i].urb);
563 static void stk_clean_iso(struct stk_camera *dev)
567 if (dev == NULL || dev->isobufs == NULL)
570 for (i = 0; i < MAX_ISO_BUFS; i++) {
573 urb = dev->isobufs[i].urb;
575 if (atomic_read(&dev->urbs_used))
579 kfree(dev->isobufs[i].data);
583 unset_memallocd(dev);
586 static int stk_setup_siobuf(struct stk_camera *dev, int index)
588 struct stk_sio_buffer *buf = dev->sio_bufs + index;
589 INIT_LIST_HEAD(&buf->list);
590 buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
591 buf->buffer = vmalloc_user(buf->v4lbuf.length);
592 if (buf->buffer == NULL)
596 buf->v4lbuf.index = index;
597 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
598 buf->v4lbuf.field = V4L2_FIELD_NONE;
599 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
600 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
604 static int stk_free_sio_buffers(struct stk_camera *dev)
609 if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
612 * If any buffers are mapped, we cannot free them at all.
614 for (i = 0; i < dev->n_sbufs; i++) {
615 if (dev->sio_bufs[i].mapcount > 0)
621 spin_lock_irqsave(&dev->spinlock, flags);
622 INIT_LIST_HEAD(&dev->sio_avail);
623 INIT_LIST_HEAD(&dev->sio_full);
624 nbufs = dev->n_sbufs;
626 spin_unlock_irqrestore(&dev->spinlock, flags);
627 for (i = 0; i < nbufs; i++) {
628 if (dev->sio_bufs[i].buffer != NULL)
629 vfree(dev->sio_bufs[i].buffer);
631 kfree(dev->sio_bufs);
632 dev->sio_bufs = NULL;
636 static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
639 if (dev->sio_bufs != NULL)
640 STK_ERROR("sio_bufs already allocated\n");
642 dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
644 if (dev->sio_bufs == NULL)
646 for (i = 0; i < n_sbufs; i++) {
647 if (stk_setup_siobuf(dev, i))
648 return (dev->n_sbufs > 1)? 0 : -ENOMEM;
655 static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
658 err = stk_prepare_iso(dev);
663 err = stk_prepare_sio_buffers(dev, n_sbufs);
665 stk_free_sio_buffers(dev);
671 static void stk_free_buffers(struct stk_camera *dev)
674 stk_free_sio_buffers(dev);
676 /* -------------------------------------------- */
678 /* v4l file operations */
680 static int v4l_stk_open(struct inode *inode, struct file *fp)
682 struct stk_camera *dev;
683 struct video_device *vdev;
685 vdev = video_devdata(fp);
686 dev = vdev_to_camera(vdev);
688 if (dev == NULL || !is_present(dev))
690 fp->private_data = vdev;
691 kref_get(&dev->kref);
692 usb_autopm_get_interface(dev->interface);
697 static int v4l_stk_release(struct inode *inode, struct file *fp)
699 struct stk_camera *dev;
700 struct video_device *vdev;
702 vdev = video_devdata(fp);
704 STK_ERROR("v4l_release called w/o video devdata\n");
707 dev = vdev_to_camera(vdev);
709 STK_ERROR("v4l_release called on removed device\n");
713 if (dev->owner != fp) {
714 usb_autopm_put_interface(dev->interface);
715 kref_put(&dev->kref, stk_camera_cleanup);
719 stk_stop_stream(dev);
721 stk_free_buffers(dev);
725 usb_autopm_put_interface(dev->interface);
726 kref_put(&dev->kref, stk_camera_cleanup);
731 static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
732 size_t count, loff_t *f_pos)
737 struct stk_camera *dev;
738 struct video_device *vdev;
739 struct stk_sio_buffer *sbuf;
741 vdev = video_devdata(fp);
744 dev = vdev_to_camera(vdev);
749 if (!is_present(dev))
751 if (dev->owner && dev->owner != fp)
754 if (!is_streaming(dev)) {
755 if (stk_initialise(dev)
756 || stk_allocate_buffers(dev, 3)
757 || stk_start_stream(dev))
759 spin_lock_irqsave(&dev->spinlock, flags);
760 for (i = 0; i < dev->n_sbufs; i++) {
761 list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
762 dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
764 spin_unlock_irqrestore(&dev->spinlock, flags);
767 if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
769 ret = wait_event_interruptible(dev->wait_frame,
770 !list_empty(&dev->sio_full) || !is_present(dev));
773 if (!is_present(dev))
776 if (count + *f_pos > dev->frame_size)
777 count = dev->frame_size - *f_pos;
778 spin_lock_irqsave(&dev->spinlock, flags);
779 if (list_empty(&dev->sio_full)) {
780 spin_unlock_irqrestore(&dev->spinlock, flags);
781 STK_ERROR("BUG: No siobufs ready\n");
784 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
785 spin_unlock_irqrestore(&dev->spinlock, flags);
787 if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
792 if (*f_pos >= dev->frame_size) {
794 spin_lock_irqsave(&dev->spinlock, flags);
795 list_move_tail(&sbuf->list, &dev->sio_avail);
796 spin_unlock_irqrestore(&dev->spinlock, flags);
801 static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
803 struct stk_camera *dev;
804 struct video_device *vdev;
806 vdev = video_devdata(fp);
811 dev = vdev_to_camera(vdev);
815 poll_wait(fp, &dev->wait_frame, wait);
817 if (!is_present(dev))
820 if (!list_empty(&dev->sio_full))
821 return (POLLIN | POLLRDNORM);
827 static void stk_v4l_vm_open(struct vm_area_struct *vma)
829 struct stk_sio_buffer *sbuf = vma->vm_private_data;
832 static void stk_v4l_vm_close(struct vm_area_struct *vma)
834 struct stk_sio_buffer *sbuf = vma->vm_private_data;
836 if (sbuf->mapcount == 0)
837 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
839 static struct vm_operations_struct stk_v4l_vm_ops = {
840 .open = stk_v4l_vm_open,
841 .close = stk_v4l_vm_close
844 static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
848 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
849 struct stk_camera *dev;
850 struct video_device *vdev;
851 struct stk_sio_buffer *sbuf = NULL;
853 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
856 vdev = video_devdata(fp);
857 dev = vdev_to_camera(vdev);
859 for (i = 0; i < dev->n_sbufs; i++) {
860 if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
861 sbuf = dev->sio_bufs + i;
867 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
870 vma->vm_flags |= VM_DONTEXPAND;
871 vma->vm_private_data = sbuf;
872 vma->vm_ops = &stk_v4l_vm_ops;
873 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
874 stk_v4l_vm_open(vma);
878 /* v4l ioctl handlers */
880 static int stk_vidioc_querycap(struct file *filp,
881 void *priv, struct v4l2_capability *cap)
883 strcpy(cap->driver, "stk");
884 strcpy(cap->card, "stk");
885 cap->version = DRIVER_VERSION_NUM;
887 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
888 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
892 static int stk_vidioc_enum_input(struct file *filp,
893 void *priv, struct v4l2_input *input)
895 if (input->index != 0)
898 strcpy(input->name, "Syntek USB Camera");
899 input->type = V4L2_INPUT_TYPE_CAMERA;
904 static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
910 static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
919 static int stk_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
924 /* List of all V4Lv2 controls supported by the driver */
925 static struct v4l2_queryctrl stk_controls[] = {
927 .id = V4L2_CID_BRIGHTNESS,
928 .type = V4L2_CTRL_TYPE_INTEGER,
929 .name = "Brightness",
933 .default_value = 0x6000,
935 /*TODO: get more controls to work */
938 static int stk_vidioc_queryctrl(struct file *filp,
939 void *priv, struct v4l2_queryctrl *c)
943 nbr = ARRAY_SIZE(stk_controls);
945 for (i = 0; i < nbr; i++) {
946 if (stk_controls[i].id == c->id) {
947 memcpy(c, &stk_controls[i],
948 sizeof(struct v4l2_queryctrl));
955 static int stk_vidioc_g_ctrl(struct file *filp,
956 void *priv, struct v4l2_control *c)
958 struct stk_camera *dev = priv;
960 case V4L2_CID_BRIGHTNESS:
961 c->value = dev->vsettings.brightness;
969 static int stk_vidioc_s_ctrl(struct file *filp,
970 void *priv, struct v4l2_control *c)
972 struct stk_camera *dev = priv;
974 case V4L2_CID_BRIGHTNESS:
975 dev->vsettings.brightness = c->value;
976 return stk_sensor_set_brightness(dev, c->value >> 8);
984 static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
985 void *priv, struct v4l2_fmtdesc *fmtd)
989 switch (fmtd->index) {
991 fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
992 strcpy(fmtd->description, "r5g6b5");
995 fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
996 strcpy(fmtd->description, "r5g6b5BE");
999 fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
1000 strcpy(fmtd->description, "yuv4:2:2");
1003 fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
1004 strcpy(fmtd->description, "Raw bayer");
1007 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1008 strcpy(fmtd->description, "yuv4:2:2");
1016 static struct stk_size {
1021 { .w = 1280, .h = 1024, .m = MODE_SXGA, },
1022 { .w = 640, .h = 480, .m = MODE_VGA, },
1023 { .w = 352, .h = 288, .m = MODE_CIF, },
1024 { .w = 320, .h = 240, .m = MODE_QVGA, },
1025 { .w = 176, .h = 144, .m = MODE_QCIF, },
1028 static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
1029 void *priv, struct v4l2_format *f)
1031 struct v4l2_pix_format *pix_format = &f->fmt.pix;
1032 struct stk_camera *dev = priv;
1035 for (i = 0; i < ARRAY_SIZE(stk_sizes)
1036 && stk_sizes[i].m != dev->vsettings.mode;
1038 if (i == ARRAY_SIZE(stk_sizes)) {
1039 STK_ERROR("ERROR: mode invalid\n");
1042 pix_format->width = stk_sizes[i].w;
1043 pix_format->height = stk_sizes[i].h;
1044 pix_format->field = V4L2_FIELD_NONE;
1045 pix_format->colorspace = V4L2_COLORSPACE_SRGB;
1046 pix_format->priv = 0;
1047 pix_format->pixelformat = dev->vsettings.palette;
1048 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
1049 pix_format->bytesperline = pix_format->width;
1051 pix_format->bytesperline = 2 * pix_format->width;
1052 pix_format->sizeimage = pix_format->bytesperline
1053 * pix_format->height;
1057 static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
1058 void *priv, struct v4l2_format *fmtd)
1061 switch (fmtd->fmt.pix.pixelformat) {
1062 case V4L2_PIX_FMT_RGB565:
1063 case V4L2_PIX_FMT_RGB565X:
1064 case V4L2_PIX_FMT_UYVY:
1065 case V4L2_PIX_FMT_YUYV:
1066 case V4L2_PIX_FMT_SBGGR8:
1071 for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
1072 if (fmtd->fmt.pix.width > stk_sizes[i].w)
1075 if (i == ARRAY_SIZE(stk_sizes)
1076 || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
1077 < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
1078 fmtd->fmt.pix.height = stk_sizes[i-1].h;
1079 fmtd->fmt.pix.width = stk_sizes[i-1].w;
1080 fmtd->fmt.pix.priv = i - 1;
1082 fmtd->fmt.pix.height = stk_sizes[i].h;
1083 fmtd->fmt.pix.width = stk_sizes[i].w;
1084 fmtd->fmt.pix.priv = i;
1087 fmtd->fmt.pix.field = V4L2_FIELD_NONE;
1088 fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
1089 if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
1090 fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
1092 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
1093 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
1094 * fmtd->fmt.pix.height;
1098 static int stk_setup_format(struct stk_camera *dev)
1102 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
1106 while (stk_sizes[i].m != dev->vsettings.mode
1107 && i < ARRAY_SIZE(stk_sizes))
1109 if (i == ARRAY_SIZE(stk_sizes)) {
1110 STK_ERROR("Something is broken in %s\n", __func__);
1113 /* This registers controls some timings, not sure of what. */
1114 stk_camera_write_reg(dev, 0x001b, 0x0e);
1115 if (dev->vsettings.mode == MODE_SXGA)
1116 stk_camera_write_reg(dev, 0x001c, 0x0e);
1118 stk_camera_write_reg(dev, 0x001c, 0x46);
1120 * Registers 0x0115 0x0114 are the size of each line (bytes),
1121 * regs 0x0117 0x0116 are the heigth of the image.
1123 stk_camera_write_reg(dev, 0x0115,
1124 ((stk_sizes[i].w * depth) >> 8) & 0xff);
1125 stk_camera_write_reg(dev, 0x0114,
1126 (stk_sizes[i].w * depth) & 0xff);
1127 stk_camera_write_reg(dev, 0x0117,
1128 (stk_sizes[i].h >> 8) & 0xff);
1129 stk_camera_write_reg(dev, 0x0116,
1130 stk_sizes[i].h & 0xff);
1131 return stk_sensor_configure(dev);
1134 static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
1135 void *priv, struct v4l2_format *fmtd)
1138 struct stk_camera *dev = priv;
1142 if (!is_present(dev))
1144 if (is_streaming(dev))
1146 if (dev->owner && dev->owner != filp)
1148 ret = stk_vidioc_try_fmt_vid_cap(filp, priv, fmtd);
1153 dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
1154 stk_free_buffers(dev);
1155 dev->frame_size = fmtd->fmt.pix.sizeimage;
1156 dev->vsettings.mode = stk_sizes[fmtd->fmt.pix.priv].m;
1158 stk_initialise(dev);
1159 return stk_setup_format(dev);
1162 static int stk_vidioc_reqbufs(struct file *filp,
1163 void *priv, struct v4l2_requestbuffers *rb)
1165 struct stk_camera *dev = priv;
1169 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1171 if (rb->memory != V4L2_MEMORY_MMAP)
1173 if (is_streaming(dev)
1174 || (dev->owner && dev->owner != filp))
1178 /*FIXME If they ask for zero, we must stop streaming and free */
1181 /* Arbitrary limit */
1182 else if (rb->count > 5)
1185 stk_allocate_buffers(dev, rb->count);
1186 rb->count = dev->n_sbufs;
1190 static int stk_vidioc_querybuf(struct file *filp,
1191 void *priv, struct v4l2_buffer *buf)
1194 struct stk_camera *dev = priv;
1195 struct stk_sio_buffer *sbuf;
1197 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1202 if (index < 0 || index >= dev->n_sbufs)
1204 sbuf = dev->sio_bufs + buf->index;
1205 *buf = sbuf->v4lbuf;
1209 static int stk_vidioc_qbuf(struct file *filp,
1210 void *priv, struct v4l2_buffer *buf)
1212 struct stk_camera *dev = priv;
1213 struct stk_sio_buffer *sbuf;
1214 unsigned long flags;
1215 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1218 if (buf->memory != V4L2_MEMORY_MMAP)
1221 if (buf->index < 0 || buf->index >= dev->n_sbufs)
1223 sbuf = dev->sio_bufs + buf->index;
1224 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
1226 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1227 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1228 spin_lock_irqsave(&dev->spinlock, flags);
1229 list_add_tail(&sbuf->list, &dev->sio_avail);
1230 *buf = sbuf->v4lbuf;
1231 spin_unlock_irqrestore(&dev->spinlock, flags);
1235 static int stk_vidioc_dqbuf(struct file *filp,
1236 void *priv, struct v4l2_buffer *buf)
1238 struct stk_camera *dev = priv;
1239 struct stk_sio_buffer *sbuf;
1240 unsigned long flags;
1243 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1244 || !is_streaming(dev))
1247 if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
1248 return -EWOULDBLOCK;
1249 ret = wait_event_interruptible(dev->wait_frame,
1250 !list_empty(&dev->sio_full) || !is_present(dev));
1253 if (!is_present(dev))
1256 spin_lock_irqsave(&dev->spinlock, flags);
1257 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
1258 list_del_init(&sbuf->list);
1259 spin_unlock_irqrestore(&dev->spinlock, flags);
1260 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1261 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1262 sbuf->v4lbuf.sequence = ++dev->sequence;
1263 do_gettimeofday(&sbuf->v4lbuf.timestamp);
1265 *buf = sbuf->v4lbuf;
1269 static int stk_vidioc_streamon(struct file *filp,
1270 void *priv, enum v4l2_buf_type type)
1272 struct stk_camera *dev = priv;
1273 if (is_streaming(dev))
1275 if (dev->sio_bufs == NULL)
1278 return stk_start_stream(dev);
1281 static int stk_vidioc_streamoff(struct file *filp,
1282 void *priv, enum v4l2_buf_type type)
1284 struct stk_camera *dev = priv;
1285 unsigned long flags;
1287 stk_stop_stream(dev);
1288 spin_lock_irqsave(&dev->spinlock, flags);
1289 INIT_LIST_HEAD(&dev->sio_avail);
1290 INIT_LIST_HEAD(&dev->sio_full);
1291 for (i = 0; i < dev->n_sbufs; i++) {
1292 INIT_LIST_HEAD(&dev->sio_bufs[i].list);
1293 dev->sio_bufs[i].v4lbuf.flags = 0;
1295 spin_unlock_irqrestore(&dev->spinlock, flags);
1300 static int stk_vidioc_g_parm(struct file *filp,
1301 void *priv, struct v4l2_streamparm *sp)
1303 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1306 sp->parm.capture.capability = 0;
1307 sp->parm.capture.capturemode = 0;
1308 /*FIXME This is not correct */
1309 sp->parm.capture.timeperframe.numerator = 1;
1310 sp->parm.capture.timeperframe.denominator = 30;
1311 sp->parm.capture.readbuffers = 2;
1312 sp->parm.capture.extendedmode = 0;
1316 static struct file_operations v4l_stk_fops = {
1317 .owner = THIS_MODULE,
1318 .open = v4l_stk_open,
1319 .release = v4l_stk_release,
1320 .read = v4l_stk_read,
1321 .poll = v4l_stk_poll,
1322 .mmap = v4l_stk_mmap,
1323 .ioctl = video_ioctl2,
1324 #ifdef CONFIG_COMPAT
1325 .compat_ioctl = v4l_compat_ioctl32,
1330 static void stk_v4l_dev_release(struct video_device *vd)
1334 static struct video_device stk_v4l_data = {
1335 .name = "stkwebcam",
1336 .type = VFL_TYPE_GRABBER,
1337 .type2 = VID_TYPE_CAPTURE,
1339 .tvnorms = V4L2_STD_UNKNOWN,
1340 .current_norm = V4L2_STD_UNKNOWN,
1341 .fops = &v4l_stk_fops,
1342 .release = stk_v4l_dev_release,
1344 .vidioc_querycap = stk_vidioc_querycap,
1345 .vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
1346 .vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
1347 .vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
1348 .vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
1349 .vidioc_enum_input = stk_vidioc_enum_input,
1350 .vidioc_s_input = stk_vidioc_s_input,
1351 .vidioc_g_input = stk_vidioc_g_input,
1352 .vidioc_s_std = stk_vidioc_s_std,
1353 .vidioc_reqbufs = stk_vidioc_reqbufs,
1354 .vidioc_querybuf = stk_vidioc_querybuf,
1355 .vidioc_qbuf = stk_vidioc_qbuf,
1356 .vidioc_dqbuf = stk_vidioc_dqbuf,
1357 .vidioc_streamon = stk_vidioc_streamon,
1358 .vidioc_streamoff = stk_vidioc_streamoff,
1359 .vidioc_queryctrl = stk_vidioc_queryctrl,
1360 .vidioc_g_ctrl = stk_vidioc_g_ctrl,
1361 .vidioc_s_ctrl = stk_vidioc_s_ctrl,
1362 .vidioc_g_parm = stk_vidioc_g_parm,
1366 static int stk_register_video_device(struct stk_camera *dev)
1370 dev->vdev = stk_v4l_data;
1371 dev->vdev.debug = debug;
1372 dev->vdev.dev = &dev->interface->dev;
1373 dev->vdev.priv = dev;
1374 err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1376 STK_ERROR("v4l registration failed\n");
1378 STK_INFO("Syntek USB2.0 Camera is now controlling video device"
1379 " /dev/video%d\n", dev->vdev.minor);
1386 static int stk_camera_probe(struct usb_interface *interface,
1387 const struct usb_device_id *id)
1392 struct stk_camera *dev = NULL;
1393 struct usb_device *udev = interface_to_usbdev(interface);
1394 struct usb_host_interface *iface_desc;
1395 struct usb_endpoint_descriptor *endpoint;
1397 dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
1399 STK_ERROR("Out of memory !\n");
1403 kref_init(&dev->kref);
1404 spin_lock_init(&dev->spinlock);
1405 init_waitqueue_head(&dev->wait_frame);
1408 dev->interface = interface;
1409 usb_get_intf(interface);
1411 dev->vsettings.vflip = vflip;
1412 dev->vsettings.hflip = hflip;
1416 /* Set up the endpoint information
1417 * use only the first isoc-in endpoint
1418 * for the current alternate setting */
1419 iface_desc = interface->cur_altsetting;
1421 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1422 endpoint = &iface_desc->endpoint[i].desc;
1425 && ((endpoint->bEndpointAddress
1426 & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
1427 && ((endpoint->bmAttributes
1428 & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_ISOC)) {
1429 /* we found an isoc in endpoint */
1430 dev->isoc_ep = (endpoint->bEndpointAddress & 0xF);
1434 if (!dev->isoc_ep) {
1435 STK_ERROR("Could not find isoc-in endpoint");
1436 kref_put(&dev->kref, stk_camera_cleanup);
1439 dev->vsettings.brightness = 0x7fff;
1440 dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
1441 dev->vsettings.mode = MODE_VGA;
1442 dev->frame_size = 640 * 480 * 2;
1444 INIT_LIST_HEAD(&dev->sio_avail);
1445 INIT_LIST_HEAD(&dev->sio_full);
1447 usb_set_intfdata(interface, dev);
1449 err = stk_register_video_device(dev);
1451 kref_put(&dev->kref, stk_camera_cleanup);
1455 stk_create_sysfs_files(&dev->vdev);
1456 usb_autopm_enable(dev->interface);
1461 static void stk_camera_disconnect(struct usb_interface *interface)
1463 struct stk_camera *dev = usb_get_intfdata(interface);
1465 usb_set_intfdata(interface, NULL);
1468 wake_up_interruptible(&dev->wait_frame);
1469 stk_remove_sysfs_files(&dev->vdev);
1471 kref_put(&dev->kref, stk_camera_cleanup);
1475 static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
1477 struct stk_camera *dev = usb_get_intfdata(intf);
1478 if (is_streaming(dev)) {
1479 stk_stop_stream(dev);
1480 /* yes, this is ugly */
1486 static int stk_camera_resume(struct usb_interface *intf)
1488 struct stk_camera *dev = usb_get_intfdata(intf);
1489 if (!is_initialised(dev))
1491 unset_initialised(dev);
1492 stk_initialise(dev);
1493 stk_setup_format(dev);
1494 if (is_streaming(dev))
1495 stk_start_stream(dev);
1500 static struct usb_driver stk_camera_driver = {
1501 .name = "stkwebcam",
1502 .probe = stk_camera_probe,
1503 .disconnect = stk_camera_disconnect,
1504 .id_table = stkwebcam_table,
1506 .suspend = stk_camera_suspend,
1507 .resume = stk_camera_resume,
1512 static int __init stk_camera_init(void)
1516 result = usb_register(&stk_camera_driver);
1518 STK_ERROR("usb_register failed ! Error number %d\n", result);
1524 static void __exit stk_camera_exit(void)
1526 usb_deregister(&stk_camera_driver);
1529 module_init(stk_camera_init);
1530 module_exit(stk_camera_exit);