blob: 0b292ca0c0802594d9d855049755985505b4eb6d [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>
Jean-Francois Moine956e42d2008-07-01 10:03:42 -030031#include <linux/io.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030032#include <asm/page.h>
Jean-Francois Moine956e42d2008-07-01 10:03:42 -030033#include <linux/uaccess.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030034#include <linux/jiffies.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030035#include <media/v4l2-ioctl.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030036
37#include "gspca.h"
38
Jean-Francois Moined43fa322008-06-12 10:58:58 -030039/* global values */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -030040#define DEF_NURBS 2 /* default number of URBs */
Jean-Francois Moined43fa322008-06-12 10:58:58 -030041
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030042MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
43MODULE_DESCRIPTION("GSPCA USB Camera Driver");
44MODULE_LICENSE("GPL");
45
Jean-Francois Moinebb64e862008-07-19 06:49:28 -030046#define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 2, 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030047
48static int video_nr = -1;
49
Jean-Francois Moine335b3f82008-07-30 04:53:02 -030050#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030051int gspca_debug = D_ERR | D_PROBE;
52EXPORT_SYMBOL(gspca_debug);
53
54static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
55{
56 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
57 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
58 txt,
59 pixfmt & 0xff,
60 (pixfmt >> 8) & 0xff,
61 (pixfmt >> 16) & 0xff,
62 pixfmt >> 24,
63 w, h);
64 } else {
65 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
66 txt,
67 pixfmt,
68 w, h);
69 }
70}
71#else
72#define PDEBUG_MODE(txt, pixfmt, w, h)
73#endif
74
Jean-Francois Moined43fa322008-06-12 10:58:58 -030075/* specific memory types - !! should different from V4L2_MEMORY_xxx */
76#define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
77#define GSPCA_MEMORY_READ 7
78
Jean-Francois Moined43fa322008-06-12 10:58:58 -030079#define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
Jean-Francois Moined43fa322008-06-12 10:58:58 -030080
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030081/*
82 * VMA operations.
83 */
84static void gspca_vm_open(struct vm_area_struct *vma)
85{
86 struct gspca_frame *frame = vma->vm_private_data;
87
88 frame->vma_use_count++;
89 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
90}
91
92static void gspca_vm_close(struct vm_area_struct *vma)
93{
94 struct gspca_frame *frame = vma->vm_private_data;
95
96 if (--frame->vma_use_count <= 0)
97 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
98}
99
100static struct vm_operations_struct gspca_vm_ops = {
101 .open = gspca_vm_open,
102 .close = gspca_vm_close,
103};
104
105/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300106 * fill a video frame from an URB and resubmit
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300107 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300108static void fill_frame(struct gspca_dev *gspca_dev,
109 struct urb *urb)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300110{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300111 struct gspca_frame *frame;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300112 __u8 *data; /* address of data in the iso message */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300113 int i, j, len, st;
114 cam_pkt_op pkt_scan;
115
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300116 if (urb->status != 0) {
Jean-Francois Moine6a709742008-09-03 16:48:10 -0300117 if (!gspca_dev->frozen)
118 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300119 return; /* disconnection ? */
120 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300121 pkt_scan = gspca_dev->sd_desc->pkt_scan;
122 for (i = 0; i < urb->number_of_packets; i++) {
123
124 /* check the availability of the frame buffer */
125 j = gspca_dev->fr_i;
126 j = gspca_dev->fr_queue[j];
127 frame = &gspca_dev->frame[j];
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300128 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300129 != V4L2_BUF_FLAG_QUEUED) {
130 gspca_dev->last_packet_type = DISCARD_PACKET;
131 break;
132 }
133
134 /* check the packet status and length */
135 len = urb->iso_frame_desc[i].actual_length;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300136 if (len == 0)
137 continue;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300138 st = urb->iso_frame_desc[i].status;
139 if (st) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300140 PDEBUG(D_ERR,
141 "ISOC data error: [%d] len=%d, status=%d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300142 i, len, st);
143 gspca_dev->last_packet_type = DISCARD_PACKET;
144 continue;
145 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300146
147 /* let the packet be analyzed by the subdriver */
148 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
149 i, urb->iso_frame_desc[i].offset, len);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300150 data = (__u8 *) urb->transfer_buffer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300151 + urb->iso_frame_desc[i].offset;
152 pkt_scan(gspca_dev, frame, data, len);
153 }
154
155 /* resubmit the URB */
156 urb->status = 0;
157 st = usb_submit_urb(urb, GFP_ATOMIC);
158 if (st < 0)
159 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
160}
161
162/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300163 * ISOC message interrupt from the USB device
164 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300165 * Analyse each packet and call the subdriver for copy to the frame buffer.
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300166 */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300167static void isoc_irq(struct urb *urb
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300168)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300169{
170 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
171
Hans de Goede3647fea2008-07-15 05:36:30 -0300172 PDEBUG(D_PACK, "isoc irq");
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300173 if (!gspca_dev->streaming)
174 return;
175 fill_frame(gspca_dev, urb);
176}
177
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300178/*
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300179 * add data to the current frame
180 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300181 * This function is called by the subdrivers at interrupt level.
182 *
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300183 * To build a frame, these ones must add
184 * - one FIRST_PACKET
185 * - 0 or many INTER_PACKETs
186 * - one LAST_PACKET
187 * DISCARD_PACKET invalidates the whole frame.
188 * On LAST_PACKET, a new frame is returned.
189 */
190struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
191 int packet_type,
192 struct gspca_frame *frame,
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300193 const __u8 *data,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300194 int len)
195{
196 int i, j;
197
Hans de Goedee2997a72008-04-23 08:09:12 -0300198 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300199
200 /* when start of a new frame, if the current frame buffer
201 * is not queued, discard the whole frame */
202 if (packet_type == FIRST_PACKET) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300203 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
204 != V4L2_BUF_FLAG_QUEUED) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300205 gspca_dev->last_packet_type = DISCARD_PACKET;
206 return frame;
207 }
208 frame->data_end = frame->data;
209 jiffies_to_timeval(get_jiffies_64(),
210 &frame->v4l2_buf.timestamp);
211 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300212 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
Jean-Francois Moine06ca78f2008-07-22 03:45:08 -0300213 if (packet_type == LAST_PACKET)
214 gspca_dev->last_packet_type = packet_type;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300215 return frame;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300216 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300217
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300218 /* append the packet to the frame buffer */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300219 if (len > 0) {
220 if (frame->data_end - frame->data + len
221 > frame->v4l2_buf.length) {
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -0300222 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300223 frame->data_end - frame->data + len,
224 frame->v4l2_buf.length);
225 packet_type = DISCARD_PACKET;
226 } else {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300227 memcpy(frame->data_end, data, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300228 frame->data_end += len;
229 }
230 }
231 gspca_dev->last_packet_type = packet_type;
232
233 /* if last packet, wake the application and advance in the queue */
234 if (packet_type == LAST_PACKET) {
235 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
236 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
237 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
238 atomic_inc(&gspca_dev->nevent);
239 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300240 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
241 gspca_dev->fr_i = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300242 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
243 frame->v4l2_buf.bytesused,
244 gspca_dev->fr_q,
245 i,
246 gspca_dev->fr_o);
247 j = gspca_dev->fr_queue[i];
248 frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300249 }
250 return frame;
251}
252EXPORT_SYMBOL(gspca_frame_add);
253
254static int gspca_is_compressed(__u32 format)
255{
256 switch (format) {
257 case V4L2_PIX_FMT_MJPEG:
258 case V4L2_PIX_FMT_JPEG:
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300259 case V4L2_PIX_FMT_SPCA561:
Hans de Goedeab8f12c2008-07-04 18:29:32 -0300260 case V4L2_PIX_FMT_PAC207:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300261 return 1;
262 }
263 return 0;
264}
265
266static void *rvmalloc(unsigned long size)
267{
268 void *mem;
269 unsigned long adr;
270
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300271/* size = PAGE_ALIGN(size); (already done) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300272 mem = vmalloc_32(size);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300273 if (mem != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300274 adr = (unsigned long) mem;
275 while ((long) size > 0) {
276 SetPageReserved(vmalloc_to_page((void *) adr));
277 adr += PAGE_SIZE;
278 size -= PAGE_SIZE;
279 }
280 }
281 return mem;
282}
283
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300284static void rvfree(void *mem, long size)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300285{
286 unsigned long adr;
287
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300288 adr = (unsigned long) mem;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300289 while (size > 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300290 ClearPageReserved(vmalloc_to_page((void *) adr));
291 adr += PAGE_SIZE;
292 size -= PAGE_SIZE;
293 }
294 vfree(mem);
295}
296
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300297static int frame_alloc(struct gspca_dev *gspca_dev,
298 unsigned int count)
299{
300 struct gspca_frame *frame;
301 unsigned int frsz;
302 int i;
303
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300304 i = gspca_dev->curr_mode;
305 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300306 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300307 frsz = PAGE_ALIGN(frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300308 gspca_dev->frsz = frsz;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300309 if (count > GSPCA_MAX_FRAMES)
310 count = GSPCA_MAX_FRAMES;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300311 gspca_dev->frbuf = rvmalloc(frsz * count);
312 if (!gspca_dev->frbuf) {
313 err("frame alloc failed");
314 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300315 }
316 gspca_dev->nframes = count;
317 for (i = 0; i < count; i++) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300318 frame = &gspca_dev->frame[i];
319 frame->v4l2_buf.index = i;
320 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
321 frame->v4l2_buf.flags = 0;
322 frame->v4l2_buf.field = V4L2_FIELD_NONE;
323 frame->v4l2_buf.length = frsz;
324 frame->v4l2_buf.memory = gspca_dev->memory;
325 frame->v4l2_buf.sequence = 0;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300326 frame->data = frame->data_end =
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300327 gspca_dev->frbuf + i * frsz;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300328 frame->v4l2_buf.m.offset = i * frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300329 }
330 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
331 gspca_dev->last_packet_type = DISCARD_PACKET;
332 gspca_dev->sequence = 0;
333 atomic_set(&gspca_dev->nevent, 0);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300334 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300335}
336
337static void frame_free(struct gspca_dev *gspca_dev)
338{
339 int i;
340
341 PDEBUG(D_STREAM, "frame free");
Harvey Harrisonefab8212008-07-05 06:12:47 -0300342 if (gspca_dev->frbuf != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300343 rvfree(gspca_dev->frbuf,
344 gspca_dev->nframes * gspca_dev->frsz);
345 gspca_dev->frbuf = NULL;
346 for (i = 0; i < gspca_dev->nframes; i++)
347 gspca_dev->frame[i].data = NULL;
348 }
349 gspca_dev->nframes = 0;
350}
351
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300352static void destroy_urbs(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300353{
354 struct urb *urb;
355 unsigned int i;
356
357 PDEBUG(D_STREAM, "kill transfer");
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300358 for (i = 0; i < MAX_NURBS; ++i) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300359 urb = gspca_dev->urb[i];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300360 if (urb == NULL)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300361 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300362
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300363 gspca_dev->urb[i] = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300364 usb_kill_urb(urb);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300365 if (urb->transfer_buffer != NULL)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300366 usb_buffer_free(gspca_dev->dev,
367 urb->transfer_buffer_length,
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300368 urb->transfer_buffer,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300369 urb->transfer_dma);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300370 usb_free_urb(urb);
371 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300372}
373
374/*
375 * search an input isochronous endpoint in an alternate setting
376 */
377static struct usb_host_endpoint *alt_isoc(struct usb_host_interface *alt,
378 __u8 epaddr)
379{
380 struct usb_host_endpoint *ep;
381 int i, attr;
382
383 epaddr |= USB_DIR_IN;
384 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
385 ep = &alt->endpoint[i];
386 if (ep->desc.bEndpointAddress == epaddr) {
387 attr = ep->desc.bmAttributes
388 & USB_ENDPOINT_XFERTYPE_MASK;
389 if (attr == USB_ENDPOINT_XFER_ISOC)
390 return ep;
391 break;
392 }
393 }
394 return NULL;
395}
396
397/*
398 * search an input isochronous endpoint
399 *
400 * The endpoint is defined by the subdriver.
401 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
402 * This routine may be called many times when the bandwidth is too small
403 * (the bandwidth is checked on urb submit).
404 */
Adrian Bunk903e10a2008-07-22 02:35:05 -0300405static struct usb_host_endpoint *get_isoc_ep(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300406{
407 struct usb_interface *intf;
408 struct usb_host_endpoint *ep;
409 int i, ret;
410
411 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300412 ep = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300413 i = gspca_dev->alt; /* previous alt setting */
414 while (--i > 0) { /* alt 0 is unusable */
415 ep = alt_isoc(&intf->altsetting[i], gspca_dev->cam.epaddr);
416 if (ep)
417 break;
418 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300419 if (ep == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300420 err("no ISOC endpoint found");
421 return NULL;
422 }
423 PDEBUG(D_STREAM, "use ISOC alt %d ep 0x%02x",
424 i, ep->desc.bEndpointAddress);
425 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
426 if (ret < 0) {
427 err("set interface err %d", ret);
428 return NULL;
429 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300430 gspca_dev->alt = i; /* memorize the current alt setting */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300431 return ep;
432}
433
434/*
435 * create the isochronous URBs
436 */
437static int create_urbs(struct gspca_dev *gspca_dev,
438 struct usb_host_endpoint *ep)
439{
440 struct urb *urb;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300441 int n, nurbs, i, psize, npkt, bsize;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300442
443 /* calculate the packet size and the number of packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300444 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300445
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300446 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
447 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
448 npkt = ISO_MAX_SIZE / psize;
449 if (npkt > ISO_MAX_PKT)
450 npkt = ISO_MAX_PKT;
451 bsize = psize * npkt;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300452 PDEBUG(D_STREAM,
453 "isoc %d pkts size %d (bsize:%d)", npkt, psize, bsize);
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300454 nurbs = DEF_NURBS;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300455 gspca_dev->nurbs = nurbs;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300456 for (n = 0; n < nurbs; n++) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300457 urb = usb_alloc_urb(npkt, GFP_KERNEL);
458 if (!urb) {
459 err("usb_alloc_urb failed");
460 return -ENOMEM;
461 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300462 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300463 bsize,
464 GFP_KERNEL,
465 &urb->transfer_dma);
466
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300467 if (urb->transfer_buffer == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300468 usb_free_urb(urb);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300469 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300470 err("usb_buffer_urb failed");
471 return -ENOMEM;
472 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300473 gspca_dev->urb[n] = urb;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300474 urb->dev = gspca_dev->dev;
475 urb->context = gspca_dev;
476 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
477 ep->desc.bEndpointAddress);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300478 urb->transfer_flags = URB_ISO_ASAP
479 | URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300480 urb->interval = ep->desc.bInterval;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300481 urb->complete = isoc_irq;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300482 urb->number_of_packets = npkt;
483 urb->transfer_buffer_length = bsize;
484 for (i = 0; i < npkt; i++) {
485 urb->iso_frame_desc[i].length = psize;
486 urb->iso_frame_desc[i].offset = psize * i;
487 }
488 }
489 return 0;
490}
491
492/*
493 * start the USB transfer
494 */
495static int gspca_init_transfer(struct gspca_dev *gspca_dev)
496{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300497 struct usb_host_endpoint *ep;
498 int n, ret;
499
Hans de Goedee2997a72008-04-23 08:09:12 -0300500 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
501 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300502
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300503 /* set the higher alternate setting and
504 * loop until urb submit succeeds */
505 gspca_dev->alt = gspca_dev->nbalt;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300506 for (;;) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300507 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300508 ep = get_isoc_ep(gspca_dev);
509 if (ep == NULL) {
510 ret = -EIO;
511 goto out;
512 }
513 ret = create_urbs(gspca_dev, ep);
514 if (ret < 0)
515 goto out;
516
517 /* start the cam */
518 gspca_dev->sd_desc->start(gspca_dev);
519 gspca_dev->streaming = 1;
520 atomic_set(&gspca_dev->nevent, 0);
521
522 /* submit the URBs */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300523 for (n = 0; n < gspca_dev->nurbs; n++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300524 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300525 if (ret < 0) {
526 PDEBUG(D_ERR|D_STREAM,
527 "usb_submit_urb [%d] err %d", n, ret);
Hans de Goedee2997a72008-04-23 08:09:12 -0300528 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300529 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300530 if (ret == -ENOSPC)
531 break; /* try the previous alt */
532 goto out;
533 }
534 }
535 if (ret >= 0)
536 break;
537 }
538out:
539 mutex_unlock(&gspca_dev->usb_lock);
540 return ret;
541}
542
543static int gspca_set_alt0(struct gspca_dev *gspca_dev)
544{
545 int ret;
546
547 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
548 if (ret < 0)
549 PDEBUG(D_ERR|D_STREAM, "set interface 0 err %d", ret);
550 return ret;
551}
552
Hans de Goedee2997a72008-04-23 08:09:12 -0300553/* Note both the queue and the usb lock should be hold when calling this */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300554static void gspca_stream_off(struct gspca_dev *gspca_dev)
555{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300556 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300557 atomic_set(&gspca_dev->nevent, 0);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300558 if (gspca_dev->present) {
559 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300560 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300561 gspca_set_alt0(gspca_dev);
562 gspca_dev->sd_desc->stop0(gspca_dev);
563 PDEBUG(D_STREAM, "stream off OK");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300564 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300565}
566
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300567static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300568{
569 int i;
570
571 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
572 gspca_dev->curr_mode = i;
573 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
574 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300575 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300576}
577
578static int wxh_to_mode(struct gspca_dev *gspca_dev,
579 int width, int height)
580{
581 int i;
582
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300583 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
584 if (width >= gspca_dev->cam.cam_mode[i].width
585 && height >= gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300586 break;
587 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300588 return i;
589}
590
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300591/*
592 * search a mode with the right pixel format
593 */
594static int gspca_get_mode(struct gspca_dev *gspca_dev,
595 int mode,
596 int pixfmt)
597{
598 int modeU, modeD;
599
600 modeU = modeD = mode;
601 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
602 if (--modeD >= 0) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300603 if (gspca_dev->cam.cam_mode[modeD].pixelformat
604 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300605 return modeD;
606 }
607 if (++modeU < gspca_dev->cam.nmodes) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300608 if (gspca_dev->cam.cam_mode[modeU].pixelformat
609 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300610 return modeU;
611 }
612 }
613 return -EINVAL;
614}
615
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300616static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300617 struct v4l2_fmtdesc *fmtdesc)
618{
619 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300620 int i, j, index;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300621 __u32 fmt_tb[8];
622
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300623 /* give an index to each format */
624 index = 0;
625 j = 0;
626 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300627 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300628 j = 0;
629 for (;;) {
630 if (fmt_tb[j] == fmt_tb[index])
631 break;
632 j++;
633 }
634 if (j == index) {
635 if (fmtdesc->index == index)
636 break; /* new format */
637 index++;
638 if (index >= sizeof fmt_tb / sizeof fmt_tb[0])
639 return -EINVAL;
640 }
641 }
642 if (i < 0)
643 return -EINVAL; /* no more format */
644
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300645 fmtdesc->pixelformat = fmt_tb[index];
646 if (gspca_is_compressed(fmt_tb[index]))
647 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300648 fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300649 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
650 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
651 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
652 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
653 fmtdesc->description[4] = '\0';
654 return 0;
655}
656
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300657static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300658 struct v4l2_format *fmt)
659{
660 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300661 int mode;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300662
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300663 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
664 return -EINVAL;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300665 mode = gspca_dev->curr_mode;
666 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
667 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300668 return 0;
669}
670
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300671static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300672 struct v4l2_format *fmt)
673{
Hans de Goede24c530b2008-07-04 17:55:18 -0300674 int w, h, mode, mode2;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300675
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300676 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
677 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300678 w = fmt->fmt.pix.width;
679 h = fmt->fmt.pix.height;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300680
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300681#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300682 if (gspca_debug & D_CONF)
683 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
684#endif
685 /* search the closest mode for width and height */
686 mode = wxh_to_mode(gspca_dev, w, h);
687
688 /* OK if right palette */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300689 if (gspca_dev->cam.cam_mode[mode].pixelformat
690 != fmt->fmt.pix.pixelformat) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300691
692 /* else, search the closest mode with the same pixel format */
693 mode2 = gspca_get_mode(gspca_dev, mode,
694 fmt->fmt.pix.pixelformat);
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300695 if (mode2 >= 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300696 mode = mode2;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300697/* else
698 ; * no chance, return this mode */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300699 }
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300700 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
701 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300702 return mode; /* used when s_fmt */
703}
704
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300705static int vidioc_try_fmt_vid_cap(struct file *file,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300706 void *priv,
707 struct v4l2_format *fmt)
708{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300709 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300710 int ret;
711
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300712 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300713 if (ret < 0)
714 return ret;
715 return 0;
716}
717
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300718static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300719 struct v4l2_format *fmt)
720{
721 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300722 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300723
Hans de Goedee2997a72008-04-23 08:09:12 -0300724 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
725 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300726
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300727 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300728 if (ret < 0)
729 goto out;
730
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300731 if (gspca_dev->nframes != 0
732 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
733 ret = -EINVAL;
734 goto out;
735 }
736
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300737 if (ret == gspca_dev->curr_mode) {
738 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300739 goto out; /* same mode */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300740 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300741
742 if (gspca_dev->streaming) {
743 ret = -EBUSY;
744 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -0300745 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300746 gspca_dev->width = fmt->fmt.pix.width;
747 gspca_dev->height = fmt->fmt.pix.height;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300748 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
749 gspca_dev->curr_mode = ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300750
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300751 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300752out:
753 mutex_unlock(&gspca_dev->queue_lock);
754 return ret;
755}
756
757static int dev_open(struct inode *inode, struct file *file)
758{
759 struct gspca_dev *gspca_dev;
760 int ret;
761
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300762 PDEBUG(D_STREAM, "%s open", current->comm);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300763 gspca_dev = (struct gspca_dev *) video_devdata(file);
Hans de Goedee2997a72008-04-23 08:09:12 -0300764 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
765 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300766 if (!gspca_dev->present) {
767 ret = -ENODEV;
768 goto out;
769 }
770
771 /* if not done yet, initialize the sensor */
772 if (gspca_dev->users == 0) {
Hans de Goedee2997a72008-04-23 08:09:12 -0300773 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
774 ret = -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300775 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -0300776 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300777 ret = gspca_dev->sd_desc->open(gspca_dev);
778 mutex_unlock(&gspca_dev->usb_lock);
779 if (ret != 0) {
780 PDEBUG(D_ERR|D_CONF, "init device failed %d", ret);
781 goto out;
782 }
Hans de Goedee2997a72008-04-23 08:09:12 -0300783 } else if (gspca_dev->users > 4) { /* (arbitrary value) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300784 ret = -EBUSY;
785 goto out;
786 }
787 gspca_dev->users++;
788 file->private_data = gspca_dev;
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300789#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300790 /* activate the v4l2 debug */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300791 if (gspca_debug & D_V4L2)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300792 gspca_dev->vdev.debug |= 3;
793 else
794 gspca_dev->vdev.debug &= ~3;
795#endif
796out:
797 mutex_unlock(&gspca_dev->queue_lock);
798 if (ret != 0)
799 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
800 else
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300801 PDEBUG(D_STREAM, "open done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300802 return ret;
803}
804
805static int dev_close(struct inode *inode, struct file *file)
806{
807 struct gspca_dev *gspca_dev = file->private_data;
808
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300809 PDEBUG(D_STREAM, "%s close", current->comm);
Hans de Goedee2997a72008-04-23 08:09:12 -0300810 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
811 return -ERESTARTSYS;
812 gspca_dev->users--;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300813
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -0300814 /* if the file did the capture, free the streaming resources */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300815 if (gspca_dev->capt_file == file) {
816 mutex_lock(&gspca_dev->usb_lock);
817 if (gspca_dev->streaming)
818 gspca_stream_off(gspca_dev);
819 gspca_dev->sd_desc->close(gspca_dev);
820 mutex_unlock(&gspca_dev->usb_lock);
821 frame_free(gspca_dev);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300822 gspca_dev->capt_file = NULL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300823 gspca_dev->memory = GSPCA_MEMORY_NO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300824 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300825 file->private_data = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300826 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300827 PDEBUG(D_STREAM, "close done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300828 return 0;
829}
830
831static int vidioc_querycap(struct file *file, void *priv,
832 struct v4l2_capability *cap)
833{
834 struct gspca_dev *gspca_dev = priv;
835
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300836 memset(cap, 0, sizeof *cap);
837 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -0300838/* strncpy(cap->card, gspca_dev->cam.dev_name, sizeof cap->card); */
839 if (gspca_dev->dev->product != NULL) {
840 strncpy(cap->card, gspca_dev->dev->product,
841 sizeof cap->card);
842 } else {
843 snprintf(cap->card, sizeof cap->card,
844 "USB Camera (%04x:%04x)",
845 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
846 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
847 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300848 strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name,
849 sizeof cap->bus_info);
850 cap->version = DRIVER_VERSION_NUMBER;
851 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
852 | V4L2_CAP_STREAMING
853 | V4L2_CAP_READWRITE;
Hans de Goede89a44b82008-09-03 16:48:16 -0300854 if (gspca_dev->flags & GSPCA_SENSOR_UPSIDE_DOWN_FLAG)
855 cap->capabilities |= V4L2_CAP_SENSOR_UPSIDE_DOWN;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300856 return 0;
857}
858
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300859/* the use of V4L2_CTRL_FLAG_NEXT_CTRL asks for the controls to be sorted */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300860static int vidioc_queryctrl(struct file *file, void *priv,
861 struct v4l2_queryctrl *q_ctrl)
862{
863 struct gspca_dev *gspca_dev = priv;
864 int i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300865 u32 id;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300866
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300867 id = q_ctrl->id;
868 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
869 id &= V4L2_CTRL_ID_MASK;
870 id++;
871 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
872 if (id >= gspca_dev->sd_desc->ctrls[i].qctrl.id) {
873 memcpy(q_ctrl,
874 &gspca_dev->sd_desc->ctrls[i].qctrl,
875 sizeof *q_ctrl);
876 return 0;
877 }
878 }
879 return -EINVAL;
880 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300881 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300882 if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300883 memcpy(q_ctrl,
884 &gspca_dev->sd_desc->ctrls[i].qctrl,
885 sizeof *q_ctrl);
886 return 0;
887 }
888 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300889 return -EINVAL;
890}
891
892static int vidioc_s_ctrl(struct file *file, void *priv,
893 struct v4l2_control *ctrl)
894{
895 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300896 const struct ctrl *ctrls;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300897 int i, ret;
898
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300899 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
900 i < gspca_dev->sd_desc->nctrls;
901 i++, ctrls++) {
902 if (ctrl->id != ctrls->qctrl.id)
903 continue;
904 if (ctrl->value < ctrls->qctrl.minimum
Rabin Vincentfcf5cb22008-08-03 07:58:54 -0300905 || ctrl->value > ctrls->qctrl.maximum)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300906 return -ERANGE;
907 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
Hans de Goedee2997a72008-04-23 08:09:12 -0300908 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
909 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300910 ret = ctrls->set(gspca_dev, ctrl->value);
911 mutex_unlock(&gspca_dev->usb_lock);
912 return ret;
913 }
914 return -EINVAL;
915}
916
917static int vidioc_g_ctrl(struct file *file, void *priv,
918 struct v4l2_control *ctrl)
919{
920 struct gspca_dev *gspca_dev = priv;
921
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300922 const struct ctrl *ctrls;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300923 int i, ret;
924
925 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
926 i < gspca_dev->sd_desc->nctrls;
927 i++, ctrls++) {
928 if (ctrl->id != ctrls->qctrl.id)
929 continue;
Hans de Goedee2997a72008-04-23 08:09:12 -0300930 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
931 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300932 ret = ctrls->get(gspca_dev, &ctrl->value);
933 mutex_unlock(&gspca_dev->usb_lock);
934 return ret;
935 }
936 return -EINVAL;
937}
938
939static int vidioc_querymenu(struct file *file, void *priv,
940 struct v4l2_querymenu *qmenu)
941{
942 struct gspca_dev *gspca_dev = priv;
943
944 if (!gspca_dev->sd_desc->querymenu)
945 return -EINVAL;
946 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
947}
948
949static int vidioc_enum_input(struct file *file, void *priv,
950 struct v4l2_input *input)
951{
952 struct gspca_dev *gspca_dev = priv;
953
954 if (input->index != 0)
955 return -EINVAL;
956 memset(input, 0, sizeof *input);
957 input->type = V4L2_INPUT_TYPE_CAMERA;
958 strncpy(input->name, gspca_dev->sd_desc->name,
959 sizeof input->name);
960 return 0;
961}
962
963static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
964{
965 *i = 0;
966 return 0;
967}
968
969static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
970{
971 if (i > 0)
972 return -EINVAL;
973 return (0);
974}
975
976static int vidioc_reqbufs(struct file *file, void *priv,
977 struct v4l2_requestbuffers *rb)
978{
979 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300980 int i, ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300981
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300982 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
983 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300984 switch (rb->memory) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -0300985 case GSPCA_MEMORY_READ: /* (internal call) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300986 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300987 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300988 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300989 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300990 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300991 }
Hans de Goedee2997a72008-04-23 08:09:12 -0300992 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
993 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300994
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -0300995 if (gspca_dev->memory != GSPCA_MEMORY_NO
996 && gspca_dev->memory != rb->memory) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300997 ret = -EBUSY;
998 goto out;
999 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001000
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001001 /* only one file may do the capture */
1002 if (gspca_dev->capt_file != NULL
1003 && gspca_dev->capt_file != file) {
1004 ret = -EBUSY;
1005 goto out;
1006 }
1007
1008 /* if allocated, the buffers must not be mapped */
1009 for (i = 0; i < gspca_dev->nframes; i++) {
1010 if (gspca_dev->frame[i].vma_use_count) {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001011 ret = -EBUSY;
1012 goto out;
1013 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001014 }
1015
1016 /* stop streaming */
1017 if (gspca_dev->streaming) {
1018 mutex_lock(&gspca_dev->usb_lock);
1019 gspca_stream_off(gspca_dev);
1020 mutex_unlock(&gspca_dev->usb_lock);
1021 }
1022
1023 /* free the previous allocated buffers, if any */
1024 if (gspca_dev->nframes != 0) {
1025 frame_free(gspca_dev);
1026 gspca_dev->capt_file = NULL;
1027 }
1028 if (rb->count == 0) /* unrequest */
1029 goto out;
1030 gspca_dev->memory = rb->memory;
1031 ret = frame_alloc(gspca_dev, rb->count);
1032 if (ret == 0) {
1033 rb->count = gspca_dev->nframes;
1034 gspca_dev->capt_file = file;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001035 }
1036out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001037 mutex_unlock(&gspca_dev->queue_lock);
1038 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1039 return ret;
1040}
1041
1042static int vidioc_querybuf(struct file *file, void *priv,
1043 struct v4l2_buffer *v4l2_buf)
1044{
1045 struct gspca_dev *gspca_dev = priv;
1046 struct gspca_frame *frame;
1047
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001048 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1049 || v4l2_buf->index < 0
1050 || v4l2_buf->index >= gspca_dev->nframes)
1051 return -EINVAL;
1052
1053 frame = &gspca_dev->frame[v4l2_buf->index];
1054 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1055 return 0;
1056}
1057
1058static int vidioc_streamon(struct file *file, void *priv,
1059 enum v4l2_buf_type buf_type)
1060{
1061 struct gspca_dev *gspca_dev = priv;
1062 int ret;
1063
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001064 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1065 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001066 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1067 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001068 if (!gspca_dev->present) {
1069 ret = -ENODEV;
1070 goto out;
1071 }
1072 if (gspca_dev->nframes == 0) {
1073 ret = -EINVAL;
1074 goto out;
1075 }
1076 if (!gspca_dev->streaming) {
1077 ret = gspca_init_transfer(gspca_dev);
1078 if (ret < 0)
1079 goto out;
1080 }
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001081#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001082 if (gspca_debug & D_STREAM) {
1083 PDEBUG_MODE("stream on OK",
1084 gspca_dev->pixfmt,
1085 gspca_dev->width,
1086 gspca_dev->height);
1087 }
1088#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001089 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001090out:
1091 mutex_unlock(&gspca_dev->queue_lock);
1092 return ret;
1093}
1094
1095static int vidioc_streamoff(struct file *file, void *priv,
1096 enum v4l2_buf_type buf_type)
1097{
1098 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001099 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001100
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001101 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1102 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001103 if (!gspca_dev->streaming)
1104 return 0;
1105 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1106 return -ERESTARTSYS;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001107
1108 /* stop streaming */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001109 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1110 ret = -ERESTARTSYS;
1111 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001112 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001113 gspca_stream_off(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001114 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001115
1116 /* empty the application queues */
1117 for (i = 0; i < gspca_dev->nframes; i++)
1118 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1119 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1120 gspca_dev->last_packet_type = DISCARD_PACKET;
1121 gspca_dev->sequence = 0;
1122 atomic_set(&gspca_dev->nevent, 0);
1123 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001124out:
1125 mutex_unlock(&gspca_dev->queue_lock);
1126 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001127}
1128
1129static int vidioc_g_jpegcomp(struct file *file, void *priv,
1130 struct v4l2_jpegcompression *jpegcomp)
1131{
1132 struct gspca_dev *gspca_dev = priv;
1133 int ret;
1134
1135 if (!gspca_dev->sd_desc->get_jcomp)
1136 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001137 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1138 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001139 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1140 mutex_unlock(&gspca_dev->usb_lock);
1141 return ret;
1142}
1143
1144static int vidioc_s_jpegcomp(struct file *file, void *priv,
1145 struct v4l2_jpegcompression *jpegcomp)
1146{
1147 struct gspca_dev *gspca_dev = priv;
1148 int ret;
1149
Hans de Goedee2997a72008-04-23 08:09:12 -03001150 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1151 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001152 if (!gspca_dev->sd_desc->set_jcomp)
1153 return -EINVAL;
1154 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1155 mutex_unlock(&gspca_dev->usb_lock);
1156 return ret;
1157}
1158
1159static int vidioc_g_parm(struct file *filp, void *priv,
1160 struct v4l2_streamparm *parm)
1161{
1162 struct gspca_dev *gspca_dev = priv;
1163
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001164 memset(parm, 0, sizeof *parm);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001165 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1166 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1167 return 0;
1168}
1169
1170static int vidioc_s_parm(struct file *filp, void *priv,
1171 struct v4l2_streamparm *parm)
1172{
1173 struct gspca_dev *gspca_dev = priv;
1174 int n;
1175
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001176 n = parm->parm.capture.readbuffers;
1177 if (n == 0 || n > GSPCA_MAX_FRAMES)
1178 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1179 else
1180 gspca_dev->nbufread = n;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001181 return 0;
1182}
1183
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001184static int vidioc_s_std(struct file *filp, void *priv,
1185 v4l2_std_id *parm)
1186{
1187 return 0;
1188}
1189
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001190#ifdef CONFIG_VIDEO_V4L1_COMPAT
1191static 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) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001199 int ret;
1200
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001201 {
1202 struct v4l2_format fmt;
1203
1204 memset(&fmt, 0, sizeof fmt);
1205 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1206 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1207 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1208 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1209 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001210 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001211 if (ret != 0)
1212 return ret;
1213 }
1214 {
1215 struct v4l2_requestbuffers rb;
1216
1217 memset(&rb, 0, sizeof rb);
1218 rb.count = 4;
1219 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1220 rb.memory = V4L2_MEMORY_MMAP;
1221 ret = vidioc_reqbufs(file, priv, &rb);
1222 if (ret != 0)
1223 return ret;
1224 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001225 }
1226 mbuf->frames = gspca_dev->nframes;
1227 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1228 for (i = 0; i < mbuf->frames; i++)
1229 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1230 return 0;
1231}
1232#endif
1233
1234static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1235{
1236 struct gspca_dev *gspca_dev = file->private_data;
Harvey Harrisonefab8212008-07-05 06:12:47 -03001237 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001238 struct page *page;
1239 unsigned long addr, start, size;
1240 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001241
1242 start = vma->vm_start;
1243 size = vma->vm_end - vma->vm_start;
1244 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1245
Hans de Goedee2997a72008-04-23 08:09:12 -03001246 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1247 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001248 if (!gspca_dev->present) {
1249 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001250 goto out;
1251 }
1252 if (gspca_dev->capt_file != file) {
1253 ret = -EINVAL;
1254 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001255 }
1256
Harvey Harrisonefab8212008-07-05 06:12:47 -03001257 frame = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001258 for (i = 0; i < gspca_dev->nframes; ++i) {
1259 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1260 PDEBUG(D_STREAM, "mmap bad memory type");
1261 break;
1262 }
1263 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1264 == vma->vm_pgoff) {
1265 frame = &gspca_dev->frame[i];
1266 break;
1267 }
1268 }
Harvey Harrisonefab8212008-07-05 06:12:47 -03001269 if (frame == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001270 PDEBUG(D_STREAM, "mmap no frame buffer found");
1271 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001272 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001273 }
1274#ifdef CONFIG_VIDEO_V4L1_COMPAT
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001275 /* v4l1 maps all the buffers */
1276 if (i != 0
1277 || size != frame->v4l2_buf.length * gspca_dev->nframes)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001278#endif
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001279 if (size != frame->v4l2_buf.length) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001280 PDEBUG(D_STREAM, "mmap bad size");
1281 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001282 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001283 }
1284
1285 /*
1286 * - VM_IO marks the area as being a mmaped region for I/O to a
1287 * device. It also prevents the region from being core dumped.
1288 */
1289 vma->vm_flags |= VM_IO;
1290
1291 addr = (unsigned long) frame->data;
1292 while (size > 0) {
1293 page = vmalloc_to_page((void *) addr);
1294 ret = vm_insert_page(vma, start, page);
1295 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001296 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001297 start += PAGE_SIZE;
1298 addr += PAGE_SIZE;
1299 size -= PAGE_SIZE;
1300 }
1301
1302 vma->vm_ops = &gspca_vm_ops;
1303 vma->vm_private_data = frame;
1304 gspca_vm_open(vma);
Hans de Goedee2997a72008-04-23 08:09:12 -03001305 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001306out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001307 mutex_unlock(&gspca_dev->queue_lock);
1308 return ret;
1309}
1310
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001311/*
1312 * wait for a video frame
1313 *
1314 * If a frame is ready, its index is returned.
1315 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001316static int frame_wait(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001317 int nonblock_ing)
1318{
1319 struct gspca_frame *frame;
1320 int i, j, ret;
1321
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001322 /* check if a frame is ready */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001323 i = gspca_dev->fr_o;
1324 j = gspca_dev->fr_queue[i];
1325 frame = &gspca_dev->frame[j];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001326 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) {
1327 atomic_dec(&gspca_dev->nevent);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001328 goto ok;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001329 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001330 if (nonblock_ing) /* no frame yet */
1331 return -EAGAIN;
1332
1333 /* wait till a frame is ready */
1334 for (;;) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001335 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1336 atomic_read(&gspca_dev->nevent) > 0,
1337 msecs_to_jiffies(3000));
1338 if (ret <= 0) {
1339 if (ret < 0)
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001340 return ret; /* interrupt */
1341 return -EIO; /* timeout */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001342 }
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001343 atomic_dec(&gspca_dev->nevent);
Hans de Goedee2997a72008-04-23 08:09:12 -03001344 if (!gspca_dev->streaming || !gspca_dev->present)
1345 return -EIO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001346 i = gspca_dev->fr_o;
1347 j = gspca_dev->fr_queue[i];
1348 frame = &gspca_dev->frame[j];
1349 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1350 break;
1351 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001352ok:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001353 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1354 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1355 gspca_dev->fr_q,
1356 gspca_dev->fr_i,
1357 gspca_dev->fr_o);
Hans de Goedee2997a72008-04-23 08:09:12 -03001358
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001359 if (gspca_dev->sd_desc->dq_callback) {
1360 mutex_lock(&gspca_dev->usb_lock);
Hans de Goedee2997a72008-04-23 08:09:12 -03001361 gspca_dev->sd_desc->dq_callback(gspca_dev);
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001362 mutex_unlock(&gspca_dev->usb_lock);
1363 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001364 return j;
1365}
1366
1367/*
1368 * dequeue a video buffer
1369 *
1370 * If nonblock_ing is false, block until a buffer is available.
1371 */
1372static int vidioc_dqbuf(struct file *file, void *priv,
1373 struct v4l2_buffer *v4l2_buf)
1374{
1375 struct gspca_dev *gspca_dev = priv;
1376 struct gspca_frame *frame;
1377 int i, ret;
1378
1379 PDEBUG(D_FRAM, "dqbuf");
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001380 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1381 return -EINVAL;
1382 if (v4l2_buf->memory != gspca_dev->memory)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001383 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001384
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001385 /* if not streaming, be sure the application will not loop forever */
1386 if (!(file->f_flags & O_NONBLOCK)
1387 && !gspca_dev->streaming && gspca_dev->users == 1)
1388 return -EINVAL;
1389
1390 /* only the capturing file may dequeue */
1391 if (gspca_dev->capt_file != file)
1392 return -EINVAL;
1393
1394 /* only one dequeue / read at a time */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001395 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1396 return -ERESTARTSYS;
1397
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001398 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001399 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001400 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001401 i = ret; /* frame index */
1402 frame = &gspca_dev->frame[i];
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001403 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001404 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001405 frame->data,
1406 frame->v4l2_buf.bytesused)) {
1407 PDEBUG(D_ERR|D_STREAM,
1408 "dqbuf cp to user failed");
1409 ret = -EFAULT;
1410 goto out;
1411 }
1412 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001413 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1414 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1415 PDEBUG(D_FRAM, "dqbuf %d", i);
1416 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001417out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001418 mutex_unlock(&gspca_dev->read_lock);
1419 return ret;
1420}
1421
1422/*
1423 * queue a video buffer
1424 *
1425 * Attempting to queue a buffer that has already been
1426 * queued will return -EINVAL.
1427 */
1428static int vidioc_qbuf(struct file *file, void *priv,
1429 struct v4l2_buffer *v4l2_buf)
1430{
1431 struct gspca_dev *gspca_dev = priv;
1432 struct gspca_frame *frame;
1433 int i, index, ret;
1434
1435 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1436 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1437 return -EINVAL;
1438
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001439 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1440 return -ERESTARTSYS;
1441
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001442 index = v4l2_buf->index;
1443 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001444 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001445 "qbuf idx %d >= %d", index, gspca_dev->nframes);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001446 ret = -EINVAL;
1447 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001448 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001449 if (v4l2_buf->memory != gspca_dev->memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001450 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001451 ret = -EINVAL;
1452 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001453 }
1454
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001455 frame = &gspca_dev->frame[index];
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001456 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1457 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001458 ret = -EINVAL;
1459 goto out;
1460 }
1461
1462 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001463/* frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001464
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001465 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001466 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1467 frame->v4l2_buf.length = v4l2_buf->length;
1468 }
1469
1470 /* put the buffer in the 'queued' queue */
1471 i = gspca_dev->fr_q;
1472 gspca_dev->fr_queue[i] = index;
1473 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1474 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1475 gspca_dev->fr_q,
1476 gspca_dev->fr_i,
1477 gspca_dev->fr_o);
1478
1479 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1480 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1481 ret = 0;
1482out:
1483 mutex_unlock(&gspca_dev->queue_lock);
1484 return ret;
1485}
1486
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001487/*
1488 * allocate the resources for read()
1489 */
1490static int read_alloc(struct gspca_dev *gspca_dev,
1491 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001492{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001493 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001494 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001495
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001496 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001497 if (gspca_dev->nframes == 0) {
1498 struct v4l2_requestbuffers rb;
1499
1500 memset(&rb, 0, sizeof rb);
1501 rb.count = gspca_dev->nbufread;
1502 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001503 rb.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001504 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1505 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001506 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001507 return ret;
1508 }
1509 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1510 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001511 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001512 for (i = 0; i < gspca_dev->nbufread; i++) {
1513 v4l2_buf.index = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001514 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1515 if (ret != 0) {
1516 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1517 return ret;
1518 }
1519 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001520 gspca_dev->memory = GSPCA_MEMORY_READ;
1521 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001522
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001523 /* start streaming */
1524 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1525 if (ret != 0)
1526 PDEBUG(D_STREAM, "read streamon err %d", ret);
1527 return ret;
1528}
1529
1530static unsigned int dev_poll(struct file *file, poll_table *wait)
1531{
1532 struct gspca_dev *gspca_dev = file->private_data;
1533 int i, ret;
1534
1535 PDEBUG(D_FRAM, "poll");
1536
1537 poll_wait(file, &gspca_dev->wq, wait);
1538 if (!gspca_dev->present)
1539 return POLLERR;
1540
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001541 /* if reqbufs is not done, the user would use read() */
1542 if (gspca_dev->nframes == 0) {
1543 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1544 return POLLERR; /* not the 1st time */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001545 ret = read_alloc(gspca_dev, file);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001546 if (ret != 0)
1547 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001548 }
1549
1550 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1551 return POLLERR;
1552 if (!gspca_dev->present) {
1553 ret = POLLERR;
1554 goto out;
1555 }
1556
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001557 /* check the next incoming buffer */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001558 i = gspca_dev->fr_o;
1559 i = gspca_dev->fr_queue[i];
1560 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1561 ret = POLLIN | POLLRDNORM; /* something to read */
1562 else
1563 ret = 0;
1564out:
1565 mutex_unlock(&gspca_dev->queue_lock);
1566 return ret;
1567}
1568
1569static ssize_t dev_read(struct file *file, char __user *data,
1570 size_t count, loff_t *ppos)
1571{
1572 struct gspca_dev *gspca_dev = file->private_data;
1573 struct gspca_frame *frame;
1574 struct v4l2_buffer v4l2_buf;
1575 struct timeval timestamp;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001576 int n, ret, ret2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001577
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001578 PDEBUG(D_FRAM, "read (%zd)", count);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001579 if (!gspca_dev->present)
1580 return -ENODEV;
1581 switch (gspca_dev->memory) {
1582 case GSPCA_MEMORY_NO: /* first time */
1583 ret = read_alloc(gspca_dev, file);
1584 if (ret != 0)
1585 return ret;
1586 break;
1587 case GSPCA_MEMORY_READ:
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001588 if (gspca_dev->capt_file == file)
1589 break;
1590 /* fall thru */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001591 default:
1592 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001593 }
1594
1595 /* get a frame */
1596 jiffies_to_timeval(get_jiffies_64(), &timestamp);
1597 timestamp.tv_sec--;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001598 n = 2;
1599 for (;;) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001600 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1601 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001602 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001603 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1604 if (ret != 0) {
1605 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1606 return ret;
1607 }
1608
1609 /* if the process slept for more than 1 second,
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001610 * get anewer frame */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001611 frame = &gspca_dev->frame[v4l2_buf.index];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001612 if (--n < 0)
1613 break; /* avoid infinite loop */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001614 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1615 break;
1616 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1617 if (ret != 0) {
1618 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1619 return ret;
1620 }
1621 }
1622
1623 /* copy the frame */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001624 if (count > frame->v4l2_buf.bytesused)
1625 count = frame->v4l2_buf.bytesused;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001626 ret = copy_to_user(data, frame->data, count);
1627 if (ret != 0) {
1628 PDEBUG(D_ERR|D_STREAM,
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001629 "read cp to user lack %d / %zd", ret, count);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001630 ret = -EFAULT;
1631 goto out;
1632 }
1633 ret = count;
1634out:
1635 /* in each case, requeue the buffer */
1636 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1637 if (ret2 != 0)
1638 return ret2;
1639 return ret;
1640}
1641
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001642static void dev_release(struct video_device *vfd)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001643{
1644 /* nothing */
1645}
1646
1647static struct file_operations dev_fops = {
1648 .owner = THIS_MODULE,
1649 .open = dev_open,
1650 .release = dev_close,
1651 .read = dev_read,
1652 .mmap = dev_mmap,
1653 .ioctl = video_ioctl2,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001654#ifdef CONFIG_COMPAT
1655 .compat_ioctl = v4l_compat_ioctl32,
1656#endif
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001657 .llseek = no_llseek,
1658 .poll = dev_poll,
1659};
1660
Hans Verkuila3998102008-07-21 02:57:38 -03001661static const struct v4l2_ioctl_ops dev_ioctl_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001662 .vidioc_querycap = vidioc_querycap,
1663 .vidioc_dqbuf = vidioc_dqbuf,
1664 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001665 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1666 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1667 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1668 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001669 .vidioc_streamon = vidioc_streamon,
1670 .vidioc_queryctrl = vidioc_queryctrl,
1671 .vidioc_g_ctrl = vidioc_g_ctrl,
1672 .vidioc_s_ctrl = vidioc_s_ctrl,
1673 .vidioc_querymenu = vidioc_querymenu,
1674 .vidioc_enum_input = vidioc_enum_input,
1675 .vidioc_g_input = vidioc_g_input,
1676 .vidioc_s_input = vidioc_s_input,
1677 .vidioc_reqbufs = vidioc_reqbufs,
1678 .vidioc_querybuf = vidioc_querybuf,
1679 .vidioc_streamoff = vidioc_streamoff,
1680 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1681 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1682 .vidioc_g_parm = vidioc_g_parm,
1683 .vidioc_s_parm = vidioc_s_parm,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001684 .vidioc_s_std = vidioc_s_std,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001685#ifdef CONFIG_VIDEO_V4L1_COMPAT
1686 .vidiocgmbuf = vidiocgmbuf,
1687#endif
1688};
1689
Hans Verkuila3998102008-07-21 02:57:38 -03001690static struct video_device gspca_template = {
1691 .name = "gspca main driver",
Hans Verkuila3998102008-07-21 02:57:38 -03001692 .fops = &dev_fops,
1693 .ioctl_ops = &dev_ioctl_ops,
1694 .release = dev_release, /* mandatory */
1695 .minor = -1,
1696};
1697
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001698/*
1699 * probe and create a new gspca device
1700 *
1701 * This function must be called by the sub-driver when it is
1702 * called for probing a new device.
1703 */
1704int gspca_dev_probe(struct usb_interface *intf,
1705 const struct usb_device_id *id,
1706 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001707 int dev_size,
1708 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001709{
1710 struct usb_interface_descriptor *interface;
1711 struct gspca_dev *gspca_dev;
1712 struct usb_device *dev = interface_to_usbdev(intf);
1713 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001714
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001715 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001716
1717 /* we don't handle multi-config cameras */
1718 if (dev->descriptor.bNumConfigurations != 1)
1719 return -ENODEV;
1720 interface = &intf->cur_altsetting->desc;
1721 if (interface->bInterfaceNumber > 0)
1722 return -ENODEV;
1723
1724 /* create the device */
1725 if (dev_size < sizeof *gspca_dev)
1726 dev_size = sizeof *gspca_dev;
1727 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
1728 if (gspca_dev == NULL) {
1729 err("couldn't kzalloc gspca struct");
1730 return -EIO;
1731 }
1732 gspca_dev->dev = dev;
1733 gspca_dev->iface = interface->bInterfaceNumber;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001734 gspca_dev->nbalt = intf->num_altsetting;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001735 gspca_dev->sd_desc = sd_desc;
1736/* gspca_dev->users = 0; (done by kzalloc) */
1737 gspca_dev->nbufread = 2;
1738
1739 /* configure the subdriver */
1740 ret = gspca_dev->sd_desc->config(gspca_dev, id);
1741 if (ret < 0)
1742 goto out;
1743 ret = gspca_set_alt0(gspca_dev);
1744 if (ret < 0)
1745 goto out;
1746 gspca_set_default_mode(gspca_dev);
1747
1748 mutex_init(&gspca_dev->usb_lock);
1749 mutex_init(&gspca_dev->read_lock);
1750 mutex_init(&gspca_dev->queue_lock);
1751 init_waitqueue_head(&gspca_dev->wq);
1752
1753 /* init video stuff */
1754 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
Hans Verkuil5e85e732008-07-20 06:31:39 -03001755 gspca_dev->vdev.parent = &dev->dev;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001756 memcpy(&gspca_dev->fops, &dev_fops, sizeof gspca_dev->fops);
1757 gspca_dev->vdev.fops = &gspca_dev->fops;
1758 gspca_dev->fops.owner = module; /* module protection */
Oliver Neukumee56a4d2008-07-27 14:01:59 -03001759 gspca_dev->present = 1;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001760 ret = video_register_device(&gspca_dev->vdev,
1761 VFL_TYPE_GRABBER,
1762 video_nr);
1763 if (ret < 0) {
1764 err("video_register_device err %d", ret);
1765 goto out;
1766 }
1767
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001768 usb_set_intfdata(intf, gspca_dev);
1769 PDEBUG(D_PROBE, "probe ok");
1770 return 0;
1771out:
1772 kfree(gspca_dev);
1773 return ret;
1774}
1775EXPORT_SYMBOL(gspca_dev_probe);
1776
1777/*
1778 * USB disconnection
1779 *
1780 * This function must be called by the sub-driver
1781 * when the device disconnects, after the specific resources are freed.
1782 */
1783void gspca_disconnect(struct usb_interface *intf)
1784{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001785 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001786
1787 if (!gspca_dev)
1788 return;
1789 gspca_dev->present = 0;
Hans de Goedee2997a72008-04-23 08:09:12 -03001790 mutex_lock(&gspca_dev->queue_lock);
1791 mutex_lock(&gspca_dev->usb_lock);
1792 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001793 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001794 mutex_unlock(&gspca_dev->usb_lock);
Hans de Goedee2997a72008-04-23 08:09:12 -03001795 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001796 while (gspca_dev->users != 0) { /* wait until fully closed */
1797 atomic_inc(&gspca_dev->nevent);
1798 wake_up_interruptible(&gspca_dev->wq); /* wake processes */
1799 schedule();
1800 }
1801/* We don't want people trying to open up the device */
1802 video_unregister_device(&gspca_dev->vdev);
1803/* Free the memory */
1804 kfree(gspca_dev);
1805 PDEBUG(D_PROBE, "disconnect complete");
1806}
1807EXPORT_SYMBOL(gspca_disconnect);
1808
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001809#ifdef CONFIG_PM
1810int gspca_suspend(struct usb_interface *intf, pm_message_t message)
1811{
1812 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1813
1814 if (!gspca_dev->streaming)
1815 return 0;
1816 gspca_dev->frozen = 1; /* avoid urb error messages */
1817 gspca_dev->sd_desc->stopN(gspca_dev);
1818 destroy_urbs(gspca_dev);
1819 gspca_set_alt0(gspca_dev);
1820 gspca_dev->sd_desc->stop0(gspca_dev);
1821 return 0;
1822}
1823EXPORT_SYMBOL(gspca_suspend);
1824
1825int gspca_resume(struct usb_interface *intf)
1826{
1827 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1828
1829 gspca_dev->frozen = 0;
1830 if (!gspca_dev->streaming)
1831 return 0;
1832 return gspca_init_transfer(gspca_dev);
1833}
1834EXPORT_SYMBOL(gspca_resume);
1835#endif
Hans de Goededcef3232008-07-10 10:40:53 -03001836/* -- cam driver utility functions -- */
1837
1838/* auto gain and exposure algorithm based on the knee algorithm described here:
1839 http://ytse.tricolour.net/docs/LowLightOptimization.html
1840
1841 Returns 0 if no changes were made, 1 if the gain and or exposure settings
1842 where changed. */
1843int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
1844 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
1845{
1846 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
1847 const struct ctrl *gain_ctrl = NULL;
1848 const struct ctrl *exposure_ctrl = NULL;
1849 const struct ctrl *autogain_ctrl = NULL;
1850 int retval = 0;
1851
1852 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1853 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
1854 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1855 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
1856 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
1857 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
1858 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1859 }
1860 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
1861 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
1862 "on cam without (auto)gain/exposure");
1863 return 0;
1864 }
1865
1866 if (gain_ctrl->get(gspca_dev, &gain) ||
1867 exposure_ctrl->get(gspca_dev, &exposure) ||
1868 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
1869 return 0;
1870
1871 orig_gain = gain;
1872 orig_exposure = exposure;
1873
1874 /* If we are of a multiple of deadzone, do multiple steps to reach the
1875 desired lumination fast (with the risc of a slight overshoot) */
1876 steps = abs(desired_avg_lum - avg_lum) / deadzone;
1877
1878 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d\n",
1879 avg_lum, desired_avg_lum, steps);
1880
1881 for (i = 0; i < steps; i++) {
1882 if (avg_lum > desired_avg_lum) {
1883 if (gain > gain_knee)
1884 gain--;
1885 else if (exposure > exposure_knee)
1886 exposure--;
1887 else if (gain > gain_ctrl->qctrl.default_value)
1888 gain--;
1889 else if (exposure > exposure_ctrl->qctrl.minimum)
1890 exposure--;
1891 else if (gain > gain_ctrl->qctrl.minimum)
1892 gain--;
1893 else
1894 break;
1895 } else {
1896 if (gain < gain_ctrl->qctrl.default_value)
1897 gain++;
1898 else if (exposure < exposure_knee)
1899 exposure++;
1900 else if (gain < gain_knee)
1901 gain++;
1902 else if (exposure < exposure_ctrl->qctrl.maximum)
1903 exposure++;
1904 else if (gain < gain_ctrl->qctrl.maximum)
1905 gain++;
1906 else
1907 break;
1908 }
1909 }
1910
1911 if (gain != orig_gain) {
1912 gain_ctrl->set(gspca_dev, gain);
1913 retval = 1;
1914 }
1915 if (exposure != orig_exposure) {
1916 exposure_ctrl->set(gspca_dev, exposure);
1917 retval = 1;
1918 }
1919
1920 return retval;
1921}
1922EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
1923
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001924/* -- module insert / remove -- */
1925static int __init gspca_init(void)
1926{
Jean-Francois Moinebb64e862008-07-19 06:49:28 -03001927 info("main v%d.%d.%d registered",
1928 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
1929 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
1930 DRIVER_VERSION_NUMBER & 0xff);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001931 return 0;
1932}
1933static void __exit gspca_exit(void)
1934{
1935 info("main deregistered");
1936}
1937
1938module_init(gspca_init);
1939module_exit(gspca_exit);
1940
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001941#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001942module_param_named(debug, gspca_debug, int, 0644);
1943MODULE_PARM_DESC(debug,
1944 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001945 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
1946 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001947#endif