blob: b8561dfb6c8c7bc08880a9057239f6854c582a5f [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 Moine11bd1992009-04-21 04:17:59 -030050#define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 6, 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030051
Jean-Francois Moine335b3f82008-07-30 04:53:02 -030052#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030053int gspca_debug = D_ERR | D_PROBE;
54EXPORT_SYMBOL(gspca_debug);
55
56static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
57{
58 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
59 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
60 txt,
61 pixfmt & 0xff,
62 (pixfmt >> 8) & 0xff,
63 (pixfmt >> 16) & 0xff,
64 pixfmt >> 24,
65 w, h);
66 } else {
67 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
68 txt,
69 pixfmt,
70 w, h);
71 }
72}
73#else
74#define PDEBUG_MODE(txt, pixfmt, w, h)
75#endif
76
Jean-Francois Moined43fa322008-06-12 10:58:58 -030077/* specific memory types - !! should different from V4L2_MEMORY_xxx */
78#define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
79#define GSPCA_MEMORY_READ 7
80
Jean-Francois Moined43fa322008-06-12 10:58:58 -030081#define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
Jean-Francois Moined43fa322008-06-12 10:58:58 -030082
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030083/*
84 * VMA operations.
85 */
86static void gspca_vm_open(struct vm_area_struct *vma)
87{
88 struct gspca_frame *frame = vma->vm_private_data;
89
90 frame->vma_use_count++;
91 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
92}
93
94static void gspca_vm_close(struct vm_area_struct *vma)
95{
96 struct gspca_frame *frame = vma->vm_private_data;
97
98 if (--frame->vma_use_count <= 0)
99 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
100}
101
102static struct vm_operations_struct gspca_vm_ops = {
103 .open = gspca_vm_open,
104 .close = gspca_vm_close,
105};
106
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300107/* get the current input frame buffer */
108struct gspca_frame *gspca_get_i_frame(struct gspca_dev *gspca_dev)
109{
110 struct gspca_frame *frame;
111 int i;
112
113 i = gspca_dev->fr_i;
114 i = gspca_dev->fr_queue[i];
115 frame = &gspca_dev->frame[i];
116 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
117 != V4L2_BUF_FLAG_QUEUED)
118 return NULL;
119 return frame;
120}
121EXPORT_SYMBOL(gspca_get_i_frame);
122
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300123/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300124 * fill a video frame from an URB and resubmit
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300125 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300126static void fill_frame(struct gspca_dev *gspca_dev,
127 struct urb *urb)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300128{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300129 struct gspca_frame *frame;
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300130 u8 *data; /* address of data in the iso message */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300131 int i, len, st;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300132 cam_pkt_op pkt_scan;
133
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300134 if (urb->status != 0) {
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300135 if (urb->status == -ESHUTDOWN)
136 return; /* disconnection */
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300137#ifdef CONFIG_PM
Jean-Francois Moine6a709742008-09-03 16:48:10 -0300138 if (!gspca_dev->frozen)
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300139#endif
Jean-Francois Moine6a709742008-09-03 16:48:10 -0300140 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300141 return;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300142 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300143 pkt_scan = gspca_dev->sd_desc->pkt_scan;
144 for (i = 0; i < urb->number_of_packets; i++) {
145
146 /* check the availability of the frame buffer */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300147 frame = gspca_get_i_frame(gspca_dev);
148 if (!frame) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300149 gspca_dev->last_packet_type = DISCARD_PACKET;
150 break;
151 }
152
153 /* check the packet status and length */
154 len = urb->iso_frame_desc[i].actual_length;
Jean-Francois Moineff374742008-10-23 07:29:51 -0300155 if (len == 0) {
156 if (gspca_dev->empty_packet == 0)
157 gspca_dev->empty_packet = 1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300158 continue;
Jean-Francois Moineff374742008-10-23 07:29:51 -0300159 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300160 st = urb->iso_frame_desc[i].status;
161 if (st) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300162 PDEBUG(D_ERR,
163 "ISOC data error: [%d] len=%d, status=%d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300164 i, len, st);
165 gspca_dev->last_packet_type = DISCARD_PACKET;
166 continue;
167 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300168
169 /* let the packet be analyzed by the subdriver */
170 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
171 i, urb->iso_frame_desc[i].offset, len);
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300172 data = (u8 *) urb->transfer_buffer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300173 + urb->iso_frame_desc[i].offset;
174 pkt_scan(gspca_dev, frame, data, len);
175 }
176
177 /* resubmit the URB */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300178 st = usb_submit_urb(urb, GFP_ATOMIC);
179 if (st < 0)
180 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
181}
182
183/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300184 * ISOC message interrupt from the USB device
185 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300186 * Analyse each packet and call the subdriver for copy to the frame buffer.
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300187 */
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300188static void isoc_irq(struct urb *urb)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300189{
190 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
191
Hans de Goede3647fea2008-07-15 05:36:30 -0300192 PDEBUG(D_PACK, "isoc irq");
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300193 if (!gspca_dev->streaming)
194 return;
195 fill_frame(gspca_dev, urb);
196}
197
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300198/*
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300199 * bulk message interrupt from the USB device
200 */
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300201static void bulk_irq(struct urb *urb)
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300202{
203 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
204 struct gspca_frame *frame;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300205 int st;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300206
207 PDEBUG(D_PACK, "bulk irq");
208 if (!gspca_dev->streaming)
209 return;
Jean-Francois Moine880ae242008-11-18 04:19:52 -0300210 switch (urb->status) {
211 case 0:
212 break;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300213 case -ESHUTDOWN:
214 return; /* disconnection */
Jean-Francois Moine880ae242008-11-18 04:19:52 -0300215 case -ECONNRESET:
216 urb->status = 0;
217 break;
218 default:
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300219#ifdef CONFIG_PM
220 if (!gspca_dev->frozen)
221#endif
222 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300223 return;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300224 }
225
226 /* check the availability of the frame buffer */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300227 frame = gspca_get_i_frame(gspca_dev);
228 if (!frame) {
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300229 gspca_dev->last_packet_type = DISCARD_PACKET;
230 } else {
231 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
232 gspca_dev->sd_desc->pkt_scan(gspca_dev,
233 frame,
234 urb->transfer_buffer,
235 urb->actual_length);
236 }
Antonio Ospitedff369a2008-11-14 07:53:32 -0300237
238 /* resubmit the URB */
239 if (gspca_dev->cam.bulk_nurbs != 0) {
240 st = usb_submit_urb(urb, GFP_ATOMIC);
241 if (st < 0)
242 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
243 }
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300244}
245
246/*
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300247 * add data to the current frame
248 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300249 * This function is called by the subdrivers at interrupt level.
250 *
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300251 * To build a frame, these ones must add
252 * - one FIRST_PACKET
253 * - 0 or many INTER_PACKETs
254 * - one LAST_PACKET
255 * DISCARD_PACKET invalidates the whole frame.
256 * On LAST_PACKET, a new frame is returned.
257 */
258struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
Erik Andrene293e592008-10-03 08:46:50 -0300259 enum gspca_packet_type packet_type,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300260 struct gspca_frame *frame,
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300261 const __u8 *data,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300262 int len)
263{
264 int i, j;
265
Hans de Goedee2997a72008-04-23 08:09:12 -0300266 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300267
268 /* when start of a new frame, if the current frame buffer
269 * is not queued, discard the whole frame */
270 if (packet_type == FIRST_PACKET) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300271 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
272 != V4L2_BUF_FLAG_QUEUED) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300273 gspca_dev->last_packet_type = DISCARD_PACKET;
274 return frame;
275 }
276 frame->data_end = frame->data;
277 jiffies_to_timeval(get_jiffies_64(),
278 &frame->v4l2_buf.timestamp);
279 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300280 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
Jean-Francois Moine06ca78f2008-07-22 03:45:08 -0300281 if (packet_type == LAST_PACKET)
282 gspca_dev->last_packet_type = packet_type;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300283 return frame;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300284 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300285
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300286 /* append the packet to the frame buffer */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300287 if (len > 0) {
288 if (frame->data_end - frame->data + len
289 > frame->v4l2_buf.length) {
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -0300290 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300291 frame->data_end - frame->data + len,
292 frame->v4l2_buf.length);
293 packet_type = DISCARD_PACKET;
294 } else {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300295 memcpy(frame->data_end, data, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300296 frame->data_end += len;
297 }
298 }
299 gspca_dev->last_packet_type = packet_type;
300
Frank Zago219423c2008-09-26 07:43:54 -0300301 /* if last packet, wake up the application and advance in the queue */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300302 if (packet_type == LAST_PACKET) {
303 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
304 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
305 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300306 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300307 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
308 gspca_dev->fr_i = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300309 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
310 frame->v4l2_buf.bytesused,
311 gspca_dev->fr_q,
312 i,
313 gspca_dev->fr_o);
314 j = gspca_dev->fr_queue[i];
315 frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300316 }
317 return frame;
318}
319EXPORT_SYMBOL(gspca_frame_add);
320
321static int gspca_is_compressed(__u32 format)
322{
323 switch (format) {
324 case V4L2_PIX_FMT_MJPEG:
325 case V4L2_PIX_FMT_JPEG:
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300326 case V4L2_PIX_FMT_SPCA561:
Hans de Goedeab8f12c2008-07-04 18:29:32 -0300327 case V4L2_PIX_FMT_PAC207:
Kyle Guinnd661e622009-01-16 05:36:14 -0300328 case V4L2_PIX_FMT_MR97310A:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300329 return 1;
330 }
331 return 0;
332}
333
334static void *rvmalloc(unsigned long size)
335{
336 void *mem;
337 unsigned long adr;
338
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300339 mem = vmalloc_32(size);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300340 if (mem != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300341 adr = (unsigned long) mem;
342 while ((long) size > 0) {
343 SetPageReserved(vmalloc_to_page((void *) adr));
344 adr += PAGE_SIZE;
345 size -= PAGE_SIZE;
346 }
347 }
348 return mem;
349}
350
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300351static void rvfree(void *mem, long size)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300352{
353 unsigned long adr;
354
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300355 adr = (unsigned long) mem;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300356 while (size > 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300357 ClearPageReserved(vmalloc_to_page((void *) adr));
358 adr += PAGE_SIZE;
359 size -= PAGE_SIZE;
360 }
361 vfree(mem);
362}
363
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300364static int frame_alloc(struct gspca_dev *gspca_dev,
365 unsigned int count)
366{
367 struct gspca_frame *frame;
368 unsigned int frsz;
369 int i;
370
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300371 i = gspca_dev->curr_mode;
372 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300373 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300374 frsz = PAGE_ALIGN(frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300375 gspca_dev->frsz = frsz;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300376 if (count > GSPCA_MAX_FRAMES)
377 count = GSPCA_MAX_FRAMES;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300378 gspca_dev->frbuf = rvmalloc(frsz * count);
379 if (!gspca_dev->frbuf) {
380 err("frame alloc failed");
381 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300382 }
383 gspca_dev->nframes = count;
384 for (i = 0; i < count; i++) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300385 frame = &gspca_dev->frame[i];
386 frame->v4l2_buf.index = i;
387 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
388 frame->v4l2_buf.flags = 0;
389 frame->v4l2_buf.field = V4L2_FIELD_NONE;
390 frame->v4l2_buf.length = frsz;
391 frame->v4l2_buf.memory = gspca_dev->memory;
392 frame->v4l2_buf.sequence = 0;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300393 frame->data = frame->data_end =
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300394 gspca_dev->frbuf + i * frsz;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300395 frame->v4l2_buf.m.offset = i * frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300396 }
397 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
398 gspca_dev->last_packet_type = DISCARD_PACKET;
399 gspca_dev->sequence = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300400 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300401}
402
403static void frame_free(struct gspca_dev *gspca_dev)
404{
405 int i;
406
407 PDEBUG(D_STREAM, "frame free");
Harvey Harrisonefab8212008-07-05 06:12:47 -0300408 if (gspca_dev->frbuf != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300409 rvfree(gspca_dev->frbuf,
410 gspca_dev->nframes * gspca_dev->frsz);
411 gspca_dev->frbuf = NULL;
412 for (i = 0; i < gspca_dev->nframes; i++)
413 gspca_dev->frame[i].data = NULL;
414 }
415 gspca_dev->nframes = 0;
416}
417
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300418static void destroy_urbs(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300419{
420 struct urb *urb;
421 unsigned int i;
422
423 PDEBUG(D_STREAM, "kill transfer");
Erik Andrén44732812008-10-13 15:52:46 -0300424 for (i = 0; i < MAX_NURBS; i++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300425 urb = gspca_dev->urb[i];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300426 if (urb == NULL)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300427 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300428
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300429 gspca_dev->urb[i] = NULL;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300430 usb_kill_urb(urb);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300431 if (urb->transfer_buffer != NULL)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300432 usb_buffer_free(gspca_dev->dev,
433 urb->transfer_buffer_length,
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300434 urb->transfer_buffer,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300435 urb->transfer_dma);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300436 usb_free_urb(urb);
437 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300438}
439
440/*
Frank Zago219423c2008-09-26 07:43:54 -0300441 * look for an input transfer endpoint in an alternate setting
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300442 */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300443static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
Jean-Francois 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 Moine0be01002008-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 */
Jean-Francois Moine0dc641d2009-03-19 05:55:22 -0300478 while (--i >= 0) {
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300479 ep = alt_xfer(&intf->altsetting[i], xfer);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300480 if (ep)
481 break;
482 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300483 if (ep == NULL) {
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300484 err("no transfer endpoint found");
485 return NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300486 }
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300487 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300488 i, ep->desc.bEndpointAddress);
Jean-Francois Moine8dd07ef2009-04-21 14:05:44 -0300489 if (gspca_dev->nbalt > 1) {
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300490 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
491 if (ret < 0) {
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300492 err("set alt %d err %d", i, ret);
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300493 return NULL;
494 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300495 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300496 gspca_dev->alt = i; /* memorize the current alt setting */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300497 return ep;
498}
499
500/*
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300501 * create the URBs for image transfer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300502 */
503static int create_urbs(struct gspca_dev *gspca_dev,
504 struct usb_host_endpoint *ep)
505{
506 struct urb *urb;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300507 int n, nurbs, i, psize, npkt, bsize;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300508
509 /* calculate the packet size and the number of packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300510 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300511
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300512 if (!gspca_dev->cam.bulk) { /* isoc */
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300513
514 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
515 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
Jean-Francois Moine49cb6b02009-04-25 13:29:01 -0300516 npkt = gspca_dev->cam.npkt;
517 if (npkt == 0)
518 npkt = 32; /* default value */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300519 bsize = psize * npkt;
520 PDEBUG(D_STREAM,
521 "isoc %d pkts size %d = bsize:%d",
522 npkt, psize, bsize);
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300523 nurbs = DEF_NURBS;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300524 } else { /* bulk */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300525 npkt = 0;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300526 bsize = gspca_dev->cam.bulk_size;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300527 if (bsize == 0)
528 bsize = psize;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300529 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300530 if (gspca_dev->cam.bulk_nurbs != 0)
531 nurbs = gspca_dev->cam.bulk_nurbs;
532 else
533 nurbs = 1;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300534 }
535
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300536 gspca_dev->nurbs = nurbs;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300537 for (n = 0; n < nurbs; n++) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300538 urb = usb_alloc_urb(npkt, GFP_KERNEL);
539 if (!urb) {
540 err("usb_alloc_urb failed");
Jean-Francois Moine0d0548e2008-09-05 12:49:54 -0300541 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300542 return -ENOMEM;
543 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300544 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300545 bsize,
546 GFP_KERNEL,
547 &urb->transfer_dma);
548
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300549 if (urb->transfer_buffer == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300550 usb_free_urb(urb);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300551 err("usb_buffer_urb failed");
Jean-Francois Moine0d0548e2008-09-05 12:49:54 -0300552 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300553 return -ENOMEM;
554 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300555 gspca_dev->urb[n] = urb;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300556 urb->dev = gspca_dev->dev;
557 urb->context = gspca_dev;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300558 urb->transfer_buffer_length = bsize;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300559 if (npkt != 0) { /* ISOC */
560 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
561 ep->desc.bEndpointAddress);
Frank Zago6c49da72008-09-28 07:43:00 -0300562 urb->transfer_flags = URB_ISO_ASAP
563 | URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300564 urb->interval = ep->desc.bInterval;
565 urb->complete = isoc_irq;
566 urb->number_of_packets = npkt;
567 for (i = 0; i < npkt; i++) {
568 urb->iso_frame_desc[i].length = psize;
569 urb->iso_frame_desc[i].offset = psize * i;
570 }
571 } else { /* bulk */
572 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
573 ep->desc.bEndpointAddress),
Frank Zago6c49da72008-09-28 07:43:00 -0300574 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300575 urb->complete = bulk_irq;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300576 }
577 }
578 return 0;
579}
580
581/*
582 * start the USB transfer
583 */
584static int gspca_init_transfer(struct gspca_dev *gspca_dev)
585{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300586 struct usb_host_endpoint *ep;
587 int n, ret;
588
Hans de Goedee2997a72008-04-23 08:09:12 -0300589 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
590 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300591
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300592 if (!gspca_dev->present) {
593 ret = -ENODEV;
594 goto out;
595 }
596
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300597 /* set the higher alternate setting and
598 * loop until urb submit succeeds */
599 gspca_dev->alt = gspca_dev->nbalt;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300600 for (;;) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300601 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300602 ep = get_ep(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300603 if (ep == NULL) {
604 ret = -EIO;
605 goto out;
606 }
607 ret = create_urbs(gspca_dev, ep);
608 if (ret < 0)
609 goto out;
610
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300611 /* clear the bulk endpoint */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300612 if (gspca_dev->cam.bulk)
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300613 usb_clear_halt(gspca_dev->dev,
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300614 gspca_dev->urb[0]->pipe);
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300615
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300616 /* start the cam */
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300617 ret = gspca_dev->sd_desc->start(gspca_dev);
618 if (ret < 0) {
619 destroy_urbs(gspca_dev);
620 goto out;
621 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300622 gspca_dev->streaming = 1;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300623
Antonio Ospitedff369a2008-11-14 07:53:32 -0300624 /* some bulk transfers are started by the subdriver */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300625 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300626 break;
627
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300628 /* submit the URBs */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300629 for (n = 0; n < gspca_dev->nurbs; n++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300630 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300631 if (ret < 0) {
632 PDEBUG(D_ERR|D_STREAM,
633 "usb_submit_urb [%d] err %d", n, ret);
Hans de Goedee2997a72008-04-23 08:09:12 -0300634 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300635 destroy_urbs(gspca_dev);
Jean-Francois Moine9edf1a62008-11-23 15:53:28 -0300636 if (ret == -ENOSPC) {
Jean-Francois Moineabf485f2008-11-24 06:38:21 -0300637 msleep(20); /* wait for kill
Jean-Francois Moine9edf1a62008-11-23 15:53:28 -0300638 * complete */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300639 break; /* try the previous alt */
Jean-Francois Moine9edf1a62008-11-23 15:53:28 -0300640 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300641 goto out;
642 }
643 }
644 if (ret >= 0)
645 break;
646 }
647out:
648 mutex_unlock(&gspca_dev->usb_lock);
649 return ret;
650}
651
652static int gspca_set_alt0(struct gspca_dev *gspca_dev)
653{
654 int ret;
655
Jean-Francois Moine8dd07ef2009-04-21 14:05:44 -0300656 if (gspca_dev->alt == 0)
657 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300658 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
659 if (ret < 0)
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -0300660 PDEBUG(D_ERR|D_STREAM, "set alt 0 err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300661 return ret;
662}
663
Frank Zago219423c2008-09-26 07:43:54 -0300664/* Note: both the queue and the usb locks should be held when calling this */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300665static void gspca_stream_off(struct gspca_dev *gspca_dev)
666{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300667 gspca_dev->streaming = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300668 if (gspca_dev->present) {
669 if (gspca_dev->sd_desc->stopN)
670 gspca_dev->sd_desc->stopN(gspca_dev);
671 destroy_urbs(gspca_dev);
Jean-Francois Moined08e2ce2009-01-20 16:21:44 -0300672 gspca_set_alt0(gspca_dev);
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300673 }
674
675 /* always call stop0 to free the subdriver's resources */
Jean-Francois Moine98522a72008-11-18 06:33:08 -0300676 if (gspca_dev->sd_desc->stop0)
677 gspca_dev->sd_desc->stop0(gspca_dev);
678 PDEBUG(D_STREAM, "stream off OK");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300679}
680
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300681static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300682{
683 int i;
684
685 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
686 gspca_dev->curr_mode = i;
687 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
688 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300689 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300690}
691
692static int wxh_to_mode(struct gspca_dev *gspca_dev,
693 int width, int height)
694{
695 int i;
696
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300697 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
698 if (width >= gspca_dev->cam.cam_mode[i].width
699 && height >= gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300700 break;
701 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300702 return i;
703}
704
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300705/*
706 * search a mode with the right pixel format
707 */
708static int gspca_get_mode(struct gspca_dev *gspca_dev,
709 int mode,
710 int pixfmt)
711{
712 int modeU, modeD;
713
714 modeU = modeD = mode;
715 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
716 if (--modeD >= 0) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300717 if (gspca_dev->cam.cam_mode[modeD].pixelformat
718 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300719 return modeD;
720 }
721 if (++modeU < gspca_dev->cam.nmodes) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300722 if (gspca_dev->cam.cam_mode[modeU].pixelformat
723 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300724 return modeU;
725 }
726 }
727 return -EINVAL;
728}
729
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300730#ifdef CONFIG_VIDEO_ADV_DEBUG
731static int vidioc_g_register(struct file *file, void *priv,
732 struct v4l2_dbg_register *reg)
733{
734 int ret;
735 struct gspca_dev *gspca_dev = priv;
736
737 if (!gspca_dev->sd_desc->get_chip_ident)
738 return -EINVAL;
739
740 if (!gspca_dev->sd_desc->get_register)
741 return -EINVAL;
742
743 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
744 return -ERESTARTSYS;
745 if (gspca_dev->present)
746 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
747 else
748 ret = -ENODEV;
749 mutex_unlock(&gspca_dev->usb_lock);
750
751 return ret;
752}
753
754static int vidioc_s_register(struct file *file, void *priv,
755 struct v4l2_dbg_register *reg)
756{
757 int ret;
758 struct gspca_dev *gspca_dev = priv;
759
760 if (!gspca_dev->sd_desc->get_chip_ident)
761 return -EINVAL;
762
763 if (!gspca_dev->sd_desc->set_register)
764 return -EINVAL;
765
766 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
767 return -ERESTARTSYS;
768 if (gspca_dev->present)
769 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
770 else
771 ret = -ENODEV;
772 mutex_unlock(&gspca_dev->usb_lock);
773
774 return ret;
775}
776#endif
777
778static int vidioc_g_chip_ident(struct file *file, void *priv,
779 struct v4l2_dbg_chip_ident *chip)
780{
781 int ret;
782 struct gspca_dev *gspca_dev = priv;
783
784 if (!gspca_dev->sd_desc->get_chip_ident)
785 return -EINVAL;
786
787 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
788 return -ERESTARTSYS;
789 if (gspca_dev->present)
790 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
791 else
792 ret = -ENODEV;
793 mutex_unlock(&gspca_dev->usb_lock);
794
795 return ret;
796}
797
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300798static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300799 struct v4l2_fmtdesc *fmtdesc)
800{
801 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300802 int i, j, index;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300803 __u32 fmt_tb[8];
804
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300805 /* give an index to each format */
806 index = 0;
807 j = 0;
808 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300809 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300810 j = 0;
811 for (;;) {
812 if (fmt_tb[j] == fmt_tb[index])
813 break;
814 j++;
815 }
816 if (j == index) {
817 if (fmtdesc->index == index)
818 break; /* new format */
819 index++;
Julia Lawall80297122008-11-12 23:18:21 -0300820 if (index >= ARRAY_SIZE(fmt_tb))
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300821 return -EINVAL;
822 }
823 }
824 if (i < 0)
825 return -EINVAL; /* no more format */
826
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300827 fmtdesc->pixelformat = fmt_tb[index];
828 if (gspca_is_compressed(fmt_tb[index]))
829 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
830 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
831 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
832 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
833 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
834 fmtdesc->description[4] = '\0';
835 return 0;
836}
837
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300838static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300839 struct v4l2_format *fmt)
840{
841 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300842 int mode;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300843
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300844 mode = gspca_dev->curr_mode;
845 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
846 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300847 return 0;
848}
849
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300850static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300851 struct v4l2_format *fmt)
852{
Hans de Goede24c530b2008-07-04 17:55:18 -0300853 int w, h, mode, mode2;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300854
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300855 w = fmt->fmt.pix.width;
856 h = fmt->fmt.pix.height;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300857
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300858#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300859 if (gspca_debug & D_CONF)
860 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
861#endif
862 /* search the closest mode for width and height */
863 mode = wxh_to_mode(gspca_dev, w, h);
864
865 /* OK if right palette */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300866 if (gspca_dev->cam.cam_mode[mode].pixelformat
867 != fmt->fmt.pix.pixelformat) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300868
869 /* else, search the closest mode with the same pixel format */
870 mode2 = gspca_get_mode(gspca_dev, mode,
871 fmt->fmt.pix.pixelformat);
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300872 if (mode2 >= 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300873 mode = mode2;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300874/* else
875 ; * no chance, return this mode */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300876 }
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300877 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
878 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300879 return mode; /* used when s_fmt */
880}
881
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300882static int vidioc_try_fmt_vid_cap(struct file *file,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300883 void *priv,
884 struct v4l2_format *fmt)
885{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300886 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300887 int ret;
888
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300889 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300890 if (ret < 0)
891 return ret;
892 return 0;
893}
894
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300895static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300896 struct v4l2_format *fmt)
897{
898 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300899 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300900
Hans de Goedee2997a72008-04-23 08:09:12 -0300901 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
902 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300903
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300904 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300905 if (ret < 0)
906 goto out;
907
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300908 if (gspca_dev->nframes != 0
909 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
910 ret = -EINVAL;
911 goto out;
912 }
913
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300914 if (ret == gspca_dev->curr_mode) {
915 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300916 goto out; /* same mode */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300917 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300918
919 if (gspca_dev->streaming) {
920 ret = -EBUSY;
921 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -0300922 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300923 gspca_dev->width = fmt->fmt.pix.width;
924 gspca_dev->height = fmt->fmt.pix.height;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300925 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
926 gspca_dev->curr_mode = ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300927
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300928 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300929out:
930 mutex_unlock(&gspca_dev->queue_lock);
931 return ret;
932}
933
Hans de Goede11c635a2009-05-25 15:04:22 -0300934static int vidioc_enum_framesizes(struct file *file, void *priv,
935 struct v4l2_frmsizeenum *fsize)
936{
937 struct gspca_dev *gspca_dev = priv;
938 int i;
939 __u32 index = 0;
940
941 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
942 if (fsize->pixel_format !=
943 gspca_dev->cam.cam_mode[i].pixelformat)
944 continue;
945
946 if (fsize->index == index) {
947 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
948 fsize->discrete.width =
949 gspca_dev->cam.cam_mode[i].width;
950 fsize->discrete.height =
951 gspca_dev->cam.cam_mode[i].height;
952 return 0;
953 }
954 index++;
955 }
956
957 return -EINVAL;
958}
959
Hans de Goeded0182302008-11-19 17:10:47 -0300960static void gspca_release(struct video_device *vfd)
Frank Zago6b060ff2008-09-28 08:12:22 -0300961{
Hans de Goeded0182302008-11-19 17:10:47 -0300962 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
Frank Zago6b060ff2008-09-28 08:12:22 -0300963
Hans de Goeded0182302008-11-19 17:10:47 -0300964 PDEBUG(D_STREAM, "device released");
Frank Zago6b060ff2008-09-28 08:12:22 -0300965
966 kfree(gspca_dev->usb_buf);
967 kfree(gspca_dev);
968}
969
Hans Verkuilbec43662008-12-30 06:58:20 -0300970static int dev_open(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300971{
972 struct gspca_dev *gspca_dev;
973 int ret;
974
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300975 PDEBUG(D_STREAM, "%s open", current->comm);
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -0300976 gspca_dev = (struct gspca_dev *) video_devdata(file);
Hans de Goedee2997a72008-04-23 08:09:12 -0300977 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
978 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300979 if (!gspca_dev->present) {
980 ret = -ENODEV;
981 goto out;
982 }
983
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300984 if (gspca_dev->users > 4) { /* (arbitrary value) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300985 ret = -EBUSY;
986 goto out;
987 }
Jean-Francois Moine5c4fa002008-11-18 15:52:31 -0300988
989 /* protect the subdriver against rmmod */
990 if (!try_module_get(gspca_dev->module)) {
991 ret = -ENODEV;
992 goto out;
993 }
994
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300995 gspca_dev->users++;
Frank Zago6b060ff2008-09-28 08:12:22 -0300996
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300997 file->private_data = gspca_dev;
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300998#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300999 /* activate the v4l2 debug */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001000 if (gspca_debug & D_V4L2)
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001001 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001002 | V4L2_DEBUG_IOCTL_ARG;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001003 else
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001004 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001005 | V4L2_DEBUG_IOCTL_ARG);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001006#endif
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001007 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001008out:
1009 mutex_unlock(&gspca_dev->queue_lock);
1010 if (ret != 0)
1011 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
1012 else
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001013 PDEBUG(D_STREAM, "open done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001014 return ret;
1015}
1016
Hans Verkuilbec43662008-12-30 06:58:20 -03001017static int dev_close(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001018{
1019 struct gspca_dev *gspca_dev = file->private_data;
1020
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001021 PDEBUG(D_STREAM, "%s close", current->comm);
Hans de Goedee2997a72008-04-23 08:09:12 -03001022 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1023 return -ERESTARTSYS;
1024 gspca_dev->users--;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001025
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001026 /* if the file did the capture, free the streaming resources */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001027 if (gspca_dev->capt_file == file) {
Jean-Francois Moinea1e82882008-09-03 17:12:17 -03001028 if (gspca_dev->streaming) {
1029 mutex_lock(&gspca_dev->usb_lock);
1030 gspca_stream_off(gspca_dev);
1031 mutex_unlock(&gspca_dev->usb_lock);
1032 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001033 frame_free(gspca_dev);
Harvey Harrisonefab8212008-07-05 06:12:47 -03001034 gspca_dev->capt_file = NULL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001035 gspca_dev->memory = GSPCA_MEMORY_NO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001036 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001037 file->private_data = NULL;
Jean-Francois Moine5c4fa002008-11-18 15:52:31 -03001038 module_put(gspca_dev->module);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001039 mutex_unlock(&gspca_dev->queue_lock);
Frank Zago6b060ff2008-09-28 08:12:22 -03001040
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001041 PDEBUG(D_STREAM, "close done");
Frank Zago6b060ff2008-09-28 08:12:22 -03001042
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001043 return 0;
1044}
1045
1046static int vidioc_querycap(struct file *file, void *priv,
1047 struct v4l2_capability *cap)
1048{
1049 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001050 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001051
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001052 /* protect the access to the usb device */
1053 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1054 return -ERESTARTSYS;
1055 if (!gspca_dev->present) {
1056 ret = -ENODEV;
1057 goto out;
1058 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001059 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -03001060 if (gspca_dev->dev->product != NULL) {
1061 strncpy(cap->card, gspca_dev->dev->product,
1062 sizeof cap->card);
1063 } else {
1064 snprintf(cap->card, sizeof cap->card,
1065 "USB Camera (%04x:%04x)",
1066 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1067 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1068 }
Thierry MERLE59320282009-01-21 15:32:26 -03001069 usb_make_path(gspca_dev->dev, cap->bus_info, sizeof(cap->bus_info));
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001070 cap->version = DRIVER_VERSION_NUMBER;
1071 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1072 | V4L2_CAP_STREAMING
1073 | V4L2_CAP_READWRITE;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001074 ret = 0;
1075out:
1076 mutex_unlock(&gspca_dev->usb_lock);
1077 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001078}
1079
Hans de Goedea0001a22009-06-10 04:52:18 -03001080static const struct ctrl *get_ctrl(struct gspca_dev *gspca_dev,
1081 int id)
1082{
1083 const struct ctrl *ctrls;
1084 int i;
1085
1086 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1087 i < gspca_dev->sd_desc->nctrls;
1088 i++, ctrls++) {
1089 if (gspca_dev->ctrl_dis & (1 << i))
1090 continue;
1091 if (id == ctrls->qctrl.id)
1092 return ctrls;
1093 }
1094 return NULL;
1095}
1096
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001097static int vidioc_queryctrl(struct file *file, void *priv,
1098 struct v4l2_queryctrl *q_ctrl)
1099{
1100 struct gspca_dev *gspca_dev = priv;
Hans de Goedea0001a22009-06-10 04:52:18 -03001101 const struct ctrl *ctrls;
1102 int i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001103 u32 id;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001104
Hans de Goedea0001a22009-06-10 04:52:18 -03001105 ctrls = NULL;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001106 id = q_ctrl->id;
1107 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1108 id &= V4L2_CTRL_ID_MASK;
1109 id++;
1110 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001111 if (gspca_dev->ctrl_dis & (1 << i))
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001112 continue;
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001113 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001114 continue;
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001115 if (ctrls && gspca_dev->sd_desc->ctrls[i].qctrl.id
Hans de Goedea0001a22009-06-10 04:52:18 -03001116 > ctrls->qctrl.id)
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001117 continue;
Hans de Goedea0001a22009-06-10 04:52:18 -03001118 ctrls = &gspca_dev->sd_desc->ctrls[i];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001119 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001120 } else {
1121 ctrls = get_ctrl(gspca_dev, id);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001122 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001123 if (ctrls == NULL)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001124 return -EINVAL;
Hans de Goedea0001a22009-06-10 04:52:18 -03001125 memcpy(q_ctrl, ctrls, sizeof *q_ctrl);
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001126 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001127}
1128
1129static int vidioc_s_ctrl(struct file *file, void *priv,
1130 struct v4l2_control *ctrl)
1131{
1132 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001133 const struct ctrl *ctrls;
Hans de Goedea0001a22009-06-10 04:52:18 -03001134 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001135
Hans de Goedea0001a22009-06-10 04:52:18 -03001136 ctrls = get_ctrl(gspca_dev, ctrl->id);
1137 if (ctrls == NULL)
1138 return -EINVAL;
1139
1140 if (ctrl->value < ctrls->qctrl.minimum
1141 || ctrl->value > ctrls->qctrl.maximum)
1142 return -ERANGE;
1143 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1144 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1145 return -ERESTARTSYS;
1146 if (gspca_dev->present)
1147 ret = ctrls->set(gspca_dev, ctrl->value);
1148 else
1149 ret = -ENODEV;
1150 mutex_unlock(&gspca_dev->usb_lock);
1151 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001152}
1153
1154static int vidioc_g_ctrl(struct file *file, void *priv,
1155 struct v4l2_control *ctrl)
1156{
1157 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001158 const struct ctrl *ctrls;
Hans de Goedea0001a22009-06-10 04:52:18 -03001159 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001160
Hans de Goedea0001a22009-06-10 04:52:18 -03001161 ctrls = get_ctrl(gspca_dev, ctrl->id);
1162 if (ctrls == NULL)
1163 return -EINVAL;
1164
1165 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1166 return -ERESTARTSYS;
1167 if (gspca_dev->present)
1168 ret = ctrls->get(gspca_dev, &ctrl->value);
1169 else
1170 ret = -ENODEV;
1171 mutex_unlock(&gspca_dev->usb_lock);
1172 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001173}
1174
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03001175/*fixme: have an audio flag in gspca_dev?*/
1176static int vidioc_s_audio(struct file *file, void *priv,
1177 struct v4l2_audio *audio)
1178{
1179 if (audio->index != 0)
1180 return -EINVAL;
1181 return 0;
1182}
1183
1184static int vidioc_g_audio(struct file *file, void *priv,
1185 struct v4l2_audio *audio)
1186{
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03001187 strcpy(audio->name, "Microphone");
1188 return 0;
1189}
1190
1191static int vidioc_enumaudio(struct file *file, void *priv,
1192 struct v4l2_audio *audio)
1193{
1194 if (audio->index != 0)
1195 return -EINVAL;
1196
1197 strcpy(audio->name, "Microphone");
1198 audio->capability = 0;
1199 audio->mode = 0;
1200 return 0;
1201}
1202
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001203static int vidioc_querymenu(struct file *file, void *priv,
1204 struct v4l2_querymenu *qmenu)
1205{
1206 struct gspca_dev *gspca_dev = priv;
1207
1208 if (!gspca_dev->sd_desc->querymenu)
1209 return -EINVAL;
1210 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1211}
1212
1213static int vidioc_enum_input(struct file *file, void *priv,
1214 struct v4l2_input *input)
1215{
1216 struct gspca_dev *gspca_dev = priv;
1217
1218 if (input->index != 0)
1219 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001220 input->type = V4L2_INPUT_TYPE_CAMERA;
Adam Bakerdfa76fa2009-03-29 19:17:10 -03001221 input->status = gspca_dev->cam.input_flags;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001222 strncpy(input->name, gspca_dev->sd_desc->name,
1223 sizeof input->name);
1224 return 0;
1225}
1226
1227static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1228{
1229 *i = 0;
1230 return 0;
1231}
1232
1233static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1234{
1235 if (i > 0)
1236 return -EINVAL;
1237 return (0);
1238}
1239
1240static int vidioc_reqbufs(struct file *file, void *priv,
1241 struct v4l2_requestbuffers *rb)
1242{
1243 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001244 int i, ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001245
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001246 switch (rb->memory) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001247 case GSPCA_MEMORY_READ: /* (internal call) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001248 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001249 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001250 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001251 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001252 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001253 }
Hans de Goedee2997a72008-04-23 08:09:12 -03001254 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1255 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001256
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001257 if (gspca_dev->memory != GSPCA_MEMORY_NO
1258 && gspca_dev->memory != rb->memory) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001259 ret = -EBUSY;
1260 goto out;
1261 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001262
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001263 /* only one file may do the capture */
1264 if (gspca_dev->capt_file != NULL
1265 && gspca_dev->capt_file != file) {
1266 ret = -EBUSY;
1267 goto out;
1268 }
1269
1270 /* if allocated, the buffers must not be mapped */
1271 for (i = 0; i < gspca_dev->nframes; i++) {
1272 if (gspca_dev->frame[i].vma_use_count) {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001273 ret = -EBUSY;
1274 goto out;
1275 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001276 }
1277
1278 /* stop streaming */
1279 if (gspca_dev->streaming) {
1280 mutex_lock(&gspca_dev->usb_lock);
1281 gspca_stream_off(gspca_dev);
1282 mutex_unlock(&gspca_dev->usb_lock);
1283 }
1284
1285 /* free the previous allocated buffers, if any */
1286 if (gspca_dev->nframes != 0) {
1287 frame_free(gspca_dev);
1288 gspca_dev->capt_file = NULL;
1289 }
1290 if (rb->count == 0) /* unrequest */
1291 goto out;
1292 gspca_dev->memory = rb->memory;
1293 ret = frame_alloc(gspca_dev, rb->count);
1294 if (ret == 0) {
1295 rb->count = gspca_dev->nframes;
1296 gspca_dev->capt_file = file;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001297 }
1298out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001299 mutex_unlock(&gspca_dev->queue_lock);
1300 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1301 return ret;
1302}
1303
1304static int vidioc_querybuf(struct file *file, void *priv,
1305 struct v4l2_buffer *v4l2_buf)
1306{
1307 struct gspca_dev *gspca_dev = priv;
1308 struct gspca_frame *frame;
1309
Jean-Francois Moine13752bd2008-12-19 15:08:51 -03001310 if (v4l2_buf->index < 0
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001311 || v4l2_buf->index >= gspca_dev->nframes)
1312 return -EINVAL;
1313
1314 frame = &gspca_dev->frame[v4l2_buf->index];
1315 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1316 return 0;
1317}
1318
1319static int vidioc_streamon(struct file *file, void *priv,
1320 enum v4l2_buf_type buf_type)
1321{
1322 struct gspca_dev *gspca_dev = priv;
1323 int ret;
1324
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001325 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1326 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001327 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1328 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001329
Jean-Francois Moine1edabe72008-12-19 15:17:25 -03001330 if (gspca_dev->nframes == 0
1331 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001332 ret = -EINVAL;
1333 goto out;
1334 }
1335 if (!gspca_dev->streaming) {
1336 ret = gspca_init_transfer(gspca_dev);
1337 if (ret < 0)
1338 goto out;
1339 }
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001340#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001341 if (gspca_debug & D_STREAM) {
1342 PDEBUG_MODE("stream on OK",
1343 gspca_dev->pixfmt,
1344 gspca_dev->width,
1345 gspca_dev->height);
1346 }
1347#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001348 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001349out:
1350 mutex_unlock(&gspca_dev->queue_lock);
1351 return ret;
1352}
1353
1354static int vidioc_streamoff(struct file *file, void *priv,
1355 enum v4l2_buf_type buf_type)
1356{
1357 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001358 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001359
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001360 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1361 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001362 if (!gspca_dev->streaming)
1363 return 0;
1364 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1365 return -ERESTARTSYS;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001366
1367 /* stop streaming */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001368 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1369 ret = -ERESTARTSYS;
1370 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001371 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001372 gspca_stream_off(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001373 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001374
1375 /* empty the application queues */
1376 for (i = 0; i < gspca_dev->nframes; i++)
1377 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1378 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1379 gspca_dev->last_packet_type = DISCARD_PACKET;
1380 gspca_dev->sequence = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001381 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001382out:
1383 mutex_unlock(&gspca_dev->queue_lock);
1384 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001385}
1386
1387static int vidioc_g_jpegcomp(struct file *file, void *priv,
1388 struct v4l2_jpegcompression *jpegcomp)
1389{
1390 struct gspca_dev *gspca_dev = priv;
1391 int ret;
1392
1393 if (!gspca_dev->sd_desc->get_jcomp)
1394 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001395 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1396 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001397 if (gspca_dev->present)
1398 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1399 else
1400 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001401 mutex_unlock(&gspca_dev->usb_lock);
1402 return ret;
1403}
1404
1405static int vidioc_s_jpegcomp(struct file *file, void *priv,
1406 struct v4l2_jpegcompression *jpegcomp)
1407{
1408 struct gspca_dev *gspca_dev = priv;
1409 int ret;
1410
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001411 if (!gspca_dev->sd_desc->set_jcomp)
1412 return -EINVAL;
Jim Parisf86d4a92008-12-10 05:50:44 -03001413 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1414 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001415 if (gspca_dev->present)
1416 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1417 else
1418 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001419 mutex_unlock(&gspca_dev->usb_lock);
1420 return ret;
1421}
1422
1423static int vidioc_g_parm(struct file *filp, void *priv,
1424 struct v4l2_streamparm *parm)
1425{
1426 struct gspca_dev *gspca_dev = priv;
1427
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001428 parm->parm.capture.readbuffers = gspca_dev->nbufread;
Jim Paris627a5ef2008-12-10 06:02:42 -03001429
1430 if (gspca_dev->sd_desc->get_streamparm) {
1431 int ret;
1432
1433 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1434 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001435 if (gspca_dev->present)
1436 ret = gspca_dev->sd_desc->get_streamparm(gspca_dev,
1437 parm);
1438 else
1439 ret = -ENODEV;
Jim Paris627a5ef2008-12-10 06:02:42 -03001440 mutex_unlock(&gspca_dev->usb_lock);
1441 return ret;
1442 }
1443
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001444 return 0;
1445}
1446
1447static int vidioc_s_parm(struct file *filp, void *priv,
1448 struct v4l2_streamparm *parm)
1449{
1450 struct gspca_dev *gspca_dev = priv;
1451 int n;
1452
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001453 n = parm->parm.capture.readbuffers;
1454 if (n == 0 || n > GSPCA_MAX_FRAMES)
1455 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1456 else
1457 gspca_dev->nbufread = n;
Jim Paris627a5ef2008-12-10 06:02:42 -03001458
1459 if (gspca_dev->sd_desc->set_streamparm) {
1460 int ret;
1461
1462 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1463 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001464 if (gspca_dev->present)
1465 ret = gspca_dev->sd_desc->set_streamparm(gspca_dev,
1466 parm);
1467 else
1468 ret = -ENODEV;
Jim Paris627a5ef2008-12-10 06:02:42 -03001469 mutex_unlock(&gspca_dev->usb_lock);
1470 return ret;
1471 }
1472
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001473 return 0;
1474}
1475
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001476static int vidioc_s_std(struct file *filp, void *priv,
1477 v4l2_std_id *parm)
1478{
1479 return 0;
1480}
1481
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001482#ifdef CONFIG_VIDEO_V4L1_COMPAT
1483static int vidiocgmbuf(struct file *file, void *priv,
1484 struct video_mbuf *mbuf)
1485{
1486 struct gspca_dev *gspca_dev = file->private_data;
1487 int i;
1488
1489 PDEBUG(D_STREAM, "cgmbuf");
1490 if (gspca_dev->nframes == 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001491 int ret;
1492
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001493 {
1494 struct v4l2_format fmt;
1495
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001496 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1497 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1498 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1499 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1500 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001501 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001502 if (ret != 0)
1503 return ret;
1504 }
1505 {
1506 struct v4l2_requestbuffers rb;
1507
1508 memset(&rb, 0, sizeof rb);
1509 rb.count = 4;
1510 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1511 rb.memory = V4L2_MEMORY_MMAP;
1512 ret = vidioc_reqbufs(file, priv, &rb);
1513 if (ret != 0)
1514 return ret;
1515 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001516 }
1517 mbuf->frames = gspca_dev->nframes;
1518 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1519 for (i = 0; i < mbuf->frames; i++)
1520 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1521 return 0;
1522}
1523#endif
1524
1525static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1526{
1527 struct gspca_dev *gspca_dev = file->private_data;
Harvey Harrisonefab8212008-07-05 06:12:47 -03001528 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001529 struct page *page;
1530 unsigned long addr, start, size;
1531 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001532
1533 start = vma->vm_start;
1534 size = vma->vm_end - vma->vm_start;
1535 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1536
Hans de Goedee2997a72008-04-23 08:09:12 -03001537 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1538 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001539 if (!gspca_dev->present) {
1540 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001541 goto out;
1542 }
1543 if (gspca_dev->capt_file != file) {
1544 ret = -EINVAL;
1545 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001546 }
1547
Harvey Harrisonefab8212008-07-05 06:12:47 -03001548 frame = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001549 for (i = 0; i < gspca_dev->nframes; ++i) {
1550 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1551 PDEBUG(D_STREAM, "mmap bad memory type");
1552 break;
1553 }
1554 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1555 == vma->vm_pgoff) {
1556 frame = &gspca_dev->frame[i];
1557 break;
1558 }
1559 }
Harvey Harrisonefab8212008-07-05 06:12:47 -03001560 if (frame == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001561 PDEBUG(D_STREAM, "mmap no frame buffer found");
1562 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001563 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001564 }
1565#ifdef CONFIG_VIDEO_V4L1_COMPAT
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001566 /* v4l1 maps all the buffers */
1567 if (i != 0
1568 || size != frame->v4l2_buf.length * gspca_dev->nframes)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001569#endif
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001570 if (size != frame->v4l2_buf.length) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001571 PDEBUG(D_STREAM, "mmap bad size");
1572 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001573 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001574 }
1575
1576 /*
1577 * - VM_IO marks the area as being a mmaped region for I/O to a
1578 * device. It also prevents the region from being core dumped.
1579 */
1580 vma->vm_flags |= VM_IO;
1581
1582 addr = (unsigned long) frame->data;
1583 while (size > 0) {
1584 page = vmalloc_to_page((void *) addr);
1585 ret = vm_insert_page(vma, start, page);
1586 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001587 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001588 start += PAGE_SIZE;
1589 addr += PAGE_SIZE;
1590 size -= PAGE_SIZE;
1591 }
1592
1593 vma->vm_ops = &gspca_vm_ops;
1594 vma->vm_private_data = frame;
1595 gspca_vm_open(vma);
Hans de Goedee2997a72008-04-23 08:09:12 -03001596 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001597out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001598 mutex_unlock(&gspca_dev->queue_lock);
1599 return ret;
1600}
1601
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001602/*
1603 * wait for a video frame
1604 *
1605 * If a frame is ready, its index is returned.
1606 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001607static int frame_wait(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001608 int nonblock_ing)
1609{
1610 struct gspca_frame *frame;
1611 int i, j, ret;
1612
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001613 /* check if a frame is ready */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001614 i = gspca_dev->fr_o;
1615 j = gspca_dev->fr_queue[i];
1616 frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001617
Hans de Goede181b7042008-11-19 17:16:26 -03001618 if (!(frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)) {
1619 if (nonblock_ing)
1620 return -EAGAIN;
1621
1622 /* wait till a frame is ready */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001623 ret = wait_event_interruptible_timeout(gspca_dev->wq,
Hans de Goede181b7042008-11-19 17:16:26 -03001624 (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) ||
1625 !gspca_dev->streaming || !gspca_dev->present,
1626 msecs_to_jiffies(3000));
1627 if (ret < 0)
1628 return ret;
1629 if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present)
Hans de Goedee2997a72008-04-23 08:09:12 -03001630 return -EIO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001631 }
Hans de Goede181b7042008-11-19 17:16:26 -03001632
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001633 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1634 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1635 gspca_dev->fr_q,
1636 gspca_dev->fr_i,
1637 gspca_dev->fr_o);
Hans de Goedee2997a72008-04-23 08:09:12 -03001638
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001639 if (gspca_dev->sd_desc->dq_callback) {
1640 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001641 if (gspca_dev->present)
1642 gspca_dev->sd_desc->dq_callback(gspca_dev);
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001643 mutex_unlock(&gspca_dev->usb_lock);
1644 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001645 return j;
1646}
1647
1648/*
1649 * dequeue a video buffer
1650 *
1651 * If nonblock_ing is false, block until a buffer is available.
1652 */
1653static int vidioc_dqbuf(struct file *file, void *priv,
1654 struct v4l2_buffer *v4l2_buf)
1655{
1656 struct gspca_dev *gspca_dev = priv;
1657 struct gspca_frame *frame;
1658 int i, ret;
1659
1660 PDEBUG(D_FRAM, "dqbuf");
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001661 if (v4l2_buf->memory != gspca_dev->memory)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001662 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001663
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001664 if (!gspca_dev->present)
1665 return -ENODEV;
1666
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001667 /* if not streaming, be sure the application will not loop forever */
1668 if (!(file->f_flags & O_NONBLOCK)
1669 && !gspca_dev->streaming && gspca_dev->users == 1)
1670 return -EINVAL;
1671
1672 /* only the capturing file may dequeue */
1673 if (gspca_dev->capt_file != file)
1674 return -EINVAL;
1675
1676 /* only one dequeue / read at a time */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001677 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1678 return -ERESTARTSYS;
1679
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001680 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001681 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001682 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001683 i = ret; /* frame index */
1684 frame = &gspca_dev->frame[i];
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001685 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001686 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001687 frame->data,
1688 frame->v4l2_buf.bytesused)) {
1689 PDEBUG(D_ERR|D_STREAM,
1690 "dqbuf cp to user failed");
1691 ret = -EFAULT;
1692 goto out;
1693 }
1694 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001695 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1696 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1697 PDEBUG(D_FRAM, "dqbuf %d", i);
1698 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001699out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001700 mutex_unlock(&gspca_dev->read_lock);
1701 return ret;
1702}
1703
1704/*
1705 * queue a video buffer
1706 *
1707 * Attempting to queue a buffer that has already been
1708 * queued will return -EINVAL.
1709 */
1710static int vidioc_qbuf(struct file *file, void *priv,
1711 struct v4l2_buffer *v4l2_buf)
1712{
1713 struct gspca_dev *gspca_dev = priv;
1714 struct gspca_frame *frame;
1715 int i, index, ret;
1716
1717 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001718
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001719 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1720 return -ERESTARTSYS;
1721
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001722 index = v4l2_buf->index;
1723 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001724 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001725 "qbuf idx %d >= %d", index, gspca_dev->nframes);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001726 ret = -EINVAL;
1727 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001728 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001729 if (v4l2_buf->memory != gspca_dev->memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001730 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001731 ret = -EINVAL;
1732 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001733 }
1734
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001735 frame = &gspca_dev->frame[index];
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001736 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1737 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001738 ret = -EINVAL;
1739 goto out;
1740 }
1741
1742 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001743
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001744 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001745 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1746 frame->v4l2_buf.length = v4l2_buf->length;
1747 }
1748
1749 /* put the buffer in the 'queued' queue */
1750 i = gspca_dev->fr_q;
1751 gspca_dev->fr_queue[i] = index;
1752 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1753 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1754 gspca_dev->fr_q,
1755 gspca_dev->fr_i,
1756 gspca_dev->fr_o);
1757
1758 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1759 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1760 ret = 0;
1761out:
1762 mutex_unlock(&gspca_dev->queue_lock);
1763 return ret;
1764}
1765
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001766/*
1767 * allocate the resources for read()
1768 */
1769static int read_alloc(struct gspca_dev *gspca_dev,
1770 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001771{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001772 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001773 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001774
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001775 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001776 if (gspca_dev->nframes == 0) {
1777 struct v4l2_requestbuffers rb;
1778
1779 memset(&rb, 0, sizeof rb);
1780 rb.count = gspca_dev->nbufread;
1781 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001782 rb.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001783 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1784 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001785 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001786 return ret;
1787 }
1788 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1789 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001790 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001791 for (i = 0; i < gspca_dev->nbufread; i++) {
1792 v4l2_buf.index = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001793 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1794 if (ret != 0) {
1795 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1796 return ret;
1797 }
1798 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001799 gspca_dev->memory = GSPCA_MEMORY_READ;
1800 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001801
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001802 /* start streaming */
1803 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1804 if (ret != 0)
1805 PDEBUG(D_STREAM, "read streamon err %d", ret);
1806 return ret;
1807}
1808
1809static unsigned int dev_poll(struct file *file, poll_table *wait)
1810{
1811 struct gspca_dev *gspca_dev = file->private_data;
1812 int i, ret;
1813
1814 PDEBUG(D_FRAM, "poll");
1815
1816 poll_wait(file, &gspca_dev->wq, wait);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001817
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001818 /* if reqbufs is not done, the user would use read() */
1819 if (gspca_dev->nframes == 0) {
1820 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1821 return POLLERR; /* not the 1st time */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001822 ret = read_alloc(gspca_dev, file);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001823 if (ret != 0)
1824 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001825 }
1826
1827 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1828 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001829
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001830 /* check the next incoming buffer */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001831 i = gspca_dev->fr_o;
1832 i = gspca_dev->fr_queue[i];
1833 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1834 ret = POLLIN | POLLRDNORM; /* something to read */
1835 else
1836 ret = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001837 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001838 if (!gspca_dev->present)
1839 return POLLHUP;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001840 return ret;
1841}
1842
1843static ssize_t dev_read(struct file *file, char __user *data,
1844 size_t count, loff_t *ppos)
1845{
1846 struct gspca_dev *gspca_dev = file->private_data;
1847 struct gspca_frame *frame;
1848 struct v4l2_buffer v4l2_buf;
1849 struct timeval timestamp;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001850 int n, ret, ret2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001851
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001852 PDEBUG(D_FRAM, "read (%zd)", count);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001853 if (!gspca_dev->present)
1854 return -ENODEV;
1855 switch (gspca_dev->memory) {
1856 case GSPCA_MEMORY_NO: /* first time */
1857 ret = read_alloc(gspca_dev, file);
1858 if (ret != 0)
1859 return ret;
1860 break;
1861 case GSPCA_MEMORY_READ:
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001862 if (gspca_dev->capt_file == file)
1863 break;
1864 /* fall thru */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001865 default:
1866 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001867 }
1868
1869 /* get a frame */
1870 jiffies_to_timeval(get_jiffies_64(), &timestamp);
1871 timestamp.tv_sec--;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001872 n = 2;
1873 for (;;) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001874 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1875 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001876 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001877 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1878 if (ret != 0) {
1879 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1880 return ret;
1881 }
1882
1883 /* if the process slept for more than 1 second,
Frank Zago219423c2008-09-26 07:43:54 -03001884 * get a newer frame */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001885 frame = &gspca_dev->frame[v4l2_buf.index];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001886 if (--n < 0)
1887 break; /* avoid infinite loop */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001888 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1889 break;
1890 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1891 if (ret != 0) {
1892 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1893 return ret;
1894 }
1895 }
1896
1897 /* copy the frame */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001898 if (count > frame->v4l2_buf.bytesused)
1899 count = frame->v4l2_buf.bytesused;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001900 ret = copy_to_user(data, frame->data, count);
1901 if (ret != 0) {
1902 PDEBUG(D_ERR|D_STREAM,
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001903 "read cp to user lack %d / %zd", ret, count);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001904 ret = -EFAULT;
1905 goto out;
1906 }
1907 ret = count;
1908out:
1909 /* in each case, requeue the buffer */
1910 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1911 if (ret2 != 0)
1912 return ret2;
1913 return ret;
1914}
1915
Hans Verkuilbec43662008-12-30 06:58:20 -03001916static struct v4l2_file_operations dev_fops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001917 .owner = THIS_MODULE,
1918 .open = dev_open,
1919 .release = dev_close,
1920 .read = dev_read,
1921 .mmap = dev_mmap,
Hans Verkuilbec43662008-12-30 06:58:20 -03001922 .unlocked_ioctl = video_ioctl2,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001923 .poll = dev_poll,
1924};
1925
Hans Verkuila3998102008-07-21 02:57:38 -03001926static const struct v4l2_ioctl_ops dev_ioctl_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001927 .vidioc_querycap = vidioc_querycap,
1928 .vidioc_dqbuf = vidioc_dqbuf,
1929 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001930 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1931 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1932 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1933 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001934 .vidioc_streamon = vidioc_streamon,
1935 .vidioc_queryctrl = vidioc_queryctrl,
1936 .vidioc_g_ctrl = vidioc_g_ctrl,
1937 .vidioc_s_ctrl = vidioc_s_ctrl,
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03001938 .vidioc_g_audio = vidioc_g_audio,
1939 .vidioc_s_audio = vidioc_s_audio,
1940 .vidioc_enumaudio = vidioc_enumaudio,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001941 .vidioc_querymenu = vidioc_querymenu,
1942 .vidioc_enum_input = vidioc_enum_input,
1943 .vidioc_g_input = vidioc_g_input,
1944 .vidioc_s_input = vidioc_s_input,
1945 .vidioc_reqbufs = vidioc_reqbufs,
1946 .vidioc_querybuf = vidioc_querybuf,
1947 .vidioc_streamoff = vidioc_streamoff,
1948 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1949 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1950 .vidioc_g_parm = vidioc_g_parm,
1951 .vidioc_s_parm = vidioc_s_parm,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001952 .vidioc_s_std = vidioc_s_std,
Hans de Goede11c635a2009-05-25 15:04:22 -03001953 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03001954#ifdef CONFIG_VIDEO_ADV_DEBUG
1955 .vidioc_g_register = vidioc_g_register,
1956 .vidioc_s_register = vidioc_s_register,
1957#endif
1958 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001959#ifdef CONFIG_VIDEO_V4L1_COMPAT
1960 .vidiocgmbuf = vidiocgmbuf,
1961#endif
1962};
1963
Hans Verkuila3998102008-07-21 02:57:38 -03001964static struct video_device gspca_template = {
1965 .name = "gspca main driver",
Hans Verkuila3998102008-07-21 02:57:38 -03001966 .fops = &dev_fops,
1967 .ioctl_ops = &dev_ioctl_ops,
Hans de Goeded0182302008-11-19 17:10:47 -03001968 .release = gspca_release,
Hans Verkuila3998102008-07-21 02:57:38 -03001969 .minor = -1,
1970};
1971
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001972/*
1973 * probe and create a new gspca device
1974 *
1975 * This function must be called by the sub-driver when it is
1976 * called for probing a new device.
1977 */
1978int gspca_dev_probe(struct usb_interface *intf,
1979 const struct usb_device_id *id,
1980 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001981 int dev_size,
1982 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001983{
1984 struct usb_interface_descriptor *interface;
1985 struct gspca_dev *gspca_dev;
1986 struct usb_device *dev = interface_to_usbdev(intf);
1987 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001988
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001989 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001990
1991 /* we don't handle multi-config cameras */
1992 if (dev->descriptor.bNumConfigurations != 1)
1993 return -ENODEV;
1994 interface = &intf->cur_altsetting->desc;
1995 if (interface->bInterfaceNumber > 0)
1996 return -ENODEV;
1997
1998 /* create the device */
1999 if (dev_size < sizeof *gspca_dev)
2000 dev_size = sizeof *gspca_dev;
2001 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
Erik Andrene293e592008-10-03 08:46:50 -03002002 if (!gspca_dev) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002003 err("couldn't kzalloc gspca struct");
Erik Andrene293e592008-10-03 08:46:50 -03002004 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002005 }
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002006 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2007 if (!gspca_dev->usb_buf) {
2008 err("out of memory");
Erik Andrene293e592008-10-03 08:46:50 -03002009 ret = -ENOMEM;
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002010 goto out;
2011 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002012 gspca_dev->dev = dev;
2013 gspca_dev->iface = interface->bInterfaceNumber;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002014 gspca_dev->nbalt = intf->num_altsetting;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002015 gspca_dev->sd_desc = sd_desc;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002016 gspca_dev->nbufread = 2;
Jean-Francois Moineff374742008-10-23 07:29:51 -03002017 gspca_dev->empty_packet = -1; /* don't check the empty packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002018
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002019 /* configure the subdriver and initialize the USB device */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002020 ret = sd_desc->config(gspca_dev, id);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002021 if (ret < 0)
2022 goto out;
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002023 ret = sd_desc->init(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002024 if (ret < 0)
2025 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002026 ret = gspca_set_alt0(gspca_dev);
2027 if (ret < 0)
2028 goto out;
2029 gspca_set_default_mode(gspca_dev);
2030
2031 mutex_init(&gspca_dev->usb_lock);
2032 mutex_init(&gspca_dev->read_lock);
2033 mutex_init(&gspca_dev->queue_lock);
2034 init_waitqueue_head(&gspca_dev->wq);
2035
2036 /* init video stuff */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002037 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
Hans de Goede24441632009-03-27 16:08:20 -03002038 gspca_dev->vdev.parent = &intf->dev;
Jean-Francois Moine5c4fa002008-11-18 15:52:31 -03002039 gspca_dev->module = module;
Oliver Neukumee56a4d2008-07-27 14:01:59 -03002040 gspca_dev->present = 1;
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002041 ret = video_register_device(&gspca_dev->vdev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002042 VFL_TYPE_GRABBER,
Jean-Francois Moine8d584a52009-01-13 06:07:59 -03002043 -1);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002044 if (ret < 0) {
2045 err("video_register_device err %d", ret);
2046 goto out;
2047 }
2048
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002049 usb_set_intfdata(intf, gspca_dev);
2050 PDEBUG(D_PROBE, "probe ok");
2051 return 0;
2052out:
Jean-Francois Moinee07a1d82008-11-19 06:37:53 -03002053 kfree(gspca_dev->usb_buf);
2054 kfree(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002055 return ret;
2056}
2057EXPORT_SYMBOL(gspca_dev_probe);
2058
2059/*
2060 * USB disconnection
2061 *
2062 * This function must be called by the sub-driver
2063 * when the device disconnects, after the specific resources are freed.
2064 */
2065void gspca_disconnect(struct usb_interface *intf)
2066{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002067 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002068
Adam Bakerad281272009-02-04 15:33:21 -03002069 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03002070 gspca_dev->present = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002071
2072 if (gspca_dev->streaming) {
2073 destroy_urbs(gspca_dev);
2074 wake_up_interruptible(&gspca_dev->wq);
2075 }
2076
2077 /* the device is freed at exit of this function */
2078 gspca_dev->dev = NULL;
Adam Bakerad281272009-02-04 15:33:21 -03002079 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03002080
Frank Zago6b060ff2008-09-28 08:12:22 -03002081 usb_set_intfdata(intf, NULL);
2082
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002083 /* release the device */
2084 /* (this will call gspca_release() immediatly or on last close) */
2085 video_unregister_device(&gspca_dev->vdev);
Jean-Francois Moine3145b8c2008-10-05 04:22:10 -03002086
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002087 PDEBUG(D_PROBE, "disconnect complete");
2088}
2089EXPORT_SYMBOL(gspca_disconnect);
2090
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002091#ifdef CONFIG_PM
2092int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2093{
2094 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2095
2096 if (!gspca_dev->streaming)
2097 return 0;
2098 gspca_dev->frozen = 1; /* avoid urb error messages */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002099 if (gspca_dev->sd_desc->stopN)
2100 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002101 destroy_urbs(gspca_dev);
2102 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002103 if (gspca_dev->sd_desc->stop0)
2104 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002105 return 0;
2106}
2107EXPORT_SYMBOL(gspca_suspend);
2108
2109int gspca_resume(struct usb_interface *intf)
2110{
2111 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2112
2113 gspca_dev->frozen = 0;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002114 gspca_dev->sd_desc->init(gspca_dev);
2115 if (gspca_dev->streaming)
2116 return gspca_init_transfer(gspca_dev);
Jean-Francois Moine1e89e2d2008-09-03 17:12:15 -03002117 return 0;
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002118}
2119EXPORT_SYMBOL(gspca_resume);
2120#endif
Hans de Goededcef3232008-07-10 10:40:53 -03002121/* -- cam driver utility functions -- */
2122
2123/* auto gain and exposure algorithm based on the knee algorithm described here:
2124 http://ytse.tricolour.net/docs/LowLightOptimization.html
2125
2126 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2127 where changed. */
2128int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2129 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2130{
2131 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2132 const struct ctrl *gain_ctrl = NULL;
2133 const struct ctrl *exposure_ctrl = NULL;
2134 const struct ctrl *autogain_ctrl = NULL;
2135 int retval = 0;
2136
2137 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
2138 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2139 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2140 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2141 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2142 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2143 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2144 }
2145 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2146 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2147 "on cam without (auto)gain/exposure");
2148 return 0;
2149 }
2150
2151 if (gain_ctrl->get(gspca_dev, &gain) ||
2152 exposure_ctrl->get(gspca_dev, &exposure) ||
2153 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2154 return 0;
2155
2156 orig_gain = gain;
2157 orig_exposure = exposure;
2158
2159 /* If we are of a multiple of deadzone, do multiple steps to reach the
2160 desired lumination fast (with the risc of a slight overshoot) */
2161 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2162
Jean-Francois Moine1c44d812008-11-10 05:56:55 -03002163 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
Hans de Goededcef3232008-07-10 10:40:53 -03002164 avg_lum, desired_avg_lum, steps);
2165
2166 for (i = 0; i < steps; i++) {
2167 if (avg_lum > desired_avg_lum) {
2168 if (gain > gain_knee)
2169 gain--;
2170 else if (exposure > exposure_knee)
2171 exposure--;
2172 else if (gain > gain_ctrl->qctrl.default_value)
2173 gain--;
2174 else if (exposure > exposure_ctrl->qctrl.minimum)
2175 exposure--;
2176 else if (gain > gain_ctrl->qctrl.minimum)
2177 gain--;
2178 else
2179 break;
2180 } else {
2181 if (gain < gain_ctrl->qctrl.default_value)
2182 gain++;
2183 else if (exposure < exposure_knee)
2184 exposure++;
2185 else if (gain < gain_knee)
2186 gain++;
2187 else if (exposure < exposure_ctrl->qctrl.maximum)
2188 exposure++;
2189 else if (gain < gain_ctrl->qctrl.maximum)
2190 gain++;
2191 else
2192 break;
2193 }
2194 }
2195
2196 if (gain != orig_gain) {
2197 gain_ctrl->set(gspca_dev, gain);
2198 retval = 1;
2199 }
2200 if (exposure != orig_exposure) {
2201 exposure_ctrl->set(gspca_dev, exposure);
2202 retval = 1;
2203 }
2204
2205 return retval;
2206}
2207EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2208
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002209/* -- module insert / remove -- */
2210static int __init gspca_init(void)
2211{
Jean-Francois Moinebb64e862008-07-19 06:49:28 -03002212 info("main v%d.%d.%d registered",
2213 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2214 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2215 DRIVER_VERSION_NUMBER & 0xff);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002216 return 0;
2217}
2218static void __exit gspca_exit(void)
2219{
2220 info("main deregistered");
2221}
2222
2223module_init(gspca_init);
2224module_exit(gspca_exit);
2225
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03002226#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002227module_param_named(debug, gspca_debug, int, 0644);
2228MODULE_PARM_DESC(debug,
2229 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002230 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
2231 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002232#endif