GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / media / video / stk-webcam.c
blobea64ecabef5e1a6247235dba8d055b050e3a0a5e
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/smp_lock.h>
32 #include <linux/usb.h>
33 #include <linux/mm.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"
42 static int hflip = 1;
43 module_param(hflip, bool, 0444);
44 MODULE_PARM_DESC(hflip, "Horizontal image flip (mirror). Defaults to 1");
46 static int vflip = 1;
47 module_param(vflip, bool, 0444);
48 MODULE_PARM_DESC(vflip, "Vertical image flip. Defaults to 1");
50 static int debug;
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) },
64 { }
66 MODULE_DEVICE_TABLE(usb, stkwebcam_table);
69 * Basic stuff
71 int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
73 struct usb_device *udev = dev->udev;
74 int ret;
76 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
77 0x01,
78 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
79 value,
80 index,
81 NULL,
83 500);
84 if (ret < 0)
85 return ret;
86 else
87 return 0;
90 int stk_camera_read_reg(struct stk_camera *dev, u16 index, int *value)
92 struct usb_device *udev = dev->udev;
93 int ret;
95 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
96 0x00,
97 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
98 0x00,
99 index,
100 (u8 *) value,
101 sizeof(u8),
102 500);
103 if (ret < 0)
104 return ret;
105 else
106 return 0;
109 static int stk_start_stream(struct stk_camera *dev)
111 int value;
112 int i, ret;
113 int value_116, value_117;
115 if (!is_present(dev))
116 return -ENODEV;
117 if (!is_memallocd(dev) || !is_initialised(dev)) {
118 STK_ERROR("FIXME: Buffers are not allocated\n");
119 return -EFAULT;
121 ret = usb_set_interface(dev->udev, 0, 5);
123 if (ret < 0)
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);
143 if (ret)
144 return ret;
147 set_streaming(dev);
148 return 0;
151 static int stk_stop_stream(struct stk_camera *dev)
153 int value;
154 int i;
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");
171 return 0;
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? */
184 {0x0000, 0x24},
185 {0x0100, 0x21},
186 {0x0002, 0x68},
187 {0x0003, 0x80},
188 {0x0005, 0x00},
189 {0x0007, 0x03},
190 {0x000d, 0x00},
191 {0x000f, 0x02},
192 {0x0300, 0x12},
193 {0x0350, 0x41},
194 {0x0351, 0x00},
195 {0x0352, 0x00},
196 {0x0353, 0x00},
197 {0x0018, 0x10},
198 {0x0019, 0x00},
199 {0x001b, 0x0e},
200 {0x001c, 0x46},
201 {0x0300, 0x80},
202 {0x001a, 0x04},
203 {0x0110, 0x00},
204 {0x0111, 0x00},
205 {0x0112, 0x00},
206 {0x0113, 0x00},
208 {0xffff, 0xff},
212 static int stk_initialise(struct stk_camera *dev)
214 struct regval *rv;
215 int ret;
216 if (!is_present(dev))
217 return -ENODEV;
218 if (is_initialised(dev))
219 return 0;
220 rv = stk1125_initvals;
221 while (rv->reg != 0xffff) {
222 ret = stk_camera_write_reg(dev, rv->reg, rv->val);
223 if (ret)
224 return ret;
225 rv++;
227 if (stk_sensor_init(dev) == 0) {
228 set_initialised(dev);
229 return 0;
230 } else
231 return -1;
234 #ifdef CONFIG_VIDEO_V4L1_COMPAT
236 /* sysfs functions */
238 static ssize_t show_brightness(struct device *class,
239 struct device_attribute *attr, char *buf)
241 struct video_device *vdev = to_video_device(class);
242 struct stk_camera *dev = vdev_to_camera(vdev);
244 return sprintf(buf, "%X\n", dev->vsettings.brightness);
247 static ssize_t store_brightness(struct device *class,
248 struct device_attribute *attr, const char *buf, size_t count)
250 char *endp;
251 unsigned long value;
252 int ret;
254 struct video_device *vdev = to_video_device(class);
255 struct stk_camera *dev = vdev_to_camera(vdev);
257 value = simple_strtoul(buf, &endp, 16);
259 dev->vsettings.brightness = (int) value;
261 ret = stk_sensor_set_brightness(dev, value >> 8);
262 if (ret)
263 return ret;
264 else
265 return count;
268 static ssize_t show_hflip(struct device *class,
269 struct device_attribute *attr, char *buf)
271 struct video_device *vdev = to_video_device(class);
272 struct stk_camera *dev = vdev_to_camera(vdev);
274 return sprintf(buf, "%d\n", dev->vsettings.hflip);
277 static ssize_t store_hflip(struct device *class,
278 struct device_attribute *attr, const char *buf, size_t count)
280 struct video_device *vdev = to_video_device(class);
281 struct stk_camera *dev = vdev_to_camera(vdev);
283 if (strncmp(buf, "1", 1) == 0)
284 dev->vsettings.hflip = 1;
285 else if (strncmp(buf, "0", 1) == 0)
286 dev->vsettings.hflip = 0;
287 else
288 return -EINVAL;
290 return strlen(buf);
293 static ssize_t show_vflip(struct device *class,
294 struct device_attribute *attr, char *buf)
296 struct video_device *vdev = to_video_device(class);
297 struct stk_camera *dev = vdev_to_camera(vdev);
299 return sprintf(buf, "%d\n", dev->vsettings.vflip);
302 static ssize_t store_vflip(struct device *class,
303 struct device_attribute *attr, const char *buf, size_t count)
305 struct video_device *vdev = to_video_device(class);
306 struct stk_camera *dev = vdev_to_camera(vdev);
308 if (strncmp(buf, "1", 1) == 0)
309 dev->vsettings.vflip = 1;
310 else if (strncmp(buf, "0", 1) == 0)
311 dev->vsettings.vflip = 0;
312 else
313 return -EINVAL;
315 return strlen(buf);
318 static DEVICE_ATTR(brightness, S_IRUGO | S_IWUGO,
319 show_brightness, store_brightness);
320 static DEVICE_ATTR(hflip, S_IRUGO | S_IWUGO, show_hflip, store_hflip);
321 static DEVICE_ATTR(vflip, S_IRUGO | S_IWUGO, show_vflip, store_vflip);
323 static int stk_create_sysfs_files(struct video_device *vdev)
325 int ret;
327 ret = device_create_file(&vdev->dev, &dev_attr_brightness);
328 ret += device_create_file(&vdev->dev, &dev_attr_hflip);
329 ret += device_create_file(&vdev->dev, &dev_attr_vflip);
330 if (ret)
331 STK_WARNING("Could not create sysfs files\n");
332 return ret;
335 static void stk_remove_sysfs_files(struct video_device *vdev)
337 device_remove_file(&vdev->dev, &dev_attr_brightness);
338 device_remove_file(&vdev->dev, &dev_attr_hflip);
339 device_remove_file(&vdev->dev, &dev_attr_vflip);
342 #else
343 #define stk_create_sysfs_files(a)
344 #define stk_remove_sysfs_files(a)
345 #endif
347 /* *********************************************** */
349 * This function is called as an URB transfert is complete (Isochronous pipe).
350 * So, the traitement is done in interrupt time, so it has be fast, not crash,
351 * and not stall. Neat.
353 static void stk_isoc_handler(struct urb *urb)
355 int i;
356 int ret;
357 int framelen;
358 unsigned long flags;
360 unsigned char *fill = NULL;
361 unsigned char *iso_buf = NULL;
363 struct stk_camera *dev;
364 struct stk_sio_buffer *fb;
366 dev = (struct stk_camera *) urb->context;
368 if (dev == NULL) {
369 STK_ERROR("isoc_handler called with NULL device !\n");
370 return;
373 if (urb->status == -ENOENT || urb->status == -ECONNRESET
374 || urb->status == -ESHUTDOWN) {
375 atomic_dec(&dev->urbs_used);
376 return;
379 spin_lock_irqsave(&dev->spinlock, flags);
381 if (urb->status != -EINPROGRESS && urb->status != 0) {
382 STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
383 goto resubmit;
386 if (list_empty(&dev->sio_avail)) {
387 (void) (printk_ratelimit() &&
388 STK_ERROR("isoc_handler without available buffer!\n"));
389 goto resubmit;
391 fb = list_first_entry(&dev->sio_avail,
392 struct stk_sio_buffer, list);
393 fill = fb->buffer + fb->v4lbuf.bytesused;
395 for (i = 0; i < urb->number_of_packets; i++) {
396 if (urb->iso_frame_desc[i].status != 0) {
397 if (urb->iso_frame_desc[i].status != -EXDEV)
398 STK_ERROR("Frame %d has error %d\n", i,
399 urb->iso_frame_desc[i].status);
400 continue;
402 framelen = urb->iso_frame_desc[i].actual_length;
403 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
405 if (framelen <= 4)
406 continue; /* no data */
409 * we found something informational from there
410 * the isoc frames have to type of headers
411 * type1: 00 xx 00 00 or 20 xx 00 00
412 * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
413 * xx is a sequencer which has never been seen over 0x3f
414 * imho data written down looks like bayer, i see similarities
415 * after every 640 bytes
417 if (*iso_buf & 0x80) {
418 framelen -= 8;
419 iso_buf += 8;
420 /* This marks a new frame */
421 if (fb->v4lbuf.bytesused != 0
422 && fb->v4lbuf.bytesused != dev->frame_size) {
423 (void) (printk_ratelimit() &&
424 STK_ERROR("frame %d, "
425 "bytesused=%d, skipping\n",
426 i, fb->v4lbuf.bytesused));
427 fb->v4lbuf.bytesused = 0;
428 fill = fb->buffer;
429 } else if (fb->v4lbuf.bytesused == dev->frame_size) {
430 if (list_is_singular(&dev->sio_avail)) {
431 /* Always reuse the last buffer */
432 fb->v4lbuf.bytesused = 0;
433 fill = fb->buffer;
434 } else {
435 list_move_tail(dev->sio_avail.next,
436 &dev->sio_full);
437 wake_up(&dev->wait_frame);
438 fb = list_first_entry(&dev->sio_avail,
439 struct stk_sio_buffer, list);
440 fb->v4lbuf.bytesused = 0;
441 fill = fb->buffer;
444 } else {
445 framelen -= 4;
446 iso_buf += 4;
449 /* Our buffer is full !!! */
450 if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
451 (void) (printk_ratelimit() &&
452 STK_ERROR("Frame buffer overflow, lost sync\n"));
453 continue;
455 spin_unlock_irqrestore(&dev->spinlock, flags);
456 memcpy(fill, iso_buf, framelen);
457 spin_lock_irqsave(&dev->spinlock, flags);
458 fill += framelen;
460 /* New size of our buffer */
461 fb->v4lbuf.bytesused += framelen;
464 resubmit:
465 spin_unlock_irqrestore(&dev->spinlock, flags);
466 urb->dev = dev->udev;
467 ret = usb_submit_urb(urb, GFP_ATOMIC);
468 if (ret != 0) {
469 STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
470 ret);
474 /* -------------------------------------------- */
476 static int stk_prepare_iso(struct stk_camera *dev)
478 void *kbuf;
479 int i, j;
480 struct urb *urb;
481 struct usb_device *udev;
483 if (dev == NULL)
484 return -ENXIO;
485 udev = dev->udev;
487 if (dev->isobufs)
488 STK_ERROR("isobufs already allocated. Bad\n");
489 else
490 dev->isobufs = kzalloc(MAX_ISO_BUFS * sizeof(*dev->isobufs),
491 GFP_KERNEL);
492 if (dev->isobufs == NULL) {
493 STK_ERROR("Unable to allocate iso buffers\n");
494 return -ENOMEM;
496 for (i = 0; i < MAX_ISO_BUFS; i++) {
497 if (dev->isobufs[i].data == NULL) {
498 kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
499 if (kbuf == NULL) {
500 STK_ERROR("Failed to allocate iso buffer %d\n",
502 goto isobufs_out;
504 dev->isobufs[i].data = kbuf;
505 } else
506 STK_ERROR("isobuf data already allocated\n");
507 if (dev->isobufs[i].urb == NULL) {
508 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
509 if (urb == NULL) {
510 STK_ERROR("Failed to allocate URB %d\n", i);
511 goto isobufs_out;
513 dev->isobufs[i].urb = urb;
514 } else {
515 STK_ERROR("Killing URB\n");
516 usb_kill_urb(dev->isobufs[i].urb);
517 urb = dev->isobufs[i].urb;
519 urb->interval = 1;
520 urb->dev = udev;
521 urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
522 urb->transfer_flags = URB_ISO_ASAP;
523 urb->transfer_buffer = dev->isobufs[i].data;
524 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
525 urb->complete = stk_isoc_handler;
526 urb->context = dev;
527 urb->start_frame = 0;
528 urb->number_of_packets = ISO_FRAMES_PER_DESC;
530 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
531 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
532 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
535 set_memallocd(dev);
536 return 0;
538 isobufs_out:
539 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
540 kfree(dev->isobufs[i].data);
541 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
542 usb_free_urb(dev->isobufs[i].urb);
543 kfree(dev->isobufs);
544 dev->isobufs = NULL;
545 return -ENOMEM;
548 static void stk_clean_iso(struct stk_camera *dev)
550 int i;
552 if (dev == NULL || dev->isobufs == NULL)
553 return;
555 for (i = 0; i < MAX_ISO_BUFS; i++) {
556 struct urb *urb;
558 urb = dev->isobufs[i].urb;
559 if (urb) {
560 if (atomic_read(&dev->urbs_used) && is_present(dev))
561 usb_kill_urb(urb);
562 usb_free_urb(urb);
564 kfree(dev->isobufs[i].data);
566 kfree(dev->isobufs);
567 dev->isobufs = NULL;
568 unset_memallocd(dev);
571 static int stk_setup_siobuf(struct stk_camera *dev, int index)
573 struct stk_sio_buffer *buf = dev->sio_bufs + index;
574 INIT_LIST_HEAD(&buf->list);
575 buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
576 buf->buffer = vmalloc_user(buf->v4lbuf.length);
577 if (buf->buffer == NULL)
578 return -ENOMEM;
579 buf->mapcount = 0;
580 buf->dev = dev;
581 buf->v4lbuf.index = index;
582 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
583 buf->v4lbuf.field = V4L2_FIELD_NONE;
584 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
585 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
586 return 0;
589 static int stk_free_sio_buffers(struct stk_camera *dev)
591 int i;
592 int nbufs;
593 unsigned long flags;
594 if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
595 return 0;
597 * If any buffers are mapped, we cannot free them at all.
599 for (i = 0; i < dev->n_sbufs; i++) {
600 if (dev->sio_bufs[i].mapcount > 0)
601 return -EBUSY;
604 * OK, let's do it.
606 spin_lock_irqsave(&dev->spinlock, flags);
607 INIT_LIST_HEAD(&dev->sio_avail);
608 INIT_LIST_HEAD(&dev->sio_full);
609 nbufs = dev->n_sbufs;
610 dev->n_sbufs = 0;
611 spin_unlock_irqrestore(&dev->spinlock, flags);
612 for (i = 0; i < nbufs; i++) {
613 if (dev->sio_bufs[i].buffer != NULL)
614 vfree(dev->sio_bufs[i].buffer);
616 kfree(dev->sio_bufs);
617 dev->sio_bufs = NULL;
618 return 0;
621 static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
623 int i;
624 if (dev->sio_bufs != NULL)
625 STK_ERROR("sio_bufs already allocated\n");
626 else {
627 dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
628 GFP_KERNEL);
629 if (dev->sio_bufs == NULL)
630 return -ENOMEM;
631 for (i = 0; i < n_sbufs; i++) {
632 if (stk_setup_siobuf(dev, i))
633 return (dev->n_sbufs > 1)? 0 : -ENOMEM;
634 dev->n_sbufs = i+1;
637 return 0;
640 static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
642 int err;
643 err = stk_prepare_iso(dev);
644 if (err) {
645 stk_clean_iso(dev);
646 return err;
648 err = stk_prepare_sio_buffers(dev, n_sbufs);
649 if (err) {
650 stk_free_sio_buffers(dev);
651 return err;
653 return 0;
656 static void stk_free_buffers(struct stk_camera *dev)
658 stk_clean_iso(dev);
659 stk_free_sio_buffers(dev);
661 /* -------------------------------------------- */
663 /* v4l file operations */
665 static int v4l_stk_open(struct file *fp)
667 struct stk_camera *dev;
668 struct video_device *vdev;
670 vdev = video_devdata(fp);
671 dev = vdev_to_camera(vdev);
673 lock_kernel();
674 if (dev == NULL || !is_present(dev)) {
675 unlock_kernel();
676 return -ENXIO;
678 fp->private_data = dev;
679 usb_autopm_get_interface(dev->interface);
680 unlock_kernel();
682 return 0;
685 static int v4l_stk_release(struct file *fp)
687 struct stk_camera *dev = fp->private_data;
689 if (dev->owner == fp) {
690 stk_stop_stream(dev);
691 stk_free_buffers(dev);
692 dev->owner = NULL;
695 if(is_present(dev))
696 usb_autopm_put_interface(dev->interface);
698 return 0;
701 static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
702 size_t count, loff_t *f_pos)
704 int i;
705 int ret;
706 unsigned long flags;
707 struct stk_sio_buffer *sbuf;
708 struct stk_camera *dev = fp->private_data;
710 if (!is_present(dev))
711 return -EIO;
712 if (dev->owner && dev->owner != fp)
713 return -EBUSY;
714 dev->owner = fp;
715 if (!is_streaming(dev)) {
716 if (stk_initialise(dev)
717 || stk_allocate_buffers(dev, 3)
718 || stk_start_stream(dev))
719 return -ENOMEM;
720 spin_lock_irqsave(&dev->spinlock, flags);
721 for (i = 0; i < dev->n_sbufs; i++) {
722 list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
723 dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
725 spin_unlock_irqrestore(&dev->spinlock, flags);
727 if (*f_pos == 0) {
728 if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
729 return -EWOULDBLOCK;
730 ret = wait_event_interruptible(dev->wait_frame,
731 !list_empty(&dev->sio_full) || !is_present(dev));
732 if (ret)
733 return ret;
734 if (!is_present(dev))
735 return -EIO;
737 if (count + *f_pos > dev->frame_size)
738 count = dev->frame_size - *f_pos;
739 spin_lock_irqsave(&dev->spinlock, flags);
740 if (list_empty(&dev->sio_full)) {
741 spin_unlock_irqrestore(&dev->spinlock, flags);
742 STK_ERROR("BUG: No siobufs ready\n");
743 return 0;
745 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
746 spin_unlock_irqrestore(&dev->spinlock, flags);
748 if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
749 return -EFAULT;
751 *f_pos += count;
753 if (*f_pos >= dev->frame_size) {
754 *f_pos = 0;
755 spin_lock_irqsave(&dev->spinlock, flags);
756 list_move_tail(&sbuf->list, &dev->sio_avail);
757 spin_unlock_irqrestore(&dev->spinlock, flags);
759 return count;
762 static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
764 struct stk_camera *dev = fp->private_data;
766 poll_wait(fp, &dev->wait_frame, wait);
768 if (!is_present(dev))
769 return POLLERR;
771 if (!list_empty(&dev->sio_full))
772 return (POLLIN | POLLRDNORM);
774 return 0;
778 static void stk_v4l_vm_open(struct vm_area_struct *vma)
780 struct stk_sio_buffer *sbuf = vma->vm_private_data;
781 sbuf->mapcount++;
783 static void stk_v4l_vm_close(struct vm_area_struct *vma)
785 struct stk_sio_buffer *sbuf = vma->vm_private_data;
786 sbuf->mapcount--;
787 if (sbuf->mapcount == 0)
788 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
790 static const struct vm_operations_struct stk_v4l_vm_ops = {
791 .open = stk_v4l_vm_open,
792 .close = stk_v4l_vm_close
795 static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
797 unsigned int i;
798 int ret;
799 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
800 struct stk_camera *dev = fp->private_data;
801 struct stk_sio_buffer *sbuf = NULL;
803 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
804 return -EINVAL;
806 for (i = 0; i < dev->n_sbufs; i++) {
807 if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
808 sbuf = dev->sio_bufs + i;
809 break;
812 if (sbuf == NULL)
813 return -EINVAL;
814 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
815 if (ret)
816 return ret;
817 vma->vm_flags |= VM_DONTEXPAND;
818 vma->vm_private_data = sbuf;
819 vma->vm_ops = &stk_v4l_vm_ops;
820 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
821 stk_v4l_vm_open(vma);
822 return 0;
825 /* v4l ioctl handlers */
827 static int stk_vidioc_querycap(struct file *filp,
828 void *priv, struct v4l2_capability *cap)
830 strcpy(cap->driver, "stk");
831 strcpy(cap->card, "stk");
832 cap->version = DRIVER_VERSION_NUM;
834 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
835 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
836 return 0;
839 static int stk_vidioc_enum_input(struct file *filp,
840 void *priv, struct v4l2_input *input)
842 if (input->index != 0)
843 return -EINVAL;
845 strcpy(input->name, "Syntek USB Camera");
846 input->type = V4L2_INPUT_TYPE_CAMERA;
847 return 0;
851 static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
853 *i = 0;
854 return 0;
857 static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
859 if (i != 0)
860 return -EINVAL;
861 else
862 return 0;
865 /* from vivi.c */
866 static int stk_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
868 return 0;
871 /* List of all V4Lv2 controls supported by the driver */
872 static struct v4l2_queryctrl stk_controls[] = {
874 .id = V4L2_CID_BRIGHTNESS,
875 .type = V4L2_CTRL_TYPE_INTEGER,
876 .name = "Brightness",
877 .minimum = 0,
878 .maximum = 0xffff,
879 .step = 0x0100,
880 .default_value = 0x6000,
882 /*TODO: get more controls to work */
885 static int stk_vidioc_queryctrl(struct file *filp,
886 void *priv, struct v4l2_queryctrl *c)
888 int i;
889 int nbr;
890 nbr = ARRAY_SIZE(stk_controls);
892 for (i = 0; i < nbr; i++) {
893 if (stk_controls[i].id == c->id) {
894 memcpy(c, &stk_controls[i],
895 sizeof(struct v4l2_queryctrl));
896 return 0;
899 return -EINVAL;
902 static int stk_vidioc_g_ctrl(struct file *filp,
903 void *priv, struct v4l2_control *c)
905 struct stk_camera *dev = priv;
906 switch (c->id) {
907 case V4L2_CID_BRIGHTNESS:
908 c->value = dev->vsettings.brightness;
909 break;
910 default:
911 return -EINVAL;
913 return 0;
916 static int stk_vidioc_s_ctrl(struct file *filp,
917 void *priv, struct v4l2_control *c)
919 struct stk_camera *dev = priv;
920 switch (c->id) {
921 case V4L2_CID_BRIGHTNESS:
922 dev->vsettings.brightness = c->value;
923 return stk_sensor_set_brightness(dev, c->value >> 8);
924 default:
925 return -EINVAL;
927 return 0;
931 static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
932 void *priv, struct v4l2_fmtdesc *fmtd)
934 switch (fmtd->index) {
935 case 0:
936 fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
937 strcpy(fmtd->description, "r5g6b5");
938 break;
939 case 1:
940 fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
941 strcpy(fmtd->description, "r5g6b5BE");
942 break;
943 case 2:
944 fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
945 strcpy(fmtd->description, "yuv4:2:2");
946 break;
947 case 3:
948 fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
949 strcpy(fmtd->description, "Raw bayer");
950 break;
951 case 4:
952 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
953 strcpy(fmtd->description, "yuv4:2:2");
954 break;
955 default:
956 return -EINVAL;
958 return 0;
961 static struct stk_size {
962 unsigned w;
963 unsigned h;
964 enum stk_mode m;
965 } stk_sizes[] = {
966 { .w = 1280, .h = 1024, .m = MODE_SXGA, },
967 { .w = 640, .h = 480, .m = MODE_VGA, },
968 { .w = 352, .h = 288, .m = MODE_CIF, },
969 { .w = 320, .h = 240, .m = MODE_QVGA, },
970 { .w = 176, .h = 144, .m = MODE_QCIF, },
973 static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
974 void *priv, struct v4l2_format *f)
976 struct v4l2_pix_format *pix_format = &f->fmt.pix;
977 struct stk_camera *dev = priv;
978 int i;
980 for (i = 0; i < ARRAY_SIZE(stk_sizes)
981 && stk_sizes[i].m != dev->vsettings.mode;
982 i++);
983 if (i == ARRAY_SIZE(stk_sizes)) {
984 STK_ERROR("ERROR: mode invalid\n");
985 return -EINVAL;
987 pix_format->width = stk_sizes[i].w;
988 pix_format->height = stk_sizes[i].h;
989 pix_format->field = V4L2_FIELD_NONE;
990 pix_format->colorspace = V4L2_COLORSPACE_SRGB;
991 pix_format->pixelformat = dev->vsettings.palette;
992 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
993 pix_format->bytesperline = pix_format->width;
994 else
995 pix_format->bytesperline = 2 * pix_format->width;
996 pix_format->sizeimage = pix_format->bytesperline
997 * pix_format->height;
998 return 0;
1001 static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
1002 void *priv, struct v4l2_format *fmtd)
1004 int i;
1005 switch (fmtd->fmt.pix.pixelformat) {
1006 case V4L2_PIX_FMT_RGB565:
1007 case V4L2_PIX_FMT_RGB565X:
1008 case V4L2_PIX_FMT_UYVY:
1009 case V4L2_PIX_FMT_YUYV:
1010 case V4L2_PIX_FMT_SBGGR8:
1011 break;
1012 default:
1013 return -EINVAL;
1015 for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
1016 if (fmtd->fmt.pix.width > stk_sizes[i].w)
1017 break;
1019 if (i == ARRAY_SIZE(stk_sizes)
1020 || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
1021 < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
1022 fmtd->fmt.pix.height = stk_sizes[i-1].h;
1023 fmtd->fmt.pix.width = stk_sizes[i-1].w;
1024 fmtd->fmt.pix.priv = i - 1;
1025 } else {
1026 fmtd->fmt.pix.height = stk_sizes[i].h;
1027 fmtd->fmt.pix.width = stk_sizes[i].w;
1028 fmtd->fmt.pix.priv = i;
1031 fmtd->fmt.pix.field = V4L2_FIELD_NONE;
1032 fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
1033 if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
1034 fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
1035 else
1036 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
1037 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
1038 * fmtd->fmt.pix.height;
1039 return 0;
1042 static int stk_setup_format(struct stk_camera *dev)
1044 int i = 0;
1045 int depth;
1046 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
1047 depth = 1;
1048 else
1049 depth = 2;
1050 while (i < ARRAY_SIZE(stk_sizes) &&
1051 stk_sizes[i].m != dev->vsettings.mode)
1052 i++;
1053 if (i == ARRAY_SIZE(stk_sizes)) {
1054 STK_ERROR("Something is broken in %s\n", __func__);
1055 return -EFAULT;
1057 /* This registers controls some timings, not sure of what. */
1058 stk_camera_write_reg(dev, 0x001b, 0x0e);
1059 if (dev->vsettings.mode == MODE_SXGA)
1060 stk_camera_write_reg(dev, 0x001c, 0x0e);
1061 else
1062 stk_camera_write_reg(dev, 0x001c, 0x46);
1064 * Registers 0x0115 0x0114 are the size of each line (bytes),
1065 * regs 0x0117 0x0116 are the heigth of the image.
1067 stk_camera_write_reg(dev, 0x0115,
1068 ((stk_sizes[i].w * depth) >> 8) & 0xff);
1069 stk_camera_write_reg(dev, 0x0114,
1070 (stk_sizes[i].w * depth) & 0xff);
1071 stk_camera_write_reg(dev, 0x0117,
1072 (stk_sizes[i].h >> 8) & 0xff);
1073 stk_camera_write_reg(dev, 0x0116,
1074 stk_sizes[i].h & 0xff);
1075 return stk_sensor_configure(dev);
1078 static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
1079 void *priv, struct v4l2_format *fmtd)
1081 int ret;
1082 struct stk_camera *dev = priv;
1084 if (dev == NULL)
1085 return -ENODEV;
1086 if (!is_present(dev))
1087 return -ENODEV;
1088 if (is_streaming(dev))
1089 return -EBUSY;
1090 if (dev->owner && dev->owner != filp)
1091 return -EBUSY;
1092 ret = stk_vidioc_try_fmt_vid_cap(filp, priv, fmtd);
1093 if (ret)
1094 return ret;
1095 dev->owner = filp;
1097 dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
1098 stk_free_buffers(dev);
1099 dev->frame_size = fmtd->fmt.pix.sizeimage;
1100 dev->vsettings.mode = stk_sizes[fmtd->fmt.pix.priv].m;
1102 stk_initialise(dev);
1103 return stk_setup_format(dev);
1106 static int stk_vidioc_reqbufs(struct file *filp,
1107 void *priv, struct v4l2_requestbuffers *rb)
1109 struct stk_camera *dev = priv;
1111 if (dev == NULL)
1112 return -ENODEV;
1113 if (rb->memory != V4L2_MEMORY_MMAP)
1114 return -EINVAL;
1115 if (is_streaming(dev)
1116 || (dev->owner && dev->owner != filp))
1117 return -EBUSY;
1118 dev->owner = filp;
1120 if (rb->count < 3)
1121 rb->count = 3;
1122 /* Arbitrary limit */
1123 else if (rb->count > 5)
1124 rb->count = 5;
1126 stk_allocate_buffers(dev, rb->count);
1127 rb->count = dev->n_sbufs;
1128 return 0;
1131 static int stk_vidioc_querybuf(struct file *filp,
1132 void *priv, struct v4l2_buffer *buf)
1134 struct stk_camera *dev = priv;
1135 struct stk_sio_buffer *sbuf;
1137 if (buf->index >= dev->n_sbufs)
1138 return -EINVAL;
1139 sbuf = dev->sio_bufs + buf->index;
1140 *buf = sbuf->v4lbuf;
1141 return 0;
1144 static int stk_vidioc_qbuf(struct file *filp,
1145 void *priv, struct v4l2_buffer *buf)
1147 struct stk_camera *dev = priv;
1148 struct stk_sio_buffer *sbuf;
1149 unsigned long flags;
1151 if (buf->memory != V4L2_MEMORY_MMAP)
1152 return -EINVAL;
1154 if (buf->index >= dev->n_sbufs)
1155 return -EINVAL;
1156 sbuf = dev->sio_bufs + buf->index;
1157 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
1158 return 0;
1159 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1160 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1161 spin_lock_irqsave(&dev->spinlock, flags);
1162 list_add_tail(&sbuf->list, &dev->sio_avail);
1163 *buf = sbuf->v4lbuf;
1164 spin_unlock_irqrestore(&dev->spinlock, flags);
1165 return 0;
1168 static int stk_vidioc_dqbuf(struct file *filp,
1169 void *priv, struct v4l2_buffer *buf)
1171 struct stk_camera *dev = priv;
1172 struct stk_sio_buffer *sbuf;
1173 unsigned long flags;
1174 int ret;
1176 if (!is_streaming(dev))
1177 return -EINVAL;
1179 if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
1180 return -EWOULDBLOCK;
1181 ret = wait_event_interruptible(dev->wait_frame,
1182 !list_empty(&dev->sio_full) || !is_present(dev));
1183 if (ret)
1184 return ret;
1185 if (!is_present(dev))
1186 return -EIO;
1188 spin_lock_irqsave(&dev->spinlock, flags);
1189 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
1190 list_del_init(&sbuf->list);
1191 spin_unlock_irqrestore(&dev->spinlock, flags);
1192 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1193 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1194 sbuf->v4lbuf.sequence = ++dev->sequence;
1195 do_gettimeofday(&sbuf->v4lbuf.timestamp);
1197 *buf = sbuf->v4lbuf;
1198 return 0;
1201 static int stk_vidioc_streamon(struct file *filp,
1202 void *priv, enum v4l2_buf_type type)
1204 struct stk_camera *dev = priv;
1205 if (is_streaming(dev))
1206 return 0;
1207 if (dev->sio_bufs == NULL)
1208 return -EINVAL;
1209 dev->sequence = 0;
1210 return stk_start_stream(dev);
1213 static int stk_vidioc_streamoff(struct file *filp,
1214 void *priv, enum v4l2_buf_type type)
1216 struct stk_camera *dev = priv;
1217 unsigned long flags;
1218 int i;
1219 stk_stop_stream(dev);
1220 spin_lock_irqsave(&dev->spinlock, flags);
1221 INIT_LIST_HEAD(&dev->sio_avail);
1222 INIT_LIST_HEAD(&dev->sio_full);
1223 for (i = 0; i < dev->n_sbufs; i++) {
1224 INIT_LIST_HEAD(&dev->sio_bufs[i].list);
1225 dev->sio_bufs[i].v4lbuf.flags = 0;
1227 spin_unlock_irqrestore(&dev->spinlock, flags);
1228 return 0;
1232 static int stk_vidioc_g_parm(struct file *filp,
1233 void *priv, struct v4l2_streamparm *sp)
1235 sp->parm.capture.timeperframe.numerator = 1;
1236 sp->parm.capture.timeperframe.denominator = 30;
1237 sp->parm.capture.readbuffers = 2;
1238 return 0;
1241 static int stk_vidioc_enum_framesizes(struct file *filp,
1242 void *priv, struct v4l2_frmsizeenum *frms)
1244 if (frms->index >= ARRAY_SIZE(stk_sizes))
1245 return -EINVAL;
1246 switch (frms->pixel_format) {
1247 case V4L2_PIX_FMT_RGB565:
1248 case V4L2_PIX_FMT_RGB565X:
1249 case V4L2_PIX_FMT_UYVY:
1250 case V4L2_PIX_FMT_YUYV:
1251 case V4L2_PIX_FMT_SBGGR8:
1252 frms->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1253 frms->discrete.width = stk_sizes[frms->index].w;
1254 frms->discrete.height = stk_sizes[frms->index].h;
1255 return 0;
1256 default: return -EINVAL;
1260 static struct v4l2_file_operations v4l_stk_fops = {
1261 .owner = THIS_MODULE,
1262 .open = v4l_stk_open,
1263 .release = v4l_stk_release,
1264 .read = v4l_stk_read,
1265 .poll = v4l_stk_poll,
1266 .mmap = v4l_stk_mmap,
1267 .ioctl = video_ioctl2,
1270 static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops = {
1271 .vidioc_querycap = stk_vidioc_querycap,
1272 .vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
1273 .vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
1274 .vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
1275 .vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
1276 .vidioc_enum_input = stk_vidioc_enum_input,
1277 .vidioc_s_input = stk_vidioc_s_input,
1278 .vidioc_g_input = stk_vidioc_g_input,
1279 .vidioc_s_std = stk_vidioc_s_std,
1280 .vidioc_reqbufs = stk_vidioc_reqbufs,
1281 .vidioc_querybuf = stk_vidioc_querybuf,
1282 .vidioc_qbuf = stk_vidioc_qbuf,
1283 .vidioc_dqbuf = stk_vidioc_dqbuf,
1284 .vidioc_streamon = stk_vidioc_streamon,
1285 .vidioc_streamoff = stk_vidioc_streamoff,
1286 .vidioc_queryctrl = stk_vidioc_queryctrl,
1287 .vidioc_g_ctrl = stk_vidioc_g_ctrl,
1288 .vidioc_s_ctrl = stk_vidioc_s_ctrl,
1289 .vidioc_g_parm = stk_vidioc_g_parm,
1290 .vidioc_enum_framesizes = stk_vidioc_enum_framesizes,
1293 static void stk_v4l_dev_release(struct video_device *vd)
1295 struct stk_camera *dev = vdev_to_camera(vd);
1297 if (dev->sio_bufs != NULL || dev->isobufs != NULL)
1298 STK_ERROR("We are leaking memory\n");
1299 usb_put_intf(dev->interface);
1300 kfree(dev);
1303 static struct video_device stk_v4l_data = {
1304 .name = "stkwebcam",
1305 .tvnorms = V4L2_STD_UNKNOWN,
1306 .current_norm = V4L2_STD_UNKNOWN,
1307 .fops = &v4l_stk_fops,
1308 .ioctl_ops = &v4l_stk_ioctl_ops,
1309 .release = stk_v4l_dev_release,
1313 static int stk_register_video_device(struct stk_camera *dev)
1315 int err;
1317 dev->vdev = stk_v4l_data;
1318 dev->vdev.debug = debug;
1319 dev->vdev.parent = &dev->interface->dev;
1320 err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1321 if (err)
1322 STK_ERROR("v4l registration failed\n");
1323 else
1324 STK_INFO("Syntek USB2.0 Camera is now controlling device %s\n",
1325 video_device_node_name(&dev->vdev));
1326 return err;
1330 /* USB Stuff */
1332 static int stk_camera_probe(struct usb_interface *interface,
1333 const struct usb_device_id *id)
1335 int i;
1336 int err = 0;
1338 struct stk_camera *dev = NULL;
1339 struct usb_device *udev = interface_to_usbdev(interface);
1340 struct usb_host_interface *iface_desc;
1341 struct usb_endpoint_descriptor *endpoint;
1343 dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
1344 if (dev == NULL) {
1345 STK_ERROR("Out of memory !\n");
1346 return -ENOMEM;
1349 spin_lock_init(&dev->spinlock);
1350 init_waitqueue_head(&dev->wait_frame);
1352 dev->udev = udev;
1353 dev->interface = interface;
1354 usb_get_intf(interface);
1356 dev->vsettings.vflip = vflip;
1357 dev->vsettings.hflip = hflip;
1358 dev->n_sbufs = 0;
1359 set_present(dev);
1361 /* Set up the endpoint information
1362 * use only the first isoc-in endpoint
1363 * for the current alternate setting */
1364 iface_desc = interface->cur_altsetting;
1366 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1367 endpoint = &iface_desc->endpoint[i].desc;
1369 if (!dev->isoc_ep
1370 && usb_endpoint_is_isoc_in(endpoint)) {
1371 /* we found an isoc in endpoint */
1372 dev->isoc_ep = usb_endpoint_num(endpoint);
1373 break;
1376 if (!dev->isoc_ep) {
1377 STK_ERROR("Could not find isoc-in endpoint");
1378 err = -ENODEV;
1379 goto error;
1381 dev->vsettings.brightness = 0x7fff;
1382 dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
1383 dev->vsettings.mode = MODE_VGA;
1384 dev->frame_size = 640 * 480 * 2;
1386 INIT_LIST_HEAD(&dev->sio_avail);
1387 INIT_LIST_HEAD(&dev->sio_full);
1389 usb_set_intfdata(interface, dev);
1391 err = stk_register_video_device(dev);
1392 if (err) {
1393 goto error;
1396 stk_create_sysfs_files(&dev->vdev);
1398 return 0;
1400 error:
1401 kfree(dev);
1402 return err;
1405 static void stk_camera_disconnect(struct usb_interface *interface)
1407 struct stk_camera *dev = usb_get_intfdata(interface);
1409 usb_set_intfdata(interface, NULL);
1410 unset_present(dev);
1412 wake_up_interruptible(&dev->wait_frame);
1413 stk_remove_sysfs_files(&dev->vdev);
1415 STK_INFO("Syntek USB2.0 Camera release resources device %s\n",
1416 video_device_node_name(&dev->vdev));
1418 video_unregister_device(&dev->vdev);
1421 #ifdef CONFIG_PM
1422 static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
1424 struct stk_camera *dev = usb_get_intfdata(intf);
1425 if (is_streaming(dev)) {
1426 stk_stop_stream(dev);
1427 /* yes, this is ugly */
1428 set_streaming(dev);
1430 return 0;
1433 static int stk_camera_resume(struct usb_interface *intf)
1435 struct stk_camera *dev = usb_get_intfdata(intf);
1436 if (!is_initialised(dev))
1437 return 0;
1438 unset_initialised(dev);
1439 stk_initialise(dev);
1440 stk_setup_format(dev);
1441 if (is_streaming(dev))
1442 stk_start_stream(dev);
1443 return 0;
1445 #endif
1447 static struct usb_driver stk_camera_driver = {
1448 .name = "stkwebcam",
1449 .probe = stk_camera_probe,
1450 .disconnect = stk_camera_disconnect,
1451 .id_table = stkwebcam_table,
1452 #ifdef CONFIG_PM
1453 .suspend = stk_camera_suspend,
1454 .resume = stk_camera_resume,
1455 #endif
1459 static int __init stk_camera_init(void)
1461 int result;
1463 result = usb_register(&stk_camera_driver);
1464 if (result)
1465 STK_ERROR("usb_register failed ! Error number %d\n", result);
1468 return result;
1471 static void __exit stk_camera_exit(void)
1473 usb_deregister(&stk_camera_driver);
1476 module_init(stk_camera_init);
1477 module_exit(stk_camera_exit);