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);
67 * The stk webcam laptop module is mounted upside down in some laptops :(
69 * Some background information (thanks to Hans de Goede for providing this):
71 * 1) Once upon a time the stkwebcam driver was written
73 * 2) The webcam in question was used mostly in Asus laptop models, including
74 * the laptop of the original author of the driver, and in these models, in
75 * typical Asus fashion (see the long long list for uvc cams inside v4l-utils),
76 * they mounted the webcam-module the wrong way up. So the hflip and vflip
77 * module options were given a default value of 1 (the correct value for
78 * upside down mounted models)
80 * 3) Years later I got a bug report from a user with a laptop with stkwebcam,
81 * where the module was actually mounted the right way up, and thus showed
82 * upside down under Linux. So now I was facing the choice of 2 options:
84 * a) Add a not-upside-down list to stkwebcam, which overrules the default.
86 * b) Do it like all the other drivers do, and make the default right for
87 * cams mounted the proper way and add an upside-down model list, with
88 * models where we need to flip-by-default.
90 * Despite knowing that going b) would cause a period of pain where we were
91 * building the table I opted to go for option b), since a) is just too ugly,
92 * and worse different from how every other driver does it leading to
93 * confusion in the long run. This change was made in kernel 3.6.
95 * So for any user report about upside-down images since kernel 3.6 ask them
96 * to provide the output of 'sudo dmidecode' so the laptop can be added in
99 static const struct dmi_system_id stk_upside_down_dmi_table[] = {
103 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
104 DMI_MATCH(DMI_PRODUCT_NAME, "G1")
107 .ident = "ASUS F3JC",
109 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
110 DMI_MATCH(DMI_PRODUCT_NAME, "F3JC")
120 int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
122 struct usb_device *udev = dev->udev;
125 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
127 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
139 int stk_camera_read_reg(struct stk_camera *dev, u16 index, int *value)
141 struct usb_device *udev = dev->udev;
144 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
146 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
158 static int stk_start_stream(struct stk_camera *dev)
162 int value_116, value_117;
164 if (!is_present(dev))
166 if (!is_memallocd(dev) || !is_initialised(dev)) {
167 STK_ERROR("FIXME: Buffers are not allocated\n");
170 ret = usb_set_interface(dev->udev, 0, 5);
173 STK_ERROR("usb_set_interface failed !\n");
174 if (stk_sensor_wakeup(dev))
175 STK_ERROR("error awaking the sensor\n");
177 stk_camera_read_reg(dev, 0x0116, &value_116);
178 stk_camera_read_reg(dev, 0x0117, &value_117);
180 stk_camera_write_reg(dev, 0x0116, 0x0000);
181 stk_camera_write_reg(dev, 0x0117, 0x0000);
183 stk_camera_read_reg(dev, 0x0100, &value);
184 stk_camera_write_reg(dev, 0x0100, value | 0x80);
186 stk_camera_write_reg(dev, 0x0116, value_116);
187 stk_camera_write_reg(dev, 0x0117, value_117);
188 for (i = 0; i < MAX_ISO_BUFS; i++) {
189 if (dev->isobufs[i].urb) {
190 ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL);
191 atomic_inc(&dev->urbs_used);
200 static int stk_stop_stream(struct stk_camera *dev)
204 if (is_present(dev)) {
205 stk_camera_read_reg(dev, 0x0100, &value);
206 stk_camera_write_reg(dev, 0x0100, value & ~0x80);
207 if (dev->isobufs != NULL) {
208 for (i = 0; i < MAX_ISO_BUFS; i++) {
209 if (dev->isobufs[i].urb)
210 usb_kill_urb(dev->isobufs[i].urb);
213 unset_streaming(dev);
215 if (usb_set_interface(dev->udev, 0, 0))
216 STK_ERROR("usb_set_interface failed !\n");
217 if (stk_sensor_sleep(dev))
218 STK_ERROR("error suspending the sensor\n");
224 * This seems to be the shortest init sequence we
225 * must do in order to find the sensor
226 * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
227 * is also reset. Maybe powers down it?
228 * Rest of values don't make a difference
231 static struct regval stk1125_initvals[] = {
232 /*TODO: What means this sequence? */
261 static int stk_initialise(struct stk_camera *dev)
265 if (!is_present(dev))
267 if (is_initialised(dev))
269 rv = stk1125_initvals;
270 while (rv->reg != 0xffff) {
271 ret = stk_camera_write_reg(dev, rv->reg, rv->val);
276 if (stk_sensor_init(dev) == 0) {
277 set_initialised(dev);
283 /* *********************************************** */
285 * This function is called as an URB transfert is complete (Isochronous pipe).
286 * So, the traitement is done in interrupt time, so it has be fast, not crash,
287 * and not stall. Neat.
289 static void stk_isoc_handler(struct urb *urb)
296 unsigned char *fill = NULL;
297 unsigned char *iso_buf = NULL;
299 struct stk_camera *dev;
300 struct stk_sio_buffer *fb;
302 dev = (struct stk_camera *) urb->context;
305 STK_ERROR("isoc_handler called with NULL device !\n");
309 if (urb->status == -ENOENT || urb->status == -ECONNRESET
310 || urb->status == -ESHUTDOWN) {
311 atomic_dec(&dev->urbs_used);
315 spin_lock_irqsave(&dev->spinlock, flags);
317 if (urb->status != -EINPROGRESS && urb->status != 0) {
318 STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
322 if (list_empty(&dev->sio_avail)) {
323 /*FIXME Stop streaming after a while */
324 (void) (printk_ratelimit() &&
325 STK_ERROR("isoc_handler without available buffer!\n"));
328 fb = list_first_entry(&dev->sio_avail,
329 struct stk_sio_buffer, list);
330 fill = fb->buffer + fb->v4lbuf.bytesused;
332 for (i = 0; i < urb->number_of_packets; i++) {
333 if (urb->iso_frame_desc[i].status != 0) {
334 if (urb->iso_frame_desc[i].status != -EXDEV)
335 STK_ERROR("Frame %d has error %d\n", i,
336 urb->iso_frame_desc[i].status);
339 framelen = urb->iso_frame_desc[i].actual_length;
340 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
343 continue; /* no data */
346 * we found something informational from there
347 * the isoc frames have to type of headers
348 * type1: 00 xx 00 00 or 20 xx 00 00
349 * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
350 * xx is a sequencer which has never been seen over 0x3f
351 * imho data written down looks like bayer, i see similarities
352 * after every 640 bytes
354 if (*iso_buf & 0x80) {
357 /* This marks a new frame */
358 if (fb->v4lbuf.bytesused != 0
359 && fb->v4lbuf.bytesused != dev->frame_size) {
360 (void) (printk_ratelimit() &&
361 STK_ERROR("frame %d, "
362 "bytesused=%d, skipping\n",
363 i, fb->v4lbuf.bytesused));
364 fb->v4lbuf.bytesused = 0;
366 } else if (fb->v4lbuf.bytesused == dev->frame_size) {
367 if (list_is_singular(&dev->sio_avail)) {
368 /* Always reuse the last buffer */
369 fb->v4lbuf.bytesused = 0;
372 list_move_tail(dev->sio_avail.next,
374 wake_up(&dev->wait_frame);
375 fb = list_first_entry(&dev->sio_avail,
376 struct stk_sio_buffer, list);
377 fb->v4lbuf.bytesused = 0;
386 /* Our buffer is full !!! */
387 if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
388 (void) (printk_ratelimit() &&
389 STK_ERROR("Frame buffer overflow, lost sync\n"));
390 /*FIXME Do something here? */
393 spin_unlock_irqrestore(&dev->spinlock, flags);
394 memcpy(fill, iso_buf, framelen);
395 spin_lock_irqsave(&dev->spinlock, flags);
398 /* New size of our buffer */
399 fb->v4lbuf.bytesused += framelen;
403 spin_unlock_irqrestore(&dev->spinlock, flags);
404 urb->dev = dev->udev;
405 ret = usb_submit_urb(urb, GFP_ATOMIC);
407 STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
412 /* -------------------------------------------- */
414 static int stk_prepare_iso(struct stk_camera *dev)
419 struct usb_device *udev;
426 STK_ERROR("isobufs already allocated. Bad\n");
428 dev->isobufs = kcalloc(MAX_ISO_BUFS, sizeof(*dev->isobufs),
430 if (dev->isobufs == NULL) {
431 STK_ERROR("Unable to allocate iso buffers\n");
434 for (i = 0; i < MAX_ISO_BUFS; i++) {
435 if (dev->isobufs[i].data == NULL) {
436 kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
438 STK_ERROR("Failed to allocate iso buffer %d\n",
442 dev->isobufs[i].data = kbuf;
444 STK_ERROR("isobuf data already allocated\n");
445 if (dev->isobufs[i].urb == NULL) {
446 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
448 STK_ERROR("Failed to allocate URB %d\n", i);
451 dev->isobufs[i].urb = urb;
453 STK_ERROR("Killing URB\n");
454 usb_kill_urb(dev->isobufs[i].urb);
455 urb = dev->isobufs[i].urb;
459 urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
460 urb->transfer_flags = URB_ISO_ASAP;
461 urb->transfer_buffer = dev->isobufs[i].data;
462 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
463 urb->complete = stk_isoc_handler;
465 urb->start_frame = 0;
466 urb->number_of_packets = ISO_FRAMES_PER_DESC;
468 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
469 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
470 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
477 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
478 kfree(dev->isobufs[i].data);
479 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
480 usb_free_urb(dev->isobufs[i].urb);
486 static void stk_clean_iso(struct stk_camera *dev)
490 if (dev == NULL || dev->isobufs == NULL)
493 for (i = 0; i < MAX_ISO_BUFS; i++) {
496 urb = dev->isobufs[i].urb;
498 if (atomic_read(&dev->urbs_used) && is_present(dev))
502 kfree(dev->isobufs[i].data);
506 unset_memallocd(dev);
509 static int stk_setup_siobuf(struct stk_camera *dev, int index)
511 struct stk_sio_buffer *buf = dev->sio_bufs + index;
512 INIT_LIST_HEAD(&buf->list);
513 buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
514 buf->buffer = vmalloc_user(buf->v4lbuf.length);
515 if (buf->buffer == NULL)
519 buf->v4lbuf.index = index;
520 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
521 buf->v4lbuf.flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
522 buf->v4lbuf.field = V4L2_FIELD_NONE;
523 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
524 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
528 static int stk_free_sio_buffers(struct stk_camera *dev)
533 if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
536 * If any buffers are mapped, we cannot free them at all.
538 for (i = 0; i < dev->n_sbufs; i++) {
539 if (dev->sio_bufs[i].mapcount > 0)
545 spin_lock_irqsave(&dev->spinlock, flags);
546 INIT_LIST_HEAD(&dev->sio_avail);
547 INIT_LIST_HEAD(&dev->sio_full);
548 nbufs = dev->n_sbufs;
550 spin_unlock_irqrestore(&dev->spinlock, flags);
551 for (i = 0; i < nbufs; i++) {
552 if (dev->sio_bufs[i].buffer != NULL)
553 vfree(dev->sio_bufs[i].buffer);
555 kfree(dev->sio_bufs);
556 dev->sio_bufs = NULL;
560 static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
563 if (dev->sio_bufs != NULL)
564 STK_ERROR("sio_bufs already allocated\n");
566 dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
568 if (dev->sio_bufs == NULL)
570 for (i = 0; i < n_sbufs; i++) {
571 if (stk_setup_siobuf(dev, i))
572 return (dev->n_sbufs > 1 ? 0 : -ENOMEM);
579 static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
582 err = stk_prepare_iso(dev);
587 err = stk_prepare_sio_buffers(dev, n_sbufs);
589 stk_free_sio_buffers(dev);
595 static void stk_free_buffers(struct stk_camera *dev)
598 stk_free_sio_buffers(dev);
600 /* -------------------------------------------- */
602 /* v4l file operations */
604 static int v4l_stk_open(struct file *fp)
606 static int first_init = 1; /* webcam LED management */
607 struct stk_camera *dev;
608 struct video_device *vdev;
610 vdev = video_devdata(fp);
611 dev = vdev_to_camera(vdev);
613 if (dev == NULL || !is_present(dev))
617 stk_camera_write_reg(dev, 0x0, 0x24);
621 fp->private_data = dev;
622 usb_autopm_get_interface(dev->interface);
627 static int v4l_stk_release(struct file *fp)
629 struct stk_camera *dev = fp->private_data;
631 if (dev->owner == fp) {
632 stk_stop_stream(dev);
633 stk_free_buffers(dev);
634 stk_camera_write_reg(dev, 0x0, 0x49); /* turn off the LED */
635 unset_initialised(dev);
640 usb_autopm_put_interface(dev->interface);
645 static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
646 size_t count, loff_t *f_pos)
651 struct stk_sio_buffer *sbuf;
652 struct stk_camera *dev = fp->private_data;
654 if (!is_present(dev))
656 if (dev->owner && dev->owner != fp)
659 if (!is_streaming(dev)) {
660 if (stk_initialise(dev)
661 || stk_allocate_buffers(dev, 3)
662 || stk_start_stream(dev))
664 spin_lock_irqsave(&dev->spinlock, flags);
665 for (i = 0; i < dev->n_sbufs; i++) {
666 list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
667 dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
669 spin_unlock_irqrestore(&dev->spinlock, flags);
672 if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
674 ret = wait_event_interruptible(dev->wait_frame,
675 !list_empty(&dev->sio_full) || !is_present(dev));
678 if (!is_present(dev))
681 if (count + *f_pos > dev->frame_size)
682 count = dev->frame_size - *f_pos;
683 spin_lock_irqsave(&dev->spinlock, flags);
684 if (list_empty(&dev->sio_full)) {
685 spin_unlock_irqrestore(&dev->spinlock, flags);
686 STK_ERROR("BUG: No siobufs ready\n");
689 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
690 spin_unlock_irqrestore(&dev->spinlock, flags);
692 if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
697 if (*f_pos >= dev->frame_size) {
699 spin_lock_irqsave(&dev->spinlock, flags);
700 list_move_tail(&sbuf->list, &dev->sio_avail);
701 spin_unlock_irqrestore(&dev->spinlock, flags);
706 static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
708 struct stk_camera *dev = fp->private_data;
710 poll_wait(fp, &dev->wait_frame, wait);
712 if (!is_present(dev))
715 if (!list_empty(&dev->sio_full))
716 return POLLIN | POLLRDNORM;
722 static void stk_v4l_vm_open(struct vm_area_struct *vma)
724 struct stk_sio_buffer *sbuf = vma->vm_private_data;
727 static void stk_v4l_vm_close(struct vm_area_struct *vma)
729 struct stk_sio_buffer *sbuf = vma->vm_private_data;
731 if (sbuf->mapcount == 0)
732 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
734 static const struct vm_operations_struct stk_v4l_vm_ops = {
735 .open = stk_v4l_vm_open,
736 .close = stk_v4l_vm_close
739 static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
743 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
744 struct stk_camera *dev = fp->private_data;
745 struct stk_sio_buffer *sbuf = NULL;
747 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
750 for (i = 0; i < dev->n_sbufs; i++) {
751 if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
752 sbuf = dev->sio_bufs + i;
758 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
761 vma->vm_flags |= VM_DONTEXPAND;
762 vma->vm_private_data = sbuf;
763 vma->vm_ops = &stk_v4l_vm_ops;
764 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
765 stk_v4l_vm_open(vma);
769 /* v4l ioctl handlers */
771 static int stk_vidioc_querycap(struct file *filp,
772 void *priv, struct v4l2_capability *cap)
774 strcpy(cap->driver, "stk");
775 strcpy(cap->card, "stk");
776 cap->version = DRIVER_VERSION_NUM;
778 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
779 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
783 static int stk_vidioc_enum_input(struct file *filp,
784 void *priv, struct v4l2_input *input)
786 if (input->index != 0)
789 strcpy(input->name, "Syntek USB Camera");
790 input->type = V4L2_INPUT_TYPE_CAMERA;
795 static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
801 static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
809 static int stk_s_ctrl(struct v4l2_ctrl *ctrl)
811 struct stk_camera *dev =
812 container_of(ctrl->handler, struct stk_camera, hdl);
815 case V4L2_CID_BRIGHTNESS:
816 return stk_sensor_set_brightness(dev, ctrl->val);
818 if (dmi_check_system(stk_upside_down_dmi_table))
819 dev->vsettings.hflip = !ctrl->val;
821 dev->vsettings.hflip = ctrl->val;
824 if (dmi_check_system(stk_upside_down_dmi_table))
825 dev->vsettings.vflip = !ctrl->val;
827 dev->vsettings.vflip = ctrl->val;
836 static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
837 void *priv, struct v4l2_fmtdesc *fmtd)
839 switch (fmtd->index) {
841 fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
842 strcpy(fmtd->description, "r5g6b5");
845 fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
846 strcpy(fmtd->description, "r5g6b5BE");
849 fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
850 strcpy(fmtd->description, "yuv4:2:2");
853 fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
854 strcpy(fmtd->description, "Raw bayer");
857 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
858 strcpy(fmtd->description, "yuv4:2:2");
866 static struct stk_size {
871 { .w = 1280, .h = 1024, .m = MODE_SXGA, },
872 { .w = 640, .h = 480, .m = MODE_VGA, },
873 { .w = 352, .h = 288, .m = MODE_CIF, },
874 { .w = 320, .h = 240, .m = MODE_QVGA, },
875 { .w = 176, .h = 144, .m = MODE_QCIF, },
878 static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
879 void *priv, struct v4l2_format *f)
881 struct v4l2_pix_format *pix_format = &f->fmt.pix;
882 struct stk_camera *dev = priv;
885 for (i = 0; i < ARRAY_SIZE(stk_sizes) &&
886 stk_sizes[i].m != dev->vsettings.mode; i++)
888 if (i == ARRAY_SIZE(stk_sizes)) {
889 STK_ERROR("ERROR: mode invalid\n");
892 pix_format->width = stk_sizes[i].w;
893 pix_format->height = stk_sizes[i].h;
894 pix_format->field = V4L2_FIELD_NONE;
895 pix_format->colorspace = V4L2_COLORSPACE_SRGB;
896 pix_format->pixelformat = dev->vsettings.palette;
897 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
898 pix_format->bytesperline = pix_format->width;
900 pix_format->bytesperline = 2 * pix_format->width;
901 pix_format->sizeimage = pix_format->bytesperline
902 * pix_format->height;
906 static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
907 void *priv, struct v4l2_format *fmtd)
910 switch (fmtd->fmt.pix.pixelformat) {
911 case V4L2_PIX_FMT_RGB565:
912 case V4L2_PIX_FMT_RGB565X:
913 case V4L2_PIX_FMT_UYVY:
914 case V4L2_PIX_FMT_YUYV:
915 case V4L2_PIX_FMT_SBGGR8:
920 for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
921 if (fmtd->fmt.pix.width > stk_sizes[i].w)
924 if (i == ARRAY_SIZE(stk_sizes)
925 || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
926 < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
927 fmtd->fmt.pix.height = stk_sizes[i-1].h;
928 fmtd->fmt.pix.width = stk_sizes[i-1].w;
929 fmtd->fmt.pix.priv = i - 1;
931 fmtd->fmt.pix.height = stk_sizes[i].h;
932 fmtd->fmt.pix.width = stk_sizes[i].w;
933 fmtd->fmt.pix.priv = i;
936 fmtd->fmt.pix.field = V4L2_FIELD_NONE;
937 fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
938 if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
939 fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
941 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
942 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
943 * fmtd->fmt.pix.height;
947 static int stk_setup_format(struct stk_camera *dev)
951 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
955 while (i < ARRAY_SIZE(stk_sizes) &&
956 stk_sizes[i].m != dev->vsettings.mode)
958 if (i == ARRAY_SIZE(stk_sizes)) {
959 STK_ERROR("Something is broken in %s\n", __func__);
962 /* This registers controls some timings, not sure of what. */
963 stk_camera_write_reg(dev, 0x001b, 0x0e);
964 if (dev->vsettings.mode == MODE_SXGA)
965 stk_camera_write_reg(dev, 0x001c, 0x0e);
967 stk_camera_write_reg(dev, 0x001c, 0x46);
969 * Registers 0x0115 0x0114 are the size of each line (bytes),
970 * regs 0x0117 0x0116 are the heigth of the image.
972 stk_camera_write_reg(dev, 0x0115,
973 ((stk_sizes[i].w * depth) >> 8) & 0xff);
974 stk_camera_write_reg(dev, 0x0114,
975 (stk_sizes[i].w * depth) & 0xff);
976 stk_camera_write_reg(dev, 0x0117,
977 (stk_sizes[i].h >> 8) & 0xff);
978 stk_camera_write_reg(dev, 0x0116,
979 stk_sizes[i].h & 0xff);
980 return stk_sensor_configure(dev);
983 static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
984 void *priv, struct v4l2_format *fmtd)
987 struct stk_camera *dev = priv;
991 if (!is_present(dev))
993 if (is_streaming(dev))
995 if (dev->owner && dev->owner != filp)
997 ret = stk_vidioc_try_fmt_vid_cap(filp, priv, fmtd);
1002 dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
1003 stk_free_buffers(dev);
1004 dev->frame_size = fmtd->fmt.pix.sizeimage;
1005 dev->vsettings.mode = stk_sizes[fmtd->fmt.pix.priv].m;
1007 stk_initialise(dev);
1008 return stk_setup_format(dev);
1011 static int stk_vidioc_reqbufs(struct file *filp,
1012 void *priv, struct v4l2_requestbuffers *rb)
1014 struct stk_camera *dev = priv;
1018 if (rb->memory != V4L2_MEMORY_MMAP)
1020 if (is_streaming(dev)
1021 || (dev->owner && dev->owner != filp))
1025 /*FIXME If they ask for zero, we must stop streaming and free */
1028 /* Arbitrary limit */
1029 else if (rb->count > 5)
1032 stk_allocate_buffers(dev, rb->count);
1033 rb->count = dev->n_sbufs;
1037 static int stk_vidioc_querybuf(struct file *filp,
1038 void *priv, struct v4l2_buffer *buf)
1040 struct stk_camera *dev = priv;
1041 struct stk_sio_buffer *sbuf;
1043 if (buf->index >= dev->n_sbufs)
1045 sbuf = dev->sio_bufs + buf->index;
1046 *buf = sbuf->v4lbuf;
1050 static int stk_vidioc_qbuf(struct file *filp,
1051 void *priv, struct v4l2_buffer *buf)
1053 struct stk_camera *dev = priv;
1054 struct stk_sio_buffer *sbuf;
1055 unsigned long flags;
1057 if (buf->memory != V4L2_MEMORY_MMAP)
1060 if (buf->index >= dev->n_sbufs)
1062 sbuf = dev->sio_bufs + buf->index;
1063 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
1065 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1066 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1067 spin_lock_irqsave(&dev->spinlock, flags);
1068 list_add_tail(&sbuf->list, &dev->sio_avail);
1069 *buf = sbuf->v4lbuf;
1070 spin_unlock_irqrestore(&dev->spinlock, flags);
1074 static int stk_vidioc_dqbuf(struct file *filp,
1075 void *priv, struct v4l2_buffer *buf)
1077 struct stk_camera *dev = priv;
1078 struct stk_sio_buffer *sbuf;
1079 unsigned long flags;
1082 if (!is_streaming(dev))
1085 if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
1086 return -EWOULDBLOCK;
1087 ret = wait_event_interruptible(dev->wait_frame,
1088 !list_empty(&dev->sio_full) || !is_present(dev));
1091 if (!is_present(dev))
1094 spin_lock_irqsave(&dev->spinlock, flags);
1095 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
1096 list_del_init(&sbuf->list);
1097 spin_unlock_irqrestore(&dev->spinlock, flags);
1098 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1099 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1100 sbuf->v4lbuf.sequence = ++dev->sequence;
1101 v4l2_get_timestamp(&sbuf->v4lbuf.timestamp);
1103 *buf = sbuf->v4lbuf;
1107 static int stk_vidioc_streamon(struct file *filp,
1108 void *priv, enum v4l2_buf_type type)
1110 struct stk_camera *dev = priv;
1111 if (is_streaming(dev))
1113 if (dev->sio_bufs == NULL)
1116 return stk_start_stream(dev);
1119 static int stk_vidioc_streamoff(struct file *filp,
1120 void *priv, enum v4l2_buf_type type)
1122 struct stk_camera *dev = priv;
1123 unsigned long flags;
1125 stk_stop_stream(dev);
1126 spin_lock_irqsave(&dev->spinlock, flags);
1127 INIT_LIST_HEAD(&dev->sio_avail);
1128 INIT_LIST_HEAD(&dev->sio_full);
1129 for (i = 0; i < dev->n_sbufs; i++) {
1130 INIT_LIST_HEAD(&dev->sio_bufs[i].list);
1131 dev->sio_bufs[i].v4lbuf.flags = 0;
1133 spin_unlock_irqrestore(&dev->spinlock, flags);
1138 static int stk_vidioc_g_parm(struct file *filp,
1139 void *priv, struct v4l2_streamparm *sp)
1141 /*FIXME This is not correct */
1142 sp->parm.capture.timeperframe.numerator = 1;
1143 sp->parm.capture.timeperframe.denominator = 30;
1144 sp->parm.capture.readbuffers = 2;
1148 static int stk_vidioc_enum_framesizes(struct file *filp,
1149 void *priv, struct v4l2_frmsizeenum *frms)
1151 if (frms->index >= ARRAY_SIZE(stk_sizes))
1153 switch (frms->pixel_format) {
1154 case V4L2_PIX_FMT_RGB565:
1155 case V4L2_PIX_FMT_RGB565X:
1156 case V4L2_PIX_FMT_UYVY:
1157 case V4L2_PIX_FMT_YUYV:
1158 case V4L2_PIX_FMT_SBGGR8:
1159 frms->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1160 frms->discrete.width = stk_sizes[frms->index].w;
1161 frms->discrete.height = stk_sizes[frms->index].h;
1163 default: return -EINVAL;
1167 static const struct v4l2_ctrl_ops stk_ctrl_ops = {
1168 .s_ctrl = stk_s_ctrl,
1171 static struct v4l2_file_operations v4l_stk_fops = {
1172 .owner = THIS_MODULE,
1173 .open = v4l_stk_open,
1174 .release = v4l_stk_release,
1175 .read = v4l_stk_read,
1176 .poll = v4l_stk_poll,
1177 .mmap = v4l_stk_mmap,
1178 .ioctl = video_ioctl2,
1181 static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops = {
1182 .vidioc_querycap = stk_vidioc_querycap,
1183 .vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
1184 .vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
1185 .vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
1186 .vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
1187 .vidioc_enum_input = stk_vidioc_enum_input,
1188 .vidioc_s_input = stk_vidioc_s_input,
1189 .vidioc_g_input = stk_vidioc_g_input,
1190 .vidioc_reqbufs = stk_vidioc_reqbufs,
1191 .vidioc_querybuf = stk_vidioc_querybuf,
1192 .vidioc_qbuf = stk_vidioc_qbuf,
1193 .vidioc_dqbuf = stk_vidioc_dqbuf,
1194 .vidioc_streamon = stk_vidioc_streamon,
1195 .vidioc_streamoff = stk_vidioc_streamoff,
1196 .vidioc_g_parm = stk_vidioc_g_parm,
1197 .vidioc_enum_framesizes = stk_vidioc_enum_framesizes,
1200 static void stk_v4l_dev_release(struct video_device *vd)
1202 struct stk_camera *dev = vdev_to_camera(vd);
1204 if (dev->sio_bufs != NULL || dev->isobufs != NULL)
1205 STK_ERROR("We are leaking memory\n");
1206 usb_put_intf(dev->interface);
1210 static struct video_device stk_v4l_data = {
1211 .name = "stkwebcam",
1212 .fops = &v4l_stk_fops,
1213 .ioctl_ops = &v4l_stk_ioctl_ops,
1214 .release = stk_v4l_dev_release,
1218 static int stk_register_video_device(struct stk_camera *dev)
1222 dev->vdev = stk_v4l_data;
1223 dev->vdev.debug = debug;
1224 dev->vdev.v4l2_dev = &dev->v4l2_dev;
1225 err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1227 STK_ERROR("v4l registration failed\n");
1229 STK_INFO("Syntek USB2.0 Camera is now controlling device %s\n",
1230 video_device_node_name(&dev->vdev));
1237 static int stk_camera_probe(struct usb_interface *interface,
1238 const struct usb_device_id *id)
1240 struct v4l2_ctrl_handler *hdl;
1244 struct stk_camera *dev = NULL;
1245 struct usb_device *udev = interface_to_usbdev(interface);
1246 struct usb_host_interface *iface_desc;
1247 struct usb_endpoint_descriptor *endpoint;
1249 dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
1251 STK_ERROR("Out of memory !\n");
1254 err = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
1256 dev_err(&udev->dev, "couldn't register v4l2_device\n");
1261 v4l2_ctrl_handler_init(hdl, 3);
1262 v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
1263 V4L2_CID_BRIGHTNESS, 0, 0xff, 0x1, 0x60);
1264 v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
1265 V4L2_CID_HFLIP, 0, 1, 1, 1);
1266 v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
1267 V4L2_CID_VFLIP, 0, 1, 1, 1);
1270 dev_err(&udev->dev, "couldn't register control\n");
1273 dev->v4l2_dev.ctrl_handler = hdl;
1275 spin_lock_init(&dev->spinlock);
1276 init_waitqueue_head(&dev->wait_frame);
1279 dev->interface = interface;
1280 usb_get_intf(interface);
1283 dev->vsettings.hflip = hflip;
1284 else if (dmi_check_system(stk_upside_down_dmi_table))
1285 dev->vsettings.hflip = 1;
1287 dev->vsettings.hflip = 0;
1289 dev->vsettings.vflip = vflip;
1290 else if (dmi_check_system(stk_upside_down_dmi_table))
1291 dev->vsettings.vflip = 1;
1293 dev->vsettings.vflip = 0;
1297 /* Set up the endpoint information
1298 * use only the first isoc-in endpoint
1299 * for the current alternate setting */
1300 iface_desc = interface->cur_altsetting;
1302 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1303 endpoint = &iface_desc->endpoint[i].desc;
1306 && usb_endpoint_is_isoc_in(endpoint)) {
1307 /* we found an isoc in endpoint */
1308 dev->isoc_ep = usb_endpoint_num(endpoint);
1312 if (!dev->isoc_ep) {
1313 STK_ERROR("Could not find isoc-in endpoint");
1317 dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
1318 dev->vsettings.mode = MODE_VGA;
1319 dev->frame_size = 640 * 480 * 2;
1321 INIT_LIST_HEAD(&dev->sio_avail);
1322 INIT_LIST_HEAD(&dev->sio_full);
1324 usb_set_intfdata(interface, dev);
1326 err = stk_register_video_device(dev);
1333 v4l2_ctrl_handler_free(hdl);
1334 v4l2_device_unregister(&dev->v4l2_dev);
1339 static void stk_camera_disconnect(struct usb_interface *interface)
1341 struct stk_camera *dev = usb_get_intfdata(interface);
1343 usb_set_intfdata(interface, NULL);
1346 wake_up_interruptible(&dev->wait_frame);
1348 STK_INFO("Syntek USB2.0 Camera release resources device %s\n",
1349 video_device_node_name(&dev->vdev));
1351 video_unregister_device(&dev->vdev);
1352 v4l2_ctrl_handler_free(&dev->hdl);
1353 v4l2_device_unregister(&dev->v4l2_dev);
1357 static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
1359 struct stk_camera *dev = usb_get_intfdata(intf);
1360 if (is_streaming(dev)) {
1361 stk_stop_stream(dev);
1362 /* yes, this is ugly */
1368 static int stk_camera_resume(struct usb_interface *intf)
1370 struct stk_camera *dev = usb_get_intfdata(intf);
1371 if (!is_initialised(dev))
1373 unset_initialised(dev);
1374 stk_initialise(dev);
1375 stk_camera_write_reg(dev, 0x0, 0x49);
1376 stk_setup_format(dev);
1377 if (is_streaming(dev))
1378 stk_start_stream(dev);
1383 static struct usb_driver stk_camera_driver = {
1384 .name = "stkwebcam",
1385 .probe = stk_camera_probe,
1386 .disconnect = stk_camera_disconnect,
1387 .id_table = stkwebcam_table,
1389 .suspend = stk_camera_suspend,
1390 .resume = stk_camera_resume,
1394 module_usb_driver(stk_camera_driver);