2 * stk-webcam.c : Driver for Syntek 1125 USB webcam controller
4 * Copyright (C) 2006 Nicolas VIVIEN
5 * Copyright 2007-2008 Jaime Velasco Juan <jsagarribay@gmail.com>
7 * Some parts are inspired from cafe_ccic.c
8 * Copyright 2006-2007 Jonathan Corbet
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/errno.h>
29 #include <linux/slab.h>
31 #include <linux/usb.h>
33 #include <linux/vmalloc.h>
34 #include <linux/videodev2.h>
35 #include <media/v4l2-common.h>
36 #include <media/v4l2-ioctl.h>
38 #include "stk-webcam.h"
42 module_param(hflip
, bool, 0444);
43 MODULE_PARM_DESC(hflip
, "Horizontal image flip (mirror). Defaults to 1");
46 module_param(vflip
, bool, 0444);
47 MODULE_PARM_DESC(vflip
, "Vertical image flip. Defaults to 1");
50 module_param(debug
, int, 0444);
51 MODULE_PARM_DESC(debug
, "Debug v4l ioctls. Defaults to 0");
53 MODULE_LICENSE("GPL");
54 MODULE_AUTHOR("Jaime Velasco Juan <jsagarribay@gmail.com> and Nicolas VIVIEN");
55 MODULE_DESCRIPTION("Syntek DC1125 webcam driver");
59 /* Some cameras have audio interfaces, we aren't interested in those */
60 static struct usb_device_id stkwebcam_table
[] = {
61 { USB_DEVICE_AND_INTERFACE_INFO(0x174f, 0xa311, 0xff, 0xff, 0xff) },
62 { USB_DEVICE_AND_INTERFACE_INFO(0x05e1, 0x0501, 0xff, 0xff, 0xff) },
65 MODULE_DEVICE_TABLE(usb
, stkwebcam_table
);
70 int stk_camera_write_reg(struct stk_camera
*dev
, u16 index
, u8 value
)
72 struct usb_device
*udev
= dev
->udev
;
75 ret
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
77 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
89 int stk_camera_read_reg(struct stk_camera
*dev
, u16 index
, int *value
)
91 struct usb_device
*udev
= dev
->udev
;
94 ret
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
96 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
108 static int stk_start_stream(struct stk_camera
*dev
)
112 int value_116
, value_117
;
114 if (!is_present(dev
))
116 if (!is_memallocd(dev
) || !is_initialised(dev
)) {
117 STK_ERROR("FIXME: Buffers are not allocated\n");
120 ret
= usb_set_interface(dev
->udev
, 0, 5);
123 STK_ERROR("usb_set_interface failed !\n");
124 if (stk_sensor_wakeup(dev
))
125 STK_ERROR("error awaking the sensor\n");
127 stk_camera_read_reg(dev
, 0x0116, &value_116
);
128 stk_camera_read_reg(dev
, 0x0117, &value_117
);
130 stk_camera_write_reg(dev
, 0x0116, 0x0000);
131 stk_camera_write_reg(dev
, 0x0117, 0x0000);
133 stk_camera_read_reg(dev
, 0x0100, &value
);
134 stk_camera_write_reg(dev
, 0x0100, value
| 0x80);
136 stk_camera_write_reg(dev
, 0x0116, value_116
);
137 stk_camera_write_reg(dev
, 0x0117, value_117
);
138 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
139 if (dev
->isobufs
[i
].urb
) {
140 ret
= usb_submit_urb(dev
->isobufs
[i
].urb
, GFP_KERNEL
);
141 atomic_inc(&dev
->urbs_used
);
150 static int stk_stop_stream(struct stk_camera
*dev
)
154 if (is_present(dev
)) {
155 stk_camera_read_reg(dev
, 0x0100, &value
);
156 stk_camera_write_reg(dev
, 0x0100, value
& ~0x80);
157 if (dev
->isobufs
!= NULL
) {
158 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
159 if (dev
->isobufs
[i
].urb
)
160 usb_kill_urb(dev
->isobufs
[i
].urb
);
163 unset_streaming(dev
);
165 if (usb_set_interface(dev
->udev
, 0, 0))
166 STK_ERROR("usb_set_interface failed !\n");
167 if (stk_sensor_sleep(dev
))
168 STK_ERROR("error suspending the sensor\n");
174 * This seems to be the shortest init sequence we
175 * must do in order to find the sensor
176 * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
177 * is also reset. Maybe powers down it?
178 * Rest of values don't make a difference
181 static struct regval stk1125_initvals
[] = {
182 /*TODO: What means this sequence? */
211 static int stk_initialise(struct stk_camera
*dev
)
215 if (!is_present(dev
))
217 if (is_initialised(dev
))
219 rv
= stk1125_initvals
;
220 while (rv
->reg
!= 0xffff) {
221 ret
= stk_camera_write_reg(dev
, rv
->reg
, rv
->val
);
226 if (stk_sensor_init(dev
) == 0) {
227 set_initialised(dev
);
233 #ifdef CONFIG_VIDEO_V4L1_COMPAT
235 /* sysfs functions */
236 /*FIXME cleanup this */
238 static ssize_t
show_brightness(struct device
*class,
239 struct device_attribute
*attr
, char *buf
)
241 struct video_device
*vdev
= to_video_device(class);
242 struct stk_camera
*dev
= vdev_to_camera(vdev
);
244 return sprintf(buf
, "%X\n", dev
->vsettings
.brightness
);
247 static ssize_t
store_brightness(struct device
*class,
248 struct device_attribute
*attr
, const char *buf
, size_t count
)
254 struct video_device
*vdev
= to_video_device(class);
255 struct stk_camera
*dev
= vdev_to_camera(vdev
);
257 value
= simple_strtoul(buf
, &endp
, 16);
259 dev
->vsettings
.brightness
= (int) value
;
261 ret
= stk_sensor_set_brightness(dev
, value
>> 8);
268 static ssize_t
show_hflip(struct device
*class,
269 struct device_attribute
*attr
, char *buf
)
271 struct video_device
*vdev
= to_video_device(class);
272 struct stk_camera
*dev
= vdev_to_camera(vdev
);
274 return sprintf(buf
, "%d\n", dev
->vsettings
.hflip
);
277 static ssize_t
store_hflip(struct device
*class,
278 struct device_attribute
*attr
, const char *buf
, size_t count
)
280 struct video_device
*vdev
= to_video_device(class);
281 struct stk_camera
*dev
= vdev_to_camera(vdev
);
283 if (strncmp(buf
, "1", 1) == 0)
284 dev
->vsettings
.hflip
= 1;
285 else if (strncmp(buf
, "0", 1) == 0)
286 dev
->vsettings
.hflip
= 0;
293 static ssize_t
show_vflip(struct device
*class,
294 struct device_attribute
*attr
, char *buf
)
296 struct video_device
*vdev
= to_video_device(class);
297 struct stk_camera
*dev
= vdev_to_camera(vdev
);
299 return sprintf(buf
, "%d\n", dev
->vsettings
.vflip
);
302 static ssize_t
store_vflip(struct device
*class,
303 struct device_attribute
*attr
, const char *buf
, size_t count
)
305 struct video_device
*vdev
= to_video_device(class);
306 struct stk_camera
*dev
= vdev_to_camera(vdev
);
308 if (strncmp(buf
, "1", 1) == 0)
309 dev
->vsettings
.vflip
= 1;
310 else if (strncmp(buf
, "0", 1) == 0)
311 dev
->vsettings
.vflip
= 0;
318 static DEVICE_ATTR(brightness
, S_IRUGO
| S_IWUGO
,
319 show_brightness
, store_brightness
);
320 static DEVICE_ATTR(hflip
, S_IRUGO
| S_IWUGO
, show_hflip
, store_hflip
);
321 static DEVICE_ATTR(vflip
, S_IRUGO
| S_IWUGO
, show_vflip
, store_vflip
);
323 static int stk_create_sysfs_files(struct video_device
*vdev
)
327 ret
= device_create_file(&vdev
->dev
, &dev_attr_brightness
);
328 ret
+= device_create_file(&vdev
->dev
, &dev_attr_hflip
);
329 ret
+= device_create_file(&vdev
->dev
, &dev_attr_vflip
);
331 STK_WARNING("Could not create sysfs files\n");
335 static void stk_remove_sysfs_files(struct video_device
*vdev
)
337 device_remove_file(&vdev
->dev
, &dev_attr_brightness
);
338 device_remove_file(&vdev
->dev
, &dev_attr_hflip
);
339 device_remove_file(&vdev
->dev
, &dev_attr_vflip
);
343 #define stk_create_sysfs_files(a)
344 #define stk_remove_sysfs_files(a)
347 /* *********************************************** */
349 * This function is called as an URB transfert is complete (Isochronous pipe).
350 * So, the traitement is done in interrupt time, so it has be fast, not crash,
351 * and not stall. Neat.
353 static void stk_isoc_handler(struct urb
*urb
)
360 unsigned char *fill
= NULL
;
361 unsigned char *iso_buf
= NULL
;
363 struct stk_camera
*dev
;
364 struct stk_sio_buffer
*fb
;
366 dev
= (struct stk_camera
*) urb
->context
;
369 STK_ERROR("isoc_handler called with NULL device !\n");
373 if (urb
->status
== -ENOENT
|| urb
->status
== -ECONNRESET
374 || urb
->status
== -ESHUTDOWN
) {
375 atomic_dec(&dev
->urbs_used
);
379 spin_lock_irqsave(&dev
->spinlock
, flags
);
381 if (urb
->status
!= -EINPROGRESS
&& urb
->status
!= 0) {
382 STK_ERROR("isoc_handler: urb->status == %d\n", urb
->status
);
386 if (list_empty(&dev
->sio_avail
)) {
387 /*FIXME Stop streaming after a while */
388 (void) (printk_ratelimit() &&
389 STK_ERROR("isoc_handler without available buffer!\n"));
392 fb
= list_first_entry(&dev
->sio_avail
,
393 struct stk_sio_buffer
, list
);
394 fill
= fb
->buffer
+ fb
->v4lbuf
.bytesused
;
396 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
397 if (urb
->iso_frame_desc
[i
].status
!= 0) {
398 if (urb
->iso_frame_desc
[i
].status
!= -EXDEV
)
399 STK_ERROR("Frame %d has error %d\n", i
,
400 urb
->iso_frame_desc
[i
].status
);
403 framelen
= urb
->iso_frame_desc
[i
].actual_length
;
404 iso_buf
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
407 continue; /* no data */
410 * we found something informational from there
411 * the isoc frames have to type of headers
412 * type1: 00 xx 00 00 or 20 xx 00 00
413 * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
414 * xx is a sequencer which has never been seen over 0x3f
415 * imho data written down looks like bayer, i see similarities
416 * after every 640 bytes
418 if (*iso_buf
& 0x80) {
421 /* This marks a new frame */
422 if (fb
->v4lbuf
.bytesused
!= 0
423 && fb
->v4lbuf
.bytesused
!= dev
->frame_size
) {
424 (void) (printk_ratelimit() &&
425 STK_ERROR("frame %d, "
426 "bytesused=%d, skipping\n",
427 i
, fb
->v4lbuf
.bytesused
));
428 fb
->v4lbuf
.bytesused
= 0;
430 } else if (fb
->v4lbuf
.bytesused
== dev
->frame_size
) {
431 if (list_is_singular(&dev
->sio_avail
)) {
432 /* Always reuse the last buffer */
433 fb
->v4lbuf
.bytesused
= 0;
436 list_move_tail(dev
->sio_avail
.next
,
438 wake_up(&dev
->wait_frame
);
439 fb
= list_first_entry(&dev
->sio_avail
,
440 struct stk_sio_buffer
, list
);
441 fb
->v4lbuf
.bytesused
= 0;
450 /* Our buffer is full !!! */
451 if (framelen
+ fb
->v4lbuf
.bytesused
> dev
->frame_size
) {
452 (void) (printk_ratelimit() &&
453 STK_ERROR("Frame buffer overflow, lost sync\n"));
454 /*FIXME Do something here? */
457 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
458 memcpy(fill
, iso_buf
, framelen
);
459 spin_lock_irqsave(&dev
->spinlock
, flags
);
462 /* New size of our buffer */
463 fb
->v4lbuf
.bytesused
+= framelen
;
467 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
468 urb
->dev
= dev
->udev
;
469 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
471 STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
476 /* -------------------------------------------- */
478 static int stk_prepare_iso(struct stk_camera
*dev
)
483 struct usb_device
*udev
;
490 STK_ERROR("isobufs already allocated. Bad\n");
492 dev
->isobufs
= kzalloc(MAX_ISO_BUFS
* sizeof(*dev
->isobufs
),
494 if (dev
->isobufs
== NULL
) {
495 STK_ERROR("Unable to allocate iso buffers\n");
498 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
499 if (dev
->isobufs
[i
].data
== NULL
) {
500 kbuf
= kzalloc(ISO_BUFFER_SIZE
, GFP_KERNEL
);
502 STK_ERROR("Failed to allocate iso buffer %d\n",
506 dev
->isobufs
[i
].data
= kbuf
;
508 STK_ERROR("isobuf data already allocated\n");
509 if (dev
->isobufs
[i
].urb
== NULL
) {
510 urb
= usb_alloc_urb(ISO_FRAMES_PER_DESC
, GFP_KERNEL
);
512 STK_ERROR("Failed to allocate URB %d\n", i
);
515 dev
->isobufs
[i
].urb
= urb
;
517 STK_ERROR("Killing URB\n");
518 usb_kill_urb(dev
->isobufs
[i
].urb
);
519 urb
= dev
->isobufs
[i
].urb
;
523 urb
->pipe
= usb_rcvisocpipe(udev
, dev
->isoc_ep
);
524 urb
->transfer_flags
= URB_ISO_ASAP
;
525 urb
->transfer_buffer
= dev
->isobufs
[i
].data
;
526 urb
->transfer_buffer_length
= ISO_BUFFER_SIZE
;
527 urb
->complete
= stk_isoc_handler
;
529 urb
->start_frame
= 0;
530 urb
->number_of_packets
= ISO_FRAMES_PER_DESC
;
532 for (j
= 0; j
< ISO_FRAMES_PER_DESC
; j
++) {
533 urb
->iso_frame_desc
[j
].offset
= j
* ISO_MAX_FRAME_SIZE
;
534 urb
->iso_frame_desc
[j
].length
= ISO_MAX_FRAME_SIZE
;
541 for (i
= 0; i
< MAX_ISO_BUFS
&& dev
->isobufs
[i
].data
; i
++)
542 kfree(dev
->isobufs
[i
].data
);
543 for (i
= 0; i
< MAX_ISO_BUFS
&& dev
->isobufs
[i
].urb
; i
++)
544 usb_free_urb(dev
->isobufs
[i
].urb
);
550 static void stk_clean_iso(struct stk_camera
*dev
)
554 if (dev
== NULL
|| dev
->isobufs
== NULL
)
557 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
560 urb
= dev
->isobufs
[i
].urb
;
562 if (atomic_read(&dev
->urbs_used
) && is_present(dev
))
566 kfree(dev
->isobufs
[i
].data
);
570 unset_memallocd(dev
);
573 static int stk_setup_siobuf(struct stk_camera
*dev
, int index
)
575 struct stk_sio_buffer
*buf
= dev
->sio_bufs
+ index
;
576 INIT_LIST_HEAD(&buf
->list
);
577 buf
->v4lbuf
.length
= PAGE_ALIGN(dev
->frame_size
);
578 buf
->buffer
= vmalloc_user(buf
->v4lbuf
.length
);
579 if (buf
->buffer
== NULL
)
583 buf
->v4lbuf
.index
= index
;
584 buf
->v4lbuf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
585 buf
->v4lbuf
.field
= V4L2_FIELD_NONE
;
586 buf
->v4lbuf
.memory
= V4L2_MEMORY_MMAP
;
587 buf
->v4lbuf
.m
.offset
= 2*index
*buf
->v4lbuf
.length
;
591 static int stk_free_sio_buffers(struct stk_camera
*dev
)
596 if (dev
->n_sbufs
== 0 || dev
->sio_bufs
== NULL
)
599 * If any buffers are mapped, we cannot free them at all.
601 for (i
= 0; i
< dev
->n_sbufs
; i
++) {
602 if (dev
->sio_bufs
[i
].mapcount
> 0)
608 spin_lock_irqsave(&dev
->spinlock
, flags
);
609 INIT_LIST_HEAD(&dev
->sio_avail
);
610 INIT_LIST_HEAD(&dev
->sio_full
);
611 nbufs
= dev
->n_sbufs
;
613 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
614 for (i
= 0; i
< nbufs
; i
++) {
615 if (dev
->sio_bufs
[i
].buffer
!= NULL
)
616 vfree(dev
->sio_bufs
[i
].buffer
);
618 kfree(dev
->sio_bufs
);
619 dev
->sio_bufs
= NULL
;
623 static int stk_prepare_sio_buffers(struct stk_camera
*dev
, unsigned n_sbufs
)
626 if (dev
->sio_bufs
!= NULL
)
627 STK_ERROR("sio_bufs already allocated\n");
629 dev
->sio_bufs
= kzalloc(n_sbufs
* sizeof(struct stk_sio_buffer
),
631 if (dev
->sio_bufs
== NULL
)
633 for (i
= 0; i
< n_sbufs
; i
++) {
634 if (stk_setup_siobuf(dev
, i
))
635 return (dev
->n_sbufs
> 1)? 0 : -ENOMEM
;
642 static int stk_allocate_buffers(struct stk_camera
*dev
, unsigned n_sbufs
)
645 err
= stk_prepare_iso(dev
);
650 err
= stk_prepare_sio_buffers(dev
, n_sbufs
);
652 stk_free_sio_buffers(dev
);
658 static void stk_free_buffers(struct stk_camera
*dev
)
661 stk_free_sio_buffers(dev
);
663 /* -------------------------------------------- */
665 /* v4l file operations */
667 static int v4l_stk_open(struct file
*fp
)
669 struct stk_camera
*dev
;
670 struct video_device
*vdev
;
672 vdev
= video_devdata(fp
);
673 dev
= vdev_to_camera(vdev
);
676 if (dev
== NULL
|| !is_present(dev
)) {
680 fp
->private_data
= dev
;
681 usb_autopm_get_interface(dev
->interface
);
687 static int v4l_stk_release(struct file
*fp
)
689 struct stk_camera
*dev
= fp
->private_data
;
691 if (dev
->owner
== fp
) {
692 stk_stop_stream(dev
);
693 stk_free_buffers(dev
);
698 usb_autopm_put_interface(dev
->interface
);
703 static ssize_t
v4l_stk_read(struct file
*fp
, char __user
*buf
,
704 size_t count
, loff_t
*f_pos
)
709 struct stk_sio_buffer
*sbuf
;
710 struct stk_camera
*dev
= fp
->private_data
;
712 if (!is_present(dev
))
714 if (dev
->owner
&& dev
->owner
!= fp
)
717 if (!is_streaming(dev
)) {
718 if (stk_initialise(dev
)
719 || stk_allocate_buffers(dev
, 3)
720 || stk_start_stream(dev
))
722 spin_lock_irqsave(&dev
->spinlock
, flags
);
723 for (i
= 0; i
< dev
->n_sbufs
; i
++) {
724 list_add_tail(&dev
->sio_bufs
[i
].list
, &dev
->sio_avail
);
725 dev
->sio_bufs
[i
].v4lbuf
.flags
= V4L2_BUF_FLAG_QUEUED
;
727 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
730 if (fp
->f_flags
& O_NONBLOCK
&& list_empty(&dev
->sio_full
))
732 ret
= wait_event_interruptible(dev
->wait_frame
,
733 !list_empty(&dev
->sio_full
) || !is_present(dev
));
736 if (!is_present(dev
))
739 if (count
+ *f_pos
> dev
->frame_size
)
740 count
= dev
->frame_size
- *f_pos
;
741 spin_lock_irqsave(&dev
->spinlock
, flags
);
742 if (list_empty(&dev
->sio_full
)) {
743 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
744 STK_ERROR("BUG: No siobufs ready\n");
747 sbuf
= list_first_entry(&dev
->sio_full
, struct stk_sio_buffer
, list
);
748 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
750 if (copy_to_user(buf
, sbuf
->buffer
+ *f_pos
, count
))
755 if (*f_pos
>= dev
->frame_size
) {
757 spin_lock_irqsave(&dev
->spinlock
, flags
);
758 list_move_tail(&sbuf
->list
, &dev
->sio_avail
);
759 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
764 static unsigned int v4l_stk_poll(struct file
*fp
, poll_table
*wait
)
766 struct stk_camera
*dev
= fp
->private_data
;
768 poll_wait(fp
, &dev
->wait_frame
, wait
);
770 if (!is_present(dev
))
773 if (!list_empty(&dev
->sio_full
))
774 return (POLLIN
| POLLRDNORM
);
780 static void stk_v4l_vm_open(struct vm_area_struct
*vma
)
782 struct stk_sio_buffer
*sbuf
= vma
->vm_private_data
;
785 static void stk_v4l_vm_close(struct vm_area_struct
*vma
)
787 struct stk_sio_buffer
*sbuf
= vma
->vm_private_data
;
789 if (sbuf
->mapcount
== 0)
790 sbuf
->v4lbuf
.flags
&= ~V4L2_BUF_FLAG_MAPPED
;
792 static struct vm_operations_struct stk_v4l_vm_ops
= {
793 .open
= stk_v4l_vm_open
,
794 .close
= stk_v4l_vm_close
797 static int v4l_stk_mmap(struct file
*fp
, struct vm_area_struct
*vma
)
801 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
802 struct stk_camera
*dev
= fp
->private_data
;
803 struct stk_sio_buffer
*sbuf
= NULL
;
805 if (!(vma
->vm_flags
& VM_WRITE
) || !(vma
->vm_flags
& VM_SHARED
))
808 for (i
= 0; i
< dev
->n_sbufs
; i
++) {
809 if (dev
->sio_bufs
[i
].v4lbuf
.m
.offset
== offset
) {
810 sbuf
= dev
->sio_bufs
+ i
;
816 ret
= remap_vmalloc_range(vma
, sbuf
->buffer
, 0);
819 vma
->vm_flags
|= VM_DONTEXPAND
;
820 vma
->vm_private_data
= sbuf
;
821 vma
->vm_ops
= &stk_v4l_vm_ops
;
822 sbuf
->v4lbuf
.flags
|= V4L2_BUF_FLAG_MAPPED
;
823 stk_v4l_vm_open(vma
);
827 /* v4l ioctl handlers */
829 static int stk_vidioc_querycap(struct file
*filp
,
830 void *priv
, struct v4l2_capability
*cap
)
832 strcpy(cap
->driver
, "stk");
833 strcpy(cap
->card
, "stk");
834 cap
->version
= DRIVER_VERSION_NUM
;
836 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
837 | V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
841 static int stk_vidioc_enum_input(struct file
*filp
,
842 void *priv
, struct v4l2_input
*input
)
844 if (input
->index
!= 0)
847 strcpy(input
->name
, "Syntek USB Camera");
848 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
853 static int stk_vidioc_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
859 static int stk_vidioc_s_input(struct file
*filp
, void *priv
, unsigned int i
)
868 static int stk_vidioc_s_std(struct file
*filp
, void *priv
, v4l2_std_id
*a
)
873 /* List of all V4Lv2 controls supported by the driver */
874 static struct v4l2_queryctrl stk_controls
[] = {
876 .id
= V4L2_CID_BRIGHTNESS
,
877 .type
= V4L2_CTRL_TYPE_INTEGER
,
878 .name
= "Brightness",
882 .default_value
= 0x6000,
884 /*TODO: get more controls to work */
887 static int stk_vidioc_queryctrl(struct file
*filp
,
888 void *priv
, struct v4l2_queryctrl
*c
)
892 nbr
= ARRAY_SIZE(stk_controls
);
894 for (i
= 0; i
< nbr
; i
++) {
895 if (stk_controls
[i
].id
== c
->id
) {
896 memcpy(c
, &stk_controls
[i
],
897 sizeof(struct v4l2_queryctrl
));
904 static int stk_vidioc_g_ctrl(struct file
*filp
,
905 void *priv
, struct v4l2_control
*c
)
907 struct stk_camera
*dev
= priv
;
909 case V4L2_CID_BRIGHTNESS
:
910 c
->value
= dev
->vsettings
.brightness
;
918 static int stk_vidioc_s_ctrl(struct file
*filp
,
919 void *priv
, struct v4l2_control
*c
)
921 struct stk_camera
*dev
= priv
;
923 case V4L2_CID_BRIGHTNESS
:
924 dev
->vsettings
.brightness
= c
->value
;
925 return stk_sensor_set_brightness(dev
, c
->value
>> 8);
933 static int stk_vidioc_enum_fmt_vid_cap(struct file
*filp
,
934 void *priv
, struct v4l2_fmtdesc
*fmtd
)
936 switch (fmtd
->index
) {
938 fmtd
->pixelformat
= V4L2_PIX_FMT_RGB565
;
939 strcpy(fmtd
->description
, "r5g6b5");
942 fmtd
->pixelformat
= V4L2_PIX_FMT_RGB565X
;
943 strcpy(fmtd
->description
, "r5g6b5BE");
946 fmtd
->pixelformat
= V4L2_PIX_FMT_UYVY
;
947 strcpy(fmtd
->description
, "yuv4:2:2");
950 fmtd
->pixelformat
= V4L2_PIX_FMT_SBGGR8
;
951 strcpy(fmtd
->description
, "Raw bayer");
954 fmtd
->pixelformat
= V4L2_PIX_FMT_YUYV
;
955 strcpy(fmtd
->description
, "yuv4:2:2");
963 static struct stk_size
{
968 { .w
= 1280, .h
= 1024, .m
= MODE_SXGA
, },
969 { .w
= 640, .h
= 480, .m
= MODE_VGA
, },
970 { .w
= 352, .h
= 288, .m
= MODE_CIF
, },
971 { .w
= 320, .h
= 240, .m
= MODE_QVGA
, },
972 { .w
= 176, .h
= 144, .m
= MODE_QCIF
, },
975 static int stk_vidioc_g_fmt_vid_cap(struct file
*filp
,
976 void *priv
, struct v4l2_format
*f
)
978 struct v4l2_pix_format
*pix_format
= &f
->fmt
.pix
;
979 struct stk_camera
*dev
= priv
;
982 for (i
= 0; i
< ARRAY_SIZE(stk_sizes
)
983 && stk_sizes
[i
].m
!= dev
->vsettings
.mode
;
985 if (i
== ARRAY_SIZE(stk_sizes
)) {
986 STK_ERROR("ERROR: mode invalid\n");
989 pix_format
->width
= stk_sizes
[i
].w
;
990 pix_format
->height
= stk_sizes
[i
].h
;
991 pix_format
->field
= V4L2_FIELD_NONE
;
992 pix_format
->colorspace
= V4L2_COLORSPACE_SRGB
;
993 pix_format
->pixelformat
= dev
->vsettings
.palette
;
994 if (dev
->vsettings
.palette
== V4L2_PIX_FMT_SBGGR8
)
995 pix_format
->bytesperline
= pix_format
->width
;
997 pix_format
->bytesperline
= 2 * pix_format
->width
;
998 pix_format
->sizeimage
= pix_format
->bytesperline
999 * pix_format
->height
;
1003 static int stk_vidioc_try_fmt_vid_cap(struct file
*filp
,
1004 void *priv
, struct v4l2_format
*fmtd
)
1007 switch (fmtd
->fmt
.pix
.pixelformat
) {
1008 case V4L2_PIX_FMT_RGB565
:
1009 case V4L2_PIX_FMT_RGB565X
:
1010 case V4L2_PIX_FMT_UYVY
:
1011 case V4L2_PIX_FMT_YUYV
:
1012 case V4L2_PIX_FMT_SBGGR8
:
1017 for (i
= 1; i
< ARRAY_SIZE(stk_sizes
); i
++) {
1018 if (fmtd
->fmt
.pix
.width
> stk_sizes
[i
].w
)
1021 if (i
== ARRAY_SIZE(stk_sizes
)
1022 || (abs(fmtd
->fmt
.pix
.width
- stk_sizes
[i
-1].w
)
1023 < abs(fmtd
->fmt
.pix
.width
- stk_sizes
[i
].w
))) {
1024 fmtd
->fmt
.pix
.height
= stk_sizes
[i
-1].h
;
1025 fmtd
->fmt
.pix
.width
= stk_sizes
[i
-1].w
;
1026 fmtd
->fmt
.pix
.priv
= i
- 1;
1028 fmtd
->fmt
.pix
.height
= stk_sizes
[i
].h
;
1029 fmtd
->fmt
.pix
.width
= stk_sizes
[i
].w
;
1030 fmtd
->fmt
.pix
.priv
= i
;
1033 fmtd
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
1034 fmtd
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SRGB
;
1035 if (fmtd
->fmt
.pix
.pixelformat
== V4L2_PIX_FMT_SBGGR8
)
1036 fmtd
->fmt
.pix
.bytesperline
= fmtd
->fmt
.pix
.width
;
1038 fmtd
->fmt
.pix
.bytesperline
= 2 * fmtd
->fmt
.pix
.width
;
1039 fmtd
->fmt
.pix
.sizeimage
= fmtd
->fmt
.pix
.bytesperline
1040 * fmtd
->fmt
.pix
.height
;
1044 static int stk_setup_format(struct stk_camera
*dev
)
1048 if (dev
->vsettings
.palette
== V4L2_PIX_FMT_SBGGR8
)
1052 while (stk_sizes
[i
].m
!= dev
->vsettings
.mode
1053 && i
< ARRAY_SIZE(stk_sizes
))
1055 if (i
== ARRAY_SIZE(stk_sizes
)) {
1056 STK_ERROR("Something is broken in %s\n", __func__
);
1059 /* This registers controls some timings, not sure of what. */
1060 stk_camera_write_reg(dev
, 0x001b, 0x0e);
1061 if (dev
->vsettings
.mode
== MODE_SXGA
)
1062 stk_camera_write_reg(dev
, 0x001c, 0x0e);
1064 stk_camera_write_reg(dev
, 0x001c, 0x46);
1066 * Registers 0x0115 0x0114 are the size of each line (bytes),
1067 * regs 0x0117 0x0116 are the heigth of the image.
1069 stk_camera_write_reg(dev
, 0x0115,
1070 ((stk_sizes
[i
].w
* depth
) >> 8) & 0xff);
1071 stk_camera_write_reg(dev
, 0x0114,
1072 (stk_sizes
[i
].w
* depth
) & 0xff);
1073 stk_camera_write_reg(dev
, 0x0117,
1074 (stk_sizes
[i
].h
>> 8) & 0xff);
1075 stk_camera_write_reg(dev
, 0x0116,
1076 stk_sizes
[i
].h
& 0xff);
1077 return stk_sensor_configure(dev
);
1080 static int stk_vidioc_s_fmt_vid_cap(struct file
*filp
,
1081 void *priv
, struct v4l2_format
*fmtd
)
1084 struct stk_camera
*dev
= priv
;
1088 if (!is_present(dev
))
1090 if (is_streaming(dev
))
1092 if (dev
->owner
&& dev
->owner
!= filp
)
1094 ret
= stk_vidioc_try_fmt_vid_cap(filp
, priv
, fmtd
);
1099 dev
->vsettings
.palette
= fmtd
->fmt
.pix
.pixelformat
;
1100 stk_free_buffers(dev
);
1101 dev
->frame_size
= fmtd
->fmt
.pix
.sizeimage
;
1102 dev
->vsettings
.mode
= stk_sizes
[fmtd
->fmt
.pix
.priv
].m
;
1104 stk_initialise(dev
);
1105 return stk_setup_format(dev
);
1108 static int stk_vidioc_reqbufs(struct file
*filp
,
1109 void *priv
, struct v4l2_requestbuffers
*rb
)
1111 struct stk_camera
*dev
= priv
;
1115 if (rb
->memory
!= V4L2_MEMORY_MMAP
)
1117 if (is_streaming(dev
)
1118 || (dev
->owner
&& dev
->owner
!= filp
))
1122 /*FIXME If they ask for zero, we must stop streaming and free */
1125 /* Arbitrary limit */
1126 else if (rb
->count
> 5)
1129 stk_allocate_buffers(dev
, rb
->count
);
1130 rb
->count
= dev
->n_sbufs
;
1134 static int stk_vidioc_querybuf(struct file
*filp
,
1135 void *priv
, struct v4l2_buffer
*buf
)
1137 struct stk_camera
*dev
= priv
;
1138 struct stk_sio_buffer
*sbuf
;
1140 if (buf
->index
>= dev
->n_sbufs
)
1142 sbuf
= dev
->sio_bufs
+ buf
->index
;
1143 *buf
= sbuf
->v4lbuf
;
1147 static int stk_vidioc_qbuf(struct file
*filp
,
1148 void *priv
, struct v4l2_buffer
*buf
)
1150 struct stk_camera
*dev
= priv
;
1151 struct stk_sio_buffer
*sbuf
;
1152 unsigned long flags
;
1154 if (buf
->memory
!= V4L2_MEMORY_MMAP
)
1157 if (buf
->index
>= dev
->n_sbufs
)
1159 sbuf
= dev
->sio_bufs
+ buf
->index
;
1160 if (sbuf
->v4lbuf
.flags
& V4L2_BUF_FLAG_QUEUED
)
1162 sbuf
->v4lbuf
.flags
|= V4L2_BUF_FLAG_QUEUED
;
1163 sbuf
->v4lbuf
.flags
&= ~V4L2_BUF_FLAG_DONE
;
1164 spin_lock_irqsave(&dev
->spinlock
, flags
);
1165 list_add_tail(&sbuf
->list
, &dev
->sio_avail
);
1166 *buf
= sbuf
->v4lbuf
;
1167 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
1171 static int stk_vidioc_dqbuf(struct file
*filp
,
1172 void *priv
, struct v4l2_buffer
*buf
)
1174 struct stk_camera
*dev
= priv
;
1175 struct stk_sio_buffer
*sbuf
;
1176 unsigned long flags
;
1179 if (!is_streaming(dev
))
1182 if (filp
->f_flags
& O_NONBLOCK
&& list_empty(&dev
->sio_full
))
1183 return -EWOULDBLOCK
;
1184 ret
= wait_event_interruptible(dev
->wait_frame
,
1185 !list_empty(&dev
->sio_full
) || !is_present(dev
));
1188 if (!is_present(dev
))
1191 spin_lock_irqsave(&dev
->spinlock
, flags
);
1192 sbuf
= list_first_entry(&dev
->sio_full
, struct stk_sio_buffer
, list
);
1193 list_del_init(&sbuf
->list
);
1194 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
1195 sbuf
->v4lbuf
.flags
&= ~V4L2_BUF_FLAG_QUEUED
;
1196 sbuf
->v4lbuf
.flags
|= V4L2_BUF_FLAG_DONE
;
1197 sbuf
->v4lbuf
.sequence
= ++dev
->sequence
;
1198 do_gettimeofday(&sbuf
->v4lbuf
.timestamp
);
1200 *buf
= sbuf
->v4lbuf
;
1204 static int stk_vidioc_streamon(struct file
*filp
,
1205 void *priv
, enum v4l2_buf_type type
)
1207 struct stk_camera
*dev
= priv
;
1208 if (is_streaming(dev
))
1210 if (dev
->sio_bufs
== NULL
)
1213 return stk_start_stream(dev
);
1216 static int stk_vidioc_streamoff(struct file
*filp
,
1217 void *priv
, enum v4l2_buf_type type
)
1219 struct stk_camera
*dev
= priv
;
1220 unsigned long flags
;
1222 stk_stop_stream(dev
);
1223 spin_lock_irqsave(&dev
->spinlock
, flags
);
1224 INIT_LIST_HEAD(&dev
->sio_avail
);
1225 INIT_LIST_HEAD(&dev
->sio_full
);
1226 for (i
= 0; i
< dev
->n_sbufs
; i
++) {
1227 INIT_LIST_HEAD(&dev
->sio_bufs
[i
].list
);
1228 dev
->sio_bufs
[i
].v4lbuf
.flags
= 0;
1230 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
1235 static int stk_vidioc_g_parm(struct file
*filp
,
1236 void *priv
, struct v4l2_streamparm
*sp
)
1238 /*FIXME This is not correct */
1239 sp
->parm
.capture
.timeperframe
.numerator
= 1;
1240 sp
->parm
.capture
.timeperframe
.denominator
= 30;
1241 sp
->parm
.capture
.readbuffers
= 2;
1245 static int stk_vidioc_enum_framesizes(struct file
*filp
,
1246 void *priv
, struct v4l2_frmsizeenum
*frms
)
1248 if (frms
->index
>= ARRAY_SIZE(stk_sizes
))
1250 switch (frms
->pixel_format
) {
1251 case V4L2_PIX_FMT_RGB565
:
1252 case V4L2_PIX_FMT_RGB565X
:
1253 case V4L2_PIX_FMT_UYVY
:
1254 case V4L2_PIX_FMT_YUYV
:
1255 case V4L2_PIX_FMT_SBGGR8
:
1256 frms
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1257 frms
->discrete
.width
= stk_sizes
[frms
->index
].w
;
1258 frms
->discrete
.height
= stk_sizes
[frms
->index
].h
;
1260 default: return -EINVAL
;
1264 static struct v4l2_file_operations v4l_stk_fops
= {
1265 .owner
= THIS_MODULE
,
1266 .open
= v4l_stk_open
,
1267 .release
= v4l_stk_release
,
1268 .read
= v4l_stk_read
,
1269 .poll
= v4l_stk_poll
,
1270 .mmap
= v4l_stk_mmap
,
1271 .ioctl
= video_ioctl2
,
1274 static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops
= {
1275 .vidioc_querycap
= stk_vidioc_querycap
,
1276 .vidioc_enum_fmt_vid_cap
= stk_vidioc_enum_fmt_vid_cap
,
1277 .vidioc_try_fmt_vid_cap
= stk_vidioc_try_fmt_vid_cap
,
1278 .vidioc_s_fmt_vid_cap
= stk_vidioc_s_fmt_vid_cap
,
1279 .vidioc_g_fmt_vid_cap
= stk_vidioc_g_fmt_vid_cap
,
1280 .vidioc_enum_input
= stk_vidioc_enum_input
,
1281 .vidioc_s_input
= stk_vidioc_s_input
,
1282 .vidioc_g_input
= stk_vidioc_g_input
,
1283 .vidioc_s_std
= stk_vidioc_s_std
,
1284 .vidioc_reqbufs
= stk_vidioc_reqbufs
,
1285 .vidioc_querybuf
= stk_vidioc_querybuf
,
1286 .vidioc_qbuf
= stk_vidioc_qbuf
,
1287 .vidioc_dqbuf
= stk_vidioc_dqbuf
,
1288 .vidioc_streamon
= stk_vidioc_streamon
,
1289 .vidioc_streamoff
= stk_vidioc_streamoff
,
1290 .vidioc_queryctrl
= stk_vidioc_queryctrl
,
1291 .vidioc_g_ctrl
= stk_vidioc_g_ctrl
,
1292 .vidioc_s_ctrl
= stk_vidioc_s_ctrl
,
1293 .vidioc_g_parm
= stk_vidioc_g_parm
,
1294 .vidioc_enum_framesizes
= stk_vidioc_enum_framesizes
,
1297 static void stk_v4l_dev_release(struct video_device
*vd
)
1299 struct stk_camera
*dev
= vdev_to_camera(vd
);
1301 if (dev
->sio_bufs
!= NULL
|| dev
->isobufs
!= NULL
)
1302 STK_ERROR("We are leaking memory\n");
1303 usb_put_intf(dev
->interface
);
1307 static struct video_device stk_v4l_data
= {
1308 .name
= "stkwebcam",
1310 .tvnorms
= V4L2_STD_UNKNOWN
,
1311 .current_norm
= V4L2_STD_UNKNOWN
,
1312 .fops
= &v4l_stk_fops
,
1313 .ioctl_ops
= &v4l_stk_ioctl_ops
,
1314 .release
= stk_v4l_dev_release
,
1318 static int stk_register_video_device(struct stk_camera
*dev
)
1322 dev
->vdev
= stk_v4l_data
;
1323 dev
->vdev
.debug
= debug
;
1324 dev
->vdev
.parent
= &dev
->interface
->dev
;
1325 err
= video_register_device(&dev
->vdev
, VFL_TYPE_GRABBER
, -1);
1327 STK_ERROR("v4l registration failed\n");
1329 STK_INFO("Syntek USB2.0 Camera is now controlling video device"
1330 " /dev/video%d\n", dev
->vdev
.num
);
1337 static int stk_camera_probe(struct usb_interface
*interface
,
1338 const struct usb_device_id
*id
)
1343 struct stk_camera
*dev
= NULL
;
1344 struct usb_device
*udev
= interface_to_usbdev(interface
);
1345 struct usb_host_interface
*iface_desc
;
1346 struct usb_endpoint_descriptor
*endpoint
;
1348 dev
= kzalloc(sizeof(struct stk_camera
), GFP_KERNEL
);
1350 STK_ERROR("Out of memory !\n");
1354 spin_lock_init(&dev
->spinlock
);
1355 init_waitqueue_head(&dev
->wait_frame
);
1358 dev
->interface
= interface
;
1359 usb_get_intf(interface
);
1361 dev
->vsettings
.vflip
= vflip
;
1362 dev
->vsettings
.hflip
= hflip
;
1366 /* Set up the endpoint information
1367 * use only the first isoc-in endpoint
1368 * for the current alternate setting */
1369 iface_desc
= interface
->cur_altsetting
;
1371 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
1372 endpoint
= &iface_desc
->endpoint
[i
].desc
;
1375 && usb_endpoint_is_isoc_in(endpoint
)) {
1376 /* we found an isoc in endpoint */
1377 dev
->isoc_ep
= usb_endpoint_num(endpoint
);
1381 if (!dev
->isoc_ep
) {
1382 STK_ERROR("Could not find isoc-in endpoint");
1386 dev
->vsettings
.brightness
= 0x7fff;
1387 dev
->vsettings
.palette
= V4L2_PIX_FMT_RGB565
;
1388 dev
->vsettings
.mode
= MODE_VGA
;
1389 dev
->frame_size
= 640 * 480 * 2;
1391 INIT_LIST_HEAD(&dev
->sio_avail
);
1392 INIT_LIST_HEAD(&dev
->sio_full
);
1394 usb_set_intfdata(interface
, dev
);
1396 err
= stk_register_video_device(dev
);
1401 stk_create_sysfs_files(&dev
->vdev
);
1402 usb_autopm_enable(dev
->interface
);
1411 static void stk_camera_disconnect(struct usb_interface
*interface
)
1413 struct stk_camera
*dev
= usb_get_intfdata(interface
);
1415 usb_set_intfdata(interface
, NULL
);
1418 wake_up_interruptible(&dev
->wait_frame
);
1419 stk_remove_sysfs_files(&dev
->vdev
);
1421 STK_INFO("Syntek USB2.0 Camera release resources "
1422 "video device /dev/video%d\n", dev
->vdev
.num
);
1424 video_unregister_device(&dev
->vdev
);
1428 static int stk_camera_suspend(struct usb_interface
*intf
, pm_message_t message
)
1430 struct stk_camera
*dev
= usb_get_intfdata(intf
);
1431 if (is_streaming(dev
)) {
1432 stk_stop_stream(dev
);
1433 /* yes, this is ugly */
1439 static int stk_camera_resume(struct usb_interface
*intf
)
1441 struct stk_camera
*dev
= usb_get_intfdata(intf
);
1442 if (!is_initialised(dev
))
1444 unset_initialised(dev
);
1445 stk_initialise(dev
);
1446 stk_setup_format(dev
);
1447 if (is_streaming(dev
))
1448 stk_start_stream(dev
);
1453 static struct usb_driver stk_camera_driver
= {
1454 .name
= "stkwebcam",
1455 .probe
= stk_camera_probe
,
1456 .disconnect
= stk_camera_disconnect
,
1457 .id_table
= stkwebcam_table
,
1459 .suspend
= stk_camera_suspend
,
1460 .resume
= stk_camera_resume
,
1465 static int __init
stk_camera_init(void)
1469 result
= usb_register(&stk_camera_driver
);
1471 STK_ERROR("usb_register failed ! Error number %d\n", result
);
1477 static void __exit
stk_camera_exit(void)
1479 usb_deregister(&stk_camera_driver
);
1482 module_init(stk_camera_init
);
1483 module_exit(stk_camera_exit
);