blob: 678675bb365256cd289eb31d8095c914163421b7 [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 Moine55461f42010-01-30 16:03:02 -030058#define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 9, 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030059
Jean-Francois Moine335b3f82008-07-30 04:53:02 -030060#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030061int gspca_debug = D_ERR | D_PROBE;
62EXPORT_SYMBOL(gspca_debug);
63
64static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
65{
66 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
67 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
68 txt,
69 pixfmt & 0xff,
70 (pixfmt >> 8) & 0xff,
71 (pixfmt >> 16) & 0xff,
72 pixfmt >> 24,
73 w, h);
74 } else {
75 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
76 txt,
77 pixfmt,
78 w, h);
79 }
80}
81#else
82#define PDEBUG_MODE(txt, pixfmt, w, h)
83#endif
84
Jean-Francois Moine77ee3312009-11-07 15:30:50 -030085/* specific memory types - !! should be different from V4L2_MEMORY_xxx */
Jean-Francois Moined43fa322008-06-12 10:58:58 -030086#define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
87#define GSPCA_MEMORY_READ 7
88
Jean-Francois Moined43fa322008-06-12 10:58:58 -030089#define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
Jean-Francois Moined43fa322008-06-12 10:58:58 -030090
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030091/*
92 * VMA operations.
93 */
94static void gspca_vm_open(struct vm_area_struct *vma)
95{
96 struct gspca_frame *frame = vma->vm_private_data;
97
98 frame->vma_use_count++;
99 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
100}
101
102static void gspca_vm_close(struct vm_area_struct *vma)
103{
104 struct gspca_frame *frame = vma->vm_private_data;
105
106 if (--frame->vma_use_count <= 0)
107 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
108}
109
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400110static const struct vm_operations_struct gspca_vm_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300111 .open = gspca_vm_open,
112 .close = gspca_vm_close,
113};
114
Márton Németh0274d422010-01-28 06:39:49 -0300115/*
116 * Input and interrupt endpoint handling functions
117 */
Randy Dunlapf7628012010-03-30 14:32:04 -0300118#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -0300119static void int_irq(struct urb *urb)
120{
121 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
122 int ret;
123
124 ret = urb->status;
125 switch (ret) {
126 case 0:
127 if (gspca_dev->sd_desc->int_pkt_scan(gspca_dev,
128 urb->transfer_buffer, urb->actual_length) < 0) {
129 PDEBUG(D_ERR, "Unknown packet received");
130 }
131 break;
132
133 case -ENOENT:
134 case -ECONNRESET:
135 case -ENODEV:
136 case -ESHUTDOWN:
137 /* Stop is requested either by software or hardware is gone,
138 * keep the ret value non-zero and don't resubmit later.
139 */
140 break;
141
142 default:
143 PDEBUG(D_ERR, "URB error %i, resubmitting", urb->status);
144 urb->status = 0;
145 ret = 0;
146 }
147
148 if (ret == 0) {
149 ret = usb_submit_urb(urb, GFP_ATOMIC);
150 if (ret < 0)
151 PDEBUG(D_ERR, "Resubmit URB failed with error %i", ret);
152 }
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) {
180 PDEBUG(D_ERR, "Input device registration failed "
181 "with error %i", err);
182 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;
204 PDEBUG(D_PROBE, "found int in endpoint: 0x%x, "
205 "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);
226 gspca_dev->int_urb = urb;
227 ret = usb_submit_urb(urb, GFP_KERNEL);
228 if (ret < 0) {
229 PDEBUG(D_ERR, "submit URB failed with error %i", ret);
230 goto error_submit;
231 }
232 return ret;
233
234error_submit:
Daniel Mack997ea582010-04-12 13:17:25 +0200235 usb_free_coherent(dev,
236 urb->transfer_buffer_length,
237 urb->transfer_buffer,
238 urb->transfer_dma);
Márton Németh0274d422010-01-28 06:39:49 -0300239error_buffer:
240 usb_free_urb(urb);
241error:
242 return ret;
243}
244
Hans de Goede10ee2402010-02-19 04:41:40 -0300245static void gspca_input_create_urb(struct gspca_dev *gspca_dev)
Márton Németh0274d422010-01-28 06:39:49 -0300246{
Márton Németh0274d422010-01-28 06:39:49 -0300247 struct usb_interface *intf;
248 struct usb_host_interface *intf_desc;
249 struct usb_endpoint_descriptor *ep;
250 int i;
251
252 if (gspca_dev->sd_desc->int_pkt_scan) {
253 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
254 intf_desc = intf->cur_altsetting;
255 for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
256 ep = &intf_desc->endpoint[i].desc;
257 if (usb_endpoint_dir_in(ep) &&
258 usb_endpoint_xfer_int(ep)) {
259
Hans de Goede10ee2402010-02-19 04:41:40 -0300260 alloc_and_submit_int_urb(gspca_dev, ep);
Márton Németh0274d422010-01-28 06:39:49 -0300261 break;
262 }
263 }
264 }
Márton Németh0274d422010-01-28 06:39:49 -0300265}
266
267static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
268{
269 struct urb *urb;
270
271 urb = gspca_dev->int_urb;
272 if (urb) {
273 gspca_dev->int_urb = NULL;
274 usb_kill_urb(urb);
Daniel Mack997ea582010-04-12 13:17:25 +0200275 usb_free_coherent(gspca_dev->dev,
276 urb->transfer_buffer_length,
277 urb->transfer_buffer,
278 urb->transfer_dma);
Márton Németh0274d422010-01-28 06:39:49 -0300279 usb_free_urb(urb);
280 }
281}
Randy Dunlapf7628012010-03-30 14:32:04 -0300282#else
283static inline void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
284{
285}
286
287static inline void gspca_input_create_urb(struct gspca_dev *gspca_dev)
288{
289}
290
291static inline int gspca_input_connect(struct gspca_dev *dev)
292{
293 return 0;
294}
Márton Németh0274d422010-01-28 06:39:49 -0300295#endif
296
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300297/* get the current input frame buffer */
298struct gspca_frame *gspca_get_i_frame(struct gspca_dev *gspca_dev)
299{
300 struct gspca_frame *frame;
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300301
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300302 frame = gspca_dev->cur_frame;
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300303 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
304 != V4L2_BUF_FLAG_QUEUED)
305 return NULL;
306 return frame;
307}
308EXPORT_SYMBOL(gspca_get_i_frame);
309
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300310/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300311 * fill a video frame from an URB and resubmit
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300312 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300313static void fill_frame(struct gspca_dev *gspca_dev,
314 struct urb *urb)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300315{
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300316 u8 *data; /* address of data in the iso message */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300317 int i, len, st;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300318 cam_pkt_op pkt_scan;
319
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300320 if (urb->status != 0) {
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300321 if (urb->status == -ESHUTDOWN)
322 return; /* disconnection */
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300323#ifdef CONFIG_PM
Hans de Goede6a540bd2009-10-24 15:02:14 -0300324 if (gspca_dev->frozen)
325 return;
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300326#endif
Hans de Goede6a540bd2009-10-24 15:02:14 -0300327 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine81578522009-11-13 07:15:08 -0300328 urb->status = 0;
Hans de Goedebf926ad2009-10-23 06:56:06 -0300329 goto resubmit;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300330 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300331 pkt_scan = gspca_dev->sd_desc->pkt_scan;
332 for (i = 0; i < urb->number_of_packets; i++) {
333
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300334 /* check the packet status and length */
335 len = urb->iso_frame_desc[i].actual_length;
Jean-Francois Moineff374742008-10-23 07:29:51 -0300336 if (len == 0) {
337 if (gspca_dev->empty_packet == 0)
338 gspca_dev->empty_packet = 1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300339 continue;
Jean-Francois Moineff374742008-10-23 07:29:51 -0300340 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300341 st = urb->iso_frame_desc[i].status;
342 if (st) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300343 PDEBUG(D_ERR,
344 "ISOC data error: [%d] len=%d, status=%d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300345 i, len, st);
346 gspca_dev->last_packet_type = DISCARD_PACKET;
347 continue;
348 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300349
350 /* let the packet be analyzed by the subdriver */
351 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
352 i, urb->iso_frame_desc[i].offset, len);
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300353 data = (u8 *) urb->transfer_buffer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300354 + urb->iso_frame_desc[i].offset;
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300355 pkt_scan(gspca_dev, data, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300356 }
357
Hans de Goedebf926ad2009-10-23 06:56:06 -0300358resubmit:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300359 /* resubmit the URB */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300360 st = usb_submit_urb(urb, GFP_ATOMIC);
361 if (st < 0)
362 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
363}
364
365/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300366 * ISOC message interrupt from the USB device
367 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300368 * Analyse each packet and call the subdriver for copy to the frame buffer.
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300369 */
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300370static void isoc_irq(struct urb *urb)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300371{
372 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
373
Hans de Goede3647fea2008-07-15 05:36:30 -0300374 PDEBUG(D_PACK, "isoc irq");
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300375 if (!gspca_dev->streaming)
376 return;
377 fill_frame(gspca_dev, urb);
378}
379
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300380/*
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300381 * bulk message interrupt from the USB device
382 */
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300383static void bulk_irq(struct urb *urb)
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300384{
385 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300386 int st;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300387
388 PDEBUG(D_PACK, "bulk irq");
389 if (!gspca_dev->streaming)
390 return;
Jean-Francois Moine880ae242008-11-18 04:19:52 -0300391 switch (urb->status) {
392 case 0:
393 break;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300394 case -ESHUTDOWN:
395 return; /* disconnection */
Jean-Francois Moine880ae242008-11-18 04:19:52 -0300396 default:
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300397#ifdef CONFIG_PM
Hans de Goede6a540bd2009-10-24 15:02:14 -0300398 if (gspca_dev->frozen)
399 return;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300400#endif
Hans de Goede6a540bd2009-10-24 15:02:14 -0300401 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine81578522009-11-13 07:15:08 -0300402 urb->status = 0;
Hans de Goedebf926ad2009-10-23 06:56:06 -0300403 goto resubmit;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300404 }
405
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300406 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
407 gspca_dev->sd_desc->pkt_scan(gspca_dev,
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300408 urb->transfer_buffer,
409 urb->actual_length);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300410
Hans de Goedebf926ad2009-10-23 06:56:06 -0300411resubmit:
Antonio Ospitedff369a2008-11-14 07:53:32 -0300412 /* resubmit the URB */
413 if (gspca_dev->cam.bulk_nurbs != 0) {
414 st = usb_submit_urb(urb, GFP_ATOMIC);
415 if (st < 0)
416 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
417 }
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300418}
419
420/*
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300421 * add data to the current frame
422 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300423 * This function is called by the subdrivers at interrupt level.
424 *
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300425 * To build a frame, these ones must add
426 * - one FIRST_PACKET
427 * - 0 or many INTER_PACKETs
428 * - one LAST_PACKET
429 * DISCARD_PACKET invalidates the whole frame.
430 * On LAST_PACKET, a new frame is returned.
431 */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300432void gspca_frame_add(struct gspca_dev *gspca_dev,
433 enum gspca_packet_type packet_type,
434 const u8 *data,
435 int len)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300436{
Jean-Francois Moine3ec342f2009-11-13 07:38:16 -0300437 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300438 int i, j;
439
Hans de Goedee2997a72008-04-23 08:09:12 -0300440 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300441
442 /* check the availability of the frame buffer */
Jean-Francois Moine3ec342f2009-11-13 07:38:16 -0300443 frame = gspca_dev->cur_frame;
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300444 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
445 != V4L2_BUF_FLAG_QUEUED) {
446 gspca_dev->last_packet_type = DISCARD_PACKET;
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300447 return;
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300448 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300449
450 /* when start of a new frame, if the current frame buffer
451 * is not queued, discard the whole frame */
452 if (packet_type == FIRST_PACKET) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300453 frame->data_end = frame->data;
Paulo Assis2f51d002010-03-07 06:56:56 -0300454 frame->v4l2_buf.timestamp = ktime_to_timeval(ktime_get());
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300455 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300456 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
Jean-Francois Moine06ca78f2008-07-22 03:45:08 -0300457 if (packet_type == LAST_PACKET)
458 gspca_dev->last_packet_type = packet_type;
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300459 return;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300460 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300461
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300462 /* append the packet to the frame buffer */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300463 if (len > 0) {
464 if (frame->data_end - frame->data + len
465 > frame->v4l2_buf.length) {
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -0300466 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300467 frame->data_end - frame->data + len,
468 frame->v4l2_buf.length);
469 packet_type = DISCARD_PACKET;
470 } else {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300471 memcpy(frame->data_end, data, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300472 frame->data_end += len;
473 }
474 }
475 gspca_dev->last_packet_type = packet_type;
476
Frank Zago219423c2008-09-26 07:43:54 -0300477 /* if last packet, wake up the application and advance in the queue */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300478 if (packet_type == LAST_PACKET) {
479 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
480 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
481 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300482 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300483 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
484 gspca_dev->fr_i = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300485 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
486 frame->v4l2_buf.bytesused,
487 gspca_dev->fr_q,
488 i,
489 gspca_dev->fr_o);
490 j = gspca_dev->fr_queue[i];
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300491 gspca_dev->cur_frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300492 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300493}
494EXPORT_SYMBOL(gspca_frame_add);
495
496static int gspca_is_compressed(__u32 format)
497{
498 switch (format) {
499 case V4L2_PIX_FMT_MJPEG:
500 case V4L2_PIX_FMT_JPEG:
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300501 case V4L2_PIX_FMT_SPCA561:
Hans de Goedeab8f12c2008-07-04 18:29:32 -0300502 case V4L2_PIX_FMT_PAC207:
Kyle Guinnd661e622009-01-16 05:36:14 -0300503 case V4L2_PIX_FMT_MR97310A:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300504 return 1;
505 }
506 return 0;
507}
508
Jean-Francois Moined50e2572009-11-26 15:36:40 -0300509static void *rvmalloc(long size)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300510{
511 void *mem;
512 unsigned long adr;
513
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300514 mem = vmalloc_32(size);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300515 if (mem != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300516 adr = (unsigned long) mem;
Jean-Francois Moined50e2572009-11-26 15:36:40 -0300517 while (size > 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300518 SetPageReserved(vmalloc_to_page((void *) adr));
519 adr += PAGE_SIZE;
520 size -= PAGE_SIZE;
521 }
522 }
523 return mem;
524}
525
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300526static void rvfree(void *mem, long size)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300527{
528 unsigned long adr;
529
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300530 adr = (unsigned long) mem;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300531 while (size > 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300532 ClearPageReserved(vmalloc_to_page((void *) adr));
533 adr += PAGE_SIZE;
534 size -= PAGE_SIZE;
535 }
536 vfree(mem);
537}
538
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300539static int frame_alloc(struct gspca_dev *gspca_dev,
540 unsigned int count)
541{
542 struct gspca_frame *frame;
543 unsigned int frsz;
544 int i;
545
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300546 i = gspca_dev->curr_mode;
547 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300548 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300549 frsz = PAGE_ALIGN(frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300550 gspca_dev->frsz = frsz;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300551 if (count > GSPCA_MAX_FRAMES)
552 count = GSPCA_MAX_FRAMES;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300553 gspca_dev->frbuf = rvmalloc(frsz * count);
554 if (!gspca_dev->frbuf) {
555 err("frame alloc failed");
556 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300557 }
558 gspca_dev->nframes = count;
559 for (i = 0; i < count; i++) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300560 frame = &gspca_dev->frame[i];
561 frame->v4l2_buf.index = i;
562 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
563 frame->v4l2_buf.flags = 0;
564 frame->v4l2_buf.field = V4L2_FIELD_NONE;
565 frame->v4l2_buf.length = frsz;
566 frame->v4l2_buf.memory = gspca_dev->memory;
567 frame->v4l2_buf.sequence = 0;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300568 frame->data = frame->data_end =
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300569 gspca_dev->frbuf + i * frsz;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300570 frame->v4l2_buf.m.offset = i * frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300571 }
572 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
Jean-Francois Moine3ec342f2009-11-13 07:38:16 -0300573 gspca_dev->cur_frame = &gspca_dev->frame[0];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300574 gspca_dev->last_packet_type = DISCARD_PACKET;
575 gspca_dev->sequence = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300576 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300577}
578
579static void frame_free(struct gspca_dev *gspca_dev)
580{
581 int i;
582
583 PDEBUG(D_STREAM, "frame free");
Harvey Harrisonefab8212008-07-05 06:12:47 -0300584 if (gspca_dev->frbuf != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300585 rvfree(gspca_dev->frbuf,
586 gspca_dev->nframes * gspca_dev->frsz);
587 gspca_dev->frbuf = NULL;
588 for (i = 0; i < gspca_dev->nframes; i++)
589 gspca_dev->frame[i].data = NULL;
590 }
591 gspca_dev->nframes = 0;
592}
593
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300594static void destroy_urbs(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300595{
596 struct urb *urb;
597 unsigned int i;
598
599 PDEBUG(D_STREAM, "kill transfer");
Erik Andrén44732812008-10-13 15:52:46 -0300600 for (i = 0; i < MAX_NURBS; i++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300601 urb = gspca_dev->urb[i];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300602 if (urb == NULL)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300603 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300604
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300605 gspca_dev->urb[i] = NULL;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300606 usb_kill_urb(urb);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300607 if (urb->transfer_buffer != NULL)
Daniel Mack997ea582010-04-12 13:17:25 +0200608 usb_free_coherent(gspca_dev->dev,
609 urb->transfer_buffer_length,
610 urb->transfer_buffer,
611 urb->transfer_dma);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300612 usb_free_urb(urb);
613 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300614}
615
Jean-François Moinee4dac282010-04-09 06:11:36 -0300616static int gspca_set_alt0(struct gspca_dev *gspca_dev)
617{
618 int ret;
619
620 if (gspca_dev->alt == 0)
621 return 0;
622 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
623 if (ret < 0)
624 PDEBUG(D_ERR|D_STREAM, "set alt 0 err %d", ret);
625 return ret;
626}
627
628/* Note: both the queue and the usb locks should be held when calling this */
629static void gspca_stream_off(struct gspca_dev *gspca_dev)
630{
631 gspca_dev->streaming = 0;
632 if (gspca_dev->present) {
633 if (gspca_dev->sd_desc->stopN)
634 gspca_dev->sd_desc->stopN(gspca_dev);
635 destroy_urbs(gspca_dev);
636 gspca_input_destroy_urb(gspca_dev);
637 gspca_set_alt0(gspca_dev);
638 gspca_input_create_urb(gspca_dev);
639 }
640
641 /* always call stop0 to free the subdriver's resources */
642 if (gspca_dev->sd_desc->stop0)
643 gspca_dev->sd_desc->stop0(gspca_dev);
644 PDEBUG(D_STREAM, "stream off OK");
645}
646
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300647/*
Frank Zago219423c2008-09-26 07:43:54 -0300648 * look for an input transfer endpoint in an alternate setting
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300649 */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300650static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300651 int xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300652{
653 struct usb_host_endpoint *ep;
654 int i, attr;
655
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300656 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
657 ep = &alt->endpoint[i];
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300658 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Jean-Francois Moine30d35e42009-04-21 13:57:31 -0300659 if (attr == xfer
660 && ep->desc.wMaxPacketSize != 0)
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300661 return ep;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300662 }
663 return NULL;
664}
665
666/*
Frank Zago219423c2008-09-26 07:43:54 -0300667 * look for an input (isoc or bulk) endpoint
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300668 *
669 * The endpoint is defined by the subdriver.
670 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
671 * This routine may be called many times when the bandwidth is too small
672 * (the bandwidth is checked on urb submit).
673 */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300674static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300675{
676 struct usb_interface *intf;
677 struct usb_host_endpoint *ep;
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300678 int xfer, i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300679
680 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300681 ep = NULL;
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300682 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
683 : USB_ENDPOINT_XFER_ISOC;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300684 i = gspca_dev->alt; /* previous alt setting */
Hans de Goedea511ba92009-10-16 07:13:07 -0300685 if (gspca_dev->cam.reverse_alts) {
686 while (++i < gspca_dev->nbalt) {
687 ep = alt_xfer(&intf->altsetting[i], xfer);
688 if (ep)
689 break;
690 }
691 } else {
692 while (--i >= 0) {
693 ep = alt_xfer(&intf->altsetting[i], xfer);
694 if (ep)
695 break;
696 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300697 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300698 if (ep == NULL) {
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300699 err("no transfer endpoint found");
700 return NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300701 }
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300702 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300703 i, ep->desc.bEndpointAddress);
Jean-Francois Moine1c8336d2009-07-28 04:38:25 -0300704 gspca_dev->alt = i; /* memorize the current alt setting */
Jean-Francois Moine8dd07ef2009-04-21 14:05:44 -0300705 if (gspca_dev->nbalt > 1) {
Márton Németh0274d422010-01-28 06:39:49 -0300706 gspca_input_destroy_urb(gspca_dev);
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300707 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
708 if (ret < 0) {
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300709 err("set alt %d err %d", i, ret);
Márton Németh0274d422010-01-28 06:39:49 -0300710 ep = NULL;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300711 }
Márton Németh0274d422010-01-28 06:39:49 -0300712 gspca_input_create_urb(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300713 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300714 return ep;
715}
716
717/*
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300718 * create the URBs for image transfer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300719 */
720static int create_urbs(struct gspca_dev *gspca_dev,
721 struct usb_host_endpoint *ep)
722{
723 struct urb *urb;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300724 int n, nurbs, i, psize, npkt, bsize;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300725
726 /* calculate the packet size and the number of packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300727 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300728
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300729 if (!gspca_dev->cam.bulk) { /* isoc */
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300730
731 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300732 if (gspca_dev->pkt_size == 0)
733 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
734 else
735 psize = gspca_dev->pkt_size;
Jean-Francois Moine49cb6b02009-04-25 13:29:01 -0300736 npkt = gspca_dev->cam.npkt;
737 if (npkt == 0)
738 npkt = 32; /* default value */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300739 bsize = psize * npkt;
740 PDEBUG(D_STREAM,
741 "isoc %d pkts size %d = bsize:%d",
742 npkt, psize, bsize);
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300743 nurbs = DEF_NURBS;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300744 } else { /* bulk */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300745 npkt = 0;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300746 bsize = gspca_dev->cam.bulk_size;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300747 if (bsize == 0)
748 bsize = psize;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300749 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300750 if (gspca_dev->cam.bulk_nurbs != 0)
751 nurbs = gspca_dev->cam.bulk_nurbs;
752 else
753 nurbs = 1;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300754 }
755
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300756 for (n = 0; n < nurbs; n++) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300757 urb = usb_alloc_urb(npkt, GFP_KERNEL);
758 if (!urb) {
759 err("usb_alloc_urb failed");
760 return -ENOMEM;
761 }
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300762 gspca_dev->urb[n] = urb;
Daniel Mack997ea582010-04-12 13:17:25 +0200763 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300764 bsize,
765 GFP_KERNEL,
766 &urb->transfer_dma);
767
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300768 if (urb->transfer_buffer == NULL) {
Daniel Mack997ea582010-04-12 13:17:25 +0200769 err("usb_alloc_coherent failed");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300770 return -ENOMEM;
771 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300772 urb->dev = gspca_dev->dev;
773 urb->context = gspca_dev;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300774 urb->transfer_buffer_length = bsize;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300775 if (npkt != 0) { /* ISOC */
776 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
777 ep->desc.bEndpointAddress);
Frank Zago6c49da72008-09-28 07:43:00 -0300778 urb->transfer_flags = URB_ISO_ASAP
779 | URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300780 urb->interval = ep->desc.bInterval;
781 urb->complete = isoc_irq;
782 urb->number_of_packets = npkt;
783 for (i = 0; i < npkt; i++) {
784 urb->iso_frame_desc[i].length = psize;
785 urb->iso_frame_desc[i].offset = psize * i;
786 }
787 } else { /* bulk */
788 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
789 ep->desc.bEndpointAddress),
Frank Zago6c49da72008-09-28 07:43:00 -0300790 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300791 urb->complete = bulk_irq;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300792 }
793 }
794 return 0;
795}
796
797/*
798 * start the USB transfer
799 */
800static int gspca_init_transfer(struct gspca_dev *gspca_dev)
801{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300802 struct usb_host_endpoint *ep;
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300803 struct urb *urb;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300804 int n, ret;
805
Hans de Goedee2997a72008-04-23 08:09:12 -0300806 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
807 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300808
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300809 if (!gspca_dev->present) {
810 ret = -ENODEV;
811 goto out;
812 }
813
Jean-Francois Moineab6c8cf2010-01-13 15:34:27 -0300814 gspca_dev->usb_err = 0;
815
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300816 /* set the higher alternate setting and
817 * loop until urb submit succeeds */
Hans de Goedea511ba92009-10-16 07:13:07 -0300818 if (gspca_dev->cam.reverse_alts)
819 gspca_dev->alt = 0;
820 else
821 gspca_dev->alt = gspca_dev->nbalt;
822
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300823 if (gspca_dev->sd_desc->isoc_init) {
824 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
825 if (ret < 0)
826 goto out;
827 }
828 ep = get_ep(gspca_dev);
829 if (ep == NULL) {
830 ret = -EIO;
831 goto out;
832 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300833 for (;;) {
Jean-Francois Moine47aaca92009-12-15 05:23:04 -0300834 if (!gspca_dev->cam.no_urb_create) {
835 PDEBUG(D_STREAM, "init transfer alt %d",
836 gspca_dev->alt);
837 ret = create_urbs(gspca_dev, ep);
838 if (ret < 0) {
839 destroy_urbs(gspca_dev);
840 goto out;
841 }
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300842 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300843
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300844 /* clear the bulk endpoint */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300845 if (gspca_dev->cam.bulk)
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300846 usb_clear_halt(gspca_dev->dev,
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300847 gspca_dev->urb[0]->pipe);
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300848
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300849 /* start the cam */
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300850 ret = gspca_dev->sd_desc->start(gspca_dev);
851 if (ret < 0) {
852 destroy_urbs(gspca_dev);
853 goto out;
854 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300855 gspca_dev->streaming = 1;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300856
Antonio Ospitedff369a2008-11-14 07:53:32 -0300857 /* some bulk transfers are started by the subdriver */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300858 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300859 break;
860
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300861 /* submit the URBs */
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300862 for (n = 0; n < MAX_NURBS; n++) {
863 urb = gspca_dev->urb[n];
864 if (urb == NULL)
865 break;
866 ret = usb_submit_urb(urb, GFP_KERNEL);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300867 if (ret < 0)
868 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300869 }
870 if (ret >= 0)
871 break;
Jean-François Moinee4dac282010-04-09 06:11:36 -0300872 gspca_stream_off(gspca_dev);
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300873 if (ret != -ENOSPC) {
874 PDEBUG(D_ERR|D_STREAM,
875 "usb_submit_urb alt %d err %d",
876 gspca_dev->alt, ret);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300877 goto out;
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300878 }
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300879
880 /* the bandwidth is not wide enough
881 * negociate or try a lower alternate setting */
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300882 PDEBUG(D_ERR|D_STREAM,
883 "bandwidth not wide enough - trying again");
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300884 msleep(20); /* wait for kill complete */
885 if (gspca_dev->sd_desc->isoc_nego) {
886 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
887 if (ret < 0)
888 goto out;
889 } else {
890 ep = get_ep(gspca_dev);
891 if (ep == NULL) {
892 ret = -EIO;
893 goto out;
894 }
895 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300896 }
897out:
898 mutex_unlock(&gspca_dev->usb_lock);
899 return ret;
900}
901
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300902static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300903{
904 int i;
905
906 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
907 gspca_dev->curr_mode = i;
908 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
909 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300910 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300911}
912
913static int wxh_to_mode(struct gspca_dev *gspca_dev,
914 int width, int height)
915{
916 int i;
917
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300918 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
919 if (width >= gspca_dev->cam.cam_mode[i].width
920 && height >= gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300921 break;
922 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300923 return i;
924}
925
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300926/*
927 * search a mode with the right pixel format
928 */
929static int gspca_get_mode(struct gspca_dev *gspca_dev,
930 int mode,
931 int pixfmt)
932{
933 int modeU, modeD;
934
935 modeU = modeD = mode;
936 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
937 if (--modeD >= 0) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300938 if (gspca_dev->cam.cam_mode[modeD].pixelformat
939 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300940 return modeD;
941 }
942 if (++modeU < gspca_dev->cam.nmodes) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300943 if (gspca_dev->cam.cam_mode[modeU].pixelformat
944 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300945 return modeU;
946 }
947 }
948 return -EINVAL;
949}
950
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300951#ifdef CONFIG_VIDEO_ADV_DEBUG
952static int vidioc_g_register(struct file *file, void *priv,
953 struct v4l2_dbg_register *reg)
954{
955 int ret;
956 struct gspca_dev *gspca_dev = priv;
957
958 if (!gspca_dev->sd_desc->get_chip_ident)
959 return -EINVAL;
960
961 if (!gspca_dev->sd_desc->get_register)
962 return -EINVAL;
963
964 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
965 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -0300966 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300967 if (gspca_dev->present)
968 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
969 else
970 ret = -ENODEV;
971 mutex_unlock(&gspca_dev->usb_lock);
972
973 return ret;
974}
975
976static int vidioc_s_register(struct file *file, void *priv,
977 struct v4l2_dbg_register *reg)
978{
979 int ret;
980 struct gspca_dev *gspca_dev = priv;
981
982 if (!gspca_dev->sd_desc->get_chip_ident)
983 return -EINVAL;
984
985 if (!gspca_dev->sd_desc->set_register)
986 return -EINVAL;
987
988 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
989 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -0300990 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300991 if (gspca_dev->present)
992 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
993 else
994 ret = -ENODEV;
995 mutex_unlock(&gspca_dev->usb_lock);
996
997 return ret;
998}
999#endif
1000
1001static int vidioc_g_chip_ident(struct file *file, void *priv,
1002 struct v4l2_dbg_chip_ident *chip)
1003{
1004 int ret;
1005 struct gspca_dev *gspca_dev = priv;
1006
1007 if (!gspca_dev->sd_desc->get_chip_ident)
1008 return -EINVAL;
1009
1010 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1011 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001012 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03001013 if (gspca_dev->present)
1014 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
1015 else
1016 ret = -ENODEV;
1017 mutex_unlock(&gspca_dev->usb_lock);
1018
1019 return ret;
1020}
1021
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001022static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001023 struct v4l2_fmtdesc *fmtdesc)
1024{
1025 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001026 int i, j, index;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001027 __u32 fmt_tb[8];
1028
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001029 /* give an index to each format */
1030 index = 0;
1031 j = 0;
1032 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001033 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001034 j = 0;
1035 for (;;) {
1036 if (fmt_tb[j] == fmt_tb[index])
1037 break;
1038 j++;
1039 }
1040 if (j == index) {
1041 if (fmtdesc->index == index)
1042 break; /* new format */
1043 index++;
Julia Lawall80297122008-11-12 23:18:21 -03001044 if (index >= ARRAY_SIZE(fmt_tb))
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001045 return -EINVAL;
1046 }
1047 }
1048 if (i < 0)
1049 return -EINVAL; /* no more format */
1050
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001051 fmtdesc->pixelformat = fmt_tb[index];
1052 if (gspca_is_compressed(fmt_tb[index]))
1053 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
1054 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
1055 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
1056 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
1057 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
1058 fmtdesc->description[4] = '\0';
1059 return 0;
1060}
1061
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001062static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001063 struct v4l2_format *fmt)
1064{
1065 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001066 int mode;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001067
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001068 mode = gspca_dev->curr_mode;
1069 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1070 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001071 return 0;
1072}
1073
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001074static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001075 struct v4l2_format *fmt)
1076{
Hans de Goede24c530b2008-07-04 17:55:18 -03001077 int w, h, mode, mode2;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001078
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001079 w = fmt->fmt.pix.width;
1080 h = fmt->fmt.pix.height;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001081
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001082#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001083 if (gspca_debug & D_CONF)
1084 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
1085#endif
1086 /* search the closest mode for width and height */
1087 mode = wxh_to_mode(gspca_dev, w, h);
1088
1089 /* OK if right palette */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001090 if (gspca_dev->cam.cam_mode[mode].pixelformat
1091 != fmt->fmt.pix.pixelformat) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001092
1093 /* else, search the closest mode with the same pixel format */
1094 mode2 = gspca_get_mode(gspca_dev, mode,
1095 fmt->fmt.pix.pixelformat);
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001096 if (mode2 >= 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001097 mode = mode2;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001098/* else
1099 ; * no chance, return this mode */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001100 }
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001101 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1102 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001103 return mode; /* used when s_fmt */
1104}
1105
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001106static int vidioc_try_fmt_vid_cap(struct file *file,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001107 void *priv,
1108 struct v4l2_format *fmt)
1109{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001110 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001111 int ret;
1112
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001113 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001114 if (ret < 0)
1115 return ret;
1116 return 0;
1117}
1118
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001119static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001120 struct v4l2_format *fmt)
1121{
1122 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001123 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001124
Hans de Goedee2997a72008-04-23 08:09:12 -03001125 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1126 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001127
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001128 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001129 if (ret < 0)
1130 goto out;
1131
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001132 if (gspca_dev->nframes != 0
1133 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
1134 ret = -EINVAL;
1135 goto out;
1136 }
1137
Jean-Francois Moine50a871f2008-06-30 19:47:33 -03001138 if (ret == gspca_dev->curr_mode) {
1139 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001140 goto out; /* same mode */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -03001141 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001142
1143 if (gspca_dev->streaming) {
1144 ret = -EBUSY;
1145 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -03001146 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001147 gspca_dev->width = fmt->fmt.pix.width;
1148 gspca_dev->height = fmt->fmt.pix.height;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001149 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
1150 gspca_dev->curr_mode = ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001151
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001152 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001153out:
1154 mutex_unlock(&gspca_dev->queue_lock);
1155 return ret;
1156}
1157
Hans de Goede11c635a2009-05-25 15:04:22 -03001158static int vidioc_enum_framesizes(struct file *file, void *priv,
1159 struct v4l2_frmsizeenum *fsize)
1160{
1161 struct gspca_dev *gspca_dev = priv;
1162 int i;
1163 __u32 index = 0;
1164
1165 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
1166 if (fsize->pixel_format !=
1167 gspca_dev->cam.cam_mode[i].pixelformat)
1168 continue;
1169
1170 if (fsize->index == index) {
1171 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1172 fsize->discrete.width =
1173 gspca_dev->cam.cam_mode[i].width;
1174 fsize->discrete.height =
1175 gspca_dev->cam.cam_mode[i].height;
1176 return 0;
1177 }
1178 index++;
1179 }
1180
1181 return -EINVAL;
1182}
1183
Antonio Ospite28ffe772009-12-02 06:18:46 -03001184static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1185 struct v4l2_frmivalenum *fival)
1186{
1187 struct gspca_dev *gspca_dev = priv;
1188 int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
1189 __u32 i;
1190
1191 if (gspca_dev->cam.mode_framerates == NULL ||
1192 gspca_dev->cam.mode_framerates[mode].nrates == 0)
1193 return -EINVAL;
1194
1195 if (fival->pixel_format !=
1196 gspca_dev->cam.cam_mode[mode].pixelformat)
1197 return -EINVAL;
1198
1199 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1200 if (fival->index == i) {
1201 fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1202 fival->discrete.numerator = 1;
1203 fival->discrete.denominator =
1204 gspca_dev->cam.mode_framerates[mode].rates[i];
1205 return 0;
1206 }
1207 }
1208
1209 return -EINVAL;
1210}
1211
Hans de Goeded0182302008-11-19 17:10:47 -03001212static void gspca_release(struct video_device *vfd)
Frank Zago6b060ff2008-09-28 08:12:22 -03001213{
Hans de Goeded0182302008-11-19 17:10:47 -03001214 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
Frank Zago6b060ff2008-09-28 08:12:22 -03001215
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001216 PDEBUG(D_PROBE, "%s released",
1217 video_device_node_name(&gspca_dev->vdev));
Frank Zago6b060ff2008-09-28 08:12:22 -03001218
1219 kfree(gspca_dev->usb_buf);
1220 kfree(gspca_dev);
1221}
1222
Hans Verkuilbec43662008-12-30 06:58:20 -03001223static int dev_open(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001224{
1225 struct gspca_dev *gspca_dev;
1226 int ret;
1227
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001228 PDEBUG(D_STREAM, "[%s] open", current->comm);
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001229 gspca_dev = (struct gspca_dev *) video_devdata(file);
Hans de Goedee2997a72008-04-23 08:09:12 -03001230 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1231 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001232 if (!gspca_dev->present) {
1233 ret = -ENODEV;
1234 goto out;
1235 }
1236
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001237 if (gspca_dev->users > 4) { /* (arbitrary value) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001238 ret = -EBUSY;
1239 goto out;
1240 }
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03001241
1242 /* protect the subdriver against rmmod */
1243 if (!try_module_get(gspca_dev->module)) {
1244 ret = -ENODEV;
1245 goto out;
1246 }
1247
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001248 gspca_dev->users++;
Frank Zago6b060ff2008-09-28 08:12:22 -03001249
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001250 file->private_data = gspca_dev;
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001251#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001252 /* activate the v4l2 debug */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001253 if (gspca_debug & D_V4L2)
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001254 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001255 | V4L2_DEBUG_IOCTL_ARG;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001256 else
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001257 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001258 | V4L2_DEBUG_IOCTL_ARG);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001259#endif
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001260 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001261out:
1262 mutex_unlock(&gspca_dev->queue_lock);
1263 if (ret != 0)
1264 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
1265 else
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001266 PDEBUG(D_STREAM, "open done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001267 return ret;
1268}
1269
Hans Verkuilbec43662008-12-30 06:58:20 -03001270static int dev_close(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001271{
1272 struct gspca_dev *gspca_dev = file->private_data;
1273
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001274 PDEBUG(D_STREAM, "[%s] close", current->comm);
Hans de Goedee2997a72008-04-23 08:09:12 -03001275 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1276 return -ERESTARTSYS;
1277 gspca_dev->users--;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001278
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001279 /* if the file did the capture, free the streaming resources */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001280 if (gspca_dev->capt_file == file) {
Jean-Francois Moinea1e82882008-09-03 17:12:17 -03001281 if (gspca_dev->streaming) {
1282 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001283 gspca_dev->usb_err = 0;
Jean-Francois Moinea1e82882008-09-03 17:12:17 -03001284 gspca_stream_off(gspca_dev);
1285 mutex_unlock(&gspca_dev->usb_lock);
1286 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001287 frame_free(gspca_dev);
Harvey Harrisonefab8212008-07-05 06:12:47 -03001288 gspca_dev->capt_file = NULL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001289 gspca_dev->memory = GSPCA_MEMORY_NO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001290 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001291 file->private_data = NULL;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03001292 module_put(gspca_dev->module);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001293 mutex_unlock(&gspca_dev->queue_lock);
Frank Zago6b060ff2008-09-28 08:12:22 -03001294
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001295 PDEBUG(D_STREAM, "close done");
Frank Zago6b060ff2008-09-28 08:12:22 -03001296
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001297 return 0;
1298}
1299
1300static int vidioc_querycap(struct file *file, void *priv,
1301 struct v4l2_capability *cap)
1302{
1303 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001304 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001305
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001306 /* protect the access to the usb device */
1307 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1308 return -ERESTARTSYS;
1309 if (!gspca_dev->present) {
1310 ret = -ENODEV;
1311 goto out;
1312 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001313 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -03001314 if (gspca_dev->dev->product != NULL) {
1315 strncpy(cap->card, gspca_dev->dev->product,
1316 sizeof cap->card);
1317 } else {
1318 snprintf(cap->card, sizeof cap->card,
1319 "USB Camera (%04x:%04x)",
1320 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1321 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1322 }
Thierry MERLE59320282009-01-21 15:32:26 -03001323 usb_make_path(gspca_dev->dev, cap->bus_info, sizeof(cap->bus_info));
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001324 cap->version = DRIVER_VERSION_NUMBER;
1325 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1326 | V4L2_CAP_STREAMING
1327 | V4L2_CAP_READWRITE;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001328 ret = 0;
1329out:
1330 mutex_unlock(&gspca_dev->usb_lock);
1331 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001332}
1333
Hans de Goedea0001a22009-06-10 04:52:18 -03001334static const struct ctrl *get_ctrl(struct gspca_dev *gspca_dev,
1335 int id)
1336{
1337 const struct ctrl *ctrls;
1338 int i;
1339
1340 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1341 i < gspca_dev->sd_desc->nctrls;
1342 i++, ctrls++) {
1343 if (gspca_dev->ctrl_dis & (1 << i))
1344 continue;
1345 if (id == ctrls->qctrl.id)
1346 return ctrls;
1347 }
1348 return NULL;
1349}
1350
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001351static int vidioc_queryctrl(struct file *file, void *priv,
1352 struct v4l2_queryctrl *q_ctrl)
1353{
1354 struct gspca_dev *gspca_dev = priv;
Hans de Goedea0001a22009-06-10 04:52:18 -03001355 const struct ctrl *ctrls;
1356 int i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001357 u32 id;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001358
Hans de Goedea0001a22009-06-10 04:52:18 -03001359 ctrls = NULL;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001360 id = q_ctrl->id;
1361 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1362 id &= V4L2_CTRL_ID_MASK;
1363 id++;
1364 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001365 if (gspca_dev->ctrl_dis & (1 << i))
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001366 continue;
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001367 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001368 continue;
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001369 if (ctrls && gspca_dev->sd_desc->ctrls[i].qctrl.id
Hans de Goedea0001a22009-06-10 04:52:18 -03001370 > ctrls->qctrl.id)
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001371 continue;
Hans de Goedea0001a22009-06-10 04:52:18 -03001372 ctrls = &gspca_dev->sd_desc->ctrls[i];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001373 }
Jean-Francois Moine83955552009-12-12 06:58:01 -03001374 if (ctrls == NULL)
1375 return -EINVAL;
Hans de Goedea0001a22009-06-10 04:52:18 -03001376 } else {
1377 ctrls = get_ctrl(gspca_dev, id);
Jean-Francois Moine83955552009-12-12 06:58:01 -03001378 if (ctrls == NULL)
1379 return -EINVAL;
Jean-Francois Moine4af85662009-11-12 15:59:27 -03001380 i = ctrls - gspca_dev->sd_desc->ctrls;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001381 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001382 memcpy(q_ctrl, ctrls, sizeof *q_ctrl);
Jean-Francois Moine4af85662009-11-12 15:59:27 -03001383 if (gspca_dev->ctrl_inac & (1 << i))
1384 q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001385 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001386}
1387
1388static int vidioc_s_ctrl(struct file *file, void *priv,
1389 struct v4l2_control *ctrl)
1390{
1391 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001392 const struct ctrl *ctrls;
Hans de Goedea0001a22009-06-10 04:52:18 -03001393 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001394
Hans de Goedea0001a22009-06-10 04:52:18 -03001395 ctrls = get_ctrl(gspca_dev, ctrl->id);
1396 if (ctrls == NULL)
1397 return -EINVAL;
1398
1399 if (ctrl->value < ctrls->qctrl.minimum
1400 || ctrl->value > ctrls->qctrl.maximum)
1401 return -ERANGE;
1402 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1403 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1404 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001405 gspca_dev->usb_err = 0;
Hans de Goedea0001a22009-06-10 04:52:18 -03001406 if (gspca_dev->present)
1407 ret = ctrls->set(gspca_dev, ctrl->value);
1408 else
1409 ret = -ENODEV;
1410 mutex_unlock(&gspca_dev->usb_lock);
1411 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001412}
1413
1414static int vidioc_g_ctrl(struct file *file, void *priv,
1415 struct v4l2_control *ctrl)
1416{
1417 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001418 const struct ctrl *ctrls;
Hans de Goedea0001a22009-06-10 04:52:18 -03001419 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001420
Hans de Goedea0001a22009-06-10 04:52:18 -03001421 ctrls = get_ctrl(gspca_dev, ctrl->id);
1422 if (ctrls == NULL)
1423 return -EINVAL;
1424
1425 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1426 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001427 gspca_dev->usb_err = 0;
Hans de Goedea0001a22009-06-10 04:52:18 -03001428 if (gspca_dev->present)
1429 ret = ctrls->get(gspca_dev, &ctrl->value);
1430 else
1431 ret = -ENODEV;
1432 mutex_unlock(&gspca_dev->usb_lock);
1433 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001434}
1435
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03001436/*fixme: have an audio flag in gspca_dev?*/
1437static int vidioc_s_audio(struct file *file, void *priv,
1438 struct v4l2_audio *audio)
1439{
1440 if (audio->index != 0)
1441 return -EINVAL;
1442 return 0;
1443}
1444
1445static int vidioc_g_audio(struct file *file, void *priv,
1446 struct v4l2_audio *audio)
1447{
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03001448 strcpy(audio->name, "Microphone");
1449 return 0;
1450}
1451
1452static int vidioc_enumaudio(struct file *file, void *priv,
1453 struct v4l2_audio *audio)
1454{
1455 if (audio->index != 0)
1456 return -EINVAL;
1457
1458 strcpy(audio->name, "Microphone");
1459 audio->capability = 0;
1460 audio->mode = 0;
1461 return 0;
1462}
1463
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001464static int vidioc_querymenu(struct file *file, void *priv,
1465 struct v4l2_querymenu *qmenu)
1466{
1467 struct gspca_dev *gspca_dev = priv;
1468
1469 if (!gspca_dev->sd_desc->querymenu)
1470 return -EINVAL;
1471 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1472}
1473
1474static int vidioc_enum_input(struct file *file, void *priv,
1475 struct v4l2_input *input)
1476{
1477 struct gspca_dev *gspca_dev = priv;
1478
1479 if (input->index != 0)
1480 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001481 input->type = V4L2_INPUT_TYPE_CAMERA;
Adam Bakerdfa76fa2009-03-29 19:17:10 -03001482 input->status = gspca_dev->cam.input_flags;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001483 strncpy(input->name, gspca_dev->sd_desc->name,
1484 sizeof input->name);
1485 return 0;
1486}
1487
1488static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1489{
1490 *i = 0;
1491 return 0;
1492}
1493
1494static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1495{
1496 if (i > 0)
1497 return -EINVAL;
1498 return (0);
1499}
1500
1501static int vidioc_reqbufs(struct file *file, void *priv,
1502 struct v4l2_requestbuffers *rb)
1503{
1504 struct gspca_dev *gspca_dev = priv;
Jean-François Moine832d0a92010-04-09 06:07:58 -03001505 int i, ret = 0, streaming;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001506
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001507 switch (rb->memory) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001508 case GSPCA_MEMORY_READ: /* (internal call) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001509 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001510 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001511 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001512 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001513 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001514 }
Hans de Goedee2997a72008-04-23 08:09:12 -03001515 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1516 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001517
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001518 if (gspca_dev->memory != GSPCA_MEMORY_NO
1519 && gspca_dev->memory != rb->memory) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001520 ret = -EBUSY;
1521 goto out;
1522 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001523
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001524 /* only one file may do the capture */
1525 if (gspca_dev->capt_file != NULL
1526 && gspca_dev->capt_file != file) {
1527 ret = -EBUSY;
1528 goto out;
1529 }
1530
1531 /* if allocated, the buffers must not be mapped */
1532 for (i = 0; i < gspca_dev->nframes; i++) {
1533 if (gspca_dev->frame[i].vma_use_count) {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001534 ret = -EBUSY;
1535 goto out;
1536 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001537 }
1538
1539 /* stop streaming */
Jean-François Moine832d0a92010-04-09 06:07:58 -03001540 streaming = gspca_dev->streaming;
1541 if (streaming) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001542 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001543 gspca_dev->usb_err = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001544 gspca_stream_off(gspca_dev);
1545 mutex_unlock(&gspca_dev->usb_lock);
1546 }
1547
1548 /* free the previous allocated buffers, if any */
1549 if (gspca_dev->nframes != 0) {
1550 frame_free(gspca_dev);
1551 gspca_dev->capt_file = NULL;
1552 }
1553 if (rb->count == 0) /* unrequest */
1554 goto out;
1555 gspca_dev->memory = rb->memory;
1556 ret = frame_alloc(gspca_dev, rb->count);
1557 if (ret == 0) {
1558 rb->count = gspca_dev->nframes;
1559 gspca_dev->capt_file = file;
Jean-François Moine832d0a92010-04-09 06:07:58 -03001560 if (streaming)
1561 ret = gspca_init_transfer(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001562 }
1563out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001564 mutex_unlock(&gspca_dev->queue_lock);
1565 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1566 return ret;
1567}
1568
1569static int vidioc_querybuf(struct file *file, void *priv,
1570 struct v4l2_buffer *v4l2_buf)
1571{
1572 struct gspca_dev *gspca_dev = priv;
1573 struct gspca_frame *frame;
1574
Jean-Francois Moine13752bd2008-12-19 15:08:51 -03001575 if (v4l2_buf->index < 0
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001576 || v4l2_buf->index >= gspca_dev->nframes)
1577 return -EINVAL;
1578
1579 frame = &gspca_dev->frame[v4l2_buf->index];
1580 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1581 return 0;
1582}
1583
1584static int vidioc_streamon(struct file *file, void *priv,
1585 enum v4l2_buf_type buf_type)
1586{
1587 struct gspca_dev *gspca_dev = priv;
1588 int ret;
1589
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001590 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1591 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001592 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1593 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001594
Jean-François Moine18bf99a2010-04-09 06:06:11 -03001595 /* check the capture file */
1596 if (gspca_dev->capt_file != file) {
1597 ret = -EBUSY;
1598 goto out;
1599 }
1600
Jean-Francois Moine1edabe72008-12-19 15:17:25 -03001601 if (gspca_dev->nframes == 0
1602 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001603 ret = -EINVAL;
1604 goto out;
1605 }
1606 if (!gspca_dev->streaming) {
1607 ret = gspca_init_transfer(gspca_dev);
1608 if (ret < 0)
1609 goto out;
1610 }
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001611#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001612 if (gspca_debug & D_STREAM) {
1613 PDEBUG_MODE("stream on OK",
1614 gspca_dev->pixfmt,
1615 gspca_dev->width,
1616 gspca_dev->height);
1617 }
1618#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001619 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001620out:
1621 mutex_unlock(&gspca_dev->queue_lock);
1622 return ret;
1623}
1624
1625static int vidioc_streamoff(struct file *file, void *priv,
1626 enum v4l2_buf_type buf_type)
1627{
1628 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001629 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001630
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001631 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1632 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001633 if (!gspca_dev->streaming)
1634 return 0;
1635 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1636 return -ERESTARTSYS;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001637
Jean-François Moine18bf99a2010-04-09 06:06:11 -03001638 /* check the capture file */
1639 if (gspca_dev->capt_file != file) {
1640 ret = -EBUSY;
1641 goto out;
1642 }
1643
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001644 /* stop streaming */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001645 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1646 ret = -ERESTARTSYS;
1647 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001648 }
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001649 gspca_dev->usb_err = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001650 gspca_stream_off(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001651 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001652
1653 /* empty the application queues */
1654 for (i = 0; i < gspca_dev->nframes; i++)
1655 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1656 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1657 gspca_dev->last_packet_type = DISCARD_PACKET;
1658 gspca_dev->sequence = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001659 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001660out:
1661 mutex_unlock(&gspca_dev->queue_lock);
1662 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001663}
1664
1665static int vidioc_g_jpegcomp(struct file *file, void *priv,
1666 struct v4l2_jpegcompression *jpegcomp)
1667{
1668 struct gspca_dev *gspca_dev = priv;
1669 int ret;
1670
1671 if (!gspca_dev->sd_desc->get_jcomp)
1672 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001673 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1674 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001675 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001676 if (gspca_dev->present)
1677 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1678 else
1679 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001680 mutex_unlock(&gspca_dev->usb_lock);
1681 return ret;
1682}
1683
1684static int vidioc_s_jpegcomp(struct file *file, void *priv,
1685 struct v4l2_jpegcompression *jpegcomp)
1686{
1687 struct gspca_dev *gspca_dev = priv;
1688 int ret;
1689
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001690 if (!gspca_dev->sd_desc->set_jcomp)
1691 return -EINVAL;
Jim Parisf86d4a92008-12-10 05:50:44 -03001692 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1693 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001694 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001695 if (gspca_dev->present)
1696 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1697 else
1698 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001699 mutex_unlock(&gspca_dev->usb_lock);
1700 return ret;
1701}
1702
1703static int vidioc_g_parm(struct file *filp, void *priv,
1704 struct v4l2_streamparm *parm)
1705{
1706 struct gspca_dev *gspca_dev = priv;
1707
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001708 parm->parm.capture.readbuffers = gspca_dev->nbufread;
Jim Paris627a5ef2008-12-10 06:02:42 -03001709
1710 if (gspca_dev->sd_desc->get_streamparm) {
1711 int ret;
1712
1713 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1714 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001715 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001716 if (gspca_dev->present)
1717 ret = gspca_dev->sd_desc->get_streamparm(gspca_dev,
1718 parm);
1719 else
1720 ret = -ENODEV;
Jim Paris627a5ef2008-12-10 06:02:42 -03001721 mutex_unlock(&gspca_dev->usb_lock);
1722 return ret;
1723 }
1724
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001725 return 0;
1726}
1727
1728static int vidioc_s_parm(struct file *filp, void *priv,
1729 struct v4l2_streamparm *parm)
1730{
1731 struct gspca_dev *gspca_dev = priv;
1732 int n;
1733
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001734 n = parm->parm.capture.readbuffers;
1735 if (n == 0 || n > GSPCA_MAX_FRAMES)
1736 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1737 else
1738 gspca_dev->nbufread = n;
Jim Paris627a5ef2008-12-10 06:02:42 -03001739
1740 if (gspca_dev->sd_desc->set_streamparm) {
1741 int ret;
1742
1743 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1744 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001745 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001746 if (gspca_dev->present)
1747 ret = gspca_dev->sd_desc->set_streamparm(gspca_dev,
1748 parm);
1749 else
1750 ret = -ENODEV;
Jim Paris627a5ef2008-12-10 06:02:42 -03001751 mutex_unlock(&gspca_dev->usb_lock);
1752 return ret;
1753 }
1754
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001755 return 0;
1756}
1757
1758#ifdef CONFIG_VIDEO_V4L1_COMPAT
1759static int vidiocgmbuf(struct file *file, void *priv,
1760 struct video_mbuf *mbuf)
1761{
1762 struct gspca_dev *gspca_dev = file->private_data;
1763 int i;
1764
1765 PDEBUG(D_STREAM, "cgmbuf");
1766 if (gspca_dev->nframes == 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001767 int ret;
1768
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001769 {
1770 struct v4l2_format fmt;
1771
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001772 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1773 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1774 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1775 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1776 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001777 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001778 if (ret != 0)
1779 return ret;
1780 }
1781 {
1782 struct v4l2_requestbuffers rb;
1783
1784 memset(&rb, 0, sizeof rb);
1785 rb.count = 4;
1786 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1787 rb.memory = V4L2_MEMORY_MMAP;
1788 ret = vidioc_reqbufs(file, priv, &rb);
1789 if (ret != 0)
1790 return ret;
1791 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001792 }
1793 mbuf->frames = gspca_dev->nframes;
1794 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1795 for (i = 0; i < mbuf->frames; i++)
1796 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1797 return 0;
1798}
1799#endif
1800
1801static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1802{
1803 struct gspca_dev *gspca_dev = file->private_data;
Harvey Harrisonefab8212008-07-05 06:12:47 -03001804 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001805 struct page *page;
1806 unsigned long addr, start, size;
1807 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001808
1809 start = vma->vm_start;
1810 size = vma->vm_end - vma->vm_start;
1811 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1812
Hans de Goedee2997a72008-04-23 08:09:12 -03001813 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1814 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001815 if (!gspca_dev->present) {
1816 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001817 goto out;
1818 }
1819 if (gspca_dev->capt_file != file) {
1820 ret = -EINVAL;
1821 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001822 }
1823
Harvey Harrisonefab8212008-07-05 06:12:47 -03001824 frame = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001825 for (i = 0; i < gspca_dev->nframes; ++i) {
1826 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1827 PDEBUG(D_STREAM, "mmap bad memory type");
1828 break;
1829 }
1830 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1831 == vma->vm_pgoff) {
1832 frame = &gspca_dev->frame[i];
1833 break;
1834 }
1835 }
Harvey Harrisonefab8212008-07-05 06:12:47 -03001836 if (frame == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001837 PDEBUG(D_STREAM, "mmap no frame buffer found");
1838 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001839 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001840 }
1841#ifdef CONFIG_VIDEO_V4L1_COMPAT
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001842 /* v4l1 maps all the buffers */
1843 if (i != 0
1844 || size != frame->v4l2_buf.length * gspca_dev->nframes)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001845#endif
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001846 if (size != frame->v4l2_buf.length) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001847 PDEBUG(D_STREAM, "mmap bad size");
1848 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001849 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001850 }
1851
1852 /*
1853 * - VM_IO marks the area as being a mmaped region for I/O to a
1854 * device. It also prevents the region from being core dumped.
1855 */
1856 vma->vm_flags |= VM_IO;
1857
1858 addr = (unsigned long) frame->data;
1859 while (size > 0) {
1860 page = vmalloc_to_page((void *) addr);
1861 ret = vm_insert_page(vma, start, page);
1862 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001863 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001864 start += PAGE_SIZE;
1865 addr += PAGE_SIZE;
1866 size -= PAGE_SIZE;
1867 }
1868
Jean-Francois Moined50e2572009-11-26 15:36:40 -03001869 vma->vm_ops = &gspca_vm_ops;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001870 vma->vm_private_data = frame;
1871 gspca_vm_open(vma);
Hans de Goedee2997a72008-04-23 08:09:12 -03001872 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001873out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001874 mutex_unlock(&gspca_dev->queue_lock);
1875 return ret;
1876}
1877
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001878/*
1879 * wait for a video frame
1880 *
1881 * If a frame is ready, its index is returned.
1882 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001883static int frame_wait(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001884 int nonblock_ing)
1885{
1886 struct gspca_frame *frame;
1887 int i, j, ret;
1888
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001889 /* check if a frame is ready */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001890 i = gspca_dev->fr_o;
1891 j = gspca_dev->fr_queue[i];
1892 frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001893
Hans de Goede181b7042008-11-19 17:16:26 -03001894 if (!(frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)) {
1895 if (nonblock_ing)
1896 return -EAGAIN;
1897
1898 /* wait till a frame is ready */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001899 ret = wait_event_interruptible_timeout(gspca_dev->wq,
Hans de Goede181b7042008-11-19 17:16:26 -03001900 (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) ||
1901 !gspca_dev->streaming || !gspca_dev->present,
1902 msecs_to_jiffies(3000));
1903 if (ret < 0)
1904 return ret;
1905 if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present)
Hans de Goedee2997a72008-04-23 08:09:12 -03001906 return -EIO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001907 }
Hans de Goede181b7042008-11-19 17:16:26 -03001908
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001909 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1910 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1911 gspca_dev->fr_q,
1912 gspca_dev->fr_i,
1913 gspca_dev->fr_o);
Hans de Goedee2997a72008-04-23 08:09:12 -03001914
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001915 if (gspca_dev->sd_desc->dq_callback) {
1916 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001917 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001918 if (gspca_dev->present)
1919 gspca_dev->sd_desc->dq_callback(gspca_dev);
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001920 mutex_unlock(&gspca_dev->usb_lock);
1921 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001922 return j;
1923}
1924
1925/*
1926 * dequeue a video buffer
1927 *
1928 * If nonblock_ing is false, block until a buffer is available.
1929 */
1930static int vidioc_dqbuf(struct file *file, void *priv,
1931 struct v4l2_buffer *v4l2_buf)
1932{
1933 struct gspca_dev *gspca_dev = priv;
1934 struct gspca_frame *frame;
1935 int i, ret;
1936
1937 PDEBUG(D_FRAM, "dqbuf");
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001938 if (v4l2_buf->memory != gspca_dev->memory)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001939 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001940
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001941 if (!gspca_dev->present)
1942 return -ENODEV;
1943
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001944 /* if not streaming, be sure the application will not loop forever */
1945 if (!(file->f_flags & O_NONBLOCK)
1946 && !gspca_dev->streaming && gspca_dev->users == 1)
1947 return -EINVAL;
1948
1949 /* only the capturing file may dequeue */
1950 if (gspca_dev->capt_file != file)
1951 return -EINVAL;
1952
1953 /* only one dequeue / read at a time */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001954 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1955 return -ERESTARTSYS;
1956
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001957 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001958 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001959 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001960 i = ret; /* frame index */
1961 frame = &gspca_dev->frame[i];
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001962 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001963 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001964 frame->data,
1965 frame->v4l2_buf.bytesused)) {
1966 PDEBUG(D_ERR|D_STREAM,
1967 "dqbuf cp to user failed");
1968 ret = -EFAULT;
1969 goto out;
1970 }
1971 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001972 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1973 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1974 PDEBUG(D_FRAM, "dqbuf %d", i);
1975 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001976out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001977 mutex_unlock(&gspca_dev->read_lock);
1978 return ret;
1979}
1980
1981/*
1982 * queue a video buffer
1983 *
1984 * Attempting to queue a buffer that has already been
1985 * queued will return -EINVAL.
1986 */
1987static int vidioc_qbuf(struct file *file, void *priv,
1988 struct v4l2_buffer *v4l2_buf)
1989{
1990 struct gspca_dev *gspca_dev = priv;
1991 struct gspca_frame *frame;
1992 int i, index, ret;
1993
1994 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001995
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001996 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1997 return -ERESTARTSYS;
1998
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001999 index = v4l2_buf->index;
2000 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002001 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002002 "qbuf idx %d >= %d", index, gspca_dev->nframes);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002003 ret = -EINVAL;
2004 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002005 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002006 if (v4l2_buf->memory != gspca_dev->memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002007 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002008 ret = -EINVAL;
2009 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002010 }
2011
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002012 frame = &gspca_dev->frame[index];
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002013 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
2014 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002015 ret = -EINVAL;
2016 goto out;
2017 }
2018
2019 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002020
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002021 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002022 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
2023 frame->v4l2_buf.length = v4l2_buf->length;
2024 }
2025
2026 /* put the buffer in the 'queued' queue */
2027 i = gspca_dev->fr_q;
2028 gspca_dev->fr_queue[i] = index;
Jean-Francois Moine8c32aa52009-12-14 14:57:49 -03002029 if (gspca_dev->fr_i == i)
2030 gspca_dev->cur_frame = frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002031 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
2032 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
2033 gspca_dev->fr_q,
2034 gspca_dev->fr_i,
2035 gspca_dev->fr_o);
2036
2037 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
2038 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
2039 ret = 0;
2040out:
2041 mutex_unlock(&gspca_dev->queue_lock);
2042 return ret;
2043}
2044
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002045/*
2046 * allocate the resources for read()
2047 */
2048static int read_alloc(struct gspca_dev *gspca_dev,
2049 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002050{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002051 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002052 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002053
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002054 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002055 if (gspca_dev->nframes == 0) {
2056 struct v4l2_requestbuffers rb;
2057
2058 memset(&rb, 0, sizeof rb);
2059 rb.count = gspca_dev->nbufread;
2060 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002061 rb.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002062 ret = vidioc_reqbufs(file, gspca_dev, &rb);
2063 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002064 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002065 return ret;
2066 }
2067 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2068 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002069 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002070 for (i = 0; i < gspca_dev->nbufread; i++) {
2071 v4l2_buf.index = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002072 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2073 if (ret != 0) {
2074 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
2075 return ret;
2076 }
2077 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002078 gspca_dev->memory = GSPCA_MEMORY_READ;
2079 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002080
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002081 /* start streaming */
2082 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2083 if (ret != 0)
2084 PDEBUG(D_STREAM, "read streamon err %d", ret);
2085 return ret;
2086}
2087
2088static unsigned int dev_poll(struct file *file, poll_table *wait)
2089{
2090 struct gspca_dev *gspca_dev = file->private_data;
2091 int i, ret;
2092
2093 PDEBUG(D_FRAM, "poll");
2094
2095 poll_wait(file, &gspca_dev->wq, wait);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002096
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002097 /* if reqbufs is not done, the user would use read() */
2098 if (gspca_dev->nframes == 0) {
2099 if (gspca_dev->memory != GSPCA_MEMORY_NO)
2100 return POLLERR; /* not the 1st time */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002101 ret = read_alloc(gspca_dev, file);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002102 if (ret != 0)
2103 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002104 }
2105
2106 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
2107 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002108
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002109 /* check the next incoming buffer */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002110 i = gspca_dev->fr_o;
2111 i = gspca_dev->fr_queue[i];
2112 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
2113 ret = POLLIN | POLLRDNORM; /* something to read */
2114 else
2115 ret = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002116 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002117 if (!gspca_dev->present)
2118 return POLLHUP;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002119 return ret;
2120}
2121
2122static ssize_t dev_read(struct file *file, char __user *data,
2123 size_t count, loff_t *ppos)
2124{
2125 struct gspca_dev *gspca_dev = file->private_data;
2126 struct gspca_frame *frame;
2127 struct v4l2_buffer v4l2_buf;
2128 struct timeval timestamp;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002129 int n, ret, ret2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002130
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03002131 PDEBUG(D_FRAM, "read (%zd)", count);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002132 if (!gspca_dev->present)
2133 return -ENODEV;
2134 switch (gspca_dev->memory) {
2135 case GSPCA_MEMORY_NO: /* first time */
2136 ret = read_alloc(gspca_dev, file);
2137 if (ret != 0)
2138 return ret;
2139 break;
2140 case GSPCA_MEMORY_READ:
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002141 if (gspca_dev->capt_file == file)
2142 break;
2143 /* fall thru */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002144 default:
2145 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002146 }
2147
2148 /* get a frame */
Paulo Assis2f51d002010-03-07 06:56:56 -03002149 timestamp = ktime_to_timeval(ktime_get());
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002150 timestamp.tv_sec--;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002151 n = 2;
2152 for (;;) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002153 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2154 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002155 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002156 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
2157 if (ret != 0) {
2158 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
2159 return ret;
2160 }
2161
2162 /* if the process slept for more than 1 second,
Frank Zago219423c2008-09-26 07:43:54 -03002163 * get a newer frame */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002164 frame = &gspca_dev->frame[v4l2_buf.index];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002165 if (--n < 0)
2166 break; /* avoid infinite loop */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002167 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
2168 break;
2169 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2170 if (ret != 0) {
2171 PDEBUG(D_STREAM, "read qbuf err %d", ret);
2172 return ret;
2173 }
2174 }
2175
2176 /* copy the frame */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002177 if (count > frame->v4l2_buf.bytesused)
2178 count = frame->v4l2_buf.bytesused;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002179 ret = copy_to_user(data, frame->data, count);
2180 if (ret != 0) {
2181 PDEBUG(D_ERR|D_STREAM,
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03002182 "read cp to user lack %d / %zd", ret, count);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002183 ret = -EFAULT;
2184 goto out;
2185 }
2186 ret = count;
2187out:
2188 /* in each case, requeue the buffer */
2189 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2190 if (ret2 != 0)
2191 return ret2;
2192 return ret;
2193}
2194
Hans Verkuilbec43662008-12-30 06:58:20 -03002195static struct v4l2_file_operations dev_fops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002196 .owner = THIS_MODULE,
2197 .open = dev_open,
2198 .release = dev_close,
2199 .read = dev_read,
2200 .mmap = dev_mmap,
Hans Verkuilbec43662008-12-30 06:58:20 -03002201 .unlocked_ioctl = video_ioctl2,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002202 .poll = dev_poll,
2203};
2204
Hans Verkuila3998102008-07-21 02:57:38 -03002205static const struct v4l2_ioctl_ops dev_ioctl_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002206 .vidioc_querycap = vidioc_querycap,
2207 .vidioc_dqbuf = vidioc_dqbuf,
2208 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03002209 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2210 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2211 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2212 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002213 .vidioc_streamon = vidioc_streamon,
2214 .vidioc_queryctrl = vidioc_queryctrl,
2215 .vidioc_g_ctrl = vidioc_g_ctrl,
2216 .vidioc_s_ctrl = vidioc_s_ctrl,
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03002217 .vidioc_g_audio = vidioc_g_audio,
2218 .vidioc_s_audio = vidioc_s_audio,
2219 .vidioc_enumaudio = vidioc_enumaudio,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002220 .vidioc_querymenu = vidioc_querymenu,
2221 .vidioc_enum_input = vidioc_enum_input,
2222 .vidioc_g_input = vidioc_g_input,
2223 .vidioc_s_input = vidioc_s_input,
2224 .vidioc_reqbufs = vidioc_reqbufs,
2225 .vidioc_querybuf = vidioc_querybuf,
2226 .vidioc_streamoff = vidioc_streamoff,
2227 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
2228 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
2229 .vidioc_g_parm = vidioc_g_parm,
2230 .vidioc_s_parm = vidioc_s_parm,
Hans de Goede11c635a2009-05-25 15:04:22 -03002231 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Antonio Ospite28ffe772009-12-02 06:18:46 -03002232 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03002233#ifdef CONFIG_VIDEO_ADV_DEBUG
2234 .vidioc_g_register = vidioc_g_register,
2235 .vidioc_s_register = vidioc_s_register,
2236#endif
2237 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002238#ifdef CONFIG_VIDEO_V4L1_COMPAT
2239 .vidiocgmbuf = vidiocgmbuf,
2240#endif
2241};
2242
Hans Verkuila3998102008-07-21 02:57:38 -03002243static struct video_device gspca_template = {
2244 .name = "gspca main driver",
Hans Verkuila3998102008-07-21 02:57:38 -03002245 .fops = &dev_fops,
2246 .ioctl_ops = &dev_ioctl_ops,
Hans de Goeded0182302008-11-19 17:10:47 -03002247 .release = gspca_release,
Hans Verkuila3998102008-07-21 02:57:38 -03002248};
2249
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002250/*
2251 * probe and create a new gspca device
2252 *
2253 * This function must be called by the sub-driver when it is
2254 * called for probing a new device.
2255 */
2256int gspca_dev_probe(struct usb_interface *intf,
2257 const struct usb_device_id *id,
2258 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002259 int dev_size,
2260 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002261{
2262 struct usb_interface_descriptor *interface;
2263 struct gspca_dev *gspca_dev;
2264 struct usb_device *dev = interface_to_usbdev(intf);
2265 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002266
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002267 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002268
2269 /* we don't handle multi-config cameras */
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002270 if (dev->descriptor.bNumConfigurations != 1) {
2271 PDEBUG(D_ERR, "Too many config");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002272 return -ENODEV;
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002273 }
Jean-Francois Moine52a9d3e2010-01-05 04:44:00 -03002274
Jean-Francois Moineb7a597d2010-01-10 16:36:53 -03002275 /* the USB video interface must be the first one */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002276 interface = &intf->cur_altsetting->desc;
Jean-Francois Moineb7a597d2010-01-10 16:36:53 -03002277 if (dev->config->desc.bNumInterfaces != 1 &&
2278 interface->bInterfaceNumber != 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002279 return -ENODEV;
2280
2281 /* create the device */
2282 if (dev_size < sizeof *gspca_dev)
2283 dev_size = sizeof *gspca_dev;
2284 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
Erik Andrene293e592008-10-03 08:46:50 -03002285 if (!gspca_dev) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002286 err("couldn't kzalloc gspca struct");
Erik Andrene293e592008-10-03 08:46:50 -03002287 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002288 }
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002289 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2290 if (!gspca_dev->usb_buf) {
2291 err("out of memory");
Erik Andrene293e592008-10-03 08:46:50 -03002292 ret = -ENOMEM;
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002293 goto out;
2294 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002295 gspca_dev->dev = dev;
2296 gspca_dev->iface = interface->bInterfaceNumber;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002297 gspca_dev->nbalt = intf->num_altsetting;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002298 gspca_dev->sd_desc = sd_desc;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002299 gspca_dev->nbufread = 2;
Jean-Francois Moineff374742008-10-23 07:29:51 -03002300 gspca_dev->empty_packet = -1; /* don't check the empty packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002301
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002302 /* configure the subdriver and initialize the USB device */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002303 ret = sd_desc->config(gspca_dev, id);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002304 if (ret < 0)
2305 goto out;
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002306 ret = sd_desc->init(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002307 if (ret < 0)
2308 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002309 gspca_set_default_mode(gspca_dev);
2310
Hans de Goede10ee2402010-02-19 04:41:40 -03002311 ret = gspca_input_connect(gspca_dev);
2312 if (ret)
2313 goto out;
2314
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002315 mutex_init(&gspca_dev->usb_lock);
2316 mutex_init(&gspca_dev->read_lock);
2317 mutex_init(&gspca_dev->queue_lock);
2318 init_waitqueue_head(&gspca_dev->wq);
2319
2320 /* init video stuff */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002321 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
Hans de Goede24441632009-03-27 16:08:20 -03002322 gspca_dev->vdev.parent = &intf->dev;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03002323 gspca_dev->module = module;
Oliver Neukumee56a4d2008-07-27 14:01:59 -03002324 gspca_dev->present = 1;
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002325 ret = video_register_device(&gspca_dev->vdev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002326 VFL_TYPE_GRABBER,
Jean-Francois Moine8d584a52009-01-13 06:07:59 -03002327 -1);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002328 if (ret < 0) {
2329 err("video_register_device err %d", ret);
2330 goto out;
2331 }
2332
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002333 usb_set_intfdata(intf, gspca_dev);
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002334 PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
Márton Németh0274d422010-01-28 06:39:49 -03002335
Hans de Goede10ee2402010-02-19 04:41:40 -03002336 gspca_input_create_urb(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002337
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002338 return 0;
2339out:
Randy Dunlapf7628012010-03-30 14:32:04 -03002340#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Hans de Goede10ee2402010-02-19 04:41:40 -03002341 if (gspca_dev->input_dev)
2342 input_unregister_device(gspca_dev->input_dev);
Hans de Goedee7637522010-02-19 05:11:09 -03002343#endif
Jean-Francois Moinee07a1d82008-11-19 06:37:53 -03002344 kfree(gspca_dev->usb_buf);
2345 kfree(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002346 return ret;
2347}
2348EXPORT_SYMBOL(gspca_dev_probe);
2349
2350/*
2351 * USB disconnection
2352 *
2353 * This function must be called by the sub-driver
2354 * when the device disconnects, after the specific resources are freed.
2355 */
2356void gspca_disconnect(struct usb_interface *intf)
2357{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002358 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Randy Dunlapf7628012010-03-30 14:32:04 -03002359#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -03002360 struct input_dev *input_dev;
Jean-François Moinee070bf92010-02-18 15:02:51 -03002361#endif
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002362
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002363 PDEBUG(D_PROBE, "%s disconnect",
2364 video_device_node_name(&gspca_dev->vdev));
Adam Bakerad281272009-02-04 15:33:21 -03002365 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03002366 gspca_dev->present = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002367
2368 if (gspca_dev->streaming) {
2369 destroy_urbs(gspca_dev);
2370 wake_up_interruptible(&gspca_dev->wq);
2371 }
2372
Randy Dunlapf7628012010-03-30 14:32:04 -03002373#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -03002374 gspca_input_destroy_urb(gspca_dev);
2375 input_dev = gspca_dev->input_dev;
2376 if (input_dev) {
2377 gspca_dev->input_dev = NULL;
2378 input_unregister_device(input_dev);
2379 }
Jean-François Moinee070bf92010-02-18 15:02:51 -03002380#endif
Márton Németh0274d422010-01-28 06:39:49 -03002381
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002382 /* the device is freed at exit of this function */
2383 gspca_dev->dev = NULL;
Adam Bakerad281272009-02-04 15:33:21 -03002384 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03002385
Frank Zago6b060ff2008-09-28 08:12:22 -03002386 usb_set_intfdata(intf, NULL);
2387
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002388 /* release the device */
2389 /* (this will call gspca_release() immediatly or on last close) */
2390 video_unregister_device(&gspca_dev->vdev);
Jean-Francois Moine3145b8c2008-10-05 04:22:10 -03002391
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002392/* PDEBUG(D_PROBE, "disconnect complete"); */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002393}
2394EXPORT_SYMBOL(gspca_disconnect);
2395
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002396#ifdef CONFIG_PM
2397int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2398{
2399 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2400
2401 if (!gspca_dev->streaming)
2402 return 0;
2403 gspca_dev->frozen = 1; /* avoid urb error messages */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002404 if (gspca_dev->sd_desc->stopN)
2405 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002406 destroy_urbs(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002407 gspca_input_destroy_urb(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002408 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002409 if (gspca_dev->sd_desc->stop0)
2410 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002411 return 0;
2412}
2413EXPORT_SYMBOL(gspca_suspend);
2414
2415int gspca_resume(struct usb_interface *intf)
2416{
2417 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2418
2419 gspca_dev->frozen = 0;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002420 gspca_dev->sd_desc->init(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002421 gspca_input_create_urb(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002422 if (gspca_dev->streaming)
2423 return gspca_init_transfer(gspca_dev);
Jean-Francois Moine1e89e2da2008-09-03 17:12:15 -03002424 return 0;
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002425}
2426EXPORT_SYMBOL(gspca_resume);
2427#endif
Hans de Goededcef32372008-07-10 10:40:53 -03002428/* -- cam driver utility functions -- */
2429
2430/* auto gain and exposure algorithm based on the knee algorithm described here:
2431 http://ytse.tricolour.net/docs/LowLightOptimization.html
2432
2433 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2434 where changed. */
2435int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2436 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2437{
2438 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2439 const struct ctrl *gain_ctrl = NULL;
2440 const struct ctrl *exposure_ctrl = NULL;
2441 const struct ctrl *autogain_ctrl = NULL;
2442 int retval = 0;
2443
2444 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goede26984b02010-02-01 13:18:37 -03002445 if (gspca_dev->ctrl_dis & (1 << i))
2446 continue;
Hans de Goededcef32372008-07-10 10:40:53 -03002447 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2448 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2449 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2450 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2451 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2452 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2453 }
2454 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2455 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2456 "on cam without (auto)gain/exposure");
2457 return 0;
2458 }
2459
2460 if (gain_ctrl->get(gspca_dev, &gain) ||
2461 exposure_ctrl->get(gspca_dev, &exposure) ||
2462 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2463 return 0;
2464
2465 orig_gain = gain;
2466 orig_exposure = exposure;
2467
2468 /* If we are of a multiple of deadzone, do multiple steps to reach the
2469 desired lumination fast (with the risc of a slight overshoot) */
2470 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2471
Jean-Francois Moine1c44d812008-11-10 05:56:55 -03002472 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
Hans de Goededcef32372008-07-10 10:40:53 -03002473 avg_lum, desired_avg_lum, steps);
2474
2475 for (i = 0; i < steps; i++) {
2476 if (avg_lum > desired_avg_lum) {
2477 if (gain > gain_knee)
2478 gain--;
2479 else if (exposure > exposure_knee)
2480 exposure--;
2481 else if (gain > gain_ctrl->qctrl.default_value)
2482 gain--;
2483 else if (exposure > exposure_ctrl->qctrl.minimum)
2484 exposure--;
2485 else if (gain > gain_ctrl->qctrl.minimum)
2486 gain--;
2487 else
2488 break;
2489 } else {
2490 if (gain < gain_ctrl->qctrl.default_value)
2491 gain++;
2492 else if (exposure < exposure_knee)
2493 exposure++;
2494 else if (gain < gain_knee)
2495 gain++;
2496 else if (exposure < exposure_ctrl->qctrl.maximum)
2497 exposure++;
2498 else if (gain < gain_ctrl->qctrl.maximum)
2499 gain++;
2500 else
2501 break;
2502 }
2503 }
2504
2505 if (gain != orig_gain) {
2506 gain_ctrl->set(gspca_dev, gain);
2507 retval = 1;
2508 }
2509 if (exposure != orig_exposure) {
2510 exposure_ctrl->set(gspca_dev, exposure);
2511 retval = 1;
2512 }
2513
2514 return retval;
2515}
2516EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2517
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002518/* -- module insert / remove -- */
2519static int __init gspca_init(void)
2520{
Jean-Francois Moinebb64e862008-07-19 06:49:28 -03002521 info("main v%d.%d.%d registered",
2522 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2523 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2524 DRIVER_VERSION_NUMBER & 0xff);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002525 return 0;
2526}
2527static void __exit gspca_exit(void)
2528{
2529 info("main deregistered");
2530}
2531
2532module_init(gspca_init);
2533module_exit(gspca_exit);
2534
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03002535#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002536module_param_named(debug, gspca_debug, int, 0644);
2537MODULE_PARM_DESC(debug,
2538 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002539 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
2540 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002541#endif