blob: f28d2f4aceac5f22a710c8c09d9f56139f3ecc7e [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>
Jean-Francois Moinecf0fe082008-10-17 04:36:47 -030024#include <linux/version.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030025#include <linux/fs.h>
26#include <linux/vmalloc.h>
27#include <linux/sched.h>
28#include <linux/slab.h>
29#include <linux/mm.h>
30#include <linux/string.h>
31#include <linux/pagemap.h>
Jean-Francois Moine956e42d2008-07-01 10:03:42 -030032#include <linux/io.h>
Frank Zago6b060ff2008-09-28 08:12:22 -030033#include <linux/kref.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030034#include <asm/page.h>
Jean-Francois Moine956e42d2008-07-01 10:03:42 -030035#include <linux/uaccess.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030036#include <linux/jiffies.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030037#include <media/v4l2-ioctl.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030038
39#include "gspca.h"
40
Jean-Francois Moined43fa322008-06-12 10:58:58 -030041/* global values */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -030042#define DEF_NURBS 2 /* default number of URBs */
Jean-Francois Moined43fa322008-06-12 10:58:58 -030043
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030044MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
45MODULE_DESCRIPTION("GSPCA USB Camera Driver");
46MODULE_LICENSE("GPL");
47
Jean-Francois Moine3371bcc2008-09-06 04:21:08 -030048#define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 3, 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030049
50static int video_nr = -1;
51
Jean-Francois Moine335b3f82008-07-30 04:53:02 -030052#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030053int gspca_debug = D_ERR | D_PROBE;
54EXPORT_SYMBOL(gspca_debug);
55
56static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
57{
58 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
59 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
60 txt,
61 pixfmt & 0xff,
62 (pixfmt >> 8) & 0xff,
63 (pixfmt >> 16) & 0xff,
64 pixfmt >> 24,
65 w, h);
66 } else {
67 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
68 txt,
69 pixfmt,
70 w, h);
71 }
72}
73#else
74#define PDEBUG_MODE(txt, pixfmt, w, h)
75#endif
76
Jean-Francois Moined43fa322008-06-12 10:58:58 -030077/* specific memory types - !! should different from V4L2_MEMORY_xxx */
78#define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
79#define GSPCA_MEMORY_READ 7
80
Jean-Francois Moined43fa322008-06-12 10:58:58 -030081#define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
Jean-Francois Moined43fa322008-06-12 10:58:58 -030082
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030083/*
84 * VMA operations.
85 */
86static void gspca_vm_open(struct vm_area_struct *vma)
87{
88 struct gspca_frame *frame = vma->vm_private_data;
89
90 frame->vma_use_count++;
91 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
92}
93
94static void gspca_vm_close(struct vm_area_struct *vma)
95{
96 struct gspca_frame *frame = vma->vm_private_data;
97
98 if (--frame->vma_use_count <= 0)
99 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
100}
101
102static struct vm_operations_struct gspca_vm_ops = {
103 .open = gspca_vm_open,
104 .close = gspca_vm_close,
105};
106
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300107/* get the current input frame buffer */
108struct gspca_frame *gspca_get_i_frame(struct gspca_dev *gspca_dev)
109{
110 struct gspca_frame *frame;
111 int i;
112
113 i = gspca_dev->fr_i;
114 i = gspca_dev->fr_queue[i];
115 frame = &gspca_dev->frame[i];
116 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
117 != V4L2_BUF_FLAG_QUEUED)
118 return NULL;
119 return frame;
120}
121EXPORT_SYMBOL(gspca_get_i_frame);
122
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300123/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300124 * fill a video frame from an URB and resubmit
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300125 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300126static void fill_frame(struct gspca_dev *gspca_dev,
127 struct urb *urb)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300128{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300129 struct gspca_frame *frame;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300130 __u8 *data; /* address of data in the iso message */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300131 int i, len, st;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300132 cam_pkt_op pkt_scan;
133
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300134 if (urb->status != 0) {
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300135#ifdef CONFIG_PM
Jean-Francois Moine6a709742008-09-03 16:48:10 -0300136 if (!gspca_dev->frozen)
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300137#endif
Jean-Francois Moine6a709742008-09-03 16:48:10 -0300138 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300139 return; /* disconnection ? */
140 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300141 pkt_scan = gspca_dev->sd_desc->pkt_scan;
142 for (i = 0; i < urb->number_of_packets; i++) {
143
144 /* check the availability of the frame buffer */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300145 frame = gspca_get_i_frame(gspca_dev);
146 if (!frame) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300147 gspca_dev->last_packet_type = DISCARD_PACKET;
148 break;
149 }
150
151 /* check the packet status and length */
152 len = urb->iso_frame_desc[i].actual_length;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300153 if (len == 0)
154 continue;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300155 st = urb->iso_frame_desc[i].status;
156 if (st) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300157 PDEBUG(D_ERR,
158 "ISOC data error: [%d] len=%d, status=%d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300159 i, len, st);
160 gspca_dev->last_packet_type = DISCARD_PACKET;
161 continue;
162 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300163
164 /* let the packet be analyzed by the subdriver */
165 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
166 i, urb->iso_frame_desc[i].offset, len);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300167 data = (__u8 *) urb->transfer_buffer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300168 + urb->iso_frame_desc[i].offset;
169 pkt_scan(gspca_dev, frame, data, len);
170 }
171
172 /* resubmit the URB */
173 urb->status = 0;
174 st = usb_submit_urb(urb, GFP_ATOMIC);
175 if (st < 0)
176 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
177}
178
179/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300180 * ISOC message interrupt from the USB device
181 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300182 * Analyse each packet and call the subdriver for copy to the frame buffer.
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300183 */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300184static void isoc_irq(struct urb *urb
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300185)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300186{
187 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
188
Hans de Goede3647fea2008-07-15 05:36:30 -0300189 PDEBUG(D_PACK, "isoc irq");
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300190 if (!gspca_dev->streaming)
191 return;
192 fill_frame(gspca_dev, urb);
193}
194
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300195/*
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300196 * bulk message interrupt from the USB device
197 */
198static void bulk_irq(struct urb *urb
199)
200{
201 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
202 struct gspca_frame *frame;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300203
204 PDEBUG(D_PACK, "bulk irq");
205 if (!gspca_dev->streaming)
206 return;
207 if (urb->status != 0 && urb->status != -ECONNRESET) {
208#ifdef CONFIG_PM
209 if (!gspca_dev->frozen)
210#endif
211 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
212 return; /* disconnection ? */
213 }
214
215 /* check the availability of the frame buffer */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300216 frame = gspca_get_i_frame(gspca_dev);
217 if (!frame) {
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300218 gspca_dev->last_packet_type = DISCARD_PACKET;
219 } else {
220 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
221 gspca_dev->sd_desc->pkt_scan(gspca_dev,
222 frame,
223 urb->transfer_buffer,
224 urb->actual_length);
225 }
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300226}
227
228/*
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300229 * add data to the current frame
230 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300231 * This function is called by the subdrivers at interrupt level.
232 *
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300233 * To build a frame, these ones must add
234 * - one FIRST_PACKET
235 * - 0 or many INTER_PACKETs
236 * - one LAST_PACKET
237 * DISCARD_PACKET invalidates the whole frame.
238 * On LAST_PACKET, a new frame is returned.
239 */
240struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
Erik Andrene293e592008-10-03 08:46:50 -0300241 enum gspca_packet_type packet_type,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300242 struct gspca_frame *frame,
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300243 const __u8 *data,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300244 int len)
245{
246 int i, j;
247
Hans de Goedee2997a72008-04-23 08:09:12 -0300248 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300249
250 /* when start of a new frame, if the current frame buffer
251 * is not queued, discard the whole frame */
252 if (packet_type == FIRST_PACKET) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300253 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
254 != V4L2_BUF_FLAG_QUEUED) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300255 gspca_dev->last_packet_type = DISCARD_PACKET;
256 return frame;
257 }
258 frame->data_end = frame->data;
259 jiffies_to_timeval(get_jiffies_64(),
260 &frame->v4l2_buf.timestamp);
261 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300262 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
Jean-Francois Moine06ca78f2008-07-22 03:45:08 -0300263 if (packet_type == LAST_PACKET)
264 gspca_dev->last_packet_type = packet_type;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300265 return frame;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300266 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300267
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300268 /* append the packet to the frame buffer */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300269 if (len > 0) {
270 if (frame->data_end - frame->data + len
271 > frame->v4l2_buf.length) {
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -0300272 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300273 frame->data_end - frame->data + len,
274 frame->v4l2_buf.length);
275 packet_type = DISCARD_PACKET;
276 } else {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300277 memcpy(frame->data_end, data, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300278 frame->data_end += len;
279 }
280 }
281 gspca_dev->last_packet_type = packet_type;
282
Frank Zago219423c2008-09-26 07:43:54 -0300283 /* if last packet, wake up the application and advance in the queue */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300284 if (packet_type == LAST_PACKET) {
285 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
286 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
287 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
288 atomic_inc(&gspca_dev->nevent);
289 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300290 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
291 gspca_dev->fr_i = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300292 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
293 frame->v4l2_buf.bytesused,
294 gspca_dev->fr_q,
295 i,
296 gspca_dev->fr_o);
297 j = gspca_dev->fr_queue[i];
298 frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300299 }
300 return frame;
301}
302EXPORT_SYMBOL(gspca_frame_add);
303
304static int gspca_is_compressed(__u32 format)
305{
306 switch (format) {
307 case V4L2_PIX_FMT_MJPEG:
308 case V4L2_PIX_FMT_JPEG:
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300309 case V4L2_PIX_FMT_SPCA561:
Hans de Goedeab8f12c2008-07-04 18:29:32 -0300310 case V4L2_PIX_FMT_PAC207:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300311 return 1;
312 }
313 return 0;
314}
315
316static void *rvmalloc(unsigned long size)
317{
318 void *mem;
319 unsigned long adr;
320
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300321 mem = vmalloc_32(size);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300322 if (mem != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300323 adr = (unsigned long) mem;
324 while ((long) size > 0) {
325 SetPageReserved(vmalloc_to_page((void *) adr));
326 adr += PAGE_SIZE;
327 size -= PAGE_SIZE;
328 }
329 }
330 return mem;
331}
332
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300333static void rvfree(void *mem, long size)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300334{
335 unsigned long adr;
336
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300337 adr = (unsigned long) mem;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300338 while (size > 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300339 ClearPageReserved(vmalloc_to_page((void *) adr));
340 adr += PAGE_SIZE;
341 size -= PAGE_SIZE;
342 }
343 vfree(mem);
344}
345
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300346static int frame_alloc(struct gspca_dev *gspca_dev,
347 unsigned int count)
348{
349 struct gspca_frame *frame;
350 unsigned int frsz;
351 int i;
352
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300353 i = gspca_dev->curr_mode;
354 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300355 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300356 frsz = PAGE_ALIGN(frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300357 gspca_dev->frsz = frsz;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300358 if (count > GSPCA_MAX_FRAMES)
359 count = GSPCA_MAX_FRAMES;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300360 gspca_dev->frbuf = rvmalloc(frsz * count);
361 if (!gspca_dev->frbuf) {
362 err("frame alloc failed");
363 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300364 }
365 gspca_dev->nframes = count;
366 for (i = 0; i < count; i++) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300367 frame = &gspca_dev->frame[i];
368 frame->v4l2_buf.index = i;
369 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
370 frame->v4l2_buf.flags = 0;
371 frame->v4l2_buf.field = V4L2_FIELD_NONE;
372 frame->v4l2_buf.length = frsz;
373 frame->v4l2_buf.memory = gspca_dev->memory;
374 frame->v4l2_buf.sequence = 0;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300375 frame->data = frame->data_end =
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300376 gspca_dev->frbuf + i * frsz;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300377 frame->v4l2_buf.m.offset = i * frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300378 }
379 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
380 gspca_dev->last_packet_type = DISCARD_PACKET;
381 gspca_dev->sequence = 0;
382 atomic_set(&gspca_dev->nevent, 0);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300383 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300384}
385
386static void frame_free(struct gspca_dev *gspca_dev)
387{
388 int i;
389
390 PDEBUG(D_STREAM, "frame free");
Harvey Harrisonefab8212008-07-05 06:12:47 -0300391 if (gspca_dev->frbuf != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300392 rvfree(gspca_dev->frbuf,
393 gspca_dev->nframes * gspca_dev->frsz);
394 gspca_dev->frbuf = NULL;
395 for (i = 0; i < gspca_dev->nframes; i++)
396 gspca_dev->frame[i].data = NULL;
397 }
398 gspca_dev->nframes = 0;
399}
400
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300401static void destroy_urbs(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300402{
403 struct urb *urb;
404 unsigned int i;
405
406 PDEBUG(D_STREAM, "kill transfer");
Erik Andrén44732812008-10-13 15:52:46 -0300407 for (i = 0; i < MAX_NURBS; i++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300408 urb = gspca_dev->urb[i];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300409 if (urb == NULL)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300410 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300411
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300412 gspca_dev->urb[i] = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300413 usb_kill_urb(urb);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300414 if (urb->transfer_buffer != NULL)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300415 usb_buffer_free(gspca_dev->dev,
416 urb->transfer_buffer_length,
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300417 urb->transfer_buffer,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300418 urb->transfer_dma);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300419 usb_free_urb(urb);
420 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300421}
422
423/*
Frank Zago219423c2008-09-26 07:43:54 -0300424 * look for an input transfer endpoint in an alternate setting
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300425 */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300426static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
427 __u8 epaddr,
428 __u8 xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300429{
430 struct usb_host_endpoint *ep;
431 int i, attr;
432
433 epaddr |= USB_DIR_IN;
434 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
435 ep = &alt->endpoint[i];
436 if (ep->desc.bEndpointAddress == epaddr) {
437 attr = ep->desc.bmAttributes
438 & USB_ENDPOINT_XFERTYPE_MASK;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300439 if (attr == xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300440 return ep;
441 break;
442 }
443 }
444 return NULL;
445}
446
447/*
Frank Zago219423c2008-09-26 07:43:54 -0300448 * look for an input (isoc or bulk) endpoint
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300449 *
450 * The endpoint is defined by the subdriver.
451 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
452 * This routine may be called many times when the bandwidth is too small
453 * (the bandwidth is checked on urb submit).
454 */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300455static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300456{
457 struct usb_interface *intf;
458 struct usb_host_endpoint *ep;
459 int i, ret;
460
461 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300462 ep = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300463 i = gspca_dev->alt; /* previous alt setting */
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300464
465 /* try isoc */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300466 while (--i > 0) { /* alt 0 is unusable */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300467 ep = alt_xfer(&intf->altsetting[i],
468 gspca_dev->cam.epaddr,
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300469 USB_ENDPOINT_XFER_ISOC);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300470 if (ep)
471 break;
472 }
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300473
474 /* if no isoc, try bulk */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300475 if (ep == NULL) {
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300476 ep = alt_xfer(&intf->altsetting[0],
477 gspca_dev->cam.epaddr,
478 USB_ENDPOINT_XFER_BULK);
479 if (ep == NULL) {
480 err("no transfer endpoint found");
481 return NULL;
482 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300483 }
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300484 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300485 i, ep->desc.bEndpointAddress);
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300486 if (i > 0) {
487 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
488 if (ret < 0) {
489 err("set interface err %d", ret);
490 return NULL;
491 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300492 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300493 gspca_dev->alt = i; /* memorize the current alt setting */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300494 return ep;
495}
496
497/*
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300498 * create the URBs for image transfer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300499 */
500static int create_urbs(struct gspca_dev *gspca_dev,
501 struct usb_host_endpoint *ep)
502{
503 struct urb *urb;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300504 int n, nurbs, i, psize, npkt, bsize;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300505
506 /* calculate the packet size and the number of packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300507 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300508
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300509 if (gspca_dev->alt != 0) { /* isoc */
510
511 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
512 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300513 npkt = ISO_MAX_SIZE / psize;
514 if (npkt > ISO_MAX_PKT)
515 npkt = ISO_MAX_PKT;
516 bsize = psize * npkt;
517 PDEBUG(D_STREAM,
518 "isoc %d pkts size %d = bsize:%d",
519 npkt, psize, bsize);
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300520 nurbs = DEF_NURBS;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300521 } else { /* bulk */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300522 npkt = 0;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300523 bsize = gspca_dev->cam. bulk_size;
524 if (bsize == 0)
525 bsize = psize;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300526 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300527 nurbs = 1;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300528 }
529
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300530 gspca_dev->nurbs = nurbs;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300531 for (n = 0; n < nurbs; n++) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300532 urb = usb_alloc_urb(npkt, GFP_KERNEL);
533 if (!urb) {
534 err("usb_alloc_urb failed");
Jean-Francois Moine0d0548e2008-09-05 12:49:54 -0300535 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300536 return -ENOMEM;
537 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300538 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300539 bsize,
540 GFP_KERNEL,
541 &urb->transfer_dma);
542
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300543 if (urb->transfer_buffer == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300544 usb_free_urb(urb);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300545 err("usb_buffer_urb failed");
Jean-Francois Moine0d0548e2008-09-05 12:49:54 -0300546 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300547 return -ENOMEM;
548 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300549 gspca_dev->urb[n] = urb;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300550 urb->dev = gspca_dev->dev;
551 urb->context = gspca_dev;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300552 urb->transfer_buffer_length = bsize;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300553 if (npkt != 0) { /* ISOC */
554 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
555 ep->desc.bEndpointAddress);
Frank Zago6c49da72008-09-28 07:43:00 -0300556 urb->transfer_flags = URB_ISO_ASAP
557 | URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300558 urb->interval = ep->desc.bInterval;
559 urb->complete = isoc_irq;
560 urb->number_of_packets = npkt;
561 for (i = 0; i < npkt; i++) {
562 urb->iso_frame_desc[i].length = psize;
563 urb->iso_frame_desc[i].offset = psize * i;
564 }
565 } else { /* bulk */
566 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
567 ep->desc.bEndpointAddress),
Frank Zago6c49da72008-09-28 07:43:00 -0300568 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300569 urb->complete = bulk_irq;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300570 }
571 }
572 return 0;
573}
574
575/*
576 * start the USB transfer
577 */
578static int gspca_init_transfer(struct gspca_dev *gspca_dev)
579{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300580 struct usb_host_endpoint *ep;
581 int n, ret;
582
Hans de Goedee2997a72008-04-23 08:09:12 -0300583 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
584 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300585
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300586 /* set the higher alternate setting and
587 * loop until urb submit succeeds */
588 gspca_dev->alt = gspca_dev->nbalt;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300589 for (;;) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300590 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300591 ep = get_ep(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300592 if (ep == NULL) {
593 ret = -EIO;
594 goto out;
595 }
596 ret = create_urbs(gspca_dev, ep);
597 if (ret < 0)
598 goto out;
599
600 /* start the cam */
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300601 ret = gspca_dev->sd_desc->start(gspca_dev);
602 if (ret < 0) {
603 destroy_urbs(gspca_dev);
604 goto out;
605 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300606 gspca_dev->streaming = 1;
607 atomic_set(&gspca_dev->nevent, 0);
608
Frank Zago219423c2008-09-26 07:43:54 -0300609 /* bulk transfers are started by the subdriver */
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300610 if (gspca_dev->alt == 0)
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300611 break;
612
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300613 /* submit the URBs */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300614 for (n = 0; n < gspca_dev->nurbs; n++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300615 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300616 if (ret < 0) {
617 PDEBUG(D_ERR|D_STREAM,
618 "usb_submit_urb [%d] err %d", n, ret);
Hans de Goedee2997a72008-04-23 08:09:12 -0300619 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300620 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300621 if (ret == -ENOSPC)
622 break; /* try the previous alt */
623 goto out;
624 }
625 }
626 if (ret >= 0)
627 break;
628 }
629out:
630 mutex_unlock(&gspca_dev->usb_lock);
631 return ret;
632}
633
634static int gspca_set_alt0(struct gspca_dev *gspca_dev)
635{
636 int ret;
637
638 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
639 if (ret < 0)
640 PDEBUG(D_ERR|D_STREAM, "set interface 0 err %d", ret);
641 return ret;
642}
643
Frank Zago219423c2008-09-26 07:43:54 -0300644/* Note: both the queue and the usb locks should be held when calling this */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300645static void gspca_stream_off(struct gspca_dev *gspca_dev)
646{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300647 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300648 atomic_set(&gspca_dev->nevent, 0);
Jean-Francois Moine98522a72008-11-18 06:33:08 -0300649 if (gspca_dev->present
650 && gspca_dev->sd_desc->stopN)
651 gspca_dev->sd_desc->stopN(gspca_dev);
652 destroy_urbs(gspca_dev);
653 gspca_set_alt0(gspca_dev);
654 if (gspca_dev->sd_desc->stop0)
655 gspca_dev->sd_desc->stop0(gspca_dev);
656 PDEBUG(D_STREAM, "stream off OK");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300657}
658
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300659static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300660{
661 int i;
662
663 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
664 gspca_dev->curr_mode = i;
665 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
666 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300667 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300668}
669
670static int wxh_to_mode(struct gspca_dev *gspca_dev,
671 int width, int height)
672{
673 int i;
674
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300675 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
676 if (width >= gspca_dev->cam.cam_mode[i].width
677 && height >= gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300678 break;
679 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300680 return i;
681}
682
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300683/*
684 * search a mode with the right pixel format
685 */
686static int gspca_get_mode(struct gspca_dev *gspca_dev,
687 int mode,
688 int pixfmt)
689{
690 int modeU, modeD;
691
692 modeU = modeD = mode;
693 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
694 if (--modeD >= 0) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300695 if (gspca_dev->cam.cam_mode[modeD].pixelformat
696 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300697 return modeD;
698 }
699 if (++modeU < gspca_dev->cam.nmodes) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300700 if (gspca_dev->cam.cam_mode[modeU].pixelformat
701 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300702 return modeU;
703 }
704 }
705 return -EINVAL;
706}
707
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300708static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300709 struct v4l2_fmtdesc *fmtdesc)
710{
711 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300712 int i, j, index;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300713 __u32 fmt_tb[8];
714
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300715 /* give an index to each format */
716 index = 0;
717 j = 0;
718 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300719 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300720 j = 0;
721 for (;;) {
722 if (fmt_tb[j] == fmt_tb[index])
723 break;
724 j++;
725 }
726 if (j == index) {
727 if (fmtdesc->index == index)
728 break; /* new format */
729 index++;
730 if (index >= sizeof fmt_tb / sizeof fmt_tb[0])
731 return -EINVAL;
732 }
733 }
734 if (i < 0)
735 return -EINVAL; /* no more format */
736
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300737 fmtdesc->pixelformat = fmt_tb[index];
738 if (gspca_is_compressed(fmt_tb[index]))
739 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300740 fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300741 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
742 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
743 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
744 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
745 fmtdesc->description[4] = '\0';
746 return 0;
747}
748
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300749static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300750 struct v4l2_format *fmt)
751{
752 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300753 int mode;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300754
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300755 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
756 return -EINVAL;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300757 mode = gspca_dev->curr_mode;
758 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
759 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300760 return 0;
761}
762
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300763static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300764 struct v4l2_format *fmt)
765{
Hans de Goede24c530b2008-07-04 17:55:18 -0300766 int w, h, mode, mode2;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300767
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300768 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
769 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300770 w = fmt->fmt.pix.width;
771 h = fmt->fmt.pix.height;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300772
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300773#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300774 if (gspca_debug & D_CONF)
775 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
776#endif
777 /* search the closest mode for width and height */
778 mode = wxh_to_mode(gspca_dev, w, h);
779
780 /* OK if right palette */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300781 if (gspca_dev->cam.cam_mode[mode].pixelformat
782 != fmt->fmt.pix.pixelformat) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300783
784 /* else, search the closest mode with the same pixel format */
785 mode2 = gspca_get_mode(gspca_dev, mode,
786 fmt->fmt.pix.pixelformat);
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300787 if (mode2 >= 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300788 mode = mode2;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300789/* else
790 ; * no chance, return this mode */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300791 }
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300792 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
793 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300794 return mode; /* used when s_fmt */
795}
796
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300797static int vidioc_try_fmt_vid_cap(struct file *file,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300798 void *priv,
799 struct v4l2_format *fmt)
800{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300801 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300802 int ret;
803
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300804 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300805 if (ret < 0)
806 return ret;
807 return 0;
808}
809
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300810static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300811 struct v4l2_format *fmt)
812{
813 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300814 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300815
Hans de Goedee2997a72008-04-23 08:09:12 -0300816 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
817 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300818
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300819 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300820 if (ret < 0)
821 goto out;
822
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300823 if (gspca_dev->nframes != 0
824 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
825 ret = -EINVAL;
826 goto out;
827 }
828
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300829 if (ret == gspca_dev->curr_mode) {
830 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300831 goto out; /* same mode */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300832 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300833
834 if (gspca_dev->streaming) {
835 ret = -EBUSY;
836 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -0300837 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300838 gspca_dev->width = fmt->fmt.pix.width;
839 gspca_dev->height = fmt->fmt.pix.height;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300840 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
841 gspca_dev->curr_mode = ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300842
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300843 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300844out:
845 mutex_unlock(&gspca_dev->queue_lock);
846 return ret;
847}
848
Frank Zago6b060ff2008-09-28 08:12:22 -0300849static void gspca_delete(struct kref *kref)
850{
851 struct gspca_dev *gspca_dev = container_of(kref, struct gspca_dev, kref);
852
853 PDEBUG(D_STREAM, "device deleted");
854
855 kfree(gspca_dev->usb_buf);
856 kfree(gspca_dev);
857}
858
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300859static int dev_open(struct inode *inode, struct file *file)
860{
861 struct gspca_dev *gspca_dev;
862 int ret;
863
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300864 PDEBUG(D_STREAM, "%s open", current->comm);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300865 gspca_dev = (struct gspca_dev *) video_devdata(file);
Hans de Goedee2997a72008-04-23 08:09:12 -0300866 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
867 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300868 if (!gspca_dev->present) {
869 ret = -ENODEV;
870 goto out;
871 }
872
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300873 if (gspca_dev->users > 4) { /* (arbitrary value) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300874 ret = -EBUSY;
875 goto out;
876 }
877 gspca_dev->users++;
Frank Zago6b060ff2008-09-28 08:12:22 -0300878
879 /* one more user */
880 kref_get(&gspca_dev->kref);
881
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300882 file->private_data = gspca_dev;
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300883#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300884 /* activate the v4l2 debug */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300885 if (gspca_debug & D_V4L2)
Frank Zago9de436c2008-09-15 05:20:38 -0300886 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
887 | V4L2_DEBUG_IOCTL_ARG;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300888 else
Frank Zago9de436c2008-09-15 05:20:38 -0300889 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
890 | V4L2_DEBUG_IOCTL_ARG);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300891#endif
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300892 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300893out:
894 mutex_unlock(&gspca_dev->queue_lock);
895 if (ret != 0)
896 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
897 else
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300898 PDEBUG(D_STREAM, "open done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300899 return ret;
900}
901
902static int dev_close(struct inode *inode, struct file *file)
903{
904 struct gspca_dev *gspca_dev = file->private_data;
905
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300906 PDEBUG(D_STREAM, "%s close", current->comm);
Hans de Goedee2997a72008-04-23 08:09:12 -0300907 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
908 return -ERESTARTSYS;
909 gspca_dev->users--;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300910
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -0300911 /* if the file did the capture, free the streaming resources */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300912 if (gspca_dev->capt_file == file) {
Jean-Francois Moinea1e82882008-09-03 17:12:17 -0300913 if (gspca_dev->streaming) {
914 mutex_lock(&gspca_dev->usb_lock);
915 gspca_stream_off(gspca_dev);
916 mutex_unlock(&gspca_dev->usb_lock);
917 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300918 frame_free(gspca_dev);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300919 gspca_dev->capt_file = NULL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300920 gspca_dev->memory = GSPCA_MEMORY_NO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300921 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300922 file->private_data = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300923 mutex_unlock(&gspca_dev->queue_lock);
Frank Zago6b060ff2008-09-28 08:12:22 -0300924
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300925 PDEBUG(D_STREAM, "close done");
Frank Zago6b060ff2008-09-28 08:12:22 -0300926
927 kref_put(&gspca_dev->kref, gspca_delete);
928
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300929 return 0;
930}
931
932static int vidioc_querycap(struct file *file, void *priv,
933 struct v4l2_capability *cap)
934{
935 struct gspca_dev *gspca_dev = priv;
936
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300937 memset(cap, 0, sizeof *cap);
938 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -0300939 if (gspca_dev->dev->product != NULL) {
940 strncpy(cap->card, gspca_dev->dev->product,
941 sizeof cap->card);
942 } else {
943 snprintf(cap->card, sizeof cap->card,
944 "USB Camera (%04x:%04x)",
945 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
946 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
947 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300948 strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name,
949 sizeof cap->bus_info);
950 cap->version = DRIVER_VERSION_NUMBER;
951 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
952 | V4L2_CAP_STREAMING
953 | V4L2_CAP_READWRITE;
954 return 0;
955}
956
957static int vidioc_queryctrl(struct file *file, void *priv,
958 struct v4l2_queryctrl *q_ctrl)
959{
960 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300961 int i, ix;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300962 u32 id;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300963
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300964 ix = -1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300965 id = q_ctrl->id;
966 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
967 id &= V4L2_CTRL_ID_MASK;
968 id++;
969 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goede0f523c22008-09-03 17:12:14 -0300970 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300971 continue;
972 if (ix < 0) {
973 ix = i;
974 continue;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300975 }
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300976 if (gspca_dev->sd_desc->ctrls[i].qctrl.id
977 > gspca_dev->sd_desc->ctrls[ix].qctrl.id)
978 continue;
979 ix = i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300980 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300981 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300982 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300983 if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) {
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300984 ix = i;
985 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300986 }
987 }
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300988 if (ix < 0)
989 return -EINVAL;
990 memcpy(q_ctrl, &gspca_dev->sd_desc->ctrls[ix].qctrl,
991 sizeof *q_ctrl);
992 if (gspca_dev->ctrl_dis & (1 << ix))
993 q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
994 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300995}
996
997static int vidioc_s_ctrl(struct file *file, void *priv,
998 struct v4l2_control *ctrl)
999{
1000 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001001 const struct ctrl *ctrls;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001002 int i, ret;
1003
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001004 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1005 i < gspca_dev->sd_desc->nctrls;
1006 i++, ctrls++) {
1007 if (ctrl->id != ctrls->qctrl.id)
1008 continue;
Hans de Goede0f523c22008-09-03 17:12:14 -03001009 if (gspca_dev->ctrl_dis & (1 << i))
1010 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001011 if (ctrl->value < ctrls->qctrl.minimum
Rabin Vincentfcf5cb22008-08-03 07:58:54 -03001012 || ctrl->value > ctrls->qctrl.maximum)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001013 return -ERANGE;
1014 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
Hans de Goedee2997a72008-04-23 08:09:12 -03001015 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1016 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001017 ret = ctrls->set(gspca_dev, ctrl->value);
1018 mutex_unlock(&gspca_dev->usb_lock);
1019 return ret;
1020 }
1021 return -EINVAL;
1022}
1023
1024static int vidioc_g_ctrl(struct file *file, void *priv,
1025 struct v4l2_control *ctrl)
1026{
1027 struct gspca_dev *gspca_dev = priv;
1028
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001029 const struct ctrl *ctrls;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001030 int i, ret;
1031
1032 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1033 i < gspca_dev->sd_desc->nctrls;
1034 i++, ctrls++) {
1035 if (ctrl->id != ctrls->qctrl.id)
1036 continue;
Hans de Goede0f523c22008-09-03 17:12:14 -03001037 if (gspca_dev->ctrl_dis & (1 << i))
1038 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001039 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1040 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001041 ret = ctrls->get(gspca_dev, &ctrl->value);
1042 mutex_unlock(&gspca_dev->usb_lock);
1043 return ret;
1044 }
1045 return -EINVAL;
1046}
1047
1048static int vidioc_querymenu(struct file *file, void *priv,
1049 struct v4l2_querymenu *qmenu)
1050{
1051 struct gspca_dev *gspca_dev = priv;
1052
1053 if (!gspca_dev->sd_desc->querymenu)
1054 return -EINVAL;
1055 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1056}
1057
1058static int vidioc_enum_input(struct file *file, void *priv,
1059 struct v4l2_input *input)
1060{
1061 struct gspca_dev *gspca_dev = priv;
1062
1063 if (input->index != 0)
1064 return -EINVAL;
1065 memset(input, 0, sizeof *input);
1066 input->type = V4L2_INPUT_TYPE_CAMERA;
1067 strncpy(input->name, gspca_dev->sd_desc->name,
1068 sizeof input->name);
1069 return 0;
1070}
1071
1072static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1073{
1074 *i = 0;
1075 return 0;
1076}
1077
1078static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1079{
1080 if (i > 0)
1081 return -EINVAL;
1082 return (0);
1083}
1084
1085static int vidioc_reqbufs(struct file *file, void *priv,
1086 struct v4l2_requestbuffers *rb)
1087{
1088 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001089 int i, ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001090
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001091 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1092 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001093 switch (rb->memory) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001094 case GSPCA_MEMORY_READ: /* (internal call) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001095 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001096 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001097 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001098 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001099 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001100 }
Hans de Goedee2997a72008-04-23 08:09:12 -03001101 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1102 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001103
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001104 if (gspca_dev->memory != GSPCA_MEMORY_NO
1105 && gspca_dev->memory != rb->memory) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001106 ret = -EBUSY;
1107 goto out;
1108 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001109
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001110 /* only one file may do the capture */
1111 if (gspca_dev->capt_file != NULL
1112 && gspca_dev->capt_file != file) {
1113 ret = -EBUSY;
1114 goto out;
1115 }
1116
1117 /* if allocated, the buffers must not be mapped */
1118 for (i = 0; i < gspca_dev->nframes; i++) {
1119 if (gspca_dev->frame[i].vma_use_count) {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001120 ret = -EBUSY;
1121 goto out;
1122 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001123 }
1124
1125 /* stop streaming */
1126 if (gspca_dev->streaming) {
1127 mutex_lock(&gspca_dev->usb_lock);
1128 gspca_stream_off(gspca_dev);
1129 mutex_unlock(&gspca_dev->usb_lock);
1130 }
1131
1132 /* free the previous allocated buffers, if any */
1133 if (gspca_dev->nframes != 0) {
1134 frame_free(gspca_dev);
1135 gspca_dev->capt_file = NULL;
1136 }
1137 if (rb->count == 0) /* unrequest */
1138 goto out;
1139 gspca_dev->memory = rb->memory;
1140 ret = frame_alloc(gspca_dev, rb->count);
1141 if (ret == 0) {
1142 rb->count = gspca_dev->nframes;
1143 gspca_dev->capt_file = file;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001144 }
1145out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001146 mutex_unlock(&gspca_dev->queue_lock);
1147 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1148 return ret;
1149}
1150
1151static int vidioc_querybuf(struct file *file, void *priv,
1152 struct v4l2_buffer *v4l2_buf)
1153{
1154 struct gspca_dev *gspca_dev = priv;
1155 struct gspca_frame *frame;
1156
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001157 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1158 || v4l2_buf->index < 0
1159 || v4l2_buf->index >= gspca_dev->nframes)
1160 return -EINVAL;
1161
1162 frame = &gspca_dev->frame[v4l2_buf->index];
1163 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1164 return 0;
1165}
1166
1167static int vidioc_streamon(struct file *file, void *priv,
1168 enum v4l2_buf_type buf_type)
1169{
1170 struct gspca_dev *gspca_dev = priv;
1171 int ret;
1172
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001173 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1174 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001175 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1176 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001177 if (!gspca_dev->present) {
1178 ret = -ENODEV;
1179 goto out;
1180 }
1181 if (gspca_dev->nframes == 0) {
1182 ret = -EINVAL;
1183 goto out;
1184 }
1185 if (!gspca_dev->streaming) {
1186 ret = gspca_init_transfer(gspca_dev);
1187 if (ret < 0)
1188 goto out;
1189 }
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001190#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001191 if (gspca_debug & D_STREAM) {
1192 PDEBUG_MODE("stream on OK",
1193 gspca_dev->pixfmt,
1194 gspca_dev->width,
1195 gspca_dev->height);
1196 }
1197#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001198 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001199out:
1200 mutex_unlock(&gspca_dev->queue_lock);
1201 return ret;
1202}
1203
1204static int vidioc_streamoff(struct file *file, void *priv,
1205 enum v4l2_buf_type buf_type)
1206{
1207 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001208 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001209
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001210 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1211 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001212 if (!gspca_dev->streaming)
1213 return 0;
1214 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1215 return -ERESTARTSYS;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001216
1217 /* stop streaming */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001218 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1219 ret = -ERESTARTSYS;
1220 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001221 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001222 gspca_stream_off(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001223 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001224
1225 /* empty the application queues */
1226 for (i = 0; i < gspca_dev->nframes; i++)
1227 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1228 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1229 gspca_dev->last_packet_type = DISCARD_PACKET;
1230 gspca_dev->sequence = 0;
1231 atomic_set(&gspca_dev->nevent, 0);
1232 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001233out:
1234 mutex_unlock(&gspca_dev->queue_lock);
1235 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001236}
1237
1238static int vidioc_g_jpegcomp(struct file *file, void *priv,
1239 struct v4l2_jpegcompression *jpegcomp)
1240{
1241 struct gspca_dev *gspca_dev = priv;
1242 int ret;
1243
1244 if (!gspca_dev->sd_desc->get_jcomp)
1245 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001246 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1247 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001248 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1249 mutex_unlock(&gspca_dev->usb_lock);
1250 return ret;
1251}
1252
1253static int vidioc_s_jpegcomp(struct file *file, void *priv,
1254 struct v4l2_jpegcompression *jpegcomp)
1255{
1256 struct gspca_dev *gspca_dev = priv;
1257 int ret;
1258
Hans de Goedee2997a72008-04-23 08:09:12 -03001259 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1260 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001261 if (!gspca_dev->sd_desc->set_jcomp)
1262 return -EINVAL;
1263 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1264 mutex_unlock(&gspca_dev->usb_lock);
1265 return ret;
1266}
1267
1268static int vidioc_g_parm(struct file *filp, void *priv,
1269 struct v4l2_streamparm *parm)
1270{
1271 struct gspca_dev *gspca_dev = priv;
1272
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001273 memset(parm, 0, sizeof *parm);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001274 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1275 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1276 return 0;
1277}
1278
1279static int vidioc_s_parm(struct file *filp, void *priv,
1280 struct v4l2_streamparm *parm)
1281{
1282 struct gspca_dev *gspca_dev = priv;
1283 int n;
1284
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001285 n = parm->parm.capture.readbuffers;
1286 if (n == 0 || n > GSPCA_MAX_FRAMES)
1287 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1288 else
1289 gspca_dev->nbufread = n;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001290 return 0;
1291}
1292
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001293static int vidioc_s_std(struct file *filp, void *priv,
1294 v4l2_std_id *parm)
1295{
1296 return 0;
1297}
1298
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001299#ifdef CONFIG_VIDEO_V4L1_COMPAT
1300static int vidiocgmbuf(struct file *file, void *priv,
1301 struct video_mbuf *mbuf)
1302{
1303 struct gspca_dev *gspca_dev = file->private_data;
1304 int i;
1305
1306 PDEBUG(D_STREAM, "cgmbuf");
1307 if (gspca_dev->nframes == 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001308 int ret;
1309
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001310 {
1311 struct v4l2_format fmt;
1312
1313 memset(&fmt, 0, sizeof fmt);
1314 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1315 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1316 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1317 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1318 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001319 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001320 if (ret != 0)
1321 return ret;
1322 }
1323 {
1324 struct v4l2_requestbuffers rb;
1325
1326 memset(&rb, 0, sizeof rb);
1327 rb.count = 4;
1328 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1329 rb.memory = V4L2_MEMORY_MMAP;
1330 ret = vidioc_reqbufs(file, priv, &rb);
1331 if (ret != 0)
1332 return ret;
1333 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001334 }
1335 mbuf->frames = gspca_dev->nframes;
1336 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1337 for (i = 0; i < mbuf->frames; i++)
1338 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1339 return 0;
1340}
1341#endif
1342
1343static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1344{
1345 struct gspca_dev *gspca_dev = file->private_data;
Harvey Harrisonefab8212008-07-05 06:12:47 -03001346 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001347 struct page *page;
1348 unsigned long addr, start, size;
1349 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001350
1351 start = vma->vm_start;
1352 size = vma->vm_end - vma->vm_start;
1353 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1354
Hans de Goedee2997a72008-04-23 08:09:12 -03001355 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1356 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001357 if (!gspca_dev->present) {
1358 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001359 goto out;
1360 }
1361 if (gspca_dev->capt_file != file) {
1362 ret = -EINVAL;
1363 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001364 }
1365
Harvey Harrisonefab8212008-07-05 06:12:47 -03001366 frame = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001367 for (i = 0; i < gspca_dev->nframes; ++i) {
1368 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1369 PDEBUG(D_STREAM, "mmap bad memory type");
1370 break;
1371 }
1372 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1373 == vma->vm_pgoff) {
1374 frame = &gspca_dev->frame[i];
1375 break;
1376 }
1377 }
Harvey Harrisonefab8212008-07-05 06:12:47 -03001378 if (frame == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001379 PDEBUG(D_STREAM, "mmap no frame buffer found");
1380 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001381 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001382 }
1383#ifdef CONFIG_VIDEO_V4L1_COMPAT
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001384 /* v4l1 maps all the buffers */
1385 if (i != 0
1386 || size != frame->v4l2_buf.length * gspca_dev->nframes)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001387#endif
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001388 if (size != frame->v4l2_buf.length) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001389 PDEBUG(D_STREAM, "mmap bad size");
1390 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001391 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001392 }
1393
1394 /*
1395 * - VM_IO marks the area as being a mmaped region for I/O to a
1396 * device. It also prevents the region from being core dumped.
1397 */
1398 vma->vm_flags |= VM_IO;
1399
1400 addr = (unsigned long) frame->data;
1401 while (size > 0) {
1402 page = vmalloc_to_page((void *) addr);
1403 ret = vm_insert_page(vma, start, page);
1404 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001405 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001406 start += PAGE_SIZE;
1407 addr += PAGE_SIZE;
1408 size -= PAGE_SIZE;
1409 }
1410
1411 vma->vm_ops = &gspca_vm_ops;
1412 vma->vm_private_data = frame;
1413 gspca_vm_open(vma);
Hans de Goedee2997a72008-04-23 08:09:12 -03001414 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001415out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001416 mutex_unlock(&gspca_dev->queue_lock);
1417 return ret;
1418}
1419
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001420/*
1421 * wait for a video frame
1422 *
1423 * If a frame is ready, its index is returned.
1424 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001425static int frame_wait(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001426 int nonblock_ing)
1427{
1428 struct gspca_frame *frame;
1429 int i, j, ret;
1430
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001431 /* check if a frame is ready */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001432 i = gspca_dev->fr_o;
1433 j = gspca_dev->fr_queue[i];
1434 frame = &gspca_dev->frame[j];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001435 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) {
1436 atomic_dec(&gspca_dev->nevent);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001437 goto ok;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001438 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001439 if (nonblock_ing) /* no frame yet */
1440 return -EAGAIN;
1441
1442 /* wait till a frame is ready */
1443 for (;;) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001444 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1445 atomic_read(&gspca_dev->nevent) > 0,
1446 msecs_to_jiffies(3000));
1447 if (ret <= 0) {
1448 if (ret < 0)
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001449 return ret; /* interrupt */
1450 return -EIO; /* timeout */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001451 }
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001452 atomic_dec(&gspca_dev->nevent);
Hans de Goedee2997a72008-04-23 08:09:12 -03001453 if (!gspca_dev->streaming || !gspca_dev->present)
1454 return -EIO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001455 i = gspca_dev->fr_o;
1456 j = gspca_dev->fr_queue[i];
1457 frame = &gspca_dev->frame[j];
1458 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1459 break;
1460 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001461ok:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001462 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1463 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1464 gspca_dev->fr_q,
1465 gspca_dev->fr_i,
1466 gspca_dev->fr_o);
Hans de Goedee2997a72008-04-23 08:09:12 -03001467
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001468 if (gspca_dev->sd_desc->dq_callback) {
1469 mutex_lock(&gspca_dev->usb_lock);
Hans de Goedee2997a72008-04-23 08:09:12 -03001470 gspca_dev->sd_desc->dq_callback(gspca_dev);
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001471 mutex_unlock(&gspca_dev->usb_lock);
1472 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001473 return j;
1474}
1475
1476/*
1477 * dequeue a video buffer
1478 *
1479 * If nonblock_ing is false, block until a buffer is available.
1480 */
1481static int vidioc_dqbuf(struct file *file, void *priv,
1482 struct v4l2_buffer *v4l2_buf)
1483{
1484 struct gspca_dev *gspca_dev = priv;
1485 struct gspca_frame *frame;
1486 int i, ret;
1487
1488 PDEBUG(D_FRAM, "dqbuf");
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001489 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1490 return -EINVAL;
1491 if (v4l2_buf->memory != gspca_dev->memory)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001492 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001493
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001494 /* if not streaming, be sure the application will not loop forever */
1495 if (!(file->f_flags & O_NONBLOCK)
1496 && !gspca_dev->streaming && gspca_dev->users == 1)
1497 return -EINVAL;
1498
1499 /* only the capturing file may dequeue */
1500 if (gspca_dev->capt_file != file)
1501 return -EINVAL;
1502
1503 /* only one dequeue / read at a time */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001504 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1505 return -ERESTARTSYS;
1506
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001507 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001508 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001509 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001510 i = ret; /* frame index */
1511 frame = &gspca_dev->frame[i];
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001512 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001513 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001514 frame->data,
1515 frame->v4l2_buf.bytesused)) {
1516 PDEBUG(D_ERR|D_STREAM,
1517 "dqbuf cp to user failed");
1518 ret = -EFAULT;
1519 goto out;
1520 }
1521 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001522 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1523 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1524 PDEBUG(D_FRAM, "dqbuf %d", i);
1525 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001526out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001527 mutex_unlock(&gspca_dev->read_lock);
1528 return ret;
1529}
1530
1531/*
1532 * queue a video buffer
1533 *
1534 * Attempting to queue a buffer that has already been
1535 * queued will return -EINVAL.
1536 */
1537static int vidioc_qbuf(struct file *file, void *priv,
1538 struct v4l2_buffer *v4l2_buf)
1539{
1540 struct gspca_dev *gspca_dev = priv;
1541 struct gspca_frame *frame;
1542 int i, index, ret;
1543
1544 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1545 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1546 return -EINVAL;
1547
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001548 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1549 return -ERESTARTSYS;
1550
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001551 index = v4l2_buf->index;
1552 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001553 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001554 "qbuf idx %d >= %d", index, gspca_dev->nframes);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001555 ret = -EINVAL;
1556 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001557 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001558 if (v4l2_buf->memory != gspca_dev->memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001559 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001560 ret = -EINVAL;
1561 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001562 }
1563
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001564 frame = &gspca_dev->frame[index];
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001565 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1566 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001567 ret = -EINVAL;
1568 goto out;
1569 }
1570
1571 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001572
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001573 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001574 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1575 frame->v4l2_buf.length = v4l2_buf->length;
1576 }
1577
1578 /* put the buffer in the 'queued' queue */
1579 i = gspca_dev->fr_q;
1580 gspca_dev->fr_queue[i] = index;
1581 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1582 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1583 gspca_dev->fr_q,
1584 gspca_dev->fr_i,
1585 gspca_dev->fr_o);
1586
1587 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1588 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1589 ret = 0;
1590out:
1591 mutex_unlock(&gspca_dev->queue_lock);
1592 return ret;
1593}
1594
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001595/*
1596 * allocate the resources for read()
1597 */
1598static int read_alloc(struct gspca_dev *gspca_dev,
1599 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001600{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001601 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001602 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001603
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001604 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001605 if (gspca_dev->nframes == 0) {
1606 struct v4l2_requestbuffers rb;
1607
1608 memset(&rb, 0, sizeof rb);
1609 rb.count = gspca_dev->nbufread;
1610 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001611 rb.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001612 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1613 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001614 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001615 return ret;
1616 }
1617 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1618 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001619 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001620 for (i = 0; i < gspca_dev->nbufread; i++) {
1621 v4l2_buf.index = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001622 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1623 if (ret != 0) {
1624 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1625 return ret;
1626 }
1627 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001628 gspca_dev->memory = GSPCA_MEMORY_READ;
1629 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001630
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001631 /* start streaming */
1632 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1633 if (ret != 0)
1634 PDEBUG(D_STREAM, "read streamon err %d", ret);
1635 return ret;
1636}
1637
1638static unsigned int dev_poll(struct file *file, poll_table *wait)
1639{
1640 struct gspca_dev *gspca_dev = file->private_data;
1641 int i, ret;
1642
1643 PDEBUG(D_FRAM, "poll");
1644
1645 poll_wait(file, &gspca_dev->wq, wait);
1646 if (!gspca_dev->present)
1647 return POLLERR;
1648
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001649 /* if reqbufs is not done, the user would use read() */
1650 if (gspca_dev->nframes == 0) {
1651 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1652 return POLLERR; /* not the 1st time */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001653 ret = read_alloc(gspca_dev, file);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001654 if (ret != 0)
1655 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001656 }
1657
1658 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1659 return POLLERR;
1660 if (!gspca_dev->present) {
1661 ret = POLLERR;
1662 goto out;
1663 }
1664
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001665 /* check the next incoming buffer */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001666 i = gspca_dev->fr_o;
1667 i = gspca_dev->fr_queue[i];
1668 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1669 ret = POLLIN | POLLRDNORM; /* something to read */
1670 else
1671 ret = 0;
1672out:
1673 mutex_unlock(&gspca_dev->queue_lock);
1674 return ret;
1675}
1676
1677static ssize_t dev_read(struct file *file, char __user *data,
1678 size_t count, loff_t *ppos)
1679{
1680 struct gspca_dev *gspca_dev = file->private_data;
1681 struct gspca_frame *frame;
1682 struct v4l2_buffer v4l2_buf;
1683 struct timeval timestamp;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001684 int n, ret, ret2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001685
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001686 PDEBUG(D_FRAM, "read (%zd)", count);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001687 if (!gspca_dev->present)
1688 return -ENODEV;
1689 switch (gspca_dev->memory) {
1690 case GSPCA_MEMORY_NO: /* first time */
1691 ret = read_alloc(gspca_dev, file);
1692 if (ret != 0)
1693 return ret;
1694 break;
1695 case GSPCA_MEMORY_READ:
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001696 if (gspca_dev->capt_file == file)
1697 break;
1698 /* fall thru */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001699 default:
1700 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001701 }
1702
1703 /* get a frame */
1704 jiffies_to_timeval(get_jiffies_64(), &timestamp);
1705 timestamp.tv_sec--;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001706 n = 2;
1707 for (;;) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001708 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1709 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001710 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001711 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1712 if (ret != 0) {
1713 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1714 return ret;
1715 }
1716
1717 /* if the process slept for more than 1 second,
Frank Zago219423c2008-09-26 07:43:54 -03001718 * get a newer frame */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001719 frame = &gspca_dev->frame[v4l2_buf.index];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001720 if (--n < 0)
1721 break; /* avoid infinite loop */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001722 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1723 break;
1724 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1725 if (ret != 0) {
1726 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1727 return ret;
1728 }
1729 }
1730
1731 /* copy the frame */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001732 if (count > frame->v4l2_buf.bytesused)
1733 count = frame->v4l2_buf.bytesused;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001734 ret = copy_to_user(data, frame->data, count);
1735 if (ret != 0) {
1736 PDEBUG(D_ERR|D_STREAM,
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001737 "read cp to user lack %d / %zd", ret, count);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001738 ret = -EFAULT;
1739 goto out;
1740 }
1741 ret = count;
1742out:
1743 /* in each case, requeue the buffer */
1744 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1745 if (ret2 != 0)
1746 return ret2;
1747 return ret;
1748}
1749
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001750static void dev_release(struct video_device *vfd)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001751{
1752 /* nothing */
1753}
1754
1755static struct file_operations dev_fops = {
1756 .owner = THIS_MODULE,
1757 .open = dev_open,
1758 .release = dev_close,
1759 .read = dev_read,
1760 .mmap = dev_mmap,
1761 .ioctl = video_ioctl2,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001762#ifdef CONFIG_COMPAT
1763 .compat_ioctl = v4l_compat_ioctl32,
1764#endif
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001765 .llseek = no_llseek,
1766 .poll = dev_poll,
1767};
1768
Hans Verkuila3998102008-07-21 02:57:38 -03001769static const struct v4l2_ioctl_ops dev_ioctl_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001770 .vidioc_querycap = vidioc_querycap,
1771 .vidioc_dqbuf = vidioc_dqbuf,
1772 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001773 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1774 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1775 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1776 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001777 .vidioc_streamon = vidioc_streamon,
1778 .vidioc_queryctrl = vidioc_queryctrl,
1779 .vidioc_g_ctrl = vidioc_g_ctrl,
1780 .vidioc_s_ctrl = vidioc_s_ctrl,
1781 .vidioc_querymenu = vidioc_querymenu,
1782 .vidioc_enum_input = vidioc_enum_input,
1783 .vidioc_g_input = vidioc_g_input,
1784 .vidioc_s_input = vidioc_s_input,
1785 .vidioc_reqbufs = vidioc_reqbufs,
1786 .vidioc_querybuf = vidioc_querybuf,
1787 .vidioc_streamoff = vidioc_streamoff,
1788 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1789 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1790 .vidioc_g_parm = vidioc_g_parm,
1791 .vidioc_s_parm = vidioc_s_parm,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001792 .vidioc_s_std = vidioc_s_std,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001793#ifdef CONFIG_VIDEO_V4L1_COMPAT
1794 .vidiocgmbuf = vidiocgmbuf,
1795#endif
1796};
1797
Hans Verkuila3998102008-07-21 02:57:38 -03001798static struct video_device gspca_template = {
1799 .name = "gspca main driver",
Hans Verkuila3998102008-07-21 02:57:38 -03001800 .fops = &dev_fops,
1801 .ioctl_ops = &dev_ioctl_ops,
1802 .release = dev_release, /* mandatory */
1803 .minor = -1,
1804};
1805
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001806/*
1807 * probe and create a new gspca device
1808 *
1809 * This function must be called by the sub-driver when it is
1810 * called for probing a new device.
1811 */
1812int gspca_dev_probe(struct usb_interface *intf,
1813 const struct usb_device_id *id,
1814 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001815 int dev_size,
1816 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001817{
1818 struct usb_interface_descriptor *interface;
1819 struct gspca_dev *gspca_dev;
1820 struct usb_device *dev = interface_to_usbdev(intf);
1821 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001822
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001823 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001824
1825 /* we don't handle multi-config cameras */
1826 if (dev->descriptor.bNumConfigurations != 1)
1827 return -ENODEV;
1828 interface = &intf->cur_altsetting->desc;
1829 if (interface->bInterfaceNumber > 0)
1830 return -ENODEV;
1831
1832 /* create the device */
1833 if (dev_size < sizeof *gspca_dev)
1834 dev_size = sizeof *gspca_dev;
1835 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
Erik Andrene293e592008-10-03 08:46:50 -03001836 if (!gspca_dev) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001837 err("couldn't kzalloc gspca struct");
Erik Andrene293e592008-10-03 08:46:50 -03001838 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001839 }
Frank Zago6b060ff2008-09-28 08:12:22 -03001840 kref_init(&gspca_dev->kref);
Jean-Francois Moine8295d992008-09-03 17:12:19 -03001841 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
1842 if (!gspca_dev->usb_buf) {
1843 err("out of memory");
Erik Andrene293e592008-10-03 08:46:50 -03001844 ret = -ENOMEM;
Jean-Francois Moine8295d992008-09-03 17:12:19 -03001845 goto out;
1846 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001847 gspca_dev->dev = dev;
1848 gspca_dev->iface = interface->bInterfaceNumber;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001849 gspca_dev->nbalt = intf->num_altsetting;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001850 gspca_dev->sd_desc = sd_desc;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001851 gspca_dev->nbufread = 2;
1852
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001853 /* configure the subdriver and initialize the USB device */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001854 ret = gspca_dev->sd_desc->config(gspca_dev, id);
1855 if (ret < 0)
1856 goto out;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001857 ret = gspca_dev->sd_desc->init(gspca_dev);
1858 if (ret < 0)
1859 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001860 ret = gspca_set_alt0(gspca_dev);
1861 if (ret < 0)
1862 goto out;
1863 gspca_set_default_mode(gspca_dev);
1864
1865 mutex_init(&gspca_dev->usb_lock);
1866 mutex_init(&gspca_dev->read_lock);
1867 mutex_init(&gspca_dev->queue_lock);
1868 init_waitqueue_head(&gspca_dev->wq);
1869
1870 /* init video stuff */
1871 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
Hans Verkuil5e85e732008-07-20 06:31:39 -03001872 gspca_dev->vdev.parent = &dev->dev;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001873 memcpy(&gspca_dev->fops, &dev_fops, sizeof gspca_dev->fops);
1874 gspca_dev->vdev.fops = &gspca_dev->fops;
1875 gspca_dev->fops.owner = module; /* module protection */
Oliver Neukumee56a4d2008-07-27 14:01:59 -03001876 gspca_dev->present = 1;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001877 ret = video_register_device(&gspca_dev->vdev,
1878 VFL_TYPE_GRABBER,
1879 video_nr);
1880 if (ret < 0) {
1881 err("video_register_device err %d", ret);
1882 goto out;
1883 }
1884
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001885 usb_set_intfdata(intf, gspca_dev);
1886 PDEBUG(D_PROBE, "probe ok");
1887 return 0;
1888out:
Frank Zago6b060ff2008-09-28 08:12:22 -03001889 kref_put(&gspca_dev->kref, gspca_delete);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001890 return ret;
1891}
1892EXPORT_SYMBOL(gspca_dev_probe);
1893
1894/*
1895 * USB disconnection
1896 *
1897 * This function must be called by the sub-driver
1898 * when the device disconnects, after the specific resources are freed.
1899 */
1900void gspca_disconnect(struct usb_interface *intf)
1901{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001902 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001903
Frank Zago6b060ff2008-09-28 08:12:22 -03001904 usb_set_intfdata(intf, NULL);
1905
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001906/* We don't want people trying to open up the device */
1907 video_unregister_device(&gspca_dev->vdev);
Frank Zago6b060ff2008-09-28 08:12:22 -03001908
Jean-Francois Moine3145b8c2008-10-05 04:22:10 -03001909 gspca_dev->present = 0;
1910 gspca_dev->streaming = 0;
1911
Frank Zago6b060ff2008-09-28 08:12:22 -03001912 kref_put(&gspca_dev->kref, gspca_delete);
1913
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001914 PDEBUG(D_PROBE, "disconnect complete");
1915}
1916EXPORT_SYMBOL(gspca_disconnect);
1917
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001918#ifdef CONFIG_PM
1919int gspca_suspend(struct usb_interface *intf, pm_message_t message)
1920{
1921 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1922
1923 if (!gspca_dev->streaming)
1924 return 0;
1925 gspca_dev->frozen = 1; /* avoid urb error messages */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001926 if (gspca_dev->sd_desc->stopN)
1927 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001928 destroy_urbs(gspca_dev);
1929 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001930 if (gspca_dev->sd_desc->stop0)
1931 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001932 return 0;
1933}
1934EXPORT_SYMBOL(gspca_suspend);
1935
1936int gspca_resume(struct usb_interface *intf)
1937{
1938 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1939
1940 gspca_dev->frozen = 0;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001941 gspca_dev->sd_desc->init(gspca_dev);
1942 if (gspca_dev->streaming)
1943 return gspca_init_transfer(gspca_dev);
Jean-Francois Moine1e89e2d2008-09-03 17:12:15 -03001944 return 0;
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001945}
1946EXPORT_SYMBOL(gspca_resume);
1947#endif
Hans de Goededcef3232008-07-10 10:40:53 -03001948/* -- cam driver utility functions -- */
1949
1950/* auto gain and exposure algorithm based on the knee algorithm described here:
1951 http://ytse.tricolour.net/docs/LowLightOptimization.html
1952
1953 Returns 0 if no changes were made, 1 if the gain and or exposure settings
1954 where changed. */
1955int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
1956 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
1957{
1958 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
1959 const struct ctrl *gain_ctrl = NULL;
1960 const struct ctrl *exposure_ctrl = NULL;
1961 const struct ctrl *autogain_ctrl = NULL;
1962 int retval = 0;
1963
1964 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1965 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
1966 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1967 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
1968 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
1969 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
1970 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1971 }
1972 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
1973 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
1974 "on cam without (auto)gain/exposure");
1975 return 0;
1976 }
1977
1978 if (gain_ctrl->get(gspca_dev, &gain) ||
1979 exposure_ctrl->get(gspca_dev, &exposure) ||
1980 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
1981 return 0;
1982
1983 orig_gain = gain;
1984 orig_exposure = exposure;
1985
1986 /* If we are of a multiple of deadzone, do multiple steps to reach the
1987 desired lumination fast (with the risc of a slight overshoot) */
1988 steps = abs(desired_avg_lum - avg_lum) / deadzone;
1989
1990 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d\n",
1991 avg_lum, desired_avg_lum, steps);
1992
1993 for (i = 0; i < steps; i++) {
1994 if (avg_lum > desired_avg_lum) {
1995 if (gain > gain_knee)
1996 gain--;
1997 else if (exposure > exposure_knee)
1998 exposure--;
1999 else if (gain > gain_ctrl->qctrl.default_value)
2000 gain--;
2001 else if (exposure > exposure_ctrl->qctrl.minimum)
2002 exposure--;
2003 else if (gain > gain_ctrl->qctrl.minimum)
2004 gain--;
2005 else
2006 break;
2007 } else {
2008 if (gain < gain_ctrl->qctrl.default_value)
2009 gain++;
2010 else if (exposure < exposure_knee)
2011 exposure++;
2012 else if (gain < gain_knee)
2013 gain++;
2014 else if (exposure < exposure_ctrl->qctrl.maximum)
2015 exposure++;
2016 else if (gain < gain_ctrl->qctrl.maximum)
2017 gain++;
2018 else
2019 break;
2020 }
2021 }
2022
2023 if (gain != orig_gain) {
2024 gain_ctrl->set(gspca_dev, gain);
2025 retval = 1;
2026 }
2027 if (exposure != orig_exposure) {
2028 exposure_ctrl->set(gspca_dev, exposure);
2029 retval = 1;
2030 }
2031
2032 return retval;
2033}
2034EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2035
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002036/* -- module insert / remove -- */
2037static int __init gspca_init(void)
2038{
Jean-Francois Moinebb64e862008-07-19 06:49:28 -03002039 info("main v%d.%d.%d registered",
2040 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2041 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2042 DRIVER_VERSION_NUMBER & 0xff);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002043 return 0;
2044}
2045static void __exit gspca_exit(void)
2046{
2047 info("main deregistered");
2048}
2049
2050module_init(gspca_init);
2051module_exit(gspca_exit);
2052
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03002053#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002054module_param_named(debug, gspca_debug, int, 0644);
2055MODULE_PARM_DESC(debug,
2056 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002057 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
2058 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002059#endif