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