blob: a2741d7dccfeca4696c0167fce5b317f5eba7e56 [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>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030033#include <asm/page.h>
Jean-Francois Moine956e42d2008-07-01 10:03:42 -030034#include <linux/uaccess.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030035#include <linux/jiffies.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030036#include <media/v4l2-ioctl.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030037
38#include "gspca.h"
39
Jean-Francois Moined43fa322008-06-12 10:58:58 -030040/* global values */
Jean-Francois Moine8fee8452009-01-17 04:46:38 -030041#define DEF_NURBS 3 /* default number of URBs */
42#if DEF_NURBS > MAX_NURBS
43#error "DEF_NURBS too big"
44#endif
Jean-Francois Moined43fa322008-06-12 10:58:58 -030045
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030046MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
47MODULE_DESCRIPTION("GSPCA USB Camera Driver");
48MODULE_LICENSE("GPL");
49
Jean-Francois Moineafd46292008-12-31 07:27:42 -030050#define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 5, 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030051
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 Moine8d584a52009-01-13 06:07:59 -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 Moine090264e2009-02-12 08:05:45 -0300135 if (urb->status == -ESHUTDOWN)
136 return; /* disconnection */
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300137#ifdef CONFIG_PM
Jean-Francois Moine6a709742008-09-03 16:48:10 -0300138 if (!gspca_dev->frozen)
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300139#endif
Jean-Francois Moine6a709742008-09-03 16:48:10 -0300140 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300141 return;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300142 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300143 pkt_scan = gspca_dev->sd_desc->pkt_scan;
144 for (i = 0; i < urb->number_of_packets; i++) {
145
146 /* check the availability of the frame buffer */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300147 frame = gspca_get_i_frame(gspca_dev);
148 if (!frame) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300149 gspca_dev->last_packet_type = DISCARD_PACKET;
150 break;
151 }
152
153 /* check the packet status and length */
154 len = urb->iso_frame_desc[i].actual_length;
Jean-Francois Moineff374742008-10-23 07:29:51 -0300155 if (len == 0) {
156 if (gspca_dev->empty_packet == 0)
157 gspca_dev->empty_packet = 1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300158 continue;
Jean-Francois Moineff374742008-10-23 07:29:51 -0300159 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300160 st = urb->iso_frame_desc[i].status;
161 if (st) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300162 PDEBUG(D_ERR,
163 "ISOC data error: [%d] len=%d, status=%d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300164 i, len, st);
165 gspca_dev->last_packet_type = DISCARD_PACKET;
166 continue;
167 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300168
169 /* let the packet be analyzed by the subdriver */
170 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
171 i, urb->iso_frame_desc[i].offset, len);
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300172 data = (u8 *) urb->transfer_buffer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300173 + urb->iso_frame_desc[i].offset;
174 pkt_scan(gspca_dev, frame, data, len);
175 }
176
177 /* resubmit the URB */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300178 st = usb_submit_urb(urb, GFP_ATOMIC);
179 if (st < 0)
180 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
181}
182
183/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300184 * ISOC message interrupt from the USB device
185 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300186 * Analyse each packet and call the subdriver for copy to the frame buffer.
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300187 */
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300188static void isoc_irq(struct urb *urb)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300189{
190 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
191
Hans de Goede3647fea2008-07-15 05:36:30 -0300192 PDEBUG(D_PACK, "isoc irq");
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300193 if (!gspca_dev->streaming)
194 return;
195 fill_frame(gspca_dev, urb);
196}
197
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300198/*
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300199 * bulk message interrupt from the USB device
200 */
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300201static void bulk_irq(struct urb *urb)
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300202{
203 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
204 struct gspca_frame *frame;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300205 int st;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300206
207 PDEBUG(D_PACK, "bulk irq");
208 if (!gspca_dev->streaming)
209 return;
Jean-Francois Moine880ae242008-11-18 04:19:52 -0300210 switch (urb->status) {
211 case 0:
212 break;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300213 case -ESHUTDOWN:
214 return; /* disconnection */
Jean-Francois Moine880ae242008-11-18 04:19:52 -0300215 case -ECONNRESET:
216 urb->status = 0;
217 break;
218 default:
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300219#ifdef CONFIG_PM
220 if (!gspca_dev->frozen)
221#endif
222 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300223 return;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300224 }
225
226 /* check the availability of the frame buffer */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300227 frame = gspca_get_i_frame(gspca_dev);
228 if (!frame) {
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300229 gspca_dev->last_packet_type = DISCARD_PACKET;
230 } else {
231 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
232 gspca_dev->sd_desc->pkt_scan(gspca_dev,
233 frame,
234 urb->transfer_buffer,
235 urb->actual_length);
236 }
Antonio Ospitedff369a2008-11-14 07:53:32 -0300237
238 /* resubmit the URB */
239 if (gspca_dev->cam.bulk_nurbs != 0) {
240 st = usb_submit_urb(urb, GFP_ATOMIC);
241 if (st < 0)
242 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
243 }
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300244}
245
246/*
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300247 * add data to the current frame
248 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300249 * This function is called by the subdrivers at interrupt level.
250 *
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300251 * To build a frame, these ones must add
252 * - one FIRST_PACKET
253 * - 0 or many INTER_PACKETs
254 * - one LAST_PACKET
255 * DISCARD_PACKET invalidates the whole frame.
256 * On LAST_PACKET, a new frame is returned.
257 */
258struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
Erik Andrene293e592008-10-03 08:46:50 -0300259 enum gspca_packet_type packet_type,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300260 struct gspca_frame *frame,
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300261 const __u8 *data,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300262 int len)
263{
264 int i, j;
265
Hans de Goedee2997a72008-04-23 08:09:12 -0300266 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300267
268 /* when start of a new frame, if the current frame buffer
269 * is not queued, discard the whole frame */
270 if (packet_type == FIRST_PACKET) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300271 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
272 != V4L2_BUF_FLAG_QUEUED) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300273 gspca_dev->last_packet_type = DISCARD_PACKET;
274 return frame;
275 }
276 frame->data_end = frame->data;
277 jiffies_to_timeval(get_jiffies_64(),
278 &frame->v4l2_buf.timestamp);
279 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300280 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
Jean-Francois Moine06ca78f2008-07-22 03:45:08 -0300281 if (packet_type == LAST_PACKET)
282 gspca_dev->last_packet_type = packet_type;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300283 return frame;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300284 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300285
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300286 /* append the packet to the frame buffer */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300287 if (len > 0) {
288 if (frame->data_end - frame->data + len
289 > frame->v4l2_buf.length) {
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -0300290 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300291 frame->data_end - frame->data + len,
292 frame->v4l2_buf.length);
293 packet_type = DISCARD_PACKET;
294 } else {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300295 memcpy(frame->data_end, data, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300296 frame->data_end += len;
297 }
298 }
299 gspca_dev->last_packet_type = packet_type;
300
Frank Zago219423c2008-09-26 07:43:54 -0300301 /* if last packet, wake up the application and advance in the queue */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300302 if (packet_type == LAST_PACKET) {
303 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
304 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
305 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300306 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300307 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
308 gspca_dev->fr_i = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300309 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
310 frame->v4l2_buf.bytesused,
311 gspca_dev->fr_q,
312 i,
313 gspca_dev->fr_o);
314 j = gspca_dev->fr_queue[i];
315 frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300316 }
317 return frame;
318}
319EXPORT_SYMBOL(gspca_frame_add);
320
321static int gspca_is_compressed(__u32 format)
322{
323 switch (format) {
324 case V4L2_PIX_FMT_MJPEG:
325 case V4L2_PIX_FMT_JPEG:
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300326 case V4L2_PIX_FMT_SPCA561:
Hans de Goedeab8f12c2008-07-04 18:29:32 -0300327 case V4L2_PIX_FMT_PAC207:
Kyle Guinnd661e622009-01-16 05:36:14 -0300328 case V4L2_PIX_FMT_MR97310A:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300329 return 1;
330 }
331 return 0;
332}
333
334static void *rvmalloc(unsigned long size)
335{
336 void *mem;
337 unsigned long adr;
338
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300339 mem = vmalloc_32(size);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300340 if (mem != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300341 adr = (unsigned long) mem;
342 while ((long) size > 0) {
343 SetPageReserved(vmalloc_to_page((void *) adr));
344 adr += PAGE_SIZE;
345 size -= PAGE_SIZE;
346 }
347 }
348 return mem;
349}
350
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300351static void rvfree(void *mem, long size)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300352{
353 unsigned long adr;
354
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300355 adr = (unsigned long) mem;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300356 while (size > 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300357 ClearPageReserved(vmalloc_to_page((void *) adr));
358 adr += PAGE_SIZE;
359 size -= PAGE_SIZE;
360 }
361 vfree(mem);
362}
363
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300364static int frame_alloc(struct gspca_dev *gspca_dev,
365 unsigned int count)
366{
367 struct gspca_frame *frame;
368 unsigned int frsz;
369 int i;
370
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300371 i = gspca_dev->curr_mode;
372 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300373 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300374 frsz = PAGE_ALIGN(frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300375 gspca_dev->frsz = frsz;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300376 if (count > GSPCA_MAX_FRAMES)
377 count = GSPCA_MAX_FRAMES;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300378 gspca_dev->frbuf = rvmalloc(frsz * count);
379 if (!gspca_dev->frbuf) {
380 err("frame alloc failed");
381 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300382 }
383 gspca_dev->nframes = count;
384 for (i = 0; i < count; i++) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300385 frame = &gspca_dev->frame[i];
386 frame->v4l2_buf.index = i;
387 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
388 frame->v4l2_buf.flags = 0;
389 frame->v4l2_buf.field = V4L2_FIELD_NONE;
390 frame->v4l2_buf.length = frsz;
391 frame->v4l2_buf.memory = gspca_dev->memory;
392 frame->v4l2_buf.sequence = 0;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300393 frame->data = frame->data_end =
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300394 gspca_dev->frbuf + i * frsz;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300395 frame->v4l2_buf.m.offset = i * frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300396 }
397 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
398 gspca_dev->last_packet_type = DISCARD_PACKET;
399 gspca_dev->sequence = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300400 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300401}
402
403static void frame_free(struct gspca_dev *gspca_dev)
404{
405 int i;
406
407 PDEBUG(D_STREAM, "frame free");
Harvey Harrisonefab8212008-07-05 06:12:47 -0300408 if (gspca_dev->frbuf != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300409 rvfree(gspca_dev->frbuf,
410 gspca_dev->nframes * gspca_dev->frsz);
411 gspca_dev->frbuf = NULL;
412 for (i = 0; i < gspca_dev->nframes; i++)
413 gspca_dev->frame[i].data = NULL;
414 }
415 gspca_dev->nframes = 0;
416}
417
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300418static void destroy_urbs(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300419{
420 struct urb *urb;
421 unsigned int i;
422
423 PDEBUG(D_STREAM, "kill transfer");
Erik Andrén44732812008-10-13 15:52:46 -0300424 for (i = 0; i < MAX_NURBS; i++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300425 urb = gspca_dev->urb[i];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300426 if (urb == NULL)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300427 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300428
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300429 gspca_dev->urb[i] = NULL;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300430 usb_kill_urb(urb);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300431 if (urb->transfer_buffer != NULL)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300432 usb_buffer_free(gspca_dev->dev,
433 urb->transfer_buffer_length,
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300434 urb->transfer_buffer,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300435 urb->transfer_dma);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300436 usb_free_urb(urb);
437 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300438}
439
440/*
Frank Zago219423c2008-09-26 07:43:54 -0300441 * look for an input transfer endpoint in an alternate setting
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300442 */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300443static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300444 __u8 xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300445{
446 struct usb_host_endpoint *ep;
447 int i, attr;
448
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300449 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
450 ep = &alt->endpoint[i];
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300451 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
452 if (attr == xfer)
453 return ep;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300454 }
455 return NULL;
456}
457
458/*
Frank Zago219423c2008-09-26 07:43:54 -0300459 * look for an input (isoc or bulk) endpoint
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300460 *
461 * The endpoint is defined by the subdriver.
462 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
463 * This routine may be called many times when the bandwidth is too small
464 * (the bandwidth is checked on urb submit).
465 */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300466static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300467{
468 struct usb_interface *intf;
469 struct usb_host_endpoint *ep;
470 int i, ret;
471
472 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300473 ep = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300474 i = gspca_dev->alt; /* previous alt setting */
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300475
476 /* try isoc */
Jean-Francois Moine0dc641d2009-03-19 05:55:22 -0300477 while (--i >= 0) {
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300478 ep = alt_xfer(&intf->altsetting[i],
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300479 USB_ENDPOINT_XFER_ISOC);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300480 if (ep)
481 break;
482 }
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300483
Jean-Francois Moine0dc641d2009-03-19 05:55:22 -0300484 /* if no isoc, try bulk (alt 0 only) */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300485 if (ep == NULL) {
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300486 ep = alt_xfer(&intf->altsetting[0],
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300487 USB_ENDPOINT_XFER_BULK);
488 if (ep == NULL) {
489 err("no transfer endpoint found");
490 return NULL;
491 }
Jean-Francois Moine0dc641d2009-03-19 05:55:22 -0300492 i = 0;
493 gspca_dev->bulk = 1;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300494 }
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300495 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300496 i, ep->desc.bEndpointAddress);
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300497 if (i > 0) {
498 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
499 if (ret < 0) {
500 err("set interface err %d", ret);
501 return NULL;
502 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300503 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300504 gspca_dev->alt = i; /* memorize the current alt setting */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300505 return ep;
506}
507
508/*
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300509 * create the URBs for image transfer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300510 */
511static int create_urbs(struct gspca_dev *gspca_dev,
512 struct usb_host_endpoint *ep)
513{
514 struct urb *urb;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300515 int n, nurbs, i, psize, npkt, bsize;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300516
517 /* calculate the packet size and the number of packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300518 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300519
Jean-Francois Moine0dc641d2009-03-19 05:55:22 -0300520 if (!gspca_dev->bulk) { /* isoc */
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300521
522 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
523 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300524 npkt = ISO_MAX_SIZE / psize;
525 if (npkt > ISO_MAX_PKT)
526 npkt = ISO_MAX_PKT;
527 bsize = psize * npkt;
528 PDEBUG(D_STREAM,
529 "isoc %d pkts size %d = bsize:%d",
530 npkt, psize, bsize);
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300531 nurbs = DEF_NURBS;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300532 } else { /* bulk */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300533 npkt = 0;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300534 bsize = gspca_dev->cam.bulk_size;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300535 if (bsize == 0)
536 bsize = psize;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300537 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300538 if (gspca_dev->cam.bulk_nurbs != 0)
539 nurbs = gspca_dev->cam.bulk_nurbs;
540 else
541 nurbs = 1;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300542 }
543
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300544 gspca_dev->nurbs = nurbs;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300545 for (n = 0; n < nurbs; n++) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300546 urb = usb_alloc_urb(npkt, GFP_KERNEL);
547 if (!urb) {
548 err("usb_alloc_urb failed");
Jean-Francois Moine0d0548e2008-09-05 12:49:54 -0300549 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300550 return -ENOMEM;
551 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300552 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300553 bsize,
554 GFP_KERNEL,
555 &urb->transfer_dma);
556
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300557 if (urb->transfer_buffer == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300558 usb_free_urb(urb);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300559 err("usb_buffer_urb failed");
Jean-Francois Moine0d0548e2008-09-05 12:49:54 -0300560 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300561 return -ENOMEM;
562 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300563 gspca_dev->urb[n] = urb;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300564 urb->dev = gspca_dev->dev;
565 urb->context = gspca_dev;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300566 urb->transfer_buffer_length = bsize;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300567 if (npkt != 0) { /* ISOC */
568 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
569 ep->desc.bEndpointAddress);
Frank Zago6c49da72008-09-28 07:43:00 -0300570 urb->transfer_flags = URB_ISO_ASAP
571 | URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300572 urb->interval = ep->desc.bInterval;
573 urb->complete = isoc_irq;
574 urb->number_of_packets = npkt;
575 for (i = 0; i < npkt; i++) {
576 urb->iso_frame_desc[i].length = psize;
577 urb->iso_frame_desc[i].offset = psize * i;
578 }
579 } else { /* bulk */
580 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
581 ep->desc.bEndpointAddress),
Frank Zago6c49da72008-09-28 07:43:00 -0300582 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300583 urb->complete = bulk_irq;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300584 }
585 }
586 return 0;
587}
588
589/*
590 * start the USB transfer
591 */
592static int gspca_init_transfer(struct gspca_dev *gspca_dev)
593{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300594 struct usb_host_endpoint *ep;
595 int n, ret;
596
Hans de Goedee2997a72008-04-23 08:09:12 -0300597 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
598 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300599
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300600 if (!gspca_dev->present) {
601 ret = -ENODEV;
602 goto out;
603 }
604
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300605 /* set the higher alternate setting and
606 * loop until urb submit succeeds */
607 gspca_dev->alt = gspca_dev->nbalt;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300608 for (;;) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300609 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300610 ep = get_ep(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300611 if (ep == NULL) {
612 ret = -EIO;
613 goto out;
614 }
615 ret = create_urbs(gspca_dev, ep);
616 if (ret < 0)
617 goto out;
618
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300619 /* clear the bulk endpoint */
Jean-Francois Moine0dc641d2009-03-19 05:55:22 -0300620 if (gspca_dev->bulk)
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300621 usb_clear_halt(gspca_dev->dev,
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300622 gspca_dev->urb[0]->pipe);
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300623
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300624 /* start the cam */
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300625 ret = gspca_dev->sd_desc->start(gspca_dev);
626 if (ret < 0) {
627 destroy_urbs(gspca_dev);
628 goto out;
629 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300630 gspca_dev->streaming = 1;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300631
Antonio Ospitedff369a2008-11-14 07:53:32 -0300632 /* some bulk transfers are started by the subdriver */
Jean-Francois Moine0dc641d2009-03-19 05:55:22 -0300633 if (gspca_dev->bulk && gspca_dev->cam.bulk_nurbs == 0)
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300634 break;
635
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300636 /* submit the URBs */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300637 for (n = 0; n < gspca_dev->nurbs; n++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300638 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300639 if (ret < 0) {
640 PDEBUG(D_ERR|D_STREAM,
641 "usb_submit_urb [%d] err %d", n, ret);
Hans de Goedee2997a72008-04-23 08:09:12 -0300642 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300643 destroy_urbs(gspca_dev);
Jean-Francois Moine9edf1a62008-11-23 15:53:28 -0300644 if (ret == -ENOSPC) {
Jean-Francois Moineabf485f2008-11-24 06:38:21 -0300645 msleep(20); /* wait for kill
Jean-Francois Moine9edf1a62008-11-23 15:53:28 -0300646 * complete */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300647 break; /* try the previous alt */
Jean-Francois Moine9edf1a62008-11-23 15:53:28 -0300648 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300649 goto out;
650 }
651 }
652 if (ret >= 0)
653 break;
654 }
655out:
656 mutex_unlock(&gspca_dev->usb_lock);
657 return ret;
658}
659
660static int gspca_set_alt0(struct gspca_dev *gspca_dev)
661{
662 int ret;
663
664 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
665 if (ret < 0)
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -0300666 PDEBUG(D_ERR|D_STREAM, "set alt 0 err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300667 return ret;
668}
669
Frank Zago219423c2008-09-26 07:43:54 -0300670/* Note: both the queue and the usb locks should be held when calling this */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300671static void gspca_stream_off(struct gspca_dev *gspca_dev)
672{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300673 gspca_dev->streaming = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300674 if (gspca_dev->present) {
675 if (gspca_dev->sd_desc->stopN)
676 gspca_dev->sd_desc->stopN(gspca_dev);
677 destroy_urbs(gspca_dev);
Jean-Francois Moined08e2ce2009-01-20 16:21:44 -0300678 gspca_set_alt0(gspca_dev);
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300679 }
680
681 /* always call stop0 to free the subdriver's resources */
Jean-Francois Moine98522a72008-11-18 06:33:08 -0300682 if (gspca_dev->sd_desc->stop0)
683 gspca_dev->sd_desc->stop0(gspca_dev);
684 PDEBUG(D_STREAM, "stream off OK");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300685}
686
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300687static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300688{
689 int i;
690
691 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
692 gspca_dev->curr_mode = i;
693 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
694 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300695 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300696}
697
698static int wxh_to_mode(struct gspca_dev *gspca_dev,
699 int width, int height)
700{
701 int i;
702
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300703 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
704 if (width >= gspca_dev->cam.cam_mode[i].width
705 && height >= gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300706 break;
707 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300708 return i;
709}
710
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300711/*
712 * search a mode with the right pixel format
713 */
714static int gspca_get_mode(struct gspca_dev *gspca_dev,
715 int mode,
716 int pixfmt)
717{
718 int modeU, modeD;
719
720 modeU = modeD = mode;
721 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
722 if (--modeD >= 0) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300723 if (gspca_dev->cam.cam_mode[modeD].pixelformat
724 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300725 return modeD;
726 }
727 if (++modeU < gspca_dev->cam.nmodes) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300728 if (gspca_dev->cam.cam_mode[modeU].pixelformat
729 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300730 return modeU;
731 }
732 }
733 return -EINVAL;
734}
735
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300736static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300737 struct v4l2_fmtdesc *fmtdesc)
738{
739 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300740 int i, j, index;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300741 __u32 fmt_tb[8];
742
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300743 /* give an index to each format */
744 index = 0;
745 j = 0;
746 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300747 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300748 j = 0;
749 for (;;) {
750 if (fmt_tb[j] == fmt_tb[index])
751 break;
752 j++;
753 }
754 if (j == index) {
755 if (fmtdesc->index == index)
756 break; /* new format */
757 index++;
Julia Lawall80297122008-11-12 23:18:21 -0300758 if (index >= ARRAY_SIZE(fmt_tb))
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300759 return -EINVAL;
760 }
761 }
762 if (i < 0)
763 return -EINVAL; /* no more format */
764
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300765 fmtdesc->pixelformat = fmt_tb[index];
766 if (gspca_is_compressed(fmt_tb[index]))
767 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
768 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
769 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
770 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
771 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
772 fmtdesc->description[4] = '\0';
773 return 0;
774}
775
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300776static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300777 struct v4l2_format *fmt)
778{
779 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300780 int mode;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300781
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300782 mode = gspca_dev->curr_mode;
783 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
784 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300785 return 0;
786}
787
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300788static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300789 struct v4l2_format *fmt)
790{
Hans de Goede24c530b2008-07-04 17:55:18 -0300791 int w, h, mode, mode2;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300792
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300793 w = fmt->fmt.pix.width;
794 h = fmt->fmt.pix.height;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300795
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300796#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300797 if (gspca_debug & D_CONF)
798 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
799#endif
800 /* search the closest mode for width and height */
801 mode = wxh_to_mode(gspca_dev, w, h);
802
803 /* OK if right palette */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300804 if (gspca_dev->cam.cam_mode[mode].pixelformat
805 != fmt->fmt.pix.pixelformat) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300806
807 /* else, search the closest mode with the same pixel format */
808 mode2 = gspca_get_mode(gspca_dev, mode,
809 fmt->fmt.pix.pixelformat);
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300810 if (mode2 >= 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300811 mode = mode2;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300812/* else
813 ; * no chance, return this mode */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300814 }
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300815 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
816 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300817 return mode; /* used when s_fmt */
818}
819
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300820static int vidioc_try_fmt_vid_cap(struct file *file,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300821 void *priv,
822 struct v4l2_format *fmt)
823{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300824 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300825 int ret;
826
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300827 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300828 if (ret < 0)
829 return ret;
830 return 0;
831}
832
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300833static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300834 struct v4l2_format *fmt)
835{
836 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300837 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300838
Hans de Goedee2997a72008-04-23 08:09:12 -0300839 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
840 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300841
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300842 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300843 if (ret < 0)
844 goto out;
845
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300846 if (gspca_dev->nframes != 0
847 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
848 ret = -EINVAL;
849 goto out;
850 }
851
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300852 if (ret == gspca_dev->curr_mode) {
853 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300854 goto out; /* same mode */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300855 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300856
857 if (gspca_dev->streaming) {
858 ret = -EBUSY;
859 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -0300860 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300861 gspca_dev->width = fmt->fmt.pix.width;
862 gspca_dev->height = fmt->fmt.pix.height;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300863 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
864 gspca_dev->curr_mode = ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300865
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300866 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300867out:
868 mutex_unlock(&gspca_dev->queue_lock);
869 return ret;
870}
871
Hans de Goeded0182302008-11-19 17:10:47 -0300872static void gspca_release(struct video_device *vfd)
Frank Zago6b060ff2008-09-28 08:12:22 -0300873{
Hans de Goeded0182302008-11-19 17:10:47 -0300874 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
Frank Zago6b060ff2008-09-28 08:12:22 -0300875
Hans de Goeded0182302008-11-19 17:10:47 -0300876 PDEBUG(D_STREAM, "device released");
Frank Zago6b060ff2008-09-28 08:12:22 -0300877
878 kfree(gspca_dev->usb_buf);
879 kfree(gspca_dev);
880}
881
Hans Verkuilbec43662008-12-30 06:58:20 -0300882static int dev_open(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300883{
884 struct gspca_dev *gspca_dev;
885 int ret;
886
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300887 PDEBUG(D_STREAM, "%s open", current->comm);
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -0300888 gspca_dev = (struct gspca_dev *) video_devdata(file);
Hans de Goedee2997a72008-04-23 08:09:12 -0300889 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
890 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300891 if (!gspca_dev->present) {
892 ret = -ENODEV;
893 goto out;
894 }
895
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300896 if (gspca_dev->users > 4) { /* (arbitrary value) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300897 ret = -EBUSY;
898 goto out;
899 }
Jean-Francois Moine5c4fa002008-11-18 15:52:31 -0300900
901 /* protect the subdriver against rmmod */
902 if (!try_module_get(gspca_dev->module)) {
903 ret = -ENODEV;
904 goto out;
905 }
906
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300907 gspca_dev->users++;
Frank Zago6b060ff2008-09-28 08:12:22 -0300908
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300909 file->private_data = gspca_dev;
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300910#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300911 /* activate the v4l2 debug */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300912 if (gspca_debug & D_V4L2)
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -0300913 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -0300914 | V4L2_DEBUG_IOCTL_ARG;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300915 else
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -0300916 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -0300917 | V4L2_DEBUG_IOCTL_ARG);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300918#endif
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300919 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300920out:
921 mutex_unlock(&gspca_dev->queue_lock);
922 if (ret != 0)
923 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
924 else
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300925 PDEBUG(D_STREAM, "open done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300926 return ret;
927}
928
Hans Verkuilbec43662008-12-30 06:58:20 -0300929static int dev_close(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300930{
931 struct gspca_dev *gspca_dev = file->private_data;
932
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300933 PDEBUG(D_STREAM, "%s close", current->comm);
Hans de Goedee2997a72008-04-23 08:09:12 -0300934 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
935 return -ERESTARTSYS;
936 gspca_dev->users--;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300937
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -0300938 /* if the file did the capture, free the streaming resources */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300939 if (gspca_dev->capt_file == file) {
Jean-Francois Moinea1e82882008-09-03 17:12:17 -0300940 if (gspca_dev->streaming) {
941 mutex_lock(&gspca_dev->usb_lock);
942 gspca_stream_off(gspca_dev);
943 mutex_unlock(&gspca_dev->usb_lock);
944 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300945 frame_free(gspca_dev);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300946 gspca_dev->capt_file = NULL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300947 gspca_dev->memory = GSPCA_MEMORY_NO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300948 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300949 file->private_data = NULL;
Jean-Francois Moine5c4fa002008-11-18 15:52:31 -0300950 module_put(gspca_dev->module);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300951 mutex_unlock(&gspca_dev->queue_lock);
Frank Zago6b060ff2008-09-28 08:12:22 -0300952
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300953 PDEBUG(D_STREAM, "close done");
Frank Zago6b060ff2008-09-28 08:12:22 -0300954
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300955 return 0;
956}
957
958static int vidioc_querycap(struct file *file, void *priv,
959 struct v4l2_capability *cap)
960{
961 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300962 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300963
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300964 /* protect the access to the usb device */
965 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
966 return -ERESTARTSYS;
967 if (!gspca_dev->present) {
968 ret = -ENODEV;
969 goto out;
970 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300971 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -0300972 if (gspca_dev->dev->product != NULL) {
973 strncpy(cap->card, gspca_dev->dev->product,
974 sizeof cap->card);
975 } else {
976 snprintf(cap->card, sizeof cap->card,
977 "USB Camera (%04x:%04x)",
978 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
979 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
980 }
Thierry MERLE59320282009-01-21 15:32:26 -0300981 usb_make_path(gspca_dev->dev, cap->bus_info, sizeof(cap->bus_info));
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300982 cap->version = DRIVER_VERSION_NUMBER;
983 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
984 | V4L2_CAP_STREAMING
985 | V4L2_CAP_READWRITE;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300986 ret = 0;
987out:
988 mutex_unlock(&gspca_dev->usb_lock);
989 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300990}
991
992static int vidioc_queryctrl(struct file *file, void *priv,
993 struct v4l2_queryctrl *q_ctrl)
994{
995 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300996 int i, ix;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300997 u32 id;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300998
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300999 ix = -1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001000 id = q_ctrl->id;
1001 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1002 id &= V4L2_CTRL_ID_MASK;
1003 id++;
1004 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goede0f523c22008-09-03 17:12:14 -03001005 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001006 continue;
1007 if (ix < 0) {
1008 ix = i;
1009 continue;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001010 }
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001011 if (gspca_dev->sd_desc->ctrls[i].qctrl.id
1012 > gspca_dev->sd_desc->ctrls[ix].qctrl.id)
1013 continue;
1014 ix = i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001015 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001016 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001017 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001018 if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) {
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001019 ix = i;
1020 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001021 }
1022 }
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001023 if (ix < 0)
1024 return -EINVAL;
1025 memcpy(q_ctrl, &gspca_dev->sd_desc->ctrls[ix].qctrl,
1026 sizeof *q_ctrl);
1027 if (gspca_dev->ctrl_dis & (1 << ix))
1028 q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
1029 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001030}
1031
1032static int vidioc_s_ctrl(struct file *file, void *priv,
1033 struct v4l2_control *ctrl)
1034{
1035 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001036 const struct ctrl *ctrls;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001037 int i, ret;
1038
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001039 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1040 i < gspca_dev->sd_desc->nctrls;
1041 i++, ctrls++) {
1042 if (ctrl->id != ctrls->qctrl.id)
1043 continue;
Hans de Goede0f523c22008-09-03 17:12:14 -03001044 if (gspca_dev->ctrl_dis & (1 << i))
1045 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001046 if (ctrl->value < ctrls->qctrl.minimum
Rabin Vincentfcf5cb22008-08-03 07:58:54 -03001047 || ctrl->value > ctrls->qctrl.maximum)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001048 return -ERANGE;
1049 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
Hans de Goedee2997a72008-04-23 08:09:12 -03001050 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1051 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001052 if (gspca_dev->present)
1053 ret = ctrls->set(gspca_dev, ctrl->value);
1054 else
1055 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001056 mutex_unlock(&gspca_dev->usb_lock);
1057 return ret;
1058 }
1059 return -EINVAL;
1060}
1061
1062static int vidioc_g_ctrl(struct file *file, void *priv,
1063 struct v4l2_control *ctrl)
1064{
1065 struct gspca_dev *gspca_dev = priv;
1066
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001067 const struct ctrl *ctrls;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001068 int i, ret;
1069
1070 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1071 i < gspca_dev->sd_desc->nctrls;
1072 i++, ctrls++) {
1073 if (ctrl->id != ctrls->qctrl.id)
1074 continue;
Hans de Goede0f523c22008-09-03 17:12:14 -03001075 if (gspca_dev->ctrl_dis & (1 << i))
1076 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001077 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1078 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001079 if (gspca_dev->present)
1080 ret = ctrls->get(gspca_dev, &ctrl->value);
1081 else
1082 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001083 mutex_unlock(&gspca_dev->usb_lock);
1084 return ret;
1085 }
1086 return -EINVAL;
1087}
1088
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03001089/*fixme: have an audio flag in gspca_dev?*/
1090static int vidioc_s_audio(struct file *file, void *priv,
1091 struct v4l2_audio *audio)
1092{
1093 if (audio->index != 0)
1094 return -EINVAL;
1095 return 0;
1096}
1097
1098static int vidioc_g_audio(struct file *file, void *priv,
1099 struct v4l2_audio *audio)
1100{
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03001101 strcpy(audio->name, "Microphone");
1102 return 0;
1103}
1104
1105static int vidioc_enumaudio(struct file *file, void *priv,
1106 struct v4l2_audio *audio)
1107{
1108 if (audio->index != 0)
1109 return -EINVAL;
1110
1111 strcpy(audio->name, "Microphone");
1112 audio->capability = 0;
1113 audio->mode = 0;
1114 return 0;
1115}
1116
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001117static int vidioc_querymenu(struct file *file, void *priv,
1118 struct v4l2_querymenu *qmenu)
1119{
1120 struct gspca_dev *gspca_dev = priv;
1121
1122 if (!gspca_dev->sd_desc->querymenu)
1123 return -EINVAL;
1124 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1125}
1126
1127static int vidioc_enum_input(struct file *file, void *priv,
1128 struct v4l2_input *input)
1129{
1130 struct gspca_dev *gspca_dev = priv;
1131
1132 if (input->index != 0)
1133 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001134 input->type = V4L2_INPUT_TYPE_CAMERA;
Adam Bakerdfa76fa2009-03-29 19:17:10 -03001135 input->status = gspca_dev->cam.input_flags;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001136 strncpy(input->name, gspca_dev->sd_desc->name,
1137 sizeof input->name);
1138 return 0;
1139}
1140
1141static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1142{
1143 *i = 0;
1144 return 0;
1145}
1146
1147static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1148{
1149 if (i > 0)
1150 return -EINVAL;
1151 return (0);
1152}
1153
1154static int vidioc_reqbufs(struct file *file, void *priv,
1155 struct v4l2_requestbuffers *rb)
1156{
1157 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001158 int i, ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001159
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001160 switch (rb->memory) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001161 case GSPCA_MEMORY_READ: /* (internal call) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001162 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001163 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001164 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001165 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001166 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001167 }
Hans de Goedee2997a72008-04-23 08:09:12 -03001168 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1169 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001170
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001171 if (gspca_dev->memory != GSPCA_MEMORY_NO
1172 && gspca_dev->memory != rb->memory) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001173 ret = -EBUSY;
1174 goto out;
1175 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001176
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001177 /* only one file may do the capture */
1178 if (gspca_dev->capt_file != NULL
1179 && gspca_dev->capt_file != file) {
1180 ret = -EBUSY;
1181 goto out;
1182 }
1183
1184 /* if allocated, the buffers must not be mapped */
1185 for (i = 0; i < gspca_dev->nframes; i++) {
1186 if (gspca_dev->frame[i].vma_use_count) {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001187 ret = -EBUSY;
1188 goto out;
1189 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001190 }
1191
1192 /* stop streaming */
1193 if (gspca_dev->streaming) {
1194 mutex_lock(&gspca_dev->usb_lock);
1195 gspca_stream_off(gspca_dev);
1196 mutex_unlock(&gspca_dev->usb_lock);
1197 }
1198
1199 /* free the previous allocated buffers, if any */
1200 if (gspca_dev->nframes != 0) {
1201 frame_free(gspca_dev);
1202 gspca_dev->capt_file = NULL;
1203 }
1204 if (rb->count == 0) /* unrequest */
1205 goto out;
1206 gspca_dev->memory = rb->memory;
1207 ret = frame_alloc(gspca_dev, rb->count);
1208 if (ret == 0) {
1209 rb->count = gspca_dev->nframes;
1210 gspca_dev->capt_file = file;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001211 }
1212out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001213 mutex_unlock(&gspca_dev->queue_lock);
1214 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1215 return ret;
1216}
1217
1218static int vidioc_querybuf(struct file *file, void *priv,
1219 struct v4l2_buffer *v4l2_buf)
1220{
1221 struct gspca_dev *gspca_dev = priv;
1222 struct gspca_frame *frame;
1223
Jean-Francois Moine13752bd2008-12-19 15:08:51 -03001224 if (v4l2_buf->index < 0
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001225 || v4l2_buf->index >= gspca_dev->nframes)
1226 return -EINVAL;
1227
1228 frame = &gspca_dev->frame[v4l2_buf->index];
1229 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1230 return 0;
1231}
1232
1233static int vidioc_streamon(struct file *file, void *priv,
1234 enum v4l2_buf_type buf_type)
1235{
1236 struct gspca_dev *gspca_dev = priv;
1237 int ret;
1238
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001239 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1240 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001241 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1242 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001243
Jean-Francois Moine1edabe72008-12-19 15:17:25 -03001244 if (gspca_dev->nframes == 0
1245 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001246 ret = -EINVAL;
1247 goto out;
1248 }
1249 if (!gspca_dev->streaming) {
1250 ret = gspca_init_transfer(gspca_dev);
1251 if (ret < 0)
1252 goto out;
1253 }
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001254#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001255 if (gspca_debug & D_STREAM) {
1256 PDEBUG_MODE("stream on OK",
1257 gspca_dev->pixfmt,
1258 gspca_dev->width,
1259 gspca_dev->height);
1260 }
1261#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001262 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001263out:
1264 mutex_unlock(&gspca_dev->queue_lock);
1265 return ret;
1266}
1267
1268static int vidioc_streamoff(struct file *file, void *priv,
1269 enum v4l2_buf_type buf_type)
1270{
1271 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001272 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001273
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001274 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1275 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001276 if (!gspca_dev->streaming)
1277 return 0;
1278 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1279 return -ERESTARTSYS;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001280
1281 /* stop streaming */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001282 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1283 ret = -ERESTARTSYS;
1284 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001285 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001286 gspca_stream_off(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001287 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001288
1289 /* empty the application queues */
1290 for (i = 0; i < gspca_dev->nframes; i++)
1291 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1292 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1293 gspca_dev->last_packet_type = DISCARD_PACKET;
1294 gspca_dev->sequence = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001295 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001296out:
1297 mutex_unlock(&gspca_dev->queue_lock);
1298 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001299}
1300
1301static int vidioc_g_jpegcomp(struct file *file, void *priv,
1302 struct v4l2_jpegcompression *jpegcomp)
1303{
1304 struct gspca_dev *gspca_dev = priv;
1305 int ret;
1306
1307 if (!gspca_dev->sd_desc->get_jcomp)
1308 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001309 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1310 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001311 if (gspca_dev->present)
1312 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1313 else
1314 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001315 mutex_unlock(&gspca_dev->usb_lock);
1316 return ret;
1317}
1318
1319static int vidioc_s_jpegcomp(struct file *file, void *priv,
1320 struct v4l2_jpegcompression *jpegcomp)
1321{
1322 struct gspca_dev *gspca_dev = priv;
1323 int ret;
1324
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001325 if (!gspca_dev->sd_desc->set_jcomp)
1326 return -EINVAL;
Jim Parisf86d4a92008-12-10 05:50:44 -03001327 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1328 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001329 if (gspca_dev->present)
1330 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1331 else
1332 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001333 mutex_unlock(&gspca_dev->usb_lock);
1334 return ret;
1335}
1336
1337static int vidioc_g_parm(struct file *filp, void *priv,
1338 struct v4l2_streamparm *parm)
1339{
1340 struct gspca_dev *gspca_dev = priv;
1341
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001342 parm->parm.capture.readbuffers = gspca_dev->nbufread;
Jim Paris627a5ef2008-12-10 06:02:42 -03001343
1344 if (gspca_dev->sd_desc->get_streamparm) {
1345 int ret;
1346
1347 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1348 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001349 if (gspca_dev->present)
1350 ret = gspca_dev->sd_desc->get_streamparm(gspca_dev,
1351 parm);
1352 else
1353 ret = -ENODEV;
Jim Paris627a5ef2008-12-10 06:02:42 -03001354 mutex_unlock(&gspca_dev->usb_lock);
1355 return ret;
1356 }
1357
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001358 return 0;
1359}
1360
1361static int vidioc_s_parm(struct file *filp, void *priv,
1362 struct v4l2_streamparm *parm)
1363{
1364 struct gspca_dev *gspca_dev = priv;
1365 int n;
1366
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001367 n = parm->parm.capture.readbuffers;
1368 if (n == 0 || n > GSPCA_MAX_FRAMES)
1369 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1370 else
1371 gspca_dev->nbufread = n;
Jim Paris627a5ef2008-12-10 06:02:42 -03001372
1373 if (gspca_dev->sd_desc->set_streamparm) {
1374 int ret;
1375
1376 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1377 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001378 if (gspca_dev->present)
1379 ret = gspca_dev->sd_desc->set_streamparm(gspca_dev,
1380 parm);
1381 else
1382 ret = -ENODEV;
Jim Paris627a5ef2008-12-10 06:02:42 -03001383 mutex_unlock(&gspca_dev->usb_lock);
1384 return ret;
1385 }
1386
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001387 return 0;
1388}
1389
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001390static int vidioc_s_std(struct file *filp, void *priv,
1391 v4l2_std_id *parm)
1392{
1393 return 0;
1394}
1395
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001396#ifdef CONFIG_VIDEO_V4L1_COMPAT
1397static int vidiocgmbuf(struct file *file, void *priv,
1398 struct video_mbuf *mbuf)
1399{
1400 struct gspca_dev *gspca_dev = file->private_data;
1401 int i;
1402
1403 PDEBUG(D_STREAM, "cgmbuf");
1404 if (gspca_dev->nframes == 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001405 int ret;
1406
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001407 {
1408 struct v4l2_format fmt;
1409
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001410 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1411 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1412 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1413 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1414 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001415 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001416 if (ret != 0)
1417 return ret;
1418 }
1419 {
1420 struct v4l2_requestbuffers rb;
1421
1422 memset(&rb, 0, sizeof rb);
1423 rb.count = 4;
1424 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1425 rb.memory = V4L2_MEMORY_MMAP;
1426 ret = vidioc_reqbufs(file, priv, &rb);
1427 if (ret != 0)
1428 return ret;
1429 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001430 }
1431 mbuf->frames = gspca_dev->nframes;
1432 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1433 for (i = 0; i < mbuf->frames; i++)
1434 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1435 return 0;
1436}
1437#endif
1438
1439static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1440{
1441 struct gspca_dev *gspca_dev = file->private_data;
Harvey Harrisonefab8212008-07-05 06:12:47 -03001442 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001443 struct page *page;
1444 unsigned long addr, start, size;
1445 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001446
1447 start = vma->vm_start;
1448 size = vma->vm_end - vma->vm_start;
1449 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1450
Hans de Goedee2997a72008-04-23 08:09:12 -03001451 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1452 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001453 if (!gspca_dev->present) {
1454 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001455 goto out;
1456 }
1457 if (gspca_dev->capt_file != file) {
1458 ret = -EINVAL;
1459 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001460 }
1461
Harvey Harrisonefab8212008-07-05 06:12:47 -03001462 frame = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001463 for (i = 0; i < gspca_dev->nframes; ++i) {
1464 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1465 PDEBUG(D_STREAM, "mmap bad memory type");
1466 break;
1467 }
1468 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1469 == vma->vm_pgoff) {
1470 frame = &gspca_dev->frame[i];
1471 break;
1472 }
1473 }
Harvey Harrisonefab8212008-07-05 06:12:47 -03001474 if (frame == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001475 PDEBUG(D_STREAM, "mmap no frame buffer found");
1476 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001477 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001478 }
1479#ifdef CONFIG_VIDEO_V4L1_COMPAT
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001480 /* v4l1 maps all the buffers */
1481 if (i != 0
1482 || size != frame->v4l2_buf.length * gspca_dev->nframes)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001483#endif
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001484 if (size != frame->v4l2_buf.length) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001485 PDEBUG(D_STREAM, "mmap bad size");
1486 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001487 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001488 }
1489
1490 /*
1491 * - VM_IO marks the area as being a mmaped region for I/O to a
1492 * device. It also prevents the region from being core dumped.
1493 */
1494 vma->vm_flags |= VM_IO;
1495
1496 addr = (unsigned long) frame->data;
1497 while (size > 0) {
1498 page = vmalloc_to_page((void *) addr);
1499 ret = vm_insert_page(vma, start, page);
1500 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001501 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001502 start += PAGE_SIZE;
1503 addr += PAGE_SIZE;
1504 size -= PAGE_SIZE;
1505 }
1506
1507 vma->vm_ops = &gspca_vm_ops;
1508 vma->vm_private_data = frame;
1509 gspca_vm_open(vma);
Hans de Goedee2997a72008-04-23 08:09:12 -03001510 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001511out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001512 mutex_unlock(&gspca_dev->queue_lock);
1513 return ret;
1514}
1515
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001516/*
1517 * wait for a video frame
1518 *
1519 * If a frame is ready, its index is returned.
1520 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001521static int frame_wait(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001522 int nonblock_ing)
1523{
1524 struct gspca_frame *frame;
1525 int i, j, ret;
1526
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001527 /* check if a frame is ready */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001528 i = gspca_dev->fr_o;
1529 j = gspca_dev->fr_queue[i];
1530 frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001531
Hans de Goede181b7042008-11-19 17:16:26 -03001532 if (!(frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)) {
1533 if (nonblock_ing)
1534 return -EAGAIN;
1535
1536 /* wait till a frame is ready */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001537 ret = wait_event_interruptible_timeout(gspca_dev->wq,
Hans de Goede181b7042008-11-19 17:16:26 -03001538 (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) ||
1539 !gspca_dev->streaming || !gspca_dev->present,
1540 msecs_to_jiffies(3000));
1541 if (ret < 0)
1542 return ret;
1543 if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present)
Hans de Goedee2997a72008-04-23 08:09:12 -03001544 return -EIO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001545 }
Hans de Goede181b7042008-11-19 17:16:26 -03001546
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001547 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1548 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1549 gspca_dev->fr_q,
1550 gspca_dev->fr_i,
1551 gspca_dev->fr_o);
Hans de Goedee2997a72008-04-23 08:09:12 -03001552
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001553 if (gspca_dev->sd_desc->dq_callback) {
1554 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001555 if (gspca_dev->present)
1556 gspca_dev->sd_desc->dq_callback(gspca_dev);
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001557 mutex_unlock(&gspca_dev->usb_lock);
1558 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001559 return j;
1560}
1561
1562/*
1563 * dequeue a video buffer
1564 *
1565 * If nonblock_ing is false, block until a buffer is available.
1566 */
1567static int vidioc_dqbuf(struct file *file, void *priv,
1568 struct v4l2_buffer *v4l2_buf)
1569{
1570 struct gspca_dev *gspca_dev = priv;
1571 struct gspca_frame *frame;
1572 int i, ret;
1573
1574 PDEBUG(D_FRAM, "dqbuf");
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001575 if (v4l2_buf->memory != gspca_dev->memory)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001576 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001577
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001578 if (!gspca_dev->present)
1579 return -ENODEV;
1580
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001581 /* if not streaming, be sure the application will not loop forever */
1582 if (!(file->f_flags & O_NONBLOCK)
1583 && !gspca_dev->streaming && gspca_dev->users == 1)
1584 return -EINVAL;
1585
1586 /* only the capturing file may dequeue */
1587 if (gspca_dev->capt_file != file)
1588 return -EINVAL;
1589
1590 /* only one dequeue / read at a time */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001591 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1592 return -ERESTARTSYS;
1593
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001594 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001595 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001596 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001597 i = ret; /* frame index */
1598 frame = &gspca_dev->frame[i];
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001599 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001600 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001601 frame->data,
1602 frame->v4l2_buf.bytesused)) {
1603 PDEBUG(D_ERR|D_STREAM,
1604 "dqbuf cp to user failed");
1605 ret = -EFAULT;
1606 goto out;
1607 }
1608 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001609 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1610 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1611 PDEBUG(D_FRAM, "dqbuf %d", i);
1612 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001613out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001614 mutex_unlock(&gspca_dev->read_lock);
1615 return ret;
1616}
1617
1618/*
1619 * queue a video buffer
1620 *
1621 * Attempting to queue a buffer that has already been
1622 * queued will return -EINVAL.
1623 */
1624static int vidioc_qbuf(struct file *file, void *priv,
1625 struct v4l2_buffer *v4l2_buf)
1626{
1627 struct gspca_dev *gspca_dev = priv;
1628 struct gspca_frame *frame;
1629 int i, index, ret;
1630
1631 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001632
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001633 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1634 return -ERESTARTSYS;
1635
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001636 index = v4l2_buf->index;
1637 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001638 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001639 "qbuf idx %d >= %d", index, gspca_dev->nframes);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001640 ret = -EINVAL;
1641 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001642 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001643 if (v4l2_buf->memory != gspca_dev->memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001644 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001645 ret = -EINVAL;
1646 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001647 }
1648
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001649 frame = &gspca_dev->frame[index];
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001650 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1651 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001652 ret = -EINVAL;
1653 goto out;
1654 }
1655
1656 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001657
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001658 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001659 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1660 frame->v4l2_buf.length = v4l2_buf->length;
1661 }
1662
1663 /* put the buffer in the 'queued' queue */
1664 i = gspca_dev->fr_q;
1665 gspca_dev->fr_queue[i] = index;
1666 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1667 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1668 gspca_dev->fr_q,
1669 gspca_dev->fr_i,
1670 gspca_dev->fr_o);
1671
1672 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1673 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1674 ret = 0;
1675out:
1676 mutex_unlock(&gspca_dev->queue_lock);
1677 return ret;
1678}
1679
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001680/*
1681 * allocate the resources for read()
1682 */
1683static int read_alloc(struct gspca_dev *gspca_dev,
1684 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001685{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001686 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001687 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001688
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001689 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001690 if (gspca_dev->nframes == 0) {
1691 struct v4l2_requestbuffers rb;
1692
1693 memset(&rb, 0, sizeof rb);
1694 rb.count = gspca_dev->nbufread;
1695 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001696 rb.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001697 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1698 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001699 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001700 return ret;
1701 }
1702 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1703 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001704 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001705 for (i = 0; i < gspca_dev->nbufread; i++) {
1706 v4l2_buf.index = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001707 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1708 if (ret != 0) {
1709 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1710 return ret;
1711 }
1712 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001713 gspca_dev->memory = GSPCA_MEMORY_READ;
1714 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001715
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001716 /* start streaming */
1717 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1718 if (ret != 0)
1719 PDEBUG(D_STREAM, "read streamon err %d", ret);
1720 return ret;
1721}
1722
1723static unsigned int dev_poll(struct file *file, poll_table *wait)
1724{
1725 struct gspca_dev *gspca_dev = file->private_data;
1726 int i, ret;
1727
1728 PDEBUG(D_FRAM, "poll");
1729
1730 poll_wait(file, &gspca_dev->wq, wait);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001731
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001732 /* if reqbufs is not done, the user would use read() */
1733 if (gspca_dev->nframes == 0) {
1734 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1735 return POLLERR; /* not the 1st time */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001736 ret = read_alloc(gspca_dev, file);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001737 if (ret != 0)
1738 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001739 }
1740
1741 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1742 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001743
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001744 /* check the next incoming buffer */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001745 i = gspca_dev->fr_o;
1746 i = gspca_dev->fr_queue[i];
1747 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1748 ret = POLLIN | POLLRDNORM; /* something to read */
1749 else
1750 ret = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001751 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001752 if (!gspca_dev->present)
1753 return POLLHUP;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001754 return ret;
1755}
1756
1757static ssize_t dev_read(struct file *file, char __user *data,
1758 size_t count, loff_t *ppos)
1759{
1760 struct gspca_dev *gspca_dev = file->private_data;
1761 struct gspca_frame *frame;
1762 struct v4l2_buffer v4l2_buf;
1763 struct timeval timestamp;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001764 int n, ret, ret2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001765
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001766 PDEBUG(D_FRAM, "read (%zd)", count);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001767 if (!gspca_dev->present)
1768 return -ENODEV;
1769 switch (gspca_dev->memory) {
1770 case GSPCA_MEMORY_NO: /* first time */
1771 ret = read_alloc(gspca_dev, file);
1772 if (ret != 0)
1773 return ret;
1774 break;
1775 case GSPCA_MEMORY_READ:
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001776 if (gspca_dev->capt_file == file)
1777 break;
1778 /* fall thru */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001779 default:
1780 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001781 }
1782
1783 /* get a frame */
1784 jiffies_to_timeval(get_jiffies_64(), &timestamp);
1785 timestamp.tv_sec--;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001786 n = 2;
1787 for (;;) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001788 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1789 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001790 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001791 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1792 if (ret != 0) {
1793 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1794 return ret;
1795 }
1796
1797 /* if the process slept for more than 1 second,
Frank Zago219423c2008-09-26 07:43:54 -03001798 * get a newer frame */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001799 frame = &gspca_dev->frame[v4l2_buf.index];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001800 if (--n < 0)
1801 break; /* avoid infinite loop */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001802 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1803 break;
1804 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1805 if (ret != 0) {
1806 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1807 return ret;
1808 }
1809 }
1810
1811 /* copy the frame */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001812 if (count > frame->v4l2_buf.bytesused)
1813 count = frame->v4l2_buf.bytesused;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001814 ret = copy_to_user(data, frame->data, count);
1815 if (ret != 0) {
1816 PDEBUG(D_ERR|D_STREAM,
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001817 "read cp to user lack %d / %zd", ret, count);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001818 ret = -EFAULT;
1819 goto out;
1820 }
1821 ret = count;
1822out:
1823 /* in each case, requeue the buffer */
1824 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1825 if (ret2 != 0)
1826 return ret2;
1827 return ret;
1828}
1829
Hans Verkuilbec43662008-12-30 06:58:20 -03001830static struct v4l2_file_operations dev_fops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001831 .owner = THIS_MODULE,
1832 .open = dev_open,
1833 .release = dev_close,
1834 .read = dev_read,
1835 .mmap = dev_mmap,
Hans Verkuilbec43662008-12-30 06:58:20 -03001836 .unlocked_ioctl = video_ioctl2,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001837 .poll = dev_poll,
1838};
1839
Hans Verkuila3998102008-07-21 02:57:38 -03001840static const struct v4l2_ioctl_ops dev_ioctl_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001841 .vidioc_querycap = vidioc_querycap,
1842 .vidioc_dqbuf = vidioc_dqbuf,
1843 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001844 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1845 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1846 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1847 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001848 .vidioc_streamon = vidioc_streamon,
1849 .vidioc_queryctrl = vidioc_queryctrl,
1850 .vidioc_g_ctrl = vidioc_g_ctrl,
1851 .vidioc_s_ctrl = vidioc_s_ctrl,
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03001852 .vidioc_g_audio = vidioc_g_audio,
1853 .vidioc_s_audio = vidioc_s_audio,
1854 .vidioc_enumaudio = vidioc_enumaudio,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001855 .vidioc_querymenu = vidioc_querymenu,
1856 .vidioc_enum_input = vidioc_enum_input,
1857 .vidioc_g_input = vidioc_g_input,
1858 .vidioc_s_input = vidioc_s_input,
1859 .vidioc_reqbufs = vidioc_reqbufs,
1860 .vidioc_querybuf = vidioc_querybuf,
1861 .vidioc_streamoff = vidioc_streamoff,
1862 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1863 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1864 .vidioc_g_parm = vidioc_g_parm,
1865 .vidioc_s_parm = vidioc_s_parm,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001866 .vidioc_s_std = vidioc_s_std,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001867#ifdef CONFIG_VIDEO_V4L1_COMPAT
1868 .vidiocgmbuf = vidiocgmbuf,
1869#endif
1870};
1871
Hans Verkuila3998102008-07-21 02:57:38 -03001872static struct video_device gspca_template = {
1873 .name = "gspca main driver",
Hans Verkuila3998102008-07-21 02:57:38 -03001874 .fops = &dev_fops,
1875 .ioctl_ops = &dev_ioctl_ops,
Hans de Goeded0182302008-11-19 17:10:47 -03001876 .release = gspca_release,
Hans Verkuila3998102008-07-21 02:57:38 -03001877 .minor = -1,
1878};
1879
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001880/*
1881 * probe and create a new gspca device
1882 *
1883 * This function must be called by the sub-driver when it is
1884 * called for probing a new device.
1885 */
1886int gspca_dev_probe(struct usb_interface *intf,
1887 const struct usb_device_id *id,
1888 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001889 int dev_size,
1890 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001891{
1892 struct usb_interface_descriptor *interface;
1893 struct gspca_dev *gspca_dev;
1894 struct usb_device *dev = interface_to_usbdev(intf);
1895 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001896
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001897 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001898
1899 /* we don't handle multi-config cameras */
1900 if (dev->descriptor.bNumConfigurations != 1)
1901 return -ENODEV;
1902 interface = &intf->cur_altsetting->desc;
1903 if (interface->bInterfaceNumber > 0)
1904 return -ENODEV;
1905
1906 /* create the device */
1907 if (dev_size < sizeof *gspca_dev)
1908 dev_size = sizeof *gspca_dev;
1909 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
Erik Andrene293e592008-10-03 08:46:50 -03001910 if (!gspca_dev) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001911 err("couldn't kzalloc gspca struct");
Erik Andrene293e592008-10-03 08:46:50 -03001912 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001913 }
Jean-Francois Moine8295d992008-09-03 17:12:19 -03001914 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
1915 if (!gspca_dev->usb_buf) {
1916 err("out of memory");
Erik Andrene293e592008-10-03 08:46:50 -03001917 ret = -ENOMEM;
Jean-Francois Moine8295d992008-09-03 17:12:19 -03001918 goto out;
1919 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001920 gspca_dev->dev = dev;
1921 gspca_dev->iface = interface->bInterfaceNumber;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001922 gspca_dev->nbalt = intf->num_altsetting;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001923 gspca_dev->sd_desc = sd_desc;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001924 gspca_dev->nbufread = 2;
Jean-Francois Moineff374742008-10-23 07:29:51 -03001925 gspca_dev->empty_packet = -1; /* don't check the empty packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001926
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001927 /* configure the subdriver and initialize the USB device */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03001928 ret = sd_desc->config(gspca_dev, id);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001929 if (ret < 0)
1930 goto out;
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03001931 ret = sd_desc->init(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001932 if (ret < 0)
1933 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001934 ret = gspca_set_alt0(gspca_dev);
1935 if (ret < 0)
1936 goto out;
1937 gspca_set_default_mode(gspca_dev);
1938
1939 mutex_init(&gspca_dev->usb_lock);
1940 mutex_init(&gspca_dev->read_lock);
1941 mutex_init(&gspca_dev->queue_lock);
1942 init_waitqueue_head(&gspca_dev->wq);
1943
1944 /* init video stuff */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03001945 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
1946 gspca_dev->vdev.parent = &dev->dev;
Jean-Francois Moine5c4fa002008-11-18 15:52:31 -03001947 gspca_dev->module = module;
Oliver Neukumee56a4d2008-07-27 14:01:59 -03001948 gspca_dev->present = 1;
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001949 ret = video_register_device(&gspca_dev->vdev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001950 VFL_TYPE_GRABBER,
Jean-Francois Moine8d584a52009-01-13 06:07:59 -03001951 -1);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001952 if (ret < 0) {
1953 err("video_register_device err %d", ret);
1954 goto out;
1955 }
1956
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001957 usb_set_intfdata(intf, gspca_dev);
1958 PDEBUG(D_PROBE, "probe ok");
1959 return 0;
1960out:
Jean-Francois Moinee07a1d82008-11-19 06:37:53 -03001961 kfree(gspca_dev->usb_buf);
1962 kfree(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001963 return ret;
1964}
1965EXPORT_SYMBOL(gspca_dev_probe);
1966
1967/*
1968 * USB disconnection
1969 *
1970 * This function must be called by the sub-driver
1971 * when the device disconnects, after the specific resources are freed.
1972 */
1973void gspca_disconnect(struct usb_interface *intf)
1974{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001975 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001976
Adam Bakerad281272009-02-04 15:33:21 -03001977 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03001978 gspca_dev->present = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001979
1980 if (gspca_dev->streaming) {
1981 destroy_urbs(gspca_dev);
1982 wake_up_interruptible(&gspca_dev->wq);
1983 }
1984
1985 /* the device is freed at exit of this function */
1986 gspca_dev->dev = NULL;
Adam Bakerad281272009-02-04 15:33:21 -03001987 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03001988
Frank Zago6b060ff2008-09-28 08:12:22 -03001989 usb_set_intfdata(intf, NULL);
1990
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001991 /* release the device */
1992 /* (this will call gspca_release() immediatly or on last close) */
1993 video_unregister_device(&gspca_dev->vdev);
Jean-Francois Moine3145b8c2008-10-05 04:22:10 -03001994
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001995 PDEBUG(D_PROBE, "disconnect complete");
1996}
1997EXPORT_SYMBOL(gspca_disconnect);
1998
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001999#ifdef CONFIG_PM
2000int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2001{
2002 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2003
2004 if (!gspca_dev->streaming)
2005 return 0;
2006 gspca_dev->frozen = 1; /* avoid urb error messages */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002007 if (gspca_dev->sd_desc->stopN)
2008 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002009 destroy_urbs(gspca_dev);
2010 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002011 if (gspca_dev->sd_desc->stop0)
2012 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002013 return 0;
2014}
2015EXPORT_SYMBOL(gspca_suspend);
2016
2017int gspca_resume(struct usb_interface *intf)
2018{
2019 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2020
2021 gspca_dev->frozen = 0;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002022 gspca_dev->sd_desc->init(gspca_dev);
2023 if (gspca_dev->streaming)
2024 return gspca_init_transfer(gspca_dev);
Jean-Francois Moine1e89e2d2008-09-03 17:12:15 -03002025 return 0;
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002026}
2027EXPORT_SYMBOL(gspca_resume);
2028#endif
Hans de Goededcef3232008-07-10 10:40:53 -03002029/* -- cam driver utility functions -- */
2030
2031/* auto gain and exposure algorithm based on the knee algorithm described here:
2032 http://ytse.tricolour.net/docs/LowLightOptimization.html
2033
2034 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2035 where changed. */
2036int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2037 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2038{
2039 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2040 const struct ctrl *gain_ctrl = NULL;
2041 const struct ctrl *exposure_ctrl = NULL;
2042 const struct ctrl *autogain_ctrl = NULL;
2043 int retval = 0;
2044
2045 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
2046 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2047 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2048 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2049 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2050 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2051 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2052 }
2053 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2054 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2055 "on cam without (auto)gain/exposure");
2056 return 0;
2057 }
2058
2059 if (gain_ctrl->get(gspca_dev, &gain) ||
2060 exposure_ctrl->get(gspca_dev, &exposure) ||
2061 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2062 return 0;
2063
2064 orig_gain = gain;
2065 orig_exposure = exposure;
2066
2067 /* If we are of a multiple of deadzone, do multiple steps to reach the
2068 desired lumination fast (with the risc of a slight overshoot) */
2069 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2070
Jean-Francois Moine1c44d812008-11-10 05:56:55 -03002071 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
Hans de Goededcef3232008-07-10 10:40:53 -03002072 avg_lum, desired_avg_lum, steps);
2073
2074 for (i = 0; i < steps; i++) {
2075 if (avg_lum > desired_avg_lum) {
2076 if (gain > gain_knee)
2077 gain--;
2078 else if (exposure > exposure_knee)
2079 exposure--;
2080 else if (gain > gain_ctrl->qctrl.default_value)
2081 gain--;
2082 else if (exposure > exposure_ctrl->qctrl.minimum)
2083 exposure--;
2084 else if (gain > gain_ctrl->qctrl.minimum)
2085 gain--;
2086 else
2087 break;
2088 } else {
2089 if (gain < gain_ctrl->qctrl.default_value)
2090 gain++;
2091 else if (exposure < exposure_knee)
2092 exposure++;
2093 else if (gain < gain_knee)
2094 gain++;
2095 else if (exposure < exposure_ctrl->qctrl.maximum)
2096 exposure++;
2097 else if (gain < gain_ctrl->qctrl.maximum)
2098 gain++;
2099 else
2100 break;
2101 }
2102 }
2103
2104 if (gain != orig_gain) {
2105 gain_ctrl->set(gspca_dev, gain);
2106 retval = 1;
2107 }
2108 if (exposure != orig_exposure) {
2109 exposure_ctrl->set(gspca_dev, exposure);
2110 retval = 1;
2111 }
2112
2113 return retval;
2114}
2115EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2116
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002117/* -- module insert / remove -- */
2118static int __init gspca_init(void)
2119{
Jean-Francois Moinebb64e862008-07-19 06:49:28 -03002120 info("main v%d.%d.%d registered",
2121 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2122 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2123 DRIVER_VERSION_NUMBER & 0xff);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002124 return 0;
2125}
2126static void __exit gspca_exit(void)
2127{
2128 info("main deregistered");
2129}
2130
2131module_init(gspca_init);
2132module_exit(gspca_exit);
2133
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03002134#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002135module_param_named(debug, gspca_debug, int, 0644);
2136MODULE_PARM_DESC(debug,
2137 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002138 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
2139 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002140#endif