blob: 95a8b84d4cf01c1409dc3dbf4513e31149b8ca9e [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 Moine77ee3312009-11-07 15:30:50 -030050#define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 8, 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 Moine77ee3312009-11-07 15:30:50 -030077/* specific memory types - !! should be different from V4L2_MEMORY_xxx */
Jean-Francois Moined43fa322008-06-12 10:58:58 -030078#define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
79#define GSPCA_MEMORY_READ 7
80
Jean-Francois Moined43fa322008-06-12 10:58:58 -030081#define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
Jean-Francois Moined43fa322008-06-12 10:58:58 -030082
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030083/*
84 * VMA operations.
85 */
86static void gspca_vm_open(struct vm_area_struct *vma)
87{
88 struct gspca_frame *frame = vma->vm_private_data;
89
90 frame->vma_use_count++;
91 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
92}
93
94static void gspca_vm_close(struct vm_area_struct *vma)
95{
96 struct gspca_frame *frame = vma->vm_private_data;
97
98 if (--frame->vma_use_count <= 0)
99 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
100}
101
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400102static const struct vm_operations_struct gspca_vm_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300103 .open = gspca_vm_open,
104 .close = gspca_vm_close,
105};
106
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300107/* get the current input frame buffer */
108struct gspca_frame *gspca_get_i_frame(struct gspca_dev *gspca_dev)
109{
110 struct gspca_frame *frame;
111 int i;
112
113 i = gspca_dev->fr_i;
114 i = gspca_dev->fr_queue[i];
115 frame = &gspca_dev->frame[i];
116 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
117 != V4L2_BUF_FLAG_QUEUED)
118 return NULL;
119 return frame;
120}
121EXPORT_SYMBOL(gspca_get_i_frame);
122
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300123/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300124 * fill a video frame from an URB and resubmit
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300125 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300126static void fill_frame(struct gspca_dev *gspca_dev,
127 struct urb *urb)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300128{
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300129 u8 *data; /* address of data in the iso message */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300130 int i, len, st;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300131 cam_pkt_op pkt_scan;
132
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300133 if (urb->status != 0) {
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300134 if (urb->status == -ESHUTDOWN)
135 return; /* disconnection */
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300136#ifdef CONFIG_PM
Hans de Goede6a540bd2009-10-24 15:02:14 -0300137 if (gspca_dev->frozen)
138 return;
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300139#endif
Hans de Goede6a540bd2009-10-24 15:02:14 -0300140 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine81578522009-11-13 07:15:08 -0300141 urb->status = 0;
Hans de Goedebf926ad2009-10-23 06:56:06 -0300142 goto resubmit;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300143 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300144 pkt_scan = gspca_dev->sd_desc->pkt_scan;
145 for (i = 0; i < urb->number_of_packets; i++) {
146
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300147 /* check the packet status and length */
148 len = urb->iso_frame_desc[i].actual_length;
Jean-Francois Moineff374742008-10-23 07:29:51 -0300149 if (len == 0) {
150 if (gspca_dev->empty_packet == 0)
151 gspca_dev->empty_packet = 1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300152 continue;
Jean-Francois Moineff374742008-10-23 07:29:51 -0300153 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300154 st = urb->iso_frame_desc[i].status;
155 if (st) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300156 PDEBUG(D_ERR,
157 "ISOC data error: [%d] len=%d, status=%d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300158 i, len, st);
159 gspca_dev->last_packet_type = DISCARD_PACKET;
160 continue;
161 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300162
163 /* let the packet be analyzed by the subdriver */
164 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
165 i, urb->iso_frame_desc[i].offset, len);
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300166 data = (u8 *) urb->transfer_buffer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300167 + urb->iso_frame_desc[i].offset;
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300168 pkt_scan(gspca_dev, data, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300169 }
170
Hans de Goedebf926ad2009-10-23 06:56:06 -0300171resubmit:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300172 /* resubmit the URB */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300173 st = usb_submit_urb(urb, GFP_ATOMIC);
174 if (st < 0)
175 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
176}
177
178/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300179 * ISOC message interrupt from the USB device
180 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300181 * Analyse each packet and call the subdriver for copy to the frame buffer.
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300182 */
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300183static void isoc_irq(struct urb *urb)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300184{
185 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
186
Hans de Goede3647fea2008-07-15 05:36:30 -0300187 PDEBUG(D_PACK, "isoc irq");
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300188 if (!gspca_dev->streaming)
189 return;
190 fill_frame(gspca_dev, urb);
191}
192
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300193/*
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300194 * bulk message interrupt from the USB device
195 */
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300196static void bulk_irq(struct urb *urb)
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300197{
198 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300199 int st;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300200
201 PDEBUG(D_PACK, "bulk irq");
202 if (!gspca_dev->streaming)
203 return;
Jean-Francois Moine880ae242008-11-18 04:19:52 -0300204 switch (urb->status) {
205 case 0:
206 break;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300207 case -ESHUTDOWN:
208 return; /* disconnection */
Jean-Francois Moine880ae242008-11-18 04:19:52 -0300209 default:
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300210#ifdef CONFIG_PM
Hans de Goede6a540bd2009-10-24 15:02:14 -0300211 if (gspca_dev->frozen)
212 return;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300213#endif
Hans de Goede6a540bd2009-10-24 15:02:14 -0300214 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine81578522009-11-13 07:15:08 -0300215 urb->status = 0;
Hans de Goedebf926ad2009-10-23 06:56:06 -0300216 goto resubmit;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300217 }
218
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300219 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
220 gspca_dev->sd_desc->pkt_scan(gspca_dev,
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300221 urb->transfer_buffer,
222 urb->actual_length);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300223
Hans de Goedebf926ad2009-10-23 06:56:06 -0300224resubmit:
Antonio Ospitedff369a2008-11-14 07:53:32 -0300225 /* resubmit the URB */
226 if (gspca_dev->cam.bulk_nurbs != 0) {
227 st = usb_submit_urb(urb, GFP_ATOMIC);
228 if (st < 0)
229 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
230 }
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300231}
232
233/*
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300234 * add data to the current frame
235 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300236 * This function is called by the subdrivers at interrupt level.
237 *
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300238 * To build a frame, these ones must add
239 * - one FIRST_PACKET
240 * - 0 or many INTER_PACKETs
241 * - one LAST_PACKET
242 * DISCARD_PACKET invalidates the whole frame.
243 * On LAST_PACKET, a new frame is returned.
244 */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300245void gspca_frame_add(struct gspca_dev *gspca_dev,
246 enum gspca_packet_type packet_type,
247 const u8 *data,
248 int len)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300249{
Jean-Francois Moine3ec342f2009-11-13 07:38:16 -0300250 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300251 int i, j;
252
Hans de Goedee2997a72008-04-23 08:09:12 -0300253 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300254
255 /* check the availability of the frame buffer */
Jean-Francois Moine3ec342f2009-11-13 07:38:16 -0300256 frame = gspca_dev->cur_frame;
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300257 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
258 != V4L2_BUF_FLAG_QUEUED) {
259 gspca_dev->last_packet_type = DISCARD_PACKET;
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300260 return;
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300261 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300262
263 /* when start of a new frame, if the current frame buffer
264 * is not queued, discard the whole frame */
265 if (packet_type == FIRST_PACKET) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300266 frame->data_end = frame->data;
267 jiffies_to_timeval(get_jiffies_64(),
268 &frame->v4l2_buf.timestamp);
269 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300270 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
Jean-Francois Moine06ca78f2008-07-22 03:45:08 -0300271 if (packet_type == LAST_PACKET)
272 gspca_dev->last_packet_type = packet_type;
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300273 return;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300274 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300275
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300276 /* append the packet to the frame buffer */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300277 if (len > 0) {
278 if (frame->data_end - frame->data + len
279 > frame->v4l2_buf.length) {
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -0300280 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300281 frame->data_end - frame->data + len,
282 frame->v4l2_buf.length);
283 packet_type = DISCARD_PACKET;
284 } else {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300285 memcpy(frame->data_end, data, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300286 frame->data_end += len;
287 }
288 }
289 gspca_dev->last_packet_type = packet_type;
290
Frank Zago219423c2008-09-26 07:43:54 -0300291 /* if last packet, wake up the application and advance in the queue */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300292 if (packet_type == LAST_PACKET) {
293 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
294 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
295 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300296 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300297 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
298 gspca_dev->fr_i = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300299 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
300 frame->v4l2_buf.bytesused,
301 gspca_dev->fr_q,
302 i,
303 gspca_dev->fr_o);
304 j = gspca_dev->fr_queue[i];
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300305 gspca_dev->cur_frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300306 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300307}
308EXPORT_SYMBOL(gspca_frame_add);
309
310static int gspca_is_compressed(__u32 format)
311{
312 switch (format) {
313 case V4L2_PIX_FMT_MJPEG:
314 case V4L2_PIX_FMT_JPEG:
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300315 case V4L2_PIX_FMT_SPCA561:
Hans de Goedeab8f12c2008-07-04 18:29:32 -0300316 case V4L2_PIX_FMT_PAC207:
Kyle Guinnd661e622009-01-16 05:36:14 -0300317 case V4L2_PIX_FMT_MR97310A:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300318 return 1;
319 }
320 return 0;
321}
322
Jean-Francois Moined50e2572009-11-26 15:36:40 -0300323static void *rvmalloc(long size)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300324{
325 void *mem;
326 unsigned long adr;
327
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300328 mem = vmalloc_32(size);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300329 if (mem != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300330 adr = (unsigned long) mem;
Jean-Francois Moined50e2572009-11-26 15:36:40 -0300331 while (size > 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300332 SetPageReserved(vmalloc_to_page((void *) adr));
333 adr += PAGE_SIZE;
334 size -= PAGE_SIZE;
335 }
336 }
337 return mem;
338}
339
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300340static void rvfree(void *mem, long size)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300341{
342 unsigned long adr;
343
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300344 adr = (unsigned long) mem;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300345 while (size > 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300346 ClearPageReserved(vmalloc_to_page((void *) adr));
347 adr += PAGE_SIZE;
348 size -= PAGE_SIZE;
349 }
350 vfree(mem);
351}
352
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300353static int frame_alloc(struct gspca_dev *gspca_dev,
354 unsigned int count)
355{
356 struct gspca_frame *frame;
357 unsigned int frsz;
358 int i;
359
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300360 i = gspca_dev->curr_mode;
361 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300362 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300363 frsz = PAGE_ALIGN(frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300364 gspca_dev->frsz = frsz;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300365 if (count > GSPCA_MAX_FRAMES)
366 count = GSPCA_MAX_FRAMES;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300367 gspca_dev->frbuf = rvmalloc(frsz * count);
368 if (!gspca_dev->frbuf) {
369 err("frame alloc failed");
370 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300371 }
372 gspca_dev->nframes = count;
373 for (i = 0; i < count; i++) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300374 frame = &gspca_dev->frame[i];
375 frame->v4l2_buf.index = i;
376 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
377 frame->v4l2_buf.flags = 0;
378 frame->v4l2_buf.field = V4L2_FIELD_NONE;
379 frame->v4l2_buf.length = frsz;
380 frame->v4l2_buf.memory = gspca_dev->memory;
381 frame->v4l2_buf.sequence = 0;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300382 frame->data = frame->data_end =
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300383 gspca_dev->frbuf + i * frsz;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300384 frame->v4l2_buf.m.offset = i * frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300385 }
386 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
Jean-Francois Moine3ec342f2009-11-13 07:38:16 -0300387 gspca_dev->cur_frame = &gspca_dev->frame[0];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300388 gspca_dev->last_packet_type = DISCARD_PACKET;
389 gspca_dev->sequence = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300390 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300391}
392
393static void frame_free(struct gspca_dev *gspca_dev)
394{
395 int i;
396
397 PDEBUG(D_STREAM, "frame free");
Harvey Harrisonefab8212008-07-05 06:12:47 -0300398 if (gspca_dev->frbuf != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300399 rvfree(gspca_dev->frbuf,
400 gspca_dev->nframes * gspca_dev->frsz);
401 gspca_dev->frbuf = NULL;
402 for (i = 0; i < gspca_dev->nframes; i++)
403 gspca_dev->frame[i].data = NULL;
404 }
405 gspca_dev->nframes = 0;
406}
407
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300408static void destroy_urbs(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300409{
410 struct urb *urb;
411 unsigned int i;
412
413 PDEBUG(D_STREAM, "kill transfer");
Erik Andrén44732812008-10-13 15:52:46 -0300414 for (i = 0; i < MAX_NURBS; i++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300415 urb = gspca_dev->urb[i];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300416 if (urb == NULL)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300417 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300418
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300419 gspca_dev->urb[i] = NULL;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300420 usb_kill_urb(urb);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300421 if (urb->transfer_buffer != NULL)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300422 usb_buffer_free(gspca_dev->dev,
423 urb->transfer_buffer_length,
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300424 urb->transfer_buffer,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300425 urb->transfer_dma);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300426 usb_free_urb(urb);
427 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300428}
429
430/*
Frank Zago219423c2008-09-26 07:43:54 -0300431 * look for an input transfer endpoint in an alternate setting
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300432 */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300433static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300434 int xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300435{
436 struct usb_host_endpoint *ep;
437 int i, attr;
438
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300439 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
440 ep = &alt->endpoint[i];
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300441 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Jean-Francois Moine30d35e42009-04-21 13:57:31 -0300442 if (attr == xfer
443 && ep->desc.wMaxPacketSize != 0)
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300444 return ep;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300445 }
446 return NULL;
447}
448
449/*
Frank Zago219423c2008-09-26 07:43:54 -0300450 * look for an input (isoc or bulk) endpoint
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300451 *
452 * The endpoint is defined by the subdriver.
453 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
454 * This routine may be called many times when the bandwidth is too small
455 * (the bandwidth is checked on urb submit).
456 */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300457static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300458{
459 struct usb_interface *intf;
460 struct usb_host_endpoint *ep;
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300461 int xfer, i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300462
463 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300464 ep = NULL;
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300465 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
466 : USB_ENDPOINT_XFER_ISOC;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300467 i = gspca_dev->alt; /* previous alt setting */
Hans de Goedea511ba92009-10-16 07:13:07 -0300468 if (gspca_dev->cam.reverse_alts) {
469 while (++i < gspca_dev->nbalt) {
470 ep = alt_xfer(&intf->altsetting[i], xfer);
471 if (ep)
472 break;
473 }
474 } else {
475 while (--i >= 0) {
476 ep = alt_xfer(&intf->altsetting[i], xfer);
477 if (ep)
478 break;
479 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300480 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300481 if (ep == NULL) {
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300482 err("no transfer endpoint found");
483 return NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300484 }
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300485 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300486 i, ep->desc.bEndpointAddress);
Jean-Francois Moine1c8336d2009-07-28 04:38:25 -0300487 gspca_dev->alt = i; /* memorize the current alt setting */
Jean-Francois Moine8dd07ef2009-04-21 14:05:44 -0300488 if (gspca_dev->nbalt > 1) {
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300489 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
490 if (ret < 0) {
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300491 err("set alt %d err %d", i, ret);
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300492 return NULL;
493 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300494 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300495 return ep;
496}
497
498/*
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300499 * create the URBs for image transfer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300500 */
501static int create_urbs(struct gspca_dev *gspca_dev,
502 struct usb_host_endpoint *ep)
503{
504 struct urb *urb;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300505 int n, nurbs, i, psize, npkt, bsize;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300506
507 /* calculate the packet size and the number of packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300508 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300509
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300510 if (!gspca_dev->cam.bulk) { /* isoc */
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300511
512 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300513 if (gspca_dev->pkt_size == 0)
514 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
515 else
516 psize = gspca_dev->pkt_size;
Jean-Francois Moine49cb6b02009-04-25 13:29:01 -0300517 npkt = gspca_dev->cam.npkt;
518 if (npkt == 0)
519 npkt = 32; /* default value */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300520 bsize = psize * npkt;
521 PDEBUG(D_STREAM,
522 "isoc %d pkts size %d = bsize:%d",
523 npkt, psize, bsize);
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300524 nurbs = DEF_NURBS;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300525 } else { /* bulk */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300526 npkt = 0;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300527 bsize = gspca_dev->cam.bulk_size;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300528 if (bsize == 0)
529 bsize = psize;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300530 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300531 if (gspca_dev->cam.bulk_nurbs != 0)
532 nurbs = gspca_dev->cam.bulk_nurbs;
533 else
534 nurbs = 1;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300535 }
536
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300537 gspca_dev->nurbs = nurbs;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300538 for (n = 0; n < nurbs; n++) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300539 urb = usb_alloc_urb(npkt, GFP_KERNEL);
540 if (!urb) {
541 err("usb_alloc_urb failed");
Jean-Francois Moine0d0548e2008-09-05 12:49:54 -0300542 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300543 return -ENOMEM;
544 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300545 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300546 bsize,
547 GFP_KERNEL,
548 &urb->transfer_dma);
549
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300550 if (urb->transfer_buffer == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300551 usb_free_urb(urb);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300552 err("usb_buffer_urb failed");
Jean-Francois Moine0d0548e2008-09-05 12:49:54 -0300553 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300554 return -ENOMEM;
555 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300556 gspca_dev->urb[n] = urb;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300557 urb->dev = gspca_dev->dev;
558 urb->context = gspca_dev;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300559 urb->transfer_buffer_length = bsize;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300560 if (npkt != 0) { /* ISOC */
561 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
562 ep->desc.bEndpointAddress);
Frank Zago6c49da72008-09-28 07:43:00 -0300563 urb->transfer_flags = URB_ISO_ASAP
564 | URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300565 urb->interval = ep->desc.bInterval;
566 urb->complete = isoc_irq;
567 urb->number_of_packets = npkt;
568 for (i = 0; i < npkt; i++) {
569 urb->iso_frame_desc[i].length = psize;
570 urb->iso_frame_desc[i].offset = psize * i;
571 }
572 } else { /* bulk */
573 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
574 ep->desc.bEndpointAddress),
Frank Zago6c49da72008-09-28 07:43:00 -0300575 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300576 urb->complete = bulk_irq;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300577 }
578 }
579 return 0;
580}
581
582/*
583 * start the USB transfer
584 */
585static int gspca_init_transfer(struct gspca_dev *gspca_dev)
586{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300587 struct usb_host_endpoint *ep;
588 int n, ret;
589
Hans de Goedee2997a72008-04-23 08:09:12 -0300590 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
591 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300592
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300593 if (!gspca_dev->present) {
594 ret = -ENODEV;
595 goto out;
596 }
597
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300598 /* set the higher alternate setting and
599 * loop until urb submit succeeds */
Hans de Goedea511ba92009-10-16 07:13:07 -0300600 if (gspca_dev->cam.reverse_alts)
601 gspca_dev->alt = 0;
602 else
603 gspca_dev->alt = gspca_dev->nbalt;
604
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300605 if (gspca_dev->sd_desc->isoc_init) {
606 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
607 if (ret < 0)
608 goto out;
609 }
610 ep = get_ep(gspca_dev);
611 if (ep == NULL) {
612 ret = -EIO;
613 goto out;
614 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300615 for (;;) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300616 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300617 ret = create_urbs(gspca_dev, ep);
618 if (ret < 0)
619 goto out;
620
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300621 /* clear the bulk endpoint */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300622 if (gspca_dev->cam.bulk)
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300623 usb_clear_halt(gspca_dev->dev,
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300624 gspca_dev->urb[0]->pipe);
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300625
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300626 /* start the cam */
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300627 ret = gspca_dev->sd_desc->start(gspca_dev);
628 if (ret < 0) {
629 destroy_urbs(gspca_dev);
630 goto out;
631 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300632 gspca_dev->streaming = 1;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300633
Antonio Ospitedff369a2008-11-14 07:53:32 -0300634 /* some bulk transfers are started by the subdriver */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300635 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300636 break;
637
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300638 /* submit the URBs */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300639 for (n = 0; n < gspca_dev->nurbs; n++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300640 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300641 if (ret < 0)
642 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300643 }
644 if (ret >= 0)
645 break;
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300646 gspca_dev->streaming = 0;
647 destroy_urbs(gspca_dev);
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300648 if (ret != -ENOSPC) {
649 PDEBUG(D_ERR|D_STREAM,
650 "usb_submit_urb alt %d err %d",
651 gspca_dev->alt, ret);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300652 goto out;
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300653 }
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300654
655 /* the bandwidth is not wide enough
656 * negociate or try a lower alternate setting */
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300657 PDEBUG(D_ERR|D_STREAM,
658 "bandwidth not wide enough - trying again");
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300659 msleep(20); /* wait for kill complete */
660 if (gspca_dev->sd_desc->isoc_nego) {
661 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
662 if (ret < 0)
663 goto out;
664 } else {
665 ep = get_ep(gspca_dev);
666 if (ep == NULL) {
667 ret = -EIO;
668 goto out;
669 }
670 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300671 }
672out:
673 mutex_unlock(&gspca_dev->usb_lock);
674 return ret;
675}
676
677static int gspca_set_alt0(struct gspca_dev *gspca_dev)
678{
679 int ret;
680
Jean-Francois Moine8dd07ef2009-04-21 14:05:44 -0300681 if (gspca_dev->alt == 0)
682 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300683 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
684 if (ret < 0)
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -0300685 PDEBUG(D_ERR|D_STREAM, "set alt 0 err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300686 return ret;
687}
688
Frank Zago219423c2008-09-26 07:43:54 -0300689/* Note: both the queue and the usb locks should be held when calling this */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300690static void gspca_stream_off(struct gspca_dev *gspca_dev)
691{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300692 gspca_dev->streaming = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300693 if (gspca_dev->present) {
694 if (gspca_dev->sd_desc->stopN)
695 gspca_dev->sd_desc->stopN(gspca_dev);
696 destroy_urbs(gspca_dev);
Jean-Francois Moined08e2ce2009-01-20 16:21:44 -0300697 gspca_set_alt0(gspca_dev);
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300698 }
699
700 /* always call stop0 to free the subdriver's resources */
Jean-Francois Moine98522a72008-11-18 06:33:08 -0300701 if (gspca_dev->sd_desc->stop0)
702 gspca_dev->sd_desc->stop0(gspca_dev);
703 PDEBUG(D_STREAM, "stream off OK");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300704}
705
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300706static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300707{
708 int i;
709
710 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
711 gspca_dev->curr_mode = i;
712 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
713 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300714 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300715}
716
717static int wxh_to_mode(struct gspca_dev *gspca_dev,
718 int width, int height)
719{
720 int i;
721
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300722 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
723 if (width >= gspca_dev->cam.cam_mode[i].width
724 && height >= gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300725 break;
726 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300727 return i;
728}
729
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300730/*
731 * search a mode with the right pixel format
732 */
733static int gspca_get_mode(struct gspca_dev *gspca_dev,
734 int mode,
735 int pixfmt)
736{
737 int modeU, modeD;
738
739 modeU = modeD = mode;
740 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
741 if (--modeD >= 0) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300742 if (gspca_dev->cam.cam_mode[modeD].pixelformat
743 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300744 return modeD;
745 }
746 if (++modeU < gspca_dev->cam.nmodes) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300747 if (gspca_dev->cam.cam_mode[modeU].pixelformat
748 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300749 return modeU;
750 }
751 }
752 return -EINVAL;
753}
754
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300755#ifdef CONFIG_VIDEO_ADV_DEBUG
756static int vidioc_g_register(struct file *file, void *priv,
757 struct v4l2_dbg_register *reg)
758{
759 int ret;
760 struct gspca_dev *gspca_dev = priv;
761
762 if (!gspca_dev->sd_desc->get_chip_ident)
763 return -EINVAL;
764
765 if (!gspca_dev->sd_desc->get_register)
766 return -EINVAL;
767
768 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
769 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -0300770 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300771 if (gspca_dev->present)
772 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
773 else
774 ret = -ENODEV;
775 mutex_unlock(&gspca_dev->usb_lock);
776
777 return ret;
778}
779
780static int vidioc_s_register(struct file *file, void *priv,
781 struct v4l2_dbg_register *reg)
782{
783 int ret;
784 struct gspca_dev *gspca_dev = priv;
785
786 if (!gspca_dev->sd_desc->get_chip_ident)
787 return -EINVAL;
788
789 if (!gspca_dev->sd_desc->set_register)
790 return -EINVAL;
791
792 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
793 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -0300794 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300795 if (gspca_dev->present)
796 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
797 else
798 ret = -ENODEV;
799 mutex_unlock(&gspca_dev->usb_lock);
800
801 return ret;
802}
803#endif
804
805static int vidioc_g_chip_ident(struct file *file, void *priv,
806 struct v4l2_dbg_chip_ident *chip)
807{
808 int ret;
809 struct gspca_dev *gspca_dev = priv;
810
811 if (!gspca_dev->sd_desc->get_chip_ident)
812 return -EINVAL;
813
814 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
815 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -0300816 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300817 if (gspca_dev->present)
818 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
819 else
820 ret = -ENODEV;
821 mutex_unlock(&gspca_dev->usb_lock);
822
823 return ret;
824}
825
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300826static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300827 struct v4l2_fmtdesc *fmtdesc)
828{
829 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300830 int i, j, index;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300831 __u32 fmt_tb[8];
832
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300833 /* give an index to each format */
834 index = 0;
835 j = 0;
836 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300837 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300838 j = 0;
839 for (;;) {
840 if (fmt_tb[j] == fmt_tb[index])
841 break;
842 j++;
843 }
844 if (j == index) {
845 if (fmtdesc->index == index)
846 break; /* new format */
847 index++;
Julia Lawall80297122008-11-12 23:18:21 -0300848 if (index >= ARRAY_SIZE(fmt_tb))
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300849 return -EINVAL;
850 }
851 }
852 if (i < 0)
853 return -EINVAL; /* no more format */
854
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300855 fmtdesc->pixelformat = fmt_tb[index];
856 if (gspca_is_compressed(fmt_tb[index]))
857 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
858 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
859 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
860 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
861 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
862 fmtdesc->description[4] = '\0';
863 return 0;
864}
865
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300866static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300867 struct v4l2_format *fmt)
868{
869 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300870 int mode;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300871
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300872 mode = gspca_dev->curr_mode;
873 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
874 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300875 return 0;
876}
877
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300878static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300879 struct v4l2_format *fmt)
880{
Hans de Goede24c530b2008-07-04 17:55:18 -0300881 int w, h, mode, mode2;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300882
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300883 w = fmt->fmt.pix.width;
884 h = fmt->fmt.pix.height;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300885
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300886#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300887 if (gspca_debug & D_CONF)
888 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
889#endif
890 /* search the closest mode for width and height */
891 mode = wxh_to_mode(gspca_dev, w, h);
892
893 /* OK if right palette */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300894 if (gspca_dev->cam.cam_mode[mode].pixelformat
895 != fmt->fmt.pix.pixelformat) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300896
897 /* else, search the closest mode with the same pixel format */
898 mode2 = gspca_get_mode(gspca_dev, mode,
899 fmt->fmt.pix.pixelformat);
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300900 if (mode2 >= 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300901 mode = mode2;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300902/* else
903 ; * no chance, return this mode */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300904 }
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300905 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
906 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300907 return mode; /* used when s_fmt */
908}
909
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300910static int vidioc_try_fmt_vid_cap(struct file *file,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300911 void *priv,
912 struct v4l2_format *fmt)
913{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300914 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300915 int ret;
916
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300917 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300918 if (ret < 0)
919 return ret;
920 return 0;
921}
922
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300923static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300924 struct v4l2_format *fmt)
925{
926 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300927 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300928
Hans de Goedee2997a72008-04-23 08:09:12 -0300929 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
930 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300931
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300932 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300933 if (ret < 0)
934 goto out;
935
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300936 if (gspca_dev->nframes != 0
937 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
938 ret = -EINVAL;
939 goto out;
940 }
941
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300942 if (ret == gspca_dev->curr_mode) {
943 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300944 goto out; /* same mode */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300945 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300946
947 if (gspca_dev->streaming) {
948 ret = -EBUSY;
949 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -0300950 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300951 gspca_dev->width = fmt->fmt.pix.width;
952 gspca_dev->height = fmt->fmt.pix.height;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300953 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
954 gspca_dev->curr_mode = ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300955
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300956 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300957out:
958 mutex_unlock(&gspca_dev->queue_lock);
959 return ret;
960}
961
Hans de Goede11c635a2009-05-25 15:04:22 -0300962static int vidioc_enum_framesizes(struct file *file, void *priv,
963 struct v4l2_frmsizeenum *fsize)
964{
965 struct gspca_dev *gspca_dev = priv;
966 int i;
967 __u32 index = 0;
968
969 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
970 if (fsize->pixel_format !=
971 gspca_dev->cam.cam_mode[i].pixelformat)
972 continue;
973
974 if (fsize->index == index) {
975 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
976 fsize->discrete.width =
977 gspca_dev->cam.cam_mode[i].width;
978 fsize->discrete.height =
979 gspca_dev->cam.cam_mode[i].height;
980 return 0;
981 }
982 index++;
983 }
984
985 return -EINVAL;
986}
987
Antonio Ospite28ffe772009-12-02 06:18:46 -0300988static int vidioc_enum_frameintervals(struct file *filp, void *priv,
989 struct v4l2_frmivalenum *fival)
990{
991 struct gspca_dev *gspca_dev = priv;
992 int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
993 __u32 i;
994
995 if (gspca_dev->cam.mode_framerates == NULL ||
996 gspca_dev->cam.mode_framerates[mode].nrates == 0)
997 return -EINVAL;
998
999 if (fival->pixel_format !=
1000 gspca_dev->cam.cam_mode[mode].pixelformat)
1001 return -EINVAL;
1002
1003 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1004 if (fival->index == i) {
1005 fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1006 fival->discrete.numerator = 1;
1007 fival->discrete.denominator =
1008 gspca_dev->cam.mode_framerates[mode].rates[i];
1009 return 0;
1010 }
1011 }
1012
1013 return -EINVAL;
1014}
1015
Hans de Goeded0182302008-11-19 17:10:47 -03001016static void gspca_release(struct video_device *vfd)
Frank Zago6b060ff2008-09-28 08:12:22 -03001017{
Hans de Goeded0182302008-11-19 17:10:47 -03001018 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
Frank Zago6b060ff2008-09-28 08:12:22 -03001019
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001020 PDEBUG(D_PROBE, "%s released",
1021 video_device_node_name(&gspca_dev->vdev));
Frank Zago6b060ff2008-09-28 08:12:22 -03001022
1023 kfree(gspca_dev->usb_buf);
1024 kfree(gspca_dev);
1025}
1026
Hans Verkuilbec43662008-12-30 06:58:20 -03001027static int dev_open(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001028{
1029 struct gspca_dev *gspca_dev;
1030 int ret;
1031
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001032 PDEBUG(D_STREAM, "[%s] open", current->comm);
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001033 gspca_dev = (struct gspca_dev *) video_devdata(file);
Hans de Goedee2997a72008-04-23 08:09:12 -03001034 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1035 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001036 if (!gspca_dev->present) {
1037 ret = -ENODEV;
1038 goto out;
1039 }
1040
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001041 if (gspca_dev->users > 4) { /* (arbitrary value) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001042 ret = -EBUSY;
1043 goto out;
1044 }
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03001045
1046 /* protect the subdriver against rmmod */
1047 if (!try_module_get(gspca_dev->module)) {
1048 ret = -ENODEV;
1049 goto out;
1050 }
1051
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001052 gspca_dev->users++;
Frank Zago6b060ff2008-09-28 08:12:22 -03001053
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001054 file->private_data = gspca_dev;
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001055#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001056 /* activate the v4l2 debug */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001057 if (gspca_debug & D_V4L2)
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001058 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001059 | V4L2_DEBUG_IOCTL_ARG;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001060 else
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001061 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001062 | V4L2_DEBUG_IOCTL_ARG);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001063#endif
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001064 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001065out:
1066 mutex_unlock(&gspca_dev->queue_lock);
1067 if (ret != 0)
1068 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
1069 else
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001070 PDEBUG(D_STREAM, "open done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001071 return ret;
1072}
1073
Hans Verkuilbec43662008-12-30 06:58:20 -03001074static int dev_close(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001075{
1076 struct gspca_dev *gspca_dev = file->private_data;
1077
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001078 PDEBUG(D_STREAM, "[%s] close", current->comm);
Hans de Goedee2997a72008-04-23 08:09:12 -03001079 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1080 return -ERESTARTSYS;
1081 gspca_dev->users--;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001082
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001083 /* if the file did the capture, free the streaming resources */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001084 if (gspca_dev->capt_file == file) {
Jean-Francois Moinea1e82882008-09-03 17:12:17 -03001085 if (gspca_dev->streaming) {
1086 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001087 gspca_dev->usb_err = 0;
Jean-Francois Moinea1e82882008-09-03 17:12:17 -03001088 gspca_stream_off(gspca_dev);
1089 mutex_unlock(&gspca_dev->usb_lock);
1090 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001091 frame_free(gspca_dev);
Harvey Harrisonefab8212008-07-05 06:12:47 -03001092 gspca_dev->capt_file = NULL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001093 gspca_dev->memory = GSPCA_MEMORY_NO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001094 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001095 file->private_data = NULL;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03001096 module_put(gspca_dev->module);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001097 mutex_unlock(&gspca_dev->queue_lock);
Frank Zago6b060ff2008-09-28 08:12:22 -03001098
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001099 PDEBUG(D_STREAM, "close done");
Frank Zago6b060ff2008-09-28 08:12:22 -03001100
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001101 return 0;
1102}
1103
1104static int vidioc_querycap(struct file *file, void *priv,
1105 struct v4l2_capability *cap)
1106{
1107 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001108 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001109
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001110 /* protect the access to the usb device */
1111 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1112 return -ERESTARTSYS;
1113 if (!gspca_dev->present) {
1114 ret = -ENODEV;
1115 goto out;
1116 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001117 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -03001118 if (gspca_dev->dev->product != NULL) {
1119 strncpy(cap->card, gspca_dev->dev->product,
1120 sizeof cap->card);
1121 } else {
1122 snprintf(cap->card, sizeof cap->card,
1123 "USB Camera (%04x:%04x)",
1124 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1125 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1126 }
Thierry MERLE59320282009-01-21 15:32:26 -03001127 usb_make_path(gspca_dev->dev, cap->bus_info, sizeof(cap->bus_info));
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001128 cap->version = DRIVER_VERSION_NUMBER;
1129 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1130 | V4L2_CAP_STREAMING
1131 | V4L2_CAP_READWRITE;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001132 ret = 0;
1133out:
1134 mutex_unlock(&gspca_dev->usb_lock);
1135 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001136}
1137
Hans de Goedea0001a22009-06-10 04:52:18 -03001138static const struct ctrl *get_ctrl(struct gspca_dev *gspca_dev,
1139 int id)
1140{
1141 const struct ctrl *ctrls;
1142 int i;
1143
1144 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1145 i < gspca_dev->sd_desc->nctrls;
1146 i++, ctrls++) {
1147 if (gspca_dev->ctrl_dis & (1 << i))
1148 continue;
1149 if (id == ctrls->qctrl.id)
1150 return ctrls;
1151 }
1152 return NULL;
1153}
1154
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001155static int vidioc_queryctrl(struct file *file, void *priv,
1156 struct v4l2_queryctrl *q_ctrl)
1157{
1158 struct gspca_dev *gspca_dev = priv;
Hans de Goedea0001a22009-06-10 04:52:18 -03001159 const struct ctrl *ctrls;
1160 int i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001161 u32 id;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001162
Hans de Goedea0001a22009-06-10 04:52:18 -03001163 ctrls = NULL;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001164 id = q_ctrl->id;
1165 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1166 id &= V4L2_CTRL_ID_MASK;
1167 id++;
1168 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001169 if (gspca_dev->ctrl_dis & (1 << i))
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001170 continue;
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001171 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001172 continue;
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001173 if (ctrls && gspca_dev->sd_desc->ctrls[i].qctrl.id
Hans de Goedea0001a22009-06-10 04:52:18 -03001174 > ctrls->qctrl.id)
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001175 continue;
Hans de Goedea0001a22009-06-10 04:52:18 -03001176 ctrls = &gspca_dev->sd_desc->ctrls[i];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001177 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001178 } else {
1179 ctrls = get_ctrl(gspca_dev, id);
Jean-Francois Moine4af85662009-11-12 15:59:27 -03001180 i = ctrls - gspca_dev->sd_desc->ctrls;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001181 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001182 if (ctrls == NULL)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001183 return -EINVAL;
Hans de Goedea0001a22009-06-10 04:52:18 -03001184 memcpy(q_ctrl, ctrls, sizeof *q_ctrl);
Jean-Francois Moine4af85662009-11-12 15:59:27 -03001185 if (gspca_dev->ctrl_inac & (1 << i))
1186 q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001187 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001188}
1189
1190static int vidioc_s_ctrl(struct file *file, void *priv,
1191 struct v4l2_control *ctrl)
1192{
1193 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001194 const struct ctrl *ctrls;
Hans de Goedea0001a22009-06-10 04:52:18 -03001195 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001196
Hans de Goedea0001a22009-06-10 04:52:18 -03001197 ctrls = get_ctrl(gspca_dev, ctrl->id);
1198 if (ctrls == NULL)
1199 return -EINVAL;
1200
1201 if (ctrl->value < ctrls->qctrl.minimum
1202 || ctrl->value > ctrls->qctrl.maximum)
1203 return -ERANGE;
1204 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1205 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1206 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001207 gspca_dev->usb_err = 0;
Hans de Goedea0001a22009-06-10 04:52:18 -03001208 if (gspca_dev->present)
1209 ret = ctrls->set(gspca_dev, ctrl->value);
1210 else
1211 ret = -ENODEV;
1212 mutex_unlock(&gspca_dev->usb_lock);
1213 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001214}
1215
1216static int vidioc_g_ctrl(struct file *file, void *priv,
1217 struct v4l2_control *ctrl)
1218{
1219 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001220 const struct ctrl *ctrls;
Hans de Goedea0001a22009-06-10 04:52:18 -03001221 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001222
Hans de Goedea0001a22009-06-10 04:52:18 -03001223 ctrls = get_ctrl(gspca_dev, ctrl->id);
1224 if (ctrls == NULL)
1225 return -EINVAL;
1226
1227 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1228 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001229 gspca_dev->usb_err = 0;
Hans de Goedea0001a22009-06-10 04:52:18 -03001230 if (gspca_dev->present)
1231 ret = ctrls->get(gspca_dev, &ctrl->value);
1232 else
1233 ret = -ENODEV;
1234 mutex_unlock(&gspca_dev->usb_lock);
1235 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001236}
1237
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03001238/*fixme: have an audio flag in gspca_dev?*/
1239static int vidioc_s_audio(struct file *file, void *priv,
1240 struct v4l2_audio *audio)
1241{
1242 if (audio->index != 0)
1243 return -EINVAL;
1244 return 0;
1245}
1246
1247static int vidioc_g_audio(struct file *file, void *priv,
1248 struct v4l2_audio *audio)
1249{
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03001250 strcpy(audio->name, "Microphone");
1251 return 0;
1252}
1253
1254static int vidioc_enumaudio(struct file *file, void *priv,
1255 struct v4l2_audio *audio)
1256{
1257 if (audio->index != 0)
1258 return -EINVAL;
1259
1260 strcpy(audio->name, "Microphone");
1261 audio->capability = 0;
1262 audio->mode = 0;
1263 return 0;
1264}
1265
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001266static int vidioc_querymenu(struct file *file, void *priv,
1267 struct v4l2_querymenu *qmenu)
1268{
1269 struct gspca_dev *gspca_dev = priv;
1270
1271 if (!gspca_dev->sd_desc->querymenu)
1272 return -EINVAL;
1273 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1274}
1275
1276static int vidioc_enum_input(struct file *file, void *priv,
1277 struct v4l2_input *input)
1278{
1279 struct gspca_dev *gspca_dev = priv;
1280
1281 if (input->index != 0)
1282 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001283 input->type = V4L2_INPUT_TYPE_CAMERA;
Adam Bakerdfa76fa2009-03-29 19:17:10 -03001284 input->status = gspca_dev->cam.input_flags;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001285 strncpy(input->name, gspca_dev->sd_desc->name,
1286 sizeof input->name);
1287 return 0;
1288}
1289
1290static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1291{
1292 *i = 0;
1293 return 0;
1294}
1295
1296static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1297{
1298 if (i > 0)
1299 return -EINVAL;
1300 return (0);
1301}
1302
1303static int vidioc_reqbufs(struct file *file, void *priv,
1304 struct v4l2_requestbuffers *rb)
1305{
1306 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001307 int i, ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001308
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001309 switch (rb->memory) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001310 case GSPCA_MEMORY_READ: /* (internal call) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001311 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001312 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001313 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001314 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001315 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001316 }
Hans de Goedee2997a72008-04-23 08:09:12 -03001317 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1318 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001319
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001320 if (gspca_dev->memory != GSPCA_MEMORY_NO
1321 && gspca_dev->memory != rb->memory) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001322 ret = -EBUSY;
1323 goto out;
1324 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001325
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001326 /* only one file may do the capture */
1327 if (gspca_dev->capt_file != NULL
1328 && gspca_dev->capt_file != file) {
1329 ret = -EBUSY;
1330 goto out;
1331 }
1332
1333 /* if allocated, the buffers must not be mapped */
1334 for (i = 0; i < gspca_dev->nframes; i++) {
1335 if (gspca_dev->frame[i].vma_use_count) {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001336 ret = -EBUSY;
1337 goto out;
1338 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001339 }
1340
1341 /* stop streaming */
1342 if (gspca_dev->streaming) {
1343 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001344 gspca_dev->usb_err = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001345 gspca_stream_off(gspca_dev);
1346 mutex_unlock(&gspca_dev->usb_lock);
1347 }
1348
1349 /* free the previous allocated buffers, if any */
1350 if (gspca_dev->nframes != 0) {
1351 frame_free(gspca_dev);
1352 gspca_dev->capt_file = NULL;
1353 }
1354 if (rb->count == 0) /* unrequest */
1355 goto out;
1356 gspca_dev->memory = rb->memory;
1357 ret = frame_alloc(gspca_dev, rb->count);
1358 if (ret == 0) {
1359 rb->count = gspca_dev->nframes;
1360 gspca_dev->capt_file = file;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001361 }
1362out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001363 mutex_unlock(&gspca_dev->queue_lock);
1364 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1365 return ret;
1366}
1367
1368static int vidioc_querybuf(struct file *file, void *priv,
1369 struct v4l2_buffer *v4l2_buf)
1370{
1371 struct gspca_dev *gspca_dev = priv;
1372 struct gspca_frame *frame;
1373
Jean-Francois Moine13752bd2008-12-19 15:08:51 -03001374 if (v4l2_buf->index < 0
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001375 || v4l2_buf->index >= gspca_dev->nframes)
1376 return -EINVAL;
1377
1378 frame = &gspca_dev->frame[v4l2_buf->index];
1379 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1380 return 0;
1381}
1382
1383static int vidioc_streamon(struct file *file, void *priv,
1384 enum v4l2_buf_type buf_type)
1385{
1386 struct gspca_dev *gspca_dev = priv;
1387 int ret;
1388
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001389 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1390 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001391 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1392 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001393
Jean-Francois Moine1edabe72008-12-19 15:17:25 -03001394 if (gspca_dev->nframes == 0
1395 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001396 ret = -EINVAL;
1397 goto out;
1398 }
1399 if (!gspca_dev->streaming) {
1400 ret = gspca_init_transfer(gspca_dev);
1401 if (ret < 0)
1402 goto out;
1403 }
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001404#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001405 if (gspca_debug & D_STREAM) {
1406 PDEBUG_MODE("stream on OK",
1407 gspca_dev->pixfmt,
1408 gspca_dev->width,
1409 gspca_dev->height);
1410 }
1411#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001412 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001413out:
1414 mutex_unlock(&gspca_dev->queue_lock);
1415 return ret;
1416}
1417
1418static int vidioc_streamoff(struct file *file, void *priv,
1419 enum v4l2_buf_type buf_type)
1420{
1421 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001422 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001423
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001424 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1425 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001426 if (!gspca_dev->streaming)
1427 return 0;
1428 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1429 return -ERESTARTSYS;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001430
1431 /* stop streaming */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001432 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1433 ret = -ERESTARTSYS;
1434 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001435 }
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001436 gspca_dev->usb_err = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001437 gspca_stream_off(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001438 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001439
1440 /* empty the application queues */
1441 for (i = 0; i < gspca_dev->nframes; i++)
1442 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1443 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1444 gspca_dev->last_packet_type = DISCARD_PACKET;
1445 gspca_dev->sequence = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001446 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001447out:
1448 mutex_unlock(&gspca_dev->queue_lock);
1449 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001450}
1451
1452static int vidioc_g_jpegcomp(struct file *file, void *priv,
1453 struct v4l2_jpegcompression *jpegcomp)
1454{
1455 struct gspca_dev *gspca_dev = priv;
1456 int ret;
1457
1458 if (!gspca_dev->sd_desc->get_jcomp)
1459 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001460 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1461 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001462 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001463 if (gspca_dev->present)
1464 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1465 else
1466 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001467 mutex_unlock(&gspca_dev->usb_lock);
1468 return ret;
1469}
1470
1471static int vidioc_s_jpegcomp(struct file *file, void *priv,
1472 struct v4l2_jpegcompression *jpegcomp)
1473{
1474 struct gspca_dev *gspca_dev = priv;
1475 int ret;
1476
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001477 if (!gspca_dev->sd_desc->set_jcomp)
1478 return -EINVAL;
Jim Parisf86d4a92008-12-10 05:50:44 -03001479 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1480 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001481 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001482 if (gspca_dev->present)
1483 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1484 else
1485 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001486 mutex_unlock(&gspca_dev->usb_lock);
1487 return ret;
1488}
1489
1490static int vidioc_g_parm(struct file *filp, void *priv,
1491 struct v4l2_streamparm *parm)
1492{
1493 struct gspca_dev *gspca_dev = priv;
1494
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001495 parm->parm.capture.readbuffers = gspca_dev->nbufread;
Jim Paris627a5ef2008-12-10 06:02:42 -03001496
1497 if (gspca_dev->sd_desc->get_streamparm) {
1498 int ret;
1499
1500 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1501 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001502 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001503 if (gspca_dev->present)
1504 ret = gspca_dev->sd_desc->get_streamparm(gspca_dev,
1505 parm);
1506 else
1507 ret = -ENODEV;
Jim Paris627a5ef2008-12-10 06:02:42 -03001508 mutex_unlock(&gspca_dev->usb_lock);
1509 return ret;
1510 }
1511
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001512 return 0;
1513}
1514
1515static int vidioc_s_parm(struct file *filp, void *priv,
1516 struct v4l2_streamparm *parm)
1517{
1518 struct gspca_dev *gspca_dev = priv;
1519 int n;
1520
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001521 n = parm->parm.capture.readbuffers;
1522 if (n == 0 || n > GSPCA_MAX_FRAMES)
1523 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1524 else
1525 gspca_dev->nbufread = n;
Jim Paris627a5ef2008-12-10 06:02:42 -03001526
1527 if (gspca_dev->sd_desc->set_streamparm) {
1528 int ret;
1529
1530 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1531 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001532 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001533 if (gspca_dev->present)
1534 ret = gspca_dev->sd_desc->set_streamparm(gspca_dev,
1535 parm);
1536 else
1537 ret = -ENODEV;
Jim Paris627a5ef2008-12-10 06:02:42 -03001538 mutex_unlock(&gspca_dev->usb_lock);
1539 return ret;
1540 }
1541
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001542 return 0;
1543}
1544
1545#ifdef CONFIG_VIDEO_V4L1_COMPAT
1546static int vidiocgmbuf(struct file *file, void *priv,
1547 struct video_mbuf *mbuf)
1548{
1549 struct gspca_dev *gspca_dev = file->private_data;
1550 int i;
1551
1552 PDEBUG(D_STREAM, "cgmbuf");
1553 if (gspca_dev->nframes == 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001554 int ret;
1555
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001556 {
1557 struct v4l2_format fmt;
1558
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001559 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1560 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1561 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1562 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1563 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001564 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001565 if (ret != 0)
1566 return ret;
1567 }
1568 {
1569 struct v4l2_requestbuffers rb;
1570
1571 memset(&rb, 0, sizeof rb);
1572 rb.count = 4;
1573 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1574 rb.memory = V4L2_MEMORY_MMAP;
1575 ret = vidioc_reqbufs(file, priv, &rb);
1576 if (ret != 0)
1577 return ret;
1578 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001579 }
1580 mbuf->frames = gspca_dev->nframes;
1581 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1582 for (i = 0; i < mbuf->frames; i++)
1583 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1584 return 0;
1585}
1586#endif
1587
1588static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1589{
1590 struct gspca_dev *gspca_dev = file->private_data;
Harvey Harrisonefab8212008-07-05 06:12:47 -03001591 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001592 struct page *page;
1593 unsigned long addr, start, size;
1594 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001595
1596 start = vma->vm_start;
1597 size = vma->vm_end - vma->vm_start;
1598 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1599
Hans de Goedee2997a72008-04-23 08:09:12 -03001600 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1601 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001602 if (!gspca_dev->present) {
1603 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001604 goto out;
1605 }
1606 if (gspca_dev->capt_file != file) {
1607 ret = -EINVAL;
1608 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001609 }
1610
Harvey Harrisonefab8212008-07-05 06:12:47 -03001611 frame = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001612 for (i = 0; i < gspca_dev->nframes; ++i) {
1613 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1614 PDEBUG(D_STREAM, "mmap bad memory type");
1615 break;
1616 }
1617 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1618 == vma->vm_pgoff) {
1619 frame = &gspca_dev->frame[i];
1620 break;
1621 }
1622 }
Harvey Harrisonefab8212008-07-05 06:12:47 -03001623 if (frame == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001624 PDEBUG(D_STREAM, "mmap no frame buffer found");
1625 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001626 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001627 }
1628#ifdef CONFIG_VIDEO_V4L1_COMPAT
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001629 /* v4l1 maps all the buffers */
1630 if (i != 0
1631 || size != frame->v4l2_buf.length * gspca_dev->nframes)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001632#endif
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001633 if (size != frame->v4l2_buf.length) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001634 PDEBUG(D_STREAM, "mmap bad size");
1635 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001636 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001637 }
1638
1639 /*
1640 * - VM_IO marks the area as being a mmaped region for I/O to a
1641 * device. It also prevents the region from being core dumped.
1642 */
1643 vma->vm_flags |= VM_IO;
1644
1645 addr = (unsigned long) frame->data;
1646 while (size > 0) {
1647 page = vmalloc_to_page((void *) addr);
1648 ret = vm_insert_page(vma, start, page);
1649 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001650 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001651 start += PAGE_SIZE;
1652 addr += PAGE_SIZE;
1653 size -= PAGE_SIZE;
1654 }
1655
Jean-Francois Moined50e2572009-11-26 15:36:40 -03001656 vma->vm_ops = &gspca_vm_ops;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001657 vma->vm_private_data = frame;
1658 gspca_vm_open(vma);
Hans de Goedee2997a72008-04-23 08:09:12 -03001659 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001660out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001661 mutex_unlock(&gspca_dev->queue_lock);
1662 return ret;
1663}
1664
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001665/*
1666 * wait for a video frame
1667 *
1668 * If a frame is ready, its index is returned.
1669 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001670static int frame_wait(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001671 int nonblock_ing)
1672{
1673 struct gspca_frame *frame;
1674 int i, j, ret;
1675
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001676 /* check if a frame is ready */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001677 i = gspca_dev->fr_o;
1678 j = gspca_dev->fr_queue[i];
1679 frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001680
Hans de Goede181b7042008-11-19 17:16:26 -03001681 if (!(frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)) {
1682 if (nonblock_ing)
1683 return -EAGAIN;
1684
1685 /* wait till a frame is ready */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001686 ret = wait_event_interruptible_timeout(gspca_dev->wq,
Hans de Goede181b7042008-11-19 17:16:26 -03001687 (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) ||
1688 !gspca_dev->streaming || !gspca_dev->present,
1689 msecs_to_jiffies(3000));
1690 if (ret < 0)
1691 return ret;
1692 if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present)
Hans de Goedee2997a72008-04-23 08:09:12 -03001693 return -EIO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001694 }
Hans de Goede181b7042008-11-19 17:16:26 -03001695
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001696 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1697 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1698 gspca_dev->fr_q,
1699 gspca_dev->fr_i,
1700 gspca_dev->fr_o);
Hans de Goedee2997a72008-04-23 08:09:12 -03001701
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001702 if (gspca_dev->sd_desc->dq_callback) {
1703 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001704 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001705 if (gspca_dev->present)
1706 gspca_dev->sd_desc->dq_callback(gspca_dev);
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001707 mutex_unlock(&gspca_dev->usb_lock);
1708 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001709 return j;
1710}
1711
1712/*
1713 * dequeue a video buffer
1714 *
1715 * If nonblock_ing is false, block until a buffer is available.
1716 */
1717static int vidioc_dqbuf(struct file *file, void *priv,
1718 struct v4l2_buffer *v4l2_buf)
1719{
1720 struct gspca_dev *gspca_dev = priv;
1721 struct gspca_frame *frame;
1722 int i, ret;
1723
1724 PDEBUG(D_FRAM, "dqbuf");
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001725 if (v4l2_buf->memory != gspca_dev->memory)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001726 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001727
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001728 if (!gspca_dev->present)
1729 return -ENODEV;
1730
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001731 /* if not streaming, be sure the application will not loop forever */
1732 if (!(file->f_flags & O_NONBLOCK)
1733 && !gspca_dev->streaming && gspca_dev->users == 1)
1734 return -EINVAL;
1735
1736 /* only the capturing file may dequeue */
1737 if (gspca_dev->capt_file != file)
1738 return -EINVAL;
1739
1740 /* only one dequeue / read at a time */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001741 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1742 return -ERESTARTSYS;
1743
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001744 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001745 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001746 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001747 i = ret; /* frame index */
1748 frame = &gspca_dev->frame[i];
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001749 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001750 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001751 frame->data,
1752 frame->v4l2_buf.bytesused)) {
1753 PDEBUG(D_ERR|D_STREAM,
1754 "dqbuf cp to user failed");
1755 ret = -EFAULT;
1756 goto out;
1757 }
1758 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001759 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1760 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1761 PDEBUG(D_FRAM, "dqbuf %d", i);
1762 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001763out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001764 mutex_unlock(&gspca_dev->read_lock);
1765 return ret;
1766}
1767
1768/*
1769 * queue a video buffer
1770 *
1771 * Attempting to queue a buffer that has already been
1772 * queued will return -EINVAL.
1773 */
1774static int vidioc_qbuf(struct file *file, void *priv,
1775 struct v4l2_buffer *v4l2_buf)
1776{
1777 struct gspca_dev *gspca_dev = priv;
1778 struct gspca_frame *frame;
1779 int i, index, ret;
1780
1781 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001782
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001783 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1784 return -ERESTARTSYS;
1785
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001786 index = v4l2_buf->index;
1787 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001788 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001789 "qbuf idx %d >= %d", index, gspca_dev->nframes);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001790 ret = -EINVAL;
1791 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001792 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001793 if (v4l2_buf->memory != gspca_dev->memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001794 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001795 ret = -EINVAL;
1796 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001797 }
1798
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001799 frame = &gspca_dev->frame[index];
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001800 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1801 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001802 ret = -EINVAL;
1803 goto out;
1804 }
1805
1806 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001807
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001808 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001809 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1810 frame->v4l2_buf.length = v4l2_buf->length;
1811 }
1812
1813 /* put the buffer in the 'queued' queue */
1814 i = gspca_dev->fr_q;
1815 gspca_dev->fr_queue[i] = index;
1816 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1817 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1818 gspca_dev->fr_q,
1819 gspca_dev->fr_i,
1820 gspca_dev->fr_o);
1821
1822 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1823 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1824 ret = 0;
1825out:
1826 mutex_unlock(&gspca_dev->queue_lock);
1827 return ret;
1828}
1829
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001830/*
1831 * allocate the resources for read()
1832 */
1833static int read_alloc(struct gspca_dev *gspca_dev,
1834 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001835{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001836 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001837 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001838
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001839 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001840 if (gspca_dev->nframes == 0) {
1841 struct v4l2_requestbuffers rb;
1842
1843 memset(&rb, 0, sizeof rb);
1844 rb.count = gspca_dev->nbufread;
1845 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001846 rb.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001847 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1848 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001849 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001850 return ret;
1851 }
1852 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1853 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001854 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001855 for (i = 0; i < gspca_dev->nbufread; i++) {
1856 v4l2_buf.index = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001857 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1858 if (ret != 0) {
1859 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1860 return ret;
1861 }
1862 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001863 gspca_dev->memory = GSPCA_MEMORY_READ;
1864 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001865
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001866 /* start streaming */
1867 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1868 if (ret != 0)
1869 PDEBUG(D_STREAM, "read streamon err %d", ret);
1870 return ret;
1871}
1872
1873static unsigned int dev_poll(struct file *file, poll_table *wait)
1874{
1875 struct gspca_dev *gspca_dev = file->private_data;
1876 int i, ret;
1877
1878 PDEBUG(D_FRAM, "poll");
1879
1880 poll_wait(file, &gspca_dev->wq, wait);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001881
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001882 /* if reqbufs is not done, the user would use read() */
1883 if (gspca_dev->nframes == 0) {
1884 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1885 return POLLERR; /* not the 1st time */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001886 ret = read_alloc(gspca_dev, file);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001887 if (ret != 0)
1888 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001889 }
1890
1891 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1892 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001893
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001894 /* check the next incoming buffer */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001895 i = gspca_dev->fr_o;
1896 i = gspca_dev->fr_queue[i];
1897 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1898 ret = POLLIN | POLLRDNORM; /* something to read */
1899 else
1900 ret = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001901 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001902 if (!gspca_dev->present)
1903 return POLLHUP;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001904 return ret;
1905}
1906
1907static ssize_t dev_read(struct file *file, char __user *data,
1908 size_t count, loff_t *ppos)
1909{
1910 struct gspca_dev *gspca_dev = file->private_data;
1911 struct gspca_frame *frame;
1912 struct v4l2_buffer v4l2_buf;
1913 struct timeval timestamp;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001914 int n, ret, ret2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001915
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001916 PDEBUG(D_FRAM, "read (%zd)", count);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001917 if (!gspca_dev->present)
1918 return -ENODEV;
1919 switch (gspca_dev->memory) {
1920 case GSPCA_MEMORY_NO: /* first time */
1921 ret = read_alloc(gspca_dev, file);
1922 if (ret != 0)
1923 return ret;
1924 break;
1925 case GSPCA_MEMORY_READ:
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001926 if (gspca_dev->capt_file == file)
1927 break;
1928 /* fall thru */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001929 default:
1930 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001931 }
1932
1933 /* get a frame */
1934 jiffies_to_timeval(get_jiffies_64(), &timestamp);
1935 timestamp.tv_sec--;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001936 n = 2;
1937 for (;;) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001938 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1939 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001940 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001941 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1942 if (ret != 0) {
1943 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1944 return ret;
1945 }
1946
1947 /* if the process slept for more than 1 second,
Frank Zago219423c2008-09-26 07:43:54 -03001948 * get a newer frame */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001949 frame = &gspca_dev->frame[v4l2_buf.index];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001950 if (--n < 0)
1951 break; /* avoid infinite loop */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001952 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1953 break;
1954 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1955 if (ret != 0) {
1956 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1957 return ret;
1958 }
1959 }
1960
1961 /* copy the frame */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001962 if (count > frame->v4l2_buf.bytesused)
1963 count = frame->v4l2_buf.bytesused;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001964 ret = copy_to_user(data, frame->data, count);
1965 if (ret != 0) {
1966 PDEBUG(D_ERR|D_STREAM,
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001967 "read cp to user lack %d / %zd", ret, count);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001968 ret = -EFAULT;
1969 goto out;
1970 }
1971 ret = count;
1972out:
1973 /* in each case, requeue the buffer */
1974 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1975 if (ret2 != 0)
1976 return ret2;
1977 return ret;
1978}
1979
Hans Verkuilbec43662008-12-30 06:58:20 -03001980static struct v4l2_file_operations dev_fops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001981 .owner = THIS_MODULE,
1982 .open = dev_open,
1983 .release = dev_close,
1984 .read = dev_read,
1985 .mmap = dev_mmap,
Hans Verkuilbec43662008-12-30 06:58:20 -03001986 .unlocked_ioctl = video_ioctl2,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001987 .poll = dev_poll,
1988};
1989
Hans Verkuila3998102008-07-21 02:57:38 -03001990static const struct v4l2_ioctl_ops dev_ioctl_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001991 .vidioc_querycap = vidioc_querycap,
1992 .vidioc_dqbuf = vidioc_dqbuf,
1993 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001994 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1995 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1996 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1997 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001998 .vidioc_streamon = vidioc_streamon,
1999 .vidioc_queryctrl = vidioc_queryctrl,
2000 .vidioc_g_ctrl = vidioc_g_ctrl,
2001 .vidioc_s_ctrl = vidioc_s_ctrl,
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03002002 .vidioc_g_audio = vidioc_g_audio,
2003 .vidioc_s_audio = vidioc_s_audio,
2004 .vidioc_enumaudio = vidioc_enumaudio,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002005 .vidioc_querymenu = vidioc_querymenu,
2006 .vidioc_enum_input = vidioc_enum_input,
2007 .vidioc_g_input = vidioc_g_input,
2008 .vidioc_s_input = vidioc_s_input,
2009 .vidioc_reqbufs = vidioc_reqbufs,
2010 .vidioc_querybuf = vidioc_querybuf,
2011 .vidioc_streamoff = vidioc_streamoff,
2012 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
2013 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
2014 .vidioc_g_parm = vidioc_g_parm,
2015 .vidioc_s_parm = vidioc_s_parm,
Hans de Goede11c635a2009-05-25 15:04:22 -03002016 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Antonio Ospite28ffe772009-12-02 06:18:46 -03002017 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03002018#ifdef CONFIG_VIDEO_ADV_DEBUG
2019 .vidioc_g_register = vidioc_g_register,
2020 .vidioc_s_register = vidioc_s_register,
2021#endif
2022 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002023#ifdef CONFIG_VIDEO_V4L1_COMPAT
2024 .vidiocgmbuf = vidiocgmbuf,
2025#endif
2026};
2027
Hans Verkuila3998102008-07-21 02:57:38 -03002028static struct video_device gspca_template = {
2029 .name = "gspca main driver",
Hans Verkuila3998102008-07-21 02:57:38 -03002030 .fops = &dev_fops,
2031 .ioctl_ops = &dev_ioctl_ops,
Hans de Goeded0182302008-11-19 17:10:47 -03002032 .release = gspca_release,
Hans Verkuila3998102008-07-21 02:57:38 -03002033};
2034
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002035/*
2036 * probe and create a new gspca device
2037 *
2038 * This function must be called by the sub-driver when it is
2039 * called for probing a new device.
2040 */
2041int gspca_dev_probe(struct usb_interface *intf,
2042 const struct usb_device_id *id,
2043 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002044 int dev_size,
2045 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002046{
2047 struct usb_interface_descriptor *interface;
2048 struct gspca_dev *gspca_dev;
2049 struct usb_device *dev = interface_to_usbdev(intf);
2050 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002051
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002052 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002053
2054 /* we don't handle multi-config cameras */
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002055 if (dev->descriptor.bNumConfigurations != 1) {
2056 PDEBUG(D_ERR, "Too many config");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002057 return -ENODEV;
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002058 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002059 interface = &intf->cur_altsetting->desc;
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002060 if (interface->bInterfaceNumber > 0) {
2061 PDEBUG(D_ERR, "intf != 0");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002062 return -ENODEV;
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002063 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002064
2065 /* create the device */
2066 if (dev_size < sizeof *gspca_dev)
2067 dev_size = sizeof *gspca_dev;
2068 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
Erik Andrene293e592008-10-03 08:46:50 -03002069 if (!gspca_dev) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002070 err("couldn't kzalloc gspca struct");
Erik Andrene293e592008-10-03 08:46:50 -03002071 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002072 }
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002073 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2074 if (!gspca_dev->usb_buf) {
2075 err("out of memory");
Erik Andrene293e592008-10-03 08:46:50 -03002076 ret = -ENOMEM;
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002077 goto out;
2078 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002079 gspca_dev->dev = dev;
2080 gspca_dev->iface = interface->bInterfaceNumber;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002081 gspca_dev->nbalt = intf->num_altsetting;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002082 gspca_dev->sd_desc = sd_desc;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002083 gspca_dev->nbufread = 2;
Jean-Francois Moineff374742008-10-23 07:29:51 -03002084 gspca_dev->empty_packet = -1; /* don't check the empty packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002085
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002086 /* configure the subdriver and initialize the USB device */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002087 ret = sd_desc->config(gspca_dev, id);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002088 if (ret < 0)
2089 goto out;
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002090 ret = sd_desc->init(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002091 if (ret < 0)
2092 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002093 gspca_set_default_mode(gspca_dev);
2094
2095 mutex_init(&gspca_dev->usb_lock);
2096 mutex_init(&gspca_dev->read_lock);
2097 mutex_init(&gspca_dev->queue_lock);
2098 init_waitqueue_head(&gspca_dev->wq);
2099
2100 /* init video stuff */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002101 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
Hans de Goede24441632009-03-27 16:08:20 -03002102 gspca_dev->vdev.parent = &intf->dev;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03002103 gspca_dev->module = module;
Oliver Neukumee56a4d2008-07-27 14:01:59 -03002104 gspca_dev->present = 1;
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002105 ret = video_register_device(&gspca_dev->vdev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002106 VFL_TYPE_GRABBER,
Jean-Francois Moine8d584a52009-01-13 06:07:59 -03002107 -1);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002108 if (ret < 0) {
2109 err("video_register_device err %d", ret);
2110 goto out;
2111 }
2112
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002113 usb_set_intfdata(intf, gspca_dev);
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002114 PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002115 return 0;
2116out:
Jean-Francois Moinee07a1d82008-11-19 06:37:53 -03002117 kfree(gspca_dev->usb_buf);
2118 kfree(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002119 return ret;
2120}
2121EXPORT_SYMBOL(gspca_dev_probe);
2122
2123/*
2124 * USB disconnection
2125 *
2126 * This function must be called by the sub-driver
2127 * when the device disconnects, after the specific resources are freed.
2128 */
2129void gspca_disconnect(struct usb_interface *intf)
2130{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002131 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002132
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002133 PDEBUG(D_PROBE, "%s disconnect",
2134 video_device_node_name(&gspca_dev->vdev));
Adam Bakerad281272009-02-04 15:33:21 -03002135 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03002136 gspca_dev->present = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002137
2138 if (gspca_dev->streaming) {
2139 destroy_urbs(gspca_dev);
2140 wake_up_interruptible(&gspca_dev->wq);
2141 }
2142
2143 /* the device is freed at exit of this function */
2144 gspca_dev->dev = NULL;
Adam Bakerad281272009-02-04 15:33:21 -03002145 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03002146
Frank Zago6b060ff2008-09-28 08:12:22 -03002147 usb_set_intfdata(intf, NULL);
2148
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002149 /* release the device */
2150 /* (this will call gspca_release() immediatly or on last close) */
2151 video_unregister_device(&gspca_dev->vdev);
Jean-Francois Moine3145b8c2008-10-05 04:22:10 -03002152
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002153/* PDEBUG(D_PROBE, "disconnect complete"); */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002154}
2155EXPORT_SYMBOL(gspca_disconnect);
2156
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002157#ifdef CONFIG_PM
2158int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2159{
2160 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2161
2162 if (!gspca_dev->streaming)
2163 return 0;
2164 gspca_dev->frozen = 1; /* avoid urb error messages */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002165 if (gspca_dev->sd_desc->stopN)
2166 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002167 destroy_urbs(gspca_dev);
2168 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002169 if (gspca_dev->sd_desc->stop0)
2170 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002171 return 0;
2172}
2173EXPORT_SYMBOL(gspca_suspend);
2174
2175int gspca_resume(struct usb_interface *intf)
2176{
2177 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2178
2179 gspca_dev->frozen = 0;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002180 gspca_dev->sd_desc->init(gspca_dev);
2181 if (gspca_dev->streaming)
2182 return gspca_init_transfer(gspca_dev);
Jean-Francois Moine1e89e2da2008-09-03 17:12:15 -03002183 return 0;
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002184}
2185EXPORT_SYMBOL(gspca_resume);
2186#endif
Hans de Goededcef32372008-07-10 10:40:53 -03002187/* -- cam driver utility functions -- */
2188
2189/* auto gain and exposure algorithm based on the knee algorithm described here:
2190 http://ytse.tricolour.net/docs/LowLightOptimization.html
2191
2192 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2193 where changed. */
2194int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2195 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2196{
2197 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2198 const struct ctrl *gain_ctrl = NULL;
2199 const struct ctrl *exposure_ctrl = NULL;
2200 const struct ctrl *autogain_ctrl = NULL;
2201 int retval = 0;
2202
2203 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
2204 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2205 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2206 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2207 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2208 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2209 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2210 }
2211 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2212 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2213 "on cam without (auto)gain/exposure");
2214 return 0;
2215 }
2216
2217 if (gain_ctrl->get(gspca_dev, &gain) ||
2218 exposure_ctrl->get(gspca_dev, &exposure) ||
2219 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2220 return 0;
2221
2222 orig_gain = gain;
2223 orig_exposure = exposure;
2224
2225 /* If we are of a multiple of deadzone, do multiple steps to reach the
2226 desired lumination fast (with the risc of a slight overshoot) */
2227 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2228
Jean-Francois Moine1c44d812008-11-10 05:56:55 -03002229 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
Hans de Goededcef32372008-07-10 10:40:53 -03002230 avg_lum, desired_avg_lum, steps);
2231
2232 for (i = 0; i < steps; i++) {
2233 if (avg_lum > desired_avg_lum) {
2234 if (gain > gain_knee)
2235 gain--;
2236 else if (exposure > exposure_knee)
2237 exposure--;
2238 else if (gain > gain_ctrl->qctrl.default_value)
2239 gain--;
2240 else if (exposure > exposure_ctrl->qctrl.minimum)
2241 exposure--;
2242 else if (gain > gain_ctrl->qctrl.minimum)
2243 gain--;
2244 else
2245 break;
2246 } else {
2247 if (gain < gain_ctrl->qctrl.default_value)
2248 gain++;
2249 else if (exposure < exposure_knee)
2250 exposure++;
2251 else if (gain < gain_knee)
2252 gain++;
2253 else if (exposure < exposure_ctrl->qctrl.maximum)
2254 exposure++;
2255 else if (gain < gain_ctrl->qctrl.maximum)
2256 gain++;
2257 else
2258 break;
2259 }
2260 }
2261
2262 if (gain != orig_gain) {
2263 gain_ctrl->set(gspca_dev, gain);
2264 retval = 1;
2265 }
2266 if (exposure != orig_exposure) {
2267 exposure_ctrl->set(gspca_dev, exposure);
2268 retval = 1;
2269 }
2270
2271 return retval;
2272}
2273EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2274
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002275/* -- module insert / remove -- */
2276static int __init gspca_init(void)
2277{
Jean-Francois Moinebb64e862008-07-19 06:49:28 -03002278 info("main v%d.%d.%d registered",
2279 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2280 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2281 DRIVER_VERSION_NUMBER & 0xff);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002282 return 0;
2283}
2284static void __exit gspca_exit(void)
2285{
2286 info("main deregistered");
2287}
2288
2289module_init(gspca_init);
2290module_exit(gspca_exit);
2291
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03002292#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002293module_param_named(debug, gspca_debug, int, 0644);
2294MODULE_PARM_DESC(debug,
2295 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002296 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
2297 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002298#endif