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