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