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 0");
46 module_param(vflip, bool, 0444);
47 MODULE_PARM_DESC(vflip, "Vertical image flip. Defaults to 0");
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");
57 /* Some cameras have audio interfaces, we aren't interested in those */
58 static struct usb_device_id stkwebcam_table[] = {
59 { USB_DEVICE_AND_INTERFACE_INFO(0x174f, 0xa311, 0xff, 0xff, 0xff) },
60 { USB_DEVICE_AND_INTERFACE_INFO(0x05e1, 0x0501, 0xff, 0xff, 0xff) },
63 MODULE_DEVICE_TABLE(usb, stkwebcam_table);
68 int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
70 struct usb_device *udev = dev->udev;
73 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
75 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
87 int stk_camera_read_reg(struct stk_camera *dev, u16 index, int *value)
89 struct usb_device *udev = dev->udev;
92 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
94 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
106 static int stk_start_stream(struct stk_camera *dev)
110 int value_116, value_117;
112 if (!is_present(dev))
114 if (!is_memallocd(dev) || !is_initialised(dev)) {
115 STK_ERROR("FIXME: Buffers are not allocated\n");
118 ret = usb_set_interface(dev->udev, 0, 5);
121 STK_ERROR("usb_set_interface failed !\n");
122 if (stk_sensor_wakeup(dev))
123 STK_ERROR("error awaking the sensor\n");
125 stk_camera_read_reg(dev, 0x0116, &value_116);
126 stk_camera_read_reg(dev, 0x0117, &value_117);
128 stk_camera_write_reg(dev, 0x0116, 0x0000);
129 stk_camera_write_reg(dev, 0x0117, 0x0000);
131 stk_camera_read_reg(dev, 0x0100, &value);
132 stk_camera_write_reg(dev, 0x0100, value | 0x80);
134 stk_camera_write_reg(dev, 0x0116, value_116);
135 stk_camera_write_reg(dev, 0x0117, value_117);
136 for (i = 0; i < MAX_ISO_BUFS; i++) {
137 if (dev->isobufs[i].urb) {
138 ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL);
139 atomic_inc(&dev->urbs_used);
148 static int stk_stop_stream(struct stk_camera *dev)
152 if (is_present(dev)) {
153 stk_camera_read_reg(dev, 0x0100, &value);
154 stk_camera_write_reg(dev, 0x0100, value & ~0x80);
155 if (dev->isobufs != NULL) {
156 for (i = 0; i < MAX_ISO_BUFS; i++) {
157 if (dev->isobufs[i].urb)
158 usb_kill_urb(dev->isobufs[i].urb);
161 unset_streaming(dev);
163 if (usb_set_interface(dev->udev, 0, 0))
164 STK_ERROR("usb_set_interface failed !\n");
165 if (stk_sensor_sleep(dev))
166 STK_ERROR("error suspending the sensor\n");
172 * This seems to be the shortest init sequence we
173 * must do in order to find the sensor
174 * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
175 * is also reset. Maybe powers down it?
176 * Rest of values don't make a difference
179 static struct regval stk1125_initvals[] = {
180 /*TODO: What means this sequence? */
209 static int stk_initialise(struct stk_camera *dev)
213 if (!is_present(dev))
215 if (is_initialised(dev))
217 rv = stk1125_initvals;
218 while (rv->reg != 0xffff) {
219 ret = stk_camera_write_reg(dev, rv->reg, rv->val);
224 if (stk_sensor_init(dev) == 0) {
225 set_initialised(dev);
231 /* *********************************************** */
233 * This function is called as an URB transfert is complete (Isochronous pipe).
234 * So, the traitement is done in interrupt time, so it has be fast, not crash,
235 * and not stall. Neat.
237 static void stk_isoc_handler(struct urb *urb)
244 unsigned char *fill = NULL;
245 unsigned char *iso_buf = NULL;
247 struct stk_camera *dev;
248 struct stk_sio_buffer *fb;
250 dev = (struct stk_camera *) urb->context;
253 STK_ERROR("isoc_handler called with NULL device !\n");
257 if (urb->status == -ENOENT || urb->status == -ECONNRESET
258 || urb->status == -ESHUTDOWN) {
259 atomic_dec(&dev->urbs_used);
263 spin_lock_irqsave(&dev->spinlock, flags);
265 if (urb->status != -EINPROGRESS && urb->status != 0) {
266 STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
270 if (list_empty(&dev->sio_avail)) {
271 /*FIXME Stop streaming after a while */
272 (void) (printk_ratelimit() &&
273 STK_ERROR("isoc_handler without available buffer!\n"));
276 fb = list_first_entry(&dev->sio_avail,
277 struct stk_sio_buffer, list);
278 fill = fb->buffer + fb->v4lbuf.bytesused;
280 for (i = 0; i < urb->number_of_packets; i++) {
281 if (urb->iso_frame_desc[i].status != 0) {
282 if (urb->iso_frame_desc[i].status != -EXDEV)
283 STK_ERROR("Frame %d has error %d\n", i,
284 urb->iso_frame_desc[i].status);
287 framelen = urb->iso_frame_desc[i].actual_length;
288 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
291 continue; /* no data */
294 * we found something informational from there
295 * the isoc frames have to type of headers
296 * type1: 00 xx 00 00 or 20 xx 00 00
297 * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
298 * xx is a sequencer which has never been seen over 0x3f
299 * imho data written down looks like bayer, i see similarities
300 * after every 640 bytes
302 if (*iso_buf & 0x80) {
305 /* This marks a new frame */
306 if (fb->v4lbuf.bytesused != 0
307 && fb->v4lbuf.bytesused != dev->frame_size) {
308 (void) (printk_ratelimit() &&
309 STK_ERROR("frame %d, "
310 "bytesused=%d, skipping\n",
311 i, fb->v4lbuf.bytesused));
312 fb->v4lbuf.bytesused = 0;
314 } else if (fb->v4lbuf.bytesused == dev->frame_size) {
315 if (list_is_singular(&dev->sio_avail)) {
316 /* Always reuse the last buffer */
317 fb->v4lbuf.bytesused = 0;
320 list_move_tail(dev->sio_avail.next,
322 wake_up(&dev->wait_frame);
323 fb = list_first_entry(&dev->sio_avail,
324 struct stk_sio_buffer, list);
325 fb->v4lbuf.bytesused = 0;
334 /* Our buffer is full !!! */
335 if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
336 (void) (printk_ratelimit() &&
337 STK_ERROR("Frame buffer overflow, lost sync\n"));
338 /*FIXME Do something here? */
341 spin_unlock_irqrestore(&dev->spinlock, flags);
342 memcpy(fill, iso_buf, framelen);
343 spin_lock_irqsave(&dev->spinlock, flags);
346 /* New size of our buffer */
347 fb->v4lbuf.bytesused += framelen;
351 spin_unlock_irqrestore(&dev->spinlock, flags);
352 urb->dev = dev->udev;
353 ret = usb_submit_urb(urb, GFP_ATOMIC);
355 STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
360 /* -------------------------------------------- */
362 static int stk_prepare_iso(struct stk_camera *dev)
367 struct usb_device *udev;
374 STK_ERROR("isobufs already allocated. Bad\n");
376 dev->isobufs = kcalloc(MAX_ISO_BUFS, sizeof(*dev->isobufs),
378 if (dev->isobufs == NULL) {
379 STK_ERROR("Unable to allocate iso buffers\n");
382 for (i = 0; i < MAX_ISO_BUFS; i++) {
383 if (dev->isobufs[i].data == NULL) {
384 kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
386 STK_ERROR("Failed to allocate iso buffer %d\n",
390 dev->isobufs[i].data = kbuf;
392 STK_ERROR("isobuf data already allocated\n");
393 if (dev->isobufs[i].urb == NULL) {
394 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
396 STK_ERROR("Failed to allocate URB %d\n", i);
399 dev->isobufs[i].urb = urb;
401 STK_ERROR("Killing URB\n");
402 usb_kill_urb(dev->isobufs[i].urb);
403 urb = dev->isobufs[i].urb;
407 urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
408 urb->transfer_flags = URB_ISO_ASAP;
409 urb->transfer_buffer = dev->isobufs[i].data;
410 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
411 urb->complete = stk_isoc_handler;
413 urb->start_frame = 0;
414 urb->number_of_packets = ISO_FRAMES_PER_DESC;
416 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
417 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
418 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
425 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
426 kfree(dev->isobufs[i].data);
427 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
428 usb_free_urb(dev->isobufs[i].urb);
434 static void stk_clean_iso(struct stk_camera *dev)
438 if (dev == NULL || dev->isobufs == NULL)
441 for (i = 0; i < MAX_ISO_BUFS; i++) {
444 urb = dev->isobufs[i].urb;
446 if (atomic_read(&dev->urbs_used) && is_present(dev))
450 kfree(dev->isobufs[i].data);
454 unset_memallocd(dev);
457 static int stk_setup_siobuf(struct stk_camera *dev, int index)
459 struct stk_sio_buffer *buf = dev->sio_bufs + index;
460 INIT_LIST_HEAD(&buf->list);
461 buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
462 buf->buffer = vmalloc_user(buf->v4lbuf.length);
463 if (buf->buffer == NULL)
467 buf->v4lbuf.index = index;
468 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
469 buf->v4lbuf.field = V4L2_FIELD_NONE;
470 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
471 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
475 static int stk_free_sio_buffers(struct stk_camera *dev)
480 if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
483 * If any buffers are mapped, we cannot free them at all.
485 for (i = 0; i < dev->n_sbufs; i++) {
486 if (dev->sio_bufs[i].mapcount > 0)
492 spin_lock_irqsave(&dev->spinlock, flags);
493 INIT_LIST_HEAD(&dev->sio_avail);
494 INIT_LIST_HEAD(&dev->sio_full);
495 nbufs = dev->n_sbufs;
497 spin_unlock_irqrestore(&dev->spinlock, flags);
498 for (i = 0; i < nbufs; i++) {
499 if (dev->sio_bufs[i].buffer != NULL)
500 vfree(dev->sio_bufs[i].buffer);
502 kfree(dev->sio_bufs);
503 dev->sio_bufs = NULL;
507 static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
510 if (dev->sio_bufs != NULL)
511 STK_ERROR("sio_bufs already allocated\n");
513 dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
515 if (dev->sio_bufs == NULL)
517 for (i = 0; i < n_sbufs; i++) {
518 if (stk_setup_siobuf(dev, i))
519 return (dev->n_sbufs > 1 ? 0 : -ENOMEM);
526 static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
529 err = stk_prepare_iso(dev);
534 err = stk_prepare_sio_buffers(dev, n_sbufs);
536 stk_free_sio_buffers(dev);
542 static void stk_free_buffers(struct stk_camera *dev)
545 stk_free_sio_buffers(dev);
547 /* -------------------------------------------- */
549 /* v4l file operations */
551 static int v4l_stk_open(struct file *fp)
553 static int first_init = 1; /* webcam LED management */
554 struct stk_camera *dev;
555 struct video_device *vdev;
557 vdev = video_devdata(fp);
558 dev = vdev_to_camera(vdev);
560 if (dev == NULL || !is_present(dev))
564 stk_camera_write_reg(dev, 0x0, 0x24);
568 fp->private_data = dev;
569 usb_autopm_get_interface(dev->interface);
574 static int v4l_stk_release(struct file *fp)
576 struct stk_camera *dev = fp->private_data;
578 if (dev->owner == fp) {
579 stk_stop_stream(dev);
580 stk_free_buffers(dev);
581 stk_camera_write_reg(dev, 0x0, 0x49); /* turn off the LED */
582 unset_initialised(dev);
587 usb_autopm_put_interface(dev->interface);
592 static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
593 size_t count, loff_t *f_pos)
598 struct stk_sio_buffer *sbuf;
599 struct stk_camera *dev = fp->private_data;
601 if (!is_present(dev))
603 if (dev->owner && dev->owner != fp)
606 if (!is_streaming(dev)) {
607 if (stk_initialise(dev)
608 || stk_allocate_buffers(dev, 3)
609 || stk_start_stream(dev))
611 spin_lock_irqsave(&dev->spinlock, flags);
612 for (i = 0; i < dev->n_sbufs; i++) {
613 list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
614 dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
616 spin_unlock_irqrestore(&dev->spinlock, flags);
619 if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
621 ret = wait_event_interruptible(dev->wait_frame,
622 !list_empty(&dev->sio_full) || !is_present(dev));
625 if (!is_present(dev))
628 if (count + *f_pos > dev->frame_size)
629 count = dev->frame_size - *f_pos;
630 spin_lock_irqsave(&dev->spinlock, flags);
631 if (list_empty(&dev->sio_full)) {
632 spin_unlock_irqrestore(&dev->spinlock, flags);
633 STK_ERROR("BUG: No siobufs ready\n");
636 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
637 spin_unlock_irqrestore(&dev->spinlock, flags);
639 if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
644 if (*f_pos >= dev->frame_size) {
646 spin_lock_irqsave(&dev->spinlock, flags);
647 list_move_tail(&sbuf->list, &dev->sio_avail);
648 spin_unlock_irqrestore(&dev->spinlock, flags);
653 static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
655 struct stk_camera *dev = fp->private_data;
657 poll_wait(fp, &dev->wait_frame, wait);
659 if (!is_present(dev))
662 if (!list_empty(&dev->sio_full))
663 return POLLIN | POLLRDNORM;
669 static void stk_v4l_vm_open(struct vm_area_struct *vma)
671 struct stk_sio_buffer *sbuf = vma->vm_private_data;
674 static void stk_v4l_vm_close(struct vm_area_struct *vma)
676 struct stk_sio_buffer *sbuf = vma->vm_private_data;
678 if (sbuf->mapcount == 0)
679 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
681 static const struct vm_operations_struct stk_v4l_vm_ops = {
682 .open = stk_v4l_vm_open,
683 .close = stk_v4l_vm_close
686 static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
690 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
691 struct stk_camera *dev = fp->private_data;
692 struct stk_sio_buffer *sbuf = NULL;
694 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
697 for (i = 0; i < dev->n_sbufs; i++) {
698 if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
699 sbuf = dev->sio_bufs + i;
705 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
708 vma->vm_flags |= VM_DONTEXPAND;
709 vma->vm_private_data = sbuf;
710 vma->vm_ops = &stk_v4l_vm_ops;
711 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
712 stk_v4l_vm_open(vma);
716 /* v4l ioctl handlers */
718 static int stk_vidioc_querycap(struct file *filp,
719 void *priv, struct v4l2_capability *cap)
721 strcpy(cap->driver, "stk");
722 strcpy(cap->card, "stk");
723 cap->version = DRIVER_VERSION_NUM;
725 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
726 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
730 static int stk_vidioc_enum_input(struct file *filp,
731 void *priv, struct v4l2_input *input)
733 if (input->index != 0)
736 strcpy(input->name, "Syntek USB Camera");
737 input->type = V4L2_INPUT_TYPE_CAMERA;
742 static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
748 static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
757 static int stk_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
762 /* List of all V4Lv2 controls supported by the driver */
763 static struct v4l2_queryctrl stk_controls[] = {
765 .id = V4L2_CID_BRIGHTNESS,
766 .type = V4L2_CTRL_TYPE_INTEGER,
767 .name = "Brightness",
771 .default_value = 0x6000,
774 .id = V4L2_CID_HFLIP,
775 .type = V4L2_CTRL_TYPE_BOOLEAN,
776 .name = "Horizontal Flip",
783 .id = V4L2_CID_VFLIP,
784 .type = V4L2_CTRL_TYPE_BOOLEAN,
785 .name = "Vertical Flip",
793 static int stk_vidioc_queryctrl(struct file *filp,
794 void *priv, struct v4l2_queryctrl *c)
798 nbr = ARRAY_SIZE(stk_controls);
800 for (i = 0; i < nbr; i++) {
801 if (stk_controls[i].id == c->id) {
802 memcpy(c, &stk_controls[i],
803 sizeof(struct v4l2_queryctrl));
810 static int stk_vidioc_g_ctrl(struct file *filp,
811 void *priv, struct v4l2_control *c)
813 struct stk_camera *dev = priv;
815 case V4L2_CID_BRIGHTNESS:
816 c->value = dev->vsettings.brightness;
819 c->value = dev->vsettings.hflip;
822 c->value = dev->vsettings.vflip;
830 static int stk_vidioc_s_ctrl(struct file *filp,
831 void *priv, struct v4l2_control *c)
833 struct stk_camera *dev = priv;
835 case V4L2_CID_BRIGHTNESS:
836 dev->vsettings.brightness = c->value;
837 return stk_sensor_set_brightness(dev, c->value >> 8);
839 dev->vsettings.hflip = c->value;
842 dev->vsettings.vflip = c->value;
851 static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
852 void *priv, struct v4l2_fmtdesc *fmtd)
854 switch (fmtd->index) {
856 fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
857 strcpy(fmtd->description, "r5g6b5");
860 fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
861 strcpy(fmtd->description, "r5g6b5BE");
864 fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
865 strcpy(fmtd->description, "yuv4:2:2");
868 fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
869 strcpy(fmtd->description, "Raw bayer");
872 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
873 strcpy(fmtd->description, "yuv4:2:2");
881 static struct stk_size {
886 { .w = 1280, .h = 1024, .m = MODE_SXGA, },
887 { .w = 640, .h = 480, .m = MODE_VGA, },
888 { .w = 352, .h = 288, .m = MODE_CIF, },
889 { .w = 320, .h = 240, .m = MODE_QVGA, },
890 { .w = 176, .h = 144, .m = MODE_QCIF, },
893 static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
894 void *priv, struct v4l2_format *f)
896 struct v4l2_pix_format *pix_format = &f->fmt.pix;
897 struct stk_camera *dev = priv;
900 for (i = 0; i < ARRAY_SIZE(stk_sizes) &&
901 stk_sizes[i].m != dev->vsettings.mode; i++)
903 if (i == ARRAY_SIZE(stk_sizes)) {
904 STK_ERROR("ERROR: mode invalid\n");
907 pix_format->width = stk_sizes[i].w;
908 pix_format->height = stk_sizes[i].h;
909 pix_format->field = V4L2_FIELD_NONE;
910 pix_format->colorspace = V4L2_COLORSPACE_SRGB;
911 pix_format->pixelformat = dev->vsettings.palette;
912 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
913 pix_format->bytesperline = pix_format->width;
915 pix_format->bytesperline = 2 * pix_format->width;
916 pix_format->sizeimage = pix_format->bytesperline
917 * pix_format->height;
921 static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
922 void *priv, struct v4l2_format *fmtd)
925 switch (fmtd->fmt.pix.pixelformat) {
926 case V4L2_PIX_FMT_RGB565:
927 case V4L2_PIX_FMT_RGB565X:
928 case V4L2_PIX_FMT_UYVY:
929 case V4L2_PIX_FMT_YUYV:
930 case V4L2_PIX_FMT_SBGGR8:
935 for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
936 if (fmtd->fmt.pix.width > stk_sizes[i].w)
939 if (i == ARRAY_SIZE(stk_sizes)
940 || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
941 < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
942 fmtd->fmt.pix.height = stk_sizes[i-1].h;
943 fmtd->fmt.pix.width = stk_sizes[i-1].w;
944 fmtd->fmt.pix.priv = i - 1;
946 fmtd->fmt.pix.height = stk_sizes[i].h;
947 fmtd->fmt.pix.width = stk_sizes[i].w;
948 fmtd->fmt.pix.priv = i;
951 fmtd->fmt.pix.field = V4L2_FIELD_NONE;
952 fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
953 if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
954 fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
956 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
957 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
958 * fmtd->fmt.pix.height;
962 static int stk_setup_format(struct stk_camera *dev)
966 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
970 while (i < ARRAY_SIZE(stk_sizes) &&
971 stk_sizes[i].m != dev->vsettings.mode)
973 if (i == ARRAY_SIZE(stk_sizes)) {
974 STK_ERROR("Something is broken in %s\n", __func__);
977 /* This registers controls some timings, not sure of what. */
978 stk_camera_write_reg(dev, 0x001b, 0x0e);
979 if (dev->vsettings.mode == MODE_SXGA)
980 stk_camera_write_reg(dev, 0x001c, 0x0e);
982 stk_camera_write_reg(dev, 0x001c, 0x46);
984 * Registers 0x0115 0x0114 are the size of each line (bytes),
985 * regs 0x0117 0x0116 are the heigth of the image.
987 stk_camera_write_reg(dev, 0x0115,
988 ((stk_sizes[i].w * depth) >> 8) & 0xff);
989 stk_camera_write_reg(dev, 0x0114,
990 (stk_sizes[i].w * depth) & 0xff);
991 stk_camera_write_reg(dev, 0x0117,
992 (stk_sizes[i].h >> 8) & 0xff);
993 stk_camera_write_reg(dev, 0x0116,
994 stk_sizes[i].h & 0xff);
995 return stk_sensor_configure(dev);
998 static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
999 void *priv, struct v4l2_format *fmtd)
1002 struct stk_camera *dev = priv;
1006 if (!is_present(dev))
1008 if (is_streaming(dev))
1010 if (dev->owner && dev->owner != filp)
1012 ret = stk_vidioc_try_fmt_vid_cap(filp, priv, fmtd);
1017 dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
1018 stk_free_buffers(dev);
1019 dev->frame_size = fmtd->fmt.pix.sizeimage;
1020 dev->vsettings.mode = stk_sizes[fmtd->fmt.pix.priv].m;
1022 stk_initialise(dev);
1023 return stk_setup_format(dev);
1026 static int stk_vidioc_reqbufs(struct file *filp,
1027 void *priv, struct v4l2_requestbuffers *rb)
1029 struct stk_camera *dev = priv;
1033 if (rb->memory != V4L2_MEMORY_MMAP)
1035 if (is_streaming(dev)
1036 || (dev->owner && dev->owner != filp))
1040 /*FIXME If they ask for zero, we must stop streaming and free */
1043 /* Arbitrary limit */
1044 else if (rb->count > 5)
1047 stk_allocate_buffers(dev, rb->count);
1048 rb->count = dev->n_sbufs;
1052 static int stk_vidioc_querybuf(struct file *filp,
1053 void *priv, struct v4l2_buffer *buf)
1055 struct stk_camera *dev = priv;
1056 struct stk_sio_buffer *sbuf;
1058 if (buf->index >= dev->n_sbufs)
1060 sbuf = dev->sio_bufs + buf->index;
1061 *buf = sbuf->v4lbuf;
1065 static int stk_vidioc_qbuf(struct file *filp,
1066 void *priv, struct v4l2_buffer *buf)
1068 struct stk_camera *dev = priv;
1069 struct stk_sio_buffer *sbuf;
1070 unsigned long flags;
1072 if (buf->memory != V4L2_MEMORY_MMAP)
1075 if (buf->index >= dev->n_sbufs)
1077 sbuf = dev->sio_bufs + buf->index;
1078 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
1080 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1081 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1082 spin_lock_irqsave(&dev->spinlock, flags);
1083 list_add_tail(&sbuf->list, &dev->sio_avail);
1084 *buf = sbuf->v4lbuf;
1085 spin_unlock_irqrestore(&dev->spinlock, flags);
1089 static int stk_vidioc_dqbuf(struct file *filp,
1090 void *priv, struct v4l2_buffer *buf)
1092 struct stk_camera *dev = priv;
1093 struct stk_sio_buffer *sbuf;
1094 unsigned long flags;
1097 if (!is_streaming(dev))
1100 if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
1101 return -EWOULDBLOCK;
1102 ret = wait_event_interruptible(dev->wait_frame,
1103 !list_empty(&dev->sio_full) || !is_present(dev));
1106 if (!is_present(dev))
1109 spin_lock_irqsave(&dev->spinlock, flags);
1110 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
1111 list_del_init(&sbuf->list);
1112 spin_unlock_irqrestore(&dev->spinlock, flags);
1113 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1114 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1115 sbuf->v4lbuf.sequence = ++dev->sequence;
1116 do_gettimeofday(&sbuf->v4lbuf.timestamp);
1118 *buf = sbuf->v4lbuf;
1122 static int stk_vidioc_streamon(struct file *filp,
1123 void *priv, enum v4l2_buf_type type)
1125 struct stk_camera *dev = priv;
1126 if (is_streaming(dev))
1128 if (dev->sio_bufs == NULL)
1131 return stk_start_stream(dev);
1134 static int stk_vidioc_streamoff(struct file *filp,
1135 void *priv, enum v4l2_buf_type type)
1137 struct stk_camera *dev = priv;
1138 unsigned long flags;
1140 stk_stop_stream(dev);
1141 spin_lock_irqsave(&dev->spinlock, flags);
1142 INIT_LIST_HEAD(&dev->sio_avail);
1143 INIT_LIST_HEAD(&dev->sio_full);
1144 for (i = 0; i < dev->n_sbufs; i++) {
1145 INIT_LIST_HEAD(&dev->sio_bufs[i].list);
1146 dev->sio_bufs[i].v4lbuf.flags = 0;
1148 spin_unlock_irqrestore(&dev->spinlock, flags);
1153 static int stk_vidioc_g_parm(struct file *filp,
1154 void *priv, struct v4l2_streamparm *sp)
1156 /*FIXME This is not correct */
1157 sp->parm.capture.timeperframe.numerator = 1;
1158 sp->parm.capture.timeperframe.denominator = 30;
1159 sp->parm.capture.readbuffers = 2;
1163 static int stk_vidioc_enum_framesizes(struct file *filp,
1164 void *priv, struct v4l2_frmsizeenum *frms)
1166 if (frms->index >= ARRAY_SIZE(stk_sizes))
1168 switch (frms->pixel_format) {
1169 case V4L2_PIX_FMT_RGB565:
1170 case V4L2_PIX_FMT_RGB565X:
1171 case V4L2_PIX_FMT_UYVY:
1172 case V4L2_PIX_FMT_YUYV:
1173 case V4L2_PIX_FMT_SBGGR8:
1174 frms->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1175 frms->discrete.width = stk_sizes[frms->index].w;
1176 frms->discrete.height = stk_sizes[frms->index].h;
1178 default: return -EINVAL;
1182 static struct v4l2_file_operations v4l_stk_fops = {
1183 .owner = THIS_MODULE,
1184 .open = v4l_stk_open,
1185 .release = v4l_stk_release,
1186 .read = v4l_stk_read,
1187 .poll = v4l_stk_poll,
1188 .mmap = v4l_stk_mmap,
1189 .ioctl = video_ioctl2,
1192 static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops = {
1193 .vidioc_querycap = stk_vidioc_querycap,
1194 .vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
1195 .vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
1196 .vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
1197 .vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
1198 .vidioc_enum_input = stk_vidioc_enum_input,
1199 .vidioc_s_input = stk_vidioc_s_input,
1200 .vidioc_g_input = stk_vidioc_g_input,
1201 .vidioc_s_std = stk_vidioc_s_std,
1202 .vidioc_reqbufs = stk_vidioc_reqbufs,
1203 .vidioc_querybuf = stk_vidioc_querybuf,
1204 .vidioc_qbuf = stk_vidioc_qbuf,
1205 .vidioc_dqbuf = stk_vidioc_dqbuf,
1206 .vidioc_streamon = stk_vidioc_streamon,
1207 .vidioc_streamoff = stk_vidioc_streamoff,
1208 .vidioc_queryctrl = stk_vidioc_queryctrl,
1209 .vidioc_g_ctrl = stk_vidioc_g_ctrl,
1210 .vidioc_s_ctrl = stk_vidioc_s_ctrl,
1211 .vidioc_g_parm = stk_vidioc_g_parm,
1212 .vidioc_enum_framesizes = stk_vidioc_enum_framesizes,
1215 static void stk_v4l_dev_release(struct video_device *vd)
1217 struct stk_camera *dev = vdev_to_camera(vd);
1219 if (dev->sio_bufs != NULL || dev->isobufs != NULL)
1220 STK_ERROR("We are leaking memory\n");
1221 usb_put_intf(dev->interface);
1225 static struct video_device stk_v4l_data = {
1226 .name = "stkwebcam",
1227 .tvnorms = V4L2_STD_UNKNOWN,
1228 .current_norm = V4L2_STD_UNKNOWN,
1229 .fops = &v4l_stk_fops,
1230 .ioctl_ops = &v4l_stk_ioctl_ops,
1231 .release = stk_v4l_dev_release,
1235 static int stk_register_video_device(struct stk_camera *dev)
1239 dev->vdev = stk_v4l_data;
1240 dev->vdev.debug = debug;
1241 dev->vdev.parent = &dev->interface->dev;
1242 err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1244 STK_ERROR("v4l registration failed\n");
1246 STK_INFO("Syntek USB2.0 Camera is now controlling device %s\n",
1247 video_device_node_name(&dev->vdev));
1254 static int stk_camera_probe(struct usb_interface *interface,
1255 const struct usb_device_id *id)
1260 struct stk_camera *dev = NULL;
1261 struct usb_device *udev = interface_to_usbdev(interface);
1262 struct usb_host_interface *iface_desc;
1263 struct usb_endpoint_descriptor *endpoint;
1265 dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
1267 STK_ERROR("Out of memory !\n");
1271 spin_lock_init(&dev->spinlock);
1272 init_waitqueue_head(&dev->wait_frame);
1275 dev->interface = interface;
1276 usb_get_intf(interface);
1278 dev->vsettings.vflip = vflip;
1279 dev->vsettings.hflip = hflip;
1283 /* Set up the endpoint information
1284 * use only the first isoc-in endpoint
1285 * for the current alternate setting */
1286 iface_desc = interface->cur_altsetting;
1288 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1289 endpoint = &iface_desc->endpoint[i].desc;
1292 && usb_endpoint_is_isoc_in(endpoint)) {
1293 /* we found an isoc in endpoint */
1294 dev->isoc_ep = usb_endpoint_num(endpoint);
1298 if (!dev->isoc_ep) {
1299 STK_ERROR("Could not find isoc-in endpoint");
1303 dev->vsettings.brightness = 0x7fff;
1304 dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
1305 dev->vsettings.mode = MODE_VGA;
1306 dev->frame_size = 640 * 480 * 2;
1308 INIT_LIST_HEAD(&dev->sio_avail);
1309 INIT_LIST_HEAD(&dev->sio_full);
1311 usb_set_intfdata(interface, dev);
1313 err = stk_register_video_device(dev);
1324 static void stk_camera_disconnect(struct usb_interface *interface)
1326 struct stk_camera *dev = usb_get_intfdata(interface);
1328 usb_set_intfdata(interface, NULL);
1331 wake_up_interruptible(&dev->wait_frame);
1333 STK_INFO("Syntek USB2.0 Camera release resources device %s\n",
1334 video_device_node_name(&dev->vdev));
1336 video_unregister_device(&dev->vdev);
1340 static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
1342 struct stk_camera *dev = usb_get_intfdata(intf);
1343 if (is_streaming(dev)) {
1344 stk_stop_stream(dev);
1345 /* yes, this is ugly */
1351 static int stk_camera_resume(struct usb_interface *intf)
1353 struct stk_camera *dev = usb_get_intfdata(intf);
1354 if (!is_initialised(dev))
1356 unset_initialised(dev);
1357 stk_initialise(dev);
1358 stk_camera_write_reg(dev, 0x0, 0x49);
1359 stk_setup_format(dev);
1360 if (is_streaming(dev))
1361 stk_start_stream(dev);
1366 static struct usb_driver stk_camera_driver = {
1367 .name = "stkwebcam",
1368 .probe = stk_camera_probe,
1369 .disconnect = stk_camera_disconnect,
1370 .id_table = stkwebcam_table,
1372 .suspend = stk_camera_suspend,
1373 .resume = stk_camera_resume,
1377 module_usb_driver(stk_camera_driver);