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