Avoid beyond bounds copy while caching ACL
[zen-stable.git] / drivers / media / video / stk-webcam.c
blobd427f8436c70468721140d655eb00196d9ccf80b
1 /*
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
13 * any later version.
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>
32 #include <linux/mm.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"
41 static bool hflip = 1;
42 module_param(hflip, bool, 0444);
43 MODULE_PARM_DESC(hflip, "Horizontal image flip (mirror). Defaults to 1");
45 static bool vflip = 1;
46 module_param(vflip, bool, 0444);
47 MODULE_PARM_DESC(vflip, "Vertical image flip. Defaults to 1");
49 static int debug;
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");
58 /* bool for webcam LED management */
59 int first_init = 1;
61 /* Some cameras have audio interfaces, we aren't interested in those */
62 static struct usb_device_id stkwebcam_table[] = {
63 { USB_DEVICE_AND_INTERFACE_INFO(0x174f, 0xa311, 0xff, 0xff, 0xff) },
64 { USB_DEVICE_AND_INTERFACE_INFO(0x05e1, 0x0501, 0xff, 0xff, 0xff) },
65 { }
67 MODULE_DEVICE_TABLE(usb, stkwebcam_table);
70 * Basic stuff
72 int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
74 struct usb_device *udev = dev->udev;
75 int ret;
77 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
78 0x01,
79 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
80 value,
81 index,
82 NULL,
84 500);
85 if (ret < 0)
86 return ret;
87 else
88 return 0;
91 int stk_camera_read_reg(struct stk_camera *dev, u16 index, int *value)
93 struct usb_device *udev = dev->udev;
94 int ret;
96 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
97 0x00,
98 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
99 0x00,
100 index,
101 (u8 *) value,
102 sizeof(u8),
103 500);
104 if (ret < 0)
105 return ret;
106 else
107 return 0;
110 static int stk_start_stream(struct stk_camera *dev)
112 int value;
113 int i, ret;
114 int value_116, value_117;
116 if (!is_present(dev))
117 return -ENODEV;
118 if (!is_memallocd(dev) || !is_initialised(dev)) {
119 STK_ERROR("FIXME: Buffers are not allocated\n");
120 return -EFAULT;
122 ret = usb_set_interface(dev->udev, 0, 5);
124 if (ret < 0)
125 STK_ERROR("usb_set_interface failed !\n");
126 if (stk_sensor_wakeup(dev))
127 STK_ERROR("error awaking the sensor\n");
129 stk_camera_read_reg(dev, 0x0116, &value_116);
130 stk_camera_read_reg(dev, 0x0117, &value_117);
132 stk_camera_write_reg(dev, 0x0116, 0x0000);
133 stk_camera_write_reg(dev, 0x0117, 0x0000);
135 stk_camera_read_reg(dev, 0x0100, &value);
136 stk_camera_write_reg(dev, 0x0100, value | 0x80);
138 stk_camera_write_reg(dev, 0x0116, value_116);
139 stk_camera_write_reg(dev, 0x0117, value_117);
140 for (i = 0; i < MAX_ISO_BUFS; i++) {
141 if (dev->isobufs[i].urb) {
142 ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL);
143 atomic_inc(&dev->urbs_used);
144 if (ret)
145 return ret;
148 set_streaming(dev);
149 return 0;
152 static int stk_stop_stream(struct stk_camera *dev)
154 int value;
155 int i;
156 if (is_present(dev)) {
157 stk_camera_read_reg(dev, 0x0100, &value);
158 stk_camera_write_reg(dev, 0x0100, value & ~0x80);
159 if (dev->isobufs != NULL) {
160 for (i = 0; i < MAX_ISO_BUFS; i++) {
161 if (dev->isobufs[i].urb)
162 usb_kill_urb(dev->isobufs[i].urb);
165 unset_streaming(dev);
167 if (usb_set_interface(dev->udev, 0, 0))
168 STK_ERROR("usb_set_interface failed !\n");
169 if (stk_sensor_sleep(dev))
170 STK_ERROR("error suspending the sensor\n");
172 return 0;
176 * This seems to be the shortest init sequence we
177 * must do in order to find the sensor
178 * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
179 * is also reset. Maybe powers down it?
180 * Rest of values don't make a difference
183 static struct regval stk1125_initvals[] = {
184 /*TODO: What means this sequence? */
185 {0x0000, 0x24},
186 {0x0100, 0x21},
187 {0x0002, 0x68},
188 {0x0003, 0x80},
189 {0x0005, 0x00},
190 {0x0007, 0x03},
191 {0x000d, 0x00},
192 {0x000f, 0x02},
193 {0x0300, 0x12},
194 {0x0350, 0x41},
195 {0x0351, 0x00},
196 {0x0352, 0x00},
197 {0x0353, 0x00},
198 {0x0018, 0x10},
199 {0x0019, 0x00},
200 {0x001b, 0x0e},
201 {0x001c, 0x46},
202 {0x0300, 0x80},
203 {0x001a, 0x04},
204 {0x0110, 0x00},
205 {0x0111, 0x00},
206 {0x0112, 0x00},
207 {0x0113, 0x00},
209 {0xffff, 0xff},
213 static int stk_initialise(struct stk_camera *dev)
215 struct regval *rv;
216 int ret;
217 if (!is_present(dev))
218 return -ENODEV;
219 if (is_initialised(dev))
220 return 0;
221 rv = stk1125_initvals;
222 while (rv->reg != 0xffff) {
223 ret = stk_camera_write_reg(dev, rv->reg, rv->val);
224 if (ret)
225 return ret;
226 rv++;
228 if (stk_sensor_init(dev) == 0) {
229 set_initialised(dev);
230 return 0;
231 } else
232 return -1;
235 /* *********************************************** */
237 * This function is called as an URB transfert is complete (Isochronous pipe).
238 * So, the traitement is done in interrupt time, so it has be fast, not crash,
239 * and not stall. Neat.
241 static void stk_isoc_handler(struct urb *urb)
243 int i;
244 int ret;
245 int framelen;
246 unsigned long flags;
248 unsigned char *fill = NULL;
249 unsigned char *iso_buf = NULL;
251 struct stk_camera *dev;
252 struct stk_sio_buffer *fb;
254 dev = (struct stk_camera *) urb->context;
256 if (dev == NULL) {
257 STK_ERROR("isoc_handler called with NULL device !\n");
258 return;
261 if (urb->status == -ENOENT || urb->status == -ECONNRESET
262 || urb->status == -ESHUTDOWN) {
263 atomic_dec(&dev->urbs_used);
264 return;
267 spin_lock_irqsave(&dev->spinlock, flags);
269 if (urb->status != -EINPROGRESS && urb->status != 0) {
270 STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
271 goto resubmit;
274 if (list_empty(&dev->sio_avail)) {
275 /*FIXME Stop streaming after a while */
276 (void) (printk_ratelimit() &&
277 STK_ERROR("isoc_handler without available buffer!\n"));
278 goto resubmit;
280 fb = list_first_entry(&dev->sio_avail,
281 struct stk_sio_buffer, list);
282 fill = fb->buffer + fb->v4lbuf.bytesused;
284 for (i = 0; i < urb->number_of_packets; i++) {
285 if (urb->iso_frame_desc[i].status != 0) {
286 if (urb->iso_frame_desc[i].status != -EXDEV)
287 STK_ERROR("Frame %d has error %d\n", i,
288 urb->iso_frame_desc[i].status);
289 continue;
291 framelen = urb->iso_frame_desc[i].actual_length;
292 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
294 if (framelen <= 4)
295 continue; /* no data */
298 * we found something informational from there
299 * the isoc frames have to type of headers
300 * type1: 00 xx 00 00 or 20 xx 00 00
301 * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
302 * xx is a sequencer which has never been seen over 0x3f
303 * imho data written down looks like bayer, i see similarities
304 * after every 640 bytes
306 if (*iso_buf & 0x80) {
307 framelen -= 8;
308 iso_buf += 8;
309 /* This marks a new frame */
310 if (fb->v4lbuf.bytesused != 0
311 && fb->v4lbuf.bytesused != dev->frame_size) {
312 (void) (printk_ratelimit() &&
313 STK_ERROR("frame %d, "
314 "bytesused=%d, skipping\n",
315 i, fb->v4lbuf.bytesused));
316 fb->v4lbuf.bytesused = 0;
317 fill = fb->buffer;
318 } else if (fb->v4lbuf.bytesused == dev->frame_size) {
319 if (list_is_singular(&dev->sio_avail)) {
320 /* Always reuse the last buffer */
321 fb->v4lbuf.bytesused = 0;
322 fill = fb->buffer;
323 } else {
324 list_move_tail(dev->sio_avail.next,
325 &dev->sio_full);
326 wake_up(&dev->wait_frame);
327 fb = list_first_entry(&dev->sio_avail,
328 struct stk_sio_buffer, list);
329 fb->v4lbuf.bytesused = 0;
330 fill = fb->buffer;
333 } else {
334 framelen -= 4;
335 iso_buf += 4;
338 /* Our buffer is full !!! */
339 if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
340 (void) (printk_ratelimit() &&
341 STK_ERROR("Frame buffer overflow, lost sync\n"));
342 /*FIXME Do something here? */
343 continue;
345 spin_unlock_irqrestore(&dev->spinlock, flags);
346 memcpy(fill, iso_buf, framelen);
347 spin_lock_irqsave(&dev->spinlock, flags);
348 fill += framelen;
350 /* New size of our buffer */
351 fb->v4lbuf.bytesused += framelen;
354 resubmit:
355 spin_unlock_irqrestore(&dev->spinlock, flags);
356 urb->dev = dev->udev;
357 ret = usb_submit_urb(urb, GFP_ATOMIC);
358 if (ret != 0) {
359 STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
360 ret);
364 /* -------------------------------------------- */
366 static int stk_prepare_iso(struct stk_camera *dev)
368 void *kbuf;
369 int i, j;
370 struct urb *urb;
371 struct usb_device *udev;
373 if (dev == NULL)
374 return -ENXIO;
375 udev = dev->udev;
377 if (dev->isobufs)
378 STK_ERROR("isobufs already allocated. Bad\n");
379 else
380 dev->isobufs = kcalloc(MAX_ISO_BUFS, sizeof(*dev->isobufs),
381 GFP_KERNEL);
382 if (dev->isobufs == NULL) {
383 STK_ERROR("Unable to allocate iso buffers\n");
384 return -ENOMEM;
386 for (i = 0; i < MAX_ISO_BUFS; i++) {
387 if (dev->isobufs[i].data == NULL) {
388 kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
389 if (kbuf == NULL) {
390 STK_ERROR("Failed to allocate iso buffer %d\n",
392 goto isobufs_out;
394 dev->isobufs[i].data = kbuf;
395 } else
396 STK_ERROR("isobuf data already allocated\n");
397 if (dev->isobufs[i].urb == NULL) {
398 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
399 if (urb == NULL) {
400 STK_ERROR("Failed to allocate URB %d\n", i);
401 goto isobufs_out;
403 dev->isobufs[i].urb = urb;
404 } else {
405 STK_ERROR("Killing URB\n");
406 usb_kill_urb(dev->isobufs[i].urb);
407 urb = dev->isobufs[i].urb;
409 urb->interval = 1;
410 urb->dev = udev;
411 urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
412 urb->transfer_flags = URB_ISO_ASAP;
413 urb->transfer_buffer = dev->isobufs[i].data;
414 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
415 urb->complete = stk_isoc_handler;
416 urb->context = dev;
417 urb->start_frame = 0;
418 urb->number_of_packets = ISO_FRAMES_PER_DESC;
420 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
421 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
422 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
425 set_memallocd(dev);
426 return 0;
428 isobufs_out:
429 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
430 kfree(dev->isobufs[i].data);
431 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
432 usb_free_urb(dev->isobufs[i].urb);
433 kfree(dev->isobufs);
434 dev->isobufs = NULL;
435 return -ENOMEM;
438 static void stk_clean_iso(struct stk_camera *dev)
440 int i;
442 if (dev == NULL || dev->isobufs == NULL)
443 return;
445 for (i = 0; i < MAX_ISO_BUFS; i++) {
446 struct urb *urb;
448 urb = dev->isobufs[i].urb;
449 if (urb) {
450 if (atomic_read(&dev->urbs_used) && is_present(dev))
451 usb_kill_urb(urb);
452 usb_free_urb(urb);
454 kfree(dev->isobufs[i].data);
456 kfree(dev->isobufs);
457 dev->isobufs = NULL;
458 unset_memallocd(dev);
461 static int stk_setup_siobuf(struct stk_camera *dev, int index)
463 struct stk_sio_buffer *buf = dev->sio_bufs + index;
464 INIT_LIST_HEAD(&buf->list);
465 buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
466 buf->buffer = vmalloc_user(buf->v4lbuf.length);
467 if (buf->buffer == NULL)
468 return -ENOMEM;
469 buf->mapcount = 0;
470 buf->dev = dev;
471 buf->v4lbuf.index = index;
472 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
473 buf->v4lbuf.field = V4L2_FIELD_NONE;
474 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
475 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
476 return 0;
479 static int stk_free_sio_buffers(struct stk_camera *dev)
481 int i;
482 int nbufs;
483 unsigned long flags;
484 if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
485 return 0;
487 * If any buffers are mapped, we cannot free them at all.
489 for (i = 0; i < dev->n_sbufs; i++) {
490 if (dev->sio_bufs[i].mapcount > 0)
491 return -EBUSY;
494 * OK, let's do it.
496 spin_lock_irqsave(&dev->spinlock, flags);
497 INIT_LIST_HEAD(&dev->sio_avail);
498 INIT_LIST_HEAD(&dev->sio_full);
499 nbufs = dev->n_sbufs;
500 dev->n_sbufs = 0;
501 spin_unlock_irqrestore(&dev->spinlock, flags);
502 for (i = 0; i < nbufs; i++) {
503 if (dev->sio_bufs[i].buffer != NULL)
504 vfree(dev->sio_bufs[i].buffer);
506 kfree(dev->sio_bufs);
507 dev->sio_bufs = NULL;
508 return 0;
511 static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
513 int i;
514 if (dev->sio_bufs != NULL)
515 STK_ERROR("sio_bufs already allocated\n");
516 else {
517 dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
518 GFP_KERNEL);
519 if (dev->sio_bufs == NULL)
520 return -ENOMEM;
521 for (i = 0; i < n_sbufs; i++) {
522 if (stk_setup_siobuf(dev, i))
523 return (dev->n_sbufs > 1 ? 0 : -ENOMEM);
524 dev->n_sbufs = i+1;
527 return 0;
530 static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
532 int err;
533 err = stk_prepare_iso(dev);
534 if (err) {
535 stk_clean_iso(dev);
536 return err;
538 err = stk_prepare_sio_buffers(dev, n_sbufs);
539 if (err) {
540 stk_free_sio_buffers(dev);
541 return err;
543 return 0;
546 static void stk_free_buffers(struct stk_camera *dev)
548 stk_clean_iso(dev);
549 stk_free_sio_buffers(dev);
551 /* -------------------------------------------- */
553 /* v4l file operations */
555 static int v4l_stk_open(struct file *fp)
557 struct stk_camera *dev;
558 struct video_device *vdev;
560 vdev = video_devdata(fp);
561 dev = vdev_to_camera(vdev);
563 if (dev == NULL || !is_present(dev))
564 return -ENXIO;
566 if (!first_init)
567 stk_camera_write_reg(dev, 0x0, 0x24);
568 else
569 first_init = 0;
571 fp->private_data = dev;
572 usb_autopm_get_interface(dev->interface);
574 return 0;
577 static int v4l_stk_release(struct file *fp)
579 struct stk_camera *dev = fp->private_data;
581 if (dev->owner == fp) {
582 stk_stop_stream(dev);
583 stk_free_buffers(dev);
584 stk_camera_write_reg(dev, 0x0, 0x49); /* turn off the LED */
585 unset_initialised(dev);
586 dev->owner = NULL;
589 if (is_present(dev))
590 usb_autopm_put_interface(dev->interface);
592 return 0;
595 static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
596 size_t count, loff_t *f_pos)
598 int i;
599 int ret;
600 unsigned long flags;
601 struct stk_sio_buffer *sbuf;
602 struct stk_camera *dev = fp->private_data;
604 if (!is_present(dev))
605 return -EIO;
606 if (dev->owner && dev->owner != fp)
607 return -EBUSY;
608 dev->owner = fp;
609 if (!is_streaming(dev)) {
610 if (stk_initialise(dev)
611 || stk_allocate_buffers(dev, 3)
612 || stk_start_stream(dev))
613 return -ENOMEM;
614 spin_lock_irqsave(&dev->spinlock, flags);
615 for (i = 0; i < dev->n_sbufs; i++) {
616 list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
617 dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
619 spin_unlock_irqrestore(&dev->spinlock, flags);
621 if (*f_pos == 0) {
622 if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
623 return -EWOULDBLOCK;
624 ret = wait_event_interruptible(dev->wait_frame,
625 !list_empty(&dev->sio_full) || !is_present(dev));
626 if (ret)
627 return ret;
628 if (!is_present(dev))
629 return -EIO;
631 if (count + *f_pos > dev->frame_size)
632 count = dev->frame_size - *f_pos;
633 spin_lock_irqsave(&dev->spinlock, flags);
634 if (list_empty(&dev->sio_full)) {
635 spin_unlock_irqrestore(&dev->spinlock, flags);
636 STK_ERROR("BUG: No siobufs ready\n");
637 return 0;
639 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
640 spin_unlock_irqrestore(&dev->spinlock, flags);
642 if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
643 return -EFAULT;
645 *f_pos += count;
647 if (*f_pos >= dev->frame_size) {
648 *f_pos = 0;
649 spin_lock_irqsave(&dev->spinlock, flags);
650 list_move_tail(&sbuf->list, &dev->sio_avail);
651 spin_unlock_irqrestore(&dev->spinlock, flags);
653 return count;
656 static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
658 struct stk_camera *dev = fp->private_data;
660 poll_wait(fp, &dev->wait_frame, wait);
662 if (!is_present(dev))
663 return POLLERR;
665 if (!list_empty(&dev->sio_full))
666 return POLLIN | POLLRDNORM;
668 return 0;
672 static void stk_v4l_vm_open(struct vm_area_struct *vma)
674 struct stk_sio_buffer *sbuf = vma->vm_private_data;
675 sbuf->mapcount++;
677 static void stk_v4l_vm_close(struct vm_area_struct *vma)
679 struct stk_sio_buffer *sbuf = vma->vm_private_data;
680 sbuf->mapcount--;
681 if (sbuf->mapcount == 0)
682 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
684 static const struct vm_operations_struct stk_v4l_vm_ops = {
685 .open = stk_v4l_vm_open,
686 .close = stk_v4l_vm_close
689 static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
691 unsigned int i;
692 int ret;
693 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
694 struct stk_camera *dev = fp->private_data;
695 struct stk_sio_buffer *sbuf = NULL;
697 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
698 return -EINVAL;
700 for (i = 0; i < dev->n_sbufs; i++) {
701 if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
702 sbuf = dev->sio_bufs + i;
703 break;
706 if (sbuf == NULL)
707 return -EINVAL;
708 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
709 if (ret)
710 return ret;
711 vma->vm_flags |= VM_DONTEXPAND;
712 vma->vm_private_data = sbuf;
713 vma->vm_ops = &stk_v4l_vm_ops;
714 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
715 stk_v4l_vm_open(vma);
716 return 0;
719 /* v4l ioctl handlers */
721 static int stk_vidioc_querycap(struct file *filp,
722 void *priv, struct v4l2_capability *cap)
724 strcpy(cap->driver, "stk");
725 strcpy(cap->card, "stk");
726 cap->version = DRIVER_VERSION_NUM;
728 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
729 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
730 return 0;
733 static int stk_vidioc_enum_input(struct file *filp,
734 void *priv, struct v4l2_input *input)
736 if (input->index != 0)
737 return -EINVAL;
739 strcpy(input->name, "Syntek USB Camera");
740 input->type = V4L2_INPUT_TYPE_CAMERA;
741 return 0;
745 static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
747 *i = 0;
748 return 0;
751 static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
753 if (i != 0)
754 return -EINVAL;
755 else
756 return 0;
759 /* from vivi.c */
760 static int stk_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
762 return 0;
765 /* List of all V4Lv2 controls supported by the driver */
766 static struct v4l2_queryctrl stk_controls[] = {
768 .id = V4L2_CID_BRIGHTNESS,
769 .type = V4L2_CTRL_TYPE_INTEGER,
770 .name = "Brightness",
771 .minimum = 0,
772 .maximum = 0xffff,
773 .step = 0x0100,
774 .default_value = 0x6000,
777 .id = V4L2_CID_HFLIP,
778 .type = V4L2_CTRL_TYPE_BOOLEAN,
779 .name = "Horizontal Flip",
780 .minimum = 0,
781 .maximum = 1,
782 .step = 1,
783 .default_value = 1,
786 .id = V4L2_CID_VFLIP,
787 .type = V4L2_CTRL_TYPE_BOOLEAN,
788 .name = "Vertical Flip",
789 .minimum = 0,
790 .maximum = 1,
791 .step = 1,
792 .default_value = 1,
796 static int stk_vidioc_queryctrl(struct file *filp,
797 void *priv, struct v4l2_queryctrl *c)
799 int i;
800 int nbr;
801 nbr = ARRAY_SIZE(stk_controls);
803 for (i = 0; i < nbr; i++) {
804 if (stk_controls[i].id == c->id) {
805 memcpy(c, &stk_controls[i],
806 sizeof(struct v4l2_queryctrl));
807 return 0;
810 return -EINVAL;
813 static int stk_vidioc_g_ctrl(struct file *filp,
814 void *priv, struct v4l2_control *c)
816 struct stk_camera *dev = priv;
817 switch (c->id) {
818 case V4L2_CID_BRIGHTNESS:
819 c->value = dev->vsettings.brightness;
820 break;
821 case V4L2_CID_HFLIP:
822 c->value = dev->vsettings.hflip;
823 break;
824 case V4L2_CID_VFLIP:
825 c->value = dev->vsettings.vflip;
826 break;
827 default:
828 return -EINVAL;
830 return 0;
833 static int stk_vidioc_s_ctrl(struct file *filp,
834 void *priv, struct v4l2_control *c)
836 struct stk_camera *dev = priv;
837 switch (c->id) {
838 case V4L2_CID_BRIGHTNESS:
839 dev->vsettings.brightness = c->value;
840 return stk_sensor_set_brightness(dev, c->value >> 8);
841 case V4L2_CID_HFLIP:
842 dev->vsettings.hflip = c->value;
843 return 0;
844 case V4L2_CID_VFLIP:
845 dev->vsettings.vflip = c->value;
846 return 0;
847 default:
848 return -EINVAL;
850 return 0;
854 static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
855 void *priv, struct v4l2_fmtdesc *fmtd)
857 switch (fmtd->index) {
858 case 0:
859 fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
860 strcpy(fmtd->description, "r5g6b5");
861 break;
862 case 1:
863 fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
864 strcpy(fmtd->description, "r5g6b5BE");
865 break;
866 case 2:
867 fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
868 strcpy(fmtd->description, "yuv4:2:2");
869 break;
870 case 3:
871 fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
872 strcpy(fmtd->description, "Raw bayer");
873 break;
874 case 4:
875 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
876 strcpy(fmtd->description, "yuv4:2:2");
877 break;
878 default:
879 return -EINVAL;
881 return 0;
884 static struct stk_size {
885 unsigned w;
886 unsigned h;
887 enum stk_mode m;
888 } stk_sizes[] = {
889 { .w = 1280, .h = 1024, .m = MODE_SXGA, },
890 { .w = 640, .h = 480, .m = MODE_VGA, },
891 { .w = 352, .h = 288, .m = MODE_CIF, },
892 { .w = 320, .h = 240, .m = MODE_QVGA, },
893 { .w = 176, .h = 144, .m = MODE_QCIF, },
896 static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
897 void *priv, struct v4l2_format *f)
899 struct v4l2_pix_format *pix_format = &f->fmt.pix;
900 struct stk_camera *dev = priv;
901 int i;
903 for (i = 0; i < ARRAY_SIZE(stk_sizes) &&
904 stk_sizes[i].m != dev->vsettings.mode; i++)
906 if (i == ARRAY_SIZE(stk_sizes)) {
907 STK_ERROR("ERROR: mode invalid\n");
908 return -EINVAL;
910 pix_format->width = stk_sizes[i].w;
911 pix_format->height = stk_sizes[i].h;
912 pix_format->field = V4L2_FIELD_NONE;
913 pix_format->colorspace = V4L2_COLORSPACE_SRGB;
914 pix_format->pixelformat = dev->vsettings.palette;
915 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
916 pix_format->bytesperline = pix_format->width;
917 else
918 pix_format->bytesperline = 2 * pix_format->width;
919 pix_format->sizeimage = pix_format->bytesperline
920 * pix_format->height;
921 return 0;
924 static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
925 void *priv, struct v4l2_format *fmtd)
927 int i;
928 switch (fmtd->fmt.pix.pixelformat) {
929 case V4L2_PIX_FMT_RGB565:
930 case V4L2_PIX_FMT_RGB565X:
931 case V4L2_PIX_FMT_UYVY:
932 case V4L2_PIX_FMT_YUYV:
933 case V4L2_PIX_FMT_SBGGR8:
934 break;
935 default:
936 return -EINVAL;
938 for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
939 if (fmtd->fmt.pix.width > stk_sizes[i].w)
940 break;
942 if (i == ARRAY_SIZE(stk_sizes)
943 || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
944 < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
945 fmtd->fmt.pix.height = stk_sizes[i-1].h;
946 fmtd->fmt.pix.width = stk_sizes[i-1].w;
947 fmtd->fmt.pix.priv = i - 1;
948 } else {
949 fmtd->fmt.pix.height = stk_sizes[i].h;
950 fmtd->fmt.pix.width = stk_sizes[i].w;
951 fmtd->fmt.pix.priv = i;
954 fmtd->fmt.pix.field = V4L2_FIELD_NONE;
955 fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
956 if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
957 fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
958 else
959 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
960 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
961 * fmtd->fmt.pix.height;
962 return 0;
965 static int stk_setup_format(struct stk_camera *dev)
967 int i = 0;
968 int depth;
969 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
970 depth = 1;
971 else
972 depth = 2;
973 while (i < ARRAY_SIZE(stk_sizes) &&
974 stk_sizes[i].m != dev->vsettings.mode)
975 i++;
976 if (i == ARRAY_SIZE(stk_sizes)) {
977 STK_ERROR("Something is broken in %s\n", __func__);
978 return -EFAULT;
980 /* This registers controls some timings, not sure of what. */
981 stk_camera_write_reg(dev, 0x001b, 0x0e);
982 if (dev->vsettings.mode == MODE_SXGA)
983 stk_camera_write_reg(dev, 0x001c, 0x0e);
984 else
985 stk_camera_write_reg(dev, 0x001c, 0x46);
987 * Registers 0x0115 0x0114 are the size of each line (bytes),
988 * regs 0x0117 0x0116 are the heigth of the image.
990 stk_camera_write_reg(dev, 0x0115,
991 ((stk_sizes[i].w * depth) >> 8) & 0xff);
992 stk_camera_write_reg(dev, 0x0114,
993 (stk_sizes[i].w * depth) & 0xff);
994 stk_camera_write_reg(dev, 0x0117,
995 (stk_sizes[i].h >> 8) & 0xff);
996 stk_camera_write_reg(dev, 0x0116,
997 stk_sizes[i].h & 0xff);
998 return stk_sensor_configure(dev);
1001 static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
1002 void *priv, struct v4l2_format *fmtd)
1004 int ret;
1005 struct stk_camera *dev = priv;
1007 if (dev == NULL)
1008 return -ENODEV;
1009 if (!is_present(dev))
1010 return -ENODEV;
1011 if (is_streaming(dev))
1012 return -EBUSY;
1013 if (dev->owner && dev->owner != filp)
1014 return -EBUSY;
1015 ret = stk_vidioc_try_fmt_vid_cap(filp, priv, fmtd);
1016 if (ret)
1017 return ret;
1018 dev->owner = filp;
1020 dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
1021 stk_free_buffers(dev);
1022 dev->frame_size = fmtd->fmt.pix.sizeimage;
1023 dev->vsettings.mode = stk_sizes[fmtd->fmt.pix.priv].m;
1025 stk_initialise(dev);
1026 return stk_setup_format(dev);
1029 static int stk_vidioc_reqbufs(struct file *filp,
1030 void *priv, struct v4l2_requestbuffers *rb)
1032 struct stk_camera *dev = priv;
1034 if (dev == NULL)
1035 return -ENODEV;
1036 if (rb->memory != V4L2_MEMORY_MMAP)
1037 return -EINVAL;
1038 if (is_streaming(dev)
1039 || (dev->owner && dev->owner != filp))
1040 return -EBUSY;
1041 dev->owner = filp;
1043 /*FIXME If they ask for zero, we must stop streaming and free */
1044 if (rb->count < 3)
1045 rb->count = 3;
1046 /* Arbitrary limit */
1047 else if (rb->count > 5)
1048 rb->count = 5;
1050 stk_allocate_buffers(dev, rb->count);
1051 rb->count = dev->n_sbufs;
1052 return 0;
1055 static int stk_vidioc_querybuf(struct file *filp,
1056 void *priv, struct v4l2_buffer *buf)
1058 struct stk_camera *dev = priv;
1059 struct stk_sio_buffer *sbuf;
1061 if (buf->index >= dev->n_sbufs)
1062 return -EINVAL;
1063 sbuf = dev->sio_bufs + buf->index;
1064 *buf = sbuf->v4lbuf;
1065 return 0;
1068 static int stk_vidioc_qbuf(struct file *filp,
1069 void *priv, struct v4l2_buffer *buf)
1071 struct stk_camera *dev = priv;
1072 struct stk_sio_buffer *sbuf;
1073 unsigned long flags;
1075 if (buf->memory != V4L2_MEMORY_MMAP)
1076 return -EINVAL;
1078 if (buf->index >= dev->n_sbufs)
1079 return -EINVAL;
1080 sbuf = dev->sio_bufs + buf->index;
1081 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
1082 return 0;
1083 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1084 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1085 spin_lock_irqsave(&dev->spinlock, flags);
1086 list_add_tail(&sbuf->list, &dev->sio_avail);
1087 *buf = sbuf->v4lbuf;
1088 spin_unlock_irqrestore(&dev->spinlock, flags);
1089 return 0;
1092 static int stk_vidioc_dqbuf(struct file *filp,
1093 void *priv, struct v4l2_buffer *buf)
1095 struct stk_camera *dev = priv;
1096 struct stk_sio_buffer *sbuf;
1097 unsigned long flags;
1098 int ret;
1100 if (!is_streaming(dev))
1101 return -EINVAL;
1103 if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
1104 return -EWOULDBLOCK;
1105 ret = wait_event_interruptible(dev->wait_frame,
1106 !list_empty(&dev->sio_full) || !is_present(dev));
1107 if (ret)
1108 return ret;
1109 if (!is_present(dev))
1110 return -EIO;
1112 spin_lock_irqsave(&dev->spinlock, flags);
1113 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
1114 list_del_init(&sbuf->list);
1115 spin_unlock_irqrestore(&dev->spinlock, flags);
1116 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1117 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1118 sbuf->v4lbuf.sequence = ++dev->sequence;
1119 do_gettimeofday(&sbuf->v4lbuf.timestamp);
1121 *buf = sbuf->v4lbuf;
1122 return 0;
1125 static int stk_vidioc_streamon(struct file *filp,
1126 void *priv, enum v4l2_buf_type type)
1128 struct stk_camera *dev = priv;
1129 if (is_streaming(dev))
1130 return 0;
1131 if (dev->sio_bufs == NULL)
1132 return -EINVAL;
1133 dev->sequence = 0;
1134 return stk_start_stream(dev);
1137 static int stk_vidioc_streamoff(struct file *filp,
1138 void *priv, enum v4l2_buf_type type)
1140 struct stk_camera *dev = priv;
1141 unsigned long flags;
1142 int i;
1143 stk_stop_stream(dev);
1144 spin_lock_irqsave(&dev->spinlock, flags);
1145 INIT_LIST_HEAD(&dev->sio_avail);
1146 INIT_LIST_HEAD(&dev->sio_full);
1147 for (i = 0; i < dev->n_sbufs; i++) {
1148 INIT_LIST_HEAD(&dev->sio_bufs[i].list);
1149 dev->sio_bufs[i].v4lbuf.flags = 0;
1151 spin_unlock_irqrestore(&dev->spinlock, flags);
1152 return 0;
1156 static int stk_vidioc_g_parm(struct file *filp,
1157 void *priv, struct v4l2_streamparm *sp)
1159 /*FIXME This is not correct */
1160 sp->parm.capture.timeperframe.numerator = 1;
1161 sp->parm.capture.timeperframe.denominator = 30;
1162 sp->parm.capture.readbuffers = 2;
1163 return 0;
1166 static int stk_vidioc_enum_framesizes(struct file *filp,
1167 void *priv, struct v4l2_frmsizeenum *frms)
1169 if (frms->index >= ARRAY_SIZE(stk_sizes))
1170 return -EINVAL;
1171 switch (frms->pixel_format) {
1172 case V4L2_PIX_FMT_RGB565:
1173 case V4L2_PIX_FMT_RGB565X:
1174 case V4L2_PIX_FMT_UYVY:
1175 case V4L2_PIX_FMT_YUYV:
1176 case V4L2_PIX_FMT_SBGGR8:
1177 frms->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1178 frms->discrete.width = stk_sizes[frms->index].w;
1179 frms->discrete.height = stk_sizes[frms->index].h;
1180 return 0;
1181 default: return -EINVAL;
1185 static struct v4l2_file_operations v4l_stk_fops = {
1186 .owner = THIS_MODULE,
1187 .open = v4l_stk_open,
1188 .release = v4l_stk_release,
1189 .read = v4l_stk_read,
1190 .poll = v4l_stk_poll,
1191 .mmap = v4l_stk_mmap,
1192 .ioctl = video_ioctl2,
1195 static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops = {
1196 .vidioc_querycap = stk_vidioc_querycap,
1197 .vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
1198 .vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
1199 .vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
1200 .vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
1201 .vidioc_enum_input = stk_vidioc_enum_input,
1202 .vidioc_s_input = stk_vidioc_s_input,
1203 .vidioc_g_input = stk_vidioc_g_input,
1204 .vidioc_s_std = stk_vidioc_s_std,
1205 .vidioc_reqbufs = stk_vidioc_reqbufs,
1206 .vidioc_querybuf = stk_vidioc_querybuf,
1207 .vidioc_qbuf = stk_vidioc_qbuf,
1208 .vidioc_dqbuf = stk_vidioc_dqbuf,
1209 .vidioc_streamon = stk_vidioc_streamon,
1210 .vidioc_streamoff = stk_vidioc_streamoff,
1211 .vidioc_queryctrl = stk_vidioc_queryctrl,
1212 .vidioc_g_ctrl = stk_vidioc_g_ctrl,
1213 .vidioc_s_ctrl = stk_vidioc_s_ctrl,
1214 .vidioc_g_parm = stk_vidioc_g_parm,
1215 .vidioc_enum_framesizes = stk_vidioc_enum_framesizes,
1218 static void stk_v4l_dev_release(struct video_device *vd)
1220 struct stk_camera *dev = vdev_to_camera(vd);
1222 if (dev->sio_bufs != NULL || dev->isobufs != NULL)
1223 STK_ERROR("We are leaking memory\n");
1224 usb_put_intf(dev->interface);
1225 kfree(dev);
1228 static struct video_device stk_v4l_data = {
1229 .name = "stkwebcam",
1230 .tvnorms = V4L2_STD_UNKNOWN,
1231 .current_norm = V4L2_STD_UNKNOWN,
1232 .fops = &v4l_stk_fops,
1233 .ioctl_ops = &v4l_stk_ioctl_ops,
1234 .release = stk_v4l_dev_release,
1238 static int stk_register_video_device(struct stk_camera *dev)
1240 int err;
1242 dev->vdev = stk_v4l_data;
1243 dev->vdev.debug = debug;
1244 dev->vdev.parent = &dev->interface->dev;
1245 err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1246 if (err)
1247 STK_ERROR("v4l registration failed\n");
1248 else
1249 STK_INFO("Syntek USB2.0 Camera is now controlling device %s\n",
1250 video_device_node_name(&dev->vdev));
1251 return err;
1255 /* USB Stuff */
1257 static int stk_camera_probe(struct usb_interface *interface,
1258 const struct usb_device_id *id)
1260 int i;
1261 int err = 0;
1263 struct stk_camera *dev = NULL;
1264 struct usb_device *udev = interface_to_usbdev(interface);
1265 struct usb_host_interface *iface_desc;
1266 struct usb_endpoint_descriptor *endpoint;
1268 dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
1269 if (dev == NULL) {
1270 STK_ERROR("Out of memory !\n");
1271 return -ENOMEM;
1274 spin_lock_init(&dev->spinlock);
1275 init_waitqueue_head(&dev->wait_frame);
1277 dev->udev = udev;
1278 dev->interface = interface;
1279 usb_get_intf(interface);
1281 dev->vsettings.vflip = vflip;
1282 dev->vsettings.hflip = hflip;
1283 dev->n_sbufs = 0;
1284 set_present(dev);
1286 /* Set up the endpoint information
1287 * use only the first isoc-in endpoint
1288 * for the current alternate setting */
1289 iface_desc = interface->cur_altsetting;
1291 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1292 endpoint = &iface_desc->endpoint[i].desc;
1294 if (!dev->isoc_ep
1295 && usb_endpoint_is_isoc_in(endpoint)) {
1296 /* we found an isoc in endpoint */
1297 dev->isoc_ep = usb_endpoint_num(endpoint);
1298 break;
1301 if (!dev->isoc_ep) {
1302 STK_ERROR("Could not find isoc-in endpoint");
1303 err = -ENODEV;
1304 goto error;
1306 dev->vsettings.brightness = 0x7fff;
1307 dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
1308 dev->vsettings.mode = MODE_VGA;
1309 dev->frame_size = 640 * 480 * 2;
1311 INIT_LIST_HEAD(&dev->sio_avail);
1312 INIT_LIST_HEAD(&dev->sio_full);
1314 usb_set_intfdata(interface, dev);
1316 err = stk_register_video_device(dev);
1317 if (err)
1318 goto error;
1320 return 0;
1322 error:
1323 kfree(dev);
1324 return err;
1327 static void stk_camera_disconnect(struct usb_interface *interface)
1329 struct stk_camera *dev = usb_get_intfdata(interface);
1331 usb_set_intfdata(interface, NULL);
1332 unset_present(dev);
1334 wake_up_interruptible(&dev->wait_frame);
1336 STK_INFO("Syntek USB2.0 Camera release resources device %s\n",
1337 video_device_node_name(&dev->vdev));
1339 video_unregister_device(&dev->vdev);
1342 #ifdef CONFIG_PM
1343 static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
1345 struct stk_camera *dev = usb_get_intfdata(intf);
1346 if (is_streaming(dev)) {
1347 stk_stop_stream(dev);
1348 /* yes, this is ugly */
1349 set_streaming(dev);
1351 return 0;
1354 static int stk_camera_resume(struct usb_interface *intf)
1356 struct stk_camera *dev = usb_get_intfdata(intf);
1357 if (!is_initialised(dev))
1358 return 0;
1359 unset_initialised(dev);
1360 stk_initialise(dev);
1361 stk_camera_write_reg(dev, 0x0, 0x49);
1362 stk_setup_format(dev);
1363 if (is_streaming(dev))
1364 stk_start_stream(dev);
1365 return 0;
1367 #endif
1369 static struct usb_driver stk_camera_driver = {
1370 .name = "stkwebcam",
1371 .probe = stk_camera_probe,
1372 .disconnect = stk_camera_disconnect,
1373 .id_table = stkwebcam_table,
1374 #ifdef CONFIG_PM
1375 .suspend = stk_camera_suspend,
1376 .resume = stk_camera_resume,
1377 #endif
1380 module_usb_driver(stk_camera_driver);