blob: 3f0fd44f19abd62f48bc6218b0e9588a97807e66 [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 Moine3371bcc2008-09-06 04:21:08 -030046#define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 3, 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
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300105/* get the current input frame buffer */
106struct gspca_frame *gspca_get_i_frame(struct gspca_dev *gspca_dev)
107{
108 struct gspca_frame *frame;
109 int i;
110
111 i = gspca_dev->fr_i;
112 i = gspca_dev->fr_queue[i];
113 frame = &gspca_dev->frame[i];
114 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
115 != V4L2_BUF_FLAG_QUEUED)
116 return NULL;
117 return frame;
118}
119EXPORT_SYMBOL(gspca_get_i_frame);
120
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300121/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300122 * fill a video frame from an URB and resubmit
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300123 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300124static void fill_frame(struct gspca_dev *gspca_dev,
125 struct urb *urb)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300126{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300127 struct gspca_frame *frame;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300128 __u8 *data; /* address of data in the iso message */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300129 int i, len, st;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300130 cam_pkt_op pkt_scan;
131
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300132 if (urb->status != 0) {
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300133#ifdef CONFIG_PM
Jean-Francois Moine6a709742008-09-03 16:48:10 -0300134 if (!gspca_dev->frozen)
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300135#endif
Jean-Francois Moine6a709742008-09-03 16:48:10 -0300136 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300137 return; /* disconnection ? */
138 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300139 pkt_scan = gspca_dev->sd_desc->pkt_scan;
140 for (i = 0; i < urb->number_of_packets; i++) {
141
142 /* check the availability of the frame buffer */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300143 frame = gspca_get_i_frame(gspca_dev);
144 if (!frame) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300145 gspca_dev->last_packet_type = DISCARD_PACKET;
146 break;
147 }
148
149 /* check the packet status and length */
150 len = urb->iso_frame_desc[i].actual_length;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300151 if (len == 0)
152 continue;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300153 st = urb->iso_frame_desc[i].status;
154 if (st) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300155 PDEBUG(D_ERR,
156 "ISOC data error: [%d] len=%d, status=%d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300157 i, len, st);
158 gspca_dev->last_packet_type = DISCARD_PACKET;
159 continue;
160 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300161
162 /* let the packet be analyzed by the subdriver */
163 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
164 i, urb->iso_frame_desc[i].offset, len);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300165 data = (__u8 *) urb->transfer_buffer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300166 + urb->iso_frame_desc[i].offset;
167 pkt_scan(gspca_dev, frame, data, len);
168 }
169
170 /* resubmit the URB */
171 urb->status = 0;
172 st = usb_submit_urb(urb, GFP_ATOMIC);
173 if (st < 0)
174 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
175}
176
177/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300178 * ISOC message interrupt from the USB device
179 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300180 * Analyse each packet and call the subdriver for copy to the frame buffer.
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300181 */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300182static void isoc_irq(struct urb *urb
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300183)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300184{
185 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
186
Hans de Goede3647fea2008-07-15 05:36:30 -0300187 PDEBUG(D_PACK, "isoc irq");
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300188 if (!gspca_dev->streaming)
189 return;
190 fill_frame(gspca_dev, urb);
191}
192
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300193/*
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300194 * bulk message interrupt from the USB device
195 */
196static void bulk_irq(struct urb *urb
197)
198{
199 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
200 struct gspca_frame *frame;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300201
202 PDEBUG(D_PACK, "bulk irq");
203 if (!gspca_dev->streaming)
204 return;
205 if (urb->status != 0 && urb->status != -ECONNRESET) {
206#ifdef CONFIG_PM
207 if (!gspca_dev->frozen)
208#endif
209 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
210 return; /* disconnection ? */
211 }
212
213 /* check the availability of the frame buffer */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300214 frame = gspca_get_i_frame(gspca_dev);
215 if (!frame) {
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300216 gspca_dev->last_packet_type = DISCARD_PACKET;
217 } else {
218 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
219 gspca_dev->sd_desc->pkt_scan(gspca_dev,
220 frame,
221 urb->transfer_buffer,
222 urb->actual_length);
223 }
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300224}
225
226/*
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300227 * add data to the current frame
228 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300229 * This function is called by the subdrivers at interrupt level.
230 *
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300231 * To build a frame, these ones must add
232 * - one FIRST_PACKET
233 * - 0 or many INTER_PACKETs
234 * - one LAST_PACKET
235 * DISCARD_PACKET invalidates the whole frame.
236 * On LAST_PACKET, a new frame is returned.
237 */
238struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
239 int packet_type,
240 struct gspca_frame *frame,
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300241 const __u8 *data,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300242 int len)
243{
244 int i, j;
245
Hans de Goedee2997a72008-04-23 08:09:12 -0300246 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300247
248 /* when start of a new frame, if the current frame buffer
249 * is not queued, discard the whole frame */
250 if (packet_type == FIRST_PACKET) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300251 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
252 != V4L2_BUF_FLAG_QUEUED) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300253 gspca_dev->last_packet_type = DISCARD_PACKET;
254 return frame;
255 }
256 frame->data_end = frame->data;
257 jiffies_to_timeval(get_jiffies_64(),
258 &frame->v4l2_buf.timestamp);
259 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300260 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
Jean-Francois Moine06ca78f2008-07-22 03:45:08 -0300261 if (packet_type == LAST_PACKET)
262 gspca_dev->last_packet_type = packet_type;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300263 return frame;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300264 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300265
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300266 /* append the packet to the frame buffer */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300267 if (len > 0) {
268 if (frame->data_end - frame->data + len
269 > frame->v4l2_buf.length) {
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -0300270 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300271 frame->data_end - frame->data + len,
272 frame->v4l2_buf.length);
273 packet_type = DISCARD_PACKET;
274 } else {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300275 memcpy(frame->data_end, data, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300276 frame->data_end += len;
277 }
278 }
279 gspca_dev->last_packet_type = packet_type;
280
Frank Zago219423c2008-09-26 07:43:54 -0300281 /* if last packet, wake up the application and advance in the queue */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300282 if (packet_type == LAST_PACKET) {
283 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
284 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
285 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
286 atomic_inc(&gspca_dev->nevent);
287 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300288 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
289 gspca_dev->fr_i = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300290 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
291 frame->v4l2_buf.bytesused,
292 gspca_dev->fr_q,
293 i,
294 gspca_dev->fr_o);
295 j = gspca_dev->fr_queue[i];
296 frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300297 }
298 return frame;
299}
300EXPORT_SYMBOL(gspca_frame_add);
301
302static int gspca_is_compressed(__u32 format)
303{
304 switch (format) {
305 case V4L2_PIX_FMT_MJPEG:
306 case V4L2_PIX_FMT_JPEG:
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300307 case V4L2_PIX_FMT_SPCA561:
Hans de Goedeab8f12c2008-07-04 18:29:32 -0300308 case V4L2_PIX_FMT_PAC207:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300309 return 1;
310 }
311 return 0;
312}
313
314static void *rvmalloc(unsigned long size)
315{
316 void *mem;
317 unsigned long adr;
318
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300319/* size = PAGE_ALIGN(size); (already done) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300320 mem = vmalloc_32(size);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300321 if (mem != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300322 adr = (unsigned long) mem;
323 while ((long) size > 0) {
324 SetPageReserved(vmalloc_to_page((void *) adr));
325 adr += PAGE_SIZE;
326 size -= PAGE_SIZE;
327 }
328 }
329 return mem;
330}
331
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300332static void rvfree(void *mem, long size)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300333{
334 unsigned long adr;
335
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300336 adr = (unsigned long) mem;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300337 while (size > 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300338 ClearPageReserved(vmalloc_to_page((void *) adr));
339 adr += PAGE_SIZE;
340 size -= PAGE_SIZE;
341 }
342 vfree(mem);
343}
344
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300345static int frame_alloc(struct gspca_dev *gspca_dev,
346 unsigned int count)
347{
348 struct gspca_frame *frame;
349 unsigned int frsz;
350 int i;
351
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300352 i = gspca_dev->curr_mode;
353 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300354 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300355 frsz = PAGE_ALIGN(frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300356 gspca_dev->frsz = frsz;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300357 if (count > GSPCA_MAX_FRAMES)
358 count = GSPCA_MAX_FRAMES;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300359 gspca_dev->frbuf = rvmalloc(frsz * count);
360 if (!gspca_dev->frbuf) {
361 err("frame alloc failed");
362 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300363 }
364 gspca_dev->nframes = count;
365 for (i = 0; i < count; i++) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300366 frame = &gspca_dev->frame[i];
367 frame->v4l2_buf.index = i;
368 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
369 frame->v4l2_buf.flags = 0;
370 frame->v4l2_buf.field = V4L2_FIELD_NONE;
371 frame->v4l2_buf.length = frsz;
372 frame->v4l2_buf.memory = gspca_dev->memory;
373 frame->v4l2_buf.sequence = 0;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300374 frame->data = frame->data_end =
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300375 gspca_dev->frbuf + i * frsz;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300376 frame->v4l2_buf.m.offset = i * frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300377 }
378 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
379 gspca_dev->last_packet_type = DISCARD_PACKET;
380 gspca_dev->sequence = 0;
381 atomic_set(&gspca_dev->nevent, 0);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300382 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300383}
384
385static void frame_free(struct gspca_dev *gspca_dev)
386{
387 int i;
388
389 PDEBUG(D_STREAM, "frame free");
Harvey Harrisonefab8212008-07-05 06:12:47 -0300390 if (gspca_dev->frbuf != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300391 rvfree(gspca_dev->frbuf,
392 gspca_dev->nframes * gspca_dev->frsz);
393 gspca_dev->frbuf = NULL;
394 for (i = 0; i < gspca_dev->nframes; i++)
395 gspca_dev->frame[i].data = NULL;
396 }
397 gspca_dev->nframes = 0;
398}
399
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300400static void destroy_urbs(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300401{
402 struct urb *urb;
403 unsigned int i;
404
405 PDEBUG(D_STREAM, "kill transfer");
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300406 for (i = 0; i < MAX_NURBS; ++i) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300407 urb = gspca_dev->urb[i];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300408 if (urb == NULL)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300409 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300410
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300411 gspca_dev->urb[i] = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300412 usb_kill_urb(urb);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300413 if (urb->transfer_buffer != NULL)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300414 usb_buffer_free(gspca_dev->dev,
415 urb->transfer_buffer_length,
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300416 urb->transfer_buffer,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300417 urb->transfer_dma);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300418 usb_free_urb(urb);
419 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300420}
421
422/*
Frank Zago219423c2008-09-26 07:43:54 -0300423 * look for an input transfer endpoint in an alternate setting
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300424 */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300425static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
426 __u8 epaddr,
427 __u8 xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300428{
429 struct usb_host_endpoint *ep;
430 int i, attr;
431
432 epaddr |= USB_DIR_IN;
433 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
434 ep = &alt->endpoint[i];
435 if (ep->desc.bEndpointAddress == epaddr) {
436 attr = ep->desc.bmAttributes
437 & USB_ENDPOINT_XFERTYPE_MASK;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300438 if (attr == xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300439 return ep;
440 break;
441 }
442 }
443 return NULL;
444}
445
446/*
Frank Zago219423c2008-09-26 07:43:54 -0300447 * look for an input (isoc or bulk) endpoint
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300448 *
449 * The endpoint is defined by the subdriver.
450 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
451 * This routine may be called many times when the bandwidth is too small
452 * (the bandwidth is checked on urb submit).
453 */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300454static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300455{
456 struct usb_interface *intf;
457 struct usb_host_endpoint *ep;
458 int i, ret;
459
460 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300461 ep = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300462 i = gspca_dev->alt; /* previous alt setting */
463 while (--i > 0) { /* alt 0 is unusable */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300464 ep = alt_xfer(&intf->altsetting[i],
465 gspca_dev->cam.epaddr,
466 gspca_dev->bulk
467 ? USB_ENDPOINT_XFER_BULK
468 : USB_ENDPOINT_XFER_ISOC);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300469 if (ep)
470 break;
471 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300472 if (ep == NULL) {
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300473 err("no transfer endpoint found");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300474 return NULL;
475 }
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300476 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300477 i, ep->desc.bEndpointAddress);
478 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
479 if (ret < 0) {
480 err("set interface err %d", ret);
481 return NULL;
482 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300483 gspca_dev->alt = i; /* memorize the current alt setting */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300484 return ep;
485}
486
487/*
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300488 * create the URBs for image transfer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300489 */
490static int create_urbs(struct gspca_dev *gspca_dev,
491 struct usb_host_endpoint *ep)
492{
493 struct urb *urb;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300494 int n, nurbs, i, psize, npkt, bsize;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300495
496 /* calculate the packet size and the number of packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300497 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300498
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300499 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
500 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300501 if (!gspca_dev->bulk) {
502 npkt = ISO_MAX_SIZE / psize;
503 if (npkt > ISO_MAX_PKT)
504 npkt = ISO_MAX_PKT;
505 bsize = psize * npkt;
506 PDEBUG(D_STREAM,
507 "isoc %d pkts size %d = bsize:%d",
508 npkt, psize, bsize);
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300509 nurbs = DEF_NURBS;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300510 } else {
511 npkt = 0;
512 bsize = psize;
513 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300514 nurbs = 1;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300515 }
516
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300517 gspca_dev->nurbs = nurbs;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300518 for (n = 0; n < nurbs; n++) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300519 urb = usb_alloc_urb(npkt, GFP_KERNEL);
520 if (!urb) {
521 err("usb_alloc_urb failed");
Jean-Francois Moine0d0548e2008-09-05 12:49:54 -0300522 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300523 return -ENOMEM;
524 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300525 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300526 bsize,
527 GFP_KERNEL,
528 &urb->transfer_dma);
529
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300530 if (urb->transfer_buffer == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300531 usb_free_urb(urb);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300532 err("usb_buffer_urb failed");
Jean-Francois Moine0d0548e2008-09-05 12:49:54 -0300533 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300534 return -ENOMEM;
535 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300536 gspca_dev->urb[n] = urb;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300537 urb->dev = gspca_dev->dev;
538 urb->context = gspca_dev;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300539 urb->transfer_buffer_length = bsize;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300540 if (npkt != 0) { /* ISOC */
541 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
542 ep->desc.bEndpointAddress);
Frank Zago6c49da72008-09-28 07:43:00 -0300543 urb->transfer_flags = URB_ISO_ASAP
544 | URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300545 urb->interval = ep->desc.bInterval;
546 urb->complete = isoc_irq;
547 urb->number_of_packets = npkt;
548 for (i = 0; i < npkt; i++) {
549 urb->iso_frame_desc[i].length = psize;
550 urb->iso_frame_desc[i].offset = psize * i;
551 }
552 } else { /* bulk */
553 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
554 ep->desc.bEndpointAddress),
Frank Zago6c49da72008-09-28 07:43:00 -0300555 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300556 urb->complete = bulk_irq;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300557 }
558 }
559 return 0;
560}
561
562/*
563 * start the USB transfer
564 */
565static int gspca_init_transfer(struct gspca_dev *gspca_dev)
566{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300567 struct usb_host_endpoint *ep;
568 int n, ret;
569
Hans de Goedee2997a72008-04-23 08:09:12 -0300570 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
571 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300572
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300573 /* set the higher alternate setting and
574 * loop until urb submit succeeds */
575 gspca_dev->alt = gspca_dev->nbalt;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300576 for (;;) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300577 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300578 ep = get_ep(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300579 if (ep == NULL) {
580 ret = -EIO;
581 goto out;
582 }
583 ret = create_urbs(gspca_dev, ep);
584 if (ret < 0)
585 goto out;
586
587 /* start the cam */
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300588 ret = gspca_dev->sd_desc->start(gspca_dev);
589 if (ret < 0) {
590 destroy_urbs(gspca_dev);
591 goto out;
592 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300593 gspca_dev->streaming = 1;
594 atomic_set(&gspca_dev->nevent, 0);
595
Frank Zago219423c2008-09-26 07:43:54 -0300596 /* bulk transfers are started by the subdriver */
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300597 if (gspca_dev->bulk)
598 break;
599
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300600 /* submit the URBs */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300601 for (n = 0; n < gspca_dev->nurbs; n++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300602 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300603 if (ret < 0) {
604 PDEBUG(D_ERR|D_STREAM,
605 "usb_submit_urb [%d] err %d", n, ret);
Hans de Goedee2997a72008-04-23 08:09:12 -0300606 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300607 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300608 if (ret == -ENOSPC)
609 break; /* try the previous alt */
610 goto out;
611 }
612 }
613 if (ret >= 0)
614 break;
615 }
616out:
617 mutex_unlock(&gspca_dev->usb_lock);
618 return ret;
619}
620
621static int gspca_set_alt0(struct gspca_dev *gspca_dev)
622{
623 int ret;
624
625 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
626 if (ret < 0)
627 PDEBUG(D_ERR|D_STREAM, "set interface 0 err %d", ret);
628 return ret;
629}
630
Frank Zago219423c2008-09-26 07:43:54 -0300631/* Note: both the queue and the usb locks should be held when calling this */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300632static void gspca_stream_off(struct gspca_dev *gspca_dev)
633{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300634 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300635 atomic_set(&gspca_dev->nevent, 0);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300636 if (gspca_dev->present) {
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300637 if (gspca_dev->sd_desc->stopN)
638 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300639 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300640 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300641 if (gspca_dev->sd_desc->stop0)
642 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300643 PDEBUG(D_STREAM, "stream off OK");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300644 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300645}
646
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300647static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300648{
649 int i;
650
651 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
652 gspca_dev->curr_mode = i;
653 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
654 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300655 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300656}
657
658static int wxh_to_mode(struct gspca_dev *gspca_dev,
659 int width, int height)
660{
661 int i;
662
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300663 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
664 if (width >= gspca_dev->cam.cam_mode[i].width
665 && height >= gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300666 break;
667 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300668 return i;
669}
670
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300671/*
672 * search a mode with the right pixel format
673 */
674static int gspca_get_mode(struct gspca_dev *gspca_dev,
675 int mode,
676 int pixfmt)
677{
678 int modeU, modeD;
679
680 modeU = modeD = mode;
681 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
682 if (--modeD >= 0) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300683 if (gspca_dev->cam.cam_mode[modeD].pixelformat
684 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300685 return modeD;
686 }
687 if (++modeU < gspca_dev->cam.nmodes) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300688 if (gspca_dev->cam.cam_mode[modeU].pixelformat
689 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300690 return modeU;
691 }
692 }
693 return -EINVAL;
694}
695
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300696static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300697 struct v4l2_fmtdesc *fmtdesc)
698{
699 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300700 int i, j, index;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300701 __u32 fmt_tb[8];
702
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300703 /* give an index to each format */
704 index = 0;
705 j = 0;
706 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300707 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300708 j = 0;
709 for (;;) {
710 if (fmt_tb[j] == fmt_tb[index])
711 break;
712 j++;
713 }
714 if (j == index) {
715 if (fmtdesc->index == index)
716 break; /* new format */
717 index++;
718 if (index >= sizeof fmt_tb / sizeof fmt_tb[0])
719 return -EINVAL;
720 }
721 }
722 if (i < 0)
723 return -EINVAL; /* no more format */
724
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300725 fmtdesc->pixelformat = fmt_tb[index];
726 if (gspca_is_compressed(fmt_tb[index]))
727 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300728 fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300729 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
730 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
731 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
732 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
733 fmtdesc->description[4] = '\0';
734 return 0;
735}
736
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300737static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300738 struct v4l2_format *fmt)
739{
740 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300741 int mode;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300742
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300743 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
744 return -EINVAL;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300745 mode = gspca_dev->curr_mode;
746 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
747 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300748 return 0;
749}
750
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300751static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300752 struct v4l2_format *fmt)
753{
Hans de Goede24c530b2008-07-04 17:55:18 -0300754 int w, h, mode, mode2;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300755
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300756 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
757 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300758 w = fmt->fmt.pix.width;
759 h = fmt->fmt.pix.height;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300760
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300761#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300762 if (gspca_debug & D_CONF)
763 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
764#endif
765 /* search the closest mode for width and height */
766 mode = wxh_to_mode(gspca_dev, w, h);
767
768 /* OK if right palette */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300769 if (gspca_dev->cam.cam_mode[mode].pixelformat
770 != fmt->fmt.pix.pixelformat) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300771
772 /* else, search the closest mode with the same pixel format */
773 mode2 = gspca_get_mode(gspca_dev, mode,
774 fmt->fmt.pix.pixelformat);
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300775 if (mode2 >= 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300776 mode = mode2;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300777/* else
778 ; * no chance, return this mode */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300779 }
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300780 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
781 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300782 return mode; /* used when s_fmt */
783}
784
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300785static int vidioc_try_fmt_vid_cap(struct file *file,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300786 void *priv,
787 struct v4l2_format *fmt)
788{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300789 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300790 int ret;
791
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300792 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300793 if (ret < 0)
794 return ret;
795 return 0;
796}
797
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300798static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300799 struct v4l2_format *fmt)
800{
801 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300802 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300803
Hans de Goedee2997a72008-04-23 08:09:12 -0300804 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
805 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300806
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300807 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300808 if (ret < 0)
809 goto out;
810
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300811 if (gspca_dev->nframes != 0
812 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
813 ret = -EINVAL;
814 goto out;
815 }
816
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300817 if (ret == gspca_dev->curr_mode) {
818 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300819 goto out; /* same mode */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300820 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300821
822 if (gspca_dev->streaming) {
823 ret = -EBUSY;
824 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -0300825 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300826 gspca_dev->width = fmt->fmt.pix.width;
827 gspca_dev->height = fmt->fmt.pix.height;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300828 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
829 gspca_dev->curr_mode = ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300830
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300831 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300832out:
833 mutex_unlock(&gspca_dev->queue_lock);
834 return ret;
835}
836
837static int dev_open(struct inode *inode, struct file *file)
838{
839 struct gspca_dev *gspca_dev;
840 int ret;
841
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300842 PDEBUG(D_STREAM, "%s open", current->comm);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300843 gspca_dev = (struct gspca_dev *) video_devdata(file);
Hans de Goedee2997a72008-04-23 08:09:12 -0300844 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
845 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300846 if (!gspca_dev->present) {
847 ret = -ENODEV;
848 goto out;
849 }
850
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300851 if (gspca_dev->users > 4) { /* (arbitrary value) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300852 ret = -EBUSY;
853 goto out;
854 }
855 gspca_dev->users++;
856 file->private_data = gspca_dev;
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300857#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300858 /* activate the v4l2 debug */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300859 if (gspca_debug & D_V4L2)
Frank Zago9de436c2008-09-15 05:20:38 -0300860 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
861 | V4L2_DEBUG_IOCTL_ARG;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300862 else
Frank Zago9de436c2008-09-15 05:20:38 -0300863 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
864 | V4L2_DEBUG_IOCTL_ARG);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300865#endif
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300866 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300867out:
868 mutex_unlock(&gspca_dev->queue_lock);
869 if (ret != 0)
870 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
871 else
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300872 PDEBUG(D_STREAM, "open done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300873 return ret;
874}
875
876static int dev_close(struct inode *inode, struct file *file)
877{
878 struct gspca_dev *gspca_dev = file->private_data;
879
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300880 PDEBUG(D_STREAM, "%s close", current->comm);
Hans de Goedee2997a72008-04-23 08:09:12 -0300881 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
882 return -ERESTARTSYS;
883 gspca_dev->users--;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300884
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -0300885 /* if the file did the capture, free the streaming resources */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300886 if (gspca_dev->capt_file == file) {
Jean-Francois Moinea1e82882008-09-03 17:12:17 -0300887 if (gspca_dev->streaming) {
888 mutex_lock(&gspca_dev->usb_lock);
889 gspca_stream_off(gspca_dev);
890 mutex_unlock(&gspca_dev->usb_lock);
891 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300892 frame_free(gspca_dev);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300893 gspca_dev->capt_file = NULL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300894 gspca_dev->memory = GSPCA_MEMORY_NO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300895 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300896 file->private_data = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300897 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300898 PDEBUG(D_STREAM, "close done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300899 return 0;
900}
901
902static int vidioc_querycap(struct file *file, void *priv,
903 struct v4l2_capability *cap)
904{
905 struct gspca_dev *gspca_dev = priv;
906
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300907 memset(cap, 0, sizeof *cap);
908 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -0300909/* strncpy(cap->card, gspca_dev->cam.dev_name, sizeof cap->card); */
910 if (gspca_dev->dev->product != NULL) {
911 strncpy(cap->card, gspca_dev->dev->product,
912 sizeof cap->card);
913 } else {
914 snprintf(cap->card, sizeof cap->card,
915 "USB Camera (%04x:%04x)",
916 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
917 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
918 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300919 strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name,
920 sizeof cap->bus_info);
921 cap->version = DRIVER_VERSION_NUMBER;
922 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
923 | V4L2_CAP_STREAMING
924 | V4L2_CAP_READWRITE;
925 return 0;
926}
927
928static int vidioc_queryctrl(struct file *file, void *priv,
929 struct v4l2_queryctrl *q_ctrl)
930{
931 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300932 int i, ix;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300933 u32 id;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300934
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300935 ix = -1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300936 id = q_ctrl->id;
937 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
938 id &= V4L2_CTRL_ID_MASK;
939 id++;
940 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goede0f523c22008-09-03 17:12:14 -0300941 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300942 continue;
943 if (ix < 0) {
944 ix = i;
945 continue;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300946 }
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300947 if (gspca_dev->sd_desc->ctrls[i].qctrl.id
948 > gspca_dev->sd_desc->ctrls[ix].qctrl.id)
949 continue;
950 ix = i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300951 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300952 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300953 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300954 if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) {
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300955 ix = i;
956 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300957 }
958 }
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300959 if (ix < 0)
960 return -EINVAL;
961 memcpy(q_ctrl, &gspca_dev->sd_desc->ctrls[ix].qctrl,
962 sizeof *q_ctrl);
963 if (gspca_dev->ctrl_dis & (1 << ix))
964 q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
965 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300966}
967
968static int vidioc_s_ctrl(struct file *file, void *priv,
969 struct v4l2_control *ctrl)
970{
971 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300972 const struct ctrl *ctrls;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300973 int i, ret;
974
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300975 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
976 i < gspca_dev->sd_desc->nctrls;
977 i++, ctrls++) {
978 if (ctrl->id != ctrls->qctrl.id)
979 continue;
Hans de Goede0f523c22008-09-03 17:12:14 -0300980 if (gspca_dev->ctrl_dis & (1 << i))
981 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300982 if (ctrl->value < ctrls->qctrl.minimum
Rabin Vincentfcf5cb22008-08-03 07:58:54 -0300983 || ctrl->value > ctrls->qctrl.maximum)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300984 return -ERANGE;
985 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
Hans de Goedee2997a72008-04-23 08:09:12 -0300986 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
987 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300988 ret = ctrls->set(gspca_dev, ctrl->value);
989 mutex_unlock(&gspca_dev->usb_lock);
990 return ret;
991 }
992 return -EINVAL;
993}
994
995static int vidioc_g_ctrl(struct file *file, void *priv,
996 struct v4l2_control *ctrl)
997{
998 struct gspca_dev *gspca_dev = priv;
999
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001000 const struct ctrl *ctrls;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001001 int i, ret;
1002
1003 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1004 i < gspca_dev->sd_desc->nctrls;
1005 i++, ctrls++) {
1006 if (ctrl->id != ctrls->qctrl.id)
1007 continue;
Hans de Goede0f523c22008-09-03 17:12:14 -03001008 if (gspca_dev->ctrl_dis & (1 << i))
1009 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001010 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1011 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001012 ret = ctrls->get(gspca_dev, &ctrl->value);
1013 mutex_unlock(&gspca_dev->usb_lock);
1014 return ret;
1015 }
1016 return -EINVAL;
1017}
1018
1019static int vidioc_querymenu(struct file *file, void *priv,
1020 struct v4l2_querymenu *qmenu)
1021{
1022 struct gspca_dev *gspca_dev = priv;
1023
1024 if (!gspca_dev->sd_desc->querymenu)
1025 return -EINVAL;
1026 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1027}
1028
1029static int vidioc_enum_input(struct file *file, void *priv,
1030 struct v4l2_input *input)
1031{
1032 struct gspca_dev *gspca_dev = priv;
1033
1034 if (input->index != 0)
1035 return -EINVAL;
1036 memset(input, 0, sizeof *input);
1037 input->type = V4L2_INPUT_TYPE_CAMERA;
1038 strncpy(input->name, gspca_dev->sd_desc->name,
1039 sizeof input->name);
1040 return 0;
1041}
1042
1043static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1044{
1045 *i = 0;
1046 return 0;
1047}
1048
1049static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1050{
1051 if (i > 0)
1052 return -EINVAL;
1053 return (0);
1054}
1055
1056static int vidioc_reqbufs(struct file *file, void *priv,
1057 struct v4l2_requestbuffers *rb)
1058{
1059 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001060 int i, ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001061
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001062 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1063 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001064 switch (rb->memory) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001065 case GSPCA_MEMORY_READ: /* (internal call) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001066 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001067 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001068 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001069 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001070 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001071 }
Hans de Goedee2997a72008-04-23 08:09:12 -03001072 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1073 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001074
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001075 if (gspca_dev->memory != GSPCA_MEMORY_NO
1076 && gspca_dev->memory != rb->memory) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001077 ret = -EBUSY;
1078 goto out;
1079 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001080
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001081 /* only one file may do the capture */
1082 if (gspca_dev->capt_file != NULL
1083 && gspca_dev->capt_file != file) {
1084 ret = -EBUSY;
1085 goto out;
1086 }
1087
1088 /* if allocated, the buffers must not be mapped */
1089 for (i = 0; i < gspca_dev->nframes; i++) {
1090 if (gspca_dev->frame[i].vma_use_count) {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001091 ret = -EBUSY;
1092 goto out;
1093 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001094 }
1095
1096 /* stop streaming */
1097 if (gspca_dev->streaming) {
1098 mutex_lock(&gspca_dev->usb_lock);
1099 gspca_stream_off(gspca_dev);
1100 mutex_unlock(&gspca_dev->usb_lock);
1101 }
1102
1103 /* free the previous allocated buffers, if any */
1104 if (gspca_dev->nframes != 0) {
1105 frame_free(gspca_dev);
1106 gspca_dev->capt_file = NULL;
1107 }
1108 if (rb->count == 0) /* unrequest */
1109 goto out;
1110 gspca_dev->memory = rb->memory;
1111 ret = frame_alloc(gspca_dev, rb->count);
1112 if (ret == 0) {
1113 rb->count = gspca_dev->nframes;
1114 gspca_dev->capt_file = file;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001115 }
1116out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001117 mutex_unlock(&gspca_dev->queue_lock);
1118 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1119 return ret;
1120}
1121
1122static int vidioc_querybuf(struct file *file, void *priv,
1123 struct v4l2_buffer *v4l2_buf)
1124{
1125 struct gspca_dev *gspca_dev = priv;
1126 struct gspca_frame *frame;
1127
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001128 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1129 || v4l2_buf->index < 0
1130 || v4l2_buf->index >= gspca_dev->nframes)
1131 return -EINVAL;
1132
1133 frame = &gspca_dev->frame[v4l2_buf->index];
1134 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1135 return 0;
1136}
1137
1138static int vidioc_streamon(struct file *file, void *priv,
1139 enum v4l2_buf_type buf_type)
1140{
1141 struct gspca_dev *gspca_dev = priv;
1142 int ret;
1143
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001144 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1145 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001146 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1147 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001148 if (!gspca_dev->present) {
1149 ret = -ENODEV;
1150 goto out;
1151 }
1152 if (gspca_dev->nframes == 0) {
1153 ret = -EINVAL;
1154 goto out;
1155 }
1156 if (!gspca_dev->streaming) {
1157 ret = gspca_init_transfer(gspca_dev);
1158 if (ret < 0)
1159 goto out;
1160 }
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001161#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001162 if (gspca_debug & D_STREAM) {
1163 PDEBUG_MODE("stream on OK",
1164 gspca_dev->pixfmt,
1165 gspca_dev->width,
1166 gspca_dev->height);
1167 }
1168#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001169 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001170out:
1171 mutex_unlock(&gspca_dev->queue_lock);
1172 return ret;
1173}
1174
1175static int vidioc_streamoff(struct file *file, void *priv,
1176 enum v4l2_buf_type buf_type)
1177{
1178 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001179 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001180
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001181 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1182 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001183 if (!gspca_dev->streaming)
1184 return 0;
1185 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1186 return -ERESTARTSYS;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001187
1188 /* stop streaming */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001189 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1190 ret = -ERESTARTSYS;
1191 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001192 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001193 gspca_stream_off(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001194 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001195
1196 /* empty the application queues */
1197 for (i = 0; i < gspca_dev->nframes; i++)
1198 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1199 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1200 gspca_dev->last_packet_type = DISCARD_PACKET;
1201 gspca_dev->sequence = 0;
1202 atomic_set(&gspca_dev->nevent, 0);
1203 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001204out:
1205 mutex_unlock(&gspca_dev->queue_lock);
1206 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001207}
1208
1209static int vidioc_g_jpegcomp(struct file *file, void *priv,
1210 struct v4l2_jpegcompression *jpegcomp)
1211{
1212 struct gspca_dev *gspca_dev = priv;
1213 int ret;
1214
1215 if (!gspca_dev->sd_desc->get_jcomp)
1216 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001217 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1218 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001219 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1220 mutex_unlock(&gspca_dev->usb_lock);
1221 return ret;
1222}
1223
1224static int vidioc_s_jpegcomp(struct file *file, void *priv,
1225 struct v4l2_jpegcompression *jpegcomp)
1226{
1227 struct gspca_dev *gspca_dev = priv;
1228 int ret;
1229
Hans de Goedee2997a72008-04-23 08:09:12 -03001230 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1231 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001232 if (!gspca_dev->sd_desc->set_jcomp)
1233 return -EINVAL;
1234 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1235 mutex_unlock(&gspca_dev->usb_lock);
1236 return ret;
1237}
1238
1239static int vidioc_g_parm(struct file *filp, void *priv,
1240 struct v4l2_streamparm *parm)
1241{
1242 struct gspca_dev *gspca_dev = priv;
1243
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001244 memset(parm, 0, sizeof *parm);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001245 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1246 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1247 return 0;
1248}
1249
1250static int vidioc_s_parm(struct file *filp, void *priv,
1251 struct v4l2_streamparm *parm)
1252{
1253 struct gspca_dev *gspca_dev = priv;
1254 int n;
1255
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001256 n = parm->parm.capture.readbuffers;
1257 if (n == 0 || n > GSPCA_MAX_FRAMES)
1258 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1259 else
1260 gspca_dev->nbufread = n;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001261 return 0;
1262}
1263
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001264static int vidioc_s_std(struct file *filp, void *priv,
1265 v4l2_std_id *parm)
1266{
1267 return 0;
1268}
1269
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001270#ifdef CONFIG_VIDEO_V4L1_COMPAT
1271static int vidiocgmbuf(struct file *file, void *priv,
1272 struct video_mbuf *mbuf)
1273{
1274 struct gspca_dev *gspca_dev = file->private_data;
1275 int i;
1276
1277 PDEBUG(D_STREAM, "cgmbuf");
1278 if (gspca_dev->nframes == 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001279 int ret;
1280
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001281 {
1282 struct v4l2_format fmt;
1283
1284 memset(&fmt, 0, sizeof fmt);
1285 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1286 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1287 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1288 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1289 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001290 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001291 if (ret != 0)
1292 return ret;
1293 }
1294 {
1295 struct v4l2_requestbuffers rb;
1296
1297 memset(&rb, 0, sizeof rb);
1298 rb.count = 4;
1299 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1300 rb.memory = V4L2_MEMORY_MMAP;
1301 ret = vidioc_reqbufs(file, priv, &rb);
1302 if (ret != 0)
1303 return ret;
1304 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001305 }
1306 mbuf->frames = gspca_dev->nframes;
1307 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1308 for (i = 0; i < mbuf->frames; i++)
1309 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1310 return 0;
1311}
1312#endif
1313
1314static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1315{
1316 struct gspca_dev *gspca_dev = file->private_data;
Harvey Harrisonefab8212008-07-05 06:12:47 -03001317 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001318 struct page *page;
1319 unsigned long addr, start, size;
1320 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001321
1322 start = vma->vm_start;
1323 size = vma->vm_end - vma->vm_start;
1324 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1325
Hans de Goedee2997a72008-04-23 08:09:12 -03001326 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1327 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001328 if (!gspca_dev->present) {
1329 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001330 goto out;
1331 }
1332 if (gspca_dev->capt_file != file) {
1333 ret = -EINVAL;
1334 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001335 }
1336
Harvey Harrisonefab8212008-07-05 06:12:47 -03001337 frame = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001338 for (i = 0; i < gspca_dev->nframes; ++i) {
1339 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1340 PDEBUG(D_STREAM, "mmap bad memory type");
1341 break;
1342 }
1343 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1344 == vma->vm_pgoff) {
1345 frame = &gspca_dev->frame[i];
1346 break;
1347 }
1348 }
Harvey Harrisonefab8212008-07-05 06:12:47 -03001349 if (frame == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001350 PDEBUG(D_STREAM, "mmap no frame buffer found");
1351 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001352 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001353 }
1354#ifdef CONFIG_VIDEO_V4L1_COMPAT
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001355 /* v4l1 maps all the buffers */
1356 if (i != 0
1357 || size != frame->v4l2_buf.length * gspca_dev->nframes)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001358#endif
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001359 if (size != frame->v4l2_buf.length) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001360 PDEBUG(D_STREAM, "mmap bad size");
1361 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001362 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001363 }
1364
1365 /*
1366 * - VM_IO marks the area as being a mmaped region for I/O to a
1367 * device. It also prevents the region from being core dumped.
1368 */
1369 vma->vm_flags |= VM_IO;
1370
1371 addr = (unsigned long) frame->data;
1372 while (size > 0) {
1373 page = vmalloc_to_page((void *) addr);
1374 ret = vm_insert_page(vma, start, page);
1375 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001376 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001377 start += PAGE_SIZE;
1378 addr += PAGE_SIZE;
1379 size -= PAGE_SIZE;
1380 }
1381
1382 vma->vm_ops = &gspca_vm_ops;
1383 vma->vm_private_data = frame;
1384 gspca_vm_open(vma);
Hans de Goedee2997a72008-04-23 08:09:12 -03001385 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001386out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001387 mutex_unlock(&gspca_dev->queue_lock);
1388 return ret;
1389}
1390
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001391/*
1392 * wait for a video frame
1393 *
1394 * If a frame is ready, its index is returned.
1395 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001396static int frame_wait(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001397 int nonblock_ing)
1398{
1399 struct gspca_frame *frame;
1400 int i, j, ret;
1401
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001402 /* check if a frame is ready */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001403 i = gspca_dev->fr_o;
1404 j = gspca_dev->fr_queue[i];
1405 frame = &gspca_dev->frame[j];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001406 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) {
1407 atomic_dec(&gspca_dev->nevent);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001408 goto ok;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001409 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001410 if (nonblock_ing) /* no frame yet */
1411 return -EAGAIN;
1412
1413 /* wait till a frame is ready */
1414 for (;;) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001415 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1416 atomic_read(&gspca_dev->nevent) > 0,
1417 msecs_to_jiffies(3000));
1418 if (ret <= 0) {
1419 if (ret < 0)
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001420 return ret; /* interrupt */
1421 return -EIO; /* timeout */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001422 }
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001423 atomic_dec(&gspca_dev->nevent);
Hans de Goedee2997a72008-04-23 08:09:12 -03001424 if (!gspca_dev->streaming || !gspca_dev->present)
1425 return -EIO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001426 i = gspca_dev->fr_o;
1427 j = gspca_dev->fr_queue[i];
1428 frame = &gspca_dev->frame[j];
1429 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1430 break;
1431 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001432ok:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001433 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1434 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1435 gspca_dev->fr_q,
1436 gspca_dev->fr_i,
1437 gspca_dev->fr_o);
Hans de Goedee2997a72008-04-23 08:09:12 -03001438
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001439 if (gspca_dev->sd_desc->dq_callback) {
1440 mutex_lock(&gspca_dev->usb_lock);
Hans de Goedee2997a72008-04-23 08:09:12 -03001441 gspca_dev->sd_desc->dq_callback(gspca_dev);
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001442 mutex_unlock(&gspca_dev->usb_lock);
1443 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001444 return j;
1445}
1446
1447/*
1448 * dequeue a video buffer
1449 *
1450 * If nonblock_ing is false, block until a buffer is available.
1451 */
1452static int vidioc_dqbuf(struct file *file, void *priv,
1453 struct v4l2_buffer *v4l2_buf)
1454{
1455 struct gspca_dev *gspca_dev = priv;
1456 struct gspca_frame *frame;
1457 int i, ret;
1458
1459 PDEBUG(D_FRAM, "dqbuf");
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001460 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1461 return -EINVAL;
1462 if (v4l2_buf->memory != gspca_dev->memory)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001463 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001464
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001465 /* if not streaming, be sure the application will not loop forever */
1466 if (!(file->f_flags & O_NONBLOCK)
1467 && !gspca_dev->streaming && gspca_dev->users == 1)
1468 return -EINVAL;
1469
1470 /* only the capturing file may dequeue */
1471 if (gspca_dev->capt_file != file)
1472 return -EINVAL;
1473
1474 /* only one dequeue / read at a time */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001475 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1476 return -ERESTARTSYS;
1477
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001478 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001479 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001480 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001481 i = ret; /* frame index */
1482 frame = &gspca_dev->frame[i];
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001483 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001484 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001485 frame->data,
1486 frame->v4l2_buf.bytesused)) {
1487 PDEBUG(D_ERR|D_STREAM,
1488 "dqbuf cp to user failed");
1489 ret = -EFAULT;
1490 goto out;
1491 }
1492 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001493 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1494 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1495 PDEBUG(D_FRAM, "dqbuf %d", i);
1496 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001497out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001498 mutex_unlock(&gspca_dev->read_lock);
1499 return ret;
1500}
1501
1502/*
1503 * queue a video buffer
1504 *
1505 * Attempting to queue a buffer that has already been
1506 * queued will return -EINVAL.
1507 */
1508static int vidioc_qbuf(struct file *file, void *priv,
1509 struct v4l2_buffer *v4l2_buf)
1510{
1511 struct gspca_dev *gspca_dev = priv;
1512 struct gspca_frame *frame;
1513 int i, index, ret;
1514
1515 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1516 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1517 return -EINVAL;
1518
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001519 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1520 return -ERESTARTSYS;
1521
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001522 index = v4l2_buf->index;
1523 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001524 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001525 "qbuf idx %d >= %d", index, gspca_dev->nframes);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001526 ret = -EINVAL;
1527 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001528 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001529 if (v4l2_buf->memory != gspca_dev->memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001530 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001531 ret = -EINVAL;
1532 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001533 }
1534
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001535 frame = &gspca_dev->frame[index];
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001536 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1537 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001538 ret = -EINVAL;
1539 goto out;
1540 }
1541
1542 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001543/* frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001544
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001545 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001546 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1547 frame->v4l2_buf.length = v4l2_buf->length;
1548 }
1549
1550 /* put the buffer in the 'queued' queue */
1551 i = gspca_dev->fr_q;
1552 gspca_dev->fr_queue[i] = index;
1553 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1554 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1555 gspca_dev->fr_q,
1556 gspca_dev->fr_i,
1557 gspca_dev->fr_o);
1558
1559 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1560 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1561 ret = 0;
1562out:
1563 mutex_unlock(&gspca_dev->queue_lock);
1564 return ret;
1565}
1566
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001567/*
1568 * allocate the resources for read()
1569 */
1570static int read_alloc(struct gspca_dev *gspca_dev,
1571 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001572{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001573 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001574 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001575
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001576 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001577 if (gspca_dev->nframes == 0) {
1578 struct v4l2_requestbuffers rb;
1579
1580 memset(&rb, 0, sizeof rb);
1581 rb.count = gspca_dev->nbufread;
1582 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001583 rb.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001584 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1585 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001586 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001587 return ret;
1588 }
1589 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1590 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001591 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001592 for (i = 0; i < gspca_dev->nbufread; i++) {
1593 v4l2_buf.index = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001594 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1595 if (ret != 0) {
1596 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1597 return ret;
1598 }
1599 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001600 gspca_dev->memory = GSPCA_MEMORY_READ;
1601 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001602
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001603 /* start streaming */
1604 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1605 if (ret != 0)
1606 PDEBUG(D_STREAM, "read streamon err %d", ret);
1607 return ret;
1608}
1609
1610static unsigned int dev_poll(struct file *file, poll_table *wait)
1611{
1612 struct gspca_dev *gspca_dev = file->private_data;
1613 int i, ret;
1614
1615 PDEBUG(D_FRAM, "poll");
1616
1617 poll_wait(file, &gspca_dev->wq, wait);
1618 if (!gspca_dev->present)
1619 return POLLERR;
1620
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001621 /* if reqbufs is not done, the user would use read() */
1622 if (gspca_dev->nframes == 0) {
1623 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1624 return POLLERR; /* not the 1st time */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001625 ret = read_alloc(gspca_dev, file);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001626 if (ret != 0)
1627 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001628 }
1629
1630 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1631 return POLLERR;
1632 if (!gspca_dev->present) {
1633 ret = POLLERR;
1634 goto out;
1635 }
1636
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001637 /* check the next incoming buffer */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001638 i = gspca_dev->fr_o;
1639 i = gspca_dev->fr_queue[i];
1640 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1641 ret = POLLIN | POLLRDNORM; /* something to read */
1642 else
1643 ret = 0;
1644out:
1645 mutex_unlock(&gspca_dev->queue_lock);
1646 return ret;
1647}
1648
1649static ssize_t dev_read(struct file *file, char __user *data,
1650 size_t count, loff_t *ppos)
1651{
1652 struct gspca_dev *gspca_dev = file->private_data;
1653 struct gspca_frame *frame;
1654 struct v4l2_buffer v4l2_buf;
1655 struct timeval timestamp;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001656 int n, ret, ret2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001657
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001658 PDEBUG(D_FRAM, "read (%zd)", count);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001659 if (!gspca_dev->present)
1660 return -ENODEV;
1661 switch (gspca_dev->memory) {
1662 case GSPCA_MEMORY_NO: /* first time */
1663 ret = read_alloc(gspca_dev, file);
1664 if (ret != 0)
1665 return ret;
1666 break;
1667 case GSPCA_MEMORY_READ:
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001668 if (gspca_dev->capt_file == file)
1669 break;
1670 /* fall thru */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001671 default:
1672 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001673 }
1674
1675 /* get a frame */
1676 jiffies_to_timeval(get_jiffies_64(), &timestamp);
1677 timestamp.tv_sec--;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001678 n = 2;
1679 for (;;) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001680 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1681 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001682 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001683 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1684 if (ret != 0) {
1685 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1686 return ret;
1687 }
1688
1689 /* if the process slept for more than 1 second,
Frank Zago219423c2008-09-26 07:43:54 -03001690 * get a newer frame */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001691 frame = &gspca_dev->frame[v4l2_buf.index];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001692 if (--n < 0)
1693 break; /* avoid infinite loop */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001694 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1695 break;
1696 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1697 if (ret != 0) {
1698 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1699 return ret;
1700 }
1701 }
1702
1703 /* copy the frame */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001704 if (count > frame->v4l2_buf.bytesused)
1705 count = frame->v4l2_buf.bytesused;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001706 ret = copy_to_user(data, frame->data, count);
1707 if (ret != 0) {
1708 PDEBUG(D_ERR|D_STREAM,
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001709 "read cp to user lack %d / %zd", ret, count);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001710 ret = -EFAULT;
1711 goto out;
1712 }
1713 ret = count;
1714out:
1715 /* in each case, requeue the buffer */
1716 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1717 if (ret2 != 0)
1718 return ret2;
1719 return ret;
1720}
1721
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001722static void dev_release(struct video_device *vfd)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001723{
1724 /* nothing */
1725}
1726
1727static struct file_operations dev_fops = {
1728 .owner = THIS_MODULE,
1729 .open = dev_open,
1730 .release = dev_close,
1731 .read = dev_read,
1732 .mmap = dev_mmap,
1733 .ioctl = video_ioctl2,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001734#ifdef CONFIG_COMPAT
1735 .compat_ioctl = v4l_compat_ioctl32,
1736#endif
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001737 .llseek = no_llseek,
1738 .poll = dev_poll,
1739};
1740
Hans Verkuila3998102008-07-21 02:57:38 -03001741static const struct v4l2_ioctl_ops dev_ioctl_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001742 .vidioc_querycap = vidioc_querycap,
1743 .vidioc_dqbuf = vidioc_dqbuf,
1744 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001745 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1746 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1747 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1748 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001749 .vidioc_streamon = vidioc_streamon,
1750 .vidioc_queryctrl = vidioc_queryctrl,
1751 .vidioc_g_ctrl = vidioc_g_ctrl,
1752 .vidioc_s_ctrl = vidioc_s_ctrl,
1753 .vidioc_querymenu = vidioc_querymenu,
1754 .vidioc_enum_input = vidioc_enum_input,
1755 .vidioc_g_input = vidioc_g_input,
1756 .vidioc_s_input = vidioc_s_input,
1757 .vidioc_reqbufs = vidioc_reqbufs,
1758 .vidioc_querybuf = vidioc_querybuf,
1759 .vidioc_streamoff = vidioc_streamoff,
1760 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1761 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1762 .vidioc_g_parm = vidioc_g_parm,
1763 .vidioc_s_parm = vidioc_s_parm,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001764 .vidioc_s_std = vidioc_s_std,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001765#ifdef CONFIG_VIDEO_V4L1_COMPAT
1766 .vidiocgmbuf = vidiocgmbuf,
1767#endif
1768};
1769
Hans Verkuila3998102008-07-21 02:57:38 -03001770static struct video_device gspca_template = {
1771 .name = "gspca main driver",
Hans Verkuila3998102008-07-21 02:57:38 -03001772 .fops = &dev_fops,
1773 .ioctl_ops = &dev_ioctl_ops,
1774 .release = dev_release, /* mandatory */
1775 .minor = -1,
1776};
1777
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001778/*
1779 * probe and create a new gspca device
1780 *
1781 * This function must be called by the sub-driver when it is
1782 * called for probing a new device.
1783 */
1784int gspca_dev_probe(struct usb_interface *intf,
1785 const struct usb_device_id *id,
1786 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001787 int dev_size,
1788 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001789{
1790 struct usb_interface_descriptor *interface;
1791 struct gspca_dev *gspca_dev;
1792 struct usb_device *dev = interface_to_usbdev(intf);
1793 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001794
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001795 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001796
1797 /* we don't handle multi-config cameras */
1798 if (dev->descriptor.bNumConfigurations != 1)
1799 return -ENODEV;
1800 interface = &intf->cur_altsetting->desc;
1801 if (interface->bInterfaceNumber > 0)
1802 return -ENODEV;
1803
1804 /* create the device */
1805 if (dev_size < sizeof *gspca_dev)
1806 dev_size = sizeof *gspca_dev;
1807 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
1808 if (gspca_dev == NULL) {
1809 err("couldn't kzalloc gspca struct");
1810 return -EIO;
1811 }
Jean-Francois Moine8295d992008-09-03 17:12:19 -03001812 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
1813 if (!gspca_dev->usb_buf) {
1814 err("out of memory");
1815 ret = -EIO;
1816 goto out;
1817 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001818 gspca_dev->dev = dev;
1819 gspca_dev->iface = interface->bInterfaceNumber;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001820 gspca_dev->nbalt = intf->num_altsetting;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001821 gspca_dev->sd_desc = sd_desc;
1822/* gspca_dev->users = 0; (done by kzalloc) */
1823 gspca_dev->nbufread = 2;
1824
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001825 /* configure the subdriver and initialize the USB device */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001826 ret = gspca_dev->sd_desc->config(gspca_dev, id);
1827 if (ret < 0)
1828 goto out;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001829 ret = gspca_dev->sd_desc->init(gspca_dev);
1830 if (ret < 0)
1831 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001832 ret = gspca_set_alt0(gspca_dev);
1833 if (ret < 0)
1834 goto out;
1835 gspca_set_default_mode(gspca_dev);
1836
1837 mutex_init(&gspca_dev->usb_lock);
1838 mutex_init(&gspca_dev->read_lock);
1839 mutex_init(&gspca_dev->queue_lock);
1840 init_waitqueue_head(&gspca_dev->wq);
1841
1842 /* init video stuff */
1843 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
Hans Verkuil5e85e732008-07-20 06:31:39 -03001844 gspca_dev->vdev.parent = &dev->dev;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001845 memcpy(&gspca_dev->fops, &dev_fops, sizeof gspca_dev->fops);
1846 gspca_dev->vdev.fops = &gspca_dev->fops;
1847 gspca_dev->fops.owner = module; /* module protection */
Oliver Neukumee56a4d2008-07-27 14:01:59 -03001848 gspca_dev->present = 1;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001849 ret = video_register_device(&gspca_dev->vdev,
1850 VFL_TYPE_GRABBER,
1851 video_nr);
1852 if (ret < 0) {
1853 err("video_register_device err %d", ret);
1854 goto out;
1855 }
1856
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001857 usb_set_intfdata(intf, gspca_dev);
1858 PDEBUG(D_PROBE, "probe ok");
1859 return 0;
1860out:
Jean-Francois Moine8295d992008-09-03 17:12:19 -03001861 kfree(gspca_dev->usb_buf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001862 kfree(gspca_dev);
1863 return ret;
1864}
1865EXPORT_SYMBOL(gspca_dev_probe);
1866
1867/*
1868 * USB disconnection
1869 *
1870 * This function must be called by the sub-driver
1871 * when the device disconnects, after the specific resources are freed.
1872 */
1873void gspca_disconnect(struct usb_interface *intf)
1874{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001875 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001876
1877 if (!gspca_dev)
1878 return;
1879 gspca_dev->present = 0;
Hans de Goedee2997a72008-04-23 08:09:12 -03001880 mutex_lock(&gspca_dev->queue_lock);
1881 mutex_lock(&gspca_dev->usb_lock);
1882 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001883 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001884 mutex_unlock(&gspca_dev->usb_lock);
Hans de Goedee2997a72008-04-23 08:09:12 -03001885 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001886 while (gspca_dev->users != 0) { /* wait until fully closed */
1887 atomic_inc(&gspca_dev->nevent);
1888 wake_up_interruptible(&gspca_dev->wq); /* wake processes */
1889 schedule();
1890 }
1891/* We don't want people trying to open up the device */
1892 video_unregister_device(&gspca_dev->vdev);
1893/* Free the memory */
Jean-Francois Moine8295d992008-09-03 17:12:19 -03001894 kfree(gspca_dev->usb_buf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001895 kfree(gspca_dev);
1896 PDEBUG(D_PROBE, "disconnect complete");
1897}
1898EXPORT_SYMBOL(gspca_disconnect);
1899
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001900#ifdef CONFIG_PM
1901int gspca_suspend(struct usb_interface *intf, pm_message_t message)
1902{
1903 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1904
1905 if (!gspca_dev->streaming)
1906 return 0;
1907 gspca_dev->frozen = 1; /* avoid urb error messages */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001908 if (gspca_dev->sd_desc->stopN)
1909 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001910 destroy_urbs(gspca_dev);
1911 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001912 if (gspca_dev->sd_desc->stop0)
1913 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001914 return 0;
1915}
1916EXPORT_SYMBOL(gspca_suspend);
1917
1918int gspca_resume(struct usb_interface *intf)
1919{
1920 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1921
1922 gspca_dev->frozen = 0;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001923 gspca_dev->sd_desc->init(gspca_dev);
1924 if (gspca_dev->streaming)
1925 return gspca_init_transfer(gspca_dev);
Jean-Francois Moine1e89e2d2008-09-03 17:12:15 -03001926 return 0;
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001927}
1928EXPORT_SYMBOL(gspca_resume);
1929#endif
Hans de Goededcef3232008-07-10 10:40:53 -03001930/* -- cam driver utility functions -- */
1931
1932/* auto gain and exposure algorithm based on the knee algorithm described here:
1933 http://ytse.tricolour.net/docs/LowLightOptimization.html
1934
1935 Returns 0 if no changes were made, 1 if the gain and or exposure settings
1936 where changed. */
1937int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
1938 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
1939{
1940 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
1941 const struct ctrl *gain_ctrl = NULL;
1942 const struct ctrl *exposure_ctrl = NULL;
1943 const struct ctrl *autogain_ctrl = NULL;
1944 int retval = 0;
1945
1946 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1947 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
1948 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1949 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
1950 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
1951 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
1952 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1953 }
1954 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
1955 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
1956 "on cam without (auto)gain/exposure");
1957 return 0;
1958 }
1959
1960 if (gain_ctrl->get(gspca_dev, &gain) ||
1961 exposure_ctrl->get(gspca_dev, &exposure) ||
1962 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
1963 return 0;
1964
1965 orig_gain = gain;
1966 orig_exposure = exposure;
1967
1968 /* If we are of a multiple of deadzone, do multiple steps to reach the
1969 desired lumination fast (with the risc of a slight overshoot) */
1970 steps = abs(desired_avg_lum - avg_lum) / deadzone;
1971
1972 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d\n",
1973 avg_lum, desired_avg_lum, steps);
1974
1975 for (i = 0; i < steps; i++) {
1976 if (avg_lum > desired_avg_lum) {
1977 if (gain > gain_knee)
1978 gain--;
1979 else if (exposure > exposure_knee)
1980 exposure--;
1981 else if (gain > gain_ctrl->qctrl.default_value)
1982 gain--;
1983 else if (exposure > exposure_ctrl->qctrl.minimum)
1984 exposure--;
1985 else if (gain > gain_ctrl->qctrl.minimum)
1986 gain--;
1987 else
1988 break;
1989 } else {
1990 if (gain < gain_ctrl->qctrl.default_value)
1991 gain++;
1992 else if (exposure < exposure_knee)
1993 exposure++;
1994 else if (gain < gain_knee)
1995 gain++;
1996 else if (exposure < exposure_ctrl->qctrl.maximum)
1997 exposure++;
1998 else if (gain < gain_ctrl->qctrl.maximum)
1999 gain++;
2000 else
2001 break;
2002 }
2003 }
2004
2005 if (gain != orig_gain) {
2006 gain_ctrl->set(gspca_dev, gain);
2007 retval = 1;
2008 }
2009 if (exposure != orig_exposure) {
2010 exposure_ctrl->set(gspca_dev, exposure);
2011 retval = 1;
2012 }
2013
2014 return retval;
2015}
2016EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2017
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002018/* -- module insert / remove -- */
2019static int __init gspca_init(void)
2020{
Jean-Francois Moinebb64e862008-07-19 06:49:28 -03002021 info("main v%d.%d.%d registered",
2022 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2023 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2024 DRIVER_VERSION_NUMBER & 0xff);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002025 return 0;
2026}
2027static void __exit gspca_exit(void)
2028{
2029 info("main deregistered");
2030}
2031
2032module_init(gspca_init);
2033module_exit(gspca_exit);
2034
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03002035#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002036module_param_named(debug, gspca_debug, int, 0644);
2037MODULE_PARM_DESC(debug,
2038 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002039 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
2040 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002041#endif