blob: ebaa2425fb50214ae3cb58eba7e7ab4dc8c942b9 [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);
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300141 return;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300142 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300143 pkt_scan = gspca_dev->sd_desc->pkt_scan;
144 for (i = 0; i < urb->number_of_packets; i++) {
145
146 /* check the availability of the frame buffer */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300147 frame = gspca_get_i_frame(gspca_dev);
148 if (!frame) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300149 gspca_dev->last_packet_type = DISCARD_PACKET;
150 break;
151 }
152
153 /* check the packet status and length */
154 len = urb->iso_frame_desc[i].actual_length;
Jean-Francois Moineff374742008-10-23 07:29:51 -0300155 if (len == 0) {
156 if (gspca_dev->empty_packet == 0)
157 gspca_dev->empty_packet = 1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300158 continue;
Jean-Francois Moineff374742008-10-23 07:29:51 -0300159 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300160 st = urb->iso_frame_desc[i].status;
161 if (st) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300162 PDEBUG(D_ERR,
163 "ISOC data error: [%d] len=%d, status=%d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300164 i, len, st);
165 gspca_dev->last_packet_type = DISCARD_PACKET;
166 continue;
167 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300168
169 /* let the packet be analyzed by the subdriver */
170 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
171 i, urb->iso_frame_desc[i].offset, len);
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300172 data = (u8 *) urb->transfer_buffer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300173 + urb->iso_frame_desc[i].offset;
174 pkt_scan(gspca_dev, frame, data, len);
175 }
176
177 /* resubmit the URB */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300178 st = usb_submit_urb(urb, GFP_ATOMIC);
179 if (st < 0)
180 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
181}
182
183/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300184 * ISOC message interrupt from the USB device
185 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300186 * Analyse each packet and call the subdriver for copy to the frame buffer.
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300187 */
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300188static void isoc_irq(struct urb *urb)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300189{
190 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
191
Hans de Goede3647fea2008-07-15 05:36:30 -0300192 PDEBUG(D_PACK, "isoc irq");
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300193 if (!gspca_dev->streaming)
194 return;
195 fill_frame(gspca_dev, urb);
196}
197
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300198/*
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300199 * bulk message interrupt from the USB device
200 */
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300201static void bulk_irq(struct urb *urb)
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300202{
203 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
204 struct gspca_frame *frame;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300205 int st;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300206
207 PDEBUG(D_PACK, "bulk irq");
208 if (!gspca_dev->streaming)
209 return;
Jean-Francois Moine880ae242008-11-18 04:19:52 -0300210 switch (urb->status) {
211 case 0:
212 break;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300213 case -ESHUTDOWN:
214 return; /* disconnection */
Jean-Francois Moine880ae242008-11-18 04:19:52 -0300215 case -ECONNRESET:
216 urb->status = 0;
217 break;
218 default:
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300219#ifdef CONFIG_PM
220 if (!gspca_dev->frozen)
221#endif
222 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300223 return;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300224 }
225
226 /* check the availability of the frame buffer */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300227 frame = gspca_get_i_frame(gspca_dev);
228 if (!frame) {
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300229 gspca_dev->last_packet_type = DISCARD_PACKET;
230 } else {
231 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
232 gspca_dev->sd_desc->pkt_scan(gspca_dev,
233 frame,
234 urb->transfer_buffer,
235 urb->actual_length);
236 }
Antonio Ospitedff369a2008-11-14 07:53:32 -0300237
238 /* resubmit the URB */
239 if (gspca_dev->cam.bulk_nurbs != 0) {
240 st = usb_submit_urb(urb, GFP_ATOMIC);
241 if (st < 0)
242 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
243 }
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300244}
245
246/*
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300247 * add data to the current frame
248 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300249 * This function is called by the subdrivers at interrupt level.
250 *
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300251 * To build a frame, these ones must add
252 * - one FIRST_PACKET
253 * - 0 or many INTER_PACKETs
254 * - one LAST_PACKET
255 * DISCARD_PACKET invalidates the whole frame.
256 * On LAST_PACKET, a new frame is returned.
257 */
258struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
Erik Andrene293e592008-10-03 08:46:50 -0300259 enum gspca_packet_type packet_type,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300260 struct gspca_frame *frame,
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300261 const __u8 *data,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300262 int len)
263{
264 int i, j;
265
Hans de Goedee2997a72008-04-23 08:09:12 -0300266 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300267
268 /* when start of a new frame, if the current frame buffer
269 * is not queued, discard the whole frame */
270 if (packet_type == FIRST_PACKET) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300271 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
272 != V4L2_BUF_FLAG_QUEUED) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300273 gspca_dev->last_packet_type = DISCARD_PACKET;
274 return frame;
275 }
276 frame->data_end = frame->data;
277 jiffies_to_timeval(get_jiffies_64(),
278 &frame->v4l2_buf.timestamp);
279 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300280 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
Jean-Francois Moine06ca78f2008-07-22 03:45:08 -0300281 if (packet_type == LAST_PACKET)
282 gspca_dev->last_packet_type = packet_type;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300283 return frame;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300284 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300285
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300286 /* append the packet to the frame buffer */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300287 if (len > 0) {
288 if (frame->data_end - frame->data + len
289 > frame->v4l2_buf.length) {
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -0300290 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300291 frame->data_end - frame->data + len,
292 frame->v4l2_buf.length);
293 packet_type = DISCARD_PACKET;
294 } else {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300295 memcpy(frame->data_end, data, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300296 frame->data_end += len;
297 }
298 }
299 gspca_dev->last_packet_type = packet_type;
300
Frank Zago219423c2008-09-26 07:43:54 -0300301 /* if last packet, wake up the application and advance in the queue */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300302 if (packet_type == LAST_PACKET) {
303 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
304 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
305 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300306 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300307 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
308 gspca_dev->fr_i = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300309 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
310 frame->v4l2_buf.bytesused,
311 gspca_dev->fr_q,
312 i,
313 gspca_dev->fr_o);
314 j = gspca_dev->fr_queue[i];
315 frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300316 }
317 return frame;
318}
319EXPORT_SYMBOL(gspca_frame_add);
320
321static int gspca_is_compressed(__u32 format)
322{
323 switch (format) {
324 case V4L2_PIX_FMT_MJPEG:
325 case V4L2_PIX_FMT_JPEG:
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300326 case V4L2_PIX_FMT_SPCA561:
Hans de Goedeab8f12c2008-07-04 18:29:32 -0300327 case V4L2_PIX_FMT_PAC207:
Kyle Guinnd661e622009-01-16 05:36:14 -0300328 case V4L2_PIX_FMT_MR97310A:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300329 return 1;
330 }
331 return 0;
332}
333
334static void *rvmalloc(unsigned long size)
335{
336 void *mem;
337 unsigned long adr;
338
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300339 mem = vmalloc_32(size);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300340 if (mem != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300341 adr = (unsigned long) mem;
342 while ((long) size > 0) {
343 SetPageReserved(vmalloc_to_page((void *) adr));
344 adr += PAGE_SIZE;
345 size -= PAGE_SIZE;
346 }
347 }
348 return mem;
349}
350
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300351static void rvfree(void *mem, long size)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300352{
353 unsigned long adr;
354
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300355 adr = (unsigned long) mem;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300356 while (size > 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300357 ClearPageReserved(vmalloc_to_page((void *) adr));
358 adr += PAGE_SIZE;
359 size -= PAGE_SIZE;
360 }
361 vfree(mem);
362}
363
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300364static int frame_alloc(struct gspca_dev *gspca_dev,
365 unsigned int count)
366{
367 struct gspca_frame *frame;
368 unsigned int frsz;
369 int i;
370
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300371 i = gspca_dev->curr_mode;
372 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300373 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300374 frsz = PAGE_ALIGN(frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300375 gspca_dev->frsz = frsz;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300376 if (count > GSPCA_MAX_FRAMES)
377 count = GSPCA_MAX_FRAMES;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300378 gspca_dev->frbuf = rvmalloc(frsz * count);
379 if (!gspca_dev->frbuf) {
380 err("frame alloc failed");
381 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300382 }
383 gspca_dev->nframes = count;
384 for (i = 0; i < count; i++) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300385 frame = &gspca_dev->frame[i];
386 frame->v4l2_buf.index = i;
387 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
388 frame->v4l2_buf.flags = 0;
389 frame->v4l2_buf.field = V4L2_FIELD_NONE;
390 frame->v4l2_buf.length = frsz;
391 frame->v4l2_buf.memory = gspca_dev->memory;
392 frame->v4l2_buf.sequence = 0;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300393 frame->data = frame->data_end =
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300394 gspca_dev->frbuf + i * frsz;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300395 frame->v4l2_buf.m.offset = i * frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300396 }
397 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
398 gspca_dev->last_packet_type = DISCARD_PACKET;
399 gspca_dev->sequence = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300400 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300401}
402
403static void frame_free(struct gspca_dev *gspca_dev)
404{
405 int i;
406
407 PDEBUG(D_STREAM, "frame free");
Harvey Harrisonefab8212008-07-05 06:12:47 -0300408 if (gspca_dev->frbuf != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300409 rvfree(gspca_dev->frbuf,
410 gspca_dev->nframes * gspca_dev->frsz);
411 gspca_dev->frbuf = NULL;
412 for (i = 0; i < gspca_dev->nframes; i++)
413 gspca_dev->frame[i].data = NULL;
414 }
415 gspca_dev->nframes = 0;
416}
417
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300418static void destroy_urbs(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300419{
420 struct urb *urb;
421 unsigned int i;
422
423 PDEBUG(D_STREAM, "kill transfer");
Erik Andrén44732812008-10-13 15:52:46 -0300424 for (i = 0; i < MAX_NURBS; i++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300425 urb = gspca_dev->urb[i];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300426 if (urb == NULL)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300427 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300428
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300429 gspca_dev->urb[i] = NULL;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300430 usb_kill_urb(urb);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300431 if (urb->transfer_buffer != NULL)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300432 usb_buffer_free(gspca_dev->dev,
433 urb->transfer_buffer_length,
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300434 urb->transfer_buffer,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300435 urb->transfer_dma);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300436 usb_free_urb(urb);
437 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300438}
439
440/*
Frank Zago219423c2008-09-26 07:43:54 -0300441 * look for an input transfer endpoint in an alternate setting
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300442 */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300443static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300444 int xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300445{
446 struct usb_host_endpoint *ep;
447 int i, attr;
448
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300449 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
450 ep = &alt->endpoint[i];
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300451 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Jean-Francois Moine30d35e42009-04-21 13:57:31 -0300452 if (attr == xfer
453 && ep->desc.wMaxPacketSize != 0)
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300454 return ep;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300455 }
456 return NULL;
457}
458
459/*
Frank Zago219423c2008-09-26 07:43:54 -0300460 * look for an input (isoc or bulk) endpoint
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300461 *
462 * The endpoint is defined by the subdriver.
463 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
464 * This routine may be called many times when the bandwidth is too small
465 * (the bandwidth is checked on urb submit).
466 */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300467static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300468{
469 struct usb_interface *intf;
470 struct usb_host_endpoint *ep;
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300471 int xfer, i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300472
473 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300474 ep = NULL;
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300475 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
476 : USB_ENDPOINT_XFER_ISOC;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300477 i = gspca_dev->alt; /* previous alt setting */
Hans de Goedea511ba92009-10-16 07:13:07 -0300478 if (gspca_dev->cam.reverse_alts) {
479 while (++i < gspca_dev->nbalt) {
480 ep = alt_xfer(&intf->altsetting[i], xfer);
481 if (ep)
482 break;
483 }
484 } else {
485 while (--i >= 0) {
486 ep = alt_xfer(&intf->altsetting[i], xfer);
487 if (ep)
488 break;
489 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300490 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300491 if (ep == NULL) {
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300492 err("no transfer endpoint found");
493 return NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300494 }
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300495 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300496 i, ep->desc.bEndpointAddress);
Jean-Francois Moine1c8336d2009-07-28 04:38:25 -0300497 gspca_dev->alt = i; /* memorize the current alt setting */
Jean-Francois Moine8dd07ef2009-04-21 14:05:44 -0300498 if (gspca_dev->nbalt > 1) {
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300499 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
500 if (ret < 0) {
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300501 err("set alt %d err %d", i, ret);
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300502 return NULL;
503 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300504 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300505 return ep;
506}
507
508/*
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300509 * create the URBs for image transfer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300510 */
511static int create_urbs(struct gspca_dev *gspca_dev,
512 struct usb_host_endpoint *ep)
513{
514 struct urb *urb;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300515 int n, nurbs, i, psize, npkt, bsize;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300516
517 /* calculate the packet size and the number of packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300518 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300519
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300520 if (!gspca_dev->cam.bulk) { /* isoc */
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300521
522 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300523 if (gspca_dev->pkt_size == 0)
524 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
525 else
526 psize = gspca_dev->pkt_size;
Jean-Francois Moine49cb6b02009-04-25 13:29:01 -0300527 npkt = gspca_dev->cam.npkt;
528 if (npkt == 0)
529 npkt = 32; /* default value */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300530 bsize = psize * npkt;
531 PDEBUG(D_STREAM,
532 "isoc %d pkts size %d = bsize:%d",
533 npkt, psize, bsize);
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300534 nurbs = DEF_NURBS;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300535 } else { /* bulk */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300536 npkt = 0;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300537 bsize = gspca_dev->cam.bulk_size;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300538 if (bsize == 0)
539 bsize = psize;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300540 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300541 if (gspca_dev->cam.bulk_nurbs != 0)
542 nurbs = gspca_dev->cam.bulk_nurbs;
543 else
544 nurbs = 1;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300545 }
546
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300547 gspca_dev->nurbs = nurbs;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300548 for (n = 0; n < nurbs; n++) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300549 urb = usb_alloc_urb(npkt, GFP_KERNEL);
550 if (!urb) {
551 err("usb_alloc_urb failed");
Jean-Francois Moine0d0548e2008-09-05 12:49:54 -0300552 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300553 return -ENOMEM;
554 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300555 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300556 bsize,
557 GFP_KERNEL,
558 &urb->transfer_dma);
559
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300560 if (urb->transfer_buffer == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300561 usb_free_urb(urb);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300562 err("usb_buffer_urb failed");
Jean-Francois Moine0d0548e2008-09-05 12:49:54 -0300563 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300564 return -ENOMEM;
565 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300566 gspca_dev->urb[n] = urb;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300567 urb->dev = gspca_dev->dev;
568 urb->context = gspca_dev;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300569 urb->transfer_buffer_length = bsize;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300570 if (npkt != 0) { /* ISOC */
571 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
572 ep->desc.bEndpointAddress);
Frank Zago6c49da72008-09-28 07:43:00 -0300573 urb->transfer_flags = URB_ISO_ASAP
574 | URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300575 urb->interval = ep->desc.bInterval;
576 urb->complete = isoc_irq;
577 urb->number_of_packets = npkt;
578 for (i = 0; i < npkt; i++) {
579 urb->iso_frame_desc[i].length = psize;
580 urb->iso_frame_desc[i].offset = psize * i;
581 }
582 } else { /* bulk */
583 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
584 ep->desc.bEndpointAddress),
Frank Zago6c49da72008-09-28 07:43:00 -0300585 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300586 urb->complete = bulk_irq;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300587 }
588 }
589 return 0;
590}
591
592/*
593 * start the USB transfer
594 */
595static int gspca_init_transfer(struct gspca_dev *gspca_dev)
596{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300597 struct usb_host_endpoint *ep;
598 int n, ret;
599
Hans de Goedee2997a72008-04-23 08:09:12 -0300600 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
601 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300602
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300603 if (!gspca_dev->present) {
604 ret = -ENODEV;
605 goto out;
606 }
607
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300608 /* set the higher alternate setting and
609 * loop until urb submit succeeds */
Hans de Goedea511ba92009-10-16 07:13:07 -0300610 if (gspca_dev->cam.reverse_alts)
611 gspca_dev->alt = 0;
612 else
613 gspca_dev->alt = gspca_dev->nbalt;
614
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300615 if (gspca_dev->sd_desc->isoc_init) {
616 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
617 if (ret < 0)
618 goto out;
619 }
620 ep = get_ep(gspca_dev);
621 if (ep == NULL) {
622 ret = -EIO;
623 goto out;
624 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300625 for (;;) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300626 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300627 ret = create_urbs(gspca_dev, ep);
628 if (ret < 0)
629 goto out;
630
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300631 /* clear the bulk endpoint */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300632 if (gspca_dev->cam.bulk)
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300633 usb_clear_halt(gspca_dev->dev,
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300634 gspca_dev->urb[0]->pipe);
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300635
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300636 /* start the cam */
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300637 ret = gspca_dev->sd_desc->start(gspca_dev);
638 if (ret < 0) {
639 destroy_urbs(gspca_dev);
640 goto out;
641 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300642 gspca_dev->streaming = 1;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300643
Antonio Ospitedff369a2008-11-14 07:53:32 -0300644 /* some bulk transfers are started by the subdriver */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300645 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300646 break;
647
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300648 /* submit the URBs */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300649 for (n = 0; n < gspca_dev->nurbs; n++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300650 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300651 if (ret < 0)
652 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300653 }
654 if (ret >= 0)
655 break;
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300656 PDEBUG(D_ERR|D_STREAM,
657 "usb_submit_urb alt %d err %d", gspca_dev->alt, ret);
658 gspca_dev->streaming = 0;
659 destroy_urbs(gspca_dev);
660 if (ret != -ENOSPC)
661 goto out;
662
663 /* the bandwidth is not wide enough
664 * negociate or try a lower alternate setting */
665 msleep(20); /* wait for kill complete */
666 if (gspca_dev->sd_desc->isoc_nego) {
667 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
668 if (ret < 0)
669 goto out;
670 } else {
671 ep = get_ep(gspca_dev);
672 if (ep == NULL) {
673 ret = -EIO;
674 goto out;
675 }
676 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300677 }
678out:
679 mutex_unlock(&gspca_dev->usb_lock);
680 return ret;
681}
682
683static int gspca_set_alt0(struct gspca_dev *gspca_dev)
684{
685 int ret;
686
Jean-Francois Moine8dd07ef2009-04-21 14:05:44 -0300687 if (gspca_dev->alt == 0)
688 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300689 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
690 if (ret < 0)
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -0300691 PDEBUG(D_ERR|D_STREAM, "set alt 0 err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300692 return ret;
693}
694
Frank Zago219423c2008-09-26 07:43:54 -0300695/* Note: both the queue and the usb locks should be held when calling this */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300696static void gspca_stream_off(struct gspca_dev *gspca_dev)
697{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300698 gspca_dev->streaming = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300699 if (gspca_dev->present) {
700 if (gspca_dev->sd_desc->stopN)
701 gspca_dev->sd_desc->stopN(gspca_dev);
702 destroy_urbs(gspca_dev);
Jean-Francois Moined08e2ce2009-01-20 16:21:44 -0300703 gspca_set_alt0(gspca_dev);
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300704 }
705
706 /* always call stop0 to free the subdriver's resources */
Jean-Francois Moine98522a72008-11-18 06:33:08 -0300707 if (gspca_dev->sd_desc->stop0)
708 gspca_dev->sd_desc->stop0(gspca_dev);
709 PDEBUG(D_STREAM, "stream off OK");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300710}
711
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300712static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300713{
714 int i;
715
716 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
717 gspca_dev->curr_mode = i;
718 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
719 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300720 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300721}
722
723static int wxh_to_mode(struct gspca_dev *gspca_dev,
724 int width, int height)
725{
726 int i;
727
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300728 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
729 if (width >= gspca_dev->cam.cam_mode[i].width
730 && height >= gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300731 break;
732 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300733 return i;
734}
735
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300736/*
737 * search a mode with the right pixel format
738 */
739static int gspca_get_mode(struct gspca_dev *gspca_dev,
740 int mode,
741 int pixfmt)
742{
743 int modeU, modeD;
744
745 modeU = modeD = mode;
746 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
747 if (--modeD >= 0) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300748 if (gspca_dev->cam.cam_mode[modeD].pixelformat
749 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300750 return modeD;
751 }
752 if (++modeU < gspca_dev->cam.nmodes) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300753 if (gspca_dev->cam.cam_mode[modeU].pixelformat
754 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300755 return modeU;
756 }
757 }
758 return -EINVAL;
759}
760
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300761#ifdef CONFIG_VIDEO_ADV_DEBUG
762static int vidioc_g_register(struct file *file, void *priv,
763 struct v4l2_dbg_register *reg)
764{
765 int ret;
766 struct gspca_dev *gspca_dev = priv;
767
768 if (!gspca_dev->sd_desc->get_chip_ident)
769 return -EINVAL;
770
771 if (!gspca_dev->sd_desc->get_register)
772 return -EINVAL;
773
774 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
775 return -ERESTARTSYS;
776 if (gspca_dev->present)
777 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
778 else
779 ret = -ENODEV;
780 mutex_unlock(&gspca_dev->usb_lock);
781
782 return ret;
783}
784
785static int vidioc_s_register(struct file *file, void *priv,
786 struct v4l2_dbg_register *reg)
787{
788 int ret;
789 struct gspca_dev *gspca_dev = priv;
790
791 if (!gspca_dev->sd_desc->get_chip_ident)
792 return -EINVAL;
793
794 if (!gspca_dev->sd_desc->set_register)
795 return -EINVAL;
796
797 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
798 return -ERESTARTSYS;
799 if (gspca_dev->present)
800 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
801 else
802 ret = -ENODEV;
803 mutex_unlock(&gspca_dev->usb_lock);
804
805 return ret;
806}
807#endif
808
809static int vidioc_g_chip_ident(struct file *file, void *priv,
810 struct v4l2_dbg_chip_ident *chip)
811{
812 int ret;
813 struct gspca_dev *gspca_dev = priv;
814
815 if (!gspca_dev->sd_desc->get_chip_ident)
816 return -EINVAL;
817
818 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
819 return -ERESTARTSYS;
820 if (gspca_dev->present)
821 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
822 else
823 ret = -ENODEV;
824 mutex_unlock(&gspca_dev->usb_lock);
825
826 return ret;
827}
828
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300829static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300830 struct v4l2_fmtdesc *fmtdesc)
831{
832 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300833 int i, j, index;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300834 __u32 fmt_tb[8];
835
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300836 /* give an index to each format */
837 index = 0;
838 j = 0;
839 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300840 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300841 j = 0;
842 for (;;) {
843 if (fmt_tb[j] == fmt_tb[index])
844 break;
845 j++;
846 }
847 if (j == index) {
848 if (fmtdesc->index == index)
849 break; /* new format */
850 index++;
Julia Lawall80297122008-11-12 23:18:21 -0300851 if (index >= ARRAY_SIZE(fmt_tb))
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300852 return -EINVAL;
853 }
854 }
855 if (i < 0)
856 return -EINVAL; /* no more format */
857
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300858 fmtdesc->pixelformat = fmt_tb[index];
859 if (gspca_is_compressed(fmt_tb[index]))
860 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
861 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
862 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
863 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
864 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
865 fmtdesc->description[4] = '\0';
866 return 0;
867}
868
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300869static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300870 struct v4l2_format *fmt)
871{
872 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300873 int mode;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300874
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300875 mode = gspca_dev->curr_mode;
876 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
877 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300878 return 0;
879}
880
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300881static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300882 struct v4l2_format *fmt)
883{
Hans de Goede24c530b2008-07-04 17:55:18 -0300884 int w, h, mode, mode2;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300885
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300886 w = fmt->fmt.pix.width;
887 h = fmt->fmt.pix.height;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300888
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300889#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300890 if (gspca_debug & D_CONF)
891 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
892#endif
893 /* search the closest mode for width and height */
894 mode = wxh_to_mode(gspca_dev, w, h);
895
896 /* OK if right palette */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300897 if (gspca_dev->cam.cam_mode[mode].pixelformat
898 != fmt->fmt.pix.pixelformat) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300899
900 /* else, search the closest mode with the same pixel format */
901 mode2 = gspca_get_mode(gspca_dev, mode,
902 fmt->fmt.pix.pixelformat);
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300903 if (mode2 >= 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300904 mode = mode2;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300905/* else
906 ; * no chance, return this mode */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300907 }
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300908 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
909 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300910 return mode; /* used when s_fmt */
911}
912
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300913static int vidioc_try_fmt_vid_cap(struct file *file,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300914 void *priv,
915 struct v4l2_format *fmt)
916{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300917 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300918 int ret;
919
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300920 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300921 if (ret < 0)
922 return ret;
923 return 0;
924}
925
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300926static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300927 struct v4l2_format *fmt)
928{
929 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300930 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300931
Hans de Goedee2997a72008-04-23 08:09:12 -0300932 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
933 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300934
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300935 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300936 if (ret < 0)
937 goto out;
938
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300939 if (gspca_dev->nframes != 0
940 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
941 ret = -EINVAL;
942 goto out;
943 }
944
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300945 if (ret == gspca_dev->curr_mode) {
946 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300947 goto out; /* same mode */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300948 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300949
950 if (gspca_dev->streaming) {
951 ret = -EBUSY;
952 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -0300953 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300954 gspca_dev->width = fmt->fmt.pix.width;
955 gspca_dev->height = fmt->fmt.pix.height;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300956 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
957 gspca_dev->curr_mode = ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300958
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300959 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300960out:
961 mutex_unlock(&gspca_dev->queue_lock);
962 return ret;
963}
964
Hans de Goede11c635a2009-05-25 15:04:22 -0300965static int vidioc_enum_framesizes(struct file *file, void *priv,
966 struct v4l2_frmsizeenum *fsize)
967{
968 struct gspca_dev *gspca_dev = priv;
969 int i;
970 __u32 index = 0;
971
972 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
973 if (fsize->pixel_format !=
974 gspca_dev->cam.cam_mode[i].pixelformat)
975 continue;
976
977 if (fsize->index == index) {
978 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
979 fsize->discrete.width =
980 gspca_dev->cam.cam_mode[i].width;
981 fsize->discrete.height =
982 gspca_dev->cam.cam_mode[i].height;
983 return 0;
984 }
985 index++;
986 }
987
988 return -EINVAL;
989}
990
Hans de Goeded0182302008-11-19 17:10:47 -0300991static void gspca_release(struct video_device *vfd)
Frank Zago6b060ff2008-09-28 08:12:22 -0300992{
Hans de Goeded0182302008-11-19 17:10:47 -0300993 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
Frank Zago6b060ff2008-09-28 08:12:22 -0300994
Jean-Francois Moine0d07e242009-10-01 13:44:49 -0300995 PDEBUG(D_PROBE, "/dev/video%d released", gspca_dev->vdev.num);
Frank Zago6b060ff2008-09-28 08:12:22 -0300996
997 kfree(gspca_dev->usb_buf);
998 kfree(gspca_dev);
999}
1000
Hans Verkuilbec43662008-12-30 06:58:20 -03001001static int dev_open(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001002{
1003 struct gspca_dev *gspca_dev;
1004 int ret;
1005
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001006 PDEBUG(D_STREAM, "[%s] open", current->comm);
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001007 gspca_dev = (struct gspca_dev *) video_devdata(file);
Hans de Goedee2997a72008-04-23 08:09:12 -03001008 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1009 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001010 if (!gspca_dev->present) {
1011 ret = -ENODEV;
1012 goto out;
1013 }
1014
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001015 if (gspca_dev->users > 4) { /* (arbitrary value) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001016 ret = -EBUSY;
1017 goto out;
1018 }
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03001019
1020 /* protect the subdriver against rmmod */
1021 if (!try_module_get(gspca_dev->module)) {
1022 ret = -ENODEV;
1023 goto out;
1024 }
1025
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001026 gspca_dev->users++;
Frank Zago6b060ff2008-09-28 08:12:22 -03001027
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001028 file->private_data = gspca_dev;
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001029#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001030 /* activate the v4l2 debug */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001031 if (gspca_debug & D_V4L2)
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001032 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001033 | V4L2_DEBUG_IOCTL_ARG;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001034 else
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001035 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001036 | V4L2_DEBUG_IOCTL_ARG);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001037#endif
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001038 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001039out:
1040 mutex_unlock(&gspca_dev->queue_lock);
1041 if (ret != 0)
1042 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
1043 else
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001044 PDEBUG(D_STREAM, "open done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001045 return ret;
1046}
1047
Hans Verkuilbec43662008-12-30 06:58:20 -03001048static int dev_close(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001049{
1050 struct gspca_dev *gspca_dev = file->private_data;
1051
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001052 PDEBUG(D_STREAM, "[%s] close", current->comm);
Hans de Goedee2997a72008-04-23 08:09:12 -03001053 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1054 return -ERESTARTSYS;
1055 gspca_dev->users--;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001056
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001057 /* if the file did the capture, free the streaming resources */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001058 if (gspca_dev->capt_file == file) {
Jean-Francois Moinea1e82882008-09-03 17:12:17 -03001059 if (gspca_dev->streaming) {
1060 mutex_lock(&gspca_dev->usb_lock);
1061 gspca_stream_off(gspca_dev);
1062 mutex_unlock(&gspca_dev->usb_lock);
1063 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001064 frame_free(gspca_dev);
Harvey Harrisonefab8212008-07-05 06:12:47 -03001065 gspca_dev->capt_file = NULL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001066 gspca_dev->memory = GSPCA_MEMORY_NO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001067 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001068 file->private_data = NULL;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03001069 module_put(gspca_dev->module);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001070 mutex_unlock(&gspca_dev->queue_lock);
Frank Zago6b060ff2008-09-28 08:12:22 -03001071
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001072 PDEBUG(D_STREAM, "close done");
Frank Zago6b060ff2008-09-28 08:12:22 -03001073
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001074 return 0;
1075}
1076
1077static int vidioc_querycap(struct file *file, void *priv,
1078 struct v4l2_capability *cap)
1079{
1080 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001081 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001082
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001083 /* protect the access to the usb device */
1084 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1085 return -ERESTARTSYS;
1086 if (!gspca_dev->present) {
1087 ret = -ENODEV;
1088 goto out;
1089 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001090 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -03001091 if (gspca_dev->dev->product != NULL) {
1092 strncpy(cap->card, gspca_dev->dev->product,
1093 sizeof cap->card);
1094 } else {
1095 snprintf(cap->card, sizeof cap->card,
1096 "USB Camera (%04x:%04x)",
1097 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1098 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1099 }
Thierry MERLE59320282009-01-21 15:32:26 -03001100 usb_make_path(gspca_dev->dev, cap->bus_info, sizeof(cap->bus_info));
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001101 cap->version = DRIVER_VERSION_NUMBER;
1102 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1103 | V4L2_CAP_STREAMING
1104 | V4L2_CAP_READWRITE;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001105 ret = 0;
1106out:
1107 mutex_unlock(&gspca_dev->usb_lock);
1108 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001109}
1110
Hans de Goedea0001a22009-06-10 04:52:18 -03001111static const struct ctrl *get_ctrl(struct gspca_dev *gspca_dev,
1112 int id)
1113{
1114 const struct ctrl *ctrls;
1115 int i;
1116
1117 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1118 i < gspca_dev->sd_desc->nctrls;
1119 i++, ctrls++) {
1120 if (gspca_dev->ctrl_dis & (1 << i))
1121 continue;
1122 if (id == ctrls->qctrl.id)
1123 return ctrls;
1124 }
1125 return NULL;
1126}
1127
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001128static int vidioc_queryctrl(struct file *file, void *priv,
1129 struct v4l2_queryctrl *q_ctrl)
1130{
1131 struct gspca_dev *gspca_dev = priv;
Hans de Goedea0001a22009-06-10 04:52:18 -03001132 const struct ctrl *ctrls;
1133 int i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001134 u32 id;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001135
Hans de Goedea0001a22009-06-10 04:52:18 -03001136 ctrls = NULL;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001137 id = q_ctrl->id;
1138 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1139 id &= V4L2_CTRL_ID_MASK;
1140 id++;
1141 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001142 if (gspca_dev->ctrl_dis & (1 << i))
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001143 continue;
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001144 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001145 continue;
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001146 if (ctrls && gspca_dev->sd_desc->ctrls[i].qctrl.id
Hans de Goedea0001a22009-06-10 04:52:18 -03001147 > ctrls->qctrl.id)
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001148 continue;
Hans de Goedea0001a22009-06-10 04:52:18 -03001149 ctrls = &gspca_dev->sd_desc->ctrls[i];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001150 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001151 } else {
1152 ctrls = get_ctrl(gspca_dev, id);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001153 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001154 if (ctrls == NULL)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001155 return -EINVAL;
Hans de Goedea0001a22009-06-10 04:52:18 -03001156 memcpy(q_ctrl, ctrls, sizeof *q_ctrl);
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001157 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001158}
1159
1160static int vidioc_s_ctrl(struct file *file, void *priv,
1161 struct v4l2_control *ctrl)
1162{
1163 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001164 const struct ctrl *ctrls;
Hans de Goedea0001a22009-06-10 04:52:18 -03001165 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001166
Hans de Goedea0001a22009-06-10 04:52:18 -03001167 ctrls = get_ctrl(gspca_dev, ctrl->id);
1168 if (ctrls == NULL)
1169 return -EINVAL;
1170
1171 if (ctrl->value < ctrls->qctrl.minimum
1172 || ctrl->value > ctrls->qctrl.maximum)
1173 return -ERANGE;
1174 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1175 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1176 return -ERESTARTSYS;
1177 if (gspca_dev->present)
1178 ret = ctrls->set(gspca_dev, ctrl->value);
1179 else
1180 ret = -ENODEV;
1181 mutex_unlock(&gspca_dev->usb_lock);
1182 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001183}
1184
1185static int vidioc_g_ctrl(struct file *file, void *priv,
1186 struct v4l2_control *ctrl)
1187{
1188 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001189 const struct ctrl *ctrls;
Hans de Goedea0001a22009-06-10 04:52:18 -03001190 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001191
Hans de Goedea0001a22009-06-10 04:52:18 -03001192 ctrls = get_ctrl(gspca_dev, ctrl->id);
1193 if (ctrls == NULL)
1194 return -EINVAL;
1195
1196 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1197 return -ERESTARTSYS;
1198 if (gspca_dev->present)
1199 ret = ctrls->get(gspca_dev, &ctrl->value);
1200 else
1201 ret = -ENODEV;
1202 mutex_unlock(&gspca_dev->usb_lock);
1203 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001204}
1205
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03001206/*fixme: have an audio flag in gspca_dev?*/
1207static int vidioc_s_audio(struct file *file, void *priv,
1208 struct v4l2_audio *audio)
1209{
1210 if (audio->index != 0)
1211 return -EINVAL;
1212 return 0;
1213}
1214
1215static int vidioc_g_audio(struct file *file, void *priv,
1216 struct v4l2_audio *audio)
1217{
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03001218 strcpy(audio->name, "Microphone");
1219 return 0;
1220}
1221
1222static int vidioc_enumaudio(struct file *file, void *priv,
1223 struct v4l2_audio *audio)
1224{
1225 if (audio->index != 0)
1226 return -EINVAL;
1227
1228 strcpy(audio->name, "Microphone");
1229 audio->capability = 0;
1230 audio->mode = 0;
1231 return 0;
1232}
1233
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001234static int vidioc_querymenu(struct file *file, void *priv,
1235 struct v4l2_querymenu *qmenu)
1236{
1237 struct gspca_dev *gspca_dev = priv;
1238
1239 if (!gspca_dev->sd_desc->querymenu)
1240 return -EINVAL;
1241 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1242}
1243
1244static int vidioc_enum_input(struct file *file, void *priv,
1245 struct v4l2_input *input)
1246{
1247 struct gspca_dev *gspca_dev = priv;
1248
1249 if (input->index != 0)
1250 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001251 input->type = V4L2_INPUT_TYPE_CAMERA;
Adam Bakerdfa76fa2009-03-29 19:17:10 -03001252 input->status = gspca_dev->cam.input_flags;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001253 strncpy(input->name, gspca_dev->sd_desc->name,
1254 sizeof input->name);
1255 return 0;
1256}
1257
1258static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1259{
1260 *i = 0;
1261 return 0;
1262}
1263
1264static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1265{
1266 if (i > 0)
1267 return -EINVAL;
1268 return (0);
1269}
1270
1271static int vidioc_reqbufs(struct file *file, void *priv,
1272 struct v4l2_requestbuffers *rb)
1273{
1274 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001275 int i, ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001276
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001277 switch (rb->memory) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001278 case GSPCA_MEMORY_READ: /* (internal call) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001279 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001280 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001281 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001282 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001283 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001284 }
Hans de Goedee2997a72008-04-23 08:09:12 -03001285 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1286 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001287
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001288 if (gspca_dev->memory != GSPCA_MEMORY_NO
1289 && gspca_dev->memory != rb->memory) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001290 ret = -EBUSY;
1291 goto out;
1292 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001293
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001294 /* only one file may do the capture */
1295 if (gspca_dev->capt_file != NULL
1296 && gspca_dev->capt_file != file) {
1297 ret = -EBUSY;
1298 goto out;
1299 }
1300
1301 /* if allocated, the buffers must not be mapped */
1302 for (i = 0; i < gspca_dev->nframes; i++) {
1303 if (gspca_dev->frame[i].vma_use_count) {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001304 ret = -EBUSY;
1305 goto out;
1306 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001307 }
1308
1309 /* stop streaming */
1310 if (gspca_dev->streaming) {
1311 mutex_lock(&gspca_dev->usb_lock);
1312 gspca_stream_off(gspca_dev);
1313 mutex_unlock(&gspca_dev->usb_lock);
1314 }
1315
1316 /* free the previous allocated buffers, if any */
1317 if (gspca_dev->nframes != 0) {
1318 frame_free(gspca_dev);
1319 gspca_dev->capt_file = NULL;
1320 }
1321 if (rb->count == 0) /* unrequest */
1322 goto out;
1323 gspca_dev->memory = rb->memory;
1324 ret = frame_alloc(gspca_dev, rb->count);
1325 if (ret == 0) {
1326 rb->count = gspca_dev->nframes;
1327 gspca_dev->capt_file = file;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001328 }
1329out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001330 mutex_unlock(&gspca_dev->queue_lock);
1331 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1332 return ret;
1333}
1334
1335static int vidioc_querybuf(struct file *file, void *priv,
1336 struct v4l2_buffer *v4l2_buf)
1337{
1338 struct gspca_dev *gspca_dev = priv;
1339 struct gspca_frame *frame;
1340
Jean-Francois Moine13752bd2008-12-19 15:08:51 -03001341 if (v4l2_buf->index < 0
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001342 || v4l2_buf->index >= gspca_dev->nframes)
1343 return -EINVAL;
1344
1345 frame = &gspca_dev->frame[v4l2_buf->index];
1346 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1347 return 0;
1348}
1349
1350static int vidioc_streamon(struct file *file, void *priv,
1351 enum v4l2_buf_type buf_type)
1352{
1353 struct gspca_dev *gspca_dev = priv;
1354 int ret;
1355
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001356 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1357 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001358 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1359 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001360
Jean-Francois Moine1edabe72008-12-19 15:17:25 -03001361 if (gspca_dev->nframes == 0
1362 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001363 ret = -EINVAL;
1364 goto out;
1365 }
1366 if (!gspca_dev->streaming) {
1367 ret = gspca_init_transfer(gspca_dev);
1368 if (ret < 0)
1369 goto out;
1370 }
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001371#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001372 if (gspca_debug & D_STREAM) {
1373 PDEBUG_MODE("stream on OK",
1374 gspca_dev->pixfmt,
1375 gspca_dev->width,
1376 gspca_dev->height);
1377 }
1378#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001379 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001380out:
1381 mutex_unlock(&gspca_dev->queue_lock);
1382 return ret;
1383}
1384
1385static int vidioc_streamoff(struct file *file, void *priv,
1386 enum v4l2_buf_type buf_type)
1387{
1388 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001389 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001390
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001391 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1392 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001393 if (!gspca_dev->streaming)
1394 return 0;
1395 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1396 return -ERESTARTSYS;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001397
1398 /* stop streaming */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001399 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1400 ret = -ERESTARTSYS;
1401 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001402 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001403 gspca_stream_off(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001404 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001405
1406 /* empty the application queues */
1407 for (i = 0; i < gspca_dev->nframes; i++)
1408 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1409 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1410 gspca_dev->last_packet_type = DISCARD_PACKET;
1411 gspca_dev->sequence = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001412 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001413out:
1414 mutex_unlock(&gspca_dev->queue_lock);
1415 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001416}
1417
1418static int vidioc_g_jpegcomp(struct file *file, void *priv,
1419 struct v4l2_jpegcompression *jpegcomp)
1420{
1421 struct gspca_dev *gspca_dev = priv;
1422 int ret;
1423
1424 if (!gspca_dev->sd_desc->get_jcomp)
1425 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001426 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1427 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001428 if (gspca_dev->present)
1429 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1430 else
1431 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001432 mutex_unlock(&gspca_dev->usb_lock);
1433 return ret;
1434}
1435
1436static int vidioc_s_jpegcomp(struct file *file, void *priv,
1437 struct v4l2_jpegcompression *jpegcomp)
1438{
1439 struct gspca_dev *gspca_dev = priv;
1440 int ret;
1441
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001442 if (!gspca_dev->sd_desc->set_jcomp)
1443 return -EINVAL;
Jim Parisf86d4a92008-12-10 05:50:44 -03001444 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1445 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001446 if (gspca_dev->present)
1447 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1448 else
1449 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001450 mutex_unlock(&gspca_dev->usb_lock);
1451 return ret;
1452}
1453
1454static int vidioc_g_parm(struct file *filp, void *priv,
1455 struct v4l2_streamparm *parm)
1456{
1457 struct gspca_dev *gspca_dev = priv;
1458
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001459 parm->parm.capture.readbuffers = gspca_dev->nbufread;
Jim Paris627a5ef2008-12-10 06:02:42 -03001460
1461 if (gspca_dev->sd_desc->get_streamparm) {
1462 int ret;
1463
1464 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1465 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001466 if (gspca_dev->present)
1467 ret = gspca_dev->sd_desc->get_streamparm(gspca_dev,
1468 parm);
1469 else
1470 ret = -ENODEV;
Jim Paris627a5ef2008-12-10 06:02:42 -03001471 mutex_unlock(&gspca_dev->usb_lock);
1472 return ret;
1473 }
1474
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001475 return 0;
1476}
1477
1478static int vidioc_s_parm(struct file *filp, void *priv,
1479 struct v4l2_streamparm *parm)
1480{
1481 struct gspca_dev *gspca_dev = priv;
1482 int n;
1483
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001484 n = parm->parm.capture.readbuffers;
1485 if (n == 0 || n > GSPCA_MAX_FRAMES)
1486 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1487 else
1488 gspca_dev->nbufread = n;
Jim Paris627a5ef2008-12-10 06:02:42 -03001489
1490 if (gspca_dev->sd_desc->set_streamparm) {
1491 int ret;
1492
1493 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1494 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001495 if (gspca_dev->present)
1496 ret = gspca_dev->sd_desc->set_streamparm(gspca_dev,
1497 parm);
1498 else
1499 ret = -ENODEV;
Jim Paris627a5ef2008-12-10 06:02:42 -03001500 mutex_unlock(&gspca_dev->usb_lock);
1501 return ret;
1502 }
1503
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001504 return 0;
1505}
1506
1507#ifdef CONFIG_VIDEO_V4L1_COMPAT
1508static int vidiocgmbuf(struct file *file, void *priv,
1509 struct video_mbuf *mbuf)
1510{
1511 struct gspca_dev *gspca_dev = file->private_data;
1512 int i;
1513
1514 PDEBUG(D_STREAM, "cgmbuf");
1515 if (gspca_dev->nframes == 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001516 int ret;
1517
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001518 {
1519 struct v4l2_format fmt;
1520
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001521 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1522 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1523 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1524 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1525 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001526 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001527 if (ret != 0)
1528 return ret;
1529 }
1530 {
1531 struct v4l2_requestbuffers rb;
1532
1533 memset(&rb, 0, sizeof rb);
1534 rb.count = 4;
1535 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1536 rb.memory = V4L2_MEMORY_MMAP;
1537 ret = vidioc_reqbufs(file, priv, &rb);
1538 if (ret != 0)
1539 return ret;
1540 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001541 }
1542 mbuf->frames = gspca_dev->nframes;
1543 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1544 for (i = 0; i < mbuf->frames; i++)
1545 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1546 return 0;
1547}
1548#endif
1549
1550static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1551{
1552 struct gspca_dev *gspca_dev = file->private_data;
Harvey Harrisonefab8212008-07-05 06:12:47 -03001553 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001554 struct page *page;
1555 unsigned long addr, start, size;
1556 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001557
1558 start = vma->vm_start;
1559 size = vma->vm_end - vma->vm_start;
1560 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1561
Hans de Goedee2997a72008-04-23 08:09:12 -03001562 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1563 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001564 if (!gspca_dev->present) {
1565 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001566 goto out;
1567 }
1568 if (gspca_dev->capt_file != file) {
1569 ret = -EINVAL;
1570 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001571 }
1572
Harvey Harrisonefab8212008-07-05 06:12:47 -03001573 frame = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001574 for (i = 0; i < gspca_dev->nframes; ++i) {
1575 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1576 PDEBUG(D_STREAM, "mmap bad memory type");
1577 break;
1578 }
1579 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1580 == vma->vm_pgoff) {
1581 frame = &gspca_dev->frame[i];
1582 break;
1583 }
1584 }
Harvey Harrisonefab8212008-07-05 06:12:47 -03001585 if (frame == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001586 PDEBUG(D_STREAM, "mmap no frame buffer found");
1587 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001588 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001589 }
1590#ifdef CONFIG_VIDEO_V4L1_COMPAT
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001591 /* v4l1 maps all the buffers */
1592 if (i != 0
1593 || size != frame->v4l2_buf.length * gspca_dev->nframes)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001594#endif
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001595 if (size != frame->v4l2_buf.length) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001596 PDEBUG(D_STREAM, "mmap bad size");
1597 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001598 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001599 }
1600
1601 /*
1602 * - VM_IO marks the area as being a mmaped region for I/O to a
1603 * device. It also prevents the region from being core dumped.
1604 */
1605 vma->vm_flags |= VM_IO;
1606
1607 addr = (unsigned long) frame->data;
1608 while (size > 0) {
1609 page = vmalloc_to_page((void *) addr);
1610 ret = vm_insert_page(vma, start, page);
1611 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001612 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001613 start += PAGE_SIZE;
1614 addr += PAGE_SIZE;
1615 size -= PAGE_SIZE;
1616 }
1617
1618 vma->vm_ops = &gspca_vm_ops;
1619 vma->vm_private_data = frame;
1620 gspca_vm_open(vma);
Hans de Goedee2997a72008-04-23 08:09:12 -03001621 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001622out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001623 mutex_unlock(&gspca_dev->queue_lock);
1624 return ret;
1625}
1626
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001627/*
1628 * wait for a video frame
1629 *
1630 * If a frame is ready, its index is returned.
1631 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001632static int frame_wait(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001633 int nonblock_ing)
1634{
1635 struct gspca_frame *frame;
1636 int i, j, ret;
1637
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001638 /* check if a frame is ready */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001639 i = gspca_dev->fr_o;
1640 j = gspca_dev->fr_queue[i];
1641 frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001642
Hans de Goede181b7042008-11-19 17:16:26 -03001643 if (!(frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)) {
1644 if (nonblock_ing)
1645 return -EAGAIN;
1646
1647 /* wait till a frame is ready */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001648 ret = wait_event_interruptible_timeout(gspca_dev->wq,
Hans de Goede181b7042008-11-19 17:16:26 -03001649 (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) ||
1650 !gspca_dev->streaming || !gspca_dev->present,
1651 msecs_to_jiffies(3000));
1652 if (ret < 0)
1653 return ret;
1654 if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present)
Hans de Goedee2997a72008-04-23 08:09:12 -03001655 return -EIO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001656 }
Hans de Goede181b7042008-11-19 17:16:26 -03001657
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001658 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1659 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1660 gspca_dev->fr_q,
1661 gspca_dev->fr_i,
1662 gspca_dev->fr_o);
Hans de Goedee2997a72008-04-23 08:09:12 -03001663
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001664 if (gspca_dev->sd_desc->dq_callback) {
1665 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001666 if (gspca_dev->present)
1667 gspca_dev->sd_desc->dq_callback(gspca_dev);
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001668 mutex_unlock(&gspca_dev->usb_lock);
1669 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001670 return j;
1671}
1672
1673/*
1674 * dequeue a video buffer
1675 *
1676 * If nonblock_ing is false, block until a buffer is available.
1677 */
1678static int vidioc_dqbuf(struct file *file, void *priv,
1679 struct v4l2_buffer *v4l2_buf)
1680{
1681 struct gspca_dev *gspca_dev = priv;
1682 struct gspca_frame *frame;
1683 int i, ret;
1684
1685 PDEBUG(D_FRAM, "dqbuf");
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001686 if (v4l2_buf->memory != gspca_dev->memory)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001687 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001688
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001689 if (!gspca_dev->present)
1690 return -ENODEV;
1691
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001692 /* if not streaming, be sure the application will not loop forever */
1693 if (!(file->f_flags & O_NONBLOCK)
1694 && !gspca_dev->streaming && gspca_dev->users == 1)
1695 return -EINVAL;
1696
1697 /* only the capturing file may dequeue */
1698 if (gspca_dev->capt_file != file)
1699 return -EINVAL;
1700
1701 /* only one dequeue / read at a time */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001702 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1703 return -ERESTARTSYS;
1704
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001705 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001706 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001707 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001708 i = ret; /* frame index */
1709 frame = &gspca_dev->frame[i];
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001710 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001711 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001712 frame->data,
1713 frame->v4l2_buf.bytesused)) {
1714 PDEBUG(D_ERR|D_STREAM,
1715 "dqbuf cp to user failed");
1716 ret = -EFAULT;
1717 goto out;
1718 }
1719 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001720 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1721 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1722 PDEBUG(D_FRAM, "dqbuf %d", i);
1723 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001724out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001725 mutex_unlock(&gspca_dev->read_lock);
1726 return ret;
1727}
1728
1729/*
1730 * queue a video buffer
1731 *
1732 * Attempting to queue a buffer that has already been
1733 * queued will return -EINVAL.
1734 */
1735static int vidioc_qbuf(struct file *file, void *priv,
1736 struct v4l2_buffer *v4l2_buf)
1737{
1738 struct gspca_dev *gspca_dev = priv;
1739 struct gspca_frame *frame;
1740 int i, index, ret;
1741
1742 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001743
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001744 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1745 return -ERESTARTSYS;
1746
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001747 index = v4l2_buf->index;
1748 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001749 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001750 "qbuf idx %d >= %d", index, gspca_dev->nframes);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001751 ret = -EINVAL;
1752 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001753 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001754 if (v4l2_buf->memory != gspca_dev->memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001755 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001756 ret = -EINVAL;
1757 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001758 }
1759
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001760 frame = &gspca_dev->frame[index];
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001761 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1762 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001763 ret = -EINVAL;
1764 goto out;
1765 }
1766
1767 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001768
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001769 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001770 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1771 frame->v4l2_buf.length = v4l2_buf->length;
1772 }
1773
1774 /* put the buffer in the 'queued' queue */
1775 i = gspca_dev->fr_q;
1776 gspca_dev->fr_queue[i] = index;
1777 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1778 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1779 gspca_dev->fr_q,
1780 gspca_dev->fr_i,
1781 gspca_dev->fr_o);
1782
1783 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1784 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1785 ret = 0;
1786out:
1787 mutex_unlock(&gspca_dev->queue_lock);
1788 return ret;
1789}
1790
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001791/*
1792 * allocate the resources for read()
1793 */
1794static int read_alloc(struct gspca_dev *gspca_dev,
1795 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001796{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001797 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001798 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001799
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001800 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001801 if (gspca_dev->nframes == 0) {
1802 struct v4l2_requestbuffers rb;
1803
1804 memset(&rb, 0, sizeof rb);
1805 rb.count = gspca_dev->nbufread;
1806 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001807 rb.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001808 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1809 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001810 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001811 return ret;
1812 }
1813 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1814 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001815 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001816 for (i = 0; i < gspca_dev->nbufread; i++) {
1817 v4l2_buf.index = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001818 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1819 if (ret != 0) {
1820 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1821 return ret;
1822 }
1823 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001824 gspca_dev->memory = GSPCA_MEMORY_READ;
1825 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001826
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001827 /* start streaming */
1828 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1829 if (ret != 0)
1830 PDEBUG(D_STREAM, "read streamon err %d", ret);
1831 return ret;
1832}
1833
1834static unsigned int dev_poll(struct file *file, poll_table *wait)
1835{
1836 struct gspca_dev *gspca_dev = file->private_data;
1837 int i, ret;
1838
1839 PDEBUG(D_FRAM, "poll");
1840
1841 poll_wait(file, &gspca_dev->wq, wait);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001842
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001843 /* if reqbufs is not done, the user would use read() */
1844 if (gspca_dev->nframes == 0) {
1845 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1846 return POLLERR; /* not the 1st time */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001847 ret = read_alloc(gspca_dev, file);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001848 if (ret != 0)
1849 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001850 }
1851
1852 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1853 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001854
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001855 /* check the next incoming buffer */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001856 i = gspca_dev->fr_o;
1857 i = gspca_dev->fr_queue[i];
1858 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1859 ret = POLLIN | POLLRDNORM; /* something to read */
1860 else
1861 ret = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001862 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001863 if (!gspca_dev->present)
1864 return POLLHUP;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001865 return ret;
1866}
1867
1868static ssize_t dev_read(struct file *file, char __user *data,
1869 size_t count, loff_t *ppos)
1870{
1871 struct gspca_dev *gspca_dev = file->private_data;
1872 struct gspca_frame *frame;
1873 struct v4l2_buffer v4l2_buf;
1874 struct timeval timestamp;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001875 int n, ret, ret2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001876
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001877 PDEBUG(D_FRAM, "read (%zd)", count);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001878 if (!gspca_dev->present)
1879 return -ENODEV;
1880 switch (gspca_dev->memory) {
1881 case GSPCA_MEMORY_NO: /* first time */
1882 ret = read_alloc(gspca_dev, file);
1883 if (ret != 0)
1884 return ret;
1885 break;
1886 case GSPCA_MEMORY_READ:
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001887 if (gspca_dev->capt_file == file)
1888 break;
1889 /* fall thru */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001890 default:
1891 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001892 }
1893
1894 /* get a frame */
1895 jiffies_to_timeval(get_jiffies_64(), &timestamp);
1896 timestamp.tv_sec--;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001897 n = 2;
1898 for (;;) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001899 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1900 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001901 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001902 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1903 if (ret != 0) {
1904 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1905 return ret;
1906 }
1907
1908 /* if the process slept for more than 1 second,
Frank Zago219423c2008-09-26 07:43:54 -03001909 * get a newer frame */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001910 frame = &gspca_dev->frame[v4l2_buf.index];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001911 if (--n < 0)
1912 break; /* avoid infinite loop */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001913 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1914 break;
1915 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1916 if (ret != 0) {
1917 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1918 return ret;
1919 }
1920 }
1921
1922 /* copy the frame */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001923 if (count > frame->v4l2_buf.bytesused)
1924 count = frame->v4l2_buf.bytesused;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001925 ret = copy_to_user(data, frame->data, count);
1926 if (ret != 0) {
1927 PDEBUG(D_ERR|D_STREAM,
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001928 "read cp to user lack %d / %zd", ret, count);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001929 ret = -EFAULT;
1930 goto out;
1931 }
1932 ret = count;
1933out:
1934 /* in each case, requeue the buffer */
1935 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1936 if (ret2 != 0)
1937 return ret2;
1938 return ret;
1939}
1940
Hans Verkuilbec43662008-12-30 06:58:20 -03001941static struct v4l2_file_operations dev_fops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001942 .owner = THIS_MODULE,
1943 .open = dev_open,
1944 .release = dev_close,
1945 .read = dev_read,
1946 .mmap = dev_mmap,
Hans Verkuilbec43662008-12-30 06:58:20 -03001947 .unlocked_ioctl = video_ioctl2,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001948 .poll = dev_poll,
1949};
1950
Hans Verkuila3998102008-07-21 02:57:38 -03001951static const struct v4l2_ioctl_ops dev_ioctl_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001952 .vidioc_querycap = vidioc_querycap,
1953 .vidioc_dqbuf = vidioc_dqbuf,
1954 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001955 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1956 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1957 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1958 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001959 .vidioc_streamon = vidioc_streamon,
1960 .vidioc_queryctrl = vidioc_queryctrl,
1961 .vidioc_g_ctrl = vidioc_g_ctrl,
1962 .vidioc_s_ctrl = vidioc_s_ctrl,
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03001963 .vidioc_g_audio = vidioc_g_audio,
1964 .vidioc_s_audio = vidioc_s_audio,
1965 .vidioc_enumaudio = vidioc_enumaudio,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001966 .vidioc_querymenu = vidioc_querymenu,
1967 .vidioc_enum_input = vidioc_enum_input,
1968 .vidioc_g_input = vidioc_g_input,
1969 .vidioc_s_input = vidioc_s_input,
1970 .vidioc_reqbufs = vidioc_reqbufs,
1971 .vidioc_querybuf = vidioc_querybuf,
1972 .vidioc_streamoff = vidioc_streamoff,
1973 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1974 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1975 .vidioc_g_parm = vidioc_g_parm,
1976 .vidioc_s_parm = vidioc_s_parm,
Hans de Goede11c635a2009-05-25 15:04:22 -03001977 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03001978#ifdef CONFIG_VIDEO_ADV_DEBUG
1979 .vidioc_g_register = vidioc_g_register,
1980 .vidioc_s_register = vidioc_s_register,
1981#endif
1982 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001983#ifdef CONFIG_VIDEO_V4L1_COMPAT
1984 .vidiocgmbuf = vidiocgmbuf,
1985#endif
1986};
1987
Hans Verkuila3998102008-07-21 02:57:38 -03001988static struct video_device gspca_template = {
1989 .name = "gspca main driver",
Hans Verkuila3998102008-07-21 02:57:38 -03001990 .fops = &dev_fops,
1991 .ioctl_ops = &dev_ioctl_ops,
Hans de Goeded0182302008-11-19 17:10:47 -03001992 .release = gspca_release,
Hans Verkuila3998102008-07-21 02:57:38 -03001993 .minor = -1,
1994};
1995
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001996/*
1997 * probe and create a new gspca device
1998 *
1999 * This function must be called by the sub-driver when it is
2000 * called for probing a new device.
2001 */
2002int gspca_dev_probe(struct usb_interface *intf,
2003 const struct usb_device_id *id,
2004 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002005 int dev_size,
2006 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002007{
2008 struct usb_interface_descriptor *interface;
2009 struct gspca_dev *gspca_dev;
2010 struct usb_device *dev = interface_to_usbdev(intf);
2011 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002012
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002013 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002014
2015 /* we don't handle multi-config cameras */
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002016 if (dev->descriptor.bNumConfigurations != 1) {
2017 PDEBUG(D_ERR, "Too many config");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002018 return -ENODEV;
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002019 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002020 interface = &intf->cur_altsetting->desc;
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002021 if (interface->bInterfaceNumber > 0) {
2022 PDEBUG(D_ERR, "intf != 0");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002023 return -ENODEV;
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002024 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002025
2026 /* create the device */
2027 if (dev_size < sizeof *gspca_dev)
2028 dev_size = sizeof *gspca_dev;
2029 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
Erik Andrene293e592008-10-03 08:46:50 -03002030 if (!gspca_dev) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002031 err("couldn't kzalloc gspca struct");
Erik Andrene293e592008-10-03 08:46:50 -03002032 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002033 }
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002034 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2035 if (!gspca_dev->usb_buf) {
2036 err("out of memory");
Erik Andrene293e592008-10-03 08:46:50 -03002037 ret = -ENOMEM;
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002038 goto out;
2039 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002040 gspca_dev->dev = dev;
2041 gspca_dev->iface = interface->bInterfaceNumber;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002042 gspca_dev->nbalt = intf->num_altsetting;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002043 gspca_dev->sd_desc = sd_desc;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002044 gspca_dev->nbufread = 2;
Jean-Francois Moineff374742008-10-23 07:29:51 -03002045 gspca_dev->empty_packet = -1; /* don't check the empty packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002046
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002047 /* configure the subdriver and initialize the USB device */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002048 ret = sd_desc->config(gspca_dev, id);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002049 if (ret < 0)
2050 goto out;
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002051 ret = sd_desc->init(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002052 if (ret < 0)
2053 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002054 ret = gspca_set_alt0(gspca_dev);
2055 if (ret < 0)
2056 goto out;
2057 gspca_set_default_mode(gspca_dev);
2058
2059 mutex_init(&gspca_dev->usb_lock);
2060 mutex_init(&gspca_dev->read_lock);
2061 mutex_init(&gspca_dev->queue_lock);
2062 init_waitqueue_head(&gspca_dev->wq);
2063
2064 /* init video stuff */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002065 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
Hans de Goede24441632009-03-27 16:08:20 -03002066 gspca_dev->vdev.parent = &intf->dev;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03002067 gspca_dev->module = module;
Oliver Neukumee56a4d2008-07-27 14:01:59 -03002068 gspca_dev->present = 1;
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002069 ret = video_register_device(&gspca_dev->vdev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002070 VFL_TYPE_GRABBER,
Jean-Francois Moine8d584a52009-01-13 06:07:59 -03002071 -1);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002072 if (ret < 0) {
2073 err("video_register_device err %d", ret);
2074 goto out;
2075 }
2076
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002077 usb_set_intfdata(intf, gspca_dev);
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002078 PDEBUG(D_PROBE, "/dev/video%d created", gspca_dev->vdev.num);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002079 return 0;
2080out:
Jean-Francois Moinee07a1d82008-11-19 06:37:53 -03002081 kfree(gspca_dev->usb_buf);
2082 kfree(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002083 return ret;
2084}
2085EXPORT_SYMBOL(gspca_dev_probe);
2086
2087/*
2088 * USB disconnection
2089 *
2090 * This function must be called by the sub-driver
2091 * when the device disconnects, after the specific resources are freed.
2092 */
2093void gspca_disconnect(struct usb_interface *intf)
2094{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002095 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002096
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002097 PDEBUG(D_PROBE, "/dev/video%d disconnect", gspca_dev->vdev.num);
Adam Bakerad281272009-02-04 15:33:21 -03002098 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03002099 gspca_dev->present = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002100
2101 if (gspca_dev->streaming) {
2102 destroy_urbs(gspca_dev);
2103 wake_up_interruptible(&gspca_dev->wq);
2104 }
2105
2106 /* the device is freed at exit of this function */
2107 gspca_dev->dev = NULL;
Adam Bakerad281272009-02-04 15:33:21 -03002108 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03002109
Frank Zago6b060ff2008-09-28 08:12:22 -03002110 usb_set_intfdata(intf, NULL);
2111
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002112 /* release the device */
2113 /* (this will call gspca_release() immediatly or on last close) */
2114 video_unregister_device(&gspca_dev->vdev);
Jean-Francois Moine3145b8c2008-10-05 04:22:10 -03002115
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002116/* PDEBUG(D_PROBE, "disconnect complete"); */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002117}
2118EXPORT_SYMBOL(gspca_disconnect);
2119
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002120#ifdef CONFIG_PM
2121int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2122{
2123 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2124
2125 if (!gspca_dev->streaming)
2126 return 0;
2127 gspca_dev->frozen = 1; /* avoid urb error messages */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002128 if (gspca_dev->sd_desc->stopN)
2129 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002130 destroy_urbs(gspca_dev);
2131 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002132 if (gspca_dev->sd_desc->stop0)
2133 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002134 return 0;
2135}
2136EXPORT_SYMBOL(gspca_suspend);
2137
2138int gspca_resume(struct usb_interface *intf)
2139{
2140 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2141
2142 gspca_dev->frozen = 0;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002143 gspca_dev->sd_desc->init(gspca_dev);
2144 if (gspca_dev->streaming)
2145 return gspca_init_transfer(gspca_dev);
Jean-Francois Moine1e89e2da2008-09-03 17:12:15 -03002146 return 0;
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002147}
2148EXPORT_SYMBOL(gspca_resume);
2149#endif
Hans de Goededcef32372008-07-10 10:40:53 -03002150/* -- cam driver utility functions -- */
2151
2152/* auto gain and exposure algorithm based on the knee algorithm described here:
2153 http://ytse.tricolour.net/docs/LowLightOptimization.html
2154
2155 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2156 where changed. */
2157int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2158 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2159{
2160 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2161 const struct ctrl *gain_ctrl = NULL;
2162 const struct ctrl *exposure_ctrl = NULL;
2163 const struct ctrl *autogain_ctrl = NULL;
2164 int retval = 0;
2165
2166 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
2167 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2168 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2169 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2170 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2171 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2172 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2173 }
2174 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2175 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2176 "on cam without (auto)gain/exposure");
2177 return 0;
2178 }
2179
2180 if (gain_ctrl->get(gspca_dev, &gain) ||
2181 exposure_ctrl->get(gspca_dev, &exposure) ||
2182 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2183 return 0;
2184
2185 orig_gain = gain;
2186 orig_exposure = exposure;
2187
2188 /* If we are of a multiple of deadzone, do multiple steps to reach the
2189 desired lumination fast (with the risc of a slight overshoot) */
2190 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2191
Jean-Francois Moine1c44d812008-11-10 05:56:55 -03002192 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
Hans de Goededcef32372008-07-10 10:40:53 -03002193 avg_lum, desired_avg_lum, steps);
2194
2195 for (i = 0; i < steps; i++) {
2196 if (avg_lum > desired_avg_lum) {
2197 if (gain > gain_knee)
2198 gain--;
2199 else if (exposure > exposure_knee)
2200 exposure--;
2201 else if (gain > gain_ctrl->qctrl.default_value)
2202 gain--;
2203 else if (exposure > exposure_ctrl->qctrl.minimum)
2204 exposure--;
2205 else if (gain > gain_ctrl->qctrl.minimum)
2206 gain--;
2207 else
2208 break;
2209 } else {
2210 if (gain < gain_ctrl->qctrl.default_value)
2211 gain++;
2212 else if (exposure < exposure_knee)
2213 exposure++;
2214 else if (gain < gain_knee)
2215 gain++;
2216 else if (exposure < exposure_ctrl->qctrl.maximum)
2217 exposure++;
2218 else if (gain < gain_ctrl->qctrl.maximum)
2219 gain++;
2220 else
2221 break;
2222 }
2223 }
2224
2225 if (gain != orig_gain) {
2226 gain_ctrl->set(gspca_dev, gain);
2227 retval = 1;
2228 }
2229 if (exposure != orig_exposure) {
2230 exposure_ctrl->set(gspca_dev, exposure);
2231 retval = 1;
2232 }
2233
2234 return retval;
2235}
2236EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2237
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002238/* -- module insert / remove -- */
2239static int __init gspca_init(void)
2240{
Jean-Francois Moinebb64e862008-07-19 06:49:28 -03002241 info("main v%d.%d.%d registered",
2242 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2243 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2244 DRIVER_VERSION_NUMBER & 0xff);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002245 return 0;
2246}
2247static void __exit gspca_exit(void)
2248{
2249 info("main deregistered");
2250}
2251
2252module_init(gspca_init);
2253module_exit(gspca_exit);
2254
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03002255#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002256module_param_named(debug, gspca_debug, int, 0644);
2257MODULE_PARM_DESC(debug,
2258 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002259 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
2260 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002261#endif