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