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>
31 #include <linux/usb.h>
33 #include <linux/vmalloc.h>
34 #include <linux/videodev2.h>
35 #include <media/v4l2-common.h>
36 #include <media/v4l2-ioctl.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);
70 int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
72 struct usb_device *udev = dev->udev;
75 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
77 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
89 int stk_camera_read_reg(struct stk_camera *dev, u16 index, int *value)
91 struct usb_device *udev = dev->udev;
94 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
96 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
108 static int stk_start_stream(struct stk_camera *dev)
112 int value_116, value_117;
114 if (!is_present(dev))
116 if (!is_memallocd(dev) || !is_initialised(dev)) {
117 STK_ERROR("FIXME: Buffers are not allocated\n");
120 ret = usb_set_interface(dev->udev, 0, 5);
123 STK_ERROR("usb_set_interface failed !\n");
124 if (stk_sensor_wakeup(dev))
125 STK_ERROR("error awaking the sensor\n");
127 stk_camera_read_reg(dev, 0x0116, &value_116);
128 stk_camera_read_reg(dev, 0x0117, &value_117);
130 stk_camera_write_reg(dev, 0x0116, 0x0000);
131 stk_camera_write_reg(dev, 0x0117, 0x0000);
133 stk_camera_read_reg(dev, 0x0100, &value);
134 stk_camera_write_reg(dev, 0x0100, value | 0x80);
136 stk_camera_write_reg(dev, 0x0116, value_116);
137 stk_camera_write_reg(dev, 0x0117, value_117);
138 for (i = 0; i < MAX_ISO_BUFS; i++) {
139 if (dev->isobufs[i].urb) {
140 ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL);
141 atomic_inc(&dev->urbs_used);
150 static int stk_stop_stream(struct stk_camera *dev)
154 if (is_present(dev)) {
155 stk_camera_read_reg(dev, 0x0100, &value);
156 stk_camera_write_reg(dev, 0x0100, value & ~0x80);
157 if (dev->isobufs != NULL) {
158 for (i = 0; i < MAX_ISO_BUFS; i++) {
159 if (dev->isobufs[i].urb)
160 usb_kill_urb(dev->isobufs[i].urb);
163 unset_streaming(dev);
165 if (usb_set_interface(dev->udev, 0, 0))
166 STK_ERROR("usb_set_interface failed !\n");
167 if (stk_sensor_sleep(dev))
168 STK_ERROR("error suspending the sensor\n");
174 * This seems to be the shortest init sequence we
175 * must do in order to find the sensor
176 * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
177 * is also reset. Maybe powers down it?
178 * Rest of values don't make a difference
181 static struct regval stk1125_initvals[] = {
182 /*TODO: What means this sequence? */
211 static int stk_initialise(struct stk_camera *dev)
215 if (!is_present(dev))
217 if (is_initialised(dev))
219 rv = stk1125_initvals;
220 while (rv->reg != 0xffff) {
221 ret = stk_camera_write_reg(dev, rv->reg, rv->val);
226 if (stk_sensor_init(dev) == 0) {
227 set_initialised(dev);
233 /* *********************************************** */
235 * This function is called as an URB transfert is complete (Isochronous pipe).
236 * So, the traitement is done in interrupt time, so it has be fast, not crash,
237 * and not stall. Neat.
239 static void stk_isoc_handler(struct urb *urb)
246 unsigned char *fill = NULL;
247 unsigned char *iso_buf = NULL;
249 struct stk_camera *dev;
250 struct stk_sio_buffer *fb;
252 dev = (struct stk_camera *) urb->context;
255 STK_ERROR("isoc_handler called with NULL device !\n");
259 if (urb->status == -ENOENT || urb->status == -ECONNRESET
260 || urb->status == -ESHUTDOWN) {
261 atomic_dec(&dev->urbs_used);
265 spin_lock_irqsave(&dev->spinlock, flags);
267 if (urb->status != -EINPROGRESS && urb->status != 0) {
268 STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
272 if (list_empty(&dev->sio_avail)) {
273 /*FIXME Stop streaming after a while */
274 (void) (printk_ratelimit() &&
275 STK_ERROR("isoc_handler without available buffer!\n"));
278 fb = list_first_entry(&dev->sio_avail,
279 struct stk_sio_buffer, list);
280 fill = fb->buffer + fb->v4lbuf.bytesused;
282 for (i = 0; i < urb->number_of_packets; i++) {
283 if (urb->iso_frame_desc[i].status != 0) {
284 if (urb->iso_frame_desc[i].status != -EXDEV)
285 STK_ERROR("Frame %d has error %d\n", i,
286 urb->iso_frame_desc[i].status);
289 framelen = urb->iso_frame_desc[i].actual_length;
290 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
293 continue; /* no data */
296 * we found something informational from there
297 * the isoc frames have to type of headers
298 * type1: 00 xx 00 00 or 20 xx 00 00
299 * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
300 * xx is a sequencer which has never been seen over 0x3f
301 * imho data written down looks like bayer, i see similarities
302 * after every 640 bytes
304 if (*iso_buf & 0x80) {
307 /* This marks a new frame */
308 if (fb->v4lbuf.bytesused != 0
309 && fb->v4lbuf.bytesused != dev->frame_size) {
310 (void) (printk_ratelimit() &&
311 STK_ERROR("frame %d, "
312 "bytesused=%d, skipping\n",
313 i, fb->v4lbuf.bytesused));
314 fb->v4lbuf.bytesused = 0;
316 } else if (fb->v4lbuf.bytesused == dev->frame_size) {
317 if (list_is_singular(&dev->sio_avail)) {
318 /* Always reuse the last buffer */
319 fb->v4lbuf.bytesused = 0;
322 list_move_tail(dev->sio_avail.next,
324 wake_up(&dev->wait_frame);
325 fb = list_first_entry(&dev->sio_avail,
326 struct stk_sio_buffer, list);
327 fb->v4lbuf.bytesused = 0;
336 /* Our buffer is full !!! */
337 if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
338 (void) (printk_ratelimit() &&
339 STK_ERROR("Frame buffer overflow, lost sync\n"));
340 /*FIXME Do something here? */
343 spin_unlock_irqrestore(&dev->spinlock, flags);
344 memcpy(fill, iso_buf, framelen);
345 spin_lock_irqsave(&dev->spinlock, flags);
348 /* New size of our buffer */
349 fb->v4lbuf.bytesused += framelen;
353 spin_unlock_irqrestore(&dev->spinlock, flags);
354 urb->dev = dev->udev;
355 ret = usb_submit_urb(urb, GFP_ATOMIC);
357 STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
362 /* -------------------------------------------- */
364 static int stk_prepare_iso(struct stk_camera *dev)
369 struct usb_device *udev;
376 STK_ERROR("isobufs already allocated. Bad\n");
378 dev->isobufs = kzalloc(MAX_ISO_BUFS * sizeof(*dev->isobufs),
380 if (dev->isobufs == NULL) {
381 STK_ERROR("Unable to allocate iso buffers\n");
384 for (i = 0; i < MAX_ISO_BUFS; i++) {
385 if (dev->isobufs[i].data == NULL) {
386 kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
388 STK_ERROR("Failed to allocate iso buffer %d\n",
392 dev->isobufs[i].data = kbuf;
394 STK_ERROR("isobuf data already allocated\n");
395 if (dev->isobufs[i].urb == NULL) {
396 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
398 STK_ERROR("Failed to allocate URB %d\n", i);
401 dev->isobufs[i].urb = urb;
403 STK_ERROR("Killing URB\n");
404 usb_kill_urb(dev->isobufs[i].urb);
405 urb = dev->isobufs[i].urb;
409 urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
410 urb->transfer_flags = URB_ISO_ASAP;
411 urb->transfer_buffer = dev->isobufs[i].data;
412 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
413 urb->complete = stk_isoc_handler;
415 urb->start_frame = 0;
416 urb->number_of_packets = ISO_FRAMES_PER_DESC;
418 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
419 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
420 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
427 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
428 kfree(dev->isobufs[i].data);
429 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
430 usb_free_urb(dev->isobufs[i].urb);
436 static void stk_clean_iso(struct stk_camera *dev)
440 if (dev == NULL || dev->isobufs == NULL)
443 for (i = 0; i < MAX_ISO_BUFS; i++) {
446 urb = dev->isobufs[i].urb;
448 if (atomic_read(&dev->urbs_used) && is_present(dev))
452 kfree(dev->isobufs[i].data);
456 unset_memallocd(dev);
459 static int stk_setup_siobuf(struct stk_camera *dev, int index)
461 struct stk_sio_buffer *buf = dev->sio_bufs + index;
462 INIT_LIST_HEAD(&buf->list);
463 buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
464 buf->buffer = vmalloc_user(buf->v4lbuf.length);
465 if (buf->buffer == NULL)
469 buf->v4lbuf.index = index;
470 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
471 buf->v4lbuf.field = V4L2_FIELD_NONE;
472 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
473 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
477 static int stk_free_sio_buffers(struct stk_camera *dev)
482 if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
485 * If any buffers are mapped, we cannot free them at all.
487 for (i = 0; i < dev->n_sbufs; i++) {
488 if (dev->sio_bufs[i].mapcount > 0)
494 spin_lock_irqsave(&dev->spinlock, flags);
495 INIT_LIST_HEAD(&dev->sio_avail);
496 INIT_LIST_HEAD(&dev->sio_full);
497 nbufs = dev->n_sbufs;
499 spin_unlock_irqrestore(&dev->spinlock, flags);
500 for (i = 0; i < nbufs; i++) {
501 if (dev->sio_bufs[i].buffer != NULL)
502 vfree(dev->sio_bufs[i].buffer);
504 kfree(dev->sio_bufs);
505 dev->sio_bufs = NULL;
509 static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
512 if (dev->sio_bufs != NULL)
513 STK_ERROR("sio_bufs already allocated\n");
515 dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
517 if (dev->sio_bufs == NULL)
519 for (i = 0; i < n_sbufs; i++) {
520 if (stk_setup_siobuf(dev, i))
521 return (dev->n_sbufs > 1)? 0 : -ENOMEM;
528 static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
531 err = stk_prepare_iso(dev);
536 err = stk_prepare_sio_buffers(dev, n_sbufs);
538 stk_free_sio_buffers(dev);
544 static void stk_free_buffers(struct stk_camera *dev)
547 stk_free_sio_buffers(dev);
549 /* -------------------------------------------- */
551 /* v4l file operations */
553 static int v4l_stk_open(struct file *fp)
555 struct stk_camera *dev;
556 struct video_device *vdev;
558 vdev = video_devdata(fp);
559 dev = vdev_to_camera(vdev);
561 if (dev == NULL || !is_present(dev)) {
564 fp->private_data = dev;
565 usb_autopm_get_interface(dev->interface);
570 static int v4l_stk_release(struct file *fp)
572 struct stk_camera *dev = fp->private_data;
574 if (dev->owner == fp) {
575 stk_stop_stream(dev);
576 stk_free_buffers(dev);
581 usb_autopm_put_interface(dev->interface);
586 static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
587 size_t count, loff_t *f_pos)
592 struct stk_sio_buffer *sbuf;
593 struct stk_camera *dev = fp->private_data;
595 if (!is_present(dev))
597 if (dev->owner && dev->owner != fp)
600 if (!is_streaming(dev)) {
601 if (stk_initialise(dev)
602 || stk_allocate_buffers(dev, 3)
603 || stk_start_stream(dev))
605 spin_lock_irqsave(&dev->spinlock, flags);
606 for (i = 0; i < dev->n_sbufs; i++) {
607 list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
608 dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
610 spin_unlock_irqrestore(&dev->spinlock, flags);
613 if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
615 ret = wait_event_interruptible(dev->wait_frame,
616 !list_empty(&dev->sio_full) || !is_present(dev));
619 if (!is_present(dev))
622 if (count + *f_pos > dev->frame_size)
623 count = dev->frame_size - *f_pos;
624 spin_lock_irqsave(&dev->spinlock, flags);
625 if (list_empty(&dev->sio_full)) {
626 spin_unlock_irqrestore(&dev->spinlock, flags);
627 STK_ERROR("BUG: No siobufs ready\n");
630 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
631 spin_unlock_irqrestore(&dev->spinlock, flags);
633 if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
638 if (*f_pos >= dev->frame_size) {
640 spin_lock_irqsave(&dev->spinlock, flags);
641 list_move_tail(&sbuf->list, &dev->sio_avail);
642 spin_unlock_irqrestore(&dev->spinlock, flags);
647 static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
649 struct stk_camera *dev = fp->private_data;
651 poll_wait(fp, &dev->wait_frame, wait);
653 if (!is_present(dev))
656 if (!list_empty(&dev->sio_full))
657 return (POLLIN | POLLRDNORM);
663 static void stk_v4l_vm_open(struct vm_area_struct *vma)
665 struct stk_sio_buffer *sbuf = vma->vm_private_data;
668 static void stk_v4l_vm_close(struct vm_area_struct *vma)
670 struct stk_sio_buffer *sbuf = vma->vm_private_data;
672 if (sbuf->mapcount == 0)
673 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
675 static const struct vm_operations_struct stk_v4l_vm_ops = {
676 .open = stk_v4l_vm_open,
677 .close = stk_v4l_vm_close
680 static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
684 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
685 struct stk_camera *dev = fp->private_data;
686 struct stk_sio_buffer *sbuf = NULL;
688 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
691 for (i = 0; i < dev->n_sbufs; i++) {
692 if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
693 sbuf = dev->sio_bufs + i;
699 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
702 vma->vm_flags |= VM_DONTEXPAND;
703 vma->vm_private_data = sbuf;
704 vma->vm_ops = &stk_v4l_vm_ops;
705 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
706 stk_v4l_vm_open(vma);
710 /* v4l ioctl handlers */
712 static int stk_vidioc_querycap(struct file *filp,
713 void *priv, struct v4l2_capability *cap)
715 strcpy(cap->driver, "stk");
716 strcpy(cap->card, "stk");
717 cap->version = DRIVER_VERSION_NUM;
719 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
720 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
724 static int stk_vidioc_enum_input(struct file *filp,
725 void *priv, struct v4l2_input *input)
727 if (input->index != 0)
730 strcpy(input->name, "Syntek USB Camera");
731 input->type = V4L2_INPUT_TYPE_CAMERA;
736 static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
742 static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
751 static int stk_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
756 /* List of all V4Lv2 controls supported by the driver */
757 static struct v4l2_queryctrl stk_controls[] = {
759 .id = V4L2_CID_BRIGHTNESS,
760 .type = V4L2_CTRL_TYPE_INTEGER,
761 .name = "Brightness",
765 .default_value = 0x6000,
768 .id = V4L2_CID_HFLIP,
769 .type = V4L2_CTRL_TYPE_BOOLEAN,
770 .name = "Horizontal Flip",
777 .id = V4L2_CID_VFLIP,
778 .type = V4L2_CTRL_TYPE_BOOLEAN,
779 .name = "Vertical Flip",
787 static int stk_vidioc_queryctrl(struct file *filp,
788 void *priv, struct v4l2_queryctrl *c)
792 nbr = ARRAY_SIZE(stk_controls);
794 for (i = 0; i < nbr; i++) {
795 if (stk_controls[i].id == c->id) {
796 memcpy(c, &stk_controls[i],
797 sizeof(struct v4l2_queryctrl));
804 static int stk_vidioc_g_ctrl(struct file *filp,
805 void *priv, struct v4l2_control *c)
807 struct stk_camera *dev = priv;
809 case V4L2_CID_BRIGHTNESS:
810 c->value = dev->vsettings.brightness;
813 c->value = dev->vsettings.hflip;
816 c->value = dev->vsettings.vflip;
824 static int stk_vidioc_s_ctrl(struct file *filp,
825 void *priv, struct v4l2_control *c)
827 struct stk_camera *dev = priv;
829 case V4L2_CID_BRIGHTNESS:
830 dev->vsettings.brightness = c->value;
831 return stk_sensor_set_brightness(dev, c->value >> 8);
833 dev->vsettings.hflip = c->value;
836 dev->vsettings.vflip = c->value;
845 static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
846 void *priv, struct v4l2_fmtdesc *fmtd)
848 switch (fmtd->index) {
850 fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
851 strcpy(fmtd->description, "r5g6b5");
854 fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
855 strcpy(fmtd->description, "r5g6b5BE");
858 fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
859 strcpy(fmtd->description, "yuv4:2:2");
862 fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
863 strcpy(fmtd->description, "Raw bayer");
866 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
867 strcpy(fmtd->description, "yuv4:2:2");
875 static struct stk_size {
880 { .w = 1280, .h = 1024, .m = MODE_SXGA, },
881 { .w = 640, .h = 480, .m = MODE_VGA, },
882 { .w = 352, .h = 288, .m = MODE_CIF, },
883 { .w = 320, .h = 240, .m = MODE_QVGA, },
884 { .w = 176, .h = 144, .m = MODE_QCIF, },
887 static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
888 void *priv, struct v4l2_format *f)
890 struct v4l2_pix_format *pix_format = &f->fmt.pix;
891 struct stk_camera *dev = priv;
894 for (i = 0; i < ARRAY_SIZE(stk_sizes)
895 && stk_sizes[i].m != dev->vsettings.mode;
897 if (i == ARRAY_SIZE(stk_sizes)) {
898 STK_ERROR("ERROR: mode invalid\n");
901 pix_format->width = stk_sizes[i].w;
902 pix_format->height = stk_sizes[i].h;
903 pix_format->field = V4L2_FIELD_NONE;
904 pix_format->colorspace = V4L2_COLORSPACE_SRGB;
905 pix_format->pixelformat = dev->vsettings.palette;
906 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
907 pix_format->bytesperline = pix_format->width;
909 pix_format->bytesperline = 2 * pix_format->width;
910 pix_format->sizeimage = pix_format->bytesperline
911 * pix_format->height;
915 static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
916 void *priv, struct v4l2_format *fmtd)
919 switch (fmtd->fmt.pix.pixelformat) {
920 case V4L2_PIX_FMT_RGB565:
921 case V4L2_PIX_FMT_RGB565X:
922 case V4L2_PIX_FMT_UYVY:
923 case V4L2_PIX_FMT_YUYV:
924 case V4L2_PIX_FMT_SBGGR8:
929 for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
930 if (fmtd->fmt.pix.width > stk_sizes[i].w)
933 if (i == ARRAY_SIZE(stk_sizes)
934 || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
935 < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
936 fmtd->fmt.pix.height = stk_sizes[i-1].h;
937 fmtd->fmt.pix.width = stk_sizes[i-1].w;
938 fmtd->fmt.pix.priv = i - 1;
940 fmtd->fmt.pix.height = stk_sizes[i].h;
941 fmtd->fmt.pix.width = stk_sizes[i].w;
942 fmtd->fmt.pix.priv = i;
945 fmtd->fmt.pix.field = V4L2_FIELD_NONE;
946 fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
947 if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
948 fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
950 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
951 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
952 * fmtd->fmt.pix.height;
956 static int stk_setup_format(struct stk_camera *dev)
960 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
964 while (i < ARRAY_SIZE(stk_sizes) &&
965 stk_sizes[i].m != dev->vsettings.mode)
967 if (i == ARRAY_SIZE(stk_sizes)) {
968 STK_ERROR("Something is broken in %s\n", __func__);
971 /* This registers controls some timings, not sure of what. */
972 stk_camera_write_reg(dev, 0x001b, 0x0e);
973 if (dev->vsettings.mode == MODE_SXGA)
974 stk_camera_write_reg(dev, 0x001c, 0x0e);
976 stk_camera_write_reg(dev, 0x001c, 0x46);
978 * Registers 0x0115 0x0114 are the size of each line (bytes),
979 * regs 0x0117 0x0116 are the heigth of the image.
981 stk_camera_write_reg(dev, 0x0115,
982 ((stk_sizes[i].w * depth) >> 8) & 0xff);
983 stk_camera_write_reg(dev, 0x0114,
984 (stk_sizes[i].w * depth) & 0xff);
985 stk_camera_write_reg(dev, 0x0117,
986 (stk_sizes[i].h >> 8) & 0xff);
987 stk_camera_write_reg(dev, 0x0116,
988 stk_sizes[i].h & 0xff);
989 return stk_sensor_configure(dev);
992 static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
993 void *priv, struct v4l2_format *fmtd)
996 struct stk_camera *dev = priv;
1000 if (!is_present(dev))
1002 if (is_streaming(dev))
1004 if (dev->owner && dev->owner != filp)
1006 ret = stk_vidioc_try_fmt_vid_cap(filp, priv, fmtd);
1011 dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
1012 stk_free_buffers(dev);
1013 dev->frame_size = fmtd->fmt.pix.sizeimage;
1014 dev->vsettings.mode = stk_sizes[fmtd->fmt.pix.priv].m;
1016 stk_initialise(dev);
1017 return stk_setup_format(dev);
1020 static int stk_vidioc_reqbufs(struct file *filp,
1021 void *priv, struct v4l2_requestbuffers *rb)
1023 struct stk_camera *dev = priv;
1027 if (rb->memory != V4L2_MEMORY_MMAP)
1029 if (is_streaming(dev)
1030 || (dev->owner && dev->owner != filp))
1034 /*FIXME If they ask for zero, we must stop streaming and free */
1037 /* Arbitrary limit */
1038 else if (rb->count > 5)
1041 stk_allocate_buffers(dev, rb->count);
1042 rb->count = dev->n_sbufs;
1046 static int stk_vidioc_querybuf(struct file *filp,
1047 void *priv, struct v4l2_buffer *buf)
1049 struct stk_camera *dev = priv;
1050 struct stk_sio_buffer *sbuf;
1052 if (buf->index >= dev->n_sbufs)
1054 sbuf = dev->sio_bufs + buf->index;
1055 *buf = sbuf->v4lbuf;
1059 static int stk_vidioc_qbuf(struct file *filp,
1060 void *priv, struct v4l2_buffer *buf)
1062 struct stk_camera *dev = priv;
1063 struct stk_sio_buffer *sbuf;
1064 unsigned long flags;
1066 if (buf->memory != V4L2_MEMORY_MMAP)
1069 if (buf->index >= dev->n_sbufs)
1071 sbuf = dev->sio_bufs + buf->index;
1072 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
1074 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1075 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1076 spin_lock_irqsave(&dev->spinlock, flags);
1077 list_add_tail(&sbuf->list, &dev->sio_avail);
1078 *buf = sbuf->v4lbuf;
1079 spin_unlock_irqrestore(&dev->spinlock, flags);
1083 static int stk_vidioc_dqbuf(struct file *filp,
1084 void *priv, struct v4l2_buffer *buf)
1086 struct stk_camera *dev = priv;
1087 struct stk_sio_buffer *sbuf;
1088 unsigned long flags;
1091 if (!is_streaming(dev))
1094 if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
1095 return -EWOULDBLOCK;
1096 ret = wait_event_interruptible(dev->wait_frame,
1097 !list_empty(&dev->sio_full) || !is_present(dev));
1100 if (!is_present(dev))
1103 spin_lock_irqsave(&dev->spinlock, flags);
1104 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
1105 list_del_init(&sbuf->list);
1106 spin_unlock_irqrestore(&dev->spinlock, flags);
1107 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1108 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1109 sbuf->v4lbuf.sequence = ++dev->sequence;
1110 do_gettimeofday(&sbuf->v4lbuf.timestamp);
1112 *buf = sbuf->v4lbuf;
1116 static int stk_vidioc_streamon(struct file *filp,
1117 void *priv, enum v4l2_buf_type type)
1119 struct stk_camera *dev = priv;
1120 if (is_streaming(dev))
1122 if (dev->sio_bufs == NULL)
1125 return stk_start_stream(dev);
1128 static int stk_vidioc_streamoff(struct file *filp,
1129 void *priv, enum v4l2_buf_type type)
1131 struct stk_camera *dev = priv;
1132 unsigned long flags;
1134 stk_stop_stream(dev);
1135 spin_lock_irqsave(&dev->spinlock, flags);
1136 INIT_LIST_HEAD(&dev->sio_avail);
1137 INIT_LIST_HEAD(&dev->sio_full);
1138 for (i = 0; i < dev->n_sbufs; i++) {
1139 INIT_LIST_HEAD(&dev->sio_bufs[i].list);
1140 dev->sio_bufs[i].v4lbuf.flags = 0;
1142 spin_unlock_irqrestore(&dev->spinlock, flags);
1147 static int stk_vidioc_g_parm(struct file *filp,
1148 void *priv, struct v4l2_streamparm *sp)
1150 /*FIXME This is not correct */
1151 sp->parm.capture.timeperframe.numerator = 1;
1152 sp->parm.capture.timeperframe.denominator = 30;
1153 sp->parm.capture.readbuffers = 2;
1157 static int stk_vidioc_enum_framesizes(struct file *filp,
1158 void *priv, struct v4l2_frmsizeenum *frms)
1160 if (frms->index >= ARRAY_SIZE(stk_sizes))
1162 switch (frms->pixel_format) {
1163 case V4L2_PIX_FMT_RGB565:
1164 case V4L2_PIX_FMT_RGB565X:
1165 case V4L2_PIX_FMT_UYVY:
1166 case V4L2_PIX_FMT_YUYV:
1167 case V4L2_PIX_FMT_SBGGR8:
1168 frms->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1169 frms->discrete.width = stk_sizes[frms->index].w;
1170 frms->discrete.height = stk_sizes[frms->index].h;
1172 default: return -EINVAL;
1176 static struct v4l2_file_operations v4l_stk_fops = {
1177 .owner = THIS_MODULE,
1178 .open = v4l_stk_open,
1179 .release = v4l_stk_release,
1180 .read = v4l_stk_read,
1181 .poll = v4l_stk_poll,
1182 .mmap = v4l_stk_mmap,
1183 .ioctl = video_ioctl2,
1186 static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops = {
1187 .vidioc_querycap = stk_vidioc_querycap,
1188 .vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
1189 .vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
1190 .vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
1191 .vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
1192 .vidioc_enum_input = stk_vidioc_enum_input,
1193 .vidioc_s_input = stk_vidioc_s_input,
1194 .vidioc_g_input = stk_vidioc_g_input,
1195 .vidioc_s_std = stk_vidioc_s_std,
1196 .vidioc_reqbufs = stk_vidioc_reqbufs,
1197 .vidioc_querybuf = stk_vidioc_querybuf,
1198 .vidioc_qbuf = stk_vidioc_qbuf,
1199 .vidioc_dqbuf = stk_vidioc_dqbuf,
1200 .vidioc_streamon = stk_vidioc_streamon,
1201 .vidioc_streamoff = stk_vidioc_streamoff,
1202 .vidioc_queryctrl = stk_vidioc_queryctrl,
1203 .vidioc_g_ctrl = stk_vidioc_g_ctrl,
1204 .vidioc_s_ctrl = stk_vidioc_s_ctrl,
1205 .vidioc_g_parm = stk_vidioc_g_parm,
1206 .vidioc_enum_framesizes = stk_vidioc_enum_framesizes,
1209 static void stk_v4l_dev_release(struct video_device *vd)
1211 struct stk_camera *dev = vdev_to_camera(vd);
1213 if (dev->sio_bufs != NULL || dev->isobufs != NULL)
1214 STK_ERROR("We are leaking memory\n");
1215 usb_put_intf(dev->interface);
1219 static struct video_device stk_v4l_data = {
1220 .name = "stkwebcam",
1221 .tvnorms = V4L2_STD_UNKNOWN,
1222 .current_norm = V4L2_STD_UNKNOWN,
1223 .fops = &v4l_stk_fops,
1224 .ioctl_ops = &v4l_stk_ioctl_ops,
1225 .release = stk_v4l_dev_release,
1229 static int stk_register_video_device(struct stk_camera *dev)
1233 dev->vdev = stk_v4l_data;
1234 dev->vdev.debug = debug;
1235 dev->vdev.parent = &dev->interface->dev;
1236 err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1238 STK_ERROR("v4l registration failed\n");
1240 STK_INFO("Syntek USB2.0 Camera is now controlling device %s\n",
1241 video_device_node_name(&dev->vdev));
1248 static int stk_camera_probe(struct usb_interface *interface,
1249 const struct usb_device_id *id)
1254 struct stk_camera *dev = NULL;
1255 struct usb_device *udev = interface_to_usbdev(interface);
1256 struct usb_host_interface *iface_desc;
1257 struct usb_endpoint_descriptor *endpoint;
1259 dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
1261 STK_ERROR("Out of memory !\n");
1265 spin_lock_init(&dev->spinlock);
1266 init_waitqueue_head(&dev->wait_frame);
1269 dev->interface = interface;
1270 usb_get_intf(interface);
1272 dev->vsettings.vflip = vflip;
1273 dev->vsettings.hflip = hflip;
1277 /* Set up the endpoint information
1278 * use only the first isoc-in endpoint
1279 * for the current alternate setting */
1280 iface_desc = interface->cur_altsetting;
1282 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1283 endpoint = &iface_desc->endpoint[i].desc;
1286 && usb_endpoint_is_isoc_in(endpoint)) {
1287 /* we found an isoc in endpoint */
1288 dev->isoc_ep = usb_endpoint_num(endpoint);
1292 if (!dev->isoc_ep) {
1293 STK_ERROR("Could not find isoc-in endpoint");
1297 dev->vsettings.brightness = 0x7fff;
1298 dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
1299 dev->vsettings.mode = MODE_VGA;
1300 dev->frame_size = 640 * 480 * 2;
1302 INIT_LIST_HEAD(&dev->sio_avail);
1303 INIT_LIST_HEAD(&dev->sio_full);
1305 usb_set_intfdata(interface, dev);
1307 err = stk_register_video_device(dev);
1319 static void stk_camera_disconnect(struct usb_interface *interface)
1321 struct stk_camera *dev = usb_get_intfdata(interface);
1323 usb_set_intfdata(interface, NULL);
1326 wake_up_interruptible(&dev->wait_frame);
1328 STK_INFO("Syntek USB2.0 Camera release resources device %s\n",
1329 video_device_node_name(&dev->vdev));
1331 video_unregister_device(&dev->vdev);
1335 static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
1337 struct stk_camera *dev = usb_get_intfdata(intf);
1338 if (is_streaming(dev)) {
1339 stk_stop_stream(dev);
1340 /* yes, this is ugly */
1346 static int stk_camera_resume(struct usb_interface *intf)
1348 struct stk_camera *dev = usb_get_intfdata(intf);
1349 if (!is_initialised(dev))
1351 unset_initialised(dev);
1352 stk_initialise(dev);
1353 stk_setup_format(dev);
1354 if (is_streaming(dev))
1355 stk_start_stream(dev);
1360 static struct usb_driver stk_camera_driver = {
1361 .name = "stkwebcam",
1362 .probe = stk_camera_probe,
1363 .disconnect = stk_camera_disconnect,
1364 .id_table = stkwebcam_table,
1366 .suspend = stk_camera_suspend,
1367 .resume = stk_camera_resume,
1372 static int __init stk_camera_init(void)
1376 result = usb_register(&stk_camera_driver);
1378 STK_ERROR("usb_register failed ! Error number %d\n", result);
1384 static void __exit stk_camera_exit(void)
1386 usb_deregister(&stk_camera_driver);
1389 module_init(stk_camera_init);
1390 module_exit(stk_camera_exit);