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