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