blob: 68b58106e4c8ca28c9c803daefdc531a507183c6 [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 Moine30d45542010-07-26 06:47:35 -030058#define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 10, 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 Dobriyanf0f37e22009-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 gspca_dev->int_urb = urb;
228 ret = usb_submit_urb(urb, GFP_KERNEL);
229 if (ret < 0) {
Jean-François Moinec4dc6922010-07-14 06:26:54 -0300230 PDEBUG(D_ERR, "submit int URB failed with error %i", ret);
Márton Németh0274d422010-01-28 06:39:49 -0300231 goto error_submit;
232 }
233 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++) {
321
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300322 /* check the packet status and length */
323 len = urb->iso_frame_desc[i].actual_length;
Jean-Francois Moineff374742008-10-23 07:29:51 -0300324 if (len == 0) {
325 if (gspca_dev->empty_packet == 0)
326 gspca_dev->empty_packet = 1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300327 continue;
Jean-Francois Moineff374742008-10-23 07:29:51 -0300328 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300329 st = urb->iso_frame_desc[i].status;
330 if (st) {
Jean-François Moine0b656322010-09-13 05:19:58 -0300331 err("ISOC data error: [%d] len=%d, status=%d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300332 i, len, st);
333 gspca_dev->last_packet_type = DISCARD_PACKET;
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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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;
435 return;
436 }
Jean-François Moineb192ca92010-06-27 03:08:19 -0300437 j = gspca_dev->fr_queue[i];
438 frame = &gspca_dev->frame[j];
Paulo Assis2f51d002010-03-07 06:56:56 -0300439 frame->v4l2_buf.timestamp = ktime_to_timeval(ktime_get());
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300440 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300441 gspca_dev->image = frame->data;
Jean-François Moineb192ca92010-06-27 03:08:19 -0300442 gspca_dev->image_len = 0;
Jean-François Moinefe988f52010-07-29 02:46:02 -0300443 } else {
444 switch (gspca_dev->last_packet_type) {
445 case DISCARD_PACKET:
446 if (packet_type == LAST_PACKET)
447 gspca_dev->last_packet_type = packet_type;
448 return;
449 case LAST_PACKET:
450 return;
451 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300452 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300453
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300454 /* append the packet to the frame buffer */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300455 if (len > 0) {
Jean-François Moineb192ca92010-06-27 03:08:19 -0300456 if (gspca_dev->image_len + len > gspca_dev->frsz) {
457 PDEBUG(D_ERR|D_PACK, "frame overflow %d > %d",
458 gspca_dev->image_len + len,
459 gspca_dev->frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300460 packet_type = DISCARD_PACKET;
461 } else {
Jean-François Moinefe988f52010-07-29 02:46:02 -0300462/* !! image is NULL only when last pkt is LAST or DISCARD
463 if (gspca_dev->image == NULL) {
464 err("gspca_frame_add() image == NULL");
465 return;
466 }
467 */
Jean-François Moineb192ca92010-06-27 03:08:19 -0300468 memcpy(gspca_dev->image + gspca_dev->image_len,
469 data, len);
470 gspca_dev->image_len += len;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300471 }
472 }
473 gspca_dev->last_packet_type = packet_type;
474
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300475 /* if last packet, invalidate packet concatenation until
476 * next first packet, wake up the application and advance
477 * in the queue */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300478 if (packet_type == LAST_PACKET) {
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300479 i = atomic_read(&gspca_dev->fr_i);
Jean-François Moineb192ca92010-06-27 03:08:19 -0300480 j = gspca_dev->fr_queue[i];
481 frame = &gspca_dev->frame[j];
482 frame->v4l2_buf.bytesused = gspca_dev->image_len;
Jean-François Moine02bbcb92010-07-06 04:16:40 -0300483 frame->v4l2_buf.flags = (frame->v4l2_buf.flags
484 | V4L2_BUF_FLAG_DONE)
485 & ~V4L2_BUF_FLAG_QUEUED;
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300486 i = (i + 1) % GSPCA_MAX_FRAMES;
487 atomic_set(&gspca_dev->fr_i, i);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300488 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300489 PDEBUG(D_FRAM, "frame complete len:%d",
490 frame->v4l2_buf.bytesused);
491 gspca_dev->image = NULL;
492 gspca_dev->image_len = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300493 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300494}
495EXPORT_SYMBOL(gspca_frame_add);
496
497static int gspca_is_compressed(__u32 format)
498{
499 switch (format) {
500 case V4L2_PIX_FMT_MJPEG:
501 case V4L2_PIX_FMT_JPEG:
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300502 case V4L2_PIX_FMT_SPCA561:
Hans de Goedeab8f12c2008-07-04 18:29:32 -0300503 case V4L2_PIX_FMT_PAC207:
Kyle Guinnd661e622009-01-16 05:36:14 -0300504 case V4L2_PIX_FMT_MR97310A:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300505 return 1;
506 }
507 return 0;
508}
509
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300510static int frame_alloc(struct gspca_dev *gspca_dev,
511 unsigned int count)
512{
513 struct gspca_frame *frame;
514 unsigned int frsz;
515 int i;
516
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300517 i = gspca_dev->curr_mode;
518 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300519 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300520 frsz = PAGE_ALIGN(frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300521 gspca_dev->frsz = 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 }
529 gspca_dev->nframes = count;
530 for (i = 0; i < count; i++) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300531 frame = &gspca_dev->frame[i];
532 frame->v4l2_buf.index = i;
533 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
534 frame->v4l2_buf.flags = 0;
535 frame->v4l2_buf.field = V4L2_FIELD_NONE;
536 frame->v4l2_buf.length = frsz;
537 frame->v4l2_buf.memory = gspca_dev->memory;
538 frame->v4l2_buf.sequence = 0;
Jean-François Moineb192ca92010-06-27 03:08:19 -0300539 frame->data = gspca_dev->frbuf + i * frsz;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300540 frame->v4l2_buf.m.offset = i * frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300541 }
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300542 atomic_set(&gspca_dev->fr_q, 0);
543 atomic_set(&gspca_dev->fr_i, 0);
544 gspca_dev->fr_o = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300545 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300546}
547
548static void frame_free(struct gspca_dev *gspca_dev)
549{
550 int i;
551
552 PDEBUG(D_STREAM, "frame free");
Harvey Harrisonefab8212008-07-05 06:12:47 -0300553 if (gspca_dev->frbuf != NULL) {
Jean-François Moinee795d912010-06-24 05:14:22 -0300554 vfree(gspca_dev->frbuf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300555 gspca_dev->frbuf = NULL;
556 for (i = 0; i < gspca_dev->nframes; i++)
557 gspca_dev->frame[i].data = NULL;
558 }
559 gspca_dev->nframes = 0;
560}
561
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300562static void destroy_urbs(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300563{
564 struct urb *urb;
565 unsigned int i;
566
567 PDEBUG(D_STREAM, "kill transfer");
Erik Andrén44732812008-10-13 15:52:46 -0300568 for (i = 0; i < MAX_NURBS; i++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300569 urb = gspca_dev->urb[i];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300570 if (urb == NULL)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300571 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300572
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300573 gspca_dev->urb[i] = NULL;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300574 usb_kill_urb(urb);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300575 if (urb->transfer_buffer != NULL)
Daniel Mack997ea582010-04-12 13:17:25 +0200576 usb_free_coherent(gspca_dev->dev,
577 urb->transfer_buffer_length,
578 urb->transfer_buffer,
579 urb->transfer_dma);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300580 usb_free_urb(urb);
581 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300582}
583
Jean-François Moinee4dac282010-04-09 06:11:36 -0300584static int gspca_set_alt0(struct gspca_dev *gspca_dev)
585{
586 int ret;
587
588 if (gspca_dev->alt == 0)
589 return 0;
590 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
591 if (ret < 0)
Jean-François Moine0b656322010-09-13 05:19:58 -0300592 err("set alt 0 err %d", ret);
Jean-François Moinee4dac282010-04-09 06:11:36 -0300593 return ret;
594}
595
596/* Note: both the queue and the usb locks should be held when calling this */
597static void gspca_stream_off(struct gspca_dev *gspca_dev)
598{
599 gspca_dev->streaming = 0;
600 if (gspca_dev->present) {
601 if (gspca_dev->sd_desc->stopN)
602 gspca_dev->sd_desc->stopN(gspca_dev);
603 destroy_urbs(gspca_dev);
604 gspca_input_destroy_urb(gspca_dev);
605 gspca_set_alt0(gspca_dev);
606 gspca_input_create_urb(gspca_dev);
607 }
608
609 /* always call stop0 to free the subdriver's resources */
610 if (gspca_dev->sd_desc->stop0)
611 gspca_dev->sd_desc->stop0(gspca_dev);
612 PDEBUG(D_STREAM, "stream off OK");
613}
614
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300615/*
Frank Zago219423c2008-09-26 07:43:54 -0300616 * look for an input transfer endpoint in an alternate setting
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300617 */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300618static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300619 int xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300620{
621 struct usb_host_endpoint *ep;
622 int i, attr;
623
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300624 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
625 ep = &alt->endpoint[i];
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300626 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Jean-Francois Moine30d35e42009-04-21 13:57:31 -0300627 if (attr == xfer
628 && ep->desc.wMaxPacketSize != 0)
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300629 return ep;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300630 }
631 return NULL;
632}
633
634/*
Frank Zago219423c2008-09-26 07:43:54 -0300635 * look for an input (isoc or bulk) endpoint
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300636 *
637 * The endpoint is defined by the subdriver.
638 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
639 * This routine may be called many times when the bandwidth is too small
640 * (the bandwidth is checked on urb submit).
641 */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300642static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300643{
644 struct usb_interface *intf;
645 struct usb_host_endpoint *ep;
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300646 int xfer, i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300647
648 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300649 ep = NULL;
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300650 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
651 : USB_ENDPOINT_XFER_ISOC;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300652 i = gspca_dev->alt; /* previous alt setting */
Hans de Goedea511ba92009-10-16 07:13:07 -0300653 if (gspca_dev->cam.reverse_alts) {
Jean-François Moine35680ba2010-07-14 06:30:18 -0300654 if (gspca_dev->audio)
655 i++;
Hans de Goedea511ba92009-10-16 07:13:07 -0300656 while (++i < gspca_dev->nbalt) {
657 ep = alt_xfer(&intf->altsetting[i], xfer);
658 if (ep)
659 break;
660 }
661 } else {
Jean-François Moine35680ba2010-07-14 06:30:18 -0300662 if (gspca_dev->audio)
663 i--;
Hans de Goedea511ba92009-10-16 07:13:07 -0300664 while (--i >= 0) {
665 ep = alt_xfer(&intf->altsetting[i], xfer);
666 if (ep)
667 break;
668 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300669 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300670 if (ep == NULL) {
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300671 err("no transfer endpoint found");
672 return NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300673 }
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300674 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300675 i, ep->desc.bEndpointAddress);
Jean-Francois Moine1c8336d2009-07-28 04:38:25 -0300676 gspca_dev->alt = i; /* memorize the current alt setting */
Jean-Francois Moine8dd07ef2009-04-21 14:05:44 -0300677 if (gspca_dev->nbalt > 1) {
Márton Németh0274d422010-01-28 06:39:49 -0300678 gspca_input_destroy_urb(gspca_dev);
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300679 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
680 if (ret < 0) {
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300681 err("set alt %d err %d", i, ret);
Márton Németh0274d422010-01-28 06:39:49 -0300682 ep = NULL;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300683 }
Márton Németh0274d422010-01-28 06:39:49 -0300684 gspca_input_create_urb(gspca_dev);
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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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,
761 ep->desc.bEndpointAddress),
Frank Zago6c49da72008-09-28 07:43:00 -0300762 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be01002008-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;
783 goto out;
784 }
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)
804 goto out;
805 }
806 ep = get_ep(gspca_dev);
807 if (ep == NULL) {
808 ret = -EIO;
809 goto out;
810 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300811 for (;;) {
Jean-Francois Moine47aaca92009-12-15 05:23:04 -0300812 if (!gspca_dev->cam.no_urb_create) {
813 PDEBUG(D_STREAM, "init transfer alt %d",
814 gspca_dev->alt);
815 ret = create_urbs(gspca_dev, ep);
816 if (ret < 0) {
817 destroy_urbs(gspca_dev);
818 goto out;
819 }
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300820 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300821
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300822 /* clear the bulk endpoint */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300823 if (gspca_dev->cam.bulk)
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300824 usb_clear_halt(gspca_dev->dev,
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300825 gspca_dev->urb[0]->pipe);
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300826
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300827 /* start the cam */
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300828 ret = gspca_dev->sd_desc->start(gspca_dev);
829 if (ret < 0) {
830 destroy_urbs(gspca_dev);
831 goto out;
832 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300833 gspca_dev->streaming = 1;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300834
Antonio Ospitedff369a2008-11-14 07:53:32 -0300835 /* some bulk transfers are started by the subdriver */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300836 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300837 break;
838
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300839 /* submit the URBs */
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300840 for (n = 0; n < MAX_NURBS; n++) {
841 urb = gspca_dev->urb[n];
842 if (urb == NULL)
843 break;
844 ret = usb_submit_urb(urb, GFP_KERNEL);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300845 if (ret < 0)
846 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300847 }
848 if (ret >= 0)
849 break;
Jean-François Moinee4dac282010-04-09 06:11:36 -0300850 gspca_stream_off(gspca_dev);
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300851 if (ret != -ENOSPC) {
Jean-François Moine0b656322010-09-13 05:19:58 -0300852 err("usb_submit_urb alt %d err %d",
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300853 gspca_dev->alt, ret);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300854 goto out;
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300855 }
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300856
857 /* the bandwidth is not wide enough
858 * negociate or try a lower alternate setting */
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300859 PDEBUG(D_ERR|D_STREAM,
860 "bandwidth not wide enough - trying again");
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300861 msleep(20); /* wait for kill complete */
862 if (gspca_dev->sd_desc->isoc_nego) {
863 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
864 if (ret < 0)
865 goto out;
866 } else {
867 ep = get_ep(gspca_dev);
868 if (ep == NULL) {
869 ret = -EIO;
870 goto out;
871 }
872 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300873 }
874out:
875 mutex_unlock(&gspca_dev->usb_lock);
876 return ret;
877}
878
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300879static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300880{
Jean-François Moine6a330912010-10-02 04:12:25 -0300881 struct gspca_ctrl *ctrl;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300882 int i;
883
884 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
885 gspca_dev->curr_mode = i;
886 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
887 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300888 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-François Moine6a330912010-10-02 04:12:25 -0300889
890 /* set the current control values to their default values
891 * which may have changed in sd_init() */
892 ctrl = gspca_dev->cam.ctrls;
893 if (ctrl != NULL) {
894 for (i = 0;
895 i < gspca_dev->sd_desc->nctrls;
896 i++, ctrl++)
897 ctrl->val = ctrl->def;
898 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300899}
900
901static int wxh_to_mode(struct gspca_dev *gspca_dev,
902 int width, int height)
903{
904 int i;
905
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300906 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
907 if (width >= gspca_dev->cam.cam_mode[i].width
908 && height >= gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300909 break;
910 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300911 return i;
912}
913
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300914/*
915 * search a mode with the right pixel format
916 */
917static int gspca_get_mode(struct gspca_dev *gspca_dev,
918 int mode,
919 int pixfmt)
920{
921 int modeU, modeD;
922
923 modeU = modeD = mode;
924 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
925 if (--modeD >= 0) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300926 if (gspca_dev->cam.cam_mode[modeD].pixelformat
927 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300928 return modeD;
929 }
930 if (++modeU < gspca_dev->cam.nmodes) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300931 if (gspca_dev->cam.cam_mode[modeU].pixelformat
932 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300933 return modeU;
934 }
935 }
936 return -EINVAL;
937}
938
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300939#ifdef CONFIG_VIDEO_ADV_DEBUG
940static int vidioc_g_register(struct file *file, void *priv,
941 struct v4l2_dbg_register *reg)
942{
943 int ret;
944 struct gspca_dev *gspca_dev = priv;
945
946 if (!gspca_dev->sd_desc->get_chip_ident)
947 return -EINVAL;
948
949 if (!gspca_dev->sd_desc->get_register)
950 return -EINVAL;
951
952 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
953 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -0300954 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300955 if (gspca_dev->present)
956 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
957 else
958 ret = -ENODEV;
959 mutex_unlock(&gspca_dev->usb_lock);
960
961 return ret;
962}
963
964static int vidioc_s_register(struct file *file, void *priv,
965 struct v4l2_dbg_register *reg)
966{
967 int ret;
968 struct gspca_dev *gspca_dev = priv;
969
970 if (!gspca_dev->sd_desc->get_chip_ident)
971 return -EINVAL;
972
973 if (!gspca_dev->sd_desc->set_register)
974 return -EINVAL;
975
976 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
977 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -0300978 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300979 if (gspca_dev->present)
980 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
981 else
982 ret = -ENODEV;
983 mutex_unlock(&gspca_dev->usb_lock);
984
985 return ret;
986}
987#endif
988
989static int vidioc_g_chip_ident(struct file *file, void *priv,
990 struct v4l2_dbg_chip_ident *chip)
991{
992 int ret;
993 struct gspca_dev *gspca_dev = priv;
994
995 if (!gspca_dev->sd_desc->get_chip_ident)
996 return -EINVAL;
997
998 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
999 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001000 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03001001 if (gspca_dev->present)
1002 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
1003 else
1004 ret = -ENODEV;
1005 mutex_unlock(&gspca_dev->usb_lock);
1006
1007 return ret;
1008}
1009
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001010static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001011 struct v4l2_fmtdesc *fmtdesc)
1012{
1013 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001014 int i, j, index;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001015 __u32 fmt_tb[8];
1016
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001017 /* give an index to each format */
1018 index = 0;
1019 j = 0;
1020 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001021 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001022 j = 0;
1023 for (;;) {
1024 if (fmt_tb[j] == fmt_tb[index])
1025 break;
1026 j++;
1027 }
1028 if (j == index) {
1029 if (fmtdesc->index == index)
1030 break; /* new format */
1031 index++;
Julia Lawall80297122008-11-12 23:18:21 -03001032 if (index >= ARRAY_SIZE(fmt_tb))
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001033 return -EINVAL;
1034 }
1035 }
1036 if (i < 0)
1037 return -EINVAL; /* no more format */
1038
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001039 fmtdesc->pixelformat = fmt_tb[index];
1040 if (gspca_is_compressed(fmt_tb[index]))
1041 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
1042 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
1043 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
1044 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
1045 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
1046 fmtdesc->description[4] = '\0';
1047 return 0;
1048}
1049
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001050static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001051 struct v4l2_format *fmt)
1052{
1053 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001054 int mode;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001055
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001056 mode = gspca_dev->curr_mode;
1057 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1058 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001059 return 0;
1060}
1061
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001062static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001063 struct v4l2_format *fmt)
1064{
Hans de Goede24c530b2008-07-04 17:55:18 -03001065 int w, h, mode, mode2;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001066
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001067 w = fmt->fmt.pix.width;
1068 h = fmt->fmt.pix.height;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001069
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001070#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001071 if (gspca_debug & D_CONF)
1072 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
1073#endif
1074 /* search the closest mode for width and height */
1075 mode = wxh_to_mode(gspca_dev, w, h);
1076
1077 /* OK if right palette */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001078 if (gspca_dev->cam.cam_mode[mode].pixelformat
1079 != fmt->fmt.pix.pixelformat) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001080
1081 /* else, search the closest mode with the same pixel format */
1082 mode2 = gspca_get_mode(gspca_dev, mode,
1083 fmt->fmt.pix.pixelformat);
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001084 if (mode2 >= 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001085 mode = mode2;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001086/* else
1087 ; * no chance, return this mode */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001088 }
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001089 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1090 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001091 return mode; /* used when s_fmt */
1092}
1093
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001094static int vidioc_try_fmt_vid_cap(struct file *file,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001095 void *priv,
1096 struct v4l2_format *fmt)
1097{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001098 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001099 int ret;
1100
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001101 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001102 if (ret < 0)
1103 return ret;
1104 return 0;
1105}
1106
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001107static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001108 struct v4l2_format *fmt)
1109{
1110 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001111 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001112
Hans de Goedee2997a72008-04-23 08:09:12 -03001113 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1114 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001115
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001116 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001117 if (ret < 0)
1118 goto out;
1119
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001120 if (gspca_dev->nframes != 0
1121 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
1122 ret = -EINVAL;
1123 goto out;
1124 }
1125
Jean-Francois Moine50a871f2008-06-30 19:47:33 -03001126 if (ret == gspca_dev->curr_mode) {
1127 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001128 goto out; /* same mode */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -03001129 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001130
1131 if (gspca_dev->streaming) {
1132 ret = -EBUSY;
1133 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -03001134 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001135 gspca_dev->width = fmt->fmt.pix.width;
1136 gspca_dev->height = fmt->fmt.pix.height;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001137 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
1138 gspca_dev->curr_mode = ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001139
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001140 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001141out:
1142 mutex_unlock(&gspca_dev->queue_lock);
1143 return ret;
1144}
1145
Hans de Goede11c635a2009-05-25 15:04:22 -03001146static int vidioc_enum_framesizes(struct file *file, void *priv,
1147 struct v4l2_frmsizeenum *fsize)
1148{
1149 struct gspca_dev *gspca_dev = priv;
1150 int i;
1151 __u32 index = 0;
1152
1153 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
1154 if (fsize->pixel_format !=
1155 gspca_dev->cam.cam_mode[i].pixelformat)
1156 continue;
1157
1158 if (fsize->index == index) {
1159 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1160 fsize->discrete.width =
1161 gspca_dev->cam.cam_mode[i].width;
1162 fsize->discrete.height =
1163 gspca_dev->cam.cam_mode[i].height;
1164 return 0;
1165 }
1166 index++;
1167 }
1168
1169 return -EINVAL;
1170}
1171
Antonio Ospite28ffe772009-12-02 06:18:46 -03001172static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1173 struct v4l2_frmivalenum *fival)
1174{
1175 struct gspca_dev *gspca_dev = priv;
1176 int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
1177 __u32 i;
1178
1179 if (gspca_dev->cam.mode_framerates == NULL ||
1180 gspca_dev->cam.mode_framerates[mode].nrates == 0)
1181 return -EINVAL;
1182
1183 if (fival->pixel_format !=
1184 gspca_dev->cam.cam_mode[mode].pixelformat)
1185 return -EINVAL;
1186
1187 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1188 if (fival->index == i) {
1189 fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1190 fival->discrete.numerator = 1;
1191 fival->discrete.denominator =
1192 gspca_dev->cam.mode_framerates[mode].rates[i];
1193 return 0;
1194 }
1195 }
1196
1197 return -EINVAL;
1198}
1199
Hans de Goeded0182302008-11-19 17:10:47 -03001200static void gspca_release(struct video_device *vfd)
Frank Zago6b060ff2008-09-28 08:12:22 -03001201{
Hans de Goeded0182302008-11-19 17:10:47 -03001202 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
Frank Zago6b060ff2008-09-28 08:12:22 -03001203
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001204 PDEBUG(D_PROBE, "%s released",
1205 video_device_node_name(&gspca_dev->vdev));
Frank Zago6b060ff2008-09-28 08:12:22 -03001206
1207 kfree(gspca_dev->usb_buf);
1208 kfree(gspca_dev);
1209}
1210
Hans Verkuilbec43662008-12-30 06:58:20 -03001211static int dev_open(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001212{
1213 struct gspca_dev *gspca_dev;
1214 int ret;
1215
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001216 PDEBUG(D_STREAM, "[%s] open", current->comm);
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001217 gspca_dev = (struct gspca_dev *) video_devdata(file);
Hans de Goedee2997a72008-04-23 08:09:12 -03001218 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1219 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001220 if (!gspca_dev->present) {
1221 ret = -ENODEV;
1222 goto out;
1223 }
1224
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001225 if (gspca_dev->users > 4) { /* (arbitrary value) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001226 ret = -EBUSY;
1227 goto out;
1228 }
Jean-Francois Moine5c4fa002008-11-18 15:52:31 -03001229
1230 /* protect the subdriver against rmmod */
1231 if (!try_module_get(gspca_dev->module)) {
1232 ret = -ENODEV;
1233 goto out;
1234 }
1235
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001236 gspca_dev->users++;
Frank Zago6b060ff2008-09-28 08:12:22 -03001237
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001238 file->private_data = gspca_dev;
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001239#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001240 /* activate the v4l2 debug */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001241 if (gspca_debug & D_V4L2)
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001242 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001243 | V4L2_DEBUG_IOCTL_ARG;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001244 else
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001245 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001246 | V4L2_DEBUG_IOCTL_ARG);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001247#endif
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001248 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001249out:
1250 mutex_unlock(&gspca_dev->queue_lock);
1251 if (ret != 0)
1252 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
1253 else
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001254 PDEBUG(D_STREAM, "open done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001255 return ret;
1256}
1257
Hans Verkuilbec43662008-12-30 06:58:20 -03001258static int dev_close(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001259{
1260 struct gspca_dev *gspca_dev = file->private_data;
1261
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001262 PDEBUG(D_STREAM, "[%s] close", current->comm);
Hans de Goedee2997a72008-04-23 08:09:12 -03001263 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1264 return -ERESTARTSYS;
1265 gspca_dev->users--;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001266
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001267 /* if the file did the capture, free the streaming resources */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001268 if (gspca_dev->capt_file == file) {
Jean-Francois Moinea1e82882008-09-03 17:12:17 -03001269 if (gspca_dev->streaming) {
1270 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001271 gspca_dev->usb_err = 0;
Jean-Francois Moinea1e82882008-09-03 17:12:17 -03001272 gspca_stream_off(gspca_dev);
1273 mutex_unlock(&gspca_dev->usb_lock);
1274 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001275 frame_free(gspca_dev);
Harvey Harrisonefab8212008-07-05 06:12:47 -03001276 gspca_dev->capt_file = NULL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001277 gspca_dev->memory = GSPCA_MEMORY_NO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001278 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001279 file->private_data = NULL;
Jean-Francois Moine5c4fa002008-11-18 15:52:31 -03001280 module_put(gspca_dev->module);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001281 mutex_unlock(&gspca_dev->queue_lock);
Frank Zago6b060ff2008-09-28 08:12:22 -03001282
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001283 PDEBUG(D_STREAM, "close done");
Frank Zago6b060ff2008-09-28 08:12:22 -03001284
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001285 return 0;
1286}
1287
1288static int vidioc_querycap(struct file *file, void *priv,
1289 struct v4l2_capability *cap)
1290{
1291 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001292 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001293
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001294 /* protect the access to the usb device */
1295 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1296 return -ERESTARTSYS;
1297 if (!gspca_dev->present) {
1298 ret = -ENODEV;
1299 goto out;
1300 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001301 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -03001302 if (gspca_dev->dev->product != NULL) {
1303 strncpy(cap->card, gspca_dev->dev->product,
1304 sizeof cap->card);
1305 } else {
1306 snprintf(cap->card, sizeof cap->card,
1307 "USB Camera (%04x:%04x)",
1308 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1309 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1310 }
Thierry MERLE59320282009-01-21 15:32:26 -03001311 usb_make_path(gspca_dev->dev, cap->bus_info, sizeof(cap->bus_info));
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001312 cap->version = DRIVER_VERSION_NUMBER;
1313 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1314 | V4L2_CAP_STREAMING
1315 | V4L2_CAP_READWRITE;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001316 ret = 0;
1317out:
1318 mutex_unlock(&gspca_dev->usb_lock);
1319 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001320}
1321
Jean-François Moine6a330912010-10-02 04:12:25 -03001322static int get_ctrl(struct gspca_dev *gspca_dev,
Hans de Goedea0001a22009-06-10 04:52:18 -03001323 int id)
1324{
1325 const struct ctrl *ctrls;
1326 int i;
1327
1328 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1329 i < gspca_dev->sd_desc->nctrls;
1330 i++, ctrls++) {
1331 if (gspca_dev->ctrl_dis & (1 << i))
1332 continue;
1333 if (id == ctrls->qctrl.id)
Jean-François Moine6a330912010-10-02 04:12:25 -03001334 return i;
Hans de Goedea0001a22009-06-10 04:52:18 -03001335 }
Jean-François Moine6a330912010-10-02 04:12:25 -03001336 return -1;
Hans de Goedea0001a22009-06-10 04:52:18 -03001337}
1338
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001339static int vidioc_queryctrl(struct file *file, void *priv,
1340 struct v4l2_queryctrl *q_ctrl)
1341{
1342 struct gspca_dev *gspca_dev = priv;
Hans de Goedea0001a22009-06-10 04:52:18 -03001343 const struct ctrl *ctrls;
Jean-François Moine6a330912010-10-02 04:12:25 -03001344 struct gspca_ctrl *gspca_ctrl;
1345 int i, idx;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001346 u32 id;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001347
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001348 id = q_ctrl->id;
1349 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1350 id &= V4L2_CTRL_ID_MASK;
1351 id++;
Jean-François Moine6a330912010-10-02 04:12:25 -03001352 idx = -1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001353 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001354 if (gspca_dev->ctrl_dis & (1 << i))
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001355 continue;
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001356 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001357 continue;
Jean-François Moine6a330912010-10-02 04:12:25 -03001358 if (idx >= 0
1359 && gspca_dev->sd_desc->ctrls[i].qctrl.id
1360 > gspca_dev->sd_desc->ctrls[idx].qctrl.id)
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001361 continue;
Jean-François Moine6a330912010-10-02 04:12:25 -03001362 idx = i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001363 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001364 } else {
Jean-François Moine6a330912010-10-02 04:12:25 -03001365 idx = get_ctrl(gspca_dev, id);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001366 }
Jean-François Moine6a330912010-10-02 04:12:25 -03001367 if (idx < 0)
1368 return -EINVAL;
1369 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1370 memcpy(q_ctrl, &ctrls->qctrl, sizeof *q_ctrl);
1371 if (gspca_dev->cam.ctrls != NULL) {
1372 gspca_ctrl = &gspca_dev->cam.ctrls[idx];
1373 q_ctrl->default_value = gspca_ctrl->def;
1374 q_ctrl->minimum = gspca_ctrl->min;
1375 q_ctrl->maximum = gspca_ctrl->max;
1376 }
1377 if (gspca_dev->ctrl_inac & (1 << idx))
Jean-Francois Moine4af85662009-11-12 15:59:27 -03001378 q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001379 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001380}
1381
1382static int vidioc_s_ctrl(struct file *file, void *priv,
1383 struct v4l2_control *ctrl)
1384{
1385 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001386 const struct ctrl *ctrls;
Jean-François Moine6a330912010-10-02 04:12:25 -03001387 struct gspca_ctrl *gspca_ctrl;
1388 int idx, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001389
Jean-François Moine6a330912010-10-02 04:12:25 -03001390 idx = get_ctrl(gspca_dev, ctrl->id);
1391 if (idx < 0)
Hans de Goedea0001a22009-06-10 04:52:18 -03001392 return -EINVAL;
Jean-François Moine6a330912010-10-02 04:12:25 -03001393 if (gspca_dev->ctrl_inac & (1 << idx))
1394 return -EINVAL;
1395 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1396 if (gspca_dev->cam.ctrls != NULL) {
1397 gspca_ctrl = &gspca_dev->cam.ctrls[idx];
1398 if (ctrl->value < gspca_ctrl->min
1399 || ctrl->value > gspca_ctrl->max)
1400 return -ERANGE;
1401 } else {
1402 gspca_ctrl = NULL;
1403 if (ctrl->value < ctrls->qctrl.minimum
1404 || ctrl->value > ctrls->qctrl.maximum)
1405 return -ERANGE;
1406 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001407 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1408 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1409 return -ERESTARTSYS;
Jean-François Moine6a330912010-10-02 04:12:25 -03001410 if (!gspca_dev->present) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001411 ret = -ENODEV;
Jean-François Moine6a330912010-10-02 04:12:25 -03001412 goto out;
1413 }
1414 gspca_dev->usb_err = 0;
1415 if (ctrls->set != NULL) {
1416 ret = ctrls->set(gspca_dev, ctrl->value);
1417 goto out;
1418 }
1419 if (gspca_ctrl != NULL) {
1420 gspca_ctrl->val = ctrl->value;
1421 if (ctrls->set_control != NULL
1422 && gspca_dev->streaming)
1423 ctrls->set_control(gspca_dev);
1424 }
1425 ret = gspca_dev->usb_err;
1426out:
Hans de Goedea0001a22009-06-10 04:52:18 -03001427 mutex_unlock(&gspca_dev->usb_lock);
1428 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001429}
1430
1431static int vidioc_g_ctrl(struct file *file, void *priv,
1432 struct v4l2_control *ctrl)
1433{
1434 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001435 const struct ctrl *ctrls;
Jean-François Moine6a330912010-10-02 04:12:25 -03001436 int idx, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001437
Jean-François Moine6a330912010-10-02 04:12:25 -03001438 idx = get_ctrl(gspca_dev, ctrl->id);
1439 if (idx < 0)
Hans de Goedea0001a22009-06-10 04:52:18 -03001440 return -EINVAL;
Jean-François Moine6a330912010-10-02 04:12:25 -03001441 ctrls = &gspca_dev->sd_desc->ctrls[idx];
Hans de Goedea0001a22009-06-10 04:52:18 -03001442
1443 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1444 return -ERESTARTSYS;
Jean-François Moine6a330912010-10-02 04:12:25 -03001445 if (!gspca_dev->present) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001446 ret = -ENODEV;
Jean-François Moine6a330912010-10-02 04:12:25 -03001447 goto out;
1448 }
1449 gspca_dev->usb_err = 0;
1450 if (ctrls->get != NULL) {
1451 ret = ctrls->get(gspca_dev, &ctrl->value);
1452 goto out;
1453 }
1454 if (gspca_dev->cam.ctrls != NULL)
1455 ctrl->value = gspca_dev->cam.ctrls[idx].val;
1456 ret = 0;
1457out:
Hans de Goedea0001a22009-06-10 04:52:18 -03001458 mutex_unlock(&gspca_dev->usb_lock);
1459 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001460}
1461
1462static int vidioc_querymenu(struct file *file, void *priv,
1463 struct v4l2_querymenu *qmenu)
1464{
1465 struct gspca_dev *gspca_dev = priv;
1466
1467 if (!gspca_dev->sd_desc->querymenu)
1468 return -EINVAL;
1469 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1470}
1471
1472static int vidioc_enum_input(struct file *file, void *priv,
1473 struct v4l2_input *input)
1474{
1475 struct gspca_dev *gspca_dev = priv;
1476
1477 if (input->index != 0)
1478 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001479 input->type = V4L2_INPUT_TYPE_CAMERA;
Adam Bakerdfa76fa2009-03-29 19:17:10 -03001480 input->status = gspca_dev->cam.input_flags;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001481 strncpy(input->name, gspca_dev->sd_desc->name,
1482 sizeof input->name);
1483 return 0;
1484}
1485
1486static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1487{
1488 *i = 0;
1489 return 0;
1490}
1491
1492static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1493{
1494 if (i > 0)
1495 return -EINVAL;
1496 return (0);
1497}
1498
1499static int vidioc_reqbufs(struct file *file, void *priv,
1500 struct v4l2_requestbuffers *rb)
1501{
1502 struct gspca_dev *gspca_dev = priv;
Jean-François Moine832d0a92010-04-09 06:07:58 -03001503 int i, ret = 0, streaming;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001504
Jean-François Moinea2ba6f22010-07-14 06:21:16 -03001505 i = rb->memory; /* (avoid compilation warning) */
1506 switch (i) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001507 case GSPCA_MEMORY_READ: /* (internal call) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001508 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001509 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001510 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001511 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001512 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001513 }
Hans de Goedee2997a72008-04-23 08:09:12 -03001514 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1515 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001516
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001517 if (gspca_dev->memory != GSPCA_MEMORY_NO
1518 && gspca_dev->memory != rb->memory) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001519 ret = -EBUSY;
1520 goto out;
1521 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001522
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001523 /* only one file may do the capture */
1524 if (gspca_dev->capt_file != NULL
1525 && gspca_dev->capt_file != file) {
1526 ret = -EBUSY;
1527 goto out;
1528 }
1529
1530 /* if allocated, the buffers must not be mapped */
1531 for (i = 0; i < gspca_dev->nframes; i++) {
1532 if (gspca_dev->frame[i].vma_use_count) {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001533 ret = -EBUSY;
1534 goto out;
1535 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001536 }
1537
1538 /* stop streaming */
Jean-François Moine832d0a92010-04-09 06:07:58 -03001539 streaming = gspca_dev->streaming;
1540 if (streaming) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001541 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001542 gspca_dev->usb_err = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001543 gspca_stream_off(gspca_dev);
1544 mutex_unlock(&gspca_dev->usb_lock);
1545 }
1546
1547 /* free the previous allocated buffers, if any */
1548 if (gspca_dev->nframes != 0) {
1549 frame_free(gspca_dev);
1550 gspca_dev->capt_file = NULL;
1551 }
1552 if (rb->count == 0) /* unrequest */
1553 goto out;
1554 gspca_dev->memory = rb->memory;
1555 ret = frame_alloc(gspca_dev, rb->count);
1556 if (ret == 0) {
1557 rb->count = gspca_dev->nframes;
1558 gspca_dev->capt_file = file;
Jean-François Moine832d0a92010-04-09 06:07:58 -03001559 if (streaming)
1560 ret = gspca_init_transfer(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001561 }
1562out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001563 mutex_unlock(&gspca_dev->queue_lock);
1564 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1565 return ret;
1566}
1567
1568static int vidioc_querybuf(struct file *file, void *priv,
1569 struct v4l2_buffer *v4l2_buf)
1570{
1571 struct gspca_dev *gspca_dev = priv;
1572 struct gspca_frame *frame;
1573
Jean-Francois Moine13752bd2008-12-19 15:08:51 -03001574 if (v4l2_buf->index < 0
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001575 || v4l2_buf->index >= gspca_dev->nframes)
1576 return -EINVAL;
1577
1578 frame = &gspca_dev->frame[v4l2_buf->index];
1579 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1580 return 0;
1581}
1582
1583static int vidioc_streamon(struct file *file, void *priv,
1584 enum v4l2_buf_type buf_type)
1585{
1586 struct gspca_dev *gspca_dev = priv;
1587 int ret;
1588
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001589 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1590 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001591 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1592 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001593
Jean-François Moine18bf99a2010-04-09 06:06:11 -03001594 /* check the capture file */
1595 if (gspca_dev->capt_file != file) {
1596 ret = -EBUSY;
1597 goto out;
1598 }
1599
Jean-Francois Moine1edabe72008-12-19 15:17:25 -03001600 if (gspca_dev->nframes == 0
1601 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001602 ret = -EINVAL;
1603 goto out;
1604 }
1605 if (!gspca_dev->streaming) {
1606 ret = gspca_init_transfer(gspca_dev);
1607 if (ret < 0)
1608 goto out;
1609 }
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001610#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001611 if (gspca_debug & D_STREAM) {
1612 PDEBUG_MODE("stream on OK",
1613 gspca_dev->pixfmt,
1614 gspca_dev->width,
1615 gspca_dev->height);
1616 }
1617#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001618 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001619out:
1620 mutex_unlock(&gspca_dev->queue_lock);
1621 return ret;
1622}
1623
1624static int vidioc_streamoff(struct file *file, void *priv,
1625 enum v4l2_buf_type buf_type)
1626{
1627 struct gspca_dev *gspca_dev = priv;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001628 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001629
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001630 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1631 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001632 if (!gspca_dev->streaming)
1633 return 0;
1634 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1635 return -ERESTARTSYS;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001636
Jean-François Moine18bf99a2010-04-09 06:06:11 -03001637 /* check the capture file */
1638 if (gspca_dev->capt_file != file) {
1639 ret = -EBUSY;
1640 goto out;
1641 }
1642
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001643 /* stop streaming */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001644 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1645 ret = -ERESTARTSYS;
1646 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001647 }
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001648 gspca_dev->usb_err = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001649 gspca_stream_off(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001650 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001651
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001652 /* empty the transfer queues */
1653 atomic_set(&gspca_dev->fr_q, 0);
1654 atomic_set(&gspca_dev->fr_i, 0);
1655 gspca_dev->fr_o = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001656 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001657out:
1658 mutex_unlock(&gspca_dev->queue_lock);
1659 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001660}
1661
1662static int vidioc_g_jpegcomp(struct file *file, void *priv,
1663 struct v4l2_jpegcompression *jpegcomp)
1664{
1665 struct gspca_dev *gspca_dev = priv;
1666 int ret;
1667
1668 if (!gspca_dev->sd_desc->get_jcomp)
1669 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001670 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1671 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001672 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001673 if (gspca_dev->present)
1674 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1675 else
1676 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001677 mutex_unlock(&gspca_dev->usb_lock);
1678 return ret;
1679}
1680
1681static int vidioc_s_jpegcomp(struct file *file, void *priv,
1682 struct v4l2_jpegcompression *jpegcomp)
1683{
1684 struct gspca_dev *gspca_dev = priv;
1685 int ret;
1686
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001687 if (!gspca_dev->sd_desc->set_jcomp)
1688 return -EINVAL;
Jim Parisf86d4a92008-12-10 05:50:44 -03001689 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1690 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001691 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001692 if (gspca_dev->present)
1693 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1694 else
1695 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001696 mutex_unlock(&gspca_dev->usb_lock);
1697 return ret;
1698}
1699
1700static int vidioc_g_parm(struct file *filp, void *priv,
1701 struct v4l2_streamparm *parm)
1702{
1703 struct gspca_dev *gspca_dev = priv;
1704
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001705 parm->parm.capture.readbuffers = gspca_dev->nbufread;
Jim Paris627a5ef2008-12-10 06:02:42 -03001706
1707 if (gspca_dev->sd_desc->get_streamparm) {
1708 int ret;
1709
1710 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1711 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001712 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001713 if (gspca_dev->present)
1714 ret = gspca_dev->sd_desc->get_streamparm(gspca_dev,
1715 parm);
1716 else
1717 ret = -ENODEV;
Jim Paris627a5ef2008-12-10 06:02:42 -03001718 mutex_unlock(&gspca_dev->usb_lock);
1719 return ret;
1720 }
1721
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001722 return 0;
1723}
1724
1725static int vidioc_s_parm(struct file *filp, void *priv,
1726 struct v4l2_streamparm *parm)
1727{
1728 struct gspca_dev *gspca_dev = priv;
1729 int n;
1730
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001731 n = parm->parm.capture.readbuffers;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001732 if (n == 0 || n >= GSPCA_MAX_FRAMES)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001733 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1734 else
1735 gspca_dev->nbufread = n;
Jim Paris627a5ef2008-12-10 06:02:42 -03001736
1737 if (gspca_dev->sd_desc->set_streamparm) {
1738 int ret;
1739
1740 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1741 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001742 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001743 if (gspca_dev->present)
1744 ret = gspca_dev->sd_desc->set_streamparm(gspca_dev,
1745 parm);
1746 else
1747 ret = -ENODEV;
Jim Paris627a5ef2008-12-10 06:02:42 -03001748 mutex_unlock(&gspca_dev->usb_lock);
1749 return ret;
1750 }
1751
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001752 return 0;
1753}
1754
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001755static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1756{
1757 struct gspca_dev *gspca_dev = file->private_data;
Harvey Harrisonefab8212008-07-05 06:12:47 -03001758 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001759 struct page *page;
1760 unsigned long addr, start, size;
1761 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001762
1763 start = vma->vm_start;
1764 size = vma->vm_end - vma->vm_start;
1765 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1766
Hans de Goedee2997a72008-04-23 08:09:12 -03001767 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1768 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001769 if (!gspca_dev->present) {
1770 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001771 goto out;
1772 }
1773 if (gspca_dev->capt_file != file) {
1774 ret = -EINVAL;
1775 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001776 }
1777
Harvey Harrisonefab8212008-07-05 06:12:47 -03001778 frame = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001779 for (i = 0; i < gspca_dev->nframes; ++i) {
1780 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1781 PDEBUG(D_STREAM, "mmap bad memory type");
1782 break;
1783 }
1784 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1785 == vma->vm_pgoff) {
1786 frame = &gspca_dev->frame[i];
1787 break;
1788 }
1789 }
Harvey Harrisonefab8212008-07-05 06:12:47 -03001790 if (frame == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001791 PDEBUG(D_STREAM, "mmap no frame buffer found");
1792 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001793 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001794 }
Jean-François Moine38bff692010-06-24 05:19:58 -03001795 if (size != frame->v4l2_buf.length) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001796 PDEBUG(D_STREAM, "mmap bad size");
1797 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001798 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001799 }
1800
1801 /*
1802 * - VM_IO marks the area as being a mmaped region for I/O to a
1803 * device. It also prevents the region from being core dumped.
1804 */
1805 vma->vm_flags |= VM_IO;
1806
1807 addr = (unsigned long) frame->data;
1808 while (size > 0) {
1809 page = vmalloc_to_page((void *) addr);
1810 ret = vm_insert_page(vma, start, page);
1811 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001812 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001813 start += PAGE_SIZE;
1814 addr += PAGE_SIZE;
1815 size -= PAGE_SIZE;
1816 }
1817
Jean-Francois Moined50e2572009-11-26 15:36:40 -03001818 vma->vm_ops = &gspca_vm_ops;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001819 vma->vm_private_data = frame;
1820 gspca_vm_open(vma);
Hans de Goedee2997a72008-04-23 08:09:12 -03001821 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001822out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001823 mutex_unlock(&gspca_dev->queue_lock);
1824 return ret;
1825}
1826
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001827/*
1828 * wait for a video frame
1829 *
1830 * If a frame is ready, its index is returned.
1831 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001832static int frame_wait(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001833 int nonblock_ing)
1834{
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001835 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001836
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001837 /* check if a frame is ready */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001838 i = gspca_dev->fr_o;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001839 if (i == atomic_read(&gspca_dev->fr_i)) {
Hans de Goede181b7042008-11-19 17:16:26 -03001840 if (nonblock_ing)
1841 return -EAGAIN;
1842
1843 /* wait till a frame is ready */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001844 ret = wait_event_interruptible_timeout(gspca_dev->wq,
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001845 i != atomic_read(&gspca_dev->fr_i) ||
Hans de Goede181b7042008-11-19 17:16:26 -03001846 !gspca_dev->streaming || !gspca_dev->present,
1847 msecs_to_jiffies(3000));
1848 if (ret < 0)
1849 return ret;
1850 if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present)
Hans de Goedee2997a72008-04-23 08:09:12 -03001851 return -EIO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001852 }
Hans de Goede181b7042008-11-19 17:16:26 -03001853
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001854 gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES;
Hans de Goedee2997a72008-04-23 08:09:12 -03001855
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001856 if (gspca_dev->sd_desc->dq_callback) {
1857 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001858 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001859 if (gspca_dev->present)
1860 gspca_dev->sd_desc->dq_callback(gspca_dev);
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001861 mutex_unlock(&gspca_dev->usb_lock);
1862 }
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001863 return gspca_dev->fr_queue[i];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001864}
1865
1866/*
1867 * dequeue a video buffer
1868 *
1869 * If nonblock_ing is false, block until a buffer is available.
1870 */
1871static int vidioc_dqbuf(struct file *file, void *priv,
1872 struct v4l2_buffer *v4l2_buf)
1873{
1874 struct gspca_dev *gspca_dev = priv;
1875 struct gspca_frame *frame;
1876 int i, ret;
1877
1878 PDEBUG(D_FRAM, "dqbuf");
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001879 if (v4l2_buf->memory != gspca_dev->memory)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001880 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001881
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001882 if (!gspca_dev->present)
1883 return -ENODEV;
1884
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001885 /* if not streaming, be sure the application will not loop forever */
1886 if (!(file->f_flags & O_NONBLOCK)
1887 && !gspca_dev->streaming && gspca_dev->users == 1)
1888 return -EINVAL;
1889
1890 /* only the capturing file may dequeue */
1891 if (gspca_dev->capt_file != file)
1892 return -EINVAL;
1893
1894 /* only one dequeue / read at a time */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001895 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1896 return -ERESTARTSYS;
1897
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001898 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001899 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001900 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001901 i = ret; /* frame index */
1902 frame = &gspca_dev->frame[i];
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001903 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001904 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001905 frame->data,
1906 frame->v4l2_buf.bytesused)) {
1907 PDEBUG(D_ERR|D_STREAM,
1908 "dqbuf cp to user failed");
1909 ret = -EFAULT;
1910 goto out;
1911 }
1912 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001913 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1914 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1915 PDEBUG(D_FRAM, "dqbuf %d", i);
1916 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001917out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001918 mutex_unlock(&gspca_dev->read_lock);
1919 return ret;
1920}
1921
1922/*
1923 * queue a video buffer
1924 *
1925 * Attempting to queue a buffer that has already been
1926 * queued will return -EINVAL.
1927 */
1928static int vidioc_qbuf(struct file *file, void *priv,
1929 struct v4l2_buffer *v4l2_buf)
1930{
1931 struct gspca_dev *gspca_dev = priv;
1932 struct gspca_frame *frame;
1933 int i, index, ret;
1934
1935 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001936
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001937 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1938 return -ERESTARTSYS;
1939
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001940 index = v4l2_buf->index;
1941 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001942 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001943 "qbuf idx %d >= %d", index, gspca_dev->nframes);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001944 ret = -EINVAL;
1945 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001946 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001947 if (v4l2_buf->memory != gspca_dev->memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001948 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001949 ret = -EINVAL;
1950 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001951 }
1952
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001953 frame = &gspca_dev->frame[index];
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001954 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1955 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001956 ret = -EINVAL;
1957 goto out;
1958 }
1959
1960 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001961
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001962 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001963 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1964 frame->v4l2_buf.length = v4l2_buf->length;
1965 }
1966
1967 /* put the buffer in the 'queued' queue */
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001968 i = atomic_read(&gspca_dev->fr_q);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001969 gspca_dev->fr_queue[i] = index;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001970 atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001971
1972 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1973 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1974 ret = 0;
1975out:
1976 mutex_unlock(&gspca_dev->queue_lock);
1977 return ret;
1978}
1979
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001980/*
1981 * allocate the resources for read()
1982 */
1983static int read_alloc(struct gspca_dev *gspca_dev,
1984 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001985{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001986 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001987 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001988
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001989 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001990 if (gspca_dev->nframes == 0) {
1991 struct v4l2_requestbuffers rb;
1992
1993 memset(&rb, 0, sizeof rb);
1994 rb.count = gspca_dev->nbufread;
1995 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001996 rb.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001997 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1998 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001999 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002000 return ret;
2001 }
2002 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2003 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002004 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002005 for (i = 0; i < gspca_dev->nbufread; i++) {
2006 v4l2_buf.index = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002007 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2008 if (ret != 0) {
2009 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
2010 return ret;
2011 }
2012 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002013 gspca_dev->memory = GSPCA_MEMORY_READ;
2014 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002015
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002016 /* start streaming */
2017 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2018 if (ret != 0)
2019 PDEBUG(D_STREAM, "read streamon err %d", ret);
2020 return ret;
2021}
2022
2023static unsigned int dev_poll(struct file *file, poll_table *wait)
2024{
2025 struct gspca_dev *gspca_dev = file->private_data;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03002026 int ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002027
2028 PDEBUG(D_FRAM, "poll");
2029
2030 poll_wait(file, &gspca_dev->wq, wait);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002031
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002032 /* if reqbufs is not done, the user would use read() */
2033 if (gspca_dev->nframes == 0) {
2034 if (gspca_dev->memory != GSPCA_MEMORY_NO)
2035 return POLLERR; /* not the 1st time */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002036 ret = read_alloc(gspca_dev, file);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002037 if (ret != 0)
2038 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002039 }
2040
2041 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
2042 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002043
Jean-François Moinef7059ea2010-07-06 04:32:27 -03002044 /* check if an image has been received */
2045 if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i))
2046 ret = POLLIN | POLLRDNORM; /* yes */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002047 else
2048 ret = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002049 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002050 if (!gspca_dev->present)
2051 return POLLHUP;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002052 return ret;
2053}
2054
2055static ssize_t dev_read(struct file *file, char __user *data,
2056 size_t count, loff_t *ppos)
2057{
2058 struct gspca_dev *gspca_dev = file->private_data;
2059 struct gspca_frame *frame;
2060 struct v4l2_buffer v4l2_buf;
2061 struct timeval timestamp;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002062 int n, ret, ret2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002063
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03002064 PDEBUG(D_FRAM, "read (%zd)", count);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002065 if (!gspca_dev->present)
2066 return -ENODEV;
2067 switch (gspca_dev->memory) {
2068 case GSPCA_MEMORY_NO: /* first time */
2069 ret = read_alloc(gspca_dev, file);
2070 if (ret != 0)
2071 return ret;
2072 break;
2073 case GSPCA_MEMORY_READ:
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002074 if (gspca_dev->capt_file == file)
2075 break;
2076 /* fall thru */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002077 default:
2078 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002079 }
2080
2081 /* get a frame */
Paulo Assis2f51d002010-03-07 06:56:56 -03002082 timestamp = ktime_to_timeval(ktime_get());
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002083 timestamp.tv_sec--;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002084 n = 2;
2085 for (;;) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002086 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2087 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002088 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002089 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
2090 if (ret != 0) {
2091 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
2092 return ret;
2093 }
2094
2095 /* if the process slept for more than 1 second,
Frank Zago219423c2008-09-26 07:43:54 -03002096 * get a newer frame */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002097 frame = &gspca_dev->frame[v4l2_buf.index];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002098 if (--n < 0)
2099 break; /* avoid infinite loop */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002100 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
2101 break;
2102 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2103 if (ret != 0) {
2104 PDEBUG(D_STREAM, "read qbuf err %d", ret);
2105 return ret;
2106 }
2107 }
2108
2109 /* copy the frame */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002110 if (count > frame->v4l2_buf.bytesused)
2111 count = frame->v4l2_buf.bytesused;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002112 ret = copy_to_user(data, frame->data, count);
2113 if (ret != 0) {
2114 PDEBUG(D_ERR|D_STREAM,
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03002115 "read cp to user lack %d / %zd", ret, count);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002116 ret = -EFAULT;
2117 goto out;
2118 }
2119 ret = count;
2120out:
2121 /* in each case, requeue the buffer */
2122 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2123 if (ret2 != 0)
2124 return ret2;
2125 return ret;
2126}
2127
Hans Verkuilbec43662008-12-30 06:58:20 -03002128static struct v4l2_file_operations dev_fops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002129 .owner = THIS_MODULE,
2130 .open = dev_open,
2131 .release = dev_close,
2132 .read = dev_read,
2133 .mmap = dev_mmap,
Hans Verkuilbec43662008-12-30 06:58:20 -03002134 .unlocked_ioctl = video_ioctl2,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002135 .poll = dev_poll,
2136};
2137
Hans Verkuila3998102008-07-21 02:57:38 -03002138static const struct v4l2_ioctl_ops dev_ioctl_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002139 .vidioc_querycap = vidioc_querycap,
2140 .vidioc_dqbuf = vidioc_dqbuf,
2141 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03002142 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2143 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2144 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2145 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002146 .vidioc_streamon = vidioc_streamon,
2147 .vidioc_queryctrl = vidioc_queryctrl,
2148 .vidioc_g_ctrl = vidioc_g_ctrl,
2149 .vidioc_s_ctrl = vidioc_s_ctrl,
2150 .vidioc_querymenu = vidioc_querymenu,
2151 .vidioc_enum_input = vidioc_enum_input,
2152 .vidioc_g_input = vidioc_g_input,
2153 .vidioc_s_input = vidioc_s_input,
2154 .vidioc_reqbufs = vidioc_reqbufs,
2155 .vidioc_querybuf = vidioc_querybuf,
2156 .vidioc_streamoff = vidioc_streamoff,
2157 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
2158 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
2159 .vidioc_g_parm = vidioc_g_parm,
2160 .vidioc_s_parm = vidioc_s_parm,
Hans de Goede11c635a2009-05-25 15:04:22 -03002161 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Antonio Ospite28ffe772009-12-02 06:18:46 -03002162 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03002163#ifdef CONFIG_VIDEO_ADV_DEBUG
2164 .vidioc_g_register = vidioc_g_register,
2165 .vidioc_s_register = vidioc_s_register,
2166#endif
2167 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002168};
2169
Hans Verkuila3998102008-07-21 02:57:38 -03002170static struct video_device gspca_template = {
2171 .name = "gspca main driver",
Hans Verkuila3998102008-07-21 02:57:38 -03002172 .fops = &dev_fops,
2173 .ioctl_ops = &dev_ioctl_ops,
Hans de Goeded0182302008-11-19 17:10:47 -03002174 .release = gspca_release,
Hans Verkuila3998102008-07-21 02:57:38 -03002175};
2176
Jean-François Moine6a330912010-10-02 04:12:25 -03002177/* initialize the controls */
2178static void ctrls_init(struct gspca_dev *gspca_dev)
2179{
2180 struct gspca_ctrl *ctrl;
2181 int i;
2182
2183 for (i = 0, ctrl = gspca_dev->cam.ctrls;
2184 i < gspca_dev->sd_desc->nctrls;
2185 i++, ctrl++) {
2186 ctrl->def = gspca_dev->sd_desc->ctrls[i].qctrl.default_value;
2187 ctrl->val = ctrl->def;
2188 ctrl->min = gspca_dev->sd_desc->ctrls[i].qctrl.minimum;
2189 ctrl->max = gspca_dev->sd_desc->ctrls[i].qctrl.maximum;
2190 }
2191}
2192
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002193/*
2194 * probe and create a new gspca device
2195 *
2196 * This function must be called by the sub-driver when it is
2197 * called for probing a new device.
2198 */
Jean-François Moine44628642010-06-05 07:47:36 -03002199int gspca_dev_probe2(struct usb_interface *intf,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002200 const struct usb_device_id *id,
2201 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002202 int dev_size,
2203 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002204{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002205 struct gspca_dev *gspca_dev;
2206 struct usb_device *dev = interface_to_usbdev(intf);
2207 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002208
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002209 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002210
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002211 /* create the device */
2212 if (dev_size < sizeof *gspca_dev)
2213 dev_size = sizeof *gspca_dev;
2214 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
Erik Andrene293e592008-10-03 08:46:50 -03002215 if (!gspca_dev) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002216 err("couldn't kzalloc gspca struct");
Erik Andrene293e592008-10-03 08:46:50 -03002217 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002218 }
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002219 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2220 if (!gspca_dev->usb_buf) {
2221 err("out of memory");
Erik Andrene293e592008-10-03 08:46:50 -03002222 ret = -ENOMEM;
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002223 goto out;
2224 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002225 gspca_dev->dev = dev;
Jean-François Moine44628642010-06-05 07:47:36 -03002226 gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002227 gspca_dev->nbalt = intf->num_altsetting;
Jean-François Moine35680ba2010-07-14 06:30:18 -03002228
2229 /* check if any audio device */
2230 if (dev->config->desc.bNumInterfaces != 1) {
2231 int i;
2232 struct usb_interface *intf2;
2233
2234 for (i = 0; i < dev->config->desc.bNumInterfaces; i++) {
2235 intf2 = dev->config->interface[i];
2236 if (intf2 != NULL
2237 && intf2->altsetting != NULL
2238 && intf2->altsetting->desc.bInterfaceClass ==
2239 USB_CLASS_AUDIO) {
2240 gspca_dev->audio = 1;
2241 break;
2242 }
2243 }
2244 }
2245
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002246 gspca_dev->sd_desc = sd_desc;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002247 gspca_dev->nbufread = 2;
Jean-Francois Moineff374742008-10-23 07:29:51 -03002248 gspca_dev->empty_packet = -1; /* don't check the empty packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002249
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002250 /* configure the subdriver and initialize the USB device */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002251 ret = sd_desc->config(gspca_dev, id);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002252 if (ret < 0)
2253 goto out;
Jean-François Moine6a330912010-10-02 04:12:25 -03002254 if (gspca_dev->cam.ctrls != NULL)
2255 ctrls_init(gspca_dev);
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002256 ret = sd_desc->init(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002257 if (ret < 0)
2258 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002259 gspca_set_default_mode(gspca_dev);
2260
Hans de Goede10ee2402010-02-19 04:41:40 -03002261 ret = gspca_input_connect(gspca_dev);
2262 if (ret)
2263 goto out;
2264
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002265 mutex_init(&gspca_dev->usb_lock);
2266 mutex_init(&gspca_dev->read_lock);
2267 mutex_init(&gspca_dev->queue_lock);
2268 init_waitqueue_head(&gspca_dev->wq);
2269
2270 /* init video stuff */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002271 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
Hans de Goede24441632009-03-27 16:08:20 -03002272 gspca_dev->vdev.parent = &intf->dev;
Jean-Francois Moine5c4fa002008-11-18 15:52:31 -03002273 gspca_dev->module = module;
Oliver Neukumee56a4d2008-07-27 14:01:59 -03002274 gspca_dev->present = 1;
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002275 ret = video_register_device(&gspca_dev->vdev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002276 VFL_TYPE_GRABBER,
Jean-Francois Moine8d584a52009-01-13 06:07:59 -03002277 -1);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002278 if (ret < 0) {
2279 err("video_register_device err %d", ret);
2280 goto out;
2281 }
2282
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002283 usb_set_intfdata(intf, gspca_dev);
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002284 PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
Márton Németh0274d422010-01-28 06:39:49 -03002285
Hans de Goede10ee2402010-02-19 04:41:40 -03002286 gspca_input_create_urb(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002287
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002288 return 0;
2289out:
Randy Dunlapf7628012010-03-30 14:32:04 -03002290#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Hans de Goede10ee2402010-02-19 04:41:40 -03002291 if (gspca_dev->input_dev)
2292 input_unregister_device(gspca_dev->input_dev);
Hans de Goedee7637522010-02-19 05:11:09 -03002293#endif
Jean-Francois Moinee07a1d82008-11-19 06:37:53 -03002294 kfree(gspca_dev->usb_buf);
2295 kfree(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002296 return ret;
2297}
Jean-François Moine44628642010-06-05 07:47:36 -03002298EXPORT_SYMBOL(gspca_dev_probe2);
2299
2300/* same function as the previous one, but check the interface */
2301int gspca_dev_probe(struct usb_interface *intf,
2302 const struct usb_device_id *id,
2303 const struct sd_desc *sd_desc,
2304 int dev_size,
2305 struct module *module)
2306{
2307 struct usb_device *dev = interface_to_usbdev(intf);
2308
2309 /* we don't handle multi-config cameras */
2310 if (dev->descriptor.bNumConfigurations != 1) {
Jean-François Moine0b656322010-09-13 05:19:58 -03002311 err("%04x:%04x too many config",
Jean-François Moine44628642010-06-05 07:47:36 -03002312 id->idVendor, id->idProduct);
2313 return -ENODEV;
2314 }
2315
2316 /* the USB video interface must be the first one */
2317 if (dev->config->desc.bNumInterfaces != 1
Jean-François Moinec4dc6922010-07-14 06:26:54 -03002318 && intf->cur_altsetting->desc.bInterfaceNumber != 0)
Jean-François Moine44628642010-06-05 07:47:36 -03002319 return -ENODEV;
Jean-François Moine44628642010-06-05 07:47:36 -03002320
2321 return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
2322}
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002323EXPORT_SYMBOL(gspca_dev_probe);
2324
2325/*
2326 * USB disconnection
2327 *
2328 * This function must be called by the sub-driver
2329 * when the device disconnects, after the specific resources are freed.
2330 */
2331void gspca_disconnect(struct usb_interface *intf)
2332{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002333 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Randy Dunlapf7628012010-03-30 14:32:04 -03002334#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -03002335 struct input_dev *input_dev;
Jean-François Moinee070bf92010-02-18 15:02:51 -03002336#endif
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002337
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002338 PDEBUG(D_PROBE, "%s disconnect",
2339 video_device_node_name(&gspca_dev->vdev));
Adam Bakerad281272009-02-04 15:33:21 -03002340 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03002341 gspca_dev->present = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002342
2343 if (gspca_dev->streaming) {
2344 destroy_urbs(gspca_dev);
2345 wake_up_interruptible(&gspca_dev->wq);
2346 }
2347
Randy Dunlapf7628012010-03-30 14:32:04 -03002348#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -03002349 gspca_input_destroy_urb(gspca_dev);
2350 input_dev = gspca_dev->input_dev;
2351 if (input_dev) {
2352 gspca_dev->input_dev = NULL;
2353 input_unregister_device(input_dev);
2354 }
Jean-François Moinee070bf92010-02-18 15:02:51 -03002355#endif
Márton Németh0274d422010-01-28 06:39:49 -03002356
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002357 /* the device is freed at exit of this function */
2358 gspca_dev->dev = NULL;
Adam Bakerad281272009-02-04 15:33:21 -03002359 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03002360
Frank Zago6b060ff2008-09-28 08:12:22 -03002361 usb_set_intfdata(intf, NULL);
2362
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002363 /* release the device */
2364 /* (this will call gspca_release() immediatly or on last close) */
2365 video_unregister_device(&gspca_dev->vdev);
Jean-Francois Moine3145b8c2008-10-05 04:22:10 -03002366
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002367/* PDEBUG(D_PROBE, "disconnect complete"); */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002368}
2369EXPORT_SYMBOL(gspca_disconnect);
2370
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002371#ifdef CONFIG_PM
2372int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2373{
2374 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2375
2376 if (!gspca_dev->streaming)
2377 return 0;
2378 gspca_dev->frozen = 1; /* avoid urb error messages */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002379 if (gspca_dev->sd_desc->stopN)
2380 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002381 destroy_urbs(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002382 gspca_input_destroy_urb(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002383 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002384 if (gspca_dev->sd_desc->stop0)
2385 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002386 return 0;
2387}
2388EXPORT_SYMBOL(gspca_suspend);
2389
2390int gspca_resume(struct usb_interface *intf)
2391{
2392 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2393
2394 gspca_dev->frozen = 0;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002395 gspca_dev->sd_desc->init(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002396 gspca_input_create_urb(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002397 if (gspca_dev->streaming)
2398 return gspca_init_transfer(gspca_dev);
Jean-Francois Moine1e89e2d2008-09-03 17:12:15 -03002399 return 0;
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002400}
2401EXPORT_SYMBOL(gspca_resume);
2402#endif
Hans de Goededcef3232008-07-10 10:40:53 -03002403/* -- cam driver utility functions -- */
2404
2405/* auto gain and exposure algorithm based on the knee algorithm described here:
2406 http://ytse.tricolour.net/docs/LowLightOptimization.html
2407
2408 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2409 where changed. */
2410int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2411 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2412{
2413 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2414 const struct ctrl *gain_ctrl = NULL;
2415 const struct ctrl *exposure_ctrl = NULL;
2416 const struct ctrl *autogain_ctrl = NULL;
2417 int retval = 0;
2418
2419 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goede26984b02010-02-01 13:18:37 -03002420 if (gspca_dev->ctrl_dis & (1 << i))
2421 continue;
Hans de Goededcef3232008-07-10 10:40:53 -03002422 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2423 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2424 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2425 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2426 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2427 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2428 }
2429 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2430 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2431 "on cam without (auto)gain/exposure");
2432 return 0;
2433 }
2434
2435 if (gain_ctrl->get(gspca_dev, &gain) ||
2436 exposure_ctrl->get(gspca_dev, &exposure) ||
2437 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2438 return 0;
2439
2440 orig_gain = gain;
2441 orig_exposure = exposure;
2442
2443 /* If we are of a multiple of deadzone, do multiple steps to reach the
2444 desired lumination fast (with the risc of a slight overshoot) */
2445 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2446
Jean-Francois Moine1c44d812008-11-10 05:56:55 -03002447 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
Hans de Goededcef3232008-07-10 10:40:53 -03002448 avg_lum, desired_avg_lum, steps);
2449
2450 for (i = 0; i < steps; i++) {
2451 if (avg_lum > desired_avg_lum) {
2452 if (gain > gain_knee)
2453 gain--;
2454 else if (exposure > exposure_knee)
2455 exposure--;
2456 else if (gain > gain_ctrl->qctrl.default_value)
2457 gain--;
2458 else if (exposure > exposure_ctrl->qctrl.minimum)
2459 exposure--;
2460 else if (gain > gain_ctrl->qctrl.minimum)
2461 gain--;
2462 else
2463 break;
2464 } else {
2465 if (gain < gain_ctrl->qctrl.default_value)
2466 gain++;
2467 else if (exposure < exposure_knee)
2468 exposure++;
2469 else if (gain < gain_knee)
2470 gain++;
2471 else if (exposure < exposure_ctrl->qctrl.maximum)
2472 exposure++;
2473 else if (gain < gain_ctrl->qctrl.maximum)
2474 gain++;
2475 else
2476 break;
2477 }
2478 }
2479
2480 if (gain != orig_gain) {
2481 gain_ctrl->set(gspca_dev, gain);
2482 retval = 1;
2483 }
2484 if (exposure != orig_exposure) {
2485 exposure_ctrl->set(gspca_dev, exposure);
2486 retval = 1;
2487 }
2488
2489 return retval;
2490}
2491EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2492
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002493/* -- module insert / remove -- */
2494static int __init gspca_init(void)
2495{
Jean-François Moine54826432010-09-13 04:53:03 -03002496 info("v%d.%d.%d registered",
Jean-Francois Moinebb64e862008-07-19 06:49:28 -03002497 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2498 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2499 DRIVER_VERSION_NUMBER & 0xff);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002500 return 0;
2501}
2502static void __exit gspca_exit(void)
2503{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002504}
2505
2506module_init(gspca_init);
2507module_exit(gspca_exit);
2508
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03002509#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002510module_param_named(debug, gspca_debug, int, 0644);
2511MODULE_PARM_DESC(debug,
2512 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002513 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
2514 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002515#endif