blob: 244fb76eaead205a567513ad86525b81adb9246d [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
Hans de Goeded642de22010-12-31 04:51:36 -0300511static int frame_alloc(struct gspca_dev *gspca_dev, struct file *file,
512 enum v4l2_memory memory, unsigned int count)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300513{
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-François Moinef7059ea2010-07-06 04:32:27 -0300522 if (count >= GSPCA_MAX_FRAMES)
523 count = GSPCA_MAX_FRAMES - 1;
Jean-François Moinee795d912010-06-24 05:14:22 -0300524 gspca_dev->frbuf = vmalloc_32(frsz * count);
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300525 if (!gspca_dev->frbuf) {
526 err("frame alloc failed");
527 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300528 }
Hans de Goeded642de22010-12-31 04:51:36 -0300529 gspca_dev->capt_file = file;
530 gspca_dev->memory = memory;
531 gspca_dev->frsz = frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300532 gspca_dev->nframes = count;
533 for (i = 0; i < count; i++) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300534 frame = &gspca_dev->frame[i];
535 frame->v4l2_buf.index = i;
536 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
537 frame->v4l2_buf.flags = 0;
538 frame->v4l2_buf.field = V4L2_FIELD_NONE;
539 frame->v4l2_buf.length = frsz;
Hans de Goeded642de22010-12-31 04:51:36 -0300540 frame->v4l2_buf.memory = memory;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300541 frame->v4l2_buf.sequence = 0;
Jean-François Moineb192ca92010-06-27 03:08:19 -0300542 frame->data = gspca_dev->frbuf + i * frsz;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300543 frame->v4l2_buf.m.offset = i * frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300544 }
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300545 atomic_set(&gspca_dev->fr_q, 0);
546 atomic_set(&gspca_dev->fr_i, 0);
547 gspca_dev->fr_o = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300548 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300549}
550
551static void frame_free(struct gspca_dev *gspca_dev)
552{
553 int i;
554
555 PDEBUG(D_STREAM, "frame free");
Harvey Harrisonefab8212008-07-05 06:12:47 -0300556 if (gspca_dev->frbuf != NULL) {
Jean-François Moinee795d912010-06-24 05:14:22 -0300557 vfree(gspca_dev->frbuf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300558 gspca_dev->frbuf = NULL;
559 for (i = 0; i < gspca_dev->nframes; i++)
560 gspca_dev->frame[i].data = NULL;
561 }
562 gspca_dev->nframes = 0;
Hans de Goeded642de22010-12-31 04:51:36 -0300563 gspca_dev->frsz = 0;
564 gspca_dev->capt_file = NULL;
565 gspca_dev->memory = GSPCA_MEMORY_NO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300566}
567
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300568static void destroy_urbs(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300569{
570 struct urb *urb;
571 unsigned int i;
572
573 PDEBUG(D_STREAM, "kill transfer");
Erik Andrén44732812008-10-13 15:52:46 -0300574 for (i = 0; i < MAX_NURBS; i++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300575 urb = gspca_dev->urb[i];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300576 if (urb == NULL)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300577 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300578
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300579 gspca_dev->urb[i] = NULL;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300580 usb_kill_urb(urb);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300581 if (urb->transfer_buffer != NULL)
Daniel Mack997ea582010-04-12 13:17:25 +0200582 usb_free_coherent(gspca_dev->dev,
583 urb->transfer_buffer_length,
584 urb->transfer_buffer,
585 urb->transfer_dma);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300586 usb_free_urb(urb);
587 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300588}
589
Jean-François Moinee4dac282010-04-09 06:11:36 -0300590static int gspca_set_alt0(struct gspca_dev *gspca_dev)
591{
592 int ret;
593
594 if (gspca_dev->alt == 0)
595 return 0;
596 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
597 if (ret < 0)
Jean-François Moine0b656322010-09-13 05:19:58 -0300598 err("set alt 0 err %d", ret);
Jean-François Moinee4dac282010-04-09 06:11:36 -0300599 return ret;
600}
601
602/* Note: both the queue and the usb locks should be held when calling this */
603static void gspca_stream_off(struct gspca_dev *gspca_dev)
604{
605 gspca_dev->streaming = 0;
606 if (gspca_dev->present) {
607 if (gspca_dev->sd_desc->stopN)
608 gspca_dev->sd_desc->stopN(gspca_dev);
609 destroy_urbs(gspca_dev);
610 gspca_input_destroy_urb(gspca_dev);
611 gspca_set_alt0(gspca_dev);
612 gspca_input_create_urb(gspca_dev);
613 }
614
615 /* always call stop0 to free the subdriver's resources */
616 if (gspca_dev->sd_desc->stop0)
617 gspca_dev->sd_desc->stop0(gspca_dev);
618 PDEBUG(D_STREAM, "stream off OK");
619}
620
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300621/*
Frank Zago219423c2008-09-26 07:43:54 -0300622 * look for an input transfer endpoint in an alternate setting
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300623 */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300624static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300625 int xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300626{
627 struct usb_host_endpoint *ep;
628 int i, attr;
629
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300630 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
631 ep = &alt->endpoint[i];
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300632 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Jean-Francois Moine30d35e42009-04-21 13:57:31 -0300633 if (attr == xfer
634 && ep->desc.wMaxPacketSize != 0)
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300635 return ep;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300636 }
637 return NULL;
638}
639
640/*
Frank Zago219423c2008-09-26 07:43:54 -0300641 * look for an input (isoc or bulk) endpoint
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300642 *
643 * The endpoint is defined by the subdriver.
644 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
645 * This routine may be called many times when the bandwidth is too small
646 * (the bandwidth is checked on urb submit).
647 */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300648static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300649{
650 struct usb_interface *intf;
651 struct usb_host_endpoint *ep;
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300652 int xfer, i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300653
654 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300655 ep = NULL;
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300656 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
657 : USB_ENDPOINT_XFER_ISOC;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300658 i = gspca_dev->alt; /* previous alt setting */
Hans de Goedea511ba92009-10-16 07:13:07 -0300659 if (gspca_dev->cam.reverse_alts) {
660 while (++i < gspca_dev->nbalt) {
661 ep = alt_xfer(&intf->altsetting[i], xfer);
662 if (ep)
663 break;
664 }
665 } else {
666 while (--i >= 0) {
667 ep = alt_xfer(&intf->altsetting[i], xfer);
668 if (ep)
669 break;
670 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300671 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300672 if (ep == NULL) {
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300673 err("no transfer endpoint found");
674 return NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300675 }
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300676 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300677 i, ep->desc.bEndpointAddress);
Jean-Francois Moine1c8336d2009-07-28 04:38:25 -0300678 gspca_dev->alt = i; /* memorize the current alt setting */
Jean-Francois Moine8dd07ef2009-04-21 14:05:44 -0300679 if (gspca_dev->nbalt > 1) {
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300680 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
681 if (ret < 0) {
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300682 err("set alt %d err %d", i, ret);
Márton Németh0274d422010-01-28 06:39:49 -0300683 ep = NULL;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300684 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300685 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300686 return ep;
687}
688
689/*
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300690 * create the URBs for image transfer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300691 */
692static int create_urbs(struct gspca_dev *gspca_dev,
693 struct usb_host_endpoint *ep)
694{
695 struct urb *urb;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300696 int n, nurbs, i, psize, npkt, bsize;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300697
698 /* calculate the packet size and the number of packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300699 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300700
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300701 if (!gspca_dev->cam.bulk) { /* isoc */
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300702
703 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300704 if (gspca_dev->pkt_size == 0)
705 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
706 else
707 psize = gspca_dev->pkt_size;
Jean-Francois Moine49cb6b02009-04-25 13:29:01 -0300708 npkt = gspca_dev->cam.npkt;
709 if (npkt == 0)
710 npkt = 32; /* default value */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300711 bsize = psize * npkt;
712 PDEBUG(D_STREAM,
713 "isoc %d pkts size %d = bsize:%d",
714 npkt, psize, bsize);
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300715 nurbs = DEF_NURBS;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300716 } else { /* bulk */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300717 npkt = 0;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300718 bsize = gspca_dev->cam.bulk_size;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300719 if (bsize == 0)
720 bsize = psize;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300721 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300722 if (gspca_dev->cam.bulk_nurbs != 0)
723 nurbs = gspca_dev->cam.bulk_nurbs;
724 else
725 nurbs = 1;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300726 }
727
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300728 for (n = 0; n < nurbs; n++) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300729 urb = usb_alloc_urb(npkt, GFP_KERNEL);
730 if (!urb) {
731 err("usb_alloc_urb failed");
732 return -ENOMEM;
733 }
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300734 gspca_dev->urb[n] = urb;
Daniel Mack997ea582010-04-12 13:17:25 +0200735 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300736 bsize,
737 GFP_KERNEL,
738 &urb->transfer_dma);
739
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300740 if (urb->transfer_buffer == NULL) {
Daniel Mack997ea582010-04-12 13:17:25 +0200741 err("usb_alloc_coherent failed");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300742 return -ENOMEM;
743 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300744 urb->dev = gspca_dev->dev;
745 urb->context = gspca_dev;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300746 urb->transfer_buffer_length = bsize;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300747 if (npkt != 0) { /* ISOC */
748 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
749 ep->desc.bEndpointAddress);
Frank Zago6c49da72008-09-28 07:43:00 -0300750 urb->transfer_flags = URB_ISO_ASAP
751 | URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300752 urb->interval = ep->desc.bInterval;
753 urb->complete = isoc_irq;
754 urb->number_of_packets = npkt;
755 for (i = 0; i < npkt; i++) {
756 urb->iso_frame_desc[i].length = psize;
757 urb->iso_frame_desc[i].offset = psize * i;
758 }
759 } else { /* bulk */
760 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
Jean-François Moinea23c1942010-10-29 07:57:03 -0300761 ep->desc.bEndpointAddress);
Frank Zago6c49da72008-09-28 07:43:00 -0300762 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300763 urb->complete = bulk_irq;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300764 }
765 }
766 return 0;
767}
768
769/*
770 * start the USB transfer
771 */
772static int gspca_init_transfer(struct gspca_dev *gspca_dev)
773{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300774 struct usb_host_endpoint *ep;
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300775 struct urb *urb;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300776 int n, ret;
777
Hans de Goedee2997a72008-04-23 08:09:12 -0300778 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
779 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300780
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300781 if (!gspca_dev->present) {
782 ret = -ENODEV;
Hans de Goede314b3e02010-10-26 05:15:46 -0300783 goto unlock;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300784 }
785
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300786 /* reset the streaming variables */
787 gspca_dev->image = NULL;
788 gspca_dev->image_len = 0;
789 gspca_dev->last_packet_type = DISCARD_PACKET;
790 gspca_dev->sequence = 0;
791
Jean-Francois Moineab6c8cf2010-01-13 15:34:27 -0300792 gspca_dev->usb_err = 0;
793
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300794 /* set the higher alternate setting and
795 * loop until urb submit succeeds */
Hans de Goedea511ba92009-10-16 07:13:07 -0300796 if (gspca_dev->cam.reverse_alts)
797 gspca_dev->alt = 0;
798 else
799 gspca_dev->alt = gspca_dev->nbalt;
800
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300801 if (gspca_dev->sd_desc->isoc_init) {
802 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
803 if (ret < 0)
Hans de Goede314b3e02010-10-26 05:15:46 -0300804 goto unlock;
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300805 }
Hans de Goede314b3e02010-10-26 05:15:46 -0300806
807 gspca_input_destroy_urb(gspca_dev);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300808 ep = get_ep(gspca_dev);
809 if (ep == NULL) {
810 ret = -EIO;
811 goto out;
812 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300813 for (;;) {
Jean-Francois Moine47aaca92009-12-15 05:23:04 -0300814 if (!gspca_dev->cam.no_urb_create) {
815 PDEBUG(D_STREAM, "init transfer alt %d",
816 gspca_dev->alt);
817 ret = create_urbs(gspca_dev, ep);
818 if (ret < 0) {
819 destroy_urbs(gspca_dev);
820 goto out;
821 }
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300822 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300823
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300824 /* clear the bulk endpoint */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300825 if (gspca_dev->cam.bulk)
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300826 usb_clear_halt(gspca_dev->dev,
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300827 gspca_dev->urb[0]->pipe);
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300828
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300829 /* start the cam */
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300830 ret = gspca_dev->sd_desc->start(gspca_dev);
831 if (ret < 0) {
832 destroy_urbs(gspca_dev);
833 goto out;
834 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300835 gspca_dev->streaming = 1;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300836
Antonio Ospitedff369a2008-11-14 07:53:32 -0300837 /* some bulk transfers are started by the subdriver */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300838 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300839 break;
840
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300841 /* submit the URBs */
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300842 for (n = 0; n < MAX_NURBS; n++) {
843 urb = gspca_dev->urb[n];
844 if (urb == NULL)
845 break;
846 ret = usb_submit_urb(urb, GFP_KERNEL);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300847 if (ret < 0)
848 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300849 }
850 if (ret >= 0)
851 break;
Jean-François Moinee4dac282010-04-09 06:11:36 -0300852 gspca_stream_off(gspca_dev);
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300853 if (ret != -ENOSPC) {
Jean-François Moine0b656322010-09-13 05:19:58 -0300854 err("usb_submit_urb alt %d err %d",
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300855 gspca_dev->alt, ret);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300856 goto out;
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300857 }
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300858
859 /* the bandwidth is not wide enough
860 * negociate or try a lower alternate setting */
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300861 PDEBUG(D_ERR|D_STREAM,
862 "bandwidth not wide enough - trying again");
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300863 msleep(20); /* wait for kill complete */
864 if (gspca_dev->sd_desc->isoc_nego) {
865 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
866 if (ret < 0)
867 goto out;
868 } else {
869 ep = get_ep(gspca_dev);
870 if (ep == NULL) {
871 ret = -EIO;
872 goto out;
873 }
874 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300875 }
876out:
Hans de Goede314b3e02010-10-26 05:15:46 -0300877 gspca_input_create_urb(gspca_dev);
878unlock:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300879 mutex_unlock(&gspca_dev->usb_lock);
880 return ret;
881}
882
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300883static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300884{
Jean-François Moine6a330912010-10-02 04:12:25 -0300885 struct gspca_ctrl *ctrl;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300886 int i;
887
888 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
889 gspca_dev->curr_mode = i;
890 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
891 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300892 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-François Moine6a330912010-10-02 04:12:25 -0300893
894 /* set the current control values to their default values
895 * which may have changed in sd_init() */
896 ctrl = gspca_dev->cam.ctrls;
897 if (ctrl != NULL) {
898 for (i = 0;
899 i < gspca_dev->sd_desc->nctrls;
900 i++, ctrl++)
901 ctrl->val = ctrl->def;
902 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300903}
904
905static int wxh_to_mode(struct gspca_dev *gspca_dev,
906 int width, int height)
907{
908 int i;
909
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300910 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
911 if (width >= gspca_dev->cam.cam_mode[i].width
912 && height >= gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300913 break;
914 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300915 return i;
916}
917
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300918/*
919 * search a mode with the right pixel format
920 */
921static int gspca_get_mode(struct gspca_dev *gspca_dev,
922 int mode,
923 int pixfmt)
924{
925 int modeU, modeD;
926
927 modeU = modeD = mode;
928 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
929 if (--modeD >= 0) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300930 if (gspca_dev->cam.cam_mode[modeD].pixelformat
931 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300932 return modeD;
933 }
934 if (++modeU < gspca_dev->cam.nmodes) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300935 if (gspca_dev->cam.cam_mode[modeU].pixelformat
936 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300937 return modeU;
938 }
939 }
940 return -EINVAL;
941}
942
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300943#ifdef CONFIG_VIDEO_ADV_DEBUG
944static int vidioc_g_register(struct file *file, void *priv,
945 struct v4l2_dbg_register *reg)
946{
947 int ret;
948 struct gspca_dev *gspca_dev = priv;
949
950 if (!gspca_dev->sd_desc->get_chip_ident)
951 return -EINVAL;
952
953 if (!gspca_dev->sd_desc->get_register)
954 return -EINVAL;
955
956 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
957 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -0300958 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300959 if (gspca_dev->present)
960 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
961 else
962 ret = -ENODEV;
963 mutex_unlock(&gspca_dev->usb_lock);
964
965 return ret;
966}
967
968static int vidioc_s_register(struct file *file, void *priv,
969 struct v4l2_dbg_register *reg)
970{
971 int ret;
972 struct gspca_dev *gspca_dev = priv;
973
974 if (!gspca_dev->sd_desc->get_chip_ident)
975 return -EINVAL;
976
977 if (!gspca_dev->sd_desc->set_register)
978 return -EINVAL;
979
980 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
981 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -0300982 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300983 if (gspca_dev->present)
984 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
985 else
986 ret = -ENODEV;
987 mutex_unlock(&gspca_dev->usb_lock);
988
989 return ret;
990}
991#endif
992
993static int vidioc_g_chip_ident(struct file *file, void *priv,
994 struct v4l2_dbg_chip_ident *chip)
995{
996 int ret;
997 struct gspca_dev *gspca_dev = priv;
998
999 if (!gspca_dev->sd_desc->get_chip_ident)
1000 return -EINVAL;
1001
1002 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1003 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001004 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03001005 if (gspca_dev->present)
1006 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
1007 else
1008 ret = -ENODEV;
1009 mutex_unlock(&gspca_dev->usb_lock);
1010
1011 return ret;
1012}
1013
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001014static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001015 struct v4l2_fmtdesc *fmtdesc)
1016{
1017 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001018 int i, j, index;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001019 __u32 fmt_tb[8];
1020
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001021 /* give an index to each format */
1022 index = 0;
1023 j = 0;
1024 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001025 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001026 j = 0;
1027 for (;;) {
1028 if (fmt_tb[j] == fmt_tb[index])
1029 break;
1030 j++;
1031 }
1032 if (j == index) {
1033 if (fmtdesc->index == index)
1034 break; /* new format */
1035 index++;
Julia Lawall80297122008-11-12 23:18:21 -03001036 if (index >= ARRAY_SIZE(fmt_tb))
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001037 return -EINVAL;
1038 }
1039 }
1040 if (i < 0)
1041 return -EINVAL; /* no more format */
1042
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001043 fmtdesc->pixelformat = fmt_tb[index];
1044 if (gspca_is_compressed(fmt_tb[index]))
1045 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
1046 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
1047 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
1048 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
1049 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
1050 fmtdesc->description[4] = '\0';
1051 return 0;
1052}
1053
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001054static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001055 struct v4l2_format *fmt)
1056{
1057 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001058 int mode;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001059
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001060 mode = gspca_dev->curr_mode;
1061 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1062 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001063 return 0;
1064}
1065
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001066static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001067 struct v4l2_format *fmt)
1068{
Hans de Goede24c530b2008-07-04 17:55:18 -03001069 int w, h, mode, mode2;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001070
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001071 w = fmt->fmt.pix.width;
1072 h = fmt->fmt.pix.height;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001073
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001074#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001075 if (gspca_debug & D_CONF)
1076 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
1077#endif
1078 /* search the closest mode for width and height */
1079 mode = wxh_to_mode(gspca_dev, w, h);
1080
1081 /* OK if right palette */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001082 if (gspca_dev->cam.cam_mode[mode].pixelformat
1083 != fmt->fmt.pix.pixelformat) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001084
1085 /* else, search the closest mode with the same pixel format */
1086 mode2 = gspca_get_mode(gspca_dev, mode,
1087 fmt->fmt.pix.pixelformat);
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001088 if (mode2 >= 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001089 mode = mode2;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001090/* else
1091 ; * no chance, return this mode */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001092 }
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001093 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1094 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001095 return mode; /* used when s_fmt */
1096}
1097
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001098static int vidioc_try_fmt_vid_cap(struct file *file,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001099 void *priv,
1100 struct v4l2_format *fmt)
1101{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001102 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001103 int ret;
1104
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001105 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001106 if (ret < 0)
1107 return ret;
1108 return 0;
1109}
1110
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001111static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001112 struct v4l2_format *fmt)
1113{
1114 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001115 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001116
Hans de Goedee2997a72008-04-23 08:09:12 -03001117 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1118 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001119
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001120 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001121 if (ret < 0)
1122 goto out;
1123
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001124 if (gspca_dev->nframes != 0
1125 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
1126 ret = -EINVAL;
1127 goto out;
1128 }
1129
Jean-Francois Moine50a871f2008-06-30 19:47:33 -03001130 if (ret == gspca_dev->curr_mode) {
1131 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001132 goto out; /* same mode */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -03001133 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001134
1135 if (gspca_dev->streaming) {
1136 ret = -EBUSY;
1137 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -03001138 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001139 gspca_dev->width = fmt->fmt.pix.width;
1140 gspca_dev->height = fmt->fmt.pix.height;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001141 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
1142 gspca_dev->curr_mode = ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001143
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001144 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001145out:
1146 mutex_unlock(&gspca_dev->queue_lock);
1147 return ret;
1148}
1149
Hans de Goede11c635a2009-05-25 15:04:22 -03001150static int vidioc_enum_framesizes(struct file *file, void *priv,
1151 struct v4l2_frmsizeenum *fsize)
1152{
1153 struct gspca_dev *gspca_dev = priv;
1154 int i;
1155 __u32 index = 0;
1156
1157 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
1158 if (fsize->pixel_format !=
1159 gspca_dev->cam.cam_mode[i].pixelformat)
1160 continue;
1161
1162 if (fsize->index == index) {
1163 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1164 fsize->discrete.width =
1165 gspca_dev->cam.cam_mode[i].width;
1166 fsize->discrete.height =
1167 gspca_dev->cam.cam_mode[i].height;
1168 return 0;
1169 }
1170 index++;
1171 }
1172
1173 return -EINVAL;
1174}
1175
Antonio Ospite28ffe772009-12-02 06:18:46 -03001176static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1177 struct v4l2_frmivalenum *fival)
1178{
1179 struct gspca_dev *gspca_dev = priv;
1180 int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
1181 __u32 i;
1182
1183 if (gspca_dev->cam.mode_framerates == NULL ||
1184 gspca_dev->cam.mode_framerates[mode].nrates == 0)
1185 return -EINVAL;
1186
1187 if (fival->pixel_format !=
1188 gspca_dev->cam.cam_mode[mode].pixelformat)
1189 return -EINVAL;
1190
1191 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1192 if (fival->index == i) {
1193 fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1194 fival->discrete.numerator = 1;
1195 fival->discrete.denominator =
1196 gspca_dev->cam.mode_framerates[mode].rates[i];
1197 return 0;
1198 }
1199 }
1200
1201 return -EINVAL;
1202}
1203
Hans de Goeded0182302008-11-19 17:10:47 -03001204static void gspca_release(struct video_device *vfd)
Frank Zago6b060ff2008-09-28 08:12:22 -03001205{
Hans de Goeded0182302008-11-19 17:10:47 -03001206 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
Frank Zago6b060ff2008-09-28 08:12:22 -03001207
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001208 PDEBUG(D_PROBE, "%s released",
1209 video_device_node_name(&gspca_dev->vdev));
Frank Zago6b060ff2008-09-28 08:12:22 -03001210
1211 kfree(gspca_dev->usb_buf);
1212 kfree(gspca_dev);
1213}
1214
Hans Verkuilbec43662008-12-30 06:58:20 -03001215static int dev_open(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001216{
1217 struct gspca_dev *gspca_dev;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001218
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001219 PDEBUG(D_STREAM, "[%s] open", current->comm);
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001220 gspca_dev = (struct gspca_dev *) video_devdata(file);
Hans de Goede7f6eb112010-12-30 20:20:09 -03001221 if (!gspca_dev->present)
1222 return -ENODEV;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03001223
1224 /* protect the subdriver against rmmod */
Hans de Goede7f6eb112010-12-30 20:20:09 -03001225 if (!try_module_get(gspca_dev->module))
1226 return -ENODEV;
Frank Zago6b060ff2008-09-28 08:12:22 -03001227
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001228 file->private_data = gspca_dev;
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001229#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001230 /* activate the v4l2 debug */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001231 if (gspca_debug & D_V4L2)
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001232 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001233 | V4L2_DEBUG_IOCTL_ARG;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001234 else
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001235 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001236 | V4L2_DEBUG_IOCTL_ARG);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001237#endif
Hans de Goede7f6eb112010-12-30 20:20:09 -03001238 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001239}
1240
Hans Verkuilbec43662008-12-30 06:58:20 -03001241static int dev_close(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001242{
1243 struct gspca_dev *gspca_dev = file->private_data;
1244
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001245 PDEBUG(D_STREAM, "[%s] close", current->comm);
Hans de Goedee2997a72008-04-23 08:09:12 -03001246 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1247 return -ERESTARTSYS;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001248
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001249 /* if the file did the capture, free the streaming resources */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001250 if (gspca_dev->capt_file == file) {
Jean-Francois Moinea1e82882008-09-03 17:12:17 -03001251 if (gspca_dev->streaming) {
1252 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001253 gspca_dev->usb_err = 0;
Jean-Francois Moinea1e82882008-09-03 17:12:17 -03001254 gspca_stream_off(gspca_dev);
1255 mutex_unlock(&gspca_dev->usb_lock);
1256 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001257 frame_free(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001258 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001259 file->private_data = NULL;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03001260 module_put(gspca_dev->module);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001261 mutex_unlock(&gspca_dev->queue_lock);
Frank Zago6b060ff2008-09-28 08:12:22 -03001262
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001263 PDEBUG(D_STREAM, "close done");
Frank Zago6b060ff2008-09-28 08:12:22 -03001264
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001265 return 0;
1266}
1267
1268static int vidioc_querycap(struct file *file, void *priv,
1269 struct v4l2_capability *cap)
1270{
1271 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001272 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001273
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001274 /* protect the access to the usb device */
1275 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1276 return -ERESTARTSYS;
1277 if (!gspca_dev->present) {
1278 ret = -ENODEV;
1279 goto out;
1280 }
Jean-François Moined8518592010-12-28 06:55:01 -03001281 strncpy((char *) cap->driver, gspca_dev->sd_desc->name,
1282 sizeof cap->driver);
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -03001283 if (gspca_dev->dev->product != NULL) {
Jean-François Moined8518592010-12-28 06:55:01 -03001284 strncpy((char *) cap->card, gspca_dev->dev->product,
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -03001285 sizeof cap->card);
1286 } else {
Jean-François Moined8518592010-12-28 06:55:01 -03001287 snprintf((char *) cap->card, sizeof cap->card,
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -03001288 "USB Camera (%04x:%04x)",
1289 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1290 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1291 }
Jean-François Moined8518592010-12-28 06:55:01 -03001292 usb_make_path(gspca_dev->dev, (char *) cap->bus_info,
1293 sizeof(cap->bus_info));
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001294 cap->version = DRIVER_VERSION_NUMBER;
1295 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1296 | V4L2_CAP_STREAMING
1297 | V4L2_CAP_READWRITE;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001298 ret = 0;
1299out:
1300 mutex_unlock(&gspca_dev->usb_lock);
1301 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001302}
1303
Jean-François Moine6a330912010-10-02 04:12:25 -03001304static int get_ctrl(struct gspca_dev *gspca_dev,
Hans de Goedea0001a22009-06-10 04:52:18 -03001305 int id)
1306{
1307 const struct ctrl *ctrls;
1308 int i;
1309
1310 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1311 i < gspca_dev->sd_desc->nctrls;
1312 i++, ctrls++) {
1313 if (gspca_dev->ctrl_dis & (1 << i))
1314 continue;
1315 if (id == ctrls->qctrl.id)
Jean-François Moine6a330912010-10-02 04:12:25 -03001316 return i;
Hans de Goedea0001a22009-06-10 04:52:18 -03001317 }
Jean-François Moine6a330912010-10-02 04:12:25 -03001318 return -1;
Hans de Goedea0001a22009-06-10 04:52:18 -03001319}
1320
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001321static int vidioc_queryctrl(struct file *file, void *priv,
1322 struct v4l2_queryctrl *q_ctrl)
1323{
1324 struct gspca_dev *gspca_dev = priv;
Hans de Goedea0001a22009-06-10 04:52:18 -03001325 const struct ctrl *ctrls;
Jean-François Moine6a330912010-10-02 04:12:25 -03001326 struct gspca_ctrl *gspca_ctrl;
1327 int i, idx;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001328 u32 id;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001329
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001330 id = q_ctrl->id;
1331 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1332 id &= V4L2_CTRL_ID_MASK;
1333 id++;
Jean-François Moine6a330912010-10-02 04:12:25 -03001334 idx = -1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001335 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001336 if (gspca_dev->ctrl_dis & (1 << i))
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001337 continue;
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001338 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001339 continue;
Jean-François Moine6a330912010-10-02 04:12:25 -03001340 if (idx >= 0
1341 && gspca_dev->sd_desc->ctrls[i].qctrl.id
Jean-François Moine780e3122010-10-19 04:29:10 -03001342 > gspca_dev->sd_desc->ctrls[idx].qctrl.id)
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001343 continue;
Jean-François Moine6a330912010-10-02 04:12:25 -03001344 idx = i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001345 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001346 } else {
Jean-François Moine6a330912010-10-02 04:12:25 -03001347 idx = get_ctrl(gspca_dev, id);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001348 }
Jean-François Moine6a330912010-10-02 04:12:25 -03001349 if (idx < 0)
1350 return -EINVAL;
1351 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1352 memcpy(q_ctrl, &ctrls->qctrl, sizeof *q_ctrl);
1353 if (gspca_dev->cam.ctrls != NULL) {
1354 gspca_ctrl = &gspca_dev->cam.ctrls[idx];
1355 q_ctrl->default_value = gspca_ctrl->def;
1356 q_ctrl->minimum = gspca_ctrl->min;
1357 q_ctrl->maximum = gspca_ctrl->max;
1358 }
1359 if (gspca_dev->ctrl_inac & (1 << idx))
Jean-Francois Moine4af85662009-11-12 15:59:27 -03001360 q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001361 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001362}
1363
1364static int vidioc_s_ctrl(struct file *file, void *priv,
1365 struct v4l2_control *ctrl)
1366{
1367 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001368 const struct ctrl *ctrls;
Jean-François Moine6a330912010-10-02 04:12:25 -03001369 struct gspca_ctrl *gspca_ctrl;
1370 int idx, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001371
Jean-François Moine6a330912010-10-02 04:12:25 -03001372 idx = get_ctrl(gspca_dev, ctrl->id);
1373 if (idx < 0)
Hans de Goedea0001a22009-06-10 04:52:18 -03001374 return -EINVAL;
Jean-François Moine6a330912010-10-02 04:12:25 -03001375 if (gspca_dev->ctrl_inac & (1 << idx))
1376 return -EINVAL;
1377 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1378 if (gspca_dev->cam.ctrls != NULL) {
1379 gspca_ctrl = &gspca_dev->cam.ctrls[idx];
1380 if (ctrl->value < gspca_ctrl->min
1381 || ctrl->value > gspca_ctrl->max)
1382 return -ERANGE;
1383 } else {
1384 gspca_ctrl = NULL;
1385 if (ctrl->value < ctrls->qctrl.minimum
1386 || ctrl->value > ctrls->qctrl.maximum)
1387 return -ERANGE;
1388 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001389 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1390 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1391 return -ERESTARTSYS;
Jean-François Moine6a330912010-10-02 04:12:25 -03001392 if (!gspca_dev->present) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001393 ret = -ENODEV;
Jean-François Moine6a330912010-10-02 04:12:25 -03001394 goto out;
1395 }
1396 gspca_dev->usb_err = 0;
1397 if (ctrls->set != NULL) {
1398 ret = ctrls->set(gspca_dev, ctrl->value);
1399 goto out;
1400 }
1401 if (gspca_ctrl != NULL) {
1402 gspca_ctrl->val = ctrl->value;
1403 if (ctrls->set_control != NULL
1404 && gspca_dev->streaming)
1405 ctrls->set_control(gspca_dev);
1406 }
1407 ret = gspca_dev->usb_err;
1408out:
Hans de Goedea0001a22009-06-10 04:52:18 -03001409 mutex_unlock(&gspca_dev->usb_lock);
1410 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001411}
1412
1413static int vidioc_g_ctrl(struct file *file, void *priv,
1414 struct v4l2_control *ctrl)
1415{
1416 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001417 const struct ctrl *ctrls;
Jean-François Moine6a330912010-10-02 04:12:25 -03001418 int idx, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001419
Jean-François Moine6a330912010-10-02 04:12:25 -03001420 idx = get_ctrl(gspca_dev, ctrl->id);
1421 if (idx < 0)
Hans de Goedea0001a22009-06-10 04:52:18 -03001422 return -EINVAL;
Jean-François Moine6a330912010-10-02 04:12:25 -03001423 ctrls = &gspca_dev->sd_desc->ctrls[idx];
Hans de Goedea0001a22009-06-10 04:52:18 -03001424
1425 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1426 return -ERESTARTSYS;
Jean-François Moine6a330912010-10-02 04:12:25 -03001427 if (!gspca_dev->present) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001428 ret = -ENODEV;
Jean-François Moine6a330912010-10-02 04:12:25 -03001429 goto out;
1430 }
1431 gspca_dev->usb_err = 0;
1432 if (ctrls->get != NULL) {
1433 ret = ctrls->get(gspca_dev, &ctrl->value);
1434 goto out;
1435 }
1436 if (gspca_dev->cam.ctrls != NULL)
1437 ctrl->value = gspca_dev->cam.ctrls[idx].val;
1438 ret = 0;
1439out:
Hans de Goedea0001a22009-06-10 04:52:18 -03001440 mutex_unlock(&gspca_dev->usb_lock);
1441 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001442}
1443
1444static int vidioc_querymenu(struct file *file, void *priv,
1445 struct v4l2_querymenu *qmenu)
1446{
1447 struct gspca_dev *gspca_dev = priv;
1448
1449 if (!gspca_dev->sd_desc->querymenu)
1450 return -EINVAL;
1451 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1452}
1453
1454static int vidioc_enum_input(struct file *file, void *priv,
1455 struct v4l2_input *input)
1456{
1457 struct gspca_dev *gspca_dev = priv;
1458
1459 if (input->index != 0)
1460 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001461 input->type = V4L2_INPUT_TYPE_CAMERA;
Adam Bakerdfa76fa2009-03-29 19:17:10 -03001462 input->status = gspca_dev->cam.input_flags;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001463 strncpy(input->name, gspca_dev->sd_desc->name,
1464 sizeof input->name);
1465 return 0;
1466}
1467
1468static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1469{
1470 *i = 0;
1471 return 0;
1472}
1473
1474static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1475{
1476 if (i > 0)
1477 return -EINVAL;
1478 return (0);
1479}
1480
1481static int vidioc_reqbufs(struct file *file, void *priv,
1482 struct v4l2_requestbuffers *rb)
1483{
1484 struct gspca_dev *gspca_dev = priv;
Jean-François Moine832d0a92010-04-09 06:07:58 -03001485 int i, ret = 0, streaming;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001486
Jean-François Moinea2ba6f22010-07-14 06:21:16 -03001487 i = rb->memory; /* (avoid compilation warning) */
1488 switch (i) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001489 case GSPCA_MEMORY_READ: /* (internal call) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001490 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001491 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001492 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001493 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001494 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001495 }
Hans de Goedee2997a72008-04-23 08:09:12 -03001496 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1497 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001498
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001499 if (gspca_dev->memory != GSPCA_MEMORY_NO
1500 && gspca_dev->memory != rb->memory) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001501 ret = -EBUSY;
1502 goto out;
1503 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001504
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001505 /* only one file may do the capture */
1506 if (gspca_dev->capt_file != NULL
1507 && gspca_dev->capt_file != file) {
1508 ret = -EBUSY;
1509 goto out;
1510 }
1511
1512 /* if allocated, the buffers must not be mapped */
1513 for (i = 0; i < gspca_dev->nframes; i++) {
1514 if (gspca_dev->frame[i].vma_use_count) {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001515 ret = -EBUSY;
1516 goto out;
1517 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001518 }
1519
1520 /* stop streaming */
Jean-François Moine832d0a92010-04-09 06:07:58 -03001521 streaming = gspca_dev->streaming;
1522 if (streaming) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001523 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001524 gspca_dev->usb_err = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001525 gspca_stream_off(gspca_dev);
1526 mutex_unlock(&gspca_dev->usb_lock);
1527 }
1528
1529 /* free the previous allocated buffers, if any */
Hans de Goeded642de22010-12-31 04:51:36 -03001530 if (gspca_dev->nframes != 0)
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001531 frame_free(gspca_dev);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001532 if (rb->count == 0) /* unrequest */
1533 goto out;
Hans de Goeded642de22010-12-31 04:51:36 -03001534 ret = frame_alloc(gspca_dev, file, rb->memory, rb->count);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001535 if (ret == 0) {
1536 rb->count = gspca_dev->nframes;
Jean-François Moine832d0a92010-04-09 06:07:58 -03001537 if (streaming)
1538 ret = gspca_init_transfer(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001539 }
1540out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001541 mutex_unlock(&gspca_dev->queue_lock);
1542 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1543 return ret;
1544}
1545
1546static int vidioc_querybuf(struct file *file, void *priv,
1547 struct v4l2_buffer *v4l2_buf)
1548{
1549 struct gspca_dev *gspca_dev = priv;
1550 struct gspca_frame *frame;
1551
Jean-Francois Moine13752bd2008-12-19 15:08:51 -03001552 if (v4l2_buf->index < 0
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001553 || v4l2_buf->index >= gspca_dev->nframes)
1554 return -EINVAL;
1555
1556 frame = &gspca_dev->frame[v4l2_buf->index];
1557 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1558 return 0;
1559}
1560
1561static int vidioc_streamon(struct file *file, void *priv,
1562 enum v4l2_buf_type buf_type)
1563{
1564 struct gspca_dev *gspca_dev = priv;
1565 int ret;
1566
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001567 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1568 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001569 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1570 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001571
Jean-François Moine18bf99a2010-04-09 06:06:11 -03001572 /* check the capture file */
1573 if (gspca_dev->capt_file != file) {
1574 ret = -EBUSY;
1575 goto out;
1576 }
1577
Jean-Francois Moine1edabe72008-12-19 15:17:25 -03001578 if (gspca_dev->nframes == 0
1579 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001580 ret = -EINVAL;
1581 goto out;
1582 }
1583 if (!gspca_dev->streaming) {
1584 ret = gspca_init_transfer(gspca_dev);
1585 if (ret < 0)
1586 goto out;
1587 }
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001588#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001589 if (gspca_debug & D_STREAM) {
1590 PDEBUG_MODE("stream on OK",
1591 gspca_dev->pixfmt,
1592 gspca_dev->width,
1593 gspca_dev->height);
1594 }
1595#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001596 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001597out:
1598 mutex_unlock(&gspca_dev->queue_lock);
1599 return ret;
1600}
1601
1602static int vidioc_streamoff(struct file *file, void *priv,
1603 enum v4l2_buf_type buf_type)
1604{
1605 struct gspca_dev *gspca_dev = priv;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001606 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001607
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001608 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1609 return -EINVAL;
Hans de Goede4e770f72010-12-30 13:11:21 -03001610
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001611 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1612 return -ERESTARTSYS;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001613
Hans de Goede4e770f72010-12-30 13:11:21 -03001614 if (!gspca_dev->streaming) {
1615 ret = 0;
1616 goto out;
1617 }
1618
Jean-François Moine18bf99a2010-04-09 06:06:11 -03001619 /* check the capture file */
1620 if (gspca_dev->capt_file != file) {
1621 ret = -EBUSY;
1622 goto out;
1623 }
1624
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001625 /* stop streaming */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001626 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1627 ret = -ERESTARTSYS;
1628 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001629 }
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001630 gspca_dev->usb_err = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001631 gspca_stream_off(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001632 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001633
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001634 /* empty the transfer queues */
1635 atomic_set(&gspca_dev->fr_q, 0);
1636 atomic_set(&gspca_dev->fr_i, 0);
1637 gspca_dev->fr_o = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001638 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001639out:
1640 mutex_unlock(&gspca_dev->queue_lock);
1641 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001642}
1643
1644static int vidioc_g_jpegcomp(struct file *file, void *priv,
1645 struct v4l2_jpegcompression *jpegcomp)
1646{
1647 struct gspca_dev *gspca_dev = priv;
1648 int ret;
1649
1650 if (!gspca_dev->sd_desc->get_jcomp)
1651 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001652 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1653 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001654 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001655 if (gspca_dev->present)
1656 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1657 else
1658 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001659 mutex_unlock(&gspca_dev->usb_lock);
1660 return ret;
1661}
1662
1663static int vidioc_s_jpegcomp(struct file *file, void *priv,
1664 struct v4l2_jpegcompression *jpegcomp)
1665{
1666 struct gspca_dev *gspca_dev = priv;
1667 int ret;
1668
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001669 if (!gspca_dev->sd_desc->set_jcomp)
1670 return -EINVAL;
Jim Parisf86d4a92008-12-10 05:50:44 -03001671 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1672 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001673 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001674 if (gspca_dev->present)
1675 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1676 else
1677 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001678 mutex_unlock(&gspca_dev->usb_lock);
1679 return ret;
1680}
1681
1682static int vidioc_g_parm(struct file *filp, void *priv,
1683 struct v4l2_streamparm *parm)
1684{
1685 struct gspca_dev *gspca_dev = priv;
1686
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001687 parm->parm.capture.readbuffers = gspca_dev->nbufread;
Jim Paris627a5ef2008-12-10 06:02:42 -03001688
1689 if (gspca_dev->sd_desc->get_streamparm) {
1690 int ret;
1691
1692 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1693 return -ERESTARTSYS;
Jean-François Moine668f44a2010-12-25 13:46:14 -03001694 if (gspca_dev->present) {
1695 gspca_dev->usb_err = 0;
1696 gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
1697 ret = gspca_dev->usb_err;
1698 } else {
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001699 ret = -ENODEV;
Jean-François Moine668f44a2010-12-25 13:46:14 -03001700 }
Jim Paris627a5ef2008-12-10 06:02:42 -03001701 mutex_unlock(&gspca_dev->usb_lock);
1702 return ret;
1703 }
1704
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001705 return 0;
1706}
1707
1708static int vidioc_s_parm(struct file *filp, void *priv,
1709 struct v4l2_streamparm *parm)
1710{
1711 struct gspca_dev *gspca_dev = priv;
1712 int n;
1713
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001714 n = parm->parm.capture.readbuffers;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001715 if (n == 0 || n >= GSPCA_MAX_FRAMES)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001716 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1717 else
1718 gspca_dev->nbufread = n;
Jim Paris627a5ef2008-12-10 06:02:42 -03001719
1720 if (gspca_dev->sd_desc->set_streamparm) {
1721 int ret;
1722
1723 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1724 return -ERESTARTSYS;
Jean-François Moine668f44a2010-12-25 13:46:14 -03001725 if (gspca_dev->present) {
1726 gspca_dev->usb_err = 0;
1727 gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
1728 ret = gspca_dev->usb_err;
1729 } else {
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001730 ret = -ENODEV;
Jean-François Moine668f44a2010-12-25 13:46:14 -03001731 }
Jim Paris627a5ef2008-12-10 06:02:42 -03001732 mutex_unlock(&gspca_dev->usb_lock);
1733 return ret;
1734 }
1735
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001736 return 0;
1737}
1738
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001739static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1740{
1741 struct gspca_dev *gspca_dev = file->private_data;
Harvey Harrisonefab8212008-07-05 06:12:47 -03001742 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001743 struct page *page;
1744 unsigned long addr, start, size;
1745 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001746
1747 start = vma->vm_start;
1748 size = vma->vm_end - vma->vm_start;
1749 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1750
Hans de Goedee2997a72008-04-23 08:09:12 -03001751 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1752 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001753 if (!gspca_dev->present) {
1754 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001755 goto out;
1756 }
1757 if (gspca_dev->capt_file != file) {
1758 ret = -EINVAL;
1759 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001760 }
1761
Harvey Harrisonefab8212008-07-05 06:12:47 -03001762 frame = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001763 for (i = 0; i < gspca_dev->nframes; ++i) {
1764 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1765 PDEBUG(D_STREAM, "mmap bad memory type");
1766 break;
1767 }
1768 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1769 == vma->vm_pgoff) {
1770 frame = &gspca_dev->frame[i];
1771 break;
1772 }
1773 }
Harvey Harrisonefab8212008-07-05 06:12:47 -03001774 if (frame == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001775 PDEBUG(D_STREAM, "mmap no frame buffer found");
1776 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001777 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001778 }
Jean-François Moine38bff692010-06-24 05:19:58 -03001779 if (size != frame->v4l2_buf.length) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001780 PDEBUG(D_STREAM, "mmap bad size");
1781 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001782 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001783 }
1784
1785 /*
1786 * - VM_IO marks the area as being a mmaped region for I/O to a
1787 * device. It also prevents the region from being core dumped.
1788 */
1789 vma->vm_flags |= VM_IO;
1790
1791 addr = (unsigned long) frame->data;
1792 while (size > 0) {
1793 page = vmalloc_to_page((void *) addr);
1794 ret = vm_insert_page(vma, start, page);
1795 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001796 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001797 start += PAGE_SIZE;
1798 addr += PAGE_SIZE;
1799 size -= PAGE_SIZE;
1800 }
1801
Jean-Francois Moined50e2572009-11-26 15:36:40 -03001802 vma->vm_ops = &gspca_vm_ops;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001803 vma->vm_private_data = frame;
1804 gspca_vm_open(vma);
Hans de Goedee2997a72008-04-23 08:09:12 -03001805 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001806out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001807 mutex_unlock(&gspca_dev->queue_lock);
1808 return ret;
1809}
1810
Hans de Goede27074ef2010-12-30 19:54:33 -03001811static int frame_ready_nolock(struct gspca_dev *gspca_dev, struct file *file,
1812 enum v4l2_memory memory)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001813{
Hans de Goede27074ef2010-12-30 19:54:33 -03001814 if (!gspca_dev->present)
1815 return -ENODEV;
1816 if (gspca_dev->capt_file != file || gspca_dev->memory != memory ||
1817 !gspca_dev->streaming)
1818 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001819
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001820 /* check if a frame is ready */
Hans de Goede27074ef2010-12-30 19:54:33 -03001821 return gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i);
1822}
Hans de Goede181b7042008-11-19 17:16:26 -03001823
Hans de Goede27074ef2010-12-30 19:54:33 -03001824static int frame_ready(struct gspca_dev *gspca_dev, struct file *file,
1825 enum v4l2_memory memory)
1826{
1827 int ret;
Hans de Goede181b7042008-11-19 17:16:26 -03001828
Hans de Goede27074ef2010-12-30 19:54:33 -03001829 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1830 return -ERESTARTSYS;
1831 ret = frame_ready_nolock(gspca_dev, file, memory);
1832 mutex_unlock(&gspca_dev->queue_lock);
1833 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001834}
1835
1836/*
1837 * dequeue a video buffer
1838 *
1839 * If nonblock_ing is false, block until a buffer is available.
1840 */
1841static int vidioc_dqbuf(struct file *file, void *priv,
1842 struct v4l2_buffer *v4l2_buf)
1843{
1844 struct gspca_dev *gspca_dev = priv;
1845 struct gspca_frame *frame;
Hans de Goede27074ef2010-12-30 19:54:33 -03001846 int i, j, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001847
1848 PDEBUG(D_FRAM, "dqbuf");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001849
Hans de Goede27074ef2010-12-30 19:54:33 -03001850 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001851 return -ERESTARTSYS;
1852
Hans de Goede27074ef2010-12-30 19:54:33 -03001853 for (;;) {
1854 ret = frame_ready_nolock(gspca_dev, file, v4l2_buf->memory);
1855 if (ret < 0)
1856 goto out;
1857 if (ret > 0)
1858 break;
1859
1860 mutex_unlock(&gspca_dev->queue_lock);
1861
1862 if (file->f_flags & O_NONBLOCK)
1863 return -EAGAIN;
1864
1865 /* wait till a frame is ready */
1866 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1867 frame_ready(gspca_dev, file, v4l2_buf->memory),
1868 msecs_to_jiffies(3000));
1869 if (ret < 0)
1870 return ret;
1871 if (ret == 0)
1872 return -EIO;
1873
1874 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1875 return -ERESTARTSYS;
1876 }
1877
1878 i = gspca_dev->fr_o;
1879 j = gspca_dev->fr_queue[i];
1880 frame = &gspca_dev->frame[j];
1881
1882 gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES;
1883
1884 if (gspca_dev->sd_desc->dq_callback) {
1885 mutex_lock(&gspca_dev->usb_lock);
1886 gspca_dev->usb_err = 0;
1887 if (gspca_dev->present)
1888 gspca_dev->sd_desc->dq_callback(gspca_dev);
1889 mutex_unlock(&gspca_dev->usb_lock);
1890 }
1891
Hans de Goede4a82bc62010-12-30 20:00:17 -03001892 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1893 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1894 PDEBUG(D_FRAM, "dqbuf %d", j);
1895 ret = 0;
1896
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001897 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001898 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001899 frame->data,
1900 frame->v4l2_buf.bytesused)) {
1901 PDEBUG(D_ERR|D_STREAM,
1902 "dqbuf cp to user failed");
1903 ret = -EFAULT;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001904 }
1905 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001906out:
Hans de Goede27074ef2010-12-30 19:54:33 -03001907 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001908 return ret;
1909}
1910
1911/*
1912 * queue a video buffer
1913 *
1914 * Attempting to queue a buffer that has already been
1915 * queued will return -EINVAL.
1916 */
1917static int vidioc_qbuf(struct file *file, void *priv,
1918 struct v4l2_buffer *v4l2_buf)
1919{
1920 struct gspca_dev *gspca_dev = priv;
1921 struct gspca_frame *frame;
1922 int i, index, ret;
1923
1924 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001925
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001926 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1927 return -ERESTARTSYS;
1928
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001929 index = v4l2_buf->index;
1930 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001931 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001932 "qbuf idx %d >= %d", index, gspca_dev->nframes);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001933 ret = -EINVAL;
1934 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001935 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001936 if (v4l2_buf->memory != gspca_dev->memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001937 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001938 ret = -EINVAL;
1939 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001940 }
1941
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001942 frame = &gspca_dev->frame[index];
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001943 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1944 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001945 ret = -EINVAL;
1946 goto out;
1947 }
1948
1949 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001950
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001951 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001952 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1953 frame->v4l2_buf.length = v4l2_buf->length;
1954 }
1955
1956 /* put the buffer in the 'queued' queue */
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001957 i = atomic_read(&gspca_dev->fr_q);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001958 gspca_dev->fr_queue[i] = index;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001959 atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001960
1961 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1962 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1963 ret = 0;
1964out:
1965 mutex_unlock(&gspca_dev->queue_lock);
1966 return ret;
1967}
1968
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001969/*
1970 * allocate the resources for read()
1971 */
1972static int read_alloc(struct gspca_dev *gspca_dev,
1973 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001974{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001975 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001976 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001977
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001978 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001979 if (gspca_dev->nframes == 0) {
1980 struct v4l2_requestbuffers rb;
1981
1982 memset(&rb, 0, sizeof rb);
1983 rb.count = gspca_dev->nbufread;
1984 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001985 rb.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001986 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1987 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001988 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001989 return ret;
1990 }
1991 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1992 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001993 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001994 for (i = 0; i < gspca_dev->nbufread; i++) {
1995 v4l2_buf.index = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001996 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1997 if (ret != 0) {
1998 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1999 return ret;
2000 }
2001 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002002 gspca_dev->memory = GSPCA_MEMORY_READ;
2003 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002004
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002005 /* start streaming */
2006 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2007 if (ret != 0)
2008 PDEBUG(D_STREAM, "read streamon err %d", ret);
2009 return ret;
2010}
2011
2012static unsigned int dev_poll(struct file *file, poll_table *wait)
2013{
2014 struct gspca_dev *gspca_dev = file->private_data;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03002015 int ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002016
2017 PDEBUG(D_FRAM, "poll");
2018
2019 poll_wait(file, &gspca_dev->wq, wait);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002020
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002021 /* if reqbufs is not done, the user would use read() */
Hans de Goedeee362992010-12-31 05:05:56 -03002022 if (gspca_dev->memory == GSPCA_MEMORY_NO) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002023 ret = read_alloc(gspca_dev, file);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002024 if (ret != 0)
2025 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002026 }
2027
2028 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
2029 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002030
Jean-François Moinef7059ea2010-07-06 04:32:27 -03002031 /* check if an image has been received */
2032 if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i))
2033 ret = POLLIN | POLLRDNORM; /* yes */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002034 else
2035 ret = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002036 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002037 if (!gspca_dev->present)
2038 return POLLHUP;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002039 return ret;
2040}
2041
2042static ssize_t dev_read(struct file *file, char __user *data,
2043 size_t count, loff_t *ppos)
2044{
2045 struct gspca_dev *gspca_dev = file->private_data;
2046 struct gspca_frame *frame;
2047 struct v4l2_buffer v4l2_buf;
2048 struct timeval timestamp;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002049 int n, ret, ret2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002050
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03002051 PDEBUG(D_FRAM, "read (%zd)", count);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002052 if (!gspca_dev->present)
2053 return -ENODEV;
Hans de Goedeee362992010-12-31 05:05:56 -03002054 if (gspca_dev->memory == GSPCA_MEMORY_NO) { /* first time ? */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002055 ret = read_alloc(gspca_dev, file);
2056 if (ret != 0)
2057 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002058 }
2059
2060 /* get a frame */
Paulo Assis2f51d002010-03-07 06:56:56 -03002061 timestamp = ktime_to_timeval(ktime_get());
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002062 timestamp.tv_sec--;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002063 n = 2;
2064 for (;;) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002065 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2066 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002067 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002068 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
2069 if (ret != 0) {
2070 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
2071 return ret;
2072 }
2073
2074 /* if the process slept for more than 1 second,
Frank Zago219423c2008-09-26 07:43:54 -03002075 * get a newer frame */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002076 frame = &gspca_dev->frame[v4l2_buf.index];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002077 if (--n < 0)
2078 break; /* avoid infinite loop */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002079 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
2080 break;
2081 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2082 if (ret != 0) {
2083 PDEBUG(D_STREAM, "read qbuf err %d", ret);
2084 return ret;
2085 }
2086 }
2087
2088 /* copy the frame */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002089 if (count > frame->v4l2_buf.bytesused)
2090 count = frame->v4l2_buf.bytesused;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002091 ret = copy_to_user(data, frame->data, count);
2092 if (ret != 0) {
2093 PDEBUG(D_ERR|D_STREAM,
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03002094 "read cp to user lack %d / %zd", ret, count);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002095 ret = -EFAULT;
2096 goto out;
2097 }
2098 ret = count;
2099out:
2100 /* in each case, requeue the buffer */
2101 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2102 if (ret2 != 0)
2103 return ret2;
2104 return ret;
2105}
2106
Hans Verkuilbec43662008-12-30 06:58:20 -03002107static struct v4l2_file_operations dev_fops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002108 .owner = THIS_MODULE,
2109 .open = dev_open,
2110 .release = dev_close,
2111 .read = dev_read,
2112 .mmap = dev_mmap,
Hans Verkuilbec43662008-12-30 06:58:20 -03002113 .unlocked_ioctl = video_ioctl2,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002114 .poll = dev_poll,
2115};
2116
Hans Verkuila3998102008-07-21 02:57:38 -03002117static const struct v4l2_ioctl_ops dev_ioctl_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002118 .vidioc_querycap = vidioc_querycap,
2119 .vidioc_dqbuf = vidioc_dqbuf,
2120 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03002121 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2122 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2123 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2124 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002125 .vidioc_streamon = vidioc_streamon,
2126 .vidioc_queryctrl = vidioc_queryctrl,
2127 .vidioc_g_ctrl = vidioc_g_ctrl,
2128 .vidioc_s_ctrl = vidioc_s_ctrl,
2129 .vidioc_querymenu = vidioc_querymenu,
2130 .vidioc_enum_input = vidioc_enum_input,
2131 .vidioc_g_input = vidioc_g_input,
2132 .vidioc_s_input = vidioc_s_input,
2133 .vidioc_reqbufs = vidioc_reqbufs,
2134 .vidioc_querybuf = vidioc_querybuf,
2135 .vidioc_streamoff = vidioc_streamoff,
2136 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
2137 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
2138 .vidioc_g_parm = vidioc_g_parm,
2139 .vidioc_s_parm = vidioc_s_parm,
Hans de Goede11c635a2009-05-25 15:04:22 -03002140 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Antonio Ospite28ffe772009-12-02 06:18:46 -03002141 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03002142#ifdef CONFIG_VIDEO_ADV_DEBUG
2143 .vidioc_g_register = vidioc_g_register,
2144 .vidioc_s_register = vidioc_s_register,
2145#endif
2146 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002147};
2148
Hans Verkuila3998102008-07-21 02:57:38 -03002149static struct video_device gspca_template = {
2150 .name = "gspca main driver",
Hans Verkuila3998102008-07-21 02:57:38 -03002151 .fops = &dev_fops,
2152 .ioctl_ops = &dev_ioctl_ops,
Hans de Goeded0182302008-11-19 17:10:47 -03002153 .release = gspca_release,
Hans Verkuila3998102008-07-21 02:57:38 -03002154};
2155
Jean-François Moine6a330912010-10-02 04:12:25 -03002156/* initialize the controls */
2157static void ctrls_init(struct gspca_dev *gspca_dev)
2158{
2159 struct gspca_ctrl *ctrl;
2160 int i;
2161
2162 for (i = 0, ctrl = gspca_dev->cam.ctrls;
2163 i < gspca_dev->sd_desc->nctrls;
2164 i++, ctrl++) {
2165 ctrl->def = gspca_dev->sd_desc->ctrls[i].qctrl.default_value;
2166 ctrl->val = ctrl->def;
2167 ctrl->min = gspca_dev->sd_desc->ctrls[i].qctrl.minimum;
2168 ctrl->max = gspca_dev->sd_desc->ctrls[i].qctrl.maximum;
2169 }
2170}
2171
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002172/*
2173 * probe and create a new gspca device
2174 *
2175 * This function must be called by the sub-driver when it is
2176 * called for probing a new device.
2177 */
Jean-François Moine44628642010-06-05 07:47:36 -03002178int gspca_dev_probe2(struct usb_interface *intf,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002179 const struct usb_device_id *id,
2180 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002181 int dev_size,
2182 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002183{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002184 struct gspca_dev *gspca_dev;
2185 struct usb_device *dev = interface_to_usbdev(intf);
2186 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002187
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002188 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002189
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002190 /* create the device */
2191 if (dev_size < sizeof *gspca_dev)
2192 dev_size = sizeof *gspca_dev;
2193 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
Erik Andrene293e592008-10-03 08:46:50 -03002194 if (!gspca_dev) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002195 err("couldn't kzalloc gspca struct");
Erik Andrene293e592008-10-03 08:46:50 -03002196 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002197 }
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002198 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2199 if (!gspca_dev->usb_buf) {
2200 err("out of memory");
Erik Andrene293e592008-10-03 08:46:50 -03002201 ret = -ENOMEM;
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002202 goto out;
2203 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002204 gspca_dev->dev = dev;
Jean-François Moine44628642010-06-05 07:47:36 -03002205 gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002206 gspca_dev->nbalt = intf->num_altsetting;
Jean-François Moine35680ba2010-07-14 06:30:18 -03002207
2208 /* check if any audio device */
2209 if (dev->config->desc.bNumInterfaces != 1) {
2210 int i;
2211 struct usb_interface *intf2;
2212
2213 for (i = 0; i < dev->config->desc.bNumInterfaces; i++) {
2214 intf2 = dev->config->interface[i];
2215 if (intf2 != NULL
2216 && intf2->altsetting != NULL
2217 && intf2->altsetting->desc.bInterfaceClass ==
2218 USB_CLASS_AUDIO) {
2219 gspca_dev->audio = 1;
2220 break;
2221 }
2222 }
2223 }
2224
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002225 gspca_dev->sd_desc = sd_desc;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002226 gspca_dev->nbufread = 2;
Jean-Francois Moineff374742008-10-23 07:29:51 -03002227 gspca_dev->empty_packet = -1; /* don't check the empty packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002228
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002229 /* configure the subdriver and initialize the USB device */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002230 ret = sd_desc->config(gspca_dev, id);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002231 if (ret < 0)
2232 goto out;
Jean-François Moine6a330912010-10-02 04:12:25 -03002233 if (gspca_dev->cam.ctrls != NULL)
2234 ctrls_init(gspca_dev);
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002235 ret = sd_desc->init(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002236 if (ret < 0)
2237 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002238 gspca_set_default_mode(gspca_dev);
2239
Hans de Goede10ee2402010-02-19 04:41:40 -03002240 ret = gspca_input_connect(gspca_dev);
2241 if (ret)
2242 goto out;
2243
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002244 mutex_init(&gspca_dev->usb_lock);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002245 mutex_init(&gspca_dev->queue_lock);
2246 init_waitqueue_head(&gspca_dev->wq);
2247
2248 /* init video stuff */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002249 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
Hans de Goede24441632009-03-27 16:08:20 -03002250 gspca_dev->vdev.parent = &intf->dev;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03002251 gspca_dev->module = module;
Oliver Neukumee56a4d2008-07-27 14:01:59 -03002252 gspca_dev->present = 1;
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002253 ret = video_register_device(&gspca_dev->vdev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002254 VFL_TYPE_GRABBER,
Jean-Francois Moine8d584a52009-01-13 06:07:59 -03002255 -1);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002256 if (ret < 0) {
2257 err("video_register_device err %d", ret);
2258 goto out;
2259 }
2260
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002261 usb_set_intfdata(intf, gspca_dev);
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002262 PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
Márton Németh0274d422010-01-28 06:39:49 -03002263
Hans de Goede10ee2402010-02-19 04:41:40 -03002264 gspca_input_create_urb(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002265
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002266 return 0;
2267out:
Randy Dunlapf7628012010-03-30 14:32:04 -03002268#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Hans de Goede10ee2402010-02-19 04:41:40 -03002269 if (gspca_dev->input_dev)
2270 input_unregister_device(gspca_dev->input_dev);
Hans de Goedee7637522010-02-19 05:11:09 -03002271#endif
Jean-Francois Moinee07a1d82008-11-19 06:37:53 -03002272 kfree(gspca_dev->usb_buf);
2273 kfree(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002274 return ret;
2275}
Jean-François Moine44628642010-06-05 07:47:36 -03002276EXPORT_SYMBOL(gspca_dev_probe2);
2277
2278/* same function as the previous one, but check the interface */
2279int gspca_dev_probe(struct usb_interface *intf,
2280 const struct usb_device_id *id,
2281 const struct sd_desc *sd_desc,
2282 int dev_size,
2283 struct module *module)
2284{
2285 struct usb_device *dev = interface_to_usbdev(intf);
2286
2287 /* we don't handle multi-config cameras */
2288 if (dev->descriptor.bNumConfigurations != 1) {
Jean-François Moine0b656322010-09-13 05:19:58 -03002289 err("%04x:%04x too many config",
Jean-François Moine44628642010-06-05 07:47:36 -03002290 id->idVendor, id->idProduct);
2291 return -ENODEV;
2292 }
2293
2294 /* the USB video interface must be the first one */
2295 if (dev->config->desc.bNumInterfaces != 1
Jean-François Moinec4dc6922010-07-14 06:26:54 -03002296 && intf->cur_altsetting->desc.bInterfaceNumber != 0)
Jean-François Moine44628642010-06-05 07:47:36 -03002297 return -ENODEV;
Jean-François Moine44628642010-06-05 07:47:36 -03002298
2299 return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
2300}
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002301EXPORT_SYMBOL(gspca_dev_probe);
2302
2303/*
2304 * USB disconnection
2305 *
2306 * This function must be called by the sub-driver
2307 * when the device disconnects, after the specific resources are freed.
2308 */
2309void gspca_disconnect(struct usb_interface *intf)
2310{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002311 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Randy Dunlapf7628012010-03-30 14:32:04 -03002312#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -03002313 struct input_dev *input_dev;
Jean-François Moinee070bf92010-02-18 15:02:51 -03002314#endif
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002315
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002316 PDEBUG(D_PROBE, "%s disconnect",
2317 video_device_node_name(&gspca_dev->vdev));
Adam Bakerad281272009-02-04 15:33:21 -03002318 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002319
Hans de Goede4e770f72010-12-30 13:11:21 -03002320 gspca_dev->present = 0;
2321 wake_up_interruptible(&gspca_dev->wq);
2322
2323 destroy_urbs(gspca_dev);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002324
Randy Dunlapf7628012010-03-30 14:32:04 -03002325#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -03002326 gspca_input_destroy_urb(gspca_dev);
2327 input_dev = gspca_dev->input_dev;
2328 if (input_dev) {
2329 gspca_dev->input_dev = NULL;
2330 input_unregister_device(input_dev);
2331 }
Jean-François Moinee070bf92010-02-18 15:02:51 -03002332#endif
Márton Németh0274d422010-01-28 06:39:49 -03002333
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002334 /* the device is freed at exit of this function */
2335 gspca_dev->dev = NULL;
Adam Bakerad281272009-02-04 15:33:21 -03002336 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03002337
Frank Zago6b060ff2008-09-28 08:12:22 -03002338 usb_set_intfdata(intf, NULL);
2339
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002340 /* release the device */
2341 /* (this will call gspca_release() immediatly or on last close) */
2342 video_unregister_device(&gspca_dev->vdev);
Jean-Francois Moine3145b8c2008-10-05 04:22:10 -03002343
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002344/* PDEBUG(D_PROBE, "disconnect complete"); */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002345}
2346EXPORT_SYMBOL(gspca_disconnect);
2347
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002348#ifdef CONFIG_PM
2349int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2350{
2351 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2352
2353 if (!gspca_dev->streaming)
2354 return 0;
2355 gspca_dev->frozen = 1; /* avoid urb error messages */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002356 if (gspca_dev->sd_desc->stopN)
2357 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002358 destroy_urbs(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002359 gspca_input_destroy_urb(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002360 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002361 if (gspca_dev->sd_desc->stop0)
2362 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002363 return 0;
2364}
2365EXPORT_SYMBOL(gspca_suspend);
2366
2367int gspca_resume(struct usb_interface *intf)
2368{
2369 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2370
2371 gspca_dev->frozen = 0;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002372 gspca_dev->sd_desc->init(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002373 gspca_input_create_urb(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002374 if (gspca_dev->streaming)
2375 return gspca_init_transfer(gspca_dev);
Jean-Francois Moine1e89e2da2008-09-03 17:12:15 -03002376 return 0;
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002377}
2378EXPORT_SYMBOL(gspca_resume);
2379#endif
Hans de Goededcef32372008-07-10 10:40:53 -03002380/* -- cam driver utility functions -- */
2381
2382/* auto gain and exposure algorithm based on the knee algorithm described here:
2383 http://ytse.tricolour.net/docs/LowLightOptimization.html
2384
2385 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2386 where changed. */
2387int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2388 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2389{
2390 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2391 const struct ctrl *gain_ctrl = NULL;
2392 const struct ctrl *exposure_ctrl = NULL;
2393 const struct ctrl *autogain_ctrl = NULL;
2394 int retval = 0;
2395
2396 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goede26984b02010-02-01 13:18:37 -03002397 if (gspca_dev->ctrl_dis & (1 << i))
2398 continue;
Hans de Goededcef32372008-07-10 10:40:53 -03002399 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2400 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2401 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2402 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2403 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2404 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2405 }
2406 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2407 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2408 "on cam without (auto)gain/exposure");
2409 return 0;
2410 }
2411
2412 if (gain_ctrl->get(gspca_dev, &gain) ||
2413 exposure_ctrl->get(gspca_dev, &exposure) ||
2414 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2415 return 0;
2416
2417 orig_gain = gain;
2418 orig_exposure = exposure;
2419
2420 /* If we are of a multiple of deadzone, do multiple steps to reach the
2421 desired lumination fast (with the risc of a slight overshoot) */
2422 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2423
Jean-Francois Moine1c44d812008-11-10 05:56:55 -03002424 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
Hans de Goededcef32372008-07-10 10:40:53 -03002425 avg_lum, desired_avg_lum, steps);
2426
2427 for (i = 0; i < steps; i++) {
2428 if (avg_lum > desired_avg_lum) {
2429 if (gain > gain_knee)
2430 gain--;
2431 else if (exposure > exposure_knee)
2432 exposure--;
2433 else if (gain > gain_ctrl->qctrl.default_value)
2434 gain--;
2435 else if (exposure > exposure_ctrl->qctrl.minimum)
2436 exposure--;
2437 else if (gain > gain_ctrl->qctrl.minimum)
2438 gain--;
2439 else
2440 break;
2441 } else {
2442 if (gain < gain_ctrl->qctrl.default_value)
2443 gain++;
2444 else if (exposure < exposure_knee)
2445 exposure++;
2446 else if (gain < gain_knee)
2447 gain++;
2448 else if (exposure < exposure_ctrl->qctrl.maximum)
2449 exposure++;
2450 else if (gain < gain_ctrl->qctrl.maximum)
2451 gain++;
2452 else
2453 break;
2454 }
2455 }
2456
2457 if (gain != orig_gain) {
2458 gain_ctrl->set(gspca_dev, gain);
2459 retval = 1;
2460 }
2461 if (exposure != orig_exposure) {
2462 exposure_ctrl->set(gspca_dev, exposure);
2463 retval = 1;
2464 }
2465
2466 return retval;
2467}
2468EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2469
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002470/* -- module insert / remove -- */
2471static int __init gspca_init(void)
2472{
Jean-François Moine54826432010-09-13 04:53:03 -03002473 info("v%d.%d.%d registered",
Jean-Francois Moinebb64e862008-07-19 06:49:28 -03002474 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2475 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2476 DRIVER_VERSION_NUMBER & 0xff);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002477 return 0;
2478}
2479static void __exit gspca_exit(void)
2480{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002481}
2482
2483module_init(gspca_init);
2484module_exit(gspca_exit);
2485
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03002486#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002487module_param_named(debug, gspca_debug, int, 0644);
2488MODULE_PARM_DESC(debug,
2489 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002490 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
2491 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002492#endif