V4L/DVB (8885): cpia2_usb: fix memory leak
[linux-2.6] / drivers / media / video / stk-webcam.c
1 /*
2  * stk-webcam.c : Driver for Syntek 1125 USB webcam controller
3  *
4  * Copyright (C) 2006 Nicolas VIVIEN
5  * Copyright 2007-2008 Jaime Velasco Juan <jsagarribay@gmail.com>
6  *
7  * Some parts are inspired from cafe_ccic.c
8  * Copyright 2006-2007 Jonathan Corbet
9  *
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.
14  *
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.
19  *
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
23  */
24
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>
31
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>
38
39 #include "stk-webcam.h"
40
41
42 static int hflip = 1;
43 module_param(hflip, bool, 0444);
44 MODULE_PARM_DESC(hflip, "Horizontal image flip (mirror). Defaults to 1");
45
46 static int vflip = 1;
47 module_param(vflip, bool, 0444);
48 MODULE_PARM_DESC(vflip, "Vertical image flip. Defaults to 1");
49
50 static int debug;
51 module_param(debug, int, 0444);
52 MODULE_PARM_DESC(debug, "Debug v4l ioctls. Defaults to 0");
53
54 MODULE_LICENSE("GPL");
55 MODULE_AUTHOR("Jaime Velasco Juan <jsagarribay@gmail.com> and Nicolas VIVIEN");
56 MODULE_DESCRIPTION("Syntek DC1125 webcam driver");
57
58
59
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         { }
65 };
66 MODULE_DEVICE_TABLE(usb, stkwebcam_table);
67
68 static void stk_camera_cleanup(struct kref *kref)
69 {
70         struct stk_camera *dev = to_stk_camera(kref);
71
72         STK_INFO("Syntek USB2.0 Camera release resources"
73                 " video device /dev/video%d\n", dev->vdev.minor);
74         video_unregister_device(&dev->vdev);
75         dev->vdev.priv = NULL;
76
77         if (dev->sio_bufs != NULL || dev->isobufs != NULL)
78                 STK_ERROR("We are leaking memory\n");
79         usb_put_intf(dev->interface);
80         kfree(dev);
81 }
82
83
84 /*
85  * Basic stuff
86  */
87 int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
88 {
89         struct usb_device *udev = dev->udev;
90         int ret;
91
92         ret =  usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
93                         0x01,
94                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
95                         value,
96                         index,
97                         NULL,
98                         0,
99                         500);
100         if (ret < 0)
101                 return ret;
102         else
103                 return 0;
104 }
105
106 int stk_camera_read_reg(struct stk_camera *dev, u16 index, int *value)
107 {
108         struct usb_device *udev = dev->udev;
109         int ret;
110
111         ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
112                         0x00,
113                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
114                         0x00,
115                         index,
116                         (u8 *) value,
117                         sizeof(u8),
118                         500);
119         if (ret < 0)
120                 return ret;
121         else
122                 return 0;
123 }
124
125 static int stk_start_stream(struct stk_camera *dev)
126 {
127         int value;
128         int i, ret;
129         int value_116, value_117;
130
131         if (!is_present(dev))
132                 return -ENODEV;
133         if (!is_memallocd(dev) || !is_initialised(dev)) {
134                 STK_ERROR("FIXME: Buffers are not allocated\n");
135                 return -EFAULT;
136         }
137         ret = usb_set_interface(dev->udev, 0, 5);
138
139         if (ret < 0)
140                 STK_ERROR("usb_set_interface failed !\n");
141         if (stk_sensor_wakeup(dev))
142                 STK_ERROR("error awaking the sensor\n");
143
144         stk_camera_read_reg(dev, 0x0116, &value_116);
145         stk_camera_read_reg(dev, 0x0117, &value_117);
146
147         stk_camera_write_reg(dev, 0x0116, 0x0000);
148         stk_camera_write_reg(dev, 0x0117, 0x0000);
149
150         stk_camera_read_reg(dev, 0x0100, &value);
151         stk_camera_write_reg(dev, 0x0100, value | 0x80);
152
153         stk_camera_write_reg(dev, 0x0116, value_116);
154         stk_camera_write_reg(dev, 0x0117, value_117);
155         for (i = 0; i < MAX_ISO_BUFS; i++) {
156                 if (dev->isobufs[i].urb) {
157                         ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL);
158                         atomic_inc(&dev->urbs_used);
159                         if (ret)
160                                 return ret;
161                 }
162         }
163         set_streaming(dev);
164         return 0;
165 }
166
167 static int stk_stop_stream(struct stk_camera *dev)
168 {
169         int value;
170         int i;
171         if (is_present(dev)) {
172                 stk_camera_read_reg(dev, 0x0100, &value);
173                 stk_camera_write_reg(dev, 0x0100, value & ~0x80);
174                 if (dev->isobufs != NULL) {
175                         for (i = 0; i < MAX_ISO_BUFS; i++) {
176                                 if (dev->isobufs[i].urb)
177                                         usb_kill_urb(dev->isobufs[i].urb);
178                         }
179                 }
180                 unset_streaming(dev);
181
182                 if (usb_set_interface(dev->udev, 0, 0))
183                         STK_ERROR("usb_set_interface failed !\n");
184                 if (stk_sensor_sleep(dev))
185                         STK_ERROR("error suspending the sensor\n");
186         }
187         return 0;
188 }
189
190 /*
191  * This seems to be the shortest init sequence we
192  * must do in order to find the sensor
193  * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
194  * is also reset. Maybe powers down it?
195  * Rest of values don't make a difference
196  */
197
198 static struct regval stk1125_initvals[] = {
199         /*TODO: What means this sequence? */
200         {0x0000, 0x24},
201         {0x0100, 0x21},
202         {0x0002, 0x68},
203         {0x0003, 0x80},
204         {0x0005, 0x00},
205         {0x0007, 0x03},
206         {0x000d, 0x00},
207         {0x000f, 0x02},
208         {0x0300, 0x12},
209         {0x0350, 0x41},
210         {0x0351, 0x00},
211         {0x0352, 0x00},
212         {0x0353, 0x00},
213         {0x0018, 0x10},
214         {0x0019, 0x00},
215         {0x001b, 0x0e},
216         {0x001c, 0x46},
217         {0x0300, 0x80},
218         {0x001a, 0x04},
219         {0x0110, 0x00},
220         {0x0111, 0x00},
221         {0x0112, 0x00},
222         {0x0113, 0x00},
223
224         {0xffff, 0xff},
225 };
226
227
228 static int stk_initialise(struct stk_camera *dev)
229 {
230         struct regval *rv;
231         int ret;
232         if (!is_present(dev))
233                 return -ENODEV;
234         if (is_initialised(dev))
235                 return 0;
236         rv = stk1125_initvals;
237         while (rv->reg != 0xffff) {
238                 ret = stk_camera_write_reg(dev, rv->reg, rv->val);
239                 if (ret)
240                         return ret;
241                 rv++;
242         }
243         if (stk_sensor_init(dev) == 0) {
244                 set_initialised(dev);
245                 return 0;
246         } else
247                 return -1;
248 }
249
250 #ifdef CONFIG_VIDEO_V4L1_COMPAT
251
252 /* sysfs functions */
253 /*FIXME cleanup this */
254
255 static ssize_t show_brightness(struct device *class,
256                         struct device_attribute *attr, char *buf)
257 {
258         struct video_device *vdev = to_video_device(class);
259         struct stk_camera *dev = vdev_to_camera(vdev);
260
261         return sprintf(buf, "%X\n", dev->vsettings.brightness);
262 }
263
264 static ssize_t store_brightness(struct device *class,
265                 struct device_attribute *attr, const char *buf, size_t count)
266 {
267         char *endp;
268         unsigned long value;
269         int ret;
270
271         struct video_device *vdev = to_video_device(class);
272         struct stk_camera *dev = vdev_to_camera(vdev);
273
274         value = simple_strtoul(buf, &endp, 16);
275
276         dev->vsettings.brightness = (int) value;
277
278         ret = stk_sensor_set_brightness(dev, value >> 8);
279         if (ret)
280                 return ret;
281         else
282                 return count;
283 }
284
285 static ssize_t show_hflip(struct device *class,
286                 struct device_attribute *attr, char *buf)
287 {
288         struct video_device *vdev = to_video_device(class);
289         struct stk_camera *dev = vdev_to_camera(vdev);
290
291         return sprintf(buf, "%d\n", dev->vsettings.hflip);
292 }
293
294 static ssize_t store_hflip(struct device *class,
295                 struct device_attribute *attr, const char *buf, size_t count)
296 {
297         struct video_device *vdev = to_video_device(class);
298         struct stk_camera *dev = vdev_to_camera(vdev);
299
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;
306
307         return strlen(buf);
308 }
309
310 static ssize_t show_vflip(struct device *class,
311                 struct device_attribute *attr, char *buf)
312 {
313         struct video_device *vdev = to_video_device(class);
314         struct stk_camera *dev = vdev_to_camera(vdev);
315
316         return sprintf(buf, "%d\n", dev->vsettings.vflip);
317 }
318
319 static ssize_t store_vflip(struct device *class,
320                 struct device_attribute *attr, const char *buf, size_t count)
321 {
322         struct video_device *vdev = to_video_device(class);
323         struct stk_camera *dev = vdev_to_camera(vdev);
324
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;
331
332         return strlen(buf);
333 }
334
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);
339
340 static int stk_create_sysfs_files(struct video_device *vdev)
341 {
342         int ret;
343
344         ret = device_create_file(&vdev->dev, &dev_attr_brightness);
345         ret += device_create_file(&vdev->dev, &dev_attr_hflip);
346         ret += device_create_file(&vdev->dev, &dev_attr_vflip);
347         if (ret)
348                 STK_WARNING("Could not create sysfs files\n");
349         return ret;
350 }
351
352 static void stk_remove_sysfs_files(struct video_device *vdev)
353 {
354         device_remove_file(&vdev->dev, &dev_attr_brightness);
355         device_remove_file(&vdev->dev, &dev_attr_hflip);
356         device_remove_file(&vdev->dev, &dev_attr_vflip);
357 }
358
359 #else
360 #define stk_create_sysfs_files(a)
361 #define stk_remove_sysfs_files(a)
362 #endif
363
364 /* *********************************************** */
365 /*
366  * This function is called as an URB transfert is complete (Isochronous pipe).
367  * So, the traitement is done in interrupt time, so it has be fast, not crash,
368  * and not stall. Neat.
369  */
370 static void stk_isoc_handler(struct urb *urb)
371 {
372         int i;
373         int ret;
374         int framelen;
375         unsigned long flags;
376
377         unsigned char *fill = NULL;
378         unsigned char *iso_buf = NULL;
379
380         struct stk_camera *dev;
381         struct stk_sio_buffer *fb;
382
383         dev = (struct stk_camera *) urb->context;
384
385         if (dev == NULL) {
386                 STK_ERROR("isoc_handler called with NULL device !\n");
387                 return;
388         }
389
390         if (urb->status == -ENOENT || urb->status == -ECONNRESET
391                 || urb->status == -ESHUTDOWN) {
392                 atomic_dec(&dev->urbs_used);
393                 return;
394         }
395
396         spin_lock_irqsave(&dev->spinlock, flags);
397
398         if (urb->status != -EINPROGRESS && urb->status != 0) {
399                 STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
400                 goto resubmit;
401         }
402
403         if (list_empty(&dev->sio_avail)) {
404                 /*FIXME Stop streaming after a while */
405                 (void) (printk_ratelimit() &&
406                 STK_ERROR("isoc_handler without available buffer!\n"));
407                 goto resubmit;
408         }
409         fb = list_first_entry(&dev->sio_avail,
410                         struct stk_sio_buffer, list);
411         fill = fb->buffer + fb->v4lbuf.bytesused;
412
413         for (i = 0; i < urb->number_of_packets; i++) {
414                 if (urb->iso_frame_desc[i].status != 0) {
415                         if (urb->iso_frame_desc[i].status != -EXDEV)
416                                 STK_ERROR("Frame %d has error %d\n", i,
417                                         urb->iso_frame_desc[i].status);
418                         continue;
419                 }
420                 framelen = urb->iso_frame_desc[i].actual_length;
421                 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
422
423                 if (framelen <= 4)
424                         continue; /* no data */
425
426                 /*
427                  * we found something informational from there
428                  * the isoc frames have to type of headers
429                  * type1: 00 xx 00 00 or 20 xx 00 00
430                  * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
431                  * xx is a sequencer which has never been seen over 0x3f
432                  * imho data written down looks like bayer, i see similarities
433                  * after every 640 bytes
434                  */
435                 if (*iso_buf & 0x80) {
436                         framelen -= 8;
437                         iso_buf += 8;
438                         /* This marks a new frame */
439                         if (fb->v4lbuf.bytesused != 0
440                                 && fb->v4lbuf.bytesused != dev->frame_size) {
441                                 (void) (printk_ratelimit() &&
442                                 STK_ERROR("frame %d, "
443                                         "bytesused=%d, skipping\n",
444                                         i, fb->v4lbuf.bytesused));
445                                 fb->v4lbuf.bytesused = 0;
446                                 fill = fb->buffer;
447                         } else if (fb->v4lbuf.bytesused == dev->frame_size) {
448                                 if (list_is_singular(&dev->sio_avail)) {
449                                         /* Always reuse the last buffer */
450                                         fb->v4lbuf.bytesused = 0;
451                                         fill = fb->buffer;
452                                 } else {
453                                         list_move_tail(dev->sio_avail.next,
454                                                 &dev->sio_full);
455                                         wake_up(&dev->wait_frame);
456                                         fb = list_first_entry(&dev->sio_avail,
457                                                 struct stk_sio_buffer, list);
458                                         fb->v4lbuf.bytesused = 0;
459                                         fill = fb->buffer;
460                                 }
461                         }
462                 } else {
463                         framelen -= 4;
464                         iso_buf += 4;
465                 }
466
467                 /* Our buffer is full !!! */
468                 if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
469                         (void) (printk_ratelimit() &&
470                         STK_ERROR("Frame buffer overflow, lost sync\n"));
471                         /*FIXME Do something here? */
472                         continue;
473                 }
474                 spin_unlock_irqrestore(&dev->spinlock, flags);
475                 memcpy(fill, iso_buf, framelen);
476                 spin_lock_irqsave(&dev->spinlock, flags);
477                 fill += framelen;
478
479                 /* New size of our buffer */
480                 fb->v4lbuf.bytesused += framelen;
481         }
482
483 resubmit:
484         spin_unlock_irqrestore(&dev->spinlock, flags);
485         urb->dev = dev->udev;
486         ret = usb_submit_urb(urb, GFP_ATOMIC);
487         if (ret != 0) {
488                 STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
489                         ret);
490         }
491 }
492
493 /* -------------------------------------------- */
494
495 static int stk_prepare_iso(struct stk_camera *dev)
496 {
497         void *kbuf;
498         int i, j;
499         struct urb *urb;
500         struct usb_device *udev;
501
502         if (dev == NULL)
503                 return -ENXIO;
504         udev = dev->udev;
505
506         if (dev->isobufs)
507                 STK_ERROR("isobufs already allocated. Bad\n");
508         else
509                 dev->isobufs = kzalloc(MAX_ISO_BUFS * sizeof(*dev->isobufs),
510                                         GFP_KERNEL);
511         if (dev->isobufs == NULL) {
512                 STK_ERROR("Unable to allocate iso buffers\n");
513                 return -ENOMEM;
514         }
515         for (i = 0; i < MAX_ISO_BUFS; i++) {
516                 if (dev->isobufs[i].data == NULL) {
517                         kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
518                         if (kbuf == NULL) {
519                                 STK_ERROR("Failed to allocate iso buffer %d\n",
520                                         i);
521                                 goto isobufs_out;
522                         }
523                         dev->isobufs[i].data = kbuf;
524                 } else
525                         STK_ERROR("isobuf data already allocated\n");
526                 if (dev->isobufs[i].urb == NULL) {
527                         urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
528                         if (urb == NULL) {
529                                 STK_ERROR("Failed to allocate URB %d\n", i);
530                                 goto isobufs_out;
531                         }
532                         dev->isobufs[i].urb = urb;
533                 } else {
534                         STK_ERROR("Killing URB\n");
535                         usb_kill_urb(dev->isobufs[i].urb);
536                         urb = dev->isobufs[i].urb;
537                 }
538                 urb->interval = 1;
539                 urb->dev = udev;
540                 urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
541                 urb->transfer_flags = URB_ISO_ASAP;
542                 urb->transfer_buffer = dev->isobufs[i].data;
543                 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
544                 urb->complete = stk_isoc_handler;
545                 urb->context = dev;
546                 urb->start_frame = 0;
547                 urb->number_of_packets = ISO_FRAMES_PER_DESC;
548
549                 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
550                         urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
551                         urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
552                 }
553         }
554         set_memallocd(dev);
555         return 0;
556
557 isobufs_out:
558         for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
559                 kfree(dev->isobufs[i].data);
560         for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
561                 usb_free_urb(dev->isobufs[i].urb);
562         kfree(dev->isobufs);
563         dev->isobufs = NULL;
564         return -ENOMEM;
565 }
566
567 static void stk_clean_iso(struct stk_camera *dev)
568 {
569         int i;
570
571         if (dev == NULL || dev->isobufs == NULL)
572                 return;
573
574         for (i = 0; i < MAX_ISO_BUFS; i++) {
575                 struct urb *urb;
576
577                 urb = dev->isobufs[i].urb;
578                 if (urb) {
579                         if (atomic_read(&dev->urbs_used))
580                                 usb_kill_urb(urb);
581                         usb_free_urb(urb);
582                 }
583                 kfree(dev->isobufs[i].data);
584         }
585         kfree(dev->isobufs);
586         dev->isobufs = NULL;
587         unset_memallocd(dev);
588 }
589
590 static int stk_setup_siobuf(struct stk_camera *dev, int index)
591 {
592         struct stk_sio_buffer *buf = dev->sio_bufs + index;
593         INIT_LIST_HEAD(&buf->list);
594         buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
595         buf->buffer = vmalloc_user(buf->v4lbuf.length);
596         if (buf->buffer == NULL)
597                 return -ENOMEM;
598         buf->mapcount = 0;
599         buf->dev = dev;
600         buf->v4lbuf.index = index;
601         buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
602         buf->v4lbuf.field = V4L2_FIELD_NONE;
603         buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
604         buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
605         return 0;
606 }
607
608 static int stk_free_sio_buffers(struct stk_camera *dev)
609 {
610         int i;
611         int nbufs;
612         unsigned long flags;
613         if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
614                 return 0;
615         /*
616         * If any buffers are mapped, we cannot free them at all.
617         */
618         for (i = 0; i < dev->n_sbufs; i++) {
619                 if (dev->sio_bufs[i].mapcount > 0)
620                         return -EBUSY;
621         }
622         /*
623         * OK, let's do it.
624         */
625         spin_lock_irqsave(&dev->spinlock, flags);
626         INIT_LIST_HEAD(&dev->sio_avail);
627         INIT_LIST_HEAD(&dev->sio_full);
628         nbufs = dev->n_sbufs;
629         dev->n_sbufs = 0;
630         spin_unlock_irqrestore(&dev->spinlock, flags);
631         for (i = 0; i < nbufs; i++) {
632                 if (dev->sio_bufs[i].buffer != NULL)
633                         vfree(dev->sio_bufs[i].buffer);
634         }
635         kfree(dev->sio_bufs);
636         dev->sio_bufs = NULL;
637         return 0;
638 }
639
640 static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
641 {
642         int i;
643         if (dev->sio_bufs != NULL)
644                 STK_ERROR("sio_bufs already allocated\n");
645         else {
646                 dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
647                                 GFP_KERNEL);
648                 if (dev->sio_bufs == NULL)
649                         return -ENOMEM;
650                 for (i = 0; i < n_sbufs; i++) {
651                         if (stk_setup_siobuf(dev, i))
652                                 return (dev->n_sbufs > 1)? 0 : -ENOMEM;
653                         dev->n_sbufs = i+1;
654                 }
655         }
656         return 0;
657 }
658
659 static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
660 {
661         int err;
662         err = stk_prepare_iso(dev);
663         if (err) {
664                 stk_clean_iso(dev);
665                 return err;
666         }
667         err = stk_prepare_sio_buffers(dev, n_sbufs);
668         if (err) {
669                 stk_free_sio_buffers(dev);
670                 return err;
671         }
672         return 0;
673 }
674
675 static void stk_free_buffers(struct stk_camera *dev)
676 {
677         stk_clean_iso(dev);
678         stk_free_sio_buffers(dev);
679 }
680 /* -------------------------------------------- */
681
682 /* v4l file operations */
683
684 static int v4l_stk_open(struct inode *inode, struct file *fp)
685 {
686         struct stk_camera *dev;
687         struct video_device *vdev;
688
689         vdev = video_devdata(fp);
690         dev = vdev_to_camera(vdev);
691
692         if (dev == NULL || !is_present(dev))
693                 return -ENXIO;
694         fp->private_data = vdev;
695         kref_get(&dev->kref);
696         usb_autopm_get_interface(dev->interface);
697
698         return 0;
699 }
700
701 static int v4l_stk_release(struct inode *inode, struct file *fp)
702 {
703         struct stk_camera *dev;
704         struct video_device *vdev;
705
706         vdev = video_devdata(fp);
707         if (vdev == NULL) {
708                 STK_ERROR("v4l_release called w/o video devdata\n");
709                 return -EFAULT;
710         }
711         dev = vdev_to_camera(vdev);
712         if (dev == NULL) {
713                 STK_ERROR("v4l_release called on removed device\n");
714                 return -ENODEV;
715         }
716
717         if (dev->owner != fp) {
718                 usb_autopm_put_interface(dev->interface);
719                 kref_put(&dev->kref, stk_camera_cleanup);
720                 return 0;
721         }
722
723         stk_stop_stream(dev);
724
725         stk_free_buffers(dev);
726
727         dev->owner = NULL;
728
729         usb_autopm_put_interface(dev->interface);
730         kref_put(&dev->kref, stk_camera_cleanup);
731
732         return 0;
733 }
734
735 static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
736                 size_t count, loff_t *f_pos)
737 {
738         int i;
739         int ret;
740         unsigned long flags;
741         struct stk_camera *dev;
742         struct video_device *vdev;
743         struct stk_sio_buffer *sbuf;
744
745         vdev = video_devdata(fp);
746         if (vdev == NULL)
747                 return -EFAULT;
748         dev = vdev_to_camera(vdev);
749
750         if (dev == NULL)
751                 return -EIO;
752
753         if (!is_present(dev))
754                 return -EIO;
755         if (dev->owner && dev->owner != fp)
756                 return -EBUSY;
757         dev->owner = fp;
758         if (!is_streaming(dev)) {
759                 if (stk_initialise(dev)
760                         || stk_allocate_buffers(dev, 3)
761                         || stk_start_stream(dev))
762                         return -ENOMEM;
763                 spin_lock_irqsave(&dev->spinlock, flags);
764                 for (i = 0; i < dev->n_sbufs; i++) {
765                         list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
766                         dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
767                 }
768                 spin_unlock_irqrestore(&dev->spinlock, flags);
769         }
770         if (*f_pos == 0) {
771                 if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
772                         return -EWOULDBLOCK;
773                 ret = wait_event_interruptible(dev->wait_frame,
774                         !list_empty(&dev->sio_full) || !is_present(dev));
775                 if (ret)
776                         return ret;
777                 if (!is_present(dev))
778                         return -EIO;
779         }
780         if (count + *f_pos > dev->frame_size)
781                 count = dev->frame_size - *f_pos;
782         spin_lock_irqsave(&dev->spinlock, flags);
783         if (list_empty(&dev->sio_full)) {
784                 spin_unlock_irqrestore(&dev->spinlock, flags);
785                 STK_ERROR("BUG: No siobufs ready\n");
786                 return 0;
787         }
788         sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
789         spin_unlock_irqrestore(&dev->spinlock, flags);
790
791         if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
792                 return -EFAULT;
793
794         *f_pos += count;
795
796         if (*f_pos >= dev->frame_size) {
797                 *f_pos = 0;
798                 spin_lock_irqsave(&dev->spinlock, flags);
799                 list_move_tail(&sbuf->list, &dev->sio_avail);
800                 spin_unlock_irqrestore(&dev->spinlock, flags);
801         }
802         return count;
803 }
804
805 static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
806 {
807         struct stk_camera *dev;
808         struct video_device *vdev;
809
810         vdev = video_devdata(fp);
811
812         if (vdev == NULL)
813                 return -EFAULT;
814
815         dev = vdev_to_camera(vdev);
816         if (dev == NULL)
817                 return -ENODEV;
818
819         poll_wait(fp, &dev->wait_frame, wait);
820
821         if (!is_present(dev))
822                 return POLLERR;
823
824         if (!list_empty(&dev->sio_full))
825                 return (POLLIN | POLLRDNORM);
826
827         return 0;
828 }
829
830
831 static void stk_v4l_vm_open(struct vm_area_struct *vma)
832 {
833         struct stk_sio_buffer *sbuf = vma->vm_private_data;
834         sbuf->mapcount++;
835 }
836 static void stk_v4l_vm_close(struct vm_area_struct *vma)
837 {
838         struct stk_sio_buffer *sbuf = vma->vm_private_data;
839         sbuf->mapcount--;
840         if (sbuf->mapcount == 0)
841                 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
842 }
843 static struct vm_operations_struct stk_v4l_vm_ops = {
844         .open = stk_v4l_vm_open,
845         .close = stk_v4l_vm_close
846 };
847
848 static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
849 {
850         unsigned int i;
851         int ret;
852         unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
853         struct stk_camera *dev;
854         struct video_device *vdev;
855         struct stk_sio_buffer *sbuf = NULL;
856
857         if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
858                 return -EINVAL;
859
860         vdev = video_devdata(fp);
861         dev = vdev_to_camera(vdev);
862
863         for (i = 0; i < dev->n_sbufs; i++) {
864                 if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
865                         sbuf = dev->sio_bufs + i;
866                         break;
867                 }
868         }
869         if (sbuf == NULL)
870                 return -EINVAL;
871         ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
872         if (ret)
873                 return ret;
874         vma->vm_flags |= VM_DONTEXPAND;
875         vma->vm_private_data = sbuf;
876         vma->vm_ops = &stk_v4l_vm_ops;
877         sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
878         stk_v4l_vm_open(vma);
879         return 0;
880 }
881
882 /* v4l ioctl handlers */
883
884 static int stk_vidioc_querycap(struct file *filp,
885                 void *priv, struct v4l2_capability *cap)
886 {
887         strcpy(cap->driver, "stk");
888         strcpy(cap->card, "stk");
889         cap->version = DRIVER_VERSION_NUM;
890
891         cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
892                 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
893         return 0;
894 }
895
896 static int stk_vidioc_enum_input(struct file *filp,
897                 void *priv, struct v4l2_input *input)
898 {
899         if (input->index != 0)
900                 return -EINVAL;
901
902         strcpy(input->name, "Syntek USB Camera");
903         input->type = V4L2_INPUT_TYPE_CAMERA;
904         return 0;
905 }
906
907
908 static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
909 {
910         *i = 0;
911         return 0;
912 }
913
914 static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
915 {
916         if (i != 0)
917                 return -EINVAL;
918         else
919                 return 0;
920 }
921
922 /* from vivi.c */
923 static int stk_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
924 {
925         return 0;
926 }
927
928 /* List of all V4Lv2 controls supported by the driver */
929 static struct v4l2_queryctrl stk_controls[] = {
930         {
931                 .id      = V4L2_CID_BRIGHTNESS,
932                 .type    = V4L2_CTRL_TYPE_INTEGER,
933                 .name    = "Brightness",
934                 .minimum = 0,
935                 .maximum = 0xffff,
936                 .step    = 0x0100,
937                 .default_value = 0x6000,
938         },
939         /*TODO: get more controls to work */
940 };
941
942 static int stk_vidioc_queryctrl(struct file *filp,
943                 void *priv, struct v4l2_queryctrl *c)
944 {
945         int i;
946         int nbr;
947         nbr = ARRAY_SIZE(stk_controls);
948
949         for (i = 0; i < nbr; i++) {
950                 if (stk_controls[i].id == c->id) {
951                         memcpy(c, &stk_controls[i],
952                                 sizeof(struct v4l2_queryctrl));
953                         return 0;
954                 }
955         }
956         return -EINVAL;
957 }
958
959 static int stk_vidioc_g_ctrl(struct file *filp,
960                 void *priv, struct v4l2_control *c)
961 {
962         struct stk_camera *dev = priv;
963         switch (c->id) {
964         case V4L2_CID_BRIGHTNESS:
965                 c->value = dev->vsettings.brightness;
966                 break;
967         default:
968                 return -EINVAL;
969         }
970         return 0;
971 }
972
973 static int stk_vidioc_s_ctrl(struct file *filp,
974                 void *priv, struct v4l2_control *c)
975 {
976         struct stk_camera *dev = priv;
977         switch (c->id) {
978         case V4L2_CID_BRIGHTNESS:
979                 dev->vsettings.brightness = c->value;
980                 return stk_sensor_set_brightness(dev, c->value >> 8);
981         default:
982                 return -EINVAL;
983         }
984         return 0;
985 }
986
987
988 static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
989                 void *priv, struct v4l2_fmtdesc *fmtd)
990 {
991         fmtd->flags = 0;
992
993         switch (fmtd->index) {
994         case 0:
995                 fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
996                 strcpy(fmtd->description, "r5g6b5");
997                 break;
998         case 1:
999                 fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
1000                 strcpy(fmtd->description, "r5g6b5BE");
1001                 break;
1002         case 2:
1003                 fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
1004                 strcpy(fmtd->description, "yuv4:2:2");
1005                 break;
1006         case 3:
1007                 fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
1008                 strcpy(fmtd->description, "Raw bayer");
1009                 break;
1010         case 4:
1011                 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1012                 strcpy(fmtd->description, "yuv4:2:2");
1013                 break;
1014         default:
1015                 return -EINVAL;
1016         }
1017         return 0;
1018 }
1019
1020 static struct stk_size {
1021         unsigned w;
1022         unsigned h;
1023         enum stk_mode m;
1024 } stk_sizes[] = {
1025         { .w = 1280, .h = 1024, .m = MODE_SXGA, },
1026         { .w = 640,  .h = 480,  .m = MODE_VGA,  },
1027         { .w = 352,  .h = 288,  .m = MODE_CIF,  },
1028         { .w = 320,  .h = 240,  .m = MODE_QVGA, },
1029         { .w = 176,  .h = 144,  .m = MODE_QCIF, },
1030 };
1031
1032 static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
1033                 void *priv, struct v4l2_format *f)
1034 {
1035         struct v4l2_pix_format *pix_format = &f->fmt.pix;
1036         struct stk_camera *dev = priv;
1037         int i;
1038
1039         for (i = 0; i < ARRAY_SIZE(stk_sizes)
1040                         && stk_sizes[i].m != dev->vsettings.mode;
1041                 i++);
1042         if (i == ARRAY_SIZE(stk_sizes)) {
1043                 STK_ERROR("ERROR: mode invalid\n");
1044                 return -EINVAL;
1045         }
1046         pix_format->width = stk_sizes[i].w;
1047         pix_format->height = stk_sizes[i].h;
1048         pix_format->field = V4L2_FIELD_NONE;
1049         pix_format->colorspace = V4L2_COLORSPACE_SRGB;
1050         pix_format->priv = 0;
1051         pix_format->pixelformat = dev->vsettings.palette;
1052         if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
1053                 pix_format->bytesperline = pix_format->width;
1054         else
1055                 pix_format->bytesperline = 2 * pix_format->width;
1056         pix_format->sizeimage = pix_format->bytesperline
1057                                 * pix_format->height;
1058         return 0;
1059 }
1060
1061 static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
1062                 void *priv, struct v4l2_format *fmtd)
1063 {
1064         int i;
1065         switch (fmtd->fmt.pix.pixelformat) {
1066         case V4L2_PIX_FMT_RGB565:
1067         case V4L2_PIX_FMT_RGB565X:
1068         case V4L2_PIX_FMT_UYVY:
1069         case V4L2_PIX_FMT_YUYV:
1070         case V4L2_PIX_FMT_SBGGR8:
1071                 break;
1072         default:
1073                 return -EINVAL;
1074         }
1075         for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
1076                 if (fmtd->fmt.pix.width > stk_sizes[i].w)
1077                         break;
1078         }
1079         if (i == ARRAY_SIZE(stk_sizes)
1080                 || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
1081                         < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
1082                 fmtd->fmt.pix.height = stk_sizes[i-1].h;
1083                 fmtd->fmt.pix.width = stk_sizes[i-1].w;
1084                 fmtd->fmt.pix.priv = i - 1;
1085         } else {
1086                 fmtd->fmt.pix.height = stk_sizes[i].h;
1087                 fmtd->fmt.pix.width = stk_sizes[i].w;
1088                 fmtd->fmt.pix.priv = i;
1089         }
1090
1091         fmtd->fmt.pix.field = V4L2_FIELD_NONE;
1092         fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
1093         if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
1094                 fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
1095         else
1096                 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
1097         fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
1098                 * fmtd->fmt.pix.height;
1099         return 0;
1100 }
1101
1102 static int stk_setup_format(struct stk_camera *dev)
1103 {
1104         int i = 0;
1105         int depth;
1106         if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
1107                 depth = 1;
1108         else
1109                 depth = 2;
1110         while (stk_sizes[i].m != dev->vsettings.mode
1111                         && i < ARRAY_SIZE(stk_sizes))
1112                 i++;
1113         if (i == ARRAY_SIZE(stk_sizes)) {
1114                 STK_ERROR("Something is broken in %s\n", __func__);
1115                 return -EFAULT;
1116         }
1117         /* This registers controls some timings, not sure of what. */
1118         stk_camera_write_reg(dev, 0x001b, 0x0e);
1119         if (dev->vsettings.mode == MODE_SXGA)
1120                 stk_camera_write_reg(dev, 0x001c, 0x0e);
1121         else
1122                 stk_camera_write_reg(dev, 0x001c, 0x46);
1123         /*
1124          * Registers 0x0115 0x0114 are the size of each line (bytes),
1125          * regs 0x0117 0x0116 are the heigth of the image.
1126          */
1127         stk_camera_write_reg(dev, 0x0115,
1128                 ((stk_sizes[i].w * depth) >> 8) & 0xff);
1129         stk_camera_write_reg(dev, 0x0114,
1130                 (stk_sizes[i].w * depth) & 0xff);
1131         stk_camera_write_reg(dev, 0x0117,
1132                 (stk_sizes[i].h >> 8) & 0xff);
1133         stk_camera_write_reg(dev, 0x0116,
1134                 stk_sizes[i].h & 0xff);
1135         return stk_sensor_configure(dev);
1136 }
1137
1138 static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
1139                 void *priv, struct v4l2_format *fmtd)
1140 {
1141         int ret;
1142         struct stk_camera *dev = priv;
1143
1144         if (dev == NULL)
1145                 return -ENODEV;
1146         if (!is_present(dev))
1147                 return -ENODEV;
1148         if (is_streaming(dev))
1149                 return -EBUSY;
1150         if (dev->owner && dev->owner != filp)
1151                 return -EBUSY;
1152         ret = stk_vidioc_try_fmt_vid_cap(filp, priv, fmtd);
1153         if (ret)
1154                 return ret;
1155         dev->owner = filp;
1156
1157         dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
1158         stk_free_buffers(dev);
1159         dev->frame_size = fmtd->fmt.pix.sizeimage;
1160         dev->vsettings.mode = stk_sizes[fmtd->fmt.pix.priv].m;
1161
1162         stk_initialise(dev);
1163         return stk_setup_format(dev);
1164 }
1165
1166 static int stk_vidioc_reqbufs(struct file *filp,
1167                 void *priv, struct v4l2_requestbuffers *rb)
1168 {
1169         struct stk_camera *dev = priv;
1170
1171         if (dev == NULL)
1172                 return -ENODEV;
1173         if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1174                 return -EINVAL;
1175         if (rb->memory != V4L2_MEMORY_MMAP)
1176                 return -EINVAL;
1177         if (is_streaming(dev)
1178                 || (dev->owner && dev->owner != filp))
1179                 return -EBUSY;
1180         dev->owner = filp;
1181
1182         /*FIXME If they ask for zero, we must stop streaming and free */
1183         if (rb->count < 3)
1184                 rb->count = 3;
1185         /* Arbitrary limit */
1186         else if (rb->count > 5)
1187                 rb->count = 5;
1188
1189         stk_allocate_buffers(dev, rb->count);
1190         rb->count = dev->n_sbufs;
1191         return 0;
1192 }
1193
1194 static int stk_vidioc_querybuf(struct file *filp,
1195                 void *priv, struct v4l2_buffer *buf)
1196 {
1197         int index;
1198         struct stk_camera *dev = priv;
1199         struct stk_sio_buffer *sbuf;
1200
1201         if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1202                 return -EINVAL;
1203
1204         index = buf->index;
1205
1206         if (index < 0 || index >= dev->n_sbufs)
1207                 return -EINVAL;
1208         sbuf = dev->sio_bufs + buf->index;
1209         *buf = sbuf->v4lbuf;
1210         return 0;
1211 }
1212
1213 static int stk_vidioc_qbuf(struct file *filp,
1214                 void *priv, struct v4l2_buffer *buf)
1215 {
1216         struct stk_camera *dev = priv;
1217         struct stk_sio_buffer *sbuf;
1218         unsigned long flags;
1219         if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1220                 return -EINVAL;
1221
1222         if (buf->memory != V4L2_MEMORY_MMAP)
1223                 return -EINVAL;
1224
1225         if (buf->index < 0 || buf->index >= dev->n_sbufs)
1226                 return -EINVAL;
1227         sbuf = dev->sio_bufs + buf->index;
1228         if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
1229                 return 0;
1230         sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1231         sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1232         spin_lock_irqsave(&dev->spinlock, flags);
1233         list_add_tail(&sbuf->list, &dev->sio_avail);
1234         *buf = sbuf->v4lbuf;
1235         spin_unlock_irqrestore(&dev->spinlock, flags);
1236         return 0;
1237 }
1238
1239 static int stk_vidioc_dqbuf(struct file *filp,
1240                 void *priv, struct v4l2_buffer *buf)
1241 {
1242         struct stk_camera *dev = priv;
1243         struct stk_sio_buffer *sbuf;
1244         unsigned long flags;
1245         int ret;
1246
1247         if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1248                 || !is_streaming(dev))
1249                 return -EINVAL;
1250
1251         if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
1252                 return -EWOULDBLOCK;
1253         ret = wait_event_interruptible(dev->wait_frame,
1254                 !list_empty(&dev->sio_full) || !is_present(dev));
1255         if (ret)
1256                 return ret;
1257         if (!is_present(dev))
1258                 return -EIO;
1259
1260         spin_lock_irqsave(&dev->spinlock, flags);
1261         sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
1262         list_del_init(&sbuf->list);
1263         spin_unlock_irqrestore(&dev->spinlock, flags);
1264         sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1265         sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1266         sbuf->v4lbuf.sequence = ++dev->sequence;
1267         do_gettimeofday(&sbuf->v4lbuf.timestamp);
1268
1269         *buf = sbuf->v4lbuf;
1270         return 0;
1271 }
1272
1273 static int stk_vidioc_streamon(struct file *filp,
1274                 void *priv, enum v4l2_buf_type type)
1275 {
1276         struct stk_camera *dev = priv;
1277         if (is_streaming(dev))
1278                 return 0;
1279         if (dev->sio_bufs == NULL)
1280                 return -EINVAL;
1281         dev->sequence = 0;
1282         return stk_start_stream(dev);
1283 }
1284
1285 static int stk_vidioc_streamoff(struct file *filp,
1286                 void *priv, enum v4l2_buf_type type)
1287 {
1288         struct stk_camera *dev = priv;
1289         unsigned long flags;
1290         int i;
1291         stk_stop_stream(dev);
1292         spin_lock_irqsave(&dev->spinlock, flags);
1293         INIT_LIST_HEAD(&dev->sio_avail);
1294         INIT_LIST_HEAD(&dev->sio_full);
1295         for (i = 0; i < dev->n_sbufs; i++) {
1296                 INIT_LIST_HEAD(&dev->sio_bufs[i].list);
1297                 dev->sio_bufs[i].v4lbuf.flags = 0;
1298         }
1299         spin_unlock_irqrestore(&dev->spinlock, flags);
1300         return 0;
1301 }
1302
1303
1304 static int stk_vidioc_g_parm(struct file *filp,
1305                 void *priv, struct v4l2_streamparm *sp)
1306 {
1307         if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1308                 return -EINVAL;
1309
1310         sp->parm.capture.capability = 0;
1311         sp->parm.capture.capturemode = 0;
1312         /*FIXME This is not correct */
1313         sp->parm.capture.timeperframe.numerator = 1;
1314         sp->parm.capture.timeperframe.denominator = 30;
1315         sp->parm.capture.readbuffers = 2;
1316         sp->parm.capture.extendedmode = 0;
1317         return 0;
1318 }
1319
1320 static struct file_operations v4l_stk_fops = {
1321         .owner = THIS_MODULE,
1322         .open = v4l_stk_open,
1323         .release = v4l_stk_release,
1324         .read = v4l_stk_read,
1325         .poll = v4l_stk_poll,
1326         .mmap = v4l_stk_mmap,
1327         .ioctl = video_ioctl2,
1328 #ifdef CONFIG_COMPAT
1329         .compat_ioctl = v4l_compat_ioctl32,
1330 #endif
1331         .llseek = no_llseek
1332 };
1333
1334 static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops = {
1335         .vidioc_querycap = stk_vidioc_querycap,
1336         .vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
1337         .vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
1338         .vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
1339         .vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
1340         .vidioc_enum_input = stk_vidioc_enum_input,
1341         .vidioc_s_input = stk_vidioc_s_input,
1342         .vidioc_g_input = stk_vidioc_g_input,
1343         .vidioc_s_std = stk_vidioc_s_std,
1344         .vidioc_reqbufs = stk_vidioc_reqbufs,
1345         .vidioc_querybuf = stk_vidioc_querybuf,
1346         .vidioc_qbuf = stk_vidioc_qbuf,
1347         .vidioc_dqbuf = stk_vidioc_dqbuf,
1348         .vidioc_streamon = stk_vidioc_streamon,
1349         .vidioc_streamoff = stk_vidioc_streamoff,
1350         .vidioc_queryctrl = stk_vidioc_queryctrl,
1351         .vidioc_g_ctrl = stk_vidioc_g_ctrl,
1352         .vidioc_s_ctrl = stk_vidioc_s_ctrl,
1353         .vidioc_g_parm = stk_vidioc_g_parm,
1354 };
1355
1356 static void stk_v4l_dev_release(struct video_device *vd)
1357 {
1358 }
1359
1360 static struct video_device stk_v4l_data = {
1361         .name = "stkwebcam",
1362         .minor = -1,
1363         .tvnorms = V4L2_STD_UNKNOWN,
1364         .current_norm = V4L2_STD_UNKNOWN,
1365         .fops = &v4l_stk_fops,
1366         .ioctl_ops = &v4l_stk_ioctl_ops,
1367         .release = stk_v4l_dev_release,
1368 };
1369
1370
1371 static int stk_register_video_device(struct stk_camera *dev)
1372 {
1373         int err;
1374
1375         dev->vdev = stk_v4l_data;
1376         dev->vdev.debug = debug;
1377         dev->vdev.parent = &dev->interface->dev;
1378         dev->vdev.priv = dev;
1379         err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1380         if (err)
1381                 STK_ERROR("v4l registration failed\n");
1382         else
1383                 STK_INFO("Syntek USB2.0 Camera is now controlling video device"
1384                         " /dev/video%d\n", dev->vdev.minor);
1385         return err;
1386 }
1387
1388
1389 /* USB Stuff */
1390
1391 static int stk_camera_probe(struct usb_interface *interface,
1392                 const struct usb_device_id *id)
1393 {
1394         int i;
1395         int err;
1396
1397         struct stk_camera *dev = NULL;
1398         struct usb_device *udev = interface_to_usbdev(interface);
1399         struct usb_host_interface *iface_desc;
1400         struct usb_endpoint_descriptor *endpoint;
1401
1402         dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
1403         if (dev == NULL) {
1404                 STK_ERROR("Out of memory !\n");
1405                 return -ENOMEM;
1406         }
1407
1408         kref_init(&dev->kref);
1409         spin_lock_init(&dev->spinlock);
1410         init_waitqueue_head(&dev->wait_frame);
1411
1412         dev->udev = udev;
1413         dev->interface = interface;
1414         usb_get_intf(interface);
1415
1416         dev->vsettings.vflip = vflip;
1417         dev->vsettings.hflip = hflip;
1418         dev->n_sbufs = 0;
1419         set_present(dev);
1420
1421         /* Set up the endpoint information
1422          * use only the first isoc-in endpoint
1423          * for the current alternate setting */
1424         iface_desc = interface->cur_altsetting;
1425
1426         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1427                 endpoint = &iface_desc->endpoint[i].desc;
1428
1429                 if (!dev->isoc_ep
1430                         && ((endpoint->bEndpointAddress
1431                                 & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
1432                         && ((endpoint->bmAttributes
1433                                 & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_ISOC)) {
1434                         /* we found an isoc in endpoint */
1435                         dev->isoc_ep = (endpoint->bEndpointAddress & 0xF);
1436                         break;
1437                 }
1438         }
1439         if (!dev->isoc_ep) {
1440                 STK_ERROR("Could not find isoc-in endpoint");
1441                 kref_put(&dev->kref, stk_camera_cleanup);
1442                 return -ENODEV;
1443         }
1444         dev->vsettings.brightness = 0x7fff;
1445         dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
1446         dev->vsettings.mode = MODE_VGA;
1447         dev->frame_size = 640 * 480 * 2;
1448
1449         INIT_LIST_HEAD(&dev->sio_avail);
1450         INIT_LIST_HEAD(&dev->sio_full);
1451
1452         usb_set_intfdata(interface, dev);
1453
1454         err = stk_register_video_device(dev);
1455         if (err) {
1456                 kref_put(&dev->kref, stk_camera_cleanup);
1457                 return err;
1458         }
1459
1460         stk_create_sysfs_files(&dev->vdev);
1461         usb_autopm_enable(dev->interface);
1462
1463         return 0;
1464 }
1465
1466 static void stk_camera_disconnect(struct usb_interface *interface)
1467 {
1468         struct stk_camera *dev = usb_get_intfdata(interface);
1469
1470         usb_set_intfdata(interface, NULL);
1471         unset_present(dev);
1472
1473         wake_up_interruptible(&dev->wait_frame);
1474         stk_remove_sysfs_files(&dev->vdev);
1475
1476         kref_put(&dev->kref, stk_camera_cleanup);
1477 }
1478
1479 #ifdef CONFIG_PM
1480 static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
1481 {
1482         struct stk_camera *dev = usb_get_intfdata(intf);
1483         if (is_streaming(dev)) {
1484                 stk_stop_stream(dev);
1485                 /* yes, this is ugly */
1486                 set_streaming(dev);
1487         }
1488         return 0;
1489 }
1490
1491 static int stk_camera_resume(struct usb_interface *intf)
1492 {
1493         struct stk_camera *dev = usb_get_intfdata(intf);
1494         if (!is_initialised(dev))
1495                 return 0;
1496         unset_initialised(dev);
1497         stk_initialise(dev);
1498         stk_setup_format(dev);
1499         if (is_streaming(dev))
1500                 stk_start_stream(dev);
1501         return 0;
1502 }
1503 #endif
1504
1505 static struct usb_driver stk_camera_driver = {
1506         .name = "stkwebcam",
1507         .probe = stk_camera_probe,
1508         .disconnect = stk_camera_disconnect,
1509         .id_table = stkwebcam_table,
1510 #ifdef CONFIG_PM
1511         .suspend = stk_camera_suspend,
1512         .resume = stk_camera_resume,
1513 #endif
1514 };
1515
1516
1517 static int __init stk_camera_init(void)
1518 {
1519         int result;
1520
1521         result = usb_register(&stk_camera_driver);
1522         if (result)
1523                 STK_ERROR("usb_register failed ! Error number %d\n", result);
1524
1525
1526         return result;
1527 }
1528
1529 static void __exit stk_camera_exit(void)
1530 {
1531         usb_deregister(&stk_camera_driver);
1532 }
1533
1534 module_init(stk_camera_init);
1535 module_exit(stk_camera_exit);
1536
1537