blob: 42b0b83d0953add389c41afd5b2428e031cdc532 [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");
462 return -ENOMEM;
463 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300464 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300465 bsize,
466 GFP_KERNEL,
467 &urb->transfer_dma);
468
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300469 if (urb->transfer_buffer == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300470 usb_free_urb(urb);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300471 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300472 err("usb_buffer_urb failed");
473 return -ENOMEM;
474 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300475 gspca_dev->urb[n] = urb;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300476 urb->dev = gspca_dev->dev;
477 urb->context = gspca_dev;
478 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
479 ep->desc.bEndpointAddress);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300480 urb->transfer_flags = URB_ISO_ASAP
481 | URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300482 urb->interval = ep->desc.bInterval;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300483 urb->complete = isoc_irq;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300484 urb->number_of_packets = npkt;
485 urb->transfer_buffer_length = bsize;
486 for (i = 0; i < npkt; i++) {
487 urb->iso_frame_desc[i].length = psize;
488 urb->iso_frame_desc[i].offset = psize * i;
489 }
490 }
491 return 0;
492}
493
494/*
495 * start the USB transfer
496 */
497static int gspca_init_transfer(struct gspca_dev *gspca_dev)
498{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300499 struct usb_host_endpoint *ep;
500 int n, ret;
501
Hans de Goedee2997a72008-04-23 08:09:12 -0300502 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
503 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300504
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300505 /* set the higher alternate setting and
506 * loop until urb submit succeeds */
507 gspca_dev->alt = gspca_dev->nbalt;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300508 for (;;) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300509 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300510 ep = get_isoc_ep(gspca_dev);
511 if (ep == NULL) {
512 ret = -EIO;
513 goto out;
514 }
515 ret = create_urbs(gspca_dev, ep);
516 if (ret < 0)
517 goto out;
518
519 /* start the cam */
520 gspca_dev->sd_desc->start(gspca_dev);
521 gspca_dev->streaming = 1;
522 atomic_set(&gspca_dev->nevent, 0);
523
524 /* submit the URBs */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300525 for (n = 0; n < gspca_dev->nurbs; n++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300526 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300527 if (ret < 0) {
528 PDEBUG(D_ERR|D_STREAM,
529 "usb_submit_urb [%d] err %d", n, ret);
Hans de Goedee2997a72008-04-23 08:09:12 -0300530 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300531 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300532 if (ret == -ENOSPC)
533 break; /* try the previous alt */
534 goto out;
535 }
536 }
537 if (ret >= 0)
538 break;
539 }
540out:
541 mutex_unlock(&gspca_dev->usb_lock);
542 return ret;
543}
544
545static int gspca_set_alt0(struct gspca_dev *gspca_dev)
546{
547 int ret;
548
549 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
550 if (ret < 0)
551 PDEBUG(D_ERR|D_STREAM, "set interface 0 err %d", ret);
552 return ret;
553}
554
Hans de Goedee2997a72008-04-23 08:09:12 -0300555/* Note both the queue and the usb lock should be hold when calling this */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300556static void gspca_stream_off(struct gspca_dev *gspca_dev)
557{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300558 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300559 atomic_set(&gspca_dev->nevent, 0);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300560 if (gspca_dev->present) {
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300561 if (gspca_dev->sd_desc->stopN)
562 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300563 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300564 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300565 if (gspca_dev->sd_desc->stop0)
566 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300567 PDEBUG(D_STREAM, "stream off OK");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300568 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300569}
570
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300571static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300572{
573 int i;
574
575 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
576 gspca_dev->curr_mode = i;
577 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
578 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300579 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300580}
581
582static int wxh_to_mode(struct gspca_dev *gspca_dev,
583 int width, int height)
584{
585 int i;
586
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300587 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
588 if (width >= gspca_dev->cam.cam_mode[i].width
589 && height >= gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300590 break;
591 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300592 return i;
593}
594
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300595/*
596 * search a mode with the right pixel format
597 */
598static int gspca_get_mode(struct gspca_dev *gspca_dev,
599 int mode,
600 int pixfmt)
601{
602 int modeU, modeD;
603
604 modeU = modeD = mode;
605 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
606 if (--modeD >= 0) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300607 if (gspca_dev->cam.cam_mode[modeD].pixelformat
608 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300609 return modeD;
610 }
611 if (++modeU < gspca_dev->cam.nmodes) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300612 if (gspca_dev->cam.cam_mode[modeU].pixelformat
613 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300614 return modeU;
615 }
616 }
617 return -EINVAL;
618}
619
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300620static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300621 struct v4l2_fmtdesc *fmtdesc)
622{
623 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300624 int i, j, index;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300625 __u32 fmt_tb[8];
626
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300627 /* give an index to each format */
628 index = 0;
629 j = 0;
630 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300631 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300632 j = 0;
633 for (;;) {
634 if (fmt_tb[j] == fmt_tb[index])
635 break;
636 j++;
637 }
638 if (j == index) {
639 if (fmtdesc->index == index)
640 break; /* new format */
641 index++;
642 if (index >= sizeof fmt_tb / sizeof fmt_tb[0])
643 return -EINVAL;
644 }
645 }
646 if (i < 0)
647 return -EINVAL; /* no more format */
648
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300649 fmtdesc->pixelformat = fmt_tb[index];
650 if (gspca_is_compressed(fmt_tb[index]))
651 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300652 fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300653 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
654 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
655 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
656 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
657 fmtdesc->description[4] = '\0';
658 return 0;
659}
660
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300661static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300662 struct v4l2_format *fmt)
663{
664 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300665 int mode;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300666
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300667 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
668 return -EINVAL;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300669 mode = gspca_dev->curr_mode;
670 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
671 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300672 return 0;
673}
674
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300675static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300676 struct v4l2_format *fmt)
677{
Hans de Goede24c530b2008-07-04 17:55:18 -0300678 int w, h, mode, mode2;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300679
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300680 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
681 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300682 w = fmt->fmt.pix.width;
683 h = fmt->fmt.pix.height;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300684
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300685#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300686 if (gspca_debug & D_CONF)
687 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
688#endif
689 /* search the closest mode for width and height */
690 mode = wxh_to_mode(gspca_dev, w, h);
691
692 /* OK if right palette */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300693 if (gspca_dev->cam.cam_mode[mode].pixelformat
694 != fmt->fmt.pix.pixelformat) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300695
696 /* else, search the closest mode with the same pixel format */
697 mode2 = gspca_get_mode(gspca_dev, mode,
698 fmt->fmt.pix.pixelformat);
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300699 if (mode2 >= 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300700 mode = mode2;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300701/* else
702 ; * no chance, return this mode */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300703 }
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300704 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
705 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300706 return mode; /* used when s_fmt */
707}
708
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300709static int vidioc_try_fmt_vid_cap(struct file *file,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300710 void *priv,
711 struct v4l2_format *fmt)
712{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300713 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300714 int ret;
715
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300716 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300717 if (ret < 0)
718 return ret;
719 return 0;
720}
721
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300722static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300723 struct v4l2_format *fmt)
724{
725 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300726 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300727
Hans de Goedee2997a72008-04-23 08:09:12 -0300728 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
729 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300730
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300731 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300732 if (ret < 0)
733 goto out;
734
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300735 if (gspca_dev->nframes != 0
736 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
737 ret = -EINVAL;
738 goto out;
739 }
740
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300741 if (ret == gspca_dev->curr_mode) {
742 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300743 goto out; /* same mode */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300744 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300745
746 if (gspca_dev->streaming) {
747 ret = -EBUSY;
748 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -0300749 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300750 gspca_dev->width = fmt->fmt.pix.width;
751 gspca_dev->height = fmt->fmt.pix.height;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300752 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
753 gspca_dev->curr_mode = ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300754
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300755 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300756out:
757 mutex_unlock(&gspca_dev->queue_lock);
758 return ret;
759}
760
761static int dev_open(struct inode *inode, struct file *file)
762{
763 struct gspca_dev *gspca_dev;
764 int ret;
765
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300766 PDEBUG(D_STREAM, "%s open", current->comm);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300767 gspca_dev = (struct gspca_dev *) video_devdata(file);
Hans de Goedee2997a72008-04-23 08:09:12 -0300768 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
769 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300770 if (!gspca_dev->present) {
771 ret = -ENODEV;
772 goto out;
773 }
774
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300775 if (gspca_dev->users > 4) { /* (arbitrary value) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300776 ret = -EBUSY;
777 goto out;
778 }
779 gspca_dev->users++;
780 file->private_data = gspca_dev;
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300781#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300782 /* activate the v4l2 debug */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300783 if (gspca_debug & D_V4L2)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300784 gspca_dev->vdev.debug |= 3;
785 else
786 gspca_dev->vdev.debug &= ~3;
787#endif
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300788 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300789out:
790 mutex_unlock(&gspca_dev->queue_lock);
791 if (ret != 0)
792 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
793 else
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300794 PDEBUG(D_STREAM, "open done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300795 return ret;
796}
797
798static int dev_close(struct inode *inode, struct file *file)
799{
800 struct gspca_dev *gspca_dev = file->private_data;
801
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300802 PDEBUG(D_STREAM, "%s close", current->comm);
Hans de Goedee2997a72008-04-23 08:09:12 -0300803 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
804 return -ERESTARTSYS;
805 gspca_dev->users--;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300806
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -0300807 /* if the file did the capture, free the streaming resources */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300808 if (gspca_dev->capt_file == file) {
Jean-Francois Moinea1e82882008-09-03 17:12:17 -0300809 if (gspca_dev->streaming) {
810 mutex_lock(&gspca_dev->usb_lock);
811 gspca_stream_off(gspca_dev);
812 mutex_unlock(&gspca_dev->usb_lock);
813 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300814 frame_free(gspca_dev);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300815 gspca_dev->capt_file = NULL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300816 gspca_dev->memory = GSPCA_MEMORY_NO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300817 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300818 file->private_data = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300819 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300820 PDEBUG(D_STREAM, "close done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300821 return 0;
822}
823
824static int vidioc_querycap(struct file *file, void *priv,
825 struct v4l2_capability *cap)
826{
827 struct gspca_dev *gspca_dev = priv;
828
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300829 memset(cap, 0, sizeof *cap);
830 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -0300831/* strncpy(cap->card, gspca_dev->cam.dev_name, sizeof cap->card); */
832 if (gspca_dev->dev->product != NULL) {
833 strncpy(cap->card, gspca_dev->dev->product,
834 sizeof cap->card);
835 } else {
836 snprintf(cap->card, sizeof cap->card,
837 "USB Camera (%04x:%04x)",
838 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
839 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
840 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300841 strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name,
842 sizeof cap->bus_info);
843 cap->version = DRIVER_VERSION_NUMBER;
844 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
845 | V4L2_CAP_STREAMING
846 | V4L2_CAP_READWRITE;
847 return 0;
848}
849
850static int vidioc_queryctrl(struct file *file, void *priv,
851 struct v4l2_queryctrl *q_ctrl)
852{
853 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300854 int i, ix;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300855 u32 id;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300856
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300857 ix = -1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300858 id = q_ctrl->id;
859 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
860 id &= V4L2_CTRL_ID_MASK;
861 id++;
862 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goede0f523c22008-09-03 17:12:14 -0300863 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300864 continue;
865 if (ix < 0) {
866 ix = i;
867 continue;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300868 }
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300869 if (gspca_dev->sd_desc->ctrls[i].qctrl.id
870 > gspca_dev->sd_desc->ctrls[ix].qctrl.id)
871 continue;
872 ix = i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300873 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300874 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300875 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300876 if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) {
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300877 ix = i;
878 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300879 }
880 }
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300881 if (ix < 0)
882 return -EINVAL;
883 memcpy(q_ctrl, &gspca_dev->sd_desc->ctrls[ix].qctrl,
884 sizeof *q_ctrl);
885 if (gspca_dev->ctrl_dis & (1 << ix))
886 q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
887 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300888}
889
890static int vidioc_s_ctrl(struct file *file, void *priv,
891 struct v4l2_control *ctrl)
892{
893 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300894 const struct ctrl *ctrls;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300895 int i, ret;
896
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300897 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
898 i < gspca_dev->sd_desc->nctrls;
899 i++, ctrls++) {
900 if (ctrl->id != ctrls->qctrl.id)
901 continue;
Hans de Goede0f523c22008-09-03 17:12:14 -0300902 if (gspca_dev->ctrl_dis & (1 << i))
903 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300904 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 Goede0f523c22008-09-03 17:12:14 -0300930 if (gspca_dev->ctrl_dis & (1 << i))
931 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -0300932 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
933 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300934 ret = ctrls->get(gspca_dev, &ctrl->value);
935 mutex_unlock(&gspca_dev->usb_lock);
936 return ret;
937 }
938 return -EINVAL;
939}
940
941static int vidioc_querymenu(struct file *file, void *priv,
942 struct v4l2_querymenu *qmenu)
943{
944 struct gspca_dev *gspca_dev = priv;
945
946 if (!gspca_dev->sd_desc->querymenu)
947 return -EINVAL;
948 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
949}
950
951static int vidioc_enum_input(struct file *file, void *priv,
952 struct v4l2_input *input)
953{
954 struct gspca_dev *gspca_dev = priv;
955
956 if (input->index != 0)
957 return -EINVAL;
958 memset(input, 0, sizeof *input);
959 input->type = V4L2_INPUT_TYPE_CAMERA;
960 strncpy(input->name, gspca_dev->sd_desc->name,
961 sizeof input->name);
962 return 0;
963}
964
965static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
966{
967 *i = 0;
968 return 0;
969}
970
971static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
972{
973 if (i > 0)
974 return -EINVAL;
975 return (0);
976}
977
978static int vidioc_reqbufs(struct file *file, void *priv,
979 struct v4l2_requestbuffers *rb)
980{
981 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300982 int i, ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300983
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300984 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
985 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300986 switch (rb->memory) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -0300987 case GSPCA_MEMORY_READ: /* (internal call) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300988 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300989 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300990 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300991 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300992 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300993 }
Hans de Goedee2997a72008-04-23 08:09:12 -0300994 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
995 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300996
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -0300997 if (gspca_dev->memory != GSPCA_MEMORY_NO
998 && gspca_dev->memory != rb->memory) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300999 ret = -EBUSY;
1000 goto out;
1001 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001002
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001003 /* only one file may do the capture */
1004 if (gspca_dev->capt_file != NULL
1005 && gspca_dev->capt_file != file) {
1006 ret = -EBUSY;
1007 goto out;
1008 }
1009
1010 /* if allocated, the buffers must not be mapped */
1011 for (i = 0; i < gspca_dev->nframes; i++) {
1012 if (gspca_dev->frame[i].vma_use_count) {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001013 ret = -EBUSY;
1014 goto out;
1015 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001016 }
1017
1018 /* stop streaming */
1019 if (gspca_dev->streaming) {
1020 mutex_lock(&gspca_dev->usb_lock);
1021 gspca_stream_off(gspca_dev);
1022 mutex_unlock(&gspca_dev->usb_lock);
1023 }
1024
1025 /* free the previous allocated buffers, if any */
1026 if (gspca_dev->nframes != 0) {
1027 frame_free(gspca_dev);
1028 gspca_dev->capt_file = NULL;
1029 }
1030 if (rb->count == 0) /* unrequest */
1031 goto out;
1032 gspca_dev->memory = rb->memory;
1033 ret = frame_alloc(gspca_dev, rb->count);
1034 if (ret == 0) {
1035 rb->count = gspca_dev->nframes;
1036 gspca_dev->capt_file = file;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001037 }
1038out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001039 mutex_unlock(&gspca_dev->queue_lock);
1040 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1041 return ret;
1042}
1043
1044static int vidioc_querybuf(struct file *file, void *priv,
1045 struct v4l2_buffer *v4l2_buf)
1046{
1047 struct gspca_dev *gspca_dev = priv;
1048 struct gspca_frame *frame;
1049
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001050 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1051 || v4l2_buf->index < 0
1052 || v4l2_buf->index >= gspca_dev->nframes)
1053 return -EINVAL;
1054
1055 frame = &gspca_dev->frame[v4l2_buf->index];
1056 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1057 return 0;
1058}
1059
1060static int vidioc_streamon(struct file *file, void *priv,
1061 enum v4l2_buf_type buf_type)
1062{
1063 struct gspca_dev *gspca_dev = priv;
1064 int ret;
1065
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001066 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1067 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001068 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1069 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001070 if (!gspca_dev->present) {
1071 ret = -ENODEV;
1072 goto out;
1073 }
1074 if (gspca_dev->nframes == 0) {
1075 ret = -EINVAL;
1076 goto out;
1077 }
1078 if (!gspca_dev->streaming) {
1079 ret = gspca_init_transfer(gspca_dev);
1080 if (ret < 0)
1081 goto out;
1082 }
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001083#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001084 if (gspca_debug & D_STREAM) {
1085 PDEBUG_MODE("stream on OK",
1086 gspca_dev->pixfmt,
1087 gspca_dev->width,
1088 gspca_dev->height);
1089 }
1090#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001091 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001092out:
1093 mutex_unlock(&gspca_dev->queue_lock);
1094 return ret;
1095}
1096
1097static int vidioc_streamoff(struct file *file, void *priv,
1098 enum v4l2_buf_type buf_type)
1099{
1100 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001101 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001102
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001103 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1104 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001105 if (!gspca_dev->streaming)
1106 return 0;
1107 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1108 return -ERESTARTSYS;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001109
1110 /* stop streaming */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001111 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1112 ret = -ERESTARTSYS;
1113 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001114 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001115 gspca_stream_off(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001116 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001117
1118 /* empty the application queues */
1119 for (i = 0; i < gspca_dev->nframes; i++)
1120 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1121 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1122 gspca_dev->last_packet_type = DISCARD_PACKET;
1123 gspca_dev->sequence = 0;
1124 atomic_set(&gspca_dev->nevent, 0);
1125 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001126out:
1127 mutex_unlock(&gspca_dev->queue_lock);
1128 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001129}
1130
1131static int vidioc_g_jpegcomp(struct file *file, void *priv,
1132 struct v4l2_jpegcompression *jpegcomp)
1133{
1134 struct gspca_dev *gspca_dev = priv;
1135 int ret;
1136
1137 if (!gspca_dev->sd_desc->get_jcomp)
1138 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001139 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1140 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001141 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1142 mutex_unlock(&gspca_dev->usb_lock);
1143 return ret;
1144}
1145
1146static int vidioc_s_jpegcomp(struct file *file, void *priv,
1147 struct v4l2_jpegcompression *jpegcomp)
1148{
1149 struct gspca_dev *gspca_dev = priv;
1150 int ret;
1151
Hans de Goedee2997a72008-04-23 08:09:12 -03001152 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1153 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001154 if (!gspca_dev->sd_desc->set_jcomp)
1155 return -EINVAL;
1156 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1157 mutex_unlock(&gspca_dev->usb_lock);
1158 return ret;
1159}
1160
1161static int vidioc_g_parm(struct file *filp, void *priv,
1162 struct v4l2_streamparm *parm)
1163{
1164 struct gspca_dev *gspca_dev = priv;
1165
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001166 memset(parm, 0, sizeof *parm);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001167 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1168 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1169 return 0;
1170}
1171
1172static int vidioc_s_parm(struct file *filp, void *priv,
1173 struct v4l2_streamparm *parm)
1174{
1175 struct gspca_dev *gspca_dev = priv;
1176 int n;
1177
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001178 n = parm->parm.capture.readbuffers;
1179 if (n == 0 || n > GSPCA_MAX_FRAMES)
1180 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1181 else
1182 gspca_dev->nbufread = n;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001183 return 0;
1184}
1185
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001186static int vidioc_s_std(struct file *filp, void *priv,
1187 v4l2_std_id *parm)
1188{
1189 return 0;
1190}
1191
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001192#ifdef CONFIG_VIDEO_V4L1_COMPAT
1193static int vidiocgmbuf(struct file *file, void *priv,
1194 struct video_mbuf *mbuf)
1195{
1196 struct gspca_dev *gspca_dev = file->private_data;
1197 int i;
1198
1199 PDEBUG(D_STREAM, "cgmbuf");
1200 if (gspca_dev->nframes == 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001201 int ret;
1202
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001203 {
1204 struct v4l2_format fmt;
1205
1206 memset(&fmt, 0, sizeof fmt);
1207 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1208 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1209 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1210 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1211 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001212 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001213 if (ret != 0)
1214 return ret;
1215 }
1216 {
1217 struct v4l2_requestbuffers rb;
1218
1219 memset(&rb, 0, sizeof rb);
1220 rb.count = 4;
1221 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1222 rb.memory = V4L2_MEMORY_MMAP;
1223 ret = vidioc_reqbufs(file, priv, &rb);
1224 if (ret != 0)
1225 return ret;
1226 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001227 }
1228 mbuf->frames = gspca_dev->nframes;
1229 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1230 for (i = 0; i < mbuf->frames; i++)
1231 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1232 return 0;
1233}
1234#endif
1235
1236static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1237{
1238 struct gspca_dev *gspca_dev = file->private_data;
Harvey Harrisonefab8212008-07-05 06:12:47 -03001239 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001240 struct page *page;
1241 unsigned long addr, start, size;
1242 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001243
1244 start = vma->vm_start;
1245 size = vma->vm_end - vma->vm_start;
1246 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1247
Hans de Goedee2997a72008-04-23 08:09:12 -03001248 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1249 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001250 if (!gspca_dev->present) {
1251 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001252 goto out;
1253 }
1254 if (gspca_dev->capt_file != file) {
1255 ret = -EINVAL;
1256 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001257 }
1258
Harvey Harrisonefab8212008-07-05 06:12:47 -03001259 frame = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001260 for (i = 0; i < gspca_dev->nframes; ++i) {
1261 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1262 PDEBUG(D_STREAM, "mmap bad memory type");
1263 break;
1264 }
1265 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1266 == vma->vm_pgoff) {
1267 frame = &gspca_dev->frame[i];
1268 break;
1269 }
1270 }
Harvey Harrisonefab8212008-07-05 06:12:47 -03001271 if (frame == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001272 PDEBUG(D_STREAM, "mmap no frame buffer found");
1273 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001274 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001275 }
1276#ifdef CONFIG_VIDEO_V4L1_COMPAT
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001277 /* v4l1 maps all the buffers */
1278 if (i != 0
1279 || size != frame->v4l2_buf.length * gspca_dev->nframes)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001280#endif
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001281 if (size != frame->v4l2_buf.length) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001282 PDEBUG(D_STREAM, "mmap bad size");
1283 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001284 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001285 }
1286
1287 /*
1288 * - VM_IO marks the area as being a mmaped region for I/O to a
1289 * device. It also prevents the region from being core dumped.
1290 */
1291 vma->vm_flags |= VM_IO;
1292
1293 addr = (unsigned long) frame->data;
1294 while (size > 0) {
1295 page = vmalloc_to_page((void *) addr);
1296 ret = vm_insert_page(vma, start, page);
1297 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001298 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001299 start += PAGE_SIZE;
1300 addr += PAGE_SIZE;
1301 size -= PAGE_SIZE;
1302 }
1303
1304 vma->vm_ops = &gspca_vm_ops;
1305 vma->vm_private_data = frame;
1306 gspca_vm_open(vma);
Hans de Goedee2997a72008-04-23 08:09:12 -03001307 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001308out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001309 mutex_unlock(&gspca_dev->queue_lock);
1310 return ret;
1311}
1312
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001313/*
1314 * wait for a video frame
1315 *
1316 * If a frame is ready, its index is returned.
1317 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001318static int frame_wait(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001319 int nonblock_ing)
1320{
1321 struct gspca_frame *frame;
1322 int i, j, ret;
1323
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001324 /* check if a frame is ready */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001325 i = gspca_dev->fr_o;
1326 j = gspca_dev->fr_queue[i];
1327 frame = &gspca_dev->frame[j];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001328 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) {
1329 atomic_dec(&gspca_dev->nevent);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001330 goto ok;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001331 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001332 if (nonblock_ing) /* no frame yet */
1333 return -EAGAIN;
1334
1335 /* wait till a frame is ready */
1336 for (;;) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001337 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1338 atomic_read(&gspca_dev->nevent) > 0,
1339 msecs_to_jiffies(3000));
1340 if (ret <= 0) {
1341 if (ret < 0)
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001342 return ret; /* interrupt */
1343 return -EIO; /* timeout */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001344 }
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001345 atomic_dec(&gspca_dev->nevent);
Hans de Goedee2997a72008-04-23 08:09:12 -03001346 if (!gspca_dev->streaming || !gspca_dev->present)
1347 return -EIO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001348 i = gspca_dev->fr_o;
1349 j = gspca_dev->fr_queue[i];
1350 frame = &gspca_dev->frame[j];
1351 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1352 break;
1353 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001354ok:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001355 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1356 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1357 gspca_dev->fr_q,
1358 gspca_dev->fr_i,
1359 gspca_dev->fr_o);
Hans de Goedee2997a72008-04-23 08:09:12 -03001360
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001361 if (gspca_dev->sd_desc->dq_callback) {
1362 mutex_lock(&gspca_dev->usb_lock);
Hans de Goedee2997a72008-04-23 08:09:12 -03001363 gspca_dev->sd_desc->dq_callback(gspca_dev);
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001364 mutex_unlock(&gspca_dev->usb_lock);
1365 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001366 return j;
1367}
1368
1369/*
1370 * dequeue a video buffer
1371 *
1372 * If nonblock_ing is false, block until a buffer is available.
1373 */
1374static int vidioc_dqbuf(struct file *file, void *priv,
1375 struct v4l2_buffer *v4l2_buf)
1376{
1377 struct gspca_dev *gspca_dev = priv;
1378 struct gspca_frame *frame;
1379 int i, ret;
1380
1381 PDEBUG(D_FRAM, "dqbuf");
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001382 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1383 return -EINVAL;
1384 if (v4l2_buf->memory != gspca_dev->memory)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001385 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001386
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001387 /* if not streaming, be sure the application will not loop forever */
1388 if (!(file->f_flags & O_NONBLOCK)
1389 && !gspca_dev->streaming && gspca_dev->users == 1)
1390 return -EINVAL;
1391
1392 /* only the capturing file may dequeue */
1393 if (gspca_dev->capt_file != file)
1394 return -EINVAL;
1395
1396 /* only one dequeue / read at a time */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001397 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1398 return -ERESTARTSYS;
1399
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001400 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001401 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001402 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001403 i = ret; /* frame index */
1404 frame = &gspca_dev->frame[i];
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001405 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001406 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001407 frame->data,
1408 frame->v4l2_buf.bytesused)) {
1409 PDEBUG(D_ERR|D_STREAM,
1410 "dqbuf cp to user failed");
1411 ret = -EFAULT;
1412 goto out;
1413 }
1414 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001415 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1416 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1417 PDEBUG(D_FRAM, "dqbuf %d", i);
1418 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001419out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001420 mutex_unlock(&gspca_dev->read_lock);
1421 return ret;
1422}
1423
1424/*
1425 * queue a video buffer
1426 *
1427 * Attempting to queue a buffer that has already been
1428 * queued will return -EINVAL.
1429 */
1430static int vidioc_qbuf(struct file *file, void *priv,
1431 struct v4l2_buffer *v4l2_buf)
1432{
1433 struct gspca_dev *gspca_dev = priv;
1434 struct gspca_frame *frame;
1435 int i, index, ret;
1436
1437 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1438 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1439 return -EINVAL;
1440
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001441 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1442 return -ERESTARTSYS;
1443
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001444 index = v4l2_buf->index;
1445 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001446 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001447 "qbuf idx %d >= %d", index, gspca_dev->nframes);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001448 ret = -EINVAL;
1449 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001450 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001451 if (v4l2_buf->memory != gspca_dev->memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001452 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001453 ret = -EINVAL;
1454 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001455 }
1456
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001457 frame = &gspca_dev->frame[index];
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001458 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1459 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001460 ret = -EINVAL;
1461 goto out;
1462 }
1463
1464 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001465/* frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001466
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001467 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001468 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1469 frame->v4l2_buf.length = v4l2_buf->length;
1470 }
1471
1472 /* put the buffer in the 'queued' queue */
1473 i = gspca_dev->fr_q;
1474 gspca_dev->fr_queue[i] = index;
1475 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1476 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1477 gspca_dev->fr_q,
1478 gspca_dev->fr_i,
1479 gspca_dev->fr_o);
1480
1481 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1482 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1483 ret = 0;
1484out:
1485 mutex_unlock(&gspca_dev->queue_lock);
1486 return ret;
1487}
1488
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001489/*
1490 * allocate the resources for read()
1491 */
1492static int read_alloc(struct gspca_dev *gspca_dev,
1493 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001494{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001495 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001496 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001497
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001498 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001499 if (gspca_dev->nframes == 0) {
1500 struct v4l2_requestbuffers rb;
1501
1502 memset(&rb, 0, sizeof rb);
1503 rb.count = gspca_dev->nbufread;
1504 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001505 rb.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001506 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1507 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001508 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001509 return ret;
1510 }
1511 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1512 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001513 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001514 for (i = 0; i < gspca_dev->nbufread; i++) {
1515 v4l2_buf.index = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001516 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1517 if (ret != 0) {
1518 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1519 return ret;
1520 }
1521 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001522 gspca_dev->memory = GSPCA_MEMORY_READ;
1523 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001524
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001525 /* start streaming */
1526 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1527 if (ret != 0)
1528 PDEBUG(D_STREAM, "read streamon err %d", ret);
1529 return ret;
1530}
1531
1532static unsigned int dev_poll(struct file *file, poll_table *wait)
1533{
1534 struct gspca_dev *gspca_dev = file->private_data;
1535 int i, ret;
1536
1537 PDEBUG(D_FRAM, "poll");
1538
1539 poll_wait(file, &gspca_dev->wq, wait);
1540 if (!gspca_dev->present)
1541 return POLLERR;
1542
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001543 /* if reqbufs is not done, the user would use read() */
1544 if (gspca_dev->nframes == 0) {
1545 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1546 return POLLERR; /* not the 1st time */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001547 ret = read_alloc(gspca_dev, file);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001548 if (ret != 0)
1549 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001550 }
1551
1552 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1553 return POLLERR;
1554 if (!gspca_dev->present) {
1555 ret = POLLERR;
1556 goto out;
1557 }
1558
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001559 /* check the next incoming buffer */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001560 i = gspca_dev->fr_o;
1561 i = gspca_dev->fr_queue[i];
1562 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1563 ret = POLLIN | POLLRDNORM; /* something to read */
1564 else
1565 ret = 0;
1566out:
1567 mutex_unlock(&gspca_dev->queue_lock);
1568 return ret;
1569}
1570
1571static ssize_t dev_read(struct file *file, char __user *data,
1572 size_t count, loff_t *ppos)
1573{
1574 struct gspca_dev *gspca_dev = file->private_data;
1575 struct gspca_frame *frame;
1576 struct v4l2_buffer v4l2_buf;
1577 struct timeval timestamp;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001578 int n, ret, ret2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001579
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001580 PDEBUG(D_FRAM, "read (%zd)", count);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001581 if (!gspca_dev->present)
1582 return -ENODEV;
1583 switch (gspca_dev->memory) {
1584 case GSPCA_MEMORY_NO: /* first time */
1585 ret = read_alloc(gspca_dev, file);
1586 if (ret != 0)
1587 return ret;
1588 break;
1589 case GSPCA_MEMORY_READ:
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001590 if (gspca_dev->capt_file == file)
1591 break;
1592 /* fall thru */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001593 default:
1594 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001595 }
1596
1597 /* get a frame */
1598 jiffies_to_timeval(get_jiffies_64(), &timestamp);
1599 timestamp.tv_sec--;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001600 n = 2;
1601 for (;;) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001602 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1603 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001604 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001605 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1606 if (ret != 0) {
1607 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1608 return ret;
1609 }
1610
1611 /* if the process slept for more than 1 second,
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001612 * get anewer frame */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001613 frame = &gspca_dev->frame[v4l2_buf.index];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001614 if (--n < 0)
1615 break; /* avoid infinite loop */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001616 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1617 break;
1618 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1619 if (ret != 0) {
1620 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1621 return ret;
1622 }
1623 }
1624
1625 /* copy the frame */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001626 if (count > frame->v4l2_buf.bytesused)
1627 count = frame->v4l2_buf.bytesused;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001628 ret = copy_to_user(data, frame->data, count);
1629 if (ret != 0) {
1630 PDEBUG(D_ERR|D_STREAM,
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001631 "read cp to user lack %d / %zd", ret, count);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001632 ret = -EFAULT;
1633 goto out;
1634 }
1635 ret = count;
1636out:
1637 /* in each case, requeue the buffer */
1638 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1639 if (ret2 != 0)
1640 return ret2;
1641 return ret;
1642}
1643
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001644static void dev_release(struct video_device *vfd)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001645{
1646 /* nothing */
1647}
1648
1649static struct file_operations dev_fops = {
1650 .owner = THIS_MODULE,
1651 .open = dev_open,
1652 .release = dev_close,
1653 .read = dev_read,
1654 .mmap = dev_mmap,
1655 .ioctl = video_ioctl2,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001656#ifdef CONFIG_COMPAT
1657 .compat_ioctl = v4l_compat_ioctl32,
1658#endif
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001659 .llseek = no_llseek,
1660 .poll = dev_poll,
1661};
1662
Hans Verkuila3998102008-07-21 02:57:38 -03001663static const struct v4l2_ioctl_ops dev_ioctl_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001664 .vidioc_querycap = vidioc_querycap,
1665 .vidioc_dqbuf = vidioc_dqbuf,
1666 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001667 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1668 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1669 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1670 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001671 .vidioc_streamon = vidioc_streamon,
1672 .vidioc_queryctrl = vidioc_queryctrl,
1673 .vidioc_g_ctrl = vidioc_g_ctrl,
1674 .vidioc_s_ctrl = vidioc_s_ctrl,
1675 .vidioc_querymenu = vidioc_querymenu,
1676 .vidioc_enum_input = vidioc_enum_input,
1677 .vidioc_g_input = vidioc_g_input,
1678 .vidioc_s_input = vidioc_s_input,
1679 .vidioc_reqbufs = vidioc_reqbufs,
1680 .vidioc_querybuf = vidioc_querybuf,
1681 .vidioc_streamoff = vidioc_streamoff,
1682 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1683 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1684 .vidioc_g_parm = vidioc_g_parm,
1685 .vidioc_s_parm = vidioc_s_parm,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001686 .vidioc_s_std = vidioc_s_std,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001687#ifdef CONFIG_VIDEO_V4L1_COMPAT
1688 .vidiocgmbuf = vidiocgmbuf,
1689#endif
1690};
1691
Hans Verkuila3998102008-07-21 02:57:38 -03001692static struct video_device gspca_template = {
1693 .name = "gspca main driver",
Hans Verkuila3998102008-07-21 02:57:38 -03001694 .fops = &dev_fops,
1695 .ioctl_ops = &dev_ioctl_ops,
1696 .release = dev_release, /* mandatory */
1697 .minor = -1,
1698};
1699
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001700/*
1701 * probe and create a new gspca device
1702 *
1703 * This function must be called by the sub-driver when it is
1704 * called for probing a new device.
1705 */
1706int gspca_dev_probe(struct usb_interface *intf,
1707 const struct usb_device_id *id,
1708 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001709 int dev_size,
1710 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001711{
1712 struct usb_interface_descriptor *interface;
1713 struct gspca_dev *gspca_dev;
1714 struct usb_device *dev = interface_to_usbdev(intf);
1715 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001716
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001717 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001718
1719 /* we don't handle multi-config cameras */
1720 if (dev->descriptor.bNumConfigurations != 1)
1721 return -ENODEV;
1722 interface = &intf->cur_altsetting->desc;
1723 if (interface->bInterfaceNumber > 0)
1724 return -ENODEV;
1725
1726 /* create the device */
1727 if (dev_size < sizeof *gspca_dev)
1728 dev_size = sizeof *gspca_dev;
1729 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
1730 if (gspca_dev == NULL) {
1731 err("couldn't kzalloc gspca struct");
1732 return -EIO;
1733 }
1734 gspca_dev->dev = dev;
1735 gspca_dev->iface = interface->bInterfaceNumber;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001736 gspca_dev->nbalt = intf->num_altsetting;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001737 gspca_dev->sd_desc = sd_desc;
1738/* gspca_dev->users = 0; (done by kzalloc) */
1739 gspca_dev->nbufread = 2;
1740
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001741 /* configure the subdriver and initialize the USB device */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001742 ret = gspca_dev->sd_desc->config(gspca_dev, id);
1743 if (ret < 0)
1744 goto out;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001745 ret = gspca_dev->sd_desc->init(gspca_dev);
1746 if (ret < 0)
1747 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001748 ret = gspca_set_alt0(gspca_dev);
1749 if (ret < 0)
1750 goto out;
1751 gspca_set_default_mode(gspca_dev);
1752
1753 mutex_init(&gspca_dev->usb_lock);
1754 mutex_init(&gspca_dev->read_lock);
1755 mutex_init(&gspca_dev->queue_lock);
1756 init_waitqueue_head(&gspca_dev->wq);
1757
1758 /* init video stuff */
1759 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
Hans Verkuil5e85e732008-07-20 06:31:39 -03001760 gspca_dev->vdev.parent = &dev->dev;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001761 memcpy(&gspca_dev->fops, &dev_fops, sizeof gspca_dev->fops);
1762 gspca_dev->vdev.fops = &gspca_dev->fops;
1763 gspca_dev->fops.owner = module; /* module protection */
Oliver Neukumee56a4d2008-07-27 14:01:59 -03001764 gspca_dev->present = 1;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001765 ret = video_register_device(&gspca_dev->vdev,
1766 VFL_TYPE_GRABBER,
1767 video_nr);
1768 if (ret < 0) {
1769 err("video_register_device err %d", ret);
1770 goto out;
1771 }
1772
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001773 usb_set_intfdata(intf, gspca_dev);
1774 PDEBUG(D_PROBE, "probe ok");
1775 return 0;
1776out:
1777 kfree(gspca_dev);
1778 return ret;
1779}
1780EXPORT_SYMBOL(gspca_dev_probe);
1781
1782/*
1783 * USB disconnection
1784 *
1785 * This function must be called by the sub-driver
1786 * when the device disconnects, after the specific resources are freed.
1787 */
1788void gspca_disconnect(struct usb_interface *intf)
1789{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001790 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001791
1792 if (!gspca_dev)
1793 return;
1794 gspca_dev->present = 0;
Hans de Goedee2997a72008-04-23 08:09:12 -03001795 mutex_lock(&gspca_dev->queue_lock);
1796 mutex_lock(&gspca_dev->usb_lock);
1797 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001798 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001799 mutex_unlock(&gspca_dev->usb_lock);
Hans de Goedee2997a72008-04-23 08:09:12 -03001800 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001801 while (gspca_dev->users != 0) { /* wait until fully closed */
1802 atomic_inc(&gspca_dev->nevent);
1803 wake_up_interruptible(&gspca_dev->wq); /* wake processes */
1804 schedule();
1805 }
1806/* We don't want people trying to open up the device */
1807 video_unregister_device(&gspca_dev->vdev);
1808/* Free the memory */
1809 kfree(gspca_dev);
1810 PDEBUG(D_PROBE, "disconnect complete");
1811}
1812EXPORT_SYMBOL(gspca_disconnect);
1813
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001814#ifdef CONFIG_PM
1815int gspca_suspend(struct usb_interface *intf, pm_message_t message)
1816{
1817 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1818
1819 if (!gspca_dev->streaming)
1820 return 0;
1821 gspca_dev->frozen = 1; /* avoid urb error messages */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001822 if (gspca_dev->sd_desc->stopN)
1823 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001824 destroy_urbs(gspca_dev);
1825 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001826 if (gspca_dev->sd_desc->stop0)
1827 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001828 return 0;
1829}
1830EXPORT_SYMBOL(gspca_suspend);
1831
1832int gspca_resume(struct usb_interface *intf)
1833{
1834 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1835
1836 gspca_dev->frozen = 0;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001837 gspca_dev->sd_desc->init(gspca_dev);
1838 if (gspca_dev->streaming)
1839 return gspca_init_transfer(gspca_dev);
Jean-Francois Moine1e89e2d2008-09-03 17:12:15 -03001840 return 0;
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001841}
1842EXPORT_SYMBOL(gspca_resume);
1843#endif
Hans de Goededcef3232008-07-10 10:40:53 -03001844/* -- cam driver utility functions -- */
1845
1846/* auto gain and exposure algorithm based on the knee algorithm described here:
1847 http://ytse.tricolour.net/docs/LowLightOptimization.html
1848
1849 Returns 0 if no changes were made, 1 if the gain and or exposure settings
1850 where changed. */
1851int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
1852 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
1853{
1854 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
1855 const struct ctrl *gain_ctrl = NULL;
1856 const struct ctrl *exposure_ctrl = NULL;
1857 const struct ctrl *autogain_ctrl = NULL;
1858 int retval = 0;
1859
1860 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1861 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
1862 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1863 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
1864 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
1865 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
1866 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1867 }
1868 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
1869 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
1870 "on cam without (auto)gain/exposure");
1871 return 0;
1872 }
1873
1874 if (gain_ctrl->get(gspca_dev, &gain) ||
1875 exposure_ctrl->get(gspca_dev, &exposure) ||
1876 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
1877 return 0;
1878
1879 orig_gain = gain;
1880 orig_exposure = exposure;
1881
1882 /* If we are of a multiple of deadzone, do multiple steps to reach the
1883 desired lumination fast (with the risc of a slight overshoot) */
1884 steps = abs(desired_avg_lum - avg_lum) / deadzone;
1885
1886 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d\n",
1887 avg_lum, desired_avg_lum, steps);
1888
1889 for (i = 0; i < steps; i++) {
1890 if (avg_lum > desired_avg_lum) {
1891 if (gain > gain_knee)
1892 gain--;
1893 else if (exposure > exposure_knee)
1894 exposure--;
1895 else if (gain > gain_ctrl->qctrl.default_value)
1896 gain--;
1897 else if (exposure > exposure_ctrl->qctrl.minimum)
1898 exposure--;
1899 else if (gain > gain_ctrl->qctrl.minimum)
1900 gain--;
1901 else
1902 break;
1903 } else {
1904 if (gain < gain_ctrl->qctrl.default_value)
1905 gain++;
1906 else if (exposure < exposure_knee)
1907 exposure++;
1908 else if (gain < gain_knee)
1909 gain++;
1910 else if (exposure < exposure_ctrl->qctrl.maximum)
1911 exposure++;
1912 else if (gain < gain_ctrl->qctrl.maximum)
1913 gain++;
1914 else
1915 break;
1916 }
1917 }
1918
1919 if (gain != orig_gain) {
1920 gain_ctrl->set(gspca_dev, gain);
1921 retval = 1;
1922 }
1923 if (exposure != orig_exposure) {
1924 exposure_ctrl->set(gspca_dev, exposure);
1925 retval = 1;
1926 }
1927
1928 return retval;
1929}
1930EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
1931
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001932/* -- module insert / remove -- */
1933static int __init gspca_init(void)
1934{
Jean-Francois Moinebb64e862008-07-19 06:49:28 -03001935 info("main v%d.%d.%d registered",
1936 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
1937 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
1938 DRIVER_VERSION_NUMBER & 0xff);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001939 return 0;
1940}
1941static void __exit gspca_exit(void)
1942{
1943 info("main deregistered");
1944}
1945
1946module_init(gspca_init);
1947module_exit(gspca_exit);
1948
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001949#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001950module_param_named(debug, gspca_debug, int, 0644);
1951MODULE_PARM_DESC(debug,
1952 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001953 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
1954 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001955#endif