diff options
author | Jean-Francois Moine <moinejf@free.fr> | 2008-04-12 08:58:09 -0400 |
---|---|---|
committer | Mauro Carvalho Chehab <mchehab@infradead.org> | 2008-07-20 06:14:28 -0400 |
commit | 63eb9546dcb5e9dc39ab88a603dede8fdd18e717 (patch) | |
tree | c61a8365a0428a58f643df35b29220f1c5e63f46 /drivers/media/video/gspca/gspca.c | |
parent | 8b53b39d6228cda41b7ddfc9d094a072afca6655 (diff) |
V4L/DVB (8152): Initial release of gspca with only one driver.
Signed-off-by: Jean-Francois Moine <moinejf@free.fr>
Signed-off-by: Mauro Carvalho Chehab <mchehab@infradead.org>
Diffstat (limited to 'drivers/media/video/gspca/gspca.c')
-rw-r--r-- | drivers/media/video/gspca/gspca.c | 1750 |
1 files changed, 1750 insertions, 0 deletions
diff --git a/drivers/media/video/gspca/gspca.c b/drivers/media/video/gspca/gspca.c new file mode 100644 index 000000000000..4fe082ff7074 --- /dev/null +++ b/drivers/media/video/gspca/gspca.c | |||
@@ -0,0 +1,1750 @@ | |||
1 | /* | ||
2 | * Main USB camera driver | ||
3 | * | ||
4 | * V4L2 by Jean-Francois Moine <http://moinejf.free.fr> | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify it | ||
7 | * under the terms of the GNU General Public License as published by the | ||
8 | * Free Software Foundation; either version 2 of the License, or (at your | ||
9 | * option) any later version. | ||
10 | * | ||
11 | * This program is distributed in the hope that it will be useful, but | ||
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
13 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
14 | * for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License | ||
17 | * along with this program; if not, write to the Free Software Foundation, | ||
18 | * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
19 | */ | ||
20 | |||
21 | #define MODULE_NAME "gspca" | ||
22 | |||
23 | #include <linux/init.h> | ||
24 | #include <linux/fs.h> | ||
25 | #include <linux/vmalloc.h> | ||
26 | #include <linux/sched.h> | ||
27 | #include <linux/slab.h> | ||
28 | #include <linux/mm.h> | ||
29 | #include <linux/string.h> | ||
30 | #include <linux/pagemap.h> | ||
31 | #include <asm/io.h> | ||
32 | #include <asm/page.h> | ||
33 | #include <asm/uaccess.h> | ||
34 | #include <linux/jiffies.h> | ||
35 | |||
36 | #include "gspca.h" | ||
37 | |||
38 | MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>"); | ||
39 | MODULE_DESCRIPTION("GSPCA USB Camera Driver"); | ||
40 | MODULE_LICENSE("GPL"); | ||
41 | |||
42 | #define DRIVER_VERSION_NUMBER KERNEL_VERSION(0, 0, 26) | ||
43 | static const char version[] = "0.0.26"; | ||
44 | |||
45 | static int video_nr = -1; | ||
46 | |||
47 | static int comp_fac = 30; /* Buffer size ratio when compressed in % */ | ||
48 | |||
49 | #ifdef GSPCA_DEBUG | ||
50 | int gspca_debug = D_ERR | D_PROBE; | ||
51 | EXPORT_SYMBOL(gspca_debug); | ||
52 | |||
53 | static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h) | ||
54 | { | ||
55 | if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') { | ||
56 | PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d", | ||
57 | txt, | ||
58 | pixfmt & 0xff, | ||
59 | (pixfmt >> 8) & 0xff, | ||
60 | (pixfmt >> 16) & 0xff, | ||
61 | pixfmt >> 24, | ||
62 | w, h); | ||
63 | } else { | ||
64 | PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d", | ||
65 | txt, | ||
66 | pixfmt, | ||
67 | w, h); | ||
68 | } | ||
69 | } | ||
70 | #else | ||
71 | #define PDEBUG_MODE(txt, pixfmt, w, h) | ||
72 | #endif | ||
73 | |||
74 | /* | ||
75 | * VMA operations. | ||
76 | */ | ||
77 | static void gspca_vm_open(struct vm_area_struct *vma) | ||
78 | { | ||
79 | struct gspca_frame *frame = vma->vm_private_data; | ||
80 | |||
81 | frame->vma_use_count++; | ||
82 | frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED; | ||
83 | } | ||
84 | |||
85 | static void gspca_vm_close(struct vm_area_struct *vma) | ||
86 | { | ||
87 | struct gspca_frame *frame = vma->vm_private_data; | ||
88 | |||
89 | if (--frame->vma_use_count <= 0) | ||
90 | frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED; | ||
91 | } | ||
92 | |||
93 | static struct vm_operations_struct gspca_vm_ops = { | ||
94 | .open = gspca_vm_open, | ||
95 | .close = gspca_vm_close, | ||
96 | }; | ||
97 | |||
98 | /* | ||
99 | * ISOC message interrupt from the USB device | ||
100 | * | ||
101 | * Analyse each packet and call the subdriver for doing the copy | ||
102 | * to the frame buffer. | ||
103 | */ | ||
104 | static void isoc_irq(struct urb *urb) | ||
105 | { | ||
106 | struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context; | ||
107 | struct gspca_frame *frame; | ||
108 | unsigned char *data; /* address of data in the iso message */ | ||
109 | int i, j, len, st; | ||
110 | cam_pkt_op pkt_scan; | ||
111 | |||
112 | PDEBUG(D_PACK, "isoc irq"); | ||
113 | if (!gspca_dev->streaming) | ||
114 | return; | ||
115 | pkt_scan = gspca_dev->sd_desc->pkt_scan; | ||
116 | for (i = 0; i < urb->number_of_packets; i++) { | ||
117 | |||
118 | /* check the availability of the frame buffer */ | ||
119 | j = gspca_dev->fr_i; | ||
120 | j = gspca_dev->fr_queue[j]; | ||
121 | frame = &gspca_dev->frame[j]; | ||
122 | if ((frame->v4l2_buf.flags | ||
123 | & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)) | ||
124 | != V4L2_BUF_FLAG_QUEUED) { | ||
125 | gspca_dev->last_packet_type = DISCARD_PACKET; | ||
126 | break; | ||
127 | } | ||
128 | |||
129 | /* check the packet status and length */ | ||
130 | len = urb->iso_frame_desc[i].actual_length; | ||
131 | st = urb->iso_frame_desc[i].status; | ||
132 | if (st) { | ||
133 | PDEBUG(D_ERR, "ISOC data error: [%d] len=%d, status=%d", | ||
134 | i, len, st); | ||
135 | gspca_dev->last_packet_type = DISCARD_PACKET; | ||
136 | continue; | ||
137 | } | ||
138 | if (len == 0) | ||
139 | continue; | ||
140 | |||
141 | /* let the packet be analyzed by the subdriver */ | ||
142 | PDEBUG(D_PACK, "packet [%d] o:%d l:%d", | ||
143 | i, urb->iso_frame_desc[i].offset, len); | ||
144 | data = (unsigned char *) urb->transfer_buffer | ||
145 | + urb->iso_frame_desc[i].offset; | ||
146 | pkt_scan(gspca_dev, frame, data, len); | ||
147 | } | ||
148 | |||
149 | /* resubmit the URB */ | ||
150 | urb->status = 0; | ||
151 | st = usb_submit_urb(urb, GFP_ATOMIC); | ||
152 | if (st < 0) | ||
153 | PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st); | ||
154 | } | ||
155 | |||
156 | /* | ||
157 | * add data to the current frame | ||
158 | * | ||
159 | * This function is called by the subdrivers at interrupt level. | ||
160 | * To build a frame, these ones must add | ||
161 | * - one FIRST_PACKET | ||
162 | * - 0 or many INTER_PACKETs | ||
163 | * - one LAST_PACKET | ||
164 | * DISCARD_PACKET invalidates the whole frame. | ||
165 | * On LAST_PACKET, a new frame is returned. | ||
166 | */ | ||
167 | struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev, | ||
168 | int packet_type, | ||
169 | struct gspca_frame *frame, | ||
170 | unsigned char *data, | ||
171 | int len) | ||
172 | { | ||
173 | int i, j; | ||
174 | |||
175 | PDEBUG(D_PACK, "add t:%d l:%d %02x %02x %02x %02x...", | ||
176 | packet_type, len, data[0], data[1], data[2], data[3]); | ||
177 | |||
178 | /* when start of a new frame, if the current frame buffer | ||
179 | * is not queued, discard the whole frame */ | ||
180 | if (packet_type == FIRST_PACKET) { | ||
181 | if ((frame->v4l2_buf.flags | ||
182 | & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)) | ||
183 | != V4L2_BUF_FLAG_QUEUED) { | ||
184 | gspca_dev->last_packet_type = DISCARD_PACKET; | ||
185 | return frame; | ||
186 | } | ||
187 | frame->data_end = frame->data; | ||
188 | jiffies_to_timeval(get_jiffies_64(), | ||
189 | &frame->v4l2_buf.timestamp); | ||
190 | frame->v4l2_buf.sequence = ++gspca_dev->sequence; | ||
191 | } else if (gspca_dev->last_packet_type == DISCARD_PACKET) | ||
192 | return frame; | ||
193 | |||
194 | /* append the packet in the frame buffer */ | ||
195 | if (len > 0) { | ||
196 | if (frame->data_end - frame->data + len | ||
197 | > frame->v4l2_buf.length) { | ||
198 | PDEBUG(D_ERR|D_PACK, "frame overflow %d > %d", | ||
199 | frame->data_end - frame->data + len, | ||
200 | frame->v4l2_buf.length); | ||
201 | packet_type = DISCARD_PACKET; | ||
202 | } else { | ||
203 | if (frame->v4l2_buf.memory != V4L2_MEMORY_USERPTR) | ||
204 | memcpy(frame->data_end, data, len); | ||
205 | else | ||
206 | copy_to_user(frame->data_end, data, len); | ||
207 | frame->data_end += len; | ||
208 | } | ||
209 | } | ||
210 | gspca_dev->last_packet_type = packet_type; | ||
211 | |||
212 | /* if last packet, wake the application and advance in the queue */ | ||
213 | if (packet_type == LAST_PACKET) { | ||
214 | frame->v4l2_buf.bytesused = frame->data_end - frame->data; | ||
215 | frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED; | ||
216 | frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE; | ||
217 | atomic_inc(&gspca_dev->nevent); | ||
218 | wake_up_interruptible(&gspca_dev->wq); /* event = new frame */ | ||
219 | i = gspca_dev->fr_i; | ||
220 | i = (i + 1) % gspca_dev->nframes; | ||
221 | PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d", | ||
222 | frame->v4l2_buf.bytesused, | ||
223 | gspca_dev->fr_q, | ||
224 | i, | ||
225 | gspca_dev->fr_o); | ||
226 | j = gspca_dev->fr_queue[i]; | ||
227 | frame = &gspca_dev->frame[j]; | ||
228 | gspca_dev->fr_i = i; | ||
229 | } | ||
230 | return frame; | ||
231 | } | ||
232 | EXPORT_SYMBOL(gspca_frame_add); | ||
233 | |||
234 | static int gspca_is_compressed(__u32 format) | ||
235 | { | ||
236 | switch (format) { | ||
237 | case V4L2_PIX_FMT_MJPEG: | ||
238 | case V4L2_PIX_FMT_JPEG: | ||
239 | return 1; | ||
240 | } | ||
241 | return 0; | ||
242 | } | ||
243 | |||
244 | static void *rvmalloc(unsigned long size) | ||
245 | { | ||
246 | void *mem; | ||
247 | unsigned long adr; | ||
248 | |||
249 | size = PAGE_ALIGN(size); | ||
250 | mem = vmalloc_32(size); | ||
251 | if (mem != 0) { | ||
252 | memset(mem, 0, size); | ||
253 | adr = (unsigned long) mem; | ||
254 | while ((long) size > 0) { | ||
255 | SetPageReserved(vmalloc_to_page((void *) adr)); | ||
256 | adr += PAGE_SIZE; | ||
257 | size -= PAGE_SIZE; | ||
258 | } | ||
259 | } | ||
260 | return mem; | ||
261 | } | ||
262 | |||
263 | static void rvfree(void *mem, unsigned long size) | ||
264 | { | ||
265 | unsigned long adr; | ||
266 | |||
267 | if (!mem) | ||
268 | return; | ||
269 | adr = (unsigned long) mem; | ||
270 | while ((long) size > 0) { | ||
271 | ClearPageReserved(vmalloc_to_page((void *) adr)); | ||
272 | adr += PAGE_SIZE; | ||
273 | size -= PAGE_SIZE; | ||
274 | } | ||
275 | vfree(mem); | ||
276 | } | ||
277 | |||
278 | static int frame_alloc(struct gspca_dev *gspca_dev, | ||
279 | unsigned int count, | ||
280 | unsigned int frsz, | ||
281 | enum v4l2_memory memory) | ||
282 | { | ||
283 | int i, ret = 0; | ||
284 | |||
285 | PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz); | ||
286 | if (gspca_dev->nframes != 0) { | ||
287 | PDEBUG(D_ERR|D_STREAM, "alloc frame already done"); | ||
288 | return -EBUSY; | ||
289 | } | ||
290 | if (count > GSPCA_MAX_FRAMES) | ||
291 | count = GSPCA_MAX_FRAMES; | ||
292 | /* if compressed, reduce the buffer size */ | ||
293 | if (gspca_is_compressed(gspca_dev->pixfmt)) | ||
294 | frsz = (frsz * comp_fac) / 100; | ||
295 | frsz = PAGE_ALIGN(frsz); | ||
296 | PDEBUG(D_STREAM, "new fr_sz: %d", frsz); | ||
297 | gspca_dev->frsz = frsz; | ||
298 | if (memory == V4L2_MEMORY_MMAP) { | ||
299 | gspca_dev->frbuf = rvmalloc(frsz * count); | ||
300 | if (!gspca_dev->frbuf) { | ||
301 | err("frame alloc failed"); | ||
302 | return -ENOMEM; | ||
303 | } | ||
304 | } | ||
305 | gspca_dev->nframes = count; | ||
306 | for (i = 0; i < count; i++) { | ||
307 | gspca_dev->frame[i].v4l2_buf.index = i; | ||
308 | gspca_dev->frame[i].v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | ||
309 | gspca_dev->frame[i].v4l2_buf.flags = 0; | ||
310 | gspca_dev->frame[i].v4l2_buf.field = V4L2_FIELD_NONE; | ||
311 | gspca_dev->frame[i].v4l2_buf.length = frsz; | ||
312 | gspca_dev->frame[i].v4l2_buf.memory = memory; | ||
313 | if (memory == V4L2_MEMORY_MMAP) { | ||
314 | gspca_dev->frame[i].data | ||
315 | = gspca_dev->frame[i].data_end | ||
316 | = gspca_dev->frbuf + i * frsz; | ||
317 | gspca_dev->frame[i].v4l2_buf.m.offset = i * frsz; | ||
318 | } | ||
319 | gspca_dev->frame[i].v4l2_buf.flags = 0; /* buf in app space */ | ||
320 | } | ||
321 | gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0; | ||
322 | gspca_dev->last_packet_type = DISCARD_PACKET; | ||
323 | gspca_dev->sequence = 0; | ||
324 | atomic_set(&gspca_dev->nevent, 0); | ||
325 | return ret; | ||
326 | } | ||
327 | |||
328 | static void frame_free(struct gspca_dev *gspca_dev) | ||
329 | { | ||
330 | int i; | ||
331 | |||
332 | PDEBUG(D_STREAM, "frame free"); | ||
333 | if (gspca_dev->frbuf != 0) { | ||
334 | rvfree(gspca_dev->frbuf, | ||
335 | gspca_dev->nframes * gspca_dev->frsz); | ||
336 | gspca_dev->frbuf = NULL; | ||
337 | for (i = 0; i < gspca_dev->nframes; i++) | ||
338 | gspca_dev->frame[i].data = NULL; | ||
339 | } | ||
340 | gspca_dev->nframes = 0; | ||
341 | } | ||
342 | |||
343 | static int gspca_kill_transfer(struct gspca_dev *gspca_dev) | ||
344 | { | ||
345 | struct urb *urb; | ||
346 | unsigned int i; | ||
347 | |||
348 | PDEBUG(D_STREAM, "kill transfer"); | ||
349 | gspca_dev->streaming = 0; | ||
350 | for (i = 0; i < NURBS; ++i) { | ||
351 | urb = gspca_dev->pktbuf[i].urb; | ||
352 | if (urb == NULL) | ||
353 | continue; | ||
354 | |||
355 | gspca_dev->pktbuf[i].urb = NULL; | ||
356 | usb_kill_urb(urb); | ||
357 | |||
358 | /* urb->transfer_buffer_length is not touched by USB core, | ||
359 | * so we can use it here as the buffer length */ | ||
360 | if (gspca_dev->pktbuf[i].data) { | ||
361 | usb_buffer_free(gspca_dev->dev, | ||
362 | urb->transfer_buffer_length, | ||
363 | gspca_dev->pktbuf[i].data, | ||
364 | urb->transfer_dma); | ||
365 | gspca_dev->pktbuf[i].data = NULL; | ||
366 | } | ||
367 | usb_free_urb(urb); | ||
368 | } | ||
369 | return 0; | ||
370 | } | ||
371 | |||
372 | /* | ||
373 | * search an input isochronous endpoint in an alternate setting | ||
374 | */ | ||
375 | static struct usb_host_endpoint *alt_isoc(struct usb_host_interface *alt, | ||
376 | __u8 epaddr) | ||
377 | { | ||
378 | struct usb_host_endpoint *ep; | ||
379 | int i, attr; | ||
380 | |||
381 | epaddr |= USB_DIR_IN; | ||
382 | for (i = 0; i < alt->desc.bNumEndpoints; i++) { | ||
383 | ep = &alt->endpoint[i]; | ||
384 | if (ep->desc.bEndpointAddress == epaddr) { | ||
385 | attr = ep->desc.bmAttributes | ||
386 | & USB_ENDPOINT_XFERTYPE_MASK; | ||
387 | if (attr == USB_ENDPOINT_XFER_ISOC) | ||
388 | return ep; | ||
389 | break; | ||
390 | } | ||
391 | } | ||
392 | return NULL; | ||
393 | } | ||
394 | |||
395 | /* | ||
396 | * search an input isochronous endpoint | ||
397 | * | ||
398 | * The endpoint is defined by the subdriver. | ||
399 | * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep). | ||
400 | * This routine may be called many times when the bandwidth is too small | ||
401 | * (the bandwidth is checked on urb submit). | ||
402 | */ | ||
403 | struct usb_host_endpoint *get_isoc_ep(struct gspca_dev *gspca_dev) | ||
404 | { | ||
405 | struct usb_interface *intf; | ||
406 | struct usb_host_endpoint *ep; | ||
407 | int i, ret; | ||
408 | |||
409 | intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface); | ||
410 | i = gspca_dev->alt; /* previous alt setting */ | ||
411 | while (--i > 0) { /* alt 0 is unusable */ | ||
412 | ep = alt_isoc(&intf->altsetting[i], gspca_dev->cam.epaddr); | ||
413 | if (ep) | ||
414 | break; | ||
415 | } | ||
416 | if (i <= 0) { | ||
417 | err("no ISOC endpoint found"); | ||
418 | return NULL; | ||
419 | } | ||
420 | PDEBUG(D_STREAM, "use ISOC alt %d ep 0x%02x", | ||
421 | i, ep->desc.bEndpointAddress); | ||
422 | ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i); | ||
423 | if (ret < 0) { | ||
424 | err("set interface err %d", ret); | ||
425 | return NULL; | ||
426 | } | ||
427 | gspca_dev->alt = i; | ||
428 | return ep; | ||
429 | } | ||
430 | |||
431 | /* | ||
432 | * create the isochronous URBs | ||
433 | */ | ||
434 | static int create_urbs(struct gspca_dev *gspca_dev, | ||
435 | struct usb_host_endpoint *ep) | ||
436 | { | ||
437 | struct urb *urb; | ||
438 | int n, i, psize, npkt, bsize; | ||
439 | |||
440 | /* calculate the packet size and the number of packets */ | ||
441 | /* the URB buffer size must be a power of 2 */ | ||
442 | psize = le16_to_cpu(ep->desc.wMaxPacketSize); | ||
443 | /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */ | ||
444 | psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3)); | ||
445 | npkt = ISO_MAX_SIZE / psize; | ||
446 | if (npkt > ISO_MAX_PKT) | ||
447 | npkt = ISO_MAX_PKT; | ||
448 | bsize = psize * npkt; | ||
449 | for (n = ISO_MAX_SIZE; n > 0; n >>= 1) { | ||
450 | if (n & bsize) /* !! assume ISO_MAX_SIZE is a power of 2 */ | ||
451 | break; | ||
452 | } | ||
453 | if (n != 0) { | ||
454 | npkt = n / psize; | ||
455 | bsize = psize * npkt; | ||
456 | } | ||
457 | PDEBUG(D_STREAM, | ||
458 | "isoc %d pkts size %d (bsize:%d)", npkt, psize, bsize); | ||
459 | for (n = 0; n < NURBS; n++) { | ||
460 | urb = usb_alloc_urb(npkt, GFP_KERNEL); | ||
461 | if (!urb) { | ||
462 | err("usb_alloc_urb failed"); | ||
463 | return -ENOMEM; | ||
464 | } | ||
465 | gspca_dev->pktbuf[n].data = usb_buffer_alloc(gspca_dev->dev, | ||
466 | bsize, | ||
467 | GFP_KERNEL, | ||
468 | &urb->transfer_dma); | ||
469 | |||
470 | if (gspca_dev->pktbuf[n].data == NULL) { | ||
471 | usb_free_urb(urb); | ||
472 | gspca_kill_transfer(gspca_dev); | ||
473 | err("usb_buffer_urb failed"); | ||
474 | return -ENOMEM; | ||
475 | } | ||
476 | gspca_dev->pktbuf[n].urb = urb; | ||
477 | urb->dev = gspca_dev->dev; | ||
478 | urb->context = gspca_dev; | ||
479 | urb->pipe = usb_rcvisocpipe(gspca_dev->dev, | ||
480 | ep->desc.bEndpointAddress); | ||
481 | urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; | ||
482 | urb->interval = ep->desc.bInterval; | ||
483 | urb->transfer_buffer = gspca_dev->pktbuf[n].data; | ||
484 | urb->complete = isoc_irq; | ||
485 | urb->number_of_packets = npkt; | ||
486 | urb->transfer_buffer_length = bsize; | ||
487 | for (i = 0; i < npkt; i++) { | ||
488 | urb->iso_frame_desc[i].length = psize; | ||
489 | urb->iso_frame_desc[i].offset = psize * i; | ||
490 | } | ||
491 | } | ||
492 | return 0; | ||
493 | } | ||
494 | |||
495 | /* | ||
496 | * start the USB transfer | ||
497 | */ | ||
498 | static int gspca_init_transfer(struct gspca_dev *gspca_dev) | ||
499 | { | ||
500 | struct usb_interface *intf; | ||
501 | struct usb_host_endpoint *ep; | ||
502 | int n, ret; | ||
503 | |||
504 | ret = mutex_lock_interruptible(&gspca_dev->usb_lock); | ||
505 | if (ret < 0) | ||
506 | return ret; | ||
507 | |||
508 | /* set the max alternate setting and loop until urb submit succeeds */ | ||
509 | intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface); | ||
510 | gspca_dev->alt = intf->num_altsetting; | ||
511 | for (;;) { | ||
512 | PDEBUG(D_STREAM, "init transfer nbalt %d", gspca_dev->alt); | ||
513 | ep = get_isoc_ep(gspca_dev); | ||
514 | if (ep == NULL) { | ||
515 | ret = -EIO; | ||
516 | goto out; | ||
517 | } | ||
518 | ret = create_urbs(gspca_dev, ep); | ||
519 | if (ret < 0) | ||
520 | goto out; | ||
521 | |||
522 | /* start the cam */ | ||
523 | gspca_dev->sd_desc->start(gspca_dev); | ||
524 | gspca_dev->streaming = 1; | ||
525 | atomic_set(&gspca_dev->nevent, 0); | ||
526 | |||
527 | /* submit the URBs */ | ||
528 | for (n = 0; n < NURBS; n++) { | ||
529 | ret = usb_submit_urb(gspca_dev->pktbuf[n].urb, | ||
530 | GFP_KERNEL); | ||
531 | if (ret < 0) { | ||
532 | PDEBUG(D_ERR|D_STREAM, | ||
533 | "usb_submit_urb [%d] err %d", n, ret); | ||
534 | gspca_kill_transfer(gspca_dev); | ||
535 | if (ret == -ENOSPC) | ||
536 | break; /* try the previous alt */ | ||
537 | goto out; | ||
538 | } | ||
539 | } | ||
540 | if (ret >= 0) | ||
541 | break; | ||
542 | } | ||
543 | out: | ||
544 | mutex_unlock(&gspca_dev->usb_lock); | ||
545 | return ret; | ||
546 | } | ||
547 | |||
548 | static int gspca_set_alt0(struct gspca_dev *gspca_dev) | ||
549 | { | ||
550 | int ret; | ||
551 | |||
552 | ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0); | ||
553 | if (ret < 0) | ||
554 | PDEBUG(D_ERR|D_STREAM, "set interface 0 err %d", ret); | ||
555 | return ret; | ||
556 | } | ||
557 | |||
558 | static void gspca_stream_off(struct gspca_dev *gspca_dev) | ||
559 | { | ||
560 | mutex_lock_interruptible(&gspca_dev->usb_lock); | ||
561 | gspca_dev->streaming = 0; | ||
562 | if (gspca_dev->present) { | ||
563 | gspca_dev->sd_desc->stopN(gspca_dev); | ||
564 | gspca_kill_transfer(gspca_dev); | ||
565 | gspca_set_alt0(gspca_dev); | ||
566 | gspca_dev->sd_desc->stop0(gspca_dev); | ||
567 | PDEBUG(D_STREAM, "stream off OK"); | ||
568 | } else { | ||
569 | gspca_kill_transfer(gspca_dev); | ||
570 | atomic_inc(&gspca_dev->nevent); | ||
571 | wake_up_interruptible(&gspca_dev->wq); | ||
572 | PDEBUG(D_ERR|D_STREAM, "stream off no device ??"); | ||
573 | } | ||
574 | mutex_unlock(&gspca_dev->usb_lock); | ||
575 | } | ||
576 | |||
577 | static int gspca_set_default_mode(struct gspca_dev *gspca_dev) | ||
578 | { | ||
579 | int i; | ||
580 | |||
581 | i = gspca_dev->cam.nmodes - 1; /* take the highest mode */ | ||
582 | gspca_dev->curr_mode = i; | ||
583 | gspca_dev->width = gspca_dev->cam.cam_mode[i].width; | ||
584 | gspca_dev->height = gspca_dev->cam.cam_mode[i].height; | ||
585 | gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixfmt; | ||
586 | return 0; | ||
587 | } | ||
588 | |||
589 | static int wxh_to_mode(struct gspca_dev *gspca_dev, | ||
590 | int width, int height) | ||
591 | { | ||
592 | int i; | ||
593 | |||
594 | for (i = gspca_dev->cam.nmodes - 1; --i >= 0; ) { | ||
595 | if (width > gspca_dev->cam.cam_mode[i].width) | ||
596 | break; | ||
597 | } | ||
598 | i++; | ||
599 | while (i < gspca_dev->cam.nmodes - 1 | ||
600 | && width == gspca_dev->cam.cam_mode[i + 1].width | ||
601 | && height < gspca_dev->cam.cam_mode[i + 1].height) | ||
602 | i++; | ||
603 | return i; | ||
604 | } | ||
605 | |||
606 | static __u32 get_v4l2_depth(__u32 pixfmt) | ||
607 | { | ||
608 | switch (pixfmt) { | ||
609 | case V4L2_PIX_FMT_BGR32: | ||
610 | case V4L2_PIX_FMT_RGB32: | ||
611 | return 32; | ||
612 | case V4L2_PIX_FMT_RGB24: | ||
613 | case V4L2_PIX_FMT_BGR24: | ||
614 | return 24; | ||
615 | case V4L2_PIX_FMT_RGB565: | ||
616 | case V4L2_PIX_FMT_YUYV: /* packed 4.2.2 */ | ||
617 | case V4L2_PIX_FMT_YYUV: | ||
618 | return 16; | ||
619 | case V4L2_PIX_FMT_YUV420: /* planar 4.2.0 */ | ||
620 | return 12; | ||
621 | case V4L2_PIX_FMT_MJPEG: | ||
622 | case V4L2_PIX_FMT_JPEG: | ||
623 | case V4L2_PIX_FMT_SBGGR8: /* Bayer */ | ||
624 | return 8; | ||
625 | } | ||
626 | PDEBUG(D_ERR|D_CONF, "Unknown pixel format %c%c%c%c", | ||
627 | pixfmt & 0xff, | ||
628 | (pixfmt >> 8) & 0xff, | ||
629 | (pixfmt >> 16) & 0xff, | ||
630 | pixfmt >> 24); | ||
631 | return -EINVAL; | ||
632 | } | ||
633 | |||
634 | /* | ||
635 | * search a mode with the right pixel format | ||
636 | */ | ||
637 | static int gspca_get_mode(struct gspca_dev *gspca_dev, | ||
638 | int mode, | ||
639 | int pixfmt) | ||
640 | { | ||
641 | int modeU, modeD; | ||
642 | |||
643 | modeU = modeD = mode; | ||
644 | while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) { | ||
645 | if (--modeD >= 0) { | ||
646 | if (gspca_dev->cam.cam_mode[modeD].pixfmt == pixfmt) | ||
647 | return modeD; | ||
648 | } | ||
649 | if (++modeU < gspca_dev->cam.nmodes) { | ||
650 | if (gspca_dev->cam.cam_mode[modeU].pixfmt == pixfmt) | ||
651 | return modeU; | ||
652 | } | ||
653 | } | ||
654 | return -EINVAL; | ||
655 | } | ||
656 | |||
657 | static int vidioc_enum_fmt_cap(struct file *file, void *priv, | ||
658 | struct v4l2_fmtdesc *fmtdesc) | ||
659 | { | ||
660 | struct gspca_dev *gspca_dev = priv; | ||
661 | int i, j, index; | ||
662 | __u32 fmt_tb[8]; | ||
663 | |||
664 | PDEBUG(D_CONF, "enum fmt cap"); | ||
665 | |||
666 | /* give an index to each format */ | ||
667 | index = 0; | ||
668 | j = 0; | ||
669 | for (i = gspca_dev->cam.nmodes; --i >= 0; ) { | ||
670 | fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixfmt; | ||
671 | j = 0; | ||
672 | for (;;) { | ||
673 | if (fmt_tb[j] == fmt_tb[index]) | ||
674 | break; | ||
675 | j++; | ||
676 | } | ||
677 | if (j == index) { | ||
678 | if (fmtdesc->index == index) | ||
679 | break; /* new format */ | ||
680 | index++; | ||
681 | if (index >= sizeof fmt_tb / sizeof fmt_tb[0]) | ||
682 | return -EINVAL; | ||
683 | } | ||
684 | } | ||
685 | if (i < 0) | ||
686 | return -EINVAL; /* no more format */ | ||
687 | |||
688 | fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | ||
689 | fmtdesc->pixelformat = fmt_tb[index]; | ||
690 | if (gspca_is_compressed(fmt_tb[index])) | ||
691 | fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED; | ||
692 | fmtdesc->description[0] = fmtdesc->pixelformat & 0xff; | ||
693 | fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff; | ||
694 | fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff; | ||
695 | fmtdesc->description[3] = fmtdesc->pixelformat >> 24; | ||
696 | fmtdesc->description[4] = '\0'; | ||
697 | return 0; | ||
698 | } | ||
699 | |||
700 | static int gspca_get_buff_size(struct gspca_dev *gspca_dev) | ||
701 | { | ||
702 | unsigned int size; | ||
703 | |||
704 | size = gspca_dev->width * gspca_dev->height | ||
705 | * get_v4l2_depth(gspca_dev->pixfmt) / 8; | ||
706 | if (!size) | ||
707 | return -ENOMEM; | ||
708 | return size; | ||
709 | } | ||
710 | |||
711 | static int vidioc_g_fmt_cap(struct file *file, void *priv, | ||
712 | struct v4l2_format *fmt) | ||
713 | { | ||
714 | struct gspca_dev *gspca_dev = priv; | ||
715 | |||
716 | fmt->fmt.pix.width = gspca_dev->width; | ||
717 | fmt->fmt.pix.height = gspca_dev->height; | ||
718 | fmt->fmt.pix.pixelformat = gspca_dev->pixfmt; | ||
719 | #ifdef GSPCA_DEBUG | ||
720 | if (gspca_debug & D_CONF) { | ||
721 | PDEBUG_MODE("get fmt cap", | ||
722 | fmt->fmt.pix.pixelformat, | ||
723 | fmt->fmt.pix.width, | ||
724 | fmt->fmt.pix.height); | ||
725 | } | ||
726 | #endif | ||
727 | fmt->fmt.pix.field = V4L2_FIELD_NONE; | ||
728 | fmt->fmt.pix.bytesperline = get_v4l2_depth(fmt->fmt.pix.pixelformat) | ||
729 | * fmt->fmt.pix.width / 8; | ||
730 | fmt->fmt.pix.sizeimage = fmt->fmt.pix.bytesperline | ||
731 | * fmt->fmt.pix.height; | ||
732 | /* (should be in the subdriver) */ | ||
733 | fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB; | ||
734 | fmt->fmt.pix.priv = 0; | ||
735 | return 0; | ||
736 | } | ||
737 | |||
738 | static int try_fmt_cap(struct file *file, | ||
739 | void *priv, | ||
740 | struct v4l2_format *fmt) | ||
741 | { | ||
742 | struct gspca_dev *gspca_dev = priv; | ||
743 | int w, h, mode, mode2, frsz; | ||
744 | |||
745 | w = (int) fmt->fmt.pix.width; | ||
746 | h = (int) fmt->fmt.pix.height; | ||
747 | #ifdef GSPCA_DEBUG | ||
748 | if (gspca_debug & D_CONF) | ||
749 | PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h); | ||
750 | #endif | ||
751 | /* search the closest mode for width and height */ | ||
752 | mode = wxh_to_mode(gspca_dev, w, h); | ||
753 | |||
754 | /* OK if right palette */ | ||
755 | if (gspca_dev->cam.cam_mode[mode].pixfmt != fmt->fmt.pix.pixelformat) { | ||
756 | |||
757 | /* else, search the closest mode with the same pixel format */ | ||
758 | mode2 = gspca_get_mode(gspca_dev, mode, | ||
759 | fmt->fmt.pix.pixelformat); | ||
760 | if (mode2 >= 0) | ||
761 | mode = mode2; | ||
762 | else { | ||
763 | |||
764 | /* no chance, return this mode */ | ||
765 | fmt->fmt.pix.pixelformat | ||
766 | = gspca_dev->cam.cam_mode[mode].pixfmt; | ||
767 | #ifdef GSPCA_DEBUG | ||
768 | if (gspca_debug & D_CONF) { | ||
769 | PDEBUG_MODE("new format", | ||
770 | fmt->fmt.pix.pixelformat, | ||
771 | gspca_dev->cam.cam_mode[mode].width, | ||
772 | gspca_dev->cam.cam_mode[mode].height); | ||
773 | } | ||
774 | #endif | ||
775 | } | ||
776 | } | ||
777 | fmt->fmt.pix.width = gspca_dev->cam.cam_mode[mode].width; | ||
778 | fmt->fmt.pix.height = gspca_dev->cam.cam_mode[mode].height; | ||
779 | fmt->fmt.pix.bytesperline = get_v4l2_depth(fmt->fmt.pix.pixelformat) | ||
780 | * fmt->fmt.pix.width / 8; | ||
781 | frsz = fmt->fmt.pix.bytesperline * fmt->fmt.pix.height; | ||
782 | if (gspca_is_compressed(fmt->fmt.pix.pixelformat)) | ||
783 | frsz = (frsz * comp_fac) / 100; | ||
784 | fmt->fmt.pix.sizeimage = frsz; | ||
785 | return mode; /* used when s_fmt */ | ||
786 | } | ||
787 | |||
788 | static int vidioc_try_fmt_cap(struct file *file, | ||
789 | void *priv, | ||
790 | struct v4l2_format *fmt) | ||
791 | { | ||
792 | int ret; | ||
793 | |||
794 | /* mutex_lock_interruptible(&gspca_dev->queue_lock); */ | ||
795 | ret = try_fmt_cap(file, priv, fmt); | ||
796 | /* mutex_unlock(&gspca_dev->queue_lock); */ | ||
797 | if (ret < 0) | ||
798 | return ret; | ||
799 | return 0; | ||
800 | } | ||
801 | |||
802 | static int vidioc_s_fmt_cap(struct file *file, void *priv, | ||
803 | struct v4l2_format *fmt) | ||
804 | { | ||
805 | struct gspca_dev *gspca_dev = priv; | ||
806 | int ret, was_streaming; | ||
807 | |||
808 | #ifdef GSPCA_DEBUG | ||
809 | if (gspca_debug & D_CONF) { | ||
810 | PDEBUG_MODE("set fmt cap", | ||
811 | fmt->fmt.pix.pixelformat, | ||
812 | fmt->fmt.pix.width, fmt->fmt.pix.height); | ||
813 | } | ||
814 | #endif | ||
815 | mutex_lock_interruptible(&gspca_dev->queue_lock); | ||
816 | ret = try_fmt_cap(file, priv, fmt); | ||
817 | if (ret < 0) | ||
818 | goto out; | ||
819 | |||
820 | if (ret == gspca_dev->curr_mode) | ||
821 | goto out; /* same mode */ | ||
822 | was_streaming = gspca_dev->streaming; | ||
823 | if (was_streaming != 0) | ||
824 | gspca_stream_off(gspca_dev); | ||
825 | gspca_dev->width = (int) fmt->fmt.pix.width; | ||
826 | gspca_dev->height = (int) fmt->fmt.pix.height; | ||
827 | gspca_dev->pixfmt = fmt->fmt.pix.pixelformat; | ||
828 | gspca_dev->curr_mode = ret; | ||
829 | if (was_streaming) | ||
830 | ret = gspca_init_transfer(gspca_dev); | ||
831 | out: | ||
832 | mutex_unlock(&gspca_dev->queue_lock); | ||
833 | return ret; | ||
834 | } | ||
835 | |||
836 | static int dev_open(struct inode *inode, struct file *file) | ||
837 | { | ||
838 | struct gspca_dev *gspca_dev; | ||
839 | int ret; | ||
840 | |||
841 | PDEBUG(D_STREAM, "opening"); | ||
842 | gspca_dev = (struct gspca_dev *) video_devdata(file); | ||
843 | ret = mutex_lock_interruptible(&gspca_dev->queue_lock); | ||
844 | if (ret < 0) | ||
845 | return ret; | ||
846 | if (!gspca_dev->present) { | ||
847 | ret = -ENODEV; | ||
848 | goto out; | ||
849 | } | ||
850 | |||
851 | /* if not done yet, initialize the sensor */ | ||
852 | if (gspca_dev->users == 0) { | ||
853 | ret = mutex_lock_interruptible(&gspca_dev->usb_lock); | ||
854 | if (ret < 0) | ||
855 | goto out; | ||
856 | ret = gspca_dev->sd_desc->open(gspca_dev); | ||
857 | mutex_unlock(&gspca_dev->usb_lock); | ||
858 | if (ret != 0) { | ||
859 | PDEBUG(D_ERR|D_CONF, "init device failed %d", ret); | ||
860 | goto out; | ||
861 | } | ||
862 | } else if (gspca_dev->users > 8) { /* (arbitrary value) */ | ||
863 | ret = -EBUSY; | ||
864 | goto out; | ||
865 | } | ||
866 | gspca_dev->users++; | ||
867 | file->private_data = gspca_dev; | ||
868 | #ifdef GSPCA_DEBUG | ||
869 | /* activate the v4l2 debug */ | ||
870 | if (gspca_debug & D_CONF) | ||
871 | gspca_dev->vdev.debug |= 3; | ||
872 | else | ||
873 | gspca_dev->vdev.debug &= ~3; | ||
874 | #endif | ||
875 | out: | ||
876 | mutex_unlock(&gspca_dev->queue_lock); | ||
877 | if (ret != 0) | ||
878 | PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret); | ||
879 | else | ||
880 | PDEBUG(D_STREAM, "open OK"); | ||
881 | return ret; | ||
882 | } | ||
883 | |||
884 | static int dev_close(struct inode *inode, struct file *file) | ||
885 | { | ||
886 | struct gspca_dev *gspca_dev = file->private_data; | ||
887 | |||
888 | PDEBUG(D_STREAM, "closing"); | ||
889 | if (gspca_dev->streaming) { | ||
890 | mutex_lock_interruptible(&gspca_dev->queue_lock); | ||
891 | gspca_stream_off(gspca_dev); | ||
892 | mutex_unlock(&gspca_dev->queue_lock); | ||
893 | } | ||
894 | mutex_lock_interruptible(&gspca_dev->usb_lock); | ||
895 | gspca_dev->sd_desc->close(gspca_dev); | ||
896 | mutex_unlock(&gspca_dev->usb_lock); | ||
897 | atomic_inc(&gspca_dev->nevent); | ||
898 | wake_up_interruptible(&gspca_dev->wq); /* wake blocked processes */ | ||
899 | schedule(); | ||
900 | mutex_lock_interruptible(&gspca_dev->queue_lock); | ||
901 | frame_free(gspca_dev); | ||
902 | file->private_data = NULL; | ||
903 | gspca_dev->users--; | ||
904 | mutex_unlock(&gspca_dev->queue_lock); | ||
905 | PDEBUG(D_STREAM, "closed"); | ||
906 | return 0; | ||
907 | } | ||
908 | |||
909 | static int vidioc_querycap(struct file *file, void *priv, | ||
910 | struct v4l2_capability *cap) | ||
911 | { | ||
912 | struct gspca_dev *gspca_dev = priv; | ||
913 | |||
914 | PDEBUG(D_CONF, "querycap"); | ||
915 | memset(cap, 0, sizeof *cap); | ||
916 | strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver); | ||
917 | strncpy(cap->card, gspca_dev->cam.dev_name, sizeof cap->card); | ||
918 | strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name, | ||
919 | sizeof cap->bus_info); | ||
920 | cap->version = DRIVER_VERSION_NUMBER; | ||
921 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | ||
922 | | V4L2_CAP_STREAMING | ||
923 | | V4L2_CAP_READWRITE; | ||
924 | return 0; | ||
925 | } | ||
926 | |||
927 | static int vidioc_queryctrl(struct file *file, void *priv, | ||
928 | struct v4l2_queryctrl *q_ctrl) | ||
929 | { | ||
930 | struct gspca_dev *gspca_dev = priv; | ||
931 | int i; | ||
932 | |||
933 | PDEBUG(D_CONF, "queryctrl"); | ||
934 | for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) { | ||
935 | if (q_ctrl->id == gspca_dev->sd_desc->ctrls[i].qctrl.id) { | ||
936 | memcpy(q_ctrl, | ||
937 | &gspca_dev->sd_desc->ctrls[i].qctrl, | ||
938 | sizeof *q_ctrl); | ||
939 | return 0; | ||
940 | } | ||
941 | } | ||
942 | if (q_ctrl->id >= V4L2_CID_BASE | ||
943 | && q_ctrl->id <= V4L2_CID_LASTP1) { | ||
944 | q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED; | ||
945 | return 0; | ||
946 | } | ||
947 | return -EINVAL; | ||
948 | } | ||
949 | |||
950 | static int vidioc_s_ctrl(struct file *file, void *priv, | ||
951 | struct v4l2_control *ctrl) | ||
952 | { | ||
953 | struct gspca_dev *gspca_dev = priv; | ||
954 | struct ctrl *ctrls; | ||
955 | int i, ret; | ||
956 | |||
957 | PDEBUG(D_CONF, "set ctrl"); | ||
958 | for (i = 0, ctrls = gspca_dev->sd_desc->ctrls; | ||
959 | i < gspca_dev->sd_desc->nctrls; | ||
960 | i++, ctrls++) { | ||
961 | if (ctrl->id != ctrls->qctrl.id) | ||
962 | continue; | ||
963 | if (ctrl->value < ctrls->qctrl.minimum | ||
964 | && ctrl->value > ctrls->qctrl.maximum) | ||
965 | return -ERANGE; | ||
966 | PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value); | ||
967 | mutex_lock_interruptible(&gspca_dev->usb_lock); | ||
968 | ret = ctrls->set(gspca_dev, ctrl->value); | ||
969 | mutex_unlock(&gspca_dev->usb_lock); | ||
970 | return ret; | ||
971 | } | ||
972 | return -EINVAL; | ||
973 | } | ||
974 | |||
975 | static int vidioc_g_ctrl(struct file *file, void *priv, | ||
976 | struct v4l2_control *ctrl) | ||
977 | { | ||
978 | struct gspca_dev *gspca_dev = priv; | ||
979 | |||
980 | struct ctrl *ctrls; | ||
981 | int i, ret; | ||
982 | |||
983 | for (i = 0, ctrls = gspca_dev->sd_desc->ctrls; | ||
984 | i < gspca_dev->sd_desc->nctrls; | ||
985 | i++, ctrls++) { | ||
986 | if (ctrl->id != ctrls->qctrl.id) | ||
987 | continue; | ||
988 | mutex_lock_interruptible(&gspca_dev->usb_lock); | ||
989 | ret = ctrls->get(gspca_dev, &ctrl->value); | ||
990 | mutex_unlock(&gspca_dev->usb_lock); | ||
991 | return ret; | ||
992 | } | ||
993 | return -EINVAL; | ||
994 | } | ||
995 | |||
996 | static int vidioc_querymenu(struct file *file, void *priv, | ||
997 | struct v4l2_querymenu *qmenu) | ||
998 | { | ||
999 | struct gspca_dev *gspca_dev = priv; | ||
1000 | |||
1001 | if (!gspca_dev->sd_desc->querymenu) | ||
1002 | return -EINVAL; | ||
1003 | return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu); | ||
1004 | } | ||
1005 | |||
1006 | static int vidioc_enum_input(struct file *file, void *priv, | ||
1007 | struct v4l2_input *input) | ||
1008 | { | ||
1009 | struct gspca_dev *gspca_dev = priv; | ||
1010 | |||
1011 | if (input->index != 0) | ||
1012 | return -EINVAL; | ||
1013 | memset(input, 0, sizeof *input); | ||
1014 | input->type = V4L2_INPUT_TYPE_CAMERA; | ||
1015 | strncpy(input->name, gspca_dev->sd_desc->name, | ||
1016 | sizeof input->name); | ||
1017 | return 0; | ||
1018 | } | ||
1019 | |||
1020 | static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) | ||
1021 | { | ||
1022 | *i = 0; | ||
1023 | return 0; | ||
1024 | } | ||
1025 | |||
1026 | static int vidioc_s_input(struct file *file, void *priv, unsigned int i) | ||
1027 | { | ||
1028 | if (i > 0) | ||
1029 | return -EINVAL; | ||
1030 | return (0); | ||
1031 | } | ||
1032 | |||
1033 | static int vidioc_reqbufs(struct file *file, void *priv, | ||
1034 | struct v4l2_requestbuffers *rb) | ||
1035 | { | ||
1036 | struct gspca_dev *gspca_dev = priv; | ||
1037 | int frsz, ret; | ||
1038 | |||
1039 | PDEBUG(D_STREAM, "reqbufs %d", rb->count); | ||
1040 | if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | ||
1041 | return -EINVAL; | ||
1042 | if (rb->memory != V4L2_MEMORY_MMAP | ||
1043 | && rb->memory != V4L2_MEMORY_USERPTR) | ||
1044 | return -EINVAL; | ||
1045 | if (rb->count == 0) | ||
1046 | return -EINVAL; | ||
1047 | frsz = gspca_get_buff_size(gspca_dev); | ||
1048 | if (frsz < 0) | ||
1049 | return frsz; | ||
1050 | ret = mutex_lock_interruptible(&gspca_dev->queue_lock); | ||
1051 | if (ret < 0) | ||
1052 | return ret; | ||
1053 | ret = frame_alloc(gspca_dev, | ||
1054 | rb->count, | ||
1055 | (unsigned int) frsz, | ||
1056 | rb->memory); | ||
1057 | if (ret == 0) | ||
1058 | rb->count = gspca_dev->nframes; | ||
1059 | mutex_unlock(&gspca_dev->queue_lock); | ||
1060 | PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count); | ||
1061 | return ret; | ||
1062 | } | ||
1063 | |||
1064 | static int vidioc_querybuf(struct file *file, void *priv, | ||
1065 | struct v4l2_buffer *v4l2_buf) | ||
1066 | { | ||
1067 | struct gspca_dev *gspca_dev = priv; | ||
1068 | struct gspca_frame *frame; | ||
1069 | |||
1070 | PDEBUG(D_STREAM, "querybuf"); | ||
1071 | if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE | ||
1072 | || v4l2_buf->index < 0 | ||
1073 | || v4l2_buf->index >= gspca_dev->nframes) | ||
1074 | return -EINVAL; | ||
1075 | |||
1076 | frame = &gspca_dev->frame[v4l2_buf->index]; | ||
1077 | memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf); | ||
1078 | return 0; | ||
1079 | } | ||
1080 | |||
1081 | static int vidioc_streamon(struct file *file, void *priv, | ||
1082 | enum v4l2_buf_type buf_type) | ||
1083 | { | ||
1084 | struct gspca_dev *gspca_dev = priv; | ||
1085 | int ret; | ||
1086 | |||
1087 | PDEBUG(D_STREAM, "stream on"); | ||
1088 | if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | ||
1089 | return -EINVAL; | ||
1090 | ret = mutex_lock_interruptible(&gspca_dev->queue_lock); | ||
1091 | if (ret < 0) | ||
1092 | return ret; | ||
1093 | if (!gspca_dev->present) { | ||
1094 | ret = -ENODEV; | ||
1095 | goto out; | ||
1096 | } | ||
1097 | if (gspca_dev->nframes == 0) { | ||
1098 | ret = -EINVAL; | ||
1099 | goto out; | ||
1100 | } | ||
1101 | if (!gspca_dev->streaming) { | ||
1102 | ret = gspca_init_transfer(gspca_dev); | ||
1103 | if (ret < 0) | ||
1104 | goto out; | ||
1105 | } | ||
1106 | #ifdef GSPCA_DEBUG | ||
1107 | if (gspca_debug & D_STREAM) { | ||
1108 | PDEBUG_MODE("stream on OK", | ||
1109 | gspca_dev->pixfmt, | ||
1110 | gspca_dev->width, | ||
1111 | gspca_dev->height); | ||
1112 | } | ||
1113 | #endif | ||
1114 | out: | ||
1115 | mutex_unlock(&gspca_dev->queue_lock); | ||
1116 | return ret; | ||
1117 | } | ||
1118 | |||
1119 | static int vidioc_streamoff(struct file *file, void *priv, | ||
1120 | enum v4l2_buf_type buf_type) | ||
1121 | { | ||
1122 | struct gspca_dev *gspca_dev = priv; | ||
1123 | |||
1124 | PDEBUG(D_STREAM, "stream off"); | ||
1125 | if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | ||
1126 | return -EINVAL; | ||
1127 | if (gspca_dev->streaming) { | ||
1128 | mutex_lock_interruptible(&gspca_dev->queue_lock); | ||
1129 | gspca_stream_off(gspca_dev); | ||
1130 | mutex_unlock(&gspca_dev->queue_lock); | ||
1131 | } | ||
1132 | return 0; | ||
1133 | } | ||
1134 | |||
1135 | static int vidioc_g_jpegcomp(struct file *file, void *priv, | ||
1136 | struct v4l2_jpegcompression *jpegcomp) | ||
1137 | { | ||
1138 | struct gspca_dev *gspca_dev = priv; | ||
1139 | int ret; | ||
1140 | |||
1141 | if (!gspca_dev->sd_desc->get_jcomp) | ||
1142 | return -EINVAL; | ||
1143 | mutex_lock_interruptible(&gspca_dev->usb_lock); | ||
1144 | ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp); | ||
1145 | mutex_unlock(&gspca_dev->usb_lock); | ||
1146 | return ret; | ||
1147 | } | ||
1148 | |||
1149 | static int vidioc_s_jpegcomp(struct file *file, void *priv, | ||
1150 | struct v4l2_jpegcompression *jpegcomp) | ||
1151 | { | ||
1152 | struct gspca_dev *gspca_dev = priv; | ||
1153 | int ret; | ||
1154 | |||
1155 | mutex_lock_interruptible(&gspca_dev->usb_lock); | ||
1156 | if (!gspca_dev->sd_desc->set_jcomp) | ||
1157 | return -EINVAL; | ||
1158 | ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp); | ||
1159 | mutex_unlock(&gspca_dev->usb_lock); | ||
1160 | return ret; | ||
1161 | } | ||
1162 | |||
1163 | static int vidioc_g_parm(struct file *filp, void *priv, | ||
1164 | struct v4l2_streamparm *parm) | ||
1165 | { | ||
1166 | struct gspca_dev *gspca_dev = priv; | ||
1167 | |||
1168 | memset(parm, 0, sizeof parm); | ||
1169 | parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | ||
1170 | parm->parm.capture.readbuffers = gspca_dev->nbufread; | ||
1171 | return 0; | ||
1172 | } | ||
1173 | |||
1174 | static int vidioc_s_parm(struct file *filp, void *priv, | ||
1175 | struct v4l2_streamparm *parm) | ||
1176 | { | ||
1177 | struct gspca_dev *gspca_dev = priv; | ||
1178 | int n; | ||
1179 | |||
1180 | mutex_lock_interruptible(&gspca_dev->usb_lock); | ||
1181 | n = parm->parm.capture.readbuffers; | ||
1182 | if (n == 0 || n > GSPCA_MAX_FRAMES) | ||
1183 | parm->parm.capture.readbuffers = gspca_dev->nbufread; | ||
1184 | else | ||
1185 | gspca_dev->nbufread = n; | ||
1186 | mutex_unlock(&gspca_dev->usb_lock); | ||
1187 | return 0; | ||
1188 | } | ||
1189 | |||
1190 | #ifdef CONFIG_VIDEO_V4L1_COMPAT | ||
1191 | static int vidiocgmbuf(struct file *file, void *priv, | ||
1192 | struct video_mbuf *mbuf) | ||
1193 | { | ||
1194 | struct gspca_dev *gspca_dev = file->private_data; | ||
1195 | int i; | ||
1196 | |||
1197 | PDEBUG(D_STREAM, "cgmbuf"); | ||
1198 | if (gspca_dev->nframes == 0) { | ||
1199 | struct v4l2_requestbuffers rb; | ||
1200 | int ret; | ||
1201 | |||
1202 | memset(&rb, 0, sizeof rb); | ||
1203 | rb.count = 4; | ||
1204 | rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | ||
1205 | rb.memory = V4L2_MEMORY_MMAP; | ||
1206 | ret = vidioc_reqbufs(file, priv, &rb); | ||
1207 | if (ret != 0) | ||
1208 | return ret; | ||
1209 | } | ||
1210 | mbuf->frames = gspca_dev->nframes; | ||
1211 | mbuf->size = gspca_dev->frsz * gspca_dev->nframes; | ||
1212 | for (i = 0; i < mbuf->frames; i++) | ||
1213 | mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset; | ||
1214 | return 0; | ||
1215 | } | ||
1216 | #endif | ||
1217 | |||
1218 | static int dev_mmap(struct file *file, struct vm_area_struct *vma) | ||
1219 | { | ||
1220 | struct gspca_dev *gspca_dev = file->private_data; | ||
1221 | struct gspca_frame *frame = 0; | ||
1222 | struct page *page; | ||
1223 | unsigned long addr, start, size; | ||
1224 | int i, ret; | ||
1225 | #ifdef CONFIG_VIDEO_V4L1_COMPAT | ||
1226 | int compat = 0; | ||
1227 | #endif | ||
1228 | |||
1229 | start = vma->vm_start; | ||
1230 | size = vma->vm_end - vma->vm_start; | ||
1231 | PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size); | ||
1232 | |||
1233 | ret = mutex_lock_interruptible(&gspca_dev->queue_lock); | ||
1234 | if (ret < 0) | ||
1235 | return ret; | ||
1236 | /* sanity check disconnect, in use, no memory available */ | ||
1237 | if (!gspca_dev->present) { | ||
1238 | ret = -ENODEV; | ||
1239 | goto done; | ||
1240 | } | ||
1241 | |||
1242 | for (i = 0; i < gspca_dev->nframes; ++i) { | ||
1243 | if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) { | ||
1244 | PDEBUG(D_STREAM, "mmap bad memory type"); | ||
1245 | break; | ||
1246 | } | ||
1247 | if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT) | ||
1248 | == vma->vm_pgoff) { | ||
1249 | frame = &gspca_dev->frame[i]; | ||
1250 | break; | ||
1251 | } | ||
1252 | } | ||
1253 | if (frame == 0) { | ||
1254 | PDEBUG(D_STREAM, "mmap no frame buffer found"); | ||
1255 | ret = -EINVAL; | ||
1256 | goto done; | ||
1257 | } | ||
1258 | #ifdef CONFIG_VIDEO_V4L1_COMPAT | ||
1259 | if (i == 0 && size == frame->v4l2_buf.length * gspca_dev->nframes) | ||
1260 | compat = 1; | ||
1261 | else | ||
1262 | #endif | ||
1263 | if (size != frame->v4l2_buf.length) { | ||
1264 | PDEBUG(D_STREAM, "mmap bad size"); | ||
1265 | ret = -EINVAL; | ||
1266 | goto done; | ||
1267 | } | ||
1268 | |||
1269 | /* | ||
1270 | * - VM_IO marks the area as being a mmaped region for I/O to a | ||
1271 | * device. It also prevents the region from being core dumped. | ||
1272 | */ | ||
1273 | vma->vm_flags |= VM_IO; | ||
1274 | |||
1275 | addr = (unsigned long) frame->data; | ||
1276 | while (size > 0) { | ||
1277 | page = vmalloc_to_page((void *) addr); | ||
1278 | ret = vm_insert_page(vma, start, page); | ||
1279 | if (ret < 0) | ||
1280 | goto done; | ||
1281 | start += PAGE_SIZE; | ||
1282 | addr += PAGE_SIZE; | ||
1283 | size -= PAGE_SIZE; | ||
1284 | } | ||
1285 | |||
1286 | vma->vm_ops = &gspca_vm_ops; | ||
1287 | vma->vm_private_data = frame; | ||
1288 | gspca_vm_open(vma); | ||
1289 | #ifdef CONFIG_VIDEO_V4L1_COMPAT | ||
1290 | if (compat) { | ||
1291 | /*fixme: ugly*/ | ||
1292 | for (i = 1; i < gspca_dev->nframes; ++i) | ||
1293 | gspca_dev->frame[i].v4l2_buf.flags |= | ||
1294 | V4L2_BUF_FLAG_MAPPED; | ||
1295 | } | ||
1296 | #endif | ||
1297 | done: | ||
1298 | mutex_unlock(&gspca_dev->queue_lock); | ||
1299 | return ret; | ||
1300 | } | ||
1301 | |||
1302 | static unsigned int dev_poll(struct file *file, poll_table * wait) | ||
1303 | { | ||
1304 | struct gspca_dev *gspca_dev = file->private_data; | ||
1305 | int i, ret; | ||
1306 | |||
1307 | PDEBUG(D_FRAM, "poll"); | ||
1308 | |||
1309 | poll_wait(file, &gspca_dev->wq, wait); | ||
1310 | |||
1311 | if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0) | ||
1312 | return POLLERR; | ||
1313 | if (gspca_dev->dev == 0 | ||
1314 | || !gspca_dev->streaming) /* if not streaming */ | ||
1315 | ret = POLLERR; | ||
1316 | else { | ||
1317 | i = gspca_dev->fr_o; | ||
1318 | i = gspca_dev->fr_queue[i]; | ||
1319 | if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE) | ||
1320 | ret = POLLIN | POLLRDNORM; /* something to read */ | ||
1321 | else | ||
1322 | ret = 0; | ||
1323 | } | ||
1324 | mutex_unlock(&gspca_dev->queue_lock); | ||
1325 | return ret; | ||
1326 | } | ||
1327 | |||
1328 | /* | ||
1329 | * wait for a video frame | ||
1330 | * | ||
1331 | * If a frame is ready, its index is returned. | ||
1332 | */ | ||
1333 | static int gspca_frame_wait(struct gspca_dev *gspca_dev, | ||
1334 | int nonblock_ing) | ||
1335 | { | ||
1336 | struct gspca_frame *frame; | ||
1337 | int i, j, ret; | ||
1338 | |||
1339 | i = gspca_dev->fr_o; | ||
1340 | j = gspca_dev->fr_queue[i]; | ||
1341 | frame = &gspca_dev->frame[j]; | ||
1342 | if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) | ||
1343 | goto ok; | ||
1344 | if (nonblock_ing) /* no frame yet */ | ||
1345 | return -EAGAIN; | ||
1346 | |||
1347 | /* wait till a frame is ready */ | ||
1348 | for (;;) { | ||
1349 | ret = wait_event_interruptible(gspca_dev->wq, | ||
1350 | atomic_read(&gspca_dev->nevent) > 0); | ||
1351 | if (ret != 0) | ||
1352 | return ret; | ||
1353 | i = gspca_dev->fr_o; | ||
1354 | j = gspca_dev->fr_queue[i]; | ||
1355 | frame = &gspca_dev->frame[j]; | ||
1356 | if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) | ||
1357 | break; | ||
1358 | } | ||
1359 | |||
1360 | ok: | ||
1361 | atomic_dec(&gspca_dev->nevent); | ||
1362 | gspca_dev->fr_o = (i + 1) % gspca_dev->nframes; | ||
1363 | PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d", | ||
1364 | gspca_dev->fr_q, | ||
1365 | gspca_dev->fr_i, | ||
1366 | gspca_dev->fr_o); | ||
1367 | return j; | ||
1368 | } | ||
1369 | |||
1370 | /* | ||
1371 | * dequeue a video buffer | ||
1372 | * | ||
1373 | * If nonblock_ing is false, block until a buffer is available. | ||
1374 | */ | ||
1375 | static int vidioc_dqbuf(struct file *file, void *priv, | ||
1376 | struct v4l2_buffer *v4l2_buf) | ||
1377 | { | ||
1378 | struct gspca_dev *gspca_dev = priv; | ||
1379 | struct gspca_frame *frame; | ||
1380 | int i, ret; | ||
1381 | |||
1382 | PDEBUG(D_FRAM, "dqbuf"); | ||
1383 | if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE | ||
1384 | || (v4l2_buf->memory != V4L2_MEMORY_MMAP | ||
1385 | && v4l2_buf->memory != V4L2_MEMORY_USERPTR)) | ||
1386 | return -EINVAL; | ||
1387 | if (!gspca_dev->streaming) | ||
1388 | return -EINVAL; | ||
1389 | |||
1390 | /* only one read */ | ||
1391 | if (mutex_lock_interruptible(&gspca_dev->read_lock)) | ||
1392 | return -ERESTARTSYS; | ||
1393 | |||
1394 | ret = gspca_frame_wait(gspca_dev, file->f_flags & O_NONBLOCK); | ||
1395 | if (ret < 0) | ||
1396 | goto done; | ||
1397 | i = ret; /* frame index */ | ||
1398 | frame = &gspca_dev->frame[i]; | ||
1399 | frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; | ||
1400 | memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf); | ||
1401 | PDEBUG(D_FRAM, "dqbuf %d", i); | ||
1402 | ret = 0; | ||
1403 | done: | ||
1404 | mutex_unlock(&gspca_dev->read_lock); | ||
1405 | return ret; | ||
1406 | } | ||
1407 | |||
1408 | /* | ||
1409 | * queue a video buffer | ||
1410 | * | ||
1411 | * Attempting to queue a buffer that has already been | ||
1412 | * queued will return -EINVAL. | ||
1413 | */ | ||
1414 | static int vidioc_qbuf(struct file *file, void *priv, | ||
1415 | struct v4l2_buffer *v4l2_buf) | ||
1416 | { | ||
1417 | struct gspca_dev *gspca_dev = priv; | ||
1418 | struct gspca_frame *frame; | ||
1419 | int i, index, ret; | ||
1420 | |||
1421 | PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index); | ||
1422 | if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | ||
1423 | return -EINVAL; | ||
1424 | |||
1425 | index = v4l2_buf->index; | ||
1426 | if ((unsigned) index >= gspca_dev->nframes) { | ||
1427 | PDEBUG(D_STREAM, | ||
1428 | "qbuf idx %d >= %d", index, gspca_dev->nframes); | ||
1429 | return -EINVAL; | ||
1430 | } | ||
1431 | frame = &gspca_dev->frame[index]; | ||
1432 | |||
1433 | if (v4l2_buf->memory != frame->v4l2_buf.memory) { | ||
1434 | PDEBUG(D_STREAM, "qbuf bad memory type"); | ||
1435 | return -EINVAL; | ||
1436 | } | ||
1437 | |||
1438 | ret = mutex_lock_interruptible(&gspca_dev->queue_lock); | ||
1439 | if (ret < 0) | ||
1440 | return ret; | ||
1441 | if (frame->v4l2_buf.flags | ||
1442 | & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)) { | ||
1443 | PDEBUG(D_STREAM, "qbuf bad state"); | ||
1444 | ret = -EINVAL; | ||
1445 | goto out; | ||
1446 | } | ||
1447 | |||
1448 | frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED; | ||
1449 | frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; | ||
1450 | |||
1451 | if (v4l2_buf->memory == V4L2_MEMORY_USERPTR) { | ||
1452 | frame->data = frame->data_end = | ||
1453 | (unsigned char *) v4l2_buf->m.userptr; | ||
1454 | frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr; | ||
1455 | frame->v4l2_buf.length = v4l2_buf->length; | ||
1456 | } | ||
1457 | |||
1458 | /* put the buffer in the 'queued' queue */ | ||
1459 | i = gspca_dev->fr_q; | ||
1460 | gspca_dev->fr_queue[i] = index; | ||
1461 | gspca_dev->fr_q = (i + 1) % gspca_dev->nframes; | ||
1462 | PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d", | ||
1463 | gspca_dev->fr_q, | ||
1464 | gspca_dev->fr_i, | ||
1465 | gspca_dev->fr_o); | ||
1466 | |||
1467 | v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED; | ||
1468 | v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE; | ||
1469 | ret = 0; | ||
1470 | out: | ||
1471 | mutex_unlock(&gspca_dev->queue_lock); | ||
1472 | return ret; | ||
1473 | } | ||
1474 | |||
1475 | static ssize_t dev_read(struct file *file, char __user *data, | ||
1476 | size_t count, loff_t *ppos) | ||
1477 | { | ||
1478 | struct gspca_dev *gspca_dev = file->private_data; | ||
1479 | struct gspca_frame *frame; | ||
1480 | struct v4l2_buffer v4l2_buf; | ||
1481 | struct timeval timestamp; | ||
1482 | int i, ret, ret2; | ||
1483 | |||
1484 | PDEBUG(D_FRAM, "read (%p, %d)", data, count); | ||
1485 | if (gspca_dev->nframes == 0) { | ||
1486 | struct v4l2_requestbuffers rb; | ||
1487 | |||
1488 | memset(&rb, 0, sizeof rb); | ||
1489 | rb.count = gspca_dev->nbufread; | ||
1490 | rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | ||
1491 | rb.memory = V4L2_MEMORY_MMAP; | ||
1492 | ret = vidioc_reqbufs(file, gspca_dev, &rb); | ||
1493 | if (ret != 0) { | ||
1494 | PDEBUG(D_STREAM, "read reqbuf err: %d", ret); | ||
1495 | return ret; | ||
1496 | } | ||
1497 | memset(&v4l2_buf, 0, sizeof v4l2_buf); | ||
1498 | v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | ||
1499 | v4l2_buf.memory = V4L2_MEMORY_MMAP; | ||
1500 | for (i = 0; i < gspca_dev->nbufread; i++) { | ||
1501 | v4l2_buf.index = i; | ||
1502 | /*fixme: ugly!*/ | ||
1503 | gspca_dev->frame[i].v4l2_buf.flags |= | ||
1504 | V4L2_BUF_FLAG_MAPPED; | ||
1505 | ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf); | ||
1506 | if (ret != 0) { | ||
1507 | PDEBUG(D_STREAM, "read qbuf err: %d", ret); | ||
1508 | return ret; | ||
1509 | } | ||
1510 | } | ||
1511 | } | ||
1512 | if (!gspca_dev->streaming) { | ||
1513 | ret = vidioc_streamon(file, gspca_dev, | ||
1514 | V4L2_BUF_TYPE_VIDEO_CAPTURE); | ||
1515 | if (ret != 0) { | ||
1516 | PDEBUG(D_STREAM, "read streamon err %d", ret); | ||
1517 | return ret; | ||
1518 | } | ||
1519 | } | ||
1520 | |||
1521 | /* get a frame */ | ||
1522 | jiffies_to_timeval(get_jiffies_64(), ×tamp); | ||
1523 | timestamp.tv_sec--; | ||
1524 | for (i = 0; i < 2; i++) { | ||
1525 | memset(&v4l2_buf, 0, sizeof v4l2_buf); | ||
1526 | v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | ||
1527 | v4l2_buf.memory = V4L2_MEMORY_MMAP; | ||
1528 | ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf); | ||
1529 | if (ret != 0) { | ||
1530 | PDEBUG(D_STREAM, "read dqbuf err %d", ret); | ||
1531 | return ret; | ||
1532 | } | ||
1533 | |||
1534 | /* if the process slept for more than 1 second, | ||
1535 | * get a brand new frame */ | ||
1536 | frame = &gspca_dev->frame[v4l2_buf.index]; | ||
1537 | if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec) | ||
1538 | break; | ||
1539 | ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf); | ||
1540 | if (ret != 0) { | ||
1541 | PDEBUG(D_STREAM, "read qbuf err %d", ret); | ||
1542 | return ret; | ||
1543 | } | ||
1544 | } | ||
1545 | |||
1546 | /* copy the frame */ | ||
1547 | if (count < frame->v4l2_buf.bytesused) { | ||
1548 | PDEBUG(D_STREAM, "read bad count: %d < %d", | ||
1549 | count, frame->v4l2_buf.bytesused); | ||
1550 | /*fixme: special errno?*/ | ||
1551 | ret = -EINVAL; | ||
1552 | goto out; | ||
1553 | } | ||
1554 | count = frame->v4l2_buf.bytesused; | ||
1555 | ret = copy_to_user(data, frame->data, count); | ||
1556 | if (ret != 0) { | ||
1557 | PDEBUG(D_ERR|D_STREAM, | ||
1558 | "read cp to user lack %d / %d", ret, count); | ||
1559 | ret = -EFAULT; | ||
1560 | goto out; | ||
1561 | } | ||
1562 | ret = count; | ||
1563 | out: | ||
1564 | /* in each case, requeue the buffer */ | ||
1565 | ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf); | ||
1566 | if (ret2 != 0) | ||
1567 | return ret2; | ||
1568 | return ret; | ||
1569 | } | ||
1570 | |||
1571 | static void gspca_dev_release(struct video_device *vfd) | ||
1572 | { | ||
1573 | /* nothing */ | ||
1574 | } | ||
1575 | |||
1576 | static struct file_operations dev_fops = { | ||
1577 | .owner = THIS_MODULE, | ||
1578 | .open = dev_open, | ||
1579 | .release = dev_close, | ||
1580 | .read = dev_read, | ||
1581 | .mmap = dev_mmap, | ||
1582 | .ioctl = video_ioctl2, | ||
1583 | .llseek = no_llseek, | ||
1584 | .poll = dev_poll, | ||
1585 | }; | ||
1586 | |||
1587 | static struct video_device gspca_template = { | ||
1588 | .name = "gspca main driver", | ||
1589 | .type = VID_TYPE_CAPTURE, | ||
1590 | .fops = &dev_fops, | ||
1591 | .release = gspca_dev_release, /* mandatory */ | ||
1592 | .minor = -1, | ||
1593 | .vidioc_querycap = vidioc_querycap, | ||
1594 | .vidioc_dqbuf = vidioc_dqbuf, | ||
1595 | .vidioc_qbuf = vidioc_qbuf, | ||
1596 | .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap, | ||
1597 | .vidioc_try_fmt_cap = vidioc_try_fmt_cap, | ||
1598 | .vidioc_g_fmt_cap = vidioc_g_fmt_cap, | ||
1599 | .vidioc_s_fmt_cap = vidioc_s_fmt_cap, | ||
1600 | .vidioc_streamon = vidioc_streamon, | ||
1601 | .vidioc_queryctrl = vidioc_queryctrl, | ||
1602 | .vidioc_g_ctrl = vidioc_g_ctrl, | ||
1603 | .vidioc_s_ctrl = vidioc_s_ctrl, | ||
1604 | .vidioc_querymenu = vidioc_querymenu, | ||
1605 | .vidioc_enum_input = vidioc_enum_input, | ||
1606 | .vidioc_g_input = vidioc_g_input, | ||
1607 | .vidioc_s_input = vidioc_s_input, | ||
1608 | .vidioc_reqbufs = vidioc_reqbufs, | ||
1609 | .vidioc_querybuf = vidioc_querybuf, | ||
1610 | .vidioc_streamoff = vidioc_streamoff, | ||
1611 | .vidioc_g_jpegcomp = vidioc_g_jpegcomp, | ||
1612 | .vidioc_s_jpegcomp = vidioc_s_jpegcomp, | ||
1613 | .vidioc_g_parm = vidioc_g_parm, | ||
1614 | .vidioc_s_parm = vidioc_s_parm, | ||
1615 | #ifdef CONFIG_VIDEO_V4L1_COMPAT | ||
1616 | .vidiocgmbuf = vidiocgmbuf, | ||
1617 | #endif | ||
1618 | }; | ||
1619 | |||
1620 | /* | ||
1621 | * probe and create a new gspca device | ||
1622 | * | ||
1623 | * This function must be called by the sub-driver when it is | ||
1624 | * called for probing a new device. | ||
1625 | */ | ||
1626 | int gspca_dev_probe(struct usb_interface *intf, | ||
1627 | const struct usb_device_id *id, | ||
1628 | const struct sd_desc *sd_desc, | ||
1629 | int dev_size) | ||
1630 | { | ||
1631 | struct usb_interface_descriptor *interface; | ||
1632 | struct gspca_dev *gspca_dev; | ||
1633 | struct usb_device *dev = interface_to_usbdev(intf); | ||
1634 | int ret; | ||
1635 | __u16 vendor; | ||
1636 | __u16 product; | ||
1637 | |||
1638 | vendor = le16_to_cpu(dev->descriptor.idVendor); | ||
1639 | product = le16_to_cpu(dev->descriptor.idProduct); | ||
1640 | PDEBUG(D_PROBE, "probing %04x:%04x", vendor, product); | ||
1641 | |||
1642 | /* we don't handle multi-config cameras */ | ||
1643 | if (dev->descriptor.bNumConfigurations != 1) | ||
1644 | return -ENODEV; | ||
1645 | interface = &intf->cur_altsetting->desc; | ||
1646 | if (interface->bInterfaceNumber > 0) | ||
1647 | return -ENODEV; | ||
1648 | |||
1649 | /* create the device */ | ||
1650 | if (dev_size < sizeof *gspca_dev) | ||
1651 | dev_size = sizeof *gspca_dev; | ||
1652 | gspca_dev = kzalloc(dev_size, GFP_KERNEL); | ||
1653 | if (gspca_dev == NULL) { | ||
1654 | err("couldn't kzalloc gspca struct"); | ||
1655 | return -EIO; | ||
1656 | } | ||
1657 | gspca_dev->dev = dev; | ||
1658 | gspca_dev->iface = interface->bInterfaceNumber; | ||
1659 | gspca_dev->sd_desc = sd_desc; | ||
1660 | /* gspca_dev->users = 0; (done by kzalloc) */ | ||
1661 | gspca_dev->nbufread = 2; | ||
1662 | |||
1663 | /* configure the subdriver */ | ||
1664 | ret = gspca_dev->sd_desc->config(gspca_dev, id); | ||
1665 | if (ret < 0) | ||
1666 | goto out; | ||
1667 | ret = gspca_set_alt0(gspca_dev); | ||
1668 | if (ret < 0) | ||
1669 | goto out; | ||
1670 | gspca_set_default_mode(gspca_dev); | ||
1671 | |||
1672 | mutex_init(&gspca_dev->usb_lock); | ||
1673 | mutex_init(&gspca_dev->read_lock); | ||
1674 | mutex_init(&gspca_dev->queue_lock); | ||
1675 | init_waitqueue_head(&gspca_dev->wq); | ||
1676 | |||
1677 | /* init video stuff */ | ||
1678 | memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template); | ||
1679 | gspca_dev->vdev.dev = &dev->dev; | ||
1680 | ret = video_register_device(&gspca_dev->vdev, | ||
1681 | VFL_TYPE_GRABBER, | ||
1682 | video_nr); | ||
1683 | if (ret < 0) { | ||
1684 | err("video_register_device err %d", ret); | ||
1685 | goto out; | ||
1686 | } | ||
1687 | |||
1688 | gspca_dev->present = 1; | ||
1689 | usb_set_intfdata(intf, gspca_dev); | ||
1690 | PDEBUG(D_PROBE, "probe ok"); | ||
1691 | return 0; | ||
1692 | out: | ||
1693 | kfree(gspca_dev); | ||
1694 | return ret; | ||
1695 | } | ||
1696 | EXPORT_SYMBOL(gspca_dev_probe); | ||
1697 | |||
1698 | /* | ||
1699 | * USB disconnection | ||
1700 | * | ||
1701 | * This function must be called by the sub-driver | ||
1702 | * when the device disconnects, after the specific resources are freed. | ||
1703 | */ | ||
1704 | void gspca_disconnect(struct usb_interface *intf) | ||
1705 | { | ||
1706 | struct gspca_dev *gspca_dev = usb_get_intfdata(intf); | ||
1707 | |||
1708 | if (!gspca_dev) | ||
1709 | return; | ||
1710 | gspca_dev->present = 0; | ||
1711 | mutex_lock_interruptible(&gspca_dev->queue_lock); | ||
1712 | mutex_lock_interruptible(&gspca_dev->usb_lock); | ||
1713 | gspca_kill_transfer(gspca_dev); | ||
1714 | mutex_unlock(&gspca_dev->queue_lock); | ||
1715 | mutex_unlock(&gspca_dev->usb_lock); | ||
1716 | while (gspca_dev->users != 0) { /* wait until fully closed */ | ||
1717 | atomic_inc(&gspca_dev->nevent); | ||
1718 | wake_up_interruptible(&gspca_dev->wq); /* wake processes */ | ||
1719 | schedule(); | ||
1720 | } | ||
1721 | /* We don't want people trying to open up the device */ | ||
1722 | video_unregister_device(&gspca_dev->vdev); | ||
1723 | /* Free the memory */ | ||
1724 | kfree(gspca_dev); | ||
1725 | PDEBUG(D_PROBE, "disconnect complete"); | ||
1726 | } | ||
1727 | EXPORT_SYMBOL(gspca_disconnect); | ||
1728 | |||
1729 | /* -- module insert / remove -- */ | ||
1730 | static int __init gspca_init(void) | ||
1731 | { | ||
1732 | info("main v%s registered", version); | ||
1733 | return 0; | ||
1734 | } | ||
1735 | static void __exit gspca_exit(void) | ||
1736 | { | ||
1737 | info("main deregistered"); | ||
1738 | } | ||
1739 | |||
1740 | module_init(gspca_init); | ||
1741 | module_exit(gspca_exit); | ||
1742 | |||
1743 | module_param_named(debug, gspca_debug, int, 0644); | ||
1744 | MODULE_PARM_DESC(debug, | ||
1745 | "Debug (bit) 0x01:error 0x02:probe 0x04:config" | ||
1746 | " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"); | ||
1747 | |||
1748 | module_param(comp_fac, int, 0644); | ||
1749 | MODULE_PARM_DESC(comp_fac, | ||
1750 | "Buffer size ratio when compressed in percent"); | ||