blob: c84e0b352f4642db7aa9f59cbaf3136c6f682c5d [file] [log] [blame]
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001/*
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
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030038#undef CONFIG_VIDEO_V4L1_COMPAT
Jean-Francois Moined43fa322008-06-12 10:58:58 -030039
40/* global values */
41#define DEF_NURBS 2 /* default number of URBs (mmap) */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030042#define USR_NURBS 5 /* default number of URBs (userptr) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -030043
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030044MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
45MODULE_DESCRIPTION("GSPCA USB Camera Driver");
46MODULE_LICENSE("GPL");
47
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030048#define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 1, 0)
49static const char version[] = "2.1.0";
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030050
51static int video_nr = -1;
52
53static int comp_fac = 30; /* Buffer size ratio when compressed in % */
54
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030055#ifdef VIDEO_ADV_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030056int gspca_debug = D_ERR | D_PROBE;
57EXPORT_SYMBOL(gspca_debug);
58
59static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
60{
61 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
62 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
63 txt,
64 pixfmt & 0xff,
65 (pixfmt >> 8) & 0xff,
66 (pixfmt >> 16) & 0xff,
67 pixfmt >> 24,
68 w, h);
69 } else {
70 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
71 txt,
72 pixfmt,
73 w, h);
74 }
75}
76#else
77#define PDEBUG_MODE(txt, pixfmt, w, h)
78#endif
79
Jean-Francois Moined43fa322008-06-12 10:58:58 -030080/* specific memory types - !! should different from V4L2_MEMORY_xxx */
81#define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
82#define GSPCA_MEMORY_READ 7
83
Jean-Francois Moined43fa322008-06-12 10:58:58 -030084#define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
Jean-Francois Moined43fa322008-06-12 10:58:58 -030085
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030086/*
87 * VMA operations.
88 */
89static void gspca_vm_open(struct vm_area_struct *vma)
90{
91 struct gspca_frame *frame = vma->vm_private_data;
92
93 frame->vma_use_count++;
94 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
95}
96
97static void gspca_vm_close(struct vm_area_struct *vma)
98{
99 struct gspca_frame *frame = vma->vm_private_data;
100
101 if (--frame->vma_use_count <= 0)
102 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
103}
104
105static struct vm_operations_struct gspca_vm_ops = {
106 .open = gspca_vm_open,
107 .close = gspca_vm_close,
108};
109
110/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300111 * fill a video frame from an URB and resubmit
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300112 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300113static void fill_frame(struct gspca_dev *gspca_dev,
114 struct urb *urb)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300115{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300116 struct gspca_frame *frame;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300117 __u8 *data; /* address of data in the iso message */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300118 int i, j, len, st;
119 cam_pkt_op pkt_scan;
120
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300121 if (urb->status != 0) {
122 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
123 return; /* disconnection ? */
124 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300125 pkt_scan = gspca_dev->sd_desc->pkt_scan;
126 for (i = 0; i < urb->number_of_packets; i++) {
127
128 /* check the availability of the frame buffer */
129 j = gspca_dev->fr_i;
130 j = gspca_dev->fr_queue[j];
131 frame = &gspca_dev->frame[j];
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300132 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300133 != V4L2_BUF_FLAG_QUEUED) {
134 gspca_dev->last_packet_type = DISCARD_PACKET;
135 break;
136 }
137
138 /* check the packet status and length */
139 len = urb->iso_frame_desc[i].actual_length;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300140 if (len == 0)
141 continue;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300142 st = urb->iso_frame_desc[i].status;
143 if (st) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300144 PDEBUG(D_ERR,
145 "ISOC data error: [%d] len=%d, status=%d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300146 i, len, st);
147 gspca_dev->last_packet_type = DISCARD_PACKET;
148 continue;
149 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300150
151 /* let the packet be analyzed by the subdriver */
152 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
153 i, urb->iso_frame_desc[i].offset, len);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300154 data = (__u8 *) urb->transfer_buffer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300155 + urb->iso_frame_desc[i].offset;
156 pkt_scan(gspca_dev, frame, data, len);
157 }
158
159 /* resubmit the URB */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300160/*fixme: don't do that when userptr and too many URBs sent*/
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300161 urb->status = 0;
162 st = usb_submit_urb(urb, GFP_ATOMIC);
163 if (st < 0)
164 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
165}
166
167/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300168 * ISOC message interrupt from the USB device
169 *
170 * Analyse each packet and call the subdriver for copy
171 * to the frame buffer.
172 *
173 * There are 2 functions:
174 * - the first one (isoc_irq_mmap) is used when the application
175 * buffers are mapped. The frame detection and copy is done
176 * at interrupt level.
177 * - the second one (isoc_irq_user) is used when the application
178 * buffers are in user space (userptr). The frame detection
179 * and copy is done by the application.
180 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300181static void isoc_irq_mmap(struct urb *urb
182)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300183{
184 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
185
186 PDEBUG(D_PACK, "isoc irq mmap");
187 if (!gspca_dev->streaming)
188 return;
189 fill_frame(gspca_dev, urb);
190}
191
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300192static void isoc_irq_user(struct urb *urb
193)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300194{
195 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
196 int i;
197
198 PDEBUG(D_PACK, "isoc irq user");
199 if (!gspca_dev->streaming)
200 return;
201
202 i = gspca_dev->urb_in % gspca_dev->nurbs;
203 if (urb != gspca_dev->urb[i]) {
204 PDEBUG(D_ERR|D_PACK, "urb out of sequence");
205 return; /* should never occur */
206 }
207
208 gspca_dev->urb_in++;
209 atomic_inc(&gspca_dev->nevent); /* new event */
210 wake_up_interruptible(&gspca_dev->wq);
211/*fixme: submit a new URBs until urb_in == urb_out (% nurbs)*/
212}
213
214/*
215 * treat the isoc messages
216 *
217 * This routine is called by the application (case userptr).
218 */
219static void isoc_transfer(struct gspca_dev *gspca_dev)
220{
221 struct urb *urb;
222 int i;
223
224 for (;;) {
225 i = gspca_dev->urb_out;
226 PDEBUG(D_PACK, "isoc transf i:%d o:%d", gspca_dev->urb_in, i);
227 if (i == gspca_dev->urb_in) /* isoc message to read */
228 break; /* no (more) message */
229 atomic_dec(&gspca_dev->nevent);
230/*PDEBUG(D_PACK, "isoc_trf nevent: %d", atomic_read(&gspca_dev->nevent));*/
231 gspca_dev->urb_out = i + 1; /* message treated */
232 urb = gspca_dev->urb[i % gspca_dev->nurbs];
233 fill_frame(gspca_dev, urb);
234 }
235}
236
237/*
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300238 * add data to the current frame
239 *
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300240 * This function is called by the subdrivers at interrupt level
241 * or user level.
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300242 * To build a frame, these ones must add
243 * - one FIRST_PACKET
244 * - 0 or many INTER_PACKETs
245 * - one LAST_PACKET
246 * DISCARD_PACKET invalidates the whole frame.
247 * On LAST_PACKET, a new frame is returned.
248 */
249struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
250 int packet_type,
251 struct gspca_frame *frame,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300252 __u8 *data,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300253 int len)
254{
255 int i, j;
256
Hans de Goedee2997a72008-04-23 08:09:12 -0300257 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300258
259 /* when start of a new frame, if the current frame buffer
260 * is not queued, discard the whole frame */
261 if (packet_type == FIRST_PACKET) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300262 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
263 != V4L2_BUF_FLAG_QUEUED) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300264 gspca_dev->last_packet_type = DISCARD_PACKET;
265 return frame;
266 }
267 frame->data_end = frame->data;
268 jiffies_to_timeval(get_jiffies_64(),
269 &frame->v4l2_buf.timestamp);
270 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300271 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300272 return frame;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300273 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300274
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300275 /* append the packet to the frame buffer */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300276 if (len > 0) {
277 if (frame->data_end - frame->data + len
278 > frame->v4l2_buf.length) {
279 PDEBUG(D_ERR|D_PACK, "frame overflow %d > %d",
280 frame->data_end - frame->data + len,
281 frame->v4l2_buf.length);
282 packet_type = DISCARD_PACKET;
283 } else {
284 if (frame->v4l2_buf.memory != V4L2_MEMORY_USERPTR)
285 memcpy(frame->data_end, data, len);
286 else
287 copy_to_user(frame->data_end, data, len);
288 frame->data_end += len;
289 }
290 }
291 gspca_dev->last_packet_type = packet_type;
292
293 /* if last packet, wake the application and advance in the queue */
294 if (packet_type == LAST_PACKET) {
295 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
296 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
297 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
298 atomic_inc(&gspca_dev->nevent);
299 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300300 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
301 gspca_dev->fr_i = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300302 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
303 frame->v4l2_buf.bytesused,
304 gspca_dev->fr_q,
305 i,
306 gspca_dev->fr_o);
307 j = gspca_dev->fr_queue[i];
308 frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300309 }
310 return frame;
311}
312EXPORT_SYMBOL(gspca_frame_add);
313
314static int gspca_is_compressed(__u32 format)
315{
316 switch (format) {
317 case V4L2_PIX_FMT_MJPEG:
318 case V4L2_PIX_FMT_JPEG:
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300319 case V4L2_PIX_FMT_SPCA561:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300320 return 1;
321 }
322 return 0;
323}
324
325static void *rvmalloc(unsigned long size)
326{
327 void *mem;
328 unsigned long adr;
329
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300330/* size = PAGE_ALIGN(size); (already done) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300331 mem = vmalloc_32(size);
332 if (mem != 0) {
333 memset(mem, 0, size);
334 adr = (unsigned long) mem;
335 while ((long) size > 0) {
336 SetPageReserved(vmalloc_to_page((void *) adr));
337 adr += PAGE_SIZE;
338 size -= PAGE_SIZE;
339 }
340 }
341 return mem;
342}
343
344static void rvfree(void *mem, unsigned long size)
345{
346 unsigned long adr;
347
348 if (!mem)
349 return;
350 adr = (unsigned long) mem;
351 while ((long) size > 0) {
352 ClearPageReserved(vmalloc_to_page((void *) adr));
353 adr += PAGE_SIZE;
354 size -= PAGE_SIZE;
355 }
356 vfree(mem);
357}
358
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300359static __u32 get_v4l2_depth(__u32 pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300360{
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300361 switch (pixfmt) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300362/* case V4L2_PIX_FMT_BGR32:
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300363 case V4L2_PIX_FMT_RGB32:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300364 return 32; */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300365 case V4L2_PIX_FMT_RGB24: /* 'RGB3' */
366 case V4L2_PIX_FMT_BGR24:
367 return 24;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300368/* case V4L2_PIX_FMT_RGB565: * 'RGBP' */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300369 case V4L2_PIX_FMT_YUYV: /* 'YUYV' packed 4.2.2 */
370 case V4L2_PIX_FMT_YYUV: /* 'YYUV' */
371 return 16;
372 case V4L2_PIX_FMT_YUV420: /* 'YU12' planar 4.2.0 */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300373 case V4L2_PIX_FMT_SPCA501: /* 'S501' YUYV per line */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300374 return 12;
375 case V4L2_PIX_FMT_MJPEG:
376 case V4L2_PIX_FMT_JPEG:
377 case V4L2_PIX_FMT_SBGGR8: /* 'BA81' Bayer */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300378 case V4L2_PIX_FMT_SN9C10X: /* 'S910' SN9C10x compression */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300379 case V4L2_PIX_FMT_SPCA561: /* 'S561' compressed BGGR bayer */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300380 return 8;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300381 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300382 PDEBUG(D_ERR|D_CONF, "Unknown pixel format %c%c%c%c",
383 pixfmt & 0xff,
384 (pixfmt >> 8) & 0xff,
385 (pixfmt >> 16) & 0xff,
386 pixfmt >> 24);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300387 return 24;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300388}
389
390static int gspca_get_buff_size(struct gspca_dev *gspca_dev)
391{
392 unsigned int size;
393
394 size = gspca_dev->width * gspca_dev->height
395 * get_v4l2_depth(gspca_dev->pixfmt) / 8;
396 if (!size)
397 return -ENOMEM;
398 return size;
399}
400
401static int frame_alloc(struct gspca_dev *gspca_dev,
402 unsigned int count)
403{
404 struct gspca_frame *frame;
405 unsigned int frsz;
406 int i;
407
408 frsz = gspca_get_buff_size(gspca_dev);
409 if (frsz < 0)
410 return frsz;
411 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300412 if (count > GSPCA_MAX_FRAMES)
413 count = GSPCA_MAX_FRAMES;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300414 /* if compressed (JPEG), reduce the buffer size */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300415 if (gspca_is_compressed(gspca_dev->pixfmt))
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300416 frsz = (frsz * comp_fac) / 100 + 600; /* (+ JPEG header sz) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300417 frsz = PAGE_ALIGN(frsz);
418 PDEBUG(D_STREAM, "new fr_sz: %d", frsz);
419 gspca_dev->frsz = frsz;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300420 if (gspca_dev->memory == V4L2_MEMORY_MMAP) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300421 gspca_dev->frbuf = rvmalloc(frsz * count);
422 if (!gspca_dev->frbuf) {
423 err("frame alloc failed");
424 return -ENOMEM;
425 }
426 }
427 gspca_dev->nframes = count;
428 for (i = 0; i < count; i++) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300429 frame = &gspca_dev->frame[i];
430 frame->v4l2_buf.index = i;
431 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
432 frame->v4l2_buf.flags = 0;
433 frame->v4l2_buf.field = V4L2_FIELD_NONE;
434 frame->v4l2_buf.length = frsz;
435 frame->v4l2_buf.memory = gspca_dev->memory;
436 frame->v4l2_buf.sequence = 0;
437 if (gspca_dev->memory == V4L2_MEMORY_MMAP) {
438 frame->data = frame->data_end =
439 gspca_dev->frbuf + i * frsz;
440 frame->v4l2_buf.m.offset = i * frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300441 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300442 }
443 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
444 gspca_dev->last_packet_type = DISCARD_PACKET;
445 gspca_dev->sequence = 0;
446 atomic_set(&gspca_dev->nevent, 0);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300447 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300448}
449
450static void frame_free(struct gspca_dev *gspca_dev)
451{
452 int i;
453
454 PDEBUG(D_STREAM, "frame free");
455 if (gspca_dev->frbuf != 0) {
456 rvfree(gspca_dev->frbuf,
457 gspca_dev->nframes * gspca_dev->frsz);
458 gspca_dev->frbuf = NULL;
459 for (i = 0; i < gspca_dev->nframes; i++)
460 gspca_dev->frame[i].data = NULL;
461 }
462 gspca_dev->nframes = 0;
463}
464
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300465static void destroy_urbs(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300466{
467 struct urb *urb;
468 unsigned int i;
469
470 PDEBUG(D_STREAM, "kill transfer");
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300471 for (i = 0; i < MAX_NURBS; ++i) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300472 urb = gspca_dev->urb[i];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300473 if (urb == NULL)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300474 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300475
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300476 gspca_dev->urb[i] = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300477 usb_kill_urb(urb);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300478 if (urb->transfer_buffer != 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300479 usb_buffer_free(gspca_dev->dev,
480 urb->transfer_buffer_length,
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300481 urb->transfer_buffer,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300482 urb->transfer_dma);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300483 usb_free_urb(urb);
484 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300485}
486
487/*
488 * search an input isochronous endpoint in an alternate setting
489 */
490static struct usb_host_endpoint *alt_isoc(struct usb_host_interface *alt,
491 __u8 epaddr)
492{
493 struct usb_host_endpoint *ep;
494 int i, attr;
495
496 epaddr |= USB_DIR_IN;
497 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
498 ep = &alt->endpoint[i];
499 if (ep->desc.bEndpointAddress == epaddr) {
500 attr = ep->desc.bmAttributes
501 & USB_ENDPOINT_XFERTYPE_MASK;
502 if (attr == USB_ENDPOINT_XFER_ISOC)
503 return ep;
504 break;
505 }
506 }
507 return NULL;
508}
509
510/*
511 * search an input isochronous endpoint
512 *
513 * The endpoint is defined by the subdriver.
514 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
515 * This routine may be called many times when the bandwidth is too small
516 * (the bandwidth is checked on urb submit).
517 */
518struct usb_host_endpoint *get_isoc_ep(struct gspca_dev *gspca_dev)
519{
520 struct usb_interface *intf;
521 struct usb_host_endpoint *ep;
522 int i, ret;
523
524 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300525 ep = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300526 i = gspca_dev->alt; /* previous alt setting */
527 while (--i > 0) { /* alt 0 is unusable */
528 ep = alt_isoc(&intf->altsetting[i], gspca_dev->cam.epaddr);
529 if (ep)
530 break;
531 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300532 if (ep == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300533 err("no ISOC endpoint found");
534 return NULL;
535 }
536 PDEBUG(D_STREAM, "use ISOC alt %d ep 0x%02x",
537 i, ep->desc.bEndpointAddress);
538 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
539 if (ret < 0) {
540 err("set interface err %d", ret);
541 return NULL;
542 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300543 gspca_dev->alt = i; /* memorize the current alt setting */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300544 return ep;
545}
546
547/*
548 * create the isochronous URBs
549 */
550static int create_urbs(struct gspca_dev *gspca_dev,
551 struct usb_host_endpoint *ep)
552{
553 struct urb *urb;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300554 int n, nurbs, i, psize, npkt, bsize;
555 usb_complete_t usb_complete;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300556
557 /* calculate the packet size and the number of packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300558 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300559
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300560 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
561 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
562 npkt = ISO_MAX_SIZE / psize;
563 if (npkt > ISO_MAX_PKT)
564 npkt = ISO_MAX_PKT;
565 bsize = psize * npkt;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300566 PDEBUG(D_STREAM,
567 "isoc %d pkts size %d (bsize:%d)", npkt, psize, bsize);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300568/*fixme:change for userptr*/
569/*fixme:don't submit all URBs when userptr*/
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300570 if (gspca_dev->memory == V4L2_MEMORY_MMAP) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300571 usb_complete = isoc_irq_mmap;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300572 nurbs = DEF_NURBS;
573 } else {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300574 usb_complete = isoc_irq_user;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300575 nurbs = USR_NURBS;
576 }
577 gspca_dev->nurbs = nurbs;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300578 for (n = 0; n < nurbs; n++) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300579 urb = usb_alloc_urb(npkt, GFP_KERNEL);
580 if (!urb) {
581 err("usb_alloc_urb failed");
582 return -ENOMEM;
583 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300584 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300585 bsize,
586 GFP_KERNEL,
587 &urb->transfer_dma);
588
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300589 if (urb->transfer_buffer == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300590 usb_free_urb(urb);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300591 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300592 err("usb_buffer_urb failed");
593 return -ENOMEM;
594 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300595 gspca_dev->urb[n] = urb;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300596 urb->dev = gspca_dev->dev;
597 urb->context = gspca_dev;
598 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
599 ep->desc.bEndpointAddress);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300600 urb->transfer_flags = URB_ISO_ASAP
601 | URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300602 urb->interval = ep->desc.bInterval;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300603 urb->complete = usb_complete;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300604 urb->number_of_packets = npkt;
605 urb->transfer_buffer_length = bsize;
606 for (i = 0; i < npkt; i++) {
607 urb->iso_frame_desc[i].length = psize;
608 urb->iso_frame_desc[i].offset = psize * i;
609 }
610 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300611 gspca_dev->urb_in = gspca_dev->urb_out = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300612 return 0;
613}
614
615/*
616 * start the USB transfer
617 */
618static int gspca_init_transfer(struct gspca_dev *gspca_dev)
619{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300620 struct usb_host_endpoint *ep;
621 int n, ret;
622
Hans de Goedee2997a72008-04-23 08:09:12 -0300623 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
624 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300625
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300626 /* set the higher alternate setting and
627 * loop until urb submit succeeds */
628 gspca_dev->alt = gspca_dev->nbalt;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300629 for (;;) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300630 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300631 ep = get_isoc_ep(gspca_dev);
632 if (ep == NULL) {
633 ret = -EIO;
634 goto out;
635 }
636 ret = create_urbs(gspca_dev, ep);
637 if (ret < 0)
638 goto out;
639
640 /* start the cam */
641 gspca_dev->sd_desc->start(gspca_dev);
642 gspca_dev->streaming = 1;
643 atomic_set(&gspca_dev->nevent, 0);
644
645 /* submit the URBs */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300646 for (n = 0; n < gspca_dev->nurbs; n++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300647 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300648 if (ret < 0) {
649 PDEBUG(D_ERR|D_STREAM,
650 "usb_submit_urb [%d] err %d", n, ret);
Hans de Goedee2997a72008-04-23 08:09:12 -0300651 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300652 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300653 if (ret == -ENOSPC)
654 break; /* try the previous alt */
655 goto out;
656 }
657 }
658 if (ret >= 0)
659 break;
660 }
661out:
662 mutex_unlock(&gspca_dev->usb_lock);
663 return ret;
664}
665
666static int gspca_set_alt0(struct gspca_dev *gspca_dev)
667{
668 int ret;
669
670 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
671 if (ret < 0)
672 PDEBUG(D_ERR|D_STREAM, "set interface 0 err %d", ret);
673 return ret;
674}
675
Hans de Goedee2997a72008-04-23 08:09:12 -0300676/* Note both the queue and the usb lock should be hold when calling this */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300677static void gspca_stream_off(struct gspca_dev *gspca_dev)
678{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300679 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300680 atomic_set(&gspca_dev->nevent, 0);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300681 if (gspca_dev->present) {
682 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300683 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300684 gspca_set_alt0(gspca_dev);
685 gspca_dev->sd_desc->stop0(gspca_dev);
686 PDEBUG(D_STREAM, "stream off OK");
687 } else {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300688 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300689 atomic_inc(&gspca_dev->nevent);
690 wake_up_interruptible(&gspca_dev->wq);
691 PDEBUG(D_ERR|D_STREAM, "stream off no device ??");
692 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300693}
694
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300695static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300696{
697 int i;
698
699 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
700 gspca_dev->curr_mode = i;
701 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
702 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
703 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixfmt;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300704}
705
706static int wxh_to_mode(struct gspca_dev *gspca_dev,
707 int width, int height)
708{
709 int i;
710
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300711 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
712 if (width >= gspca_dev->cam.cam_mode[i].width
713 && height >= gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300714 break;
715 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300716 return i;
717}
718
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300719/*
720 * search a mode with the right pixel format
721 */
722static int gspca_get_mode(struct gspca_dev *gspca_dev,
723 int mode,
724 int pixfmt)
725{
726 int modeU, modeD;
727
728 modeU = modeD = mode;
729 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
730 if (--modeD >= 0) {
731 if (gspca_dev->cam.cam_mode[modeD].pixfmt == pixfmt)
732 return modeD;
733 }
734 if (++modeU < gspca_dev->cam.nmodes) {
735 if (gspca_dev->cam.cam_mode[modeU].pixfmt == pixfmt)
736 return modeU;
737 }
738 }
739 return -EINVAL;
740}
741
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300742static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300743 struct v4l2_fmtdesc *fmtdesc)
744{
745 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300746 int i, j, index;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300747 __u32 fmt_tb[8];
748
749 PDEBUG(D_CONF, "enum fmt cap");
750
751 /* give an index to each format */
752 index = 0;
753 j = 0;
754 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
755 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixfmt;
756 j = 0;
757 for (;;) {
758 if (fmt_tb[j] == fmt_tb[index])
759 break;
760 j++;
761 }
762 if (j == index) {
763 if (fmtdesc->index == index)
764 break; /* new format */
765 index++;
766 if (index >= sizeof fmt_tb / sizeof fmt_tb[0])
767 return -EINVAL;
768 }
769 }
770 if (i < 0)
771 return -EINVAL; /* no more format */
772
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300773 fmtdesc->pixelformat = fmt_tb[index];
774 if (gspca_is_compressed(fmt_tb[index]))
775 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300776 fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300777 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
778 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
779 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
780 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
781 fmtdesc->description[4] = '\0';
782 return 0;
783}
784
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300785static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300786 struct v4l2_format *fmt)
787{
788 struct gspca_dev *gspca_dev = priv;
789
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300790 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
791 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300792 fmt->fmt.pix.width = gspca_dev->width;
793 fmt->fmt.pix.height = gspca_dev->height;
794 fmt->fmt.pix.pixelformat = gspca_dev->pixfmt;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300795#ifdef VIDEO_ADV_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300796 if (gspca_debug & D_CONF) {
797 PDEBUG_MODE("get fmt cap",
798 fmt->fmt.pix.pixelformat,
799 fmt->fmt.pix.width,
800 fmt->fmt.pix.height);
801 }
802#endif
803 fmt->fmt.pix.field = V4L2_FIELD_NONE;
804 fmt->fmt.pix.bytesperline = get_v4l2_depth(fmt->fmt.pix.pixelformat)
805 * fmt->fmt.pix.width / 8;
806 fmt->fmt.pix.sizeimage = fmt->fmt.pix.bytesperline
807 * fmt->fmt.pix.height;
808/* (should be in the subdriver) */
809 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
810 fmt->fmt.pix.priv = 0;
811 return 0;
812}
813
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300814static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300815 struct v4l2_format *fmt)
816{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300817 int w, h, mode, mode2, frsz;
818
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300819 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
820 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300821 w = fmt->fmt.pix.width;
822 h = fmt->fmt.pix.height;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300823
824 /* (luvcview problem) */
825 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
826 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300827#ifdef VIDEO_ADV_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300828 if (gspca_debug & D_CONF)
829 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
830#endif
831 /* search the closest mode for width and height */
832 mode = wxh_to_mode(gspca_dev, w, h);
833
834 /* OK if right palette */
835 if (gspca_dev->cam.cam_mode[mode].pixfmt != fmt->fmt.pix.pixelformat) {
836
837 /* else, search the closest mode with the same pixel format */
838 mode2 = gspca_get_mode(gspca_dev, mode,
839 fmt->fmt.pix.pixelformat);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300840 if (mode2 >= 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300841 mode = mode2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300842 } else {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300843
844 /* no chance, return this mode */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300845 fmt->fmt.pix.pixelformat =
846 gspca_dev->cam.cam_mode[mode].pixfmt;
847#ifdef VIDEO_ADV_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300848 if (gspca_debug & D_CONF) {
849 PDEBUG_MODE("new format",
850 fmt->fmt.pix.pixelformat,
851 gspca_dev->cam.cam_mode[mode].width,
852 gspca_dev->cam.cam_mode[mode].height);
853 }
854#endif
855 }
856 }
857 fmt->fmt.pix.width = gspca_dev->cam.cam_mode[mode].width;
858 fmt->fmt.pix.height = gspca_dev->cam.cam_mode[mode].height;
859 fmt->fmt.pix.bytesperline = get_v4l2_depth(fmt->fmt.pix.pixelformat)
860 * fmt->fmt.pix.width / 8;
861 frsz = fmt->fmt.pix.bytesperline * fmt->fmt.pix.height;
862 if (gspca_is_compressed(fmt->fmt.pix.pixelformat))
863 frsz = (frsz * comp_fac) / 100;
864 fmt->fmt.pix.sizeimage = frsz;
865 return mode; /* used when s_fmt */
866}
867
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300868static int vidioc_try_fmt_vid_cap(struct file *file,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300869 void *priv,
870 struct v4l2_format *fmt)
871{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300872 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300873 int ret;
874
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300875 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300876 if (ret < 0)
877 return ret;
878 return 0;
879}
880
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300881static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300882 struct v4l2_format *fmt)
883{
884 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300885 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300886
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300887#ifdef CONFIG_VIDEO_V4L1_COMPAT
888 /* if v4l1 got JPEG */
889 if (fmt->fmt.pix.pixelformat == 0
890 && gspca_dev->streaming) {
891 fmt->fmt.pix.width = gspca_dev->width;
892 fmt->fmt.pix.height = gspca_dev->height;
893 fmt->fmt.pix.pixelformat = gspca_dev->pixfmt;
894 return 0;
895 }
896#endif
897#ifdef VIDEO_ADV_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300898 if (gspca_debug & D_CONF) {
899 PDEBUG_MODE("set fmt cap",
900 fmt->fmt.pix.pixelformat,
901 fmt->fmt.pix.width, fmt->fmt.pix.height);
902 }
903#endif
Hans de Goedee2997a72008-04-23 08:09:12 -0300904 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
905 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300906
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300907 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300908 if (ret < 0)
909 goto out;
910
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300911 if (gspca_dev->nframes != 0
912 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
913 ret = -EINVAL;
914 goto out;
915 }
916
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300917 if (ret == gspca_dev->curr_mode) {
918 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300919 goto out; /* same mode */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300920 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300921
922 if (gspca_dev->streaming) {
923 ret = -EBUSY;
924 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -0300925 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300926 gspca_dev->width = fmt->fmt.pix.width;
927 gspca_dev->height = fmt->fmt.pix.height;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300928 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
929 gspca_dev->curr_mode = ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300930
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300931 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300932out:
933 mutex_unlock(&gspca_dev->queue_lock);
934 return ret;
935}
936
937static int dev_open(struct inode *inode, struct file *file)
938{
939 struct gspca_dev *gspca_dev;
940 int ret;
941
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300942 PDEBUG(D_STREAM, "%s open", current->comm);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300943 gspca_dev = (struct gspca_dev *) video_devdata(file);
Hans de Goedee2997a72008-04-23 08:09:12 -0300944 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
945 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300946 if (!gspca_dev->present) {
947 ret = -ENODEV;
948 goto out;
949 }
950
951 /* if not done yet, initialize the sensor */
952 if (gspca_dev->users == 0) {
Hans de Goedee2997a72008-04-23 08:09:12 -0300953 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
954 ret = -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300955 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -0300956 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300957 ret = gspca_dev->sd_desc->open(gspca_dev);
958 mutex_unlock(&gspca_dev->usb_lock);
959 if (ret != 0) {
960 PDEBUG(D_ERR|D_CONF, "init device failed %d", ret);
961 goto out;
962 }
Hans de Goedee2997a72008-04-23 08:09:12 -0300963 } else if (gspca_dev->users > 4) { /* (arbitrary value) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300964 ret = -EBUSY;
965 goto out;
966 }
967 gspca_dev->users++;
968 file->private_data = gspca_dev;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300969#ifdef VIDEO_ADV_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300970 /* activate the v4l2 debug */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300971 if (gspca_debug & D_V4L2)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300972 gspca_dev->vdev.debug |= 3;
973 else
974 gspca_dev->vdev.debug &= ~3;
975#endif
976out:
977 mutex_unlock(&gspca_dev->queue_lock);
978 if (ret != 0)
979 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
980 else
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300981 PDEBUG(D_STREAM, "open done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300982 return ret;
983}
984
985static int dev_close(struct inode *inode, struct file *file)
986{
987 struct gspca_dev *gspca_dev = file->private_data;
988
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300989 PDEBUG(D_STREAM, "%s close", current->comm);
Hans de Goedee2997a72008-04-23 08:09:12 -0300990 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
991 return -ERESTARTSYS;
992 gspca_dev->users--;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300993
994 /* if the file did capture, free the streaming resources */
995 if (gspca_dev->capt_file == file) {
996 mutex_lock(&gspca_dev->usb_lock);
997 if (gspca_dev->streaming)
998 gspca_stream_off(gspca_dev);
999 gspca_dev->sd_desc->close(gspca_dev);
1000 mutex_unlock(&gspca_dev->usb_lock);
1001 frame_free(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001002 gspca_dev->capt_file = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001003 gspca_dev->memory = GSPCA_MEMORY_NO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001004 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001005 file->private_data = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001006 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001007 PDEBUG(D_STREAM, "close done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001008 return 0;
1009}
1010
1011static int vidioc_querycap(struct file *file, void *priv,
1012 struct v4l2_capability *cap)
1013{
1014 struct gspca_dev *gspca_dev = priv;
1015
1016 PDEBUG(D_CONF, "querycap");
1017 memset(cap, 0, sizeof *cap);
1018 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
1019 strncpy(cap->card, gspca_dev->cam.dev_name, sizeof cap->card);
1020 strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name,
1021 sizeof cap->bus_info);
1022 cap->version = DRIVER_VERSION_NUMBER;
1023 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1024 | V4L2_CAP_STREAMING
1025 | V4L2_CAP_READWRITE;
1026 return 0;
1027}
1028
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001029/* the use of V4L2_CTRL_FLAG_NEXT_CTRL asks for the controls to be sorted */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001030static int vidioc_queryctrl(struct file *file, void *priv,
1031 struct v4l2_queryctrl *q_ctrl)
1032{
1033 struct gspca_dev *gspca_dev = priv;
1034 int i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001035 u32 id;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001036
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001037 id = q_ctrl->id;
1038 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1039 id &= V4L2_CTRL_ID_MASK;
1040 id++;
1041 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1042 if (id >= gspca_dev->sd_desc->ctrls[i].qctrl.id) {
1043 memcpy(q_ctrl,
1044 &gspca_dev->sd_desc->ctrls[i].qctrl,
1045 sizeof *q_ctrl);
1046 return 0;
1047 }
1048 }
1049 return -EINVAL;
1050 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001051 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001052 if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001053 memcpy(q_ctrl,
1054 &gspca_dev->sd_desc->ctrls[i].qctrl,
1055 sizeof *q_ctrl);
1056 return 0;
1057 }
1058 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001059 if (id >= V4L2_CID_BASE
1060 && id <= V4L2_CID_LASTP1) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001061 q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
1062 return 0;
1063 }
1064 return -EINVAL;
1065}
1066
1067static int vidioc_s_ctrl(struct file *file, void *priv,
1068 struct v4l2_control *ctrl)
1069{
1070 struct gspca_dev *gspca_dev = priv;
1071 struct ctrl *ctrls;
1072 int i, ret;
1073
1074 PDEBUG(D_CONF, "set ctrl");
1075 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1076 i < gspca_dev->sd_desc->nctrls;
1077 i++, ctrls++) {
1078 if (ctrl->id != ctrls->qctrl.id)
1079 continue;
1080 if (ctrl->value < ctrls->qctrl.minimum
1081 && ctrl->value > ctrls->qctrl.maximum)
1082 return -ERANGE;
1083 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
Hans de Goedee2997a72008-04-23 08:09:12 -03001084 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1085 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001086 ret = ctrls->set(gspca_dev, ctrl->value);
1087 mutex_unlock(&gspca_dev->usb_lock);
1088 return ret;
1089 }
1090 return -EINVAL;
1091}
1092
1093static int vidioc_g_ctrl(struct file *file, void *priv,
1094 struct v4l2_control *ctrl)
1095{
1096 struct gspca_dev *gspca_dev = priv;
1097
1098 struct ctrl *ctrls;
1099 int i, ret;
1100
1101 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1102 i < gspca_dev->sd_desc->nctrls;
1103 i++, ctrls++) {
1104 if (ctrl->id != ctrls->qctrl.id)
1105 continue;
Hans de Goedee2997a72008-04-23 08:09:12 -03001106 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1107 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001108 ret = ctrls->get(gspca_dev, &ctrl->value);
1109 mutex_unlock(&gspca_dev->usb_lock);
1110 return ret;
1111 }
1112 return -EINVAL;
1113}
1114
1115static int vidioc_querymenu(struct file *file, void *priv,
1116 struct v4l2_querymenu *qmenu)
1117{
1118 struct gspca_dev *gspca_dev = priv;
1119
1120 if (!gspca_dev->sd_desc->querymenu)
1121 return -EINVAL;
1122 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1123}
1124
1125static int vidioc_enum_input(struct file *file, void *priv,
1126 struct v4l2_input *input)
1127{
1128 struct gspca_dev *gspca_dev = priv;
1129
1130 if (input->index != 0)
1131 return -EINVAL;
1132 memset(input, 0, sizeof *input);
1133 input->type = V4L2_INPUT_TYPE_CAMERA;
1134 strncpy(input->name, gspca_dev->sd_desc->name,
1135 sizeof input->name);
1136 return 0;
1137}
1138
1139static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1140{
1141 *i = 0;
1142 return 0;
1143}
1144
1145static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1146{
1147 if (i > 0)
1148 return -EINVAL;
1149 return (0);
1150}
1151
1152static int vidioc_reqbufs(struct file *file, void *priv,
1153 struct v4l2_requestbuffers *rb)
1154{
1155 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001156 int i, ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001157
1158 PDEBUG(D_STREAM, "reqbufs %d", rb->count);
1159 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1160 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001161 switch (rb->memory) {
1162 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001163 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001164 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001165 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001166 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001167 }
Hans de Goedee2997a72008-04-23 08:09:12 -03001168 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1169 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001170
1171 for (i = 0; i < gspca_dev->nframes; i++) {
1172 if (gspca_dev->frame[i].vma_use_count) {
1173 ret = -EBUSY;
1174 goto out;
1175 }
1176 }
1177
1178 /* only one file may do capture */
1179 if ((gspca_dev->capt_file != 0 && gspca_dev->capt_file != file)
1180 || gspca_dev->streaming) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001181 ret = -EBUSY;
1182 goto out;
1183 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001184
1185 if (rb->count == 0) { /* unrequest? */
1186 frame_free(gspca_dev);
1187 gspca_dev->capt_file = 0;
1188 } else {
1189 gspca_dev->memory = rb->memory;
1190 ret = frame_alloc(gspca_dev, rb->count);
1191 if (ret == 0) {
1192 rb->count = gspca_dev->nframes;
1193 gspca_dev->capt_file = file;
1194 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001195 }
1196out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001197 mutex_unlock(&gspca_dev->queue_lock);
1198 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1199 return ret;
1200}
1201
1202static int vidioc_querybuf(struct file *file, void *priv,
1203 struct v4l2_buffer *v4l2_buf)
1204{
1205 struct gspca_dev *gspca_dev = priv;
1206 struct gspca_frame *frame;
1207
1208 PDEBUG(D_STREAM, "querybuf");
1209 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1210 || v4l2_buf->index < 0
1211 || v4l2_buf->index >= gspca_dev->nframes)
1212 return -EINVAL;
1213
1214 frame = &gspca_dev->frame[v4l2_buf->index];
1215 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1216 return 0;
1217}
1218
1219static int vidioc_streamon(struct file *file, void *priv,
1220 enum v4l2_buf_type buf_type)
1221{
1222 struct gspca_dev *gspca_dev = priv;
1223 int ret;
1224
1225 PDEBUG(D_STREAM, "stream on");
1226 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1227 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001228 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1229 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001230 if (!gspca_dev->present) {
1231 ret = -ENODEV;
1232 goto out;
1233 }
1234 if (gspca_dev->nframes == 0) {
1235 ret = -EINVAL;
1236 goto out;
1237 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001238 if (gspca_dev->capt_file != file) {
1239 ret = -EINVAL;
1240 goto out;
1241 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001242 if (!gspca_dev->streaming) {
1243 ret = gspca_init_transfer(gspca_dev);
1244 if (ret < 0)
1245 goto out;
1246 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001247#ifdef VIDEO_ADV_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001248 if (gspca_debug & D_STREAM) {
1249 PDEBUG_MODE("stream on OK",
1250 gspca_dev->pixfmt,
1251 gspca_dev->width,
1252 gspca_dev->height);
1253 }
1254#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001255 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001256out:
1257 mutex_unlock(&gspca_dev->queue_lock);
1258 return ret;
1259}
1260
1261static int vidioc_streamoff(struct file *file, void *priv,
1262 enum v4l2_buf_type buf_type)
1263{
1264 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001265 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001266
1267 PDEBUG(D_STREAM, "stream off");
1268 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1269 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001270 if (!gspca_dev->streaming)
1271 return 0;
1272 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1273 return -ERESTARTSYS;
1274 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1275 ret = -ERESTARTSYS;
1276 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001277 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001278 if (gspca_dev->capt_file != file) {
1279 ret = -EINVAL;
1280 goto out2;
1281 }
1282 gspca_stream_off(gspca_dev);
1283 ret = 0;
1284out2:
1285 mutex_unlock(&gspca_dev->usb_lock);
1286out:
1287 mutex_unlock(&gspca_dev->queue_lock);
1288 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001289}
1290
1291static int vidioc_g_jpegcomp(struct file *file, void *priv,
1292 struct v4l2_jpegcompression *jpegcomp)
1293{
1294 struct gspca_dev *gspca_dev = priv;
1295 int ret;
1296
1297 if (!gspca_dev->sd_desc->get_jcomp)
1298 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001299 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1300 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001301 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1302 mutex_unlock(&gspca_dev->usb_lock);
1303 return ret;
1304}
1305
1306static int vidioc_s_jpegcomp(struct file *file, void *priv,
1307 struct v4l2_jpegcompression *jpegcomp)
1308{
1309 struct gspca_dev *gspca_dev = priv;
1310 int ret;
1311
Hans de Goedee2997a72008-04-23 08:09:12 -03001312 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1313 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001314 if (!gspca_dev->sd_desc->set_jcomp)
1315 return -EINVAL;
1316 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1317 mutex_unlock(&gspca_dev->usb_lock);
1318 return ret;
1319}
1320
1321static int vidioc_g_parm(struct file *filp, void *priv,
1322 struct v4l2_streamparm *parm)
1323{
1324 struct gspca_dev *gspca_dev = priv;
1325
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001326 memset(parm, 0, sizeof *parm);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001327 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1328 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1329 return 0;
1330}
1331
1332static int vidioc_s_parm(struct file *filp, void *priv,
1333 struct v4l2_streamparm *parm)
1334{
1335 struct gspca_dev *gspca_dev = priv;
1336 int n;
1337
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001338 n = parm->parm.capture.readbuffers;
1339 if (n == 0 || n > GSPCA_MAX_FRAMES)
1340 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1341 else
1342 gspca_dev->nbufread = n;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001343 return 0;
1344}
1345
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001346static int vidioc_s_std(struct file *filp, void *priv,
1347 v4l2_std_id *parm)
1348{
1349 return 0;
1350}
1351
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001352#ifdef CONFIG_VIDEO_V4L1_COMPAT
1353static int vidiocgmbuf(struct file *file, void *priv,
1354 struct video_mbuf *mbuf)
1355{
1356 struct gspca_dev *gspca_dev = file->private_data;
1357 int i;
1358
1359 PDEBUG(D_STREAM, "cgmbuf");
1360 if (gspca_dev->nframes == 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001361 int ret;
1362
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001363 {
1364 struct v4l2_format fmt;
1365
1366 memset(&fmt, 0, sizeof fmt);
1367 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1368 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1369 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1370 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1371 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001372 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001373 if (ret != 0)
1374 return ret;
1375 }
1376 {
1377 struct v4l2_requestbuffers rb;
1378
1379 memset(&rb, 0, sizeof rb);
1380 rb.count = 4;
1381 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1382 rb.memory = V4L2_MEMORY_MMAP;
1383 ret = vidioc_reqbufs(file, priv, &rb);
1384 if (ret != 0)
1385 return ret;
1386 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001387 }
1388 mbuf->frames = gspca_dev->nframes;
1389 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1390 for (i = 0; i < mbuf->frames; i++)
1391 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1392 return 0;
1393}
1394#endif
1395
1396static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1397{
1398 struct gspca_dev *gspca_dev = file->private_data;
1399 struct gspca_frame *frame = 0;
1400 struct page *page;
1401 unsigned long addr, start, size;
1402 int i, ret;
1403#ifdef CONFIG_VIDEO_V4L1_COMPAT
1404 int compat = 0;
1405#endif
1406
1407 start = vma->vm_start;
1408 size = vma->vm_end - vma->vm_start;
1409 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1410
Hans de Goedee2997a72008-04-23 08:09:12 -03001411 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1412 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001413 if (!gspca_dev->present) {
1414 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001415 goto out;
1416 }
1417 if (gspca_dev->capt_file != file) {
1418 ret = -EINVAL;
1419 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001420 }
1421
1422 for (i = 0; i < gspca_dev->nframes; ++i) {
1423 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1424 PDEBUG(D_STREAM, "mmap bad memory type");
1425 break;
1426 }
1427 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1428 == vma->vm_pgoff) {
1429 frame = &gspca_dev->frame[i];
1430 break;
1431 }
1432 }
1433 if (frame == 0) {
1434 PDEBUG(D_STREAM, "mmap no frame buffer found");
1435 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001436 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001437 }
1438#ifdef CONFIG_VIDEO_V4L1_COMPAT
1439 if (i == 0 && size == frame->v4l2_buf.length * gspca_dev->nframes)
1440 compat = 1;
1441 else
1442#endif
1443 if (size != frame->v4l2_buf.length) {
1444 PDEBUG(D_STREAM, "mmap bad size");
1445 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001446 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001447 }
1448
1449 /*
1450 * - VM_IO marks the area as being a mmaped region for I/O to a
1451 * device. It also prevents the region from being core dumped.
1452 */
1453 vma->vm_flags |= VM_IO;
1454
1455 addr = (unsigned long) frame->data;
1456 while (size > 0) {
1457 page = vmalloc_to_page((void *) addr);
1458 ret = vm_insert_page(vma, start, page);
1459 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001460 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001461 start += PAGE_SIZE;
1462 addr += PAGE_SIZE;
1463 size -= PAGE_SIZE;
1464 }
1465
1466 vma->vm_ops = &gspca_vm_ops;
1467 vma->vm_private_data = frame;
1468 gspca_vm_open(vma);
1469#ifdef CONFIG_VIDEO_V4L1_COMPAT
1470 if (compat) {
1471/*fixme: ugly*/
1472 for (i = 1; i < gspca_dev->nframes; ++i)
1473 gspca_dev->frame[i].v4l2_buf.flags |=
1474 V4L2_BUF_FLAG_MAPPED;
1475 }
1476#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001477 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001478out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001479 mutex_unlock(&gspca_dev->queue_lock);
1480 return ret;
1481}
1482
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001483/*
1484 * wait for a video frame
1485 *
1486 * If a frame is ready, its index is returned.
1487 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001488static int frame_wait(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001489 int nonblock_ing)
1490{
1491 struct gspca_frame *frame;
1492 int i, j, ret;
1493
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001494 /* if userptr, treat the awaiting URBs */
1495 if (gspca_dev->memory == V4L2_MEMORY_USERPTR)
1496 isoc_transfer(gspca_dev);
1497
1498 /* check if a frame is ready */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001499 i = gspca_dev->fr_o;
1500 j = gspca_dev->fr_queue[i];
1501 frame = &gspca_dev->frame[j];
1502 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1503 goto ok;
1504 if (nonblock_ing) /* no frame yet */
1505 return -EAGAIN;
1506
1507 /* wait till a frame is ready */
1508 for (;;) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001509 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1510 atomic_read(&gspca_dev->nevent) > 0,
1511 msecs_to_jiffies(3000));
1512 if (ret <= 0) {
1513 if (ret < 0)
1514 return ret;
1515 return -EIO;
1516 }
Hans de Goedee2997a72008-04-23 08:09:12 -03001517 if (!gspca_dev->streaming || !gspca_dev->present)
1518 return -EIO;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001519 if (gspca_dev->memory == V4L2_MEMORY_USERPTR)
1520 isoc_transfer(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001521 i = gspca_dev->fr_o;
1522 j = gspca_dev->fr_queue[i];
1523 frame = &gspca_dev->frame[j];
1524 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1525 break;
1526 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001527ok:
1528 atomic_dec(&gspca_dev->nevent);
1529 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1530 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1531 gspca_dev->fr_q,
1532 gspca_dev->fr_i,
1533 gspca_dev->fr_o);
Hans de Goedee2997a72008-04-23 08:09:12 -03001534
1535 if (gspca_dev->sd_desc->dq_callback)
1536 gspca_dev->sd_desc->dq_callback(gspca_dev);
1537
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001538 return j;
1539}
1540
1541/*
1542 * dequeue a video buffer
1543 *
1544 * If nonblock_ing is false, block until a buffer is available.
1545 */
1546static int vidioc_dqbuf(struct file *file, void *priv,
1547 struct v4l2_buffer *v4l2_buf)
1548{
1549 struct gspca_dev *gspca_dev = priv;
1550 struct gspca_frame *frame;
1551 int i, ret;
1552
1553 PDEBUG(D_FRAM, "dqbuf");
1554 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1555 || (v4l2_buf->memory != V4L2_MEMORY_MMAP
1556 && v4l2_buf->memory != V4L2_MEMORY_USERPTR))
1557 return -EINVAL;
1558 if (!gspca_dev->streaming)
1559 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001560 if (gspca_dev->capt_file != file) {
1561 ret = -EINVAL;
1562 goto out;
1563 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001564
1565 /* only one read */
1566 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1567 return -ERESTARTSYS;
1568
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001569 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001570 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001571 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001572 i = ret; /* frame index */
1573 frame = &gspca_dev->frame[i];
1574 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1575 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1576 PDEBUG(D_FRAM, "dqbuf %d", i);
1577 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001578out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001579 mutex_unlock(&gspca_dev->read_lock);
1580 return ret;
1581}
1582
1583/*
1584 * queue a video buffer
1585 *
1586 * Attempting to queue a buffer that has already been
1587 * queued will return -EINVAL.
1588 */
1589static int vidioc_qbuf(struct file *file, void *priv,
1590 struct v4l2_buffer *v4l2_buf)
1591{
1592 struct gspca_dev *gspca_dev = priv;
1593 struct gspca_frame *frame;
1594 int i, index, ret;
1595
1596 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1597 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1598 return -EINVAL;
1599
1600 index = v4l2_buf->index;
1601 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001602 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001603 "qbuf idx %d >= %d", index, gspca_dev->nframes);
1604 return -EINVAL;
1605 }
1606 frame = &gspca_dev->frame[index];
1607
1608 if (v4l2_buf->memory != frame->v4l2_buf.memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001609 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001610 return -EINVAL;
1611 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001612 if (gspca_dev->capt_file != file)
1613 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001614
Hans de Goedee2997a72008-04-23 08:09:12 -03001615 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1616 return -ERESTARTSYS;
1617
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001618 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1619 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001620 ret = -EINVAL;
1621 goto out;
1622 }
1623
1624 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001625/* frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001626
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001627 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001628 frame->data = frame->data_end =
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001629 (__u8 *) v4l2_buf->m.userptr;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001630 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1631 frame->v4l2_buf.length = v4l2_buf->length;
1632 }
1633
1634 /* put the buffer in the 'queued' queue */
1635 i = gspca_dev->fr_q;
1636 gspca_dev->fr_queue[i] = index;
1637 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1638 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1639 gspca_dev->fr_q,
1640 gspca_dev->fr_i,
1641 gspca_dev->fr_o);
1642
1643 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1644 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1645 ret = 0;
1646out:
1647 mutex_unlock(&gspca_dev->queue_lock);
1648 return ret;
1649}
1650
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001651/*
1652 * allocate the resources for read()
1653 */
1654static int read_alloc(struct gspca_dev *gspca_dev,
1655 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001656{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001657 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001658 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001659
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001660 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001661 if (gspca_dev->nframes == 0) {
1662 struct v4l2_requestbuffers rb;
1663
1664 memset(&rb, 0, sizeof rb);
1665 rb.count = gspca_dev->nbufread;
1666 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1667 rb.memory = V4L2_MEMORY_MMAP;
1668 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1669 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001670 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001671 return ret;
1672 }
1673 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1674 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1675 v4l2_buf.memory = V4L2_MEMORY_MMAP;
1676 for (i = 0; i < gspca_dev->nbufread; i++) {
1677 v4l2_buf.index = i;
1678/*fixme: ugly!*/
1679 gspca_dev->frame[i].v4l2_buf.flags |=
1680 V4L2_BUF_FLAG_MAPPED;
1681 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1682 if (ret != 0) {
1683 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1684 return ret;
1685 }
1686 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001687 gspca_dev->memory = GSPCA_MEMORY_READ;
1688 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001689
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001690 /* start streaming */
1691 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1692 if (ret != 0)
1693 PDEBUG(D_STREAM, "read streamon err %d", ret);
1694 return ret;
1695}
1696
1697static unsigned int dev_poll(struct file *file, poll_table *wait)
1698{
1699 struct gspca_dev *gspca_dev = file->private_data;
1700 int i, ret;
1701
1702 PDEBUG(D_FRAM, "poll");
1703
1704 poll_wait(file, &gspca_dev->wq, wait);
1705 if (!gspca_dev->present)
1706 return POLLERR;
1707
1708 /* if not streaming, the user would use read() */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001709 if (!gspca_dev->streaming) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001710 if (gspca_dev->memory != GSPCA_MEMORY_NO) {
1711 ret = POLLERR; /* not the 1st time */
1712 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001713 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001714 ret = read_alloc(gspca_dev, file);
1715 if (ret != 0) {
1716 ret = POLLERR;
1717 goto out;
1718 }
1719 }
1720
1721 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1722 return POLLERR;
1723 if (!gspca_dev->present) {
1724 ret = POLLERR;
1725 goto out;
1726 }
1727
1728 /* if not mmap, treat the awaiting URBs */
1729 if (gspca_dev->memory == V4L2_MEMORY_USERPTR
1730 && gspca_dev->capt_file == file)
1731 isoc_transfer(gspca_dev);
1732
1733 i = gspca_dev->fr_o;
1734 i = gspca_dev->fr_queue[i];
1735 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1736 ret = POLLIN | POLLRDNORM; /* something to read */
1737 else
1738 ret = 0;
1739out:
1740 mutex_unlock(&gspca_dev->queue_lock);
1741 return ret;
1742}
1743
1744static ssize_t dev_read(struct file *file, char __user *data,
1745 size_t count, loff_t *ppos)
1746{
1747 struct gspca_dev *gspca_dev = file->private_data;
1748 struct gspca_frame *frame;
1749 struct v4l2_buffer v4l2_buf;
1750 struct timeval timestamp;
1751 int i, ret, ret2;
1752
1753 PDEBUG(D_FRAM, "read (%d)", count);
1754 if (!gspca_dev->present)
1755 return -ENODEV;
1756 switch (gspca_dev->memory) {
1757 case GSPCA_MEMORY_NO: /* first time */
1758 ret = read_alloc(gspca_dev, file);
1759 if (ret != 0)
1760 return ret;
1761 break;
1762 case GSPCA_MEMORY_READ:
1763 if (gspca_dev->capt_file != file)
1764 return -EINVAL;
1765 break;
1766 default:
1767 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001768 }
1769
1770 /* get a frame */
1771 jiffies_to_timeval(get_jiffies_64(), &timestamp);
1772 timestamp.tv_sec--;
1773 for (i = 0; i < 2; i++) {
1774 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1775 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1776 v4l2_buf.memory = V4L2_MEMORY_MMAP;
1777 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1778 if (ret != 0) {
1779 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1780 return ret;
1781 }
1782
1783 /* if the process slept for more than 1 second,
1784 * get a brand new frame */
1785 frame = &gspca_dev->frame[v4l2_buf.index];
1786 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1787 break;
1788 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1789 if (ret != 0) {
1790 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1791 return ret;
1792 }
1793 }
1794
1795 /* copy the frame */
1796 if (count < frame->v4l2_buf.bytesused) {
1797 PDEBUG(D_STREAM, "read bad count: %d < %d",
1798 count, frame->v4l2_buf.bytesused);
1799/*fixme: special errno?*/
1800 ret = -EINVAL;
1801 goto out;
1802 }
1803 count = frame->v4l2_buf.bytesused;
1804 ret = copy_to_user(data, frame->data, count);
1805 if (ret != 0) {
1806 PDEBUG(D_ERR|D_STREAM,
1807 "read cp to user lack %d / %d", ret, count);
1808 ret = -EFAULT;
1809 goto out;
1810 }
1811 ret = count;
1812out:
1813 /* in each case, requeue the buffer */
1814 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1815 if (ret2 != 0)
1816 return ret2;
1817 return ret;
1818}
1819
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001820static void dev_release(struct video_device *vfd)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001821{
1822 /* nothing */
1823}
1824
1825static struct file_operations dev_fops = {
1826 .owner = THIS_MODULE,
1827 .open = dev_open,
1828 .release = dev_close,
1829 .read = dev_read,
1830 .mmap = dev_mmap,
1831 .ioctl = video_ioctl2,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001832#ifdef CONFIG_COMPAT
1833 .compat_ioctl = v4l_compat_ioctl32,
1834#endif
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001835 .llseek = no_llseek,
1836 .poll = dev_poll,
1837};
1838
1839static struct video_device gspca_template = {
1840 .name = "gspca main driver",
1841 .type = VID_TYPE_CAPTURE,
1842 .fops = &dev_fops,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001843 .release = dev_release, /* mandatory */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001844 .minor = -1,
1845 .vidioc_querycap = vidioc_querycap,
1846 .vidioc_dqbuf = vidioc_dqbuf,
1847 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001848 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1849 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1850 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1851 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001852 .vidioc_streamon = vidioc_streamon,
1853 .vidioc_queryctrl = vidioc_queryctrl,
1854 .vidioc_g_ctrl = vidioc_g_ctrl,
1855 .vidioc_s_ctrl = vidioc_s_ctrl,
1856 .vidioc_querymenu = vidioc_querymenu,
1857 .vidioc_enum_input = vidioc_enum_input,
1858 .vidioc_g_input = vidioc_g_input,
1859 .vidioc_s_input = vidioc_s_input,
1860 .vidioc_reqbufs = vidioc_reqbufs,
1861 .vidioc_querybuf = vidioc_querybuf,
1862 .vidioc_streamoff = vidioc_streamoff,
1863 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1864 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1865 .vidioc_g_parm = vidioc_g_parm,
1866 .vidioc_s_parm = vidioc_s_parm,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001867 .vidioc_s_std = vidioc_s_std,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001868#ifdef CONFIG_VIDEO_V4L1_COMPAT
1869 .vidiocgmbuf = vidiocgmbuf,
1870#endif
1871};
1872
1873/*
1874 * probe and create a new gspca device
1875 *
1876 * This function must be called by the sub-driver when it is
1877 * called for probing a new device.
1878 */
1879int gspca_dev_probe(struct usb_interface *intf,
1880 const struct usb_device_id *id,
1881 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001882 int dev_size,
1883 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001884{
1885 struct usb_interface_descriptor *interface;
1886 struct gspca_dev *gspca_dev;
1887 struct usb_device *dev = interface_to_usbdev(intf);
1888 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001889
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001890 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001891
1892 /* we don't handle multi-config cameras */
1893 if (dev->descriptor.bNumConfigurations != 1)
1894 return -ENODEV;
1895 interface = &intf->cur_altsetting->desc;
1896 if (interface->bInterfaceNumber > 0)
1897 return -ENODEV;
1898
1899 /* create the device */
1900 if (dev_size < sizeof *gspca_dev)
1901 dev_size = sizeof *gspca_dev;
1902 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
1903 if (gspca_dev == NULL) {
1904 err("couldn't kzalloc gspca struct");
1905 return -EIO;
1906 }
1907 gspca_dev->dev = dev;
1908 gspca_dev->iface = interface->bInterfaceNumber;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001909 gspca_dev->nbalt = intf->num_altsetting;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001910 gspca_dev->sd_desc = sd_desc;
1911/* gspca_dev->users = 0; (done by kzalloc) */
1912 gspca_dev->nbufread = 2;
1913
1914 /* configure the subdriver */
1915 ret = gspca_dev->sd_desc->config(gspca_dev, id);
1916 if (ret < 0)
1917 goto out;
1918 ret = gspca_set_alt0(gspca_dev);
1919 if (ret < 0)
1920 goto out;
1921 gspca_set_default_mode(gspca_dev);
1922
1923 mutex_init(&gspca_dev->usb_lock);
1924 mutex_init(&gspca_dev->read_lock);
1925 mutex_init(&gspca_dev->queue_lock);
1926 init_waitqueue_head(&gspca_dev->wq);
1927
1928 /* init video stuff */
1929 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
1930 gspca_dev->vdev.dev = &dev->dev;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001931 memcpy(&gspca_dev->fops, &dev_fops, sizeof gspca_dev->fops);
1932 gspca_dev->vdev.fops = &gspca_dev->fops;
1933 gspca_dev->fops.owner = module; /* module protection */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001934 ret = video_register_device(&gspca_dev->vdev,
1935 VFL_TYPE_GRABBER,
1936 video_nr);
1937 if (ret < 0) {
1938 err("video_register_device err %d", ret);
1939 goto out;
1940 }
1941
1942 gspca_dev->present = 1;
1943 usb_set_intfdata(intf, gspca_dev);
1944 PDEBUG(D_PROBE, "probe ok");
1945 return 0;
1946out:
1947 kfree(gspca_dev);
1948 return ret;
1949}
1950EXPORT_SYMBOL(gspca_dev_probe);
1951
1952/*
1953 * USB disconnection
1954 *
1955 * This function must be called by the sub-driver
1956 * when the device disconnects, after the specific resources are freed.
1957 */
1958void gspca_disconnect(struct usb_interface *intf)
1959{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001960 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001961
1962 if (!gspca_dev)
1963 return;
1964 gspca_dev->present = 0;
Hans de Goedee2997a72008-04-23 08:09:12 -03001965 mutex_lock(&gspca_dev->queue_lock);
1966 mutex_lock(&gspca_dev->usb_lock);
1967 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001968 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001969 mutex_unlock(&gspca_dev->usb_lock);
Hans de Goedee2997a72008-04-23 08:09:12 -03001970 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001971 while (gspca_dev->users != 0) { /* wait until fully closed */
1972 atomic_inc(&gspca_dev->nevent);
1973 wake_up_interruptible(&gspca_dev->wq); /* wake processes */
1974 schedule();
1975 }
1976/* We don't want people trying to open up the device */
1977 video_unregister_device(&gspca_dev->vdev);
1978/* Free the memory */
1979 kfree(gspca_dev);
1980 PDEBUG(D_PROBE, "disconnect complete");
1981}
1982EXPORT_SYMBOL(gspca_disconnect);
1983
1984/* -- module insert / remove -- */
1985static int __init gspca_init(void)
1986{
1987 info("main v%s registered", version);
1988 return 0;
1989}
1990static void __exit gspca_exit(void)
1991{
1992 info("main deregistered");
1993}
1994
1995module_init(gspca_init);
1996module_exit(gspca_exit);
1997
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001998#ifdef VIDEO_ADV_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001999module_param_named(debug, gspca_debug, int, 0644);
2000MODULE_PARM_DESC(debug,
2001 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002002 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
2003 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002004#endif
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002005module_param(comp_fac, int, 0644);
2006MODULE_PARM_DESC(comp_fac,
2007 "Buffer size ratio when compressed in percent");