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>
30 #include <linux/smp_lock.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"
43 module_param(hflip
, bool, 0444);
44 MODULE_PARM_DESC(hflip
, "Horizontal image flip (mirror). Defaults to 1");
47 module_param(vflip
, bool, 0444);
48 MODULE_PARM_DESC(vflip
, "Vertical image flip. Defaults to 1");
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");
60 /* Some cameras have audio interfaces, we aren't interested in those */
61 static struct usb_device_id stkwebcam_table
[] = {
62 { USB_DEVICE_AND_INTERFACE_INFO(0x174f, 0xa311, 0xff, 0xff, 0xff) },
63 { USB_DEVICE_AND_INTERFACE_INFO(0x05e1, 0x0501, 0xff, 0xff, 0xff) },
66 MODULE_DEVICE_TABLE(usb
, stkwebcam_table
);
71 int stk_camera_write_reg(struct stk_camera
*dev
, u16 index
, u8 value
)
73 struct usb_device
*udev
= dev
->udev
;
76 ret
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
78 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
90 int stk_camera_read_reg(struct stk_camera
*dev
, u16 index
, int *value
)
92 struct usb_device
*udev
= dev
->udev
;
95 ret
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
97 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
109 static int stk_start_stream(struct stk_camera
*dev
)
113 int value_116
, value_117
;
115 if (!is_present(dev
))
117 if (!is_memallocd(dev
) || !is_initialised(dev
)) {
118 STK_ERROR("FIXME: Buffers are not allocated\n");
121 ret
= usb_set_interface(dev
->udev
, 0, 5);
124 STK_ERROR("usb_set_interface failed !\n");
125 if (stk_sensor_wakeup(dev
))
126 STK_ERROR("error awaking the sensor\n");
128 stk_camera_read_reg(dev
, 0x0116, &value_116
);
129 stk_camera_read_reg(dev
, 0x0117, &value_117
);
131 stk_camera_write_reg(dev
, 0x0116, 0x0000);
132 stk_camera_write_reg(dev
, 0x0117, 0x0000);
134 stk_camera_read_reg(dev
, 0x0100, &value
);
135 stk_camera_write_reg(dev
, 0x0100, value
| 0x80);
137 stk_camera_write_reg(dev
, 0x0116, value_116
);
138 stk_camera_write_reg(dev
, 0x0117, value_117
);
139 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
140 if (dev
->isobufs
[i
].urb
) {
141 ret
= usb_submit_urb(dev
->isobufs
[i
].urb
, GFP_KERNEL
);
142 atomic_inc(&dev
->urbs_used
);
151 static int stk_stop_stream(struct stk_camera
*dev
)
155 if (is_present(dev
)) {
156 stk_camera_read_reg(dev
, 0x0100, &value
);
157 stk_camera_write_reg(dev
, 0x0100, value
& ~0x80);
158 if (dev
->isobufs
!= NULL
) {
159 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
160 if (dev
->isobufs
[i
].urb
)
161 usb_kill_urb(dev
->isobufs
[i
].urb
);
164 unset_streaming(dev
);
166 if (usb_set_interface(dev
->udev
, 0, 0))
167 STK_ERROR("usb_set_interface failed !\n");
168 if (stk_sensor_sleep(dev
))
169 STK_ERROR("error suspending the sensor\n");
175 * This seems to be the shortest init sequence we
176 * must do in order to find the sensor
177 * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
178 * is also reset. Maybe powers down it?
179 * Rest of values don't make a difference
182 static struct regval stk1125_initvals
[] = {
183 /*TODO: What means this sequence? */
212 static int stk_initialise(struct stk_camera
*dev
)
216 if (!is_present(dev
))
218 if (is_initialised(dev
))
220 rv
= stk1125_initvals
;
221 while (rv
->reg
!= 0xffff) {
222 ret
= stk_camera_write_reg(dev
, rv
->reg
, rv
->val
);
227 if (stk_sensor_init(dev
) == 0) {
228 set_initialised(dev
);
234 #ifdef CONFIG_VIDEO_V4L1_COMPAT
236 /* sysfs functions */
237 /*FIXME cleanup this */
239 static ssize_t
show_brightness(struct device
*class,
240 struct device_attribute
*attr
, char *buf
)
242 struct video_device
*vdev
= to_video_device(class);
243 struct stk_camera
*dev
= vdev_to_camera(vdev
);
245 return sprintf(buf
, "%X\n", dev
->vsettings
.brightness
);
248 static ssize_t
store_brightness(struct device
*class,
249 struct device_attribute
*attr
, const char *buf
, size_t count
)
255 struct video_device
*vdev
= to_video_device(class);
256 struct stk_camera
*dev
= vdev_to_camera(vdev
);
258 value
= simple_strtoul(buf
, &endp
, 16);
260 dev
->vsettings
.brightness
= (int) value
;
262 ret
= stk_sensor_set_brightness(dev
, value
>> 8);
269 static ssize_t
show_hflip(struct device
*class,
270 struct device_attribute
*attr
, char *buf
)
272 struct video_device
*vdev
= to_video_device(class);
273 struct stk_camera
*dev
= vdev_to_camera(vdev
);
275 return sprintf(buf
, "%d\n", dev
->vsettings
.hflip
);
278 static ssize_t
store_hflip(struct device
*class,
279 struct device_attribute
*attr
, const char *buf
, size_t count
)
281 struct video_device
*vdev
= to_video_device(class);
282 struct stk_camera
*dev
= vdev_to_camera(vdev
);
284 if (strncmp(buf
, "1", 1) == 0)
285 dev
->vsettings
.hflip
= 1;
286 else if (strncmp(buf
, "0", 1) == 0)
287 dev
->vsettings
.hflip
= 0;
294 static ssize_t
show_vflip(struct device
*class,
295 struct device_attribute
*attr
, char *buf
)
297 struct video_device
*vdev
= to_video_device(class);
298 struct stk_camera
*dev
= vdev_to_camera(vdev
);
300 return sprintf(buf
, "%d\n", dev
->vsettings
.vflip
);
303 static ssize_t
store_vflip(struct device
*class,
304 struct device_attribute
*attr
, const char *buf
, size_t count
)
306 struct video_device
*vdev
= to_video_device(class);
307 struct stk_camera
*dev
= vdev_to_camera(vdev
);
309 if (strncmp(buf
, "1", 1) == 0)
310 dev
->vsettings
.vflip
= 1;
311 else if (strncmp(buf
, "0", 1) == 0)
312 dev
->vsettings
.vflip
= 0;
319 static DEVICE_ATTR(brightness
, S_IRUGO
| S_IWUGO
,
320 show_brightness
, store_brightness
);
321 static DEVICE_ATTR(hflip
, S_IRUGO
| S_IWUGO
, show_hflip
, store_hflip
);
322 static DEVICE_ATTR(vflip
, S_IRUGO
| S_IWUGO
, show_vflip
, store_vflip
);
324 static int stk_create_sysfs_files(struct video_device
*vdev
)
328 ret
= device_create_file(&vdev
->dev
, &dev_attr_brightness
);
329 ret
+= device_create_file(&vdev
->dev
, &dev_attr_hflip
);
330 ret
+= device_create_file(&vdev
->dev
, &dev_attr_vflip
);
332 STK_WARNING("Could not create sysfs files\n");
336 static void stk_remove_sysfs_files(struct video_device
*vdev
)
338 device_remove_file(&vdev
->dev
, &dev_attr_brightness
);
339 device_remove_file(&vdev
->dev
, &dev_attr_hflip
);
340 device_remove_file(&vdev
->dev
, &dev_attr_vflip
);
344 #define stk_create_sysfs_files(a)
345 #define stk_remove_sysfs_files(a)
348 /* *********************************************** */
350 * This function is called as an URB transfert is complete (Isochronous pipe).
351 * So, the traitement is done in interrupt time, so it has be fast, not crash,
352 * and not stall. Neat.
354 static void stk_isoc_handler(struct urb
*urb
)
361 unsigned char *fill
= NULL
;
362 unsigned char *iso_buf
= NULL
;
364 struct stk_camera
*dev
;
365 struct stk_sio_buffer
*fb
;
367 dev
= (struct stk_camera
*) urb
->context
;
370 STK_ERROR("isoc_handler called with NULL device !\n");
374 if (urb
->status
== -ENOENT
|| urb
->status
== -ECONNRESET
375 || urb
->status
== -ESHUTDOWN
) {
376 atomic_dec(&dev
->urbs_used
);
380 spin_lock_irqsave(&dev
->spinlock
, flags
);
382 if (urb
->status
!= -EINPROGRESS
&& urb
->status
!= 0) {
383 STK_ERROR("isoc_handler: urb->status == %d\n", urb
->status
);
387 if (list_empty(&dev
->sio_avail
)) {
388 /*FIXME Stop streaming after a while */
389 (void) (printk_ratelimit() &&
390 STK_ERROR("isoc_handler without available buffer!\n"));
393 fb
= list_first_entry(&dev
->sio_avail
,
394 struct stk_sio_buffer
, list
);
395 fill
= fb
->buffer
+ fb
->v4lbuf
.bytesused
;
397 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
398 if (urb
->iso_frame_desc
[i
].status
!= 0) {
399 if (urb
->iso_frame_desc
[i
].status
!= -EXDEV
)
400 STK_ERROR("Frame %d has error %d\n", i
,
401 urb
->iso_frame_desc
[i
].status
);
404 framelen
= urb
->iso_frame_desc
[i
].actual_length
;
405 iso_buf
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
408 continue; /* no data */
411 * we found something informational from there
412 * the isoc frames have to type of headers
413 * type1: 00 xx 00 00 or 20 xx 00 00
414 * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
415 * xx is a sequencer which has never been seen over 0x3f
416 * imho data written down looks like bayer, i see similarities
417 * after every 640 bytes
419 if (*iso_buf
& 0x80) {
422 /* This marks a new frame */
423 if (fb
->v4lbuf
.bytesused
!= 0
424 && fb
->v4lbuf
.bytesused
!= dev
->frame_size
) {
425 (void) (printk_ratelimit() &&
426 STK_ERROR("frame %d, "
427 "bytesused=%d, skipping\n",
428 i
, fb
->v4lbuf
.bytesused
));
429 fb
->v4lbuf
.bytesused
= 0;
431 } else if (fb
->v4lbuf
.bytesused
== dev
->frame_size
) {
432 if (list_is_singular(&dev
->sio_avail
)) {
433 /* Always reuse the last buffer */
434 fb
->v4lbuf
.bytesused
= 0;
437 list_move_tail(dev
->sio_avail
.next
,
439 wake_up(&dev
->wait_frame
);
440 fb
= list_first_entry(&dev
->sio_avail
,
441 struct stk_sio_buffer
, list
);
442 fb
->v4lbuf
.bytesused
= 0;
451 /* Our buffer is full !!! */
452 if (framelen
+ fb
->v4lbuf
.bytesused
> dev
->frame_size
) {
453 (void) (printk_ratelimit() &&
454 STK_ERROR("Frame buffer overflow, lost sync\n"));
455 /*FIXME Do something here? */
458 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
459 memcpy(fill
, iso_buf
, framelen
);
460 spin_lock_irqsave(&dev
->spinlock
, flags
);
463 /* New size of our buffer */
464 fb
->v4lbuf
.bytesused
+= framelen
;
468 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
469 urb
->dev
= dev
->udev
;
470 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
472 STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
477 /* -------------------------------------------- */
479 static int stk_prepare_iso(struct stk_camera
*dev
)
484 struct usb_device
*udev
;
491 STK_ERROR("isobufs already allocated. Bad\n");
493 dev
->isobufs
= kzalloc(MAX_ISO_BUFS
* sizeof(*dev
->isobufs
),
495 if (dev
->isobufs
== NULL
) {
496 STK_ERROR("Unable to allocate iso buffers\n");
499 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
500 if (dev
->isobufs
[i
].data
== NULL
) {
501 kbuf
= kzalloc(ISO_BUFFER_SIZE
, GFP_KERNEL
);
503 STK_ERROR("Failed to allocate iso buffer %d\n",
507 dev
->isobufs
[i
].data
= kbuf
;
509 STK_ERROR("isobuf data already allocated\n");
510 if (dev
->isobufs
[i
].urb
== NULL
) {
511 urb
= usb_alloc_urb(ISO_FRAMES_PER_DESC
, GFP_KERNEL
);
513 STK_ERROR("Failed to allocate URB %d\n", i
);
516 dev
->isobufs
[i
].urb
= urb
;
518 STK_ERROR("Killing URB\n");
519 usb_kill_urb(dev
->isobufs
[i
].urb
);
520 urb
= dev
->isobufs
[i
].urb
;
524 urb
->pipe
= usb_rcvisocpipe(udev
, dev
->isoc_ep
);
525 urb
->transfer_flags
= URB_ISO_ASAP
;
526 urb
->transfer_buffer
= dev
->isobufs
[i
].data
;
527 urb
->transfer_buffer_length
= ISO_BUFFER_SIZE
;
528 urb
->complete
= stk_isoc_handler
;
530 urb
->start_frame
= 0;
531 urb
->number_of_packets
= ISO_FRAMES_PER_DESC
;
533 for (j
= 0; j
< ISO_FRAMES_PER_DESC
; j
++) {
534 urb
->iso_frame_desc
[j
].offset
= j
* ISO_MAX_FRAME_SIZE
;
535 urb
->iso_frame_desc
[j
].length
= ISO_MAX_FRAME_SIZE
;
542 for (i
= 0; i
< MAX_ISO_BUFS
&& dev
->isobufs
[i
].data
; i
++)
543 kfree(dev
->isobufs
[i
].data
);
544 for (i
= 0; i
< MAX_ISO_BUFS
&& dev
->isobufs
[i
].urb
; i
++)
545 usb_free_urb(dev
->isobufs
[i
].urb
);
551 static void stk_clean_iso(struct stk_camera
*dev
)
555 if (dev
== NULL
|| dev
->isobufs
== NULL
)
558 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
561 urb
= dev
->isobufs
[i
].urb
;
563 if (atomic_read(&dev
->urbs_used
) && is_present(dev
))
567 kfree(dev
->isobufs
[i
].data
);
571 unset_memallocd(dev
);
574 static int stk_setup_siobuf(struct stk_camera
*dev
, int index
)
576 struct stk_sio_buffer
*buf
= dev
->sio_bufs
+ index
;
577 INIT_LIST_HEAD(&buf
->list
);
578 buf
->v4lbuf
.length
= PAGE_ALIGN(dev
->frame_size
);
579 buf
->buffer
= vmalloc_user(buf
->v4lbuf
.length
);
580 if (buf
->buffer
== NULL
)
584 buf
->v4lbuf
.index
= index
;
585 buf
->v4lbuf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
586 buf
->v4lbuf
.field
= V4L2_FIELD_NONE
;
587 buf
->v4lbuf
.memory
= V4L2_MEMORY_MMAP
;
588 buf
->v4lbuf
.m
.offset
= 2*index
*buf
->v4lbuf
.length
;
592 static int stk_free_sio_buffers(struct stk_camera
*dev
)
597 if (dev
->n_sbufs
== 0 || dev
->sio_bufs
== NULL
)
600 * If any buffers are mapped, we cannot free them at all.
602 for (i
= 0; i
< dev
->n_sbufs
; i
++) {
603 if (dev
->sio_bufs
[i
].mapcount
> 0)
609 spin_lock_irqsave(&dev
->spinlock
, flags
);
610 INIT_LIST_HEAD(&dev
->sio_avail
);
611 INIT_LIST_HEAD(&dev
->sio_full
);
612 nbufs
= dev
->n_sbufs
;
614 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
615 for (i
= 0; i
< nbufs
; i
++) {
616 if (dev
->sio_bufs
[i
].buffer
!= NULL
)
617 vfree(dev
->sio_bufs
[i
].buffer
);
619 kfree(dev
->sio_bufs
);
620 dev
->sio_bufs
= NULL
;
624 static int stk_prepare_sio_buffers(struct stk_camera
*dev
, unsigned n_sbufs
)
627 if (dev
->sio_bufs
!= NULL
)
628 STK_ERROR("sio_bufs already allocated\n");
630 dev
->sio_bufs
= kzalloc(n_sbufs
* sizeof(struct stk_sio_buffer
),
632 if (dev
->sio_bufs
== NULL
)
634 for (i
= 0; i
< n_sbufs
; i
++) {
635 if (stk_setup_siobuf(dev
, i
))
636 return (dev
->n_sbufs
> 1)? 0 : -ENOMEM
;
643 static int stk_allocate_buffers(struct stk_camera
*dev
, unsigned n_sbufs
)
646 err
= stk_prepare_iso(dev
);
651 err
= stk_prepare_sio_buffers(dev
, n_sbufs
);
653 stk_free_sio_buffers(dev
);
659 static void stk_free_buffers(struct stk_camera
*dev
)
662 stk_free_sio_buffers(dev
);
664 /* -------------------------------------------- */
666 /* v4l file operations */
668 static int v4l_stk_open(struct file
*fp
)
670 struct stk_camera
*dev
;
671 struct video_device
*vdev
;
673 vdev
= video_devdata(fp
);
674 dev
= vdev_to_camera(vdev
);
677 if (dev
== NULL
|| !is_present(dev
)) {
681 fp
->private_data
= dev
;
682 usb_autopm_get_interface(dev
->interface
);
688 static int v4l_stk_release(struct file
*fp
)
690 struct stk_camera
*dev
= fp
->private_data
;
692 if (dev
->owner
== fp
) {
693 stk_stop_stream(dev
);
694 stk_free_buffers(dev
);
699 usb_autopm_put_interface(dev
->interface
);
704 static ssize_t
v4l_stk_read(struct file
*fp
, char __user
*buf
,
705 size_t count
, loff_t
*f_pos
)
710 struct stk_sio_buffer
*sbuf
;
711 struct stk_camera
*dev
= fp
->private_data
;
713 if (!is_present(dev
))
715 if (dev
->owner
&& dev
->owner
!= fp
)
718 if (!is_streaming(dev
)) {
719 if (stk_initialise(dev
)
720 || stk_allocate_buffers(dev
, 3)
721 || stk_start_stream(dev
))
723 spin_lock_irqsave(&dev
->spinlock
, flags
);
724 for (i
= 0; i
< dev
->n_sbufs
; i
++) {
725 list_add_tail(&dev
->sio_bufs
[i
].list
, &dev
->sio_avail
);
726 dev
->sio_bufs
[i
].v4lbuf
.flags
= V4L2_BUF_FLAG_QUEUED
;
728 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
731 if (fp
->f_flags
& O_NONBLOCK
&& list_empty(&dev
->sio_full
))
733 ret
= wait_event_interruptible(dev
->wait_frame
,
734 !list_empty(&dev
->sio_full
) || !is_present(dev
));
737 if (!is_present(dev
))
740 if (count
+ *f_pos
> dev
->frame_size
)
741 count
= dev
->frame_size
- *f_pos
;
742 spin_lock_irqsave(&dev
->spinlock
, flags
);
743 if (list_empty(&dev
->sio_full
)) {
744 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
745 STK_ERROR("BUG: No siobufs ready\n");
748 sbuf
= list_first_entry(&dev
->sio_full
, struct stk_sio_buffer
, list
);
749 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
751 if (copy_to_user(buf
, sbuf
->buffer
+ *f_pos
, count
))
756 if (*f_pos
>= dev
->frame_size
) {
758 spin_lock_irqsave(&dev
->spinlock
, flags
);
759 list_move_tail(&sbuf
->list
, &dev
->sio_avail
);
760 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
765 static unsigned int v4l_stk_poll(struct file
*fp
, poll_table
*wait
)
767 struct stk_camera
*dev
= fp
->private_data
;
769 poll_wait(fp
, &dev
->wait_frame
, wait
);
771 if (!is_present(dev
))
774 if (!list_empty(&dev
->sio_full
))
775 return (POLLIN
| POLLRDNORM
);
781 static void stk_v4l_vm_open(struct vm_area_struct
*vma
)
783 struct stk_sio_buffer
*sbuf
= vma
->vm_private_data
;
786 static void stk_v4l_vm_close(struct vm_area_struct
*vma
)
788 struct stk_sio_buffer
*sbuf
= vma
->vm_private_data
;
790 if (sbuf
->mapcount
== 0)
791 sbuf
->v4lbuf
.flags
&= ~V4L2_BUF_FLAG_MAPPED
;
793 static const struct vm_operations_struct stk_v4l_vm_ops
= {
794 .open
= stk_v4l_vm_open
,
795 .close
= stk_v4l_vm_close
798 static int v4l_stk_mmap(struct file
*fp
, struct vm_area_struct
*vma
)
802 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
803 struct stk_camera
*dev
= fp
->private_data
;
804 struct stk_sio_buffer
*sbuf
= NULL
;
806 if (!(vma
->vm_flags
& VM_WRITE
) || !(vma
->vm_flags
& VM_SHARED
))
809 for (i
= 0; i
< dev
->n_sbufs
; i
++) {
810 if (dev
->sio_bufs
[i
].v4lbuf
.m
.offset
== offset
) {
811 sbuf
= dev
->sio_bufs
+ i
;
817 ret
= remap_vmalloc_range(vma
, sbuf
->buffer
, 0);
820 vma
->vm_flags
|= VM_DONTEXPAND
;
821 vma
->vm_private_data
= sbuf
;
822 vma
->vm_ops
= &stk_v4l_vm_ops
;
823 sbuf
->v4lbuf
.flags
|= V4L2_BUF_FLAG_MAPPED
;
824 stk_v4l_vm_open(vma
);
828 /* v4l ioctl handlers */
830 static int stk_vidioc_querycap(struct file
*filp
,
831 void *priv
, struct v4l2_capability
*cap
)
833 strcpy(cap
->driver
, "stk");
834 strcpy(cap
->card
, "stk");
835 cap
->version
= DRIVER_VERSION_NUM
;
837 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
838 | V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
842 static int stk_vidioc_enum_input(struct file
*filp
,
843 void *priv
, struct v4l2_input
*input
)
845 if (input
->index
!= 0)
848 strcpy(input
->name
, "Syntek USB Camera");
849 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
854 static int stk_vidioc_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
860 static int stk_vidioc_s_input(struct file
*filp
, void *priv
, unsigned int i
)
869 static int stk_vidioc_s_std(struct file
*filp
, void *priv
, v4l2_std_id
*a
)
874 /* List of all V4Lv2 controls supported by the driver */
875 static struct v4l2_queryctrl stk_controls
[] = {
877 .id
= V4L2_CID_BRIGHTNESS
,
878 .type
= V4L2_CTRL_TYPE_INTEGER
,
879 .name
= "Brightness",
883 .default_value
= 0x6000,
885 /*TODO: get more controls to work */
888 static int stk_vidioc_queryctrl(struct file
*filp
,
889 void *priv
, struct v4l2_queryctrl
*c
)
893 nbr
= ARRAY_SIZE(stk_controls
);
895 for (i
= 0; i
< nbr
; i
++) {
896 if (stk_controls
[i
].id
== c
->id
) {
897 memcpy(c
, &stk_controls
[i
],
898 sizeof(struct v4l2_queryctrl
));
905 static int stk_vidioc_g_ctrl(struct file
*filp
,
906 void *priv
, struct v4l2_control
*c
)
908 struct stk_camera
*dev
= priv
;
910 case V4L2_CID_BRIGHTNESS
:
911 c
->value
= dev
->vsettings
.brightness
;
919 static int stk_vidioc_s_ctrl(struct file
*filp
,
920 void *priv
, struct v4l2_control
*c
)
922 struct stk_camera
*dev
= priv
;
924 case V4L2_CID_BRIGHTNESS
:
925 dev
->vsettings
.brightness
= c
->value
;
926 return stk_sensor_set_brightness(dev
, c
->value
>> 8);
934 static int stk_vidioc_enum_fmt_vid_cap(struct file
*filp
,
935 void *priv
, struct v4l2_fmtdesc
*fmtd
)
937 switch (fmtd
->index
) {
939 fmtd
->pixelformat
= V4L2_PIX_FMT_RGB565
;
940 strcpy(fmtd
->description
, "r5g6b5");
943 fmtd
->pixelformat
= V4L2_PIX_FMT_RGB565X
;
944 strcpy(fmtd
->description
, "r5g6b5BE");
947 fmtd
->pixelformat
= V4L2_PIX_FMT_UYVY
;
948 strcpy(fmtd
->description
, "yuv4:2:2");
951 fmtd
->pixelformat
= V4L2_PIX_FMT_SBGGR8
;
952 strcpy(fmtd
->description
, "Raw bayer");
955 fmtd
->pixelformat
= V4L2_PIX_FMT_YUYV
;
956 strcpy(fmtd
->description
, "yuv4:2:2");
964 static struct stk_size
{
969 { .w
= 1280, .h
= 1024, .m
= MODE_SXGA
, },
970 { .w
= 640, .h
= 480, .m
= MODE_VGA
, },
971 { .w
= 352, .h
= 288, .m
= MODE_CIF
, },
972 { .w
= 320, .h
= 240, .m
= MODE_QVGA
, },
973 { .w
= 176, .h
= 144, .m
= MODE_QCIF
, },
976 static int stk_vidioc_g_fmt_vid_cap(struct file
*filp
,
977 void *priv
, struct v4l2_format
*f
)
979 struct v4l2_pix_format
*pix_format
= &f
->fmt
.pix
;
980 struct stk_camera
*dev
= priv
;
983 for (i
= 0; i
< ARRAY_SIZE(stk_sizes
)
984 && stk_sizes
[i
].m
!= dev
->vsettings
.mode
;
986 if (i
== ARRAY_SIZE(stk_sizes
)) {
987 STK_ERROR("ERROR: mode invalid\n");
990 pix_format
->width
= stk_sizes
[i
].w
;
991 pix_format
->height
= stk_sizes
[i
].h
;
992 pix_format
->field
= V4L2_FIELD_NONE
;
993 pix_format
->colorspace
= V4L2_COLORSPACE_SRGB
;
994 pix_format
->pixelformat
= dev
->vsettings
.palette
;
995 if (dev
->vsettings
.palette
== V4L2_PIX_FMT_SBGGR8
)
996 pix_format
->bytesperline
= pix_format
->width
;
998 pix_format
->bytesperline
= 2 * pix_format
->width
;
999 pix_format
->sizeimage
= pix_format
->bytesperline
1000 * pix_format
->height
;
1004 static int stk_vidioc_try_fmt_vid_cap(struct file
*filp
,
1005 void *priv
, struct v4l2_format
*fmtd
)
1008 switch (fmtd
->fmt
.pix
.pixelformat
) {
1009 case V4L2_PIX_FMT_RGB565
:
1010 case V4L2_PIX_FMT_RGB565X
:
1011 case V4L2_PIX_FMT_UYVY
:
1012 case V4L2_PIX_FMT_YUYV
:
1013 case V4L2_PIX_FMT_SBGGR8
:
1018 for (i
= 1; i
< ARRAY_SIZE(stk_sizes
); i
++) {
1019 if (fmtd
->fmt
.pix
.width
> stk_sizes
[i
].w
)
1022 if (i
== ARRAY_SIZE(stk_sizes
)
1023 || (abs(fmtd
->fmt
.pix
.width
- stk_sizes
[i
-1].w
)
1024 < abs(fmtd
->fmt
.pix
.width
- stk_sizes
[i
].w
))) {
1025 fmtd
->fmt
.pix
.height
= stk_sizes
[i
-1].h
;
1026 fmtd
->fmt
.pix
.width
= stk_sizes
[i
-1].w
;
1027 fmtd
->fmt
.pix
.priv
= i
- 1;
1029 fmtd
->fmt
.pix
.height
= stk_sizes
[i
].h
;
1030 fmtd
->fmt
.pix
.width
= stk_sizes
[i
].w
;
1031 fmtd
->fmt
.pix
.priv
= i
;
1034 fmtd
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
1035 fmtd
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SRGB
;
1036 if (fmtd
->fmt
.pix
.pixelformat
== V4L2_PIX_FMT_SBGGR8
)
1037 fmtd
->fmt
.pix
.bytesperline
= fmtd
->fmt
.pix
.width
;
1039 fmtd
->fmt
.pix
.bytesperline
= 2 * fmtd
->fmt
.pix
.width
;
1040 fmtd
->fmt
.pix
.sizeimage
= fmtd
->fmt
.pix
.bytesperline
1041 * fmtd
->fmt
.pix
.height
;
1045 static int stk_setup_format(struct stk_camera
*dev
)
1049 if (dev
->vsettings
.palette
== V4L2_PIX_FMT_SBGGR8
)
1053 while (i
< ARRAY_SIZE(stk_sizes
) &&
1054 stk_sizes
[i
].m
!= dev
->vsettings
.mode
)
1056 if (i
== ARRAY_SIZE(stk_sizes
)) {
1057 STK_ERROR("Something is broken in %s\n", __func__
);
1060 /* This registers controls some timings, not sure of what. */
1061 stk_camera_write_reg(dev
, 0x001b, 0x0e);
1062 if (dev
->vsettings
.mode
== MODE_SXGA
)
1063 stk_camera_write_reg(dev
, 0x001c, 0x0e);
1065 stk_camera_write_reg(dev
, 0x001c, 0x46);
1067 * Registers 0x0115 0x0114 are the size of each line (bytes),
1068 * regs 0x0117 0x0116 are the heigth of the image.
1070 stk_camera_write_reg(dev
, 0x0115,
1071 ((stk_sizes
[i
].w
* depth
) >> 8) & 0xff);
1072 stk_camera_write_reg(dev
, 0x0114,
1073 (stk_sizes
[i
].w
* depth
) & 0xff);
1074 stk_camera_write_reg(dev
, 0x0117,
1075 (stk_sizes
[i
].h
>> 8) & 0xff);
1076 stk_camera_write_reg(dev
, 0x0116,
1077 stk_sizes
[i
].h
& 0xff);
1078 return stk_sensor_configure(dev
);
1081 static int stk_vidioc_s_fmt_vid_cap(struct file
*filp
,
1082 void *priv
, struct v4l2_format
*fmtd
)
1085 struct stk_camera
*dev
= priv
;
1089 if (!is_present(dev
))
1091 if (is_streaming(dev
))
1093 if (dev
->owner
&& dev
->owner
!= filp
)
1095 ret
= stk_vidioc_try_fmt_vid_cap(filp
, priv
, fmtd
);
1100 dev
->vsettings
.palette
= fmtd
->fmt
.pix
.pixelformat
;
1101 stk_free_buffers(dev
);
1102 dev
->frame_size
= fmtd
->fmt
.pix
.sizeimage
;
1103 dev
->vsettings
.mode
= stk_sizes
[fmtd
->fmt
.pix
.priv
].m
;
1105 stk_initialise(dev
);
1106 return stk_setup_format(dev
);
1109 static int stk_vidioc_reqbufs(struct file
*filp
,
1110 void *priv
, struct v4l2_requestbuffers
*rb
)
1112 struct stk_camera
*dev
= priv
;
1116 if (rb
->memory
!= V4L2_MEMORY_MMAP
)
1118 if (is_streaming(dev
)
1119 || (dev
->owner
&& dev
->owner
!= filp
))
1123 /*FIXME If they ask for zero, we must stop streaming and free */
1126 /* Arbitrary limit */
1127 else if (rb
->count
> 5)
1130 stk_allocate_buffers(dev
, rb
->count
);
1131 rb
->count
= dev
->n_sbufs
;
1135 static int stk_vidioc_querybuf(struct file
*filp
,
1136 void *priv
, struct v4l2_buffer
*buf
)
1138 struct stk_camera
*dev
= priv
;
1139 struct stk_sio_buffer
*sbuf
;
1141 if (buf
->index
>= dev
->n_sbufs
)
1143 sbuf
= dev
->sio_bufs
+ buf
->index
;
1144 *buf
= sbuf
->v4lbuf
;
1148 static int stk_vidioc_qbuf(struct file
*filp
,
1149 void *priv
, struct v4l2_buffer
*buf
)
1151 struct stk_camera
*dev
= priv
;
1152 struct stk_sio_buffer
*sbuf
;
1153 unsigned long flags
;
1155 if (buf
->memory
!= V4L2_MEMORY_MMAP
)
1158 if (buf
->index
>= dev
->n_sbufs
)
1160 sbuf
= dev
->sio_bufs
+ buf
->index
;
1161 if (sbuf
->v4lbuf
.flags
& V4L2_BUF_FLAG_QUEUED
)
1163 sbuf
->v4lbuf
.flags
|= V4L2_BUF_FLAG_QUEUED
;
1164 sbuf
->v4lbuf
.flags
&= ~V4L2_BUF_FLAG_DONE
;
1165 spin_lock_irqsave(&dev
->spinlock
, flags
);
1166 list_add_tail(&sbuf
->list
, &dev
->sio_avail
);
1167 *buf
= sbuf
->v4lbuf
;
1168 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
1172 static int stk_vidioc_dqbuf(struct file
*filp
,
1173 void *priv
, struct v4l2_buffer
*buf
)
1175 struct stk_camera
*dev
= priv
;
1176 struct stk_sio_buffer
*sbuf
;
1177 unsigned long flags
;
1180 if (!is_streaming(dev
))
1183 if (filp
->f_flags
& O_NONBLOCK
&& list_empty(&dev
->sio_full
))
1184 return -EWOULDBLOCK
;
1185 ret
= wait_event_interruptible(dev
->wait_frame
,
1186 !list_empty(&dev
->sio_full
) || !is_present(dev
));
1189 if (!is_present(dev
))
1192 spin_lock_irqsave(&dev
->spinlock
, flags
);
1193 sbuf
= list_first_entry(&dev
->sio_full
, struct stk_sio_buffer
, list
);
1194 list_del_init(&sbuf
->list
);
1195 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
1196 sbuf
->v4lbuf
.flags
&= ~V4L2_BUF_FLAG_QUEUED
;
1197 sbuf
->v4lbuf
.flags
|= V4L2_BUF_FLAG_DONE
;
1198 sbuf
->v4lbuf
.sequence
= ++dev
->sequence
;
1199 do_gettimeofday(&sbuf
->v4lbuf
.timestamp
);
1201 *buf
= sbuf
->v4lbuf
;
1205 static int stk_vidioc_streamon(struct file
*filp
,
1206 void *priv
, enum v4l2_buf_type type
)
1208 struct stk_camera
*dev
= priv
;
1209 if (is_streaming(dev
))
1211 if (dev
->sio_bufs
== NULL
)
1214 return stk_start_stream(dev
);
1217 static int stk_vidioc_streamoff(struct file
*filp
,
1218 void *priv
, enum v4l2_buf_type type
)
1220 struct stk_camera
*dev
= priv
;
1221 unsigned long flags
;
1223 stk_stop_stream(dev
);
1224 spin_lock_irqsave(&dev
->spinlock
, flags
);
1225 INIT_LIST_HEAD(&dev
->sio_avail
);
1226 INIT_LIST_HEAD(&dev
->sio_full
);
1227 for (i
= 0; i
< dev
->n_sbufs
; i
++) {
1228 INIT_LIST_HEAD(&dev
->sio_bufs
[i
].list
);
1229 dev
->sio_bufs
[i
].v4lbuf
.flags
= 0;
1231 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
1236 static int stk_vidioc_g_parm(struct file
*filp
,
1237 void *priv
, struct v4l2_streamparm
*sp
)
1239 /*FIXME This is not correct */
1240 sp
->parm
.capture
.timeperframe
.numerator
= 1;
1241 sp
->parm
.capture
.timeperframe
.denominator
= 30;
1242 sp
->parm
.capture
.readbuffers
= 2;
1246 static int stk_vidioc_enum_framesizes(struct file
*filp
,
1247 void *priv
, struct v4l2_frmsizeenum
*frms
)
1249 if (frms
->index
>= ARRAY_SIZE(stk_sizes
))
1251 switch (frms
->pixel_format
) {
1252 case V4L2_PIX_FMT_RGB565
:
1253 case V4L2_PIX_FMT_RGB565X
:
1254 case V4L2_PIX_FMT_UYVY
:
1255 case V4L2_PIX_FMT_YUYV
:
1256 case V4L2_PIX_FMT_SBGGR8
:
1257 frms
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1258 frms
->discrete
.width
= stk_sizes
[frms
->index
].w
;
1259 frms
->discrete
.height
= stk_sizes
[frms
->index
].h
;
1261 default: return -EINVAL
;
1265 static struct v4l2_file_operations v4l_stk_fops
= {
1266 .owner
= THIS_MODULE
,
1267 .open
= v4l_stk_open
,
1268 .release
= v4l_stk_release
,
1269 .read
= v4l_stk_read
,
1270 .poll
= v4l_stk_poll
,
1271 .mmap
= v4l_stk_mmap
,
1272 .ioctl
= video_ioctl2
,
1275 static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops
= {
1276 .vidioc_querycap
= stk_vidioc_querycap
,
1277 .vidioc_enum_fmt_vid_cap
= stk_vidioc_enum_fmt_vid_cap
,
1278 .vidioc_try_fmt_vid_cap
= stk_vidioc_try_fmt_vid_cap
,
1279 .vidioc_s_fmt_vid_cap
= stk_vidioc_s_fmt_vid_cap
,
1280 .vidioc_g_fmt_vid_cap
= stk_vidioc_g_fmt_vid_cap
,
1281 .vidioc_enum_input
= stk_vidioc_enum_input
,
1282 .vidioc_s_input
= stk_vidioc_s_input
,
1283 .vidioc_g_input
= stk_vidioc_g_input
,
1284 .vidioc_s_std
= stk_vidioc_s_std
,
1285 .vidioc_reqbufs
= stk_vidioc_reqbufs
,
1286 .vidioc_querybuf
= stk_vidioc_querybuf
,
1287 .vidioc_qbuf
= stk_vidioc_qbuf
,
1288 .vidioc_dqbuf
= stk_vidioc_dqbuf
,
1289 .vidioc_streamon
= stk_vidioc_streamon
,
1290 .vidioc_streamoff
= stk_vidioc_streamoff
,
1291 .vidioc_queryctrl
= stk_vidioc_queryctrl
,
1292 .vidioc_g_ctrl
= stk_vidioc_g_ctrl
,
1293 .vidioc_s_ctrl
= stk_vidioc_s_ctrl
,
1294 .vidioc_g_parm
= stk_vidioc_g_parm
,
1295 .vidioc_enum_framesizes
= stk_vidioc_enum_framesizes
,
1298 static void stk_v4l_dev_release(struct video_device
*vd
)
1300 struct stk_camera
*dev
= vdev_to_camera(vd
);
1302 if (dev
->sio_bufs
!= NULL
|| dev
->isobufs
!= NULL
)
1303 STK_ERROR("We are leaking memory\n");
1304 usb_put_intf(dev
->interface
);
1308 static struct video_device stk_v4l_data
= {
1309 .name
= "stkwebcam",
1311 .tvnorms
= V4L2_STD_UNKNOWN
,
1312 .current_norm
= V4L2_STD_UNKNOWN
,
1313 .fops
= &v4l_stk_fops
,
1314 .ioctl_ops
= &v4l_stk_ioctl_ops
,
1315 .release
= stk_v4l_dev_release
,
1319 static int stk_register_video_device(struct stk_camera
*dev
)
1323 dev
->vdev
= stk_v4l_data
;
1324 dev
->vdev
.debug
= debug
;
1325 dev
->vdev
.parent
= &dev
->interface
->dev
;
1326 err
= video_register_device(&dev
->vdev
, VFL_TYPE_GRABBER
, -1);
1328 STK_ERROR("v4l registration failed\n");
1330 STK_INFO("Syntek USB2.0 Camera is now controlling video device"
1331 " /dev/video%d\n", dev
->vdev
.num
);
1338 static int stk_camera_probe(struct usb_interface
*interface
,
1339 const struct usb_device_id
*id
)
1344 struct stk_camera
*dev
= NULL
;
1345 struct usb_device
*udev
= interface_to_usbdev(interface
);
1346 struct usb_host_interface
*iface_desc
;
1347 struct usb_endpoint_descriptor
*endpoint
;
1349 dev
= kzalloc(sizeof(struct stk_camera
), GFP_KERNEL
);
1351 STK_ERROR("Out of memory !\n");
1355 spin_lock_init(&dev
->spinlock
);
1356 init_waitqueue_head(&dev
->wait_frame
);
1359 dev
->interface
= interface
;
1360 usb_get_intf(interface
);
1362 dev
->vsettings
.vflip
= vflip
;
1363 dev
->vsettings
.hflip
= hflip
;
1367 /* Set up the endpoint information
1368 * use only the first isoc-in endpoint
1369 * for the current alternate setting */
1370 iface_desc
= interface
->cur_altsetting
;
1372 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
1373 endpoint
= &iface_desc
->endpoint
[i
].desc
;
1376 && usb_endpoint_is_isoc_in(endpoint
)) {
1377 /* we found an isoc in endpoint */
1378 dev
->isoc_ep
= usb_endpoint_num(endpoint
);
1382 if (!dev
->isoc_ep
) {
1383 STK_ERROR("Could not find isoc-in endpoint");
1387 dev
->vsettings
.brightness
= 0x7fff;
1388 dev
->vsettings
.palette
= V4L2_PIX_FMT_RGB565
;
1389 dev
->vsettings
.mode
= MODE_VGA
;
1390 dev
->frame_size
= 640 * 480 * 2;
1392 INIT_LIST_HEAD(&dev
->sio_avail
);
1393 INIT_LIST_HEAD(&dev
->sio_full
);
1395 usb_set_intfdata(interface
, dev
);
1397 err
= stk_register_video_device(dev
);
1402 stk_create_sysfs_files(&dev
->vdev
);
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
);