]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/media/video/gspca/gspca.c
[media] gspca: Add reset_resume callback to all sub-drivers
[mirror_ubuntu-artful-kernel.git] / drivers / media / video / gspca / gspca.c
1 /*
2 * Main USB camera driver
3 *
4 * Copyright (C) 2008-2011 Jean-François Moine <http://moinejf.free.fr>
5 *
6 * Camera button input handling by Márton Németh
7 * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
26 #define GSPCA_VERSION "2.14.0"
27
28 #include <linux/init.h>
29 #include <linux/fs.h>
30 #include <linux/vmalloc.h>
31 #include <linux/sched.h>
32 #include <linux/slab.h>
33 #include <linux/mm.h>
34 #include <linux/string.h>
35 #include <linux/pagemap.h>
36 #include <linux/io.h>
37 #include <asm/page.h>
38 #include <linux/uaccess.h>
39 #include <linux/ktime.h>
40 #include <media/v4l2-ioctl.h>
41 #include <media/v4l2-ctrls.h>
42 #include <media/v4l2-fh.h>
43 #include <media/v4l2-event.h>
44
45 #include "gspca.h"
46
47 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
48 #include <linux/input.h>
49 #include <linux/usb/input.h>
50 #endif
51
52 /* global values */
53 #define DEF_NURBS 3 /* default number of URBs */
54 #if DEF_NURBS > MAX_NURBS
55 #error "DEF_NURBS too big"
56 #endif
57
58 MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
59 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
60 MODULE_LICENSE("GPL");
61 MODULE_VERSION(GSPCA_VERSION);
62
63 #ifdef GSPCA_DEBUG
64 int gspca_debug = D_ERR | D_PROBE;
65 EXPORT_SYMBOL(gspca_debug);
66
67 static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
68 {
69 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
70 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
71 txt,
72 pixfmt & 0xff,
73 (pixfmt >> 8) & 0xff,
74 (pixfmt >> 16) & 0xff,
75 pixfmt >> 24,
76 w, h);
77 } else {
78 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
79 txt,
80 pixfmt,
81 w, h);
82 }
83 }
84 #else
85 #define PDEBUG_MODE(txt, pixfmt, w, h)
86 #endif
87
88 /* specific memory types - !! should be different from V4L2_MEMORY_xxx */
89 #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
90 #define GSPCA_MEMORY_READ 7
91
92 #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
93
94 /*
95 * VMA operations.
96 */
97 static void gspca_vm_open(struct vm_area_struct *vma)
98 {
99 struct gspca_frame *frame = vma->vm_private_data;
100
101 frame->vma_use_count++;
102 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
103 }
104
105 static void gspca_vm_close(struct vm_area_struct *vma)
106 {
107 struct gspca_frame *frame = vma->vm_private_data;
108
109 if (--frame->vma_use_count <= 0)
110 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
111 }
112
113 static const struct vm_operations_struct gspca_vm_ops = {
114 .open = gspca_vm_open,
115 .close = gspca_vm_close,
116 };
117
118 /*
119 * Input and interrupt endpoint handling functions
120 */
121 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
122 static void int_irq(struct urb *urb)
123 {
124 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
125 int ret;
126
127 ret = urb->status;
128 switch (ret) {
129 case 0:
130 if (gspca_dev->sd_desc->int_pkt_scan(gspca_dev,
131 urb->transfer_buffer, urb->actual_length) < 0) {
132 PDEBUG(D_ERR, "Unknown packet received");
133 }
134 break;
135
136 case -ENOENT:
137 case -ECONNRESET:
138 case -ENODEV:
139 case -ESHUTDOWN:
140 /* Stop is requested either by software or hardware is gone,
141 * keep the ret value non-zero and don't resubmit later.
142 */
143 break;
144
145 default:
146 PDEBUG(D_ERR, "URB error %i, resubmitting", urb->status);
147 urb->status = 0;
148 ret = 0;
149 }
150
151 if (ret == 0) {
152 ret = usb_submit_urb(urb, GFP_ATOMIC);
153 if (ret < 0)
154 pr_err("Resubmit URB failed with error %i\n", ret);
155 }
156 }
157
158 static int gspca_input_connect(struct gspca_dev *dev)
159 {
160 struct input_dev *input_dev;
161 int err = 0;
162
163 dev->input_dev = NULL;
164 if (dev->sd_desc->int_pkt_scan || dev->sd_desc->other_input) {
165 input_dev = input_allocate_device();
166 if (!input_dev)
167 return -ENOMEM;
168
169 usb_make_path(dev->dev, dev->phys, sizeof(dev->phys));
170 strlcat(dev->phys, "/input0", sizeof(dev->phys));
171
172 input_dev->name = dev->sd_desc->name;
173 input_dev->phys = dev->phys;
174
175 usb_to_input_id(dev->dev, &input_dev->id);
176
177 input_dev->evbit[0] = BIT_MASK(EV_KEY);
178 input_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
179 input_dev->dev.parent = &dev->dev->dev;
180
181 err = input_register_device(input_dev);
182 if (err) {
183 pr_err("Input device registration failed with error %i\n",
184 err);
185 input_dev->dev.parent = NULL;
186 input_free_device(input_dev);
187 } else {
188 dev->input_dev = input_dev;
189 }
190 }
191
192 return err;
193 }
194
195 static int alloc_and_submit_int_urb(struct gspca_dev *gspca_dev,
196 struct usb_endpoint_descriptor *ep)
197 {
198 unsigned int buffer_len;
199 int interval;
200 struct urb *urb;
201 struct usb_device *dev;
202 void *buffer = NULL;
203 int ret = -EINVAL;
204
205 buffer_len = le16_to_cpu(ep->wMaxPacketSize);
206 interval = ep->bInterval;
207 PDEBUG(D_CONF, "found int in endpoint: 0x%x, "
208 "buffer_len=%u, interval=%u",
209 ep->bEndpointAddress, buffer_len, interval);
210
211 dev = gspca_dev->dev;
212
213 urb = usb_alloc_urb(0, GFP_KERNEL);
214 if (!urb) {
215 ret = -ENOMEM;
216 goto error;
217 }
218
219 buffer = usb_alloc_coherent(dev, buffer_len,
220 GFP_KERNEL, &urb->transfer_dma);
221 if (!buffer) {
222 ret = -ENOMEM;
223 goto error_buffer;
224 }
225 usb_fill_int_urb(urb, dev,
226 usb_rcvintpipe(dev, ep->bEndpointAddress),
227 buffer, buffer_len,
228 int_irq, (void *)gspca_dev, interval);
229 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
230 ret = usb_submit_urb(urb, GFP_KERNEL);
231 if (ret < 0) {
232 PDEBUG(D_ERR, "submit int URB failed with error %i", ret);
233 goto error_submit;
234 }
235 gspca_dev->int_urb = urb;
236 return ret;
237
238 error_submit:
239 usb_free_coherent(dev,
240 urb->transfer_buffer_length,
241 urb->transfer_buffer,
242 urb->transfer_dma);
243 error_buffer:
244 usb_free_urb(urb);
245 error:
246 return ret;
247 }
248
249 static void gspca_input_create_urb(struct gspca_dev *gspca_dev)
250 {
251 struct usb_interface *intf;
252 struct usb_host_interface *intf_desc;
253 struct usb_endpoint_descriptor *ep;
254 int i;
255
256 if (gspca_dev->sd_desc->int_pkt_scan) {
257 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
258 intf_desc = intf->cur_altsetting;
259 for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
260 ep = &intf_desc->endpoint[i].desc;
261 if (usb_endpoint_dir_in(ep) &&
262 usb_endpoint_xfer_int(ep)) {
263
264 alloc_and_submit_int_urb(gspca_dev, ep);
265 break;
266 }
267 }
268 }
269 }
270
271 static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
272 {
273 struct urb *urb;
274
275 urb = gspca_dev->int_urb;
276 if (urb) {
277 gspca_dev->int_urb = NULL;
278 usb_kill_urb(urb);
279 usb_free_coherent(gspca_dev->dev,
280 urb->transfer_buffer_length,
281 urb->transfer_buffer,
282 urb->transfer_dma);
283 usb_free_urb(urb);
284 }
285 }
286 #else
287 static inline void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
288 {
289 }
290
291 static inline void gspca_input_create_urb(struct gspca_dev *gspca_dev)
292 {
293 }
294
295 static inline int gspca_input_connect(struct gspca_dev *dev)
296 {
297 return 0;
298 }
299 #endif
300
301 /*
302 * fill a video frame from an URB and resubmit
303 */
304 static void fill_frame(struct gspca_dev *gspca_dev,
305 struct urb *urb)
306 {
307 u8 *data; /* address of data in the iso message */
308 int i, len, st;
309 cam_pkt_op pkt_scan;
310
311 if (urb->status != 0) {
312 if (urb->status == -ESHUTDOWN)
313 return; /* disconnection */
314 #ifdef CONFIG_PM
315 if (gspca_dev->frozen)
316 return;
317 #endif
318 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
319 urb->status = 0;
320 goto resubmit;
321 }
322 pkt_scan = gspca_dev->sd_desc->pkt_scan;
323 for (i = 0; i < urb->number_of_packets; i++) {
324 len = urb->iso_frame_desc[i].actual_length;
325
326 /* check the packet status and length */
327 st = urb->iso_frame_desc[i].status;
328 if (st) {
329 pr_err("ISOC data error: [%d] len=%d, status=%d\n",
330 i, len, st);
331 gspca_dev->last_packet_type = DISCARD_PACKET;
332 continue;
333 }
334 if (len == 0) {
335 if (gspca_dev->empty_packet == 0)
336 gspca_dev->empty_packet = 1;
337 continue;
338 }
339
340 /* let the packet be analyzed by the subdriver */
341 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
342 i, urb->iso_frame_desc[i].offset, len);
343 data = (u8 *) urb->transfer_buffer
344 + urb->iso_frame_desc[i].offset;
345 pkt_scan(gspca_dev, data, len);
346 }
347
348 resubmit:
349 /* resubmit the URB */
350 st = usb_submit_urb(urb, GFP_ATOMIC);
351 if (st < 0)
352 pr_err("usb_submit_urb() ret %d\n", st);
353 }
354
355 /*
356 * ISOC message interrupt from the USB device
357 *
358 * Analyse each packet and call the subdriver for copy to the frame buffer.
359 */
360 static void isoc_irq(struct urb *urb)
361 {
362 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
363
364 PDEBUG(D_PACK, "isoc irq");
365 if (!gspca_dev->streaming)
366 return;
367 fill_frame(gspca_dev, urb);
368 }
369
370 /*
371 * bulk message interrupt from the USB device
372 */
373 static void bulk_irq(struct urb *urb)
374 {
375 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
376 int st;
377
378 PDEBUG(D_PACK, "bulk irq");
379 if (!gspca_dev->streaming)
380 return;
381 switch (urb->status) {
382 case 0:
383 break;
384 case -ESHUTDOWN:
385 return; /* disconnection */
386 default:
387 #ifdef CONFIG_PM
388 if (gspca_dev->frozen)
389 return;
390 #endif
391 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
392 urb->status = 0;
393 goto resubmit;
394 }
395
396 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
397 gspca_dev->sd_desc->pkt_scan(gspca_dev,
398 urb->transfer_buffer,
399 urb->actual_length);
400
401 resubmit:
402 /* resubmit the URB */
403 if (gspca_dev->cam.bulk_nurbs != 0) {
404 st = usb_submit_urb(urb, GFP_ATOMIC);
405 if (st < 0)
406 pr_err("usb_submit_urb() ret %d\n", st);
407 }
408 }
409
410 /*
411 * add data to the current frame
412 *
413 * This function is called by the subdrivers at interrupt level.
414 *
415 * To build a frame, these ones must add
416 * - one FIRST_PACKET
417 * - 0 or many INTER_PACKETs
418 * - one LAST_PACKET
419 * DISCARD_PACKET invalidates the whole frame.
420 */
421 void gspca_frame_add(struct gspca_dev *gspca_dev,
422 enum gspca_packet_type packet_type,
423 const u8 *data,
424 int len)
425 {
426 struct gspca_frame *frame;
427 int i, j;
428
429 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
430
431 if (packet_type == FIRST_PACKET) {
432 i = atomic_read(&gspca_dev->fr_i);
433
434 /* if there are no queued buffer, discard the whole frame */
435 if (i == atomic_read(&gspca_dev->fr_q)) {
436 gspca_dev->last_packet_type = DISCARD_PACKET;
437 gspca_dev->sequence++;
438 return;
439 }
440 j = gspca_dev->fr_queue[i];
441 frame = &gspca_dev->frame[j];
442 frame->v4l2_buf.timestamp = ktime_to_timeval(ktime_get());
443 frame->v4l2_buf.sequence = gspca_dev->sequence++;
444 gspca_dev->image = frame->data;
445 gspca_dev->image_len = 0;
446 } else {
447 switch (gspca_dev->last_packet_type) {
448 case DISCARD_PACKET:
449 if (packet_type == LAST_PACKET) {
450 gspca_dev->last_packet_type = packet_type;
451 gspca_dev->image = NULL;
452 gspca_dev->image_len = 0;
453 }
454 return;
455 case LAST_PACKET:
456 return;
457 }
458 }
459
460 /* append the packet to the frame buffer */
461 if (len > 0) {
462 if (gspca_dev->image_len + len > gspca_dev->frsz) {
463 PDEBUG(D_ERR|D_PACK, "frame overflow %d > %d",
464 gspca_dev->image_len + len,
465 gspca_dev->frsz);
466 packet_type = DISCARD_PACKET;
467 } else {
468 /* !! image is NULL only when last pkt is LAST or DISCARD
469 if (gspca_dev->image == NULL) {
470 pr_err("gspca_frame_add() image == NULL\n");
471 return;
472 }
473 */
474 memcpy(gspca_dev->image + gspca_dev->image_len,
475 data, len);
476 gspca_dev->image_len += len;
477 }
478 }
479 gspca_dev->last_packet_type = packet_type;
480
481 /* if last packet, invalidate packet concatenation until
482 * next first packet, wake up the application and advance
483 * in the queue */
484 if (packet_type == LAST_PACKET) {
485 i = atomic_read(&gspca_dev->fr_i);
486 j = gspca_dev->fr_queue[i];
487 frame = &gspca_dev->frame[j];
488 frame->v4l2_buf.bytesused = gspca_dev->image_len;
489 frame->v4l2_buf.flags = (frame->v4l2_buf.flags
490 | V4L2_BUF_FLAG_DONE)
491 & ~V4L2_BUF_FLAG_QUEUED;
492 i = (i + 1) % GSPCA_MAX_FRAMES;
493 atomic_set(&gspca_dev->fr_i, i);
494 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
495 PDEBUG(D_FRAM, "frame complete len:%d",
496 frame->v4l2_buf.bytesused);
497 gspca_dev->image = NULL;
498 gspca_dev->image_len = 0;
499 }
500 }
501 EXPORT_SYMBOL(gspca_frame_add);
502
503 static int frame_alloc(struct gspca_dev *gspca_dev, struct file *file,
504 enum v4l2_memory memory, unsigned int count)
505 {
506 struct gspca_frame *frame;
507 unsigned int frsz;
508 int i;
509
510 i = gspca_dev->curr_mode;
511 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
512 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
513 frsz = PAGE_ALIGN(frsz);
514 if (count >= GSPCA_MAX_FRAMES)
515 count = GSPCA_MAX_FRAMES - 1;
516 gspca_dev->frbuf = vmalloc_32(frsz * count);
517 if (!gspca_dev->frbuf) {
518 pr_err("frame alloc failed\n");
519 return -ENOMEM;
520 }
521 gspca_dev->capt_file = file;
522 gspca_dev->memory = memory;
523 gspca_dev->frsz = frsz;
524 gspca_dev->nframes = count;
525 for (i = 0; i < count; i++) {
526 frame = &gspca_dev->frame[i];
527 frame->v4l2_buf.index = i;
528 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
529 frame->v4l2_buf.flags = 0;
530 frame->v4l2_buf.field = V4L2_FIELD_NONE;
531 frame->v4l2_buf.length = frsz;
532 frame->v4l2_buf.memory = memory;
533 frame->v4l2_buf.sequence = 0;
534 frame->data = gspca_dev->frbuf + i * frsz;
535 frame->v4l2_buf.m.offset = i * frsz;
536 }
537 atomic_set(&gspca_dev->fr_q, 0);
538 atomic_set(&gspca_dev->fr_i, 0);
539 gspca_dev->fr_o = 0;
540 return 0;
541 }
542
543 static void frame_free(struct gspca_dev *gspca_dev)
544 {
545 int i;
546
547 PDEBUG(D_STREAM, "frame free");
548 if (gspca_dev->frbuf != NULL) {
549 vfree(gspca_dev->frbuf);
550 gspca_dev->frbuf = NULL;
551 for (i = 0; i < gspca_dev->nframes; i++)
552 gspca_dev->frame[i].data = NULL;
553 }
554 gspca_dev->nframes = 0;
555 gspca_dev->frsz = 0;
556 gspca_dev->capt_file = NULL;
557 gspca_dev->memory = GSPCA_MEMORY_NO;
558 }
559
560 static void destroy_urbs(struct gspca_dev *gspca_dev)
561 {
562 struct urb *urb;
563 unsigned int i;
564
565 PDEBUG(D_STREAM, "kill transfer");
566 for (i = 0; i < MAX_NURBS; i++) {
567 urb = gspca_dev->urb[i];
568 if (urb == NULL)
569 break;
570
571 gspca_dev->urb[i] = NULL;
572 usb_kill_urb(urb);
573 if (urb->transfer_buffer != NULL)
574 usb_free_coherent(gspca_dev->dev,
575 urb->transfer_buffer_length,
576 urb->transfer_buffer,
577 urb->transfer_dma);
578 usb_free_urb(urb);
579 }
580 }
581
582 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
583 {
584 int ret;
585
586 if (gspca_dev->alt == 0)
587 return 0;
588 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
589 if (ret < 0)
590 pr_err("set alt 0 err %d\n", ret);
591 return ret;
592 }
593
594 /* Note: both the queue and the usb locks should be held when calling this */
595 static void gspca_stream_off(struct gspca_dev *gspca_dev)
596 {
597 gspca_dev->streaming = 0;
598 gspca_dev->usb_err = 0;
599 if (gspca_dev->sd_desc->stopN)
600 gspca_dev->sd_desc->stopN(gspca_dev);
601 destroy_urbs(gspca_dev);
602 gspca_input_destroy_urb(gspca_dev);
603 gspca_set_alt0(gspca_dev);
604 gspca_input_create_urb(gspca_dev);
605 if (gspca_dev->sd_desc->stop0)
606 gspca_dev->sd_desc->stop0(gspca_dev);
607 PDEBUG(D_STREAM, "stream off OK");
608 }
609
610 /*
611 * look for an input transfer endpoint in an alternate setting
612 */
613 static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
614 int xfer)
615 {
616 struct usb_host_endpoint *ep;
617 int i, attr;
618
619 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
620 ep = &alt->endpoint[i];
621 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
622 if (attr == xfer
623 && ep->desc.wMaxPacketSize != 0
624 && usb_endpoint_dir_in(&ep->desc))
625 return ep;
626 }
627 return NULL;
628 }
629
630 /* compute the minimum bandwidth for the current transfer */
631 static u32 which_bandwidth(struct gspca_dev *gspca_dev)
632 {
633 u32 bandwidth;
634 int i;
635
636 /* get the (max) image size */
637 i = gspca_dev->curr_mode;
638 bandwidth = gspca_dev->cam.cam_mode[i].sizeimage;
639
640 /* if the image is compressed, estimate its mean size */
641 if (!gspca_dev->cam.needs_full_bandwidth &&
642 bandwidth < gspca_dev->cam.cam_mode[i].width *
643 gspca_dev->cam.cam_mode[i].height)
644 bandwidth = bandwidth * 3 / 8; /* 0.375 */
645
646 /* estimate the frame rate */
647 if (gspca_dev->sd_desc->get_streamparm) {
648 struct v4l2_streamparm parm;
649
650 gspca_dev->sd_desc->get_streamparm(gspca_dev, &parm);
651 bandwidth *= parm.parm.capture.timeperframe.denominator;
652 bandwidth /= parm.parm.capture.timeperframe.numerator;
653 } else {
654
655 /* don't hope more than 15 fps with USB 1.1 and
656 * image resolution >= 640x480 */
657 if (gspca_dev->width >= 640
658 && gspca_dev->dev->speed == USB_SPEED_FULL)
659 bandwidth *= 15; /* 15 fps */
660 else
661 bandwidth *= 30; /* 30 fps */
662 }
663
664 PDEBUG(D_STREAM, "min bandwidth: %d", bandwidth);
665 return bandwidth;
666 }
667
668 /* endpoint table */
669 #define MAX_ALT 16
670 struct ep_tb_s {
671 u32 alt;
672 u32 bandwidth;
673 };
674
675 /*
676 * build the table of the endpoints
677 * and compute the minimum bandwidth for the image transfer
678 */
679 static int build_isoc_ep_tb(struct gspca_dev *gspca_dev,
680 struct usb_interface *intf,
681 struct ep_tb_s *ep_tb)
682 {
683 struct usb_host_endpoint *ep;
684 int i, j, nbalt, psize, found;
685 u32 bandwidth, last_bw;
686
687 nbalt = intf->num_altsetting;
688 if (nbalt > MAX_ALT)
689 nbalt = MAX_ALT; /* fixme: should warn */
690
691 /* build the endpoint table */
692 i = 0;
693 last_bw = 0;
694 for (;;) {
695 ep_tb->bandwidth = 2000 * 2000 * 120;
696 found = 0;
697 for (j = 0; j < nbalt; j++) {
698 ep = alt_xfer(&intf->altsetting[j],
699 USB_ENDPOINT_XFER_ISOC);
700 if (ep == NULL)
701 continue;
702 if (ep->desc.bInterval == 0) {
703 pr_err("alt %d iso endp with 0 interval\n", j);
704 continue;
705 }
706 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
707 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
708 bandwidth = psize * 1000;
709 if (gspca_dev->dev->speed == USB_SPEED_HIGH
710 || gspca_dev->dev->speed == USB_SPEED_SUPER)
711 bandwidth *= 8;
712 bandwidth /= 1 << (ep->desc.bInterval - 1);
713 if (bandwidth <= last_bw)
714 continue;
715 if (bandwidth < ep_tb->bandwidth) {
716 ep_tb->bandwidth = bandwidth;
717 ep_tb->alt = j;
718 found = 1;
719 }
720 }
721 if (!found)
722 break;
723 PDEBUG(D_STREAM, "alt %d bandwidth %d",
724 ep_tb->alt, ep_tb->bandwidth);
725 last_bw = ep_tb->bandwidth;
726 i++;
727 ep_tb++;
728 }
729
730 /*
731 * If the camera:
732 * has a usb audio class interface (a built in usb mic); and
733 * is a usb 1 full speed device; and
734 * uses the max full speed iso bandwidth; and
735 * and has more than 1 alt setting
736 * then skip the highest alt setting to spare bandwidth for the mic
737 */
738 if (gspca_dev->audio &&
739 gspca_dev->dev->speed == USB_SPEED_FULL &&
740 last_bw >= 1000000 &&
741 i > 1) {
742 PDEBUG(D_STREAM, "dev has usb audio, skipping highest alt");
743 i--;
744 ep_tb--;
745 }
746
747 /* get the requested bandwidth and start at the highest atlsetting */
748 bandwidth = which_bandwidth(gspca_dev);
749 ep_tb--;
750 while (i > 1) {
751 ep_tb--;
752 if (ep_tb->bandwidth < bandwidth)
753 break;
754 i--;
755 }
756 return i;
757 }
758
759 /*
760 * create the URBs for image transfer
761 */
762 static int create_urbs(struct gspca_dev *gspca_dev,
763 struct usb_host_endpoint *ep)
764 {
765 struct urb *urb;
766 int n, nurbs, i, psize, npkt, bsize;
767
768 /* calculate the packet size and the number of packets */
769 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
770
771 if (!gspca_dev->cam.bulk) { /* isoc */
772
773 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
774 if (gspca_dev->pkt_size == 0)
775 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
776 else
777 psize = gspca_dev->pkt_size;
778 npkt = gspca_dev->cam.npkt;
779 if (npkt == 0)
780 npkt = 32; /* default value */
781 bsize = psize * npkt;
782 PDEBUG(D_STREAM,
783 "isoc %d pkts size %d = bsize:%d",
784 npkt, psize, bsize);
785 nurbs = DEF_NURBS;
786 } else { /* bulk */
787 npkt = 0;
788 bsize = gspca_dev->cam.bulk_size;
789 if (bsize == 0)
790 bsize = psize;
791 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
792 if (gspca_dev->cam.bulk_nurbs != 0)
793 nurbs = gspca_dev->cam.bulk_nurbs;
794 else
795 nurbs = 1;
796 }
797
798 for (n = 0; n < nurbs; n++) {
799 urb = usb_alloc_urb(npkt, GFP_KERNEL);
800 if (!urb) {
801 pr_err("usb_alloc_urb failed\n");
802 return -ENOMEM;
803 }
804 gspca_dev->urb[n] = urb;
805 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
806 bsize,
807 GFP_KERNEL,
808 &urb->transfer_dma);
809
810 if (urb->transfer_buffer == NULL) {
811 pr_err("usb_alloc_coherent failed\n");
812 return -ENOMEM;
813 }
814 urb->dev = gspca_dev->dev;
815 urb->context = gspca_dev;
816 urb->transfer_buffer_length = bsize;
817 if (npkt != 0) { /* ISOC */
818 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
819 ep->desc.bEndpointAddress);
820 urb->transfer_flags = URB_ISO_ASAP
821 | URB_NO_TRANSFER_DMA_MAP;
822 urb->interval = 1 << (ep->desc.bInterval - 1);
823 urb->complete = isoc_irq;
824 urb->number_of_packets = npkt;
825 for (i = 0; i < npkt; i++) {
826 urb->iso_frame_desc[i].length = psize;
827 urb->iso_frame_desc[i].offset = psize * i;
828 }
829 } else { /* bulk */
830 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
831 ep->desc.bEndpointAddress);
832 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
833 urb->complete = bulk_irq;
834 }
835 }
836 return 0;
837 }
838
839 /*
840 * start the USB transfer
841 */
842 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
843 {
844 struct usb_interface *intf;
845 struct usb_host_endpoint *ep;
846 struct urb *urb;
847 struct ep_tb_s ep_tb[MAX_ALT];
848 int n, ret, xfer, alt, alt_idx;
849
850 /* reset the streaming variables */
851 gspca_dev->image = NULL;
852 gspca_dev->image_len = 0;
853 gspca_dev->last_packet_type = DISCARD_PACKET;
854 gspca_dev->sequence = 0;
855
856 gspca_dev->usb_err = 0;
857
858 /* do the specific subdriver stuff before endpoint selection */
859 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
860 gspca_dev->alt = gspca_dev->cam.bulk ? intf->num_altsetting : 0;
861 if (gspca_dev->sd_desc->isoc_init) {
862 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
863 if (ret < 0)
864 return ret;
865 }
866 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
867 : USB_ENDPOINT_XFER_ISOC;
868
869 /* if bulk or the subdriver forced an altsetting, get the endpoint */
870 if (gspca_dev->alt != 0) {
871 gspca_dev->alt--; /* (previous version compatibility) */
872 ep = alt_xfer(&intf->altsetting[gspca_dev->alt], xfer);
873 if (ep == NULL) {
874 pr_err("bad altsetting %d\n", gspca_dev->alt);
875 return -EIO;
876 }
877 ep_tb[0].alt = gspca_dev->alt;
878 alt_idx = 1;
879 } else {
880
881 /* else, compute the minimum bandwidth
882 * and build the endpoint table */
883 alt_idx = build_isoc_ep_tb(gspca_dev, intf, ep_tb);
884 if (alt_idx <= 0) {
885 pr_err("no transfer endpoint found\n");
886 return -EIO;
887 }
888 }
889
890 /* set the highest alternate setting and
891 * loop until urb submit succeeds */
892 gspca_input_destroy_urb(gspca_dev);
893
894 gspca_dev->alt = ep_tb[--alt_idx].alt;
895 alt = -1;
896 for (;;) {
897 if (alt != gspca_dev->alt) {
898 alt = gspca_dev->alt;
899 if (intf->num_altsetting > 1) {
900 ret = usb_set_interface(gspca_dev->dev,
901 gspca_dev->iface,
902 alt);
903 if (ret < 0) {
904 if (ret == -ENOSPC)
905 goto retry; /*fixme: ugly*/
906 pr_err("set alt %d err %d\n", alt, ret);
907 goto out;
908 }
909 }
910 }
911 if (!gspca_dev->cam.no_urb_create) {
912 PDEBUG(D_STREAM, "init transfer alt %d", alt);
913 ret = create_urbs(gspca_dev,
914 alt_xfer(&intf->altsetting[alt], xfer));
915 if (ret < 0) {
916 destroy_urbs(gspca_dev);
917 goto out;
918 }
919 }
920
921 /* clear the bulk endpoint */
922 if (gspca_dev->cam.bulk)
923 usb_clear_halt(gspca_dev->dev,
924 gspca_dev->urb[0]->pipe);
925
926 /* start the cam */
927 ret = gspca_dev->sd_desc->start(gspca_dev);
928 if (ret < 0) {
929 destroy_urbs(gspca_dev);
930 goto out;
931 }
932 gspca_dev->streaming = 1;
933 v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
934
935 /* some bulk transfers are started by the subdriver */
936 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
937 break;
938
939 /* submit the URBs */
940 for (n = 0; n < MAX_NURBS; n++) {
941 urb = gspca_dev->urb[n];
942 if (urb == NULL)
943 break;
944 ret = usb_submit_urb(urb, GFP_KERNEL);
945 if (ret < 0)
946 break;
947 }
948 if (ret >= 0)
949 break; /* transfer is started */
950
951 /* something when wrong
952 * stop the webcam and free the transfer resources */
953 gspca_stream_off(gspca_dev);
954 if (ret != -ENOSPC) {
955 pr_err("usb_submit_urb alt %d err %d\n",
956 gspca_dev->alt, ret);
957 goto out;
958 }
959
960 /* the bandwidth is not wide enough
961 * negotiate or try a lower alternate setting */
962 retry:
963 PDEBUG(D_ERR|D_STREAM,
964 "alt %d - bandwidth not wide enough - trying again",
965 alt);
966 msleep(20); /* wait for kill complete */
967 if (gspca_dev->sd_desc->isoc_nego) {
968 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
969 if (ret < 0)
970 goto out;
971 } else {
972 if (alt_idx <= 0) {
973 pr_err("no transfer endpoint found\n");
974 ret = -EIO;
975 goto out;
976 }
977 gspca_dev->alt = ep_tb[--alt_idx].alt;
978 }
979 }
980 out:
981 gspca_input_create_urb(gspca_dev);
982 return ret;
983 }
984
985 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
986 {
987 struct gspca_ctrl *ctrl;
988 int i;
989
990 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
991 gspca_dev->curr_mode = i;
992 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
993 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
994 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
995
996 /* set the current control values to their default values
997 * which may have changed in sd_init() */
998 /* does nothing if ctrl_handler == NULL */
999 v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
1000 ctrl = gspca_dev->cam.ctrls;
1001 if (ctrl != NULL) {
1002 for (i = 0;
1003 i < gspca_dev->sd_desc->nctrls;
1004 i++, ctrl++)
1005 ctrl->val = ctrl->def;
1006 }
1007 }
1008
1009 static int wxh_to_mode(struct gspca_dev *gspca_dev,
1010 int width, int height)
1011 {
1012 int i;
1013
1014 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
1015 if (width >= gspca_dev->cam.cam_mode[i].width
1016 && height >= gspca_dev->cam.cam_mode[i].height)
1017 break;
1018 }
1019 return i;
1020 }
1021
1022 /*
1023 * search a mode with the right pixel format
1024 */
1025 static int gspca_get_mode(struct gspca_dev *gspca_dev,
1026 int mode,
1027 int pixfmt)
1028 {
1029 int modeU, modeD;
1030
1031 modeU = modeD = mode;
1032 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
1033 if (--modeD >= 0) {
1034 if (gspca_dev->cam.cam_mode[modeD].pixelformat
1035 == pixfmt)
1036 return modeD;
1037 }
1038 if (++modeU < gspca_dev->cam.nmodes) {
1039 if (gspca_dev->cam.cam_mode[modeU].pixelformat
1040 == pixfmt)
1041 return modeU;
1042 }
1043 }
1044 return -EINVAL;
1045 }
1046
1047 #ifdef CONFIG_VIDEO_ADV_DEBUG
1048 static int vidioc_g_register(struct file *file, void *priv,
1049 struct v4l2_dbg_register *reg)
1050 {
1051 struct gspca_dev *gspca_dev = video_drvdata(file);
1052
1053 gspca_dev->usb_err = 0;
1054 return gspca_dev->sd_desc->get_register(gspca_dev, reg);
1055 }
1056
1057 static int vidioc_s_register(struct file *file, void *priv,
1058 struct v4l2_dbg_register *reg)
1059 {
1060 struct gspca_dev *gspca_dev = video_drvdata(file);
1061
1062 gspca_dev->usb_err = 0;
1063 return gspca_dev->sd_desc->set_register(gspca_dev, reg);
1064 }
1065 #endif
1066
1067 static int vidioc_g_chip_ident(struct file *file, void *priv,
1068 struct v4l2_dbg_chip_ident *chip)
1069 {
1070 struct gspca_dev *gspca_dev = video_drvdata(file);
1071
1072 gspca_dev->usb_err = 0;
1073 return gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
1074 }
1075
1076 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1077 struct v4l2_fmtdesc *fmtdesc)
1078 {
1079 struct gspca_dev *gspca_dev = video_drvdata(file);
1080 int i, j, index;
1081 __u32 fmt_tb[8];
1082
1083 /* give an index to each format */
1084 index = 0;
1085 j = 0;
1086 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
1087 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
1088 j = 0;
1089 for (;;) {
1090 if (fmt_tb[j] == fmt_tb[index])
1091 break;
1092 j++;
1093 }
1094 if (j == index) {
1095 if (fmtdesc->index == index)
1096 break; /* new format */
1097 index++;
1098 if (index >= ARRAY_SIZE(fmt_tb))
1099 return -EINVAL;
1100 }
1101 }
1102 if (i < 0)
1103 return -EINVAL; /* no more format */
1104
1105 fmtdesc->pixelformat = fmt_tb[index];
1106 if (gspca_dev->cam.cam_mode[i].sizeimage <
1107 gspca_dev->cam.cam_mode[i].width *
1108 gspca_dev->cam.cam_mode[i].height)
1109 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
1110 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
1111 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
1112 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
1113 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
1114 fmtdesc->description[4] = '\0';
1115 return 0;
1116 }
1117
1118 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1119 struct v4l2_format *fmt)
1120 {
1121 struct gspca_dev *gspca_dev = video_drvdata(file);
1122 int mode;
1123
1124 mode = gspca_dev->curr_mode;
1125 fmt->fmt.pix = gspca_dev->cam.cam_mode[mode];
1126 /* some drivers use priv internally, zero it before giving it to
1127 userspace */
1128 fmt->fmt.pix.priv = 0;
1129 return 0;
1130 }
1131
1132 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
1133 struct v4l2_format *fmt)
1134 {
1135 int w, h, mode, mode2;
1136
1137 w = fmt->fmt.pix.width;
1138 h = fmt->fmt.pix.height;
1139
1140 #ifdef GSPCA_DEBUG
1141 if (gspca_debug & D_CONF)
1142 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
1143 #endif
1144 /* search the closest mode for width and height */
1145 mode = wxh_to_mode(gspca_dev, w, h);
1146
1147 /* OK if right palette */
1148 if (gspca_dev->cam.cam_mode[mode].pixelformat
1149 != fmt->fmt.pix.pixelformat) {
1150
1151 /* else, search the closest mode with the same pixel format */
1152 mode2 = gspca_get_mode(gspca_dev, mode,
1153 fmt->fmt.pix.pixelformat);
1154 if (mode2 >= 0)
1155 mode = mode2;
1156 /* else
1157 ; * no chance, return this mode */
1158 }
1159 fmt->fmt.pix = gspca_dev->cam.cam_mode[mode];
1160 /* some drivers use priv internally, zero it before giving it to
1161 userspace */
1162 fmt->fmt.pix.priv = 0;
1163 return mode; /* used when s_fmt */
1164 }
1165
1166 static int vidioc_try_fmt_vid_cap(struct file *file,
1167 void *priv,
1168 struct v4l2_format *fmt)
1169 {
1170 struct gspca_dev *gspca_dev = video_drvdata(file);
1171 int ret;
1172
1173 ret = try_fmt_vid_cap(gspca_dev, fmt);
1174 if (ret < 0)
1175 return ret;
1176 return 0;
1177 }
1178
1179 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1180 struct v4l2_format *fmt)
1181 {
1182 struct gspca_dev *gspca_dev = video_drvdata(file);
1183 int ret;
1184
1185 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1186 return -ERESTARTSYS;
1187
1188 ret = try_fmt_vid_cap(gspca_dev, fmt);
1189 if (ret < 0)
1190 goto out;
1191
1192 if (gspca_dev->nframes != 0
1193 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
1194 ret = -EINVAL;
1195 goto out;
1196 }
1197
1198 if (ret == gspca_dev->curr_mode) {
1199 ret = 0;
1200 goto out; /* same mode */
1201 }
1202
1203 if (gspca_dev->streaming) {
1204 ret = -EBUSY;
1205 goto out;
1206 }
1207 gspca_dev->width = fmt->fmt.pix.width;
1208 gspca_dev->height = fmt->fmt.pix.height;
1209 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
1210 gspca_dev->curr_mode = ret;
1211
1212 ret = 0;
1213 out:
1214 mutex_unlock(&gspca_dev->queue_lock);
1215 return ret;
1216 }
1217
1218 static int vidioc_enum_framesizes(struct file *file, void *priv,
1219 struct v4l2_frmsizeenum *fsize)
1220 {
1221 struct gspca_dev *gspca_dev = video_drvdata(file);
1222 int i;
1223 __u32 index = 0;
1224
1225 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
1226 if (fsize->pixel_format !=
1227 gspca_dev->cam.cam_mode[i].pixelformat)
1228 continue;
1229
1230 if (fsize->index == index) {
1231 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1232 fsize->discrete.width =
1233 gspca_dev->cam.cam_mode[i].width;
1234 fsize->discrete.height =
1235 gspca_dev->cam.cam_mode[i].height;
1236 return 0;
1237 }
1238 index++;
1239 }
1240
1241 return -EINVAL;
1242 }
1243
1244 static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1245 struct v4l2_frmivalenum *fival)
1246 {
1247 struct gspca_dev *gspca_dev = video_drvdata(filp);
1248 int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
1249 __u32 i;
1250
1251 if (gspca_dev->cam.mode_framerates == NULL ||
1252 gspca_dev->cam.mode_framerates[mode].nrates == 0)
1253 return -EINVAL;
1254
1255 if (fival->pixel_format !=
1256 gspca_dev->cam.cam_mode[mode].pixelformat)
1257 return -EINVAL;
1258
1259 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1260 if (fival->index == i) {
1261 fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1262 fival->discrete.numerator = 1;
1263 fival->discrete.denominator =
1264 gspca_dev->cam.mode_framerates[mode].rates[i];
1265 return 0;
1266 }
1267 }
1268
1269 return -EINVAL;
1270 }
1271
1272 static void gspca_release(struct v4l2_device *v4l2_device)
1273 {
1274 struct gspca_dev *gspca_dev =
1275 container_of(v4l2_device, struct gspca_dev, v4l2_dev);
1276
1277 PDEBUG(D_PROBE, "%s released",
1278 video_device_node_name(&gspca_dev->vdev));
1279
1280 v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
1281 v4l2_device_unregister(&gspca_dev->v4l2_dev);
1282 kfree(gspca_dev->usb_buf);
1283 kfree(gspca_dev);
1284 }
1285
1286 static int dev_open(struct file *file)
1287 {
1288 struct gspca_dev *gspca_dev = video_drvdata(file);
1289
1290 PDEBUG(D_STREAM, "[%s] open", current->comm);
1291
1292 /* protect the subdriver against rmmod */
1293 if (!try_module_get(gspca_dev->module))
1294 return -ENODEV;
1295
1296 #ifdef GSPCA_DEBUG
1297 /* activate the v4l2 debug */
1298 if (gspca_debug & D_V4L2)
1299 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
1300 | V4L2_DEBUG_IOCTL_ARG;
1301 else
1302 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
1303 | V4L2_DEBUG_IOCTL_ARG);
1304 #endif
1305 return v4l2_fh_open(file);
1306 }
1307
1308 static int dev_close(struct file *file)
1309 {
1310 struct gspca_dev *gspca_dev = video_drvdata(file);
1311
1312 PDEBUG(D_STREAM, "[%s] close", current->comm);
1313
1314 /* Needed for gspca_stream_off, always lock before queue_lock! */
1315 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1316 return -ERESTARTSYS;
1317
1318 if (mutex_lock_interruptible(&gspca_dev->queue_lock)) {
1319 mutex_unlock(&gspca_dev->usb_lock);
1320 return -ERESTARTSYS;
1321 }
1322
1323 /* if the file did the capture, free the streaming resources */
1324 if (gspca_dev->capt_file == file) {
1325 if (gspca_dev->streaming)
1326 gspca_stream_off(gspca_dev);
1327 frame_free(gspca_dev);
1328 }
1329 module_put(gspca_dev->module);
1330 mutex_unlock(&gspca_dev->queue_lock);
1331 mutex_unlock(&gspca_dev->usb_lock);
1332
1333 PDEBUG(D_STREAM, "close done");
1334
1335 return v4l2_fh_release(file);
1336 }
1337
1338 static int vidioc_querycap(struct file *file, void *priv,
1339 struct v4l2_capability *cap)
1340 {
1341 struct gspca_dev *gspca_dev = video_drvdata(file);
1342
1343 strlcpy((char *) cap->driver, gspca_dev->sd_desc->name,
1344 sizeof cap->driver);
1345 if (gspca_dev->dev->product != NULL) {
1346 strlcpy((char *) cap->card, gspca_dev->dev->product,
1347 sizeof cap->card);
1348 } else {
1349 snprintf((char *) cap->card, sizeof cap->card,
1350 "USB Camera (%04x:%04x)",
1351 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1352 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1353 }
1354 usb_make_path(gspca_dev->dev, (char *) cap->bus_info,
1355 sizeof(cap->bus_info));
1356 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE
1357 | V4L2_CAP_STREAMING
1358 | V4L2_CAP_READWRITE;
1359 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1360 return 0;
1361 }
1362
1363 static int get_ctrl(struct gspca_dev *gspca_dev,
1364 int id)
1365 {
1366 const struct ctrl *ctrls;
1367 int i;
1368
1369 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1370 i < gspca_dev->sd_desc->nctrls;
1371 i++, ctrls++) {
1372 if (gspca_dev->ctrl_dis & (1 << i))
1373 continue;
1374 if (id == ctrls->qctrl.id)
1375 return i;
1376 }
1377 return -1;
1378 }
1379
1380 static int vidioc_queryctrl(struct file *file, void *priv,
1381 struct v4l2_queryctrl *q_ctrl)
1382 {
1383 struct gspca_dev *gspca_dev = video_drvdata(file);
1384 const struct ctrl *ctrls;
1385 struct gspca_ctrl *gspca_ctrl;
1386 int i, idx;
1387 u32 id;
1388
1389 id = q_ctrl->id;
1390 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1391 id &= V4L2_CTRL_ID_MASK;
1392 id++;
1393 idx = -1;
1394 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1395 if (gspca_dev->ctrl_dis & (1 << i))
1396 continue;
1397 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
1398 continue;
1399 if (idx >= 0
1400 && gspca_dev->sd_desc->ctrls[i].qctrl.id
1401 > gspca_dev->sd_desc->ctrls[idx].qctrl.id)
1402 continue;
1403 idx = i;
1404 }
1405 } else {
1406 idx = get_ctrl(gspca_dev, id);
1407 }
1408 if (idx < 0)
1409 return -EINVAL;
1410 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1411 memcpy(q_ctrl, &ctrls->qctrl, sizeof *q_ctrl);
1412 if (gspca_dev->cam.ctrls != NULL) {
1413 gspca_ctrl = &gspca_dev->cam.ctrls[idx];
1414 q_ctrl->default_value = gspca_ctrl->def;
1415 q_ctrl->minimum = gspca_ctrl->min;
1416 q_ctrl->maximum = gspca_ctrl->max;
1417 }
1418 if (gspca_dev->ctrl_inac & (1 << idx))
1419 q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1420 return 0;
1421 }
1422
1423 static int vidioc_s_ctrl(struct file *file, void *priv,
1424 struct v4l2_control *ctrl)
1425 {
1426 struct gspca_dev *gspca_dev = video_drvdata(file);
1427 const struct ctrl *ctrls;
1428 struct gspca_ctrl *gspca_ctrl;
1429 int idx;
1430
1431 idx = get_ctrl(gspca_dev, ctrl->id);
1432 if (idx < 0)
1433 return -EINVAL;
1434 if (gspca_dev->ctrl_inac & (1 << idx))
1435 return -EINVAL;
1436 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1437 if (gspca_dev->cam.ctrls != NULL) {
1438 gspca_ctrl = &gspca_dev->cam.ctrls[idx];
1439 if (ctrl->value < gspca_ctrl->min
1440 || ctrl->value > gspca_ctrl->max)
1441 return -ERANGE;
1442 } else {
1443 gspca_ctrl = NULL;
1444 if (ctrl->value < ctrls->qctrl.minimum
1445 || ctrl->value > ctrls->qctrl.maximum)
1446 return -ERANGE;
1447 }
1448 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1449 gspca_dev->usb_err = 0;
1450 if (ctrls->set != NULL)
1451 return ctrls->set(gspca_dev, ctrl->value);
1452 if (gspca_ctrl != NULL) {
1453 gspca_ctrl->val = ctrl->value;
1454 if (ctrls->set_control != NULL
1455 && gspca_dev->streaming)
1456 ctrls->set_control(gspca_dev);
1457 }
1458 return gspca_dev->usb_err;
1459 }
1460
1461 static int vidioc_g_ctrl(struct file *file, void *priv,
1462 struct v4l2_control *ctrl)
1463 {
1464 struct gspca_dev *gspca_dev = video_drvdata(file);
1465 const struct ctrl *ctrls;
1466 int idx;
1467
1468 idx = get_ctrl(gspca_dev, ctrl->id);
1469 if (idx < 0)
1470 return -EINVAL;
1471 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1472
1473 gspca_dev->usb_err = 0;
1474 if (ctrls->get != NULL)
1475 return ctrls->get(gspca_dev, &ctrl->value);
1476 if (gspca_dev->cam.ctrls != NULL)
1477 ctrl->value = gspca_dev->cam.ctrls[idx].val;
1478 return 0;
1479 }
1480
1481 static int vidioc_querymenu(struct file *file, void *priv,
1482 struct v4l2_querymenu *qmenu)
1483 {
1484 struct gspca_dev *gspca_dev = video_drvdata(file);
1485
1486 if (!gspca_dev->sd_desc->querymenu)
1487 return -ENOTTY;
1488 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1489 }
1490
1491 static int vidioc_enum_input(struct file *file, void *priv,
1492 struct v4l2_input *input)
1493 {
1494 struct gspca_dev *gspca_dev = video_drvdata(file);
1495
1496 if (input->index != 0)
1497 return -EINVAL;
1498 input->type = V4L2_INPUT_TYPE_CAMERA;
1499 input->status = gspca_dev->cam.input_flags;
1500 strlcpy(input->name, gspca_dev->sd_desc->name,
1501 sizeof input->name);
1502 return 0;
1503 }
1504
1505 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1506 {
1507 *i = 0;
1508 return 0;
1509 }
1510
1511 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1512 {
1513 if (i > 0)
1514 return -EINVAL;
1515 return (0);
1516 }
1517
1518 static int vidioc_reqbufs(struct file *file, void *priv,
1519 struct v4l2_requestbuffers *rb)
1520 {
1521 struct gspca_dev *gspca_dev = video_drvdata(file);
1522 int i, ret = 0, streaming;
1523
1524 i = rb->memory; /* (avoid compilation warning) */
1525 switch (i) {
1526 case GSPCA_MEMORY_READ: /* (internal call) */
1527 case V4L2_MEMORY_MMAP:
1528 case V4L2_MEMORY_USERPTR:
1529 break;
1530 default:
1531 return -EINVAL;
1532 }
1533 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1534 return -ERESTARTSYS;
1535
1536 if (gspca_dev->memory != GSPCA_MEMORY_NO
1537 && gspca_dev->memory != GSPCA_MEMORY_READ
1538 && gspca_dev->memory != rb->memory) {
1539 ret = -EBUSY;
1540 goto out;
1541 }
1542
1543 /* only one file may do the capture */
1544 if (gspca_dev->capt_file != NULL
1545 && gspca_dev->capt_file != file) {
1546 ret = -EBUSY;
1547 goto out;
1548 }
1549
1550 /* if allocated, the buffers must not be mapped */
1551 for (i = 0; i < gspca_dev->nframes; i++) {
1552 if (gspca_dev->frame[i].vma_use_count) {
1553 ret = -EBUSY;
1554 goto out;
1555 }
1556 }
1557
1558 /* stop streaming */
1559 streaming = gspca_dev->streaming;
1560 if (streaming) {
1561 gspca_stream_off(gspca_dev);
1562
1563 /* Don't restart the stream when switching from read
1564 * to mmap mode */
1565 if (gspca_dev->memory == GSPCA_MEMORY_READ)
1566 streaming = 0;
1567 }
1568
1569 /* free the previous allocated buffers, if any */
1570 if (gspca_dev->nframes != 0)
1571 frame_free(gspca_dev);
1572 if (rb->count == 0) /* unrequest */
1573 goto out;
1574 ret = frame_alloc(gspca_dev, file, rb->memory, rb->count);
1575 if (ret == 0) {
1576 rb->count = gspca_dev->nframes;
1577 if (streaming)
1578 ret = gspca_init_transfer(gspca_dev);
1579 }
1580 out:
1581 mutex_unlock(&gspca_dev->queue_lock);
1582 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1583 return ret;
1584 }
1585
1586 static int vidioc_querybuf(struct file *file, void *priv,
1587 struct v4l2_buffer *v4l2_buf)
1588 {
1589 struct gspca_dev *gspca_dev = video_drvdata(file);
1590 struct gspca_frame *frame;
1591
1592 if (v4l2_buf->index < 0
1593 || v4l2_buf->index >= gspca_dev->nframes)
1594 return -EINVAL;
1595
1596 frame = &gspca_dev->frame[v4l2_buf->index];
1597 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1598 return 0;
1599 }
1600
1601 static int vidioc_streamon(struct file *file, void *priv,
1602 enum v4l2_buf_type buf_type)
1603 {
1604 struct gspca_dev *gspca_dev = video_drvdata(file);
1605 int ret;
1606
1607 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1608 return -EINVAL;
1609 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1610 return -ERESTARTSYS;
1611
1612 /* check the capture file */
1613 if (gspca_dev->capt_file != file) {
1614 ret = -EBUSY;
1615 goto out;
1616 }
1617
1618 if (gspca_dev->nframes == 0
1619 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
1620 ret = -EINVAL;
1621 goto out;
1622 }
1623 if (!gspca_dev->streaming) {
1624 ret = gspca_init_transfer(gspca_dev);
1625 if (ret < 0)
1626 goto out;
1627 }
1628 #ifdef GSPCA_DEBUG
1629 if (gspca_debug & D_STREAM) {
1630 PDEBUG_MODE("stream on OK",
1631 gspca_dev->pixfmt,
1632 gspca_dev->width,
1633 gspca_dev->height);
1634 }
1635 #endif
1636 ret = 0;
1637 out:
1638 mutex_unlock(&gspca_dev->queue_lock);
1639 return ret;
1640 }
1641
1642 static int vidioc_streamoff(struct file *file, void *priv,
1643 enum v4l2_buf_type buf_type)
1644 {
1645 struct gspca_dev *gspca_dev = video_drvdata(file);
1646 int i, ret;
1647
1648 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1649 return -EINVAL;
1650
1651 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1652 return -ERESTARTSYS;
1653
1654 if (!gspca_dev->streaming) {
1655 ret = 0;
1656 goto out;
1657 }
1658
1659 /* check the capture file */
1660 if (gspca_dev->capt_file != file) {
1661 ret = -EBUSY;
1662 goto out;
1663 }
1664
1665 /* stop streaming */
1666 gspca_stream_off(gspca_dev);
1667 /* In case another thread is waiting in dqbuf */
1668 wake_up_interruptible(&gspca_dev->wq);
1669
1670 /* empty the transfer queues */
1671 for (i = 0; i < gspca_dev->nframes; i++)
1672 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1673 atomic_set(&gspca_dev->fr_q, 0);
1674 atomic_set(&gspca_dev->fr_i, 0);
1675 gspca_dev->fr_o = 0;
1676 ret = 0;
1677 out:
1678 mutex_unlock(&gspca_dev->queue_lock);
1679 return ret;
1680 }
1681
1682 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1683 struct v4l2_jpegcompression *jpegcomp)
1684 {
1685 struct gspca_dev *gspca_dev = video_drvdata(file);
1686
1687 gspca_dev->usb_err = 0;
1688 return gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1689 }
1690
1691 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1692 struct v4l2_jpegcompression *jpegcomp)
1693 {
1694 struct gspca_dev *gspca_dev = video_drvdata(file);
1695
1696 gspca_dev->usb_err = 0;
1697 return gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1698 }
1699
1700 static int vidioc_g_parm(struct file *filp, void *priv,
1701 struct v4l2_streamparm *parm)
1702 {
1703 struct gspca_dev *gspca_dev = video_drvdata(filp);
1704
1705 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1706
1707 if (gspca_dev->sd_desc->get_streamparm) {
1708 gspca_dev->usb_err = 0;
1709 gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
1710 return gspca_dev->usb_err;
1711 }
1712 return 0;
1713 }
1714
1715 static int vidioc_s_parm(struct file *filp, void *priv,
1716 struct v4l2_streamparm *parm)
1717 {
1718 struct gspca_dev *gspca_dev = video_drvdata(filp);
1719 int n;
1720
1721 n = parm->parm.capture.readbuffers;
1722 if (n == 0 || n >= GSPCA_MAX_FRAMES)
1723 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1724 else
1725 gspca_dev->nbufread = n;
1726
1727 if (gspca_dev->sd_desc->set_streamparm) {
1728 gspca_dev->usb_err = 0;
1729 gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
1730 return gspca_dev->usb_err;
1731 }
1732
1733 return 0;
1734 }
1735
1736 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1737 {
1738 struct gspca_dev *gspca_dev = video_drvdata(file);
1739 struct gspca_frame *frame;
1740 struct page *page;
1741 unsigned long addr, start, size;
1742 int i, ret;
1743
1744 start = vma->vm_start;
1745 size = vma->vm_end - vma->vm_start;
1746 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1747
1748 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1749 return -ERESTARTSYS;
1750 if (gspca_dev->capt_file != file) {
1751 ret = -EINVAL;
1752 goto out;
1753 }
1754
1755 frame = NULL;
1756 for (i = 0; i < gspca_dev->nframes; ++i) {
1757 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1758 PDEBUG(D_STREAM, "mmap bad memory type");
1759 break;
1760 }
1761 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1762 == vma->vm_pgoff) {
1763 frame = &gspca_dev->frame[i];
1764 break;
1765 }
1766 }
1767 if (frame == NULL) {
1768 PDEBUG(D_STREAM, "mmap no frame buffer found");
1769 ret = -EINVAL;
1770 goto out;
1771 }
1772 if (size != frame->v4l2_buf.length) {
1773 PDEBUG(D_STREAM, "mmap bad size");
1774 ret = -EINVAL;
1775 goto out;
1776 }
1777
1778 /*
1779 * - VM_IO marks the area as being a mmaped region for I/O to a
1780 * device. It also prevents the region from being core dumped.
1781 */
1782 vma->vm_flags |= VM_IO;
1783
1784 addr = (unsigned long) frame->data;
1785 while (size > 0) {
1786 page = vmalloc_to_page((void *) addr);
1787 ret = vm_insert_page(vma, start, page);
1788 if (ret < 0)
1789 goto out;
1790 start += PAGE_SIZE;
1791 addr += PAGE_SIZE;
1792 size -= PAGE_SIZE;
1793 }
1794
1795 vma->vm_ops = &gspca_vm_ops;
1796 vma->vm_private_data = frame;
1797 gspca_vm_open(vma);
1798 ret = 0;
1799 out:
1800 mutex_unlock(&gspca_dev->queue_lock);
1801 return ret;
1802 }
1803
1804 static int frame_ready_nolock(struct gspca_dev *gspca_dev, struct file *file,
1805 enum v4l2_memory memory)
1806 {
1807 if (!gspca_dev->present)
1808 return -ENODEV;
1809 if (gspca_dev->capt_file != file || gspca_dev->memory != memory ||
1810 !gspca_dev->streaming)
1811 return -EINVAL;
1812
1813 /* check if a frame is ready */
1814 return gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i);
1815 }
1816
1817 static int frame_ready(struct gspca_dev *gspca_dev, struct file *file,
1818 enum v4l2_memory memory)
1819 {
1820 int ret;
1821
1822 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1823 return -ERESTARTSYS;
1824 ret = frame_ready_nolock(gspca_dev, file, memory);
1825 mutex_unlock(&gspca_dev->queue_lock);
1826 return ret;
1827 }
1828
1829 /*
1830 * dequeue a video buffer
1831 *
1832 * If nonblock_ing is false, block until a buffer is available.
1833 */
1834 static int vidioc_dqbuf(struct file *file, void *priv,
1835 struct v4l2_buffer *v4l2_buf)
1836 {
1837 struct gspca_dev *gspca_dev = video_drvdata(file);
1838 struct gspca_frame *frame;
1839 int i, j, ret;
1840
1841 PDEBUG(D_FRAM, "dqbuf");
1842
1843 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1844 return -ERESTARTSYS;
1845
1846 for (;;) {
1847 ret = frame_ready_nolock(gspca_dev, file, v4l2_buf->memory);
1848 if (ret < 0)
1849 goto out;
1850 if (ret > 0)
1851 break;
1852
1853 mutex_unlock(&gspca_dev->queue_lock);
1854
1855 if (file->f_flags & O_NONBLOCK)
1856 return -EAGAIN;
1857
1858 /* wait till a frame is ready */
1859 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1860 frame_ready(gspca_dev, file, v4l2_buf->memory),
1861 msecs_to_jiffies(3000));
1862 if (ret < 0)
1863 return ret;
1864 if (ret == 0)
1865 return -EIO;
1866
1867 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1868 return -ERESTARTSYS;
1869 }
1870
1871 i = gspca_dev->fr_o;
1872 j = gspca_dev->fr_queue[i];
1873 frame = &gspca_dev->frame[j];
1874
1875 gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES;
1876
1877 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1878 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1879 PDEBUG(D_FRAM, "dqbuf %d", j);
1880 ret = 0;
1881
1882 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
1883 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
1884 frame->data,
1885 frame->v4l2_buf.bytesused)) {
1886 PDEBUG(D_ERR|D_STREAM,
1887 "dqbuf cp to user failed");
1888 ret = -EFAULT;
1889 }
1890 }
1891 out:
1892 mutex_unlock(&gspca_dev->queue_lock);
1893
1894 if (ret == 0 && gspca_dev->sd_desc->dq_callback) {
1895 mutex_lock(&gspca_dev->usb_lock);
1896 gspca_dev->usb_err = 0;
1897 if (gspca_dev->present)
1898 gspca_dev->sd_desc->dq_callback(gspca_dev);
1899 mutex_unlock(&gspca_dev->usb_lock);
1900 }
1901
1902 return ret;
1903 }
1904
1905 /*
1906 * queue a video buffer
1907 *
1908 * Attempting to queue a buffer that has already been
1909 * queued will return -EINVAL.
1910 */
1911 static int vidioc_qbuf(struct file *file, void *priv,
1912 struct v4l2_buffer *v4l2_buf)
1913 {
1914 struct gspca_dev *gspca_dev = video_drvdata(file);
1915 struct gspca_frame *frame;
1916 int i, index, ret;
1917
1918 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1919
1920 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1921 return -ERESTARTSYS;
1922
1923 index = v4l2_buf->index;
1924 if ((unsigned) index >= gspca_dev->nframes) {
1925 PDEBUG(D_FRAM,
1926 "qbuf idx %d >= %d", index, gspca_dev->nframes);
1927 ret = -EINVAL;
1928 goto out;
1929 }
1930 if (v4l2_buf->memory != gspca_dev->memory) {
1931 PDEBUG(D_FRAM, "qbuf bad memory type");
1932 ret = -EINVAL;
1933 goto out;
1934 }
1935
1936 frame = &gspca_dev->frame[index];
1937 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1938 PDEBUG(D_FRAM, "qbuf bad state");
1939 ret = -EINVAL;
1940 goto out;
1941 }
1942
1943 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
1944
1945 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
1946 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1947 frame->v4l2_buf.length = v4l2_buf->length;
1948 }
1949
1950 /* put the buffer in the 'queued' queue */
1951 i = atomic_read(&gspca_dev->fr_q);
1952 gspca_dev->fr_queue[i] = index;
1953 atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES);
1954
1955 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1956 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1957 ret = 0;
1958 out:
1959 mutex_unlock(&gspca_dev->queue_lock);
1960 return ret;
1961 }
1962
1963 /*
1964 * allocate the resources for read()
1965 */
1966 static int read_alloc(struct gspca_dev *gspca_dev,
1967 struct file *file)
1968 {
1969 struct v4l2_buffer v4l2_buf;
1970 int i, ret;
1971
1972 PDEBUG(D_STREAM, "read alloc");
1973
1974 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1975 return -ERESTARTSYS;
1976
1977 if (gspca_dev->nframes == 0) {
1978 struct v4l2_requestbuffers rb;
1979
1980 memset(&rb, 0, sizeof rb);
1981 rb.count = gspca_dev->nbufread;
1982 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1983 rb.memory = GSPCA_MEMORY_READ;
1984 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1985 if (ret != 0) {
1986 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
1987 goto out;
1988 }
1989 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1990 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1991 v4l2_buf.memory = GSPCA_MEMORY_READ;
1992 for (i = 0; i < gspca_dev->nbufread; i++) {
1993 v4l2_buf.index = i;
1994 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1995 if (ret != 0) {
1996 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1997 goto out;
1998 }
1999 }
2000 }
2001
2002 /* start streaming */
2003 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2004 if (ret != 0)
2005 PDEBUG(D_STREAM, "read streamon err %d", ret);
2006 out:
2007 mutex_unlock(&gspca_dev->usb_lock);
2008 return ret;
2009 }
2010
2011 static unsigned int dev_poll(struct file *file, poll_table *wait)
2012 {
2013 struct gspca_dev *gspca_dev = video_drvdata(file);
2014 unsigned long req_events = poll_requested_events(wait);
2015 int ret = 0;
2016
2017 PDEBUG(D_FRAM, "poll");
2018
2019 if (req_events & POLLPRI)
2020 ret |= v4l2_ctrl_poll(file, wait);
2021
2022 if (req_events & (POLLIN | POLLRDNORM)) {
2023 /* if reqbufs is not done, the user would use read() */
2024 if (gspca_dev->memory == GSPCA_MEMORY_NO) {
2025 if (read_alloc(gspca_dev, file) != 0) {
2026 ret |= POLLERR;
2027 goto out;
2028 }
2029 }
2030
2031 poll_wait(file, &gspca_dev->wq, wait);
2032
2033 /* check if an image has been received */
2034 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0) {
2035 ret |= POLLERR;
2036 goto out;
2037 }
2038 if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i))
2039 ret |= POLLIN | POLLRDNORM;
2040 mutex_unlock(&gspca_dev->queue_lock);
2041 }
2042
2043 out:
2044 if (!gspca_dev->present)
2045 ret |= POLLHUP;
2046
2047 return ret;
2048 }
2049
2050 static ssize_t dev_read(struct file *file, char __user *data,
2051 size_t count, loff_t *ppos)
2052 {
2053 struct gspca_dev *gspca_dev = video_drvdata(file);
2054 struct gspca_frame *frame;
2055 struct v4l2_buffer v4l2_buf;
2056 struct timeval timestamp;
2057 int n, ret, ret2;
2058
2059 PDEBUG(D_FRAM, "read (%zd)", count);
2060 if (gspca_dev->memory == GSPCA_MEMORY_NO) { /* first time ? */
2061 ret = read_alloc(gspca_dev, file);
2062 if (ret != 0)
2063 return ret;
2064 }
2065
2066 /* get a frame */
2067 timestamp = ktime_to_timeval(ktime_get());
2068 timestamp.tv_sec--;
2069 n = 2;
2070 for (;;) {
2071 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2072 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2073 v4l2_buf.memory = GSPCA_MEMORY_READ;
2074 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
2075 if (ret != 0) {
2076 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
2077 return ret;
2078 }
2079
2080 /* if the process slept for more than 1 second,
2081 * get a newer frame */
2082 frame = &gspca_dev->frame[v4l2_buf.index];
2083 if (--n < 0)
2084 break; /* avoid infinite loop */
2085 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
2086 break;
2087 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2088 if (ret != 0) {
2089 PDEBUG(D_STREAM, "read qbuf err %d", ret);
2090 return ret;
2091 }
2092 }
2093
2094 /* copy the frame */
2095 if (count > frame->v4l2_buf.bytesused)
2096 count = frame->v4l2_buf.bytesused;
2097 ret = copy_to_user(data, frame->data, count);
2098 if (ret != 0) {
2099 PDEBUG(D_ERR|D_STREAM,
2100 "read cp to user lack %d / %zd", ret, count);
2101 ret = -EFAULT;
2102 goto out;
2103 }
2104 ret = count;
2105 out:
2106 /* in each case, requeue the buffer */
2107 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2108 if (ret2 != 0)
2109 return ret2;
2110 return ret;
2111 }
2112
2113 static struct v4l2_file_operations dev_fops = {
2114 .owner = THIS_MODULE,
2115 .open = dev_open,
2116 .release = dev_close,
2117 .read = dev_read,
2118 .mmap = dev_mmap,
2119 .unlocked_ioctl = video_ioctl2,
2120 .poll = dev_poll,
2121 };
2122
2123 static const struct v4l2_ioctl_ops dev_ioctl_ops = {
2124 .vidioc_querycap = vidioc_querycap,
2125 .vidioc_dqbuf = vidioc_dqbuf,
2126 .vidioc_qbuf = vidioc_qbuf,
2127 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2128 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2129 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2130 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2131 .vidioc_streamon = vidioc_streamon,
2132 .vidioc_queryctrl = vidioc_queryctrl,
2133 .vidioc_g_ctrl = vidioc_g_ctrl,
2134 .vidioc_s_ctrl = vidioc_s_ctrl,
2135 .vidioc_querymenu = vidioc_querymenu,
2136 .vidioc_enum_input = vidioc_enum_input,
2137 .vidioc_g_input = vidioc_g_input,
2138 .vidioc_s_input = vidioc_s_input,
2139 .vidioc_reqbufs = vidioc_reqbufs,
2140 .vidioc_querybuf = vidioc_querybuf,
2141 .vidioc_streamoff = vidioc_streamoff,
2142 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
2143 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
2144 .vidioc_g_parm = vidioc_g_parm,
2145 .vidioc_s_parm = vidioc_s_parm,
2146 .vidioc_enum_framesizes = vidioc_enum_framesizes,
2147 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
2148 #ifdef CONFIG_VIDEO_ADV_DEBUG
2149 .vidioc_g_register = vidioc_g_register,
2150 .vidioc_s_register = vidioc_s_register,
2151 #endif
2152 .vidioc_g_chip_ident = vidioc_g_chip_ident,
2153 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2154 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2155 };
2156
2157 static const struct video_device gspca_template = {
2158 .name = "gspca main driver",
2159 .fops = &dev_fops,
2160 .ioctl_ops = &dev_ioctl_ops,
2161 .release = video_device_release_empty, /* We use v4l2_dev.release */
2162 };
2163
2164 /* initialize the controls */
2165 static void ctrls_init(struct gspca_dev *gspca_dev)
2166 {
2167 struct gspca_ctrl *ctrl;
2168 int i;
2169
2170 for (i = 0, ctrl = gspca_dev->cam.ctrls;
2171 i < gspca_dev->sd_desc->nctrls;
2172 i++, ctrl++) {
2173 ctrl->def = gspca_dev->sd_desc->ctrls[i].qctrl.default_value;
2174 ctrl->val = ctrl->def;
2175 ctrl->min = gspca_dev->sd_desc->ctrls[i].qctrl.minimum;
2176 ctrl->max = gspca_dev->sd_desc->ctrls[i].qctrl.maximum;
2177 }
2178 }
2179
2180 /*
2181 * probe and create a new gspca device
2182 *
2183 * This function must be called by the sub-driver when it is
2184 * called for probing a new device.
2185 */
2186 int gspca_dev_probe2(struct usb_interface *intf,
2187 const struct usb_device_id *id,
2188 const struct sd_desc *sd_desc,
2189 int dev_size,
2190 struct module *module)
2191 {
2192 struct gspca_dev *gspca_dev;
2193 struct usb_device *dev = interface_to_usbdev(intf);
2194 int ret;
2195
2196 pr_info("%s-" GSPCA_VERSION " probing %04x:%04x\n",
2197 sd_desc->name, id->idVendor, id->idProduct);
2198
2199 /* create the device */
2200 if (dev_size < sizeof *gspca_dev)
2201 dev_size = sizeof *gspca_dev;
2202 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
2203 if (!gspca_dev) {
2204 pr_err("couldn't kzalloc gspca struct\n");
2205 return -ENOMEM;
2206 }
2207 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2208 if (!gspca_dev->usb_buf) {
2209 pr_err("out of memory\n");
2210 ret = -ENOMEM;
2211 goto out;
2212 }
2213 gspca_dev->dev = dev;
2214 gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
2215
2216 /* check if any audio device */
2217 if (dev->actconfig->desc.bNumInterfaces != 1) {
2218 int i;
2219 struct usb_interface *intf2;
2220
2221 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
2222 intf2 = dev->actconfig->interface[i];
2223 if (intf2 != NULL
2224 && intf2->altsetting != NULL
2225 && intf2->altsetting->desc.bInterfaceClass ==
2226 USB_CLASS_AUDIO) {
2227 gspca_dev->audio = 1;
2228 break;
2229 }
2230 }
2231 }
2232
2233 gspca_dev->v4l2_dev.release = gspca_release;
2234 ret = v4l2_device_register(&intf->dev, &gspca_dev->v4l2_dev);
2235 if (ret)
2236 goto out;
2237 gspca_dev->sd_desc = sd_desc;
2238 gspca_dev->nbufread = 2;
2239 gspca_dev->empty_packet = -1; /* don't check the empty packets */
2240 gspca_dev->vdev = gspca_template;
2241 gspca_dev->vdev.v4l2_dev = &gspca_dev->v4l2_dev;
2242 video_set_drvdata(&gspca_dev->vdev, gspca_dev);
2243 set_bit(V4L2_FL_USE_FH_PRIO, &gspca_dev->vdev.flags);
2244 gspca_dev->module = module;
2245 gspca_dev->present = 1;
2246
2247 mutex_init(&gspca_dev->usb_lock);
2248 gspca_dev->vdev.lock = &gspca_dev->usb_lock;
2249 mutex_init(&gspca_dev->queue_lock);
2250 init_waitqueue_head(&gspca_dev->wq);
2251
2252 /* configure the subdriver and initialize the USB device */
2253 ret = sd_desc->config(gspca_dev, id);
2254 if (ret < 0)
2255 goto out;
2256 if (gspca_dev->cam.ctrls != NULL)
2257 ctrls_init(gspca_dev);
2258 ret = sd_desc->init(gspca_dev);
2259 if (ret < 0)
2260 goto out;
2261 if (sd_desc->init_controls)
2262 ret = sd_desc->init_controls(gspca_dev);
2263 if (ret < 0)
2264 goto out;
2265 gspca_set_default_mode(gspca_dev);
2266
2267 ret = gspca_input_connect(gspca_dev);
2268 if (ret)
2269 goto out;
2270
2271 /*
2272 * Don't take usb_lock for these ioctls. This improves latency if
2273 * usb_lock is taken for a long time, e.g. when changing a control
2274 * value, and a new frame is ready to be dequeued.
2275 */
2276 v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_DQBUF);
2277 v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QBUF);
2278 v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QUERYBUF);
2279 if (!gspca_dev->sd_desc->get_chip_ident)
2280 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_G_CHIP_IDENT);
2281 #ifdef CONFIG_VIDEO_ADV_DEBUG
2282 if (!gspca_dev->sd_desc->get_chip_ident ||
2283 !gspca_dev->sd_desc->get_register)
2284 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_G_REGISTER);
2285 if (!gspca_dev->sd_desc->get_chip_ident ||
2286 !gspca_dev->sd_desc->set_register)
2287 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_S_REGISTER);
2288 #endif
2289 if (!gspca_dev->sd_desc->get_jcomp)
2290 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_G_JPEGCOMP);
2291 if (!gspca_dev->sd_desc->set_jcomp)
2292 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_S_JPEGCOMP);
2293
2294 /* init video stuff */
2295 ret = video_register_device(&gspca_dev->vdev,
2296 VFL_TYPE_GRABBER,
2297 -1);
2298 if (ret < 0) {
2299 pr_err("video_register_device err %d\n", ret);
2300 goto out;
2301 }
2302
2303 usb_set_intfdata(intf, gspca_dev);
2304 PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
2305
2306 gspca_input_create_urb(gspca_dev);
2307
2308 return 0;
2309 out:
2310 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2311 if (gspca_dev->input_dev)
2312 input_unregister_device(gspca_dev->input_dev);
2313 #endif
2314 v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
2315 kfree(gspca_dev->usb_buf);
2316 kfree(gspca_dev);
2317 return ret;
2318 }
2319 EXPORT_SYMBOL(gspca_dev_probe2);
2320
2321 /* same function as the previous one, but check the interface */
2322 int gspca_dev_probe(struct usb_interface *intf,
2323 const struct usb_device_id *id,
2324 const struct sd_desc *sd_desc,
2325 int dev_size,
2326 struct module *module)
2327 {
2328 struct usb_device *dev = interface_to_usbdev(intf);
2329
2330 /* we don't handle multi-config cameras */
2331 if (dev->descriptor.bNumConfigurations != 1) {
2332 pr_err("%04x:%04x too many config\n",
2333 id->idVendor, id->idProduct);
2334 return -ENODEV;
2335 }
2336
2337 /* the USB video interface must be the first one */
2338 if (dev->actconfig->desc.bNumInterfaces != 1
2339 && intf->cur_altsetting->desc.bInterfaceNumber != 0)
2340 return -ENODEV;
2341
2342 return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
2343 }
2344 EXPORT_SYMBOL(gspca_dev_probe);
2345
2346 /*
2347 * USB disconnection
2348 *
2349 * This function must be called by the sub-driver
2350 * when the device disconnects, after the specific resources are freed.
2351 */
2352 void gspca_disconnect(struct usb_interface *intf)
2353 {
2354 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2355 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2356 struct input_dev *input_dev;
2357 #endif
2358
2359 PDEBUG(D_PROBE, "%s disconnect",
2360 video_device_node_name(&gspca_dev->vdev));
2361
2362 mutex_lock(&gspca_dev->usb_lock);
2363
2364 usb_set_intfdata(intf, NULL);
2365 gspca_dev->dev = NULL;
2366 gspca_dev->present = 0;
2367 destroy_urbs(gspca_dev);
2368
2369 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2370 gspca_input_destroy_urb(gspca_dev);
2371 input_dev = gspca_dev->input_dev;
2372 if (input_dev) {
2373 gspca_dev->input_dev = NULL;
2374 input_unregister_device(input_dev);
2375 }
2376 #endif
2377 /* Free subdriver's streaming resources / stop sd workqueue(s) */
2378 if (gspca_dev->sd_desc->stop0 && gspca_dev->streaming)
2379 gspca_dev->sd_desc->stop0(gspca_dev);
2380 gspca_dev->streaming = 0;
2381 wake_up_interruptible(&gspca_dev->wq);
2382
2383 v4l2_device_disconnect(&gspca_dev->v4l2_dev);
2384 video_unregister_device(&gspca_dev->vdev);
2385
2386 mutex_unlock(&gspca_dev->usb_lock);
2387
2388 /* (this will call gspca_release() immediately or on last close) */
2389 v4l2_device_put(&gspca_dev->v4l2_dev);
2390 }
2391 EXPORT_SYMBOL(gspca_disconnect);
2392
2393 #ifdef CONFIG_PM
2394 int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2395 {
2396 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2397
2398 if (!gspca_dev->streaming)
2399 return 0;
2400 mutex_lock(&gspca_dev->usb_lock);
2401 gspca_dev->frozen = 1; /* avoid urb error messages */
2402 gspca_dev->usb_err = 0;
2403 if (gspca_dev->sd_desc->stopN)
2404 gspca_dev->sd_desc->stopN(gspca_dev);
2405 destroy_urbs(gspca_dev);
2406 gspca_input_destroy_urb(gspca_dev);
2407 gspca_set_alt0(gspca_dev);
2408 if (gspca_dev->sd_desc->stop0)
2409 gspca_dev->sd_desc->stop0(gspca_dev);
2410 mutex_unlock(&gspca_dev->usb_lock);
2411 return 0;
2412 }
2413 EXPORT_SYMBOL(gspca_suspend);
2414
2415 int gspca_resume(struct usb_interface *intf)
2416 {
2417 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2418 int streaming, ret = 0;
2419
2420 mutex_lock(&gspca_dev->usb_lock);
2421 gspca_dev->frozen = 0;
2422 gspca_dev->usb_err = 0;
2423 gspca_dev->sd_desc->init(gspca_dev);
2424 gspca_input_create_urb(gspca_dev);
2425 /*
2426 * Most subdrivers send all ctrl values on sd_start and thus
2427 * only write to the device registers on s_ctrl when streaming ->
2428 * Clear streaming to avoid setting all ctrls twice.
2429 */
2430 streaming = gspca_dev->streaming;
2431 gspca_dev->streaming = 0;
2432 if (streaming)
2433 ret = gspca_init_transfer(gspca_dev);
2434 mutex_unlock(&gspca_dev->usb_lock);
2435 return ret;
2436 }
2437 EXPORT_SYMBOL(gspca_resume);
2438 #endif
2439
2440 /* -- module insert / remove -- */
2441 static int __init gspca_init(void)
2442 {
2443 pr_info("v" GSPCA_VERSION " registered\n");
2444 return 0;
2445 }
2446 static void __exit gspca_exit(void)
2447 {
2448 }
2449
2450 module_init(gspca_init);
2451 module_exit(gspca_exit);
2452
2453 #ifdef GSPCA_DEBUG
2454 module_param_named(debug, gspca_debug, int, 0644);
2455 MODULE_PARM_DESC(debug,
2456 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2457 " 0x08:stream 0x10:frame 0x20:packet"
2458 " 0x0100: v4l2");
2459 #endif