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