blob: 5dd2abc17ae60b4d7355b4214414d7b204f81433 [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 Moineac0d6df2008-07-08 06:58:15 -030041#define DEF_NURBS 2 /* default number of URBs */
Jean-Francois Moined43fa322008-06-12 10:58:58 -030042
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030043MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
44MODULE_DESCRIPTION("GSPCA USB Camera Driver");
45MODULE_LICENSE("GPL");
46
Jean-Francois Moineafd46292008-12-31 07:27:42 -030047#define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 5, 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030048
Jean-Francois Moine335b3f82008-07-30 04:53:02 -030049#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030050int gspca_debug = D_ERR | D_PROBE;
51EXPORT_SYMBOL(gspca_debug);
52
53static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
54{
55 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
56 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
57 txt,
58 pixfmt & 0xff,
59 (pixfmt >> 8) & 0xff,
60 (pixfmt >> 16) & 0xff,
61 pixfmt >> 24,
62 w, h);
63 } else {
64 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
65 txt,
66 pixfmt,
67 w, h);
68 }
69}
70#else
71#define PDEBUG_MODE(txt, pixfmt, w, h)
72#endif
73
Jean-Francois Moined43fa322008-06-12 10:58:58 -030074/* specific memory types - !! should different from V4L2_MEMORY_xxx */
75#define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
76#define GSPCA_MEMORY_READ 7
77
Jean-Francois Moined43fa322008-06-12 10:58:58 -030078#define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
Jean-Francois Moined43fa322008-06-12 10:58:58 -030079
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030080/*
81 * VMA operations.
82 */
83static void gspca_vm_open(struct vm_area_struct *vma)
84{
85 struct gspca_frame *frame = vma->vm_private_data;
86
87 frame->vma_use_count++;
88 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
89}
90
91static void gspca_vm_close(struct vm_area_struct *vma)
92{
93 struct gspca_frame *frame = vma->vm_private_data;
94
95 if (--frame->vma_use_count <= 0)
96 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
97}
98
99static struct vm_operations_struct gspca_vm_ops = {
100 .open = gspca_vm_open,
101 .close = gspca_vm_close,
102};
103
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300104/* get the current input frame buffer */
105struct gspca_frame *gspca_get_i_frame(struct gspca_dev *gspca_dev)
106{
107 struct gspca_frame *frame;
108 int i;
109
110 i = gspca_dev->fr_i;
111 i = gspca_dev->fr_queue[i];
112 frame = &gspca_dev->frame[i];
113 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
114 != V4L2_BUF_FLAG_QUEUED)
115 return NULL;
116 return frame;
117}
118EXPORT_SYMBOL(gspca_get_i_frame);
119
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300120/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300121 * fill a video frame from an URB and resubmit
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300122 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300123static void fill_frame(struct gspca_dev *gspca_dev,
124 struct urb *urb)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300125{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300126 struct gspca_frame *frame;
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300127 u8 *data; /* address of data in the iso message */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300128 int i, len, st;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300129 cam_pkt_op pkt_scan;
130
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300131 if (urb->status != 0) {
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300132#ifdef CONFIG_PM
Jean-Francois Moine6a709742008-09-03 16:48:10 -0300133 if (!gspca_dev->frozen)
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300134#endif
Jean-Francois Moine6a709742008-09-03 16:48:10 -0300135 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300136 return; /* disconnection ? */
137 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300138 pkt_scan = gspca_dev->sd_desc->pkt_scan;
139 for (i = 0; i < urb->number_of_packets; i++) {
140
141 /* check the availability of the frame buffer */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300142 frame = gspca_get_i_frame(gspca_dev);
143 if (!frame) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300144 gspca_dev->last_packet_type = DISCARD_PACKET;
145 break;
146 }
147
148 /* check the packet status and length */
149 len = urb->iso_frame_desc[i].actual_length;
Jean-Francois Moineff374742008-10-23 07:29:51 -0300150 if (len == 0) {
151 if (gspca_dev->empty_packet == 0)
152 gspca_dev->empty_packet = 1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300153 continue;
Jean-Francois Moineff374742008-10-23 07:29:51 -0300154 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300155 st = urb->iso_frame_desc[i].status;
156 if (st) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300157 PDEBUG(D_ERR,
158 "ISOC data error: [%d] len=%d, status=%d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300159 i, len, st);
160 gspca_dev->last_packet_type = DISCARD_PACKET;
161 continue;
162 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300163
164 /* let the packet be analyzed by the subdriver */
165 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
166 i, urb->iso_frame_desc[i].offset, len);
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300167 data = (u8 *) urb->transfer_buffer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300168 + urb->iso_frame_desc[i].offset;
169 pkt_scan(gspca_dev, frame, data, len);
170 }
171
172 /* resubmit the URB */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300173 st = usb_submit_urb(urb, GFP_ATOMIC);
174 if (st < 0)
175 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
176}
177
178/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300179 * ISOC message interrupt from the USB device
180 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300181 * Analyse each packet and call the subdriver for copy to the frame buffer.
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300182 */
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300183static void isoc_irq(struct urb *urb)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300184{
185 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
186
Hans de Goede3647fea2008-07-15 05:36:30 -0300187 PDEBUG(D_PACK, "isoc irq");
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300188 if (!gspca_dev->streaming)
189 return;
190 fill_frame(gspca_dev, urb);
191}
192
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300193/*
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300194 * bulk message interrupt from the USB device
195 */
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300196static void bulk_irq(struct urb *urb)
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300197{
198 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
199 struct gspca_frame *frame;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300200 int st;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300201
202 PDEBUG(D_PACK, "bulk irq");
203 if (!gspca_dev->streaming)
204 return;
Jean-Francois Moine880ae242008-11-18 04:19:52 -0300205 switch (urb->status) {
206 case 0:
207 break;
208 case -ECONNRESET:
209 urb->status = 0;
210 break;
211 default:
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300212#ifdef CONFIG_PM
213 if (!gspca_dev->frozen)
214#endif
215 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
216 return; /* disconnection ? */
217 }
218
219 /* check the availability of the frame buffer */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300220 frame = gspca_get_i_frame(gspca_dev);
221 if (!frame) {
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300222 gspca_dev->last_packet_type = DISCARD_PACKET;
223 } else {
224 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
225 gspca_dev->sd_desc->pkt_scan(gspca_dev,
226 frame,
227 urb->transfer_buffer,
228 urb->actual_length);
229 }
Antonio Ospitedff369a2008-11-14 07:53:32 -0300230
231 /* resubmit the URB */
232 if (gspca_dev->cam.bulk_nurbs != 0) {
233 st = usb_submit_urb(urb, GFP_ATOMIC);
234 if (st < 0)
235 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
236 }
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300237}
238
239/*
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300240 * add data to the current frame
241 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300242 * This function is called by the subdrivers at interrupt level.
243 *
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300244 * To build a frame, these ones must add
245 * - one FIRST_PACKET
246 * - 0 or many INTER_PACKETs
247 * - one LAST_PACKET
248 * DISCARD_PACKET invalidates the whole frame.
249 * On LAST_PACKET, a new frame is returned.
250 */
251struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
Erik Andrene293e592008-10-03 08:46:50 -0300252 enum gspca_packet_type packet_type,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300253 struct gspca_frame *frame,
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300254 const __u8 *data,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300255 int len)
256{
257 int i, j;
258
Hans de Goedee2997a72008-04-23 08:09:12 -0300259 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300260
261 /* when start of a new frame, if the current frame buffer
262 * is not queued, discard the whole frame */
263 if (packet_type == FIRST_PACKET) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300264 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
265 != V4L2_BUF_FLAG_QUEUED) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300266 gspca_dev->last_packet_type = DISCARD_PACKET;
267 return frame;
268 }
269 frame->data_end = frame->data;
270 jiffies_to_timeval(get_jiffies_64(),
271 &frame->v4l2_buf.timestamp);
272 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300273 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
Jean-Francois Moine06ca78f2008-07-22 03:45:08 -0300274 if (packet_type == LAST_PACKET)
275 gspca_dev->last_packet_type = packet_type;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300276 return frame;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300277 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300278
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300279 /* append the packet to the frame buffer */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300280 if (len > 0) {
281 if (frame->data_end - frame->data + len
282 > frame->v4l2_buf.length) {
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -0300283 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300284 frame->data_end - frame->data + len,
285 frame->v4l2_buf.length);
286 packet_type = DISCARD_PACKET;
287 } else {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300288 memcpy(frame->data_end, data, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300289 frame->data_end += len;
290 }
291 }
292 gspca_dev->last_packet_type = packet_type;
293
Frank Zago219423c2008-09-26 07:43:54 -0300294 /* if last packet, wake up the application and advance in the queue */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300295 if (packet_type == LAST_PACKET) {
296 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
297 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
298 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300299 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300300 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
301 gspca_dev->fr_i = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300302 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
303 frame->v4l2_buf.bytesused,
304 gspca_dev->fr_q,
305 i,
306 gspca_dev->fr_o);
307 j = gspca_dev->fr_queue[i];
308 frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300309 }
310 return frame;
311}
312EXPORT_SYMBOL(gspca_frame_add);
313
314static int gspca_is_compressed(__u32 format)
315{
316 switch (format) {
317 case V4L2_PIX_FMT_MJPEG:
318 case V4L2_PIX_FMT_JPEG:
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300319 case V4L2_PIX_FMT_SPCA561:
Hans de Goedeab8f12c2008-07-04 18:29:32 -0300320 case V4L2_PIX_FMT_PAC207:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300321 return 1;
322 }
323 return 0;
324}
325
326static void *rvmalloc(unsigned long size)
327{
328 void *mem;
329 unsigned long adr;
330
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300331 mem = vmalloc_32(size);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300332 if (mem != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300333 adr = (unsigned long) mem;
334 while ((long) size > 0) {
335 SetPageReserved(vmalloc_to_page((void *) adr));
336 adr += PAGE_SIZE;
337 size -= PAGE_SIZE;
338 }
339 }
340 return mem;
341}
342
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300343static void rvfree(void *mem, long size)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300344{
345 unsigned long adr;
346
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300347 adr = (unsigned long) mem;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300348 while (size > 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300349 ClearPageReserved(vmalloc_to_page((void *) adr));
350 adr += PAGE_SIZE;
351 size -= PAGE_SIZE;
352 }
353 vfree(mem);
354}
355
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300356static int frame_alloc(struct gspca_dev *gspca_dev,
357 unsigned int count)
358{
359 struct gspca_frame *frame;
360 unsigned int frsz;
361 int i;
362
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300363 i = gspca_dev->curr_mode;
364 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300365 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300366 frsz = PAGE_ALIGN(frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300367 gspca_dev->frsz = frsz;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300368 if (count > GSPCA_MAX_FRAMES)
369 count = GSPCA_MAX_FRAMES;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300370 gspca_dev->frbuf = rvmalloc(frsz * count);
371 if (!gspca_dev->frbuf) {
372 err("frame alloc failed");
373 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300374 }
375 gspca_dev->nframes = count;
376 for (i = 0; i < count; i++) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300377 frame = &gspca_dev->frame[i];
378 frame->v4l2_buf.index = i;
379 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
380 frame->v4l2_buf.flags = 0;
381 frame->v4l2_buf.field = V4L2_FIELD_NONE;
382 frame->v4l2_buf.length = frsz;
383 frame->v4l2_buf.memory = gspca_dev->memory;
384 frame->v4l2_buf.sequence = 0;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300385 frame->data = frame->data_end =
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300386 gspca_dev->frbuf + i * frsz;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300387 frame->v4l2_buf.m.offset = i * frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300388 }
389 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
390 gspca_dev->last_packet_type = DISCARD_PACKET;
391 gspca_dev->sequence = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300392 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300393}
394
395static void frame_free(struct gspca_dev *gspca_dev)
396{
397 int i;
398
399 PDEBUG(D_STREAM, "frame free");
Harvey Harrisonefab8212008-07-05 06:12:47 -0300400 if (gspca_dev->frbuf != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300401 rvfree(gspca_dev->frbuf,
402 gspca_dev->nframes * gspca_dev->frsz);
403 gspca_dev->frbuf = NULL;
404 for (i = 0; i < gspca_dev->nframes; i++)
405 gspca_dev->frame[i].data = NULL;
406 }
407 gspca_dev->nframes = 0;
408}
409
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300410static void destroy_urbs(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300411{
412 struct urb *urb;
413 unsigned int i;
414
415 PDEBUG(D_STREAM, "kill transfer");
Erik Andrén44732812008-10-13 15:52:46 -0300416 for (i = 0; i < MAX_NURBS; i++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300417 urb = gspca_dev->urb[i];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300418 if (urb == NULL)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300419 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300420
Adam Bakerad281272009-02-04 15:33:21 -0300421 BUG_ON(!gspca_dev->dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300422 gspca_dev->urb[i] = NULL;
Jean-Francois Moine2fd9c2e2009-01-24 07:45:14 -0300423 if (!gspca_dev->present)
424 usb_kill_urb(urb);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300425 if (urb->transfer_buffer != NULL)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300426 usb_buffer_free(gspca_dev->dev,
427 urb->transfer_buffer_length,
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300428 urb->transfer_buffer,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300429 urb->transfer_dma);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300430 usb_free_urb(urb);
431 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300432}
433
434/*
Frank Zago219423c2008-09-26 07:43:54 -0300435 * look for an input transfer endpoint in an alternate setting
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300436 */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300437static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300438 __u8 xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300439{
440 struct usb_host_endpoint *ep;
441 int i, attr;
442
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300443 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
444 ep = &alt->endpoint[i];
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300445 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
446 if (attr == xfer)
447 return ep;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300448 }
449 return NULL;
450}
451
452/*
Frank Zago219423c2008-09-26 07:43:54 -0300453 * look for an input (isoc or bulk) endpoint
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300454 *
455 * The endpoint is defined by the subdriver.
456 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
457 * This routine may be called many times when the bandwidth is too small
458 * (the bandwidth is checked on urb submit).
459 */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300460static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300461{
462 struct usb_interface *intf;
463 struct usb_host_endpoint *ep;
464 int i, ret;
465
466 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300467 ep = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300468 i = gspca_dev->alt; /* previous alt setting */
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300469
470 /* try isoc */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300471 while (--i > 0) { /* alt 0 is unusable */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300472 ep = alt_xfer(&intf->altsetting[i],
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300473 USB_ENDPOINT_XFER_ISOC);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300474 if (ep)
475 break;
476 }
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300477
478 /* if no isoc, try bulk */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300479 if (ep == NULL) {
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300480 ep = alt_xfer(&intf->altsetting[0],
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300481 USB_ENDPOINT_XFER_BULK);
482 if (ep == NULL) {
483 err("no transfer endpoint found");
484 return NULL;
485 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300486 }
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300487 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300488 i, ep->desc.bEndpointAddress);
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300489 if (i > 0) {
490 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
491 if (ret < 0) {
492 err("set interface err %d", ret);
493 return NULL;
494 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300495 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300496 gspca_dev->alt = i; /* memorize the current alt setting */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300497 return ep;
498}
499
500/*
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300501 * create the URBs for image transfer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300502 */
503static int create_urbs(struct gspca_dev *gspca_dev,
504 struct usb_host_endpoint *ep)
505{
506 struct urb *urb;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300507 int n, nurbs, i, psize, npkt, bsize;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300508
509 /* calculate the packet size and the number of packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300510 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300511
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300512 if (gspca_dev->alt != 0) { /* isoc */
513
514 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
515 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300516 npkt = ISO_MAX_SIZE / psize;
517 if (npkt > ISO_MAX_PKT)
518 npkt = ISO_MAX_PKT;
519 bsize = psize * npkt;
520 PDEBUG(D_STREAM,
521 "isoc %d pkts size %d = bsize:%d",
522 npkt, psize, bsize);
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300523 nurbs = DEF_NURBS;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300524 } else { /* bulk */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300525 npkt = 0;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300526 bsize = gspca_dev->cam.bulk_size;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300527 if (bsize == 0)
528 bsize = psize;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300529 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300530 if (gspca_dev->cam.bulk_nurbs != 0)
531 nurbs = gspca_dev->cam.bulk_nurbs;
532 else
533 nurbs = 1;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300534 }
535
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300536 gspca_dev->nurbs = nurbs;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300537 for (n = 0; n < nurbs; n++) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300538 urb = usb_alloc_urb(npkt, GFP_KERNEL);
539 if (!urb) {
540 err("usb_alloc_urb failed");
Jean-Francois Moine0d0548e2008-09-05 12:49:54 -0300541 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300542 return -ENOMEM;
543 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300544 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300545 bsize,
546 GFP_KERNEL,
547 &urb->transfer_dma);
548
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300549 if (urb->transfer_buffer == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300550 usb_free_urb(urb);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300551 err("usb_buffer_urb failed");
Jean-Francois Moine0d0548e2008-09-05 12:49:54 -0300552 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300553 return -ENOMEM;
554 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300555 gspca_dev->urb[n] = urb;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300556 urb->dev = gspca_dev->dev;
557 urb->context = gspca_dev;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300558 urb->transfer_buffer_length = bsize;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300559 if (npkt != 0) { /* ISOC */
560 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
561 ep->desc.bEndpointAddress);
Frank Zago6c49da72008-09-28 07:43:00 -0300562 urb->transfer_flags = URB_ISO_ASAP
563 | URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300564 urb->interval = ep->desc.bInterval;
565 urb->complete = isoc_irq;
566 urb->number_of_packets = npkt;
567 for (i = 0; i < npkt; i++) {
568 urb->iso_frame_desc[i].length = psize;
569 urb->iso_frame_desc[i].offset = psize * i;
570 }
571 } else { /* bulk */
572 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
573 ep->desc.bEndpointAddress),
Frank Zago6c49da72008-09-28 07:43:00 -0300574 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300575 urb->complete = bulk_irq;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300576 }
577 }
578 return 0;
579}
580
581/*
582 * start the USB transfer
583 */
584static int gspca_init_transfer(struct gspca_dev *gspca_dev)
585{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300586 struct usb_host_endpoint *ep;
587 int n, ret;
588
Hans de Goedee2997a72008-04-23 08:09:12 -0300589 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
590 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300591
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300592 /* set the higher alternate setting and
593 * loop until urb submit succeeds */
594 gspca_dev->alt = gspca_dev->nbalt;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300595 for (;;) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300596 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300597 ep = get_ep(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300598 if (ep == NULL) {
599 ret = -EIO;
600 goto out;
601 }
602 ret = create_urbs(gspca_dev, ep);
603 if (ret < 0)
604 goto out;
605
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300606 /* clear the bulk endpoint */
607 if (gspca_dev->alt == 0) /* if bulk transfer */
608 usb_clear_halt(gspca_dev->dev,
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300609 gspca_dev->urb[0]->pipe);
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300610
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300611 /* start the cam */
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300612 ret = gspca_dev->sd_desc->start(gspca_dev);
613 if (ret < 0) {
614 destroy_urbs(gspca_dev);
615 goto out;
616 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300617 gspca_dev->streaming = 1;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300618
Antonio Ospitedff369a2008-11-14 07:53:32 -0300619 /* some bulk transfers are started by the subdriver */
620 if (gspca_dev->alt == 0 && gspca_dev->cam.bulk_nurbs == 0)
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300621 break;
622
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300623 /* submit the URBs */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300624 for (n = 0; n < gspca_dev->nurbs; n++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300625 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300626 if (ret < 0) {
627 PDEBUG(D_ERR|D_STREAM,
628 "usb_submit_urb [%d] err %d", n, ret);
Hans de Goedee2997a72008-04-23 08:09:12 -0300629 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300630 destroy_urbs(gspca_dev);
Jean-Francois Moine9edf1a62008-11-23 15:53:28 -0300631 if (ret == -ENOSPC) {
Jean-Francois Moineabf485f2008-11-24 06:38:21 -0300632 msleep(20); /* wait for kill
Jean-Francois Moine9edf1a62008-11-23 15:53:28 -0300633 * complete */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300634 break; /* try the previous alt */
Jean-Francois Moine9edf1a62008-11-23 15:53:28 -0300635 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300636 goto out;
637 }
638 }
639 if (ret >= 0)
640 break;
641 }
642out:
643 mutex_unlock(&gspca_dev->usb_lock);
644 return ret;
645}
646
647static int gspca_set_alt0(struct gspca_dev *gspca_dev)
648{
649 int ret;
650
651 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
652 if (ret < 0)
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -0300653 PDEBUG(D_ERR|D_STREAM, "set alt 0 err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300654 return ret;
655}
656
Frank Zago219423c2008-09-26 07:43:54 -0300657/* Note: both the queue and the usb locks should be held when calling this */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300658static void gspca_stream_off(struct gspca_dev *gspca_dev)
659{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300660 gspca_dev->streaming = 0;
Jean-Francois Moine98522a72008-11-18 06:33:08 -0300661 if (gspca_dev->present
662 && gspca_dev->sd_desc->stopN)
663 gspca_dev->sd_desc->stopN(gspca_dev);
664 destroy_urbs(gspca_dev);
665 gspca_set_alt0(gspca_dev);
666 if (gspca_dev->sd_desc->stop0)
667 gspca_dev->sd_desc->stop0(gspca_dev);
668 PDEBUG(D_STREAM, "stream off OK");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300669}
670
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300671static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300672{
673 int i;
674
675 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
676 gspca_dev->curr_mode = i;
677 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
678 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300679 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300680}
681
682static int wxh_to_mode(struct gspca_dev *gspca_dev,
683 int width, int height)
684{
685 int i;
686
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300687 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
688 if (width >= gspca_dev->cam.cam_mode[i].width
689 && height >= gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300690 break;
691 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300692 return i;
693}
694
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300695/*
696 * search a mode with the right pixel format
697 */
698static int gspca_get_mode(struct gspca_dev *gspca_dev,
699 int mode,
700 int pixfmt)
701{
702 int modeU, modeD;
703
704 modeU = modeD = mode;
705 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
706 if (--modeD >= 0) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300707 if (gspca_dev->cam.cam_mode[modeD].pixelformat
708 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300709 return modeD;
710 }
711 if (++modeU < gspca_dev->cam.nmodes) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300712 if (gspca_dev->cam.cam_mode[modeU].pixelformat
713 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300714 return modeU;
715 }
716 }
717 return -EINVAL;
718}
719
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300720static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300721 struct v4l2_fmtdesc *fmtdesc)
722{
723 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300724 int i, j, index;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300725 __u32 fmt_tb[8];
726
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300727 /* give an index to each format */
728 index = 0;
729 j = 0;
730 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300731 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300732 j = 0;
733 for (;;) {
734 if (fmt_tb[j] == fmt_tb[index])
735 break;
736 j++;
737 }
738 if (j == index) {
739 if (fmtdesc->index == index)
740 break; /* new format */
741 index++;
Julia Lawall80297122008-11-12 23:18:21 -0300742 if (index >= ARRAY_SIZE(fmt_tb))
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300743 return -EINVAL;
744 }
745 }
746 if (i < 0)
747 return -EINVAL; /* no more format */
748
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300749 fmtdesc->pixelformat = fmt_tb[index];
750 if (gspca_is_compressed(fmt_tb[index]))
751 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300752 fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300753 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
754 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
755 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
756 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
757 fmtdesc->description[4] = '\0';
758 return 0;
759}
760
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300761static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300762 struct v4l2_format *fmt)
763{
764 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300765 int mode;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300766
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300767 mode = gspca_dev->curr_mode;
768 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
769 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300770 return 0;
771}
772
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300773static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300774 struct v4l2_format *fmt)
775{
Hans de Goede24c530b2008-07-04 17:55:18 -0300776 int w, h, mode, mode2;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300777
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300778 w = fmt->fmt.pix.width;
779 h = fmt->fmt.pix.height;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300780
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300781#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300782 if (gspca_debug & D_CONF)
783 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
784#endif
785 /* search the closest mode for width and height */
786 mode = wxh_to_mode(gspca_dev, w, h);
787
788 /* OK if right palette */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300789 if (gspca_dev->cam.cam_mode[mode].pixelformat
790 != fmt->fmt.pix.pixelformat) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300791
792 /* else, search the closest mode with the same pixel format */
793 mode2 = gspca_get_mode(gspca_dev, mode,
794 fmt->fmt.pix.pixelformat);
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300795 if (mode2 >= 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300796 mode = mode2;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300797/* else
798 ; * no chance, return this mode */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300799 }
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300800 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
801 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300802 return mode; /* used when s_fmt */
803}
804
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300805static int vidioc_try_fmt_vid_cap(struct file *file,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300806 void *priv,
807 struct v4l2_format *fmt)
808{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300809 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300810 int ret;
811
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300812 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300813 if (ret < 0)
814 return ret;
815 return 0;
816}
817
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300818static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300819 struct v4l2_format *fmt)
820{
821 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300822 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300823
Hans de Goedee2997a72008-04-23 08:09:12 -0300824 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
825 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300826
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 goto out;
830
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300831 if (gspca_dev->nframes != 0
832 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
833 ret = -EINVAL;
834 goto out;
835 }
836
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300837 if (ret == gspca_dev->curr_mode) {
838 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300839 goto out; /* same mode */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300840 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300841
842 if (gspca_dev->streaming) {
843 ret = -EBUSY;
844 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -0300845 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300846 gspca_dev->width = fmt->fmt.pix.width;
847 gspca_dev->height = fmt->fmt.pix.height;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300848 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
849 gspca_dev->curr_mode = ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300850
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300851 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300852out:
853 mutex_unlock(&gspca_dev->queue_lock);
854 return ret;
855}
856
Hans de Goeded0182302008-11-19 17:10:47 -0300857static void gspca_release(struct video_device *vfd)
Frank Zago6b060ff2008-09-28 08:12:22 -0300858{
Hans de Goeded0182302008-11-19 17:10:47 -0300859 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
Frank Zago6b060ff2008-09-28 08:12:22 -0300860
Hans de Goeded0182302008-11-19 17:10:47 -0300861 PDEBUG(D_STREAM, "device released");
Frank Zago6b060ff2008-09-28 08:12:22 -0300862
863 kfree(gspca_dev->usb_buf);
864 kfree(gspca_dev);
865}
866
Hans Verkuilbec43662008-12-30 06:58:20 -0300867static int dev_open(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300868{
869 struct gspca_dev *gspca_dev;
870 int ret;
871
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300872 PDEBUG(D_STREAM, "%s open", current->comm);
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -0300873 gspca_dev = (struct gspca_dev *) video_devdata(file);
Hans de Goedee2997a72008-04-23 08:09:12 -0300874 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
875 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300876 if (!gspca_dev->present) {
877 ret = -ENODEV;
878 goto out;
879 }
880
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300881 if (gspca_dev->users > 4) { /* (arbitrary value) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300882 ret = -EBUSY;
883 goto out;
884 }
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -0300885
886 /* protect the subdriver against rmmod */
887 if (!try_module_get(gspca_dev->module)) {
888 ret = -ENODEV;
889 goto out;
890 }
891
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300892 gspca_dev->users++;
Frank Zago6b060ff2008-09-28 08:12:22 -0300893
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300894 file->private_data = gspca_dev;
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300895#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300896 /* activate the v4l2 debug */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300897 if (gspca_debug & D_V4L2)
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -0300898 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -0300899 | V4L2_DEBUG_IOCTL_ARG;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300900 else
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -0300901 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -0300902 | V4L2_DEBUG_IOCTL_ARG);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300903#endif
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300904 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300905out:
906 mutex_unlock(&gspca_dev->queue_lock);
907 if (ret != 0)
908 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
909 else
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300910 PDEBUG(D_STREAM, "open done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300911 return ret;
912}
913
Hans Verkuilbec43662008-12-30 06:58:20 -0300914static int dev_close(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300915{
916 struct gspca_dev *gspca_dev = file->private_data;
917
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300918 PDEBUG(D_STREAM, "%s close", current->comm);
Hans de Goedee2997a72008-04-23 08:09:12 -0300919 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
920 return -ERESTARTSYS;
921 gspca_dev->users--;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300922
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -0300923 /* if the file did the capture, free the streaming resources */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300924 if (gspca_dev->capt_file == file) {
Jean-Francois Moinea1e82882008-09-03 17:12:17 -0300925 if (gspca_dev->streaming) {
926 mutex_lock(&gspca_dev->usb_lock);
927 gspca_stream_off(gspca_dev);
928 mutex_unlock(&gspca_dev->usb_lock);
929 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300930 frame_free(gspca_dev);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300931 gspca_dev->capt_file = NULL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300932 gspca_dev->memory = GSPCA_MEMORY_NO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300933 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300934 file->private_data = NULL;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -0300935 module_put(gspca_dev->module);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300936 mutex_unlock(&gspca_dev->queue_lock);
Frank Zago6b060ff2008-09-28 08:12:22 -0300937
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300938 PDEBUG(D_STREAM, "close done");
Frank Zago6b060ff2008-09-28 08:12:22 -0300939
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300940 return 0;
941}
942
943static int vidioc_querycap(struct file *file, void *priv,
944 struct v4l2_capability *cap)
945{
946 struct gspca_dev *gspca_dev = priv;
947
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300948 memset(cap, 0, sizeof *cap);
949 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -0300950 if (gspca_dev->dev->product != NULL) {
951 strncpy(cap->card, gspca_dev->dev->product,
952 sizeof cap->card);
953 } else {
954 snprintf(cap->card, sizeof cap->card,
955 "USB Camera (%04x:%04x)",
956 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
957 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
958 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300959 strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name,
960 sizeof cap->bus_info);
961 cap->version = DRIVER_VERSION_NUMBER;
962 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
963 | V4L2_CAP_STREAMING
964 | V4L2_CAP_READWRITE;
965 return 0;
966}
967
968static int vidioc_queryctrl(struct file *file, void *priv,
969 struct v4l2_queryctrl *q_ctrl)
970{
971 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300972 int i, ix;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300973 u32 id;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300974
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300975 ix = -1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300976 id = q_ctrl->id;
977 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
978 id &= V4L2_CTRL_ID_MASK;
979 id++;
980 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goede0f523c22008-09-03 17:12:14 -0300981 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300982 continue;
983 if (ix < 0) {
984 ix = i;
985 continue;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300986 }
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300987 if (gspca_dev->sd_desc->ctrls[i].qctrl.id
988 > gspca_dev->sd_desc->ctrls[ix].qctrl.id)
989 continue;
990 ix = i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300991 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300992 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300993 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300994 if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) {
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300995 ix = i;
996 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300997 }
998 }
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300999 if (ix < 0)
1000 return -EINVAL;
1001 memcpy(q_ctrl, &gspca_dev->sd_desc->ctrls[ix].qctrl,
1002 sizeof *q_ctrl);
1003 if (gspca_dev->ctrl_dis & (1 << ix))
1004 q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
1005 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001006}
1007
1008static int vidioc_s_ctrl(struct file *file, void *priv,
1009 struct v4l2_control *ctrl)
1010{
1011 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001012 const struct ctrl *ctrls;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001013 int i, ret;
1014
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001015 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1016 i < gspca_dev->sd_desc->nctrls;
1017 i++, ctrls++) {
1018 if (ctrl->id != ctrls->qctrl.id)
1019 continue;
Hans de Goede0f523c22008-09-03 17:12:14 -03001020 if (gspca_dev->ctrl_dis & (1 << i))
1021 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001022 if (ctrl->value < ctrls->qctrl.minimum
Rabin Vincentfcf5cb22008-08-03 07:58:54 -03001023 || ctrl->value > ctrls->qctrl.maximum)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001024 return -ERANGE;
1025 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
Hans de Goedee2997a72008-04-23 08:09:12 -03001026 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1027 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001028 ret = ctrls->set(gspca_dev, ctrl->value);
1029 mutex_unlock(&gspca_dev->usb_lock);
1030 return ret;
1031 }
1032 return -EINVAL;
1033}
1034
1035static int vidioc_g_ctrl(struct file *file, void *priv,
1036 struct v4l2_control *ctrl)
1037{
1038 struct gspca_dev *gspca_dev = priv;
1039
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001040 const struct ctrl *ctrls;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001041 int i, ret;
1042
1043 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1044 i < gspca_dev->sd_desc->nctrls;
1045 i++, ctrls++) {
1046 if (ctrl->id != ctrls->qctrl.id)
1047 continue;
Hans de Goede0f523c22008-09-03 17:12:14 -03001048 if (gspca_dev->ctrl_dis & (1 << i))
1049 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001050 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1051 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001052 ret = ctrls->get(gspca_dev, &ctrl->value);
1053 mutex_unlock(&gspca_dev->usb_lock);
1054 return ret;
1055 }
1056 return -EINVAL;
1057}
1058
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03001059/*fixme: have an audio flag in gspca_dev?*/
1060static int vidioc_s_audio(struct file *file, void *priv,
1061 struct v4l2_audio *audio)
1062{
1063 if (audio->index != 0)
1064 return -EINVAL;
1065 return 0;
1066}
1067
1068static int vidioc_g_audio(struct file *file, void *priv,
1069 struct v4l2_audio *audio)
1070{
1071 memset(audio, 0, sizeof *audio);
1072 strcpy(audio->name, "Microphone");
1073 return 0;
1074}
1075
1076static int vidioc_enumaudio(struct file *file, void *priv,
1077 struct v4l2_audio *audio)
1078{
1079 if (audio->index != 0)
1080 return -EINVAL;
1081
1082 strcpy(audio->name, "Microphone");
1083 audio->capability = 0;
1084 audio->mode = 0;
1085 return 0;
1086}
1087
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001088static int vidioc_querymenu(struct file *file, void *priv,
1089 struct v4l2_querymenu *qmenu)
1090{
1091 struct gspca_dev *gspca_dev = priv;
1092
1093 if (!gspca_dev->sd_desc->querymenu)
1094 return -EINVAL;
1095 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1096}
1097
1098static int vidioc_enum_input(struct file *file, void *priv,
1099 struct v4l2_input *input)
1100{
1101 struct gspca_dev *gspca_dev = priv;
1102
1103 if (input->index != 0)
1104 return -EINVAL;
1105 memset(input, 0, sizeof *input);
1106 input->type = V4L2_INPUT_TYPE_CAMERA;
1107 strncpy(input->name, gspca_dev->sd_desc->name,
1108 sizeof input->name);
1109 return 0;
1110}
1111
1112static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1113{
1114 *i = 0;
1115 return 0;
1116}
1117
1118static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1119{
1120 if (i > 0)
1121 return -EINVAL;
1122 return (0);
1123}
1124
1125static int vidioc_reqbufs(struct file *file, void *priv,
1126 struct v4l2_requestbuffers *rb)
1127{
1128 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001129 int i, ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001130
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001131 switch (rb->memory) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001132 case GSPCA_MEMORY_READ: /* (internal call) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001133 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001134 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001135 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001136 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001137 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001138 }
Hans de Goedee2997a72008-04-23 08:09:12 -03001139 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1140 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001141
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001142 if (gspca_dev->memory != GSPCA_MEMORY_NO
1143 && gspca_dev->memory != rb->memory) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001144 ret = -EBUSY;
1145 goto out;
1146 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001147
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001148 /* only one file may do the capture */
1149 if (gspca_dev->capt_file != NULL
1150 && gspca_dev->capt_file != file) {
1151 ret = -EBUSY;
1152 goto out;
1153 }
1154
1155 /* if allocated, the buffers must not be mapped */
1156 for (i = 0; i < gspca_dev->nframes; i++) {
1157 if (gspca_dev->frame[i].vma_use_count) {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001158 ret = -EBUSY;
1159 goto out;
1160 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001161 }
1162
1163 /* stop streaming */
1164 if (gspca_dev->streaming) {
1165 mutex_lock(&gspca_dev->usb_lock);
1166 gspca_stream_off(gspca_dev);
1167 mutex_unlock(&gspca_dev->usb_lock);
1168 }
1169
1170 /* free the previous allocated buffers, if any */
1171 if (gspca_dev->nframes != 0) {
1172 frame_free(gspca_dev);
1173 gspca_dev->capt_file = NULL;
1174 }
1175 if (rb->count == 0) /* unrequest */
1176 goto out;
1177 gspca_dev->memory = rb->memory;
1178 ret = frame_alloc(gspca_dev, rb->count);
1179 if (ret == 0) {
1180 rb->count = gspca_dev->nframes;
1181 gspca_dev->capt_file = file;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001182 }
1183out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001184 mutex_unlock(&gspca_dev->queue_lock);
1185 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1186 return ret;
1187}
1188
1189static int vidioc_querybuf(struct file *file, void *priv,
1190 struct v4l2_buffer *v4l2_buf)
1191{
1192 struct gspca_dev *gspca_dev = priv;
1193 struct gspca_frame *frame;
1194
Jean-Francois Moine13752bd2008-12-19 15:08:51 -03001195 if (v4l2_buf->index < 0
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001196 || v4l2_buf->index >= gspca_dev->nframes)
1197 return -EINVAL;
1198
1199 frame = &gspca_dev->frame[v4l2_buf->index];
1200 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1201 return 0;
1202}
1203
1204static int vidioc_streamon(struct file *file, void *priv,
1205 enum v4l2_buf_type buf_type)
1206{
1207 struct gspca_dev *gspca_dev = priv;
1208 int ret;
1209
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001210 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1211 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001212 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1213 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001214 if (!gspca_dev->present) {
1215 ret = -ENODEV;
1216 goto out;
1217 }
Jean-Francois Moine1edabe72008-12-19 15:17:25 -03001218 if (gspca_dev->nframes == 0
1219 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001220 ret = -EINVAL;
1221 goto out;
1222 }
1223 if (!gspca_dev->streaming) {
1224 ret = gspca_init_transfer(gspca_dev);
1225 if (ret < 0)
1226 goto out;
1227 }
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001228#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001229 if (gspca_debug & D_STREAM) {
1230 PDEBUG_MODE("stream on OK",
1231 gspca_dev->pixfmt,
1232 gspca_dev->width,
1233 gspca_dev->height);
1234 }
1235#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001236 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001237out:
1238 mutex_unlock(&gspca_dev->queue_lock);
1239 return ret;
1240}
1241
1242static int vidioc_streamoff(struct file *file, void *priv,
1243 enum v4l2_buf_type buf_type)
1244{
1245 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001246 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001247
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001248 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1249 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001250 if (!gspca_dev->streaming)
1251 return 0;
1252 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1253 return -ERESTARTSYS;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001254
1255 /* stop streaming */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001256 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1257 ret = -ERESTARTSYS;
1258 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001259 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001260 gspca_stream_off(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001261 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001262
1263 /* empty the application queues */
1264 for (i = 0; i < gspca_dev->nframes; i++)
1265 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1266 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1267 gspca_dev->last_packet_type = DISCARD_PACKET;
1268 gspca_dev->sequence = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001269 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001270out:
1271 mutex_unlock(&gspca_dev->queue_lock);
1272 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001273}
1274
1275static int vidioc_g_jpegcomp(struct file *file, void *priv,
1276 struct v4l2_jpegcompression *jpegcomp)
1277{
1278 struct gspca_dev *gspca_dev = priv;
1279 int ret;
1280
1281 if (!gspca_dev->sd_desc->get_jcomp)
1282 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001283 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1284 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001285 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1286 mutex_unlock(&gspca_dev->usb_lock);
1287 return ret;
1288}
1289
1290static int vidioc_s_jpegcomp(struct file *file, void *priv,
1291 struct v4l2_jpegcompression *jpegcomp)
1292{
1293 struct gspca_dev *gspca_dev = priv;
1294 int ret;
1295
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001296 if (!gspca_dev->sd_desc->set_jcomp)
1297 return -EINVAL;
Jim Parisf86d4a92008-12-10 05:50:44 -03001298 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1299 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001300 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1301 mutex_unlock(&gspca_dev->usb_lock);
1302 return ret;
1303}
1304
1305static int vidioc_g_parm(struct file *filp, void *priv,
1306 struct v4l2_streamparm *parm)
1307{
1308 struct gspca_dev *gspca_dev = priv;
1309
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001310 memset(parm, 0, sizeof *parm);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001311 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1312 parm->parm.capture.readbuffers = gspca_dev->nbufread;
Jim Paris627a5ef2008-12-10 06:02:42 -03001313
1314 if (gspca_dev->sd_desc->get_streamparm) {
1315 int ret;
1316
1317 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1318 return -ERESTARTSYS;
1319 ret = gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
1320 mutex_unlock(&gspca_dev->usb_lock);
1321 return ret;
1322 }
1323
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001324 return 0;
1325}
1326
1327static int vidioc_s_parm(struct file *filp, void *priv,
1328 struct v4l2_streamparm *parm)
1329{
1330 struct gspca_dev *gspca_dev = priv;
1331 int n;
1332
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001333 n = parm->parm.capture.readbuffers;
1334 if (n == 0 || n > GSPCA_MAX_FRAMES)
1335 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1336 else
1337 gspca_dev->nbufread = n;
Jim Paris627a5ef2008-12-10 06:02:42 -03001338
1339 if (gspca_dev->sd_desc->set_streamparm) {
1340 int ret;
1341
1342 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1343 return -ERESTARTSYS;
1344 ret = gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
1345 mutex_unlock(&gspca_dev->usb_lock);
1346 return ret;
1347 }
1348
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001349 return 0;
1350}
1351
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001352static int vidioc_s_std(struct file *filp, void *priv,
1353 v4l2_std_id *parm)
1354{
1355 return 0;
1356}
1357
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001358#ifdef CONFIG_VIDEO_V4L1_COMPAT
1359static int vidiocgmbuf(struct file *file, void *priv,
1360 struct video_mbuf *mbuf)
1361{
1362 struct gspca_dev *gspca_dev = file->private_data;
1363 int i;
1364
1365 PDEBUG(D_STREAM, "cgmbuf");
1366 if (gspca_dev->nframes == 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001367 int ret;
1368
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001369 {
1370 struct v4l2_format fmt;
1371
1372 memset(&fmt, 0, sizeof fmt);
1373 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1374 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1375 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1376 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1377 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001378 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001379 if (ret != 0)
1380 return ret;
1381 }
1382 {
1383 struct v4l2_requestbuffers rb;
1384
1385 memset(&rb, 0, sizeof rb);
1386 rb.count = 4;
1387 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1388 rb.memory = V4L2_MEMORY_MMAP;
1389 ret = vidioc_reqbufs(file, priv, &rb);
1390 if (ret != 0)
1391 return ret;
1392 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001393 }
1394 mbuf->frames = gspca_dev->nframes;
1395 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1396 for (i = 0; i < mbuf->frames; i++)
1397 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1398 return 0;
1399}
1400#endif
1401
1402static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1403{
1404 struct gspca_dev *gspca_dev = file->private_data;
Harvey Harrisonefab8212008-07-05 06:12:47 -03001405 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001406 struct page *page;
1407 unsigned long addr, start, size;
1408 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001409
1410 start = vma->vm_start;
1411 size = vma->vm_end - vma->vm_start;
1412 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1413
Hans de Goedee2997a72008-04-23 08:09:12 -03001414 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1415 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001416 if (!gspca_dev->present) {
1417 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001418 goto out;
1419 }
1420 if (gspca_dev->capt_file != file) {
1421 ret = -EINVAL;
1422 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001423 }
1424
Harvey Harrisonefab8212008-07-05 06:12:47 -03001425 frame = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001426 for (i = 0; i < gspca_dev->nframes; ++i) {
1427 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1428 PDEBUG(D_STREAM, "mmap bad memory type");
1429 break;
1430 }
1431 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1432 == vma->vm_pgoff) {
1433 frame = &gspca_dev->frame[i];
1434 break;
1435 }
1436 }
Harvey Harrisonefab8212008-07-05 06:12:47 -03001437 if (frame == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001438 PDEBUG(D_STREAM, "mmap no frame buffer found");
1439 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001440 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001441 }
1442#ifdef CONFIG_VIDEO_V4L1_COMPAT
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001443 /* v4l1 maps all the buffers */
1444 if (i != 0
1445 || size != frame->v4l2_buf.length * gspca_dev->nframes)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001446#endif
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001447 if (size != frame->v4l2_buf.length) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001448 PDEBUG(D_STREAM, "mmap bad size");
1449 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001450 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001451 }
1452
1453 /*
1454 * - VM_IO marks the area as being a mmaped region for I/O to a
1455 * device. It also prevents the region from being core dumped.
1456 */
1457 vma->vm_flags |= VM_IO;
1458
1459 addr = (unsigned long) frame->data;
1460 while (size > 0) {
1461 page = vmalloc_to_page((void *) addr);
1462 ret = vm_insert_page(vma, start, page);
1463 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001464 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001465 start += PAGE_SIZE;
1466 addr += PAGE_SIZE;
1467 size -= PAGE_SIZE;
1468 }
1469
1470 vma->vm_ops = &gspca_vm_ops;
1471 vma->vm_private_data = frame;
1472 gspca_vm_open(vma);
Hans de Goedee2997a72008-04-23 08:09:12 -03001473 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001474out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001475 mutex_unlock(&gspca_dev->queue_lock);
1476 return ret;
1477}
1478
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001479/*
1480 * wait for a video frame
1481 *
1482 * If a frame is ready, its index is returned.
1483 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001484static int frame_wait(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001485 int nonblock_ing)
1486{
1487 struct gspca_frame *frame;
1488 int i, j, ret;
1489
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001490 /* check if a frame is ready */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001491 i = gspca_dev->fr_o;
1492 j = gspca_dev->fr_queue[i];
1493 frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001494
Hans de Goede181b7042008-11-19 17:16:26 -03001495 if (!(frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)) {
1496 if (nonblock_ing)
1497 return -EAGAIN;
1498
1499 /* wait till a frame is ready */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001500 ret = wait_event_interruptible_timeout(gspca_dev->wq,
Hans de Goede181b7042008-11-19 17:16:26 -03001501 (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) ||
1502 !gspca_dev->streaming || !gspca_dev->present,
1503 msecs_to_jiffies(3000));
1504 if (ret < 0)
1505 return ret;
1506 if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present)
Hans de Goedee2997a72008-04-23 08:09:12 -03001507 return -EIO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001508 }
Hans de Goede181b7042008-11-19 17:16:26 -03001509
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001510 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1511 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1512 gspca_dev->fr_q,
1513 gspca_dev->fr_i,
1514 gspca_dev->fr_o);
Hans de Goedee2997a72008-04-23 08:09:12 -03001515
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001516 if (gspca_dev->sd_desc->dq_callback) {
1517 mutex_lock(&gspca_dev->usb_lock);
Hans de Goedee2997a72008-04-23 08:09:12 -03001518 gspca_dev->sd_desc->dq_callback(gspca_dev);
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001519 mutex_unlock(&gspca_dev->usb_lock);
1520 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001521 return j;
1522}
1523
1524/*
1525 * dequeue a video buffer
1526 *
1527 * If nonblock_ing is false, block until a buffer is available.
1528 */
1529static int vidioc_dqbuf(struct file *file, void *priv,
1530 struct v4l2_buffer *v4l2_buf)
1531{
1532 struct gspca_dev *gspca_dev = priv;
1533 struct gspca_frame *frame;
1534 int i, ret;
1535
1536 PDEBUG(D_FRAM, "dqbuf");
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001537 if (v4l2_buf->memory != gspca_dev->memory)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001538 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001539
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001540 /* if not streaming, be sure the application will not loop forever */
1541 if (!(file->f_flags & O_NONBLOCK)
1542 && !gspca_dev->streaming && gspca_dev->users == 1)
1543 return -EINVAL;
1544
1545 /* only the capturing file may dequeue */
1546 if (gspca_dev->capt_file != file)
1547 return -EINVAL;
1548
1549 /* only one dequeue / read at a time */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001550 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1551 return -ERESTARTSYS;
1552
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001553 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001554 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001555 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001556 i = ret; /* frame index */
1557 frame = &gspca_dev->frame[i];
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001558 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001559 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001560 frame->data,
1561 frame->v4l2_buf.bytesused)) {
1562 PDEBUG(D_ERR|D_STREAM,
1563 "dqbuf cp to user failed");
1564 ret = -EFAULT;
1565 goto out;
1566 }
1567 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001568 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1569 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1570 PDEBUG(D_FRAM, "dqbuf %d", i);
1571 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001572out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001573 mutex_unlock(&gspca_dev->read_lock);
1574 return ret;
1575}
1576
1577/*
1578 * queue a video buffer
1579 *
1580 * Attempting to queue a buffer that has already been
1581 * queued will return -EINVAL.
1582 */
1583static int vidioc_qbuf(struct file *file, void *priv,
1584 struct v4l2_buffer *v4l2_buf)
1585{
1586 struct gspca_dev *gspca_dev = priv;
1587 struct gspca_frame *frame;
1588 int i, index, ret;
1589
1590 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001591
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001592 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1593 return -ERESTARTSYS;
1594
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001595 index = v4l2_buf->index;
1596 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001597 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001598 "qbuf idx %d >= %d", index, gspca_dev->nframes);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001599 ret = -EINVAL;
1600 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001601 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001602 if (v4l2_buf->memory != gspca_dev->memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001603 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001604 ret = -EINVAL;
1605 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001606 }
1607
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001608 frame = &gspca_dev->frame[index];
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001609 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1610 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001611 ret = -EINVAL;
1612 goto out;
1613 }
1614
1615 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001616
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001617 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001618 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1619 frame->v4l2_buf.length = v4l2_buf->length;
1620 }
1621
1622 /* put the buffer in the 'queued' queue */
1623 i = gspca_dev->fr_q;
1624 gspca_dev->fr_queue[i] = index;
1625 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1626 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1627 gspca_dev->fr_q,
1628 gspca_dev->fr_i,
1629 gspca_dev->fr_o);
1630
1631 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1632 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1633 ret = 0;
1634out:
1635 mutex_unlock(&gspca_dev->queue_lock);
1636 return ret;
1637}
1638
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001639/*
1640 * allocate the resources for read()
1641 */
1642static int read_alloc(struct gspca_dev *gspca_dev,
1643 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001644{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001645 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001646 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001647
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001648 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001649 if (gspca_dev->nframes == 0) {
1650 struct v4l2_requestbuffers rb;
1651
1652 memset(&rb, 0, sizeof rb);
1653 rb.count = gspca_dev->nbufread;
1654 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001655 rb.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001656 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1657 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001658 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001659 return ret;
1660 }
1661 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1662 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001663 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001664 for (i = 0; i < gspca_dev->nbufread; i++) {
1665 v4l2_buf.index = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001666 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1667 if (ret != 0) {
1668 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1669 return ret;
1670 }
1671 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001672 gspca_dev->memory = GSPCA_MEMORY_READ;
1673 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001674
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001675 /* start streaming */
1676 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1677 if (ret != 0)
1678 PDEBUG(D_STREAM, "read streamon err %d", ret);
1679 return ret;
1680}
1681
1682static unsigned int dev_poll(struct file *file, poll_table *wait)
1683{
1684 struct gspca_dev *gspca_dev = file->private_data;
1685 int i, ret;
1686
1687 PDEBUG(D_FRAM, "poll");
1688
1689 poll_wait(file, &gspca_dev->wq, wait);
1690 if (!gspca_dev->present)
1691 return POLLERR;
1692
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001693 /* if reqbufs is not done, the user would use read() */
1694 if (gspca_dev->nframes == 0) {
1695 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1696 return POLLERR; /* not the 1st time */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001697 ret = read_alloc(gspca_dev, file);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001698 if (ret != 0)
1699 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001700 }
1701
1702 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1703 return POLLERR;
1704 if (!gspca_dev->present) {
1705 ret = POLLERR;
1706 goto out;
1707 }
1708
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001709 /* check the next incoming buffer */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001710 i = gspca_dev->fr_o;
1711 i = gspca_dev->fr_queue[i];
1712 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1713 ret = POLLIN | POLLRDNORM; /* something to read */
1714 else
1715 ret = 0;
1716out:
1717 mutex_unlock(&gspca_dev->queue_lock);
1718 return ret;
1719}
1720
1721static ssize_t dev_read(struct file *file, char __user *data,
1722 size_t count, loff_t *ppos)
1723{
1724 struct gspca_dev *gspca_dev = file->private_data;
1725 struct gspca_frame *frame;
1726 struct v4l2_buffer v4l2_buf;
1727 struct timeval timestamp;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001728 int n, ret, ret2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001729
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001730 PDEBUG(D_FRAM, "read (%zd)", count);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001731 if (!gspca_dev->present)
1732 return -ENODEV;
1733 switch (gspca_dev->memory) {
1734 case GSPCA_MEMORY_NO: /* first time */
1735 ret = read_alloc(gspca_dev, file);
1736 if (ret != 0)
1737 return ret;
1738 break;
1739 case GSPCA_MEMORY_READ:
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001740 if (gspca_dev->capt_file == file)
1741 break;
1742 /* fall thru */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001743 default:
1744 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001745 }
1746
1747 /* get a frame */
1748 jiffies_to_timeval(get_jiffies_64(), &timestamp);
1749 timestamp.tv_sec--;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001750 n = 2;
1751 for (;;) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001752 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1753 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001754 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001755 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1756 if (ret != 0) {
1757 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1758 return ret;
1759 }
1760
1761 /* if the process slept for more than 1 second,
Frank Zago219423c2008-09-26 07:43:54 -03001762 * get a newer frame */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001763 frame = &gspca_dev->frame[v4l2_buf.index];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001764 if (--n < 0)
1765 break; /* avoid infinite loop */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001766 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1767 break;
1768 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1769 if (ret != 0) {
1770 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1771 return ret;
1772 }
1773 }
1774
1775 /* copy the frame */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001776 if (count > frame->v4l2_buf.bytesused)
1777 count = frame->v4l2_buf.bytesused;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001778 ret = copy_to_user(data, frame->data, count);
1779 if (ret != 0) {
1780 PDEBUG(D_ERR|D_STREAM,
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001781 "read cp to user lack %d / %zd", ret, count);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001782 ret = -EFAULT;
1783 goto out;
1784 }
1785 ret = count;
1786out:
1787 /* in each case, requeue the buffer */
1788 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1789 if (ret2 != 0)
1790 return ret2;
1791 return ret;
1792}
1793
Hans Verkuilbec43662008-12-30 06:58:20 -03001794static struct v4l2_file_operations dev_fops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001795 .owner = THIS_MODULE,
1796 .open = dev_open,
1797 .release = dev_close,
1798 .read = dev_read,
1799 .mmap = dev_mmap,
Hans Verkuilbec43662008-12-30 06:58:20 -03001800 .unlocked_ioctl = video_ioctl2,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001801 .poll = dev_poll,
1802};
1803
Hans Verkuila3998102008-07-21 02:57:38 -03001804static const struct v4l2_ioctl_ops dev_ioctl_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001805 .vidioc_querycap = vidioc_querycap,
1806 .vidioc_dqbuf = vidioc_dqbuf,
1807 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001808 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1809 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1810 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1811 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001812 .vidioc_streamon = vidioc_streamon,
1813 .vidioc_queryctrl = vidioc_queryctrl,
1814 .vidioc_g_ctrl = vidioc_g_ctrl,
1815 .vidioc_s_ctrl = vidioc_s_ctrl,
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03001816 .vidioc_g_audio = vidioc_g_audio,
1817 .vidioc_s_audio = vidioc_s_audio,
1818 .vidioc_enumaudio = vidioc_enumaudio,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001819 .vidioc_querymenu = vidioc_querymenu,
1820 .vidioc_enum_input = vidioc_enum_input,
1821 .vidioc_g_input = vidioc_g_input,
1822 .vidioc_s_input = vidioc_s_input,
1823 .vidioc_reqbufs = vidioc_reqbufs,
1824 .vidioc_querybuf = vidioc_querybuf,
1825 .vidioc_streamoff = vidioc_streamoff,
1826 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1827 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1828 .vidioc_g_parm = vidioc_g_parm,
1829 .vidioc_s_parm = vidioc_s_parm,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001830 .vidioc_s_std = vidioc_s_std,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001831#ifdef CONFIG_VIDEO_V4L1_COMPAT
1832 .vidiocgmbuf = vidiocgmbuf,
1833#endif
1834};
1835
Hans Verkuila3998102008-07-21 02:57:38 -03001836static struct video_device gspca_template = {
1837 .name = "gspca main driver",
Hans Verkuila3998102008-07-21 02:57:38 -03001838 .fops = &dev_fops,
1839 .ioctl_ops = &dev_ioctl_ops,
Hans de Goeded0182302008-11-19 17:10:47 -03001840 .release = gspca_release,
Hans Verkuila3998102008-07-21 02:57:38 -03001841 .minor = -1,
1842};
1843
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001844/*
1845 * probe and create a new gspca device
1846 *
1847 * This function must be called by the sub-driver when it is
1848 * called for probing a new device.
1849 */
1850int gspca_dev_probe(struct usb_interface *intf,
1851 const struct usb_device_id *id,
1852 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001853 int dev_size,
1854 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001855{
1856 struct usb_interface_descriptor *interface;
1857 struct gspca_dev *gspca_dev;
1858 struct usb_device *dev = interface_to_usbdev(intf);
1859 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001860
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001861 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001862
1863 /* we don't handle multi-config cameras */
1864 if (dev->descriptor.bNumConfigurations != 1)
1865 return -ENODEV;
1866 interface = &intf->cur_altsetting->desc;
1867 if (interface->bInterfaceNumber > 0)
1868 return -ENODEV;
1869
1870 /* create the device */
1871 if (dev_size < sizeof *gspca_dev)
1872 dev_size = sizeof *gspca_dev;
1873 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
Erik Andrene293e592008-10-03 08:46:50 -03001874 if (!gspca_dev) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001875 err("couldn't kzalloc gspca struct");
Erik Andrene293e592008-10-03 08:46:50 -03001876 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001877 }
Jean-Francois Moine8295d992008-09-03 17:12:19 -03001878 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
1879 if (!gspca_dev->usb_buf) {
1880 err("out of memory");
Erik Andrene293e592008-10-03 08:46:50 -03001881 ret = -ENOMEM;
Jean-Francois Moine8295d992008-09-03 17:12:19 -03001882 goto out;
1883 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001884 gspca_dev->dev = dev;
1885 gspca_dev->iface = interface->bInterfaceNumber;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001886 gspca_dev->nbalt = intf->num_altsetting;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001887 gspca_dev->sd_desc = sd_desc;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001888 gspca_dev->nbufread = 2;
Jean-Francois Moineff374742008-10-23 07:29:51 -03001889 gspca_dev->empty_packet = -1; /* don't check the empty packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001890
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001891 /* configure the subdriver and initialize the USB device */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03001892 ret = sd_desc->config(gspca_dev, id);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001893 if (ret < 0)
1894 goto out;
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03001895 ret = sd_desc->init(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001896 if (ret < 0)
1897 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001898 ret = gspca_set_alt0(gspca_dev);
1899 if (ret < 0)
1900 goto out;
1901 gspca_set_default_mode(gspca_dev);
1902
1903 mutex_init(&gspca_dev->usb_lock);
1904 mutex_init(&gspca_dev->read_lock);
1905 mutex_init(&gspca_dev->queue_lock);
1906 init_waitqueue_head(&gspca_dev->wq);
1907
1908 /* init video stuff */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03001909 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
1910 gspca_dev->vdev.parent = &dev->dev;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03001911 gspca_dev->module = module;
Oliver Neukumee56a4d2008-07-27 14:01:59 -03001912 gspca_dev->present = 1;
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001913 ret = video_register_device(&gspca_dev->vdev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001914 VFL_TYPE_GRABBER,
Jean-Francois Moine8d584a52009-01-13 06:07:59 -03001915 -1);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001916 if (ret < 0) {
1917 err("video_register_device err %d", ret);
1918 goto out;
1919 }
1920
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001921 usb_set_intfdata(intf, gspca_dev);
1922 PDEBUG(D_PROBE, "probe ok");
1923 return 0;
1924out:
Jean-Francois Moinee07a1d82008-11-19 06:37:53 -03001925 kfree(gspca_dev->usb_buf);
1926 kfree(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001927 return ret;
1928}
1929EXPORT_SYMBOL(gspca_dev_probe);
1930
1931/*
1932 * USB disconnection
1933 *
1934 * This function must be called by the sub-driver
1935 * when the device disconnects, after the specific resources are freed.
1936 */
1937void gspca_disconnect(struct usb_interface *intf)
1938{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001939 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001940
Adam Bakerad281272009-02-04 15:33:21 -03001941 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03001942 gspca_dev->present = 0;
Adam Bakerad281272009-02-04 15:33:21 -03001943 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03001944
Adam Bakerad281272009-02-04 15:33:21 -03001945 destroy_urbs(gspca_dev);
1946 gspca_dev->dev = NULL;
Frank Zago6b060ff2008-09-28 08:12:22 -03001947 usb_set_intfdata(intf, NULL);
1948
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001949 /* release the device */
1950 /* (this will call gspca_release() immediatly or on last close) */
1951 video_unregister_device(&gspca_dev->vdev);
Jean-Francois Moine3145b8c2008-10-05 04:22:10 -03001952
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001953 PDEBUG(D_PROBE, "disconnect complete");
1954}
1955EXPORT_SYMBOL(gspca_disconnect);
1956
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001957#ifdef CONFIG_PM
1958int gspca_suspend(struct usb_interface *intf, pm_message_t message)
1959{
1960 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1961
1962 if (!gspca_dev->streaming)
1963 return 0;
1964 gspca_dev->frozen = 1; /* avoid urb error messages */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001965 if (gspca_dev->sd_desc->stopN)
1966 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001967 destroy_urbs(gspca_dev);
1968 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001969 if (gspca_dev->sd_desc->stop0)
1970 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001971 return 0;
1972}
1973EXPORT_SYMBOL(gspca_suspend);
1974
1975int gspca_resume(struct usb_interface *intf)
1976{
1977 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1978
1979 gspca_dev->frozen = 0;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001980 gspca_dev->sd_desc->init(gspca_dev);
1981 if (gspca_dev->streaming)
1982 return gspca_init_transfer(gspca_dev);
Jean-Francois Moine1e89e2da2008-09-03 17:12:15 -03001983 return 0;
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001984}
1985EXPORT_SYMBOL(gspca_resume);
1986#endif
Hans de Goededcef32372008-07-10 10:40:53 -03001987/* -- cam driver utility functions -- */
1988
1989/* auto gain and exposure algorithm based on the knee algorithm described here:
1990 http://ytse.tricolour.net/docs/LowLightOptimization.html
1991
1992 Returns 0 if no changes were made, 1 if the gain and or exposure settings
1993 where changed. */
1994int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
1995 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
1996{
1997 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
1998 const struct ctrl *gain_ctrl = NULL;
1999 const struct ctrl *exposure_ctrl = NULL;
2000 const struct ctrl *autogain_ctrl = NULL;
2001 int retval = 0;
2002
2003 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
2004 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2005 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2006 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2007 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2008 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2009 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2010 }
2011 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2012 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2013 "on cam without (auto)gain/exposure");
2014 return 0;
2015 }
2016
2017 if (gain_ctrl->get(gspca_dev, &gain) ||
2018 exposure_ctrl->get(gspca_dev, &exposure) ||
2019 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2020 return 0;
2021
2022 orig_gain = gain;
2023 orig_exposure = exposure;
2024
2025 /* If we are of a multiple of deadzone, do multiple steps to reach the
2026 desired lumination fast (with the risc of a slight overshoot) */
2027 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2028
Jean-Francois Moine1c44d812008-11-10 05:56:55 -03002029 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
Hans de Goededcef32372008-07-10 10:40:53 -03002030 avg_lum, desired_avg_lum, steps);
2031
2032 for (i = 0; i < steps; i++) {
2033 if (avg_lum > desired_avg_lum) {
2034 if (gain > gain_knee)
2035 gain--;
2036 else if (exposure > exposure_knee)
2037 exposure--;
2038 else if (gain > gain_ctrl->qctrl.default_value)
2039 gain--;
2040 else if (exposure > exposure_ctrl->qctrl.minimum)
2041 exposure--;
2042 else if (gain > gain_ctrl->qctrl.minimum)
2043 gain--;
2044 else
2045 break;
2046 } else {
2047 if (gain < gain_ctrl->qctrl.default_value)
2048 gain++;
2049 else if (exposure < exposure_knee)
2050 exposure++;
2051 else if (gain < gain_knee)
2052 gain++;
2053 else if (exposure < exposure_ctrl->qctrl.maximum)
2054 exposure++;
2055 else if (gain < gain_ctrl->qctrl.maximum)
2056 gain++;
2057 else
2058 break;
2059 }
2060 }
2061
2062 if (gain != orig_gain) {
2063 gain_ctrl->set(gspca_dev, gain);
2064 retval = 1;
2065 }
2066 if (exposure != orig_exposure) {
2067 exposure_ctrl->set(gspca_dev, exposure);
2068 retval = 1;
2069 }
2070
2071 return retval;
2072}
2073EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2074
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002075/* -- module insert / remove -- */
2076static int __init gspca_init(void)
2077{
Jean-Francois Moinebb64e862008-07-19 06:49:28 -03002078 info("main v%d.%d.%d registered",
2079 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2080 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2081 DRIVER_VERSION_NUMBER & 0xff);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002082 return 0;
2083}
2084static void __exit gspca_exit(void)
2085{
2086 info("main deregistered");
2087}
2088
2089module_init(gspca_init);
2090module_exit(gspca_exit);
2091
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03002092#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002093module_param_named(debug, gspca_debug, int, 0644);
2094MODULE_PARM_DESC(debug,
2095 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002096 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
2097 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002098#endif