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