blob: bd6214d4ab3beac8e9352b45a42ed80516c07346 [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 Dobriyanf0f37e22009-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine5c4fa002008-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 Moine5c4fa002008-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 }
Jean-Francois Moine83955552009-12-12 06:58:01 -03001178 if (ctrls == NULL)
1179 return -EINVAL;
Hans de Goedea0001a22009-06-10 04:52:18 -03001180 } else {
1181 ctrls = get_ctrl(gspca_dev, id);
Jean-Francois Moine83955552009-12-12 06:58:01 -03001182 if (ctrls == NULL)
1183 return -EINVAL;
Jean-Francois Moine4af85662009-11-12 15:59:27 -03001184 i = ctrls - gspca_dev->sd_desc->ctrls;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001185 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001186 memcpy(q_ctrl, ctrls, sizeof *q_ctrl);
Jean-Francois Moine4af85662009-11-12 15:59:27 -03001187 if (gspca_dev->ctrl_inac & (1 << i))
1188 q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001189 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001190}
1191
1192static int vidioc_s_ctrl(struct file *file, void *priv,
1193 struct v4l2_control *ctrl)
1194{
1195 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001196 const struct ctrl *ctrls;
Hans de Goedea0001a22009-06-10 04:52:18 -03001197 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001198
Hans de Goedea0001a22009-06-10 04:52:18 -03001199 ctrls = get_ctrl(gspca_dev, ctrl->id);
1200 if (ctrls == NULL)
1201 return -EINVAL;
1202
1203 if (ctrl->value < ctrls->qctrl.minimum
1204 || ctrl->value > ctrls->qctrl.maximum)
1205 return -ERANGE;
1206 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1207 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1208 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001209 gspca_dev->usb_err = 0;
Hans de Goedea0001a22009-06-10 04:52:18 -03001210 if (gspca_dev->present)
1211 ret = ctrls->set(gspca_dev, ctrl->value);
1212 else
1213 ret = -ENODEV;
1214 mutex_unlock(&gspca_dev->usb_lock);
1215 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001216}
1217
1218static int vidioc_g_ctrl(struct file *file, void *priv,
1219 struct v4l2_control *ctrl)
1220{
1221 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001222 const struct ctrl *ctrls;
Hans de Goedea0001a22009-06-10 04:52:18 -03001223 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001224
Hans de Goedea0001a22009-06-10 04:52:18 -03001225 ctrls = get_ctrl(gspca_dev, ctrl->id);
1226 if (ctrls == NULL)
1227 return -EINVAL;
1228
1229 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1230 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001231 gspca_dev->usb_err = 0;
Hans de Goedea0001a22009-06-10 04:52:18 -03001232 if (gspca_dev->present)
1233 ret = ctrls->get(gspca_dev, &ctrl->value);
1234 else
1235 ret = -ENODEV;
1236 mutex_unlock(&gspca_dev->usb_lock);
1237 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001238}
1239
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03001240/*fixme: have an audio flag in gspca_dev?*/
1241static int vidioc_s_audio(struct file *file, void *priv,
1242 struct v4l2_audio *audio)
1243{
1244 if (audio->index != 0)
1245 return -EINVAL;
1246 return 0;
1247}
1248
1249static int vidioc_g_audio(struct file *file, void *priv,
1250 struct v4l2_audio *audio)
1251{
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03001252 strcpy(audio->name, "Microphone");
1253 return 0;
1254}
1255
1256static int vidioc_enumaudio(struct file *file, void *priv,
1257 struct v4l2_audio *audio)
1258{
1259 if (audio->index != 0)
1260 return -EINVAL;
1261
1262 strcpy(audio->name, "Microphone");
1263 audio->capability = 0;
1264 audio->mode = 0;
1265 return 0;
1266}
1267
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001268static int vidioc_querymenu(struct file *file, void *priv,
1269 struct v4l2_querymenu *qmenu)
1270{
1271 struct gspca_dev *gspca_dev = priv;
1272
1273 if (!gspca_dev->sd_desc->querymenu)
1274 return -EINVAL;
1275 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1276}
1277
1278static int vidioc_enum_input(struct file *file, void *priv,
1279 struct v4l2_input *input)
1280{
1281 struct gspca_dev *gspca_dev = priv;
1282
1283 if (input->index != 0)
1284 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001285 input->type = V4L2_INPUT_TYPE_CAMERA;
Adam Bakerdfa76fa2009-03-29 19:17:10 -03001286 input->status = gspca_dev->cam.input_flags;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001287 strncpy(input->name, gspca_dev->sd_desc->name,
1288 sizeof input->name);
1289 return 0;
1290}
1291
1292static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1293{
1294 *i = 0;
1295 return 0;
1296}
1297
1298static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1299{
1300 if (i > 0)
1301 return -EINVAL;
1302 return (0);
1303}
1304
1305static int vidioc_reqbufs(struct file *file, void *priv,
1306 struct v4l2_requestbuffers *rb)
1307{
1308 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001309 int i, ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001310
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001311 switch (rb->memory) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001312 case GSPCA_MEMORY_READ: /* (internal call) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001313 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001314 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001315 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001316 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001317 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001318 }
Hans de Goedee2997a72008-04-23 08:09:12 -03001319 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1320 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001321
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001322 if (gspca_dev->memory != GSPCA_MEMORY_NO
1323 && gspca_dev->memory != rb->memory) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001324 ret = -EBUSY;
1325 goto out;
1326 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001327
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001328 /* only one file may do the capture */
1329 if (gspca_dev->capt_file != NULL
1330 && gspca_dev->capt_file != file) {
1331 ret = -EBUSY;
1332 goto out;
1333 }
1334
1335 /* if allocated, the buffers must not be mapped */
1336 for (i = 0; i < gspca_dev->nframes; i++) {
1337 if (gspca_dev->frame[i].vma_use_count) {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001338 ret = -EBUSY;
1339 goto out;
1340 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001341 }
1342
1343 /* stop streaming */
1344 if (gspca_dev->streaming) {
1345 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001346 gspca_dev->usb_err = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001347 gspca_stream_off(gspca_dev);
1348 mutex_unlock(&gspca_dev->usb_lock);
1349 }
1350
1351 /* free the previous allocated buffers, if any */
1352 if (gspca_dev->nframes != 0) {
1353 frame_free(gspca_dev);
1354 gspca_dev->capt_file = NULL;
1355 }
1356 if (rb->count == 0) /* unrequest */
1357 goto out;
1358 gspca_dev->memory = rb->memory;
1359 ret = frame_alloc(gspca_dev, rb->count);
1360 if (ret == 0) {
1361 rb->count = gspca_dev->nframes;
1362 gspca_dev->capt_file = file;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001363 }
1364out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001365 mutex_unlock(&gspca_dev->queue_lock);
1366 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1367 return ret;
1368}
1369
1370static int vidioc_querybuf(struct file *file, void *priv,
1371 struct v4l2_buffer *v4l2_buf)
1372{
1373 struct gspca_dev *gspca_dev = priv;
1374 struct gspca_frame *frame;
1375
Jean-Francois Moine13752bd2008-12-19 15:08:51 -03001376 if (v4l2_buf->index < 0
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001377 || v4l2_buf->index >= gspca_dev->nframes)
1378 return -EINVAL;
1379
1380 frame = &gspca_dev->frame[v4l2_buf->index];
1381 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1382 return 0;
1383}
1384
1385static int vidioc_streamon(struct file *file, void *priv,
1386 enum v4l2_buf_type buf_type)
1387{
1388 struct gspca_dev *gspca_dev = priv;
1389 int ret;
1390
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001391 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1392 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001393 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1394 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001395
Jean-Francois Moine1edabe72008-12-19 15:17:25 -03001396 if (gspca_dev->nframes == 0
1397 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001398 ret = -EINVAL;
1399 goto out;
1400 }
1401 if (!gspca_dev->streaming) {
1402 ret = gspca_init_transfer(gspca_dev);
1403 if (ret < 0)
1404 goto out;
1405 }
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001406#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001407 if (gspca_debug & D_STREAM) {
1408 PDEBUG_MODE("stream on OK",
1409 gspca_dev->pixfmt,
1410 gspca_dev->width,
1411 gspca_dev->height);
1412 }
1413#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001414 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001415out:
1416 mutex_unlock(&gspca_dev->queue_lock);
1417 return ret;
1418}
1419
1420static int vidioc_streamoff(struct file *file, void *priv,
1421 enum v4l2_buf_type buf_type)
1422{
1423 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001424 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001425
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001426 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1427 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001428 if (!gspca_dev->streaming)
1429 return 0;
1430 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1431 return -ERESTARTSYS;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001432
1433 /* stop streaming */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001434 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1435 ret = -ERESTARTSYS;
1436 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001437 }
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001438 gspca_dev->usb_err = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001439 gspca_stream_off(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001440 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001441
1442 /* empty the application queues */
1443 for (i = 0; i < gspca_dev->nframes; i++)
1444 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1445 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1446 gspca_dev->last_packet_type = DISCARD_PACKET;
1447 gspca_dev->sequence = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001448 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001449out:
1450 mutex_unlock(&gspca_dev->queue_lock);
1451 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001452}
1453
1454static int vidioc_g_jpegcomp(struct file *file, void *priv,
1455 struct v4l2_jpegcompression *jpegcomp)
1456{
1457 struct gspca_dev *gspca_dev = priv;
1458 int ret;
1459
1460 if (!gspca_dev->sd_desc->get_jcomp)
1461 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001462 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1463 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001464 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001465 if (gspca_dev->present)
1466 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1467 else
1468 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001469 mutex_unlock(&gspca_dev->usb_lock);
1470 return ret;
1471}
1472
1473static int vidioc_s_jpegcomp(struct file *file, void *priv,
1474 struct v4l2_jpegcompression *jpegcomp)
1475{
1476 struct gspca_dev *gspca_dev = priv;
1477 int ret;
1478
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001479 if (!gspca_dev->sd_desc->set_jcomp)
1480 return -EINVAL;
Jim Parisf86d4a92008-12-10 05:50:44 -03001481 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1482 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001483 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001484 if (gspca_dev->present)
1485 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1486 else
1487 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001488 mutex_unlock(&gspca_dev->usb_lock);
1489 return ret;
1490}
1491
1492static int vidioc_g_parm(struct file *filp, void *priv,
1493 struct v4l2_streamparm *parm)
1494{
1495 struct gspca_dev *gspca_dev = priv;
1496
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001497 parm->parm.capture.readbuffers = gspca_dev->nbufread;
Jim Paris627a5ef2008-12-10 06:02:42 -03001498
1499 if (gspca_dev->sd_desc->get_streamparm) {
1500 int ret;
1501
1502 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1503 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001504 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001505 if (gspca_dev->present)
1506 ret = gspca_dev->sd_desc->get_streamparm(gspca_dev,
1507 parm);
1508 else
1509 ret = -ENODEV;
Jim Paris627a5ef2008-12-10 06:02:42 -03001510 mutex_unlock(&gspca_dev->usb_lock);
1511 return ret;
1512 }
1513
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001514 return 0;
1515}
1516
1517static int vidioc_s_parm(struct file *filp, void *priv,
1518 struct v4l2_streamparm *parm)
1519{
1520 struct gspca_dev *gspca_dev = priv;
1521 int n;
1522
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001523 n = parm->parm.capture.readbuffers;
1524 if (n == 0 || n > GSPCA_MAX_FRAMES)
1525 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1526 else
1527 gspca_dev->nbufread = n;
Jim Paris627a5ef2008-12-10 06:02:42 -03001528
1529 if (gspca_dev->sd_desc->set_streamparm) {
1530 int ret;
1531
1532 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1533 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001534 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001535 if (gspca_dev->present)
1536 ret = gspca_dev->sd_desc->set_streamparm(gspca_dev,
1537 parm);
1538 else
1539 ret = -ENODEV;
Jim Paris627a5ef2008-12-10 06:02:42 -03001540 mutex_unlock(&gspca_dev->usb_lock);
1541 return ret;
1542 }
1543
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001544 return 0;
1545}
1546
1547#ifdef CONFIG_VIDEO_V4L1_COMPAT
1548static int vidiocgmbuf(struct file *file, void *priv,
1549 struct video_mbuf *mbuf)
1550{
1551 struct gspca_dev *gspca_dev = file->private_data;
1552 int i;
1553
1554 PDEBUG(D_STREAM, "cgmbuf");
1555 if (gspca_dev->nframes == 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001556 int ret;
1557
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001558 {
1559 struct v4l2_format fmt;
1560
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001561 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1562 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1563 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1564 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1565 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001566 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001567 if (ret != 0)
1568 return ret;
1569 }
1570 {
1571 struct v4l2_requestbuffers rb;
1572
1573 memset(&rb, 0, sizeof rb);
1574 rb.count = 4;
1575 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1576 rb.memory = V4L2_MEMORY_MMAP;
1577 ret = vidioc_reqbufs(file, priv, &rb);
1578 if (ret != 0)
1579 return ret;
1580 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001581 }
1582 mbuf->frames = gspca_dev->nframes;
1583 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1584 for (i = 0; i < mbuf->frames; i++)
1585 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1586 return 0;
1587}
1588#endif
1589
1590static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1591{
1592 struct gspca_dev *gspca_dev = file->private_data;
Harvey Harrisonefab8212008-07-05 06:12:47 -03001593 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001594 struct page *page;
1595 unsigned long addr, start, size;
1596 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001597
1598 start = vma->vm_start;
1599 size = vma->vm_end - vma->vm_start;
1600 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1601
Hans de Goedee2997a72008-04-23 08:09:12 -03001602 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1603 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001604 if (!gspca_dev->present) {
1605 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001606 goto out;
1607 }
1608 if (gspca_dev->capt_file != file) {
1609 ret = -EINVAL;
1610 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001611 }
1612
Harvey Harrisonefab8212008-07-05 06:12:47 -03001613 frame = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001614 for (i = 0; i < gspca_dev->nframes; ++i) {
1615 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1616 PDEBUG(D_STREAM, "mmap bad memory type");
1617 break;
1618 }
1619 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1620 == vma->vm_pgoff) {
1621 frame = &gspca_dev->frame[i];
1622 break;
1623 }
1624 }
Harvey Harrisonefab8212008-07-05 06:12:47 -03001625 if (frame == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001626 PDEBUG(D_STREAM, "mmap no frame buffer found");
1627 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001628 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001629 }
1630#ifdef CONFIG_VIDEO_V4L1_COMPAT
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001631 /* v4l1 maps all the buffers */
1632 if (i != 0
1633 || size != frame->v4l2_buf.length * gspca_dev->nframes)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001634#endif
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001635 if (size != frame->v4l2_buf.length) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001636 PDEBUG(D_STREAM, "mmap bad size");
1637 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001638 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001639 }
1640
1641 /*
1642 * - VM_IO marks the area as being a mmaped region for I/O to a
1643 * device. It also prevents the region from being core dumped.
1644 */
1645 vma->vm_flags |= VM_IO;
1646
1647 addr = (unsigned long) frame->data;
1648 while (size > 0) {
1649 page = vmalloc_to_page((void *) addr);
1650 ret = vm_insert_page(vma, start, page);
1651 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001652 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001653 start += PAGE_SIZE;
1654 addr += PAGE_SIZE;
1655 size -= PAGE_SIZE;
1656 }
1657
Jean-Francois Moined50e2572009-11-26 15:36:40 -03001658 vma->vm_ops = &gspca_vm_ops;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001659 vma->vm_private_data = frame;
1660 gspca_vm_open(vma);
Hans de Goedee2997a72008-04-23 08:09:12 -03001661 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001662out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001663 mutex_unlock(&gspca_dev->queue_lock);
1664 return ret;
1665}
1666
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001667/*
1668 * wait for a video frame
1669 *
1670 * If a frame is ready, its index is returned.
1671 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001672static int frame_wait(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001673 int nonblock_ing)
1674{
1675 struct gspca_frame *frame;
1676 int i, j, ret;
1677
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001678 /* check if a frame is ready */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001679 i = gspca_dev->fr_o;
1680 j = gspca_dev->fr_queue[i];
1681 frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001682
Hans de Goede181b7042008-11-19 17:16:26 -03001683 if (!(frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)) {
1684 if (nonblock_ing)
1685 return -EAGAIN;
1686
1687 /* wait till a frame is ready */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001688 ret = wait_event_interruptible_timeout(gspca_dev->wq,
Hans de Goede181b7042008-11-19 17:16:26 -03001689 (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) ||
1690 !gspca_dev->streaming || !gspca_dev->present,
1691 msecs_to_jiffies(3000));
1692 if (ret < 0)
1693 return ret;
1694 if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present)
Hans de Goedee2997a72008-04-23 08:09:12 -03001695 return -EIO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001696 }
Hans de Goede181b7042008-11-19 17:16:26 -03001697
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001698 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1699 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1700 gspca_dev->fr_q,
1701 gspca_dev->fr_i,
1702 gspca_dev->fr_o);
Hans de Goedee2997a72008-04-23 08:09:12 -03001703
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001704 if (gspca_dev->sd_desc->dq_callback) {
1705 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001706 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001707 if (gspca_dev->present)
1708 gspca_dev->sd_desc->dq_callback(gspca_dev);
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001709 mutex_unlock(&gspca_dev->usb_lock);
1710 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001711 return j;
1712}
1713
1714/*
1715 * dequeue a video buffer
1716 *
1717 * If nonblock_ing is false, block until a buffer is available.
1718 */
1719static int vidioc_dqbuf(struct file *file, void *priv,
1720 struct v4l2_buffer *v4l2_buf)
1721{
1722 struct gspca_dev *gspca_dev = priv;
1723 struct gspca_frame *frame;
1724 int i, ret;
1725
1726 PDEBUG(D_FRAM, "dqbuf");
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001727 if (v4l2_buf->memory != gspca_dev->memory)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001728 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001729
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001730 if (!gspca_dev->present)
1731 return -ENODEV;
1732
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001733 /* if not streaming, be sure the application will not loop forever */
1734 if (!(file->f_flags & O_NONBLOCK)
1735 && !gspca_dev->streaming && gspca_dev->users == 1)
1736 return -EINVAL;
1737
1738 /* only the capturing file may dequeue */
1739 if (gspca_dev->capt_file != file)
1740 return -EINVAL;
1741
1742 /* only one dequeue / read at a time */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001743 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1744 return -ERESTARTSYS;
1745
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001746 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001747 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001748 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001749 i = ret; /* frame index */
1750 frame = &gspca_dev->frame[i];
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001751 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001752 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001753 frame->data,
1754 frame->v4l2_buf.bytesused)) {
1755 PDEBUG(D_ERR|D_STREAM,
1756 "dqbuf cp to user failed");
1757 ret = -EFAULT;
1758 goto out;
1759 }
1760 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001761 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1762 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1763 PDEBUG(D_FRAM, "dqbuf %d", i);
1764 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001765out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001766 mutex_unlock(&gspca_dev->read_lock);
1767 return ret;
1768}
1769
1770/*
1771 * queue a video buffer
1772 *
1773 * Attempting to queue a buffer that has already been
1774 * queued will return -EINVAL.
1775 */
1776static int vidioc_qbuf(struct file *file, void *priv,
1777 struct v4l2_buffer *v4l2_buf)
1778{
1779 struct gspca_dev *gspca_dev = priv;
1780 struct gspca_frame *frame;
1781 int i, index, ret;
1782
1783 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001784
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001785 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1786 return -ERESTARTSYS;
1787
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001788 index = v4l2_buf->index;
1789 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001790 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001791 "qbuf idx %d >= %d", index, gspca_dev->nframes);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001792 ret = -EINVAL;
1793 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001794 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001795 if (v4l2_buf->memory != gspca_dev->memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001796 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001797 ret = -EINVAL;
1798 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001799 }
1800
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001801 frame = &gspca_dev->frame[index];
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001802 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1803 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001804 ret = -EINVAL;
1805 goto out;
1806 }
1807
1808 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001809
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001810 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001811 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1812 frame->v4l2_buf.length = v4l2_buf->length;
1813 }
1814
1815 /* put the buffer in the 'queued' queue */
1816 i = gspca_dev->fr_q;
1817 gspca_dev->fr_queue[i] = index;
Jean-Francois Moine8c32aa52009-12-14 14:57:49 -03001818 if (gspca_dev->fr_i == i)
1819 gspca_dev->cur_frame = frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001820 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1821 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1822 gspca_dev->fr_q,
1823 gspca_dev->fr_i,
1824 gspca_dev->fr_o);
1825
1826 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1827 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1828 ret = 0;
1829out:
1830 mutex_unlock(&gspca_dev->queue_lock);
1831 return ret;
1832}
1833
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001834/*
1835 * allocate the resources for read()
1836 */
1837static int read_alloc(struct gspca_dev *gspca_dev,
1838 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001839{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001840 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001841 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001842
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001843 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001844 if (gspca_dev->nframes == 0) {
1845 struct v4l2_requestbuffers rb;
1846
1847 memset(&rb, 0, sizeof rb);
1848 rb.count = gspca_dev->nbufread;
1849 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001850 rb.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001851 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1852 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001853 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001854 return ret;
1855 }
1856 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1857 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001858 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001859 for (i = 0; i < gspca_dev->nbufread; i++) {
1860 v4l2_buf.index = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001861 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1862 if (ret != 0) {
1863 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1864 return ret;
1865 }
1866 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001867 gspca_dev->memory = GSPCA_MEMORY_READ;
1868 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001869
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001870 /* start streaming */
1871 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1872 if (ret != 0)
1873 PDEBUG(D_STREAM, "read streamon err %d", ret);
1874 return ret;
1875}
1876
1877static unsigned int dev_poll(struct file *file, poll_table *wait)
1878{
1879 struct gspca_dev *gspca_dev = file->private_data;
1880 int i, ret;
1881
1882 PDEBUG(D_FRAM, "poll");
1883
1884 poll_wait(file, &gspca_dev->wq, wait);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001885
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001886 /* if reqbufs is not done, the user would use read() */
1887 if (gspca_dev->nframes == 0) {
1888 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1889 return POLLERR; /* not the 1st time */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001890 ret = read_alloc(gspca_dev, file);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001891 if (ret != 0)
1892 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001893 }
1894
1895 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1896 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001897
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001898 /* check the next incoming buffer */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001899 i = gspca_dev->fr_o;
1900 i = gspca_dev->fr_queue[i];
1901 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1902 ret = POLLIN | POLLRDNORM; /* something to read */
1903 else
1904 ret = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001905 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001906 if (!gspca_dev->present)
1907 return POLLHUP;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001908 return ret;
1909}
1910
1911static ssize_t dev_read(struct file *file, char __user *data,
1912 size_t count, loff_t *ppos)
1913{
1914 struct gspca_dev *gspca_dev = file->private_data;
1915 struct gspca_frame *frame;
1916 struct v4l2_buffer v4l2_buf;
1917 struct timeval timestamp;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001918 int n, ret, ret2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001919
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001920 PDEBUG(D_FRAM, "read (%zd)", count);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001921 if (!gspca_dev->present)
1922 return -ENODEV;
1923 switch (gspca_dev->memory) {
1924 case GSPCA_MEMORY_NO: /* first time */
1925 ret = read_alloc(gspca_dev, file);
1926 if (ret != 0)
1927 return ret;
1928 break;
1929 case GSPCA_MEMORY_READ:
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001930 if (gspca_dev->capt_file == file)
1931 break;
1932 /* fall thru */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001933 default:
1934 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001935 }
1936
1937 /* get a frame */
1938 jiffies_to_timeval(get_jiffies_64(), &timestamp);
1939 timestamp.tv_sec--;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001940 n = 2;
1941 for (;;) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001942 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1943 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001944 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001945 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1946 if (ret != 0) {
1947 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1948 return ret;
1949 }
1950
1951 /* if the process slept for more than 1 second,
Frank Zago219423c2008-09-26 07:43:54 -03001952 * get a newer frame */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001953 frame = &gspca_dev->frame[v4l2_buf.index];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001954 if (--n < 0)
1955 break; /* avoid infinite loop */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001956 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1957 break;
1958 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1959 if (ret != 0) {
1960 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1961 return ret;
1962 }
1963 }
1964
1965 /* copy the frame */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001966 if (count > frame->v4l2_buf.bytesused)
1967 count = frame->v4l2_buf.bytesused;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001968 ret = copy_to_user(data, frame->data, count);
1969 if (ret != 0) {
1970 PDEBUG(D_ERR|D_STREAM,
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001971 "read cp to user lack %d / %zd", ret, count);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001972 ret = -EFAULT;
1973 goto out;
1974 }
1975 ret = count;
1976out:
1977 /* in each case, requeue the buffer */
1978 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1979 if (ret2 != 0)
1980 return ret2;
1981 return ret;
1982}
1983
Hans Verkuilbec43662008-12-30 06:58:20 -03001984static struct v4l2_file_operations dev_fops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001985 .owner = THIS_MODULE,
1986 .open = dev_open,
1987 .release = dev_close,
1988 .read = dev_read,
1989 .mmap = dev_mmap,
Hans Verkuilbec43662008-12-30 06:58:20 -03001990 .unlocked_ioctl = video_ioctl2,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001991 .poll = dev_poll,
1992};
1993
Hans Verkuila3998102008-07-21 02:57:38 -03001994static const struct v4l2_ioctl_ops dev_ioctl_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001995 .vidioc_querycap = vidioc_querycap,
1996 .vidioc_dqbuf = vidioc_dqbuf,
1997 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001998 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1999 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2000 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2001 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002002 .vidioc_streamon = vidioc_streamon,
2003 .vidioc_queryctrl = vidioc_queryctrl,
2004 .vidioc_g_ctrl = vidioc_g_ctrl,
2005 .vidioc_s_ctrl = vidioc_s_ctrl,
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03002006 .vidioc_g_audio = vidioc_g_audio,
2007 .vidioc_s_audio = vidioc_s_audio,
2008 .vidioc_enumaudio = vidioc_enumaudio,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002009 .vidioc_querymenu = vidioc_querymenu,
2010 .vidioc_enum_input = vidioc_enum_input,
2011 .vidioc_g_input = vidioc_g_input,
2012 .vidioc_s_input = vidioc_s_input,
2013 .vidioc_reqbufs = vidioc_reqbufs,
2014 .vidioc_querybuf = vidioc_querybuf,
2015 .vidioc_streamoff = vidioc_streamoff,
2016 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
2017 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
2018 .vidioc_g_parm = vidioc_g_parm,
2019 .vidioc_s_parm = vidioc_s_parm,
Hans de Goede11c635a2009-05-25 15:04:22 -03002020 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Antonio Ospite28ffe772009-12-02 06:18:46 -03002021 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03002022#ifdef CONFIG_VIDEO_ADV_DEBUG
2023 .vidioc_g_register = vidioc_g_register,
2024 .vidioc_s_register = vidioc_s_register,
2025#endif
2026 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002027#ifdef CONFIG_VIDEO_V4L1_COMPAT
2028 .vidiocgmbuf = vidiocgmbuf,
2029#endif
2030};
2031
Hans Verkuila3998102008-07-21 02:57:38 -03002032static struct video_device gspca_template = {
2033 .name = "gspca main driver",
Hans Verkuila3998102008-07-21 02:57:38 -03002034 .fops = &dev_fops,
2035 .ioctl_ops = &dev_ioctl_ops,
Hans de Goeded0182302008-11-19 17:10:47 -03002036 .release = gspca_release,
Hans Verkuila3998102008-07-21 02:57:38 -03002037};
2038
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002039/*
2040 * probe and create a new gspca device
2041 *
2042 * This function must be called by the sub-driver when it is
2043 * called for probing a new device.
2044 */
2045int gspca_dev_probe(struct usb_interface *intf,
2046 const struct usb_device_id *id,
2047 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002048 int dev_size,
2049 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002050{
2051 struct usb_interface_descriptor *interface;
2052 struct gspca_dev *gspca_dev;
2053 struct usb_device *dev = interface_to_usbdev(intf);
2054 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002055
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002056 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002057
2058 /* we don't handle multi-config cameras */
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002059 if (dev->descriptor.bNumConfigurations != 1) {
2060 PDEBUG(D_ERR, "Too many config");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002061 return -ENODEV;
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002062 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002063 interface = &intf->cur_altsetting->desc;
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002064 if (interface->bInterfaceNumber > 0) {
2065 PDEBUG(D_ERR, "intf != 0");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002066 return -ENODEV;
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002067 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002068
2069 /* create the device */
2070 if (dev_size < sizeof *gspca_dev)
2071 dev_size = sizeof *gspca_dev;
2072 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
Erik Andrene293e592008-10-03 08:46:50 -03002073 if (!gspca_dev) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002074 err("couldn't kzalloc gspca struct");
Erik Andrene293e592008-10-03 08:46:50 -03002075 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002076 }
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002077 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2078 if (!gspca_dev->usb_buf) {
2079 err("out of memory");
Erik Andrene293e592008-10-03 08:46:50 -03002080 ret = -ENOMEM;
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002081 goto out;
2082 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002083 gspca_dev->dev = dev;
2084 gspca_dev->iface = interface->bInterfaceNumber;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002085 gspca_dev->nbalt = intf->num_altsetting;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002086 gspca_dev->sd_desc = sd_desc;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002087 gspca_dev->nbufread = 2;
Jean-Francois Moineff374742008-10-23 07:29:51 -03002088 gspca_dev->empty_packet = -1; /* don't check the empty packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002089
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002090 /* configure the subdriver and initialize the USB device */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002091 ret = sd_desc->config(gspca_dev, id);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002092 if (ret < 0)
2093 goto out;
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002094 ret = sd_desc->init(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002095 if (ret < 0)
2096 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002097 gspca_set_default_mode(gspca_dev);
2098
2099 mutex_init(&gspca_dev->usb_lock);
2100 mutex_init(&gspca_dev->read_lock);
2101 mutex_init(&gspca_dev->queue_lock);
2102 init_waitqueue_head(&gspca_dev->wq);
2103
2104 /* init video stuff */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002105 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
Hans de Goede24441632009-03-27 16:08:20 -03002106 gspca_dev->vdev.parent = &intf->dev;
Jean-Francois Moine5c4fa002008-11-18 15:52:31 -03002107 gspca_dev->module = module;
Oliver Neukumee56a4d2008-07-27 14:01:59 -03002108 gspca_dev->present = 1;
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002109 ret = video_register_device(&gspca_dev->vdev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002110 VFL_TYPE_GRABBER,
Jean-Francois Moine8d584a52009-01-13 06:07:59 -03002111 -1);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002112 if (ret < 0) {
2113 err("video_register_device err %d", ret);
2114 goto out;
2115 }
2116
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002117 usb_set_intfdata(intf, gspca_dev);
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002118 PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002119 return 0;
2120out:
Jean-Francois Moinee07a1d82008-11-19 06:37:53 -03002121 kfree(gspca_dev->usb_buf);
2122 kfree(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002123 return ret;
2124}
2125EXPORT_SYMBOL(gspca_dev_probe);
2126
2127/*
2128 * USB disconnection
2129 *
2130 * This function must be called by the sub-driver
2131 * when the device disconnects, after the specific resources are freed.
2132 */
2133void gspca_disconnect(struct usb_interface *intf)
2134{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002135 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002136
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002137 PDEBUG(D_PROBE, "%s disconnect",
2138 video_device_node_name(&gspca_dev->vdev));
Adam Bakerad281272009-02-04 15:33:21 -03002139 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03002140 gspca_dev->present = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002141
2142 if (gspca_dev->streaming) {
2143 destroy_urbs(gspca_dev);
2144 wake_up_interruptible(&gspca_dev->wq);
2145 }
2146
2147 /* the device is freed at exit of this function */
2148 gspca_dev->dev = NULL;
Adam Bakerad281272009-02-04 15:33:21 -03002149 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03002150
Frank Zago6b060ff2008-09-28 08:12:22 -03002151 usb_set_intfdata(intf, NULL);
2152
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002153 /* release the device */
2154 /* (this will call gspca_release() immediatly or on last close) */
2155 video_unregister_device(&gspca_dev->vdev);
Jean-Francois Moine3145b8c2008-10-05 04:22:10 -03002156
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002157/* PDEBUG(D_PROBE, "disconnect complete"); */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002158}
2159EXPORT_SYMBOL(gspca_disconnect);
2160
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002161#ifdef CONFIG_PM
2162int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2163{
2164 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2165
2166 if (!gspca_dev->streaming)
2167 return 0;
2168 gspca_dev->frozen = 1; /* avoid urb error messages */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002169 if (gspca_dev->sd_desc->stopN)
2170 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002171 destroy_urbs(gspca_dev);
2172 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002173 if (gspca_dev->sd_desc->stop0)
2174 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002175 return 0;
2176}
2177EXPORT_SYMBOL(gspca_suspend);
2178
2179int gspca_resume(struct usb_interface *intf)
2180{
2181 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2182
2183 gspca_dev->frozen = 0;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002184 gspca_dev->sd_desc->init(gspca_dev);
2185 if (gspca_dev->streaming)
2186 return gspca_init_transfer(gspca_dev);
Jean-Francois Moine1e89e2d2008-09-03 17:12:15 -03002187 return 0;
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002188}
2189EXPORT_SYMBOL(gspca_resume);
2190#endif
Hans de Goededcef3232008-07-10 10:40:53 -03002191/* -- cam driver utility functions -- */
2192
2193/* auto gain and exposure algorithm based on the knee algorithm described here:
2194 http://ytse.tricolour.net/docs/LowLightOptimization.html
2195
2196 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2197 where changed. */
2198int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2199 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2200{
2201 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2202 const struct ctrl *gain_ctrl = NULL;
2203 const struct ctrl *exposure_ctrl = NULL;
2204 const struct ctrl *autogain_ctrl = NULL;
2205 int retval = 0;
2206
2207 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
2208 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2209 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2210 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2211 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2212 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2213 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2214 }
2215 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2216 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2217 "on cam without (auto)gain/exposure");
2218 return 0;
2219 }
2220
2221 if (gain_ctrl->get(gspca_dev, &gain) ||
2222 exposure_ctrl->get(gspca_dev, &exposure) ||
2223 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2224 return 0;
2225
2226 orig_gain = gain;
2227 orig_exposure = exposure;
2228
2229 /* If we are of a multiple of deadzone, do multiple steps to reach the
2230 desired lumination fast (with the risc of a slight overshoot) */
2231 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2232
Jean-Francois Moine1c44d812008-11-10 05:56:55 -03002233 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
Hans de Goededcef3232008-07-10 10:40:53 -03002234 avg_lum, desired_avg_lum, steps);
2235
2236 for (i = 0; i < steps; i++) {
2237 if (avg_lum > desired_avg_lum) {
2238 if (gain > gain_knee)
2239 gain--;
2240 else if (exposure > exposure_knee)
2241 exposure--;
2242 else if (gain > gain_ctrl->qctrl.default_value)
2243 gain--;
2244 else if (exposure > exposure_ctrl->qctrl.minimum)
2245 exposure--;
2246 else if (gain > gain_ctrl->qctrl.minimum)
2247 gain--;
2248 else
2249 break;
2250 } else {
2251 if (gain < gain_ctrl->qctrl.default_value)
2252 gain++;
2253 else if (exposure < exposure_knee)
2254 exposure++;
2255 else if (gain < gain_knee)
2256 gain++;
2257 else if (exposure < exposure_ctrl->qctrl.maximum)
2258 exposure++;
2259 else if (gain < gain_ctrl->qctrl.maximum)
2260 gain++;
2261 else
2262 break;
2263 }
2264 }
2265
2266 if (gain != orig_gain) {
2267 gain_ctrl->set(gspca_dev, gain);
2268 retval = 1;
2269 }
2270 if (exposure != orig_exposure) {
2271 exposure_ctrl->set(gspca_dev, exposure);
2272 retval = 1;
2273 }
2274
2275 return retval;
2276}
2277EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2278
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002279/* -- module insert / remove -- */
2280static int __init gspca_init(void)
2281{
Jean-Francois Moinebb64e862008-07-19 06:49:28 -03002282 info("main v%d.%d.%d registered",
2283 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2284 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2285 DRIVER_VERSION_NUMBER & 0xff);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002286 return 0;
2287}
2288static void __exit gspca_exit(void)
2289{
2290 info("main deregistered");
2291}
2292
2293module_init(gspca_init);
2294module_exit(gspca_exit);
2295
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03002296#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002297module_param_named(debug, gspca_debug, int, 0644);
2298MODULE_PARM_DESC(debug,
2299 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002300 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
2301 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002302#endif