blob: fbc5666ee29ee3389f4cec37fe249423ae4bc469 [file] [log] [blame]
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001/*
2 * Main USB camera driver
3 *
Jean-François Moine27c6f9e2010-04-02 06:59:13 -03004 * Copyright (C) 2008-2010 Jean-François Moine <http://moinejf.free.fr>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03005 *
Márton Németh0274d422010-01-28 06:39:49 -03006 * Camera button input handling by Márton Németh
7 * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu>
8 *
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03009 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#define MODULE_NAME "gspca"
25
26#include <linux/init.h>
Jean-Francois Moinecf0fe082008-10-17 04:36:47 -030027#include <linux/version.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030028#include <linux/fs.h>
29#include <linux/vmalloc.h>
30#include <linux/sched.h>
31#include <linux/slab.h>
32#include <linux/mm.h>
33#include <linux/string.h>
34#include <linux/pagemap.h>
Jean-Francois Moine956e42d2008-07-01 10:03:42 -030035#include <linux/io.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030036#include <asm/page.h>
Jean-Francois Moine956e42d2008-07-01 10:03:42 -030037#include <linux/uaccess.h>
Paulo Assis2f51d002010-03-07 06:56:56 -030038#include <linux/ktime.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030039#include <media/v4l2-ioctl.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030040
41#include "gspca.h"
42
Randy Dunlapf7628012010-03-30 14:32:04 -030043#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -030044#include <linux/input.h>
45#include <linux/usb/input.h>
Jean-François Moinee070bf92010-02-18 15:02:51 -030046#endif
Márton Németh0274d422010-01-28 06:39:49 -030047
Jean-Francois Moined43fa322008-06-12 10:58:58 -030048/* global values */
Jean-Francois Moine8fee8452009-01-17 04:46:38 -030049#define DEF_NURBS 3 /* default number of URBs */
50#if DEF_NURBS > MAX_NURBS
51#error "DEF_NURBS too big"
52#endif
Jean-Francois Moined43fa322008-06-12 10:58:58 -030053
Jean-François Moine27c6f9e2010-04-02 06:59:13 -030054MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030055MODULE_DESCRIPTION("GSPCA USB Camera Driver");
56MODULE_LICENSE("GPL");
57
Jean-François Moine0b8851b2010-10-29 07:56:06 -030058#define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 11, 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030059
Jean-Francois Moine335b3f82008-07-30 04:53:02 -030060#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030061int gspca_debug = D_ERR | D_PROBE;
62EXPORT_SYMBOL(gspca_debug);
63
64static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
65{
66 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
67 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
68 txt,
69 pixfmt & 0xff,
70 (pixfmt >> 8) & 0xff,
71 (pixfmt >> 16) & 0xff,
72 pixfmt >> 24,
73 w, h);
74 } else {
75 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
76 txt,
77 pixfmt,
78 w, h);
79 }
80}
81#else
82#define PDEBUG_MODE(txt, pixfmt, w, h)
83#endif
84
Jean-Francois Moine77ee3312009-11-07 15:30:50 -030085/* specific memory types - !! should be different from V4L2_MEMORY_xxx */
Jean-Francois Moined43fa322008-06-12 10:58:58 -030086#define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
87#define GSPCA_MEMORY_READ 7
88
Jean-Francois Moined43fa322008-06-12 10:58:58 -030089#define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
Jean-Francois Moined43fa322008-06-12 10:58:58 -030090
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030091/*
92 * VMA operations.
93 */
94static void gspca_vm_open(struct vm_area_struct *vma)
95{
96 struct gspca_frame *frame = vma->vm_private_data;
97
98 frame->vma_use_count++;
99 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
100}
101
102static void gspca_vm_close(struct vm_area_struct *vma)
103{
104 struct gspca_frame *frame = vma->vm_private_data;
105
106 if (--frame->vma_use_count <= 0)
107 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
108}
109
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400110static const struct vm_operations_struct gspca_vm_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300111 .open = gspca_vm_open,
112 .close = gspca_vm_close,
113};
114
Márton Németh0274d422010-01-28 06:39:49 -0300115/*
116 * Input and interrupt endpoint handling functions
117 */
Randy Dunlapf7628012010-03-30 14:32:04 -0300118#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -0300119static void int_irq(struct urb *urb)
120{
121 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
122 int ret;
123
124 ret = urb->status;
125 switch (ret) {
126 case 0:
127 if (gspca_dev->sd_desc->int_pkt_scan(gspca_dev,
128 urb->transfer_buffer, urb->actual_length) < 0) {
129 PDEBUG(D_ERR, "Unknown packet received");
130 }
131 break;
132
133 case -ENOENT:
134 case -ECONNRESET:
135 case -ENODEV:
136 case -ESHUTDOWN:
137 /* Stop is requested either by software or hardware is gone,
138 * keep the ret value non-zero and don't resubmit later.
139 */
140 break;
141
142 default:
143 PDEBUG(D_ERR, "URB error %i, resubmitting", urb->status);
144 urb->status = 0;
145 ret = 0;
146 }
147
148 if (ret == 0) {
149 ret = usb_submit_urb(urb, GFP_ATOMIC);
150 if (ret < 0)
Jean-François Moine0b656322010-09-13 05:19:58 -0300151 err("Resubmit URB failed with error %i", ret);
Márton Németh0274d422010-01-28 06:39:49 -0300152 }
153}
154
155static int gspca_input_connect(struct gspca_dev *dev)
156{
157 struct input_dev *input_dev;
158 int err = 0;
159
160 dev->input_dev = NULL;
Hans de Goedeac82f592010-02-19 04:28:39 -0300161 if (dev->sd_desc->int_pkt_scan || dev->sd_desc->other_input) {
Márton Németh0274d422010-01-28 06:39:49 -0300162 input_dev = input_allocate_device();
163 if (!input_dev)
164 return -ENOMEM;
165
166 usb_make_path(dev->dev, dev->phys, sizeof(dev->phys));
167 strlcat(dev->phys, "/input0", sizeof(dev->phys));
168
169 input_dev->name = dev->sd_desc->name;
170 input_dev->phys = dev->phys;
171
172 usb_to_input_id(dev->dev, &input_dev->id);
173
174 input_dev->evbit[0] = BIT_MASK(EV_KEY);
175 input_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
176 input_dev->dev.parent = &dev->dev->dev;
177
178 err = input_register_device(input_dev);
179 if (err) {
Jean-François Moine0b656322010-09-13 05:19:58 -0300180 err("Input device registration failed with error %i",
181 err);
Márton Németh0274d422010-01-28 06:39:49 -0300182 input_dev->dev.parent = NULL;
183 input_free_device(input_dev);
184 } else {
185 dev->input_dev = input_dev;
186 }
Hans de Goede10ee2402010-02-19 04:41:40 -0300187 }
Márton Németh0274d422010-01-28 06:39:49 -0300188
189 return err;
190}
191
192static int alloc_and_submit_int_urb(struct gspca_dev *gspca_dev,
193 struct usb_endpoint_descriptor *ep)
194{
195 unsigned int buffer_len;
196 int interval;
197 struct urb *urb;
198 struct usb_device *dev;
199 void *buffer = NULL;
200 int ret = -EINVAL;
201
Jean-François Moineb26d6e22010-04-29 06:01:53 -0300202 buffer_len = le16_to_cpu(ep->wMaxPacketSize);
Márton Németh0274d422010-01-28 06:39:49 -0300203 interval = ep->bInterval;
Jean-François Moinec4dc6922010-07-14 06:26:54 -0300204 PDEBUG(D_CONF, "found int in endpoint: 0x%x, "
Márton Németh0274d422010-01-28 06:39:49 -0300205 "buffer_len=%u, interval=%u",
206 ep->bEndpointAddress, buffer_len, interval);
207
208 dev = gspca_dev->dev;
209
210 urb = usb_alloc_urb(0, GFP_KERNEL);
211 if (!urb) {
212 ret = -ENOMEM;
213 goto error;
214 }
215
Linus Torvalds7a9b1492010-05-20 21:26:12 -0700216 buffer = usb_alloc_coherent(dev, buffer_len,
Márton Németh0274d422010-01-28 06:39:49 -0300217 GFP_KERNEL, &urb->transfer_dma);
218 if (!buffer) {
219 ret = -ENOMEM;
220 goto error_buffer;
221 }
222 usb_fill_int_urb(urb, dev,
223 usb_rcvintpipe(dev, ep->bEndpointAddress),
224 buffer, buffer_len,
225 int_irq, (void *)gspca_dev, interval);
Jason Wang882787f2010-09-03 06:57:19 -0300226 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Márton Németh0274d422010-01-28 06:39:49 -0300227 ret = usb_submit_urb(urb, GFP_KERNEL);
228 if (ret < 0) {
Jean-François Moinec4dc6922010-07-14 06:26:54 -0300229 PDEBUG(D_ERR, "submit int URB failed with error %i", ret);
Márton Németh0274d422010-01-28 06:39:49 -0300230 goto error_submit;
231 }
Hans de Goede2661da42010-10-26 05:16:32 -0300232 gspca_dev->int_urb = urb;
Márton Németh0274d422010-01-28 06:39:49 -0300233 return ret;
234
235error_submit:
Daniel Mack997ea582010-04-12 13:17:25 +0200236 usb_free_coherent(dev,
237 urb->transfer_buffer_length,
238 urb->transfer_buffer,
239 urb->transfer_dma);
Márton Németh0274d422010-01-28 06:39:49 -0300240error_buffer:
241 usb_free_urb(urb);
242error:
243 return ret;
244}
245
Hans de Goede10ee2402010-02-19 04:41:40 -0300246static void gspca_input_create_urb(struct gspca_dev *gspca_dev)
Márton Németh0274d422010-01-28 06:39:49 -0300247{
Márton Németh0274d422010-01-28 06:39:49 -0300248 struct usb_interface *intf;
249 struct usb_host_interface *intf_desc;
250 struct usb_endpoint_descriptor *ep;
251 int i;
252
253 if (gspca_dev->sd_desc->int_pkt_scan) {
254 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
255 intf_desc = intf->cur_altsetting;
256 for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
257 ep = &intf_desc->endpoint[i].desc;
258 if (usb_endpoint_dir_in(ep) &&
259 usb_endpoint_xfer_int(ep)) {
260
Hans de Goede10ee2402010-02-19 04:41:40 -0300261 alloc_and_submit_int_urb(gspca_dev, ep);
Márton Németh0274d422010-01-28 06:39:49 -0300262 break;
263 }
264 }
265 }
Márton Németh0274d422010-01-28 06:39:49 -0300266}
267
268static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
269{
270 struct urb *urb;
271
272 urb = gspca_dev->int_urb;
273 if (urb) {
274 gspca_dev->int_urb = NULL;
275 usb_kill_urb(urb);
Daniel Mack997ea582010-04-12 13:17:25 +0200276 usb_free_coherent(gspca_dev->dev,
277 urb->transfer_buffer_length,
278 urb->transfer_buffer,
279 urb->transfer_dma);
Márton Németh0274d422010-01-28 06:39:49 -0300280 usb_free_urb(urb);
281 }
282}
Randy Dunlapf7628012010-03-30 14:32:04 -0300283#else
284static inline void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
285{
286}
287
288static inline void gspca_input_create_urb(struct gspca_dev *gspca_dev)
289{
290}
291
292static inline int gspca_input_connect(struct gspca_dev *dev)
293{
294 return 0;
295}
Márton Németh0274d422010-01-28 06:39:49 -0300296#endif
297
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300298/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300299 * fill a video frame from an URB and resubmit
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300300 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300301static void fill_frame(struct gspca_dev *gspca_dev,
302 struct urb *urb)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300303{
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300304 u8 *data; /* address of data in the iso message */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300305 int i, len, st;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300306 cam_pkt_op pkt_scan;
307
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300308 if (urb->status != 0) {
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300309 if (urb->status == -ESHUTDOWN)
310 return; /* disconnection */
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300311#ifdef CONFIG_PM
Hans de Goede6a540bd2009-10-24 15:02:14 -0300312 if (gspca_dev->frozen)
313 return;
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300314#endif
Hans de Goede6a540bd2009-10-24 15:02:14 -0300315 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine81578522009-11-13 07:15:08 -0300316 urb->status = 0;
Hans de Goedebf926ad2009-10-23 06:56:06 -0300317 goto resubmit;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300318 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300319 pkt_scan = gspca_dev->sd_desc->pkt_scan;
320 for (i = 0; i < urb->number_of_packets; i++) {
Mauro Carvalho Chehab95f18be2010-12-27 12:00:03 -0300321 len = urb->iso_frame_desc[i].actual_length;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300322
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300323 /* check the packet status and length */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300324 st = urb->iso_frame_desc[i].status;
325 if (st) {
Jean-François Moine0b656322010-09-13 05:19:58 -0300326 err("ISOC data error: [%d] len=%d, status=%d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300327 i, len, st);
328 gspca_dev->last_packet_type = DISCARD_PACKET;
329 continue;
330 }
Jean-François Moine76ebc162010-12-25 13:22:21 -0300331 if (len == 0) {
332 if (gspca_dev->empty_packet == 0)
333 gspca_dev->empty_packet = 1;
334 continue;
335 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300336
337 /* let the packet be analyzed by the subdriver */
338 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
339 i, urb->iso_frame_desc[i].offset, len);
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300340 data = (u8 *) urb->transfer_buffer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300341 + urb->iso_frame_desc[i].offset;
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300342 pkt_scan(gspca_dev, data, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300343 }
344
Hans de Goedebf926ad2009-10-23 06:56:06 -0300345resubmit:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300346 /* resubmit the URB */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300347 st = usb_submit_urb(urb, GFP_ATOMIC);
348 if (st < 0)
Jean-François Moine0b656322010-09-13 05:19:58 -0300349 err("usb_submit_urb() ret %d", st);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300350}
351
352/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300353 * ISOC message interrupt from the USB device
354 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300355 * Analyse each packet and call the subdriver for copy to the frame buffer.
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300356 */
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300357static void isoc_irq(struct urb *urb)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300358{
359 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
360
Hans de Goede3647fea2008-07-15 05:36:30 -0300361 PDEBUG(D_PACK, "isoc irq");
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300362 if (!gspca_dev->streaming)
363 return;
364 fill_frame(gspca_dev, urb);
365}
366
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300367/*
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300368 * bulk message interrupt from the USB device
369 */
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300370static void bulk_irq(struct urb *urb)
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300371{
372 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300373 int st;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300374
375 PDEBUG(D_PACK, "bulk irq");
376 if (!gspca_dev->streaming)
377 return;
Jean-Francois Moine880ae242008-11-18 04:19:52 -0300378 switch (urb->status) {
379 case 0:
380 break;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300381 case -ESHUTDOWN:
382 return; /* disconnection */
Jean-Francois Moine880ae242008-11-18 04:19:52 -0300383 default:
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300384#ifdef CONFIG_PM
Hans de Goede6a540bd2009-10-24 15:02:14 -0300385 if (gspca_dev->frozen)
386 return;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300387#endif
Hans de Goede6a540bd2009-10-24 15:02:14 -0300388 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine81578522009-11-13 07:15:08 -0300389 urb->status = 0;
Hans de Goedebf926ad2009-10-23 06:56:06 -0300390 goto resubmit;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300391 }
392
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300393 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
394 gspca_dev->sd_desc->pkt_scan(gspca_dev,
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300395 urb->transfer_buffer,
396 urb->actual_length);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300397
Hans de Goedebf926ad2009-10-23 06:56:06 -0300398resubmit:
Antonio Ospitedff369a2008-11-14 07:53:32 -0300399 /* resubmit the URB */
400 if (gspca_dev->cam.bulk_nurbs != 0) {
401 st = usb_submit_urb(urb, GFP_ATOMIC);
402 if (st < 0)
Jean-François Moine0b656322010-09-13 05:19:58 -0300403 err("usb_submit_urb() ret %d", st);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300404 }
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300405}
406
407/*
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300408 * add data to the current frame
409 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300410 * This function is called by the subdrivers at interrupt level.
411 *
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300412 * To build a frame, these ones must add
413 * - one FIRST_PACKET
414 * - 0 or many INTER_PACKETs
415 * - one LAST_PACKET
416 * DISCARD_PACKET invalidates the whole frame.
417 * On LAST_PACKET, a new frame is returned.
418 */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300419void gspca_frame_add(struct gspca_dev *gspca_dev,
420 enum gspca_packet_type packet_type,
421 const u8 *data,
422 int len)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300423{
Jean-Francois Moine3ec342f2009-11-13 07:38:16 -0300424 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300425 int i, j;
426
Hans de Goedee2997a72008-04-23 08:09:12 -0300427 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300428
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300429 if (packet_type == FIRST_PACKET) {
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300430 i = atomic_read(&gspca_dev->fr_i);
431
432 /* if there are no queued buffer, discard the whole frame */
433 if (i == atomic_read(&gspca_dev->fr_q)) {
434 gspca_dev->last_packet_type = DISCARD_PACKET;
Jean-François Moine2e0544a2010-10-16 13:59:13 -0300435 gspca_dev->sequence++;
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300436 return;
437 }
Jean-François Moineb192ca92010-06-27 03:08:19 -0300438 j = gspca_dev->fr_queue[i];
439 frame = &gspca_dev->frame[j];
Paulo Assis2f51d002010-03-07 06:56:56 -0300440 frame->v4l2_buf.timestamp = ktime_to_timeval(ktime_get());
Jean-François Moine2e0544a2010-10-16 13:59:13 -0300441 frame->v4l2_buf.sequence = gspca_dev->sequence++;
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300442 gspca_dev->image = frame->data;
Jean-François Moineb192ca92010-06-27 03:08:19 -0300443 gspca_dev->image_len = 0;
Jean-François Moinefe988f52010-07-29 02:46:02 -0300444 } else {
445 switch (gspca_dev->last_packet_type) {
446 case DISCARD_PACKET:
447 if (packet_type == LAST_PACKET)
448 gspca_dev->last_packet_type = packet_type;
449 return;
450 case LAST_PACKET:
451 return;
452 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300453 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300454
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300455 /* append the packet to the frame buffer */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300456 if (len > 0) {
Jean-François Moineb192ca92010-06-27 03:08:19 -0300457 if (gspca_dev->image_len + len > gspca_dev->frsz) {
458 PDEBUG(D_ERR|D_PACK, "frame overflow %d > %d",
459 gspca_dev->image_len + len,
460 gspca_dev->frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300461 packet_type = DISCARD_PACKET;
462 } else {
Jean-François Moinefe988f52010-07-29 02:46:02 -0300463/* !! image is NULL only when last pkt is LAST or DISCARD
464 if (gspca_dev->image == NULL) {
465 err("gspca_frame_add() image == NULL");
466 return;
467 }
468 */
Jean-François Moineb192ca92010-06-27 03:08:19 -0300469 memcpy(gspca_dev->image + gspca_dev->image_len,
470 data, len);
471 gspca_dev->image_len += len;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300472 }
473 }
474 gspca_dev->last_packet_type = packet_type;
475
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300476 /* if last packet, invalidate packet concatenation until
477 * next first packet, wake up the application and advance
478 * in the queue */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300479 if (packet_type == LAST_PACKET) {
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300480 i = atomic_read(&gspca_dev->fr_i);
Jean-François Moineb192ca92010-06-27 03:08:19 -0300481 j = gspca_dev->fr_queue[i];
482 frame = &gspca_dev->frame[j];
483 frame->v4l2_buf.bytesused = gspca_dev->image_len;
Jean-François Moine02bbcb92010-07-06 04:16:40 -0300484 frame->v4l2_buf.flags = (frame->v4l2_buf.flags
485 | V4L2_BUF_FLAG_DONE)
486 & ~V4L2_BUF_FLAG_QUEUED;
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300487 i = (i + 1) % GSPCA_MAX_FRAMES;
488 atomic_set(&gspca_dev->fr_i, i);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300489 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300490 PDEBUG(D_FRAM, "frame complete len:%d",
491 frame->v4l2_buf.bytesused);
492 gspca_dev->image = NULL;
493 gspca_dev->image_len = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300494 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300495}
496EXPORT_SYMBOL(gspca_frame_add);
497
498static int gspca_is_compressed(__u32 format)
499{
500 switch (format) {
501 case V4L2_PIX_FMT_MJPEG:
502 case V4L2_PIX_FMT_JPEG:
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300503 case V4L2_PIX_FMT_SPCA561:
Hans de Goedeab8f12c2008-07-04 18:29:32 -0300504 case V4L2_PIX_FMT_PAC207:
Kyle Guinnd661e622009-01-16 05:36:14 -0300505 case V4L2_PIX_FMT_MR97310A:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300506 return 1;
507 }
508 return 0;
509}
510
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300511static int frame_alloc(struct gspca_dev *gspca_dev,
512 unsigned int count)
513{
514 struct gspca_frame *frame;
515 unsigned int frsz;
516 int i;
517
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300518 i = gspca_dev->curr_mode;
519 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300520 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300521 frsz = PAGE_ALIGN(frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300522 gspca_dev->frsz = frsz;
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300523 if (count >= GSPCA_MAX_FRAMES)
524 count = GSPCA_MAX_FRAMES - 1;
Jean-François Moinee795d912010-06-24 05:14:22 -0300525 gspca_dev->frbuf = vmalloc_32(frsz * count);
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300526 if (!gspca_dev->frbuf) {
527 err("frame alloc failed");
528 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300529 }
530 gspca_dev->nframes = count;
531 for (i = 0; i < count; i++) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300532 frame = &gspca_dev->frame[i];
533 frame->v4l2_buf.index = i;
534 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
535 frame->v4l2_buf.flags = 0;
536 frame->v4l2_buf.field = V4L2_FIELD_NONE;
537 frame->v4l2_buf.length = frsz;
538 frame->v4l2_buf.memory = gspca_dev->memory;
539 frame->v4l2_buf.sequence = 0;
Jean-François Moineb192ca92010-06-27 03:08:19 -0300540 frame->data = gspca_dev->frbuf + i * frsz;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300541 frame->v4l2_buf.m.offset = i * frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300542 }
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300543 atomic_set(&gspca_dev->fr_q, 0);
544 atomic_set(&gspca_dev->fr_i, 0);
545 gspca_dev->fr_o = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300546 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300547}
548
549static void frame_free(struct gspca_dev *gspca_dev)
550{
551 int i;
552
553 PDEBUG(D_STREAM, "frame free");
Harvey Harrisonefab8212008-07-05 06:12:47 -0300554 if (gspca_dev->frbuf != NULL) {
Jean-François Moinee795d912010-06-24 05:14:22 -0300555 vfree(gspca_dev->frbuf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300556 gspca_dev->frbuf = NULL;
557 for (i = 0; i < gspca_dev->nframes; i++)
558 gspca_dev->frame[i].data = NULL;
559 }
560 gspca_dev->nframes = 0;
561}
562
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300563static void destroy_urbs(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300564{
565 struct urb *urb;
566 unsigned int i;
567
568 PDEBUG(D_STREAM, "kill transfer");
Erik Andrén44732812008-10-13 15:52:46 -0300569 for (i = 0; i < MAX_NURBS; i++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300570 urb = gspca_dev->urb[i];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300571 if (urb == NULL)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300572 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300573
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300574 gspca_dev->urb[i] = NULL;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300575 usb_kill_urb(urb);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300576 if (urb->transfer_buffer != NULL)
Daniel Mack997ea582010-04-12 13:17:25 +0200577 usb_free_coherent(gspca_dev->dev,
578 urb->transfer_buffer_length,
579 urb->transfer_buffer,
580 urb->transfer_dma);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300581 usb_free_urb(urb);
582 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300583}
584
Jean-François Moinee4dac282010-04-09 06:11:36 -0300585static int gspca_set_alt0(struct gspca_dev *gspca_dev)
586{
587 int ret;
588
589 if (gspca_dev->alt == 0)
590 return 0;
591 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
592 if (ret < 0)
Jean-François Moine0b656322010-09-13 05:19:58 -0300593 err("set alt 0 err %d", ret);
Jean-François Moinee4dac282010-04-09 06:11:36 -0300594 return ret;
595}
596
597/* Note: both the queue and the usb locks should be held when calling this */
598static void gspca_stream_off(struct gspca_dev *gspca_dev)
599{
600 gspca_dev->streaming = 0;
601 if (gspca_dev->present) {
602 if (gspca_dev->sd_desc->stopN)
603 gspca_dev->sd_desc->stopN(gspca_dev);
604 destroy_urbs(gspca_dev);
605 gspca_input_destroy_urb(gspca_dev);
606 gspca_set_alt0(gspca_dev);
607 gspca_input_create_urb(gspca_dev);
608 }
609
610 /* always call stop0 to free the subdriver's resources */
611 if (gspca_dev->sd_desc->stop0)
612 gspca_dev->sd_desc->stop0(gspca_dev);
613 PDEBUG(D_STREAM, "stream off OK");
614}
615
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300616/*
Frank Zago219423c2008-09-26 07:43:54 -0300617 * look for an input transfer endpoint in an alternate setting
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300618 */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300619static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300620 int xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300621{
622 struct usb_host_endpoint *ep;
623 int i, attr;
624
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300625 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
626 ep = &alt->endpoint[i];
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300627 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Jean-Francois Moine30d35e42009-04-21 13:57:31 -0300628 if (attr == xfer
629 && ep->desc.wMaxPacketSize != 0)
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300630 return ep;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300631 }
632 return NULL;
633}
634
635/*
Frank Zago219423c2008-09-26 07:43:54 -0300636 * look for an input (isoc or bulk) endpoint
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300637 *
638 * The endpoint is defined by the subdriver.
639 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
640 * This routine may be called many times when the bandwidth is too small
641 * (the bandwidth is checked on urb submit).
642 */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300643static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300644{
645 struct usb_interface *intf;
646 struct usb_host_endpoint *ep;
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300647 int xfer, i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300648
649 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300650 ep = NULL;
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300651 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
652 : USB_ENDPOINT_XFER_ISOC;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300653 i = gspca_dev->alt; /* previous alt setting */
Hans de Goedea511ba92009-10-16 07:13:07 -0300654 if (gspca_dev->cam.reverse_alts) {
655 while (++i < gspca_dev->nbalt) {
656 ep = alt_xfer(&intf->altsetting[i], xfer);
657 if (ep)
658 break;
659 }
660 } else {
661 while (--i >= 0) {
662 ep = alt_xfer(&intf->altsetting[i], xfer);
663 if (ep)
664 break;
665 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300666 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300667 if (ep == NULL) {
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300668 err("no transfer endpoint found");
669 return NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300670 }
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300671 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300672 i, ep->desc.bEndpointAddress);
Jean-Francois Moine1c8336d2009-07-28 04:38:25 -0300673 gspca_dev->alt = i; /* memorize the current alt setting */
Jean-Francois Moine8dd07ef2009-04-21 14:05:44 -0300674 if (gspca_dev->nbalt > 1) {
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300675 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
676 if (ret < 0) {
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300677 err("set alt %d err %d", i, ret);
Márton Németh0274d422010-01-28 06:39:49 -0300678 ep = NULL;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300679 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300680 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300681 return ep;
682}
683
684/*
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300685 * create the URBs for image transfer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300686 */
687static int create_urbs(struct gspca_dev *gspca_dev,
688 struct usb_host_endpoint *ep)
689{
690 struct urb *urb;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300691 int n, nurbs, i, psize, npkt, bsize;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300692
693 /* calculate the packet size and the number of packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300694 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300695
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300696 if (!gspca_dev->cam.bulk) { /* isoc */
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300697
698 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300699 if (gspca_dev->pkt_size == 0)
700 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
701 else
702 psize = gspca_dev->pkt_size;
Jean-Francois Moine49cb6b02009-04-25 13:29:01 -0300703 npkt = gspca_dev->cam.npkt;
704 if (npkt == 0)
705 npkt = 32; /* default value */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300706 bsize = psize * npkt;
707 PDEBUG(D_STREAM,
708 "isoc %d pkts size %d = bsize:%d",
709 npkt, psize, bsize);
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300710 nurbs = DEF_NURBS;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300711 } else { /* bulk */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300712 npkt = 0;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300713 bsize = gspca_dev->cam.bulk_size;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300714 if (bsize == 0)
715 bsize = psize;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300716 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300717 if (gspca_dev->cam.bulk_nurbs != 0)
718 nurbs = gspca_dev->cam.bulk_nurbs;
719 else
720 nurbs = 1;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300721 }
722
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300723 for (n = 0; n < nurbs; n++) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300724 urb = usb_alloc_urb(npkt, GFP_KERNEL);
725 if (!urb) {
726 err("usb_alloc_urb failed");
727 return -ENOMEM;
728 }
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300729 gspca_dev->urb[n] = urb;
Daniel Mack997ea582010-04-12 13:17:25 +0200730 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300731 bsize,
732 GFP_KERNEL,
733 &urb->transfer_dma);
734
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300735 if (urb->transfer_buffer == NULL) {
Daniel Mack997ea582010-04-12 13:17:25 +0200736 err("usb_alloc_coherent failed");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300737 return -ENOMEM;
738 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300739 urb->dev = gspca_dev->dev;
740 urb->context = gspca_dev;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300741 urb->transfer_buffer_length = bsize;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300742 if (npkt != 0) { /* ISOC */
743 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
744 ep->desc.bEndpointAddress);
Frank Zago6c49da72008-09-28 07:43:00 -0300745 urb->transfer_flags = URB_ISO_ASAP
746 | URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300747 urb->interval = ep->desc.bInterval;
748 urb->complete = isoc_irq;
749 urb->number_of_packets = npkt;
750 for (i = 0; i < npkt; i++) {
751 urb->iso_frame_desc[i].length = psize;
752 urb->iso_frame_desc[i].offset = psize * i;
753 }
754 } else { /* bulk */
755 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
Jean-François Moinea23c1942010-10-29 07:57:03 -0300756 ep->desc.bEndpointAddress);
Frank Zago6c49da72008-09-28 07:43:00 -0300757 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300758 urb->complete = bulk_irq;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300759 }
760 }
761 return 0;
762}
763
764/*
765 * start the USB transfer
766 */
767static int gspca_init_transfer(struct gspca_dev *gspca_dev)
768{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300769 struct usb_host_endpoint *ep;
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300770 struct urb *urb;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300771 int n, ret;
772
Hans de Goedee2997a72008-04-23 08:09:12 -0300773 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
774 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300775
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300776 if (!gspca_dev->present) {
777 ret = -ENODEV;
Hans de Goede314b3e02010-10-26 05:15:46 -0300778 goto unlock;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300779 }
780
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300781 /* reset the streaming variables */
782 gspca_dev->image = NULL;
783 gspca_dev->image_len = 0;
784 gspca_dev->last_packet_type = DISCARD_PACKET;
785 gspca_dev->sequence = 0;
786
Jean-Francois Moineab6c8cf2010-01-13 15:34:27 -0300787 gspca_dev->usb_err = 0;
788
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300789 /* set the higher alternate setting and
790 * loop until urb submit succeeds */
Hans de Goedea511ba92009-10-16 07:13:07 -0300791 if (gspca_dev->cam.reverse_alts)
792 gspca_dev->alt = 0;
793 else
794 gspca_dev->alt = gspca_dev->nbalt;
795
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300796 if (gspca_dev->sd_desc->isoc_init) {
797 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
798 if (ret < 0)
Hans de Goede314b3e02010-10-26 05:15:46 -0300799 goto unlock;
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300800 }
Hans de Goede314b3e02010-10-26 05:15:46 -0300801
802 gspca_input_destroy_urb(gspca_dev);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300803 ep = get_ep(gspca_dev);
804 if (ep == NULL) {
805 ret = -EIO;
806 goto out;
807 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300808 for (;;) {
Jean-Francois Moine47aaca92009-12-15 05:23:04 -0300809 if (!gspca_dev->cam.no_urb_create) {
810 PDEBUG(D_STREAM, "init transfer alt %d",
811 gspca_dev->alt);
812 ret = create_urbs(gspca_dev, ep);
813 if (ret < 0) {
814 destroy_urbs(gspca_dev);
815 goto out;
816 }
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300817 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300818
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300819 /* clear the bulk endpoint */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300820 if (gspca_dev->cam.bulk)
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300821 usb_clear_halt(gspca_dev->dev,
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300822 gspca_dev->urb[0]->pipe);
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300823
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300824 /* start the cam */
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300825 ret = gspca_dev->sd_desc->start(gspca_dev);
826 if (ret < 0) {
827 destroy_urbs(gspca_dev);
828 goto out;
829 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300830 gspca_dev->streaming = 1;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300831
Antonio Ospitedff369a2008-11-14 07:53:32 -0300832 /* some bulk transfers are started by the subdriver */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300833 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300834 break;
835
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300836 /* submit the URBs */
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300837 for (n = 0; n < MAX_NURBS; n++) {
838 urb = gspca_dev->urb[n];
839 if (urb == NULL)
840 break;
841 ret = usb_submit_urb(urb, GFP_KERNEL);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300842 if (ret < 0)
843 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300844 }
845 if (ret >= 0)
846 break;
Jean-François Moinee4dac282010-04-09 06:11:36 -0300847 gspca_stream_off(gspca_dev);
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300848 if (ret != -ENOSPC) {
Jean-François Moine0b656322010-09-13 05:19:58 -0300849 err("usb_submit_urb alt %d err %d",
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300850 gspca_dev->alt, ret);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300851 goto out;
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300852 }
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300853
854 /* the bandwidth is not wide enough
855 * negociate or try a lower alternate setting */
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300856 PDEBUG(D_ERR|D_STREAM,
857 "bandwidth not wide enough - trying again");
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300858 msleep(20); /* wait for kill complete */
859 if (gspca_dev->sd_desc->isoc_nego) {
860 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
861 if (ret < 0)
862 goto out;
863 } else {
864 ep = get_ep(gspca_dev);
865 if (ep == NULL) {
866 ret = -EIO;
867 goto out;
868 }
869 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300870 }
871out:
Hans de Goede314b3e02010-10-26 05:15:46 -0300872 gspca_input_create_urb(gspca_dev);
873unlock:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300874 mutex_unlock(&gspca_dev->usb_lock);
875 return ret;
876}
877
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300878static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300879{
Jean-François Moine6a330912010-10-02 04:12:25 -0300880 struct gspca_ctrl *ctrl;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300881 int i;
882
883 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
884 gspca_dev->curr_mode = i;
885 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
886 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300887 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-François Moine6a330912010-10-02 04:12:25 -0300888
889 /* set the current control values to their default values
890 * which may have changed in sd_init() */
891 ctrl = gspca_dev->cam.ctrls;
892 if (ctrl != NULL) {
893 for (i = 0;
894 i < gspca_dev->sd_desc->nctrls;
895 i++, ctrl++)
896 ctrl->val = ctrl->def;
897 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300898}
899
900static int wxh_to_mode(struct gspca_dev *gspca_dev,
901 int width, int height)
902{
903 int i;
904
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300905 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
906 if (width >= gspca_dev->cam.cam_mode[i].width
907 && height >= gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300908 break;
909 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300910 return i;
911}
912
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300913/*
914 * search a mode with the right pixel format
915 */
916static int gspca_get_mode(struct gspca_dev *gspca_dev,
917 int mode,
918 int pixfmt)
919{
920 int modeU, modeD;
921
922 modeU = modeD = mode;
923 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
924 if (--modeD >= 0) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300925 if (gspca_dev->cam.cam_mode[modeD].pixelformat
926 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300927 return modeD;
928 }
929 if (++modeU < gspca_dev->cam.nmodes) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300930 if (gspca_dev->cam.cam_mode[modeU].pixelformat
931 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300932 return modeU;
933 }
934 }
935 return -EINVAL;
936}
937
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300938#ifdef CONFIG_VIDEO_ADV_DEBUG
939static int vidioc_g_register(struct file *file, void *priv,
940 struct v4l2_dbg_register *reg)
941{
942 int ret;
943 struct gspca_dev *gspca_dev = priv;
944
945 if (!gspca_dev->sd_desc->get_chip_ident)
946 return -EINVAL;
947
948 if (!gspca_dev->sd_desc->get_register)
949 return -EINVAL;
950
951 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
952 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -0300953 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300954 if (gspca_dev->present)
955 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
956 else
957 ret = -ENODEV;
958 mutex_unlock(&gspca_dev->usb_lock);
959
960 return ret;
961}
962
963static int vidioc_s_register(struct file *file, void *priv,
964 struct v4l2_dbg_register *reg)
965{
966 int ret;
967 struct gspca_dev *gspca_dev = priv;
968
969 if (!gspca_dev->sd_desc->get_chip_ident)
970 return -EINVAL;
971
972 if (!gspca_dev->sd_desc->set_register)
973 return -EINVAL;
974
975 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
976 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -0300977 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300978 if (gspca_dev->present)
979 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
980 else
981 ret = -ENODEV;
982 mutex_unlock(&gspca_dev->usb_lock);
983
984 return ret;
985}
986#endif
987
988static int vidioc_g_chip_ident(struct file *file, void *priv,
989 struct v4l2_dbg_chip_ident *chip)
990{
991 int ret;
992 struct gspca_dev *gspca_dev = priv;
993
994 if (!gspca_dev->sd_desc->get_chip_ident)
995 return -EINVAL;
996
997 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
998 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -0300999 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03001000 if (gspca_dev->present)
1001 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
1002 else
1003 ret = -ENODEV;
1004 mutex_unlock(&gspca_dev->usb_lock);
1005
1006 return ret;
1007}
1008
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001009static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001010 struct v4l2_fmtdesc *fmtdesc)
1011{
1012 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001013 int i, j, index;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001014 __u32 fmt_tb[8];
1015
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001016 /* give an index to each format */
1017 index = 0;
1018 j = 0;
1019 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001020 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001021 j = 0;
1022 for (;;) {
1023 if (fmt_tb[j] == fmt_tb[index])
1024 break;
1025 j++;
1026 }
1027 if (j == index) {
1028 if (fmtdesc->index == index)
1029 break; /* new format */
1030 index++;
Julia Lawall80297122008-11-12 23:18:21 -03001031 if (index >= ARRAY_SIZE(fmt_tb))
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001032 return -EINVAL;
1033 }
1034 }
1035 if (i < 0)
1036 return -EINVAL; /* no more format */
1037
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001038 fmtdesc->pixelformat = fmt_tb[index];
1039 if (gspca_is_compressed(fmt_tb[index]))
1040 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
1041 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
1042 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
1043 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
1044 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
1045 fmtdesc->description[4] = '\0';
1046 return 0;
1047}
1048
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001049static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001050 struct v4l2_format *fmt)
1051{
1052 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001053 int mode;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001054
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001055 mode = gspca_dev->curr_mode;
1056 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1057 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001058 return 0;
1059}
1060
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001061static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001062 struct v4l2_format *fmt)
1063{
Hans de Goede24c530b2008-07-04 17:55:18 -03001064 int w, h, mode, mode2;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001065
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001066 w = fmt->fmt.pix.width;
1067 h = fmt->fmt.pix.height;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001068
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001069#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001070 if (gspca_debug & D_CONF)
1071 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
1072#endif
1073 /* search the closest mode for width and height */
1074 mode = wxh_to_mode(gspca_dev, w, h);
1075
1076 /* OK if right palette */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001077 if (gspca_dev->cam.cam_mode[mode].pixelformat
1078 != fmt->fmt.pix.pixelformat) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001079
1080 /* else, search the closest mode with the same pixel format */
1081 mode2 = gspca_get_mode(gspca_dev, mode,
1082 fmt->fmt.pix.pixelformat);
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001083 if (mode2 >= 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001084 mode = mode2;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001085/* else
1086 ; * no chance, return this mode */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001087 }
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001088 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1089 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001090 return mode; /* used when s_fmt */
1091}
1092
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001093static int vidioc_try_fmt_vid_cap(struct file *file,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001094 void *priv,
1095 struct v4l2_format *fmt)
1096{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001097 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001098 int ret;
1099
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001100 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001101 if (ret < 0)
1102 return ret;
1103 return 0;
1104}
1105
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001106static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001107 struct v4l2_format *fmt)
1108{
1109 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001110 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001111
Hans de Goedee2997a72008-04-23 08:09:12 -03001112 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1113 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001114
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001115 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001116 if (ret < 0)
1117 goto out;
1118
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001119 if (gspca_dev->nframes != 0
1120 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
1121 ret = -EINVAL;
1122 goto out;
1123 }
1124
Jean-Francois Moine50a871f2008-06-30 19:47:33 -03001125 if (ret == gspca_dev->curr_mode) {
1126 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001127 goto out; /* same mode */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -03001128 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001129
1130 if (gspca_dev->streaming) {
1131 ret = -EBUSY;
1132 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -03001133 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001134 gspca_dev->width = fmt->fmt.pix.width;
1135 gspca_dev->height = fmt->fmt.pix.height;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001136 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
1137 gspca_dev->curr_mode = ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001138
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001139 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001140out:
1141 mutex_unlock(&gspca_dev->queue_lock);
1142 return ret;
1143}
1144
Hans de Goede11c635a2009-05-25 15:04:22 -03001145static int vidioc_enum_framesizes(struct file *file, void *priv,
1146 struct v4l2_frmsizeenum *fsize)
1147{
1148 struct gspca_dev *gspca_dev = priv;
1149 int i;
1150 __u32 index = 0;
1151
1152 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
1153 if (fsize->pixel_format !=
1154 gspca_dev->cam.cam_mode[i].pixelformat)
1155 continue;
1156
1157 if (fsize->index == index) {
1158 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1159 fsize->discrete.width =
1160 gspca_dev->cam.cam_mode[i].width;
1161 fsize->discrete.height =
1162 gspca_dev->cam.cam_mode[i].height;
1163 return 0;
1164 }
1165 index++;
1166 }
1167
1168 return -EINVAL;
1169}
1170
Antonio Ospite28ffe772009-12-02 06:18:46 -03001171static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1172 struct v4l2_frmivalenum *fival)
1173{
1174 struct gspca_dev *gspca_dev = priv;
1175 int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
1176 __u32 i;
1177
1178 if (gspca_dev->cam.mode_framerates == NULL ||
1179 gspca_dev->cam.mode_framerates[mode].nrates == 0)
1180 return -EINVAL;
1181
1182 if (fival->pixel_format !=
1183 gspca_dev->cam.cam_mode[mode].pixelformat)
1184 return -EINVAL;
1185
1186 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1187 if (fival->index == i) {
1188 fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1189 fival->discrete.numerator = 1;
1190 fival->discrete.denominator =
1191 gspca_dev->cam.mode_framerates[mode].rates[i];
1192 return 0;
1193 }
1194 }
1195
1196 return -EINVAL;
1197}
1198
Hans de Goeded0182302008-11-19 17:10:47 -03001199static void gspca_release(struct video_device *vfd)
Frank Zago6b060ff2008-09-28 08:12:22 -03001200{
Hans de Goeded0182302008-11-19 17:10:47 -03001201 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
Frank Zago6b060ff2008-09-28 08:12:22 -03001202
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001203 PDEBUG(D_PROBE, "%s released",
1204 video_device_node_name(&gspca_dev->vdev));
Frank Zago6b060ff2008-09-28 08:12:22 -03001205
1206 kfree(gspca_dev->usb_buf);
1207 kfree(gspca_dev);
1208}
1209
Hans Verkuilbec43662008-12-30 06:58:20 -03001210static int dev_open(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001211{
1212 struct gspca_dev *gspca_dev;
1213 int ret;
1214
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001215 PDEBUG(D_STREAM, "[%s] open", current->comm);
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001216 gspca_dev = (struct gspca_dev *) video_devdata(file);
Hans de Goedee2997a72008-04-23 08:09:12 -03001217 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1218 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001219 if (!gspca_dev->present) {
1220 ret = -ENODEV;
1221 goto out;
1222 }
1223
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001224 if (gspca_dev->users > 4) { /* (arbitrary value) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001225 ret = -EBUSY;
1226 goto out;
1227 }
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03001228
1229 /* protect the subdriver against rmmod */
1230 if (!try_module_get(gspca_dev->module)) {
1231 ret = -ENODEV;
1232 goto out;
1233 }
1234
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001235 gspca_dev->users++;
Frank Zago6b060ff2008-09-28 08:12:22 -03001236
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001237 file->private_data = gspca_dev;
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001238#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001239 /* activate the v4l2 debug */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001240 if (gspca_debug & D_V4L2)
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001241 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001242 | V4L2_DEBUG_IOCTL_ARG;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001243 else
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001244 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001245 | V4L2_DEBUG_IOCTL_ARG);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001246#endif
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001247 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001248out:
1249 mutex_unlock(&gspca_dev->queue_lock);
1250 if (ret != 0)
1251 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
1252 else
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001253 PDEBUG(D_STREAM, "open done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001254 return ret;
1255}
1256
Hans Verkuilbec43662008-12-30 06:58:20 -03001257static int dev_close(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001258{
1259 struct gspca_dev *gspca_dev = file->private_data;
1260
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001261 PDEBUG(D_STREAM, "[%s] close", current->comm);
Hans de Goedee2997a72008-04-23 08:09:12 -03001262 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1263 return -ERESTARTSYS;
1264 gspca_dev->users--;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001265
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001266 /* if the file did the capture, free the streaming resources */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001267 if (gspca_dev->capt_file == file) {
Jean-Francois Moinea1e82882008-09-03 17:12:17 -03001268 if (gspca_dev->streaming) {
1269 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001270 gspca_dev->usb_err = 0;
Jean-Francois Moinea1e82882008-09-03 17:12:17 -03001271 gspca_stream_off(gspca_dev);
1272 mutex_unlock(&gspca_dev->usb_lock);
1273 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001274 frame_free(gspca_dev);
Harvey Harrisonefab8212008-07-05 06:12:47 -03001275 gspca_dev->capt_file = NULL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001276 gspca_dev->memory = GSPCA_MEMORY_NO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001277 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001278 file->private_data = NULL;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03001279 module_put(gspca_dev->module);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001280 mutex_unlock(&gspca_dev->queue_lock);
Frank Zago6b060ff2008-09-28 08:12:22 -03001281
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001282 PDEBUG(D_STREAM, "close done");
Frank Zago6b060ff2008-09-28 08:12:22 -03001283
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001284 return 0;
1285}
1286
1287static int vidioc_querycap(struct file *file, void *priv,
1288 struct v4l2_capability *cap)
1289{
1290 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001291 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001292
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001293 /* protect the access to the usb device */
1294 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1295 return -ERESTARTSYS;
1296 if (!gspca_dev->present) {
1297 ret = -ENODEV;
1298 goto out;
1299 }
Jean-François Moined8518592010-12-28 06:55:01 -03001300 strncpy((char *) cap->driver, gspca_dev->sd_desc->name,
1301 sizeof cap->driver);
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -03001302 if (gspca_dev->dev->product != NULL) {
Jean-François Moined8518592010-12-28 06:55:01 -03001303 strncpy((char *) cap->card, gspca_dev->dev->product,
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -03001304 sizeof cap->card);
1305 } else {
Jean-François Moined8518592010-12-28 06:55:01 -03001306 snprintf((char *) cap->card, sizeof cap->card,
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -03001307 "USB Camera (%04x:%04x)",
1308 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1309 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1310 }
Jean-François Moined8518592010-12-28 06:55:01 -03001311 usb_make_path(gspca_dev->dev, (char *) cap->bus_info,
1312 sizeof(cap->bus_info));
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001313 cap->version = DRIVER_VERSION_NUMBER;
1314 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1315 | V4L2_CAP_STREAMING
1316 | V4L2_CAP_READWRITE;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001317 ret = 0;
1318out:
1319 mutex_unlock(&gspca_dev->usb_lock);
1320 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001321}
1322
Jean-François Moine6a330912010-10-02 04:12:25 -03001323static int get_ctrl(struct gspca_dev *gspca_dev,
Hans de Goedea0001a22009-06-10 04:52:18 -03001324 int id)
1325{
1326 const struct ctrl *ctrls;
1327 int i;
1328
1329 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1330 i < gspca_dev->sd_desc->nctrls;
1331 i++, ctrls++) {
1332 if (gspca_dev->ctrl_dis & (1 << i))
1333 continue;
1334 if (id == ctrls->qctrl.id)
Jean-François Moine6a330912010-10-02 04:12:25 -03001335 return i;
Hans de Goedea0001a22009-06-10 04:52:18 -03001336 }
Jean-François Moine6a330912010-10-02 04:12:25 -03001337 return -1;
Hans de Goedea0001a22009-06-10 04:52:18 -03001338}
1339
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001340static int vidioc_queryctrl(struct file *file, void *priv,
1341 struct v4l2_queryctrl *q_ctrl)
1342{
1343 struct gspca_dev *gspca_dev = priv;
Hans de Goedea0001a22009-06-10 04:52:18 -03001344 const struct ctrl *ctrls;
Jean-François Moine6a330912010-10-02 04:12:25 -03001345 struct gspca_ctrl *gspca_ctrl;
1346 int i, idx;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001347 u32 id;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001348
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001349 id = q_ctrl->id;
1350 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1351 id &= V4L2_CTRL_ID_MASK;
1352 id++;
Jean-François Moine6a330912010-10-02 04:12:25 -03001353 idx = -1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001354 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001355 if (gspca_dev->ctrl_dis & (1 << i))
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001356 continue;
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001357 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001358 continue;
Jean-François Moine6a330912010-10-02 04:12:25 -03001359 if (idx >= 0
1360 && gspca_dev->sd_desc->ctrls[i].qctrl.id
Jean-François Moine780e3122010-10-19 04:29:10 -03001361 > gspca_dev->sd_desc->ctrls[idx].qctrl.id)
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001362 continue;
Jean-François Moine6a330912010-10-02 04:12:25 -03001363 idx = i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001364 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001365 } else {
Jean-François Moine6a330912010-10-02 04:12:25 -03001366 idx = get_ctrl(gspca_dev, id);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001367 }
Jean-François Moine6a330912010-10-02 04:12:25 -03001368 if (idx < 0)
1369 return -EINVAL;
1370 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1371 memcpy(q_ctrl, &ctrls->qctrl, sizeof *q_ctrl);
1372 if (gspca_dev->cam.ctrls != NULL) {
1373 gspca_ctrl = &gspca_dev->cam.ctrls[idx];
1374 q_ctrl->default_value = gspca_ctrl->def;
1375 q_ctrl->minimum = gspca_ctrl->min;
1376 q_ctrl->maximum = gspca_ctrl->max;
1377 }
1378 if (gspca_dev->ctrl_inac & (1 << idx))
Jean-Francois Moine4af85662009-11-12 15:59:27 -03001379 q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001380 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001381}
1382
1383static int vidioc_s_ctrl(struct file *file, void *priv,
1384 struct v4l2_control *ctrl)
1385{
1386 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001387 const struct ctrl *ctrls;
Jean-François Moine6a330912010-10-02 04:12:25 -03001388 struct gspca_ctrl *gspca_ctrl;
1389 int idx, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001390
Jean-François Moine6a330912010-10-02 04:12:25 -03001391 idx = get_ctrl(gspca_dev, ctrl->id);
1392 if (idx < 0)
Hans de Goedea0001a22009-06-10 04:52:18 -03001393 return -EINVAL;
Jean-François Moine6a330912010-10-02 04:12:25 -03001394 if (gspca_dev->ctrl_inac & (1 << idx))
1395 return -EINVAL;
1396 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1397 if (gspca_dev->cam.ctrls != NULL) {
1398 gspca_ctrl = &gspca_dev->cam.ctrls[idx];
1399 if (ctrl->value < gspca_ctrl->min
1400 || ctrl->value > gspca_ctrl->max)
1401 return -ERANGE;
1402 } else {
1403 gspca_ctrl = NULL;
1404 if (ctrl->value < ctrls->qctrl.minimum
1405 || ctrl->value > ctrls->qctrl.maximum)
1406 return -ERANGE;
1407 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001408 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1409 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1410 return -ERESTARTSYS;
Jean-François Moine6a330912010-10-02 04:12:25 -03001411 if (!gspca_dev->present) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001412 ret = -ENODEV;
Jean-François Moine6a330912010-10-02 04:12:25 -03001413 goto out;
1414 }
1415 gspca_dev->usb_err = 0;
1416 if (ctrls->set != NULL) {
1417 ret = ctrls->set(gspca_dev, ctrl->value);
1418 goto out;
1419 }
1420 if (gspca_ctrl != NULL) {
1421 gspca_ctrl->val = ctrl->value;
1422 if (ctrls->set_control != NULL
1423 && gspca_dev->streaming)
1424 ctrls->set_control(gspca_dev);
1425 }
1426 ret = gspca_dev->usb_err;
1427out:
Hans de Goedea0001a22009-06-10 04:52:18 -03001428 mutex_unlock(&gspca_dev->usb_lock);
1429 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001430}
1431
1432static int vidioc_g_ctrl(struct file *file, void *priv,
1433 struct v4l2_control *ctrl)
1434{
1435 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001436 const struct ctrl *ctrls;
Jean-François Moine6a330912010-10-02 04:12:25 -03001437 int idx, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001438
Jean-François Moine6a330912010-10-02 04:12:25 -03001439 idx = get_ctrl(gspca_dev, ctrl->id);
1440 if (idx < 0)
Hans de Goedea0001a22009-06-10 04:52:18 -03001441 return -EINVAL;
Jean-François Moine6a330912010-10-02 04:12:25 -03001442 ctrls = &gspca_dev->sd_desc->ctrls[idx];
Hans de Goedea0001a22009-06-10 04:52:18 -03001443
1444 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1445 return -ERESTARTSYS;
Jean-François Moine6a330912010-10-02 04:12:25 -03001446 if (!gspca_dev->present) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001447 ret = -ENODEV;
Jean-François Moine6a330912010-10-02 04:12:25 -03001448 goto out;
1449 }
1450 gspca_dev->usb_err = 0;
1451 if (ctrls->get != NULL) {
1452 ret = ctrls->get(gspca_dev, &ctrl->value);
1453 goto out;
1454 }
1455 if (gspca_dev->cam.ctrls != NULL)
1456 ctrl->value = gspca_dev->cam.ctrls[idx].val;
1457 ret = 0;
1458out:
Hans de Goedea0001a22009-06-10 04:52:18 -03001459 mutex_unlock(&gspca_dev->usb_lock);
1460 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001461}
1462
1463static int vidioc_querymenu(struct file *file, void *priv,
1464 struct v4l2_querymenu *qmenu)
1465{
1466 struct gspca_dev *gspca_dev = priv;
1467
1468 if (!gspca_dev->sd_desc->querymenu)
1469 return -EINVAL;
1470 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1471}
1472
1473static int vidioc_enum_input(struct file *file, void *priv,
1474 struct v4l2_input *input)
1475{
1476 struct gspca_dev *gspca_dev = priv;
1477
1478 if (input->index != 0)
1479 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001480 input->type = V4L2_INPUT_TYPE_CAMERA;
Adam Bakerdfa76fa2009-03-29 19:17:10 -03001481 input->status = gspca_dev->cam.input_flags;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001482 strncpy(input->name, gspca_dev->sd_desc->name,
1483 sizeof input->name);
1484 return 0;
1485}
1486
1487static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1488{
1489 *i = 0;
1490 return 0;
1491}
1492
1493static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1494{
1495 if (i > 0)
1496 return -EINVAL;
1497 return (0);
1498}
1499
1500static int vidioc_reqbufs(struct file *file, void *priv,
1501 struct v4l2_requestbuffers *rb)
1502{
1503 struct gspca_dev *gspca_dev = priv;
Jean-François Moine832d0a92010-04-09 06:07:58 -03001504 int i, ret = 0, streaming;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001505
Jean-François Moinea2ba6f22010-07-14 06:21:16 -03001506 i = rb->memory; /* (avoid compilation warning) */
1507 switch (i) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001508 case GSPCA_MEMORY_READ: /* (internal call) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001509 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001510 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001511 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001512 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001513 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001514 }
Hans de Goedee2997a72008-04-23 08:09:12 -03001515 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1516 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001517
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001518 if (gspca_dev->memory != GSPCA_MEMORY_NO
1519 && gspca_dev->memory != rb->memory) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001520 ret = -EBUSY;
1521 goto out;
1522 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001523
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001524 /* only one file may do the capture */
1525 if (gspca_dev->capt_file != NULL
1526 && gspca_dev->capt_file != file) {
1527 ret = -EBUSY;
1528 goto out;
1529 }
1530
1531 /* if allocated, the buffers must not be mapped */
1532 for (i = 0; i < gspca_dev->nframes; i++) {
1533 if (gspca_dev->frame[i].vma_use_count) {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001534 ret = -EBUSY;
1535 goto out;
1536 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001537 }
1538
1539 /* stop streaming */
Jean-François Moine832d0a92010-04-09 06:07:58 -03001540 streaming = gspca_dev->streaming;
1541 if (streaming) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001542 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001543 gspca_dev->usb_err = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001544 gspca_stream_off(gspca_dev);
1545 mutex_unlock(&gspca_dev->usb_lock);
1546 }
1547
1548 /* free the previous allocated buffers, if any */
1549 if (gspca_dev->nframes != 0) {
1550 frame_free(gspca_dev);
1551 gspca_dev->capt_file = NULL;
1552 }
1553 if (rb->count == 0) /* unrequest */
1554 goto out;
1555 gspca_dev->memory = rb->memory;
1556 ret = frame_alloc(gspca_dev, rb->count);
1557 if (ret == 0) {
1558 rb->count = gspca_dev->nframes;
1559 gspca_dev->capt_file = file;
Jean-François Moine832d0a92010-04-09 06:07:58 -03001560 if (streaming)
1561 ret = gspca_init_transfer(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001562 }
1563out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001564 mutex_unlock(&gspca_dev->queue_lock);
1565 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1566 return ret;
1567}
1568
1569static int vidioc_querybuf(struct file *file, void *priv,
1570 struct v4l2_buffer *v4l2_buf)
1571{
1572 struct gspca_dev *gspca_dev = priv;
1573 struct gspca_frame *frame;
1574
Jean-Francois Moine13752bd2008-12-19 15:08:51 -03001575 if (v4l2_buf->index < 0
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001576 || v4l2_buf->index >= gspca_dev->nframes)
1577 return -EINVAL;
1578
1579 frame = &gspca_dev->frame[v4l2_buf->index];
1580 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1581 return 0;
1582}
1583
1584static int vidioc_streamon(struct file *file, void *priv,
1585 enum v4l2_buf_type buf_type)
1586{
1587 struct gspca_dev *gspca_dev = priv;
1588 int ret;
1589
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001590 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1591 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001592 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1593 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001594
Jean-François Moine18bf99a2010-04-09 06:06:11 -03001595 /* check the capture file */
1596 if (gspca_dev->capt_file != file) {
1597 ret = -EBUSY;
1598 goto out;
1599 }
1600
Jean-Francois Moine1edabe72008-12-19 15:17:25 -03001601 if (gspca_dev->nframes == 0
1602 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001603 ret = -EINVAL;
1604 goto out;
1605 }
1606 if (!gspca_dev->streaming) {
1607 ret = gspca_init_transfer(gspca_dev);
1608 if (ret < 0)
1609 goto out;
1610 }
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001611#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001612 if (gspca_debug & D_STREAM) {
1613 PDEBUG_MODE("stream on OK",
1614 gspca_dev->pixfmt,
1615 gspca_dev->width,
1616 gspca_dev->height);
1617 }
1618#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001619 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001620out:
1621 mutex_unlock(&gspca_dev->queue_lock);
1622 return ret;
1623}
1624
1625static int vidioc_streamoff(struct file *file, void *priv,
1626 enum v4l2_buf_type buf_type)
1627{
1628 struct gspca_dev *gspca_dev = priv;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001629 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001630
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001631 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1632 return -EINVAL;
Hans de Goede4e770f72010-12-30 13:11:21 -03001633
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001634 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1635 return -ERESTARTSYS;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001636
Hans de Goede4e770f72010-12-30 13:11:21 -03001637 if (!gspca_dev->streaming) {
1638 ret = 0;
1639 goto out;
1640 }
1641
Jean-François Moine18bf99a2010-04-09 06:06:11 -03001642 /* check the capture file */
1643 if (gspca_dev->capt_file != file) {
1644 ret = -EBUSY;
1645 goto out;
1646 }
1647
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001648 /* stop streaming */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001649 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1650 ret = -ERESTARTSYS;
1651 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001652 }
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001653 gspca_dev->usb_err = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001654 gspca_stream_off(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001655 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001656
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001657 /* empty the transfer queues */
1658 atomic_set(&gspca_dev->fr_q, 0);
1659 atomic_set(&gspca_dev->fr_i, 0);
1660 gspca_dev->fr_o = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001661 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001662out:
1663 mutex_unlock(&gspca_dev->queue_lock);
1664 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001665}
1666
1667static int vidioc_g_jpegcomp(struct file *file, void *priv,
1668 struct v4l2_jpegcompression *jpegcomp)
1669{
1670 struct gspca_dev *gspca_dev = priv;
1671 int ret;
1672
1673 if (!gspca_dev->sd_desc->get_jcomp)
1674 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001675 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1676 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001677 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001678 if (gspca_dev->present)
1679 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1680 else
1681 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001682 mutex_unlock(&gspca_dev->usb_lock);
1683 return ret;
1684}
1685
1686static int vidioc_s_jpegcomp(struct file *file, void *priv,
1687 struct v4l2_jpegcompression *jpegcomp)
1688{
1689 struct gspca_dev *gspca_dev = priv;
1690 int ret;
1691
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001692 if (!gspca_dev->sd_desc->set_jcomp)
1693 return -EINVAL;
Jim Parisf86d4a92008-12-10 05:50:44 -03001694 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1695 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001696 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001697 if (gspca_dev->present)
1698 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1699 else
1700 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001701 mutex_unlock(&gspca_dev->usb_lock);
1702 return ret;
1703}
1704
1705static int vidioc_g_parm(struct file *filp, void *priv,
1706 struct v4l2_streamparm *parm)
1707{
1708 struct gspca_dev *gspca_dev = priv;
1709
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001710 parm->parm.capture.readbuffers = gspca_dev->nbufread;
Jim Paris627a5ef2008-12-10 06:02:42 -03001711
1712 if (gspca_dev->sd_desc->get_streamparm) {
1713 int ret;
1714
1715 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1716 return -ERESTARTSYS;
Jean-François Moine668f44a2010-12-25 13:46:14 -03001717 if (gspca_dev->present) {
1718 gspca_dev->usb_err = 0;
1719 gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
1720 ret = gspca_dev->usb_err;
1721 } else {
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001722 ret = -ENODEV;
Jean-François Moine668f44a2010-12-25 13:46:14 -03001723 }
Jim Paris627a5ef2008-12-10 06:02:42 -03001724 mutex_unlock(&gspca_dev->usb_lock);
1725 return ret;
1726 }
1727
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001728 return 0;
1729}
1730
1731static int vidioc_s_parm(struct file *filp, void *priv,
1732 struct v4l2_streamparm *parm)
1733{
1734 struct gspca_dev *gspca_dev = priv;
1735 int n;
1736
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001737 n = parm->parm.capture.readbuffers;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001738 if (n == 0 || n >= GSPCA_MAX_FRAMES)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001739 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1740 else
1741 gspca_dev->nbufread = n;
Jim Paris627a5ef2008-12-10 06:02:42 -03001742
1743 if (gspca_dev->sd_desc->set_streamparm) {
1744 int ret;
1745
1746 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1747 return -ERESTARTSYS;
Jean-François Moine668f44a2010-12-25 13:46:14 -03001748 if (gspca_dev->present) {
1749 gspca_dev->usb_err = 0;
1750 gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
1751 ret = gspca_dev->usb_err;
1752 } else {
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001753 ret = -ENODEV;
Jean-François Moine668f44a2010-12-25 13:46:14 -03001754 }
Jim Paris627a5ef2008-12-10 06:02:42 -03001755 mutex_unlock(&gspca_dev->usb_lock);
1756 return ret;
1757 }
1758
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001759 return 0;
1760}
1761
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001762static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1763{
1764 struct gspca_dev *gspca_dev = file->private_data;
Harvey Harrisonefab8212008-07-05 06:12:47 -03001765 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001766 struct page *page;
1767 unsigned long addr, start, size;
1768 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001769
1770 start = vma->vm_start;
1771 size = vma->vm_end - vma->vm_start;
1772 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1773
Hans de Goedee2997a72008-04-23 08:09:12 -03001774 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1775 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001776 if (!gspca_dev->present) {
1777 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001778 goto out;
1779 }
1780 if (gspca_dev->capt_file != file) {
1781 ret = -EINVAL;
1782 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001783 }
1784
Harvey Harrisonefab8212008-07-05 06:12:47 -03001785 frame = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001786 for (i = 0; i < gspca_dev->nframes; ++i) {
1787 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1788 PDEBUG(D_STREAM, "mmap bad memory type");
1789 break;
1790 }
1791 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1792 == vma->vm_pgoff) {
1793 frame = &gspca_dev->frame[i];
1794 break;
1795 }
1796 }
Harvey Harrisonefab8212008-07-05 06:12:47 -03001797 if (frame == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001798 PDEBUG(D_STREAM, "mmap no frame buffer found");
1799 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001800 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001801 }
Jean-François Moine38bff692010-06-24 05:19:58 -03001802 if (size != frame->v4l2_buf.length) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001803 PDEBUG(D_STREAM, "mmap bad size");
1804 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001805 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001806 }
1807
1808 /*
1809 * - VM_IO marks the area as being a mmaped region for I/O to a
1810 * device. It also prevents the region from being core dumped.
1811 */
1812 vma->vm_flags |= VM_IO;
1813
1814 addr = (unsigned long) frame->data;
1815 while (size > 0) {
1816 page = vmalloc_to_page((void *) addr);
1817 ret = vm_insert_page(vma, start, page);
1818 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001819 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001820 start += PAGE_SIZE;
1821 addr += PAGE_SIZE;
1822 size -= PAGE_SIZE;
1823 }
1824
Jean-Francois Moined50e2572009-11-26 15:36:40 -03001825 vma->vm_ops = &gspca_vm_ops;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001826 vma->vm_private_data = frame;
1827 gspca_vm_open(vma);
Hans de Goedee2997a72008-04-23 08:09:12 -03001828 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001829out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001830 mutex_unlock(&gspca_dev->queue_lock);
1831 return ret;
1832}
1833
Hans de Goede27074ef2010-12-30 19:54:33 -03001834static int frame_ready_nolock(struct gspca_dev *gspca_dev, struct file *file,
1835 enum v4l2_memory memory)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001836{
Hans de Goede27074ef2010-12-30 19:54:33 -03001837 if (!gspca_dev->present)
1838 return -ENODEV;
1839 if (gspca_dev->capt_file != file || gspca_dev->memory != memory ||
1840 !gspca_dev->streaming)
1841 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001842
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001843 /* check if a frame is ready */
Hans de Goede27074ef2010-12-30 19:54:33 -03001844 return gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i);
1845}
Hans de Goede181b7042008-11-19 17:16:26 -03001846
Hans de Goede27074ef2010-12-30 19:54:33 -03001847static int frame_ready(struct gspca_dev *gspca_dev, struct file *file,
1848 enum v4l2_memory memory)
1849{
1850 int ret;
Hans de Goede181b7042008-11-19 17:16:26 -03001851
Hans de Goede27074ef2010-12-30 19:54:33 -03001852 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1853 return -ERESTARTSYS;
1854 ret = frame_ready_nolock(gspca_dev, file, memory);
1855 mutex_unlock(&gspca_dev->queue_lock);
1856 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001857}
1858
1859/*
1860 * dequeue a video buffer
1861 *
1862 * If nonblock_ing is false, block until a buffer is available.
1863 */
1864static int vidioc_dqbuf(struct file *file, void *priv,
1865 struct v4l2_buffer *v4l2_buf)
1866{
1867 struct gspca_dev *gspca_dev = priv;
1868 struct gspca_frame *frame;
Hans de Goede27074ef2010-12-30 19:54:33 -03001869 int i, j, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001870
1871 PDEBUG(D_FRAM, "dqbuf");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001872
Hans de Goede27074ef2010-12-30 19:54:33 -03001873 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001874 return -ERESTARTSYS;
1875
Hans de Goede27074ef2010-12-30 19:54:33 -03001876 for (;;) {
1877 ret = frame_ready_nolock(gspca_dev, file, v4l2_buf->memory);
1878 if (ret < 0)
1879 goto out;
1880 if (ret > 0)
1881 break;
1882
1883 mutex_unlock(&gspca_dev->queue_lock);
1884
1885 if (file->f_flags & O_NONBLOCK)
1886 return -EAGAIN;
1887
1888 /* wait till a frame is ready */
1889 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1890 frame_ready(gspca_dev, file, v4l2_buf->memory),
1891 msecs_to_jiffies(3000));
1892 if (ret < 0)
1893 return ret;
1894 if (ret == 0)
1895 return -EIO;
1896
1897 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1898 return -ERESTARTSYS;
1899 }
1900
1901 i = gspca_dev->fr_o;
1902 j = gspca_dev->fr_queue[i];
1903 frame = &gspca_dev->frame[j];
1904
1905 gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES;
1906
1907 if (gspca_dev->sd_desc->dq_callback) {
1908 mutex_lock(&gspca_dev->usb_lock);
1909 gspca_dev->usb_err = 0;
1910 if (gspca_dev->present)
1911 gspca_dev->sd_desc->dq_callback(gspca_dev);
1912 mutex_unlock(&gspca_dev->usb_lock);
1913 }
1914
Hans de Goede4a82bc62010-12-30 20:00:17 -03001915 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1916 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1917 PDEBUG(D_FRAM, "dqbuf %d", j);
1918 ret = 0;
1919
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001920 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001921 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001922 frame->data,
1923 frame->v4l2_buf.bytesused)) {
1924 PDEBUG(D_ERR|D_STREAM,
1925 "dqbuf cp to user failed");
1926 ret = -EFAULT;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001927 }
1928 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001929out:
Hans de Goede27074ef2010-12-30 19:54:33 -03001930 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001931 return ret;
1932}
1933
1934/*
1935 * queue a video buffer
1936 *
1937 * Attempting to queue a buffer that has already been
1938 * queued will return -EINVAL.
1939 */
1940static int vidioc_qbuf(struct file *file, void *priv,
1941 struct v4l2_buffer *v4l2_buf)
1942{
1943 struct gspca_dev *gspca_dev = priv;
1944 struct gspca_frame *frame;
1945 int i, index, ret;
1946
1947 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001948
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001949 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1950 return -ERESTARTSYS;
1951
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001952 index = v4l2_buf->index;
1953 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001954 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001955 "qbuf idx %d >= %d", index, gspca_dev->nframes);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001956 ret = -EINVAL;
1957 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001958 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001959 if (v4l2_buf->memory != gspca_dev->memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001960 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001961 ret = -EINVAL;
1962 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001963 }
1964
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001965 frame = &gspca_dev->frame[index];
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001966 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1967 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001968 ret = -EINVAL;
1969 goto out;
1970 }
1971
1972 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001973
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001974 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001975 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1976 frame->v4l2_buf.length = v4l2_buf->length;
1977 }
1978
1979 /* put the buffer in the 'queued' queue */
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001980 i = atomic_read(&gspca_dev->fr_q);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001981 gspca_dev->fr_queue[i] = index;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001982 atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001983
1984 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1985 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1986 ret = 0;
1987out:
1988 mutex_unlock(&gspca_dev->queue_lock);
1989 return ret;
1990}
1991
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001992/*
1993 * allocate the resources for read()
1994 */
1995static int read_alloc(struct gspca_dev *gspca_dev,
1996 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001997{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001998 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001999 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002000
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002001 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002002 if (gspca_dev->nframes == 0) {
2003 struct v4l2_requestbuffers rb;
2004
2005 memset(&rb, 0, sizeof rb);
2006 rb.count = gspca_dev->nbufread;
2007 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002008 rb.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002009 ret = vidioc_reqbufs(file, gspca_dev, &rb);
2010 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002011 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002012 return ret;
2013 }
2014 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2015 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002016 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002017 for (i = 0; i < gspca_dev->nbufread; i++) {
2018 v4l2_buf.index = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002019 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2020 if (ret != 0) {
2021 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
2022 return ret;
2023 }
2024 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002025 gspca_dev->memory = GSPCA_MEMORY_READ;
2026 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002027
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002028 /* start streaming */
2029 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2030 if (ret != 0)
2031 PDEBUG(D_STREAM, "read streamon err %d", ret);
2032 return ret;
2033}
2034
2035static unsigned int dev_poll(struct file *file, poll_table *wait)
2036{
2037 struct gspca_dev *gspca_dev = file->private_data;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03002038 int ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002039
2040 PDEBUG(D_FRAM, "poll");
2041
2042 poll_wait(file, &gspca_dev->wq, wait);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002043
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002044 /* if reqbufs is not done, the user would use read() */
2045 if (gspca_dev->nframes == 0) {
2046 if (gspca_dev->memory != GSPCA_MEMORY_NO)
2047 return POLLERR; /* not the 1st time */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002048 ret = read_alloc(gspca_dev, file);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002049 if (ret != 0)
2050 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002051 }
2052
2053 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
2054 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002055
Jean-François Moinef7059ea2010-07-06 04:32:27 -03002056 /* check if an image has been received */
2057 if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i))
2058 ret = POLLIN | POLLRDNORM; /* yes */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002059 else
2060 ret = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002061 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002062 if (!gspca_dev->present)
2063 return POLLHUP;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002064 return ret;
2065}
2066
2067static ssize_t dev_read(struct file *file, char __user *data,
2068 size_t count, loff_t *ppos)
2069{
2070 struct gspca_dev *gspca_dev = file->private_data;
2071 struct gspca_frame *frame;
2072 struct v4l2_buffer v4l2_buf;
2073 struct timeval timestamp;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002074 int n, ret, ret2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002075
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03002076 PDEBUG(D_FRAM, "read (%zd)", count);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002077 if (!gspca_dev->present)
2078 return -ENODEV;
2079 switch (gspca_dev->memory) {
2080 case GSPCA_MEMORY_NO: /* first time */
2081 ret = read_alloc(gspca_dev, file);
2082 if (ret != 0)
2083 return ret;
2084 break;
2085 case GSPCA_MEMORY_READ:
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002086 if (gspca_dev->capt_file == file)
2087 break;
2088 /* fall thru */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002089 default:
2090 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002091 }
2092
2093 /* get a frame */
Paulo Assis2f51d002010-03-07 06:56:56 -03002094 timestamp = ktime_to_timeval(ktime_get());
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002095 timestamp.tv_sec--;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002096 n = 2;
2097 for (;;) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002098 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2099 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002100 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002101 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
2102 if (ret != 0) {
2103 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
2104 return ret;
2105 }
2106
2107 /* if the process slept for more than 1 second,
Frank Zago219423c2008-09-26 07:43:54 -03002108 * get a newer frame */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002109 frame = &gspca_dev->frame[v4l2_buf.index];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002110 if (--n < 0)
2111 break; /* avoid infinite loop */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002112 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
2113 break;
2114 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2115 if (ret != 0) {
2116 PDEBUG(D_STREAM, "read qbuf err %d", ret);
2117 return ret;
2118 }
2119 }
2120
2121 /* copy the frame */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002122 if (count > frame->v4l2_buf.bytesused)
2123 count = frame->v4l2_buf.bytesused;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002124 ret = copy_to_user(data, frame->data, count);
2125 if (ret != 0) {
2126 PDEBUG(D_ERR|D_STREAM,
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03002127 "read cp to user lack %d / %zd", ret, count);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002128 ret = -EFAULT;
2129 goto out;
2130 }
2131 ret = count;
2132out:
2133 /* in each case, requeue the buffer */
2134 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2135 if (ret2 != 0)
2136 return ret2;
2137 return ret;
2138}
2139
Hans Verkuilbec43662008-12-30 06:58:20 -03002140static struct v4l2_file_operations dev_fops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002141 .owner = THIS_MODULE,
2142 .open = dev_open,
2143 .release = dev_close,
2144 .read = dev_read,
2145 .mmap = dev_mmap,
Hans Verkuilbec43662008-12-30 06:58:20 -03002146 .unlocked_ioctl = video_ioctl2,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002147 .poll = dev_poll,
2148};
2149
Hans Verkuila3998102008-07-21 02:57:38 -03002150static const struct v4l2_ioctl_ops dev_ioctl_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002151 .vidioc_querycap = vidioc_querycap,
2152 .vidioc_dqbuf = vidioc_dqbuf,
2153 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03002154 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2155 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2156 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2157 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002158 .vidioc_streamon = vidioc_streamon,
2159 .vidioc_queryctrl = vidioc_queryctrl,
2160 .vidioc_g_ctrl = vidioc_g_ctrl,
2161 .vidioc_s_ctrl = vidioc_s_ctrl,
2162 .vidioc_querymenu = vidioc_querymenu,
2163 .vidioc_enum_input = vidioc_enum_input,
2164 .vidioc_g_input = vidioc_g_input,
2165 .vidioc_s_input = vidioc_s_input,
2166 .vidioc_reqbufs = vidioc_reqbufs,
2167 .vidioc_querybuf = vidioc_querybuf,
2168 .vidioc_streamoff = vidioc_streamoff,
2169 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
2170 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
2171 .vidioc_g_parm = vidioc_g_parm,
2172 .vidioc_s_parm = vidioc_s_parm,
Hans de Goede11c635a2009-05-25 15:04:22 -03002173 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Antonio Ospite28ffe772009-12-02 06:18:46 -03002174 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03002175#ifdef CONFIG_VIDEO_ADV_DEBUG
2176 .vidioc_g_register = vidioc_g_register,
2177 .vidioc_s_register = vidioc_s_register,
2178#endif
2179 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002180};
2181
Hans Verkuila3998102008-07-21 02:57:38 -03002182static struct video_device gspca_template = {
2183 .name = "gspca main driver",
Hans Verkuila3998102008-07-21 02:57:38 -03002184 .fops = &dev_fops,
2185 .ioctl_ops = &dev_ioctl_ops,
Hans de Goeded0182302008-11-19 17:10:47 -03002186 .release = gspca_release,
Hans Verkuila3998102008-07-21 02:57:38 -03002187};
2188
Jean-François Moine6a330912010-10-02 04:12:25 -03002189/* initialize the controls */
2190static void ctrls_init(struct gspca_dev *gspca_dev)
2191{
2192 struct gspca_ctrl *ctrl;
2193 int i;
2194
2195 for (i = 0, ctrl = gspca_dev->cam.ctrls;
2196 i < gspca_dev->sd_desc->nctrls;
2197 i++, ctrl++) {
2198 ctrl->def = gspca_dev->sd_desc->ctrls[i].qctrl.default_value;
2199 ctrl->val = ctrl->def;
2200 ctrl->min = gspca_dev->sd_desc->ctrls[i].qctrl.minimum;
2201 ctrl->max = gspca_dev->sd_desc->ctrls[i].qctrl.maximum;
2202 }
2203}
2204
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002205/*
2206 * probe and create a new gspca device
2207 *
2208 * This function must be called by the sub-driver when it is
2209 * called for probing a new device.
2210 */
Jean-François Moine44628642010-06-05 07:47:36 -03002211int gspca_dev_probe2(struct usb_interface *intf,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002212 const struct usb_device_id *id,
2213 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002214 int dev_size,
2215 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002216{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002217 struct gspca_dev *gspca_dev;
2218 struct usb_device *dev = interface_to_usbdev(intf);
2219 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002220
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002221 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002222
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002223 /* create the device */
2224 if (dev_size < sizeof *gspca_dev)
2225 dev_size = sizeof *gspca_dev;
2226 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
Erik Andrene293e592008-10-03 08:46:50 -03002227 if (!gspca_dev) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002228 err("couldn't kzalloc gspca struct");
Erik Andrene293e592008-10-03 08:46:50 -03002229 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002230 }
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002231 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2232 if (!gspca_dev->usb_buf) {
2233 err("out of memory");
Erik Andrene293e592008-10-03 08:46:50 -03002234 ret = -ENOMEM;
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002235 goto out;
2236 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002237 gspca_dev->dev = dev;
Jean-François Moine44628642010-06-05 07:47:36 -03002238 gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002239 gspca_dev->nbalt = intf->num_altsetting;
Jean-François Moine35680ba2010-07-14 06:30:18 -03002240
2241 /* check if any audio device */
2242 if (dev->config->desc.bNumInterfaces != 1) {
2243 int i;
2244 struct usb_interface *intf2;
2245
2246 for (i = 0; i < dev->config->desc.bNumInterfaces; i++) {
2247 intf2 = dev->config->interface[i];
2248 if (intf2 != NULL
2249 && intf2->altsetting != NULL
2250 && intf2->altsetting->desc.bInterfaceClass ==
2251 USB_CLASS_AUDIO) {
2252 gspca_dev->audio = 1;
2253 break;
2254 }
2255 }
2256 }
2257
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002258 gspca_dev->sd_desc = sd_desc;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002259 gspca_dev->nbufread = 2;
Jean-Francois Moineff374742008-10-23 07:29:51 -03002260 gspca_dev->empty_packet = -1; /* don't check the empty packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002261
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002262 /* configure the subdriver and initialize the USB device */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002263 ret = sd_desc->config(gspca_dev, id);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002264 if (ret < 0)
2265 goto out;
Jean-François Moine6a330912010-10-02 04:12:25 -03002266 if (gspca_dev->cam.ctrls != NULL)
2267 ctrls_init(gspca_dev);
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002268 ret = sd_desc->init(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002269 if (ret < 0)
2270 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002271 gspca_set_default_mode(gspca_dev);
2272
Hans de Goede10ee2402010-02-19 04:41:40 -03002273 ret = gspca_input_connect(gspca_dev);
2274 if (ret)
2275 goto out;
2276
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002277 mutex_init(&gspca_dev->usb_lock);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002278 mutex_init(&gspca_dev->queue_lock);
2279 init_waitqueue_head(&gspca_dev->wq);
2280
2281 /* init video stuff */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002282 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
Hans de Goede24441632009-03-27 16:08:20 -03002283 gspca_dev->vdev.parent = &intf->dev;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03002284 gspca_dev->module = module;
Oliver Neukumee56a4d2008-07-27 14:01:59 -03002285 gspca_dev->present = 1;
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002286 ret = video_register_device(&gspca_dev->vdev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002287 VFL_TYPE_GRABBER,
Jean-Francois Moine8d584a52009-01-13 06:07:59 -03002288 -1);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002289 if (ret < 0) {
2290 err("video_register_device err %d", ret);
2291 goto out;
2292 }
2293
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002294 usb_set_intfdata(intf, gspca_dev);
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002295 PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
Márton Németh0274d422010-01-28 06:39:49 -03002296
Hans de Goede10ee2402010-02-19 04:41:40 -03002297 gspca_input_create_urb(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002298
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002299 return 0;
2300out:
Randy Dunlapf7628012010-03-30 14:32:04 -03002301#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Hans de Goede10ee2402010-02-19 04:41:40 -03002302 if (gspca_dev->input_dev)
2303 input_unregister_device(gspca_dev->input_dev);
Hans de Goedee7637522010-02-19 05:11:09 -03002304#endif
Jean-Francois Moinee07a1d82008-11-19 06:37:53 -03002305 kfree(gspca_dev->usb_buf);
2306 kfree(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002307 return ret;
2308}
Jean-François Moine44628642010-06-05 07:47:36 -03002309EXPORT_SYMBOL(gspca_dev_probe2);
2310
2311/* same function as the previous one, but check the interface */
2312int gspca_dev_probe(struct usb_interface *intf,
2313 const struct usb_device_id *id,
2314 const struct sd_desc *sd_desc,
2315 int dev_size,
2316 struct module *module)
2317{
2318 struct usb_device *dev = interface_to_usbdev(intf);
2319
2320 /* we don't handle multi-config cameras */
2321 if (dev->descriptor.bNumConfigurations != 1) {
Jean-François Moine0b656322010-09-13 05:19:58 -03002322 err("%04x:%04x too many config",
Jean-François Moine44628642010-06-05 07:47:36 -03002323 id->idVendor, id->idProduct);
2324 return -ENODEV;
2325 }
2326
2327 /* the USB video interface must be the first one */
2328 if (dev->config->desc.bNumInterfaces != 1
Jean-François Moinec4dc6922010-07-14 06:26:54 -03002329 && intf->cur_altsetting->desc.bInterfaceNumber != 0)
Jean-François Moine44628642010-06-05 07:47:36 -03002330 return -ENODEV;
Jean-François Moine44628642010-06-05 07:47:36 -03002331
2332 return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
2333}
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002334EXPORT_SYMBOL(gspca_dev_probe);
2335
2336/*
2337 * USB disconnection
2338 *
2339 * This function must be called by the sub-driver
2340 * when the device disconnects, after the specific resources are freed.
2341 */
2342void gspca_disconnect(struct usb_interface *intf)
2343{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002344 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Randy Dunlapf7628012010-03-30 14:32:04 -03002345#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -03002346 struct input_dev *input_dev;
Jean-François Moinee070bf92010-02-18 15:02:51 -03002347#endif
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002348
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002349 PDEBUG(D_PROBE, "%s disconnect",
2350 video_device_node_name(&gspca_dev->vdev));
Adam Bakerad281272009-02-04 15:33:21 -03002351 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002352
Hans de Goede4e770f72010-12-30 13:11:21 -03002353 gspca_dev->present = 0;
2354 wake_up_interruptible(&gspca_dev->wq);
2355
2356 destroy_urbs(gspca_dev);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002357
Randy Dunlapf7628012010-03-30 14:32:04 -03002358#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -03002359 gspca_input_destroy_urb(gspca_dev);
2360 input_dev = gspca_dev->input_dev;
2361 if (input_dev) {
2362 gspca_dev->input_dev = NULL;
2363 input_unregister_device(input_dev);
2364 }
Jean-François Moinee070bf92010-02-18 15:02:51 -03002365#endif
Márton Németh0274d422010-01-28 06:39:49 -03002366
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002367 /* the device is freed at exit of this function */
2368 gspca_dev->dev = NULL;
Adam Bakerad281272009-02-04 15:33:21 -03002369 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03002370
Frank Zago6b060ff2008-09-28 08:12:22 -03002371 usb_set_intfdata(intf, NULL);
2372
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002373 /* release the device */
2374 /* (this will call gspca_release() immediatly or on last close) */
2375 video_unregister_device(&gspca_dev->vdev);
Jean-Francois Moine3145b8c2008-10-05 04:22:10 -03002376
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002377/* PDEBUG(D_PROBE, "disconnect complete"); */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002378}
2379EXPORT_SYMBOL(gspca_disconnect);
2380
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002381#ifdef CONFIG_PM
2382int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2383{
2384 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2385
2386 if (!gspca_dev->streaming)
2387 return 0;
2388 gspca_dev->frozen = 1; /* avoid urb error messages */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002389 if (gspca_dev->sd_desc->stopN)
2390 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002391 destroy_urbs(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002392 gspca_input_destroy_urb(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002393 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002394 if (gspca_dev->sd_desc->stop0)
2395 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002396 return 0;
2397}
2398EXPORT_SYMBOL(gspca_suspend);
2399
2400int gspca_resume(struct usb_interface *intf)
2401{
2402 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2403
2404 gspca_dev->frozen = 0;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002405 gspca_dev->sd_desc->init(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002406 gspca_input_create_urb(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002407 if (gspca_dev->streaming)
2408 return gspca_init_transfer(gspca_dev);
Jean-Francois Moine1e89e2da2008-09-03 17:12:15 -03002409 return 0;
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002410}
2411EXPORT_SYMBOL(gspca_resume);
2412#endif
Hans de Goededcef32372008-07-10 10:40:53 -03002413/* -- cam driver utility functions -- */
2414
2415/* auto gain and exposure algorithm based on the knee algorithm described here:
2416 http://ytse.tricolour.net/docs/LowLightOptimization.html
2417
2418 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2419 where changed. */
2420int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2421 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2422{
2423 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2424 const struct ctrl *gain_ctrl = NULL;
2425 const struct ctrl *exposure_ctrl = NULL;
2426 const struct ctrl *autogain_ctrl = NULL;
2427 int retval = 0;
2428
2429 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goede26984b02010-02-01 13:18:37 -03002430 if (gspca_dev->ctrl_dis & (1 << i))
2431 continue;
Hans de Goededcef32372008-07-10 10:40:53 -03002432 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2433 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2434 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2435 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2436 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2437 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2438 }
2439 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2440 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2441 "on cam without (auto)gain/exposure");
2442 return 0;
2443 }
2444
2445 if (gain_ctrl->get(gspca_dev, &gain) ||
2446 exposure_ctrl->get(gspca_dev, &exposure) ||
2447 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2448 return 0;
2449
2450 orig_gain = gain;
2451 orig_exposure = exposure;
2452
2453 /* If we are of a multiple of deadzone, do multiple steps to reach the
2454 desired lumination fast (with the risc of a slight overshoot) */
2455 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2456
Jean-Francois Moine1c44d812008-11-10 05:56:55 -03002457 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
Hans de Goededcef32372008-07-10 10:40:53 -03002458 avg_lum, desired_avg_lum, steps);
2459
2460 for (i = 0; i < steps; i++) {
2461 if (avg_lum > desired_avg_lum) {
2462 if (gain > gain_knee)
2463 gain--;
2464 else if (exposure > exposure_knee)
2465 exposure--;
2466 else if (gain > gain_ctrl->qctrl.default_value)
2467 gain--;
2468 else if (exposure > exposure_ctrl->qctrl.minimum)
2469 exposure--;
2470 else if (gain > gain_ctrl->qctrl.minimum)
2471 gain--;
2472 else
2473 break;
2474 } else {
2475 if (gain < gain_ctrl->qctrl.default_value)
2476 gain++;
2477 else if (exposure < exposure_knee)
2478 exposure++;
2479 else if (gain < gain_knee)
2480 gain++;
2481 else if (exposure < exposure_ctrl->qctrl.maximum)
2482 exposure++;
2483 else if (gain < gain_ctrl->qctrl.maximum)
2484 gain++;
2485 else
2486 break;
2487 }
2488 }
2489
2490 if (gain != orig_gain) {
2491 gain_ctrl->set(gspca_dev, gain);
2492 retval = 1;
2493 }
2494 if (exposure != orig_exposure) {
2495 exposure_ctrl->set(gspca_dev, exposure);
2496 retval = 1;
2497 }
2498
2499 return retval;
2500}
2501EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2502
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002503/* -- module insert / remove -- */
2504static int __init gspca_init(void)
2505{
Jean-François Moine54826432010-09-13 04:53:03 -03002506 info("v%d.%d.%d registered",
Jean-Francois Moinebb64e862008-07-19 06:49:28 -03002507 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2508 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2509 DRIVER_VERSION_NUMBER & 0xff);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002510 return 0;
2511}
2512static void __exit gspca_exit(void)
2513{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002514}
2515
2516module_init(gspca_init);
2517module_exit(gspca_exit);
2518
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03002519#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002520module_param_named(debug, gspca_debug, int, 0644);
2521MODULE_PARM_DESC(debug,
2522 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002523 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
2524 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002525#endif