blob: 04aaa88ce4f579b20f7798da501da513cca8ec6e [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;
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300111
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300112 frame = gspca_dev->cur_frame;
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300113 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
114 != V4L2_BUF_FLAG_QUEUED)
115 return NULL;
116 return frame;
117}
118EXPORT_SYMBOL(gspca_get_i_frame);
119
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300120/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300121 * fill a video frame from an URB and resubmit
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300122 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300123static void fill_frame(struct gspca_dev *gspca_dev,
124 struct urb *urb)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300125{
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300126 u8 *data; /* address of data in the iso message */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300127 int i, len, st;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300128 cam_pkt_op pkt_scan;
129
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300130 if (urb->status != 0) {
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300131 if (urb->status == -ESHUTDOWN)
132 return; /* disconnection */
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300133#ifdef CONFIG_PM
Hans de Goede6a540bd2009-10-24 15:02:14 -0300134 if (gspca_dev->frozen)
135 return;
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300136#endif
Hans de Goede6a540bd2009-10-24 15:02:14 -0300137 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine81578522009-11-13 07:15:08 -0300138 urb->status = 0;
Hans de Goedebf926ad2009-10-23 06:56:06 -0300139 goto resubmit;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300140 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300141 pkt_scan = gspca_dev->sd_desc->pkt_scan;
142 for (i = 0; i < urb->number_of_packets; i++) {
143
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300144 /* check the packet status and length */
145 len = urb->iso_frame_desc[i].actual_length;
Jean-Francois Moineff374742008-10-23 07:29:51 -0300146 if (len == 0) {
147 if (gspca_dev->empty_packet == 0)
148 gspca_dev->empty_packet = 1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300149 continue;
Jean-Francois Moineff374742008-10-23 07:29:51 -0300150 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300151 st = urb->iso_frame_desc[i].status;
152 if (st) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300153 PDEBUG(D_ERR,
154 "ISOC data error: [%d] len=%d, status=%d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300155 i, len, st);
156 gspca_dev->last_packet_type = DISCARD_PACKET;
157 continue;
158 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300159
160 /* let the packet be analyzed by the subdriver */
161 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
162 i, urb->iso_frame_desc[i].offset, len);
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300163 data = (u8 *) urb->transfer_buffer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300164 + urb->iso_frame_desc[i].offset;
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300165 pkt_scan(gspca_dev, data, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300166 }
167
Hans de Goedebf926ad2009-10-23 06:56:06 -0300168resubmit:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300169 /* resubmit the URB */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300170 st = usb_submit_urb(urb, GFP_ATOMIC);
171 if (st < 0)
172 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
173}
174
175/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300176 * ISOC message interrupt from the USB device
177 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300178 * Analyse each packet and call the subdriver for copy to the frame buffer.
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300179 */
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300180static void isoc_irq(struct urb *urb)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300181{
182 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
183
Hans de Goede3647fea2008-07-15 05:36:30 -0300184 PDEBUG(D_PACK, "isoc irq");
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300185 if (!gspca_dev->streaming)
186 return;
187 fill_frame(gspca_dev, urb);
188}
189
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300190/*
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300191 * bulk message interrupt from the USB device
192 */
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300193static void bulk_irq(struct urb *urb)
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300194{
195 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300196 int st;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300197
198 PDEBUG(D_PACK, "bulk irq");
199 if (!gspca_dev->streaming)
200 return;
Jean-Francois Moine880ae242008-11-18 04:19:52 -0300201 switch (urb->status) {
202 case 0:
203 break;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300204 case -ESHUTDOWN:
205 return; /* disconnection */
Jean-Francois Moine880ae242008-11-18 04:19:52 -0300206 default:
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300207#ifdef CONFIG_PM
Hans de Goede6a540bd2009-10-24 15:02:14 -0300208 if (gspca_dev->frozen)
209 return;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300210#endif
Hans de Goede6a540bd2009-10-24 15:02:14 -0300211 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine81578522009-11-13 07:15:08 -0300212 urb->status = 0;
Hans de Goedebf926ad2009-10-23 06:56:06 -0300213 goto resubmit;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300214 }
215
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300216 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
217 gspca_dev->sd_desc->pkt_scan(gspca_dev,
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300218 urb->transfer_buffer,
219 urb->actual_length);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300220
Hans de Goedebf926ad2009-10-23 06:56:06 -0300221resubmit:
Antonio Ospitedff369a2008-11-14 07:53:32 -0300222 /* resubmit the URB */
223 if (gspca_dev->cam.bulk_nurbs != 0) {
224 st = usb_submit_urb(urb, GFP_ATOMIC);
225 if (st < 0)
226 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
227 }
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300228}
229
230/*
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300231 * add data to the current frame
232 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300233 * This function is called by the subdrivers at interrupt level.
234 *
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300235 * To build a frame, these ones must add
236 * - one FIRST_PACKET
237 * - 0 or many INTER_PACKETs
238 * - one LAST_PACKET
239 * DISCARD_PACKET invalidates the whole frame.
240 * On LAST_PACKET, a new frame is returned.
241 */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300242void gspca_frame_add(struct gspca_dev *gspca_dev,
243 enum gspca_packet_type packet_type,
244 const u8 *data,
245 int len)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300246{
Jean-Francois Moine3ec342f2009-11-13 07:38:16 -0300247 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300248 int i, j;
249
Hans de Goedee2997a72008-04-23 08:09:12 -0300250 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300251
252 /* check the availability of the frame buffer */
Jean-Francois Moine3ec342f2009-11-13 07:38:16 -0300253 frame = gspca_dev->cur_frame;
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300254 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
255 != V4L2_BUF_FLAG_QUEUED) {
256 gspca_dev->last_packet_type = DISCARD_PACKET;
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300257 return;
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300258 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300259
260 /* when start of a new frame, if the current frame buffer
261 * is not queued, discard the whole frame */
262 if (packet_type == FIRST_PACKET) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300263 frame->data_end = frame->data;
264 jiffies_to_timeval(get_jiffies_64(),
265 &frame->v4l2_buf.timestamp);
266 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300267 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
Jean-Francois Moine06ca78f2008-07-22 03:45:08 -0300268 if (packet_type == LAST_PACKET)
269 gspca_dev->last_packet_type = packet_type;
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300270 return;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300271 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300272
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300273 /* append the packet to the frame buffer */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300274 if (len > 0) {
275 if (frame->data_end - frame->data + len
276 > frame->v4l2_buf.length) {
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -0300277 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300278 frame->data_end - frame->data + len,
279 frame->v4l2_buf.length);
280 packet_type = DISCARD_PACKET;
281 } else {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300282 memcpy(frame->data_end, data, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300283 frame->data_end += len;
284 }
285 }
286 gspca_dev->last_packet_type = packet_type;
287
Frank Zago219423c2008-09-26 07:43:54 -0300288 /* if last packet, wake up the application and advance in the queue */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300289 if (packet_type == LAST_PACKET) {
290 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
291 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
292 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300293 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300294 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
295 gspca_dev->fr_i = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300296 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
297 frame->v4l2_buf.bytesused,
298 gspca_dev->fr_q,
299 i,
300 gspca_dev->fr_o);
301 j = gspca_dev->fr_queue[i];
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300302 gspca_dev->cur_frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300303 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300304}
305EXPORT_SYMBOL(gspca_frame_add);
306
307static int gspca_is_compressed(__u32 format)
308{
309 switch (format) {
310 case V4L2_PIX_FMT_MJPEG:
311 case V4L2_PIX_FMT_JPEG:
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300312 case V4L2_PIX_FMT_SPCA561:
Hans de Goedeab8f12c2008-07-04 18:29:32 -0300313 case V4L2_PIX_FMT_PAC207:
Kyle Guinnd661e622009-01-16 05:36:14 -0300314 case V4L2_PIX_FMT_MR97310A:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300315 return 1;
316 }
317 return 0;
318}
319
Jean-Francois Moined50e2572009-11-26 15:36:40 -0300320static void *rvmalloc(long size)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300321{
322 void *mem;
323 unsigned long adr;
324
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300325 mem = vmalloc_32(size);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300326 if (mem != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300327 adr = (unsigned long) mem;
Jean-Francois Moined50e2572009-11-26 15:36:40 -0300328 while (size > 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300329 SetPageReserved(vmalloc_to_page((void *) adr));
330 adr += PAGE_SIZE;
331 size -= PAGE_SIZE;
332 }
333 }
334 return mem;
335}
336
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300337static void rvfree(void *mem, long size)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300338{
339 unsigned long adr;
340
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300341 adr = (unsigned long) mem;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300342 while (size > 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300343 ClearPageReserved(vmalloc_to_page((void *) adr));
344 adr += PAGE_SIZE;
345 size -= PAGE_SIZE;
346 }
347 vfree(mem);
348}
349
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300350static int frame_alloc(struct gspca_dev *gspca_dev,
351 unsigned int count)
352{
353 struct gspca_frame *frame;
354 unsigned int frsz;
355 int i;
356
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300357 i = gspca_dev->curr_mode;
358 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300359 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300360 frsz = PAGE_ALIGN(frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300361 gspca_dev->frsz = frsz;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300362 if (count > GSPCA_MAX_FRAMES)
363 count = GSPCA_MAX_FRAMES;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300364 gspca_dev->frbuf = rvmalloc(frsz * count);
365 if (!gspca_dev->frbuf) {
366 err("frame alloc failed");
367 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300368 }
369 gspca_dev->nframes = count;
370 for (i = 0; i < count; i++) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300371 frame = &gspca_dev->frame[i];
372 frame->v4l2_buf.index = i;
373 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
374 frame->v4l2_buf.flags = 0;
375 frame->v4l2_buf.field = V4L2_FIELD_NONE;
376 frame->v4l2_buf.length = frsz;
377 frame->v4l2_buf.memory = gspca_dev->memory;
378 frame->v4l2_buf.sequence = 0;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300379 frame->data = frame->data_end =
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300380 gspca_dev->frbuf + i * frsz;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300381 frame->v4l2_buf.m.offset = i * frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300382 }
383 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
Jean-Francois Moine3ec342f2009-11-13 07:38:16 -0300384 gspca_dev->cur_frame = &gspca_dev->frame[0];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300385 gspca_dev->last_packet_type = DISCARD_PACKET;
386 gspca_dev->sequence = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300387 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300388}
389
390static void frame_free(struct gspca_dev *gspca_dev)
391{
392 int i;
393
394 PDEBUG(D_STREAM, "frame free");
Harvey Harrisonefab8212008-07-05 06:12:47 -0300395 if (gspca_dev->frbuf != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300396 rvfree(gspca_dev->frbuf,
397 gspca_dev->nframes * gspca_dev->frsz);
398 gspca_dev->frbuf = NULL;
399 for (i = 0; i < gspca_dev->nframes; i++)
400 gspca_dev->frame[i].data = NULL;
401 }
402 gspca_dev->nframes = 0;
403}
404
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300405static void destroy_urbs(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300406{
407 struct urb *urb;
408 unsigned int i;
409
410 PDEBUG(D_STREAM, "kill transfer");
Erik Andrén44732812008-10-13 15:52:46 -0300411 for (i = 0; i < MAX_NURBS; i++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300412 urb = gspca_dev->urb[i];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300413 if (urb == NULL)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300414 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300415
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300416 gspca_dev->urb[i] = NULL;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300417 usb_kill_urb(urb);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300418 if (urb->transfer_buffer != NULL)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300419 usb_buffer_free(gspca_dev->dev,
420 urb->transfer_buffer_length,
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300421 urb->transfer_buffer,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300422 urb->transfer_dma);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300423 usb_free_urb(urb);
424 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300425}
426
427/*
Frank Zago219423c2008-09-26 07:43:54 -0300428 * look for an input transfer endpoint in an alternate setting
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300429 */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300430static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300431 int xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300432{
433 struct usb_host_endpoint *ep;
434 int i, attr;
435
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300436 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
437 ep = &alt->endpoint[i];
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300438 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Jean-Francois Moine30d35e42009-04-21 13:57:31 -0300439 if (attr == xfer
440 && ep->desc.wMaxPacketSize != 0)
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300441 return ep;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300442 }
443 return NULL;
444}
445
446/*
Frank Zago219423c2008-09-26 07:43:54 -0300447 * look for an input (isoc or bulk) endpoint
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300448 *
449 * The endpoint is defined by the subdriver.
450 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
451 * This routine may be called many times when the bandwidth is too small
452 * (the bandwidth is checked on urb submit).
453 */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300454static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300455{
456 struct usb_interface *intf;
457 struct usb_host_endpoint *ep;
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300458 int xfer, i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300459
460 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300461 ep = NULL;
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300462 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
463 : USB_ENDPOINT_XFER_ISOC;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300464 i = gspca_dev->alt; /* previous alt setting */
Hans de Goedea511ba92009-10-16 07:13:07 -0300465 if (gspca_dev->cam.reverse_alts) {
466 while (++i < gspca_dev->nbalt) {
467 ep = alt_xfer(&intf->altsetting[i], xfer);
468 if (ep)
469 break;
470 }
471 } else {
472 while (--i >= 0) {
473 ep = alt_xfer(&intf->altsetting[i], xfer);
474 if (ep)
475 break;
476 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300477 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300478 if (ep == NULL) {
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300479 err("no transfer endpoint found");
480 return NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300481 }
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300482 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300483 i, ep->desc.bEndpointAddress);
Jean-Francois Moine1c8336d2009-07-28 04:38:25 -0300484 gspca_dev->alt = i; /* memorize the current alt setting */
Jean-Francois Moine8dd07ef2009-04-21 14:05:44 -0300485 if (gspca_dev->nbalt > 1) {
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300486 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
487 if (ret < 0) {
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300488 err("set alt %d err %d", i, ret);
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300489 return NULL;
490 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300491 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300492 return ep;
493}
494
495/*
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300496 * create the URBs for image transfer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300497 */
498static int create_urbs(struct gspca_dev *gspca_dev,
499 struct usb_host_endpoint *ep)
500{
501 struct urb *urb;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300502 int n, nurbs, i, psize, npkt, bsize;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300503
504 /* calculate the packet size and the number of packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300505 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300506
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300507 if (!gspca_dev->cam.bulk) { /* isoc */
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300508
509 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300510 if (gspca_dev->pkt_size == 0)
511 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
512 else
513 psize = gspca_dev->pkt_size;
Jean-Francois Moine49cb6b02009-04-25 13:29:01 -0300514 npkt = gspca_dev->cam.npkt;
515 if (npkt == 0)
516 npkt = 32; /* default value */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300517 bsize = psize * npkt;
518 PDEBUG(D_STREAM,
519 "isoc %d pkts size %d = bsize:%d",
520 npkt, psize, bsize);
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300521 nurbs = DEF_NURBS;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300522 } else { /* bulk */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300523 npkt = 0;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300524 bsize = gspca_dev->cam.bulk_size;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300525 if (bsize == 0)
526 bsize = psize;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300527 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300528 if (gspca_dev->cam.bulk_nurbs != 0)
529 nurbs = gspca_dev->cam.bulk_nurbs;
530 else
531 nurbs = 1;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300532 }
533
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300534 for (n = 0; n < nurbs; n++) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300535 urb = usb_alloc_urb(npkt, GFP_KERNEL);
536 if (!urb) {
537 err("usb_alloc_urb failed");
538 return -ENOMEM;
539 }
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300540 gspca_dev->urb[n] = urb;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300541 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300542 bsize,
543 GFP_KERNEL,
544 &urb->transfer_dma);
545
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300546 if (urb->transfer_buffer == NULL) {
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300547 err("usb_buffer_alloc failed");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300548 return -ENOMEM;
549 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300550 urb->dev = gspca_dev->dev;
551 urb->context = gspca_dev;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300552 urb->transfer_buffer_length = bsize;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300553 if (npkt != 0) { /* ISOC */
554 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
555 ep->desc.bEndpointAddress);
Frank Zago6c49da72008-09-28 07:43:00 -0300556 urb->transfer_flags = URB_ISO_ASAP
557 | URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300558 urb->interval = ep->desc.bInterval;
559 urb->complete = isoc_irq;
560 urb->number_of_packets = npkt;
561 for (i = 0; i < npkt; i++) {
562 urb->iso_frame_desc[i].length = psize;
563 urb->iso_frame_desc[i].offset = psize * i;
564 }
565 } else { /* bulk */
566 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
567 ep->desc.bEndpointAddress),
Frank Zago6c49da72008-09-28 07:43:00 -0300568 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300569 urb->complete = bulk_irq;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300570 }
571 }
572 return 0;
573}
574
575/*
576 * start the USB transfer
577 */
578static int gspca_init_transfer(struct gspca_dev *gspca_dev)
579{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300580 struct usb_host_endpoint *ep;
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300581 struct urb *urb;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300582 int n, ret;
583
Hans de Goedee2997a72008-04-23 08:09:12 -0300584 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
585 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300586
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300587 if (!gspca_dev->present) {
588 ret = -ENODEV;
589 goto out;
590 }
591
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300592 /* set the higher alternate setting and
593 * loop until urb submit succeeds */
Hans de Goedea511ba92009-10-16 07:13:07 -0300594 if (gspca_dev->cam.reverse_alts)
595 gspca_dev->alt = 0;
596 else
597 gspca_dev->alt = gspca_dev->nbalt;
598
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300599 if (gspca_dev->sd_desc->isoc_init) {
600 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
601 if (ret < 0)
602 goto out;
603 }
604 ep = get_ep(gspca_dev);
605 if (ep == NULL) {
606 ret = -EIO;
607 goto out;
608 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300609 for (;;) {
Jean-Francois Moine47aaca92009-12-15 05:23:04 -0300610 if (!gspca_dev->cam.no_urb_create) {
611 PDEBUG(D_STREAM, "init transfer alt %d",
612 gspca_dev->alt);
613 ret = create_urbs(gspca_dev, ep);
614 if (ret < 0) {
615 destroy_urbs(gspca_dev);
616 goto out;
617 }
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300618 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300619
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300620 /* clear the bulk endpoint */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300621 if (gspca_dev->cam.bulk)
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300622 usb_clear_halt(gspca_dev->dev,
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300623 gspca_dev->urb[0]->pipe);
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300624
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300625 /* start the cam */
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300626 ret = gspca_dev->sd_desc->start(gspca_dev);
627 if (ret < 0) {
628 destroy_urbs(gspca_dev);
629 goto out;
630 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300631 gspca_dev->streaming = 1;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300632
Antonio Ospitedff369a2008-11-14 07:53:32 -0300633 /* some bulk transfers are started by the subdriver */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300634 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300635 break;
636
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300637 /* submit the URBs */
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300638 for (n = 0; n < MAX_NURBS; n++) {
639 urb = gspca_dev->urb[n];
640 if (urb == NULL)
641 break;
642 ret = usb_submit_urb(urb, GFP_KERNEL);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300643 if (ret < 0)
644 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300645 }
646 if (ret >= 0)
647 break;
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300648 gspca_dev->streaming = 0;
649 destroy_urbs(gspca_dev);
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300650 if (ret != -ENOSPC) {
651 PDEBUG(D_ERR|D_STREAM,
652 "usb_submit_urb alt %d err %d",
653 gspca_dev->alt, ret);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300654 goto out;
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300655 }
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300656
657 /* the bandwidth is not wide enough
658 * negociate or try a lower alternate setting */
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300659 PDEBUG(D_ERR|D_STREAM,
660 "bandwidth not wide enough - trying again");
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300661 msleep(20); /* wait for kill complete */
662 if (gspca_dev->sd_desc->isoc_nego) {
663 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
664 if (ret < 0)
665 goto out;
666 } else {
667 ep = get_ep(gspca_dev);
668 if (ep == NULL) {
669 ret = -EIO;
670 goto out;
671 }
672 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300673 }
674out:
675 mutex_unlock(&gspca_dev->usb_lock);
676 return ret;
677}
678
679static int gspca_set_alt0(struct gspca_dev *gspca_dev)
680{
681 int ret;
682
Jean-Francois Moine8dd07ef2009-04-21 14:05:44 -0300683 if (gspca_dev->alt == 0)
684 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300685 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
686 if (ret < 0)
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -0300687 PDEBUG(D_ERR|D_STREAM, "set alt 0 err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300688 return ret;
689}
690
Frank Zago219423c2008-09-26 07:43:54 -0300691/* Note: both the queue and the usb locks should be held when calling this */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300692static void gspca_stream_off(struct gspca_dev *gspca_dev)
693{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300694 gspca_dev->streaming = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300695 if (gspca_dev->present) {
696 if (gspca_dev->sd_desc->stopN)
697 gspca_dev->sd_desc->stopN(gspca_dev);
698 destroy_urbs(gspca_dev);
Jean-Francois Moined08e2ce2009-01-20 16:21:44 -0300699 gspca_set_alt0(gspca_dev);
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300700 }
701
702 /* always call stop0 to free the subdriver's resources */
Jean-Francois Moine98522a72008-11-18 06:33:08 -0300703 if (gspca_dev->sd_desc->stop0)
704 gspca_dev->sd_desc->stop0(gspca_dev);
705 PDEBUG(D_STREAM, "stream off OK");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300706}
707
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300708static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300709{
710 int i;
711
712 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
713 gspca_dev->curr_mode = i;
714 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
715 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300716 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300717}
718
719static int wxh_to_mode(struct gspca_dev *gspca_dev,
720 int width, int height)
721{
722 int i;
723
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300724 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
725 if (width >= gspca_dev->cam.cam_mode[i].width
726 && height >= gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300727 break;
728 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300729 return i;
730}
731
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300732/*
733 * search a mode with the right pixel format
734 */
735static int gspca_get_mode(struct gspca_dev *gspca_dev,
736 int mode,
737 int pixfmt)
738{
739 int modeU, modeD;
740
741 modeU = modeD = mode;
742 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
743 if (--modeD >= 0) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300744 if (gspca_dev->cam.cam_mode[modeD].pixelformat
745 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300746 return modeD;
747 }
748 if (++modeU < gspca_dev->cam.nmodes) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300749 if (gspca_dev->cam.cam_mode[modeU].pixelformat
750 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300751 return modeU;
752 }
753 }
754 return -EINVAL;
755}
756
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300757#ifdef CONFIG_VIDEO_ADV_DEBUG
758static int vidioc_g_register(struct file *file, void *priv,
759 struct v4l2_dbg_register *reg)
760{
761 int ret;
762 struct gspca_dev *gspca_dev = priv;
763
764 if (!gspca_dev->sd_desc->get_chip_ident)
765 return -EINVAL;
766
767 if (!gspca_dev->sd_desc->get_register)
768 return -EINVAL;
769
770 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
771 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -0300772 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300773 if (gspca_dev->present)
774 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
775 else
776 ret = -ENODEV;
777 mutex_unlock(&gspca_dev->usb_lock);
778
779 return ret;
780}
781
782static int vidioc_s_register(struct file *file, void *priv,
783 struct v4l2_dbg_register *reg)
784{
785 int ret;
786 struct gspca_dev *gspca_dev = priv;
787
788 if (!gspca_dev->sd_desc->get_chip_ident)
789 return -EINVAL;
790
791 if (!gspca_dev->sd_desc->set_register)
792 return -EINVAL;
793
794 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
795 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -0300796 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300797 if (gspca_dev->present)
798 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
799 else
800 ret = -ENODEV;
801 mutex_unlock(&gspca_dev->usb_lock);
802
803 return ret;
804}
805#endif
806
807static int vidioc_g_chip_ident(struct file *file, void *priv,
808 struct v4l2_dbg_chip_ident *chip)
809{
810 int ret;
811 struct gspca_dev *gspca_dev = priv;
812
813 if (!gspca_dev->sd_desc->get_chip_ident)
814 return -EINVAL;
815
816 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
817 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -0300818 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300819 if (gspca_dev->present)
820 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
821 else
822 ret = -ENODEV;
823 mutex_unlock(&gspca_dev->usb_lock);
824
825 return ret;
826}
827
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300828static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300829 struct v4l2_fmtdesc *fmtdesc)
830{
831 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300832 int i, j, index;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300833 __u32 fmt_tb[8];
834
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300835 /* give an index to each format */
836 index = 0;
837 j = 0;
838 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300839 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300840 j = 0;
841 for (;;) {
842 if (fmt_tb[j] == fmt_tb[index])
843 break;
844 j++;
845 }
846 if (j == index) {
847 if (fmtdesc->index == index)
848 break; /* new format */
849 index++;
Julia Lawall80297122008-11-12 23:18:21 -0300850 if (index >= ARRAY_SIZE(fmt_tb))
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300851 return -EINVAL;
852 }
853 }
854 if (i < 0)
855 return -EINVAL; /* no more format */
856
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300857 fmtdesc->pixelformat = fmt_tb[index];
858 if (gspca_is_compressed(fmt_tb[index]))
859 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
860 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
861 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
862 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
863 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
864 fmtdesc->description[4] = '\0';
865 return 0;
866}
867
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300868static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300869 struct v4l2_format *fmt)
870{
871 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300872 int mode;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300873
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300874 mode = gspca_dev->curr_mode;
875 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
876 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300877 return 0;
878}
879
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300880static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300881 struct v4l2_format *fmt)
882{
Hans de Goede24c530b2008-07-04 17:55:18 -0300883 int w, h, mode, mode2;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300884
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300885 w = fmt->fmt.pix.width;
886 h = fmt->fmt.pix.height;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300887
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300888#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300889 if (gspca_debug & D_CONF)
890 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
891#endif
892 /* search the closest mode for width and height */
893 mode = wxh_to_mode(gspca_dev, w, h);
894
895 /* OK if right palette */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300896 if (gspca_dev->cam.cam_mode[mode].pixelformat
897 != fmt->fmt.pix.pixelformat) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300898
899 /* else, search the closest mode with the same pixel format */
900 mode2 = gspca_get_mode(gspca_dev, mode,
901 fmt->fmt.pix.pixelformat);
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300902 if (mode2 >= 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300903 mode = mode2;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300904/* else
905 ; * no chance, return this mode */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300906 }
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300907 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
908 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300909 return mode; /* used when s_fmt */
910}
911
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300912static int vidioc_try_fmt_vid_cap(struct file *file,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300913 void *priv,
914 struct v4l2_format *fmt)
915{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300916 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300917 int ret;
918
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300919 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300920 if (ret < 0)
921 return ret;
922 return 0;
923}
924
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300925static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300926 struct v4l2_format *fmt)
927{
928 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300929 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300930
Hans de Goedee2997a72008-04-23 08:09:12 -0300931 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
932 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300933
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300934 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300935 if (ret < 0)
936 goto out;
937
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300938 if (gspca_dev->nframes != 0
939 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
940 ret = -EINVAL;
941 goto out;
942 }
943
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300944 if (ret == gspca_dev->curr_mode) {
945 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300946 goto out; /* same mode */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300947 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300948
949 if (gspca_dev->streaming) {
950 ret = -EBUSY;
951 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -0300952 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300953 gspca_dev->width = fmt->fmt.pix.width;
954 gspca_dev->height = fmt->fmt.pix.height;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300955 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
956 gspca_dev->curr_mode = ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300957
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300958 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300959out:
960 mutex_unlock(&gspca_dev->queue_lock);
961 return ret;
962}
963
Hans de Goede11c635a2009-05-25 15:04:22 -0300964static int vidioc_enum_framesizes(struct file *file, void *priv,
965 struct v4l2_frmsizeenum *fsize)
966{
967 struct gspca_dev *gspca_dev = priv;
968 int i;
969 __u32 index = 0;
970
971 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
972 if (fsize->pixel_format !=
973 gspca_dev->cam.cam_mode[i].pixelformat)
974 continue;
975
976 if (fsize->index == index) {
977 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
978 fsize->discrete.width =
979 gspca_dev->cam.cam_mode[i].width;
980 fsize->discrete.height =
981 gspca_dev->cam.cam_mode[i].height;
982 return 0;
983 }
984 index++;
985 }
986
987 return -EINVAL;
988}
989
Antonio Ospite28ffe772009-12-02 06:18:46 -0300990static int vidioc_enum_frameintervals(struct file *filp, void *priv,
991 struct v4l2_frmivalenum *fival)
992{
993 struct gspca_dev *gspca_dev = priv;
994 int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
995 __u32 i;
996
997 if (gspca_dev->cam.mode_framerates == NULL ||
998 gspca_dev->cam.mode_framerates[mode].nrates == 0)
999 return -EINVAL;
1000
1001 if (fival->pixel_format !=
1002 gspca_dev->cam.cam_mode[mode].pixelformat)
1003 return -EINVAL;
1004
1005 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1006 if (fival->index == i) {
1007 fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1008 fival->discrete.numerator = 1;
1009 fival->discrete.denominator =
1010 gspca_dev->cam.mode_framerates[mode].rates[i];
1011 return 0;
1012 }
1013 }
1014
1015 return -EINVAL;
1016}
1017
Hans de Goeded0182302008-11-19 17:10:47 -03001018static void gspca_release(struct video_device *vfd)
Frank Zago6b060ff2008-09-28 08:12:22 -03001019{
Hans de Goeded0182302008-11-19 17:10:47 -03001020 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
Frank Zago6b060ff2008-09-28 08:12:22 -03001021
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001022 PDEBUG(D_PROBE, "%s released",
1023 video_device_node_name(&gspca_dev->vdev));
Frank Zago6b060ff2008-09-28 08:12:22 -03001024
1025 kfree(gspca_dev->usb_buf);
1026 kfree(gspca_dev);
1027}
1028
Hans Verkuilbec43662008-12-30 06:58:20 -03001029static int dev_open(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001030{
1031 struct gspca_dev *gspca_dev;
1032 int ret;
1033
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001034 PDEBUG(D_STREAM, "[%s] open", current->comm);
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001035 gspca_dev = (struct gspca_dev *) video_devdata(file);
Hans de Goedee2997a72008-04-23 08:09:12 -03001036 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1037 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001038 if (!gspca_dev->present) {
1039 ret = -ENODEV;
1040 goto out;
1041 }
1042
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001043 if (gspca_dev->users > 4) { /* (arbitrary value) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001044 ret = -EBUSY;
1045 goto out;
1046 }
Jean-Francois Moine5c4fa002008-11-18 15:52:31 -03001047
1048 /* protect the subdriver against rmmod */
1049 if (!try_module_get(gspca_dev->module)) {
1050 ret = -ENODEV;
1051 goto out;
1052 }
1053
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001054 gspca_dev->users++;
Frank Zago6b060ff2008-09-28 08:12:22 -03001055
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001056 file->private_data = gspca_dev;
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001057#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001058 /* activate the v4l2 debug */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001059 if (gspca_debug & D_V4L2)
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001060 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001061 | V4L2_DEBUG_IOCTL_ARG;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001062 else
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001063 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001064 | V4L2_DEBUG_IOCTL_ARG);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001065#endif
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001066 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001067out:
1068 mutex_unlock(&gspca_dev->queue_lock);
1069 if (ret != 0)
1070 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
1071 else
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001072 PDEBUG(D_STREAM, "open done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001073 return ret;
1074}
1075
Hans Verkuilbec43662008-12-30 06:58:20 -03001076static int dev_close(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001077{
1078 struct gspca_dev *gspca_dev = file->private_data;
1079
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001080 PDEBUG(D_STREAM, "[%s] close", current->comm);
Hans de Goedee2997a72008-04-23 08:09:12 -03001081 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1082 return -ERESTARTSYS;
1083 gspca_dev->users--;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001084
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001085 /* if the file did the capture, free the streaming resources */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001086 if (gspca_dev->capt_file == file) {
Jean-Francois Moinea1e82882008-09-03 17:12:17 -03001087 if (gspca_dev->streaming) {
1088 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001089 gspca_dev->usb_err = 0;
Jean-Francois Moinea1e82882008-09-03 17:12:17 -03001090 gspca_stream_off(gspca_dev);
1091 mutex_unlock(&gspca_dev->usb_lock);
1092 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001093 frame_free(gspca_dev);
Harvey Harrisonefab8212008-07-05 06:12:47 -03001094 gspca_dev->capt_file = NULL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001095 gspca_dev->memory = GSPCA_MEMORY_NO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001096 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001097 file->private_data = NULL;
Jean-Francois Moine5c4fa002008-11-18 15:52:31 -03001098 module_put(gspca_dev->module);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001099 mutex_unlock(&gspca_dev->queue_lock);
Frank Zago6b060ff2008-09-28 08:12:22 -03001100
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001101 PDEBUG(D_STREAM, "close done");
Frank Zago6b060ff2008-09-28 08:12:22 -03001102
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001103 return 0;
1104}
1105
1106static int vidioc_querycap(struct file *file, void *priv,
1107 struct v4l2_capability *cap)
1108{
1109 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001110 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001111
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001112 /* protect the access to the usb device */
1113 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1114 return -ERESTARTSYS;
1115 if (!gspca_dev->present) {
1116 ret = -ENODEV;
1117 goto out;
1118 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001119 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -03001120 if (gspca_dev->dev->product != NULL) {
1121 strncpy(cap->card, gspca_dev->dev->product,
1122 sizeof cap->card);
1123 } else {
1124 snprintf(cap->card, sizeof cap->card,
1125 "USB Camera (%04x:%04x)",
1126 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1127 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1128 }
Thierry MERLE59320282009-01-21 15:32:26 -03001129 usb_make_path(gspca_dev->dev, cap->bus_info, sizeof(cap->bus_info));
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001130 cap->version = DRIVER_VERSION_NUMBER;
1131 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1132 | V4L2_CAP_STREAMING
1133 | V4L2_CAP_READWRITE;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001134 ret = 0;
1135out:
1136 mutex_unlock(&gspca_dev->usb_lock);
1137 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001138}
1139
Hans de Goedea0001a22009-06-10 04:52:18 -03001140static const struct ctrl *get_ctrl(struct gspca_dev *gspca_dev,
1141 int id)
1142{
1143 const struct ctrl *ctrls;
1144 int i;
1145
1146 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1147 i < gspca_dev->sd_desc->nctrls;
1148 i++, ctrls++) {
1149 if (gspca_dev->ctrl_dis & (1 << i))
1150 continue;
1151 if (id == ctrls->qctrl.id)
1152 return ctrls;
1153 }
1154 return NULL;
1155}
1156
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001157static int vidioc_queryctrl(struct file *file, void *priv,
1158 struct v4l2_queryctrl *q_ctrl)
1159{
1160 struct gspca_dev *gspca_dev = priv;
Hans de Goedea0001a22009-06-10 04:52:18 -03001161 const struct ctrl *ctrls;
1162 int i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001163 u32 id;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001164
Hans de Goedea0001a22009-06-10 04:52:18 -03001165 ctrls = NULL;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001166 id = q_ctrl->id;
1167 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1168 id &= V4L2_CTRL_ID_MASK;
1169 id++;
1170 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001171 if (gspca_dev->ctrl_dis & (1 << i))
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001172 continue;
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001173 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001174 continue;
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001175 if (ctrls && gspca_dev->sd_desc->ctrls[i].qctrl.id
Hans de Goedea0001a22009-06-10 04:52:18 -03001176 > ctrls->qctrl.id)
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001177 continue;
Hans de Goedea0001a22009-06-10 04:52:18 -03001178 ctrls = &gspca_dev->sd_desc->ctrls[i];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001179 }
Jean-Francois Moine83955552009-12-12 06:58:01 -03001180 if (ctrls == NULL)
1181 return -EINVAL;
Hans de Goedea0001a22009-06-10 04:52:18 -03001182 } else {
1183 ctrls = get_ctrl(gspca_dev, id);
Jean-Francois Moine83955552009-12-12 06:58:01 -03001184 if (ctrls == NULL)
1185 return -EINVAL;
Jean-Francois Moine4af85662009-11-12 15:59:27 -03001186 i = ctrls - gspca_dev->sd_desc->ctrls;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001187 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001188 memcpy(q_ctrl, ctrls, sizeof *q_ctrl);
Jean-Francois Moine4af85662009-11-12 15:59:27 -03001189 if (gspca_dev->ctrl_inac & (1 << i))
1190 q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001191 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001192}
1193
1194static int vidioc_s_ctrl(struct file *file, void *priv,
1195 struct v4l2_control *ctrl)
1196{
1197 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001198 const struct ctrl *ctrls;
Hans de Goedea0001a22009-06-10 04:52:18 -03001199 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001200
Hans de Goedea0001a22009-06-10 04:52:18 -03001201 ctrls = get_ctrl(gspca_dev, ctrl->id);
1202 if (ctrls == NULL)
1203 return -EINVAL;
1204
1205 if (ctrl->value < ctrls->qctrl.minimum
1206 || ctrl->value > ctrls->qctrl.maximum)
1207 return -ERANGE;
1208 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1209 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1210 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001211 gspca_dev->usb_err = 0;
Hans de Goedea0001a22009-06-10 04:52:18 -03001212 if (gspca_dev->present)
1213 ret = ctrls->set(gspca_dev, ctrl->value);
1214 else
1215 ret = -ENODEV;
1216 mutex_unlock(&gspca_dev->usb_lock);
1217 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001218}
1219
1220static int vidioc_g_ctrl(struct file *file, void *priv,
1221 struct v4l2_control *ctrl)
1222{
1223 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001224 const struct ctrl *ctrls;
Hans de Goedea0001a22009-06-10 04:52:18 -03001225 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001226
Hans de Goedea0001a22009-06-10 04:52:18 -03001227 ctrls = get_ctrl(gspca_dev, ctrl->id);
1228 if (ctrls == NULL)
1229 return -EINVAL;
1230
1231 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1232 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001233 gspca_dev->usb_err = 0;
Hans de Goedea0001a22009-06-10 04:52:18 -03001234 if (gspca_dev->present)
1235 ret = ctrls->get(gspca_dev, &ctrl->value);
1236 else
1237 ret = -ENODEV;
1238 mutex_unlock(&gspca_dev->usb_lock);
1239 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001240}
1241
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03001242/*fixme: have an audio flag in gspca_dev?*/
1243static int vidioc_s_audio(struct file *file, void *priv,
1244 struct v4l2_audio *audio)
1245{
1246 if (audio->index != 0)
1247 return -EINVAL;
1248 return 0;
1249}
1250
1251static int vidioc_g_audio(struct file *file, void *priv,
1252 struct v4l2_audio *audio)
1253{
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03001254 strcpy(audio->name, "Microphone");
1255 return 0;
1256}
1257
1258static int vidioc_enumaudio(struct file *file, void *priv,
1259 struct v4l2_audio *audio)
1260{
1261 if (audio->index != 0)
1262 return -EINVAL;
1263
1264 strcpy(audio->name, "Microphone");
1265 audio->capability = 0;
1266 audio->mode = 0;
1267 return 0;
1268}
1269
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001270static int vidioc_querymenu(struct file *file, void *priv,
1271 struct v4l2_querymenu *qmenu)
1272{
1273 struct gspca_dev *gspca_dev = priv;
1274
1275 if (!gspca_dev->sd_desc->querymenu)
1276 return -EINVAL;
1277 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1278}
1279
1280static int vidioc_enum_input(struct file *file, void *priv,
1281 struct v4l2_input *input)
1282{
1283 struct gspca_dev *gspca_dev = priv;
1284
1285 if (input->index != 0)
1286 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001287 input->type = V4L2_INPUT_TYPE_CAMERA;
Adam Bakerdfa76fa2009-03-29 19:17:10 -03001288 input->status = gspca_dev->cam.input_flags;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001289 strncpy(input->name, gspca_dev->sd_desc->name,
1290 sizeof input->name);
1291 return 0;
1292}
1293
1294static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1295{
1296 *i = 0;
1297 return 0;
1298}
1299
1300static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1301{
1302 if (i > 0)
1303 return -EINVAL;
1304 return (0);
1305}
1306
1307static int vidioc_reqbufs(struct file *file, void *priv,
1308 struct v4l2_requestbuffers *rb)
1309{
1310 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001311 int i, ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001312
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001313 switch (rb->memory) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001314 case GSPCA_MEMORY_READ: /* (internal call) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001315 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001316 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001317 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001318 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001319 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001320 }
Hans de Goedee2997a72008-04-23 08:09:12 -03001321 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1322 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001323
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001324 if (gspca_dev->memory != GSPCA_MEMORY_NO
1325 && gspca_dev->memory != rb->memory) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001326 ret = -EBUSY;
1327 goto out;
1328 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001329
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001330 /* only one file may do the capture */
1331 if (gspca_dev->capt_file != NULL
1332 && gspca_dev->capt_file != file) {
1333 ret = -EBUSY;
1334 goto out;
1335 }
1336
1337 /* if allocated, the buffers must not be mapped */
1338 for (i = 0; i < gspca_dev->nframes; i++) {
1339 if (gspca_dev->frame[i].vma_use_count) {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001340 ret = -EBUSY;
1341 goto out;
1342 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001343 }
1344
1345 /* stop streaming */
1346 if (gspca_dev->streaming) {
1347 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001348 gspca_dev->usb_err = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001349 gspca_stream_off(gspca_dev);
1350 mutex_unlock(&gspca_dev->usb_lock);
1351 }
1352
1353 /* free the previous allocated buffers, if any */
1354 if (gspca_dev->nframes != 0) {
1355 frame_free(gspca_dev);
1356 gspca_dev->capt_file = NULL;
1357 }
1358 if (rb->count == 0) /* unrequest */
1359 goto out;
1360 gspca_dev->memory = rb->memory;
1361 ret = frame_alloc(gspca_dev, rb->count);
1362 if (ret == 0) {
1363 rb->count = gspca_dev->nframes;
1364 gspca_dev->capt_file = file;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001365 }
1366out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001367 mutex_unlock(&gspca_dev->queue_lock);
1368 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1369 return ret;
1370}
1371
1372static int vidioc_querybuf(struct file *file, void *priv,
1373 struct v4l2_buffer *v4l2_buf)
1374{
1375 struct gspca_dev *gspca_dev = priv;
1376 struct gspca_frame *frame;
1377
Jean-Francois Moine13752bd2008-12-19 15:08:51 -03001378 if (v4l2_buf->index < 0
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001379 || v4l2_buf->index >= gspca_dev->nframes)
1380 return -EINVAL;
1381
1382 frame = &gspca_dev->frame[v4l2_buf->index];
1383 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1384 return 0;
1385}
1386
1387static int vidioc_streamon(struct file *file, void *priv,
1388 enum v4l2_buf_type buf_type)
1389{
1390 struct gspca_dev *gspca_dev = priv;
1391 int ret;
1392
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001393 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1394 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001395 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1396 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001397
Jean-Francois Moine1edabe72008-12-19 15:17:25 -03001398 if (gspca_dev->nframes == 0
1399 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001400 ret = -EINVAL;
1401 goto out;
1402 }
1403 if (!gspca_dev->streaming) {
1404 ret = gspca_init_transfer(gspca_dev);
1405 if (ret < 0)
1406 goto out;
1407 }
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001408#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001409 if (gspca_debug & D_STREAM) {
1410 PDEBUG_MODE("stream on OK",
1411 gspca_dev->pixfmt,
1412 gspca_dev->width,
1413 gspca_dev->height);
1414 }
1415#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001416 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001417out:
1418 mutex_unlock(&gspca_dev->queue_lock);
1419 return ret;
1420}
1421
1422static int vidioc_streamoff(struct file *file, void *priv,
1423 enum v4l2_buf_type buf_type)
1424{
1425 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001426 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001427
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001428 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1429 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001430 if (!gspca_dev->streaming)
1431 return 0;
1432 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1433 return -ERESTARTSYS;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001434
1435 /* stop streaming */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001436 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1437 ret = -ERESTARTSYS;
1438 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001439 }
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001440 gspca_dev->usb_err = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001441 gspca_stream_off(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001442 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001443
1444 /* empty the application queues */
1445 for (i = 0; i < gspca_dev->nframes; i++)
1446 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1447 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1448 gspca_dev->last_packet_type = DISCARD_PACKET;
1449 gspca_dev->sequence = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001450 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001451out:
1452 mutex_unlock(&gspca_dev->queue_lock);
1453 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001454}
1455
1456static int vidioc_g_jpegcomp(struct file *file, void *priv,
1457 struct v4l2_jpegcompression *jpegcomp)
1458{
1459 struct gspca_dev *gspca_dev = priv;
1460 int ret;
1461
1462 if (!gspca_dev->sd_desc->get_jcomp)
1463 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001464 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1465 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001466 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001467 if (gspca_dev->present)
1468 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1469 else
1470 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001471 mutex_unlock(&gspca_dev->usb_lock);
1472 return ret;
1473}
1474
1475static int vidioc_s_jpegcomp(struct file *file, void *priv,
1476 struct v4l2_jpegcompression *jpegcomp)
1477{
1478 struct gspca_dev *gspca_dev = priv;
1479 int ret;
1480
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001481 if (!gspca_dev->sd_desc->set_jcomp)
1482 return -EINVAL;
Jim Parisf86d4a92008-12-10 05:50:44 -03001483 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1484 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001485 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001486 if (gspca_dev->present)
1487 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1488 else
1489 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001490 mutex_unlock(&gspca_dev->usb_lock);
1491 return ret;
1492}
1493
1494static int vidioc_g_parm(struct file *filp, void *priv,
1495 struct v4l2_streamparm *parm)
1496{
1497 struct gspca_dev *gspca_dev = priv;
1498
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001499 parm->parm.capture.readbuffers = gspca_dev->nbufread;
Jim Paris627a5ef2008-12-10 06:02:42 -03001500
1501 if (gspca_dev->sd_desc->get_streamparm) {
1502 int ret;
1503
1504 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1505 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001506 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001507 if (gspca_dev->present)
1508 ret = gspca_dev->sd_desc->get_streamparm(gspca_dev,
1509 parm);
1510 else
1511 ret = -ENODEV;
Jim Paris627a5ef2008-12-10 06:02:42 -03001512 mutex_unlock(&gspca_dev->usb_lock);
1513 return ret;
1514 }
1515
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001516 return 0;
1517}
1518
1519static int vidioc_s_parm(struct file *filp, void *priv,
1520 struct v4l2_streamparm *parm)
1521{
1522 struct gspca_dev *gspca_dev = priv;
1523 int n;
1524
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001525 n = parm->parm.capture.readbuffers;
1526 if (n == 0 || n > GSPCA_MAX_FRAMES)
1527 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1528 else
1529 gspca_dev->nbufread = n;
Jim Paris627a5ef2008-12-10 06:02:42 -03001530
1531 if (gspca_dev->sd_desc->set_streamparm) {
1532 int ret;
1533
1534 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1535 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001536 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001537 if (gspca_dev->present)
1538 ret = gspca_dev->sd_desc->set_streamparm(gspca_dev,
1539 parm);
1540 else
1541 ret = -ENODEV;
Jim Paris627a5ef2008-12-10 06:02:42 -03001542 mutex_unlock(&gspca_dev->usb_lock);
1543 return ret;
1544 }
1545
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001546 return 0;
1547}
1548
1549#ifdef CONFIG_VIDEO_V4L1_COMPAT
1550static int vidiocgmbuf(struct file *file, void *priv,
1551 struct video_mbuf *mbuf)
1552{
1553 struct gspca_dev *gspca_dev = file->private_data;
1554 int i;
1555
1556 PDEBUG(D_STREAM, "cgmbuf");
1557 if (gspca_dev->nframes == 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001558 int ret;
1559
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001560 {
1561 struct v4l2_format fmt;
1562
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001563 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1564 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1565 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1566 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1567 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001568 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001569 if (ret != 0)
1570 return ret;
1571 }
1572 {
1573 struct v4l2_requestbuffers rb;
1574
1575 memset(&rb, 0, sizeof rb);
1576 rb.count = 4;
1577 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1578 rb.memory = V4L2_MEMORY_MMAP;
1579 ret = vidioc_reqbufs(file, priv, &rb);
1580 if (ret != 0)
1581 return ret;
1582 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001583 }
1584 mbuf->frames = gspca_dev->nframes;
1585 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1586 for (i = 0; i < mbuf->frames; i++)
1587 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1588 return 0;
1589}
1590#endif
1591
1592static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1593{
1594 struct gspca_dev *gspca_dev = file->private_data;
Harvey Harrisonefab8212008-07-05 06:12:47 -03001595 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001596 struct page *page;
1597 unsigned long addr, start, size;
1598 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001599
1600 start = vma->vm_start;
1601 size = vma->vm_end - vma->vm_start;
1602 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1603
Hans de Goedee2997a72008-04-23 08:09:12 -03001604 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1605 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001606 if (!gspca_dev->present) {
1607 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001608 goto out;
1609 }
1610 if (gspca_dev->capt_file != file) {
1611 ret = -EINVAL;
1612 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001613 }
1614
Harvey Harrisonefab8212008-07-05 06:12:47 -03001615 frame = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001616 for (i = 0; i < gspca_dev->nframes; ++i) {
1617 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1618 PDEBUG(D_STREAM, "mmap bad memory type");
1619 break;
1620 }
1621 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1622 == vma->vm_pgoff) {
1623 frame = &gspca_dev->frame[i];
1624 break;
1625 }
1626 }
Harvey Harrisonefab8212008-07-05 06:12:47 -03001627 if (frame == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001628 PDEBUG(D_STREAM, "mmap no frame buffer found");
1629 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001630 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001631 }
1632#ifdef CONFIG_VIDEO_V4L1_COMPAT
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001633 /* v4l1 maps all the buffers */
1634 if (i != 0
1635 || size != frame->v4l2_buf.length * gspca_dev->nframes)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001636#endif
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001637 if (size != frame->v4l2_buf.length) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001638 PDEBUG(D_STREAM, "mmap bad size");
1639 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001640 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001641 }
1642
1643 /*
1644 * - VM_IO marks the area as being a mmaped region for I/O to a
1645 * device. It also prevents the region from being core dumped.
1646 */
1647 vma->vm_flags |= VM_IO;
1648
1649 addr = (unsigned long) frame->data;
1650 while (size > 0) {
1651 page = vmalloc_to_page((void *) addr);
1652 ret = vm_insert_page(vma, start, page);
1653 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001654 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001655 start += PAGE_SIZE;
1656 addr += PAGE_SIZE;
1657 size -= PAGE_SIZE;
1658 }
1659
Jean-Francois Moined50e2572009-11-26 15:36:40 -03001660 vma->vm_ops = &gspca_vm_ops;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001661 vma->vm_private_data = frame;
1662 gspca_vm_open(vma);
Hans de Goedee2997a72008-04-23 08:09:12 -03001663 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001664out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001665 mutex_unlock(&gspca_dev->queue_lock);
1666 return ret;
1667}
1668
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001669/*
1670 * wait for a video frame
1671 *
1672 * If a frame is ready, its index is returned.
1673 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001674static int frame_wait(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001675 int nonblock_ing)
1676{
1677 struct gspca_frame *frame;
1678 int i, j, ret;
1679
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001680 /* check if a frame is ready */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001681 i = gspca_dev->fr_o;
1682 j = gspca_dev->fr_queue[i];
1683 frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001684
Hans de Goede181b7042008-11-19 17:16:26 -03001685 if (!(frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)) {
1686 if (nonblock_ing)
1687 return -EAGAIN;
1688
1689 /* wait till a frame is ready */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001690 ret = wait_event_interruptible_timeout(gspca_dev->wq,
Hans de Goede181b7042008-11-19 17:16:26 -03001691 (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) ||
1692 !gspca_dev->streaming || !gspca_dev->present,
1693 msecs_to_jiffies(3000));
1694 if (ret < 0)
1695 return ret;
1696 if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present)
Hans de Goedee2997a72008-04-23 08:09:12 -03001697 return -EIO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001698 }
Hans de Goede181b7042008-11-19 17:16:26 -03001699
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001700 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1701 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1702 gspca_dev->fr_q,
1703 gspca_dev->fr_i,
1704 gspca_dev->fr_o);
Hans de Goedee2997a72008-04-23 08:09:12 -03001705
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001706 if (gspca_dev->sd_desc->dq_callback) {
1707 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001708 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001709 if (gspca_dev->present)
1710 gspca_dev->sd_desc->dq_callback(gspca_dev);
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001711 mutex_unlock(&gspca_dev->usb_lock);
1712 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001713 return j;
1714}
1715
1716/*
1717 * dequeue a video buffer
1718 *
1719 * If nonblock_ing is false, block until a buffer is available.
1720 */
1721static int vidioc_dqbuf(struct file *file, void *priv,
1722 struct v4l2_buffer *v4l2_buf)
1723{
1724 struct gspca_dev *gspca_dev = priv;
1725 struct gspca_frame *frame;
1726 int i, ret;
1727
1728 PDEBUG(D_FRAM, "dqbuf");
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001729 if (v4l2_buf->memory != gspca_dev->memory)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001730 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001731
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001732 if (!gspca_dev->present)
1733 return -ENODEV;
1734
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001735 /* if not streaming, be sure the application will not loop forever */
1736 if (!(file->f_flags & O_NONBLOCK)
1737 && !gspca_dev->streaming && gspca_dev->users == 1)
1738 return -EINVAL;
1739
1740 /* only the capturing file may dequeue */
1741 if (gspca_dev->capt_file != file)
1742 return -EINVAL;
1743
1744 /* only one dequeue / read at a time */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001745 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1746 return -ERESTARTSYS;
1747
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001748 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001749 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001750 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001751 i = ret; /* frame index */
1752 frame = &gspca_dev->frame[i];
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001753 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001754 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001755 frame->data,
1756 frame->v4l2_buf.bytesused)) {
1757 PDEBUG(D_ERR|D_STREAM,
1758 "dqbuf cp to user failed");
1759 ret = -EFAULT;
1760 goto out;
1761 }
1762 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001763 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1764 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1765 PDEBUG(D_FRAM, "dqbuf %d", i);
1766 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001767out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001768 mutex_unlock(&gspca_dev->read_lock);
1769 return ret;
1770}
1771
1772/*
1773 * queue a video buffer
1774 *
1775 * Attempting to queue a buffer that has already been
1776 * queued will return -EINVAL.
1777 */
1778static int vidioc_qbuf(struct file *file, void *priv,
1779 struct v4l2_buffer *v4l2_buf)
1780{
1781 struct gspca_dev *gspca_dev = priv;
1782 struct gspca_frame *frame;
1783 int i, index, ret;
1784
1785 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001786
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001787 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1788 return -ERESTARTSYS;
1789
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001790 index = v4l2_buf->index;
1791 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001792 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001793 "qbuf idx %d >= %d", index, gspca_dev->nframes);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001794 ret = -EINVAL;
1795 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001796 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001797 if (v4l2_buf->memory != gspca_dev->memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001798 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001799 ret = -EINVAL;
1800 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001801 }
1802
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001803 frame = &gspca_dev->frame[index];
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001804 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1805 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001806 ret = -EINVAL;
1807 goto out;
1808 }
1809
1810 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001811
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001812 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001813 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1814 frame->v4l2_buf.length = v4l2_buf->length;
1815 }
1816
1817 /* put the buffer in the 'queued' queue */
1818 i = gspca_dev->fr_q;
1819 gspca_dev->fr_queue[i] = index;
Jean-Francois Moine8c32aa52009-12-14 14:57:49 -03001820 if (gspca_dev->fr_i == i)
1821 gspca_dev->cur_frame = frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001822 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1823 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1824 gspca_dev->fr_q,
1825 gspca_dev->fr_i,
1826 gspca_dev->fr_o);
1827
1828 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1829 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1830 ret = 0;
1831out:
1832 mutex_unlock(&gspca_dev->queue_lock);
1833 return ret;
1834}
1835
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001836/*
1837 * allocate the resources for read()
1838 */
1839static int read_alloc(struct gspca_dev *gspca_dev,
1840 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001841{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001842 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001843 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001844
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001845 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001846 if (gspca_dev->nframes == 0) {
1847 struct v4l2_requestbuffers rb;
1848
1849 memset(&rb, 0, sizeof rb);
1850 rb.count = gspca_dev->nbufread;
1851 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001852 rb.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001853 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1854 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001855 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001856 return ret;
1857 }
1858 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1859 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001860 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001861 for (i = 0; i < gspca_dev->nbufread; i++) {
1862 v4l2_buf.index = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001863 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1864 if (ret != 0) {
1865 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1866 return ret;
1867 }
1868 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001869 gspca_dev->memory = GSPCA_MEMORY_READ;
1870 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001871
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001872 /* start streaming */
1873 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1874 if (ret != 0)
1875 PDEBUG(D_STREAM, "read streamon err %d", ret);
1876 return ret;
1877}
1878
1879static unsigned int dev_poll(struct file *file, poll_table *wait)
1880{
1881 struct gspca_dev *gspca_dev = file->private_data;
1882 int i, ret;
1883
1884 PDEBUG(D_FRAM, "poll");
1885
1886 poll_wait(file, &gspca_dev->wq, wait);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001887
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001888 /* if reqbufs is not done, the user would use read() */
1889 if (gspca_dev->nframes == 0) {
1890 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1891 return POLLERR; /* not the 1st time */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001892 ret = read_alloc(gspca_dev, file);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001893 if (ret != 0)
1894 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001895 }
1896
1897 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1898 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001899
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001900 /* check the next incoming buffer */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001901 i = gspca_dev->fr_o;
1902 i = gspca_dev->fr_queue[i];
1903 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1904 ret = POLLIN | POLLRDNORM; /* something to read */
1905 else
1906 ret = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001907 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001908 if (!gspca_dev->present)
1909 return POLLHUP;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001910 return ret;
1911}
1912
1913static ssize_t dev_read(struct file *file, char __user *data,
1914 size_t count, loff_t *ppos)
1915{
1916 struct gspca_dev *gspca_dev = file->private_data;
1917 struct gspca_frame *frame;
1918 struct v4l2_buffer v4l2_buf;
1919 struct timeval timestamp;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001920 int n, ret, ret2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001921
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001922 PDEBUG(D_FRAM, "read (%zd)", count);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001923 if (!gspca_dev->present)
1924 return -ENODEV;
1925 switch (gspca_dev->memory) {
1926 case GSPCA_MEMORY_NO: /* first time */
1927 ret = read_alloc(gspca_dev, file);
1928 if (ret != 0)
1929 return ret;
1930 break;
1931 case GSPCA_MEMORY_READ:
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001932 if (gspca_dev->capt_file == file)
1933 break;
1934 /* fall thru */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001935 default:
1936 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001937 }
1938
1939 /* get a frame */
1940 jiffies_to_timeval(get_jiffies_64(), &timestamp);
1941 timestamp.tv_sec--;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001942 n = 2;
1943 for (;;) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001944 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1945 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001946 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001947 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1948 if (ret != 0) {
1949 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1950 return ret;
1951 }
1952
1953 /* if the process slept for more than 1 second,
Frank Zago219423c2008-09-26 07:43:54 -03001954 * get a newer frame */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001955 frame = &gspca_dev->frame[v4l2_buf.index];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001956 if (--n < 0)
1957 break; /* avoid infinite loop */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001958 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1959 break;
1960 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1961 if (ret != 0) {
1962 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1963 return ret;
1964 }
1965 }
1966
1967 /* copy the frame */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001968 if (count > frame->v4l2_buf.bytesused)
1969 count = frame->v4l2_buf.bytesused;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001970 ret = copy_to_user(data, frame->data, count);
1971 if (ret != 0) {
1972 PDEBUG(D_ERR|D_STREAM,
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001973 "read cp to user lack %d / %zd", ret, count);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001974 ret = -EFAULT;
1975 goto out;
1976 }
1977 ret = count;
1978out:
1979 /* in each case, requeue the buffer */
1980 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1981 if (ret2 != 0)
1982 return ret2;
1983 return ret;
1984}
1985
Hans Verkuilbec43662008-12-30 06:58:20 -03001986static struct v4l2_file_operations dev_fops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001987 .owner = THIS_MODULE,
1988 .open = dev_open,
1989 .release = dev_close,
1990 .read = dev_read,
1991 .mmap = dev_mmap,
Hans Verkuilbec43662008-12-30 06:58:20 -03001992 .unlocked_ioctl = video_ioctl2,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001993 .poll = dev_poll,
1994};
1995
Hans Verkuila3998102008-07-21 02:57:38 -03001996static const struct v4l2_ioctl_ops dev_ioctl_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001997 .vidioc_querycap = vidioc_querycap,
1998 .vidioc_dqbuf = vidioc_dqbuf,
1999 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03002000 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2001 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2002 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2003 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002004 .vidioc_streamon = vidioc_streamon,
2005 .vidioc_queryctrl = vidioc_queryctrl,
2006 .vidioc_g_ctrl = vidioc_g_ctrl,
2007 .vidioc_s_ctrl = vidioc_s_ctrl,
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03002008 .vidioc_g_audio = vidioc_g_audio,
2009 .vidioc_s_audio = vidioc_s_audio,
2010 .vidioc_enumaudio = vidioc_enumaudio,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002011 .vidioc_querymenu = vidioc_querymenu,
2012 .vidioc_enum_input = vidioc_enum_input,
2013 .vidioc_g_input = vidioc_g_input,
2014 .vidioc_s_input = vidioc_s_input,
2015 .vidioc_reqbufs = vidioc_reqbufs,
2016 .vidioc_querybuf = vidioc_querybuf,
2017 .vidioc_streamoff = vidioc_streamoff,
2018 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
2019 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
2020 .vidioc_g_parm = vidioc_g_parm,
2021 .vidioc_s_parm = vidioc_s_parm,
Hans de Goede11c635a2009-05-25 15:04:22 -03002022 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Antonio Ospite28ffe772009-12-02 06:18:46 -03002023 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03002024#ifdef CONFIG_VIDEO_ADV_DEBUG
2025 .vidioc_g_register = vidioc_g_register,
2026 .vidioc_s_register = vidioc_s_register,
2027#endif
2028 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002029#ifdef CONFIG_VIDEO_V4L1_COMPAT
2030 .vidiocgmbuf = vidiocgmbuf,
2031#endif
2032};
2033
Hans Verkuila3998102008-07-21 02:57:38 -03002034static struct video_device gspca_template = {
2035 .name = "gspca main driver",
Hans Verkuila3998102008-07-21 02:57:38 -03002036 .fops = &dev_fops,
2037 .ioctl_ops = &dev_ioctl_ops,
Hans de Goeded0182302008-11-19 17:10:47 -03002038 .release = gspca_release,
Hans Verkuila3998102008-07-21 02:57:38 -03002039};
2040
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002041/*
2042 * probe and create a new gspca device
2043 *
2044 * This function must be called by the sub-driver when it is
2045 * called for probing a new device.
2046 */
2047int gspca_dev_probe(struct usb_interface *intf,
2048 const struct usb_device_id *id,
2049 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002050 int dev_size,
2051 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002052{
2053 struct usb_interface_descriptor *interface;
2054 struct gspca_dev *gspca_dev;
2055 struct usb_device *dev = interface_to_usbdev(intf);
2056 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002057
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002058 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002059
2060 /* we don't handle multi-config cameras */
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002061 if (dev->descriptor.bNumConfigurations != 1) {
2062 PDEBUG(D_ERR, "Too many config");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002063 return -ENODEV;
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002064 }
Jean-Francois Moine52a9d3e2010-01-05 04:44:00 -03002065
Jean-Francois Moineb7a597d2010-01-10 16:36:53 -03002066 /* the USB video interface must be the first one */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002067 interface = &intf->cur_altsetting->desc;
Jean-Francois Moineb7a597d2010-01-10 16:36:53 -03002068 if (dev->config->desc.bNumInterfaces != 1 &&
2069 interface->bInterfaceNumber != 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002070 return -ENODEV;
2071
2072 /* create the device */
2073 if (dev_size < sizeof *gspca_dev)
2074 dev_size = sizeof *gspca_dev;
2075 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
Erik Andrene293e592008-10-03 08:46:50 -03002076 if (!gspca_dev) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002077 err("couldn't kzalloc gspca struct");
Erik Andrene293e592008-10-03 08:46:50 -03002078 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002079 }
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002080 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2081 if (!gspca_dev->usb_buf) {
2082 err("out of memory");
Erik Andrene293e592008-10-03 08:46:50 -03002083 ret = -ENOMEM;
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002084 goto out;
2085 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002086 gspca_dev->dev = dev;
2087 gspca_dev->iface = interface->bInterfaceNumber;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002088 gspca_dev->nbalt = intf->num_altsetting;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002089 gspca_dev->sd_desc = sd_desc;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002090 gspca_dev->nbufread = 2;
Jean-Francois Moineff374742008-10-23 07:29:51 -03002091 gspca_dev->empty_packet = -1; /* don't check the empty packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002092
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002093 /* configure the subdriver and initialize the USB device */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002094 ret = sd_desc->config(gspca_dev, id);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002095 if (ret < 0)
2096 goto out;
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002097 ret = sd_desc->init(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002098 if (ret < 0)
2099 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002100 gspca_set_default_mode(gspca_dev);
2101
2102 mutex_init(&gspca_dev->usb_lock);
2103 mutex_init(&gspca_dev->read_lock);
2104 mutex_init(&gspca_dev->queue_lock);
2105 init_waitqueue_head(&gspca_dev->wq);
2106
2107 /* init video stuff */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002108 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
Hans de Goede24441632009-03-27 16:08:20 -03002109 gspca_dev->vdev.parent = &intf->dev;
Jean-Francois Moine5c4fa002008-11-18 15:52:31 -03002110 gspca_dev->module = module;
Oliver Neukumee56a4d2008-07-27 14:01:59 -03002111 gspca_dev->present = 1;
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002112 ret = video_register_device(&gspca_dev->vdev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002113 VFL_TYPE_GRABBER,
Jean-Francois Moine8d584a52009-01-13 06:07:59 -03002114 -1);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002115 if (ret < 0) {
2116 err("video_register_device err %d", ret);
2117 goto out;
2118 }
2119
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002120 usb_set_intfdata(intf, gspca_dev);
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002121 PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002122 return 0;
2123out:
Jean-Francois Moinee07a1d82008-11-19 06:37:53 -03002124 kfree(gspca_dev->usb_buf);
2125 kfree(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002126 return ret;
2127}
2128EXPORT_SYMBOL(gspca_dev_probe);
2129
2130/*
2131 * USB disconnection
2132 *
2133 * This function must be called by the sub-driver
2134 * when the device disconnects, after the specific resources are freed.
2135 */
2136void gspca_disconnect(struct usb_interface *intf)
2137{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002138 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002139
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002140 PDEBUG(D_PROBE, "%s disconnect",
2141 video_device_node_name(&gspca_dev->vdev));
Adam Bakerad281272009-02-04 15:33:21 -03002142 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03002143 gspca_dev->present = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002144
2145 if (gspca_dev->streaming) {
2146 destroy_urbs(gspca_dev);
2147 wake_up_interruptible(&gspca_dev->wq);
2148 }
2149
2150 /* the device is freed at exit of this function */
2151 gspca_dev->dev = NULL;
Adam Bakerad281272009-02-04 15:33:21 -03002152 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03002153
Frank Zago6b060ff2008-09-28 08:12:22 -03002154 usb_set_intfdata(intf, NULL);
2155
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002156 /* release the device */
2157 /* (this will call gspca_release() immediatly or on last close) */
2158 video_unregister_device(&gspca_dev->vdev);
Jean-Francois Moine3145b8c2008-10-05 04:22:10 -03002159
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002160/* PDEBUG(D_PROBE, "disconnect complete"); */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002161}
2162EXPORT_SYMBOL(gspca_disconnect);
2163
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002164#ifdef CONFIG_PM
2165int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2166{
2167 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2168
2169 if (!gspca_dev->streaming)
2170 return 0;
2171 gspca_dev->frozen = 1; /* avoid urb error messages */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002172 if (gspca_dev->sd_desc->stopN)
2173 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002174 destroy_urbs(gspca_dev);
2175 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002176 if (gspca_dev->sd_desc->stop0)
2177 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002178 return 0;
2179}
2180EXPORT_SYMBOL(gspca_suspend);
2181
2182int gspca_resume(struct usb_interface *intf)
2183{
2184 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2185
2186 gspca_dev->frozen = 0;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002187 gspca_dev->sd_desc->init(gspca_dev);
2188 if (gspca_dev->streaming)
2189 return gspca_init_transfer(gspca_dev);
Jean-Francois Moine1e89e2d2008-09-03 17:12:15 -03002190 return 0;
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002191}
2192EXPORT_SYMBOL(gspca_resume);
2193#endif
Hans de Goededcef3232008-07-10 10:40:53 -03002194/* -- cam driver utility functions -- */
2195
2196/* auto gain and exposure algorithm based on the knee algorithm described here:
2197 http://ytse.tricolour.net/docs/LowLightOptimization.html
2198
2199 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2200 where changed. */
2201int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2202 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2203{
2204 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2205 const struct ctrl *gain_ctrl = NULL;
2206 const struct ctrl *exposure_ctrl = NULL;
2207 const struct ctrl *autogain_ctrl = NULL;
2208 int retval = 0;
2209
2210 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
2211 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2212 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2213 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2214 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2215 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2216 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2217 }
2218 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2219 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2220 "on cam without (auto)gain/exposure");
2221 return 0;
2222 }
2223
2224 if (gain_ctrl->get(gspca_dev, &gain) ||
2225 exposure_ctrl->get(gspca_dev, &exposure) ||
2226 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2227 return 0;
2228
2229 orig_gain = gain;
2230 orig_exposure = exposure;
2231
2232 /* If we are of a multiple of deadzone, do multiple steps to reach the
2233 desired lumination fast (with the risc of a slight overshoot) */
2234 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2235
Jean-Francois Moine1c44d812008-11-10 05:56:55 -03002236 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
Hans de Goededcef3232008-07-10 10:40:53 -03002237 avg_lum, desired_avg_lum, steps);
2238
2239 for (i = 0; i < steps; i++) {
2240 if (avg_lum > desired_avg_lum) {
2241 if (gain > gain_knee)
2242 gain--;
2243 else if (exposure > exposure_knee)
2244 exposure--;
2245 else if (gain > gain_ctrl->qctrl.default_value)
2246 gain--;
2247 else if (exposure > exposure_ctrl->qctrl.minimum)
2248 exposure--;
2249 else if (gain > gain_ctrl->qctrl.minimum)
2250 gain--;
2251 else
2252 break;
2253 } else {
2254 if (gain < gain_ctrl->qctrl.default_value)
2255 gain++;
2256 else if (exposure < exposure_knee)
2257 exposure++;
2258 else if (gain < gain_knee)
2259 gain++;
2260 else if (exposure < exposure_ctrl->qctrl.maximum)
2261 exposure++;
2262 else if (gain < gain_ctrl->qctrl.maximum)
2263 gain++;
2264 else
2265 break;
2266 }
2267 }
2268
2269 if (gain != orig_gain) {
2270 gain_ctrl->set(gspca_dev, gain);
2271 retval = 1;
2272 }
2273 if (exposure != orig_exposure) {
2274 exposure_ctrl->set(gspca_dev, exposure);
2275 retval = 1;
2276 }
2277
2278 return retval;
2279}
2280EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2281
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002282/* -- module insert / remove -- */
2283static int __init gspca_init(void)
2284{
Jean-Francois Moinebb64e862008-07-19 06:49:28 -03002285 info("main v%d.%d.%d registered",
2286 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2287 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2288 DRIVER_VERSION_NUMBER & 0xff);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002289 return 0;
2290}
2291static void __exit gspca_exit(void)
2292{
2293 info("main deregistered");
2294}
2295
2296module_init(gspca_init);
2297module_exit(gspca_exit);
2298
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03002299#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002300module_param_named(debug, gspca_debug, int, 0644);
2301MODULE_PARM_DESC(debug,
2302 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002303 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
2304 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002305#endif