V4L/DVB (8158): gspca: minor changes
[linux-2.6] / drivers / media / video / gspca / gspca.c
1 /*
2  * Main USB camera driver
3  *
4  * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; either version 2 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #define MODULE_NAME "gspca"
22
23 #include <linux/init.h>
24 #include <linux/fs.h>
25 #include <linux/vmalloc.h>
26 #include <linux/sched.h>
27 #include <linux/slab.h>
28 #include <linux/mm.h>
29 #include <linux/string.h>
30 #include <linux/pagemap.h>
31 #include <asm/io.h>
32 #include <asm/page.h>
33 #include <asm/uaccess.h>
34 #include <linux/jiffies.h>
35
36 #include "gspca.h"
37
38 #undef CONFIG_VIDEO_V4L1_COMPAT
39
40 /* global values */
41 #define DEF_NURBS 2             /* default number of URBs (mmap) */
42 #define USR_NURBS 5             /* default number of URBs (userptr) */
43
44 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
45 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
46 MODULE_LICENSE("GPL");
47
48 #define DRIVER_VERSION_NUMBER   KERNEL_VERSION(2, 1, 0)
49 static const char version[] = "2.1.0";
50
51 static int video_nr = -1;
52
53 static int comp_fac = 30;       /* Buffer size ratio when compressed in % */
54
55 #ifdef VIDEO_ADV_DEBUG
56 int gspca_debug = D_ERR | D_PROBE;
57 EXPORT_SYMBOL(gspca_debug);
58
59 static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
60 {
61         if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
62                 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
63                         txt,
64                         pixfmt & 0xff,
65                         (pixfmt >> 8) & 0xff,
66                         (pixfmt >> 16) & 0xff,
67                         pixfmt >> 24,
68                         w, h);
69         } else {
70                 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
71                         txt,
72                         pixfmt,
73                         w, h);
74         }
75 }
76 #else
77 #define PDEBUG_MODE(txt, pixfmt, w, h)
78 #endif
79
80 /* specific memory types - !! should different from V4L2_MEMORY_xxx */
81 #define GSPCA_MEMORY_NO 0       /* V4L2_MEMORY_xxx starts from 1 */
82 #define GSPCA_MEMORY_READ 7
83
84 #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
85
86 /*
87  * VMA operations.
88  */
89 static void gspca_vm_open(struct vm_area_struct *vma)
90 {
91         struct gspca_frame *frame = vma->vm_private_data;
92
93         frame->vma_use_count++;
94         frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
95 }
96
97 static void gspca_vm_close(struct vm_area_struct *vma)
98 {
99         struct gspca_frame *frame = vma->vm_private_data;
100
101         if (--frame->vma_use_count <= 0)
102                 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
103 }
104
105 static struct vm_operations_struct gspca_vm_ops = {
106         .open           = gspca_vm_open,
107         .close          = gspca_vm_close,
108 };
109
110 /*
111  * fill a video frame from an URB and resubmit
112  */
113 static void fill_frame(struct gspca_dev *gspca_dev,
114                         struct urb *urb)
115 {
116         struct gspca_frame *frame;
117         __u8 *data;             /* address of data in the iso message */
118         int i, j, len, st;
119         cam_pkt_op pkt_scan;
120
121         if (urb->status != 0) {
122                 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
123                 return;         /* disconnection ? */
124         }
125         pkt_scan = gspca_dev->sd_desc->pkt_scan;
126         for (i = 0; i < urb->number_of_packets; i++) {
127
128                 /* check the availability of the frame buffer */
129                 j = gspca_dev->fr_i;
130                 j = gspca_dev->fr_queue[j];
131                 frame = &gspca_dev->frame[j];
132                 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
133                                         != V4L2_BUF_FLAG_QUEUED) {
134                         gspca_dev->last_packet_type = DISCARD_PACKET;
135                         break;
136                 }
137
138                 /* check the packet status and length */
139                 len = urb->iso_frame_desc[i].actual_length;
140                 if (len == 0)
141                         continue;
142                 st = urb->iso_frame_desc[i].status;
143                 if (st) {
144                         PDEBUG(D_ERR,
145                                 "ISOC data error: [%d] len=%d, status=%d",
146                                 i, len, st);
147                         gspca_dev->last_packet_type = DISCARD_PACKET;
148                         continue;
149                 }
150
151                 /* let the packet be analyzed by the subdriver */
152                 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
153                         i, urb->iso_frame_desc[i].offset, len);
154                 data = (__u8 *) urb->transfer_buffer
155                                         + urb->iso_frame_desc[i].offset;
156                 pkt_scan(gspca_dev, frame, data, len);
157         }
158
159         /* resubmit the URB */
160 /*fixme: don't do that when userptr and too many URBs sent*/
161         urb->status = 0;
162         st = usb_submit_urb(urb, GFP_ATOMIC);
163         if (st < 0)
164                 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
165 }
166
167 /*
168  * ISOC message interrupt from the USB device
169  *
170  * Analyse each packet and call the subdriver for copy
171  * to the frame buffer.
172  *
173  * There are 2 functions:
174  *      - the first one (isoc_irq_mmap) is used when the application
175  *        buffers are mapped. The frame detection and copy is done
176  *        at interrupt level.
177  *      - the second one (isoc_irq_user) is used when the application
178  *        buffers are in user space (userptr). The frame detection
179  *        and copy is done by the application.
180  */
181 static void isoc_irq_mmap(struct urb *urb
182 )
183 {
184         struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
185
186         PDEBUG(D_PACK, "isoc irq mmap");
187         if (!gspca_dev->streaming)
188                 return;
189         fill_frame(gspca_dev, urb);
190 }
191
192 static void isoc_irq_user(struct urb *urb
193 )
194 {
195         struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
196         int i;
197
198         PDEBUG(D_PACK, "isoc irq user");
199         if (!gspca_dev->streaming)
200                 return;
201
202         i = gspca_dev->urb_in % gspca_dev->nurbs;
203         if (urb != gspca_dev->urb[i]) {
204                 PDEBUG(D_ERR|D_PACK, "urb out of sequence");
205                 return;                 /* should never occur */
206         }
207
208         gspca_dev->urb_in++;
209         atomic_inc(&gspca_dev->nevent);         /* new event */
210         wake_up_interruptible(&gspca_dev->wq);
211 /*fixme: submit a new URBs until urb_in == urb_out (% nurbs)*/
212 }
213
214 /*
215  * treat the isoc messages
216  *
217  * This routine is called by the application (case userptr).
218  */
219 static void isoc_transfer(struct gspca_dev *gspca_dev)
220 {
221         struct urb *urb;
222         int i;
223
224         for (;;) {
225                 i = gspca_dev->urb_out;
226                 PDEBUG(D_PACK, "isoc transf i:%d o:%d", gspca_dev->urb_in, i);
227                 if (i == gspca_dev->urb_in)     /* isoc message to read */
228                         break;                  /* no (more) message */
229                 atomic_dec(&gspca_dev->nevent);
230 /*PDEBUG(D_PACK, "isoc_trf nevent: %d", atomic_read(&gspca_dev->nevent));*/
231                 gspca_dev->urb_out = i + 1;     /* message treated */
232                 urb = gspca_dev->urb[i % gspca_dev->nurbs];
233                 fill_frame(gspca_dev, urb);
234         }
235 }
236
237 /*
238  * add data to the current frame
239  *
240  * This function is called by the subdrivers at interrupt level
241  * or user level.
242  * To build a frame, these ones must add
243  *      - one FIRST_PACKET
244  *      - 0 or many INTER_PACKETs
245  *      - one LAST_PACKET
246  * DISCARD_PACKET invalidates the whole frame.
247  * On LAST_PACKET, a new frame is returned.
248  */
249 struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
250                                     int packet_type,
251                                     struct gspca_frame *frame,
252                                     __u8 *data,
253                                     int len)
254 {
255         int i, j;
256
257         PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
258
259         /* when start of a new frame, if the current frame buffer
260          * is not queued, discard the whole frame */
261         if (packet_type == FIRST_PACKET) {
262                 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
263                                                 != V4L2_BUF_FLAG_QUEUED) {
264                         gspca_dev->last_packet_type = DISCARD_PACKET;
265                         return frame;
266                 }
267                 frame->data_end = frame->data;
268                 jiffies_to_timeval(get_jiffies_64(),
269                                    &frame->v4l2_buf.timestamp);
270                 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
271         } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
272                 return frame;
273         }
274
275         /* append the packet to the frame buffer */
276         if (len > 0) {
277                 if (frame->data_end - frame->data + len
278                                                  > frame->v4l2_buf.length) {
279                         PDEBUG(D_ERR|D_PACK, "frame overflow %d > %d",
280                                 frame->data_end - frame->data + len,
281                                 frame->v4l2_buf.length);
282                         packet_type = DISCARD_PACKET;
283                 } else {
284                         if (frame->v4l2_buf.memory != V4L2_MEMORY_USERPTR)
285                                 memcpy(frame->data_end, data, len);
286                         else
287                                 copy_to_user(frame->data_end, data, len);
288                         frame->data_end += len;
289                 }
290         }
291         gspca_dev->last_packet_type = packet_type;
292
293         /* if last packet, wake the application and advance in the queue */
294         if (packet_type == LAST_PACKET) {
295                 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
296                 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
297                 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
298                 atomic_inc(&gspca_dev->nevent);
299                 wake_up_interruptible(&gspca_dev->wq);  /* event = new frame */
300                 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
301                 gspca_dev->fr_i = i;
302                 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
303                         frame->v4l2_buf.bytesused,
304                         gspca_dev->fr_q,
305                         i,
306                         gspca_dev->fr_o);
307                 j = gspca_dev->fr_queue[i];
308                 frame = &gspca_dev->frame[j];
309         }
310         return frame;
311 }
312 EXPORT_SYMBOL(gspca_frame_add);
313
314 static int gspca_is_compressed(__u32 format)
315 {
316         switch (format) {
317         case V4L2_PIX_FMT_MJPEG:
318         case V4L2_PIX_FMT_JPEG:
319         case V4L2_PIX_FMT_SPCA561:
320                 return 1;
321         }
322         return 0;
323 }
324
325 static void *rvmalloc(unsigned long size)
326 {
327         void *mem;
328         unsigned long adr;
329
330 /*      size = PAGE_ALIGN(size);        (already done) */
331         mem = vmalloc_32(size);
332         if (mem != 0) {
333                 memset(mem, 0, size);
334                 adr = (unsigned long) mem;
335                 while ((long) size > 0) {
336                         SetPageReserved(vmalloc_to_page((void *) adr));
337                         adr += PAGE_SIZE;
338                         size -= PAGE_SIZE;
339                 }
340         }
341         return mem;
342 }
343
344 static void rvfree(void *mem, unsigned long size)
345 {
346         unsigned long adr;
347
348         if (!mem)
349                 return;
350         adr = (unsigned long) mem;
351         while ((long) size > 0) {
352                 ClearPageReserved(vmalloc_to_page((void *) adr));
353                 adr += PAGE_SIZE;
354                 size -= PAGE_SIZE;
355         }
356         vfree(mem);
357 }
358
359 static __u32 get_v4l2_depth(__u32 pixfmt)
360 {
361         switch (pixfmt) {
362 /*      case V4L2_PIX_FMT_BGR32:
363         case V4L2_PIX_FMT_RGB32:
364                 return 32; */
365         case V4L2_PIX_FMT_RGB24:        /* 'RGB3' */
366         case V4L2_PIX_FMT_BGR24:
367                 return 24;
368 /*      case V4L2_PIX_FMT_RGB565:        * 'RGBP' */
369         case V4L2_PIX_FMT_YUYV:         /* 'YUYV' packed 4.2.2 */
370         case V4L2_PIX_FMT_YYUV:         /* 'YYUV' */
371                 return 16;
372         case V4L2_PIX_FMT_YUV420:       /* 'YU12' planar 4.2.0 */
373         case V4L2_PIX_FMT_SPCA501:      /* 'S501' YUYV per line */
374                 return 12;
375         case V4L2_PIX_FMT_MJPEG:
376         case V4L2_PIX_FMT_JPEG:
377         case V4L2_PIX_FMT_SBGGR8:       /* 'BA81' Bayer */
378         case V4L2_PIX_FMT_SN9C10X:      /* 'S910' SN9C10x compression */
379         case V4L2_PIX_FMT_SPCA561:      /* 'S561' compressed BGGR bayer */
380                 return 8;
381         }
382         PDEBUG(D_ERR|D_CONF, "Unknown pixel format %c%c%c%c",
383                 pixfmt & 0xff,
384                 (pixfmt >> 8) & 0xff,
385                 (pixfmt >> 16) & 0xff,
386                 pixfmt >> 24);
387         return 24;
388 }
389
390 static int gspca_get_buff_size(struct gspca_dev *gspca_dev)
391 {
392         unsigned int size;
393
394         size = gspca_dev->width * gspca_dev->height
395                                 * get_v4l2_depth(gspca_dev->pixfmt) / 8;
396         if (!size)
397                 return -ENOMEM;
398         return size;
399 }
400
401 static int frame_alloc(struct gspca_dev *gspca_dev,
402                         unsigned int count)
403 {
404         struct gspca_frame *frame;
405         unsigned int frsz;
406         int i;
407
408         frsz = gspca_get_buff_size(gspca_dev);
409         if (frsz < 0)
410                 return frsz;
411         PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
412         if (count > GSPCA_MAX_FRAMES)
413                 count = GSPCA_MAX_FRAMES;
414         /* if compressed (JPEG), reduce the buffer size */
415         if (gspca_is_compressed(gspca_dev->pixfmt))
416                 frsz = (frsz * comp_fac) / 100 + 600; /* (+ JPEG header sz) */
417         frsz = PAGE_ALIGN(frsz);
418         PDEBUG(D_STREAM, "new fr_sz: %d", frsz);
419         gspca_dev->frsz = frsz;
420         if (gspca_dev->memory == V4L2_MEMORY_MMAP) {
421                 gspca_dev->frbuf = rvmalloc(frsz * count);
422                 if (!gspca_dev->frbuf) {
423                         err("frame alloc failed");
424                         return -ENOMEM;
425                 }
426         }
427         gspca_dev->nframes = count;
428         for (i = 0; i < count; i++) {
429                 frame = &gspca_dev->frame[i];
430                 frame->v4l2_buf.index = i;
431                 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
432                 frame->v4l2_buf.flags = 0;
433                 frame->v4l2_buf.field = V4L2_FIELD_NONE;
434                 frame->v4l2_buf.length = frsz;
435                 frame->v4l2_buf.memory = gspca_dev->memory;
436                 frame->v4l2_buf.sequence = 0;
437                 if (gspca_dev->memory == V4L2_MEMORY_MMAP) {
438                         frame->data = frame->data_end =
439                                         gspca_dev->frbuf + i * frsz;
440                         frame->v4l2_buf.m.offset = i * frsz;
441                 }
442         }
443         gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
444         gspca_dev->last_packet_type = DISCARD_PACKET;
445         gspca_dev->sequence = 0;
446         atomic_set(&gspca_dev->nevent, 0);
447         return 0;
448 }
449
450 static void frame_free(struct gspca_dev *gspca_dev)
451 {
452         int i;
453
454         PDEBUG(D_STREAM, "frame free");
455         if (gspca_dev->frbuf != 0) {
456                 rvfree(gspca_dev->frbuf,
457                         gspca_dev->nframes * gspca_dev->frsz);
458                 gspca_dev->frbuf = NULL;
459                 for (i = 0; i < gspca_dev->nframes; i++)
460                         gspca_dev->frame[i].data = NULL;
461         }
462         gspca_dev->nframes = 0;
463 }
464
465 static void destroy_urbs(struct gspca_dev *gspca_dev)
466 {
467         struct urb *urb;
468         unsigned int i;
469
470         PDEBUG(D_STREAM, "kill transfer");
471         for (i = 0; i < MAX_NURBS; ++i) {
472                 urb = gspca_dev->urb[i];
473                 if (urb == NULL)
474                         break;
475
476                 gspca_dev->urb[i] = NULL;
477                 usb_kill_urb(urb);
478                 if (urb->transfer_buffer != 0)
479                         usb_buffer_free(gspca_dev->dev,
480                                         urb->transfer_buffer_length,
481                                         urb->transfer_buffer,
482                                         urb->transfer_dma);
483                 usb_free_urb(urb);
484         }
485 }
486
487 /*
488  * search an input isochronous endpoint in an alternate setting
489  */
490 static struct usb_host_endpoint *alt_isoc(struct usb_host_interface *alt,
491                                           __u8 epaddr)
492 {
493         struct usb_host_endpoint *ep;
494         int i, attr;
495
496         epaddr |= USB_DIR_IN;
497         for (i = 0; i < alt->desc.bNumEndpoints; i++) {
498                 ep = &alt->endpoint[i];
499                 if (ep->desc.bEndpointAddress == epaddr) {
500                         attr = ep->desc.bmAttributes
501                                                 & USB_ENDPOINT_XFERTYPE_MASK;
502                         if (attr == USB_ENDPOINT_XFER_ISOC)
503                                 return ep;
504                         break;
505                 }
506         }
507         return NULL;
508 }
509
510 /*
511  * search an input isochronous endpoint
512  *
513  * The endpoint is defined by the subdriver.
514  * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
515  * This routine may be called many times when the bandwidth is too small
516  * (the bandwidth is checked on urb submit).
517  */
518 struct usb_host_endpoint *get_isoc_ep(struct gspca_dev *gspca_dev)
519 {
520         struct usb_interface *intf;
521         struct usb_host_endpoint *ep;
522         int i, ret;
523
524         intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
525         ep = NULL;
526         i = gspca_dev->alt;                     /* previous alt setting */
527         while (--i > 0) {                       /* alt 0 is unusable */
528                 ep = alt_isoc(&intf->altsetting[i], gspca_dev->cam.epaddr);
529                 if (ep)
530                         break;
531         }
532         if (ep == NULL) {
533                 err("no ISOC endpoint found");
534                 return NULL;
535         }
536         PDEBUG(D_STREAM, "use ISOC alt %d ep 0x%02x",
537                         i, ep->desc.bEndpointAddress);
538         ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
539         if (ret < 0) {
540                 err("set interface err %d", ret);
541                 return NULL;
542         }
543         gspca_dev->alt = i;             /* memorize the current alt setting */
544         return ep;
545 }
546
547 /*
548  * create the isochronous URBs
549  */
550 static int create_urbs(struct gspca_dev *gspca_dev,
551                         struct usb_host_endpoint *ep)
552 {
553         struct urb *urb;
554         int n, nurbs, i, psize, npkt, bsize;
555         usb_complete_t usb_complete;
556
557         /* calculate the packet size and the number of packets */
558         psize = le16_to_cpu(ep->desc.wMaxPacketSize);
559
560         /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
561         psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
562         npkt = ISO_MAX_SIZE / psize;
563         if (npkt > ISO_MAX_PKT)
564                 npkt = ISO_MAX_PKT;
565         bsize = psize * npkt;
566         PDEBUG(D_STREAM,
567                 "isoc %d pkts size %d (bsize:%d)", npkt, psize, bsize);
568 /*fixme:change for userptr*/
569 /*fixme:don't submit all URBs when userptr*/
570         if (gspca_dev->memory == V4L2_MEMORY_MMAP) {
571                 usb_complete = isoc_irq_mmap;
572                 nurbs = DEF_NURBS;
573         } else {
574                 usb_complete = isoc_irq_user;
575                 nurbs = USR_NURBS;
576         }
577         gspca_dev->nurbs = nurbs;
578         for (n = 0; n < nurbs; n++) {
579                 urb = usb_alloc_urb(npkt, GFP_KERNEL);
580                 if (!urb) {
581                         err("usb_alloc_urb failed");
582                         return -ENOMEM;
583                 }
584                 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
585                                                 bsize,
586                                                 GFP_KERNEL,
587                                                 &urb->transfer_dma);
588
589                 if (urb->transfer_buffer == NULL) {
590                         usb_free_urb(urb);
591                         destroy_urbs(gspca_dev);
592                         err("usb_buffer_urb failed");
593                         return -ENOMEM;
594                 }
595                 gspca_dev->urb[n] = urb;
596                 urb->dev = gspca_dev->dev;
597                 urb->context = gspca_dev;
598                 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
599                                             ep->desc.bEndpointAddress);
600                 urb->transfer_flags = URB_ISO_ASAP
601                                         | URB_NO_TRANSFER_DMA_MAP;
602                 urb->interval = ep->desc.bInterval;
603                 urb->complete = usb_complete;
604                 urb->number_of_packets = npkt;
605                 urb->transfer_buffer_length = bsize;
606                 for (i = 0; i < npkt; i++) {
607                         urb->iso_frame_desc[i].length = psize;
608                         urb->iso_frame_desc[i].offset = psize * i;
609                 }
610         }
611         gspca_dev->urb_in = gspca_dev->urb_out = 0;
612         return 0;
613 }
614
615 /*
616  * start the USB transfer
617  */
618 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
619 {
620         struct usb_host_endpoint *ep;
621         int n, ret;
622
623         if (mutex_lock_interruptible(&gspca_dev->usb_lock))
624                 return -ERESTARTSYS;
625
626         /* set the higher alternate setting and
627          * loop until urb submit succeeds */
628         gspca_dev->alt = gspca_dev->nbalt;
629         for (;;) {
630                 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
631                 ep = get_isoc_ep(gspca_dev);
632                 if (ep == NULL) {
633                         ret = -EIO;
634                         goto out;
635                 }
636                 ret = create_urbs(gspca_dev, ep);
637                 if (ret < 0)
638                         goto out;
639
640                 /* start the cam */
641                 gspca_dev->sd_desc->start(gspca_dev);
642                 gspca_dev->streaming = 1;
643                 atomic_set(&gspca_dev->nevent, 0);
644
645                 /* submit the URBs */
646                 for (n = 0; n < gspca_dev->nurbs; n++) {
647                         ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
648                         if (ret < 0) {
649                                 PDEBUG(D_ERR|D_STREAM,
650                                         "usb_submit_urb [%d] err %d", n, ret);
651                                 gspca_dev->streaming = 0;
652                                 destroy_urbs(gspca_dev);
653                                 if (ret == -ENOSPC)
654                                         break;  /* try the previous alt */
655                                 goto out;
656                         }
657                 }
658                 if (ret >= 0)
659                         break;
660         }
661 out:
662         mutex_unlock(&gspca_dev->usb_lock);
663         return ret;
664 }
665
666 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
667 {
668         int ret;
669
670         ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
671         if (ret < 0)
672                 PDEBUG(D_ERR|D_STREAM, "set interface 0 err %d", ret);
673         return ret;
674 }
675
676 /* Note both the queue and the usb lock should be hold when calling this */
677 static void gspca_stream_off(struct gspca_dev *gspca_dev)
678 {
679         gspca_dev->streaming = 0;
680         atomic_set(&gspca_dev->nevent, 0);
681         if (gspca_dev->present) {
682                 gspca_dev->sd_desc->stopN(gspca_dev);
683                 destroy_urbs(gspca_dev);
684                 gspca_set_alt0(gspca_dev);
685                 gspca_dev->sd_desc->stop0(gspca_dev);
686                 PDEBUG(D_STREAM, "stream off OK");
687         } else {
688                 destroy_urbs(gspca_dev);
689                 atomic_inc(&gspca_dev->nevent);
690                 wake_up_interruptible(&gspca_dev->wq);
691                 PDEBUG(D_ERR|D_STREAM, "stream off no device ??");
692         }
693 }
694
695 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
696 {
697         int i;
698
699         i = gspca_dev->cam.nmodes - 1;  /* take the highest mode */
700         gspca_dev->curr_mode = i;
701         gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
702         gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
703         gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixfmt;
704 }
705
706 static int wxh_to_mode(struct gspca_dev *gspca_dev,
707                         int width, int height)
708 {
709         int i;
710
711         for (i = gspca_dev->cam.nmodes; --i > 0; ) {
712                 if (width >= gspca_dev->cam.cam_mode[i].width
713                     && height >= gspca_dev->cam.cam_mode[i].height)
714                         break;
715         }
716         return i;
717 }
718
719 /*
720  * search a mode with the right pixel format
721  */
722 static int gspca_get_mode(struct gspca_dev *gspca_dev,
723                         int mode,
724                         int pixfmt)
725 {
726         int modeU, modeD;
727
728         modeU = modeD = mode;
729         while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
730                 if (--modeD >= 0) {
731                         if (gspca_dev->cam.cam_mode[modeD].pixfmt == pixfmt)
732                                 return modeD;
733                 }
734                 if (++modeU < gspca_dev->cam.nmodes) {
735                         if (gspca_dev->cam.cam_mode[modeU].pixfmt == pixfmt)
736                                 return modeU;
737                 }
738         }
739         return -EINVAL;
740 }
741
742 static int vidioc_enum_fmt_cap(struct file *file, void  *priv,
743                                 struct v4l2_fmtdesc *fmtdesc)
744 {
745         struct gspca_dev *gspca_dev = priv;
746         int i, j, index;
747         __u32 fmt_tb[8];
748
749         PDEBUG(D_CONF, "enum fmt cap");
750
751         /* give an index to each format */
752         index = 0;
753         j = 0;
754         for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
755                 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixfmt;
756                 j = 0;
757                 for (;;) {
758                         if (fmt_tb[j] == fmt_tb[index])
759                                 break;
760                         j++;
761                 }
762                 if (j == index) {
763                         if (fmtdesc->index == index)
764                                 break;          /* new format */
765                         index++;
766                         if (index >= sizeof fmt_tb / sizeof fmt_tb[0])
767                                 return -EINVAL;
768                 }
769         }
770         if (i < 0)
771                 return -EINVAL;         /* no more format */
772
773         fmtdesc->pixelformat = fmt_tb[index];
774         if (gspca_is_compressed(fmt_tb[index]))
775                 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
776         fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
777         fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
778         fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
779         fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
780         fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
781         fmtdesc->description[4] = '\0';
782         return 0;
783 }
784
785 static int vidioc_g_fmt_cap(struct file *file, void *priv,
786                             struct v4l2_format *fmt)
787 {
788         struct gspca_dev *gspca_dev = priv;
789
790         if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
791                 return -EINVAL;
792         fmt->fmt.pix.width = gspca_dev->width;
793         fmt->fmt.pix.height = gspca_dev->height;
794         fmt->fmt.pix.pixelformat = gspca_dev->pixfmt;
795 #ifdef VIDEO_ADV_DEBUG
796         if (gspca_debug & D_CONF) {
797                 PDEBUG_MODE("get fmt cap",
798                         fmt->fmt.pix.pixelformat,
799                         fmt->fmt.pix.width,
800                         fmt->fmt.pix.height);
801         }
802 #endif
803         fmt->fmt.pix.field = V4L2_FIELD_NONE;
804         fmt->fmt.pix.bytesperline = get_v4l2_depth(fmt->fmt.pix.pixelformat)
805                                         * fmt->fmt.pix.width / 8;
806         fmt->fmt.pix.sizeimage = fmt->fmt.pix.bytesperline
807                                         * fmt->fmt.pix.height;
808 /* (should be in the subdriver) */
809         fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
810         fmt->fmt.pix.priv = 0;
811         return 0;
812 }
813
814 static int try_fmt_cap(struct gspca_dev *gspca_dev,
815                         struct v4l2_format *fmt)
816 {
817         int w, h, mode, mode2, frsz;
818
819         if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
820                 return -EINVAL;
821         w = fmt->fmt.pix.width;
822         h = fmt->fmt.pix.height;
823
824         /* (luvcview problem) */
825         if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
826                 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG;
827 #ifdef VIDEO_ADV_DEBUG
828         if (gspca_debug & D_CONF)
829                 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
830 #endif
831         /* search the closest mode for width and height */
832         mode = wxh_to_mode(gspca_dev, w, h);
833
834         /* OK if right palette */
835         if (gspca_dev->cam.cam_mode[mode].pixfmt != fmt->fmt.pix.pixelformat) {
836
837                 /* else, search the closest mode with the same pixel format */
838                 mode2 = gspca_get_mode(gspca_dev, mode,
839                                         fmt->fmt.pix.pixelformat);
840                 if (mode2 >= 0) {
841                         mode = mode2;
842                 } else {
843
844                         /* no chance, return this mode */
845                         fmt->fmt.pix.pixelformat =
846                                         gspca_dev->cam.cam_mode[mode].pixfmt;
847 #ifdef VIDEO_ADV_DEBUG
848                         if (gspca_debug & D_CONF) {
849                                 PDEBUG_MODE("new format",
850                                         fmt->fmt.pix.pixelformat,
851                                         gspca_dev->cam.cam_mode[mode].width,
852                                         gspca_dev->cam.cam_mode[mode].height);
853                         }
854 #endif
855                 }
856         }
857         fmt->fmt.pix.width = gspca_dev->cam.cam_mode[mode].width;
858         fmt->fmt.pix.height = gspca_dev->cam.cam_mode[mode].height;
859         fmt->fmt.pix.bytesperline = get_v4l2_depth(fmt->fmt.pix.pixelformat)
860                                         * fmt->fmt.pix.width / 8;
861         frsz = fmt->fmt.pix.bytesperline * fmt->fmt.pix.height;
862         if (gspca_is_compressed(fmt->fmt.pix.pixelformat))
863                 frsz = (frsz * comp_fac) / 100;
864         fmt->fmt.pix.sizeimage = frsz;
865         return mode;                    /* used when s_fmt */
866 }
867
868 static int vidioc_try_fmt_cap(struct file *file,
869                               void *priv,
870                               struct v4l2_format *fmt)
871 {
872         struct gspca_dev *gspca_dev = priv;
873         int ret;
874
875         ret = try_fmt_cap(gspca_dev, fmt);
876         if (ret < 0)
877                 return ret;
878         return 0;
879 }
880
881 static int vidioc_s_fmt_cap(struct file *file, void *priv,
882                             struct v4l2_format *fmt)
883 {
884         struct gspca_dev *gspca_dev = priv;
885         int ret;
886
887 #ifdef CONFIG_VIDEO_V4L1_COMPAT
888         /* if v4l1 got JPEG */
889         if (fmt->fmt.pix.pixelformat == 0
890             && gspca_dev->streaming) {
891                 fmt->fmt.pix.width = gspca_dev->width;
892                 fmt->fmt.pix.height = gspca_dev->height;
893                 fmt->fmt.pix.pixelformat = gspca_dev->pixfmt;
894                 return 0;
895         }
896 #endif
897 #ifdef VIDEO_ADV_DEBUG
898         if (gspca_debug & D_CONF) {
899                 PDEBUG_MODE("set fmt cap",
900                         fmt->fmt.pix.pixelformat,
901                         fmt->fmt.pix.width, fmt->fmt.pix.height);
902         }
903 #endif
904         if (mutex_lock_interruptible(&gspca_dev->queue_lock))
905                 return -ERESTARTSYS;
906
907         ret = try_fmt_cap(gspca_dev, fmt);
908         if (ret < 0)
909                 goto out;
910
911         if (gspca_dev->nframes != 0
912             && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
913                 ret = -EINVAL;
914                 goto out;
915         }
916
917         if (ret == gspca_dev->curr_mode) {
918                 ret = 0;
919                 goto out;                       /* same mode */
920         }
921
922         if (gspca_dev->streaming) {
923                 ret = -EBUSY;
924                 goto out;
925         }
926         gspca_dev->width = fmt->fmt.pix.width;
927         gspca_dev->height = fmt->fmt.pix.height;
928         gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
929         gspca_dev->curr_mode = ret;
930
931         ret = 0;
932 out:
933         mutex_unlock(&gspca_dev->queue_lock);
934         return ret;
935 }
936
937 static int dev_open(struct inode *inode, struct file *file)
938 {
939         struct gspca_dev *gspca_dev;
940         int ret;
941
942         PDEBUG(D_STREAM, "%s open", current->comm);
943         gspca_dev = (struct gspca_dev *) video_devdata(file);
944         if (mutex_lock_interruptible(&gspca_dev->queue_lock))
945                 return -ERESTARTSYS;
946         if (!gspca_dev->present) {
947                 ret = -ENODEV;
948                 goto out;
949         }
950
951         /* if not done yet, initialize the sensor */
952         if (gspca_dev->users == 0) {
953                 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
954                         ret = -ERESTARTSYS;
955                         goto out;
956                 }
957                 ret = gspca_dev->sd_desc->open(gspca_dev);
958                 mutex_unlock(&gspca_dev->usb_lock);
959                 if (ret != 0) {
960                         PDEBUG(D_ERR|D_CONF, "init device failed %d", ret);
961                         goto out;
962                 }
963         } else if (gspca_dev->users > 4) {      /* (arbitrary value) */
964                 ret = -EBUSY;
965                 goto out;
966         }
967         gspca_dev->users++;
968         file->private_data = gspca_dev;
969 #ifdef VIDEO_ADV_DEBUG
970         /* activate the v4l2 debug */
971         if (gspca_debug & D_V4L2)
972                 gspca_dev->vdev.debug |= 3;
973         else
974                 gspca_dev->vdev.debug &= ~3;
975 #endif
976 out:
977         mutex_unlock(&gspca_dev->queue_lock);
978         if (ret != 0)
979                 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
980         else
981                 PDEBUG(D_STREAM, "open done");
982         return ret;
983 }
984
985 static int dev_close(struct inode *inode, struct file *file)
986 {
987         struct gspca_dev *gspca_dev = file->private_data;
988
989         PDEBUG(D_STREAM, "%s close", current->comm);
990         if (mutex_lock_interruptible(&gspca_dev->queue_lock))
991                 return -ERESTARTSYS;
992         gspca_dev->users--;
993
994         /* if the file did capture, free the streaming resources */
995         if (gspca_dev->capt_file == file) {
996                 mutex_lock(&gspca_dev->usb_lock);
997                 if (gspca_dev->streaming)
998                         gspca_stream_off(gspca_dev);
999                 gspca_dev->sd_desc->close(gspca_dev);
1000                 mutex_unlock(&gspca_dev->usb_lock);
1001                 frame_free(gspca_dev);
1002                 gspca_dev->capt_file = 0;
1003                 gspca_dev->memory = GSPCA_MEMORY_NO;
1004         }
1005         file->private_data = NULL;
1006         mutex_unlock(&gspca_dev->queue_lock);
1007         PDEBUG(D_STREAM, "close done");
1008         return 0;
1009 }
1010
1011 static int vidioc_querycap(struct file *file, void  *priv,
1012                            struct v4l2_capability *cap)
1013 {
1014         struct gspca_dev *gspca_dev = priv;
1015
1016         PDEBUG(D_CONF, "querycap");
1017         memset(cap, 0, sizeof *cap);
1018         strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
1019         strncpy(cap->card, gspca_dev->cam.dev_name, sizeof cap->card);
1020         strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name,
1021                 sizeof cap->bus_info);
1022         cap->version = DRIVER_VERSION_NUMBER;
1023         cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1024                           | V4L2_CAP_STREAMING
1025                           | V4L2_CAP_READWRITE;
1026         return 0;
1027 }
1028
1029 /* the use of V4L2_CTRL_FLAG_NEXT_CTRL asks for the controls to be sorted */
1030 static int vidioc_queryctrl(struct file *file, void *priv,
1031                            struct v4l2_queryctrl *q_ctrl)
1032 {
1033         struct gspca_dev *gspca_dev = priv;
1034         int i;
1035         u32 id;
1036
1037         id = q_ctrl->id;
1038         if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1039                 id &= V4L2_CTRL_ID_MASK;
1040                 id++;
1041                 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1042                         if (id >= gspca_dev->sd_desc->ctrls[i].qctrl.id) {
1043                                 memcpy(q_ctrl,
1044                                         &gspca_dev->sd_desc->ctrls[i].qctrl,
1045                                         sizeof *q_ctrl);
1046                                 return 0;
1047                         }
1048                 }
1049                 return -EINVAL;
1050         }
1051         for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1052                 if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) {
1053                         memcpy(q_ctrl,
1054                                 &gspca_dev->sd_desc->ctrls[i].qctrl,
1055                                 sizeof *q_ctrl);
1056                         return 0;
1057                 }
1058         }
1059         if (id >= V4L2_CID_BASE
1060             && id <= V4L2_CID_LASTP1) {
1061                 q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
1062                 return 0;
1063         }
1064         return -EINVAL;
1065 }
1066
1067 static int vidioc_s_ctrl(struct file *file, void *priv,
1068                          struct v4l2_control *ctrl)
1069 {
1070         struct gspca_dev *gspca_dev = priv;
1071         struct ctrl *ctrls;
1072         int i, ret;
1073
1074         PDEBUG(D_CONF, "set ctrl");
1075         for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1076              i < gspca_dev->sd_desc->nctrls;
1077              i++, ctrls++) {
1078                 if (ctrl->id != ctrls->qctrl.id)
1079                         continue;
1080                 if (ctrl->value < ctrls->qctrl.minimum
1081                     && ctrl->value > ctrls->qctrl.maximum)
1082                         return -ERANGE;
1083                 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1084                 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1085                         return -ERESTARTSYS;
1086                 ret = ctrls->set(gspca_dev, ctrl->value);
1087                 mutex_unlock(&gspca_dev->usb_lock);
1088                 return ret;
1089         }
1090         return -EINVAL;
1091 }
1092
1093 static int vidioc_g_ctrl(struct file *file, void *priv,
1094                          struct v4l2_control *ctrl)
1095 {
1096         struct gspca_dev *gspca_dev = priv;
1097
1098         struct ctrl *ctrls;
1099         int i, ret;
1100
1101         for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1102              i < gspca_dev->sd_desc->nctrls;
1103              i++, ctrls++) {
1104                 if (ctrl->id != ctrls->qctrl.id)
1105                         continue;
1106                 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1107                         return -ERESTARTSYS;
1108                 ret = ctrls->get(gspca_dev, &ctrl->value);
1109                 mutex_unlock(&gspca_dev->usb_lock);
1110                 return ret;
1111         }
1112         return -EINVAL;
1113 }
1114
1115 static int vidioc_querymenu(struct file *file, void *priv,
1116                             struct v4l2_querymenu *qmenu)
1117 {
1118         struct gspca_dev *gspca_dev = priv;
1119
1120         if (!gspca_dev->sd_desc->querymenu)
1121                 return -EINVAL;
1122         return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1123 }
1124
1125 static int vidioc_enum_input(struct file *file, void *priv,
1126                                 struct v4l2_input *input)
1127 {
1128         struct gspca_dev *gspca_dev = priv;
1129
1130         if (input->index != 0)
1131                 return -EINVAL;
1132         memset(input, 0, sizeof *input);
1133         input->type = V4L2_INPUT_TYPE_CAMERA;
1134         strncpy(input->name, gspca_dev->sd_desc->name,
1135                 sizeof input->name);
1136         return 0;
1137 }
1138
1139 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1140 {
1141         *i = 0;
1142         return 0;
1143 }
1144
1145 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1146 {
1147         if (i > 0)
1148                 return -EINVAL;
1149         return (0);
1150 }
1151
1152 static int vidioc_reqbufs(struct file *file, void *priv,
1153                           struct v4l2_requestbuffers *rb)
1154 {
1155         struct gspca_dev *gspca_dev = priv;
1156         int i, ret = 0;
1157
1158         PDEBUG(D_STREAM, "reqbufs %d", rb->count);
1159         if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1160                 return -EINVAL;
1161         switch (rb->memory) {
1162         case V4L2_MEMORY_MMAP:
1163         case V4L2_MEMORY_USERPTR:
1164                 break;
1165         default:
1166                 return -EINVAL;
1167         }
1168         if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1169                 return -ERESTARTSYS;
1170
1171         for (i = 0; i < gspca_dev->nframes; i++) {
1172                 if (gspca_dev->frame[i].vma_use_count) {
1173                         ret = -EBUSY;
1174                         goto out;
1175                 }
1176         }
1177
1178         /* only one file may do capture */
1179         if ((gspca_dev->capt_file != 0 && gspca_dev->capt_file != file)
1180             || gspca_dev->streaming) {
1181                 ret = -EBUSY;
1182                 goto out;
1183         }
1184
1185         if (rb->count == 0) { /* unrequest? */
1186                 frame_free(gspca_dev);
1187                 gspca_dev->capt_file = 0;
1188         } else {
1189                 gspca_dev->memory = rb->memory;
1190                 ret = frame_alloc(gspca_dev, rb->count);
1191                 if (ret == 0) {
1192                         rb->count = gspca_dev->nframes;
1193                         gspca_dev->capt_file = file;
1194                 }
1195         }
1196 out:
1197         mutex_unlock(&gspca_dev->queue_lock);
1198         PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1199         return ret;
1200 }
1201
1202 static int vidioc_querybuf(struct file *file, void *priv,
1203                            struct v4l2_buffer *v4l2_buf)
1204 {
1205         struct gspca_dev *gspca_dev = priv;
1206         struct gspca_frame *frame;
1207
1208         PDEBUG(D_STREAM, "querybuf");
1209         if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1210             || v4l2_buf->index < 0
1211             || v4l2_buf->index >= gspca_dev->nframes)
1212                 return -EINVAL;
1213
1214         frame = &gspca_dev->frame[v4l2_buf->index];
1215         memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1216         return 0;
1217 }
1218
1219 static int vidioc_streamon(struct file *file, void *priv,
1220                            enum v4l2_buf_type buf_type)
1221 {
1222         struct gspca_dev *gspca_dev = priv;
1223         int ret;
1224
1225         PDEBUG(D_STREAM, "stream on");
1226         if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1227                 return -EINVAL;
1228         if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1229                 return -ERESTARTSYS;
1230         if (!gspca_dev->present) {
1231                 ret = -ENODEV;
1232                 goto out;
1233         }
1234         if (gspca_dev->nframes == 0) {
1235                 ret = -EINVAL;
1236                 goto out;
1237         }
1238         if (gspca_dev->capt_file != file) {
1239                 ret = -EINVAL;
1240                 goto out;
1241         }
1242         if (!gspca_dev->streaming) {
1243                 ret = gspca_init_transfer(gspca_dev);
1244                 if (ret < 0)
1245                         goto out;
1246         }
1247 #ifdef VIDEO_ADV_DEBUG
1248         if (gspca_debug & D_STREAM) {
1249                 PDEBUG_MODE("stream on OK",
1250                         gspca_dev->pixfmt,
1251                         gspca_dev->width,
1252                         gspca_dev->height);
1253         }
1254 #endif
1255         ret = 0;
1256 out:
1257         mutex_unlock(&gspca_dev->queue_lock);
1258         return ret;
1259 }
1260
1261 static int vidioc_streamoff(struct file *file, void *priv,
1262                                 enum v4l2_buf_type buf_type)
1263 {
1264         struct gspca_dev *gspca_dev = priv;
1265         int ret;
1266
1267         PDEBUG(D_STREAM, "stream off");
1268         if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1269                 return -EINVAL;
1270         if (!gspca_dev->streaming)
1271                 return 0;
1272         if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1273                 return -ERESTARTSYS;
1274         if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1275                 ret = -ERESTARTSYS;
1276                 goto out;
1277         }
1278         if (gspca_dev->capt_file != file) {
1279                 ret = -EINVAL;
1280                 goto out2;
1281         }
1282         gspca_stream_off(gspca_dev);
1283         ret = 0;
1284 out2:
1285         mutex_unlock(&gspca_dev->usb_lock);
1286 out:
1287         mutex_unlock(&gspca_dev->queue_lock);
1288         return ret;
1289 }
1290
1291 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1292                         struct v4l2_jpegcompression *jpegcomp)
1293 {
1294         struct gspca_dev *gspca_dev = priv;
1295         int ret;
1296
1297         if (!gspca_dev->sd_desc->get_jcomp)
1298                 return -EINVAL;
1299         if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1300                 return -ERESTARTSYS;
1301         ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1302         mutex_unlock(&gspca_dev->usb_lock);
1303         return ret;
1304 }
1305
1306 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1307                         struct v4l2_jpegcompression *jpegcomp)
1308 {
1309         struct gspca_dev *gspca_dev = priv;
1310         int ret;
1311
1312         if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1313                 return -ERESTARTSYS;
1314         if (!gspca_dev->sd_desc->set_jcomp)
1315                 return -EINVAL;
1316         ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1317         mutex_unlock(&gspca_dev->usb_lock);
1318         return ret;
1319 }
1320
1321 static int vidioc_g_parm(struct file *filp, void *priv,
1322                         struct v4l2_streamparm *parm)
1323 {
1324         struct gspca_dev *gspca_dev = priv;
1325
1326         memset(parm, 0, sizeof *parm);
1327         parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1328         parm->parm.capture.readbuffers = gspca_dev->nbufread;
1329         return 0;
1330 }
1331
1332 static int vidioc_s_parm(struct file *filp, void *priv,
1333                         struct v4l2_streamparm *parm)
1334 {
1335         struct gspca_dev *gspca_dev = priv;
1336         int n;
1337
1338         n = parm->parm.capture.readbuffers;
1339         if (n == 0 || n > GSPCA_MAX_FRAMES)
1340                 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1341         else
1342                 gspca_dev->nbufread = n;
1343         return 0;
1344 }
1345
1346 static int vidioc_s_std(struct file *filp, void *priv,
1347                         v4l2_std_id *parm)
1348 {
1349         return 0;
1350 }
1351
1352 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1353 static int vidiocgmbuf(struct file *file, void *priv,
1354                         struct video_mbuf *mbuf)
1355 {
1356         struct gspca_dev *gspca_dev = file->private_data;
1357         int i;
1358
1359         PDEBUG(D_STREAM, "cgmbuf");
1360         if (gspca_dev->nframes == 0) {
1361                 int ret;
1362
1363                 {
1364                         struct v4l2_format fmt;
1365
1366                         memset(&fmt, 0, sizeof fmt);
1367                         fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1368                         i = gspca_dev->cam.nmodes - 1;  /* highest mode */
1369                         fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1370                         fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1371                         fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
1372                         ret = vidioc_s_fmt_cap(file, priv, &fmt);
1373                         if (ret != 0)
1374                                 return ret;
1375                 }
1376                 {
1377                         struct v4l2_requestbuffers rb;
1378
1379                         memset(&rb, 0, sizeof rb);
1380                         rb.count = 4;
1381                         rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1382                         rb.memory = V4L2_MEMORY_MMAP;
1383                         ret = vidioc_reqbufs(file, priv, &rb);
1384                         if (ret != 0)
1385                                 return ret;
1386                 }
1387         }
1388         mbuf->frames = gspca_dev->nframes;
1389         mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1390         for (i = 0; i < mbuf->frames; i++)
1391                 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1392         return 0;
1393 }
1394 #endif
1395
1396 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1397 {
1398         struct gspca_dev *gspca_dev = file->private_data;
1399         struct gspca_frame *frame = 0;
1400         struct page *page;
1401         unsigned long addr, start, size;
1402         int i, ret;
1403 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1404         int compat = 0;
1405 #endif
1406
1407         start = vma->vm_start;
1408         size = vma->vm_end - vma->vm_start;
1409         PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1410
1411         if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1412                 return -ERESTARTSYS;
1413         if (!gspca_dev->present) {
1414                 ret = -ENODEV;
1415                 goto out;
1416         }
1417         if (gspca_dev->capt_file != file) {
1418                 ret = -EINVAL;
1419                 goto out;
1420         }
1421
1422         for (i = 0; i < gspca_dev->nframes; ++i) {
1423                 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1424                         PDEBUG(D_STREAM, "mmap bad memory type");
1425                         break;
1426                 }
1427                 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1428                                                 == vma->vm_pgoff) {
1429                         frame = &gspca_dev->frame[i];
1430                         break;
1431                 }
1432         }
1433         if (frame == 0) {
1434                 PDEBUG(D_STREAM, "mmap no frame buffer found");
1435                 ret = -EINVAL;
1436                 goto out;
1437         }
1438 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1439         if (i == 0 && size == frame->v4l2_buf.length * gspca_dev->nframes)
1440                 compat = 1;
1441         else
1442 #endif
1443         if (size != frame->v4l2_buf.length) {
1444                 PDEBUG(D_STREAM, "mmap bad size");
1445                 ret = -EINVAL;
1446                 goto out;
1447         }
1448
1449         /*
1450          * - VM_IO marks the area as being a mmaped region for I/O to a
1451          *   device. It also prevents the region from being core dumped.
1452          */
1453         vma->vm_flags |= VM_IO;
1454
1455         addr = (unsigned long) frame->data;
1456         while (size > 0) {
1457                 page = vmalloc_to_page((void *) addr);
1458                 ret = vm_insert_page(vma, start, page);
1459                 if (ret < 0)
1460                         goto out;
1461                 start += PAGE_SIZE;
1462                 addr += PAGE_SIZE;
1463                 size -= PAGE_SIZE;
1464         }
1465
1466         vma->vm_ops = &gspca_vm_ops;
1467         vma->vm_private_data = frame;
1468         gspca_vm_open(vma);
1469 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1470         if (compat) {
1471 /*fixme: ugly*/
1472                 for (i = 1; i < gspca_dev->nframes; ++i)
1473                         gspca_dev->frame[i].v4l2_buf.flags |=
1474                                                 V4L2_BUF_FLAG_MAPPED;
1475         }
1476 #endif
1477         ret = 0;
1478 out:
1479         mutex_unlock(&gspca_dev->queue_lock);
1480         return ret;
1481 }
1482
1483 /*
1484  * wait for a video frame
1485  *
1486  * If a frame is ready, its index is returned.
1487  */
1488 static int frame_wait(struct gspca_dev *gspca_dev,
1489                         int nonblock_ing)
1490 {
1491         struct gspca_frame *frame;
1492         int i, j, ret;
1493
1494         /* if userptr, treat the awaiting URBs */
1495         if (gspca_dev->memory == V4L2_MEMORY_USERPTR)
1496                 isoc_transfer(gspca_dev);
1497
1498         /* check if a frame is ready */
1499         i = gspca_dev->fr_o;
1500         j = gspca_dev->fr_queue[i];
1501         frame = &gspca_dev->frame[j];
1502         if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1503                 goto ok;
1504         if (nonblock_ing)                       /* no frame yet */
1505                 return -EAGAIN;
1506
1507         /* wait till a frame is ready */
1508         for (;;) {
1509                 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1510                                         atomic_read(&gspca_dev->nevent) > 0,
1511                                         msecs_to_jiffies(3000));
1512                 if (ret <= 0) {
1513                         if (ret < 0)
1514                                 return ret;
1515                         return -EIO;
1516                 }
1517                 if (!gspca_dev->streaming || !gspca_dev->present)
1518                         return -EIO;
1519                 if (gspca_dev->memory == V4L2_MEMORY_USERPTR)
1520                         isoc_transfer(gspca_dev);
1521                 i = gspca_dev->fr_o;
1522                 j = gspca_dev->fr_queue[i];
1523                 frame = &gspca_dev->frame[j];
1524                 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1525                         break;
1526         }
1527 ok:
1528         atomic_dec(&gspca_dev->nevent);
1529         gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1530         PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1531                 gspca_dev->fr_q,
1532                 gspca_dev->fr_i,
1533                 gspca_dev->fr_o);
1534
1535         if (gspca_dev->sd_desc->dq_callback)
1536                 gspca_dev->sd_desc->dq_callback(gspca_dev);
1537
1538         return j;
1539 }
1540
1541 /*
1542  * dequeue a video buffer
1543  *
1544  * If nonblock_ing is false, block until a buffer is available.
1545  */
1546 static int vidioc_dqbuf(struct file *file, void *priv,
1547                         struct v4l2_buffer *v4l2_buf)
1548 {
1549         struct gspca_dev *gspca_dev = priv;
1550         struct gspca_frame *frame;
1551         int i, ret;
1552
1553         PDEBUG(D_FRAM, "dqbuf");
1554         if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1555             || (v4l2_buf->memory != V4L2_MEMORY_MMAP
1556                 && v4l2_buf->memory != V4L2_MEMORY_USERPTR))
1557                 return -EINVAL;
1558         if (!gspca_dev->streaming)
1559                 return -EINVAL;
1560         if (gspca_dev->capt_file != file) {
1561                 ret = -EINVAL;
1562                 goto out;
1563         }
1564
1565         /* only one read */
1566         if (mutex_lock_interruptible(&gspca_dev->read_lock))
1567                 return -ERESTARTSYS;
1568
1569         ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
1570         if (ret < 0)
1571                 goto out;
1572         i = ret;                                /* frame index */
1573         frame = &gspca_dev->frame[i];
1574         frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1575         memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1576         PDEBUG(D_FRAM, "dqbuf %d", i);
1577         ret = 0;
1578 out:
1579         mutex_unlock(&gspca_dev->read_lock);
1580         return ret;
1581 }
1582
1583 /*
1584  * queue a video buffer
1585  *
1586  * Attempting to queue a buffer that has already been
1587  * queued will return -EINVAL.
1588  */
1589 static int vidioc_qbuf(struct file *file, void *priv,
1590                         struct v4l2_buffer *v4l2_buf)
1591 {
1592         struct gspca_dev *gspca_dev = priv;
1593         struct gspca_frame *frame;
1594         int i, index, ret;
1595
1596         PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1597         if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1598                 return -EINVAL;
1599
1600         index = v4l2_buf->index;
1601         if ((unsigned) index >= gspca_dev->nframes) {
1602                 PDEBUG(D_FRAM,
1603                         "qbuf idx %d >= %d", index, gspca_dev->nframes);
1604                 return -EINVAL;
1605         }
1606         frame = &gspca_dev->frame[index];
1607
1608         if (v4l2_buf->memory != frame->v4l2_buf.memory) {
1609                 PDEBUG(D_FRAM, "qbuf bad memory type");
1610                 return -EINVAL;
1611         }
1612         if (gspca_dev->capt_file != file)
1613                 return -EINVAL;
1614
1615         if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1616                 return -ERESTARTSYS;
1617
1618         if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1619                 PDEBUG(D_FRAM, "qbuf bad state");
1620                 ret = -EINVAL;
1621                 goto out;
1622         }
1623
1624         frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
1625 /*      frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; */
1626
1627         if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
1628                 frame->data = frame->data_end =
1629                                 (__u8 *) v4l2_buf->m.userptr;
1630                 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1631                 frame->v4l2_buf.length = v4l2_buf->length;
1632         }
1633
1634         /* put the buffer in the 'queued' queue */
1635         i = gspca_dev->fr_q;
1636         gspca_dev->fr_queue[i] = index;
1637         gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1638         PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1639                 gspca_dev->fr_q,
1640                 gspca_dev->fr_i,
1641                 gspca_dev->fr_o);
1642
1643         v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1644         v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1645         ret = 0;
1646 out:
1647         mutex_unlock(&gspca_dev->queue_lock);
1648         return ret;
1649 }
1650
1651 /*
1652  * allocate the resources for read()
1653  */
1654 static int read_alloc(struct gspca_dev *gspca_dev,
1655                         struct file *file)
1656 {
1657         struct v4l2_buffer v4l2_buf;
1658         int i, ret;
1659
1660         PDEBUG(D_STREAM, "read alloc");
1661         if (gspca_dev->nframes == 0) {
1662                 struct v4l2_requestbuffers rb;
1663
1664                 memset(&rb, 0, sizeof rb);
1665                 rb.count = gspca_dev->nbufread;
1666                 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1667                 rb.memory = V4L2_MEMORY_MMAP;
1668                 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1669                 if (ret != 0) {
1670                         PDEBUG(D_STREAM, "read reqbuf err %d", ret);
1671                         return ret;
1672                 }
1673                 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1674                 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1675                 v4l2_buf.memory = V4L2_MEMORY_MMAP;
1676                 for (i = 0; i < gspca_dev->nbufread; i++) {
1677                         v4l2_buf.index = i;
1678 /*fixme: ugly!*/
1679                         gspca_dev->frame[i].v4l2_buf.flags |=
1680                                                         V4L2_BUF_FLAG_MAPPED;
1681                         ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1682                         if (ret != 0) {
1683                                 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1684                                 return ret;
1685                         }
1686                 }
1687                 gspca_dev->memory = GSPCA_MEMORY_READ;
1688         }
1689
1690         /* start streaming */
1691         ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1692         if (ret != 0)
1693                 PDEBUG(D_STREAM, "read streamon err %d", ret);
1694         return ret;
1695 }
1696
1697 static unsigned int dev_poll(struct file *file, poll_table *wait)
1698 {
1699         struct gspca_dev *gspca_dev = file->private_data;
1700         int i, ret;
1701
1702         PDEBUG(D_FRAM, "poll");
1703
1704         poll_wait(file, &gspca_dev->wq, wait);
1705         if (!gspca_dev->present)
1706                 return POLLERR;
1707
1708         /* if not streaming, the user would use read() */
1709         if (!gspca_dev->streaming) {
1710                 if (gspca_dev->memory != GSPCA_MEMORY_NO) {
1711                         ret = POLLERR;          /* not the 1st time */
1712                         goto out;
1713                 }
1714                 ret = read_alloc(gspca_dev, file);
1715                 if (ret != 0) {
1716                         ret = POLLERR;
1717                         goto out;
1718                 }
1719         }
1720
1721         if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1722                 return POLLERR;
1723         if (!gspca_dev->present) {
1724                 ret = POLLERR;
1725                 goto out;
1726         }
1727
1728         /* if not mmap, treat the awaiting URBs */
1729         if (gspca_dev->memory == V4L2_MEMORY_USERPTR
1730             && gspca_dev->capt_file == file)
1731                 isoc_transfer(gspca_dev);
1732
1733         i = gspca_dev->fr_o;
1734         i = gspca_dev->fr_queue[i];
1735         if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1736                 ret = POLLIN | POLLRDNORM;      /* something to read */
1737         else
1738                 ret = 0;
1739 out:
1740         mutex_unlock(&gspca_dev->queue_lock);
1741         return ret;
1742 }
1743
1744 static ssize_t dev_read(struct file *file, char __user *data,
1745                     size_t count, loff_t *ppos)
1746 {
1747         struct gspca_dev *gspca_dev = file->private_data;
1748         struct gspca_frame *frame;
1749         struct v4l2_buffer v4l2_buf;
1750         struct timeval timestamp;
1751         int i, ret, ret2;
1752
1753         PDEBUG(D_FRAM, "read (%d)", count);
1754         if (!gspca_dev->present)
1755                 return -ENODEV;
1756         switch (gspca_dev->memory) {
1757         case GSPCA_MEMORY_NO:                   /* first time */
1758                 ret = read_alloc(gspca_dev, file);
1759                 if (ret != 0)
1760                         return ret;
1761                 break;
1762         case GSPCA_MEMORY_READ:
1763                 if (gspca_dev->capt_file != file)
1764                         return -EINVAL;
1765                 break;
1766         default:
1767                 return -EINVAL;
1768         }
1769
1770         /* get a frame */
1771         jiffies_to_timeval(get_jiffies_64(), &timestamp);
1772         timestamp.tv_sec--;
1773         for (i = 0; i < 2; i++) {
1774                 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1775                 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1776                 v4l2_buf.memory = V4L2_MEMORY_MMAP;
1777                 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1778                 if (ret != 0) {
1779                         PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1780                         return ret;
1781                 }
1782
1783                 /* if the process slept for more than 1 second,
1784                  * get a brand new frame */
1785                 frame = &gspca_dev->frame[v4l2_buf.index];
1786                 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1787                         break;
1788                 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1789                 if (ret != 0) {
1790                         PDEBUG(D_STREAM, "read qbuf err %d", ret);
1791                         return ret;
1792                 }
1793         }
1794
1795         /* copy the frame */
1796         if (count < frame->v4l2_buf.bytesused) {
1797                 PDEBUG(D_STREAM, "read bad count: %d < %d",
1798                         count, frame->v4l2_buf.bytesused);
1799 /*fixme: special errno?*/
1800                 ret = -EINVAL;
1801                 goto out;
1802         }
1803         count = frame->v4l2_buf.bytesused;
1804         ret = copy_to_user(data, frame->data, count);
1805         if (ret != 0) {
1806                 PDEBUG(D_ERR|D_STREAM,
1807                         "read cp to user lack %d / %d", ret, count);
1808                 ret = -EFAULT;
1809                 goto out;
1810         }
1811         ret = count;
1812 out:
1813         /* in each case, requeue the buffer */
1814         ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1815         if (ret2 != 0)
1816                 return ret2;
1817         return ret;
1818 }
1819
1820 static void dev_release(struct video_device *vfd)
1821 {
1822         /* nothing */
1823 }
1824
1825 static struct file_operations dev_fops = {
1826         .owner = THIS_MODULE,
1827         .open = dev_open,
1828         .release = dev_close,
1829         .read = dev_read,
1830         .mmap = dev_mmap,
1831         .ioctl = video_ioctl2,
1832 #ifdef CONFIG_COMPAT
1833         .compat_ioctl = v4l_compat_ioctl32,
1834 #endif
1835         .llseek = no_llseek,
1836         .poll   = dev_poll,
1837 };
1838
1839 static struct video_device gspca_template = {
1840         .name = "gspca main driver",
1841         .type = VID_TYPE_CAPTURE,
1842         .fops = &dev_fops,
1843         .release = dev_release,         /* mandatory */
1844         .minor = -1,
1845         .vidioc_querycap        = vidioc_querycap,
1846         .vidioc_dqbuf           = vidioc_dqbuf,
1847         .vidioc_qbuf            = vidioc_qbuf,
1848         .vidioc_enum_fmt_cap    = vidioc_enum_fmt_cap,
1849         .vidioc_try_fmt_cap     = vidioc_try_fmt_cap,
1850         .vidioc_g_fmt_cap       = vidioc_g_fmt_cap,
1851         .vidioc_s_fmt_cap       = vidioc_s_fmt_cap,
1852         .vidioc_streamon        = vidioc_streamon,
1853         .vidioc_queryctrl       = vidioc_queryctrl,
1854         .vidioc_g_ctrl          = vidioc_g_ctrl,
1855         .vidioc_s_ctrl          = vidioc_s_ctrl,
1856         .vidioc_querymenu       = vidioc_querymenu,
1857         .vidioc_enum_input      = vidioc_enum_input,
1858         .vidioc_g_input         = vidioc_g_input,
1859         .vidioc_s_input         = vidioc_s_input,
1860         .vidioc_reqbufs         = vidioc_reqbufs,
1861         .vidioc_querybuf        = vidioc_querybuf,
1862         .vidioc_streamoff       = vidioc_streamoff,
1863         .vidioc_g_jpegcomp      = vidioc_g_jpegcomp,
1864         .vidioc_s_jpegcomp      = vidioc_s_jpegcomp,
1865         .vidioc_g_parm          = vidioc_g_parm,
1866         .vidioc_s_parm          = vidioc_s_parm,
1867         .vidioc_s_std           = vidioc_s_std,
1868 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1869         .vidiocgmbuf          = vidiocgmbuf,
1870 #endif
1871 };
1872
1873 /*
1874  * probe and create a new gspca device
1875  *
1876  * This function must be called by the sub-driver when it is
1877  * called for probing a new device.
1878  */
1879 int gspca_dev_probe(struct usb_interface *intf,
1880                 const struct usb_device_id *id,
1881                 const struct sd_desc *sd_desc,
1882                 int dev_size,
1883                 struct module *module)
1884 {
1885         struct usb_interface_descriptor *interface;
1886         struct gspca_dev *gspca_dev;
1887         struct usb_device *dev = interface_to_usbdev(intf);
1888         int ret;
1889
1890         PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
1891
1892         /* we don't handle multi-config cameras */
1893         if (dev->descriptor.bNumConfigurations != 1)
1894                 return -ENODEV;
1895         interface = &intf->cur_altsetting->desc;
1896         if (interface->bInterfaceNumber > 0)
1897                 return -ENODEV;
1898
1899         /* create the device */
1900         if (dev_size < sizeof *gspca_dev)
1901                 dev_size = sizeof *gspca_dev;
1902         gspca_dev = kzalloc(dev_size, GFP_KERNEL);
1903         if (gspca_dev == NULL) {
1904                 err("couldn't kzalloc gspca struct");
1905                 return -EIO;
1906         }
1907         gspca_dev->dev = dev;
1908         gspca_dev->iface = interface->bInterfaceNumber;
1909         gspca_dev->nbalt = intf->num_altsetting;
1910         gspca_dev->sd_desc = sd_desc;
1911 /*      gspca_dev->users = 0;                   (done by kzalloc) */
1912         gspca_dev->nbufread = 2;
1913
1914         /* configure the subdriver */
1915         ret = gspca_dev->sd_desc->config(gspca_dev, id);
1916         if (ret < 0)
1917                 goto out;
1918         ret = gspca_set_alt0(gspca_dev);
1919         if (ret < 0)
1920                 goto out;
1921         gspca_set_default_mode(gspca_dev);
1922
1923         mutex_init(&gspca_dev->usb_lock);
1924         mutex_init(&gspca_dev->read_lock);
1925         mutex_init(&gspca_dev->queue_lock);
1926         init_waitqueue_head(&gspca_dev->wq);
1927
1928         /* init video stuff */
1929         memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
1930         gspca_dev->vdev.dev = &dev->dev;
1931         memcpy(&gspca_dev->fops, &dev_fops, sizeof gspca_dev->fops);
1932         gspca_dev->vdev.fops = &gspca_dev->fops;
1933         gspca_dev->fops.owner = module;         /* module protection */
1934         ret = video_register_device(&gspca_dev->vdev,
1935                                   VFL_TYPE_GRABBER,
1936                                   video_nr);
1937         if (ret < 0) {
1938                 err("video_register_device err %d", ret);
1939                 goto out;
1940         }
1941
1942         gspca_dev->present = 1;
1943         usb_set_intfdata(intf, gspca_dev);
1944         PDEBUG(D_PROBE, "probe ok");
1945         return 0;
1946 out:
1947         kfree(gspca_dev);
1948         return ret;
1949 }
1950 EXPORT_SYMBOL(gspca_dev_probe);
1951
1952 /*
1953  * USB disconnection
1954  *
1955  * This function must be called by the sub-driver
1956  * when the device disconnects, after the specific resources are freed.
1957  */
1958 void gspca_disconnect(struct usb_interface *intf)
1959 {
1960         struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1961
1962         if (!gspca_dev)
1963                 return;
1964         gspca_dev->present = 0;
1965         mutex_lock(&gspca_dev->queue_lock);
1966         mutex_lock(&gspca_dev->usb_lock);
1967         gspca_dev->streaming = 0;
1968         destroy_urbs(gspca_dev);
1969         mutex_unlock(&gspca_dev->usb_lock);
1970         mutex_unlock(&gspca_dev->queue_lock);
1971         while (gspca_dev->users != 0) {         /* wait until fully closed */
1972                 atomic_inc(&gspca_dev->nevent);
1973                 wake_up_interruptible(&gspca_dev->wq);  /* wake processes */
1974                 schedule();
1975         }
1976 /* We don't want people trying to open up the device */
1977         video_unregister_device(&gspca_dev->vdev);
1978 /* Free the memory */
1979         kfree(gspca_dev);
1980         PDEBUG(D_PROBE, "disconnect complete");
1981 }
1982 EXPORT_SYMBOL(gspca_disconnect);
1983
1984 /* -- module insert / remove -- */
1985 static int __init gspca_init(void)
1986 {
1987         info("main v%s registered", version);
1988         return 0;
1989 }
1990 static void __exit gspca_exit(void)
1991 {
1992         info("main deregistered");
1993 }
1994
1995 module_init(gspca_init);
1996 module_exit(gspca_exit);
1997
1998 #ifdef VIDEO_ADV_DEBUG
1999 module_param_named(debug, gspca_debug, int, 0644);
2000 MODULE_PARM_DESC(debug,
2001                 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2002                 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
2003                 " 0x0100: v4l2");
2004 #endif
2005 module_param(comp_fac, int, 0644);
2006 MODULE_PARM_DESC(comp_fac,
2007                 "Buffer size ratio when compressed in percent");