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