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