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