blob: 3298e33c7822722f9894af88337832632e77a313 [file] [log] [blame]
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001/*
2 * Main USB camera driver
3 *
Jean-Francois Moine11bd1992009-04-21 04:17:59 -03004 * Copyright (C) 2008-2009 Jean-Francois Moine (http://moinejf.free.fr)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#define MODULE_NAME "gspca"
22
23#include <linux/init.h>
Jean-Francois Moinecf0fe082008-10-17 04:36:47 -030024#include <linux/version.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030025#include <linux/fs.h>
26#include <linux/vmalloc.h>
27#include <linux/sched.h>
28#include <linux/slab.h>
29#include <linux/mm.h>
30#include <linux/string.h>
31#include <linux/pagemap.h>
Jean-Francois Moine956e42d2008-07-01 10:03:42 -030032#include <linux/io.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030033#include <asm/page.h>
Jean-Francois Moine956e42d2008-07-01 10:03:42 -030034#include <linux/uaccess.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030035#include <linux/jiffies.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030036#include <media/v4l2-ioctl.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030037
38#include "gspca.h"
39
Jean-Francois Moined43fa322008-06-12 10:58:58 -030040/* global values */
Jean-Francois Moine8fee8452009-01-17 04:46:38 -030041#define DEF_NURBS 3 /* default number of URBs */
42#if DEF_NURBS > MAX_NURBS
43#error "DEF_NURBS too big"
44#endif
Jean-Francois Moined43fa322008-06-12 10:58:58 -030045
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030046MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
47MODULE_DESCRIPTION("GSPCA USB Camera Driver");
48MODULE_LICENSE("GPL");
49
Jean-Francois Moinea0d64452009-07-07 04:13:23 -030050#define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 7, 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030051
Jean-Francois Moine335b3f82008-07-30 04:53:02 -030052#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030053int gspca_debug = D_ERR | D_PROBE;
54EXPORT_SYMBOL(gspca_debug);
55
56static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
57{
58 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
59 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
60 txt,
61 pixfmt & 0xff,
62 (pixfmt >> 8) & 0xff,
63 (pixfmt >> 16) & 0xff,
64 pixfmt >> 24,
65 w, h);
66 } else {
67 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
68 txt,
69 pixfmt,
70 w, h);
71 }
72}
73#else
74#define PDEBUG_MODE(txt, pixfmt, w, h)
75#endif
76
Jean-Francois Moined43fa322008-06-12 10:58:58 -030077/* specific memory types - !! should different from V4L2_MEMORY_xxx */
78#define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
79#define GSPCA_MEMORY_READ 7
80
Jean-Francois Moined43fa322008-06-12 10:58:58 -030081#define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
Jean-Francois Moined43fa322008-06-12 10:58:58 -030082
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030083/*
84 * VMA operations.
85 */
86static void gspca_vm_open(struct vm_area_struct *vma)
87{
88 struct gspca_frame *frame = vma->vm_private_data;
89
90 frame->vma_use_count++;
91 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
92}
93
94static void gspca_vm_close(struct vm_area_struct *vma)
95{
96 struct gspca_frame *frame = vma->vm_private_data;
97
98 if (--frame->vma_use_count <= 0)
99 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
100}
101
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400102static const struct vm_operations_struct gspca_vm_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300103 .open = gspca_vm_open,
104 .close = gspca_vm_close,
105};
106
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300107/* get the current input frame buffer */
108struct gspca_frame *gspca_get_i_frame(struct gspca_dev *gspca_dev)
109{
110 struct gspca_frame *frame;
111 int i;
112
113 i = gspca_dev->fr_i;
114 i = gspca_dev->fr_queue[i];
115 frame = &gspca_dev->frame[i];
116 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
117 != V4L2_BUF_FLAG_QUEUED)
118 return NULL;
119 return frame;
120}
121EXPORT_SYMBOL(gspca_get_i_frame);
122
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300123/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300124 * fill a video frame from an URB and resubmit
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300125 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300126static void fill_frame(struct gspca_dev *gspca_dev,
127 struct urb *urb)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300128{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300129 struct gspca_frame *frame;
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300130 u8 *data; /* address of data in the iso message */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300131 int i, len, st;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300132 cam_pkt_op pkt_scan;
133
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300134 if (urb->status != 0) {
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300135 if (urb->status == -ESHUTDOWN)
136 return; /* disconnection */
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300137#ifdef CONFIG_PM
Jean-Francois Moine6a709742008-09-03 16:48:10 -0300138 if (!gspca_dev->frozen)
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300139#endif
Jean-Francois Moine6a709742008-09-03 16:48:10 -0300140 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Hans de Goedebf926ad2009-10-23 06:56:06 -0300141 goto resubmit;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300142 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300143 pkt_scan = gspca_dev->sd_desc->pkt_scan;
144 for (i = 0; i < urb->number_of_packets; i++) {
145
146 /* check the availability of the frame buffer */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300147 frame = gspca_get_i_frame(gspca_dev);
148 if (!frame) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300149 gspca_dev->last_packet_type = DISCARD_PACKET;
150 break;
151 }
152
153 /* check the packet status and length */
154 len = urb->iso_frame_desc[i].actual_length;
Jean-Francois Moineff374742008-10-23 07:29:51 -0300155 if (len == 0) {
156 if (gspca_dev->empty_packet == 0)
157 gspca_dev->empty_packet = 1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300158 continue;
Jean-Francois Moineff374742008-10-23 07:29:51 -0300159 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300160 st = urb->iso_frame_desc[i].status;
161 if (st) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300162 PDEBUG(D_ERR,
163 "ISOC data error: [%d] len=%d, status=%d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300164 i, len, st);
165 gspca_dev->last_packet_type = DISCARD_PACKET;
166 continue;
167 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300168
169 /* let the packet be analyzed by the subdriver */
170 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
171 i, urb->iso_frame_desc[i].offset, len);
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300172 data = (u8 *) urb->transfer_buffer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300173 + urb->iso_frame_desc[i].offset;
174 pkt_scan(gspca_dev, frame, data, len);
175 }
176
Hans de Goedebf926ad2009-10-23 06:56:06 -0300177resubmit:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300178 /* resubmit the URB */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300179 st = usb_submit_urb(urb, GFP_ATOMIC);
180 if (st < 0)
181 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
182}
183
184/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300185 * ISOC message interrupt from the USB device
186 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300187 * Analyse each packet and call the subdriver for copy to the frame buffer.
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300188 */
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300189static void isoc_irq(struct urb *urb)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300190{
191 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
192
Hans de Goede3647fea2008-07-15 05:36:30 -0300193 PDEBUG(D_PACK, "isoc irq");
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300194 if (!gspca_dev->streaming)
195 return;
196 fill_frame(gspca_dev, urb);
197}
198
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300199/*
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300200 * bulk message interrupt from the USB device
201 */
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300202static void bulk_irq(struct urb *urb)
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300203{
204 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
205 struct gspca_frame *frame;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300206 int st;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300207
208 PDEBUG(D_PACK, "bulk irq");
209 if (!gspca_dev->streaming)
210 return;
Jean-Francois Moine880ae242008-11-18 04:19:52 -0300211 switch (urb->status) {
212 case 0:
213 break;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300214 case -ESHUTDOWN:
215 return; /* disconnection */
Jean-Francois Moine880ae242008-11-18 04:19:52 -0300216 case -ECONNRESET:
217 urb->status = 0;
218 break;
219 default:
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300220#ifdef CONFIG_PM
221 if (!gspca_dev->frozen)
222#endif
223 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Hans de Goedebf926ad2009-10-23 06:56:06 -0300224 goto resubmit;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300225 }
226
227 /* check the availability of the frame buffer */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300228 frame = gspca_get_i_frame(gspca_dev);
229 if (!frame) {
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300230 gspca_dev->last_packet_type = DISCARD_PACKET;
231 } else {
232 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
233 gspca_dev->sd_desc->pkt_scan(gspca_dev,
234 frame,
235 urb->transfer_buffer,
236 urb->actual_length);
237 }
Antonio Ospitedff369a2008-11-14 07:53:32 -0300238
Hans de Goedebf926ad2009-10-23 06:56:06 -0300239resubmit:
Antonio Ospitedff369a2008-11-14 07:53:32 -0300240 /* resubmit the URB */
241 if (gspca_dev->cam.bulk_nurbs != 0) {
242 st = usb_submit_urb(urb, GFP_ATOMIC);
243 if (st < 0)
244 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
245 }
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300246}
247
248/*
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300249 * add data to the current frame
250 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300251 * This function is called by the subdrivers at interrupt level.
252 *
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300253 * To build a frame, these ones must add
254 * - one FIRST_PACKET
255 * - 0 or many INTER_PACKETs
256 * - one LAST_PACKET
257 * DISCARD_PACKET invalidates the whole frame.
258 * On LAST_PACKET, a new frame is returned.
259 */
260struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
Erik Andrene293e592008-10-03 08:46:50 -0300261 enum gspca_packet_type packet_type,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300262 struct gspca_frame *frame,
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300263 const __u8 *data,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300264 int len)
265{
266 int i, j;
267
Hans de Goedee2997a72008-04-23 08:09:12 -0300268 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300269
270 /* when start of a new frame, if the current frame buffer
271 * is not queued, discard the whole frame */
272 if (packet_type == FIRST_PACKET) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300273 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
274 != V4L2_BUF_FLAG_QUEUED) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300275 gspca_dev->last_packet_type = DISCARD_PACKET;
276 return frame;
277 }
278 frame->data_end = frame->data;
279 jiffies_to_timeval(get_jiffies_64(),
280 &frame->v4l2_buf.timestamp);
281 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300282 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
Jean-Francois Moine06ca78f2008-07-22 03:45:08 -0300283 if (packet_type == LAST_PACKET)
284 gspca_dev->last_packet_type = packet_type;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300285 return frame;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300286 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300287
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300288 /* append the packet to the frame buffer */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300289 if (len > 0) {
290 if (frame->data_end - frame->data + len
291 > frame->v4l2_buf.length) {
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -0300292 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300293 frame->data_end - frame->data + len,
294 frame->v4l2_buf.length);
295 packet_type = DISCARD_PACKET;
296 } else {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300297 memcpy(frame->data_end, data, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300298 frame->data_end += len;
299 }
300 }
301 gspca_dev->last_packet_type = packet_type;
302
Frank Zago219423c2008-09-26 07:43:54 -0300303 /* if last packet, wake up the application and advance in the queue */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300304 if (packet_type == LAST_PACKET) {
305 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
306 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
307 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300308 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300309 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
310 gspca_dev->fr_i = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300311 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
312 frame->v4l2_buf.bytesused,
313 gspca_dev->fr_q,
314 i,
315 gspca_dev->fr_o);
316 j = gspca_dev->fr_queue[i];
317 frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300318 }
319 return frame;
320}
321EXPORT_SYMBOL(gspca_frame_add);
322
323static int gspca_is_compressed(__u32 format)
324{
325 switch (format) {
326 case V4L2_PIX_FMT_MJPEG:
327 case V4L2_PIX_FMT_JPEG:
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300328 case V4L2_PIX_FMT_SPCA561:
Hans de Goedeab8f12c2008-07-04 18:29:32 -0300329 case V4L2_PIX_FMT_PAC207:
Kyle Guinnd661e622009-01-16 05:36:14 -0300330 case V4L2_PIX_FMT_MR97310A:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300331 return 1;
332 }
333 return 0;
334}
335
336static void *rvmalloc(unsigned long size)
337{
338 void *mem;
339 unsigned long adr;
340
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300341 mem = vmalloc_32(size);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300342 if (mem != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300343 adr = (unsigned long) mem;
344 while ((long) size > 0) {
345 SetPageReserved(vmalloc_to_page((void *) adr));
346 adr += PAGE_SIZE;
347 size -= PAGE_SIZE;
348 }
349 }
350 return mem;
351}
352
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300353static void rvfree(void *mem, long size)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300354{
355 unsigned long adr;
356
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300357 adr = (unsigned long) mem;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300358 while (size > 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300359 ClearPageReserved(vmalloc_to_page((void *) adr));
360 adr += PAGE_SIZE;
361 size -= PAGE_SIZE;
362 }
363 vfree(mem);
364}
365
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300366static int frame_alloc(struct gspca_dev *gspca_dev,
367 unsigned int count)
368{
369 struct gspca_frame *frame;
370 unsigned int frsz;
371 int i;
372
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300373 i = gspca_dev->curr_mode;
374 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300375 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300376 frsz = PAGE_ALIGN(frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300377 gspca_dev->frsz = frsz;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300378 if (count > GSPCA_MAX_FRAMES)
379 count = GSPCA_MAX_FRAMES;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300380 gspca_dev->frbuf = rvmalloc(frsz * count);
381 if (!gspca_dev->frbuf) {
382 err("frame alloc failed");
383 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300384 }
385 gspca_dev->nframes = count;
386 for (i = 0; i < count; i++) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300387 frame = &gspca_dev->frame[i];
388 frame->v4l2_buf.index = i;
389 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
390 frame->v4l2_buf.flags = 0;
391 frame->v4l2_buf.field = V4L2_FIELD_NONE;
392 frame->v4l2_buf.length = frsz;
393 frame->v4l2_buf.memory = gspca_dev->memory;
394 frame->v4l2_buf.sequence = 0;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300395 frame->data = frame->data_end =
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300396 gspca_dev->frbuf + i * frsz;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300397 frame->v4l2_buf.m.offset = i * frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300398 }
399 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
400 gspca_dev->last_packet_type = DISCARD_PACKET;
401 gspca_dev->sequence = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300402 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300403}
404
405static void frame_free(struct gspca_dev *gspca_dev)
406{
407 int i;
408
409 PDEBUG(D_STREAM, "frame free");
Harvey Harrisonefab8212008-07-05 06:12:47 -0300410 if (gspca_dev->frbuf != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300411 rvfree(gspca_dev->frbuf,
412 gspca_dev->nframes * gspca_dev->frsz);
413 gspca_dev->frbuf = NULL;
414 for (i = 0; i < gspca_dev->nframes; i++)
415 gspca_dev->frame[i].data = NULL;
416 }
417 gspca_dev->nframes = 0;
418}
419
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300420static void destroy_urbs(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300421{
422 struct urb *urb;
423 unsigned int i;
424
425 PDEBUG(D_STREAM, "kill transfer");
Erik Andrén44732812008-10-13 15:52:46 -0300426 for (i = 0; i < MAX_NURBS; i++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300427 urb = gspca_dev->urb[i];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300428 if (urb == NULL)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300429 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300430
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300431 gspca_dev->urb[i] = NULL;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300432 usb_kill_urb(urb);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300433 if (urb->transfer_buffer != NULL)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300434 usb_buffer_free(gspca_dev->dev,
435 urb->transfer_buffer_length,
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300436 urb->transfer_buffer,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300437 urb->transfer_dma);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300438 usb_free_urb(urb);
439 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300440}
441
442/*
Frank Zago219423c2008-09-26 07:43:54 -0300443 * look for an input transfer endpoint in an alternate setting
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300444 */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300445static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300446 int xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300447{
448 struct usb_host_endpoint *ep;
449 int i, attr;
450
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300451 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
452 ep = &alt->endpoint[i];
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300453 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Jean-Francois Moine30d35e42009-04-21 13:57:31 -0300454 if (attr == xfer
455 && ep->desc.wMaxPacketSize != 0)
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300456 return ep;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300457 }
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 Moine0be010042008-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;
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300473 int xfer, i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300474
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 Moine6929dc62009-04-21 13:45:56 -0300477 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
478 : USB_ENDPOINT_XFER_ISOC;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300479 i = gspca_dev->alt; /* previous alt setting */
Hans de Goedea511ba92009-10-16 07:13:07 -0300480 if (gspca_dev->cam.reverse_alts) {
481 while (++i < gspca_dev->nbalt) {
482 ep = alt_xfer(&intf->altsetting[i], xfer);
483 if (ep)
484 break;
485 }
486 } else {
487 while (--i >= 0) {
488 ep = alt_xfer(&intf->altsetting[i], xfer);
489 if (ep)
490 break;
491 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300492 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300493 if (ep == NULL) {
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300494 err("no transfer endpoint found");
495 return NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300496 }
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300497 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300498 i, ep->desc.bEndpointAddress);
Jean-Francois Moine1c8336d2009-07-28 04:38:25 -0300499 gspca_dev->alt = i; /* memorize the current alt setting */
Jean-Francois Moine8dd07ef2009-04-21 14:05:44 -0300500 if (gspca_dev->nbalt > 1) {
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300501 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
502 if (ret < 0) {
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300503 err("set alt %d err %d", i, ret);
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300504 return NULL;
505 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300506 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300507 return ep;
508}
509
510/*
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300511 * create the URBs for image transfer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300512 */
513static int create_urbs(struct gspca_dev *gspca_dev,
514 struct usb_host_endpoint *ep)
515{
516 struct urb *urb;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300517 int n, nurbs, i, psize, npkt, bsize;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300518
519 /* calculate the packet size and the number of packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300520 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300521
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300522 if (!gspca_dev->cam.bulk) { /* isoc */
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300523
524 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300525 if (gspca_dev->pkt_size == 0)
526 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
527 else
528 psize = gspca_dev->pkt_size;
Jean-Francois Moine49cb6b02009-04-25 13:29:01 -0300529 npkt = gspca_dev->cam.npkt;
530 if (npkt == 0)
531 npkt = 32; /* default value */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300532 bsize = psize * npkt;
533 PDEBUG(D_STREAM,
534 "isoc %d pkts size %d = bsize:%d",
535 npkt, psize, bsize);
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300536 nurbs = DEF_NURBS;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300537 } else { /* bulk */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300538 npkt = 0;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300539 bsize = gspca_dev->cam.bulk_size;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300540 if (bsize == 0)
541 bsize = psize;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300542 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300543 if (gspca_dev->cam.bulk_nurbs != 0)
544 nurbs = gspca_dev->cam.bulk_nurbs;
545 else
546 nurbs = 1;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300547 }
548
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300549 gspca_dev->nurbs = nurbs;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300550 for (n = 0; n < nurbs; n++) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300551 urb = usb_alloc_urb(npkt, GFP_KERNEL);
552 if (!urb) {
553 err("usb_alloc_urb failed");
Jean-Francois Moine0d0548e2008-09-05 12:49:54 -0300554 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300555 return -ENOMEM;
556 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300557 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300558 bsize,
559 GFP_KERNEL,
560 &urb->transfer_dma);
561
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300562 if (urb->transfer_buffer == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300563 usb_free_urb(urb);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300564 err("usb_buffer_urb failed");
Jean-Francois Moine0d0548e2008-09-05 12:49:54 -0300565 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300566 return -ENOMEM;
567 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300568 gspca_dev->urb[n] = urb;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300569 urb->dev = gspca_dev->dev;
570 urb->context = gspca_dev;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300571 urb->transfer_buffer_length = bsize;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300572 if (npkt != 0) { /* ISOC */
573 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
574 ep->desc.bEndpointAddress);
Frank Zago6c49da72008-09-28 07:43:00 -0300575 urb->transfer_flags = URB_ISO_ASAP
576 | URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300577 urb->interval = ep->desc.bInterval;
578 urb->complete = isoc_irq;
579 urb->number_of_packets = npkt;
580 for (i = 0; i < npkt; i++) {
581 urb->iso_frame_desc[i].length = psize;
582 urb->iso_frame_desc[i].offset = psize * i;
583 }
584 } else { /* bulk */
585 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
586 ep->desc.bEndpointAddress),
Frank Zago6c49da72008-09-28 07:43:00 -0300587 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300588 urb->complete = bulk_irq;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300589 }
590 }
591 return 0;
592}
593
594/*
595 * start the USB transfer
596 */
597static int gspca_init_transfer(struct gspca_dev *gspca_dev)
598{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300599 struct usb_host_endpoint *ep;
600 int n, ret;
601
Hans de Goedee2997a72008-04-23 08:09:12 -0300602 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
603 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300604
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300605 if (!gspca_dev->present) {
606 ret = -ENODEV;
607 goto out;
608 }
609
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300610 /* set the higher alternate setting and
611 * loop until urb submit succeeds */
Hans de Goedea511ba92009-10-16 07:13:07 -0300612 if (gspca_dev->cam.reverse_alts)
613 gspca_dev->alt = 0;
614 else
615 gspca_dev->alt = gspca_dev->nbalt;
616
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300617 if (gspca_dev->sd_desc->isoc_init) {
618 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
619 if (ret < 0)
620 goto out;
621 }
622 ep = get_ep(gspca_dev);
623 if (ep == NULL) {
624 ret = -EIO;
625 goto out;
626 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300627 for (;;) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300628 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300629 ret = create_urbs(gspca_dev, ep);
630 if (ret < 0)
631 goto out;
632
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300633 /* clear the bulk endpoint */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300634 if (gspca_dev->cam.bulk)
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300635 usb_clear_halt(gspca_dev->dev,
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300636 gspca_dev->urb[0]->pipe);
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300637
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300638 /* start the cam */
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300639 ret = gspca_dev->sd_desc->start(gspca_dev);
640 if (ret < 0) {
641 destroy_urbs(gspca_dev);
642 goto out;
643 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300644 gspca_dev->streaming = 1;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300645
Antonio Ospitedff369a2008-11-14 07:53:32 -0300646 /* some bulk transfers are started by the subdriver */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300647 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300648 break;
649
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300650 /* submit the URBs */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300651 for (n = 0; n < gspca_dev->nurbs; n++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300652 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300653 if (ret < 0)
654 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300655 }
656 if (ret >= 0)
657 break;
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300658 PDEBUG(D_ERR|D_STREAM,
659 "usb_submit_urb alt %d err %d", gspca_dev->alt, ret);
660 gspca_dev->streaming = 0;
661 destroy_urbs(gspca_dev);
662 if (ret != -ENOSPC)
663 goto out;
664
665 /* the bandwidth is not wide enough
666 * negociate or try a lower alternate setting */
667 msleep(20); /* wait for kill complete */
668 if (gspca_dev->sd_desc->isoc_nego) {
669 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
670 if (ret < 0)
671 goto out;
672 } else {
673 ep = get_ep(gspca_dev);
674 if (ep == NULL) {
675 ret = -EIO;
676 goto out;
677 }
678 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300679 }
680out:
681 mutex_unlock(&gspca_dev->usb_lock);
682 return ret;
683}
684
685static int gspca_set_alt0(struct gspca_dev *gspca_dev)
686{
687 int ret;
688
Jean-Francois Moine8dd07ef2009-04-21 14:05:44 -0300689 if (gspca_dev->alt == 0)
690 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300691 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
692 if (ret < 0)
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -0300693 PDEBUG(D_ERR|D_STREAM, "set alt 0 err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300694 return ret;
695}
696
Frank Zago219423c2008-09-26 07:43:54 -0300697/* Note: both the queue and the usb locks should be held when calling this */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300698static void gspca_stream_off(struct gspca_dev *gspca_dev)
699{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300700 gspca_dev->streaming = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300701 if (gspca_dev->present) {
702 if (gspca_dev->sd_desc->stopN)
703 gspca_dev->sd_desc->stopN(gspca_dev);
704 destroy_urbs(gspca_dev);
Jean-Francois Moined08e2ce2009-01-20 16:21:44 -0300705 gspca_set_alt0(gspca_dev);
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300706 }
707
708 /* always call stop0 to free the subdriver's resources */
Jean-Francois Moine98522a72008-11-18 06:33:08 -0300709 if (gspca_dev->sd_desc->stop0)
710 gspca_dev->sd_desc->stop0(gspca_dev);
711 PDEBUG(D_STREAM, "stream off OK");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300712}
713
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300714static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300715{
716 int i;
717
718 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
719 gspca_dev->curr_mode = i;
720 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
721 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300722 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300723}
724
725static int wxh_to_mode(struct gspca_dev *gspca_dev,
726 int width, int height)
727{
728 int i;
729
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300730 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
731 if (width >= gspca_dev->cam.cam_mode[i].width
732 && height >= gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300733 break;
734 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300735 return i;
736}
737
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300738/*
739 * search a mode with the right pixel format
740 */
741static int gspca_get_mode(struct gspca_dev *gspca_dev,
742 int mode,
743 int pixfmt)
744{
745 int modeU, modeD;
746
747 modeU = modeD = mode;
748 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
749 if (--modeD >= 0) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300750 if (gspca_dev->cam.cam_mode[modeD].pixelformat
751 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300752 return modeD;
753 }
754 if (++modeU < gspca_dev->cam.nmodes) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300755 if (gspca_dev->cam.cam_mode[modeU].pixelformat
756 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300757 return modeU;
758 }
759 }
760 return -EINVAL;
761}
762
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300763#ifdef CONFIG_VIDEO_ADV_DEBUG
764static int vidioc_g_register(struct file *file, void *priv,
765 struct v4l2_dbg_register *reg)
766{
767 int ret;
768 struct gspca_dev *gspca_dev = priv;
769
770 if (!gspca_dev->sd_desc->get_chip_ident)
771 return -EINVAL;
772
773 if (!gspca_dev->sd_desc->get_register)
774 return -EINVAL;
775
776 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
777 return -ERESTARTSYS;
778 if (gspca_dev->present)
779 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
780 else
781 ret = -ENODEV;
782 mutex_unlock(&gspca_dev->usb_lock);
783
784 return ret;
785}
786
787static int vidioc_s_register(struct file *file, void *priv,
788 struct v4l2_dbg_register *reg)
789{
790 int ret;
791 struct gspca_dev *gspca_dev = priv;
792
793 if (!gspca_dev->sd_desc->get_chip_ident)
794 return -EINVAL;
795
796 if (!gspca_dev->sd_desc->set_register)
797 return -EINVAL;
798
799 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
800 return -ERESTARTSYS;
801 if (gspca_dev->present)
802 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
803 else
804 ret = -ENODEV;
805 mutex_unlock(&gspca_dev->usb_lock);
806
807 return ret;
808}
809#endif
810
811static int vidioc_g_chip_ident(struct file *file, void *priv,
812 struct v4l2_dbg_chip_ident *chip)
813{
814 int ret;
815 struct gspca_dev *gspca_dev = priv;
816
817 if (!gspca_dev->sd_desc->get_chip_ident)
818 return -EINVAL;
819
820 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
821 return -ERESTARTSYS;
822 if (gspca_dev->present)
823 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
824 else
825 ret = -ENODEV;
826 mutex_unlock(&gspca_dev->usb_lock);
827
828 return ret;
829}
830
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300831static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300832 struct v4l2_fmtdesc *fmtdesc)
833{
834 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300835 int i, j, index;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300836 __u32 fmt_tb[8];
837
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300838 /* give an index to each format */
839 index = 0;
840 j = 0;
841 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300842 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300843 j = 0;
844 for (;;) {
845 if (fmt_tb[j] == fmt_tb[index])
846 break;
847 j++;
848 }
849 if (j == index) {
850 if (fmtdesc->index == index)
851 break; /* new format */
852 index++;
Julia Lawall80297122008-11-12 23:18:21 -0300853 if (index >= ARRAY_SIZE(fmt_tb))
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300854 return -EINVAL;
855 }
856 }
857 if (i < 0)
858 return -EINVAL; /* no more format */
859
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300860 fmtdesc->pixelformat = fmt_tb[index];
861 if (gspca_is_compressed(fmt_tb[index]))
862 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
863 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
864 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
865 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
866 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
867 fmtdesc->description[4] = '\0';
868 return 0;
869}
870
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300871static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300872 struct v4l2_format *fmt)
873{
874 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300875 int mode;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300876
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300877 mode = gspca_dev->curr_mode;
878 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
879 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300880 return 0;
881}
882
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300883static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300884 struct v4l2_format *fmt)
885{
Hans de Goede24c530b2008-07-04 17:55:18 -0300886 int w, h, mode, mode2;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300887
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300888 w = fmt->fmt.pix.width;
889 h = fmt->fmt.pix.height;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300890
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300891#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300892 if (gspca_debug & D_CONF)
893 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
894#endif
895 /* search the closest mode for width and height */
896 mode = wxh_to_mode(gspca_dev, w, h);
897
898 /* OK if right palette */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300899 if (gspca_dev->cam.cam_mode[mode].pixelformat
900 != fmt->fmt.pix.pixelformat) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300901
902 /* else, search the closest mode with the same pixel format */
903 mode2 = gspca_get_mode(gspca_dev, mode,
904 fmt->fmt.pix.pixelformat);
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300905 if (mode2 >= 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300906 mode = mode2;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300907/* else
908 ; * no chance, return this mode */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300909 }
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300910 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
911 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300912 return mode; /* used when s_fmt */
913}
914
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300915static int vidioc_try_fmt_vid_cap(struct file *file,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300916 void *priv,
917 struct v4l2_format *fmt)
918{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300919 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300920 int ret;
921
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300922 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300923 if (ret < 0)
924 return ret;
925 return 0;
926}
927
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300928static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300929 struct v4l2_format *fmt)
930{
931 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300932 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300933
Hans de Goedee2997a72008-04-23 08:09:12 -0300934 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
935 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300936
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300937 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300938 if (ret < 0)
939 goto out;
940
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300941 if (gspca_dev->nframes != 0
942 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
943 ret = -EINVAL;
944 goto out;
945 }
946
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300947 if (ret == gspca_dev->curr_mode) {
948 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300949 goto out; /* same mode */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300950 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300951
952 if (gspca_dev->streaming) {
953 ret = -EBUSY;
954 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -0300955 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300956 gspca_dev->width = fmt->fmt.pix.width;
957 gspca_dev->height = fmt->fmt.pix.height;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300958 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
959 gspca_dev->curr_mode = ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300960
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300961 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300962out:
963 mutex_unlock(&gspca_dev->queue_lock);
964 return ret;
965}
966
Hans de Goede11c635a2009-05-25 15:04:22 -0300967static int vidioc_enum_framesizes(struct file *file, void *priv,
968 struct v4l2_frmsizeenum *fsize)
969{
970 struct gspca_dev *gspca_dev = priv;
971 int i;
972 __u32 index = 0;
973
974 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
975 if (fsize->pixel_format !=
976 gspca_dev->cam.cam_mode[i].pixelformat)
977 continue;
978
979 if (fsize->index == index) {
980 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
981 fsize->discrete.width =
982 gspca_dev->cam.cam_mode[i].width;
983 fsize->discrete.height =
984 gspca_dev->cam.cam_mode[i].height;
985 return 0;
986 }
987 index++;
988 }
989
990 return -EINVAL;
991}
992
Hans de Goeded0182302008-11-19 17:10:47 -0300993static void gspca_release(struct video_device *vfd)
Frank Zago6b060ff2008-09-28 08:12:22 -0300994{
Hans de Goeded0182302008-11-19 17:10:47 -0300995 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
Frank Zago6b060ff2008-09-28 08:12:22 -0300996
Jean-Francois Moine0d07e242009-10-01 13:44:49 -0300997 PDEBUG(D_PROBE, "/dev/video%d released", gspca_dev->vdev.num);
Frank Zago6b060ff2008-09-28 08:12:22 -0300998
999 kfree(gspca_dev->usb_buf);
1000 kfree(gspca_dev);
1001}
1002
Hans Verkuilbec43662008-12-30 06:58:20 -03001003static int dev_open(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001004{
1005 struct gspca_dev *gspca_dev;
1006 int ret;
1007
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001008 PDEBUG(D_STREAM, "[%s] open", current->comm);
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001009 gspca_dev = (struct gspca_dev *) video_devdata(file);
Hans de Goedee2997a72008-04-23 08:09:12 -03001010 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1011 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001012 if (!gspca_dev->present) {
1013 ret = -ENODEV;
1014 goto out;
1015 }
1016
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001017 if (gspca_dev->users > 4) { /* (arbitrary value) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001018 ret = -EBUSY;
1019 goto out;
1020 }
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03001021
1022 /* protect the subdriver against rmmod */
1023 if (!try_module_get(gspca_dev->module)) {
1024 ret = -ENODEV;
1025 goto out;
1026 }
1027
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001028 gspca_dev->users++;
Frank Zago6b060ff2008-09-28 08:12:22 -03001029
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001030 file->private_data = gspca_dev;
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001031#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001032 /* activate the v4l2 debug */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001033 if (gspca_debug & D_V4L2)
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001034 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001035 | V4L2_DEBUG_IOCTL_ARG;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001036 else
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001037 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001038 | V4L2_DEBUG_IOCTL_ARG);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001039#endif
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001040 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001041out:
1042 mutex_unlock(&gspca_dev->queue_lock);
1043 if (ret != 0)
1044 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
1045 else
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001046 PDEBUG(D_STREAM, "open done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001047 return ret;
1048}
1049
Hans Verkuilbec43662008-12-30 06:58:20 -03001050static int dev_close(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001051{
1052 struct gspca_dev *gspca_dev = file->private_data;
1053
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001054 PDEBUG(D_STREAM, "[%s] close", current->comm);
Hans de Goedee2997a72008-04-23 08:09:12 -03001055 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1056 return -ERESTARTSYS;
1057 gspca_dev->users--;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001058
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001059 /* if the file did the capture, free the streaming resources */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001060 if (gspca_dev->capt_file == file) {
Jean-Francois Moinea1e82882008-09-03 17:12:17 -03001061 if (gspca_dev->streaming) {
1062 mutex_lock(&gspca_dev->usb_lock);
1063 gspca_stream_off(gspca_dev);
1064 mutex_unlock(&gspca_dev->usb_lock);
1065 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001066 frame_free(gspca_dev);
Harvey Harrisonefab8212008-07-05 06:12:47 -03001067 gspca_dev->capt_file = NULL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001068 gspca_dev->memory = GSPCA_MEMORY_NO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001069 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001070 file->private_data = NULL;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03001071 module_put(gspca_dev->module);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001072 mutex_unlock(&gspca_dev->queue_lock);
Frank Zago6b060ff2008-09-28 08:12:22 -03001073
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001074 PDEBUG(D_STREAM, "close done");
Frank Zago6b060ff2008-09-28 08:12:22 -03001075
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001076 return 0;
1077}
1078
1079static int vidioc_querycap(struct file *file, void *priv,
1080 struct v4l2_capability *cap)
1081{
1082 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001083 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001084
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001085 /* protect the access to the usb device */
1086 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1087 return -ERESTARTSYS;
1088 if (!gspca_dev->present) {
1089 ret = -ENODEV;
1090 goto out;
1091 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001092 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -03001093 if (gspca_dev->dev->product != NULL) {
1094 strncpy(cap->card, gspca_dev->dev->product,
1095 sizeof cap->card);
1096 } else {
1097 snprintf(cap->card, sizeof cap->card,
1098 "USB Camera (%04x:%04x)",
1099 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1100 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1101 }
Thierry MERLE59320282009-01-21 15:32:26 -03001102 usb_make_path(gspca_dev->dev, cap->bus_info, sizeof(cap->bus_info));
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001103 cap->version = DRIVER_VERSION_NUMBER;
1104 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1105 | V4L2_CAP_STREAMING
1106 | V4L2_CAP_READWRITE;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001107 ret = 0;
1108out:
1109 mutex_unlock(&gspca_dev->usb_lock);
1110 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001111}
1112
Hans de Goedea0001a22009-06-10 04:52:18 -03001113static const struct ctrl *get_ctrl(struct gspca_dev *gspca_dev,
1114 int id)
1115{
1116 const struct ctrl *ctrls;
1117 int i;
1118
1119 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1120 i < gspca_dev->sd_desc->nctrls;
1121 i++, ctrls++) {
1122 if (gspca_dev->ctrl_dis & (1 << i))
1123 continue;
1124 if (id == ctrls->qctrl.id)
1125 return ctrls;
1126 }
1127 return NULL;
1128}
1129
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001130static int vidioc_queryctrl(struct file *file, void *priv,
1131 struct v4l2_queryctrl *q_ctrl)
1132{
1133 struct gspca_dev *gspca_dev = priv;
Hans de Goedea0001a22009-06-10 04:52:18 -03001134 const struct ctrl *ctrls;
1135 int i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001136 u32 id;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001137
Hans de Goedea0001a22009-06-10 04:52:18 -03001138 ctrls = NULL;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001139 id = q_ctrl->id;
1140 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1141 id &= V4L2_CTRL_ID_MASK;
1142 id++;
1143 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001144 if (gspca_dev->ctrl_dis & (1 << i))
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001145 continue;
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001146 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001147 continue;
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001148 if (ctrls && gspca_dev->sd_desc->ctrls[i].qctrl.id
Hans de Goedea0001a22009-06-10 04:52:18 -03001149 > ctrls->qctrl.id)
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001150 continue;
Hans de Goedea0001a22009-06-10 04:52:18 -03001151 ctrls = &gspca_dev->sd_desc->ctrls[i];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001152 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001153 } else {
1154 ctrls = get_ctrl(gspca_dev, id);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001155 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001156 if (ctrls == NULL)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001157 return -EINVAL;
Hans de Goedea0001a22009-06-10 04:52:18 -03001158 memcpy(q_ctrl, ctrls, sizeof *q_ctrl);
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001159 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001160}
1161
1162static int vidioc_s_ctrl(struct file *file, void *priv,
1163 struct v4l2_control *ctrl)
1164{
1165 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001166 const struct ctrl *ctrls;
Hans de Goedea0001a22009-06-10 04:52:18 -03001167 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001168
Hans de Goedea0001a22009-06-10 04:52:18 -03001169 ctrls = get_ctrl(gspca_dev, ctrl->id);
1170 if (ctrls == NULL)
1171 return -EINVAL;
1172
1173 if (ctrl->value < ctrls->qctrl.minimum
1174 || ctrl->value > ctrls->qctrl.maximum)
1175 return -ERANGE;
1176 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1177 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1178 return -ERESTARTSYS;
1179 if (gspca_dev->present)
1180 ret = ctrls->set(gspca_dev, ctrl->value);
1181 else
1182 ret = -ENODEV;
1183 mutex_unlock(&gspca_dev->usb_lock);
1184 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001185}
1186
1187static int vidioc_g_ctrl(struct file *file, void *priv,
1188 struct v4l2_control *ctrl)
1189{
1190 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001191 const struct ctrl *ctrls;
Hans de Goedea0001a22009-06-10 04:52:18 -03001192 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001193
Hans de Goedea0001a22009-06-10 04:52:18 -03001194 ctrls = get_ctrl(gspca_dev, ctrl->id);
1195 if (ctrls == NULL)
1196 return -EINVAL;
1197
1198 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1199 return -ERESTARTSYS;
1200 if (gspca_dev->present)
1201 ret = ctrls->get(gspca_dev, &ctrl->value);
1202 else
1203 ret = -ENODEV;
1204 mutex_unlock(&gspca_dev->usb_lock);
1205 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001206}
1207
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03001208/*fixme: have an audio flag in gspca_dev?*/
1209static int vidioc_s_audio(struct file *file, void *priv,
1210 struct v4l2_audio *audio)
1211{
1212 if (audio->index != 0)
1213 return -EINVAL;
1214 return 0;
1215}
1216
1217static int vidioc_g_audio(struct file *file, void *priv,
1218 struct v4l2_audio *audio)
1219{
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03001220 strcpy(audio->name, "Microphone");
1221 return 0;
1222}
1223
1224static int vidioc_enumaudio(struct file *file, void *priv,
1225 struct v4l2_audio *audio)
1226{
1227 if (audio->index != 0)
1228 return -EINVAL;
1229
1230 strcpy(audio->name, "Microphone");
1231 audio->capability = 0;
1232 audio->mode = 0;
1233 return 0;
1234}
1235
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001236static int vidioc_querymenu(struct file *file, void *priv,
1237 struct v4l2_querymenu *qmenu)
1238{
1239 struct gspca_dev *gspca_dev = priv;
1240
1241 if (!gspca_dev->sd_desc->querymenu)
1242 return -EINVAL;
1243 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1244}
1245
1246static int vidioc_enum_input(struct file *file, void *priv,
1247 struct v4l2_input *input)
1248{
1249 struct gspca_dev *gspca_dev = priv;
1250
1251 if (input->index != 0)
1252 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001253 input->type = V4L2_INPUT_TYPE_CAMERA;
Adam Bakerdfa76fa2009-03-29 19:17:10 -03001254 input->status = gspca_dev->cam.input_flags;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001255 strncpy(input->name, gspca_dev->sd_desc->name,
1256 sizeof input->name);
1257 return 0;
1258}
1259
1260static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1261{
1262 *i = 0;
1263 return 0;
1264}
1265
1266static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1267{
1268 if (i > 0)
1269 return -EINVAL;
1270 return (0);
1271}
1272
1273static int vidioc_reqbufs(struct file *file, void *priv,
1274 struct v4l2_requestbuffers *rb)
1275{
1276 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001277 int i, ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001278
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001279 switch (rb->memory) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001280 case GSPCA_MEMORY_READ: /* (internal call) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001281 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001282 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001283 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001284 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001285 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001286 }
Hans de Goedee2997a72008-04-23 08:09:12 -03001287 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1288 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001289
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001290 if (gspca_dev->memory != GSPCA_MEMORY_NO
1291 && gspca_dev->memory != rb->memory) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001292 ret = -EBUSY;
1293 goto out;
1294 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001295
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001296 /* only one file may do the capture */
1297 if (gspca_dev->capt_file != NULL
1298 && gspca_dev->capt_file != file) {
1299 ret = -EBUSY;
1300 goto out;
1301 }
1302
1303 /* if allocated, the buffers must not be mapped */
1304 for (i = 0; i < gspca_dev->nframes; i++) {
1305 if (gspca_dev->frame[i].vma_use_count) {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001306 ret = -EBUSY;
1307 goto out;
1308 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001309 }
1310
1311 /* stop streaming */
1312 if (gspca_dev->streaming) {
1313 mutex_lock(&gspca_dev->usb_lock);
1314 gspca_stream_off(gspca_dev);
1315 mutex_unlock(&gspca_dev->usb_lock);
1316 }
1317
1318 /* free the previous allocated buffers, if any */
1319 if (gspca_dev->nframes != 0) {
1320 frame_free(gspca_dev);
1321 gspca_dev->capt_file = NULL;
1322 }
1323 if (rb->count == 0) /* unrequest */
1324 goto out;
1325 gspca_dev->memory = rb->memory;
1326 ret = frame_alloc(gspca_dev, rb->count);
1327 if (ret == 0) {
1328 rb->count = gspca_dev->nframes;
1329 gspca_dev->capt_file = file;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001330 }
1331out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001332 mutex_unlock(&gspca_dev->queue_lock);
1333 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1334 return ret;
1335}
1336
1337static int vidioc_querybuf(struct file *file, void *priv,
1338 struct v4l2_buffer *v4l2_buf)
1339{
1340 struct gspca_dev *gspca_dev = priv;
1341 struct gspca_frame *frame;
1342
Jean-Francois Moine13752bd2008-12-19 15:08:51 -03001343 if (v4l2_buf->index < 0
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001344 || v4l2_buf->index >= gspca_dev->nframes)
1345 return -EINVAL;
1346
1347 frame = &gspca_dev->frame[v4l2_buf->index];
1348 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1349 return 0;
1350}
1351
1352static int vidioc_streamon(struct file *file, void *priv,
1353 enum v4l2_buf_type buf_type)
1354{
1355 struct gspca_dev *gspca_dev = priv;
1356 int ret;
1357
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001358 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1359 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001360 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1361 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001362
Jean-Francois Moine1edabe72008-12-19 15:17:25 -03001363 if (gspca_dev->nframes == 0
1364 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001365 ret = -EINVAL;
1366 goto out;
1367 }
1368 if (!gspca_dev->streaming) {
1369 ret = gspca_init_transfer(gspca_dev);
1370 if (ret < 0)
1371 goto out;
1372 }
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001373#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001374 if (gspca_debug & D_STREAM) {
1375 PDEBUG_MODE("stream on OK",
1376 gspca_dev->pixfmt,
1377 gspca_dev->width,
1378 gspca_dev->height);
1379 }
1380#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001381 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001382out:
1383 mutex_unlock(&gspca_dev->queue_lock);
1384 return ret;
1385}
1386
1387static int vidioc_streamoff(struct file *file, void *priv,
1388 enum v4l2_buf_type buf_type)
1389{
1390 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001391 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001392
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001393 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1394 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001395 if (!gspca_dev->streaming)
1396 return 0;
1397 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1398 return -ERESTARTSYS;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001399
1400 /* stop streaming */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001401 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1402 ret = -ERESTARTSYS;
1403 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001404 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001405 gspca_stream_off(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001406 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001407
1408 /* empty the application queues */
1409 for (i = 0; i < gspca_dev->nframes; i++)
1410 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1411 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1412 gspca_dev->last_packet_type = DISCARD_PACKET;
1413 gspca_dev->sequence = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001414 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001415out:
1416 mutex_unlock(&gspca_dev->queue_lock);
1417 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001418}
1419
1420static int vidioc_g_jpegcomp(struct file *file, void *priv,
1421 struct v4l2_jpegcompression *jpegcomp)
1422{
1423 struct gspca_dev *gspca_dev = priv;
1424 int ret;
1425
1426 if (!gspca_dev->sd_desc->get_jcomp)
1427 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001428 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1429 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001430 if (gspca_dev->present)
1431 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1432 else
1433 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001434 mutex_unlock(&gspca_dev->usb_lock);
1435 return ret;
1436}
1437
1438static int vidioc_s_jpegcomp(struct file *file, void *priv,
1439 struct v4l2_jpegcompression *jpegcomp)
1440{
1441 struct gspca_dev *gspca_dev = priv;
1442 int ret;
1443
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001444 if (!gspca_dev->sd_desc->set_jcomp)
1445 return -EINVAL;
Jim Parisf86d4a92008-12-10 05:50:44 -03001446 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1447 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001448 if (gspca_dev->present)
1449 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1450 else
1451 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001452 mutex_unlock(&gspca_dev->usb_lock);
1453 return ret;
1454}
1455
1456static int vidioc_g_parm(struct file *filp, void *priv,
1457 struct v4l2_streamparm *parm)
1458{
1459 struct gspca_dev *gspca_dev = priv;
1460
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001461 parm->parm.capture.readbuffers = gspca_dev->nbufread;
Jim Paris627a5ef2008-12-10 06:02:42 -03001462
1463 if (gspca_dev->sd_desc->get_streamparm) {
1464 int ret;
1465
1466 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1467 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001468 if (gspca_dev->present)
1469 ret = gspca_dev->sd_desc->get_streamparm(gspca_dev,
1470 parm);
1471 else
1472 ret = -ENODEV;
Jim Paris627a5ef2008-12-10 06:02:42 -03001473 mutex_unlock(&gspca_dev->usb_lock);
1474 return ret;
1475 }
1476
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001477 return 0;
1478}
1479
1480static int vidioc_s_parm(struct file *filp, void *priv,
1481 struct v4l2_streamparm *parm)
1482{
1483 struct gspca_dev *gspca_dev = priv;
1484 int n;
1485
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001486 n = parm->parm.capture.readbuffers;
1487 if (n == 0 || n > GSPCA_MAX_FRAMES)
1488 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1489 else
1490 gspca_dev->nbufread = n;
Jim Paris627a5ef2008-12-10 06:02:42 -03001491
1492 if (gspca_dev->sd_desc->set_streamparm) {
1493 int ret;
1494
1495 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1496 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001497 if (gspca_dev->present)
1498 ret = gspca_dev->sd_desc->set_streamparm(gspca_dev,
1499 parm);
1500 else
1501 ret = -ENODEV;
Jim Paris627a5ef2008-12-10 06:02:42 -03001502 mutex_unlock(&gspca_dev->usb_lock);
1503 return ret;
1504 }
1505
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001506 return 0;
1507}
1508
1509#ifdef CONFIG_VIDEO_V4L1_COMPAT
1510static int vidiocgmbuf(struct file *file, void *priv,
1511 struct video_mbuf *mbuf)
1512{
1513 struct gspca_dev *gspca_dev = file->private_data;
1514 int i;
1515
1516 PDEBUG(D_STREAM, "cgmbuf");
1517 if (gspca_dev->nframes == 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001518 int ret;
1519
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001520 {
1521 struct v4l2_format fmt;
1522
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001523 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1524 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1525 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1526 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1527 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001528 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001529 if (ret != 0)
1530 return ret;
1531 }
1532 {
1533 struct v4l2_requestbuffers rb;
1534
1535 memset(&rb, 0, sizeof rb);
1536 rb.count = 4;
1537 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1538 rb.memory = V4L2_MEMORY_MMAP;
1539 ret = vidioc_reqbufs(file, priv, &rb);
1540 if (ret != 0)
1541 return ret;
1542 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001543 }
1544 mbuf->frames = gspca_dev->nframes;
1545 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1546 for (i = 0; i < mbuf->frames; i++)
1547 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1548 return 0;
1549}
1550#endif
1551
1552static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1553{
1554 struct gspca_dev *gspca_dev = file->private_data;
Harvey Harrisonefab8212008-07-05 06:12:47 -03001555 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001556 struct page *page;
1557 unsigned long addr, start, size;
1558 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001559
1560 start = vma->vm_start;
1561 size = vma->vm_end - vma->vm_start;
1562 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1563
Hans de Goedee2997a72008-04-23 08:09:12 -03001564 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1565 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001566 if (!gspca_dev->present) {
1567 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001568 goto out;
1569 }
1570 if (gspca_dev->capt_file != file) {
1571 ret = -EINVAL;
1572 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001573 }
1574
Harvey Harrisonefab8212008-07-05 06:12:47 -03001575 frame = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001576 for (i = 0; i < gspca_dev->nframes; ++i) {
1577 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1578 PDEBUG(D_STREAM, "mmap bad memory type");
1579 break;
1580 }
1581 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1582 == vma->vm_pgoff) {
1583 frame = &gspca_dev->frame[i];
1584 break;
1585 }
1586 }
Harvey Harrisonefab8212008-07-05 06:12:47 -03001587 if (frame == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001588 PDEBUG(D_STREAM, "mmap no frame buffer found");
1589 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001590 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001591 }
1592#ifdef CONFIG_VIDEO_V4L1_COMPAT
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001593 /* v4l1 maps all the buffers */
1594 if (i != 0
1595 || size != frame->v4l2_buf.length * gspca_dev->nframes)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001596#endif
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001597 if (size != frame->v4l2_buf.length) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001598 PDEBUG(D_STREAM, "mmap bad size");
1599 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001600 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001601 }
1602
1603 /*
1604 * - VM_IO marks the area as being a mmaped region for I/O to a
1605 * device. It also prevents the region from being core dumped.
1606 */
1607 vma->vm_flags |= VM_IO;
1608
1609 addr = (unsigned long) frame->data;
1610 while (size > 0) {
1611 page = vmalloc_to_page((void *) addr);
1612 ret = vm_insert_page(vma, start, page);
1613 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001614 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001615 start += PAGE_SIZE;
1616 addr += PAGE_SIZE;
1617 size -= PAGE_SIZE;
1618 }
1619
1620 vma->vm_ops = &gspca_vm_ops;
1621 vma->vm_private_data = frame;
1622 gspca_vm_open(vma);
Hans de Goedee2997a72008-04-23 08:09:12 -03001623 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001624out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001625 mutex_unlock(&gspca_dev->queue_lock);
1626 return ret;
1627}
1628
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001629/*
1630 * wait for a video frame
1631 *
1632 * If a frame is ready, its index is returned.
1633 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001634static int frame_wait(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001635 int nonblock_ing)
1636{
1637 struct gspca_frame *frame;
1638 int i, j, ret;
1639
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001640 /* check if a frame is ready */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001641 i = gspca_dev->fr_o;
1642 j = gspca_dev->fr_queue[i];
1643 frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001644
Hans de Goede181b7042008-11-19 17:16:26 -03001645 if (!(frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)) {
1646 if (nonblock_ing)
1647 return -EAGAIN;
1648
1649 /* wait till a frame is ready */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001650 ret = wait_event_interruptible_timeout(gspca_dev->wq,
Hans de Goede181b7042008-11-19 17:16:26 -03001651 (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) ||
1652 !gspca_dev->streaming || !gspca_dev->present,
1653 msecs_to_jiffies(3000));
1654 if (ret < 0)
1655 return ret;
1656 if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present)
Hans de Goedee2997a72008-04-23 08:09:12 -03001657 return -EIO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001658 }
Hans de Goede181b7042008-11-19 17:16:26 -03001659
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001660 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1661 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1662 gspca_dev->fr_q,
1663 gspca_dev->fr_i,
1664 gspca_dev->fr_o);
Hans de Goedee2997a72008-04-23 08:09:12 -03001665
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001666 if (gspca_dev->sd_desc->dq_callback) {
1667 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001668 if (gspca_dev->present)
1669 gspca_dev->sd_desc->dq_callback(gspca_dev);
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001670 mutex_unlock(&gspca_dev->usb_lock);
1671 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001672 return j;
1673}
1674
1675/*
1676 * dequeue a video buffer
1677 *
1678 * If nonblock_ing is false, block until a buffer is available.
1679 */
1680static int vidioc_dqbuf(struct file *file, void *priv,
1681 struct v4l2_buffer *v4l2_buf)
1682{
1683 struct gspca_dev *gspca_dev = priv;
1684 struct gspca_frame *frame;
1685 int i, ret;
1686
1687 PDEBUG(D_FRAM, "dqbuf");
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001688 if (v4l2_buf->memory != gspca_dev->memory)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001689 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001690
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001691 if (!gspca_dev->present)
1692 return -ENODEV;
1693
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001694 /* if not streaming, be sure the application will not loop forever */
1695 if (!(file->f_flags & O_NONBLOCK)
1696 && !gspca_dev->streaming && gspca_dev->users == 1)
1697 return -EINVAL;
1698
1699 /* only the capturing file may dequeue */
1700 if (gspca_dev->capt_file != file)
1701 return -EINVAL;
1702
1703 /* only one dequeue / read at a time */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001704 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1705 return -ERESTARTSYS;
1706
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001707 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001708 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001709 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001710 i = ret; /* frame index */
1711 frame = &gspca_dev->frame[i];
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001712 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001713 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001714 frame->data,
1715 frame->v4l2_buf.bytesused)) {
1716 PDEBUG(D_ERR|D_STREAM,
1717 "dqbuf cp to user failed");
1718 ret = -EFAULT;
1719 goto out;
1720 }
1721 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001722 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1723 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1724 PDEBUG(D_FRAM, "dqbuf %d", i);
1725 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001726out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001727 mutex_unlock(&gspca_dev->read_lock);
1728 return ret;
1729}
1730
1731/*
1732 * queue a video buffer
1733 *
1734 * Attempting to queue a buffer that has already been
1735 * queued will return -EINVAL.
1736 */
1737static int vidioc_qbuf(struct file *file, void *priv,
1738 struct v4l2_buffer *v4l2_buf)
1739{
1740 struct gspca_dev *gspca_dev = priv;
1741 struct gspca_frame *frame;
1742 int i, index, ret;
1743
1744 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001745
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001746 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1747 return -ERESTARTSYS;
1748
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001749 index = v4l2_buf->index;
1750 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001751 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001752 "qbuf idx %d >= %d", index, gspca_dev->nframes);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001753 ret = -EINVAL;
1754 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001755 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001756 if (v4l2_buf->memory != gspca_dev->memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001757 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001758 ret = -EINVAL;
1759 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001760 }
1761
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001762 frame = &gspca_dev->frame[index];
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001763 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1764 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001765 ret = -EINVAL;
1766 goto out;
1767 }
1768
1769 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001770
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001771 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001772 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1773 frame->v4l2_buf.length = v4l2_buf->length;
1774 }
1775
1776 /* put the buffer in the 'queued' queue */
1777 i = gspca_dev->fr_q;
1778 gspca_dev->fr_queue[i] = index;
1779 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1780 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1781 gspca_dev->fr_q,
1782 gspca_dev->fr_i,
1783 gspca_dev->fr_o);
1784
1785 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1786 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1787 ret = 0;
1788out:
1789 mutex_unlock(&gspca_dev->queue_lock);
1790 return ret;
1791}
1792
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001793/*
1794 * allocate the resources for read()
1795 */
1796static int read_alloc(struct gspca_dev *gspca_dev,
1797 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001798{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001799 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001800 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001801
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001802 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001803 if (gspca_dev->nframes == 0) {
1804 struct v4l2_requestbuffers rb;
1805
1806 memset(&rb, 0, sizeof rb);
1807 rb.count = gspca_dev->nbufread;
1808 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001809 rb.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001810 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1811 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001812 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001813 return ret;
1814 }
1815 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1816 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001817 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001818 for (i = 0; i < gspca_dev->nbufread; i++) {
1819 v4l2_buf.index = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001820 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1821 if (ret != 0) {
1822 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1823 return ret;
1824 }
1825 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001826 gspca_dev->memory = GSPCA_MEMORY_READ;
1827 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001828
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001829 /* start streaming */
1830 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1831 if (ret != 0)
1832 PDEBUG(D_STREAM, "read streamon err %d", ret);
1833 return ret;
1834}
1835
1836static unsigned int dev_poll(struct file *file, poll_table *wait)
1837{
1838 struct gspca_dev *gspca_dev = file->private_data;
1839 int i, ret;
1840
1841 PDEBUG(D_FRAM, "poll");
1842
1843 poll_wait(file, &gspca_dev->wq, wait);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001844
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001845 /* if reqbufs is not done, the user would use read() */
1846 if (gspca_dev->nframes == 0) {
1847 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1848 return POLLERR; /* not the 1st time */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001849 ret = read_alloc(gspca_dev, file);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001850 if (ret != 0)
1851 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001852 }
1853
1854 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1855 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001856
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001857 /* check the next incoming buffer */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001858 i = gspca_dev->fr_o;
1859 i = gspca_dev->fr_queue[i];
1860 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1861 ret = POLLIN | POLLRDNORM; /* something to read */
1862 else
1863 ret = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001864 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001865 if (!gspca_dev->present)
1866 return POLLHUP;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001867 return ret;
1868}
1869
1870static ssize_t dev_read(struct file *file, char __user *data,
1871 size_t count, loff_t *ppos)
1872{
1873 struct gspca_dev *gspca_dev = file->private_data;
1874 struct gspca_frame *frame;
1875 struct v4l2_buffer v4l2_buf;
1876 struct timeval timestamp;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001877 int n, ret, ret2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001878
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001879 PDEBUG(D_FRAM, "read (%zd)", count);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001880 if (!gspca_dev->present)
1881 return -ENODEV;
1882 switch (gspca_dev->memory) {
1883 case GSPCA_MEMORY_NO: /* first time */
1884 ret = read_alloc(gspca_dev, file);
1885 if (ret != 0)
1886 return ret;
1887 break;
1888 case GSPCA_MEMORY_READ:
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001889 if (gspca_dev->capt_file == file)
1890 break;
1891 /* fall thru */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001892 default:
1893 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001894 }
1895
1896 /* get a frame */
1897 jiffies_to_timeval(get_jiffies_64(), &timestamp);
1898 timestamp.tv_sec--;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001899 n = 2;
1900 for (;;) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001901 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1902 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001903 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001904 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1905 if (ret != 0) {
1906 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1907 return ret;
1908 }
1909
1910 /* if the process slept for more than 1 second,
Frank Zago219423c2008-09-26 07:43:54 -03001911 * get a newer frame */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001912 frame = &gspca_dev->frame[v4l2_buf.index];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001913 if (--n < 0)
1914 break; /* avoid infinite loop */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001915 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1916 break;
1917 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1918 if (ret != 0) {
1919 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1920 return ret;
1921 }
1922 }
1923
1924 /* copy the frame */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001925 if (count > frame->v4l2_buf.bytesused)
1926 count = frame->v4l2_buf.bytesused;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001927 ret = copy_to_user(data, frame->data, count);
1928 if (ret != 0) {
1929 PDEBUG(D_ERR|D_STREAM,
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001930 "read cp to user lack %d / %zd", ret, count);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001931 ret = -EFAULT;
1932 goto out;
1933 }
1934 ret = count;
1935out:
1936 /* in each case, requeue the buffer */
1937 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1938 if (ret2 != 0)
1939 return ret2;
1940 return ret;
1941}
1942
Hans Verkuilbec43662008-12-30 06:58:20 -03001943static struct v4l2_file_operations dev_fops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001944 .owner = THIS_MODULE,
1945 .open = dev_open,
1946 .release = dev_close,
1947 .read = dev_read,
1948 .mmap = dev_mmap,
Hans Verkuilbec43662008-12-30 06:58:20 -03001949 .unlocked_ioctl = video_ioctl2,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001950 .poll = dev_poll,
1951};
1952
Hans Verkuila3998102008-07-21 02:57:38 -03001953static const struct v4l2_ioctl_ops dev_ioctl_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001954 .vidioc_querycap = vidioc_querycap,
1955 .vidioc_dqbuf = vidioc_dqbuf,
1956 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001957 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1958 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1959 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1960 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001961 .vidioc_streamon = vidioc_streamon,
1962 .vidioc_queryctrl = vidioc_queryctrl,
1963 .vidioc_g_ctrl = vidioc_g_ctrl,
1964 .vidioc_s_ctrl = vidioc_s_ctrl,
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03001965 .vidioc_g_audio = vidioc_g_audio,
1966 .vidioc_s_audio = vidioc_s_audio,
1967 .vidioc_enumaudio = vidioc_enumaudio,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001968 .vidioc_querymenu = vidioc_querymenu,
1969 .vidioc_enum_input = vidioc_enum_input,
1970 .vidioc_g_input = vidioc_g_input,
1971 .vidioc_s_input = vidioc_s_input,
1972 .vidioc_reqbufs = vidioc_reqbufs,
1973 .vidioc_querybuf = vidioc_querybuf,
1974 .vidioc_streamoff = vidioc_streamoff,
1975 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1976 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1977 .vidioc_g_parm = vidioc_g_parm,
1978 .vidioc_s_parm = vidioc_s_parm,
Hans de Goede11c635a2009-05-25 15:04:22 -03001979 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03001980#ifdef CONFIG_VIDEO_ADV_DEBUG
1981 .vidioc_g_register = vidioc_g_register,
1982 .vidioc_s_register = vidioc_s_register,
1983#endif
1984 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001985#ifdef CONFIG_VIDEO_V4L1_COMPAT
1986 .vidiocgmbuf = vidiocgmbuf,
1987#endif
1988};
1989
Hans Verkuila3998102008-07-21 02:57:38 -03001990static struct video_device gspca_template = {
1991 .name = "gspca main driver",
Hans Verkuila3998102008-07-21 02:57:38 -03001992 .fops = &dev_fops,
1993 .ioctl_ops = &dev_ioctl_ops,
Hans de Goeded0182302008-11-19 17:10:47 -03001994 .release = gspca_release,
Hans Verkuila3998102008-07-21 02:57:38 -03001995 .minor = -1,
1996};
1997
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001998/*
1999 * probe and create a new gspca device
2000 *
2001 * This function must be called by the sub-driver when it is
2002 * called for probing a new device.
2003 */
2004int gspca_dev_probe(struct usb_interface *intf,
2005 const struct usb_device_id *id,
2006 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002007 int dev_size,
2008 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002009{
2010 struct usb_interface_descriptor *interface;
2011 struct gspca_dev *gspca_dev;
2012 struct usb_device *dev = interface_to_usbdev(intf);
2013 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002014
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002015 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002016
2017 /* we don't handle multi-config cameras */
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002018 if (dev->descriptor.bNumConfigurations != 1) {
2019 PDEBUG(D_ERR, "Too many config");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002020 return -ENODEV;
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002021 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002022 interface = &intf->cur_altsetting->desc;
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002023 if (interface->bInterfaceNumber > 0) {
2024 PDEBUG(D_ERR, "intf != 0");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002025 return -ENODEV;
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002026 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002027
2028 /* create the device */
2029 if (dev_size < sizeof *gspca_dev)
2030 dev_size = sizeof *gspca_dev;
2031 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
Erik Andrene293e592008-10-03 08:46:50 -03002032 if (!gspca_dev) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002033 err("couldn't kzalloc gspca struct");
Erik Andrene293e592008-10-03 08:46:50 -03002034 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002035 }
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002036 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2037 if (!gspca_dev->usb_buf) {
2038 err("out of memory");
Erik Andrene293e592008-10-03 08:46:50 -03002039 ret = -ENOMEM;
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002040 goto out;
2041 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002042 gspca_dev->dev = dev;
2043 gspca_dev->iface = interface->bInterfaceNumber;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002044 gspca_dev->nbalt = intf->num_altsetting;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002045 gspca_dev->sd_desc = sd_desc;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002046 gspca_dev->nbufread = 2;
Jean-Francois Moineff374742008-10-23 07:29:51 -03002047 gspca_dev->empty_packet = -1; /* don't check the empty packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002048
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002049 /* configure the subdriver and initialize the USB device */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002050 ret = sd_desc->config(gspca_dev, id);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002051 if (ret < 0)
2052 goto out;
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002053 ret = sd_desc->init(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002054 if (ret < 0)
2055 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002056 ret = gspca_set_alt0(gspca_dev);
2057 if (ret < 0)
2058 goto out;
2059 gspca_set_default_mode(gspca_dev);
2060
2061 mutex_init(&gspca_dev->usb_lock);
2062 mutex_init(&gspca_dev->read_lock);
2063 mutex_init(&gspca_dev->queue_lock);
2064 init_waitqueue_head(&gspca_dev->wq);
2065
2066 /* init video stuff */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002067 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
Hans de Goede24441632009-03-27 16:08:20 -03002068 gspca_dev->vdev.parent = &intf->dev;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03002069 gspca_dev->module = module;
Oliver Neukumee56a4d2008-07-27 14:01:59 -03002070 gspca_dev->present = 1;
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002071 ret = video_register_device(&gspca_dev->vdev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002072 VFL_TYPE_GRABBER,
Jean-Francois Moine8d584a52009-01-13 06:07:59 -03002073 -1);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002074 if (ret < 0) {
2075 err("video_register_device err %d", ret);
2076 goto out;
2077 }
2078
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002079 usb_set_intfdata(intf, gspca_dev);
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002080 PDEBUG(D_PROBE, "/dev/video%d created", gspca_dev->vdev.num);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002081 return 0;
2082out:
Jean-Francois Moinee07a1d82008-11-19 06:37:53 -03002083 kfree(gspca_dev->usb_buf);
2084 kfree(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002085 return ret;
2086}
2087EXPORT_SYMBOL(gspca_dev_probe);
2088
2089/*
2090 * USB disconnection
2091 *
2092 * This function must be called by the sub-driver
2093 * when the device disconnects, after the specific resources are freed.
2094 */
2095void gspca_disconnect(struct usb_interface *intf)
2096{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002097 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002098
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002099 PDEBUG(D_PROBE, "/dev/video%d disconnect", gspca_dev->vdev.num);
Adam Bakerad281272009-02-04 15:33:21 -03002100 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03002101 gspca_dev->present = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002102
2103 if (gspca_dev->streaming) {
2104 destroy_urbs(gspca_dev);
2105 wake_up_interruptible(&gspca_dev->wq);
2106 }
2107
2108 /* the device is freed at exit of this function */
2109 gspca_dev->dev = NULL;
Adam Bakerad281272009-02-04 15:33:21 -03002110 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03002111
Frank Zago6b060ff2008-09-28 08:12:22 -03002112 usb_set_intfdata(intf, NULL);
2113
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002114 /* release the device */
2115 /* (this will call gspca_release() immediatly or on last close) */
2116 video_unregister_device(&gspca_dev->vdev);
Jean-Francois Moine3145b8c2008-10-05 04:22:10 -03002117
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002118/* PDEBUG(D_PROBE, "disconnect complete"); */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002119}
2120EXPORT_SYMBOL(gspca_disconnect);
2121
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002122#ifdef CONFIG_PM
2123int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2124{
2125 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2126
2127 if (!gspca_dev->streaming)
2128 return 0;
2129 gspca_dev->frozen = 1; /* avoid urb error messages */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002130 if (gspca_dev->sd_desc->stopN)
2131 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002132 destroy_urbs(gspca_dev);
2133 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002134 if (gspca_dev->sd_desc->stop0)
2135 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002136 return 0;
2137}
2138EXPORT_SYMBOL(gspca_suspend);
2139
2140int gspca_resume(struct usb_interface *intf)
2141{
2142 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2143
2144 gspca_dev->frozen = 0;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002145 gspca_dev->sd_desc->init(gspca_dev);
2146 if (gspca_dev->streaming)
2147 return gspca_init_transfer(gspca_dev);
Jean-Francois Moine1e89e2da2008-09-03 17:12:15 -03002148 return 0;
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002149}
2150EXPORT_SYMBOL(gspca_resume);
2151#endif
Hans de Goededcef32372008-07-10 10:40:53 -03002152/* -- cam driver utility functions -- */
2153
2154/* auto gain and exposure algorithm based on the knee algorithm described here:
2155 http://ytse.tricolour.net/docs/LowLightOptimization.html
2156
2157 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2158 where changed. */
2159int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2160 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2161{
2162 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2163 const struct ctrl *gain_ctrl = NULL;
2164 const struct ctrl *exposure_ctrl = NULL;
2165 const struct ctrl *autogain_ctrl = NULL;
2166 int retval = 0;
2167
2168 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
2169 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2170 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2171 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2172 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2173 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2174 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2175 }
2176 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2177 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2178 "on cam without (auto)gain/exposure");
2179 return 0;
2180 }
2181
2182 if (gain_ctrl->get(gspca_dev, &gain) ||
2183 exposure_ctrl->get(gspca_dev, &exposure) ||
2184 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2185 return 0;
2186
2187 orig_gain = gain;
2188 orig_exposure = exposure;
2189
2190 /* If we are of a multiple of deadzone, do multiple steps to reach the
2191 desired lumination fast (with the risc of a slight overshoot) */
2192 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2193
Jean-Francois Moine1c44d812008-11-10 05:56:55 -03002194 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
Hans de Goededcef32372008-07-10 10:40:53 -03002195 avg_lum, desired_avg_lum, steps);
2196
2197 for (i = 0; i < steps; i++) {
2198 if (avg_lum > desired_avg_lum) {
2199 if (gain > gain_knee)
2200 gain--;
2201 else if (exposure > exposure_knee)
2202 exposure--;
2203 else if (gain > gain_ctrl->qctrl.default_value)
2204 gain--;
2205 else if (exposure > exposure_ctrl->qctrl.minimum)
2206 exposure--;
2207 else if (gain > gain_ctrl->qctrl.minimum)
2208 gain--;
2209 else
2210 break;
2211 } else {
2212 if (gain < gain_ctrl->qctrl.default_value)
2213 gain++;
2214 else if (exposure < exposure_knee)
2215 exposure++;
2216 else if (gain < gain_knee)
2217 gain++;
2218 else if (exposure < exposure_ctrl->qctrl.maximum)
2219 exposure++;
2220 else if (gain < gain_ctrl->qctrl.maximum)
2221 gain++;
2222 else
2223 break;
2224 }
2225 }
2226
2227 if (gain != orig_gain) {
2228 gain_ctrl->set(gspca_dev, gain);
2229 retval = 1;
2230 }
2231 if (exposure != orig_exposure) {
2232 exposure_ctrl->set(gspca_dev, exposure);
2233 retval = 1;
2234 }
2235
2236 return retval;
2237}
2238EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2239
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002240/* -- module insert / remove -- */
2241static int __init gspca_init(void)
2242{
Jean-Francois Moinebb64e862008-07-19 06:49:28 -03002243 info("main v%d.%d.%d registered",
2244 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2245 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2246 DRIVER_VERSION_NUMBER & 0xff);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002247 return 0;
2248}
2249static void __exit gspca_exit(void)
2250{
2251 info("main deregistered");
2252}
2253
2254module_init(gspca_init);
2255module_exit(gspca_exit);
2256
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03002257#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002258module_param_named(debug, gspca_debug, int, 0644);
2259MODULE_PARM_DESC(debug,
2260 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002261 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
2262 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002263#endif