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