xref: /openbmc/linux/drivers/media/usb/gspca/gspca.c (revision 8781e5df)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Main USB camera driver
4  *
5  * Copyright (C) 2008-2011 Jean-François Moine <http://moinejf.free.fr>
6  *
7  * Camera button input handling by Márton Németh
8  * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu>
9  */
10 
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 
13 #define GSPCA_VERSION	"2.14.0"
14 
15 #include <linux/init.h>
16 #include <linux/fs.h>
17 #include <linux/vmalloc.h>
18 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <linux/mm.h>
21 #include <linux/string.h>
22 #include <linux/pagemap.h>
23 #include <linux/io.h>
24 #include <asm/page.h>
25 #include <linux/uaccess.h>
26 #include <linux/ktime.h>
27 #include <media/v4l2-ioctl.h>
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-fh.h>
30 #include <media/v4l2-event.h>
31 
32 #include "gspca.h"
33 
34 #if IS_ENABLED(CONFIG_INPUT)
35 #include <linux/input.h>
36 #include <linux/usb/input.h>
37 #endif
38 
39 /* global values */
40 #define DEF_NURBS 3		/* default number of URBs */
41 #if DEF_NURBS > MAX_NURBS
42 #error "DEF_NURBS too big"
43 #endif
44 
45 MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
46 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
47 MODULE_LICENSE("GPL");
48 MODULE_VERSION(GSPCA_VERSION);
49 
50 int gspca_debug;
51 EXPORT_SYMBOL(gspca_debug);
52 
53 static void PDEBUG_MODE(struct gspca_dev *gspca_dev, int debug, char *txt,
54 			__u32 pixfmt, int w, int h)
55 {
56 	if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
57 		gspca_dbg(gspca_dev, debug, "%s %c%c%c%c %dx%d\n",
58 			  txt,
59 			  pixfmt & 0xff,
60 			  (pixfmt >> 8) & 0xff,
61 			  (pixfmt >> 16) & 0xff,
62 			  pixfmt >> 24,
63 			  w, h);
64 	} else {
65 		gspca_dbg(gspca_dev, debug, "%s 0x%08x %dx%d\n",
66 			  txt,
67 			  pixfmt,
68 			  w, h);
69 	}
70 }
71 
72 /* specific memory types - !! should be different from V4L2_MEMORY_xxx */
73 #define GSPCA_MEMORY_NO 0	/* V4L2_MEMORY_xxx starts from 1 */
74 #define GSPCA_MEMORY_READ 7
75 
76 /*
77  * Input and interrupt endpoint handling functions
78  */
79 #if IS_ENABLED(CONFIG_INPUT)
80 static void int_irq(struct urb *urb)
81 {
82 	struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
83 	int ret;
84 
85 	ret = urb->status;
86 	switch (ret) {
87 	case 0:
88 		if (gspca_dev->sd_desc->int_pkt_scan(gspca_dev,
89 		    urb->transfer_buffer, urb->actual_length) < 0) {
90 			gspca_err(gspca_dev, "Unknown packet received\n");
91 		}
92 		break;
93 
94 	case -ENOENT:
95 	case -ECONNRESET:
96 	case -ENODEV:
97 	case -ESHUTDOWN:
98 		/* Stop is requested either by software or hardware is gone,
99 		 * keep the ret value non-zero and don't resubmit later.
100 		 */
101 		break;
102 
103 	default:
104 		gspca_err(gspca_dev, "URB error %i, resubmitting\n",
105 			  urb->status);
106 		urb->status = 0;
107 		ret = 0;
108 	}
109 
110 	if (ret == 0) {
111 		ret = usb_submit_urb(urb, GFP_ATOMIC);
112 		if (ret < 0)
113 			pr_err("Resubmit URB failed with error %i\n", ret);
114 	}
115 }
116 
117 static int gspca_input_connect(struct gspca_dev *dev)
118 {
119 	struct input_dev *input_dev;
120 	int err = 0;
121 
122 	dev->input_dev = NULL;
123 	if (dev->sd_desc->int_pkt_scan || dev->sd_desc->other_input)  {
124 		input_dev = input_allocate_device();
125 		if (!input_dev)
126 			return -ENOMEM;
127 
128 		usb_make_path(dev->dev, dev->phys, sizeof(dev->phys));
129 		strlcat(dev->phys, "/input0", sizeof(dev->phys));
130 
131 		input_dev->name = dev->sd_desc->name;
132 		input_dev->phys = dev->phys;
133 
134 		usb_to_input_id(dev->dev, &input_dev->id);
135 
136 		input_dev->evbit[0] = BIT_MASK(EV_KEY);
137 		input_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
138 		input_dev->dev.parent = &dev->dev->dev;
139 
140 		err = input_register_device(input_dev);
141 		if (err) {
142 			pr_err("Input device registration failed with error %i\n",
143 			       err);
144 			input_dev->dev.parent = NULL;
145 			input_free_device(input_dev);
146 		} else {
147 			dev->input_dev = input_dev;
148 		}
149 	}
150 
151 	return err;
152 }
153 
154 static int alloc_and_submit_int_urb(struct gspca_dev *gspca_dev,
155 			  struct usb_endpoint_descriptor *ep)
156 {
157 	unsigned int buffer_len;
158 	int interval;
159 	struct urb *urb;
160 	struct usb_device *dev;
161 	void *buffer = NULL;
162 	int ret = -EINVAL;
163 
164 	buffer_len = le16_to_cpu(ep->wMaxPacketSize);
165 	interval = ep->bInterval;
166 	gspca_dbg(gspca_dev, D_CONF, "found int in endpoint: 0x%x, buffer_len=%u, interval=%u\n",
167 		  ep->bEndpointAddress, buffer_len, interval);
168 
169 	dev = gspca_dev->dev;
170 
171 	urb = usb_alloc_urb(0, GFP_KERNEL);
172 	if (!urb) {
173 		ret = -ENOMEM;
174 		goto error;
175 	}
176 
177 	buffer = usb_alloc_coherent(dev, buffer_len,
178 				GFP_KERNEL, &urb->transfer_dma);
179 	if (!buffer) {
180 		ret = -ENOMEM;
181 		goto error_buffer;
182 	}
183 	usb_fill_int_urb(urb, dev,
184 		usb_rcvintpipe(dev, ep->bEndpointAddress),
185 		buffer, buffer_len,
186 		int_irq, (void *)gspca_dev, interval);
187 	urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
188 	ret = usb_submit_urb(urb, GFP_KERNEL);
189 	if (ret < 0) {
190 		gspca_err(gspca_dev, "submit int URB failed with error %i\n",
191 			  ret);
192 		goto error_submit;
193 	}
194 	gspca_dev->int_urb = urb;
195 	return ret;
196 
197 error_submit:
198 	usb_free_coherent(dev,
199 			  urb->transfer_buffer_length,
200 			  urb->transfer_buffer,
201 			  urb->transfer_dma);
202 error_buffer:
203 	usb_free_urb(urb);
204 error:
205 	return ret;
206 }
207 
208 static void gspca_input_create_urb(struct gspca_dev *gspca_dev)
209 {
210 	struct usb_interface *intf;
211 	struct usb_host_interface *intf_desc;
212 	struct usb_endpoint_descriptor *ep;
213 	int i;
214 
215 	if (gspca_dev->sd_desc->int_pkt_scan)  {
216 		intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
217 		intf_desc = intf->cur_altsetting;
218 		for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
219 			ep = &intf_desc->endpoint[i].desc;
220 			if (usb_endpoint_dir_in(ep) &&
221 			    usb_endpoint_xfer_int(ep)) {
222 
223 				alloc_and_submit_int_urb(gspca_dev, ep);
224 				break;
225 			}
226 		}
227 	}
228 }
229 
230 static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
231 {
232 	struct urb *urb;
233 
234 	urb = gspca_dev->int_urb;
235 	if (urb) {
236 		gspca_dev->int_urb = NULL;
237 		usb_kill_urb(urb);
238 		usb_free_coherent(gspca_dev->dev,
239 				  urb->transfer_buffer_length,
240 				  urb->transfer_buffer,
241 				  urb->transfer_dma);
242 		usb_free_urb(urb);
243 	}
244 }
245 #else
246 static inline void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
247 {
248 }
249 
250 static inline void gspca_input_create_urb(struct gspca_dev *gspca_dev)
251 {
252 }
253 
254 static inline int gspca_input_connect(struct gspca_dev *dev)
255 {
256 	return 0;
257 }
258 #endif
259 
260 /*
261  * fill a video frame from an URB and resubmit
262  */
263 static void fill_frame(struct gspca_dev *gspca_dev,
264 			struct urb *urb)
265 {
266 	u8 *data;		/* address of data in the iso message */
267 	int i, len, st;
268 	cam_pkt_op pkt_scan;
269 
270 	if (urb->status != 0) {
271 		if (urb->status == -ESHUTDOWN)
272 			return;		/* disconnection */
273 #ifdef CONFIG_PM
274 		if (gspca_dev->frozen)
275 			return;
276 #endif
277 		gspca_err(gspca_dev, "urb status: %d\n", urb->status);
278 		urb->status = 0;
279 		goto resubmit;
280 	}
281 	pkt_scan = gspca_dev->sd_desc->pkt_scan;
282 	for (i = 0; i < urb->number_of_packets; i++) {
283 		len = urb->iso_frame_desc[i].actual_length;
284 
285 		/* check the packet status and length */
286 		st = urb->iso_frame_desc[i].status;
287 		if (st) {
288 			gspca_dbg(gspca_dev, D_PACK, "ISOC data error: [%d] len=%d, status=%d\n",
289 			       i, len, st);
290 			gspca_dev->last_packet_type = DISCARD_PACKET;
291 			continue;
292 		}
293 		if (len == 0) {
294 			if (gspca_dev->empty_packet == 0)
295 				gspca_dev->empty_packet = 1;
296 			continue;
297 		}
298 
299 		/* let the packet be analyzed by the subdriver */
300 		gspca_dbg(gspca_dev, D_PACK, "packet [%d] o:%d l:%d\n",
301 			  i, urb->iso_frame_desc[i].offset, len);
302 		data = (u8 *) urb->transfer_buffer
303 					+ urb->iso_frame_desc[i].offset;
304 		pkt_scan(gspca_dev, data, len);
305 	}
306 
307 resubmit:
308 	if (!gspca_dev->streaming)
309 		return;
310 	/* resubmit the URB */
311 	st = usb_submit_urb(urb, GFP_ATOMIC);
312 	if (st < 0)
313 		pr_err("usb_submit_urb() ret %d\n", st);
314 }
315 
316 /*
317  * ISOC message interrupt from the USB device
318  *
319  * Analyse each packet and call the subdriver for copy to the frame buffer.
320  */
321 static void isoc_irq(struct urb *urb)
322 {
323 	struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
324 
325 	gspca_dbg(gspca_dev, D_PACK, "isoc irq\n");
326 	if (!gspca_dev->streaming)
327 		return;
328 	fill_frame(gspca_dev, urb);
329 }
330 
331 /*
332  * bulk message interrupt from the USB device
333  */
334 static void bulk_irq(struct urb *urb)
335 {
336 	struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
337 	int st;
338 
339 	gspca_dbg(gspca_dev, D_PACK, "bulk irq\n");
340 	if (!gspca_dev->streaming)
341 		return;
342 	switch (urb->status) {
343 	case 0:
344 		break;
345 	case -ESHUTDOWN:
346 		return;		/* disconnection */
347 	default:
348 #ifdef CONFIG_PM
349 		if (gspca_dev->frozen)
350 			return;
351 #endif
352 		gspca_err(gspca_dev, "urb status: %d\n", urb->status);
353 		urb->status = 0;
354 		goto resubmit;
355 	}
356 
357 	gspca_dbg(gspca_dev, D_PACK, "packet l:%d\n", urb->actual_length);
358 	gspca_dev->sd_desc->pkt_scan(gspca_dev,
359 				urb->transfer_buffer,
360 				urb->actual_length);
361 
362 resubmit:
363 	if (!gspca_dev->streaming)
364 		return;
365 	/* resubmit the URB */
366 	if (gspca_dev->cam.bulk_nurbs != 0) {
367 		st = usb_submit_urb(urb, GFP_ATOMIC);
368 		if (st < 0)
369 			pr_err("usb_submit_urb() ret %d\n", st);
370 	}
371 }
372 
373 /*
374  * add data to the current frame
375  *
376  * This function is called by the subdrivers at interrupt level.
377  *
378  * To build a frame, these ones must add
379  *	- one FIRST_PACKET
380  *	- 0 or many INTER_PACKETs
381  *	- one LAST_PACKET
382  * DISCARD_PACKET invalidates the whole frame.
383  */
384 void gspca_frame_add(struct gspca_dev *gspca_dev,
385 			enum gspca_packet_type packet_type,
386 			const u8 *data,
387 			int len)
388 {
389 	struct gspca_buffer *buf;
390 	unsigned long flags;
391 
392 	gspca_dbg(gspca_dev, D_PACK, "add t:%d l:%d\n",	packet_type, len);
393 
394 	spin_lock_irqsave(&gspca_dev->qlock, flags);
395 	buf = list_first_entry_or_null(&gspca_dev->buf_list,
396 				       typeof(*buf), list);
397 	spin_unlock_irqrestore(&gspca_dev->qlock, flags);
398 
399 	if (packet_type == FIRST_PACKET) {
400 		/* if there is no queued buffer, discard the whole frame */
401 		if (!buf) {
402 			gspca_dev->last_packet_type = DISCARD_PACKET;
403 			gspca_dev->sequence++;
404 			return;
405 		}
406 		gspca_dev->image = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
407 		gspca_dev->image_len = 0;
408 	} else {
409 		switch (gspca_dev->last_packet_type) {
410 		case DISCARD_PACKET:
411 			if (packet_type == LAST_PACKET) {
412 				gspca_dev->last_packet_type = packet_type;
413 				gspca_dev->image = NULL;
414 				gspca_dev->image_len = 0;
415 			}
416 			return;
417 		case LAST_PACKET:
418 			return;
419 		}
420 	}
421 
422 	/* append the packet to the frame buffer */
423 	if (len > 0) {
424 		if (gspca_dev->image_len + len > PAGE_ALIGN(gspca_dev->pixfmt.sizeimage)) {
425 			gspca_err(gspca_dev, "frame overflow %d > %d\n",
426 				  gspca_dev->image_len + len,
427 				  PAGE_ALIGN(gspca_dev->pixfmt.sizeimage));
428 			packet_type = DISCARD_PACKET;
429 		} else {
430 /* !! image is NULL only when last pkt is LAST or DISCARD
431 			if (gspca_dev->image == NULL) {
432 				pr_err("gspca_frame_add() image == NULL\n");
433 				return;
434 			}
435  */
436 			memcpy(gspca_dev->image + gspca_dev->image_len,
437 				data, len);
438 			gspca_dev->image_len += len;
439 		}
440 	}
441 	gspca_dev->last_packet_type = packet_type;
442 
443 	/* if last packet, invalidate packet concatenation until
444 	 * next first packet, wake up the application and advance
445 	 * in the queue */
446 	if (packet_type == LAST_PACKET) {
447 		spin_lock_irqsave(&gspca_dev->qlock, flags);
448 		list_del(&buf->list);
449 		spin_unlock_irqrestore(&gspca_dev->qlock, flags);
450 		buf->vb.vb2_buf.timestamp = ktime_get_ns();
451 		vb2_set_plane_payload(&buf->vb.vb2_buf, 0,
452 				      gspca_dev->image_len);
453 		buf->vb.sequence = gspca_dev->sequence++;
454 		buf->vb.field = V4L2_FIELD_NONE;
455 		gspca_dbg(gspca_dev, D_FRAM, "frame complete len:%d\n",
456 			  gspca_dev->image_len);
457 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
458 		gspca_dev->image = NULL;
459 		gspca_dev->image_len = 0;
460 	}
461 }
462 EXPORT_SYMBOL(gspca_frame_add);
463 
464 static void destroy_urbs(struct gspca_dev *gspca_dev)
465 {
466 	struct urb *urb;
467 	unsigned int i;
468 
469 	gspca_dbg(gspca_dev, D_STREAM, "kill transfer\n");
470 
471 	/* Killing all URBs guarantee that no URB completion
472 	 * handler is running. Therefore, there shouldn't
473 	 * be anyone trying to access gspca_dev->urb[i]
474 	 */
475 	for (i = 0; i < MAX_NURBS; i++)
476 		usb_kill_urb(gspca_dev->urb[i]);
477 
478 	gspca_dbg(gspca_dev, D_STREAM, "releasing urbs\n");
479 	for (i = 0; i < MAX_NURBS; i++) {
480 		urb = gspca_dev->urb[i];
481 		if (!urb)
482 			continue;
483 		gspca_dev->urb[i] = NULL;
484 		usb_free_coherent(gspca_dev->dev,
485 				  urb->transfer_buffer_length,
486 				  urb->transfer_buffer,
487 				  urb->transfer_dma);
488 		usb_free_urb(urb);
489 	}
490 }
491 
492 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
493 {
494 	int ret;
495 
496 	if (gspca_dev->alt == 0)
497 		return 0;
498 	ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
499 	if (ret < 0)
500 		pr_err("set alt 0 err %d\n", ret);
501 	return ret;
502 }
503 
504 /*
505  * look for an input transfer endpoint in an alternate setting.
506  *
507  * If xfer_ep is invalid, return the first valid ep found, otherwise
508  * look for exactly the ep with address equal to xfer_ep.
509  */
510 static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
511 					  int xfer, int xfer_ep)
512 {
513 	struct usb_host_endpoint *ep;
514 	int i, attr;
515 
516 	for (i = 0; i < alt->desc.bNumEndpoints; i++) {
517 		ep = &alt->endpoint[i];
518 		attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
519 		if (attr == xfer
520 		    && ep->desc.wMaxPacketSize != 0
521 		    && usb_endpoint_dir_in(&ep->desc)
522 		    && (xfer_ep < 0 || ep->desc.bEndpointAddress == xfer_ep))
523 			return ep;
524 	}
525 	return NULL;
526 }
527 
528 /* compute the minimum bandwidth for the current transfer */
529 static u32 which_bandwidth(struct gspca_dev *gspca_dev)
530 {
531 	u32 bandwidth;
532 
533 	/* get the (max) image size */
534 	bandwidth = gspca_dev->pixfmt.sizeimage;
535 
536 	/* if the image is compressed, estimate its mean size */
537 	if (!gspca_dev->cam.needs_full_bandwidth &&
538 	    bandwidth < gspca_dev->pixfmt.width *
539 				gspca_dev->pixfmt.height)
540 		bandwidth = bandwidth * 3 / 8;	/* 0.375 */
541 
542 	/* estimate the frame rate */
543 	if (gspca_dev->sd_desc->get_streamparm) {
544 		struct v4l2_streamparm parm;
545 
546 		gspca_dev->sd_desc->get_streamparm(gspca_dev, &parm);
547 		bandwidth *= parm.parm.capture.timeperframe.denominator;
548 		bandwidth /= parm.parm.capture.timeperframe.numerator;
549 	} else {
550 
551 		/* don't hope more than 15 fps with USB 1.1 and
552 		 * image resolution >= 640x480 */
553 		if (gspca_dev->pixfmt.width >= 640
554 		 && gspca_dev->dev->speed == USB_SPEED_FULL)
555 			bandwidth *= 15;		/* 15 fps */
556 		else
557 			bandwidth *= 30;		/* 30 fps */
558 	}
559 
560 	gspca_dbg(gspca_dev, D_STREAM, "min bandwidth: %d\n", bandwidth);
561 	return bandwidth;
562 }
563 
564 /* endpoint table */
565 #define MAX_ALT 16
566 struct ep_tb_s {
567 	u32 alt;
568 	u32 bandwidth;
569 };
570 
571 /*
572  * build the table of the endpoints
573  * and compute the minimum bandwidth for the image transfer
574  */
575 static int build_isoc_ep_tb(struct gspca_dev *gspca_dev,
576 			struct usb_interface *intf,
577 			struct ep_tb_s *ep_tb)
578 {
579 	struct usb_host_endpoint *ep;
580 	int i, j, nbalt, psize, found;
581 	u32 bandwidth, last_bw;
582 
583 	nbalt = intf->num_altsetting;
584 	if (nbalt > MAX_ALT)
585 		nbalt = MAX_ALT;	/* fixme: should warn */
586 
587 	/* build the endpoint table */
588 	i = 0;
589 	last_bw = 0;
590 	for (;;) {
591 		ep_tb->bandwidth = 2000 * 2000 * 120;
592 		found = 0;
593 		for (j = 0; j < nbalt; j++) {
594 			ep = alt_xfer(&intf->altsetting[j],
595 				      USB_ENDPOINT_XFER_ISOC,
596 				      gspca_dev->xfer_ep);
597 			if (ep == NULL)
598 				continue;
599 			if (ep->desc.bInterval == 0) {
600 				pr_err("alt %d iso endp with 0 interval\n", j);
601 				continue;
602 			}
603 			psize = le16_to_cpu(ep->desc.wMaxPacketSize);
604 			psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
605 			bandwidth = psize * 1000;
606 			if (gspca_dev->dev->speed == USB_SPEED_HIGH
607 			 || gspca_dev->dev->speed >= USB_SPEED_SUPER)
608 				bandwidth *= 8;
609 			bandwidth /= 1 << (ep->desc.bInterval - 1);
610 			if (bandwidth <= last_bw)
611 				continue;
612 			if (bandwidth < ep_tb->bandwidth) {
613 				ep_tb->bandwidth = bandwidth;
614 				ep_tb->alt = j;
615 				found = 1;
616 			}
617 		}
618 		if (!found)
619 			break;
620 		gspca_dbg(gspca_dev, D_STREAM, "alt %d bandwidth %d\n",
621 			  ep_tb->alt, ep_tb->bandwidth);
622 		last_bw = ep_tb->bandwidth;
623 		i++;
624 		ep_tb++;
625 	}
626 
627 	/*
628 	 * If the camera:
629 	 * has a usb audio class interface (a built in usb mic); and
630 	 * is a usb 1 full speed device; and
631 	 * uses the max full speed iso bandwidth; and
632 	 * and has more than 1 alt setting
633 	 * then skip the highest alt setting to spare bandwidth for the mic
634 	 */
635 	if (gspca_dev->audio &&
636 			gspca_dev->dev->speed == USB_SPEED_FULL &&
637 			last_bw >= 1000000 &&
638 			i > 1) {
639 		gspca_dbg(gspca_dev, D_STREAM, "dev has usb audio, skipping highest alt\n");
640 		i--;
641 		ep_tb--;
642 	}
643 
644 	/* get the requested bandwidth and start at the highest atlsetting */
645 	bandwidth = which_bandwidth(gspca_dev);
646 	ep_tb--;
647 	while (i > 1) {
648 		ep_tb--;
649 		if (ep_tb->bandwidth < bandwidth)
650 			break;
651 		i--;
652 	}
653 	return i;
654 }
655 
656 /*
657  * create the URBs for image transfer
658  */
659 static int create_urbs(struct gspca_dev *gspca_dev,
660 			struct usb_host_endpoint *ep)
661 {
662 	struct urb *urb;
663 	int n, nurbs, i, psize, npkt, bsize;
664 
665 	/* calculate the packet size and the number of packets */
666 	psize = le16_to_cpu(ep->desc.wMaxPacketSize);
667 
668 	if (!gspca_dev->cam.bulk) {		/* isoc */
669 
670 		/* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
671 		if (gspca_dev->pkt_size == 0)
672 			psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
673 		else
674 			psize = gspca_dev->pkt_size;
675 		npkt = gspca_dev->cam.npkt;
676 		if (npkt == 0)
677 			npkt = 32;		/* default value */
678 		bsize = psize * npkt;
679 		gspca_dbg(gspca_dev, D_STREAM,
680 			  "isoc %d pkts size %d = bsize:%d\n",
681 			  npkt, psize, bsize);
682 		nurbs = DEF_NURBS;
683 	} else {				/* bulk */
684 		npkt = 0;
685 		bsize = gspca_dev->cam.bulk_size;
686 		if (bsize == 0)
687 			bsize = psize;
688 		gspca_dbg(gspca_dev, D_STREAM, "bulk bsize:%d\n", bsize);
689 		if (gspca_dev->cam.bulk_nurbs != 0)
690 			nurbs = gspca_dev->cam.bulk_nurbs;
691 		else
692 			nurbs = 1;
693 	}
694 
695 	for (n = 0; n < nurbs; n++) {
696 		urb = usb_alloc_urb(npkt, GFP_KERNEL);
697 		if (!urb)
698 			return -ENOMEM;
699 		gspca_dev->urb[n] = urb;
700 		urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
701 						bsize,
702 						GFP_KERNEL,
703 						&urb->transfer_dma);
704 
705 		if (urb->transfer_buffer == NULL) {
706 			pr_err("usb_alloc_coherent failed\n");
707 			return -ENOMEM;
708 		}
709 		urb->dev = gspca_dev->dev;
710 		urb->context = gspca_dev;
711 		urb->transfer_buffer_length = bsize;
712 		if (npkt != 0) {		/* ISOC */
713 			urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
714 						    ep->desc.bEndpointAddress);
715 			urb->transfer_flags = URB_ISO_ASAP
716 					| URB_NO_TRANSFER_DMA_MAP;
717 			urb->interval = 1 << (ep->desc.bInterval - 1);
718 			urb->complete = isoc_irq;
719 			urb->number_of_packets = npkt;
720 			for (i = 0; i < npkt; i++) {
721 				urb->iso_frame_desc[i].length = psize;
722 				urb->iso_frame_desc[i].offset = psize * i;
723 			}
724 		} else {		/* bulk */
725 			urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
726 						ep->desc.bEndpointAddress);
727 			urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
728 			urb->complete = bulk_irq;
729 		}
730 	}
731 	return 0;
732 }
733 
734 /* Note: both the queue and the usb locks should be held when calling this */
735 static void gspca_stream_off(struct gspca_dev *gspca_dev)
736 {
737 	gspca_dev->streaming = false;
738 	gspca_dev->usb_err = 0;
739 	if (gspca_dev->sd_desc->stopN)
740 		gspca_dev->sd_desc->stopN(gspca_dev);
741 	destroy_urbs(gspca_dev);
742 	gspca_input_destroy_urb(gspca_dev);
743 	gspca_set_alt0(gspca_dev);
744 	if (gspca_dev->present)
745 		gspca_input_create_urb(gspca_dev);
746 	if (gspca_dev->sd_desc->stop0)
747 		gspca_dev->sd_desc->stop0(gspca_dev);
748 	gspca_dbg(gspca_dev, D_STREAM, "stream off OK\n");
749 }
750 
751 /*
752  * start the USB transfer
753  */
754 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
755 {
756 	struct usb_interface *intf;
757 	struct usb_host_endpoint *ep;
758 	struct urb *urb;
759 	struct ep_tb_s ep_tb[MAX_ALT];
760 	int n, ret, xfer, alt, alt_idx;
761 
762 	/* reset the streaming variables */
763 	gspca_dev->image = NULL;
764 	gspca_dev->image_len = 0;
765 	gspca_dev->last_packet_type = DISCARD_PACKET;
766 
767 	gspca_dev->usb_err = 0;
768 
769 	/* do the specific subdriver stuff before endpoint selection */
770 	intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
771 	gspca_dev->alt = gspca_dev->cam.bulk ? intf->num_altsetting : 0;
772 	if (gspca_dev->sd_desc->isoc_init) {
773 		ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
774 		if (ret < 0)
775 			return ret;
776 	}
777 	xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
778 				   : USB_ENDPOINT_XFER_ISOC;
779 
780 	/* if bulk or the subdriver forced an altsetting, get the endpoint */
781 	if (gspca_dev->alt != 0) {
782 		gspca_dev->alt--;	/* (previous version compatibility) */
783 		ep = alt_xfer(&intf->altsetting[gspca_dev->alt], xfer,
784 			      gspca_dev->xfer_ep);
785 		if (ep == NULL) {
786 			pr_err("bad altsetting %d\n", gspca_dev->alt);
787 			return -EIO;
788 		}
789 		ep_tb[0].alt = gspca_dev->alt;
790 		alt_idx = 1;
791 	} else {
792 		/* else, compute the minimum bandwidth
793 		 * and build the endpoint table */
794 		alt_idx = build_isoc_ep_tb(gspca_dev, intf, ep_tb);
795 		if (alt_idx <= 0) {
796 			pr_err("no transfer endpoint found\n");
797 			return -EIO;
798 		}
799 	}
800 
801 	/* set the highest alternate setting and
802 	 * loop until urb submit succeeds */
803 	gspca_input_destroy_urb(gspca_dev);
804 
805 	gspca_dev->alt = ep_tb[--alt_idx].alt;
806 	alt = -1;
807 	for (;;) {
808 		if (alt != gspca_dev->alt) {
809 			alt = gspca_dev->alt;
810 			if (intf->num_altsetting > 1) {
811 				ret = usb_set_interface(gspca_dev->dev,
812 							gspca_dev->iface,
813 							alt);
814 				if (ret < 0) {
815 					if (ret == -ENOSPC)
816 						goto retry; /*fixme: ugly*/
817 					pr_err("set alt %d err %d\n", alt, ret);
818 					goto out;
819 				}
820 			}
821 		}
822 		if (!gspca_dev->cam.no_urb_create) {
823 			gspca_dbg(gspca_dev, D_STREAM, "init transfer alt %d\n",
824 				  alt);
825 			ret = create_urbs(gspca_dev,
826 				alt_xfer(&intf->altsetting[alt], xfer,
827 					 gspca_dev->xfer_ep));
828 			if (ret < 0) {
829 				destroy_urbs(gspca_dev);
830 				goto out;
831 			}
832 		}
833 
834 		/* clear the bulk endpoint */
835 		if (gspca_dev->cam.bulk)
836 			usb_clear_halt(gspca_dev->dev,
837 					gspca_dev->urb[0]->pipe);
838 
839 		/* start the cam */
840 		ret = gspca_dev->sd_desc->start(gspca_dev);
841 		if (ret < 0) {
842 			destroy_urbs(gspca_dev);
843 			goto out;
844 		}
845 		v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
846 		gspca_dev->streaming = true;
847 
848 		/* some bulk transfers are started by the subdriver */
849 		if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
850 			break;
851 
852 		/* submit the URBs */
853 		for (n = 0; n < MAX_NURBS; n++) {
854 			urb = gspca_dev->urb[n];
855 			if (urb == NULL)
856 				break;
857 			ret = usb_submit_urb(urb, GFP_KERNEL);
858 			if (ret < 0)
859 				break;
860 		}
861 		if (ret >= 0)
862 			break;			/* transfer is started */
863 
864 		/* something when wrong
865 		 * stop the webcam and free the transfer resources */
866 		gspca_stream_off(gspca_dev);
867 		if (ret != -ENOSPC) {
868 			pr_err("usb_submit_urb alt %d err %d\n",
869 			       gspca_dev->alt, ret);
870 			goto out;
871 		}
872 
873 		/* the bandwidth is not wide enough
874 		 * negotiate or try a lower alternate setting */
875 retry:
876 		gspca_err(gspca_dev, "alt %d - bandwidth not wide enough, trying again\n",
877 			  alt);
878 		msleep(20);	/* wait for kill complete */
879 		if (gspca_dev->sd_desc->isoc_nego) {
880 			ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
881 			if (ret < 0)
882 				goto out;
883 		} else {
884 			if (alt_idx <= 0) {
885 				pr_err("no transfer endpoint found\n");
886 				ret = -EIO;
887 				goto out;
888 			}
889 			gspca_dev->alt = ep_tb[--alt_idx].alt;
890 		}
891 	}
892 out:
893 	gspca_input_create_urb(gspca_dev);
894 	return ret;
895 }
896 
897 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
898 {
899 	int i;
900 
901 	i = gspca_dev->cam.nmodes - 1;	/* take the highest mode */
902 	gspca_dev->curr_mode = i;
903 	gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i];
904 
905 	/* does nothing if ctrl_handler == NULL */
906 	v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
907 }
908 
909 static int wxh_to_mode(struct gspca_dev *gspca_dev,
910 			int width, int height, u32 pixelformat)
911 {
912 	int i;
913 
914 	for (i = 0; i < gspca_dev->cam.nmodes; i++) {
915 		if (width == gspca_dev->cam.cam_mode[i].width
916 		    && height == gspca_dev->cam.cam_mode[i].height
917 		    && pixelformat == gspca_dev->cam.cam_mode[i].pixelformat)
918 			return i;
919 	}
920 	return -EINVAL;
921 }
922 
923 static int wxh_to_nearest_mode(struct gspca_dev *gspca_dev,
924 			int width, int height, u32 pixelformat)
925 {
926 	int i;
927 
928 	for (i = gspca_dev->cam.nmodes; --i > 0; ) {
929 		if (width >= gspca_dev->cam.cam_mode[i].width
930 		    && height >= gspca_dev->cam.cam_mode[i].height
931 		    && pixelformat == gspca_dev->cam.cam_mode[i].pixelformat)
932 			return i;
933 	}
934 	for (i = gspca_dev->cam.nmodes; --i > 0; ) {
935 		if (width >= gspca_dev->cam.cam_mode[i].width
936 		    && height >= gspca_dev->cam.cam_mode[i].height)
937 			break;
938 	}
939 	return i;
940 }
941 
942 /*
943  * search a mode with the right pixel format
944  */
945 static int gspca_get_mode(struct gspca_dev *gspca_dev,
946 			int mode,
947 			int pixfmt)
948 {
949 	int modeU, modeD;
950 
951 	modeU = modeD = mode;
952 	while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
953 		if (--modeD >= 0) {
954 			if (gspca_dev->cam.cam_mode[modeD].pixelformat
955 								== pixfmt)
956 				return modeD;
957 		}
958 		if (++modeU < gspca_dev->cam.nmodes) {
959 			if (gspca_dev->cam.cam_mode[modeU].pixelformat
960 								== pixfmt)
961 				return modeU;
962 		}
963 	}
964 	return -EINVAL;
965 }
966 
967 #ifdef CONFIG_VIDEO_ADV_DEBUG
968 static int vidioc_g_chip_info(struct file *file, void *priv,
969 				struct v4l2_dbg_chip_info *chip)
970 {
971 	struct gspca_dev *gspca_dev = video_drvdata(file);
972 
973 	gspca_dev->usb_err = 0;
974 	if (gspca_dev->sd_desc->get_chip_info)
975 		return gspca_dev->sd_desc->get_chip_info(gspca_dev, chip);
976 	return chip->match.addr ? -EINVAL : 0;
977 }
978 
979 static int vidioc_g_register(struct file *file, void *priv,
980 		struct v4l2_dbg_register *reg)
981 {
982 	struct gspca_dev *gspca_dev = video_drvdata(file);
983 
984 	gspca_dev->usb_err = 0;
985 	return gspca_dev->sd_desc->get_register(gspca_dev, reg);
986 }
987 
988 static int vidioc_s_register(struct file *file, void *priv,
989 		const struct v4l2_dbg_register *reg)
990 {
991 	struct gspca_dev *gspca_dev = video_drvdata(file);
992 
993 	gspca_dev->usb_err = 0;
994 	return gspca_dev->sd_desc->set_register(gspca_dev, reg);
995 }
996 #endif
997 
998 static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
999 				struct v4l2_fmtdesc *fmtdesc)
1000 {
1001 	struct gspca_dev *gspca_dev = video_drvdata(file);
1002 	int i, j, index;
1003 	__u32 fmt_tb[8];
1004 
1005 	/* give an index to each format */
1006 	index = 0;
1007 	for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
1008 		fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
1009 		j = 0;
1010 		for (;;) {
1011 			if (fmt_tb[j] == fmt_tb[index])
1012 				break;
1013 			j++;
1014 		}
1015 		if (j == index) {
1016 			if (fmtdesc->index == index)
1017 				break;		/* new format */
1018 			index++;
1019 			if (index >= ARRAY_SIZE(fmt_tb))
1020 				return -EINVAL;
1021 		}
1022 	}
1023 	if (i < 0)
1024 		return -EINVAL;		/* no more format */
1025 
1026 	fmtdesc->pixelformat = fmt_tb[index];
1027 	return 0;
1028 }
1029 
1030 static int vidioc_g_fmt_vid_cap(struct file *file, void *_priv,
1031 				struct v4l2_format *fmt)
1032 {
1033 	struct gspca_dev *gspca_dev = video_drvdata(file);
1034 	u32 priv = fmt->fmt.pix.priv;
1035 
1036 	fmt->fmt.pix = gspca_dev->pixfmt;
1037 	/* some drivers use priv internally, so keep the original value */
1038 	fmt->fmt.pix.priv = priv;
1039 	return 0;
1040 }
1041 
1042 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
1043 			struct v4l2_format *fmt)
1044 {
1045 	int w, h, mode, mode2;
1046 
1047 	w = fmt->fmt.pix.width;
1048 	h = fmt->fmt.pix.height;
1049 
1050 	PDEBUG_MODE(gspca_dev, D_CONF, "try fmt cap",
1051 		    fmt->fmt.pix.pixelformat, w, h);
1052 
1053 	/* search the nearest mode for width and height */
1054 	mode = wxh_to_nearest_mode(gspca_dev, w, h, fmt->fmt.pix.pixelformat);
1055 
1056 	/* OK if right palette */
1057 	if (gspca_dev->cam.cam_mode[mode].pixelformat
1058 						!= fmt->fmt.pix.pixelformat) {
1059 
1060 		/* else, search the closest mode with the same pixel format */
1061 		mode2 = gspca_get_mode(gspca_dev, mode,
1062 					fmt->fmt.pix.pixelformat);
1063 		if (mode2 >= 0)
1064 			mode = mode2;
1065 	}
1066 	fmt->fmt.pix = gspca_dev->cam.cam_mode[mode];
1067 	if (gspca_dev->sd_desc->try_fmt) {
1068 		/* pass original resolution to subdriver try_fmt */
1069 		fmt->fmt.pix.width = w;
1070 		fmt->fmt.pix.height = h;
1071 		gspca_dev->sd_desc->try_fmt(gspca_dev, fmt);
1072 	}
1073 	return mode;			/* used when s_fmt */
1074 }
1075 
1076 static int vidioc_try_fmt_vid_cap(struct file *file, void *_priv,
1077 				  struct v4l2_format *fmt)
1078 {
1079 	struct gspca_dev *gspca_dev = video_drvdata(file);
1080 	u32 priv = fmt->fmt.pix.priv;
1081 
1082 	if (try_fmt_vid_cap(gspca_dev, fmt) < 0)
1083 		return -EINVAL;
1084 	/* some drivers use priv internally, so keep the original value */
1085 	fmt->fmt.pix.priv = priv;
1086 	return 0;
1087 }
1088 
1089 static int vidioc_s_fmt_vid_cap(struct file *file, void *_priv,
1090 				struct v4l2_format *fmt)
1091 {
1092 	struct gspca_dev *gspca_dev = video_drvdata(file);
1093 	u32 priv = fmt->fmt.pix.priv;
1094 	int mode;
1095 
1096 	if (vb2_is_busy(&gspca_dev->queue))
1097 		return -EBUSY;
1098 
1099 	mode = try_fmt_vid_cap(gspca_dev, fmt);
1100 	if (mode < 0)
1101 		return -EINVAL;
1102 
1103 	gspca_dev->curr_mode = mode;
1104 	if (gspca_dev->sd_desc->try_fmt)
1105 		/* subdriver try_fmt can modify format parameters */
1106 		gspca_dev->pixfmt = fmt->fmt.pix;
1107 	else
1108 		gspca_dev->pixfmt = gspca_dev->cam.cam_mode[mode];
1109 	/* some drivers use priv internally, so keep the original value */
1110 	fmt->fmt.pix.priv = priv;
1111 	return 0;
1112 }
1113 
1114 static int vidioc_enum_framesizes(struct file *file, void *priv,
1115 				  struct v4l2_frmsizeenum *fsize)
1116 {
1117 	struct gspca_dev *gspca_dev = video_drvdata(file);
1118 	int i;
1119 	__u32 index = 0;
1120 
1121 	if (gspca_dev->sd_desc->enum_framesizes)
1122 		return gspca_dev->sd_desc->enum_framesizes(gspca_dev, fsize);
1123 
1124 	for (i = 0; i < gspca_dev->cam.nmodes; i++) {
1125 		if (fsize->pixel_format !=
1126 				gspca_dev->cam.cam_mode[i].pixelformat)
1127 			continue;
1128 
1129 		if (fsize->index == index) {
1130 			fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1131 			fsize->discrete.width =
1132 				gspca_dev->cam.cam_mode[i].width;
1133 			fsize->discrete.height =
1134 				gspca_dev->cam.cam_mode[i].height;
1135 			return 0;
1136 		}
1137 		index++;
1138 	}
1139 
1140 	return -EINVAL;
1141 }
1142 
1143 static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1144 				      struct v4l2_frmivalenum *fival)
1145 {
1146 	struct gspca_dev *gspca_dev = video_drvdata(filp);
1147 	int mode;
1148 	__u32 i;
1149 
1150 	mode = wxh_to_mode(gspca_dev, fival->width, fival->height,
1151 			   fival->pixel_format);
1152 	if (mode < 0)
1153 		return -EINVAL;
1154 
1155 	if (gspca_dev->cam.mode_framerates == NULL ||
1156 			gspca_dev->cam.mode_framerates[mode].nrates == 0)
1157 		return -EINVAL;
1158 
1159 	if (fival->pixel_format !=
1160 			gspca_dev->cam.cam_mode[mode].pixelformat)
1161 		return -EINVAL;
1162 
1163 	for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1164 		if (fival->index == i) {
1165 			fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1166 			fival->discrete.numerator = 1;
1167 			fival->discrete.denominator =
1168 				gspca_dev->cam.mode_framerates[mode].rates[i];
1169 			return 0;
1170 		}
1171 	}
1172 
1173 	return -EINVAL;
1174 }
1175 
1176 static void gspca_release(struct v4l2_device *v4l2_device)
1177 {
1178 	struct gspca_dev *gspca_dev =
1179 		container_of(v4l2_device, struct gspca_dev, v4l2_dev);
1180 
1181 	v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
1182 	v4l2_device_unregister(&gspca_dev->v4l2_dev);
1183 	kfree(gspca_dev->usb_buf);
1184 	kfree(gspca_dev);
1185 }
1186 
1187 static int vidioc_querycap(struct file *file, void  *priv,
1188 			   struct v4l2_capability *cap)
1189 {
1190 	struct gspca_dev *gspca_dev = video_drvdata(file);
1191 
1192 	strscpy((char *)cap->driver, gspca_dev->sd_desc->name,
1193 		sizeof(cap->driver));
1194 	if (gspca_dev->dev->product != NULL) {
1195 		strscpy((char *)cap->card, gspca_dev->dev->product,
1196 			sizeof(cap->card));
1197 	} else {
1198 		snprintf((char *) cap->card, sizeof cap->card,
1199 			"USB Camera (%04x:%04x)",
1200 			le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1201 			le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1202 	}
1203 	usb_make_path(gspca_dev->dev, (char *) cap->bus_info,
1204 			sizeof(cap->bus_info));
1205 	return 0;
1206 }
1207 
1208 static int vidioc_enum_input(struct file *file, void *priv,
1209 				struct v4l2_input *input)
1210 {
1211 	struct gspca_dev *gspca_dev = video_drvdata(file);
1212 
1213 	if (input->index != 0)
1214 		return -EINVAL;
1215 	input->type = V4L2_INPUT_TYPE_CAMERA;
1216 	input->status = gspca_dev->cam.input_flags;
1217 	strscpy(input->name, gspca_dev->sd_desc->name,
1218 		sizeof input->name);
1219 	return 0;
1220 }
1221 
1222 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1223 {
1224 	*i = 0;
1225 	return 0;
1226 }
1227 
1228 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1229 {
1230 	if (i > 0)
1231 		return -EINVAL;
1232 	return 0;
1233 }
1234 
1235 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1236 			struct v4l2_jpegcompression *jpegcomp)
1237 {
1238 	struct gspca_dev *gspca_dev = video_drvdata(file);
1239 
1240 	gspca_dev->usb_err = 0;
1241 	return gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1242 }
1243 
1244 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1245 			const struct v4l2_jpegcompression *jpegcomp)
1246 {
1247 	struct gspca_dev *gspca_dev = video_drvdata(file);
1248 
1249 	gspca_dev->usb_err = 0;
1250 	return gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1251 }
1252 
1253 static int vidioc_g_parm(struct file *filp, void *priv,
1254 			struct v4l2_streamparm *parm)
1255 {
1256 	struct gspca_dev *gspca_dev = video_drvdata(filp);
1257 
1258 	parm->parm.capture.readbuffers = gspca_dev->queue.min_buffers_needed;
1259 
1260 	if (!gspca_dev->sd_desc->get_streamparm)
1261 		return 0;
1262 
1263 	parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1264 	gspca_dev->usb_err = 0;
1265 	gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
1266 	return gspca_dev->usb_err;
1267 }
1268 
1269 static int vidioc_s_parm(struct file *filp, void *priv,
1270 			struct v4l2_streamparm *parm)
1271 {
1272 	struct gspca_dev *gspca_dev = video_drvdata(filp);
1273 
1274 	parm->parm.capture.readbuffers = gspca_dev->queue.min_buffers_needed;
1275 
1276 	if (!gspca_dev->sd_desc->set_streamparm) {
1277 		parm->parm.capture.capability = 0;
1278 		return 0;
1279 	}
1280 
1281 	parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1282 	gspca_dev->usb_err = 0;
1283 	gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
1284 	return gspca_dev->usb_err;
1285 }
1286 
1287 static int gspca_queue_setup(struct vb2_queue *vq,
1288 			     unsigned int *nbuffers, unsigned int *nplanes,
1289 			     unsigned int sizes[], struct device *alloc_devs[])
1290 {
1291 	struct gspca_dev *gspca_dev = vb2_get_drv_priv(vq);
1292 	unsigned int size = PAGE_ALIGN(gspca_dev->pixfmt.sizeimage);
1293 
1294 	if (*nplanes)
1295 		return sizes[0] < size ? -EINVAL : 0;
1296 	*nplanes = 1;
1297 	sizes[0] = size;
1298 	return 0;
1299 }
1300 
1301 static int gspca_buffer_prepare(struct vb2_buffer *vb)
1302 {
1303 	struct gspca_dev *gspca_dev = vb2_get_drv_priv(vb->vb2_queue);
1304 	unsigned long size = PAGE_ALIGN(gspca_dev->pixfmt.sizeimage);
1305 
1306 	if (vb2_plane_size(vb, 0) < size) {
1307 		gspca_err(gspca_dev, "buffer too small (%lu < %lu)\n",
1308 			 vb2_plane_size(vb, 0), size);
1309 		return -EINVAL;
1310 	}
1311 	return 0;
1312 }
1313 
1314 static void gspca_buffer_finish(struct vb2_buffer *vb)
1315 {
1316 	struct gspca_dev *gspca_dev = vb2_get_drv_priv(vb->vb2_queue);
1317 
1318 	if (!gspca_dev->sd_desc->dq_callback)
1319 		return;
1320 
1321 	gspca_dev->usb_err = 0;
1322 	if (gspca_dev->present)
1323 		gspca_dev->sd_desc->dq_callback(gspca_dev);
1324 }
1325 
1326 static void gspca_buffer_queue(struct vb2_buffer *vb)
1327 {
1328 	struct gspca_dev *gspca_dev = vb2_get_drv_priv(vb->vb2_queue);
1329 	struct gspca_buffer *buf = to_gspca_buffer(vb);
1330 	unsigned long flags;
1331 
1332 	spin_lock_irqsave(&gspca_dev->qlock, flags);
1333 	list_add_tail(&buf->list, &gspca_dev->buf_list);
1334 	spin_unlock_irqrestore(&gspca_dev->qlock, flags);
1335 }
1336 
1337 static void gspca_return_all_buffers(struct gspca_dev *gspca_dev,
1338 				     enum vb2_buffer_state state)
1339 {
1340 	struct gspca_buffer *buf, *node;
1341 	unsigned long flags;
1342 
1343 	spin_lock_irqsave(&gspca_dev->qlock, flags);
1344 	list_for_each_entry_safe(buf, node, &gspca_dev->buf_list, list) {
1345 		vb2_buffer_done(&buf->vb.vb2_buf, state);
1346 		list_del(&buf->list);
1347 	}
1348 	spin_unlock_irqrestore(&gspca_dev->qlock, flags);
1349 }
1350 
1351 static int gspca_start_streaming(struct vb2_queue *vq, unsigned int count)
1352 {
1353 	struct gspca_dev *gspca_dev = vb2_get_drv_priv(vq);
1354 	int ret;
1355 
1356 	gspca_dev->sequence = 0;
1357 
1358 	ret = gspca_init_transfer(gspca_dev);
1359 	if (ret)
1360 		gspca_return_all_buffers(gspca_dev, VB2_BUF_STATE_QUEUED);
1361 	return ret;
1362 }
1363 
1364 static void gspca_stop_streaming(struct vb2_queue *vq)
1365 {
1366 	struct gspca_dev *gspca_dev = vb2_get_drv_priv(vq);
1367 
1368 	gspca_stream_off(gspca_dev);
1369 
1370 	/* Release all active buffers */
1371 	gspca_return_all_buffers(gspca_dev, VB2_BUF_STATE_ERROR);
1372 }
1373 
1374 static const struct vb2_ops gspca_qops = {
1375 	.queue_setup		= gspca_queue_setup,
1376 	.buf_prepare		= gspca_buffer_prepare,
1377 	.buf_finish		= gspca_buffer_finish,
1378 	.buf_queue		= gspca_buffer_queue,
1379 	.start_streaming	= gspca_start_streaming,
1380 	.stop_streaming		= gspca_stop_streaming,
1381 	.wait_prepare		= vb2_ops_wait_prepare,
1382 	.wait_finish		= vb2_ops_wait_finish,
1383 };
1384 
1385 static const struct v4l2_file_operations dev_fops = {
1386 	.owner = THIS_MODULE,
1387 	.open = v4l2_fh_open,
1388 	.release = vb2_fop_release,
1389 	.unlocked_ioctl = video_ioctl2,
1390 	.read = vb2_fop_read,
1391 	.mmap = vb2_fop_mmap,
1392 	.poll = vb2_fop_poll,
1393 };
1394 
1395 static const struct v4l2_ioctl_ops dev_ioctl_ops = {
1396 	.vidioc_querycap	= vidioc_querycap,
1397 	.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1398 	.vidioc_try_fmt_vid_cap	= vidioc_try_fmt_vid_cap,
1399 	.vidioc_g_fmt_vid_cap	= vidioc_g_fmt_vid_cap,
1400 	.vidioc_s_fmt_vid_cap	= vidioc_s_fmt_vid_cap,
1401 	.vidioc_enum_input	= vidioc_enum_input,
1402 	.vidioc_g_input		= vidioc_g_input,
1403 	.vidioc_s_input		= vidioc_s_input,
1404 	.vidioc_g_jpegcomp	= vidioc_g_jpegcomp,
1405 	.vidioc_s_jpegcomp	= vidioc_s_jpegcomp,
1406 	.vidioc_g_parm		= vidioc_g_parm,
1407 	.vidioc_s_parm		= vidioc_s_parm,
1408 	.vidioc_enum_framesizes = vidioc_enum_framesizes,
1409 	.vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1410 
1411 	.vidioc_reqbufs		= vb2_ioctl_reqbufs,
1412 	.vidioc_create_bufs	= vb2_ioctl_create_bufs,
1413 	.vidioc_querybuf	= vb2_ioctl_querybuf,
1414 	.vidioc_qbuf		= vb2_ioctl_qbuf,
1415 	.vidioc_dqbuf		= vb2_ioctl_dqbuf,
1416 	.vidioc_expbuf		= vb2_ioctl_expbuf,
1417 	.vidioc_streamon	= vb2_ioctl_streamon,
1418 	.vidioc_streamoff	= vb2_ioctl_streamoff,
1419 
1420 #ifdef CONFIG_VIDEO_ADV_DEBUG
1421 	.vidioc_g_chip_info	= vidioc_g_chip_info,
1422 	.vidioc_g_register	= vidioc_g_register,
1423 	.vidioc_s_register	= vidioc_s_register,
1424 #endif
1425 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1426 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1427 };
1428 
1429 static const struct video_device gspca_template = {
1430 	.name = "gspca main driver",
1431 	.fops = &dev_fops,
1432 	.ioctl_ops = &dev_ioctl_ops,
1433 	.release = video_device_release_empty, /* We use v4l2_dev.release */
1434 };
1435 
1436 /*
1437  * probe and create a new gspca device
1438  *
1439  * This function must be called by the sub-driver when it is
1440  * called for probing a new device.
1441  */
1442 int gspca_dev_probe2(struct usb_interface *intf,
1443 		const struct usb_device_id *id,
1444 		const struct sd_desc *sd_desc,
1445 		int dev_size,
1446 		struct module *module)
1447 {
1448 	struct gspca_dev *gspca_dev;
1449 	struct usb_device *dev = interface_to_usbdev(intf);
1450 	struct vb2_queue *q;
1451 	int ret;
1452 
1453 	pr_info("%s-" GSPCA_VERSION " probing %04x:%04x\n",
1454 		sd_desc->name, id->idVendor, id->idProduct);
1455 
1456 	/* create the device */
1457 	if (dev_size < sizeof *gspca_dev)
1458 		dev_size = sizeof *gspca_dev;
1459 	gspca_dev = kzalloc(dev_size, GFP_KERNEL);
1460 	if (!gspca_dev) {
1461 		pr_err("couldn't kzalloc gspca struct\n");
1462 		return -ENOMEM;
1463 	}
1464 	gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
1465 	if (!gspca_dev->usb_buf) {
1466 		pr_err("out of memory\n");
1467 		ret = -ENOMEM;
1468 		goto out;
1469 	}
1470 	gspca_dev->dev = dev;
1471 	gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
1472 	gspca_dev->xfer_ep = -1;
1473 
1474 	/* check if any audio device */
1475 	if (dev->actconfig->desc.bNumInterfaces != 1) {
1476 		int i;
1477 		struct usb_interface *intf2;
1478 
1479 		for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1480 			intf2 = dev->actconfig->interface[i];
1481 			if (intf2 != NULL
1482 			 && intf2->altsetting != NULL
1483 			 && intf2->altsetting->desc.bInterfaceClass ==
1484 					 USB_CLASS_AUDIO) {
1485 				gspca_dev->audio = 1;
1486 				break;
1487 			}
1488 		}
1489 	}
1490 
1491 	gspca_dev->v4l2_dev.release = gspca_release;
1492 	ret = v4l2_device_register(&intf->dev, &gspca_dev->v4l2_dev);
1493 	if (ret)
1494 		goto out;
1495 	gspca_dev->present = true;
1496 	gspca_dev->sd_desc = sd_desc;
1497 	gspca_dev->empty_packet = -1;	/* don't check the empty packets */
1498 	gspca_dev->vdev = gspca_template;
1499 	gspca_dev->vdev.v4l2_dev = &gspca_dev->v4l2_dev;
1500 	gspca_dev->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE |
1501 				      V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1502 	video_set_drvdata(&gspca_dev->vdev, gspca_dev);
1503 	gspca_dev->module = module;
1504 
1505 	mutex_init(&gspca_dev->usb_lock);
1506 	gspca_dev->vdev.lock = &gspca_dev->usb_lock;
1507 	init_waitqueue_head(&gspca_dev->wq);
1508 
1509 	/* Initialize the vb2 queue */
1510 	q = &gspca_dev->queue;
1511 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1512 	q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1513 	q->drv_priv = gspca_dev;
1514 	q->buf_struct_size = sizeof(struct gspca_buffer);
1515 	q->ops = &gspca_qops;
1516 	q->mem_ops = &vb2_vmalloc_memops;
1517 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1518 	q->min_buffers_needed = 2;
1519 	q->lock = &gspca_dev->usb_lock;
1520 	ret = vb2_queue_init(q);
1521 	if (ret)
1522 		goto out;
1523 	gspca_dev->vdev.queue = q;
1524 
1525 	INIT_LIST_HEAD(&gspca_dev->buf_list);
1526 	spin_lock_init(&gspca_dev->qlock);
1527 
1528 	/* configure the subdriver and initialize the USB device */
1529 	ret = sd_desc->config(gspca_dev, id);
1530 	if (ret < 0)
1531 		goto out;
1532 	ret = sd_desc->init(gspca_dev);
1533 	if (ret < 0)
1534 		goto out;
1535 	if (sd_desc->init_controls)
1536 		ret = sd_desc->init_controls(gspca_dev);
1537 	if (ret < 0)
1538 		goto out;
1539 	gspca_set_default_mode(gspca_dev);
1540 
1541 	ret = gspca_input_connect(gspca_dev);
1542 	if (ret)
1543 		goto out;
1544 
1545 #ifdef CONFIG_VIDEO_ADV_DEBUG
1546 	if (!gspca_dev->sd_desc->get_register)
1547 		v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_G_REGISTER);
1548 	if (!gspca_dev->sd_desc->set_register)
1549 		v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_S_REGISTER);
1550 #endif
1551 	if (!gspca_dev->sd_desc->get_jcomp)
1552 		v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_G_JPEGCOMP);
1553 	if (!gspca_dev->sd_desc->set_jcomp)
1554 		v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_S_JPEGCOMP);
1555 
1556 	/* init video stuff */
1557 	ret = video_register_device(&gspca_dev->vdev,
1558 				  VFL_TYPE_GRABBER,
1559 				  -1);
1560 	if (ret < 0) {
1561 		pr_err("video_register_device err %d\n", ret);
1562 		goto out;
1563 	}
1564 
1565 	usb_set_intfdata(intf, gspca_dev);
1566 	gspca_dbg(gspca_dev, D_PROBE, "%s created\n",
1567 		  video_device_node_name(&gspca_dev->vdev));
1568 
1569 	gspca_input_create_urb(gspca_dev);
1570 
1571 	return 0;
1572 out:
1573 #if IS_ENABLED(CONFIG_INPUT)
1574 	if (gspca_dev->input_dev)
1575 		input_unregister_device(gspca_dev->input_dev);
1576 #endif
1577 	v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
1578 	kfree(gspca_dev->usb_buf);
1579 	kfree(gspca_dev);
1580 	return ret;
1581 }
1582 EXPORT_SYMBOL(gspca_dev_probe2);
1583 
1584 /* same function as the previous one, but check the interface */
1585 int gspca_dev_probe(struct usb_interface *intf,
1586 		const struct usb_device_id *id,
1587 		const struct sd_desc *sd_desc,
1588 		int dev_size,
1589 		struct module *module)
1590 {
1591 	struct usb_device *dev = interface_to_usbdev(intf);
1592 
1593 	/* we don't handle multi-config cameras */
1594 	if (dev->descriptor.bNumConfigurations != 1) {
1595 		pr_err("%04x:%04x too many config\n",
1596 		       id->idVendor, id->idProduct);
1597 		return -ENODEV;
1598 	}
1599 
1600 	/* the USB video interface must be the first one */
1601 	if (dev->actconfig->desc.bNumInterfaces != 1
1602 	 && intf->cur_altsetting->desc.bInterfaceNumber != 0)
1603 		return -ENODEV;
1604 
1605 	return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
1606 }
1607 EXPORT_SYMBOL(gspca_dev_probe);
1608 
1609 /*
1610  * USB disconnection
1611  *
1612  * This function must be called by the sub-driver
1613  * when the device disconnects, after the specific resources are freed.
1614  */
1615 void gspca_disconnect(struct usb_interface *intf)
1616 {
1617 	struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1618 #if IS_ENABLED(CONFIG_INPUT)
1619 	struct input_dev *input_dev;
1620 #endif
1621 
1622 	gspca_dbg(gspca_dev, D_PROBE, "%s disconnect\n",
1623 		  video_device_node_name(&gspca_dev->vdev));
1624 
1625 	mutex_lock(&gspca_dev->usb_lock);
1626 	gspca_dev->present = false;
1627 	destroy_urbs(gspca_dev);
1628 	gspca_input_destroy_urb(gspca_dev);
1629 
1630 	vb2_queue_error(&gspca_dev->queue);
1631 
1632 #if IS_ENABLED(CONFIG_INPUT)
1633 	input_dev = gspca_dev->input_dev;
1634 	if (input_dev) {
1635 		gspca_dev->input_dev = NULL;
1636 		input_unregister_device(input_dev);
1637 	}
1638 #endif
1639 
1640 	v4l2_device_disconnect(&gspca_dev->v4l2_dev);
1641 	video_unregister_device(&gspca_dev->vdev);
1642 
1643 	mutex_unlock(&gspca_dev->usb_lock);
1644 
1645 	/* (this will call gspca_release() immediately or on last close) */
1646 	v4l2_device_put(&gspca_dev->v4l2_dev);
1647 }
1648 EXPORT_SYMBOL(gspca_disconnect);
1649 
1650 #ifdef CONFIG_PM
1651 int gspca_suspend(struct usb_interface *intf, pm_message_t message)
1652 {
1653 	struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1654 
1655 	gspca_input_destroy_urb(gspca_dev);
1656 
1657 	if (!vb2_start_streaming_called(&gspca_dev->queue))
1658 		return 0;
1659 
1660 	mutex_lock(&gspca_dev->usb_lock);
1661 	gspca_dev->frozen = 1;		/* avoid urb error messages */
1662 	gspca_dev->usb_err = 0;
1663 	if (gspca_dev->sd_desc->stopN)
1664 		gspca_dev->sd_desc->stopN(gspca_dev);
1665 	destroy_urbs(gspca_dev);
1666 	gspca_set_alt0(gspca_dev);
1667 	if (gspca_dev->sd_desc->stop0)
1668 		gspca_dev->sd_desc->stop0(gspca_dev);
1669 	mutex_unlock(&gspca_dev->usb_lock);
1670 
1671 	return 0;
1672 }
1673 EXPORT_SYMBOL(gspca_suspend);
1674 
1675 int gspca_resume(struct usb_interface *intf)
1676 {
1677 	struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1678 	int streaming, ret = 0;
1679 
1680 	mutex_lock(&gspca_dev->usb_lock);
1681 	gspca_dev->frozen = 0;
1682 	gspca_dev->usb_err = 0;
1683 	gspca_dev->sd_desc->init(gspca_dev);
1684 	/*
1685 	 * Most subdrivers send all ctrl values on sd_start and thus
1686 	 * only write to the device registers on s_ctrl when streaming ->
1687 	 * Clear streaming to avoid setting all ctrls twice.
1688 	 */
1689 	streaming = vb2_start_streaming_called(&gspca_dev->queue);
1690 	if (streaming)
1691 		ret = gspca_init_transfer(gspca_dev);
1692 	else
1693 		gspca_input_create_urb(gspca_dev);
1694 	mutex_unlock(&gspca_dev->usb_lock);
1695 
1696 	return ret;
1697 }
1698 EXPORT_SYMBOL(gspca_resume);
1699 #endif
1700 
1701 /* -- module insert / remove -- */
1702 static int __init gspca_init(void)
1703 {
1704 	pr_info("v" GSPCA_VERSION " registered\n");
1705 	return 0;
1706 }
1707 static void __exit gspca_exit(void)
1708 {
1709 }
1710 
1711 module_init(gspca_init);
1712 module_exit(gspca_exit);
1713 
1714 module_param_named(debug, gspca_debug, int, 0644);
1715 MODULE_PARM_DESC(debug,
1716 		"1:probe 2:config 3:stream 4:frame 5:packet 6:usbi 7:usbo");
1717