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/dmi.h>
32 #include <linux/usb.h>
34 #include <linux/vmalloc.h>
35 #include <linux/videodev2.h>
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-ioctl.h>
39 #include "stk-webcam.h"
42 static int hflip = -1;
43 module_param(hflip, int, 0444);
44 MODULE_PARM_DESC(hflip, "Horizontal image flip (mirror). Defaults to 0");
46 static int vflip = -1;
47 module_param(vflip, int, 0444);
48 MODULE_PARM_DESC(vflip, "Vertical image flip. Defaults to 0");
51 module_param(debug, int, 0444);
52 MODULE_PARM_DESC(debug, "Debug v4l ioctls. Defaults to 0");
54 MODULE_LICENSE("GPL");
55 MODULE_AUTHOR("Jaime Velasco Juan <jsagarribay@gmail.com> and Nicolas VIVIEN");
56 MODULE_DESCRIPTION("Syntek DC1125 webcam driver");
58 /* Some cameras have audio interfaces, we aren't interested in those */
59 static struct usb_device_id stkwebcam_table[] = {
60 { USB_DEVICE_AND_INTERFACE_INFO(0x174f, 0xa311, 0xff, 0xff, 0xff) },
61 { USB_DEVICE_AND_INTERFACE_INFO(0x05e1, 0x0501, 0xff, 0xff, 0xff) },
64 MODULE_DEVICE_TABLE(usb, stkwebcam_table);
66 /* The stk webcam laptop module is mounted upside down in some laptops :( */
67 static const struct dmi_system_id stk_upside_down_dmi_table[] = {
71 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
72 DMI_MATCH(DMI_PRODUCT_NAME, "G1")
82 int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
84 struct usb_device *udev = dev->udev;
87 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
89 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
101 int stk_camera_read_reg(struct stk_camera *dev, u16 index, int *value)
103 struct usb_device *udev = dev->udev;
106 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
108 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
120 static int stk_start_stream(struct stk_camera *dev)
124 int value_116, value_117;
126 if (!is_present(dev))
128 if (!is_memallocd(dev) || !is_initialised(dev)) {
129 STK_ERROR("FIXME: Buffers are not allocated\n");
132 ret = usb_set_interface(dev->udev, 0, 5);
135 STK_ERROR("usb_set_interface failed !\n");
136 if (stk_sensor_wakeup(dev))
137 STK_ERROR("error awaking the sensor\n");
139 stk_camera_read_reg(dev, 0x0116, &value_116);
140 stk_camera_read_reg(dev, 0x0117, &value_117);
142 stk_camera_write_reg(dev, 0x0116, 0x0000);
143 stk_camera_write_reg(dev, 0x0117, 0x0000);
145 stk_camera_read_reg(dev, 0x0100, &value);
146 stk_camera_write_reg(dev, 0x0100, value | 0x80);
148 stk_camera_write_reg(dev, 0x0116, value_116);
149 stk_camera_write_reg(dev, 0x0117, value_117);
150 for (i = 0; i < MAX_ISO_BUFS; i++) {
151 if (dev->isobufs[i].urb) {
152 ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL);
153 atomic_inc(&dev->urbs_used);
162 static int stk_stop_stream(struct stk_camera *dev)
166 if (is_present(dev)) {
167 stk_camera_read_reg(dev, 0x0100, &value);
168 stk_camera_write_reg(dev, 0x0100, value & ~0x80);
169 if (dev->isobufs != NULL) {
170 for (i = 0; i < MAX_ISO_BUFS; i++) {
171 if (dev->isobufs[i].urb)
172 usb_kill_urb(dev->isobufs[i].urb);
175 unset_streaming(dev);
177 if (usb_set_interface(dev->udev, 0, 0))
178 STK_ERROR("usb_set_interface failed !\n");
179 if (stk_sensor_sleep(dev))
180 STK_ERROR("error suspending the sensor\n");
186 * This seems to be the shortest init sequence we
187 * must do in order to find the sensor
188 * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
189 * is also reset. Maybe powers down it?
190 * Rest of values don't make a difference
193 static struct regval stk1125_initvals[] = {
194 /*TODO: What means this sequence? */
223 static int stk_initialise(struct stk_camera *dev)
227 if (!is_present(dev))
229 if (is_initialised(dev))
231 rv = stk1125_initvals;
232 while (rv->reg != 0xffff) {
233 ret = stk_camera_write_reg(dev, rv->reg, rv->val);
238 if (stk_sensor_init(dev) == 0) {
239 set_initialised(dev);
245 /* *********************************************** */
247 * This function is called as an URB transfert is complete (Isochronous pipe).
248 * So, the traitement is done in interrupt time, so it has be fast, not crash,
249 * and not stall. Neat.
251 static void stk_isoc_handler(struct urb *urb)
258 unsigned char *fill = NULL;
259 unsigned char *iso_buf = NULL;
261 struct stk_camera *dev;
262 struct stk_sio_buffer *fb;
264 dev = (struct stk_camera *) urb->context;
267 STK_ERROR("isoc_handler called with NULL device !\n");
271 if (urb->status == -ENOENT || urb->status == -ECONNRESET
272 || urb->status == -ESHUTDOWN) {
273 atomic_dec(&dev->urbs_used);
277 spin_lock_irqsave(&dev->spinlock, flags);
279 if (urb->status != -EINPROGRESS && urb->status != 0) {
280 STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
284 if (list_empty(&dev->sio_avail)) {
285 /*FIXME Stop streaming after a while */
286 (void) (printk_ratelimit() &&
287 STK_ERROR("isoc_handler without available buffer!\n"));
290 fb = list_first_entry(&dev->sio_avail,
291 struct stk_sio_buffer, list);
292 fill = fb->buffer + fb->v4lbuf.bytesused;
294 for (i = 0; i < urb->number_of_packets; i++) {
295 if (urb->iso_frame_desc[i].status != 0) {
296 if (urb->iso_frame_desc[i].status != -EXDEV)
297 STK_ERROR("Frame %d has error %d\n", i,
298 urb->iso_frame_desc[i].status);
301 framelen = urb->iso_frame_desc[i].actual_length;
302 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
305 continue; /* no data */
308 * we found something informational from there
309 * the isoc frames have to type of headers
310 * type1: 00 xx 00 00 or 20 xx 00 00
311 * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
312 * xx is a sequencer which has never been seen over 0x3f
313 * imho data written down looks like bayer, i see similarities
314 * after every 640 bytes
316 if (*iso_buf & 0x80) {
319 /* This marks a new frame */
320 if (fb->v4lbuf.bytesused != 0
321 && fb->v4lbuf.bytesused != dev->frame_size) {
322 (void) (printk_ratelimit() &&
323 STK_ERROR("frame %d, "
324 "bytesused=%d, skipping\n",
325 i, fb->v4lbuf.bytesused));
326 fb->v4lbuf.bytesused = 0;
328 } else if (fb->v4lbuf.bytesused == dev->frame_size) {
329 if (list_is_singular(&dev->sio_avail)) {
330 /* Always reuse the last buffer */
331 fb->v4lbuf.bytesused = 0;
334 list_move_tail(dev->sio_avail.next,
336 wake_up(&dev->wait_frame);
337 fb = list_first_entry(&dev->sio_avail,
338 struct stk_sio_buffer, list);
339 fb->v4lbuf.bytesused = 0;
348 /* Our buffer is full !!! */
349 if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
350 (void) (printk_ratelimit() &&
351 STK_ERROR("Frame buffer overflow, lost sync\n"));
352 /*FIXME Do something here? */
355 spin_unlock_irqrestore(&dev->spinlock, flags);
356 memcpy(fill, iso_buf, framelen);
357 spin_lock_irqsave(&dev->spinlock, flags);
360 /* New size of our buffer */
361 fb->v4lbuf.bytesused += framelen;
365 spin_unlock_irqrestore(&dev->spinlock, flags);
366 urb->dev = dev->udev;
367 ret = usb_submit_urb(urb, GFP_ATOMIC);
369 STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
374 /* -------------------------------------------- */
376 static int stk_prepare_iso(struct stk_camera *dev)
381 struct usb_device *udev;
388 STK_ERROR("isobufs already allocated. Bad\n");
390 dev->isobufs = kcalloc(MAX_ISO_BUFS, sizeof(*dev->isobufs),
392 if (dev->isobufs == NULL) {
393 STK_ERROR("Unable to allocate iso buffers\n");
396 for (i = 0; i < MAX_ISO_BUFS; i++) {
397 if (dev->isobufs[i].data == NULL) {
398 kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
400 STK_ERROR("Failed to allocate iso buffer %d\n",
404 dev->isobufs[i].data = kbuf;
406 STK_ERROR("isobuf data already allocated\n");
407 if (dev->isobufs[i].urb == NULL) {
408 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
410 STK_ERROR("Failed to allocate URB %d\n", i);
413 dev->isobufs[i].urb = urb;
415 STK_ERROR("Killing URB\n");
416 usb_kill_urb(dev->isobufs[i].urb);
417 urb = dev->isobufs[i].urb;
421 urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
422 urb->transfer_flags = URB_ISO_ASAP;
423 urb->transfer_buffer = dev->isobufs[i].data;
424 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
425 urb->complete = stk_isoc_handler;
427 urb->start_frame = 0;
428 urb->number_of_packets = ISO_FRAMES_PER_DESC;
430 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
431 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
432 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
439 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
440 kfree(dev->isobufs[i].data);
441 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
442 usb_free_urb(dev->isobufs[i].urb);
448 static void stk_clean_iso(struct stk_camera *dev)
452 if (dev == NULL || dev->isobufs == NULL)
455 for (i = 0; i < MAX_ISO_BUFS; i++) {
458 urb = dev->isobufs[i].urb;
460 if (atomic_read(&dev->urbs_used) && is_present(dev))
464 kfree(dev->isobufs[i].data);
468 unset_memallocd(dev);
471 static int stk_setup_siobuf(struct stk_camera *dev, int index)
473 struct stk_sio_buffer *buf = dev->sio_bufs + index;
474 INIT_LIST_HEAD(&buf->list);
475 buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
476 buf->buffer = vmalloc_user(buf->v4lbuf.length);
477 if (buf->buffer == NULL)
481 buf->v4lbuf.index = index;
482 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
483 buf->v4lbuf.flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
484 buf->v4lbuf.field = V4L2_FIELD_NONE;
485 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
486 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
490 static int stk_free_sio_buffers(struct stk_camera *dev)
495 if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
498 * If any buffers are mapped, we cannot free them at all.
500 for (i = 0; i < dev->n_sbufs; i++) {
501 if (dev->sio_bufs[i].mapcount > 0)
507 spin_lock_irqsave(&dev->spinlock, flags);
508 INIT_LIST_HEAD(&dev->sio_avail);
509 INIT_LIST_HEAD(&dev->sio_full);
510 nbufs = dev->n_sbufs;
512 spin_unlock_irqrestore(&dev->spinlock, flags);
513 for (i = 0; i < nbufs; i++) {
514 if (dev->sio_bufs[i].buffer != NULL)
515 vfree(dev->sio_bufs[i].buffer);
517 kfree(dev->sio_bufs);
518 dev->sio_bufs = NULL;
522 static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
525 if (dev->sio_bufs != NULL)
526 STK_ERROR("sio_bufs already allocated\n");
528 dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
530 if (dev->sio_bufs == NULL)
532 for (i = 0; i < n_sbufs; i++) {
533 if (stk_setup_siobuf(dev, i))
534 return (dev->n_sbufs > 1 ? 0 : -ENOMEM);
541 static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
544 err = stk_prepare_iso(dev);
549 err = stk_prepare_sio_buffers(dev, n_sbufs);
551 stk_free_sio_buffers(dev);
557 static void stk_free_buffers(struct stk_camera *dev)
560 stk_free_sio_buffers(dev);
562 /* -------------------------------------------- */
564 /* v4l file operations */
566 static int v4l_stk_open(struct file *fp)
568 static int first_init = 1; /* webcam LED management */
569 struct stk_camera *dev;
570 struct video_device *vdev;
572 vdev = video_devdata(fp);
573 dev = vdev_to_camera(vdev);
575 if (dev == NULL || !is_present(dev))
579 stk_camera_write_reg(dev, 0x0, 0x24);
583 fp->private_data = dev;
584 usb_autopm_get_interface(dev->interface);
589 static int v4l_stk_release(struct file *fp)
591 struct stk_camera *dev = fp->private_data;
593 if (dev->owner == fp) {
594 stk_stop_stream(dev);
595 stk_free_buffers(dev);
596 stk_camera_write_reg(dev, 0x0, 0x49); /* turn off the LED */
597 unset_initialised(dev);
602 usb_autopm_put_interface(dev->interface);
607 static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
608 size_t count, loff_t *f_pos)
613 struct stk_sio_buffer *sbuf;
614 struct stk_camera *dev = fp->private_data;
616 if (!is_present(dev))
618 if (dev->owner && dev->owner != fp)
621 if (!is_streaming(dev)) {
622 if (stk_initialise(dev)
623 || stk_allocate_buffers(dev, 3)
624 || stk_start_stream(dev))
626 spin_lock_irqsave(&dev->spinlock, flags);
627 for (i = 0; i < dev->n_sbufs; i++) {
628 list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
629 dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
631 spin_unlock_irqrestore(&dev->spinlock, flags);
634 if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
636 ret = wait_event_interruptible(dev->wait_frame,
637 !list_empty(&dev->sio_full) || !is_present(dev));
640 if (!is_present(dev))
643 if (count + *f_pos > dev->frame_size)
644 count = dev->frame_size - *f_pos;
645 spin_lock_irqsave(&dev->spinlock, flags);
646 if (list_empty(&dev->sio_full)) {
647 spin_unlock_irqrestore(&dev->spinlock, flags);
648 STK_ERROR("BUG: No siobufs ready\n");
651 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
652 spin_unlock_irqrestore(&dev->spinlock, flags);
654 if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
659 if (*f_pos >= dev->frame_size) {
661 spin_lock_irqsave(&dev->spinlock, flags);
662 list_move_tail(&sbuf->list, &dev->sio_avail);
663 spin_unlock_irqrestore(&dev->spinlock, flags);
668 static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
670 struct stk_camera *dev = fp->private_data;
672 poll_wait(fp, &dev->wait_frame, wait);
674 if (!is_present(dev))
677 if (!list_empty(&dev->sio_full))
678 return POLLIN | POLLRDNORM;
684 static void stk_v4l_vm_open(struct vm_area_struct *vma)
686 struct stk_sio_buffer *sbuf = vma->vm_private_data;
689 static void stk_v4l_vm_close(struct vm_area_struct *vma)
691 struct stk_sio_buffer *sbuf = vma->vm_private_data;
693 if (sbuf->mapcount == 0)
694 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
696 static const struct vm_operations_struct stk_v4l_vm_ops = {
697 .open = stk_v4l_vm_open,
698 .close = stk_v4l_vm_close
701 static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
705 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
706 struct stk_camera *dev = fp->private_data;
707 struct stk_sio_buffer *sbuf = NULL;
709 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
712 for (i = 0; i < dev->n_sbufs; i++) {
713 if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
714 sbuf = dev->sio_bufs + i;
720 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
723 vma->vm_flags |= VM_DONTEXPAND;
724 vma->vm_private_data = sbuf;
725 vma->vm_ops = &stk_v4l_vm_ops;
726 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
727 stk_v4l_vm_open(vma);
731 /* v4l ioctl handlers */
733 static int stk_vidioc_querycap(struct file *filp,
734 void *priv, struct v4l2_capability *cap)
736 strcpy(cap->driver, "stk");
737 strcpy(cap->card, "stk");
738 cap->version = DRIVER_VERSION_NUM;
740 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
741 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
745 static int stk_vidioc_enum_input(struct file *filp,
746 void *priv, struct v4l2_input *input)
748 if (input->index != 0)
751 strcpy(input->name, "Syntek USB Camera");
752 input->type = V4L2_INPUT_TYPE_CAMERA;
757 static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
763 static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
772 static int stk_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
777 /* List of all V4Lv2 controls supported by the driver */
778 static struct v4l2_queryctrl stk_controls[] = {
780 .id = V4L2_CID_BRIGHTNESS,
781 .type = V4L2_CTRL_TYPE_INTEGER,
782 .name = "Brightness",
786 .default_value = 0x6000,
789 .id = V4L2_CID_HFLIP,
790 .type = V4L2_CTRL_TYPE_BOOLEAN,
791 .name = "Horizontal Flip",
798 .id = V4L2_CID_VFLIP,
799 .type = V4L2_CTRL_TYPE_BOOLEAN,
800 .name = "Vertical Flip",
808 static int stk_vidioc_queryctrl(struct file *filp,
809 void *priv, struct v4l2_queryctrl *c)
813 nbr = ARRAY_SIZE(stk_controls);
815 for (i = 0; i < nbr; i++) {
816 if (stk_controls[i].id == c->id) {
817 memcpy(c, &stk_controls[i],
818 sizeof(struct v4l2_queryctrl));
825 static int stk_vidioc_g_ctrl(struct file *filp,
826 void *priv, struct v4l2_control *c)
828 struct stk_camera *dev = priv;
830 case V4L2_CID_BRIGHTNESS:
831 c->value = dev->vsettings.brightness;
834 if (dmi_check_system(stk_upside_down_dmi_table))
835 c->value = !dev->vsettings.hflip;
837 c->value = dev->vsettings.hflip;
840 if (dmi_check_system(stk_upside_down_dmi_table))
841 c->value = !dev->vsettings.vflip;
843 c->value = dev->vsettings.vflip;
851 static int stk_vidioc_s_ctrl(struct file *filp,
852 void *priv, struct v4l2_control *c)
854 struct stk_camera *dev = priv;
856 case V4L2_CID_BRIGHTNESS:
857 dev->vsettings.brightness = c->value;
858 return stk_sensor_set_brightness(dev, c->value >> 8);
860 if (dmi_check_system(stk_upside_down_dmi_table))
861 dev->vsettings.hflip = !c->value;
863 dev->vsettings.hflip = c->value;
866 if (dmi_check_system(stk_upside_down_dmi_table))
867 dev->vsettings.vflip = !c->value;
869 dev->vsettings.vflip = c->value;
878 static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
879 void *priv, struct v4l2_fmtdesc *fmtd)
881 switch (fmtd->index) {
883 fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
884 strcpy(fmtd->description, "r5g6b5");
887 fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
888 strcpy(fmtd->description, "r5g6b5BE");
891 fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
892 strcpy(fmtd->description, "yuv4:2:2");
895 fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
896 strcpy(fmtd->description, "Raw bayer");
899 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
900 strcpy(fmtd->description, "yuv4:2:2");
908 static struct stk_size {
913 { .w = 1280, .h = 1024, .m = MODE_SXGA, },
914 { .w = 640, .h = 480, .m = MODE_VGA, },
915 { .w = 352, .h = 288, .m = MODE_CIF, },
916 { .w = 320, .h = 240, .m = MODE_QVGA, },
917 { .w = 176, .h = 144, .m = MODE_QCIF, },
920 static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
921 void *priv, struct v4l2_format *f)
923 struct v4l2_pix_format *pix_format = &f->fmt.pix;
924 struct stk_camera *dev = priv;
927 for (i = 0; i < ARRAY_SIZE(stk_sizes) &&
928 stk_sizes[i].m != dev->vsettings.mode; i++)
930 if (i == ARRAY_SIZE(stk_sizes)) {
931 STK_ERROR("ERROR: mode invalid\n");
934 pix_format->width = stk_sizes[i].w;
935 pix_format->height = stk_sizes[i].h;
936 pix_format->field = V4L2_FIELD_NONE;
937 pix_format->colorspace = V4L2_COLORSPACE_SRGB;
938 pix_format->pixelformat = dev->vsettings.palette;
939 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
940 pix_format->bytesperline = pix_format->width;
942 pix_format->bytesperline = 2 * pix_format->width;
943 pix_format->sizeimage = pix_format->bytesperline
944 * pix_format->height;
948 static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
949 void *priv, struct v4l2_format *fmtd)
952 switch (fmtd->fmt.pix.pixelformat) {
953 case V4L2_PIX_FMT_RGB565:
954 case V4L2_PIX_FMT_RGB565X:
955 case V4L2_PIX_FMT_UYVY:
956 case V4L2_PIX_FMT_YUYV:
957 case V4L2_PIX_FMT_SBGGR8:
962 for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
963 if (fmtd->fmt.pix.width > stk_sizes[i].w)
966 if (i == ARRAY_SIZE(stk_sizes)
967 || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
968 < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
969 fmtd->fmt.pix.height = stk_sizes[i-1].h;
970 fmtd->fmt.pix.width = stk_sizes[i-1].w;
971 fmtd->fmt.pix.priv = i - 1;
973 fmtd->fmt.pix.height = stk_sizes[i].h;
974 fmtd->fmt.pix.width = stk_sizes[i].w;
975 fmtd->fmt.pix.priv = i;
978 fmtd->fmt.pix.field = V4L2_FIELD_NONE;
979 fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
980 if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
981 fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
983 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
984 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
985 * fmtd->fmt.pix.height;
989 static int stk_setup_format(struct stk_camera *dev)
993 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
997 while (i < ARRAY_SIZE(stk_sizes) &&
998 stk_sizes[i].m != dev->vsettings.mode)
1000 if (i == ARRAY_SIZE(stk_sizes)) {
1001 STK_ERROR("Something is broken in %s\n", __func__);
1004 /* This registers controls some timings, not sure of what. */
1005 stk_camera_write_reg(dev, 0x001b, 0x0e);
1006 if (dev->vsettings.mode == MODE_SXGA)
1007 stk_camera_write_reg(dev, 0x001c, 0x0e);
1009 stk_camera_write_reg(dev, 0x001c, 0x46);
1011 * Registers 0x0115 0x0114 are the size of each line (bytes),
1012 * regs 0x0117 0x0116 are the heigth of the image.
1014 stk_camera_write_reg(dev, 0x0115,
1015 ((stk_sizes[i].w * depth) >> 8) & 0xff);
1016 stk_camera_write_reg(dev, 0x0114,
1017 (stk_sizes[i].w * depth) & 0xff);
1018 stk_camera_write_reg(dev, 0x0117,
1019 (stk_sizes[i].h >> 8) & 0xff);
1020 stk_camera_write_reg(dev, 0x0116,
1021 stk_sizes[i].h & 0xff);
1022 return stk_sensor_configure(dev);
1025 static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
1026 void *priv, struct v4l2_format *fmtd)
1029 struct stk_camera *dev = priv;
1033 if (!is_present(dev))
1035 if (is_streaming(dev))
1037 if (dev->owner && dev->owner != filp)
1039 ret = stk_vidioc_try_fmt_vid_cap(filp, priv, fmtd);
1044 dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
1045 stk_free_buffers(dev);
1046 dev->frame_size = fmtd->fmt.pix.sizeimage;
1047 dev->vsettings.mode = stk_sizes[fmtd->fmt.pix.priv].m;
1049 stk_initialise(dev);
1050 return stk_setup_format(dev);
1053 static int stk_vidioc_reqbufs(struct file *filp,
1054 void *priv, struct v4l2_requestbuffers *rb)
1056 struct stk_camera *dev = priv;
1060 if (rb->memory != V4L2_MEMORY_MMAP)
1062 if (is_streaming(dev)
1063 || (dev->owner && dev->owner != filp))
1067 /*FIXME If they ask for zero, we must stop streaming and free */
1070 /* Arbitrary limit */
1071 else if (rb->count > 5)
1074 stk_allocate_buffers(dev, rb->count);
1075 rb->count = dev->n_sbufs;
1079 static int stk_vidioc_querybuf(struct file *filp,
1080 void *priv, struct v4l2_buffer *buf)
1082 struct stk_camera *dev = priv;
1083 struct stk_sio_buffer *sbuf;
1085 if (buf->index >= dev->n_sbufs)
1087 sbuf = dev->sio_bufs + buf->index;
1088 *buf = sbuf->v4lbuf;
1092 static int stk_vidioc_qbuf(struct file *filp,
1093 void *priv, struct v4l2_buffer *buf)
1095 struct stk_camera *dev = priv;
1096 struct stk_sio_buffer *sbuf;
1097 unsigned long flags;
1099 if (buf->memory != V4L2_MEMORY_MMAP)
1102 if (buf->index >= dev->n_sbufs)
1104 sbuf = dev->sio_bufs + buf->index;
1105 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
1107 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1108 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1109 spin_lock_irqsave(&dev->spinlock, flags);
1110 list_add_tail(&sbuf->list, &dev->sio_avail);
1111 *buf = sbuf->v4lbuf;
1112 spin_unlock_irqrestore(&dev->spinlock, flags);
1116 static int stk_vidioc_dqbuf(struct file *filp,
1117 void *priv, struct v4l2_buffer *buf)
1119 struct stk_camera *dev = priv;
1120 struct stk_sio_buffer *sbuf;
1121 unsigned long flags;
1124 if (!is_streaming(dev))
1127 if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
1128 return -EWOULDBLOCK;
1129 ret = wait_event_interruptible(dev->wait_frame,
1130 !list_empty(&dev->sio_full) || !is_present(dev));
1133 if (!is_present(dev))
1136 spin_lock_irqsave(&dev->spinlock, flags);
1137 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
1138 list_del_init(&sbuf->list);
1139 spin_unlock_irqrestore(&dev->spinlock, flags);
1140 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1141 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1142 sbuf->v4lbuf.sequence = ++dev->sequence;
1143 v4l2_get_timestamp(&sbuf->v4lbuf.timestamp);
1145 *buf = sbuf->v4lbuf;
1149 static int stk_vidioc_streamon(struct file *filp,
1150 void *priv, enum v4l2_buf_type type)
1152 struct stk_camera *dev = priv;
1153 if (is_streaming(dev))
1155 if (dev->sio_bufs == NULL)
1158 return stk_start_stream(dev);
1161 static int stk_vidioc_streamoff(struct file *filp,
1162 void *priv, enum v4l2_buf_type type)
1164 struct stk_camera *dev = priv;
1165 unsigned long flags;
1167 stk_stop_stream(dev);
1168 spin_lock_irqsave(&dev->spinlock, flags);
1169 INIT_LIST_HEAD(&dev->sio_avail);
1170 INIT_LIST_HEAD(&dev->sio_full);
1171 for (i = 0; i < dev->n_sbufs; i++) {
1172 INIT_LIST_HEAD(&dev->sio_bufs[i].list);
1173 dev->sio_bufs[i].v4lbuf.flags = 0;
1175 spin_unlock_irqrestore(&dev->spinlock, flags);
1180 static int stk_vidioc_g_parm(struct file *filp,
1181 void *priv, struct v4l2_streamparm *sp)
1183 /*FIXME This is not correct */
1184 sp->parm.capture.timeperframe.numerator = 1;
1185 sp->parm.capture.timeperframe.denominator = 30;
1186 sp->parm.capture.readbuffers = 2;
1190 static int stk_vidioc_enum_framesizes(struct file *filp,
1191 void *priv, struct v4l2_frmsizeenum *frms)
1193 if (frms->index >= ARRAY_SIZE(stk_sizes))
1195 switch (frms->pixel_format) {
1196 case V4L2_PIX_FMT_RGB565:
1197 case V4L2_PIX_FMT_RGB565X:
1198 case V4L2_PIX_FMT_UYVY:
1199 case V4L2_PIX_FMT_YUYV:
1200 case V4L2_PIX_FMT_SBGGR8:
1201 frms->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1202 frms->discrete.width = stk_sizes[frms->index].w;
1203 frms->discrete.height = stk_sizes[frms->index].h;
1205 default: return -EINVAL;
1209 static struct v4l2_file_operations v4l_stk_fops = {
1210 .owner = THIS_MODULE,
1211 .open = v4l_stk_open,
1212 .release = v4l_stk_release,
1213 .read = v4l_stk_read,
1214 .poll = v4l_stk_poll,
1215 .mmap = v4l_stk_mmap,
1216 .ioctl = video_ioctl2,
1219 static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops = {
1220 .vidioc_querycap = stk_vidioc_querycap,
1221 .vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
1222 .vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
1223 .vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
1224 .vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
1225 .vidioc_enum_input = stk_vidioc_enum_input,
1226 .vidioc_s_input = stk_vidioc_s_input,
1227 .vidioc_g_input = stk_vidioc_g_input,
1228 .vidioc_s_std = stk_vidioc_s_std,
1229 .vidioc_reqbufs = stk_vidioc_reqbufs,
1230 .vidioc_querybuf = stk_vidioc_querybuf,
1231 .vidioc_qbuf = stk_vidioc_qbuf,
1232 .vidioc_dqbuf = stk_vidioc_dqbuf,
1233 .vidioc_streamon = stk_vidioc_streamon,
1234 .vidioc_streamoff = stk_vidioc_streamoff,
1235 .vidioc_queryctrl = stk_vidioc_queryctrl,
1236 .vidioc_g_ctrl = stk_vidioc_g_ctrl,
1237 .vidioc_s_ctrl = stk_vidioc_s_ctrl,
1238 .vidioc_g_parm = stk_vidioc_g_parm,
1239 .vidioc_enum_framesizes = stk_vidioc_enum_framesizes,
1242 static void stk_v4l_dev_release(struct video_device *vd)
1244 struct stk_camera *dev = vdev_to_camera(vd);
1246 if (dev->sio_bufs != NULL || dev->isobufs != NULL)
1247 STK_ERROR("We are leaking memory\n");
1248 usb_put_intf(dev->interface);
1252 static struct video_device stk_v4l_data = {
1253 .name = "stkwebcam",
1254 .tvnorms = V4L2_STD_UNKNOWN,
1255 .current_norm = V4L2_STD_UNKNOWN,
1256 .fops = &v4l_stk_fops,
1257 .ioctl_ops = &v4l_stk_ioctl_ops,
1258 .release = stk_v4l_dev_release,
1262 static int stk_register_video_device(struct stk_camera *dev)
1266 dev->vdev = stk_v4l_data;
1267 dev->vdev.debug = debug;
1268 dev->vdev.parent = &dev->interface->dev;
1269 err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1271 STK_ERROR("v4l registration failed\n");
1273 STK_INFO("Syntek USB2.0 Camera is now controlling device %s\n",
1274 video_device_node_name(&dev->vdev));
1281 static int stk_camera_probe(struct usb_interface *interface,
1282 const struct usb_device_id *id)
1287 struct stk_camera *dev = NULL;
1288 struct usb_device *udev = interface_to_usbdev(interface);
1289 struct usb_host_interface *iface_desc;
1290 struct usb_endpoint_descriptor *endpoint;
1292 dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
1294 STK_ERROR("Out of memory !\n");
1298 spin_lock_init(&dev->spinlock);
1299 init_waitqueue_head(&dev->wait_frame);
1302 dev->interface = interface;
1303 usb_get_intf(interface);
1306 dev->vsettings.hflip = hflip;
1307 else if (dmi_check_system(stk_upside_down_dmi_table))
1308 dev->vsettings.hflip = 1;
1310 dev->vsettings.hflip = 0;
1312 dev->vsettings.vflip = vflip;
1313 else if (dmi_check_system(stk_upside_down_dmi_table))
1314 dev->vsettings.vflip = 1;
1316 dev->vsettings.vflip = 0;
1320 /* Set up the endpoint information
1321 * use only the first isoc-in endpoint
1322 * for the current alternate setting */
1323 iface_desc = interface->cur_altsetting;
1325 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1326 endpoint = &iface_desc->endpoint[i].desc;
1329 && usb_endpoint_is_isoc_in(endpoint)) {
1330 /* we found an isoc in endpoint */
1331 dev->isoc_ep = usb_endpoint_num(endpoint);
1335 if (!dev->isoc_ep) {
1336 STK_ERROR("Could not find isoc-in endpoint");
1340 dev->vsettings.brightness = 0x7fff;
1341 dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
1342 dev->vsettings.mode = MODE_VGA;
1343 dev->frame_size = 640 * 480 * 2;
1345 INIT_LIST_HEAD(&dev->sio_avail);
1346 INIT_LIST_HEAD(&dev->sio_full);
1348 usb_set_intfdata(interface, dev);
1350 err = stk_register_video_device(dev);
1361 static void stk_camera_disconnect(struct usb_interface *interface)
1363 struct stk_camera *dev = usb_get_intfdata(interface);
1365 usb_set_intfdata(interface, NULL);
1368 wake_up_interruptible(&dev->wait_frame);
1370 STK_INFO("Syntek USB2.0 Camera release resources device %s\n",
1371 video_device_node_name(&dev->vdev));
1373 video_unregister_device(&dev->vdev);
1377 static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
1379 struct stk_camera *dev = usb_get_intfdata(intf);
1380 if (is_streaming(dev)) {
1381 stk_stop_stream(dev);
1382 /* yes, this is ugly */
1388 static int stk_camera_resume(struct usb_interface *intf)
1390 struct stk_camera *dev = usb_get_intfdata(intf);
1391 if (!is_initialised(dev))
1393 unset_initialised(dev);
1394 stk_initialise(dev);
1395 stk_camera_write_reg(dev, 0x0, 0x49);
1396 stk_setup_format(dev);
1397 if (is_streaming(dev))
1398 stk_start_stream(dev);
1403 static struct usb_driver stk_camera_driver = {
1404 .name = "stkwebcam",
1405 .probe = stk_camera_probe,
1406 .disconnect = stk_camera_disconnect,
1407 .id_table = stkwebcam_table,
1409 .suspend = stk_camera_suspend,
1410 .resume = stk_camera_resume,
1414 module_usb_driver(stk_camera_driver);