blob: 2ed24527ecd61cea70287dd668725445799ee4fa [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 Moine240d3ad2008-10-24 05:02:58 -030047#define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 4, 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030048
49static int video_nr = -1;
50
Jean-Francois Moine335b3f82008-07-30 04:53:02 -030051#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030052int gspca_debug = D_ERR | D_PROBE;
53EXPORT_SYMBOL(gspca_debug);
54
55static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
56{
57 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
58 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
59 txt,
60 pixfmt & 0xff,
61 (pixfmt >> 8) & 0xff,
62 (pixfmt >> 16) & 0xff,
63 pixfmt >> 24,
64 w, h);
65 } else {
66 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
67 txt,
68 pixfmt,
69 w, h);
70 }
71}
72#else
73#define PDEBUG_MODE(txt, pixfmt, w, h)
74#endif
75
Jean-Francois Moined43fa322008-06-12 10:58:58 -030076/* specific memory types - !! should different from V4L2_MEMORY_xxx */
77#define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
78#define GSPCA_MEMORY_READ 7
79
Jean-Francois Moined43fa322008-06-12 10:58:58 -030080#define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
Jean-Francois Moined43fa322008-06-12 10:58:58 -030081
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030082/*
83 * VMA operations.
84 */
85static void gspca_vm_open(struct vm_area_struct *vma)
86{
87 struct gspca_frame *frame = vma->vm_private_data;
88
89 frame->vma_use_count++;
90 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
91}
92
93static void gspca_vm_close(struct vm_area_struct *vma)
94{
95 struct gspca_frame *frame = vma->vm_private_data;
96
97 if (--frame->vma_use_count <= 0)
98 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
99}
100
101static struct vm_operations_struct gspca_vm_ops = {
102 .open = gspca_vm_open,
103 .close = gspca_vm_close,
104};
105
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300106/* get the current input frame buffer */
107struct gspca_frame *gspca_get_i_frame(struct gspca_dev *gspca_dev)
108{
109 struct gspca_frame *frame;
110 int i;
111
112 i = gspca_dev->fr_i;
113 i = gspca_dev->fr_queue[i];
114 frame = &gspca_dev->frame[i];
115 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
116 != V4L2_BUF_FLAG_QUEUED)
117 return NULL;
118 return frame;
119}
120EXPORT_SYMBOL(gspca_get_i_frame);
121
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300122/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300123 * fill a video frame from an URB and resubmit
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300124 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300125static void fill_frame(struct gspca_dev *gspca_dev,
126 struct urb *urb)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300127{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300128 struct gspca_frame *frame;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300129 __u8 *data; /* address of data in the iso message */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300130 int i, len, st;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300131 cam_pkt_op pkt_scan;
132
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300133 if (urb->status != 0) {
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300134#ifdef CONFIG_PM
Jean-Francois Moine6a709742008-09-03 16:48:10 -0300135 if (!gspca_dev->frozen)
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300136#endif
Jean-Francois Moine6a709742008-09-03 16:48:10 -0300137 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300138 return; /* disconnection ? */
139 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300140 pkt_scan = gspca_dev->sd_desc->pkt_scan;
141 for (i = 0; i < urb->number_of_packets; i++) {
142
143 /* check the availability of the frame buffer */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300144 frame = gspca_get_i_frame(gspca_dev);
145 if (!frame) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300146 gspca_dev->last_packet_type = DISCARD_PACKET;
147 break;
148 }
149
150 /* check the packet status and length */
151 len = urb->iso_frame_desc[i].actual_length;
Jean-Francois Moineff374742008-10-23 07:29:51 -0300152 if (len == 0) {
153 if (gspca_dev->empty_packet == 0)
154 gspca_dev->empty_packet = 1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300155 continue;
Jean-Francois Moineff374742008-10-23 07:29:51 -0300156 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300157 st = urb->iso_frame_desc[i].status;
158 if (st) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300159 PDEBUG(D_ERR,
160 "ISOC data error: [%d] len=%d, status=%d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300161 i, len, st);
162 gspca_dev->last_packet_type = DISCARD_PACKET;
163 continue;
164 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300165
166 /* let the packet be analyzed by the subdriver */
167 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
168 i, urb->iso_frame_desc[i].offset, len);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300169 data = (__u8 *) urb->transfer_buffer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300170 + urb->iso_frame_desc[i].offset;
171 pkt_scan(gspca_dev, frame, data, len);
172 }
173
174 /* resubmit the URB */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300175 st = usb_submit_urb(urb, GFP_ATOMIC);
176 if (st < 0)
177 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
178}
179
180/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300181 * ISOC message interrupt from the USB device
182 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300183 * Analyse each packet and call the subdriver for copy to the frame buffer.
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300184 */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300185static void isoc_irq(struct urb *urb
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300186)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300187{
188 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
189
Hans de Goede3647fea2008-07-15 05:36:30 -0300190 PDEBUG(D_PACK, "isoc irq");
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300191 if (!gspca_dev->streaming)
192 return;
193 fill_frame(gspca_dev, urb);
194}
195
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300196/*
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300197 * bulk message interrupt from the USB device
198 */
199static void bulk_irq(struct urb *urb
200)
201{
202 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
203 struct gspca_frame *frame;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300204 int st;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300205
206 PDEBUG(D_PACK, "bulk irq");
207 if (!gspca_dev->streaming)
208 return;
Jean-Francois Moine880ae242008-11-18 04:19:52 -0300209 switch (urb->status) {
210 case 0:
211 break;
212 case -ECONNRESET:
213 urb->status = 0;
214 break;
215 default:
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300216#ifdef CONFIG_PM
217 if (!gspca_dev->frozen)
218#endif
219 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
220 return; /* disconnection ? */
221 }
222
223 /* check the availability of the frame buffer */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300224 frame = gspca_get_i_frame(gspca_dev);
225 if (!frame) {
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300226 gspca_dev->last_packet_type = DISCARD_PACKET;
227 } else {
228 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
229 gspca_dev->sd_desc->pkt_scan(gspca_dev,
230 frame,
231 urb->transfer_buffer,
232 urb->actual_length);
233 }
Antonio Ospitedff369a2008-11-14 07:53:32 -0300234
235 /* resubmit the URB */
236 if (gspca_dev->cam.bulk_nurbs != 0) {
237 st = usb_submit_urb(urb, GFP_ATOMIC);
238 if (st < 0)
239 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
240 }
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300241}
242
243/*
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300244 * add data to the current frame
245 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300246 * This function is called by the subdrivers at interrupt level.
247 *
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300248 * To build a frame, these ones must add
249 * - one FIRST_PACKET
250 * - 0 or many INTER_PACKETs
251 * - one LAST_PACKET
252 * DISCARD_PACKET invalidates the whole frame.
253 * On LAST_PACKET, a new frame is returned.
254 */
255struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
Erik Andrene293e592008-10-03 08:46:50 -0300256 enum gspca_packet_type packet_type,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300257 struct gspca_frame *frame,
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300258 const __u8 *data,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300259 int len)
260{
261 int i, j;
262
Hans de Goedee2997a72008-04-23 08:09:12 -0300263 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300264
265 /* when start of a new frame, if the current frame buffer
266 * is not queued, discard the whole frame */
267 if (packet_type == FIRST_PACKET) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300268 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
269 != V4L2_BUF_FLAG_QUEUED) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300270 gspca_dev->last_packet_type = DISCARD_PACKET;
271 return frame;
272 }
273 frame->data_end = frame->data;
274 jiffies_to_timeval(get_jiffies_64(),
275 &frame->v4l2_buf.timestamp);
276 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300277 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
Jean-Francois Moine06ca78f2008-07-22 03:45:08 -0300278 if (packet_type == LAST_PACKET)
279 gspca_dev->last_packet_type = packet_type;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300280 return frame;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300281 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300282
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300283 /* append the packet to the frame buffer */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300284 if (len > 0) {
285 if (frame->data_end - frame->data + len
286 > frame->v4l2_buf.length) {
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -0300287 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300288 frame->data_end - frame->data + len,
289 frame->v4l2_buf.length);
290 packet_type = DISCARD_PACKET;
291 } else {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300292 memcpy(frame->data_end, data, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300293 frame->data_end += len;
294 }
295 }
296 gspca_dev->last_packet_type = packet_type;
297
Frank Zago219423c2008-09-26 07:43:54 -0300298 /* if last packet, wake up the application and advance in the queue */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300299 if (packet_type == LAST_PACKET) {
300 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
301 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
302 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300303 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300304 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
305 gspca_dev->fr_i = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300306 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
307 frame->v4l2_buf.bytesused,
308 gspca_dev->fr_q,
309 i,
310 gspca_dev->fr_o);
311 j = gspca_dev->fr_queue[i];
312 frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300313 }
314 return frame;
315}
316EXPORT_SYMBOL(gspca_frame_add);
317
318static int gspca_is_compressed(__u32 format)
319{
320 switch (format) {
321 case V4L2_PIX_FMT_MJPEG:
322 case V4L2_PIX_FMT_JPEG:
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300323 case V4L2_PIX_FMT_SPCA561:
Hans de Goedeab8f12c2008-07-04 18:29:32 -0300324 case V4L2_PIX_FMT_PAC207:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300325 return 1;
326 }
327 return 0;
328}
329
330static void *rvmalloc(unsigned long size)
331{
332 void *mem;
333 unsigned long adr;
334
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300335 mem = vmalloc_32(size);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300336 if (mem != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300337 adr = (unsigned long) mem;
338 while ((long) size > 0) {
339 SetPageReserved(vmalloc_to_page((void *) adr));
340 adr += PAGE_SIZE;
341 size -= PAGE_SIZE;
342 }
343 }
344 return mem;
345}
346
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300347static void rvfree(void *mem, long size)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300348{
349 unsigned long adr;
350
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300351 adr = (unsigned long) mem;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300352 while (size > 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300353 ClearPageReserved(vmalloc_to_page((void *) adr));
354 adr += PAGE_SIZE;
355 size -= PAGE_SIZE;
356 }
357 vfree(mem);
358}
359
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300360static int frame_alloc(struct gspca_dev *gspca_dev,
361 unsigned int count)
362{
363 struct gspca_frame *frame;
364 unsigned int frsz;
365 int i;
366
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300367 i = gspca_dev->curr_mode;
368 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300369 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300370 frsz = PAGE_ALIGN(frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300371 gspca_dev->frsz = frsz;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300372 if (count > GSPCA_MAX_FRAMES)
373 count = GSPCA_MAX_FRAMES;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300374 gspca_dev->frbuf = rvmalloc(frsz * count);
375 if (!gspca_dev->frbuf) {
376 err("frame alloc failed");
377 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300378 }
379 gspca_dev->nframes = count;
380 for (i = 0; i < count; i++) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300381 frame = &gspca_dev->frame[i];
382 frame->v4l2_buf.index = i;
383 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
384 frame->v4l2_buf.flags = 0;
385 frame->v4l2_buf.field = V4L2_FIELD_NONE;
386 frame->v4l2_buf.length = frsz;
387 frame->v4l2_buf.memory = gspca_dev->memory;
388 frame->v4l2_buf.sequence = 0;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300389 frame->data = frame->data_end =
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300390 gspca_dev->frbuf + i * frsz;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300391 frame->v4l2_buf.m.offset = i * frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300392 }
393 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
394 gspca_dev->last_packet_type = DISCARD_PACKET;
395 gspca_dev->sequence = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300396 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300397}
398
399static void frame_free(struct gspca_dev *gspca_dev)
400{
401 int i;
402
403 PDEBUG(D_STREAM, "frame free");
Harvey Harrisonefab8212008-07-05 06:12:47 -0300404 if (gspca_dev->frbuf != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300405 rvfree(gspca_dev->frbuf,
406 gspca_dev->nframes * gspca_dev->frsz);
407 gspca_dev->frbuf = NULL;
408 for (i = 0; i < gspca_dev->nframes; i++)
409 gspca_dev->frame[i].data = NULL;
410 }
411 gspca_dev->nframes = 0;
412}
413
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300414static void destroy_urbs(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300415{
416 struct urb *urb;
417 unsigned int i;
418
419 PDEBUG(D_STREAM, "kill transfer");
Erik Andrén44732812008-10-13 15:52:46 -0300420 for (i = 0; i < MAX_NURBS; i++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300421 urb = gspca_dev->urb[i];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300422 if (urb == NULL)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300423 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300424
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300425 gspca_dev->urb[i] = NULL;
Jean-Francois Moine2fd9c2e2009-01-24 07:45:14 -0300426 if (!gspca_dev->present)
427 usb_kill_urb(urb);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300428 if (urb->transfer_buffer != NULL)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300429 usb_buffer_free(gspca_dev->dev,
430 urb->transfer_buffer_length,
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300431 urb->transfer_buffer,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300432 urb->transfer_dma);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300433 usb_free_urb(urb);
434 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300435}
436
437/*
Frank Zago219423c2008-09-26 07:43:54 -0300438 * look for an input transfer endpoint in an alternate setting
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300439 */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300440static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
441 __u8 epaddr,
442 __u8 xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300443{
444 struct usb_host_endpoint *ep;
445 int i, attr;
446
447 epaddr |= USB_DIR_IN;
448 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
449 ep = &alt->endpoint[i];
450 if (ep->desc.bEndpointAddress == epaddr) {
451 attr = ep->desc.bmAttributes
452 & USB_ENDPOINT_XFERTYPE_MASK;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300453 if (attr == xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300454 return ep;
455 break;
456 }
457 }
458 return NULL;
459}
460
461/*
Frank Zago219423c2008-09-26 07:43:54 -0300462 * look for an input (isoc or bulk) endpoint
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300463 *
464 * The endpoint is defined by the subdriver.
465 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
466 * This routine may be called many times when the bandwidth is too small
467 * (the bandwidth is checked on urb submit).
468 */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300469static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300470{
471 struct usb_interface *intf;
472 struct usb_host_endpoint *ep;
473 int i, ret;
474
475 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300476 ep = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300477 i = gspca_dev->alt; /* previous alt setting */
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300478
479 /* try isoc */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300480 while (--i > 0) { /* alt 0 is unusable */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300481 ep = alt_xfer(&intf->altsetting[i],
482 gspca_dev->cam.epaddr,
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300483 USB_ENDPOINT_XFER_ISOC);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300484 if (ep)
485 break;
486 }
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300487
488 /* if no isoc, try bulk */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300489 if (ep == NULL) {
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300490 ep = alt_xfer(&intf->altsetting[0],
491 gspca_dev->cam.epaddr,
492 USB_ENDPOINT_XFER_BULK);
493 if (ep == NULL) {
494 err("no transfer endpoint found");
495 return NULL;
496 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300497 }
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300498 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300499 i, ep->desc.bEndpointAddress);
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300500 if (i > 0) {
501 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
502 if (ret < 0) {
503 err("set interface err %d", ret);
504 return NULL;
505 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300506 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300507 gspca_dev->alt = i; /* memorize the current alt setting */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300508 return ep;
509}
510
511/*
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300512 * create the URBs for image transfer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300513 */
514static int create_urbs(struct gspca_dev *gspca_dev,
515 struct usb_host_endpoint *ep)
516{
517 struct urb *urb;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300518 int n, nurbs, i, psize, npkt, bsize;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300519
520 /* calculate the packet size and the number of packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300521 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300522
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300523 if (gspca_dev->alt != 0) { /* isoc */
524
525 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
526 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300527 npkt = ISO_MAX_SIZE / psize;
528 if (npkt > ISO_MAX_PKT)
529 npkt = ISO_MAX_PKT;
530 bsize = psize * npkt;
531 PDEBUG(D_STREAM,
532 "isoc %d pkts size %d = bsize:%d",
533 npkt, psize, bsize);
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300534 nurbs = DEF_NURBS;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300535 } else { /* bulk */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300536 npkt = 0;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300537 bsize = gspca_dev->cam.bulk_size;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300538 if (bsize == 0)
539 bsize = psize;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300540 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300541 if (gspca_dev->cam.bulk_nurbs != 0)
542 nurbs = gspca_dev->cam.bulk_nurbs;
543 else
544 nurbs = 1;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300545 }
546
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300547 gspca_dev->nurbs = nurbs;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300548 for (n = 0; n < nurbs; n++) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300549 urb = usb_alloc_urb(npkt, GFP_KERNEL);
550 if (!urb) {
551 err("usb_alloc_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 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300556 bsize,
557 GFP_KERNEL,
558 &urb->transfer_dma);
559
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300560 if (urb->transfer_buffer == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300561 usb_free_urb(urb);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300562 err("usb_buffer_urb failed");
Jean-Francois Moine0d0548e2008-09-05 12:49:54 -0300563 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300564 return -ENOMEM;
565 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300566 gspca_dev->urb[n] = urb;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300567 urb->dev = gspca_dev->dev;
568 urb->context = gspca_dev;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300569 urb->transfer_buffer_length = bsize;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300570 if (npkt != 0) { /* ISOC */
571 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
572 ep->desc.bEndpointAddress);
Frank Zago6c49da72008-09-28 07:43:00 -0300573 urb->transfer_flags = URB_ISO_ASAP
574 | URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300575 urb->interval = ep->desc.bInterval;
576 urb->complete = isoc_irq;
577 urb->number_of_packets = npkt;
578 for (i = 0; i < npkt; i++) {
579 urb->iso_frame_desc[i].length = psize;
580 urb->iso_frame_desc[i].offset = psize * i;
581 }
582 } else { /* bulk */
583 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
584 ep->desc.bEndpointAddress),
Frank Zago6c49da72008-09-28 07:43:00 -0300585 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300586 urb->complete = bulk_irq;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300587 }
588 }
589 return 0;
590}
591
592/*
593 * start the USB transfer
594 */
595static int gspca_init_transfer(struct gspca_dev *gspca_dev)
596{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300597 struct usb_host_endpoint *ep;
598 int n, ret;
599
Hans de Goedee2997a72008-04-23 08:09:12 -0300600 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
601 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300602
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300603 /* set the higher alternate setting and
604 * loop until urb submit succeeds */
605 gspca_dev->alt = gspca_dev->nbalt;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300606 for (;;) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300607 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300608 ep = get_ep(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300609 if (ep == NULL) {
610 ret = -EIO;
611 goto out;
612 }
613 ret = create_urbs(gspca_dev, ep);
614 if (ret < 0)
615 goto out;
616
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300617 /* clear the bulk endpoint */
618 if (gspca_dev->alt == 0) /* if bulk transfer */
619 usb_clear_halt(gspca_dev->dev,
620 usb_rcvintpipe(gspca_dev->dev,
621 gspca_dev->cam.epaddr));
622
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300623 /* start the cam */
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300624 ret = gspca_dev->sd_desc->start(gspca_dev);
625 if (ret < 0) {
626 destroy_urbs(gspca_dev);
627 goto out;
628 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300629 gspca_dev->streaming = 1;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300630
Antonio Ospitedff369a2008-11-14 07:53:32 -0300631 /* some bulk transfers are started by the subdriver */
632 if (gspca_dev->alt == 0 && gspca_dev->cam.bulk_nurbs == 0)
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300633 break;
634
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300635 /* submit the URBs */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300636 for (n = 0; n < gspca_dev->nurbs; n++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300637 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300638 if (ret < 0) {
639 PDEBUG(D_ERR|D_STREAM,
640 "usb_submit_urb [%d] err %d", n, ret);
Hans de Goedee2997a72008-04-23 08:09:12 -0300641 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300642 destroy_urbs(gspca_dev);
Jean-Francois Moine9edf1a62008-11-23 15:53:28 -0300643 if (ret == -ENOSPC) {
Jean-Francois Moineabf485f2008-11-24 06:38:21 -0300644 msleep(20); /* wait for kill
Jean-Francois Moine9edf1a62008-11-23 15:53:28 -0300645 * complete */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300646 break; /* try the previous alt */
Jean-Francois Moine9edf1a62008-11-23 15:53:28 -0300647 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300648 goto out;
649 }
650 }
651 if (ret >= 0)
652 break;
653 }
654out:
655 mutex_unlock(&gspca_dev->usb_lock);
656 return ret;
657}
658
659static int gspca_set_alt0(struct gspca_dev *gspca_dev)
660{
661 int ret;
662
663 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
664 if (ret < 0)
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -0300665 PDEBUG(D_ERR|D_STREAM, "set alt 0 err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300666 return ret;
667}
668
Frank Zago219423c2008-09-26 07:43:54 -0300669/* Note: both the queue and the usb locks should be held when calling this */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300670static void gspca_stream_off(struct gspca_dev *gspca_dev)
671{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300672 gspca_dev->streaming = 0;
Jean-Francois Moine98522a72008-11-18 06:33:08 -0300673 if (gspca_dev->present
674 && gspca_dev->sd_desc->stopN)
675 gspca_dev->sd_desc->stopN(gspca_dev);
676 destroy_urbs(gspca_dev);
677 gspca_set_alt0(gspca_dev);
678 if (gspca_dev->sd_desc->stop0)
679 gspca_dev->sd_desc->stop0(gspca_dev);
680 PDEBUG(D_STREAM, "stream off OK");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300681}
682
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300683static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300684{
685 int i;
686
687 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
688 gspca_dev->curr_mode = i;
689 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
690 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300691 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300692}
693
694static int wxh_to_mode(struct gspca_dev *gspca_dev,
695 int width, int height)
696{
697 int i;
698
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300699 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
700 if (width >= gspca_dev->cam.cam_mode[i].width
701 && height >= gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300702 break;
703 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300704 return i;
705}
706
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300707/*
708 * search a mode with the right pixel format
709 */
710static int gspca_get_mode(struct gspca_dev *gspca_dev,
711 int mode,
712 int pixfmt)
713{
714 int modeU, modeD;
715
716 modeU = modeD = mode;
717 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
718 if (--modeD >= 0) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300719 if (gspca_dev->cam.cam_mode[modeD].pixelformat
720 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300721 return modeD;
722 }
723 if (++modeU < gspca_dev->cam.nmodes) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300724 if (gspca_dev->cam.cam_mode[modeU].pixelformat
725 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300726 return modeU;
727 }
728 }
729 return -EINVAL;
730}
731
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300732static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300733 struct v4l2_fmtdesc *fmtdesc)
734{
735 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300736 int i, j, index;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300737 __u32 fmt_tb[8];
738
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300739 /* give an index to each format */
740 index = 0;
741 j = 0;
742 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300743 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300744 j = 0;
745 for (;;) {
746 if (fmt_tb[j] == fmt_tb[index])
747 break;
748 j++;
749 }
750 if (j == index) {
751 if (fmtdesc->index == index)
752 break; /* new format */
753 index++;
Julia Lawall80297122008-11-12 23:18:21 -0300754 if (index >= ARRAY_SIZE(fmt_tb))
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300755 return -EINVAL;
756 }
757 }
758 if (i < 0)
759 return -EINVAL; /* no more format */
760
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300761 fmtdesc->pixelformat = fmt_tb[index];
762 if (gspca_is_compressed(fmt_tb[index]))
763 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300764 fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300765 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
766 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
767 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
768 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
769 fmtdesc->description[4] = '\0';
770 return 0;
771}
772
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300773static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300774 struct v4l2_format *fmt)
775{
776 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300777 int mode;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300778
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300779 mode = gspca_dev->curr_mode;
780 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
781 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300782 return 0;
783}
784
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300785static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300786 struct v4l2_format *fmt)
787{
Hans de Goede24c530b2008-07-04 17:55:18 -0300788 int w, h, mode, mode2;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300789
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300790 w = fmt->fmt.pix.width;
791 h = fmt->fmt.pix.height;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300792
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300793#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300794 if (gspca_debug & D_CONF)
795 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
796#endif
797 /* search the closest mode for width and height */
798 mode = wxh_to_mode(gspca_dev, w, h);
799
800 /* OK if right palette */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300801 if (gspca_dev->cam.cam_mode[mode].pixelformat
802 != fmt->fmt.pix.pixelformat) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300803
804 /* else, search the closest mode with the same pixel format */
805 mode2 = gspca_get_mode(gspca_dev, mode,
806 fmt->fmt.pix.pixelformat);
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300807 if (mode2 >= 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300808 mode = mode2;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300809/* else
810 ; * no chance, return this mode */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300811 }
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300812 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
813 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300814 return mode; /* used when s_fmt */
815}
816
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300817static int vidioc_try_fmt_vid_cap(struct file *file,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300818 void *priv,
819 struct v4l2_format *fmt)
820{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300821 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300822 int ret;
823
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300824 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300825 if (ret < 0)
826 return ret;
827 return 0;
828}
829
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300830static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300831 struct v4l2_format *fmt)
832{
833 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300834 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300835
Hans de Goedee2997a72008-04-23 08:09:12 -0300836 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
837 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300838
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300839 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300840 if (ret < 0)
841 goto out;
842
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300843 if (gspca_dev->nframes != 0
844 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
845 ret = -EINVAL;
846 goto out;
847 }
848
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300849 if (ret == gspca_dev->curr_mode) {
850 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300851 goto out; /* same mode */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300852 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300853
854 if (gspca_dev->streaming) {
855 ret = -EBUSY;
856 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -0300857 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300858 gspca_dev->width = fmt->fmt.pix.width;
859 gspca_dev->height = fmt->fmt.pix.height;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300860 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
861 gspca_dev->curr_mode = ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300862
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300863 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300864out:
865 mutex_unlock(&gspca_dev->queue_lock);
866 return ret;
867}
868
Hans de Goeded0182302008-11-19 17:10:47 -0300869static void gspca_release(struct video_device *vfd)
Frank Zago6b060ff2008-09-28 08:12:22 -0300870{
Hans de Goeded0182302008-11-19 17:10:47 -0300871 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
Frank Zago6b060ff2008-09-28 08:12:22 -0300872
Hans de Goeded0182302008-11-19 17:10:47 -0300873 PDEBUG(D_STREAM, "device released");
Frank Zago6b060ff2008-09-28 08:12:22 -0300874
875 kfree(gspca_dev->usb_buf);
876 kfree(gspca_dev);
877}
878
Hans Verkuilbec43662008-12-30 06:58:20 -0300879static int dev_open(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300880{
881 struct gspca_dev *gspca_dev;
882 int ret;
883
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300884 PDEBUG(D_STREAM, "%s open", current->comm);
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -0300885 gspca_dev = (struct gspca_dev *) video_devdata(file);
Hans de Goedee2997a72008-04-23 08:09:12 -0300886 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
887 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300888 if (!gspca_dev->present) {
889 ret = -ENODEV;
890 goto out;
891 }
892
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300893 if (gspca_dev->users > 4) { /* (arbitrary value) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300894 ret = -EBUSY;
895 goto out;
896 }
Jean-Francois Moine5c4fa002008-11-18 15:52:31 -0300897
898 /* protect the subdriver against rmmod */
899 if (!try_module_get(gspca_dev->module)) {
900 ret = -ENODEV;
901 goto out;
902 }
903
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300904 gspca_dev->users++;
Frank Zago6b060ff2008-09-28 08:12:22 -0300905
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300906 file->private_data = gspca_dev;
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300907#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300908 /* activate the v4l2 debug */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300909 if (gspca_debug & D_V4L2)
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -0300910 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -0300911 | V4L2_DEBUG_IOCTL_ARG;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300912 else
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#endif
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300916 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300917out:
918 mutex_unlock(&gspca_dev->queue_lock);
919 if (ret != 0)
920 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
921 else
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300922 PDEBUG(D_STREAM, "open done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300923 return ret;
924}
925
Hans Verkuilbec43662008-12-30 06:58:20 -0300926static int dev_close(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300927{
928 struct gspca_dev *gspca_dev = file->private_data;
929
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300930 PDEBUG(D_STREAM, "%s close", current->comm);
Hans de Goedee2997a72008-04-23 08:09:12 -0300931 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
932 return -ERESTARTSYS;
933 gspca_dev->users--;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300934
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -0300935 /* if the file did the capture, free the streaming resources */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300936 if (gspca_dev->capt_file == file) {
Jean-Francois Moinea1e82882008-09-03 17:12:17 -0300937 if (gspca_dev->streaming) {
938 mutex_lock(&gspca_dev->usb_lock);
939 gspca_stream_off(gspca_dev);
940 mutex_unlock(&gspca_dev->usb_lock);
941 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300942 frame_free(gspca_dev);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300943 gspca_dev->capt_file = NULL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300944 gspca_dev->memory = GSPCA_MEMORY_NO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300945 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300946 file->private_data = NULL;
Jean-Francois Moine5c4fa002008-11-18 15:52:31 -0300947 module_put(gspca_dev->module);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300948 mutex_unlock(&gspca_dev->queue_lock);
Frank Zago6b060ff2008-09-28 08:12:22 -0300949
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300950 PDEBUG(D_STREAM, "close done");
Frank Zago6b060ff2008-09-28 08:12:22 -0300951
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300952 return 0;
953}
954
955static int vidioc_querycap(struct file *file, void *priv,
956 struct v4l2_capability *cap)
957{
958 struct gspca_dev *gspca_dev = priv;
959
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300960 memset(cap, 0, sizeof *cap);
961 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -0300962 if (gspca_dev->dev->product != NULL) {
963 strncpy(cap->card, gspca_dev->dev->product,
964 sizeof cap->card);
965 } else {
966 snprintf(cap->card, sizeof cap->card,
967 "USB Camera (%04x:%04x)",
968 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
969 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
970 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300971 strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name,
972 sizeof cap->bus_info);
973 cap->version = DRIVER_VERSION_NUMBER;
974 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
975 | V4L2_CAP_STREAMING
976 | V4L2_CAP_READWRITE;
977 return 0;
978}
979
980static int vidioc_queryctrl(struct file *file, void *priv,
981 struct v4l2_queryctrl *q_ctrl)
982{
983 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300984 int i, ix;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300985 u32 id;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300986
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300987 ix = -1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300988 id = q_ctrl->id;
989 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
990 id &= V4L2_CTRL_ID_MASK;
991 id++;
992 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goede0f523c22008-09-03 17:12:14 -0300993 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300994 continue;
995 if (ix < 0) {
996 ix = i;
997 continue;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300998 }
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300999 if (gspca_dev->sd_desc->ctrls[i].qctrl.id
1000 > gspca_dev->sd_desc->ctrls[ix].qctrl.id)
1001 continue;
1002 ix = i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001003 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001004 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001005 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001006 if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) {
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001007 ix = i;
1008 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001009 }
1010 }
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001011 if (ix < 0)
1012 return -EINVAL;
1013 memcpy(q_ctrl, &gspca_dev->sd_desc->ctrls[ix].qctrl,
1014 sizeof *q_ctrl);
1015 if (gspca_dev->ctrl_dis & (1 << ix))
1016 q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
1017 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001018}
1019
1020static int vidioc_s_ctrl(struct file *file, void *priv,
1021 struct v4l2_control *ctrl)
1022{
1023 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001024 const struct ctrl *ctrls;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001025 int i, ret;
1026
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001027 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1028 i < gspca_dev->sd_desc->nctrls;
1029 i++, ctrls++) {
1030 if (ctrl->id != ctrls->qctrl.id)
1031 continue;
Hans de Goede0f523c22008-09-03 17:12:14 -03001032 if (gspca_dev->ctrl_dis & (1 << i))
1033 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001034 if (ctrl->value < ctrls->qctrl.minimum
Rabin Vincentfcf5cb22008-08-03 07:58:54 -03001035 || ctrl->value > ctrls->qctrl.maximum)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001036 return -ERANGE;
1037 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
Hans de Goedee2997a72008-04-23 08:09:12 -03001038 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1039 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001040 ret = ctrls->set(gspca_dev, ctrl->value);
1041 mutex_unlock(&gspca_dev->usb_lock);
1042 return ret;
1043 }
1044 return -EINVAL;
1045}
1046
1047static int vidioc_g_ctrl(struct file *file, void *priv,
1048 struct v4l2_control *ctrl)
1049{
1050 struct gspca_dev *gspca_dev = priv;
1051
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001052 const struct ctrl *ctrls;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001053 int i, ret;
1054
1055 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1056 i < gspca_dev->sd_desc->nctrls;
1057 i++, ctrls++) {
1058 if (ctrl->id != ctrls->qctrl.id)
1059 continue;
Hans de Goede0f523c22008-09-03 17:12:14 -03001060 if (gspca_dev->ctrl_dis & (1 << i))
1061 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001062 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1063 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001064 ret = ctrls->get(gspca_dev, &ctrl->value);
1065 mutex_unlock(&gspca_dev->usb_lock);
1066 return ret;
1067 }
1068 return -EINVAL;
1069}
1070
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03001071/*fixme: have an audio flag in gspca_dev?*/
1072static int vidioc_s_audio(struct file *file, void *priv,
1073 struct v4l2_audio *audio)
1074{
1075 if (audio->index != 0)
1076 return -EINVAL;
1077 return 0;
1078}
1079
1080static int vidioc_g_audio(struct file *file, void *priv,
1081 struct v4l2_audio *audio)
1082{
1083 memset(audio, 0, sizeof *audio);
1084 strcpy(audio->name, "Microphone");
1085 return 0;
1086}
1087
1088static int vidioc_enumaudio(struct file *file, void *priv,
1089 struct v4l2_audio *audio)
1090{
1091 if (audio->index != 0)
1092 return -EINVAL;
1093
1094 strcpy(audio->name, "Microphone");
1095 audio->capability = 0;
1096 audio->mode = 0;
1097 return 0;
1098}
1099
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001100static int vidioc_querymenu(struct file *file, void *priv,
1101 struct v4l2_querymenu *qmenu)
1102{
1103 struct gspca_dev *gspca_dev = priv;
1104
1105 if (!gspca_dev->sd_desc->querymenu)
1106 return -EINVAL;
1107 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1108}
1109
1110static int vidioc_enum_input(struct file *file, void *priv,
1111 struct v4l2_input *input)
1112{
1113 struct gspca_dev *gspca_dev = priv;
1114
1115 if (input->index != 0)
1116 return -EINVAL;
1117 memset(input, 0, sizeof *input);
1118 input->type = V4L2_INPUT_TYPE_CAMERA;
1119 strncpy(input->name, gspca_dev->sd_desc->name,
1120 sizeof input->name);
1121 return 0;
1122}
1123
1124static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1125{
1126 *i = 0;
1127 return 0;
1128}
1129
1130static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1131{
1132 if (i > 0)
1133 return -EINVAL;
1134 return (0);
1135}
1136
1137static int vidioc_reqbufs(struct file *file, void *priv,
1138 struct v4l2_requestbuffers *rb)
1139{
1140 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001141 int i, ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001142
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001143 switch (rb->memory) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001144 case GSPCA_MEMORY_READ: /* (internal call) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001145 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001146 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001147 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001148 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001149 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001150 }
Hans de Goedee2997a72008-04-23 08:09:12 -03001151 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1152 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001153
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001154 if (gspca_dev->memory != GSPCA_MEMORY_NO
1155 && gspca_dev->memory != rb->memory) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001156 ret = -EBUSY;
1157 goto out;
1158 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001159
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001160 /* only one file may do the capture */
1161 if (gspca_dev->capt_file != NULL
1162 && gspca_dev->capt_file != file) {
1163 ret = -EBUSY;
1164 goto out;
1165 }
1166
1167 /* if allocated, the buffers must not be mapped */
1168 for (i = 0; i < gspca_dev->nframes; i++) {
1169 if (gspca_dev->frame[i].vma_use_count) {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001170 ret = -EBUSY;
1171 goto out;
1172 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001173 }
1174
1175 /* stop streaming */
1176 if (gspca_dev->streaming) {
1177 mutex_lock(&gspca_dev->usb_lock);
1178 gspca_stream_off(gspca_dev);
1179 mutex_unlock(&gspca_dev->usb_lock);
1180 }
1181
1182 /* free the previous allocated buffers, if any */
1183 if (gspca_dev->nframes != 0) {
1184 frame_free(gspca_dev);
1185 gspca_dev->capt_file = NULL;
1186 }
1187 if (rb->count == 0) /* unrequest */
1188 goto out;
1189 gspca_dev->memory = rb->memory;
1190 ret = frame_alloc(gspca_dev, rb->count);
1191 if (ret == 0) {
1192 rb->count = gspca_dev->nframes;
1193 gspca_dev->capt_file = file;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001194 }
1195out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001196 mutex_unlock(&gspca_dev->queue_lock);
1197 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1198 return ret;
1199}
1200
1201static int vidioc_querybuf(struct file *file, void *priv,
1202 struct v4l2_buffer *v4l2_buf)
1203{
1204 struct gspca_dev *gspca_dev = priv;
1205 struct gspca_frame *frame;
1206
Jean-Francois Moine13752bd2008-12-19 15:08:51 -03001207 if (v4l2_buf->index < 0
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001208 || v4l2_buf->index >= gspca_dev->nframes)
1209 return -EINVAL;
1210
1211 frame = &gspca_dev->frame[v4l2_buf->index];
1212 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1213 return 0;
1214}
1215
1216static int vidioc_streamon(struct file *file, void *priv,
1217 enum v4l2_buf_type buf_type)
1218{
1219 struct gspca_dev *gspca_dev = priv;
1220 int ret;
1221
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001222 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1223 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001224 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1225 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001226 if (!gspca_dev->present) {
1227 ret = -ENODEV;
1228 goto out;
1229 }
Jean-Francois Moine1edabe72008-12-19 15:17:25 -03001230 if (gspca_dev->nframes == 0
1231 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001232 ret = -EINVAL;
1233 goto out;
1234 }
1235 if (!gspca_dev->streaming) {
1236 ret = gspca_init_transfer(gspca_dev);
1237 if (ret < 0)
1238 goto out;
1239 }
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001240#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001241 if (gspca_debug & D_STREAM) {
1242 PDEBUG_MODE("stream on OK",
1243 gspca_dev->pixfmt,
1244 gspca_dev->width,
1245 gspca_dev->height);
1246 }
1247#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001248 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001249out:
1250 mutex_unlock(&gspca_dev->queue_lock);
1251 return ret;
1252}
1253
1254static int vidioc_streamoff(struct file *file, void *priv,
1255 enum v4l2_buf_type buf_type)
1256{
1257 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001258 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001259
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001260 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1261 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001262 if (!gspca_dev->streaming)
1263 return 0;
1264 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1265 return -ERESTARTSYS;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001266
1267 /* stop streaming */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001268 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1269 ret = -ERESTARTSYS;
1270 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001271 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001272 gspca_stream_off(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001273 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001274
1275 /* empty the application queues */
1276 for (i = 0; i < gspca_dev->nframes; i++)
1277 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1278 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1279 gspca_dev->last_packet_type = DISCARD_PACKET;
1280 gspca_dev->sequence = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001281 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001282out:
1283 mutex_unlock(&gspca_dev->queue_lock);
1284 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001285}
1286
1287static int vidioc_g_jpegcomp(struct file *file, void *priv,
1288 struct v4l2_jpegcompression *jpegcomp)
1289{
1290 struct gspca_dev *gspca_dev = priv;
1291 int ret;
1292
1293 if (!gspca_dev->sd_desc->get_jcomp)
1294 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001295 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1296 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001297 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1298 mutex_unlock(&gspca_dev->usb_lock);
1299 return ret;
1300}
1301
1302static int vidioc_s_jpegcomp(struct file *file, void *priv,
1303 struct v4l2_jpegcompression *jpegcomp)
1304{
1305 struct gspca_dev *gspca_dev = priv;
1306 int ret;
1307
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001308 if (!gspca_dev->sd_desc->set_jcomp)
1309 return -EINVAL;
Jim Parisf86d4a92008-12-10 05:50:44 -03001310 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1311 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001312 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1313 mutex_unlock(&gspca_dev->usb_lock);
1314 return ret;
1315}
1316
1317static int vidioc_g_parm(struct file *filp, void *priv,
1318 struct v4l2_streamparm *parm)
1319{
1320 struct gspca_dev *gspca_dev = priv;
1321
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001322 memset(parm, 0, sizeof *parm);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001323 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1324 parm->parm.capture.readbuffers = gspca_dev->nbufread;
Jim Paris627a5ef2008-12-10 06:02:42 -03001325
1326 if (gspca_dev->sd_desc->get_streamparm) {
1327 int ret;
1328
1329 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1330 return -ERESTARTSYS;
1331 ret = gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
1332 mutex_unlock(&gspca_dev->usb_lock);
1333 return ret;
1334 }
1335
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001336 return 0;
1337}
1338
1339static int vidioc_s_parm(struct file *filp, void *priv,
1340 struct v4l2_streamparm *parm)
1341{
1342 struct gspca_dev *gspca_dev = priv;
1343 int n;
1344
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001345 n = parm->parm.capture.readbuffers;
1346 if (n == 0 || n > GSPCA_MAX_FRAMES)
1347 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1348 else
1349 gspca_dev->nbufread = n;
Jim Paris627a5ef2008-12-10 06:02:42 -03001350
1351 if (gspca_dev->sd_desc->set_streamparm) {
1352 int ret;
1353
1354 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1355 return -ERESTARTSYS;
1356 ret = gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
1357 mutex_unlock(&gspca_dev->usb_lock);
1358 return ret;
1359 }
1360
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001361 return 0;
1362}
1363
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001364static int vidioc_s_std(struct file *filp, void *priv,
1365 v4l2_std_id *parm)
1366{
1367 return 0;
1368}
1369
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001370#ifdef CONFIG_VIDEO_V4L1_COMPAT
1371static int vidiocgmbuf(struct file *file, void *priv,
1372 struct video_mbuf *mbuf)
1373{
1374 struct gspca_dev *gspca_dev = file->private_data;
1375 int i;
1376
1377 PDEBUG(D_STREAM, "cgmbuf");
1378 if (gspca_dev->nframes == 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001379 int ret;
1380
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001381 {
1382 struct v4l2_format fmt;
1383
1384 memset(&fmt, 0, sizeof fmt);
1385 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1386 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1387 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1388 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1389 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001390 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001391 if (ret != 0)
1392 return ret;
1393 }
1394 {
1395 struct v4l2_requestbuffers rb;
1396
1397 memset(&rb, 0, sizeof rb);
1398 rb.count = 4;
1399 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1400 rb.memory = V4L2_MEMORY_MMAP;
1401 ret = vidioc_reqbufs(file, priv, &rb);
1402 if (ret != 0)
1403 return ret;
1404 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001405 }
1406 mbuf->frames = gspca_dev->nframes;
1407 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1408 for (i = 0; i < mbuf->frames; i++)
1409 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1410 return 0;
1411}
1412#endif
1413
1414static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1415{
1416 struct gspca_dev *gspca_dev = file->private_data;
Harvey Harrisonefab8212008-07-05 06:12:47 -03001417 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001418 struct page *page;
1419 unsigned long addr, start, size;
1420 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001421
1422 start = vma->vm_start;
1423 size = vma->vm_end - vma->vm_start;
1424 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1425
Hans de Goedee2997a72008-04-23 08:09:12 -03001426 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1427 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001428 if (!gspca_dev->present) {
1429 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001430 goto out;
1431 }
1432 if (gspca_dev->capt_file != file) {
1433 ret = -EINVAL;
1434 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001435 }
1436
Harvey Harrisonefab8212008-07-05 06:12:47 -03001437 frame = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001438 for (i = 0; i < gspca_dev->nframes; ++i) {
1439 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1440 PDEBUG(D_STREAM, "mmap bad memory type");
1441 break;
1442 }
1443 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1444 == vma->vm_pgoff) {
1445 frame = &gspca_dev->frame[i];
1446 break;
1447 }
1448 }
Harvey Harrisonefab8212008-07-05 06:12:47 -03001449 if (frame == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001450 PDEBUG(D_STREAM, "mmap no frame buffer found");
1451 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001452 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001453 }
1454#ifdef CONFIG_VIDEO_V4L1_COMPAT
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001455 /* v4l1 maps all the buffers */
1456 if (i != 0
1457 || size != frame->v4l2_buf.length * gspca_dev->nframes)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001458#endif
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001459 if (size != frame->v4l2_buf.length) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001460 PDEBUG(D_STREAM, "mmap bad size");
1461 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001462 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001463 }
1464
1465 /*
1466 * - VM_IO marks the area as being a mmaped region for I/O to a
1467 * device. It also prevents the region from being core dumped.
1468 */
1469 vma->vm_flags |= VM_IO;
1470
1471 addr = (unsigned long) frame->data;
1472 while (size > 0) {
1473 page = vmalloc_to_page((void *) addr);
1474 ret = vm_insert_page(vma, start, page);
1475 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001476 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001477 start += PAGE_SIZE;
1478 addr += PAGE_SIZE;
1479 size -= PAGE_SIZE;
1480 }
1481
1482 vma->vm_ops = &gspca_vm_ops;
1483 vma->vm_private_data = frame;
1484 gspca_vm_open(vma);
Hans de Goedee2997a72008-04-23 08:09:12 -03001485 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001486out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001487 mutex_unlock(&gspca_dev->queue_lock);
1488 return ret;
1489}
1490
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001491/*
1492 * wait for a video frame
1493 *
1494 * If a frame is ready, its index is returned.
1495 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001496static int frame_wait(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001497 int nonblock_ing)
1498{
1499 struct gspca_frame *frame;
1500 int i, j, ret;
1501
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001502 /* check if a frame is ready */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001503 i = gspca_dev->fr_o;
1504 j = gspca_dev->fr_queue[i];
1505 frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001506
Hans de Goede181b7042008-11-19 17:16:26 -03001507 if (!(frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)) {
1508 if (nonblock_ing)
1509 return -EAGAIN;
1510
1511 /* wait till a frame is ready */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001512 ret = wait_event_interruptible_timeout(gspca_dev->wq,
Hans de Goede181b7042008-11-19 17:16:26 -03001513 (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) ||
1514 !gspca_dev->streaming || !gspca_dev->present,
1515 msecs_to_jiffies(3000));
1516 if (ret < 0)
1517 return ret;
1518 if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present)
Hans de Goedee2997a72008-04-23 08:09:12 -03001519 return -EIO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001520 }
Hans de Goede181b7042008-11-19 17:16:26 -03001521
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001522 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1523 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1524 gspca_dev->fr_q,
1525 gspca_dev->fr_i,
1526 gspca_dev->fr_o);
Hans de Goedee2997a72008-04-23 08:09:12 -03001527
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001528 if (gspca_dev->sd_desc->dq_callback) {
1529 mutex_lock(&gspca_dev->usb_lock);
Hans de Goedee2997a72008-04-23 08:09:12 -03001530 gspca_dev->sd_desc->dq_callback(gspca_dev);
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001531 mutex_unlock(&gspca_dev->usb_lock);
1532 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001533 return j;
1534}
1535
1536/*
1537 * dequeue a video buffer
1538 *
1539 * If nonblock_ing is false, block until a buffer is available.
1540 */
1541static int vidioc_dqbuf(struct file *file, void *priv,
1542 struct v4l2_buffer *v4l2_buf)
1543{
1544 struct gspca_dev *gspca_dev = priv;
1545 struct gspca_frame *frame;
1546 int i, ret;
1547
1548 PDEBUG(D_FRAM, "dqbuf");
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001549 if (v4l2_buf->memory != gspca_dev->memory)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001550 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001551
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001552 /* if not streaming, be sure the application will not loop forever */
1553 if (!(file->f_flags & O_NONBLOCK)
1554 && !gspca_dev->streaming && gspca_dev->users == 1)
1555 return -EINVAL;
1556
1557 /* only the capturing file may dequeue */
1558 if (gspca_dev->capt_file != file)
1559 return -EINVAL;
1560
1561 /* only one dequeue / read at a time */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001562 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1563 return -ERESTARTSYS;
1564
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001565 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001566 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001567 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001568 i = ret; /* frame index */
1569 frame = &gspca_dev->frame[i];
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001570 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001571 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001572 frame->data,
1573 frame->v4l2_buf.bytesused)) {
1574 PDEBUG(D_ERR|D_STREAM,
1575 "dqbuf cp to user failed");
1576 ret = -EFAULT;
1577 goto out;
1578 }
1579 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001580 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1581 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1582 PDEBUG(D_FRAM, "dqbuf %d", i);
1583 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001584out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001585 mutex_unlock(&gspca_dev->read_lock);
1586 return ret;
1587}
1588
1589/*
1590 * queue a video buffer
1591 *
1592 * Attempting to queue a buffer that has already been
1593 * queued will return -EINVAL.
1594 */
1595static int vidioc_qbuf(struct file *file, void *priv,
1596 struct v4l2_buffer *v4l2_buf)
1597{
1598 struct gspca_dev *gspca_dev = priv;
1599 struct gspca_frame *frame;
1600 int i, index, ret;
1601
1602 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001603
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001604 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1605 return -ERESTARTSYS;
1606
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001607 index = v4l2_buf->index;
1608 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001609 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001610 "qbuf idx %d >= %d", index, gspca_dev->nframes);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001611 ret = -EINVAL;
1612 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001613 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001614 if (v4l2_buf->memory != gspca_dev->memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001615 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001616 ret = -EINVAL;
1617 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001618 }
1619
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001620 frame = &gspca_dev->frame[index];
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001621 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1622 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001623 ret = -EINVAL;
1624 goto out;
1625 }
1626
1627 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001628
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001629 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001630 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1631 frame->v4l2_buf.length = v4l2_buf->length;
1632 }
1633
1634 /* put the buffer in the 'queued' queue */
1635 i = gspca_dev->fr_q;
1636 gspca_dev->fr_queue[i] = index;
1637 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1638 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1639 gspca_dev->fr_q,
1640 gspca_dev->fr_i,
1641 gspca_dev->fr_o);
1642
1643 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1644 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1645 ret = 0;
1646out:
1647 mutex_unlock(&gspca_dev->queue_lock);
1648 return ret;
1649}
1650
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001651/*
1652 * allocate the resources for read()
1653 */
1654static int read_alloc(struct gspca_dev *gspca_dev,
1655 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001656{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001657 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001658 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001659
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001660 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001661 if (gspca_dev->nframes == 0) {
1662 struct v4l2_requestbuffers rb;
1663
1664 memset(&rb, 0, sizeof rb);
1665 rb.count = gspca_dev->nbufread;
1666 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001667 rb.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001668 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1669 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001670 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001671 return ret;
1672 }
1673 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1674 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001675 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001676 for (i = 0; i < gspca_dev->nbufread; i++) {
1677 v4l2_buf.index = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001678 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1679 if (ret != 0) {
1680 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1681 return ret;
1682 }
1683 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001684 gspca_dev->memory = GSPCA_MEMORY_READ;
1685 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001686
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001687 /* start streaming */
1688 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1689 if (ret != 0)
1690 PDEBUG(D_STREAM, "read streamon err %d", ret);
1691 return ret;
1692}
1693
1694static unsigned int dev_poll(struct file *file, poll_table *wait)
1695{
1696 struct gspca_dev *gspca_dev = file->private_data;
1697 int i, ret;
1698
1699 PDEBUG(D_FRAM, "poll");
1700
1701 poll_wait(file, &gspca_dev->wq, wait);
1702 if (!gspca_dev->present)
1703 return POLLERR;
1704
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001705 /* if reqbufs is not done, the user would use read() */
1706 if (gspca_dev->nframes == 0) {
1707 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1708 return POLLERR; /* not the 1st time */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001709 ret = read_alloc(gspca_dev, file);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001710 if (ret != 0)
1711 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001712 }
1713
1714 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1715 return POLLERR;
1716 if (!gspca_dev->present) {
1717 ret = POLLERR;
1718 goto out;
1719 }
1720
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001721 /* check the next incoming buffer */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001722 i = gspca_dev->fr_o;
1723 i = gspca_dev->fr_queue[i];
1724 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1725 ret = POLLIN | POLLRDNORM; /* something to read */
1726 else
1727 ret = 0;
1728out:
1729 mutex_unlock(&gspca_dev->queue_lock);
1730 return ret;
1731}
1732
1733static ssize_t dev_read(struct file *file, char __user *data,
1734 size_t count, loff_t *ppos)
1735{
1736 struct gspca_dev *gspca_dev = file->private_data;
1737 struct gspca_frame *frame;
1738 struct v4l2_buffer v4l2_buf;
1739 struct timeval timestamp;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001740 int n, ret, ret2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001741
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001742 PDEBUG(D_FRAM, "read (%zd)", count);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001743 if (!gspca_dev->present)
1744 return -ENODEV;
1745 switch (gspca_dev->memory) {
1746 case GSPCA_MEMORY_NO: /* first time */
1747 ret = read_alloc(gspca_dev, file);
1748 if (ret != 0)
1749 return ret;
1750 break;
1751 case GSPCA_MEMORY_READ:
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001752 if (gspca_dev->capt_file == file)
1753 break;
1754 /* fall thru */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001755 default:
1756 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001757 }
1758
1759 /* get a frame */
1760 jiffies_to_timeval(get_jiffies_64(), &timestamp);
1761 timestamp.tv_sec--;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001762 n = 2;
1763 for (;;) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001764 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1765 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001766 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001767 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1768 if (ret != 0) {
1769 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1770 return ret;
1771 }
1772
1773 /* if the process slept for more than 1 second,
Frank Zago219423c2008-09-26 07:43:54 -03001774 * get a newer frame */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001775 frame = &gspca_dev->frame[v4l2_buf.index];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001776 if (--n < 0)
1777 break; /* avoid infinite loop */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001778 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1779 break;
1780 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1781 if (ret != 0) {
1782 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1783 return ret;
1784 }
1785 }
1786
1787 /* copy the frame */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001788 if (count > frame->v4l2_buf.bytesused)
1789 count = frame->v4l2_buf.bytesused;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001790 ret = copy_to_user(data, frame->data, count);
1791 if (ret != 0) {
1792 PDEBUG(D_ERR|D_STREAM,
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001793 "read cp to user lack %d / %zd", ret, count);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001794 ret = -EFAULT;
1795 goto out;
1796 }
1797 ret = count;
1798out:
1799 /* in each case, requeue the buffer */
1800 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1801 if (ret2 != 0)
1802 return ret2;
1803 return ret;
1804}
1805
Hans Verkuilbec43662008-12-30 06:58:20 -03001806static struct v4l2_file_operations dev_fops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001807 .owner = THIS_MODULE,
1808 .open = dev_open,
1809 .release = dev_close,
1810 .read = dev_read,
1811 .mmap = dev_mmap,
Hans Verkuilbec43662008-12-30 06:58:20 -03001812 .unlocked_ioctl = video_ioctl2,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001813 .poll = dev_poll,
1814};
1815
Hans Verkuila3998102008-07-21 02:57:38 -03001816static const struct v4l2_ioctl_ops dev_ioctl_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001817 .vidioc_querycap = vidioc_querycap,
1818 .vidioc_dqbuf = vidioc_dqbuf,
1819 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001820 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1821 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1822 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1823 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001824 .vidioc_streamon = vidioc_streamon,
1825 .vidioc_queryctrl = vidioc_queryctrl,
1826 .vidioc_g_ctrl = vidioc_g_ctrl,
1827 .vidioc_s_ctrl = vidioc_s_ctrl,
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03001828 .vidioc_g_audio = vidioc_g_audio,
1829 .vidioc_s_audio = vidioc_s_audio,
1830 .vidioc_enumaudio = vidioc_enumaudio,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001831 .vidioc_querymenu = vidioc_querymenu,
1832 .vidioc_enum_input = vidioc_enum_input,
1833 .vidioc_g_input = vidioc_g_input,
1834 .vidioc_s_input = vidioc_s_input,
1835 .vidioc_reqbufs = vidioc_reqbufs,
1836 .vidioc_querybuf = vidioc_querybuf,
1837 .vidioc_streamoff = vidioc_streamoff,
1838 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1839 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1840 .vidioc_g_parm = vidioc_g_parm,
1841 .vidioc_s_parm = vidioc_s_parm,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001842 .vidioc_s_std = vidioc_s_std,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001843#ifdef CONFIG_VIDEO_V4L1_COMPAT
1844 .vidiocgmbuf = vidiocgmbuf,
1845#endif
1846};
1847
Hans Verkuila3998102008-07-21 02:57:38 -03001848static struct video_device gspca_template = {
1849 .name = "gspca main driver",
Hans Verkuila3998102008-07-21 02:57:38 -03001850 .fops = &dev_fops,
1851 .ioctl_ops = &dev_ioctl_ops,
Hans de Goeded0182302008-11-19 17:10:47 -03001852 .release = gspca_release,
Hans Verkuila3998102008-07-21 02:57:38 -03001853 .minor = -1,
1854};
1855
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001856/*
1857 * probe and create a new gspca device
1858 *
1859 * This function must be called by the sub-driver when it is
1860 * called for probing a new device.
1861 */
1862int gspca_dev_probe(struct usb_interface *intf,
1863 const struct usb_device_id *id,
1864 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001865 int dev_size,
1866 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001867{
1868 struct usb_interface_descriptor *interface;
1869 struct gspca_dev *gspca_dev;
1870 struct usb_device *dev = interface_to_usbdev(intf);
1871 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001872
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001873 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001874
1875 /* we don't handle multi-config cameras */
1876 if (dev->descriptor.bNumConfigurations != 1)
1877 return -ENODEV;
1878 interface = &intf->cur_altsetting->desc;
1879 if (interface->bInterfaceNumber > 0)
1880 return -ENODEV;
1881
1882 /* create the device */
1883 if (dev_size < sizeof *gspca_dev)
1884 dev_size = sizeof *gspca_dev;
1885 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
Erik Andrene293e592008-10-03 08:46:50 -03001886 if (!gspca_dev) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001887 err("couldn't kzalloc gspca struct");
Erik Andrene293e592008-10-03 08:46:50 -03001888 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001889 }
Jean-Francois Moine8295d992008-09-03 17:12:19 -03001890 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
1891 if (!gspca_dev->usb_buf) {
1892 err("out of memory");
Erik Andrene293e592008-10-03 08:46:50 -03001893 ret = -ENOMEM;
Jean-Francois Moine8295d992008-09-03 17:12:19 -03001894 goto out;
1895 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001896 gspca_dev->dev = dev;
1897 gspca_dev->iface = interface->bInterfaceNumber;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001898 gspca_dev->nbalt = intf->num_altsetting;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001899 gspca_dev->sd_desc = sd_desc;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001900 gspca_dev->nbufread = 2;
Jean-Francois Moineff374742008-10-23 07:29:51 -03001901 gspca_dev->empty_packet = -1; /* don't check the empty packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001902
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001903 /* configure the subdriver and initialize the USB device */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03001904 ret = sd_desc->config(gspca_dev, id);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001905 if (ret < 0)
1906 goto out;
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03001907 ret = sd_desc->init(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001908 if (ret < 0)
1909 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001910 ret = gspca_set_alt0(gspca_dev);
1911 if (ret < 0)
1912 goto out;
1913 gspca_set_default_mode(gspca_dev);
1914
1915 mutex_init(&gspca_dev->usb_lock);
1916 mutex_init(&gspca_dev->read_lock);
1917 mutex_init(&gspca_dev->queue_lock);
1918 init_waitqueue_head(&gspca_dev->wq);
1919
1920 /* init video stuff */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03001921 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
1922 gspca_dev->vdev.parent = &dev->dev;
Jean-Francois Moine5c4fa002008-11-18 15:52:31 -03001923 gspca_dev->module = module;
Oliver Neukumee56a4d2008-07-27 14:01:59 -03001924 gspca_dev->present = 1;
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001925 ret = video_register_device(&gspca_dev->vdev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001926 VFL_TYPE_GRABBER,
1927 video_nr);
1928 if (ret < 0) {
1929 err("video_register_device err %d", ret);
1930 goto out;
1931 }
1932
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001933 usb_set_intfdata(intf, gspca_dev);
1934 PDEBUG(D_PROBE, "probe ok");
1935 return 0;
1936out:
Jean-Francois Moinee07a1d82008-11-19 06:37:53 -03001937 kfree(gspca_dev->usb_buf);
1938 kfree(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001939 return ret;
1940}
1941EXPORT_SYMBOL(gspca_dev_probe);
1942
1943/*
1944 * USB disconnection
1945 *
1946 * This function must be called by the sub-driver
1947 * when the device disconnects, after the specific resources are freed.
1948 */
1949void gspca_disconnect(struct usb_interface *intf)
1950{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001951 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001952
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03001953 gspca_dev->present = 0;
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03001954
Frank Zago6b060ff2008-09-28 08:12:22 -03001955 usb_set_intfdata(intf, NULL);
1956
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001957 /* release the device */
1958 /* (this will call gspca_release() immediatly or on last close) */
1959 video_unregister_device(&gspca_dev->vdev);
Jean-Francois Moine3145b8c2008-10-05 04:22:10 -03001960
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001961 PDEBUG(D_PROBE, "disconnect complete");
1962}
1963EXPORT_SYMBOL(gspca_disconnect);
1964
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001965#ifdef CONFIG_PM
1966int gspca_suspend(struct usb_interface *intf, pm_message_t message)
1967{
1968 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1969
1970 if (!gspca_dev->streaming)
1971 return 0;
1972 gspca_dev->frozen = 1; /* avoid urb error messages */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001973 if (gspca_dev->sd_desc->stopN)
1974 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001975 destroy_urbs(gspca_dev);
1976 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001977 if (gspca_dev->sd_desc->stop0)
1978 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001979 return 0;
1980}
1981EXPORT_SYMBOL(gspca_suspend);
1982
1983int gspca_resume(struct usb_interface *intf)
1984{
1985 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1986
1987 gspca_dev->frozen = 0;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001988 gspca_dev->sd_desc->init(gspca_dev);
1989 if (gspca_dev->streaming)
1990 return gspca_init_transfer(gspca_dev);
Jean-Francois Moine1e89e2d2008-09-03 17:12:15 -03001991 return 0;
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001992}
1993EXPORT_SYMBOL(gspca_resume);
1994#endif
Hans de Goededcef3232008-07-10 10:40:53 -03001995/* -- cam driver utility functions -- */
1996
1997/* auto gain and exposure algorithm based on the knee algorithm described here:
1998 http://ytse.tricolour.net/docs/LowLightOptimization.html
1999
2000 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2001 where changed. */
2002int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2003 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2004{
2005 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2006 const struct ctrl *gain_ctrl = NULL;
2007 const struct ctrl *exposure_ctrl = NULL;
2008 const struct ctrl *autogain_ctrl = NULL;
2009 int retval = 0;
2010
2011 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
2012 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2013 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2014 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2015 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2016 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2017 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2018 }
2019 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2020 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2021 "on cam without (auto)gain/exposure");
2022 return 0;
2023 }
2024
2025 if (gain_ctrl->get(gspca_dev, &gain) ||
2026 exposure_ctrl->get(gspca_dev, &exposure) ||
2027 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2028 return 0;
2029
2030 orig_gain = gain;
2031 orig_exposure = exposure;
2032
2033 /* If we are of a multiple of deadzone, do multiple steps to reach the
2034 desired lumination fast (with the risc of a slight overshoot) */
2035 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2036
Jean-Francois Moine1c44d812008-11-10 05:56:55 -03002037 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
Hans de Goededcef3232008-07-10 10:40:53 -03002038 avg_lum, desired_avg_lum, steps);
2039
2040 for (i = 0; i < steps; i++) {
2041 if (avg_lum > desired_avg_lum) {
2042 if (gain > gain_knee)
2043 gain--;
2044 else if (exposure > exposure_knee)
2045 exposure--;
2046 else if (gain > gain_ctrl->qctrl.default_value)
2047 gain--;
2048 else if (exposure > exposure_ctrl->qctrl.minimum)
2049 exposure--;
2050 else if (gain > gain_ctrl->qctrl.minimum)
2051 gain--;
2052 else
2053 break;
2054 } else {
2055 if (gain < gain_ctrl->qctrl.default_value)
2056 gain++;
2057 else if (exposure < exposure_knee)
2058 exposure++;
2059 else if (gain < gain_knee)
2060 gain++;
2061 else if (exposure < exposure_ctrl->qctrl.maximum)
2062 exposure++;
2063 else if (gain < gain_ctrl->qctrl.maximum)
2064 gain++;
2065 else
2066 break;
2067 }
2068 }
2069
2070 if (gain != orig_gain) {
2071 gain_ctrl->set(gspca_dev, gain);
2072 retval = 1;
2073 }
2074 if (exposure != orig_exposure) {
2075 exposure_ctrl->set(gspca_dev, exposure);
2076 retval = 1;
2077 }
2078
2079 return retval;
2080}
2081EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2082
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002083/* -- module insert / remove -- */
2084static int __init gspca_init(void)
2085{
Jean-Francois Moinebb64e862008-07-19 06:49:28 -03002086 info("main v%d.%d.%d registered",
2087 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2088 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2089 DRIVER_VERSION_NUMBER & 0xff);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002090 return 0;
2091}
2092static void __exit gspca_exit(void)
2093{
2094 info("main deregistered");
2095}
2096
2097module_init(gspca_init);
2098module_exit(gspca_exit);
2099
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03002100#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002101module_param_named(debug, gspca_debug, int, 0644);
2102MODULE_PARM_DESC(debug,
2103 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002104 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
2105 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002106#endif