Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / media / video / stk-webcam.c
blobe9b61c90865a2cbfaefb51c13c373d80f038bb7f
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>
30 #include <linux/kref.h>
32 #include <linux/usb.h>
33 #include <linux/vmalloc.h>
34 #include <linux/videodev2.h>
35 #include <media/v4l2-common.h>
37 #include "stk-webcam.h"
40 static int hflip = 1;
41 module_param(hflip, bool, 0444);
42 MODULE_PARM_DESC(hflip, "Horizontal image flip (mirror). Defaults to 1");
44 static int vflip = 1;
45 module_param(vflip, bool, 0444);
46 MODULE_PARM_DESC(vflip, "Vertical image flip. Defaults to 1");
48 static int debug;
49 module_param(debug, int, 0444);
50 MODULE_PARM_DESC(debug, "Debug v4l ioctls. Defaults to 0");
52 MODULE_LICENSE("GPL");
53 MODULE_AUTHOR("Jaime Velasco Juan <jsagarribay@gmail.com> and Nicolas VIVIEN");
54 MODULE_DESCRIPTION("Syntek DC1125 webcam driver");
58 /* Some cameras have audio interfaces, we aren't interested in those */
59 static struct usb_device_id stkwebcam_table[] = {
60 { USB_DEVICE_AND_INTERFACE_INFO(0x174f, 0xa311, 0xff, 0xff, 0xff) },
61 { USB_DEVICE_AND_INTERFACE_INFO(0x05e1, 0x0501, 0xff, 0xff, 0xff) },
62 { }
64 MODULE_DEVICE_TABLE(usb, stkwebcam_table);
66 <<<<<<< HEAD:drivers/media/video/stk-webcam.c
67 void stk_camera_cleanup(struct kref *kref)
68 =======
69 static void stk_camera_cleanup(struct kref *kref)
70 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/stk-webcam.c
72 struct stk_camera *dev = to_stk_camera(kref);
74 STK_INFO("Syntek USB2.0 Camera release resources"
75 " video device /dev/video%d\n", dev->vdev.minor);
76 video_unregister_device(&dev->vdev);
77 dev->vdev.priv = NULL;
79 if (dev->sio_bufs != NULL || dev->isobufs != NULL)
80 STK_ERROR("We are leaking memory\n");
81 usb_put_intf(dev->interface);
82 kfree(dev);
87 * Basic stuff
89 int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
91 struct usb_device *udev = dev->udev;
92 int ret;
94 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
95 0x01,
96 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
97 value,
98 index,
99 NULL,
101 500);
102 if (ret < 0)
103 return ret;
104 else
105 return 0;
108 int stk_camera_read_reg(struct stk_camera *dev, u16 index, int *value)
110 struct usb_device *udev = dev->udev;
111 int ret;
113 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
114 0x00,
115 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
116 0x00,
117 index,
118 (u8 *) value,
119 sizeof(u8),
120 500);
121 if (ret < 0)
122 return ret;
123 else
124 return 0;
127 static int stk_start_stream(struct stk_camera *dev)
129 int value;
130 int i, ret;
131 int value_116, value_117;
133 if (!is_present(dev))
134 return -ENODEV;
135 if (!is_memallocd(dev) || !is_initialised(dev)) {
136 STK_ERROR("FIXME: Buffers are not allocated\n");
137 return -EFAULT;
139 ret = usb_set_interface(dev->udev, 0, 5);
141 if (ret < 0)
142 STK_ERROR("usb_set_interface failed !\n");
143 if (stk_sensor_wakeup(dev))
144 STK_ERROR("error awaking the sensor\n");
146 stk_camera_read_reg(dev, 0x0116, &value_116);
147 stk_camera_read_reg(dev, 0x0117, &value_117);
149 stk_camera_write_reg(dev, 0x0116, 0x0000);
150 stk_camera_write_reg(dev, 0x0117, 0x0000);
152 stk_camera_read_reg(dev, 0x0100, &value);
153 stk_camera_write_reg(dev, 0x0100, value | 0x80);
155 stk_camera_write_reg(dev, 0x0116, value_116);
156 stk_camera_write_reg(dev, 0x0117, value_117);
157 for (i = 0; i < MAX_ISO_BUFS; i++) {
158 if (dev->isobufs[i].urb) {
159 ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL);
160 atomic_inc(&dev->urbs_used);
161 if (ret)
162 return ret;
165 set_streaming(dev);
166 return 0;
169 static int stk_stop_stream(struct stk_camera *dev)
171 int value;
172 int i;
173 if (is_present(dev)) {
174 stk_camera_read_reg(dev, 0x0100, &value);
175 stk_camera_write_reg(dev, 0x0100, value & ~0x80);
176 if (dev->isobufs != NULL) {
177 for (i = 0; i < MAX_ISO_BUFS; i++) {
178 if (dev->isobufs[i].urb)
179 usb_kill_urb(dev->isobufs[i].urb);
182 unset_streaming(dev);
184 if (usb_set_interface(dev->udev, 0, 0))
185 STK_ERROR("usb_set_interface failed !\n");
186 if (stk_sensor_sleep(dev))
187 STK_ERROR("error suspending the sensor\n");
189 return 0;
193 * This seems to be the shortest init sequence we
194 * must do in order to find the sensor
195 * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
196 * is also reset. Maybe powers down it?
197 * Rest of values don't make a difference
200 static struct regval stk1125_initvals[] = {
201 /*TODO: What means this sequence? */
202 {0x0000, 0x24},
203 {0x0100, 0x21},
204 {0x0002, 0x68},
205 {0x0003, 0x80},
206 {0x0005, 0x00},
207 {0x0007, 0x03},
208 {0x000d, 0x00},
209 {0x000f, 0x02},
210 {0x0300, 0x12},
211 {0x0350, 0x41},
212 {0x0351, 0x00},
213 {0x0352, 0x00},
214 {0x0353, 0x00},
215 {0x0018, 0x10},
216 {0x0019, 0x00},
217 {0x001b, 0x0e},
218 {0x001c, 0x46},
219 {0x0300, 0x80},
220 {0x001a, 0x04},
221 {0x0110, 0x00},
222 {0x0111, 0x00},
223 {0x0112, 0x00},
224 {0x0113, 0x00},
226 {0xffff, 0xff},
230 static int stk_initialise(struct stk_camera *dev)
232 struct regval *rv;
233 int ret;
234 if (!is_present(dev))
235 return -ENODEV;
236 if (is_initialised(dev))
237 return 0;
238 rv = stk1125_initvals;
239 while (rv->reg != 0xffff) {
240 ret = stk_camera_write_reg(dev, rv->reg, rv->val);
241 if (ret)
242 return ret;
243 rv++;
245 if (stk_sensor_init(dev) == 0) {
246 set_initialised(dev);
247 return 0;
248 } else
249 return -1;
252 /* sysfs functions */
253 /*FIXME cleanup this */
255 static ssize_t show_brightness(struct device *class,
256 struct device_attribute *attr, char *buf)
258 struct video_device *vdev = to_video_device(class);
259 struct stk_camera *dev = vdev_to_camera(vdev);
261 return sprintf(buf, "%X\n", dev->vsettings.brightness);
264 static ssize_t store_brightness(struct device *class,
265 struct device_attribute *attr, const char *buf, size_t count)
267 char *endp;
268 unsigned long value;
269 int ret;
271 struct video_device *vdev = to_video_device(class);
272 struct stk_camera *dev = vdev_to_camera(vdev);
274 value = simple_strtoul(buf, &endp, 16);
276 dev->vsettings.brightness = (int) value;
278 ret = stk_sensor_set_brightness(dev, value >> 8);
279 if (ret)
280 return ret;
281 else
282 return count;
285 static ssize_t show_hflip(struct device *class,
286 struct device_attribute *attr, char *buf)
288 struct video_device *vdev = to_video_device(class);
289 struct stk_camera *dev = vdev_to_camera(vdev);
291 return sprintf(buf, "%d\n", dev->vsettings.hflip);
294 static ssize_t store_hflip(struct device *class,
295 struct device_attribute *attr, const char *buf, size_t count)
297 struct video_device *vdev = to_video_device(class);
298 struct stk_camera *dev = vdev_to_camera(vdev);
300 if (strncmp(buf, "1", 1) == 0)
301 dev->vsettings.hflip = 1;
302 else if (strncmp(buf, "0", 1) == 0)
303 dev->vsettings.hflip = 0;
304 else
305 return -EINVAL;
307 return strlen(buf);
310 static ssize_t show_vflip(struct device *class,
311 struct device_attribute *attr, char *buf)
313 struct video_device *vdev = to_video_device(class);
314 struct stk_camera *dev = vdev_to_camera(vdev);
316 return sprintf(buf, "%d\n", dev->vsettings.vflip);
319 static ssize_t store_vflip(struct device *class,
320 struct device_attribute *attr, const char *buf, size_t count)
322 struct video_device *vdev = to_video_device(class);
323 struct stk_camera *dev = vdev_to_camera(vdev);
325 if (strncmp(buf, "1", 1) == 0)
326 dev->vsettings.vflip = 1;
327 else if (strncmp(buf, "0", 1) == 0)
328 dev->vsettings.vflip = 0;
329 else
330 return -EINVAL;
332 return strlen(buf);
335 static DEVICE_ATTR(brightness, S_IRUGO | S_IWUGO,
336 show_brightness, store_brightness);
337 static DEVICE_ATTR(hflip, S_IRUGO | S_IWUGO, show_hflip, store_hflip);
338 static DEVICE_ATTR(vflip, S_IRUGO | S_IWUGO, show_vflip, store_vflip);
340 static int stk_create_sysfs_files(struct video_device *vdev)
342 int ret;
344 ret = video_device_create_file(vdev, &dev_attr_brightness);
345 ret += video_device_create_file(vdev, &dev_attr_hflip);
346 ret += video_device_create_file(vdev, &dev_attr_vflip);
347 return ret;
350 static void stk_remove_sysfs_files(struct video_device *vdev)
352 video_device_remove_file(vdev, &dev_attr_brightness);
353 video_device_remove_file(vdev, &dev_attr_hflip);
354 video_device_remove_file(vdev, &dev_attr_vflip);
358 /* *********************************************** */
360 * This function is called as an URB transfert is complete (Isochronous pipe).
361 * So, the traitement is done in interrupt time, so it has be fast, not crash,
362 * and not stall. Neat.
364 static void stk_isoc_handler(struct urb *urb)
366 int i;
367 int ret;
368 int framelen;
369 unsigned long flags;
371 unsigned char *fill = NULL;
372 unsigned char *iso_buf = NULL;
374 struct stk_camera *dev;
375 struct stk_sio_buffer *fb;
377 dev = (struct stk_camera *) urb->context;
379 if (dev == NULL) {
380 STK_ERROR("isoc_handler called with NULL device !\n");
381 return;
384 if (urb->status == -ENOENT || urb->status == -ECONNRESET
385 || urb->status == -ESHUTDOWN) {
386 atomic_dec(&dev->urbs_used);
387 return;
390 spin_lock_irqsave(&dev->spinlock, flags);
392 if (urb->status != -EINPROGRESS && urb->status != 0) {
393 STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
394 goto resubmit;
397 if (list_empty(&dev->sio_avail)) {
398 /*FIXME Stop streaming after a while */
399 (void) (printk_ratelimit() &&
400 STK_ERROR("isoc_handler without available buffer!\n"));
401 goto resubmit;
403 fb = list_first_entry(&dev->sio_avail,
404 struct stk_sio_buffer, list);
405 fill = fb->buffer + fb->v4lbuf.bytesused;
407 for (i = 0; i < urb->number_of_packets; i++) {
408 if (urb->iso_frame_desc[i].status != 0) {
409 if (urb->iso_frame_desc[i].status != -EXDEV)
410 STK_ERROR("Frame %d has error %d\n", i,
411 urb->iso_frame_desc[i].status);
412 continue;
414 framelen = urb->iso_frame_desc[i].actual_length;
415 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
417 if (framelen <= 4)
418 continue; /* no data */
421 * we found something informational from there
422 * the isoc frames have to type of headers
423 * type1: 00 xx 00 00 or 20 xx 00 00
424 * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
425 * xx is a sequencer which has never been seen over 0x3f
426 * imho data written down looks like bayer, i see similarities
427 * after every 640 bytes
429 if (*iso_buf & 0x80) {
430 framelen -= 8;
431 iso_buf += 8;
432 /* This marks a new frame */
433 if (fb->v4lbuf.bytesused != 0
434 && fb->v4lbuf.bytesused != dev->frame_size) {
435 (void) (printk_ratelimit() &&
436 STK_ERROR("frame %d, "
437 "bytesused=%d, skipping\n",
438 i, fb->v4lbuf.bytesused));
439 fb->v4lbuf.bytesused = 0;
440 fill = fb->buffer;
441 } else if (fb->v4lbuf.bytesused == dev->frame_size) {
442 list_move_tail(dev->sio_avail.next,
443 &dev->sio_full);
444 wake_up(&dev->wait_frame);
445 if (list_empty(&dev->sio_avail)) {
446 (void) (printk_ratelimit() &&
447 STK_ERROR("No buffer available\n"));
448 goto resubmit;
450 fb = list_first_entry(&dev->sio_avail,
451 struct stk_sio_buffer, list);
452 fb->v4lbuf.bytesused = 0;
453 fill = fb->buffer;
455 } else {
456 framelen -= 4;
457 iso_buf += 4;
460 /* Our buffer is full !!! */
461 if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
462 (void) (printk_ratelimit() &&
463 STK_ERROR("Frame buffer overflow, lost sync\n"));
464 /*FIXME Do something here? */
465 continue;
467 spin_unlock_irqrestore(&dev->spinlock, flags);
468 memcpy(fill, iso_buf, framelen);
469 spin_lock_irqsave(&dev->spinlock, flags);
470 fill += framelen;
472 /* New size of our buffer */
473 fb->v4lbuf.bytesused += framelen;
476 resubmit:
477 spin_unlock_irqrestore(&dev->spinlock, flags);
478 urb->dev = dev->udev;
479 ret = usb_submit_urb(urb, GFP_ATOMIC);
480 if (ret != 0) {
481 STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
482 ret);
486 /* -------------------------------------------- */
488 static int stk_prepare_iso(struct stk_camera *dev)
490 void *kbuf;
491 int i, j;
492 struct urb *urb;
493 struct usb_device *udev;
495 if (dev == NULL)
496 return -ENXIO;
497 udev = dev->udev;
499 if (dev->isobufs)
500 STK_ERROR("isobufs already allocated. Bad\n");
501 else
502 dev->isobufs = kzalloc(MAX_ISO_BUFS * sizeof(*dev->isobufs),
503 GFP_KERNEL);
504 if (dev->isobufs == NULL) {
505 STK_ERROR("Unable to allocate iso buffers\n");
506 return -ENOMEM;
508 for (i = 0; i < MAX_ISO_BUFS; i++) {
509 if (dev->isobufs[i].data == NULL) {
510 kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
511 if (kbuf == NULL) {
512 STK_ERROR("Failed to allocate iso buffer %d\n",
514 goto isobufs_out;
516 dev->isobufs[i].data = kbuf;
517 } else
518 STK_ERROR("isobuf data already allocated\n");
519 if (dev->isobufs[i].urb == NULL) {
520 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
521 if (urb == NULL) {
522 STK_ERROR("Failed to allocate URB %d\n", i);
523 goto isobufs_out;
525 dev->isobufs[i].urb = urb;
526 } else {
527 STK_ERROR("Killing URB\n");
528 usb_kill_urb(dev->isobufs[i].urb);
529 urb = dev->isobufs[i].urb;
531 urb->interval = 1;
532 urb->dev = udev;
533 urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
534 urb->transfer_flags = URB_ISO_ASAP;
535 urb->transfer_buffer = dev->isobufs[i].data;
536 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
537 urb->complete = stk_isoc_handler;
538 urb->context = dev;
539 urb->start_frame = 0;
540 urb->number_of_packets = ISO_FRAMES_PER_DESC;
542 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
543 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
544 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
547 set_memallocd(dev);
548 return 0;
550 isobufs_out:
551 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
552 kfree(dev->isobufs[i].data);
553 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
554 usb_free_urb(dev->isobufs[i].urb);
555 kfree(dev->isobufs);
556 dev->isobufs = NULL;
557 return -ENOMEM;
560 static void stk_clean_iso(struct stk_camera *dev)
562 int i;
564 if (dev == NULL || dev->isobufs == NULL)
565 return;
567 for (i = 0; i < MAX_ISO_BUFS; i++) {
568 struct urb *urb;
570 urb = dev->isobufs[i].urb;
571 if (urb) {
572 if (atomic_read(&dev->urbs_used))
573 usb_kill_urb(urb);
574 usb_free_urb(urb);
576 kfree(dev->isobufs[i].data);
578 kfree(dev->isobufs);
579 dev->isobufs = NULL;
580 unset_memallocd(dev);
583 static int stk_setup_siobuf(struct stk_camera *dev, int index)
585 struct stk_sio_buffer *buf = dev->sio_bufs + index;
586 INIT_LIST_HEAD(&buf->list);
587 buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
588 buf->buffer = vmalloc_user(buf->v4lbuf.length);
589 if (buf->buffer == NULL)
590 return -ENOMEM;
591 buf->mapcount = 0;
592 buf->dev = dev;
593 buf->v4lbuf.index = index;
594 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
595 buf->v4lbuf.field = V4L2_FIELD_NONE;
596 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
597 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
598 return 0;
601 static int stk_free_sio_buffers(struct stk_camera *dev)
603 int i;
604 int nbufs;
605 unsigned long flags;
606 if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
607 return 0;
609 * If any buffers are mapped, we cannot free them at all.
611 for (i = 0; i < dev->n_sbufs; i++) {
612 if (dev->sio_bufs[i].mapcount > 0)
613 return -EBUSY;
616 * OK, let's do it.
618 spin_lock_irqsave(&dev->spinlock, flags);
619 INIT_LIST_HEAD(&dev->sio_avail);
620 INIT_LIST_HEAD(&dev->sio_full);
621 nbufs = dev->n_sbufs;
622 dev->n_sbufs = 0;
623 spin_unlock_irqrestore(&dev->spinlock, flags);
624 for (i = 0; i < nbufs; i++) {
625 if (dev->sio_bufs[i].buffer != NULL)
626 vfree(dev->sio_bufs[i].buffer);
628 kfree(dev->sio_bufs);
629 dev->sio_bufs = NULL;
630 return 0;
633 static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
635 int i;
636 if (dev->sio_bufs != NULL)
637 STK_ERROR("sio_bufs already allocated\n");
638 else {
639 dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
640 GFP_KERNEL);
641 if (dev->sio_bufs == NULL)
642 return -ENOMEM;
643 for (i = 0; i < n_sbufs; i++) {
644 if (stk_setup_siobuf(dev, i))
645 return (dev->n_sbufs > 1)? 0 : -ENOMEM;
646 dev->n_sbufs = i+1;
649 return 0;
652 static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
654 int err;
655 err = stk_prepare_iso(dev);
656 if (err) {
657 stk_clean_iso(dev);
658 return err;
660 err = stk_prepare_sio_buffers(dev, n_sbufs);
661 if (err) {
662 stk_free_sio_buffers(dev);
663 return err;
665 return 0;
668 static void stk_free_buffers(struct stk_camera *dev)
670 stk_clean_iso(dev);
671 stk_free_sio_buffers(dev);
673 /* -------------------------------------------- */
675 /* v4l file operations */
677 static int v4l_stk_open(struct inode *inode, struct file *fp)
679 struct stk_camera *dev;
680 struct video_device *vdev;
682 vdev = video_devdata(fp);
683 dev = vdev_to_camera(vdev);
685 if (dev == NULL || !is_present(dev))
686 return -ENXIO;
687 fp->private_data = vdev;
688 kref_get(&dev->kref);
689 <<<<<<< HEAD:drivers/media/video/stk-webcam.c
690 =======
691 usb_autopm_get_interface(dev->interface);
692 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/stk-webcam.c
694 return 0;
697 static int v4l_stk_release(struct inode *inode, struct file *fp)
699 struct stk_camera *dev;
700 struct video_device *vdev;
702 vdev = video_devdata(fp);
703 if (vdev == NULL) {
704 STK_ERROR("v4l_release called w/o video devdata\n");
705 return -EFAULT;
707 dev = vdev_to_camera(vdev);
708 if (dev == NULL) {
709 STK_ERROR("v4l_release called on removed device\n");
710 return -ENODEV;
713 if (dev->owner != fp) {
714 <<<<<<< HEAD:drivers/media/video/stk-webcam.c
715 =======
716 usb_autopm_put_interface(dev->interface);
717 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/stk-webcam.c
718 kref_put(&dev->kref, stk_camera_cleanup);
719 return 0;
722 stk_stop_stream(dev);
724 stk_free_buffers(dev);
726 dev->owner = NULL;
728 <<<<<<< HEAD:drivers/media/video/stk-webcam.c
729 =======
730 usb_autopm_put_interface(dev->interface);
731 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/stk-webcam.c
732 kref_put(&dev->kref, stk_camera_cleanup);
734 return 0;
737 static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
738 size_t count, loff_t *f_pos)
740 int i;
741 int ret;
742 unsigned long flags;
743 struct stk_camera *dev;
744 struct video_device *vdev;
745 struct stk_sio_buffer *sbuf;
747 vdev = video_devdata(fp);
748 if (vdev == NULL)
749 return -EFAULT;
750 dev = vdev_to_camera(vdev);
752 if (dev == NULL)
753 return -EIO;
755 if (!is_present(dev))
756 return -EIO;
757 if (dev->owner && dev->owner != fp)
758 return -EBUSY;
759 dev->owner = fp;
760 if (!is_streaming(dev)) {
761 if (stk_initialise(dev)
762 || stk_allocate_buffers(dev, 3)
763 || stk_start_stream(dev))
764 return -ENOMEM;
765 spin_lock_irqsave(&dev->spinlock, flags);
766 for (i = 0; i < dev->n_sbufs; i++) {
767 list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
768 dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
770 spin_unlock_irqrestore(&dev->spinlock, flags);
772 if (*f_pos == 0) {
773 if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
774 return -EWOULDBLOCK;
775 ret = wait_event_interruptible(dev->wait_frame,
776 !list_empty(&dev->sio_full) || !is_present(dev));
777 if (ret)
778 return ret;
779 if (!is_present(dev))
780 return -EIO;
782 if (count + *f_pos > dev->frame_size)
783 count = dev->frame_size - *f_pos;
784 spin_lock_irqsave(&dev->spinlock, flags);
785 if (list_empty(&dev->sio_full)) {
786 spin_unlock_irqrestore(&dev->spinlock, flags);
787 STK_ERROR("BUG: No siobufs ready\n");
788 return 0;
790 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
791 spin_unlock_irqrestore(&dev->spinlock, flags);
793 if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
794 return -EFAULT;
796 *f_pos += count;
798 if (*f_pos >= dev->frame_size) {
799 *f_pos = 0;
800 spin_lock_irqsave(&dev->spinlock, flags);
801 list_move_tail(&sbuf->list, &dev->sio_avail);
802 spin_unlock_irqrestore(&dev->spinlock, flags);
804 return count;
807 static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
809 struct stk_camera *dev;
810 struct video_device *vdev;
812 vdev = video_devdata(fp);
814 if (vdev == NULL)
815 return -EFAULT;
817 dev = vdev_to_camera(vdev);
818 if (dev == NULL)
819 return -ENODEV;
821 poll_wait(fp, &dev->wait_frame, wait);
823 if (!is_present(dev))
824 return POLLERR;
826 if (!list_empty(&dev->sio_full))
827 return (POLLIN | POLLRDNORM);
829 return 0;
833 static void stk_v4l_vm_open(struct vm_area_struct *vma)
835 struct stk_sio_buffer *sbuf = vma->vm_private_data;
836 sbuf->mapcount++;
838 static void stk_v4l_vm_close(struct vm_area_struct *vma)
840 struct stk_sio_buffer *sbuf = vma->vm_private_data;
841 sbuf->mapcount--;
842 if (sbuf->mapcount == 0)
843 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
845 static struct vm_operations_struct stk_v4l_vm_ops = {
846 .open = stk_v4l_vm_open,
847 .close = stk_v4l_vm_close
850 static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
852 unsigned int i;
853 int ret;
854 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
855 struct stk_camera *dev;
856 struct video_device *vdev;
857 struct stk_sio_buffer *sbuf = NULL;
859 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
860 return -EINVAL;
862 vdev = video_devdata(fp);
863 dev = vdev_to_camera(vdev);
865 for (i = 0; i < dev->n_sbufs; i++) {
866 if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
867 sbuf = dev->sio_bufs + i;
868 break;
871 if (sbuf == NULL)
872 return -EINVAL;
873 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
874 if (ret)
875 return ret;
876 vma->vm_flags |= VM_DONTEXPAND;
877 vma->vm_private_data = sbuf;
878 vma->vm_ops = &stk_v4l_vm_ops;
879 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
880 stk_v4l_vm_open(vma);
881 return 0;
884 /* v4l ioctl handlers */
886 static int stk_vidioc_querycap(struct file *filp,
887 void *priv, struct v4l2_capability *cap)
889 strcpy(cap->driver, "stk");
890 strcpy(cap->card, "stk");
891 cap->version = DRIVER_VERSION_NUM;
893 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
894 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
895 return 0;
898 static int stk_vidioc_enum_input(struct file *filp,
899 void *priv, struct v4l2_input *input)
901 if (input->index != 0)
902 return -EINVAL;
904 strcpy(input->name, "Syntek USB Camera");
905 input->type = V4L2_INPUT_TYPE_CAMERA;
906 return 0;
910 static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
912 *i = 0;
913 return 0;
916 static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
918 if (i != 0)
919 return -EINVAL;
920 else
921 return 0;
924 /* from vivi.c */
925 static int stk_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
927 return 0;
930 /* List of all V4Lv2 controls supported by the driver */
931 static struct v4l2_queryctrl stk_controls[] = {
933 .id = V4L2_CID_BRIGHTNESS,
934 .type = V4L2_CTRL_TYPE_INTEGER,
935 .name = "Brightness",
936 .minimum = 0,
937 .maximum = 0xffff,
938 .step = 0x0100,
939 .default_value = 0x6000,
941 /*TODO: get more controls to work */
944 static int stk_vidioc_queryctrl(struct file *filp,
945 void *priv, struct v4l2_queryctrl *c)
947 int i;
948 int nbr;
949 nbr = ARRAY_SIZE(stk_controls);
951 for (i = 0; i < nbr; i++) {
952 if (stk_controls[i].id == c->id) {
953 memcpy(c, &stk_controls[i],
954 sizeof(struct v4l2_queryctrl));
955 return 0;
958 return -EINVAL;
961 static int stk_vidioc_g_ctrl(struct file *filp,
962 void *priv, struct v4l2_control *c)
964 struct stk_camera *dev = priv;
965 switch (c->id) {
966 case V4L2_CID_BRIGHTNESS:
967 c->value = dev->vsettings.brightness;
968 break;
969 default:
970 return -EINVAL;
972 return 0;
975 static int stk_vidioc_s_ctrl(struct file *filp,
976 void *priv, struct v4l2_control *c)
978 struct stk_camera *dev = priv;
979 switch (c->id) {
980 case V4L2_CID_BRIGHTNESS:
981 dev->vsettings.brightness = c->value;
982 return stk_sensor_set_brightness(dev, c->value >> 8);
983 default:
984 return -EINVAL;
986 return 0;
990 static int stk_vidioc_enum_fmt_cap(struct file *filp,
991 void *priv, struct v4l2_fmtdesc *fmtd)
993 fmtd->flags = 0;
995 switch (fmtd->index) {
996 case 0:
997 fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
998 strcpy(fmtd->description, "r5g6b5");
999 break;
1000 case 1:
1001 fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
1002 strcpy(fmtd->description, "r5g6b5BE");
1003 break;
1004 case 2:
1005 fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
1006 strcpy(fmtd->description, "yuv4:2:2");
1007 break;
1008 case 3:
1009 fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
1010 strcpy(fmtd->description, "Raw bayer");
1011 break;
1012 <<<<<<< HEAD:drivers/media/video/stk-webcam.c
1013 =======
1014 case 4:
1015 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1016 strcpy(fmtd->description, "yuv4:2:2");
1017 break;
1018 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/stk-webcam.c
1019 default:
1020 return -EINVAL;
1022 return 0;
1025 static struct stk_size {
1026 unsigned w;
1027 unsigned h;
1028 enum stk_mode m;
1029 } stk_sizes[] = {
1030 { .w = 1280, .h = 1024, .m = MODE_SXGA, },
1031 { .w = 640, .h = 480, .m = MODE_VGA, },
1032 { .w = 352, .h = 288, .m = MODE_CIF, },
1033 { .w = 320, .h = 240, .m = MODE_QVGA, },
1034 { .w = 176, .h = 144, .m = MODE_QCIF, },
1037 static int stk_vidioc_g_fmt_cap(struct file *filp,
1038 void *priv, struct v4l2_format *f)
1040 struct v4l2_pix_format *pix_format = &f->fmt.pix;
1041 struct stk_camera *dev = priv;
1042 int i;
1044 for (i = 0; i < ARRAY_SIZE(stk_sizes)
1045 && stk_sizes[i].m != dev->vsettings.mode;
1046 i++);
1047 if (i == ARRAY_SIZE(stk_sizes)) {
1048 STK_ERROR("ERROR: mode invalid\n");
1049 return -EINVAL;
1051 pix_format->width = stk_sizes[i].w;
1052 pix_format->height = stk_sizes[i].h;
1053 pix_format->field = V4L2_FIELD_NONE;
1054 pix_format->colorspace = V4L2_COLORSPACE_SRGB;
1055 pix_format->priv = 0;
1056 pix_format->pixelformat = dev->vsettings.palette;
1057 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
1058 pix_format->bytesperline = pix_format->width;
1059 else
1060 pix_format->bytesperline = 2 * pix_format->width;
1061 pix_format->sizeimage = pix_format->bytesperline
1062 * pix_format->height;
1063 return 0;
1066 static int stk_vidioc_try_fmt_cap(struct file *filp,
1067 void *priv, struct v4l2_format *fmtd)
1069 int i;
1070 switch (fmtd->fmt.pix.pixelformat) {
1071 case V4L2_PIX_FMT_RGB565:
1072 case V4L2_PIX_FMT_RGB565X:
1073 case V4L2_PIX_FMT_UYVY:
1074 <<<<<<< HEAD:drivers/media/video/stk-webcam.c
1075 =======
1076 case V4L2_PIX_FMT_YUYV:
1077 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/stk-webcam.c
1078 case V4L2_PIX_FMT_SBGGR8:
1079 break;
1080 default:
1081 return -EINVAL;
1083 for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
1084 if (fmtd->fmt.pix.width > stk_sizes[i].w)
1085 break;
1087 if (i == ARRAY_SIZE(stk_sizes)
1088 || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
1089 < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
1090 fmtd->fmt.pix.height = stk_sizes[i-1].h;
1091 fmtd->fmt.pix.width = stk_sizes[i-1].w;
1092 fmtd->fmt.pix.priv = i - 1;
1093 } else {
1094 fmtd->fmt.pix.height = stk_sizes[i].h;
1095 fmtd->fmt.pix.width = stk_sizes[i].w;
1096 fmtd->fmt.pix.priv = i;
1099 fmtd->fmt.pix.field = V4L2_FIELD_NONE;
1100 fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
1101 if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
1102 fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
1103 else
1104 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
1105 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
1106 * fmtd->fmt.pix.height;
1107 return 0;
1110 <<<<<<< HEAD:drivers/media/video/stk-webcam.c
1111 =======
1112 static int stk_setup_format(struct stk_camera *dev)
1114 int i = 0;
1115 int depth;
1116 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
1117 depth = 1;
1118 else
1119 depth = 2;
1120 while (stk_sizes[i].m != dev->vsettings.mode
1121 && i < ARRAY_SIZE(stk_sizes))
1122 i++;
1123 if (i == ARRAY_SIZE(stk_sizes)) {
1124 STK_ERROR("Something is broken in %s\n", __FUNCTION__);
1125 return -EFAULT;
1127 /* This registers controls some timings, not sure of what. */
1128 stk_camera_write_reg(dev, 0x001b, 0x0e);
1129 if (dev->vsettings.mode == MODE_SXGA)
1130 stk_camera_write_reg(dev, 0x001c, 0x0e);
1131 else
1132 stk_camera_write_reg(dev, 0x001c, 0x46);
1134 * Registers 0x0115 0x0114 are the size of each line (bytes),
1135 * regs 0x0117 0x0116 are the heigth of the image.
1137 stk_camera_write_reg(dev, 0x0115,
1138 ((stk_sizes[i].w * depth) >> 8) & 0xff);
1139 stk_camera_write_reg(dev, 0x0114,
1140 (stk_sizes[i].w * depth) & 0xff);
1141 stk_camera_write_reg(dev, 0x0117,
1142 (stk_sizes[i].h >> 8) & 0xff);
1143 stk_camera_write_reg(dev, 0x0116,
1144 stk_sizes[i].h & 0xff);
1145 return stk_sensor_configure(dev);
1148 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/stk-webcam.c
1149 static int stk_vidioc_s_fmt_cap(struct file *filp,
1150 void *priv, struct v4l2_format *fmtd)
1152 int ret;
1153 struct stk_camera *dev = priv;
1155 if (dev == NULL)
1156 return -ENODEV;
1157 if (!is_present(dev))
1158 return -ENODEV;
1159 if (is_streaming(dev))
1160 return -EBUSY;
1161 if (dev->owner && dev->owner != filp)
1162 return -EBUSY;
1163 <<<<<<< HEAD:drivers/media/video/stk-webcam.c
1164 dev->owner = filp;
1165 =======
1166 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/stk-webcam.c
1167 ret = stk_vidioc_try_fmt_cap(filp, priv, fmtd);
1168 if (ret)
1169 return ret;
1170 <<<<<<< HEAD:drivers/media/video/stk-webcam.c
1171 =======
1172 dev->owner = filp;
1173 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/stk-webcam.c
1175 dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
1176 stk_free_buffers(dev);
1177 dev->frame_size = fmtd->fmt.pix.sizeimage;
1178 dev->vsettings.mode = stk_sizes[fmtd->fmt.pix.priv].m;
1180 stk_initialise(dev);
1181 <<<<<<< HEAD:drivers/media/video/stk-webcam.c
1182 /* This registers controls some timings, not sure of what. */
1183 stk_camera_write_reg(dev, 0x001b, 0x0e);
1184 if (dev->vsettings.mode == MODE_SXGA)
1185 stk_camera_write_reg(dev, 0x001c, 0x0e);
1186 else
1187 stk_camera_write_reg(dev, 0x001c, 0x46);
1189 * Registers 0x0115 0x0114 are the size of each line (bytes),
1190 * regs 0x0117 0x0116 are the heigth of the image.
1192 stk_camera_write_reg(dev, 0x0115,
1193 (fmtd->fmt.pix.bytesperline >> 8) & 0xff);
1194 stk_camera_write_reg(dev, 0x0114,
1195 fmtd->fmt.pix.bytesperline & 0xff);
1196 stk_camera_write_reg(dev, 0x0117,
1197 (fmtd->fmt.pix.height >> 8) & 0xff);
1198 stk_camera_write_reg(dev, 0x0116,
1199 fmtd->fmt.pix.height & 0xff);
1200 return stk_sensor_configure(dev);
1201 =======
1202 return stk_setup_format(dev);
1203 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/stk-webcam.c
1206 static int stk_vidioc_reqbufs(struct file *filp,
1207 void *priv, struct v4l2_requestbuffers *rb)
1209 struct stk_camera *dev = priv;
1211 if (dev == NULL)
1212 return -ENODEV;
1213 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1214 return -EINVAL;
1215 if (rb->memory != V4L2_MEMORY_MMAP)
1216 return -EINVAL;
1217 if (is_streaming(dev)
1218 || (dev->owner && dev->owner != filp))
1219 return -EBUSY;
1220 dev->owner = filp;
1222 /*FIXME If they ask for zero, we must stop streaming and free */
1223 if (rb->count < 3)
1224 rb->count = 3;
1225 /* Arbitrary limit */
1226 else if (rb->count > 5)
1227 rb->count = 5;
1229 stk_allocate_buffers(dev, rb->count);
1230 rb->count = dev->n_sbufs;
1231 return 0;
1234 static int stk_vidioc_querybuf(struct file *filp,
1235 void *priv, struct v4l2_buffer *buf)
1237 int index;
1238 struct stk_camera *dev = priv;
1239 struct stk_sio_buffer *sbuf;
1241 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1242 return -EINVAL;
1244 index = buf->index;
1246 if (index < 0 || index >= dev->n_sbufs)
1247 return -EINVAL;
1248 sbuf = dev->sio_bufs + buf->index;
1249 *buf = sbuf->v4lbuf;
1250 return 0;
1253 static int stk_vidioc_qbuf(struct file *filp,
1254 void *priv, struct v4l2_buffer *buf)
1256 struct stk_camera *dev = priv;
1257 struct stk_sio_buffer *sbuf;
1258 unsigned long flags;
1259 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1260 return -EINVAL;
1262 if (buf->memory != V4L2_MEMORY_MMAP)
1263 return -EINVAL;
1265 if (buf->index < 0 || buf->index >= dev->n_sbufs)
1266 return -EINVAL;
1267 sbuf = dev->sio_bufs + buf->index;
1268 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
1269 return 0;
1270 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1271 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1272 spin_lock_irqsave(&dev->spinlock, flags);
1273 list_add_tail(&sbuf->list, &dev->sio_avail);
1274 *buf = sbuf->v4lbuf;
1275 spin_unlock_irqrestore(&dev->spinlock, flags);
1276 return 0;
1279 static int stk_vidioc_dqbuf(struct file *filp,
1280 void *priv, struct v4l2_buffer *buf)
1282 struct stk_camera *dev = priv;
1283 struct stk_sio_buffer *sbuf;
1284 unsigned long flags;
1285 int ret;
1287 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1288 || !is_streaming(dev))
1289 return -EINVAL;
1291 if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
1292 return -EWOULDBLOCK;
1293 ret = wait_event_interruptible(dev->wait_frame,
1294 !list_empty(&dev->sio_full) || !is_present(dev));
1295 if (ret)
1296 return ret;
1297 if (!is_present(dev))
1298 return -EIO;
1300 spin_lock_irqsave(&dev->spinlock, flags);
1301 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
1302 list_del_init(&sbuf->list);
1303 spin_unlock_irqrestore(&dev->spinlock, flags);
1304 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1305 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1306 sbuf->v4lbuf.sequence = ++dev->sequence;
1307 do_gettimeofday(&sbuf->v4lbuf.timestamp);
1309 *buf = sbuf->v4lbuf;
1310 return 0;
1313 static int stk_vidioc_streamon(struct file *filp,
1314 void *priv, enum v4l2_buf_type type)
1316 struct stk_camera *dev = priv;
1317 if (is_streaming(dev))
1318 return 0;
1319 if (dev->sio_bufs == NULL)
1320 return -EINVAL;
1321 dev->sequence = 0;
1322 return stk_start_stream(dev);
1325 static int stk_vidioc_streamoff(struct file *filp,
1326 void *priv, enum v4l2_buf_type type)
1328 struct stk_camera *dev = priv;
1329 unsigned long flags;
1330 int i;
1331 stk_stop_stream(dev);
1332 spin_lock_irqsave(&dev->spinlock, flags);
1333 INIT_LIST_HEAD(&dev->sio_avail);
1334 INIT_LIST_HEAD(&dev->sio_full);
1335 for (i = 0; i < dev->n_sbufs; i++) {
1336 INIT_LIST_HEAD(&dev->sio_bufs[i].list);
1337 dev->sio_bufs[i].v4lbuf.flags = 0;
1339 spin_unlock_irqrestore(&dev->spinlock, flags);
1340 return 0;
1344 static int stk_vidioc_g_parm(struct file *filp,
1345 void *priv, struct v4l2_streamparm *sp)
1347 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1348 return -EINVAL;
1350 sp->parm.capture.capability = 0;
1351 sp->parm.capture.capturemode = 0;
1352 /*FIXME This is not correct */
1353 sp->parm.capture.timeperframe.numerator = 1;
1354 sp->parm.capture.timeperframe.denominator = 30;
1355 sp->parm.capture.readbuffers = 2;
1356 sp->parm.capture.extendedmode = 0;
1357 return 0;
1360 static struct file_operations v4l_stk_fops = {
1361 .owner = THIS_MODULE,
1362 .open = v4l_stk_open,
1363 .release = v4l_stk_release,
1364 .read = v4l_stk_read,
1365 .poll = v4l_stk_poll,
1366 .mmap = v4l_stk_mmap,
1367 .ioctl = video_ioctl2,
1368 <<<<<<< HEAD:drivers/media/video/stk-webcam.c
1369 =======
1370 #ifdef CONFIG_COMPAT
1371 .compat_ioctl = v4l_compat_ioctl32,
1372 #endif
1373 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/stk-webcam.c
1374 .llseek = no_llseek
1377 static void stk_v4l_dev_release(struct video_device *vd)
1381 static struct video_device stk_v4l_data = {
1382 .name = "stkwebcam",
1383 .type = VFL_TYPE_GRABBER,
1384 .type2 = VID_TYPE_CAPTURE,
1385 .minor = -1,
1386 .tvnorms = V4L2_STD_UNKNOWN,
1387 .current_norm = V4L2_STD_UNKNOWN,
1388 .fops = &v4l_stk_fops,
1389 .release = stk_v4l_dev_release,
1391 .vidioc_querycap = stk_vidioc_querycap,
1392 .vidioc_enum_fmt_cap = stk_vidioc_enum_fmt_cap,
1393 .vidioc_try_fmt_cap = stk_vidioc_try_fmt_cap,
1394 .vidioc_s_fmt_cap = stk_vidioc_s_fmt_cap,
1395 .vidioc_g_fmt_cap = stk_vidioc_g_fmt_cap,
1396 .vidioc_enum_input = stk_vidioc_enum_input,
1397 .vidioc_s_input = stk_vidioc_s_input,
1398 .vidioc_g_input = stk_vidioc_g_input,
1399 .vidioc_s_std = stk_vidioc_s_std,
1400 .vidioc_reqbufs = stk_vidioc_reqbufs,
1401 .vidioc_querybuf = stk_vidioc_querybuf,
1402 .vidioc_qbuf = stk_vidioc_qbuf,
1403 .vidioc_dqbuf = stk_vidioc_dqbuf,
1404 .vidioc_streamon = stk_vidioc_streamon,
1405 .vidioc_streamoff = stk_vidioc_streamoff,
1406 .vidioc_queryctrl = stk_vidioc_queryctrl,
1407 .vidioc_g_ctrl = stk_vidioc_g_ctrl,
1408 .vidioc_s_ctrl = stk_vidioc_s_ctrl,
1409 .vidioc_g_parm = stk_vidioc_g_parm,
1413 static int stk_register_video_device(struct stk_camera *dev)
1415 int err;
1417 dev->vdev = stk_v4l_data;
1418 dev->vdev.debug = debug;
1419 dev->vdev.dev = &dev->interface->dev;
1420 dev->vdev.priv = dev;
1421 err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1422 if (err)
1423 STK_ERROR("v4l registration failed\n");
1424 else
1425 STK_INFO("Syntek USB2.0 Camera is now controlling video device"
1426 " /dev/video%d\n", dev->vdev.minor);
1427 return err;
1431 /* USB Stuff */
1433 static int stk_camera_probe(struct usb_interface *interface,
1434 const struct usb_device_id *id)
1436 int i;
1437 int err;
1439 struct stk_camera *dev = NULL;
1440 struct usb_device *udev = interface_to_usbdev(interface);
1441 struct usb_host_interface *iface_desc;
1442 struct usb_endpoint_descriptor *endpoint;
1444 dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
1445 if (dev == NULL) {
1446 STK_ERROR("Out of memory !\n");
1447 return -ENOMEM;
1450 kref_init(&dev->kref);
1451 spin_lock_init(&dev->spinlock);
1452 init_waitqueue_head(&dev->wait_frame);
1454 dev->udev = udev;
1455 dev->interface = interface;
1456 usb_get_intf(interface);
1458 dev->vsettings.vflip = vflip;
1459 dev->vsettings.hflip = hflip;
1460 dev->n_sbufs = 0;
1461 set_present(dev);
1463 /* Set up the endpoint information
1464 * use only the first isoc-in endpoint
1465 * for the current alternate setting */
1466 iface_desc = interface->cur_altsetting;
1468 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1469 endpoint = &iface_desc->endpoint[i].desc;
1471 if (!dev->isoc_ep
1472 && ((endpoint->bEndpointAddress
1473 & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
1474 && ((endpoint->bmAttributes
1475 & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_ISOC)) {
1476 /* we found an isoc in endpoint */
1477 dev->isoc_ep = (endpoint->bEndpointAddress & 0xF);
1478 break;
1481 if (!dev->isoc_ep) {
1482 STK_ERROR("Could not find isoc-in endpoint");
1483 kref_put(&dev->kref, stk_camera_cleanup);
1484 return -ENODEV;
1486 dev->vsettings.brightness = 0x7fff;
1487 dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
1488 dev->vsettings.mode = MODE_VGA;
1489 <<<<<<< HEAD:drivers/media/video/stk-webcam.c
1490 dev->frame_size = 640*480*2;
1491 =======
1492 dev->frame_size = 640 * 480 * 2;
1493 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/stk-webcam.c
1495 INIT_LIST_HEAD(&dev->sio_avail);
1496 INIT_LIST_HEAD(&dev->sio_full);
1498 usb_set_intfdata(interface, dev);
1500 err = stk_register_video_device(dev);
1501 if (err) {
1502 kref_put(&dev->kref, stk_camera_cleanup);
1503 return err;
1506 stk_create_sysfs_files(&dev->vdev);
1507 <<<<<<< HEAD:drivers/media/video/stk-webcam.c
1508 =======
1509 usb_autopm_enable(dev->interface);
1510 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/stk-webcam.c
1512 return 0;
1515 static void stk_camera_disconnect(struct usb_interface *interface)
1517 struct stk_camera *dev = usb_get_intfdata(interface);
1519 usb_set_intfdata(interface, NULL);
1520 unset_present(dev);
1522 wake_up_interruptible(&dev->wait_frame);
1523 stk_remove_sysfs_files(&dev->vdev);
1525 kref_put(&dev->kref, stk_camera_cleanup);
1528 <<<<<<< HEAD:drivers/media/video/stk-webcam.c
1529 =======
1530 #ifdef CONFIG_PM
1531 int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
1533 struct stk_camera *dev = usb_get_intfdata(intf);
1534 if (is_streaming(dev)) {
1535 stk_stop_stream(dev);
1536 /* yes, this is ugly */
1537 set_streaming(dev);
1539 return 0;
1542 int stk_camera_resume(struct usb_interface *intf)
1544 struct stk_camera *dev = usb_get_intfdata(intf);
1545 if (!is_initialised(dev))
1546 return 0;
1547 unset_initialised(dev);
1548 stk_initialise(dev);
1549 stk_setup_format(dev);
1550 if (is_streaming(dev))
1551 stk_start_stream(dev);
1552 return 0;
1554 #endif
1556 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/stk-webcam.c
1557 static struct usb_driver stk_camera_driver = {
1558 .name = "stkwebcam",
1559 .probe = stk_camera_probe,
1560 .disconnect = stk_camera_disconnect,
1561 .id_table = stkwebcam_table,
1562 <<<<<<< HEAD:drivers/media/video/stk-webcam.c
1563 =======
1564 #ifdef CONFIG_PM
1565 .suspend = stk_camera_suspend,
1566 .resume = stk_camera_resume,
1567 #endif
1568 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/stk-webcam.c
1572 static int __init stk_camera_init(void)
1574 int result;
1576 result = usb_register(&stk_camera_driver);
1577 if (result)
1578 STK_ERROR("usb_register failed ! Error number %d\n", result);
1581 return result;
1584 static void __exit stk_camera_exit(void)
1586 usb_deregister(&stk_camera_driver);
1589 module_init(stk_camera_init);
1590 module_exit(stk_camera_exit);