blob: ac95c55887df4b38eb1cd8c4478538c53e042aef [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 Moine62ee8ee2008-09-03 17:12:13 -0300117#ifdef CONFIG_PM
Jean-Francois Moine6a709742008-09-03 16:48:10 -0300118 if (!gspca_dev->frozen)
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300119#endif
Jean-Francois Moine6a709742008-09-03 16:48:10 -0300120 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300121 return; /* disconnection ? */
122 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300123 pkt_scan = gspca_dev->sd_desc->pkt_scan;
124 for (i = 0; i < urb->number_of_packets; i++) {
125
126 /* check the availability of the frame buffer */
127 j = gspca_dev->fr_i;
128 j = gspca_dev->fr_queue[j];
129 frame = &gspca_dev->frame[j];
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300130 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300131 != V4L2_BUF_FLAG_QUEUED) {
132 gspca_dev->last_packet_type = DISCARD_PACKET;
133 break;
134 }
135
136 /* check the packet status and length */
137 len = urb->iso_frame_desc[i].actual_length;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300138 if (len == 0)
139 continue;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300140 st = urb->iso_frame_desc[i].status;
141 if (st) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300142 PDEBUG(D_ERR,
143 "ISOC data error: [%d] len=%d, status=%d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300144 i, len, st);
145 gspca_dev->last_packet_type = DISCARD_PACKET;
146 continue;
147 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300148
149 /* let the packet be analyzed by the subdriver */
150 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
151 i, urb->iso_frame_desc[i].offset, len);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300152 data = (__u8 *) urb->transfer_buffer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300153 + urb->iso_frame_desc[i].offset;
154 pkt_scan(gspca_dev, frame, data, len);
155 }
156
157 /* resubmit the URB */
158 urb->status = 0;
159 st = usb_submit_urb(urb, GFP_ATOMIC);
160 if (st < 0)
161 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
162}
163
164/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300165 * ISOC message interrupt from the USB device
166 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300167 * Analyse each packet and call the subdriver for copy to the frame buffer.
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300168 */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300169static void isoc_irq(struct urb *urb
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300170)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300171{
172 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
173
Hans de Goede3647fea2008-07-15 05:36:30 -0300174 PDEBUG(D_PACK, "isoc irq");
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300175 if (!gspca_dev->streaming)
176 return;
177 fill_frame(gspca_dev, urb);
178}
179
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300180/*
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300181 * add data to the current frame
182 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300183 * This function is called by the subdrivers at interrupt level.
184 *
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300185 * To build a frame, these ones must add
186 * - one FIRST_PACKET
187 * - 0 or many INTER_PACKETs
188 * - one LAST_PACKET
189 * DISCARD_PACKET invalidates the whole frame.
190 * On LAST_PACKET, a new frame is returned.
191 */
192struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
193 int packet_type,
194 struct gspca_frame *frame,
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300195 const __u8 *data,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300196 int len)
197{
198 int i, j;
199
Hans de Goedee2997a72008-04-23 08:09:12 -0300200 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300201
202 /* when start of a new frame, if the current frame buffer
203 * is not queued, discard the whole frame */
204 if (packet_type == FIRST_PACKET) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300205 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
206 != V4L2_BUF_FLAG_QUEUED) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300207 gspca_dev->last_packet_type = DISCARD_PACKET;
208 return frame;
209 }
210 frame->data_end = frame->data;
211 jiffies_to_timeval(get_jiffies_64(),
212 &frame->v4l2_buf.timestamp);
213 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300214 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
Jean-Francois Moine06ca78f2008-07-22 03:45:08 -0300215 if (packet_type == LAST_PACKET)
216 gspca_dev->last_packet_type = packet_type;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300217 return frame;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300218 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300219
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300220 /* append the packet to the frame buffer */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300221 if (len > 0) {
222 if (frame->data_end - frame->data + len
223 > frame->v4l2_buf.length) {
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -0300224 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300225 frame->data_end - frame->data + len,
226 frame->v4l2_buf.length);
227 packet_type = DISCARD_PACKET;
228 } else {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300229 memcpy(frame->data_end, data, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300230 frame->data_end += len;
231 }
232 }
233 gspca_dev->last_packet_type = packet_type;
234
235 /* if last packet, wake the application and advance in the queue */
236 if (packet_type == LAST_PACKET) {
237 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
238 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
239 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
240 atomic_inc(&gspca_dev->nevent);
241 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300242 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
243 gspca_dev->fr_i = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300244 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
245 frame->v4l2_buf.bytesused,
246 gspca_dev->fr_q,
247 i,
248 gspca_dev->fr_o);
249 j = gspca_dev->fr_queue[i];
250 frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300251 }
252 return frame;
253}
254EXPORT_SYMBOL(gspca_frame_add);
255
256static int gspca_is_compressed(__u32 format)
257{
258 switch (format) {
259 case V4L2_PIX_FMT_MJPEG:
260 case V4L2_PIX_FMT_JPEG:
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300261 case V4L2_PIX_FMT_SPCA561:
Hans de Goedeab8f12c2008-07-04 18:29:32 -0300262 case V4L2_PIX_FMT_PAC207:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300263 return 1;
264 }
265 return 0;
266}
267
268static void *rvmalloc(unsigned long size)
269{
270 void *mem;
271 unsigned long adr;
272
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300273/* size = PAGE_ALIGN(size); (already done) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300274 mem = vmalloc_32(size);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300275 if (mem != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300276 adr = (unsigned long) mem;
277 while ((long) size > 0) {
278 SetPageReserved(vmalloc_to_page((void *) adr));
279 adr += PAGE_SIZE;
280 size -= PAGE_SIZE;
281 }
282 }
283 return mem;
284}
285
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300286static void rvfree(void *mem, long size)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300287{
288 unsigned long adr;
289
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300290 adr = (unsigned long) mem;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300291 while (size > 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300292 ClearPageReserved(vmalloc_to_page((void *) adr));
293 adr += PAGE_SIZE;
294 size -= PAGE_SIZE;
295 }
296 vfree(mem);
297}
298
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300299static int frame_alloc(struct gspca_dev *gspca_dev,
300 unsigned int count)
301{
302 struct gspca_frame *frame;
303 unsigned int frsz;
304 int i;
305
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300306 i = gspca_dev->curr_mode;
307 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300308 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300309 frsz = PAGE_ALIGN(frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300310 gspca_dev->frsz = frsz;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300311 if (count > GSPCA_MAX_FRAMES)
312 count = GSPCA_MAX_FRAMES;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300313 gspca_dev->frbuf = rvmalloc(frsz * count);
314 if (!gspca_dev->frbuf) {
315 err("frame alloc failed");
316 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300317 }
318 gspca_dev->nframes = count;
319 for (i = 0; i < count; i++) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300320 frame = &gspca_dev->frame[i];
321 frame->v4l2_buf.index = i;
322 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
323 frame->v4l2_buf.flags = 0;
324 frame->v4l2_buf.field = V4L2_FIELD_NONE;
325 frame->v4l2_buf.length = frsz;
326 frame->v4l2_buf.memory = gspca_dev->memory;
327 frame->v4l2_buf.sequence = 0;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300328 frame->data = frame->data_end =
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300329 gspca_dev->frbuf + i * frsz;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300330 frame->v4l2_buf.m.offset = i * frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300331 }
332 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
333 gspca_dev->last_packet_type = DISCARD_PACKET;
334 gspca_dev->sequence = 0;
335 atomic_set(&gspca_dev->nevent, 0);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300336 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300337}
338
339static void frame_free(struct gspca_dev *gspca_dev)
340{
341 int i;
342
343 PDEBUG(D_STREAM, "frame free");
Harvey Harrisonefab8212008-07-05 06:12:47 -0300344 if (gspca_dev->frbuf != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300345 rvfree(gspca_dev->frbuf,
346 gspca_dev->nframes * gspca_dev->frsz);
347 gspca_dev->frbuf = NULL;
348 for (i = 0; i < gspca_dev->nframes; i++)
349 gspca_dev->frame[i].data = NULL;
350 }
351 gspca_dev->nframes = 0;
352}
353
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300354static void destroy_urbs(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300355{
356 struct urb *urb;
357 unsigned int i;
358
359 PDEBUG(D_STREAM, "kill transfer");
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300360 for (i = 0; i < MAX_NURBS; ++i) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300361 urb = gspca_dev->urb[i];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300362 if (urb == NULL)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300363 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300364
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300365 gspca_dev->urb[i] = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300366 usb_kill_urb(urb);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300367 if (urb->transfer_buffer != NULL)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300368 usb_buffer_free(gspca_dev->dev,
369 urb->transfer_buffer_length,
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300370 urb->transfer_buffer,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300371 urb->transfer_dma);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300372 usb_free_urb(urb);
373 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300374}
375
376/*
377 * search an input isochronous endpoint in an alternate setting
378 */
379static struct usb_host_endpoint *alt_isoc(struct usb_host_interface *alt,
380 __u8 epaddr)
381{
382 struct usb_host_endpoint *ep;
383 int i, attr;
384
385 epaddr |= USB_DIR_IN;
386 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
387 ep = &alt->endpoint[i];
388 if (ep->desc.bEndpointAddress == epaddr) {
389 attr = ep->desc.bmAttributes
390 & USB_ENDPOINT_XFERTYPE_MASK;
391 if (attr == USB_ENDPOINT_XFER_ISOC)
392 return ep;
393 break;
394 }
395 }
396 return NULL;
397}
398
399/*
400 * search an input isochronous endpoint
401 *
402 * The endpoint is defined by the subdriver.
403 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
404 * This routine may be called many times when the bandwidth is too small
405 * (the bandwidth is checked on urb submit).
406 */
Adrian Bunk903e10a2008-07-22 02:35:05 -0300407static struct usb_host_endpoint *get_isoc_ep(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300408{
409 struct usb_interface *intf;
410 struct usb_host_endpoint *ep;
411 int i, ret;
412
413 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300414 ep = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300415 i = gspca_dev->alt; /* previous alt setting */
416 while (--i > 0) { /* alt 0 is unusable */
417 ep = alt_isoc(&intf->altsetting[i], gspca_dev->cam.epaddr);
418 if (ep)
419 break;
420 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300421 if (ep == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300422 err("no ISOC endpoint found");
423 return NULL;
424 }
425 PDEBUG(D_STREAM, "use ISOC alt %d ep 0x%02x",
426 i, ep->desc.bEndpointAddress);
427 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
428 if (ret < 0) {
429 err("set interface err %d", ret);
430 return NULL;
431 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300432 gspca_dev->alt = i; /* memorize the current alt setting */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300433 return ep;
434}
435
436/*
437 * create the isochronous URBs
438 */
439static int create_urbs(struct gspca_dev *gspca_dev,
440 struct usb_host_endpoint *ep)
441{
442 struct urb *urb;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300443 int n, nurbs, i, psize, npkt, bsize;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300444
445 /* calculate the packet size and the number of packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300446 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300447
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300448 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
449 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
450 npkt = ISO_MAX_SIZE / psize;
451 if (npkt > ISO_MAX_PKT)
452 npkt = ISO_MAX_PKT;
453 bsize = psize * npkt;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300454 PDEBUG(D_STREAM,
455 "isoc %d pkts size %d (bsize:%d)", npkt, psize, bsize);
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300456 nurbs = DEF_NURBS;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300457 gspca_dev->nurbs = nurbs;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300458 for (n = 0; n < nurbs; n++) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300459 urb = usb_alloc_urb(npkt, GFP_KERNEL);
460 if (!urb) {
461 err("usb_alloc_urb failed");
Jean-Francois Moine0d0548e2008-09-05 12:49:54 -0300462 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300463 return -ENOMEM;
464 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300465 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300466 bsize,
467 GFP_KERNEL,
468 &urb->transfer_dma);
469
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300470 if (urb->transfer_buffer == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300471 usb_free_urb(urb);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300472 err("usb_buffer_urb failed");
Jean-Francois Moine0d0548e2008-09-05 12:49:54 -0300473 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300474 return -ENOMEM;
475 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300476 gspca_dev->urb[n] = urb;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300477 urb->dev = gspca_dev->dev;
478 urb->context = gspca_dev;
479 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
480 ep->desc.bEndpointAddress);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300481 urb->transfer_flags = URB_ISO_ASAP
482 | URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300483 urb->interval = ep->desc.bInterval;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300484 urb->complete = isoc_irq;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300485 urb->number_of_packets = npkt;
486 urb->transfer_buffer_length = bsize;
487 for (i = 0; i < npkt; i++) {
488 urb->iso_frame_desc[i].length = psize;
489 urb->iso_frame_desc[i].offset = psize * i;
490 }
491 }
492 return 0;
493}
494
495/*
496 * start the USB transfer
497 */
498static int gspca_init_transfer(struct gspca_dev *gspca_dev)
499{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300500 struct usb_host_endpoint *ep;
501 int n, ret;
502
Hans de Goedee2997a72008-04-23 08:09:12 -0300503 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
504 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300505
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300506 /* set the higher alternate setting and
507 * loop until urb submit succeeds */
508 gspca_dev->alt = gspca_dev->nbalt;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300509 for (;;) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300510 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300511 ep = get_isoc_ep(gspca_dev);
512 if (ep == NULL) {
513 ret = -EIO;
514 goto out;
515 }
516 ret = create_urbs(gspca_dev, ep);
517 if (ret < 0)
518 goto out;
519
520 /* start the cam */
521 gspca_dev->sd_desc->start(gspca_dev);
522 gspca_dev->streaming = 1;
523 atomic_set(&gspca_dev->nevent, 0);
524
525 /* submit the URBs */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300526 for (n = 0; n < gspca_dev->nurbs; n++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300527 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300528 if (ret < 0) {
529 PDEBUG(D_ERR|D_STREAM,
530 "usb_submit_urb [%d] err %d", n, ret);
Hans de Goedee2997a72008-04-23 08:09:12 -0300531 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300532 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300533 if (ret == -ENOSPC)
534 break; /* try the previous alt */
535 goto out;
536 }
537 }
538 if (ret >= 0)
539 break;
540 }
541out:
542 mutex_unlock(&gspca_dev->usb_lock);
543 return ret;
544}
545
546static int gspca_set_alt0(struct gspca_dev *gspca_dev)
547{
548 int ret;
549
550 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
551 if (ret < 0)
552 PDEBUG(D_ERR|D_STREAM, "set interface 0 err %d", ret);
553 return ret;
554}
555
Hans de Goedee2997a72008-04-23 08:09:12 -0300556/* Note both the queue and the usb lock should be hold when calling this */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300557static void gspca_stream_off(struct gspca_dev *gspca_dev)
558{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300559 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300560 atomic_set(&gspca_dev->nevent, 0);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300561 if (gspca_dev->present) {
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300562 if (gspca_dev->sd_desc->stopN)
563 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300564 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300565 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300566 if (gspca_dev->sd_desc->stop0)
567 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300568 PDEBUG(D_STREAM, "stream off OK");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300569 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300570}
571
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300572static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300573{
574 int i;
575
576 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
577 gspca_dev->curr_mode = i;
578 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
579 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300580 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300581}
582
583static int wxh_to_mode(struct gspca_dev *gspca_dev,
584 int width, int height)
585{
586 int i;
587
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300588 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
589 if (width >= gspca_dev->cam.cam_mode[i].width
590 && height >= gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300591 break;
592 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300593 return i;
594}
595
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300596/*
597 * search a mode with the right pixel format
598 */
599static int gspca_get_mode(struct gspca_dev *gspca_dev,
600 int mode,
601 int pixfmt)
602{
603 int modeU, modeD;
604
605 modeU = modeD = mode;
606 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
607 if (--modeD >= 0) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300608 if (gspca_dev->cam.cam_mode[modeD].pixelformat
609 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300610 return modeD;
611 }
612 if (++modeU < gspca_dev->cam.nmodes) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300613 if (gspca_dev->cam.cam_mode[modeU].pixelformat
614 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300615 return modeU;
616 }
617 }
618 return -EINVAL;
619}
620
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300621static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300622 struct v4l2_fmtdesc *fmtdesc)
623{
624 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300625 int i, j, index;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300626 __u32 fmt_tb[8];
627
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300628 /* give an index to each format */
629 index = 0;
630 j = 0;
631 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300632 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300633 j = 0;
634 for (;;) {
635 if (fmt_tb[j] == fmt_tb[index])
636 break;
637 j++;
638 }
639 if (j == index) {
640 if (fmtdesc->index == index)
641 break; /* new format */
642 index++;
643 if (index >= sizeof fmt_tb / sizeof fmt_tb[0])
644 return -EINVAL;
645 }
646 }
647 if (i < 0)
648 return -EINVAL; /* no more format */
649
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300650 fmtdesc->pixelformat = fmt_tb[index];
651 if (gspca_is_compressed(fmt_tb[index]))
652 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300653 fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300654 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
655 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
656 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
657 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
658 fmtdesc->description[4] = '\0';
659 return 0;
660}
661
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300662static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300663 struct v4l2_format *fmt)
664{
665 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300666 int mode;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300667
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300668 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
669 return -EINVAL;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300670 mode = gspca_dev->curr_mode;
671 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
672 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300673 return 0;
674}
675
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300676static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300677 struct v4l2_format *fmt)
678{
Hans de Goede24c530b2008-07-04 17:55:18 -0300679 int w, h, mode, mode2;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300680
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300681 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
682 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300683 w = fmt->fmt.pix.width;
684 h = fmt->fmt.pix.height;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300685
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300686#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300687 if (gspca_debug & D_CONF)
688 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
689#endif
690 /* search the closest mode for width and height */
691 mode = wxh_to_mode(gspca_dev, w, h);
692
693 /* OK if right palette */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300694 if (gspca_dev->cam.cam_mode[mode].pixelformat
695 != fmt->fmt.pix.pixelformat) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300696
697 /* else, search the closest mode with the same pixel format */
698 mode2 = gspca_get_mode(gspca_dev, mode,
699 fmt->fmt.pix.pixelformat);
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300700 if (mode2 >= 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300701 mode = mode2;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300702/* else
703 ; * no chance, return this mode */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300704 }
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300705 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
706 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300707 return mode; /* used when s_fmt */
708}
709
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300710static int vidioc_try_fmt_vid_cap(struct file *file,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300711 void *priv,
712 struct v4l2_format *fmt)
713{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300714 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300715 int ret;
716
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300717 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300718 if (ret < 0)
719 return ret;
720 return 0;
721}
722
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300723static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300724 struct v4l2_format *fmt)
725{
726 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300727 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300728
Hans de Goedee2997a72008-04-23 08:09:12 -0300729 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
730 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300731
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300732 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300733 if (ret < 0)
734 goto out;
735
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300736 if (gspca_dev->nframes != 0
737 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
738 ret = -EINVAL;
739 goto out;
740 }
741
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300742 if (ret == gspca_dev->curr_mode) {
743 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300744 goto out; /* same mode */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300745 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300746
747 if (gspca_dev->streaming) {
748 ret = -EBUSY;
749 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -0300750 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300751 gspca_dev->width = fmt->fmt.pix.width;
752 gspca_dev->height = fmt->fmt.pix.height;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300753 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
754 gspca_dev->curr_mode = ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300755
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300756 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300757out:
758 mutex_unlock(&gspca_dev->queue_lock);
759 return ret;
760}
761
762static int dev_open(struct inode *inode, struct file *file)
763{
764 struct gspca_dev *gspca_dev;
765 int ret;
766
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300767 PDEBUG(D_STREAM, "%s open", current->comm);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300768 gspca_dev = (struct gspca_dev *) video_devdata(file);
Hans de Goedee2997a72008-04-23 08:09:12 -0300769 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
770 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300771 if (!gspca_dev->present) {
772 ret = -ENODEV;
773 goto out;
774 }
775
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300776 if (gspca_dev->users > 4) { /* (arbitrary value) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300777 ret = -EBUSY;
778 goto out;
779 }
780 gspca_dev->users++;
781 file->private_data = gspca_dev;
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300782#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300783 /* activate the v4l2 debug */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300784 if (gspca_debug & D_V4L2)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300785 gspca_dev->vdev.debug |= 3;
786 else
787 gspca_dev->vdev.debug &= ~3;
788#endif
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300789 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300790out:
791 mutex_unlock(&gspca_dev->queue_lock);
792 if (ret != 0)
793 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
794 else
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300795 PDEBUG(D_STREAM, "open done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300796 return ret;
797}
798
799static int dev_close(struct inode *inode, struct file *file)
800{
801 struct gspca_dev *gspca_dev = file->private_data;
802
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300803 PDEBUG(D_STREAM, "%s close", current->comm);
Hans de Goedee2997a72008-04-23 08:09:12 -0300804 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
805 return -ERESTARTSYS;
806 gspca_dev->users--;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300807
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -0300808 /* if the file did the capture, free the streaming resources */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300809 if (gspca_dev->capt_file == file) {
Jean-Francois Moinea1e82882008-09-03 17:12:17 -0300810 if (gspca_dev->streaming) {
811 mutex_lock(&gspca_dev->usb_lock);
812 gspca_stream_off(gspca_dev);
813 mutex_unlock(&gspca_dev->usb_lock);
814 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300815 frame_free(gspca_dev);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300816 gspca_dev->capt_file = NULL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300817 gspca_dev->memory = GSPCA_MEMORY_NO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300818 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300819 file->private_data = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300820 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300821 PDEBUG(D_STREAM, "close done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300822 return 0;
823}
824
825static int vidioc_querycap(struct file *file, void *priv,
826 struct v4l2_capability *cap)
827{
828 struct gspca_dev *gspca_dev = priv;
829
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300830 memset(cap, 0, sizeof *cap);
831 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -0300832/* strncpy(cap->card, gspca_dev->cam.dev_name, sizeof cap->card); */
833 if (gspca_dev->dev->product != NULL) {
834 strncpy(cap->card, gspca_dev->dev->product,
835 sizeof cap->card);
836 } else {
837 snprintf(cap->card, sizeof cap->card,
838 "USB Camera (%04x:%04x)",
839 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
840 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
841 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300842 strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name,
843 sizeof cap->bus_info);
844 cap->version = DRIVER_VERSION_NUMBER;
845 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
846 | V4L2_CAP_STREAMING
847 | V4L2_CAP_READWRITE;
848 return 0;
849}
850
851static int vidioc_queryctrl(struct file *file, void *priv,
852 struct v4l2_queryctrl *q_ctrl)
853{
854 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300855 int i, ix;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300856 u32 id;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300857
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300858 ix = -1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300859 id = q_ctrl->id;
860 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
861 id &= V4L2_CTRL_ID_MASK;
862 id++;
863 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goede0f523c22008-09-03 17:12:14 -0300864 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300865 continue;
866 if (ix < 0) {
867 ix = i;
868 continue;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300869 }
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300870 if (gspca_dev->sd_desc->ctrls[i].qctrl.id
871 > gspca_dev->sd_desc->ctrls[ix].qctrl.id)
872 continue;
873 ix = i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300874 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300875 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300876 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300877 if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) {
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300878 ix = i;
879 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300880 }
881 }
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300882 if (ix < 0)
883 return -EINVAL;
884 memcpy(q_ctrl, &gspca_dev->sd_desc->ctrls[ix].qctrl,
885 sizeof *q_ctrl);
886 if (gspca_dev->ctrl_dis & (1 << ix))
887 q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
888 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300889}
890
891static int vidioc_s_ctrl(struct file *file, void *priv,
892 struct v4l2_control *ctrl)
893{
894 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300895 const struct ctrl *ctrls;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300896 int i, ret;
897
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300898 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
899 i < gspca_dev->sd_desc->nctrls;
900 i++, ctrls++) {
901 if (ctrl->id != ctrls->qctrl.id)
902 continue;
Hans de Goede0f523c22008-09-03 17:12:14 -0300903 if (gspca_dev->ctrl_dis & (1 << i))
904 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300905 if (ctrl->value < ctrls->qctrl.minimum
Rabin Vincentfcf5cb22008-08-03 07:58:54 -0300906 || ctrl->value > ctrls->qctrl.maximum)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300907 return -ERANGE;
908 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
Hans de Goedee2997a72008-04-23 08:09:12 -0300909 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
910 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300911 ret = ctrls->set(gspca_dev, ctrl->value);
912 mutex_unlock(&gspca_dev->usb_lock);
913 return ret;
914 }
915 return -EINVAL;
916}
917
918static int vidioc_g_ctrl(struct file *file, void *priv,
919 struct v4l2_control *ctrl)
920{
921 struct gspca_dev *gspca_dev = priv;
922
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300923 const struct ctrl *ctrls;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300924 int i, ret;
925
926 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
927 i < gspca_dev->sd_desc->nctrls;
928 i++, ctrls++) {
929 if (ctrl->id != ctrls->qctrl.id)
930 continue;
Hans de Goede0f523c22008-09-03 17:12:14 -0300931 if (gspca_dev->ctrl_dis & (1 << i))
932 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -0300933 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
934 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300935 ret = ctrls->get(gspca_dev, &ctrl->value);
936 mutex_unlock(&gspca_dev->usb_lock);
937 return ret;
938 }
939 return -EINVAL;
940}
941
942static int vidioc_querymenu(struct file *file, void *priv,
943 struct v4l2_querymenu *qmenu)
944{
945 struct gspca_dev *gspca_dev = priv;
946
947 if (!gspca_dev->sd_desc->querymenu)
948 return -EINVAL;
949 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
950}
951
952static int vidioc_enum_input(struct file *file, void *priv,
953 struct v4l2_input *input)
954{
955 struct gspca_dev *gspca_dev = priv;
956
957 if (input->index != 0)
958 return -EINVAL;
959 memset(input, 0, sizeof *input);
960 input->type = V4L2_INPUT_TYPE_CAMERA;
961 strncpy(input->name, gspca_dev->sd_desc->name,
962 sizeof input->name);
963 return 0;
964}
965
966static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
967{
968 *i = 0;
969 return 0;
970}
971
972static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
973{
974 if (i > 0)
975 return -EINVAL;
976 return (0);
977}
978
979static int vidioc_reqbufs(struct file *file, void *priv,
980 struct v4l2_requestbuffers *rb)
981{
982 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300983 int i, ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300984
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300985 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
986 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300987 switch (rb->memory) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -0300988 case GSPCA_MEMORY_READ: /* (internal call) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300989 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300990 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300991 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300992 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300993 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300994 }
Hans de Goedee2997a72008-04-23 08:09:12 -0300995 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
996 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300997
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -0300998 if (gspca_dev->memory != GSPCA_MEMORY_NO
999 && gspca_dev->memory != rb->memory) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001000 ret = -EBUSY;
1001 goto out;
1002 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001003
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001004 /* only one file may do the capture */
1005 if (gspca_dev->capt_file != NULL
1006 && gspca_dev->capt_file != file) {
1007 ret = -EBUSY;
1008 goto out;
1009 }
1010
1011 /* if allocated, the buffers must not be mapped */
1012 for (i = 0; i < gspca_dev->nframes; i++) {
1013 if (gspca_dev->frame[i].vma_use_count) {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001014 ret = -EBUSY;
1015 goto out;
1016 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001017 }
1018
1019 /* stop streaming */
1020 if (gspca_dev->streaming) {
1021 mutex_lock(&gspca_dev->usb_lock);
1022 gspca_stream_off(gspca_dev);
1023 mutex_unlock(&gspca_dev->usb_lock);
1024 }
1025
1026 /* free the previous allocated buffers, if any */
1027 if (gspca_dev->nframes != 0) {
1028 frame_free(gspca_dev);
1029 gspca_dev->capt_file = NULL;
1030 }
1031 if (rb->count == 0) /* unrequest */
1032 goto out;
1033 gspca_dev->memory = rb->memory;
1034 ret = frame_alloc(gspca_dev, rb->count);
1035 if (ret == 0) {
1036 rb->count = gspca_dev->nframes;
1037 gspca_dev->capt_file = file;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001038 }
1039out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001040 mutex_unlock(&gspca_dev->queue_lock);
1041 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1042 return ret;
1043}
1044
1045static int vidioc_querybuf(struct file *file, void *priv,
1046 struct v4l2_buffer *v4l2_buf)
1047{
1048 struct gspca_dev *gspca_dev = priv;
1049 struct gspca_frame *frame;
1050
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001051 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1052 || v4l2_buf->index < 0
1053 || v4l2_buf->index >= gspca_dev->nframes)
1054 return -EINVAL;
1055
1056 frame = &gspca_dev->frame[v4l2_buf->index];
1057 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1058 return 0;
1059}
1060
1061static int vidioc_streamon(struct file *file, void *priv,
1062 enum v4l2_buf_type buf_type)
1063{
1064 struct gspca_dev *gspca_dev = priv;
1065 int ret;
1066
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001067 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1068 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001069 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1070 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001071 if (!gspca_dev->present) {
1072 ret = -ENODEV;
1073 goto out;
1074 }
1075 if (gspca_dev->nframes == 0) {
1076 ret = -EINVAL;
1077 goto out;
1078 }
1079 if (!gspca_dev->streaming) {
1080 ret = gspca_init_transfer(gspca_dev);
1081 if (ret < 0)
1082 goto out;
1083 }
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001084#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001085 if (gspca_debug & D_STREAM) {
1086 PDEBUG_MODE("stream on OK",
1087 gspca_dev->pixfmt,
1088 gspca_dev->width,
1089 gspca_dev->height);
1090 }
1091#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001092 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001093out:
1094 mutex_unlock(&gspca_dev->queue_lock);
1095 return ret;
1096}
1097
1098static int vidioc_streamoff(struct file *file, void *priv,
1099 enum v4l2_buf_type buf_type)
1100{
1101 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001102 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001103
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001104 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1105 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001106 if (!gspca_dev->streaming)
1107 return 0;
1108 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1109 return -ERESTARTSYS;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001110
1111 /* stop streaming */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001112 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1113 ret = -ERESTARTSYS;
1114 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001115 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001116 gspca_stream_off(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001117 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001118
1119 /* empty the application queues */
1120 for (i = 0; i < gspca_dev->nframes; i++)
1121 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1122 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1123 gspca_dev->last_packet_type = DISCARD_PACKET;
1124 gspca_dev->sequence = 0;
1125 atomic_set(&gspca_dev->nevent, 0);
1126 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001127out:
1128 mutex_unlock(&gspca_dev->queue_lock);
1129 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001130}
1131
1132static int vidioc_g_jpegcomp(struct file *file, void *priv,
1133 struct v4l2_jpegcompression *jpegcomp)
1134{
1135 struct gspca_dev *gspca_dev = priv;
1136 int ret;
1137
1138 if (!gspca_dev->sd_desc->get_jcomp)
1139 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001140 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1141 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001142 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1143 mutex_unlock(&gspca_dev->usb_lock);
1144 return ret;
1145}
1146
1147static int vidioc_s_jpegcomp(struct file *file, void *priv,
1148 struct v4l2_jpegcompression *jpegcomp)
1149{
1150 struct gspca_dev *gspca_dev = priv;
1151 int ret;
1152
Hans de Goedee2997a72008-04-23 08:09:12 -03001153 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1154 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001155 if (!gspca_dev->sd_desc->set_jcomp)
1156 return -EINVAL;
1157 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1158 mutex_unlock(&gspca_dev->usb_lock);
1159 return ret;
1160}
1161
1162static int vidioc_g_parm(struct file *filp, void *priv,
1163 struct v4l2_streamparm *parm)
1164{
1165 struct gspca_dev *gspca_dev = priv;
1166
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001167 memset(parm, 0, sizeof *parm);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001168 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1169 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1170 return 0;
1171}
1172
1173static int vidioc_s_parm(struct file *filp, void *priv,
1174 struct v4l2_streamparm *parm)
1175{
1176 struct gspca_dev *gspca_dev = priv;
1177 int n;
1178
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001179 n = parm->parm.capture.readbuffers;
1180 if (n == 0 || n > GSPCA_MAX_FRAMES)
1181 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1182 else
1183 gspca_dev->nbufread = n;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001184 return 0;
1185}
1186
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001187static int vidioc_s_std(struct file *filp, void *priv,
1188 v4l2_std_id *parm)
1189{
1190 return 0;
1191}
1192
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001193#ifdef CONFIG_VIDEO_V4L1_COMPAT
1194static int vidiocgmbuf(struct file *file, void *priv,
1195 struct video_mbuf *mbuf)
1196{
1197 struct gspca_dev *gspca_dev = file->private_data;
1198 int i;
1199
1200 PDEBUG(D_STREAM, "cgmbuf");
1201 if (gspca_dev->nframes == 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001202 int ret;
1203
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001204 {
1205 struct v4l2_format fmt;
1206
1207 memset(&fmt, 0, sizeof fmt);
1208 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1209 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1210 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1211 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1212 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001213 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001214 if (ret != 0)
1215 return ret;
1216 }
1217 {
1218 struct v4l2_requestbuffers rb;
1219
1220 memset(&rb, 0, sizeof rb);
1221 rb.count = 4;
1222 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1223 rb.memory = V4L2_MEMORY_MMAP;
1224 ret = vidioc_reqbufs(file, priv, &rb);
1225 if (ret != 0)
1226 return ret;
1227 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001228 }
1229 mbuf->frames = gspca_dev->nframes;
1230 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1231 for (i = 0; i < mbuf->frames; i++)
1232 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1233 return 0;
1234}
1235#endif
1236
1237static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1238{
1239 struct gspca_dev *gspca_dev = file->private_data;
Harvey Harrisonefab8212008-07-05 06:12:47 -03001240 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001241 struct page *page;
1242 unsigned long addr, start, size;
1243 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001244
1245 start = vma->vm_start;
1246 size = vma->vm_end - vma->vm_start;
1247 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1248
Hans de Goedee2997a72008-04-23 08:09:12 -03001249 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1250 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001251 if (!gspca_dev->present) {
1252 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001253 goto out;
1254 }
1255 if (gspca_dev->capt_file != file) {
1256 ret = -EINVAL;
1257 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001258 }
1259
Harvey Harrisonefab8212008-07-05 06:12:47 -03001260 frame = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001261 for (i = 0; i < gspca_dev->nframes; ++i) {
1262 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1263 PDEBUG(D_STREAM, "mmap bad memory type");
1264 break;
1265 }
1266 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1267 == vma->vm_pgoff) {
1268 frame = &gspca_dev->frame[i];
1269 break;
1270 }
1271 }
Harvey Harrisonefab8212008-07-05 06:12:47 -03001272 if (frame == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001273 PDEBUG(D_STREAM, "mmap no frame buffer found");
1274 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001275 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001276 }
1277#ifdef CONFIG_VIDEO_V4L1_COMPAT
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001278 /* v4l1 maps all the buffers */
1279 if (i != 0
1280 || size != frame->v4l2_buf.length * gspca_dev->nframes)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001281#endif
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001282 if (size != frame->v4l2_buf.length) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001283 PDEBUG(D_STREAM, "mmap bad size");
1284 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001285 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001286 }
1287
1288 /*
1289 * - VM_IO marks the area as being a mmaped region for I/O to a
1290 * device. It also prevents the region from being core dumped.
1291 */
1292 vma->vm_flags |= VM_IO;
1293
1294 addr = (unsigned long) frame->data;
1295 while (size > 0) {
1296 page = vmalloc_to_page((void *) addr);
1297 ret = vm_insert_page(vma, start, page);
1298 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001299 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001300 start += PAGE_SIZE;
1301 addr += PAGE_SIZE;
1302 size -= PAGE_SIZE;
1303 }
1304
1305 vma->vm_ops = &gspca_vm_ops;
1306 vma->vm_private_data = frame;
1307 gspca_vm_open(vma);
Hans de Goedee2997a72008-04-23 08:09:12 -03001308 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001309out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001310 mutex_unlock(&gspca_dev->queue_lock);
1311 return ret;
1312}
1313
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001314/*
1315 * wait for a video frame
1316 *
1317 * If a frame is ready, its index is returned.
1318 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001319static int frame_wait(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001320 int nonblock_ing)
1321{
1322 struct gspca_frame *frame;
1323 int i, j, ret;
1324
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001325 /* check if a frame is ready */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001326 i = gspca_dev->fr_o;
1327 j = gspca_dev->fr_queue[i];
1328 frame = &gspca_dev->frame[j];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001329 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) {
1330 atomic_dec(&gspca_dev->nevent);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001331 goto ok;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001332 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001333 if (nonblock_ing) /* no frame yet */
1334 return -EAGAIN;
1335
1336 /* wait till a frame is ready */
1337 for (;;) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001338 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1339 atomic_read(&gspca_dev->nevent) > 0,
1340 msecs_to_jiffies(3000));
1341 if (ret <= 0) {
1342 if (ret < 0)
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001343 return ret; /* interrupt */
1344 return -EIO; /* timeout */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001345 }
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001346 atomic_dec(&gspca_dev->nevent);
Hans de Goedee2997a72008-04-23 08:09:12 -03001347 if (!gspca_dev->streaming || !gspca_dev->present)
1348 return -EIO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001349 i = gspca_dev->fr_o;
1350 j = gspca_dev->fr_queue[i];
1351 frame = &gspca_dev->frame[j];
1352 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1353 break;
1354 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001355ok:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001356 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1357 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1358 gspca_dev->fr_q,
1359 gspca_dev->fr_i,
1360 gspca_dev->fr_o);
Hans de Goedee2997a72008-04-23 08:09:12 -03001361
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001362 if (gspca_dev->sd_desc->dq_callback) {
1363 mutex_lock(&gspca_dev->usb_lock);
Hans de Goedee2997a72008-04-23 08:09:12 -03001364 gspca_dev->sd_desc->dq_callback(gspca_dev);
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001365 mutex_unlock(&gspca_dev->usb_lock);
1366 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001367 return j;
1368}
1369
1370/*
1371 * dequeue a video buffer
1372 *
1373 * If nonblock_ing is false, block until a buffer is available.
1374 */
1375static int vidioc_dqbuf(struct file *file, void *priv,
1376 struct v4l2_buffer *v4l2_buf)
1377{
1378 struct gspca_dev *gspca_dev = priv;
1379 struct gspca_frame *frame;
1380 int i, ret;
1381
1382 PDEBUG(D_FRAM, "dqbuf");
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001383 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1384 return -EINVAL;
1385 if (v4l2_buf->memory != gspca_dev->memory)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001386 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001387
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001388 /* if not streaming, be sure the application will not loop forever */
1389 if (!(file->f_flags & O_NONBLOCK)
1390 && !gspca_dev->streaming && gspca_dev->users == 1)
1391 return -EINVAL;
1392
1393 /* only the capturing file may dequeue */
1394 if (gspca_dev->capt_file != file)
1395 return -EINVAL;
1396
1397 /* only one dequeue / read at a time */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001398 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1399 return -ERESTARTSYS;
1400
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001401 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001402 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001403 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001404 i = ret; /* frame index */
1405 frame = &gspca_dev->frame[i];
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001406 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001407 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001408 frame->data,
1409 frame->v4l2_buf.bytesused)) {
1410 PDEBUG(D_ERR|D_STREAM,
1411 "dqbuf cp to user failed");
1412 ret = -EFAULT;
1413 goto out;
1414 }
1415 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001416 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1417 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1418 PDEBUG(D_FRAM, "dqbuf %d", i);
1419 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001420out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001421 mutex_unlock(&gspca_dev->read_lock);
1422 return ret;
1423}
1424
1425/*
1426 * queue a video buffer
1427 *
1428 * Attempting to queue a buffer that has already been
1429 * queued will return -EINVAL.
1430 */
1431static int vidioc_qbuf(struct file *file, void *priv,
1432 struct v4l2_buffer *v4l2_buf)
1433{
1434 struct gspca_dev *gspca_dev = priv;
1435 struct gspca_frame *frame;
1436 int i, index, ret;
1437
1438 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1439 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1440 return -EINVAL;
1441
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001442 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1443 return -ERESTARTSYS;
1444
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001445 index = v4l2_buf->index;
1446 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001447 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001448 "qbuf idx %d >= %d", index, gspca_dev->nframes);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001449 ret = -EINVAL;
1450 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001451 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001452 if (v4l2_buf->memory != gspca_dev->memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001453 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001454 ret = -EINVAL;
1455 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001456 }
1457
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001458 frame = &gspca_dev->frame[index];
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001459 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1460 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001461 ret = -EINVAL;
1462 goto out;
1463 }
1464
1465 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001466/* frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001467
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001468 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001469 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1470 frame->v4l2_buf.length = v4l2_buf->length;
1471 }
1472
1473 /* put the buffer in the 'queued' queue */
1474 i = gspca_dev->fr_q;
1475 gspca_dev->fr_queue[i] = index;
1476 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1477 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1478 gspca_dev->fr_q,
1479 gspca_dev->fr_i,
1480 gspca_dev->fr_o);
1481
1482 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1483 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1484 ret = 0;
1485out:
1486 mutex_unlock(&gspca_dev->queue_lock);
1487 return ret;
1488}
1489
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001490/*
1491 * allocate the resources for read()
1492 */
1493static int read_alloc(struct gspca_dev *gspca_dev,
1494 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001495{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001496 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001497 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001498
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001499 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001500 if (gspca_dev->nframes == 0) {
1501 struct v4l2_requestbuffers rb;
1502
1503 memset(&rb, 0, sizeof rb);
1504 rb.count = gspca_dev->nbufread;
1505 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001506 rb.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001507 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1508 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001509 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001510 return ret;
1511 }
1512 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1513 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001514 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001515 for (i = 0; i < gspca_dev->nbufread; i++) {
1516 v4l2_buf.index = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001517 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1518 if (ret != 0) {
1519 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1520 return ret;
1521 }
1522 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001523 gspca_dev->memory = GSPCA_MEMORY_READ;
1524 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001525
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001526 /* start streaming */
1527 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1528 if (ret != 0)
1529 PDEBUG(D_STREAM, "read streamon err %d", ret);
1530 return ret;
1531}
1532
1533static unsigned int dev_poll(struct file *file, poll_table *wait)
1534{
1535 struct gspca_dev *gspca_dev = file->private_data;
1536 int i, ret;
1537
1538 PDEBUG(D_FRAM, "poll");
1539
1540 poll_wait(file, &gspca_dev->wq, wait);
1541 if (!gspca_dev->present)
1542 return POLLERR;
1543
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001544 /* if reqbufs is not done, the user would use read() */
1545 if (gspca_dev->nframes == 0) {
1546 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1547 return POLLERR; /* not the 1st time */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001548 ret = read_alloc(gspca_dev, file);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001549 if (ret != 0)
1550 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001551 }
1552
1553 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1554 return POLLERR;
1555 if (!gspca_dev->present) {
1556 ret = POLLERR;
1557 goto out;
1558 }
1559
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001560 /* check the next incoming buffer */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001561 i = gspca_dev->fr_o;
1562 i = gspca_dev->fr_queue[i];
1563 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1564 ret = POLLIN | POLLRDNORM; /* something to read */
1565 else
1566 ret = 0;
1567out:
1568 mutex_unlock(&gspca_dev->queue_lock);
1569 return ret;
1570}
1571
1572static ssize_t dev_read(struct file *file, char __user *data,
1573 size_t count, loff_t *ppos)
1574{
1575 struct gspca_dev *gspca_dev = file->private_data;
1576 struct gspca_frame *frame;
1577 struct v4l2_buffer v4l2_buf;
1578 struct timeval timestamp;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001579 int n, ret, ret2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001580
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001581 PDEBUG(D_FRAM, "read (%zd)", count);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001582 if (!gspca_dev->present)
1583 return -ENODEV;
1584 switch (gspca_dev->memory) {
1585 case GSPCA_MEMORY_NO: /* first time */
1586 ret = read_alloc(gspca_dev, file);
1587 if (ret != 0)
1588 return ret;
1589 break;
1590 case GSPCA_MEMORY_READ:
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001591 if (gspca_dev->capt_file == file)
1592 break;
1593 /* fall thru */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001594 default:
1595 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001596 }
1597
1598 /* get a frame */
1599 jiffies_to_timeval(get_jiffies_64(), &timestamp);
1600 timestamp.tv_sec--;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001601 n = 2;
1602 for (;;) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001603 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1604 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001605 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001606 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1607 if (ret != 0) {
1608 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1609 return ret;
1610 }
1611
1612 /* if the process slept for more than 1 second,
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001613 * get anewer frame */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001614 frame = &gspca_dev->frame[v4l2_buf.index];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001615 if (--n < 0)
1616 break; /* avoid infinite loop */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001617 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1618 break;
1619 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1620 if (ret != 0) {
1621 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1622 return ret;
1623 }
1624 }
1625
1626 /* copy the frame */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001627 if (count > frame->v4l2_buf.bytesused)
1628 count = frame->v4l2_buf.bytesused;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001629 ret = copy_to_user(data, frame->data, count);
1630 if (ret != 0) {
1631 PDEBUG(D_ERR|D_STREAM,
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001632 "read cp to user lack %d / %zd", ret, count);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001633 ret = -EFAULT;
1634 goto out;
1635 }
1636 ret = count;
1637out:
1638 /* in each case, requeue the buffer */
1639 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1640 if (ret2 != 0)
1641 return ret2;
1642 return ret;
1643}
1644
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001645static void dev_release(struct video_device *vfd)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001646{
1647 /* nothing */
1648}
1649
1650static struct file_operations dev_fops = {
1651 .owner = THIS_MODULE,
1652 .open = dev_open,
1653 .release = dev_close,
1654 .read = dev_read,
1655 .mmap = dev_mmap,
1656 .ioctl = video_ioctl2,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001657#ifdef CONFIG_COMPAT
1658 .compat_ioctl = v4l_compat_ioctl32,
1659#endif
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001660 .llseek = no_llseek,
1661 .poll = dev_poll,
1662};
1663
Hans Verkuila3998102008-07-21 02:57:38 -03001664static const struct v4l2_ioctl_ops dev_ioctl_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001665 .vidioc_querycap = vidioc_querycap,
1666 .vidioc_dqbuf = vidioc_dqbuf,
1667 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001668 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1669 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1670 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1671 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001672 .vidioc_streamon = vidioc_streamon,
1673 .vidioc_queryctrl = vidioc_queryctrl,
1674 .vidioc_g_ctrl = vidioc_g_ctrl,
1675 .vidioc_s_ctrl = vidioc_s_ctrl,
1676 .vidioc_querymenu = vidioc_querymenu,
1677 .vidioc_enum_input = vidioc_enum_input,
1678 .vidioc_g_input = vidioc_g_input,
1679 .vidioc_s_input = vidioc_s_input,
1680 .vidioc_reqbufs = vidioc_reqbufs,
1681 .vidioc_querybuf = vidioc_querybuf,
1682 .vidioc_streamoff = vidioc_streamoff,
1683 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1684 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1685 .vidioc_g_parm = vidioc_g_parm,
1686 .vidioc_s_parm = vidioc_s_parm,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001687 .vidioc_s_std = vidioc_s_std,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001688#ifdef CONFIG_VIDEO_V4L1_COMPAT
1689 .vidiocgmbuf = vidiocgmbuf,
1690#endif
1691};
1692
Hans Verkuila3998102008-07-21 02:57:38 -03001693static struct video_device gspca_template = {
1694 .name = "gspca main driver",
Hans Verkuila3998102008-07-21 02:57:38 -03001695 .fops = &dev_fops,
1696 .ioctl_ops = &dev_ioctl_ops,
1697 .release = dev_release, /* mandatory */
1698 .minor = -1,
1699};
1700
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001701/*
1702 * probe and create a new gspca device
1703 *
1704 * This function must be called by the sub-driver when it is
1705 * called for probing a new device.
1706 */
1707int gspca_dev_probe(struct usb_interface *intf,
1708 const struct usb_device_id *id,
1709 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001710 int dev_size,
1711 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001712{
1713 struct usb_interface_descriptor *interface;
1714 struct gspca_dev *gspca_dev;
1715 struct usb_device *dev = interface_to_usbdev(intf);
1716 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001717
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001718 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001719
1720 /* we don't handle multi-config cameras */
1721 if (dev->descriptor.bNumConfigurations != 1)
1722 return -ENODEV;
1723 interface = &intf->cur_altsetting->desc;
1724 if (interface->bInterfaceNumber > 0)
1725 return -ENODEV;
1726
1727 /* create the device */
1728 if (dev_size < sizeof *gspca_dev)
1729 dev_size = sizeof *gspca_dev;
1730 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
1731 if (gspca_dev == NULL) {
1732 err("couldn't kzalloc gspca struct");
1733 return -EIO;
1734 }
Jean-Francois Moine8295d992008-09-03 17:12:19 -03001735 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
1736 if (!gspca_dev->usb_buf) {
1737 err("out of memory");
1738 ret = -EIO;
1739 goto out;
1740 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001741 gspca_dev->dev = dev;
1742 gspca_dev->iface = interface->bInterfaceNumber;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001743 gspca_dev->nbalt = intf->num_altsetting;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001744 gspca_dev->sd_desc = sd_desc;
1745/* gspca_dev->users = 0; (done by kzalloc) */
1746 gspca_dev->nbufread = 2;
1747
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001748 /* configure the subdriver and initialize the USB device */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001749 ret = gspca_dev->sd_desc->config(gspca_dev, id);
1750 if (ret < 0)
1751 goto out;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001752 ret = gspca_dev->sd_desc->init(gspca_dev);
1753 if (ret < 0)
1754 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001755 ret = gspca_set_alt0(gspca_dev);
1756 if (ret < 0)
1757 goto out;
1758 gspca_set_default_mode(gspca_dev);
1759
1760 mutex_init(&gspca_dev->usb_lock);
1761 mutex_init(&gspca_dev->read_lock);
1762 mutex_init(&gspca_dev->queue_lock);
1763 init_waitqueue_head(&gspca_dev->wq);
1764
1765 /* init video stuff */
1766 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
Hans Verkuil5e85e732008-07-20 06:31:39 -03001767 gspca_dev->vdev.parent = &dev->dev;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001768 memcpy(&gspca_dev->fops, &dev_fops, sizeof gspca_dev->fops);
1769 gspca_dev->vdev.fops = &gspca_dev->fops;
1770 gspca_dev->fops.owner = module; /* module protection */
Oliver Neukumee56a4d2008-07-27 14:01:59 -03001771 gspca_dev->present = 1;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001772 ret = video_register_device(&gspca_dev->vdev,
1773 VFL_TYPE_GRABBER,
1774 video_nr);
1775 if (ret < 0) {
1776 err("video_register_device err %d", ret);
1777 goto out;
1778 }
1779
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001780 usb_set_intfdata(intf, gspca_dev);
1781 PDEBUG(D_PROBE, "probe ok");
1782 return 0;
1783out:
Jean-Francois Moine8295d992008-09-03 17:12:19 -03001784 kfree(gspca_dev->usb_buf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001785 kfree(gspca_dev);
1786 return ret;
1787}
1788EXPORT_SYMBOL(gspca_dev_probe);
1789
1790/*
1791 * USB disconnection
1792 *
1793 * This function must be called by the sub-driver
1794 * when the device disconnects, after the specific resources are freed.
1795 */
1796void gspca_disconnect(struct usb_interface *intf)
1797{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001798 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001799
1800 if (!gspca_dev)
1801 return;
1802 gspca_dev->present = 0;
Hans de Goedee2997a72008-04-23 08:09:12 -03001803 mutex_lock(&gspca_dev->queue_lock);
1804 mutex_lock(&gspca_dev->usb_lock);
1805 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001806 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001807 mutex_unlock(&gspca_dev->usb_lock);
Hans de Goedee2997a72008-04-23 08:09:12 -03001808 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001809 while (gspca_dev->users != 0) { /* wait until fully closed */
1810 atomic_inc(&gspca_dev->nevent);
1811 wake_up_interruptible(&gspca_dev->wq); /* wake processes */
1812 schedule();
1813 }
1814/* We don't want people trying to open up the device */
1815 video_unregister_device(&gspca_dev->vdev);
1816/* Free the memory */
Jean-Francois Moine8295d992008-09-03 17:12:19 -03001817 kfree(gspca_dev->usb_buf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001818 kfree(gspca_dev);
1819 PDEBUG(D_PROBE, "disconnect complete");
1820}
1821EXPORT_SYMBOL(gspca_disconnect);
1822
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001823#ifdef CONFIG_PM
1824int gspca_suspend(struct usb_interface *intf, pm_message_t message)
1825{
1826 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1827
1828 if (!gspca_dev->streaming)
1829 return 0;
1830 gspca_dev->frozen = 1; /* avoid urb error messages */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001831 if (gspca_dev->sd_desc->stopN)
1832 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001833 destroy_urbs(gspca_dev);
1834 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001835 if (gspca_dev->sd_desc->stop0)
1836 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001837 return 0;
1838}
1839EXPORT_SYMBOL(gspca_suspend);
1840
1841int gspca_resume(struct usb_interface *intf)
1842{
1843 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1844
1845 gspca_dev->frozen = 0;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001846 gspca_dev->sd_desc->init(gspca_dev);
1847 if (gspca_dev->streaming)
1848 return gspca_init_transfer(gspca_dev);
Jean-Francois Moine1e89e2da2008-09-03 17:12:15 -03001849 return 0;
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001850}
1851EXPORT_SYMBOL(gspca_resume);
1852#endif
Hans de Goededcef32372008-07-10 10:40:53 -03001853/* -- cam driver utility functions -- */
1854
1855/* auto gain and exposure algorithm based on the knee algorithm described here:
1856 http://ytse.tricolour.net/docs/LowLightOptimization.html
1857
1858 Returns 0 if no changes were made, 1 if the gain and or exposure settings
1859 where changed. */
1860int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
1861 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
1862{
1863 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
1864 const struct ctrl *gain_ctrl = NULL;
1865 const struct ctrl *exposure_ctrl = NULL;
1866 const struct ctrl *autogain_ctrl = NULL;
1867 int retval = 0;
1868
1869 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1870 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
1871 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1872 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
1873 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
1874 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
1875 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1876 }
1877 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
1878 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
1879 "on cam without (auto)gain/exposure");
1880 return 0;
1881 }
1882
1883 if (gain_ctrl->get(gspca_dev, &gain) ||
1884 exposure_ctrl->get(gspca_dev, &exposure) ||
1885 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
1886 return 0;
1887
1888 orig_gain = gain;
1889 orig_exposure = exposure;
1890
1891 /* If we are of a multiple of deadzone, do multiple steps to reach the
1892 desired lumination fast (with the risc of a slight overshoot) */
1893 steps = abs(desired_avg_lum - avg_lum) / deadzone;
1894
1895 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d\n",
1896 avg_lum, desired_avg_lum, steps);
1897
1898 for (i = 0; i < steps; i++) {
1899 if (avg_lum > desired_avg_lum) {
1900 if (gain > gain_knee)
1901 gain--;
1902 else if (exposure > exposure_knee)
1903 exposure--;
1904 else if (gain > gain_ctrl->qctrl.default_value)
1905 gain--;
1906 else if (exposure > exposure_ctrl->qctrl.minimum)
1907 exposure--;
1908 else if (gain > gain_ctrl->qctrl.minimum)
1909 gain--;
1910 else
1911 break;
1912 } else {
1913 if (gain < gain_ctrl->qctrl.default_value)
1914 gain++;
1915 else if (exposure < exposure_knee)
1916 exposure++;
1917 else if (gain < gain_knee)
1918 gain++;
1919 else if (exposure < exposure_ctrl->qctrl.maximum)
1920 exposure++;
1921 else if (gain < gain_ctrl->qctrl.maximum)
1922 gain++;
1923 else
1924 break;
1925 }
1926 }
1927
1928 if (gain != orig_gain) {
1929 gain_ctrl->set(gspca_dev, gain);
1930 retval = 1;
1931 }
1932 if (exposure != orig_exposure) {
1933 exposure_ctrl->set(gspca_dev, exposure);
1934 retval = 1;
1935 }
1936
1937 return retval;
1938}
1939EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
1940
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001941/* -- module insert / remove -- */
1942static int __init gspca_init(void)
1943{
Jean-Francois Moinebb64e862008-07-19 06:49:28 -03001944 info("main v%d.%d.%d registered",
1945 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
1946 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
1947 DRIVER_VERSION_NUMBER & 0xff);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001948 return 0;
1949}
1950static void __exit gspca_exit(void)
1951{
1952 info("main deregistered");
1953}
1954
1955module_init(gspca_init);
1956module_exit(gspca_exit);
1957
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001958#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001959module_param_named(debug, gspca_debug, int, 0644);
1960MODULE_PARM_DESC(debug,
1961 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001962 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
1963 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001964#endif