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