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.
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/errno.h>
27 #include <linux/slab.h>
29 #include <linux/dmi.h>
30 #include <linux/usb.h>
32 #include <linux/vmalloc.h>
33 #include <linux/videodev2.h>
34 #include <media/v4l2-common.h>
35 #include <media/v4l2-ioctl.h>
36 #include <media/v4l2-event.h>
38 #include "stk-webcam.h"
41 static int hflip
= -1;
42 module_param(hflip
, int, 0444);
43 MODULE_PARM_DESC(hflip
, "Horizontal image flip (mirror). Defaults to 0");
45 static int vflip
= -1;
46 module_param(vflip
, int, 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 const 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
);
66 * The stk webcam laptop module is mounted upside down in some laptops :(
68 * Some background information (thanks to Hans de Goede for providing this):
70 * 1) Once upon a time the stkwebcam driver was written
72 * 2) The webcam in question was used mostly in Asus laptop models, including
73 * the laptop of the original author of the driver, and in these models, in
74 * typical Asus fashion (see the long long list for uvc cams inside v4l-utils),
75 * they mounted the webcam-module the wrong way up. So the hflip and vflip
76 * module options were given a default value of 1 (the correct value for
77 * upside down mounted models)
79 * 3) Years later I got a bug report from a user with a laptop with stkwebcam,
80 * where the module was actually mounted the right way up, and thus showed
81 * upside down under Linux. So now I was facing the choice of 2 options:
83 * a) Add a not-upside-down list to stkwebcam, which overrules the default.
85 * b) Do it like all the other drivers do, and make the default right for
86 * cams mounted the proper way and add an upside-down model list, with
87 * models where we need to flip-by-default.
89 * Despite knowing that going b) would cause a period of pain where we were
90 * building the table I opted to go for option b), since a) is just too ugly,
91 * and worse different from how every other driver does it leading to
92 * confusion in the long run. This change was made in kernel 3.6.
94 * So for any user report about upside-down images since kernel 3.6 ask them
95 * to provide the output of 'sudo dmidecode' so the laptop can be added in
98 static const struct dmi_system_id stk_upside_down_dmi_table
[] = {
102 DMI_MATCH(DMI_SYS_VENDOR
, "ASUSTeK Computer Inc."),
103 DMI_MATCH(DMI_PRODUCT_NAME
, "G1")
106 .ident
= "ASUS F3JC",
108 DMI_MATCH(DMI_SYS_VENDOR
, "ASUSTeK Computer Inc."),
109 DMI_MATCH(DMI_PRODUCT_NAME
, "F3JC")
115 DMI_MATCH(DMI_SYS_VENDOR
, "HCL Infosystems Limited"),
116 DMI_MATCH(DMI_PRODUCT_NAME
, "T12Rg-H")
126 int stk_camera_write_reg(struct stk_camera
*dev
, u16 index
, u8 value
)
128 struct usb_device
*udev
= dev
->udev
;
131 ret
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
133 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
145 int stk_camera_read_reg(struct stk_camera
*dev
, u16 index
, u8
*value
)
147 struct usb_device
*udev
= dev
->udev
;
151 buf
= kmalloc(sizeof(u8
), GFP_KERNEL
);
155 ret
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
157 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
170 static int stk_start_stream(struct stk_camera
*dev
)
174 u8 value_116
, value_117
;
177 if (!is_present(dev
))
179 if (!is_memallocd(dev
) || !is_initialised(dev
)) {
180 pr_err("FIXME: Buffers are not allocated\n");
183 ret
= usb_set_interface(dev
->udev
, 0, 5);
186 pr_err("usb_set_interface failed !\n");
187 if (stk_sensor_wakeup(dev
))
188 pr_err("error awaking the sensor\n");
190 stk_camera_read_reg(dev
, 0x0116, &value_116
);
191 stk_camera_read_reg(dev
, 0x0117, &value_117
);
193 stk_camera_write_reg(dev
, 0x0116, 0x0000);
194 stk_camera_write_reg(dev
, 0x0117, 0x0000);
196 stk_camera_read_reg(dev
, 0x0100, &value
);
197 stk_camera_write_reg(dev
, 0x0100, value
| 0x80);
199 stk_camera_write_reg(dev
, 0x0116, value_116
);
200 stk_camera_write_reg(dev
, 0x0117, value_117
);
201 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
202 if (dev
->isobufs
[i
].urb
) {
203 ret
= usb_submit_urb(dev
->isobufs
[i
].urb
, GFP_KERNEL
);
204 atomic_inc(&dev
->urbs_used
);
213 static int stk_stop_stream(struct stk_camera
*dev
)
217 if (is_present(dev
)) {
218 stk_camera_read_reg(dev
, 0x0100, &value
);
219 stk_camera_write_reg(dev
, 0x0100, value
& ~0x80);
220 if (dev
->isobufs
!= NULL
) {
221 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
222 if (dev
->isobufs
[i
].urb
)
223 usb_kill_urb(dev
->isobufs
[i
].urb
);
226 unset_streaming(dev
);
228 if (usb_set_interface(dev
->udev
, 0, 0))
229 pr_err("usb_set_interface failed !\n");
230 if (stk_sensor_sleep(dev
))
231 pr_err("error suspending the sensor\n");
237 * This seems to be the shortest init sequence we
238 * must do in order to find the sensor
239 * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
240 * is also reset. Maybe powers down it?
241 * Rest of values don't make a difference
244 static struct regval stk1125_initvals
[] = {
245 /*TODO: What means this sequence? */
274 static int stk_initialise(struct stk_camera
*dev
)
278 if (!is_present(dev
))
280 if (is_initialised(dev
))
282 rv
= stk1125_initvals
;
283 while (rv
->reg
!= 0xffff) {
284 ret
= stk_camera_write_reg(dev
, rv
->reg
, rv
->val
);
289 if (stk_sensor_init(dev
) == 0) {
290 set_initialised(dev
);
296 /* *********************************************** */
298 * This function is called as an URB transfert is complete (Isochronous pipe).
299 * So, the traitement is done in interrupt time, so it has be fast, not crash,
300 * and not stall. Neat.
302 static void stk_isoc_handler(struct urb
*urb
)
309 unsigned char *fill
= NULL
;
310 unsigned char *iso_buf
= NULL
;
312 struct stk_camera
*dev
;
313 struct stk_sio_buffer
*fb
;
315 dev
= (struct stk_camera
*) urb
->context
;
318 pr_err("isoc_handler called with NULL device !\n");
322 if (urb
->status
== -ENOENT
|| urb
->status
== -ECONNRESET
323 || urb
->status
== -ESHUTDOWN
) {
324 atomic_dec(&dev
->urbs_used
);
328 spin_lock_irqsave(&dev
->spinlock
, flags
);
330 if (urb
->status
!= -EINPROGRESS
&& urb
->status
!= 0) {
331 pr_err("isoc_handler: urb->status == %d\n", urb
->status
);
335 if (list_empty(&dev
->sio_avail
)) {
336 /*FIXME Stop streaming after a while */
337 pr_err_ratelimited("isoc_handler without available buffer!\n");
340 fb
= list_first_entry(&dev
->sio_avail
,
341 struct stk_sio_buffer
, list
);
342 fill
= fb
->buffer
+ fb
->v4lbuf
.bytesused
;
344 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
345 if (urb
->iso_frame_desc
[i
].status
!= 0) {
346 if (urb
->iso_frame_desc
[i
].status
!= -EXDEV
)
347 pr_err("Frame %d has error %d\n",
348 i
, urb
->iso_frame_desc
[i
].status
);
351 framelen
= urb
->iso_frame_desc
[i
].actual_length
;
352 iso_buf
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
355 continue; /* no data */
358 * we found something informational from there
359 * the isoc frames have to type of headers
360 * type1: 00 xx 00 00 or 20 xx 00 00
361 * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
362 * xx is a sequencer which has never been seen over 0x3f
363 * imho data written down looks like bayer, i see similarities
364 * after every 640 bytes
366 if (*iso_buf
& 0x80) {
369 /* This marks a new frame */
370 if (fb
->v4lbuf
.bytesused
!= 0
371 && fb
->v4lbuf
.bytesused
!= dev
->frame_size
) {
372 pr_err_ratelimited("frame %d, bytesused=%d, skipping\n",
373 i
, fb
->v4lbuf
.bytesused
);
374 fb
->v4lbuf
.bytesused
= 0;
376 } else if (fb
->v4lbuf
.bytesused
== dev
->frame_size
) {
377 if (list_is_singular(&dev
->sio_avail
)) {
378 /* Always reuse the last buffer */
379 fb
->v4lbuf
.bytesused
= 0;
382 list_move_tail(dev
->sio_avail
.next
,
384 wake_up(&dev
->wait_frame
);
385 fb
= list_first_entry(&dev
->sio_avail
,
386 struct stk_sio_buffer
, list
);
387 fb
->v4lbuf
.bytesused
= 0;
396 /* Our buffer is full !!! */
397 if (framelen
+ fb
->v4lbuf
.bytesused
> dev
->frame_size
) {
398 pr_err_ratelimited("Frame buffer overflow, lost sync\n");
399 /*FIXME Do something here? */
402 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
403 memcpy(fill
, iso_buf
, framelen
);
404 spin_lock_irqsave(&dev
->spinlock
, flags
);
407 /* New size of our buffer */
408 fb
->v4lbuf
.bytesused
+= framelen
;
412 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
413 urb
->dev
= dev
->udev
;
414 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
416 pr_err("Error (%d) re-submitting urb in stk_isoc_handler\n",
421 /* -------------------------------------------- */
423 static int stk_prepare_iso(struct stk_camera
*dev
)
428 struct usb_device
*udev
;
435 pr_err("isobufs already allocated. Bad\n");
437 dev
->isobufs
= kcalloc(MAX_ISO_BUFS
, sizeof(*dev
->isobufs
),
439 if (dev
->isobufs
== NULL
) {
440 pr_err("Unable to allocate iso buffers\n");
443 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
444 if (dev
->isobufs
[i
].data
== NULL
) {
445 kbuf
= kzalloc(ISO_BUFFER_SIZE
, GFP_KERNEL
);
447 pr_err("Failed to allocate iso buffer %d\n", i
);
450 dev
->isobufs
[i
].data
= kbuf
;
452 pr_err("isobuf data already allocated\n");
453 if (dev
->isobufs
[i
].urb
== NULL
) {
454 urb
= usb_alloc_urb(ISO_FRAMES_PER_DESC
, GFP_KERNEL
);
457 dev
->isobufs
[i
].urb
= urb
;
459 pr_err("Killing URB\n");
460 usb_kill_urb(dev
->isobufs
[i
].urb
);
461 urb
= dev
->isobufs
[i
].urb
;
465 urb
->pipe
= usb_rcvisocpipe(udev
, dev
->isoc_ep
);
466 urb
->transfer_flags
= URB_ISO_ASAP
;
467 urb
->transfer_buffer
= dev
->isobufs
[i
].data
;
468 urb
->transfer_buffer_length
= ISO_BUFFER_SIZE
;
469 urb
->complete
= stk_isoc_handler
;
471 urb
->start_frame
= 0;
472 urb
->number_of_packets
= ISO_FRAMES_PER_DESC
;
474 for (j
= 0; j
< ISO_FRAMES_PER_DESC
; j
++) {
475 urb
->iso_frame_desc
[j
].offset
= j
* ISO_MAX_FRAME_SIZE
;
476 urb
->iso_frame_desc
[j
].length
= ISO_MAX_FRAME_SIZE
;
483 for (i
= 0; i
< MAX_ISO_BUFS
&& dev
->isobufs
[i
].data
; i
++)
484 kfree(dev
->isobufs
[i
].data
);
485 for (i
= 0; i
< MAX_ISO_BUFS
&& dev
->isobufs
[i
].urb
; i
++)
486 usb_free_urb(dev
->isobufs
[i
].urb
);
492 static void stk_clean_iso(struct stk_camera
*dev
)
496 if (dev
== NULL
|| dev
->isobufs
== NULL
)
499 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
502 urb
= dev
->isobufs
[i
].urb
;
504 if (atomic_read(&dev
->urbs_used
) && is_present(dev
))
508 kfree(dev
->isobufs
[i
].data
);
512 unset_memallocd(dev
);
515 static int stk_setup_siobuf(struct stk_camera
*dev
, int index
)
517 struct stk_sio_buffer
*buf
= dev
->sio_bufs
+ index
;
518 INIT_LIST_HEAD(&buf
->list
);
519 buf
->v4lbuf
.length
= PAGE_ALIGN(dev
->frame_size
);
520 buf
->buffer
= vmalloc_user(buf
->v4lbuf
.length
);
521 if (buf
->buffer
== NULL
)
525 buf
->v4lbuf
.index
= index
;
526 buf
->v4lbuf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
527 buf
->v4lbuf
.flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
528 buf
->v4lbuf
.field
= V4L2_FIELD_NONE
;
529 buf
->v4lbuf
.memory
= V4L2_MEMORY_MMAP
;
530 buf
->v4lbuf
.m
.offset
= 2*index
*buf
->v4lbuf
.length
;
534 static int stk_free_sio_buffers(struct stk_camera
*dev
)
539 if (dev
->n_sbufs
== 0 || dev
->sio_bufs
== NULL
)
542 * If any buffers are mapped, we cannot free them at all.
544 for (i
= 0; i
< dev
->n_sbufs
; i
++) {
545 if (dev
->sio_bufs
[i
].mapcount
> 0)
551 spin_lock_irqsave(&dev
->spinlock
, flags
);
552 INIT_LIST_HEAD(&dev
->sio_avail
);
553 INIT_LIST_HEAD(&dev
->sio_full
);
554 nbufs
= dev
->n_sbufs
;
556 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
557 for (i
= 0; i
< nbufs
; i
++)
558 vfree(dev
->sio_bufs
[i
].buffer
);
559 kfree(dev
->sio_bufs
);
560 dev
->sio_bufs
= NULL
;
564 static int stk_prepare_sio_buffers(struct stk_camera
*dev
, unsigned n_sbufs
)
567 if (dev
->sio_bufs
!= NULL
)
568 pr_err("sio_bufs already allocated\n");
570 dev
->sio_bufs
= kcalloc(n_sbufs
,
571 sizeof(struct stk_sio_buffer
),
573 if (dev
->sio_bufs
== NULL
)
575 for (i
= 0; i
< n_sbufs
; i
++) {
576 if (stk_setup_siobuf(dev
, i
))
577 return (dev
->n_sbufs
> 1 ? 0 : -ENOMEM
);
584 static int stk_allocate_buffers(struct stk_camera
*dev
, unsigned n_sbufs
)
587 err
= stk_prepare_iso(dev
);
592 err
= stk_prepare_sio_buffers(dev
, n_sbufs
);
594 stk_free_sio_buffers(dev
);
600 static void stk_free_buffers(struct stk_camera
*dev
)
603 stk_free_sio_buffers(dev
);
605 /* -------------------------------------------- */
607 /* v4l file operations */
609 static int v4l_stk_open(struct file
*fp
)
611 struct stk_camera
*dev
= video_drvdata(fp
);
614 if (dev
== NULL
|| !is_present(dev
))
617 if (mutex_lock_interruptible(&dev
->lock
))
619 if (!dev
->first_init
)
620 stk_camera_write_reg(dev
, 0x0, 0x24);
624 err
= v4l2_fh_open(fp
);
626 usb_autopm_get_interface(dev
->interface
);
627 mutex_unlock(&dev
->lock
);
631 static int v4l_stk_release(struct file
*fp
)
633 struct stk_camera
*dev
= video_drvdata(fp
);
635 mutex_lock(&dev
->lock
);
636 if (dev
->owner
== fp
) {
637 stk_stop_stream(dev
);
638 stk_free_buffers(dev
);
639 stk_camera_write_reg(dev
, 0x0, 0x49); /* turn off the LED */
640 unset_initialised(dev
);
645 usb_autopm_put_interface(dev
->interface
);
646 mutex_unlock(&dev
->lock
);
647 return v4l2_fh_release(fp
);
650 static ssize_t
stk_read(struct file
*fp
, char __user
*buf
,
651 size_t count
, loff_t
*f_pos
)
656 struct stk_sio_buffer
*sbuf
;
657 struct stk_camera
*dev
= video_drvdata(fp
);
659 if (!is_present(dev
))
661 if (dev
->owner
&& (!dev
->reading
|| dev
->owner
!= fp
))
664 if (!is_streaming(dev
)) {
665 if (stk_initialise(dev
)
666 || stk_allocate_buffers(dev
, 3)
667 || stk_start_stream(dev
))
670 spin_lock_irqsave(&dev
->spinlock
, flags
);
671 for (i
= 0; i
< dev
->n_sbufs
; i
++) {
672 list_add_tail(&dev
->sio_bufs
[i
].list
, &dev
->sio_avail
);
673 dev
->sio_bufs
[i
].v4lbuf
.flags
= V4L2_BUF_FLAG_QUEUED
;
675 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
678 if (fp
->f_flags
& O_NONBLOCK
&& list_empty(&dev
->sio_full
))
680 ret
= wait_event_interruptible(dev
->wait_frame
,
681 !list_empty(&dev
->sio_full
) || !is_present(dev
));
684 if (!is_present(dev
))
687 if (count
+ *f_pos
> dev
->frame_size
)
688 count
= dev
->frame_size
- *f_pos
;
689 spin_lock_irqsave(&dev
->spinlock
, flags
);
690 if (list_empty(&dev
->sio_full
)) {
691 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
692 pr_err("BUG: No siobufs ready\n");
695 sbuf
= list_first_entry(&dev
->sio_full
, struct stk_sio_buffer
, list
);
696 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
698 if (copy_to_user(buf
, sbuf
->buffer
+ *f_pos
, count
))
703 if (*f_pos
>= dev
->frame_size
) {
705 spin_lock_irqsave(&dev
->spinlock
, flags
);
706 list_move_tail(&sbuf
->list
, &dev
->sio_avail
);
707 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
712 static ssize_t
v4l_stk_read(struct file
*fp
, char __user
*buf
,
713 size_t count
, loff_t
*f_pos
)
715 struct stk_camera
*dev
= video_drvdata(fp
);
718 if (mutex_lock_interruptible(&dev
->lock
))
720 ret
= stk_read(fp
, buf
, count
, f_pos
);
721 mutex_unlock(&dev
->lock
);
725 static __poll_t
v4l_stk_poll(struct file
*fp
, poll_table
*wait
)
727 struct stk_camera
*dev
= video_drvdata(fp
);
728 __poll_t res
= v4l2_ctrl_poll(fp
, wait
);
730 poll_wait(fp
, &dev
->wait_frame
, wait
);
732 if (!is_present(dev
))
735 if (!list_empty(&dev
->sio_full
))
736 return res
| EPOLLIN
| EPOLLRDNORM
;
742 static void stk_v4l_vm_open(struct vm_area_struct
*vma
)
744 struct stk_sio_buffer
*sbuf
= vma
->vm_private_data
;
747 static void stk_v4l_vm_close(struct vm_area_struct
*vma
)
749 struct stk_sio_buffer
*sbuf
= vma
->vm_private_data
;
751 if (sbuf
->mapcount
== 0)
752 sbuf
->v4lbuf
.flags
&= ~V4L2_BUF_FLAG_MAPPED
;
754 static const struct vm_operations_struct stk_v4l_vm_ops
= {
755 .open
= stk_v4l_vm_open
,
756 .close
= stk_v4l_vm_close
759 static int v4l_stk_mmap(struct file
*fp
, struct vm_area_struct
*vma
)
763 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
764 struct stk_camera
*dev
= video_drvdata(fp
);
765 struct stk_sio_buffer
*sbuf
= NULL
;
767 if (!(vma
->vm_flags
& VM_WRITE
) || !(vma
->vm_flags
& VM_SHARED
))
770 for (i
= 0; i
< dev
->n_sbufs
; i
++) {
771 if (dev
->sio_bufs
[i
].v4lbuf
.m
.offset
== offset
) {
772 sbuf
= dev
->sio_bufs
+ i
;
778 ret
= remap_vmalloc_range(vma
, sbuf
->buffer
, 0);
781 vma
->vm_flags
|= VM_DONTEXPAND
;
782 vma
->vm_private_data
= sbuf
;
783 vma
->vm_ops
= &stk_v4l_vm_ops
;
784 sbuf
->v4lbuf
.flags
|= V4L2_BUF_FLAG_MAPPED
;
785 stk_v4l_vm_open(vma
);
789 /* v4l ioctl handlers */
791 static int stk_vidioc_querycap(struct file
*filp
,
792 void *priv
, struct v4l2_capability
*cap
)
794 struct stk_camera
*dev
= video_drvdata(filp
);
796 strcpy(cap
->driver
, "stk");
797 strcpy(cap
->card
, "stk");
798 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
800 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
801 | V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
802 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
806 static int stk_vidioc_enum_input(struct file
*filp
,
807 void *priv
, struct v4l2_input
*input
)
809 if (input
->index
!= 0)
812 strcpy(input
->name
, "Syntek USB Camera");
813 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
818 static int stk_vidioc_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
824 static int stk_vidioc_s_input(struct file
*filp
, void *priv
, unsigned int i
)
826 return i
? -EINVAL
: 0;
829 static int stk_s_ctrl(struct v4l2_ctrl
*ctrl
)
831 struct stk_camera
*dev
=
832 container_of(ctrl
->handler
, struct stk_camera
, hdl
);
835 case V4L2_CID_BRIGHTNESS
:
836 return stk_sensor_set_brightness(dev
, ctrl
->val
);
838 if (dmi_check_system(stk_upside_down_dmi_table
))
839 dev
->vsettings
.hflip
= !ctrl
->val
;
841 dev
->vsettings
.hflip
= ctrl
->val
;
844 if (dmi_check_system(stk_upside_down_dmi_table
))
845 dev
->vsettings
.vflip
= !ctrl
->val
;
847 dev
->vsettings
.vflip
= ctrl
->val
;
856 static int stk_vidioc_enum_fmt_vid_cap(struct file
*filp
,
857 void *priv
, struct v4l2_fmtdesc
*fmtd
)
859 switch (fmtd
->index
) {
861 fmtd
->pixelformat
= V4L2_PIX_FMT_RGB565
;
862 strcpy(fmtd
->description
, "r5g6b5");
865 fmtd
->pixelformat
= V4L2_PIX_FMT_RGB565X
;
866 strcpy(fmtd
->description
, "r5g6b5BE");
869 fmtd
->pixelformat
= V4L2_PIX_FMT_UYVY
;
870 strcpy(fmtd
->description
, "yuv4:2:2");
873 fmtd
->pixelformat
= V4L2_PIX_FMT_SBGGR8
;
874 strcpy(fmtd
->description
, "Raw bayer");
877 fmtd
->pixelformat
= V4L2_PIX_FMT_YUYV
;
878 strcpy(fmtd
->description
, "yuv4:2:2");
886 static struct stk_size
{
891 { .w
= 1280, .h
= 1024, .m
= MODE_SXGA
, },
892 { .w
= 640, .h
= 480, .m
= MODE_VGA
, },
893 { .w
= 352, .h
= 288, .m
= MODE_CIF
, },
894 { .w
= 320, .h
= 240, .m
= MODE_QVGA
, },
895 { .w
= 176, .h
= 144, .m
= MODE_QCIF
, },
898 static int stk_vidioc_g_fmt_vid_cap(struct file
*filp
,
899 void *priv
, struct v4l2_format
*f
)
901 struct v4l2_pix_format
*pix_format
= &f
->fmt
.pix
;
902 struct stk_camera
*dev
= video_drvdata(filp
);
905 for (i
= 0; i
< ARRAY_SIZE(stk_sizes
) &&
906 stk_sizes
[i
].m
!= dev
->vsettings
.mode
; i
++)
908 if (i
== ARRAY_SIZE(stk_sizes
)) {
909 pr_err("ERROR: mode invalid\n");
912 pix_format
->width
= stk_sizes
[i
].w
;
913 pix_format
->height
= stk_sizes
[i
].h
;
914 pix_format
->field
= V4L2_FIELD_NONE
;
915 pix_format
->colorspace
= V4L2_COLORSPACE_SRGB
;
916 pix_format
->pixelformat
= dev
->vsettings
.palette
;
917 if (dev
->vsettings
.palette
== V4L2_PIX_FMT_SBGGR8
)
918 pix_format
->bytesperline
= pix_format
->width
;
920 pix_format
->bytesperline
= 2 * pix_format
->width
;
921 pix_format
->sizeimage
= pix_format
->bytesperline
922 * pix_format
->height
;
926 static int stk_try_fmt_vid_cap(struct file
*filp
,
927 struct v4l2_format
*fmtd
, int *idx
)
930 switch (fmtd
->fmt
.pix
.pixelformat
) {
931 case V4L2_PIX_FMT_RGB565
:
932 case V4L2_PIX_FMT_RGB565X
:
933 case V4L2_PIX_FMT_UYVY
:
934 case V4L2_PIX_FMT_YUYV
:
935 case V4L2_PIX_FMT_SBGGR8
:
940 for (i
= 1; i
< ARRAY_SIZE(stk_sizes
); i
++) {
941 if (fmtd
->fmt
.pix
.width
> stk_sizes
[i
].w
)
944 if (i
== ARRAY_SIZE(stk_sizes
)
945 || (abs(fmtd
->fmt
.pix
.width
- stk_sizes
[i
-1].w
)
946 < abs(fmtd
->fmt
.pix
.width
- stk_sizes
[i
].w
))) {
947 fmtd
->fmt
.pix
.height
= stk_sizes
[i
-1].h
;
948 fmtd
->fmt
.pix
.width
= stk_sizes
[i
-1].w
;
952 fmtd
->fmt
.pix
.height
= stk_sizes
[i
].h
;
953 fmtd
->fmt
.pix
.width
= stk_sizes
[i
].w
;
958 fmtd
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
959 fmtd
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SRGB
;
960 if (fmtd
->fmt
.pix
.pixelformat
== V4L2_PIX_FMT_SBGGR8
)
961 fmtd
->fmt
.pix
.bytesperline
= fmtd
->fmt
.pix
.width
;
963 fmtd
->fmt
.pix
.bytesperline
= 2 * fmtd
->fmt
.pix
.width
;
964 fmtd
->fmt
.pix
.sizeimage
= fmtd
->fmt
.pix
.bytesperline
965 * fmtd
->fmt
.pix
.height
;
969 static int stk_vidioc_try_fmt_vid_cap(struct file
*filp
,
970 void *priv
, struct v4l2_format
*fmtd
)
972 return stk_try_fmt_vid_cap(filp
, fmtd
, NULL
);
975 static int stk_setup_format(struct stk_camera
*dev
)
979 if (dev
->vsettings
.palette
== V4L2_PIX_FMT_SBGGR8
)
983 while (i
< ARRAY_SIZE(stk_sizes
) &&
984 stk_sizes
[i
].m
!= dev
->vsettings
.mode
)
986 if (i
== ARRAY_SIZE(stk_sizes
)) {
987 pr_err("Something is broken in %s\n", __func__
);
990 /* This registers controls some timings, not sure of what. */
991 stk_camera_write_reg(dev
, 0x001b, 0x0e);
992 if (dev
->vsettings
.mode
== MODE_SXGA
)
993 stk_camera_write_reg(dev
, 0x001c, 0x0e);
995 stk_camera_write_reg(dev
, 0x001c, 0x46);
997 * Registers 0x0115 0x0114 are the size of each line (bytes),
998 * regs 0x0117 0x0116 are the heigth of the image.
1000 stk_camera_write_reg(dev
, 0x0115,
1001 ((stk_sizes
[i
].w
* depth
) >> 8) & 0xff);
1002 stk_camera_write_reg(dev
, 0x0114,
1003 (stk_sizes
[i
].w
* depth
) & 0xff);
1004 stk_camera_write_reg(dev
, 0x0117,
1005 (stk_sizes
[i
].h
>> 8) & 0xff);
1006 stk_camera_write_reg(dev
, 0x0116,
1007 stk_sizes
[i
].h
& 0xff);
1008 return stk_sensor_configure(dev
);
1011 static int stk_vidioc_s_fmt_vid_cap(struct file
*filp
,
1012 void *priv
, struct v4l2_format
*fmtd
)
1016 struct stk_camera
*dev
= video_drvdata(filp
);
1020 if (!is_present(dev
))
1022 if (is_streaming(dev
))
1026 ret
= stk_try_fmt_vid_cap(filp
, fmtd
, &idx
);
1030 dev
->vsettings
.palette
= fmtd
->fmt
.pix
.pixelformat
;
1031 stk_free_buffers(dev
);
1032 dev
->frame_size
= fmtd
->fmt
.pix
.sizeimage
;
1033 dev
->vsettings
.mode
= stk_sizes
[idx
].m
;
1035 stk_initialise(dev
);
1036 return stk_setup_format(dev
);
1039 static int stk_vidioc_reqbufs(struct file
*filp
,
1040 void *priv
, struct v4l2_requestbuffers
*rb
)
1042 struct stk_camera
*dev
= video_drvdata(filp
);
1046 if (rb
->memory
!= V4L2_MEMORY_MMAP
)
1048 if (is_streaming(dev
)
1049 || (dev
->owner
&& dev
->owner
!= filp
))
1051 stk_free_buffers(dev
);
1052 if (rb
->count
== 0) {
1053 stk_camera_write_reg(dev
, 0x0, 0x49); /* turn off the LED */
1054 unset_initialised(dev
);
1060 /*FIXME If they ask for zero, we must stop streaming and free */
1063 /* Arbitrary limit */
1064 else if (rb
->count
> 5)
1067 stk_allocate_buffers(dev
, rb
->count
);
1068 rb
->count
= dev
->n_sbufs
;
1072 static int stk_vidioc_querybuf(struct file
*filp
,
1073 void *priv
, struct v4l2_buffer
*buf
)
1075 struct stk_camera
*dev
= video_drvdata(filp
);
1076 struct stk_sio_buffer
*sbuf
;
1078 if (buf
->index
>= dev
->n_sbufs
)
1080 sbuf
= dev
->sio_bufs
+ buf
->index
;
1081 *buf
= sbuf
->v4lbuf
;
1085 static int stk_vidioc_qbuf(struct file
*filp
,
1086 void *priv
, struct v4l2_buffer
*buf
)
1088 struct stk_camera
*dev
= video_drvdata(filp
);
1089 struct stk_sio_buffer
*sbuf
;
1090 unsigned long flags
;
1092 if (buf
->memory
!= V4L2_MEMORY_MMAP
)
1095 if (buf
->index
>= dev
->n_sbufs
)
1097 sbuf
= dev
->sio_bufs
+ buf
->index
;
1098 if (sbuf
->v4lbuf
.flags
& V4L2_BUF_FLAG_QUEUED
)
1100 sbuf
->v4lbuf
.flags
|= V4L2_BUF_FLAG_QUEUED
;
1101 sbuf
->v4lbuf
.flags
&= ~V4L2_BUF_FLAG_DONE
;
1102 spin_lock_irqsave(&dev
->spinlock
, flags
);
1103 list_add_tail(&sbuf
->list
, &dev
->sio_avail
);
1104 *buf
= sbuf
->v4lbuf
;
1105 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
1109 static int stk_vidioc_dqbuf(struct file
*filp
,
1110 void *priv
, struct v4l2_buffer
*buf
)
1112 struct stk_camera
*dev
= video_drvdata(filp
);
1113 struct stk_sio_buffer
*sbuf
;
1114 unsigned long flags
;
1117 if (!is_streaming(dev
))
1120 if (filp
->f_flags
& O_NONBLOCK
&& list_empty(&dev
->sio_full
))
1121 return -EWOULDBLOCK
;
1122 ret
= wait_event_interruptible(dev
->wait_frame
,
1123 !list_empty(&dev
->sio_full
) || !is_present(dev
));
1126 if (!is_present(dev
))
1129 spin_lock_irqsave(&dev
->spinlock
, flags
);
1130 sbuf
= list_first_entry(&dev
->sio_full
, struct stk_sio_buffer
, list
);
1131 list_del_init(&sbuf
->list
);
1132 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
1133 sbuf
->v4lbuf
.flags
&= ~V4L2_BUF_FLAG_QUEUED
;
1134 sbuf
->v4lbuf
.flags
|= V4L2_BUF_FLAG_DONE
;
1135 sbuf
->v4lbuf
.sequence
= ++dev
->sequence
;
1136 v4l2_get_timestamp(&sbuf
->v4lbuf
.timestamp
);
1138 *buf
= sbuf
->v4lbuf
;
1142 static int stk_vidioc_streamon(struct file
*filp
,
1143 void *priv
, enum v4l2_buf_type type
)
1145 struct stk_camera
*dev
= video_drvdata(filp
);
1146 if (is_streaming(dev
))
1148 if (dev
->sio_bufs
== NULL
)
1151 return stk_start_stream(dev
);
1154 static int stk_vidioc_streamoff(struct file
*filp
,
1155 void *priv
, enum v4l2_buf_type type
)
1157 struct stk_camera
*dev
= video_drvdata(filp
);
1158 unsigned long flags
;
1160 stk_stop_stream(dev
);
1161 spin_lock_irqsave(&dev
->spinlock
, flags
);
1162 INIT_LIST_HEAD(&dev
->sio_avail
);
1163 INIT_LIST_HEAD(&dev
->sio_full
);
1164 for (i
= 0; i
< dev
->n_sbufs
; i
++) {
1165 INIT_LIST_HEAD(&dev
->sio_bufs
[i
].list
);
1166 dev
->sio_bufs
[i
].v4lbuf
.flags
= 0;
1168 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
1173 static int stk_vidioc_g_parm(struct file
*filp
,
1174 void *priv
, struct v4l2_streamparm
*sp
)
1176 /*FIXME This is not correct */
1177 sp
->parm
.capture
.timeperframe
.numerator
= 1;
1178 sp
->parm
.capture
.timeperframe
.denominator
= 30;
1179 sp
->parm
.capture
.readbuffers
= 2;
1183 static int stk_vidioc_enum_framesizes(struct file
*filp
,
1184 void *priv
, struct v4l2_frmsizeenum
*frms
)
1186 if (frms
->index
>= ARRAY_SIZE(stk_sizes
))
1188 switch (frms
->pixel_format
) {
1189 case V4L2_PIX_FMT_RGB565
:
1190 case V4L2_PIX_FMT_RGB565X
:
1191 case V4L2_PIX_FMT_UYVY
:
1192 case V4L2_PIX_FMT_YUYV
:
1193 case V4L2_PIX_FMT_SBGGR8
:
1194 frms
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1195 frms
->discrete
.width
= stk_sizes
[frms
->index
].w
;
1196 frms
->discrete
.height
= stk_sizes
[frms
->index
].h
;
1198 default: return -EINVAL
;
1202 static const struct v4l2_ctrl_ops stk_ctrl_ops
= {
1203 .s_ctrl
= stk_s_ctrl
,
1206 static const struct v4l2_file_operations v4l_stk_fops
= {
1207 .owner
= THIS_MODULE
,
1208 .open
= v4l_stk_open
,
1209 .release
= v4l_stk_release
,
1210 .read
= v4l_stk_read
,
1211 .poll
= v4l_stk_poll
,
1212 .mmap
= v4l_stk_mmap
,
1213 .unlocked_ioctl
= video_ioctl2
,
1216 static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops
= {
1217 .vidioc_querycap
= stk_vidioc_querycap
,
1218 .vidioc_enum_fmt_vid_cap
= stk_vidioc_enum_fmt_vid_cap
,
1219 .vidioc_try_fmt_vid_cap
= stk_vidioc_try_fmt_vid_cap
,
1220 .vidioc_s_fmt_vid_cap
= stk_vidioc_s_fmt_vid_cap
,
1221 .vidioc_g_fmt_vid_cap
= stk_vidioc_g_fmt_vid_cap
,
1222 .vidioc_enum_input
= stk_vidioc_enum_input
,
1223 .vidioc_s_input
= stk_vidioc_s_input
,
1224 .vidioc_g_input
= stk_vidioc_g_input
,
1225 .vidioc_reqbufs
= stk_vidioc_reqbufs
,
1226 .vidioc_querybuf
= stk_vidioc_querybuf
,
1227 .vidioc_qbuf
= stk_vidioc_qbuf
,
1228 .vidioc_dqbuf
= stk_vidioc_dqbuf
,
1229 .vidioc_streamon
= stk_vidioc_streamon
,
1230 .vidioc_streamoff
= stk_vidioc_streamoff
,
1231 .vidioc_g_parm
= stk_vidioc_g_parm
,
1232 .vidioc_enum_framesizes
= stk_vidioc_enum_framesizes
,
1233 .vidioc_log_status
= v4l2_ctrl_log_status
,
1234 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1235 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1238 static void stk_v4l_dev_release(struct video_device
*vd
)
1240 struct stk_camera
*dev
= vdev_to_camera(vd
);
1242 if (dev
->sio_bufs
!= NULL
|| dev
->isobufs
!= NULL
)
1243 pr_err("We are leaking memory\n");
1244 usb_put_intf(dev
->interface
);
1247 static const struct video_device stk_v4l_data
= {
1248 .name
= "stkwebcam",
1249 .fops
= &v4l_stk_fops
,
1250 .ioctl_ops
= &v4l_stk_ioctl_ops
,
1251 .release
= stk_v4l_dev_release
,
1255 static int stk_register_video_device(struct stk_camera
*dev
)
1259 dev
->vdev
= stk_v4l_data
;
1260 dev
->vdev
.lock
= &dev
->lock
;
1261 dev
->vdev
.v4l2_dev
= &dev
->v4l2_dev
;
1262 video_set_drvdata(&dev
->vdev
, dev
);
1263 err
= video_register_device(&dev
->vdev
, VFL_TYPE_GRABBER
, -1);
1265 pr_err("v4l registration failed\n");
1267 pr_info("Syntek USB2.0 Camera is now controlling device %s\n",
1268 video_device_node_name(&dev
->vdev
));
1275 static int stk_camera_probe(struct usb_interface
*interface
,
1276 const struct usb_device_id
*id
)
1278 struct v4l2_ctrl_handler
*hdl
;
1282 struct stk_camera
*dev
= NULL
;
1283 struct usb_device
*udev
= interface_to_usbdev(interface
);
1284 struct usb_host_interface
*iface_desc
;
1285 struct usb_endpoint_descriptor
*endpoint
;
1287 dev
= kzalloc(sizeof(struct stk_camera
), GFP_KERNEL
);
1289 pr_err("Out of memory !\n");
1292 err
= v4l2_device_register(&interface
->dev
, &dev
->v4l2_dev
);
1294 dev_err(&udev
->dev
, "couldn't register v4l2_device\n");
1299 v4l2_ctrl_handler_init(hdl
, 3);
1300 v4l2_ctrl_new_std(hdl
, &stk_ctrl_ops
,
1301 V4L2_CID_BRIGHTNESS
, 0, 0xff, 0x1, 0x60);
1302 v4l2_ctrl_new_std(hdl
, &stk_ctrl_ops
,
1303 V4L2_CID_HFLIP
, 0, 1, 1, 1);
1304 v4l2_ctrl_new_std(hdl
, &stk_ctrl_ops
,
1305 V4L2_CID_VFLIP
, 0, 1, 1, 1);
1308 dev_err(&udev
->dev
, "couldn't register control\n");
1311 dev
->v4l2_dev
.ctrl_handler
= hdl
;
1313 spin_lock_init(&dev
->spinlock
);
1314 mutex_init(&dev
->lock
);
1315 init_waitqueue_head(&dev
->wait_frame
);
1316 dev
->first_init
= 1; /* webcam LED management */
1319 dev
->interface
= interface
;
1320 usb_get_intf(interface
);
1323 dev
->vsettings
.hflip
= hflip
;
1324 else if (dmi_check_system(stk_upside_down_dmi_table
))
1325 dev
->vsettings
.hflip
= 1;
1327 dev
->vsettings
.hflip
= 0;
1329 dev
->vsettings
.vflip
= vflip
;
1330 else if (dmi_check_system(stk_upside_down_dmi_table
))
1331 dev
->vsettings
.vflip
= 1;
1333 dev
->vsettings
.vflip
= 0;
1337 /* Set up the endpoint information
1338 * use only the first isoc-in endpoint
1339 * for the current alternate setting */
1340 iface_desc
= interface
->cur_altsetting
;
1342 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
1343 endpoint
= &iface_desc
->endpoint
[i
].desc
;
1346 && usb_endpoint_is_isoc_in(endpoint
)) {
1347 /* we found an isoc in endpoint */
1348 dev
->isoc_ep
= usb_endpoint_num(endpoint
);
1352 if (!dev
->isoc_ep
) {
1353 pr_err("Could not find isoc-in endpoint\n");
1357 dev
->vsettings
.palette
= V4L2_PIX_FMT_RGB565
;
1358 dev
->vsettings
.mode
= MODE_VGA
;
1359 dev
->frame_size
= 640 * 480 * 2;
1361 INIT_LIST_HEAD(&dev
->sio_avail
);
1362 INIT_LIST_HEAD(&dev
->sio_full
);
1364 usb_set_intfdata(interface
, dev
);
1366 err
= stk_register_video_device(dev
);
1373 v4l2_ctrl_handler_free(hdl
);
1374 v4l2_device_unregister(&dev
->v4l2_dev
);
1379 static void stk_camera_disconnect(struct usb_interface
*interface
)
1381 struct stk_camera
*dev
= usb_get_intfdata(interface
);
1383 usb_set_intfdata(interface
, NULL
);
1386 wake_up_interruptible(&dev
->wait_frame
);
1388 pr_info("Syntek USB2.0 Camera release resources device %s\n",
1389 video_device_node_name(&dev
->vdev
));
1391 video_unregister_device(&dev
->vdev
);
1392 v4l2_ctrl_handler_free(&dev
->hdl
);
1393 v4l2_device_unregister(&dev
->v4l2_dev
);
1398 static int stk_camera_suspend(struct usb_interface
*intf
, pm_message_t message
)
1400 struct stk_camera
*dev
= usb_get_intfdata(intf
);
1401 if (is_streaming(dev
)) {
1402 stk_stop_stream(dev
);
1403 /* yes, this is ugly */
1409 static int stk_camera_resume(struct usb_interface
*intf
)
1411 struct stk_camera
*dev
= usb_get_intfdata(intf
);
1412 if (!is_initialised(dev
))
1414 unset_initialised(dev
);
1415 stk_initialise(dev
);
1416 stk_camera_write_reg(dev
, 0x0, 0x49);
1417 stk_setup_format(dev
);
1418 if (is_streaming(dev
))
1419 stk_start_stream(dev
);
1424 static struct usb_driver stk_camera_driver
= {
1425 .name
= "stkwebcam",
1426 .probe
= stk_camera_probe
,
1427 .disconnect
= stk_camera_disconnect
,
1428 .id_table
= stkwebcam_table
,
1430 .suspend
= stk_camera_suspend
,
1431 .resume
= stk_camera_resume
,
1435 module_usb_driver(stk_camera_driver
);