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