blob: 0a7af737150946f0b0903d08ca8afeb343a7bb94 [file] [log] [blame]
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001/*
2 * Main USB camera driver
3 *
Jean-François Moine27c6f9e2010-04-02 06:59:13 -03004 * Copyright (C) 2008-2010 Jean-François Moine <http://moinejf.free.fr>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03005 *
Márton Németh0274d422010-01-28 06:39:49 -03006 * Camera button input handling by Márton Németh
7 * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu>
8 *
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03009 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#define MODULE_NAME "gspca"
25
26#include <linux/init.h>
Jean-Francois Moinecf0fe082008-10-17 04:36:47 -030027#include <linux/version.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030028#include <linux/fs.h>
29#include <linux/vmalloc.h>
30#include <linux/sched.h>
31#include <linux/slab.h>
32#include <linux/mm.h>
33#include <linux/string.h>
34#include <linux/pagemap.h>
Jean-Francois Moine956e42d2008-07-01 10:03:42 -030035#include <linux/io.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030036#include <asm/page.h>
Jean-Francois Moine956e42d2008-07-01 10:03:42 -030037#include <linux/uaccess.h>
Paulo Assis2f51d002010-03-07 06:56:56 -030038#include <linux/ktime.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030039#include <media/v4l2-ioctl.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030040
41#include "gspca.h"
42
Randy Dunlapf7628012010-03-30 14:32:04 -030043#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -030044#include <linux/input.h>
45#include <linux/usb/input.h>
Jean-François Moinee070bf92010-02-18 15:02:51 -030046#endif
Márton Németh0274d422010-01-28 06:39:49 -030047
Jean-Francois Moined43fa322008-06-12 10:58:58 -030048/* global values */
Jean-Francois Moine8fee8452009-01-17 04:46:38 -030049#define DEF_NURBS 3 /* default number of URBs */
50#if DEF_NURBS > MAX_NURBS
51#error "DEF_NURBS too big"
52#endif
Jean-Francois Moined43fa322008-06-12 10:58:58 -030053
Jean-François Moine27c6f9e2010-04-02 06:59:13 -030054MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030055MODULE_DESCRIPTION("GSPCA USB Camera Driver");
56MODULE_LICENSE("GPL");
57
Jean-François Moine0b8851b2010-10-29 07:56:06 -030058#define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 11, 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030059
Jean-Francois Moine335b3f82008-07-30 04:53:02 -030060#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030061int gspca_debug = D_ERR | D_PROBE;
62EXPORT_SYMBOL(gspca_debug);
63
64static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
65{
66 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
67 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
68 txt,
69 pixfmt & 0xff,
70 (pixfmt >> 8) & 0xff,
71 (pixfmt >> 16) & 0xff,
72 pixfmt >> 24,
73 w, h);
74 } else {
75 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
76 txt,
77 pixfmt,
78 w, h);
79 }
80}
81#else
82#define PDEBUG_MODE(txt, pixfmt, w, h)
83#endif
84
Jean-Francois Moine77ee3312009-11-07 15:30:50 -030085/* specific memory types - !! should be different from V4L2_MEMORY_xxx */
Jean-Francois Moined43fa322008-06-12 10:58:58 -030086#define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
87#define GSPCA_MEMORY_READ 7
88
Jean-Francois Moined43fa322008-06-12 10:58:58 -030089#define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
Jean-Francois Moined43fa322008-06-12 10:58:58 -030090
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030091/*
92 * VMA operations.
93 */
94static void gspca_vm_open(struct vm_area_struct *vma)
95{
96 struct gspca_frame *frame = vma->vm_private_data;
97
98 frame->vma_use_count++;
99 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
100}
101
102static void gspca_vm_close(struct vm_area_struct *vma)
103{
104 struct gspca_frame *frame = vma->vm_private_data;
105
106 if (--frame->vma_use_count <= 0)
107 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
108}
109
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400110static const struct vm_operations_struct gspca_vm_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300111 .open = gspca_vm_open,
112 .close = gspca_vm_close,
113};
114
Márton Németh0274d422010-01-28 06:39:49 -0300115/*
116 * Input and interrupt endpoint handling functions
117 */
Randy Dunlapf7628012010-03-30 14:32:04 -0300118#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -0300119static void int_irq(struct urb *urb)
120{
121 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
122 int ret;
123
124 ret = urb->status;
125 switch (ret) {
126 case 0:
127 if (gspca_dev->sd_desc->int_pkt_scan(gspca_dev,
128 urb->transfer_buffer, urb->actual_length) < 0) {
129 PDEBUG(D_ERR, "Unknown packet received");
130 }
131 break;
132
133 case -ENOENT:
134 case -ECONNRESET:
135 case -ENODEV:
136 case -ESHUTDOWN:
137 /* Stop is requested either by software or hardware is gone,
138 * keep the ret value non-zero and don't resubmit later.
139 */
140 break;
141
142 default:
143 PDEBUG(D_ERR, "URB error %i, resubmitting", urb->status);
144 urb->status = 0;
145 ret = 0;
146 }
147
148 if (ret == 0) {
149 ret = usb_submit_urb(urb, GFP_ATOMIC);
150 if (ret < 0)
Jean-François Moine0b656322010-09-13 05:19:58 -0300151 err("Resubmit URB failed with error %i", ret);
Márton Németh0274d422010-01-28 06:39:49 -0300152 }
153}
154
155static int gspca_input_connect(struct gspca_dev *dev)
156{
157 struct input_dev *input_dev;
158 int err = 0;
159
160 dev->input_dev = NULL;
Hans de Goedeac82f592010-02-19 04:28:39 -0300161 if (dev->sd_desc->int_pkt_scan || dev->sd_desc->other_input) {
Márton Németh0274d422010-01-28 06:39:49 -0300162 input_dev = input_allocate_device();
163 if (!input_dev)
164 return -ENOMEM;
165
166 usb_make_path(dev->dev, dev->phys, sizeof(dev->phys));
167 strlcat(dev->phys, "/input0", sizeof(dev->phys));
168
169 input_dev->name = dev->sd_desc->name;
170 input_dev->phys = dev->phys;
171
172 usb_to_input_id(dev->dev, &input_dev->id);
173
174 input_dev->evbit[0] = BIT_MASK(EV_KEY);
175 input_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
176 input_dev->dev.parent = &dev->dev->dev;
177
178 err = input_register_device(input_dev);
179 if (err) {
Jean-François Moine0b656322010-09-13 05:19:58 -0300180 err("Input device registration failed with error %i",
181 err);
Márton Németh0274d422010-01-28 06:39:49 -0300182 input_dev->dev.parent = NULL;
183 input_free_device(input_dev);
184 } else {
185 dev->input_dev = input_dev;
186 }
Hans de Goede10ee2402010-02-19 04:41:40 -0300187 }
Márton Németh0274d422010-01-28 06:39:49 -0300188
189 return err;
190}
191
192static int alloc_and_submit_int_urb(struct gspca_dev *gspca_dev,
193 struct usb_endpoint_descriptor *ep)
194{
195 unsigned int buffer_len;
196 int interval;
197 struct urb *urb;
198 struct usb_device *dev;
199 void *buffer = NULL;
200 int ret = -EINVAL;
201
Jean-François Moineb26d6e22010-04-29 06:01:53 -0300202 buffer_len = le16_to_cpu(ep->wMaxPacketSize);
Márton Németh0274d422010-01-28 06:39:49 -0300203 interval = ep->bInterval;
Jean-François Moinec4dc6922010-07-14 06:26:54 -0300204 PDEBUG(D_CONF, "found int in endpoint: 0x%x, "
Márton Németh0274d422010-01-28 06:39:49 -0300205 "buffer_len=%u, interval=%u",
206 ep->bEndpointAddress, buffer_len, interval);
207
208 dev = gspca_dev->dev;
209
210 urb = usb_alloc_urb(0, GFP_KERNEL);
211 if (!urb) {
212 ret = -ENOMEM;
213 goto error;
214 }
215
Linus Torvalds7a9b1492010-05-20 21:26:12 -0700216 buffer = usb_alloc_coherent(dev, buffer_len,
Márton Németh0274d422010-01-28 06:39:49 -0300217 GFP_KERNEL, &urb->transfer_dma);
218 if (!buffer) {
219 ret = -ENOMEM;
220 goto error_buffer;
221 }
222 usb_fill_int_urb(urb, dev,
223 usb_rcvintpipe(dev, ep->bEndpointAddress),
224 buffer, buffer_len,
225 int_irq, (void *)gspca_dev, interval);
Jason Wang882787f2010-09-03 06:57:19 -0300226 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Márton Németh0274d422010-01-28 06:39:49 -0300227 ret = usb_submit_urb(urb, GFP_KERNEL);
228 if (ret < 0) {
Jean-François Moinec4dc6922010-07-14 06:26:54 -0300229 PDEBUG(D_ERR, "submit int URB failed with error %i", ret);
Márton Németh0274d422010-01-28 06:39:49 -0300230 goto error_submit;
231 }
Hans de Goede2661da42010-10-26 05:16:32 -0300232 gspca_dev->int_urb = urb;
Márton Németh0274d422010-01-28 06:39:49 -0300233 return ret;
234
235error_submit:
Daniel Mack997ea582010-04-12 13:17:25 +0200236 usb_free_coherent(dev,
237 urb->transfer_buffer_length,
238 urb->transfer_buffer,
239 urb->transfer_dma);
Márton Németh0274d422010-01-28 06:39:49 -0300240error_buffer:
241 usb_free_urb(urb);
242error:
243 return ret;
244}
245
Hans de Goede10ee2402010-02-19 04:41:40 -0300246static void gspca_input_create_urb(struct gspca_dev *gspca_dev)
Márton Németh0274d422010-01-28 06:39:49 -0300247{
Márton Németh0274d422010-01-28 06:39:49 -0300248 struct usb_interface *intf;
249 struct usb_host_interface *intf_desc;
250 struct usb_endpoint_descriptor *ep;
251 int i;
252
253 if (gspca_dev->sd_desc->int_pkt_scan) {
254 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
255 intf_desc = intf->cur_altsetting;
256 for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
257 ep = &intf_desc->endpoint[i].desc;
258 if (usb_endpoint_dir_in(ep) &&
259 usb_endpoint_xfer_int(ep)) {
260
Hans de Goede10ee2402010-02-19 04:41:40 -0300261 alloc_and_submit_int_urb(gspca_dev, ep);
Márton Németh0274d422010-01-28 06:39:49 -0300262 break;
263 }
264 }
265 }
Márton Németh0274d422010-01-28 06:39:49 -0300266}
267
268static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
269{
270 struct urb *urb;
271
272 urb = gspca_dev->int_urb;
273 if (urb) {
274 gspca_dev->int_urb = NULL;
275 usb_kill_urb(urb);
Daniel Mack997ea582010-04-12 13:17:25 +0200276 usb_free_coherent(gspca_dev->dev,
277 urb->transfer_buffer_length,
278 urb->transfer_buffer,
279 urb->transfer_dma);
Márton Németh0274d422010-01-28 06:39:49 -0300280 usb_free_urb(urb);
281 }
282}
Randy Dunlapf7628012010-03-30 14:32:04 -0300283#else
284static inline void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
285{
286}
287
288static inline void gspca_input_create_urb(struct gspca_dev *gspca_dev)
289{
290}
291
292static inline int gspca_input_connect(struct gspca_dev *dev)
293{
294 return 0;
295}
Márton Németh0274d422010-01-28 06:39:49 -0300296#endif
297
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300298/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300299 * fill a video frame from an URB and resubmit
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300300 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300301static void fill_frame(struct gspca_dev *gspca_dev,
302 struct urb *urb)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300303{
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300304 u8 *data; /* address of data in the iso message */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300305 int i, len, st;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300306 cam_pkt_op pkt_scan;
307
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300308 if (urb->status != 0) {
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300309 if (urb->status == -ESHUTDOWN)
310 return; /* disconnection */
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300311#ifdef CONFIG_PM
Hans de Goede6a540bd2009-10-24 15:02:14 -0300312 if (gspca_dev->frozen)
313 return;
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300314#endif
Hans de Goede6a540bd2009-10-24 15:02:14 -0300315 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine81578522009-11-13 07:15:08 -0300316 urb->status = 0;
Hans de Goedebf926ad2009-10-23 06:56:06 -0300317 goto resubmit;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300318 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300319 pkt_scan = gspca_dev->sd_desc->pkt_scan;
320 for (i = 0; i < urb->number_of_packets; i++) {
321
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300322 /* check the packet status and length */
323 len = urb->iso_frame_desc[i].actual_length;
Jean-Francois Moineff374742008-10-23 07:29:51 -0300324 if (len == 0) {
325 if (gspca_dev->empty_packet == 0)
326 gspca_dev->empty_packet = 1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300327 continue;
Jean-Francois Moineff374742008-10-23 07:29:51 -0300328 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300329 st = urb->iso_frame_desc[i].status;
330 if (st) {
Jean-François Moine0b656322010-09-13 05:19:58 -0300331 err("ISOC data error: [%d] len=%d, status=%d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300332 i, len, st);
333 gspca_dev->last_packet_type = DISCARD_PACKET;
334 continue;
335 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300336
337 /* let the packet be analyzed by the subdriver */
338 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
339 i, urb->iso_frame_desc[i].offset, len);
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300340 data = (u8 *) urb->transfer_buffer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300341 + urb->iso_frame_desc[i].offset;
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300342 pkt_scan(gspca_dev, data, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300343 }
344
Hans de Goedebf926ad2009-10-23 06:56:06 -0300345resubmit:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300346 /* resubmit the URB */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300347 st = usb_submit_urb(urb, GFP_ATOMIC);
348 if (st < 0)
Jean-François Moine0b656322010-09-13 05:19:58 -0300349 err("usb_submit_urb() ret %d", st);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300350}
351
352/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300353 * ISOC message interrupt from the USB device
354 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300355 * Analyse each packet and call the subdriver for copy to the frame buffer.
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300356 */
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300357static void isoc_irq(struct urb *urb)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300358{
359 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
360
Hans de Goede3647fea2008-07-15 05:36:30 -0300361 PDEBUG(D_PACK, "isoc irq");
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300362 if (!gspca_dev->streaming)
363 return;
364 fill_frame(gspca_dev, urb);
365}
366
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300367/*
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300368 * bulk message interrupt from the USB device
369 */
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300370static void bulk_irq(struct urb *urb)
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300371{
372 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300373 int st;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300374
375 PDEBUG(D_PACK, "bulk irq");
376 if (!gspca_dev->streaming)
377 return;
Jean-Francois Moine880ae242008-11-18 04:19:52 -0300378 switch (urb->status) {
379 case 0:
380 break;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300381 case -ESHUTDOWN:
382 return; /* disconnection */
Jean-Francois Moine880ae242008-11-18 04:19:52 -0300383 default:
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300384#ifdef CONFIG_PM
Hans de Goede6a540bd2009-10-24 15:02:14 -0300385 if (gspca_dev->frozen)
386 return;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300387#endif
Hans de Goede6a540bd2009-10-24 15:02:14 -0300388 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine81578522009-11-13 07:15:08 -0300389 urb->status = 0;
Hans de Goedebf926ad2009-10-23 06:56:06 -0300390 goto resubmit;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300391 }
392
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300393 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
394 gspca_dev->sd_desc->pkt_scan(gspca_dev,
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300395 urb->transfer_buffer,
396 urb->actual_length);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300397
Hans de Goedebf926ad2009-10-23 06:56:06 -0300398resubmit:
Antonio Ospitedff369a2008-11-14 07:53:32 -0300399 /* resubmit the URB */
400 if (gspca_dev->cam.bulk_nurbs != 0) {
401 st = usb_submit_urb(urb, GFP_ATOMIC);
402 if (st < 0)
Jean-François Moine0b656322010-09-13 05:19:58 -0300403 err("usb_submit_urb() ret %d", st);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300404 }
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300405}
406
407/*
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300408 * add data to the current frame
409 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300410 * This function is called by the subdrivers at interrupt level.
411 *
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300412 * To build a frame, these ones must add
413 * - one FIRST_PACKET
414 * - 0 or many INTER_PACKETs
415 * - one LAST_PACKET
416 * DISCARD_PACKET invalidates the whole frame.
417 * On LAST_PACKET, a new frame is returned.
418 */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300419void gspca_frame_add(struct gspca_dev *gspca_dev,
420 enum gspca_packet_type packet_type,
421 const u8 *data,
422 int len)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300423{
Jean-Francois Moine3ec342f2009-11-13 07:38:16 -0300424 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300425 int i, j;
426
Hans de Goedee2997a72008-04-23 08:09:12 -0300427 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300428
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300429 if (packet_type == FIRST_PACKET) {
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300430 i = atomic_read(&gspca_dev->fr_i);
431
432 /* if there are no queued buffer, discard the whole frame */
433 if (i == atomic_read(&gspca_dev->fr_q)) {
434 gspca_dev->last_packet_type = DISCARD_PACKET;
Jean-François Moine2e0544a2010-10-16 13:59:13 -0300435 gspca_dev->sequence++;
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300436 return;
437 }
Jean-François Moineb192ca92010-06-27 03:08:19 -0300438 j = gspca_dev->fr_queue[i];
439 frame = &gspca_dev->frame[j];
Paulo Assis2f51d002010-03-07 06:56:56 -0300440 frame->v4l2_buf.timestamp = ktime_to_timeval(ktime_get());
Jean-François Moine2e0544a2010-10-16 13:59:13 -0300441 frame->v4l2_buf.sequence = gspca_dev->sequence++;
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300442 gspca_dev->image = frame->data;
Jean-François Moineb192ca92010-06-27 03:08:19 -0300443 gspca_dev->image_len = 0;
Jean-François Moinefe988f52010-07-29 02:46:02 -0300444 } else {
445 switch (gspca_dev->last_packet_type) {
446 case DISCARD_PACKET:
447 if (packet_type == LAST_PACKET)
448 gspca_dev->last_packet_type = packet_type;
449 return;
450 case LAST_PACKET:
451 return;
452 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300453 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300454
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300455 /* append the packet to the frame buffer */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300456 if (len > 0) {
Jean-François Moineb192ca92010-06-27 03:08:19 -0300457 if (gspca_dev->image_len + len > gspca_dev->frsz) {
458 PDEBUG(D_ERR|D_PACK, "frame overflow %d > %d",
459 gspca_dev->image_len + len,
460 gspca_dev->frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300461 packet_type = DISCARD_PACKET;
462 } else {
Jean-François Moinefe988f52010-07-29 02:46:02 -0300463/* !! image is NULL only when last pkt is LAST or DISCARD
464 if (gspca_dev->image == NULL) {
465 err("gspca_frame_add() image == NULL");
466 return;
467 }
468 */
Jean-François Moineb192ca92010-06-27 03:08:19 -0300469 memcpy(gspca_dev->image + gspca_dev->image_len,
470 data, len);
471 gspca_dev->image_len += len;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300472 }
473 }
474 gspca_dev->last_packet_type = packet_type;
475
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300476 /* if last packet, invalidate packet concatenation until
477 * next first packet, wake up the application and advance
478 * in the queue */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300479 if (packet_type == LAST_PACKET) {
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300480 i = atomic_read(&gspca_dev->fr_i);
Jean-François Moineb192ca92010-06-27 03:08:19 -0300481 j = gspca_dev->fr_queue[i];
482 frame = &gspca_dev->frame[j];
483 frame->v4l2_buf.bytesused = gspca_dev->image_len;
Jean-François Moine02bbcb92010-07-06 04:16:40 -0300484 frame->v4l2_buf.flags = (frame->v4l2_buf.flags
485 | V4L2_BUF_FLAG_DONE)
486 & ~V4L2_BUF_FLAG_QUEUED;
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300487 i = (i + 1) % GSPCA_MAX_FRAMES;
488 atomic_set(&gspca_dev->fr_i, i);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300489 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300490 PDEBUG(D_FRAM, "frame complete len:%d",
491 frame->v4l2_buf.bytesused);
492 gspca_dev->image = NULL;
493 gspca_dev->image_len = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300494 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300495}
496EXPORT_SYMBOL(gspca_frame_add);
497
498static int gspca_is_compressed(__u32 format)
499{
500 switch (format) {
501 case V4L2_PIX_FMT_MJPEG:
502 case V4L2_PIX_FMT_JPEG:
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300503 case V4L2_PIX_FMT_SPCA561:
Hans de Goedeab8f12c2008-07-04 18:29:32 -0300504 case V4L2_PIX_FMT_PAC207:
Kyle Guinnd661e622009-01-16 05:36:14 -0300505 case V4L2_PIX_FMT_MR97310A:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300506 return 1;
507 }
508 return 0;
509}
510
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300511static int frame_alloc(struct gspca_dev *gspca_dev,
512 unsigned int count)
513{
514 struct gspca_frame *frame;
515 unsigned int frsz;
516 int i;
517
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300518 i = gspca_dev->curr_mode;
519 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300520 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300521 frsz = PAGE_ALIGN(frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300522 gspca_dev->frsz = frsz;
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300523 if (count >= GSPCA_MAX_FRAMES)
524 count = GSPCA_MAX_FRAMES - 1;
Jean-François Moinee795d912010-06-24 05:14:22 -0300525 gspca_dev->frbuf = vmalloc_32(frsz * count);
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300526 if (!gspca_dev->frbuf) {
527 err("frame alloc failed");
528 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300529 }
530 gspca_dev->nframes = count;
531 for (i = 0; i < count; i++) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300532 frame = &gspca_dev->frame[i];
533 frame->v4l2_buf.index = i;
534 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
535 frame->v4l2_buf.flags = 0;
536 frame->v4l2_buf.field = V4L2_FIELD_NONE;
537 frame->v4l2_buf.length = frsz;
538 frame->v4l2_buf.memory = gspca_dev->memory;
539 frame->v4l2_buf.sequence = 0;
Jean-François Moineb192ca92010-06-27 03:08:19 -0300540 frame->data = gspca_dev->frbuf + i * frsz;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300541 frame->v4l2_buf.m.offset = i * frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300542 }
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300543 atomic_set(&gspca_dev->fr_q, 0);
544 atomic_set(&gspca_dev->fr_i, 0);
545 gspca_dev->fr_o = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300546 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300547}
548
549static void frame_free(struct gspca_dev *gspca_dev)
550{
551 int i;
552
553 PDEBUG(D_STREAM, "frame free");
Harvey Harrisonefab8212008-07-05 06:12:47 -0300554 if (gspca_dev->frbuf != NULL) {
Jean-François Moinee795d912010-06-24 05:14:22 -0300555 vfree(gspca_dev->frbuf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300556 gspca_dev->frbuf = NULL;
557 for (i = 0; i < gspca_dev->nframes; i++)
558 gspca_dev->frame[i].data = NULL;
559 }
560 gspca_dev->nframes = 0;
561}
562
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300563static void destroy_urbs(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300564{
565 struct urb *urb;
566 unsigned int i;
567
568 PDEBUG(D_STREAM, "kill transfer");
Erik Andrén44732812008-10-13 15:52:46 -0300569 for (i = 0; i < MAX_NURBS; i++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300570 urb = gspca_dev->urb[i];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300571 if (urb == NULL)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300572 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300573
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300574 gspca_dev->urb[i] = NULL;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300575 usb_kill_urb(urb);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300576 if (urb->transfer_buffer != NULL)
Daniel Mack997ea582010-04-12 13:17:25 +0200577 usb_free_coherent(gspca_dev->dev,
578 urb->transfer_buffer_length,
579 urb->transfer_buffer,
580 urb->transfer_dma);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300581 usb_free_urb(urb);
582 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300583}
584
Jean-François Moinee4dac282010-04-09 06:11:36 -0300585static int gspca_set_alt0(struct gspca_dev *gspca_dev)
586{
587 int ret;
588
589 if (gspca_dev->alt == 0)
590 return 0;
591 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
592 if (ret < 0)
Jean-François Moine0b656322010-09-13 05:19:58 -0300593 err("set alt 0 err %d", ret);
Jean-François Moinee4dac282010-04-09 06:11:36 -0300594 return ret;
595}
596
597/* Note: both the queue and the usb locks should be held when calling this */
598static void gspca_stream_off(struct gspca_dev *gspca_dev)
599{
600 gspca_dev->streaming = 0;
601 if (gspca_dev->present) {
602 if (gspca_dev->sd_desc->stopN)
603 gspca_dev->sd_desc->stopN(gspca_dev);
604 destroy_urbs(gspca_dev);
605 gspca_input_destroy_urb(gspca_dev);
606 gspca_set_alt0(gspca_dev);
607 gspca_input_create_urb(gspca_dev);
608 }
609
610 /* always call stop0 to free the subdriver's resources */
611 if (gspca_dev->sd_desc->stop0)
612 gspca_dev->sd_desc->stop0(gspca_dev);
613 PDEBUG(D_STREAM, "stream off OK");
614}
615
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300616/*
Frank Zago219423c2008-09-26 07:43:54 -0300617 * look for an input transfer endpoint in an alternate setting
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300618 */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300619static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300620 int xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300621{
622 struct usb_host_endpoint *ep;
623 int i, attr;
624
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300625 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
626 ep = &alt->endpoint[i];
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300627 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Jean-Francois Moine30d35e42009-04-21 13:57:31 -0300628 if (attr == xfer
629 && ep->desc.wMaxPacketSize != 0)
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300630 return ep;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300631 }
632 return NULL;
633}
634
635/*
Frank Zago219423c2008-09-26 07:43:54 -0300636 * look for an input (isoc or bulk) endpoint
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300637 *
638 * The endpoint is defined by the subdriver.
639 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
640 * This routine may be called many times when the bandwidth is too small
641 * (the bandwidth is checked on urb submit).
642 */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300643static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300644{
645 struct usb_interface *intf;
646 struct usb_host_endpoint *ep;
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300647 int xfer, i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300648
649 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300650 ep = NULL;
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300651 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
652 : USB_ENDPOINT_XFER_ISOC;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300653 i = gspca_dev->alt; /* previous alt setting */
Hans de Goedea511ba92009-10-16 07:13:07 -0300654 if (gspca_dev->cam.reverse_alts) {
Jean-François Moinef43402f2010-10-16 13:54:05 -0300655 if (gspca_dev->audio && i < gspca_dev->nbalt - 2)
Jean-François Moine35680ba2010-07-14 06:30:18 -0300656 i++;
Hans de Goedea511ba92009-10-16 07:13:07 -0300657 while (++i < gspca_dev->nbalt) {
658 ep = alt_xfer(&intf->altsetting[i], xfer);
659 if (ep)
660 break;
661 }
662 } else {
Jean-François Moinef43402f2010-10-16 13:54:05 -0300663 if (gspca_dev->audio && i > 1)
Jean-François Moine35680ba2010-07-14 06:30:18 -0300664 i--;
Hans de Goedea511ba92009-10-16 07:13:07 -0300665 while (--i >= 0) {
666 ep = alt_xfer(&intf->altsetting[i], xfer);
667 if (ep)
668 break;
669 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300670 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300671 if (ep == NULL) {
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300672 err("no transfer endpoint found");
673 return NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300674 }
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300675 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300676 i, ep->desc.bEndpointAddress);
Jean-Francois Moine1c8336d2009-07-28 04:38:25 -0300677 gspca_dev->alt = i; /* memorize the current alt setting */
Jean-Francois Moine8dd07ef2009-04-21 14:05:44 -0300678 if (gspca_dev->nbalt > 1) {
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300679 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
680 if (ret < 0) {
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300681 err("set alt %d err %d", i, ret);
Márton Németh0274d422010-01-28 06:39:49 -0300682 ep = NULL;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300683 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300684 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300685 return ep;
686}
687
688/*
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300689 * create the URBs for image transfer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300690 */
691static int create_urbs(struct gspca_dev *gspca_dev,
692 struct usb_host_endpoint *ep)
693{
694 struct urb *urb;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300695 int n, nurbs, i, psize, npkt, bsize;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300696
697 /* calculate the packet size and the number of packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300698 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300699
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300700 if (!gspca_dev->cam.bulk) { /* isoc */
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300701
702 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300703 if (gspca_dev->pkt_size == 0)
704 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
705 else
706 psize = gspca_dev->pkt_size;
Jean-Francois Moine49cb6b02009-04-25 13:29:01 -0300707 npkt = gspca_dev->cam.npkt;
708 if (npkt == 0)
709 npkt = 32; /* default value */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300710 bsize = psize * npkt;
711 PDEBUG(D_STREAM,
712 "isoc %d pkts size %d = bsize:%d",
713 npkt, psize, bsize);
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300714 nurbs = DEF_NURBS;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300715 } else { /* bulk */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300716 npkt = 0;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300717 bsize = gspca_dev->cam.bulk_size;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300718 if (bsize == 0)
719 bsize = psize;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300720 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300721 if (gspca_dev->cam.bulk_nurbs != 0)
722 nurbs = gspca_dev->cam.bulk_nurbs;
723 else
724 nurbs = 1;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300725 }
726
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300727 for (n = 0; n < nurbs; n++) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300728 urb = usb_alloc_urb(npkt, GFP_KERNEL);
729 if (!urb) {
730 err("usb_alloc_urb failed");
731 return -ENOMEM;
732 }
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300733 gspca_dev->urb[n] = urb;
Daniel Mack997ea582010-04-12 13:17:25 +0200734 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300735 bsize,
736 GFP_KERNEL,
737 &urb->transfer_dma);
738
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300739 if (urb->transfer_buffer == NULL) {
Daniel Mack997ea582010-04-12 13:17:25 +0200740 err("usb_alloc_coherent failed");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300741 return -ENOMEM;
742 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300743 urb->dev = gspca_dev->dev;
744 urb->context = gspca_dev;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300745 urb->transfer_buffer_length = bsize;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300746 if (npkt != 0) { /* ISOC */
747 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
748 ep->desc.bEndpointAddress);
Frank Zago6c49da72008-09-28 07:43:00 -0300749 urb->transfer_flags = URB_ISO_ASAP
750 | URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300751 urb->interval = ep->desc.bInterval;
752 urb->complete = isoc_irq;
753 urb->number_of_packets = npkt;
754 for (i = 0; i < npkt; i++) {
755 urb->iso_frame_desc[i].length = psize;
756 urb->iso_frame_desc[i].offset = psize * i;
757 }
758 } else { /* bulk */
759 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
Jean-François Moinea23c1942010-10-29 07:57:03 -0300760 ep->desc.bEndpointAddress);
Frank Zago6c49da72008-09-28 07:43:00 -0300761 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300762 urb->complete = bulk_irq;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300763 }
764 }
765 return 0;
766}
767
768/*
769 * start the USB transfer
770 */
771static int gspca_init_transfer(struct gspca_dev *gspca_dev)
772{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300773 struct usb_host_endpoint *ep;
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300774 struct urb *urb;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300775 int n, ret;
776
Hans de Goedee2997a72008-04-23 08:09:12 -0300777 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
778 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300779
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300780 if (!gspca_dev->present) {
781 ret = -ENODEV;
Hans de Goede314b3e02010-10-26 05:15:46 -0300782 goto unlock;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300783 }
784
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300785 /* reset the streaming variables */
786 gspca_dev->image = NULL;
787 gspca_dev->image_len = 0;
788 gspca_dev->last_packet_type = DISCARD_PACKET;
789 gspca_dev->sequence = 0;
790
Jean-Francois Moineab6c8cf2010-01-13 15:34:27 -0300791 gspca_dev->usb_err = 0;
792
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300793 /* set the higher alternate setting and
794 * loop until urb submit succeeds */
Hans de Goedea511ba92009-10-16 07:13:07 -0300795 if (gspca_dev->cam.reverse_alts)
796 gspca_dev->alt = 0;
797 else
798 gspca_dev->alt = gspca_dev->nbalt;
799
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300800 if (gspca_dev->sd_desc->isoc_init) {
801 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
802 if (ret < 0)
Hans de Goede314b3e02010-10-26 05:15:46 -0300803 goto unlock;
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300804 }
Hans de Goede314b3e02010-10-26 05:15:46 -0300805
806 gspca_input_destroy_urb(gspca_dev);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300807 ep = get_ep(gspca_dev);
808 if (ep == NULL) {
809 ret = -EIO;
810 goto out;
811 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300812 for (;;) {
Jean-Francois Moine47aaca92009-12-15 05:23:04 -0300813 if (!gspca_dev->cam.no_urb_create) {
814 PDEBUG(D_STREAM, "init transfer alt %d",
815 gspca_dev->alt);
816 ret = create_urbs(gspca_dev, ep);
817 if (ret < 0) {
818 destroy_urbs(gspca_dev);
819 goto out;
820 }
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300821 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300822
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300823 /* clear the bulk endpoint */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300824 if (gspca_dev->cam.bulk)
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300825 usb_clear_halt(gspca_dev->dev,
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300826 gspca_dev->urb[0]->pipe);
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300827
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300828 /* start the cam */
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300829 ret = gspca_dev->sd_desc->start(gspca_dev);
830 if (ret < 0) {
831 destroy_urbs(gspca_dev);
832 goto out;
833 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300834 gspca_dev->streaming = 1;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300835
Antonio Ospitedff369a2008-11-14 07:53:32 -0300836 /* some bulk transfers are started by the subdriver */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300837 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300838 break;
839
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300840 /* submit the URBs */
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300841 for (n = 0; n < MAX_NURBS; n++) {
842 urb = gspca_dev->urb[n];
843 if (urb == NULL)
844 break;
845 ret = usb_submit_urb(urb, GFP_KERNEL);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300846 if (ret < 0)
847 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300848 }
849 if (ret >= 0)
850 break;
Jean-François Moinee4dac282010-04-09 06:11:36 -0300851 gspca_stream_off(gspca_dev);
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300852 if (ret != -ENOSPC) {
Jean-François Moine0b656322010-09-13 05:19:58 -0300853 err("usb_submit_urb alt %d err %d",
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300854 gspca_dev->alt, ret);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300855 goto out;
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300856 }
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300857
858 /* the bandwidth is not wide enough
859 * negociate or try a lower alternate setting */
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300860 PDEBUG(D_ERR|D_STREAM,
861 "bandwidth not wide enough - trying again");
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300862 msleep(20); /* wait for kill complete */
863 if (gspca_dev->sd_desc->isoc_nego) {
864 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
865 if (ret < 0)
866 goto out;
867 } else {
868 ep = get_ep(gspca_dev);
869 if (ep == NULL) {
870 ret = -EIO;
871 goto out;
872 }
873 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300874 }
875out:
Hans de Goede314b3e02010-10-26 05:15:46 -0300876 gspca_input_create_urb(gspca_dev);
877unlock:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300878 mutex_unlock(&gspca_dev->usb_lock);
879 return ret;
880}
881
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300882static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300883{
Jean-François Moine6a330912010-10-02 04:12:25 -0300884 struct gspca_ctrl *ctrl;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300885 int i;
886
887 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
888 gspca_dev->curr_mode = i;
889 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
890 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300891 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-François Moine6a330912010-10-02 04:12:25 -0300892
893 /* set the current control values to their default values
894 * which may have changed in sd_init() */
895 ctrl = gspca_dev->cam.ctrls;
896 if (ctrl != NULL) {
897 for (i = 0;
898 i < gspca_dev->sd_desc->nctrls;
899 i++, ctrl++)
900 ctrl->val = ctrl->def;
901 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300902}
903
904static int wxh_to_mode(struct gspca_dev *gspca_dev,
905 int width, int height)
906{
907 int i;
908
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300909 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
910 if (width >= gspca_dev->cam.cam_mode[i].width
911 && height >= gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300912 break;
913 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300914 return i;
915}
916
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300917/*
918 * search a mode with the right pixel format
919 */
920static int gspca_get_mode(struct gspca_dev *gspca_dev,
921 int mode,
922 int pixfmt)
923{
924 int modeU, modeD;
925
926 modeU = modeD = mode;
927 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
928 if (--modeD >= 0) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300929 if (gspca_dev->cam.cam_mode[modeD].pixelformat
930 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300931 return modeD;
932 }
933 if (++modeU < gspca_dev->cam.nmodes) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300934 if (gspca_dev->cam.cam_mode[modeU].pixelformat
935 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300936 return modeU;
937 }
938 }
939 return -EINVAL;
940}
941
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300942#ifdef CONFIG_VIDEO_ADV_DEBUG
943static int vidioc_g_register(struct file *file, void *priv,
944 struct v4l2_dbg_register *reg)
945{
946 int ret;
947 struct gspca_dev *gspca_dev = priv;
948
949 if (!gspca_dev->sd_desc->get_chip_ident)
950 return -EINVAL;
951
952 if (!gspca_dev->sd_desc->get_register)
953 return -EINVAL;
954
955 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
956 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -0300957 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300958 if (gspca_dev->present)
959 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
960 else
961 ret = -ENODEV;
962 mutex_unlock(&gspca_dev->usb_lock);
963
964 return ret;
965}
966
967static int vidioc_s_register(struct file *file, void *priv,
968 struct v4l2_dbg_register *reg)
969{
970 int ret;
971 struct gspca_dev *gspca_dev = priv;
972
973 if (!gspca_dev->sd_desc->get_chip_ident)
974 return -EINVAL;
975
976 if (!gspca_dev->sd_desc->set_register)
977 return -EINVAL;
978
979 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
980 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -0300981 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300982 if (gspca_dev->present)
983 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
984 else
985 ret = -ENODEV;
986 mutex_unlock(&gspca_dev->usb_lock);
987
988 return ret;
989}
990#endif
991
992static int vidioc_g_chip_ident(struct file *file, void *priv,
993 struct v4l2_dbg_chip_ident *chip)
994{
995 int ret;
996 struct gspca_dev *gspca_dev = priv;
997
998 if (!gspca_dev->sd_desc->get_chip_ident)
999 return -EINVAL;
1000
1001 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1002 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001003 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03001004 if (gspca_dev->present)
1005 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
1006 else
1007 ret = -ENODEV;
1008 mutex_unlock(&gspca_dev->usb_lock);
1009
1010 return ret;
1011}
1012
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001013static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001014 struct v4l2_fmtdesc *fmtdesc)
1015{
1016 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001017 int i, j, index;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001018 __u32 fmt_tb[8];
1019
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001020 /* give an index to each format */
1021 index = 0;
1022 j = 0;
1023 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001024 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001025 j = 0;
1026 for (;;) {
1027 if (fmt_tb[j] == fmt_tb[index])
1028 break;
1029 j++;
1030 }
1031 if (j == index) {
1032 if (fmtdesc->index == index)
1033 break; /* new format */
1034 index++;
Julia Lawall80297122008-11-12 23:18:21 -03001035 if (index >= ARRAY_SIZE(fmt_tb))
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001036 return -EINVAL;
1037 }
1038 }
1039 if (i < 0)
1040 return -EINVAL; /* no more format */
1041
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001042 fmtdesc->pixelformat = fmt_tb[index];
1043 if (gspca_is_compressed(fmt_tb[index]))
1044 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
1045 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
1046 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
1047 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
1048 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
1049 fmtdesc->description[4] = '\0';
1050 return 0;
1051}
1052
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001053static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001054 struct v4l2_format *fmt)
1055{
1056 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001057 int mode;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001058
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001059 mode = gspca_dev->curr_mode;
1060 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1061 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001062 return 0;
1063}
1064
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001065static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001066 struct v4l2_format *fmt)
1067{
Hans de Goede24c530b2008-07-04 17:55:18 -03001068 int w, h, mode, mode2;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001069
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001070 w = fmt->fmt.pix.width;
1071 h = fmt->fmt.pix.height;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001072
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001073#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001074 if (gspca_debug & D_CONF)
1075 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
1076#endif
1077 /* search the closest mode for width and height */
1078 mode = wxh_to_mode(gspca_dev, w, h);
1079
1080 /* OK if right palette */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001081 if (gspca_dev->cam.cam_mode[mode].pixelformat
1082 != fmt->fmt.pix.pixelformat) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001083
1084 /* else, search the closest mode with the same pixel format */
1085 mode2 = gspca_get_mode(gspca_dev, mode,
1086 fmt->fmt.pix.pixelformat);
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001087 if (mode2 >= 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001088 mode = mode2;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001089/* else
1090 ; * no chance, return this mode */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001091 }
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001092 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1093 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001094 return mode; /* used when s_fmt */
1095}
1096
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001097static int vidioc_try_fmt_vid_cap(struct file *file,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001098 void *priv,
1099 struct v4l2_format *fmt)
1100{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001101 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001102 int ret;
1103
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001104 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001105 if (ret < 0)
1106 return ret;
1107 return 0;
1108}
1109
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001110static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001111 struct v4l2_format *fmt)
1112{
1113 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001114 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001115
Hans de Goedee2997a72008-04-23 08:09:12 -03001116 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1117 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001118
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001119 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001120 if (ret < 0)
1121 goto out;
1122
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001123 if (gspca_dev->nframes != 0
1124 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
1125 ret = -EINVAL;
1126 goto out;
1127 }
1128
Jean-Francois Moine50a871f2008-06-30 19:47:33 -03001129 if (ret == gspca_dev->curr_mode) {
1130 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001131 goto out; /* same mode */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -03001132 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001133
1134 if (gspca_dev->streaming) {
1135 ret = -EBUSY;
1136 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -03001137 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001138 gspca_dev->width = fmt->fmt.pix.width;
1139 gspca_dev->height = fmt->fmt.pix.height;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001140 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
1141 gspca_dev->curr_mode = ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001142
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001143 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001144out:
1145 mutex_unlock(&gspca_dev->queue_lock);
1146 return ret;
1147}
1148
Hans de Goede11c635a2009-05-25 15:04:22 -03001149static int vidioc_enum_framesizes(struct file *file, void *priv,
1150 struct v4l2_frmsizeenum *fsize)
1151{
1152 struct gspca_dev *gspca_dev = priv;
1153 int i;
1154 __u32 index = 0;
1155
1156 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
1157 if (fsize->pixel_format !=
1158 gspca_dev->cam.cam_mode[i].pixelformat)
1159 continue;
1160
1161 if (fsize->index == index) {
1162 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1163 fsize->discrete.width =
1164 gspca_dev->cam.cam_mode[i].width;
1165 fsize->discrete.height =
1166 gspca_dev->cam.cam_mode[i].height;
1167 return 0;
1168 }
1169 index++;
1170 }
1171
1172 return -EINVAL;
1173}
1174
Antonio Ospite28ffe772009-12-02 06:18:46 -03001175static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1176 struct v4l2_frmivalenum *fival)
1177{
1178 struct gspca_dev *gspca_dev = priv;
1179 int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
1180 __u32 i;
1181
1182 if (gspca_dev->cam.mode_framerates == NULL ||
1183 gspca_dev->cam.mode_framerates[mode].nrates == 0)
1184 return -EINVAL;
1185
1186 if (fival->pixel_format !=
1187 gspca_dev->cam.cam_mode[mode].pixelformat)
1188 return -EINVAL;
1189
1190 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1191 if (fival->index == i) {
1192 fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1193 fival->discrete.numerator = 1;
1194 fival->discrete.denominator =
1195 gspca_dev->cam.mode_framerates[mode].rates[i];
1196 return 0;
1197 }
1198 }
1199
1200 return -EINVAL;
1201}
1202
Hans de Goeded0182302008-11-19 17:10:47 -03001203static void gspca_release(struct video_device *vfd)
Frank Zago6b060ff2008-09-28 08:12:22 -03001204{
Hans de Goeded0182302008-11-19 17:10:47 -03001205 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
Frank Zago6b060ff2008-09-28 08:12:22 -03001206
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001207 PDEBUG(D_PROBE, "%s released",
1208 video_device_node_name(&gspca_dev->vdev));
Frank Zago6b060ff2008-09-28 08:12:22 -03001209
1210 kfree(gspca_dev->usb_buf);
1211 kfree(gspca_dev);
1212}
1213
Hans Verkuilbec43662008-12-30 06:58:20 -03001214static int dev_open(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001215{
1216 struct gspca_dev *gspca_dev;
1217 int ret;
1218
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001219 PDEBUG(D_STREAM, "[%s] open", current->comm);
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001220 gspca_dev = (struct gspca_dev *) video_devdata(file);
Hans de Goedee2997a72008-04-23 08:09:12 -03001221 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1222 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001223 if (!gspca_dev->present) {
1224 ret = -ENODEV;
1225 goto out;
1226 }
1227
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001228 if (gspca_dev->users > 4) { /* (arbitrary value) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001229 ret = -EBUSY;
1230 goto out;
1231 }
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03001232
1233 /* protect the subdriver against rmmod */
1234 if (!try_module_get(gspca_dev->module)) {
1235 ret = -ENODEV;
1236 goto out;
1237 }
1238
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001239 gspca_dev->users++;
Frank Zago6b060ff2008-09-28 08:12:22 -03001240
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001241 file->private_data = gspca_dev;
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001242#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001243 /* activate the v4l2 debug */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001244 if (gspca_debug & D_V4L2)
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001245 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001246 | V4L2_DEBUG_IOCTL_ARG;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001247 else
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001248 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001249 | V4L2_DEBUG_IOCTL_ARG);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001250#endif
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001251 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001252out:
1253 mutex_unlock(&gspca_dev->queue_lock);
1254 if (ret != 0)
1255 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
1256 else
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001257 PDEBUG(D_STREAM, "open done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001258 return ret;
1259}
1260
Hans Verkuilbec43662008-12-30 06:58:20 -03001261static int dev_close(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001262{
1263 struct gspca_dev *gspca_dev = file->private_data;
1264
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001265 PDEBUG(D_STREAM, "[%s] close", current->comm);
Hans de Goedee2997a72008-04-23 08:09:12 -03001266 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1267 return -ERESTARTSYS;
1268 gspca_dev->users--;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001269
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001270 /* if the file did the capture, free the streaming resources */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001271 if (gspca_dev->capt_file == file) {
Jean-Francois Moinea1e82882008-09-03 17:12:17 -03001272 if (gspca_dev->streaming) {
1273 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001274 gspca_dev->usb_err = 0;
Jean-Francois Moinea1e82882008-09-03 17:12:17 -03001275 gspca_stream_off(gspca_dev);
1276 mutex_unlock(&gspca_dev->usb_lock);
1277 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001278 frame_free(gspca_dev);
Harvey Harrisonefab8212008-07-05 06:12:47 -03001279 gspca_dev->capt_file = NULL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001280 gspca_dev->memory = GSPCA_MEMORY_NO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001281 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001282 file->private_data = NULL;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03001283 module_put(gspca_dev->module);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001284 mutex_unlock(&gspca_dev->queue_lock);
Frank Zago6b060ff2008-09-28 08:12:22 -03001285
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001286 PDEBUG(D_STREAM, "close done");
Frank Zago6b060ff2008-09-28 08:12:22 -03001287
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001288 return 0;
1289}
1290
1291static int vidioc_querycap(struct file *file, void *priv,
1292 struct v4l2_capability *cap)
1293{
1294 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001295 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001296
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001297 /* protect the access to the usb device */
1298 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1299 return -ERESTARTSYS;
1300 if (!gspca_dev->present) {
1301 ret = -ENODEV;
1302 goto out;
1303 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001304 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -03001305 if (gspca_dev->dev->product != NULL) {
1306 strncpy(cap->card, gspca_dev->dev->product,
1307 sizeof cap->card);
1308 } else {
1309 snprintf(cap->card, sizeof cap->card,
1310 "USB Camera (%04x:%04x)",
1311 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1312 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1313 }
Thierry MERLE59320282009-01-21 15:32:26 -03001314 usb_make_path(gspca_dev->dev, cap->bus_info, sizeof(cap->bus_info));
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001315 cap->version = DRIVER_VERSION_NUMBER;
1316 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1317 | V4L2_CAP_STREAMING
1318 | V4L2_CAP_READWRITE;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001319 ret = 0;
1320out:
1321 mutex_unlock(&gspca_dev->usb_lock);
1322 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001323}
1324
Jean-François Moine6a330912010-10-02 04:12:25 -03001325static int get_ctrl(struct gspca_dev *gspca_dev,
Hans de Goedea0001a22009-06-10 04:52:18 -03001326 int id)
1327{
1328 const struct ctrl *ctrls;
1329 int i;
1330
1331 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1332 i < gspca_dev->sd_desc->nctrls;
1333 i++, ctrls++) {
1334 if (gspca_dev->ctrl_dis & (1 << i))
1335 continue;
1336 if (id == ctrls->qctrl.id)
Jean-François Moine6a330912010-10-02 04:12:25 -03001337 return i;
Hans de Goedea0001a22009-06-10 04:52:18 -03001338 }
Jean-François Moine6a330912010-10-02 04:12:25 -03001339 return -1;
Hans de Goedea0001a22009-06-10 04:52:18 -03001340}
1341
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001342static int vidioc_queryctrl(struct file *file, void *priv,
1343 struct v4l2_queryctrl *q_ctrl)
1344{
1345 struct gspca_dev *gspca_dev = priv;
Hans de Goedea0001a22009-06-10 04:52:18 -03001346 const struct ctrl *ctrls;
Jean-François Moine6a330912010-10-02 04:12:25 -03001347 struct gspca_ctrl *gspca_ctrl;
1348 int i, idx;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001349 u32 id;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001350
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001351 id = q_ctrl->id;
1352 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1353 id &= V4L2_CTRL_ID_MASK;
1354 id++;
Jean-François Moine6a330912010-10-02 04:12:25 -03001355 idx = -1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001356 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001357 if (gspca_dev->ctrl_dis & (1 << i))
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001358 continue;
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001359 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001360 continue;
Jean-François Moine6a330912010-10-02 04:12:25 -03001361 if (idx >= 0
1362 && gspca_dev->sd_desc->ctrls[i].qctrl.id
Jean-François Moine780e3122010-10-19 04:29:10 -03001363 > gspca_dev->sd_desc->ctrls[idx].qctrl.id)
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001364 continue;
Jean-François Moine6a330912010-10-02 04:12:25 -03001365 idx = i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001366 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001367 } else {
Jean-François Moine6a330912010-10-02 04:12:25 -03001368 idx = get_ctrl(gspca_dev, id);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001369 }
Jean-François Moine6a330912010-10-02 04:12:25 -03001370 if (idx < 0)
1371 return -EINVAL;
1372 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1373 memcpy(q_ctrl, &ctrls->qctrl, sizeof *q_ctrl);
1374 if (gspca_dev->cam.ctrls != NULL) {
1375 gspca_ctrl = &gspca_dev->cam.ctrls[idx];
1376 q_ctrl->default_value = gspca_ctrl->def;
1377 q_ctrl->minimum = gspca_ctrl->min;
1378 q_ctrl->maximum = gspca_ctrl->max;
1379 }
1380 if (gspca_dev->ctrl_inac & (1 << idx))
Jean-Francois Moine4af85662009-11-12 15:59:27 -03001381 q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001382 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001383}
1384
1385static int vidioc_s_ctrl(struct file *file, void *priv,
1386 struct v4l2_control *ctrl)
1387{
1388 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001389 const struct ctrl *ctrls;
Jean-François Moine6a330912010-10-02 04:12:25 -03001390 struct gspca_ctrl *gspca_ctrl;
1391 int idx, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001392
Jean-François Moine6a330912010-10-02 04:12:25 -03001393 idx = get_ctrl(gspca_dev, ctrl->id);
1394 if (idx < 0)
Hans de Goedea0001a22009-06-10 04:52:18 -03001395 return -EINVAL;
Jean-François Moine6a330912010-10-02 04:12:25 -03001396 if (gspca_dev->ctrl_inac & (1 << idx))
1397 return -EINVAL;
1398 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1399 if (gspca_dev->cam.ctrls != NULL) {
1400 gspca_ctrl = &gspca_dev->cam.ctrls[idx];
1401 if (ctrl->value < gspca_ctrl->min
1402 || ctrl->value > gspca_ctrl->max)
1403 return -ERANGE;
1404 } else {
1405 gspca_ctrl = NULL;
1406 if (ctrl->value < ctrls->qctrl.minimum
1407 || ctrl->value > ctrls->qctrl.maximum)
1408 return -ERANGE;
1409 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001410 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1411 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1412 return -ERESTARTSYS;
Jean-François Moine6a330912010-10-02 04:12:25 -03001413 if (!gspca_dev->present) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001414 ret = -ENODEV;
Jean-François Moine6a330912010-10-02 04:12:25 -03001415 goto out;
1416 }
1417 gspca_dev->usb_err = 0;
1418 if (ctrls->set != NULL) {
1419 ret = ctrls->set(gspca_dev, ctrl->value);
1420 goto out;
1421 }
1422 if (gspca_ctrl != NULL) {
1423 gspca_ctrl->val = ctrl->value;
1424 if (ctrls->set_control != NULL
1425 && gspca_dev->streaming)
1426 ctrls->set_control(gspca_dev);
1427 }
1428 ret = gspca_dev->usb_err;
1429out:
Hans de Goedea0001a22009-06-10 04:52:18 -03001430 mutex_unlock(&gspca_dev->usb_lock);
1431 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001432}
1433
1434static int vidioc_g_ctrl(struct file *file, void *priv,
1435 struct v4l2_control *ctrl)
1436{
1437 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001438 const struct ctrl *ctrls;
Jean-François Moine6a330912010-10-02 04:12:25 -03001439 int idx, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001440
Jean-François Moine6a330912010-10-02 04:12:25 -03001441 idx = get_ctrl(gspca_dev, ctrl->id);
1442 if (idx < 0)
Hans de Goedea0001a22009-06-10 04:52:18 -03001443 return -EINVAL;
Jean-François Moine6a330912010-10-02 04:12:25 -03001444 ctrls = &gspca_dev->sd_desc->ctrls[idx];
Hans de Goedea0001a22009-06-10 04:52:18 -03001445
1446 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1447 return -ERESTARTSYS;
Jean-François Moine6a330912010-10-02 04:12:25 -03001448 if (!gspca_dev->present) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001449 ret = -ENODEV;
Jean-François Moine6a330912010-10-02 04:12:25 -03001450 goto out;
1451 }
1452 gspca_dev->usb_err = 0;
1453 if (ctrls->get != NULL) {
1454 ret = ctrls->get(gspca_dev, &ctrl->value);
1455 goto out;
1456 }
1457 if (gspca_dev->cam.ctrls != NULL)
1458 ctrl->value = gspca_dev->cam.ctrls[idx].val;
1459 ret = 0;
1460out:
Hans de Goedea0001a22009-06-10 04:52:18 -03001461 mutex_unlock(&gspca_dev->usb_lock);
1462 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001463}
1464
1465static int vidioc_querymenu(struct file *file, void *priv,
1466 struct v4l2_querymenu *qmenu)
1467{
1468 struct gspca_dev *gspca_dev = priv;
1469
1470 if (!gspca_dev->sd_desc->querymenu)
1471 return -EINVAL;
1472 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1473}
1474
1475static int vidioc_enum_input(struct file *file, void *priv,
1476 struct v4l2_input *input)
1477{
1478 struct gspca_dev *gspca_dev = priv;
1479
1480 if (input->index != 0)
1481 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001482 input->type = V4L2_INPUT_TYPE_CAMERA;
Adam Bakerdfa76fa2009-03-29 19:17:10 -03001483 input->status = gspca_dev->cam.input_flags;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001484 strncpy(input->name, gspca_dev->sd_desc->name,
1485 sizeof input->name);
1486 return 0;
1487}
1488
1489static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1490{
1491 *i = 0;
1492 return 0;
1493}
1494
1495static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1496{
1497 if (i > 0)
1498 return -EINVAL;
1499 return (0);
1500}
1501
1502static int vidioc_reqbufs(struct file *file, void *priv,
1503 struct v4l2_requestbuffers *rb)
1504{
1505 struct gspca_dev *gspca_dev = priv;
Jean-François Moine832d0a92010-04-09 06:07:58 -03001506 int i, ret = 0, streaming;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001507
Jean-François Moinea2ba6f22010-07-14 06:21:16 -03001508 i = rb->memory; /* (avoid compilation warning) */
1509 switch (i) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001510 case GSPCA_MEMORY_READ: /* (internal call) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001511 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001512 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001513 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001514 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001515 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001516 }
Hans de Goedee2997a72008-04-23 08:09:12 -03001517 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1518 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001519
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001520 if (gspca_dev->memory != GSPCA_MEMORY_NO
1521 && gspca_dev->memory != rb->memory) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001522 ret = -EBUSY;
1523 goto out;
1524 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001525
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001526 /* only one file may do the capture */
1527 if (gspca_dev->capt_file != NULL
1528 && gspca_dev->capt_file != file) {
1529 ret = -EBUSY;
1530 goto out;
1531 }
1532
1533 /* if allocated, the buffers must not be mapped */
1534 for (i = 0; i < gspca_dev->nframes; i++) {
1535 if (gspca_dev->frame[i].vma_use_count) {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001536 ret = -EBUSY;
1537 goto out;
1538 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001539 }
1540
1541 /* stop streaming */
Jean-François Moine832d0a92010-04-09 06:07:58 -03001542 streaming = gspca_dev->streaming;
1543 if (streaming) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001544 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001545 gspca_dev->usb_err = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001546 gspca_stream_off(gspca_dev);
1547 mutex_unlock(&gspca_dev->usb_lock);
1548 }
1549
1550 /* free the previous allocated buffers, if any */
1551 if (gspca_dev->nframes != 0) {
1552 frame_free(gspca_dev);
1553 gspca_dev->capt_file = NULL;
1554 }
1555 if (rb->count == 0) /* unrequest */
1556 goto out;
1557 gspca_dev->memory = rb->memory;
1558 ret = frame_alloc(gspca_dev, rb->count);
1559 if (ret == 0) {
1560 rb->count = gspca_dev->nframes;
1561 gspca_dev->capt_file = file;
Jean-François Moine832d0a92010-04-09 06:07:58 -03001562 if (streaming)
1563 ret = gspca_init_transfer(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001564 }
1565out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001566 mutex_unlock(&gspca_dev->queue_lock);
1567 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1568 return ret;
1569}
1570
1571static int vidioc_querybuf(struct file *file, void *priv,
1572 struct v4l2_buffer *v4l2_buf)
1573{
1574 struct gspca_dev *gspca_dev = priv;
1575 struct gspca_frame *frame;
1576
Jean-Francois Moine13752bd2008-12-19 15:08:51 -03001577 if (v4l2_buf->index < 0
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001578 || v4l2_buf->index >= gspca_dev->nframes)
1579 return -EINVAL;
1580
1581 frame = &gspca_dev->frame[v4l2_buf->index];
1582 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1583 return 0;
1584}
1585
1586static int vidioc_streamon(struct file *file, void *priv,
1587 enum v4l2_buf_type buf_type)
1588{
1589 struct gspca_dev *gspca_dev = priv;
1590 int ret;
1591
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001592 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1593 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001594 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1595 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001596
Jean-François Moine18bf99a2010-04-09 06:06:11 -03001597 /* check the capture file */
1598 if (gspca_dev->capt_file != file) {
1599 ret = -EBUSY;
1600 goto out;
1601 }
1602
Jean-Francois Moine1edabe72008-12-19 15:17:25 -03001603 if (gspca_dev->nframes == 0
1604 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001605 ret = -EINVAL;
1606 goto out;
1607 }
1608 if (!gspca_dev->streaming) {
1609 ret = gspca_init_transfer(gspca_dev);
1610 if (ret < 0)
1611 goto out;
1612 }
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001613#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001614 if (gspca_debug & D_STREAM) {
1615 PDEBUG_MODE("stream on OK",
1616 gspca_dev->pixfmt,
1617 gspca_dev->width,
1618 gspca_dev->height);
1619 }
1620#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001621 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001622out:
1623 mutex_unlock(&gspca_dev->queue_lock);
1624 return ret;
1625}
1626
1627static int vidioc_streamoff(struct file *file, void *priv,
1628 enum v4l2_buf_type buf_type)
1629{
1630 struct gspca_dev *gspca_dev = priv;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001631 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001632
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001633 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1634 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001635 if (!gspca_dev->streaming)
1636 return 0;
1637 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1638 return -ERESTARTSYS;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001639
Jean-François Moine18bf99a2010-04-09 06:06:11 -03001640 /* check the capture file */
1641 if (gspca_dev->capt_file != file) {
1642 ret = -EBUSY;
1643 goto out;
1644 }
1645
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001646 /* stop streaming */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001647 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1648 ret = -ERESTARTSYS;
1649 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001650 }
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001651 gspca_dev->usb_err = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001652 gspca_stream_off(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001653 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001654
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001655 /* empty the transfer queues */
1656 atomic_set(&gspca_dev->fr_q, 0);
1657 atomic_set(&gspca_dev->fr_i, 0);
1658 gspca_dev->fr_o = 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;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001735 if (n == 0 || n >= GSPCA_MAX_FRAMES)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001736 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
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001758static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1759{
1760 struct gspca_dev *gspca_dev = file->private_data;
Harvey Harrisonefab8212008-07-05 06:12:47 -03001761 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001762 struct page *page;
1763 unsigned long addr, start, size;
1764 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001765
1766 start = vma->vm_start;
1767 size = vma->vm_end - vma->vm_start;
1768 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1769
Hans de Goedee2997a72008-04-23 08:09:12 -03001770 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1771 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001772 if (!gspca_dev->present) {
1773 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001774 goto out;
1775 }
1776 if (gspca_dev->capt_file != file) {
1777 ret = -EINVAL;
1778 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001779 }
1780
Harvey Harrisonefab8212008-07-05 06:12:47 -03001781 frame = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001782 for (i = 0; i < gspca_dev->nframes; ++i) {
1783 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1784 PDEBUG(D_STREAM, "mmap bad memory type");
1785 break;
1786 }
1787 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1788 == vma->vm_pgoff) {
1789 frame = &gspca_dev->frame[i];
1790 break;
1791 }
1792 }
Harvey Harrisonefab8212008-07-05 06:12:47 -03001793 if (frame == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001794 PDEBUG(D_STREAM, "mmap no frame buffer found");
1795 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001796 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001797 }
Jean-François Moine38bff692010-06-24 05:19:58 -03001798 if (size != frame->v4l2_buf.length) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001799 PDEBUG(D_STREAM, "mmap bad size");
1800 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001801 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001802 }
1803
1804 /*
1805 * - VM_IO marks the area as being a mmaped region for I/O to a
1806 * device. It also prevents the region from being core dumped.
1807 */
1808 vma->vm_flags |= VM_IO;
1809
1810 addr = (unsigned long) frame->data;
1811 while (size > 0) {
1812 page = vmalloc_to_page((void *) addr);
1813 ret = vm_insert_page(vma, start, page);
1814 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001815 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001816 start += PAGE_SIZE;
1817 addr += PAGE_SIZE;
1818 size -= PAGE_SIZE;
1819 }
1820
Jean-Francois Moined50e2572009-11-26 15:36:40 -03001821 vma->vm_ops = &gspca_vm_ops;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001822 vma->vm_private_data = frame;
1823 gspca_vm_open(vma);
Hans de Goedee2997a72008-04-23 08:09:12 -03001824 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001825out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001826 mutex_unlock(&gspca_dev->queue_lock);
1827 return ret;
1828}
1829
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001830/*
1831 * wait for a video frame
1832 *
1833 * If a frame is ready, its index is returned.
1834 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001835static int frame_wait(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001836 int nonblock_ing)
1837{
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001838 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001839
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001840 /* check if a frame is ready */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001841 i = gspca_dev->fr_o;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001842 if (i == atomic_read(&gspca_dev->fr_i)) {
Hans de Goede181b7042008-11-19 17:16:26 -03001843 if (nonblock_ing)
1844 return -EAGAIN;
1845
1846 /* wait till a frame is ready */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001847 ret = wait_event_interruptible_timeout(gspca_dev->wq,
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001848 i != atomic_read(&gspca_dev->fr_i) ||
Hans de Goede181b7042008-11-19 17:16:26 -03001849 !gspca_dev->streaming || !gspca_dev->present,
1850 msecs_to_jiffies(3000));
1851 if (ret < 0)
1852 return ret;
1853 if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present)
Hans de Goedee2997a72008-04-23 08:09:12 -03001854 return -EIO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001855 }
Hans de Goede181b7042008-11-19 17:16:26 -03001856
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001857 gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES;
Hans de Goedee2997a72008-04-23 08:09:12 -03001858
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001859 if (gspca_dev->sd_desc->dq_callback) {
1860 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001861 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001862 if (gspca_dev->present)
1863 gspca_dev->sd_desc->dq_callback(gspca_dev);
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001864 mutex_unlock(&gspca_dev->usb_lock);
1865 }
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001866 return gspca_dev->fr_queue[i];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001867}
1868
1869/*
1870 * dequeue a video buffer
1871 *
1872 * If nonblock_ing is false, block until a buffer is available.
1873 */
1874static int vidioc_dqbuf(struct file *file, void *priv,
1875 struct v4l2_buffer *v4l2_buf)
1876{
1877 struct gspca_dev *gspca_dev = priv;
1878 struct gspca_frame *frame;
1879 int i, ret;
1880
1881 PDEBUG(D_FRAM, "dqbuf");
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001882 if (v4l2_buf->memory != gspca_dev->memory)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001883 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001884
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001885 if (!gspca_dev->present)
1886 return -ENODEV;
1887
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001888 /* if not streaming, be sure the application will not loop forever */
1889 if (!(file->f_flags & O_NONBLOCK)
1890 && !gspca_dev->streaming && gspca_dev->users == 1)
1891 return -EINVAL;
1892
1893 /* only the capturing file may dequeue */
1894 if (gspca_dev->capt_file != file)
1895 return -EINVAL;
1896
1897 /* only one dequeue / read at a time */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001898 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1899 return -ERESTARTSYS;
1900
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001901 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001902 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001903 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001904 i = ret; /* frame index */
1905 frame = &gspca_dev->frame[i];
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001906 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001907 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001908 frame->data,
1909 frame->v4l2_buf.bytesused)) {
1910 PDEBUG(D_ERR|D_STREAM,
1911 "dqbuf cp to user failed");
1912 ret = -EFAULT;
1913 goto out;
1914 }
1915 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001916 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1917 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1918 PDEBUG(D_FRAM, "dqbuf %d", i);
1919 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001920out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001921 mutex_unlock(&gspca_dev->read_lock);
1922 return ret;
1923}
1924
1925/*
1926 * queue a video buffer
1927 *
1928 * Attempting to queue a buffer that has already been
1929 * queued will return -EINVAL.
1930 */
1931static int vidioc_qbuf(struct file *file, void *priv,
1932 struct v4l2_buffer *v4l2_buf)
1933{
1934 struct gspca_dev *gspca_dev = priv;
1935 struct gspca_frame *frame;
1936 int i, index, ret;
1937
1938 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001939
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001940 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1941 return -ERESTARTSYS;
1942
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001943 index = v4l2_buf->index;
1944 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001945 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001946 "qbuf idx %d >= %d", index, gspca_dev->nframes);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001947 ret = -EINVAL;
1948 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001949 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001950 if (v4l2_buf->memory != gspca_dev->memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001951 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001952 ret = -EINVAL;
1953 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001954 }
1955
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001956 frame = &gspca_dev->frame[index];
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001957 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1958 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001959 ret = -EINVAL;
1960 goto out;
1961 }
1962
1963 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001964
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001965 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001966 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1967 frame->v4l2_buf.length = v4l2_buf->length;
1968 }
1969
1970 /* put the buffer in the 'queued' queue */
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001971 i = atomic_read(&gspca_dev->fr_q);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001972 gspca_dev->fr_queue[i] = index;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001973 atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001974
1975 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1976 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1977 ret = 0;
1978out:
1979 mutex_unlock(&gspca_dev->queue_lock);
1980 return ret;
1981}
1982
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001983/*
1984 * allocate the resources for read()
1985 */
1986static int read_alloc(struct gspca_dev *gspca_dev,
1987 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001988{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001989 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001990 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001991
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001992 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001993 if (gspca_dev->nframes == 0) {
1994 struct v4l2_requestbuffers rb;
1995
1996 memset(&rb, 0, sizeof rb);
1997 rb.count = gspca_dev->nbufread;
1998 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001999 rb.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002000 ret = vidioc_reqbufs(file, gspca_dev, &rb);
2001 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002002 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002003 return ret;
2004 }
2005 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2006 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002007 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002008 for (i = 0; i < gspca_dev->nbufread; i++) {
2009 v4l2_buf.index = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002010 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2011 if (ret != 0) {
2012 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
2013 return ret;
2014 }
2015 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002016 gspca_dev->memory = GSPCA_MEMORY_READ;
2017 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002018
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002019 /* start streaming */
2020 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2021 if (ret != 0)
2022 PDEBUG(D_STREAM, "read streamon err %d", ret);
2023 return ret;
2024}
2025
2026static unsigned int dev_poll(struct file *file, poll_table *wait)
2027{
2028 struct gspca_dev *gspca_dev = file->private_data;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03002029 int ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002030
2031 PDEBUG(D_FRAM, "poll");
2032
2033 poll_wait(file, &gspca_dev->wq, wait);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002034
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002035 /* if reqbufs is not done, the user would use read() */
2036 if (gspca_dev->nframes == 0) {
2037 if (gspca_dev->memory != GSPCA_MEMORY_NO)
2038 return POLLERR; /* not the 1st time */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002039 ret = read_alloc(gspca_dev, file);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002040 if (ret != 0)
2041 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002042 }
2043
2044 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
2045 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002046
Jean-François Moinef7059ea2010-07-06 04:32:27 -03002047 /* check if an image has been received */
2048 if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i))
2049 ret = POLLIN | POLLRDNORM; /* yes */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002050 else
2051 ret = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002052 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002053 if (!gspca_dev->present)
2054 return POLLHUP;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002055 return ret;
2056}
2057
2058static ssize_t dev_read(struct file *file, char __user *data,
2059 size_t count, loff_t *ppos)
2060{
2061 struct gspca_dev *gspca_dev = file->private_data;
2062 struct gspca_frame *frame;
2063 struct v4l2_buffer v4l2_buf;
2064 struct timeval timestamp;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002065 int n, ret, ret2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002066
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03002067 PDEBUG(D_FRAM, "read (%zd)", count);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002068 if (!gspca_dev->present)
2069 return -ENODEV;
2070 switch (gspca_dev->memory) {
2071 case GSPCA_MEMORY_NO: /* first time */
2072 ret = read_alloc(gspca_dev, file);
2073 if (ret != 0)
2074 return ret;
2075 break;
2076 case GSPCA_MEMORY_READ:
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002077 if (gspca_dev->capt_file == file)
2078 break;
2079 /* fall thru */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002080 default:
2081 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002082 }
2083
2084 /* get a frame */
Paulo Assis2f51d002010-03-07 06:56:56 -03002085 timestamp = ktime_to_timeval(ktime_get());
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002086 timestamp.tv_sec--;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002087 n = 2;
2088 for (;;) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002089 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2090 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002091 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002092 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
2093 if (ret != 0) {
2094 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
2095 return ret;
2096 }
2097
2098 /* if the process slept for more than 1 second,
Frank Zago219423c2008-09-26 07:43:54 -03002099 * get a newer frame */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002100 frame = &gspca_dev->frame[v4l2_buf.index];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002101 if (--n < 0)
2102 break; /* avoid infinite loop */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002103 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
2104 break;
2105 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2106 if (ret != 0) {
2107 PDEBUG(D_STREAM, "read qbuf err %d", ret);
2108 return ret;
2109 }
2110 }
2111
2112 /* copy the frame */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002113 if (count > frame->v4l2_buf.bytesused)
2114 count = frame->v4l2_buf.bytesused;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002115 ret = copy_to_user(data, frame->data, count);
2116 if (ret != 0) {
2117 PDEBUG(D_ERR|D_STREAM,
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03002118 "read cp to user lack %d / %zd", ret, count);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002119 ret = -EFAULT;
2120 goto out;
2121 }
2122 ret = count;
2123out:
2124 /* in each case, requeue the buffer */
2125 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2126 if (ret2 != 0)
2127 return ret2;
2128 return ret;
2129}
2130
Hans Verkuilbec43662008-12-30 06:58:20 -03002131static struct v4l2_file_operations dev_fops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002132 .owner = THIS_MODULE,
2133 .open = dev_open,
2134 .release = dev_close,
2135 .read = dev_read,
2136 .mmap = dev_mmap,
Hans Verkuilbec43662008-12-30 06:58:20 -03002137 .unlocked_ioctl = video_ioctl2,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002138 .poll = dev_poll,
2139};
2140
Hans Verkuila3998102008-07-21 02:57:38 -03002141static const struct v4l2_ioctl_ops dev_ioctl_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002142 .vidioc_querycap = vidioc_querycap,
2143 .vidioc_dqbuf = vidioc_dqbuf,
2144 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03002145 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2146 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2147 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2148 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002149 .vidioc_streamon = vidioc_streamon,
2150 .vidioc_queryctrl = vidioc_queryctrl,
2151 .vidioc_g_ctrl = vidioc_g_ctrl,
2152 .vidioc_s_ctrl = vidioc_s_ctrl,
2153 .vidioc_querymenu = vidioc_querymenu,
2154 .vidioc_enum_input = vidioc_enum_input,
2155 .vidioc_g_input = vidioc_g_input,
2156 .vidioc_s_input = vidioc_s_input,
2157 .vidioc_reqbufs = vidioc_reqbufs,
2158 .vidioc_querybuf = vidioc_querybuf,
2159 .vidioc_streamoff = vidioc_streamoff,
2160 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
2161 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
2162 .vidioc_g_parm = vidioc_g_parm,
2163 .vidioc_s_parm = vidioc_s_parm,
Hans de Goede11c635a2009-05-25 15:04:22 -03002164 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Antonio Ospite28ffe772009-12-02 06:18:46 -03002165 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03002166#ifdef CONFIG_VIDEO_ADV_DEBUG
2167 .vidioc_g_register = vidioc_g_register,
2168 .vidioc_s_register = vidioc_s_register,
2169#endif
2170 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002171};
2172
Hans Verkuila3998102008-07-21 02:57:38 -03002173static struct video_device gspca_template = {
2174 .name = "gspca main driver",
Hans Verkuila3998102008-07-21 02:57:38 -03002175 .fops = &dev_fops,
2176 .ioctl_ops = &dev_ioctl_ops,
Hans de Goeded0182302008-11-19 17:10:47 -03002177 .release = gspca_release,
Hans Verkuila3998102008-07-21 02:57:38 -03002178};
2179
Jean-François Moine6a330912010-10-02 04:12:25 -03002180/* initialize the controls */
2181static void ctrls_init(struct gspca_dev *gspca_dev)
2182{
2183 struct gspca_ctrl *ctrl;
2184 int i;
2185
2186 for (i = 0, ctrl = gspca_dev->cam.ctrls;
2187 i < gspca_dev->sd_desc->nctrls;
2188 i++, ctrl++) {
2189 ctrl->def = gspca_dev->sd_desc->ctrls[i].qctrl.default_value;
2190 ctrl->val = ctrl->def;
2191 ctrl->min = gspca_dev->sd_desc->ctrls[i].qctrl.minimum;
2192 ctrl->max = gspca_dev->sd_desc->ctrls[i].qctrl.maximum;
2193 }
2194}
2195
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002196/*
2197 * probe and create a new gspca device
2198 *
2199 * This function must be called by the sub-driver when it is
2200 * called for probing a new device.
2201 */
Jean-François Moine44628642010-06-05 07:47:36 -03002202int gspca_dev_probe2(struct usb_interface *intf,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002203 const struct usb_device_id *id,
2204 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002205 int dev_size,
2206 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002207{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002208 struct gspca_dev *gspca_dev;
2209 struct usb_device *dev = interface_to_usbdev(intf);
2210 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002211
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002212 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002213
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002214 /* create the device */
2215 if (dev_size < sizeof *gspca_dev)
2216 dev_size = sizeof *gspca_dev;
2217 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
Erik Andrene293e592008-10-03 08:46:50 -03002218 if (!gspca_dev) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002219 err("couldn't kzalloc gspca struct");
Erik Andrene293e592008-10-03 08:46:50 -03002220 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002221 }
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002222 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2223 if (!gspca_dev->usb_buf) {
2224 err("out of memory");
Erik Andrene293e592008-10-03 08:46:50 -03002225 ret = -ENOMEM;
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002226 goto out;
2227 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002228 gspca_dev->dev = dev;
Jean-François Moine44628642010-06-05 07:47:36 -03002229 gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002230 gspca_dev->nbalt = intf->num_altsetting;
Jean-François Moine35680ba2010-07-14 06:30:18 -03002231
2232 /* check if any audio device */
2233 if (dev->config->desc.bNumInterfaces != 1) {
2234 int i;
2235 struct usb_interface *intf2;
2236
2237 for (i = 0; i < dev->config->desc.bNumInterfaces; i++) {
2238 intf2 = dev->config->interface[i];
2239 if (intf2 != NULL
2240 && intf2->altsetting != NULL
2241 && intf2->altsetting->desc.bInterfaceClass ==
2242 USB_CLASS_AUDIO) {
2243 gspca_dev->audio = 1;
2244 break;
2245 }
2246 }
2247 }
2248
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002249 gspca_dev->sd_desc = sd_desc;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002250 gspca_dev->nbufread = 2;
Jean-Francois Moineff374742008-10-23 07:29:51 -03002251 gspca_dev->empty_packet = -1; /* don't check the empty packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002252
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002253 /* configure the subdriver and initialize the USB device */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002254 ret = sd_desc->config(gspca_dev, id);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002255 if (ret < 0)
2256 goto out;
Jean-François Moine6a330912010-10-02 04:12:25 -03002257 if (gspca_dev->cam.ctrls != NULL)
2258 ctrls_init(gspca_dev);
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002259 ret = sd_desc->init(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002260 if (ret < 0)
2261 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002262 gspca_set_default_mode(gspca_dev);
2263
Hans de Goede10ee2402010-02-19 04:41:40 -03002264 ret = gspca_input_connect(gspca_dev);
2265 if (ret)
2266 goto out;
2267
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002268 mutex_init(&gspca_dev->usb_lock);
2269 mutex_init(&gspca_dev->read_lock);
2270 mutex_init(&gspca_dev->queue_lock);
2271 init_waitqueue_head(&gspca_dev->wq);
2272
2273 /* init video stuff */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002274 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
Hans de Goede24441632009-03-27 16:08:20 -03002275 gspca_dev->vdev.parent = &intf->dev;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03002276 gspca_dev->module = module;
Oliver Neukumee56a4d2008-07-27 14:01:59 -03002277 gspca_dev->present = 1;
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002278 ret = video_register_device(&gspca_dev->vdev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002279 VFL_TYPE_GRABBER,
Jean-Francois Moine8d584a52009-01-13 06:07:59 -03002280 -1);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002281 if (ret < 0) {
2282 err("video_register_device err %d", ret);
2283 goto out;
2284 }
2285
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002286 usb_set_intfdata(intf, gspca_dev);
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002287 PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
Márton Németh0274d422010-01-28 06:39:49 -03002288
Hans de Goede10ee2402010-02-19 04:41:40 -03002289 gspca_input_create_urb(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002290
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002291 return 0;
2292out:
Randy Dunlapf7628012010-03-30 14:32:04 -03002293#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Hans de Goede10ee2402010-02-19 04:41:40 -03002294 if (gspca_dev->input_dev)
2295 input_unregister_device(gspca_dev->input_dev);
Hans de Goedee7637522010-02-19 05:11:09 -03002296#endif
Jean-Francois Moinee07a1d82008-11-19 06:37:53 -03002297 kfree(gspca_dev->usb_buf);
2298 kfree(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002299 return ret;
2300}
Jean-François Moine44628642010-06-05 07:47:36 -03002301EXPORT_SYMBOL(gspca_dev_probe2);
2302
2303/* same function as the previous one, but check the interface */
2304int gspca_dev_probe(struct usb_interface *intf,
2305 const struct usb_device_id *id,
2306 const struct sd_desc *sd_desc,
2307 int dev_size,
2308 struct module *module)
2309{
2310 struct usb_device *dev = interface_to_usbdev(intf);
2311
2312 /* we don't handle multi-config cameras */
2313 if (dev->descriptor.bNumConfigurations != 1) {
Jean-François Moine0b656322010-09-13 05:19:58 -03002314 err("%04x:%04x too many config",
Jean-François Moine44628642010-06-05 07:47:36 -03002315 id->idVendor, id->idProduct);
2316 return -ENODEV;
2317 }
2318
2319 /* the USB video interface must be the first one */
2320 if (dev->config->desc.bNumInterfaces != 1
Jean-François Moinec4dc6922010-07-14 06:26:54 -03002321 && intf->cur_altsetting->desc.bInterfaceNumber != 0)
Jean-François Moine44628642010-06-05 07:47:36 -03002322 return -ENODEV;
Jean-François Moine44628642010-06-05 07:47:36 -03002323
2324 return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
2325}
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002326EXPORT_SYMBOL(gspca_dev_probe);
2327
2328/*
2329 * USB disconnection
2330 *
2331 * This function must be called by the sub-driver
2332 * when the device disconnects, after the specific resources are freed.
2333 */
2334void gspca_disconnect(struct usb_interface *intf)
2335{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002336 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Randy Dunlapf7628012010-03-30 14:32:04 -03002337#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -03002338 struct input_dev *input_dev;
Jean-François Moinee070bf92010-02-18 15:02:51 -03002339#endif
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002340
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002341 PDEBUG(D_PROBE, "%s disconnect",
2342 video_device_node_name(&gspca_dev->vdev));
Adam Bakerad281272009-02-04 15:33:21 -03002343 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03002344 gspca_dev->present = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002345
2346 if (gspca_dev->streaming) {
2347 destroy_urbs(gspca_dev);
2348 wake_up_interruptible(&gspca_dev->wq);
2349 }
2350
Randy Dunlapf7628012010-03-30 14:32:04 -03002351#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -03002352 gspca_input_destroy_urb(gspca_dev);
2353 input_dev = gspca_dev->input_dev;
2354 if (input_dev) {
2355 gspca_dev->input_dev = NULL;
2356 input_unregister_device(input_dev);
2357 }
Jean-François Moinee070bf92010-02-18 15:02:51 -03002358#endif
Márton Németh0274d422010-01-28 06:39:49 -03002359
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002360 /* the device is freed at exit of this function */
2361 gspca_dev->dev = NULL;
Adam Bakerad281272009-02-04 15:33:21 -03002362 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03002363
Frank Zago6b060ff2008-09-28 08:12:22 -03002364 usb_set_intfdata(intf, NULL);
2365
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002366 /* release the device */
2367 /* (this will call gspca_release() immediatly or on last close) */
2368 video_unregister_device(&gspca_dev->vdev);
Jean-Francois Moine3145b8c2008-10-05 04:22:10 -03002369
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002370/* PDEBUG(D_PROBE, "disconnect complete"); */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002371}
2372EXPORT_SYMBOL(gspca_disconnect);
2373
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002374#ifdef CONFIG_PM
2375int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2376{
2377 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2378
2379 if (!gspca_dev->streaming)
2380 return 0;
2381 gspca_dev->frozen = 1; /* avoid urb error messages */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002382 if (gspca_dev->sd_desc->stopN)
2383 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002384 destroy_urbs(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002385 gspca_input_destroy_urb(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002386 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002387 if (gspca_dev->sd_desc->stop0)
2388 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002389 return 0;
2390}
2391EXPORT_SYMBOL(gspca_suspend);
2392
2393int gspca_resume(struct usb_interface *intf)
2394{
2395 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2396
2397 gspca_dev->frozen = 0;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002398 gspca_dev->sd_desc->init(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002399 gspca_input_create_urb(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002400 if (gspca_dev->streaming)
2401 return gspca_init_transfer(gspca_dev);
Jean-Francois Moine1e89e2da2008-09-03 17:12:15 -03002402 return 0;
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002403}
2404EXPORT_SYMBOL(gspca_resume);
2405#endif
Hans de Goededcef32372008-07-10 10:40:53 -03002406/* -- cam driver utility functions -- */
2407
2408/* auto gain and exposure algorithm based on the knee algorithm described here:
2409 http://ytse.tricolour.net/docs/LowLightOptimization.html
2410
2411 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2412 where changed. */
2413int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2414 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2415{
2416 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2417 const struct ctrl *gain_ctrl = NULL;
2418 const struct ctrl *exposure_ctrl = NULL;
2419 const struct ctrl *autogain_ctrl = NULL;
2420 int retval = 0;
2421
2422 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goede26984b02010-02-01 13:18:37 -03002423 if (gspca_dev->ctrl_dis & (1 << i))
2424 continue;
Hans de Goededcef32372008-07-10 10:40:53 -03002425 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2426 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2427 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2428 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2429 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2430 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2431 }
2432 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2433 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2434 "on cam without (auto)gain/exposure");
2435 return 0;
2436 }
2437
2438 if (gain_ctrl->get(gspca_dev, &gain) ||
2439 exposure_ctrl->get(gspca_dev, &exposure) ||
2440 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2441 return 0;
2442
2443 orig_gain = gain;
2444 orig_exposure = exposure;
2445
2446 /* If we are of a multiple of deadzone, do multiple steps to reach the
2447 desired lumination fast (with the risc of a slight overshoot) */
2448 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2449
Jean-Francois Moine1c44d812008-11-10 05:56:55 -03002450 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
Hans de Goededcef32372008-07-10 10:40:53 -03002451 avg_lum, desired_avg_lum, steps);
2452
2453 for (i = 0; i < steps; i++) {
2454 if (avg_lum > desired_avg_lum) {
2455 if (gain > gain_knee)
2456 gain--;
2457 else if (exposure > exposure_knee)
2458 exposure--;
2459 else if (gain > gain_ctrl->qctrl.default_value)
2460 gain--;
2461 else if (exposure > exposure_ctrl->qctrl.minimum)
2462 exposure--;
2463 else if (gain > gain_ctrl->qctrl.minimum)
2464 gain--;
2465 else
2466 break;
2467 } else {
2468 if (gain < gain_ctrl->qctrl.default_value)
2469 gain++;
2470 else if (exposure < exposure_knee)
2471 exposure++;
2472 else if (gain < gain_knee)
2473 gain++;
2474 else if (exposure < exposure_ctrl->qctrl.maximum)
2475 exposure++;
2476 else if (gain < gain_ctrl->qctrl.maximum)
2477 gain++;
2478 else
2479 break;
2480 }
2481 }
2482
2483 if (gain != orig_gain) {
2484 gain_ctrl->set(gspca_dev, gain);
2485 retval = 1;
2486 }
2487 if (exposure != orig_exposure) {
2488 exposure_ctrl->set(gspca_dev, exposure);
2489 retval = 1;
2490 }
2491
2492 return retval;
2493}
2494EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2495
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002496/* -- module insert / remove -- */
2497static int __init gspca_init(void)
2498{
Jean-François Moine54826432010-09-13 04:53:03 -03002499 info("v%d.%d.%d registered",
Jean-Francois Moinebb64e862008-07-19 06:49:28 -03002500 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2501 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2502 DRIVER_VERSION_NUMBER & 0xff);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002503 return 0;
2504}
2505static void __exit gspca_exit(void)
2506{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002507}
2508
2509module_init(gspca_init);
2510module_exit(gspca_exit);
2511
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03002512#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002513module_param_named(debug, gspca_debug, int, 0644);
2514MODULE_PARM_DESC(debug,
2515 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002516 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
2517 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002518#endif