blob: 4a3bbffafcd4c3ef004d1bc81067f82a19d5fa5b [file] [log] [blame]
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001/*
2 * Main USB camera driver
3 *
Jean-François Moine59929ce2011-03-14 05:33:01 -03004 * Copyright (C) 2008-2011 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
Joe Perches133a9fe2011-08-21 19:56:57 -030024#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
Jean-François Moinef22123f2011-09-23 05:17:10 -030026#define GSPCA_VERSION "2.14.0"
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030027
28#include <linux/init.h>
29#include <linux/fs.h>
30#include <linux/vmalloc.h>
31#include <linux/sched.h>
32#include <linux/slab.h>
33#include <linux/mm.h>
34#include <linux/string.h>
35#include <linux/pagemap.h>
Jean-Francois Moine956e42d2008-07-01 10:03:42 -030036#include <linux/io.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030037#include <asm/page.h>
Jean-Francois Moine956e42d2008-07-01 10:03:42 -030038#include <linux/uaccess.h>
Paulo Assis2f51d002010-03-07 06:56:56 -030039#include <linux/ktime.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030040#include <media/v4l2-ioctl.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030041
42#include "gspca.h"
43
Randy Dunlapf7628012010-03-30 14:32:04 -030044#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -030045#include <linux/input.h>
46#include <linux/usb/input.h>
Jean-François Moinee070bf92010-02-18 15:02:51 -030047#endif
Márton Németh0274d422010-01-28 06:39:49 -030048
Jean-Francois Moined43fa322008-06-12 10:58:58 -030049/* global values */
Jean-Francois Moine8fee8452009-01-17 04:46:38 -030050#define DEF_NURBS 3 /* default number of URBs */
51#if DEF_NURBS > MAX_NURBS
52#error "DEF_NURBS too big"
53#endif
Jean-Francois Moined43fa322008-06-12 10:58:58 -030054
Jean-François Moine27c6f9e2010-04-02 06:59:13 -030055MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030056MODULE_DESCRIPTION("GSPCA USB Camera Driver");
57MODULE_LICENSE("GPL");
Jean-François Moinef22123f2011-09-23 05:17:10 -030058MODULE_VERSION(GSPCA_VERSION);
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)
Joe Perches133a9fe2011-08-21 19:56:57 -0300151 pr_err("Resubmit URB failed with error %i\n", 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) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300180 pr_err("Input device registration failed with error %i\n",
181 err);
Márton Németh0274d422010-01-28 06:39:49 -0300182 input_dev->dev.parent = NULL;
183 input_free_device(input_dev);
184 } else {
185 dev->input_dev = input_dev;
186 }
Hans de Goede10ee2402010-02-19 04:41:40 -0300187 }
Márton Németh0274d422010-01-28 06:39:49 -0300188
189 return err;
190}
191
192static int alloc_and_submit_int_urb(struct gspca_dev *gspca_dev,
193 struct usb_endpoint_descriptor *ep)
194{
195 unsigned int buffer_len;
196 int interval;
197 struct urb *urb;
198 struct usb_device *dev;
199 void *buffer = NULL;
200 int ret = -EINVAL;
201
Jean-François Moineb26d6e22010-04-29 06:01:53 -0300202 buffer_len = le16_to_cpu(ep->wMaxPacketSize);
Márton Németh0274d422010-01-28 06:39:49 -0300203 interval = ep->bInterval;
Jean-François Moinec4dc6922010-07-14 06:26:54 -0300204 PDEBUG(D_CONF, "found int in endpoint: 0x%x, "
Márton Németh0274d422010-01-28 06:39:49 -0300205 "buffer_len=%u, interval=%u",
206 ep->bEndpointAddress, buffer_len, interval);
207
208 dev = gspca_dev->dev;
209
210 urb = usb_alloc_urb(0, GFP_KERNEL);
211 if (!urb) {
212 ret = -ENOMEM;
213 goto error;
214 }
215
Linus Torvalds7a9b1492010-05-20 21:26:12 -0700216 buffer = usb_alloc_coherent(dev, buffer_len,
Márton Németh0274d422010-01-28 06:39:49 -0300217 GFP_KERNEL, &urb->transfer_dma);
218 if (!buffer) {
219 ret = -ENOMEM;
220 goto error_buffer;
221 }
222 usb_fill_int_urb(urb, dev,
223 usb_rcvintpipe(dev, ep->bEndpointAddress),
224 buffer, buffer_len,
225 int_irq, (void *)gspca_dev, interval);
Jason Wang882787f2010-09-03 06:57:19 -0300226 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Márton Németh0274d422010-01-28 06:39:49 -0300227 ret = usb_submit_urb(urb, GFP_KERNEL);
228 if (ret < 0) {
Jean-François Moinec4dc6922010-07-14 06:26:54 -0300229 PDEBUG(D_ERR, "submit int URB failed with error %i", ret);
Márton Németh0274d422010-01-28 06:39:49 -0300230 goto error_submit;
231 }
Hans de Goede2661da42010-10-26 05:16:32 -0300232 gspca_dev->int_urb = urb;
Márton Németh0274d422010-01-28 06:39:49 -0300233 return ret;
234
235error_submit:
Daniel Mack997ea582010-04-12 13:17:25 +0200236 usb_free_coherent(dev,
237 urb->transfer_buffer_length,
238 urb->transfer_buffer,
239 urb->transfer_dma);
Márton Németh0274d422010-01-28 06:39:49 -0300240error_buffer:
241 usb_free_urb(urb);
242error:
243 return ret;
244}
245
Hans de Goede10ee2402010-02-19 04:41:40 -0300246static void gspca_input_create_urb(struct gspca_dev *gspca_dev)
Márton Németh0274d422010-01-28 06:39:49 -0300247{
Márton Németh0274d422010-01-28 06:39:49 -0300248 struct usb_interface *intf;
249 struct usb_host_interface *intf_desc;
250 struct usb_endpoint_descriptor *ep;
251 int i;
252
253 if (gspca_dev->sd_desc->int_pkt_scan) {
254 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
255 intf_desc = intf->cur_altsetting;
256 for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
257 ep = &intf_desc->endpoint[i].desc;
258 if (usb_endpoint_dir_in(ep) &&
259 usb_endpoint_xfer_int(ep)) {
260
Hans de Goede10ee2402010-02-19 04:41:40 -0300261 alloc_and_submit_int_urb(gspca_dev, ep);
Márton Németh0274d422010-01-28 06:39:49 -0300262 break;
263 }
264 }
265 }
Márton Németh0274d422010-01-28 06:39:49 -0300266}
267
268static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
269{
270 struct urb *urb;
271
272 urb = gspca_dev->int_urb;
273 if (urb) {
274 gspca_dev->int_urb = NULL;
275 usb_kill_urb(urb);
Daniel Mack997ea582010-04-12 13:17:25 +0200276 usb_free_coherent(gspca_dev->dev,
277 urb->transfer_buffer_length,
278 urb->transfer_buffer,
279 urb->transfer_dma);
Márton Németh0274d422010-01-28 06:39:49 -0300280 usb_free_urb(urb);
281 }
282}
Randy Dunlapf7628012010-03-30 14:32:04 -0300283#else
284static inline void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
285{
286}
287
288static inline void gspca_input_create_urb(struct gspca_dev *gspca_dev)
289{
290}
291
292static inline int gspca_input_connect(struct gspca_dev *dev)
293{
294 return 0;
295}
Márton Németh0274d422010-01-28 06:39:49 -0300296#endif
297
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300298/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300299 * fill a video frame from an URB and resubmit
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300300 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300301static void fill_frame(struct gspca_dev *gspca_dev,
302 struct urb *urb)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300303{
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300304 u8 *data; /* address of data in the iso message */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300305 int i, len, st;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300306 cam_pkt_op pkt_scan;
307
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300308 if (urb->status != 0) {
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300309 if (urb->status == -ESHUTDOWN)
310 return; /* disconnection */
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300311#ifdef CONFIG_PM
Hans de Goede6a540bd2009-10-24 15:02:14 -0300312 if (gspca_dev->frozen)
313 return;
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300314#endif
Hans de Goede6a540bd2009-10-24 15:02:14 -0300315 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine81578522009-11-13 07:15:08 -0300316 urb->status = 0;
Hans de Goedebf926ad2009-10-23 06:56:06 -0300317 goto resubmit;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300318 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300319 pkt_scan = gspca_dev->sd_desc->pkt_scan;
320 for (i = 0; i < urb->number_of_packets; i++) {
Mauro Carvalho Chehab95f18be2010-12-27 12:00:03 -0300321 len = urb->iso_frame_desc[i].actual_length;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300322
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300323 /* check the packet status and length */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300324 st = urb->iso_frame_desc[i].status;
325 if (st) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300326 pr_err("ISOC data error: [%d] len=%d, status=%d\n",
327 i, len, st);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300328 gspca_dev->last_packet_type = DISCARD_PACKET;
329 continue;
330 }
Jean-François Moine76ebc162010-12-25 13:22:21 -0300331 if (len == 0) {
332 if (gspca_dev->empty_packet == 0)
333 gspca_dev->empty_packet = 1;
334 continue;
335 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300336
337 /* let the packet be analyzed by the subdriver */
338 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
339 i, urb->iso_frame_desc[i].offset, len);
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300340 data = (u8 *) urb->transfer_buffer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300341 + urb->iso_frame_desc[i].offset;
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300342 pkt_scan(gspca_dev, data, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300343 }
344
Hans de Goedebf926ad2009-10-23 06:56:06 -0300345resubmit:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300346 /* resubmit the URB */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300347 st = usb_submit_urb(urb, GFP_ATOMIC);
348 if (st < 0)
Joe Perches133a9fe2011-08-21 19:56:57 -0300349 pr_err("usb_submit_urb() ret %d\n", 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)
Joe Perches133a9fe2011-08-21 19:56:57 -0300403 pr_err("usb_submit_urb() ret %d\n", 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.
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300417 */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300418void gspca_frame_add(struct gspca_dev *gspca_dev,
419 enum gspca_packet_type packet_type,
420 const u8 *data,
421 int len)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300422{
Jean-Francois Moine3ec342f2009-11-13 07:38:16 -0300423 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300424 int i, j;
425
Hans de Goedee2997a72008-04-23 08:09:12 -0300426 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300427
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300428 if (packet_type == FIRST_PACKET) {
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300429 i = atomic_read(&gspca_dev->fr_i);
430
431 /* if there are no queued buffer, discard the whole frame */
432 if (i == atomic_read(&gspca_dev->fr_q)) {
433 gspca_dev->last_packet_type = DISCARD_PACKET;
Jean-François Moine2e0544a2010-10-16 13:59:13 -0300434 gspca_dev->sequence++;
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300435 return;
436 }
Jean-François Moineb192ca92010-06-27 03:08:19 -0300437 j = gspca_dev->fr_queue[i];
438 frame = &gspca_dev->frame[j];
Paulo Assis2f51d002010-03-07 06:56:56 -0300439 frame->v4l2_buf.timestamp = ktime_to_timeval(ktime_get());
Jean-François Moine2e0544a2010-10-16 13:59:13 -0300440 frame->v4l2_buf.sequence = gspca_dev->sequence++;
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300441 gspca_dev->image = frame->data;
Jean-François Moineb192ca92010-06-27 03:08:19 -0300442 gspca_dev->image_len = 0;
Jean-François Moinefe988f52010-07-29 02:46:02 -0300443 } else {
444 switch (gspca_dev->last_packet_type) {
445 case DISCARD_PACKET:
Hans de Goedefa4376d2011-06-05 03:44:43 -0300446 if (packet_type == LAST_PACKET) {
Jean-François Moinefe988f52010-07-29 02:46:02 -0300447 gspca_dev->last_packet_type = packet_type;
Hans de Goedefa4376d2011-06-05 03:44:43 -0300448 gspca_dev->image = NULL;
449 gspca_dev->image_len = 0;
450 }
Jean-François Moinefe988f52010-07-29 02:46:02 -0300451 return;
452 case LAST_PACKET:
453 return;
454 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300455 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300456
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300457 /* append the packet to the frame buffer */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300458 if (len > 0) {
Jean-François Moineb192ca92010-06-27 03:08:19 -0300459 if (gspca_dev->image_len + len > gspca_dev->frsz) {
460 PDEBUG(D_ERR|D_PACK, "frame overflow %d > %d",
461 gspca_dev->image_len + len,
462 gspca_dev->frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300463 packet_type = DISCARD_PACKET;
464 } else {
Jean-François Moinefe988f52010-07-29 02:46:02 -0300465/* !! image is NULL only when last pkt is LAST or DISCARD
466 if (gspca_dev->image == NULL) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300467 pr_err("gspca_frame_add() image == NULL\n");
Jean-François Moinefe988f52010-07-29 02:46:02 -0300468 return;
469 }
470 */
Jean-François Moineb192ca92010-06-27 03:08:19 -0300471 memcpy(gspca_dev->image + gspca_dev->image_len,
472 data, len);
473 gspca_dev->image_len += len;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300474 }
475 }
476 gspca_dev->last_packet_type = packet_type;
477
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300478 /* if last packet, invalidate packet concatenation until
479 * next first packet, wake up the application and advance
480 * in the queue */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300481 if (packet_type == LAST_PACKET) {
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300482 i = atomic_read(&gspca_dev->fr_i);
Jean-François Moineb192ca92010-06-27 03:08:19 -0300483 j = gspca_dev->fr_queue[i];
484 frame = &gspca_dev->frame[j];
485 frame->v4l2_buf.bytesused = gspca_dev->image_len;
Jean-François Moine02bbcb92010-07-06 04:16:40 -0300486 frame->v4l2_buf.flags = (frame->v4l2_buf.flags
487 | V4L2_BUF_FLAG_DONE)
488 & ~V4L2_BUF_FLAG_QUEUED;
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300489 i = (i + 1) % GSPCA_MAX_FRAMES;
490 atomic_set(&gspca_dev->fr_i, i);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300491 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300492 PDEBUG(D_FRAM, "frame complete len:%d",
493 frame->v4l2_buf.bytesused);
494 gspca_dev->image = NULL;
495 gspca_dev->image_len = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300496 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300497}
498EXPORT_SYMBOL(gspca_frame_add);
499
Hans de Goeded642de22010-12-31 04:51:36 -0300500static int frame_alloc(struct gspca_dev *gspca_dev, struct file *file,
501 enum v4l2_memory memory, unsigned int count)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300502{
503 struct gspca_frame *frame;
504 unsigned int frsz;
505 int i;
506
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300507 i = gspca_dev->curr_mode;
508 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300509 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300510 frsz = PAGE_ALIGN(frsz);
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300511 if (count >= GSPCA_MAX_FRAMES)
512 count = GSPCA_MAX_FRAMES - 1;
Jean-François Moinee795d912010-06-24 05:14:22 -0300513 gspca_dev->frbuf = vmalloc_32(frsz * count);
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300514 if (!gspca_dev->frbuf) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300515 pr_err("frame alloc failed\n");
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300516 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300517 }
Hans de Goeded642de22010-12-31 04:51:36 -0300518 gspca_dev->capt_file = file;
519 gspca_dev->memory = memory;
520 gspca_dev->frsz = frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300521 gspca_dev->nframes = count;
522 for (i = 0; i < count; i++) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300523 frame = &gspca_dev->frame[i];
524 frame->v4l2_buf.index = i;
525 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
526 frame->v4l2_buf.flags = 0;
527 frame->v4l2_buf.field = V4L2_FIELD_NONE;
528 frame->v4l2_buf.length = frsz;
Hans de Goeded642de22010-12-31 04:51:36 -0300529 frame->v4l2_buf.memory = memory;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300530 frame->v4l2_buf.sequence = 0;
Jean-François Moineb192ca92010-06-27 03:08:19 -0300531 frame->data = gspca_dev->frbuf + i * frsz;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300532 frame->v4l2_buf.m.offset = i * frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300533 }
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300534 atomic_set(&gspca_dev->fr_q, 0);
535 atomic_set(&gspca_dev->fr_i, 0);
536 gspca_dev->fr_o = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300537 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300538}
539
540static void frame_free(struct gspca_dev *gspca_dev)
541{
542 int i;
543
544 PDEBUG(D_STREAM, "frame free");
Harvey Harrisonefab8212008-07-05 06:12:47 -0300545 if (gspca_dev->frbuf != NULL) {
Jean-François Moinee795d912010-06-24 05:14:22 -0300546 vfree(gspca_dev->frbuf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300547 gspca_dev->frbuf = NULL;
548 for (i = 0; i < gspca_dev->nframes; i++)
549 gspca_dev->frame[i].data = NULL;
550 }
551 gspca_dev->nframes = 0;
Hans de Goeded642de22010-12-31 04:51:36 -0300552 gspca_dev->frsz = 0;
553 gspca_dev->capt_file = NULL;
554 gspca_dev->memory = GSPCA_MEMORY_NO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300555}
556
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300557static void destroy_urbs(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300558{
559 struct urb *urb;
560 unsigned int i;
561
562 PDEBUG(D_STREAM, "kill transfer");
Erik Andrén44732812008-10-13 15:52:46 -0300563 for (i = 0; i < MAX_NURBS; i++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300564 urb = gspca_dev->urb[i];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300565 if (urb == NULL)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300566 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300567
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300568 gspca_dev->urb[i] = NULL;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300569 usb_kill_urb(urb);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300570 if (urb->transfer_buffer != NULL)
Daniel Mack997ea582010-04-12 13:17:25 +0200571 usb_free_coherent(gspca_dev->dev,
572 urb->transfer_buffer_length,
573 urb->transfer_buffer,
574 urb->transfer_dma);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300575 usb_free_urb(urb);
576 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300577}
578
Jean-François Moinee4dac282010-04-09 06:11:36 -0300579static int gspca_set_alt0(struct gspca_dev *gspca_dev)
580{
581 int ret;
582
583 if (gspca_dev->alt == 0)
584 return 0;
585 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
586 if (ret < 0)
Joe Perches133a9fe2011-08-21 19:56:57 -0300587 pr_err("set alt 0 err %d\n", ret);
Jean-François Moinee4dac282010-04-09 06:11:36 -0300588 return ret;
589}
590
591/* Note: both the queue and the usb locks should be held when calling this */
592static void gspca_stream_off(struct gspca_dev *gspca_dev)
593{
594 gspca_dev->streaming = 0;
595 if (gspca_dev->present) {
596 if (gspca_dev->sd_desc->stopN)
597 gspca_dev->sd_desc->stopN(gspca_dev);
598 destroy_urbs(gspca_dev);
599 gspca_input_destroy_urb(gspca_dev);
600 gspca_set_alt0(gspca_dev);
601 gspca_input_create_urb(gspca_dev);
602 }
603
604 /* always call stop0 to free the subdriver's resources */
605 if (gspca_dev->sd_desc->stop0)
606 gspca_dev->sd_desc->stop0(gspca_dev);
607 PDEBUG(D_STREAM, "stream off OK");
608}
609
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300610/*
Frank Zago219423c2008-09-26 07:43:54 -0300611 * look for an input transfer endpoint in an alternate setting
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300612 */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300613static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300614 int xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300615{
616 struct usb_host_endpoint *ep;
617 int i, attr;
618
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300619 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
620 ep = &alt->endpoint[i];
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300621 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Jean-Francois Moine30d35e42009-04-21 13:57:31 -0300622 if (attr == xfer
Patrice Chotard71062252011-03-21 16:58:56 -0300623 && ep->desc.wMaxPacketSize != 0
624 && usb_endpoint_dir_in(&ep->desc))
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300625 return ep;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300626 }
627 return NULL;
628}
629
Jean-François Moine27b12022011-08-10 07:38:48 -0300630/* compute the minimum bandwidth for the current transfer */
631static u32 which_bandwidth(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300632{
Jean-François Moine27b12022011-08-10 07:38:48 -0300633 u32 bandwidth;
634 int i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300635
Jean-François Moine18bef422012-01-04 13:44:02 -0300636 /* get the (max) image size */
Jean-François Moine27b12022011-08-10 07:38:48 -0300637 i = gspca_dev->curr_mode;
638 bandwidth = gspca_dev->cam.cam_mode[i].sizeimage;
639
Jean-François Moine18bef422012-01-04 13:44:02 -0300640 /* if the image is compressed, estimate its mean size */
Jean-François Moine27b12022011-08-10 07:38:48 -0300641 if (bandwidth < gspca_dev->cam.cam_mode[i].width *
642 gspca_dev->cam.cam_mode[i].height)
Jean-François Moine18bef422012-01-04 13:44:02 -0300643 bandwidth = bandwidth * 3 / 8; /* 0.375 */
Jean-François Moine27b12022011-08-10 07:38:48 -0300644
645 /* estimate the frame rate */
646 if (gspca_dev->sd_desc->get_streamparm) {
647 struct v4l2_streamparm parm;
648
Jean-François Moine27b12022011-08-10 07:38:48 -0300649 gspca_dev->sd_desc->get_streamparm(gspca_dev, &parm);
650 bandwidth *= parm.parm.capture.timeperframe.denominator;
Hans de Goede1153f042011-12-30 09:13:07 -0300651 bandwidth /= parm.parm.capture.timeperframe.numerator;
Hans de Goedea511ba92009-10-16 07:13:07 -0300652 } else {
Jean-François Moine18bef422012-01-04 13:44:02 -0300653
654 /* don't hope more than 15 fps with USB 1.1 and
655 * image resolution >= 640x480 */
656 if (gspca_dev->width >= 640
657 && gspca_dev->dev->speed == USB_SPEED_FULL)
658 bandwidth *= 15; /* 15 fps */
659 else
660 bandwidth *= 30; /* 30 fps */
Jean-François Moine27b12022011-08-10 07:38:48 -0300661 }
662
663 PDEBUG(D_STREAM, "min bandwidth: %d", bandwidth);
664 return bandwidth;
665}
666
667/* endpoint table */
668#define MAX_ALT 16
669struct ep_tb_s {
670 u32 alt;
671 u32 bandwidth;
672};
673
674/*
675 * build the table of the endpoints
676 * and compute the minimum bandwidth for the image transfer
677 */
Hans de Goede66957b82011-12-30 07:58:10 -0300678static int build_isoc_ep_tb(struct gspca_dev *gspca_dev,
Jean-François Moine27b12022011-08-10 07:38:48 -0300679 struct usb_interface *intf,
Jean-François Moine27b12022011-08-10 07:38:48 -0300680 struct ep_tb_s *ep_tb)
681{
682 struct usb_host_endpoint *ep;
683 int i, j, nbalt, psize, found;
684 u32 bandwidth, last_bw;
685
686 nbalt = intf->num_altsetting;
687 if (nbalt > MAX_ALT)
688 nbalt = MAX_ALT; /* fixme: should warn */
689
690 /* build the endpoint table */
691 i = 0;
692 last_bw = 0;
693 for (;;) {
694 ep_tb->bandwidth = 2000 * 2000 * 120;
695 found = 0;
696 for (j = 0; j < nbalt; j++) {
Hans de Goede66957b82011-12-30 07:58:10 -0300697 ep = alt_xfer(&intf->altsetting[j],
698 USB_ENDPOINT_XFER_ISOC);
Jean-François Moine27b12022011-08-10 07:38:48 -0300699 if (ep == NULL)
700 continue;
Hans de Goeded0d34352011-12-30 08:20:50 -0300701 if (ep->desc.bInterval == 0) {
702 pr_err("alt %d iso endp with 0 interval\n", j);
703 continue;
704 }
Jean-François Moine27b12022011-08-10 07:38:48 -0300705 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
Hans de Goede66957b82011-12-30 07:58:10 -0300706 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
Hans de Goeded0d34352011-12-30 08:20:50 -0300707 bandwidth = psize * 1000;
Jean-François Moine27b12022011-08-10 07:38:48 -0300708 if (gspca_dev->dev->speed == USB_SPEED_HIGH
709 || gspca_dev->dev->speed == USB_SPEED_SUPER)
710 bandwidth *= 8;
Hans de Goeded0d34352011-12-30 08:20:50 -0300711 bandwidth /= 1 << (ep->desc.bInterval - 1);
Jean-François Moine27b12022011-08-10 07:38:48 -0300712 if (bandwidth <= last_bw)
713 continue;
714 if (bandwidth < ep_tb->bandwidth) {
715 ep_tb->bandwidth = bandwidth;
716 ep_tb->alt = j;
717 found = 1;
718 }
Hans de Goedea511ba92009-10-16 07:13:07 -0300719 }
Jean-François Moine27b12022011-08-10 07:38:48 -0300720 if (!found)
721 break;
722 PDEBUG(D_STREAM, "alt %d bandwidth %d",
723 ep_tb->alt, ep_tb->bandwidth);
724 last_bw = ep_tb->bandwidth;
725 i++;
726 ep_tb++;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300727 }
Jean-François Moine27b12022011-08-10 07:38:48 -0300728
Hans de Goede2d390592011-12-30 18:26:53 -0300729 /*
730 * If the camera:
731 * has a usb audio class interface (a built in usb mic); and
732 * is a usb 1 full speed device; and
733 * uses the max full speed iso bandwidth; and
734 * and has more than 1 alt setting
735 * then skip the highest alt setting to spare bandwidth for the mic
736 */
737 if (gspca_dev->audio &&
738 gspca_dev->dev->speed == USB_SPEED_FULL &&
739 last_bw >= 1000000 &&
740 i > 1) {
741 PDEBUG(D_STREAM, "dev has usb audio, skipping highest alt");
742 i--;
743 ep_tb--;
744 }
745
Jean-François Moine27b12022011-08-10 07:38:48 -0300746 /* get the requested bandwidth and start at the highest atlsetting */
747 bandwidth = which_bandwidth(gspca_dev);
748 ep_tb--;
749 while (i > 1) {
750 ep_tb--;
751 if (ep_tb->bandwidth < bandwidth)
752 break;
753 i--;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300754 }
Jean-François Moine27b12022011-08-10 07:38:48 -0300755 return i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300756}
757
758/*
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300759 * create the URBs for image transfer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300760 */
761static int create_urbs(struct gspca_dev *gspca_dev,
762 struct usb_host_endpoint *ep)
763{
764 struct urb *urb;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300765 int n, nurbs, i, psize, npkt, bsize;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300766
767 /* calculate the packet size and the number of packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300768 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300769
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300770 if (!gspca_dev->cam.bulk) { /* isoc */
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300771
772 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300773 if (gspca_dev->pkt_size == 0)
774 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
775 else
776 psize = gspca_dev->pkt_size;
Jean-Francois Moine49cb6b02009-04-25 13:29:01 -0300777 npkt = gspca_dev->cam.npkt;
778 if (npkt == 0)
779 npkt = 32; /* default value */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300780 bsize = psize * npkt;
781 PDEBUG(D_STREAM,
782 "isoc %d pkts size %d = bsize:%d",
783 npkt, psize, bsize);
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300784 nurbs = DEF_NURBS;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300785 } else { /* bulk */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300786 npkt = 0;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300787 bsize = gspca_dev->cam.bulk_size;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300788 if (bsize == 0)
789 bsize = psize;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300790 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300791 if (gspca_dev->cam.bulk_nurbs != 0)
792 nurbs = gspca_dev->cam.bulk_nurbs;
793 else
794 nurbs = 1;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300795 }
796
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300797 for (n = 0; n < nurbs; n++) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300798 urb = usb_alloc_urb(npkt, GFP_KERNEL);
799 if (!urb) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300800 pr_err("usb_alloc_urb failed\n");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300801 return -ENOMEM;
802 }
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300803 gspca_dev->urb[n] = urb;
Daniel Mack997ea582010-04-12 13:17:25 +0200804 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300805 bsize,
806 GFP_KERNEL,
807 &urb->transfer_dma);
808
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300809 if (urb->transfer_buffer == NULL) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300810 pr_err("usb_alloc_coherent failed\n");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300811 return -ENOMEM;
812 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300813 urb->dev = gspca_dev->dev;
814 urb->context = gspca_dev;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300815 urb->transfer_buffer_length = bsize;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300816 if (npkt != 0) { /* ISOC */
817 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
818 ep->desc.bEndpointAddress);
Frank Zago6c49da72008-09-28 07:43:00 -0300819 urb->transfer_flags = URB_ISO_ASAP
820 | URB_NO_TRANSFER_DMA_MAP;
Jean-François Moine480992b2011-08-10 07:28:44 -0300821 if (gspca_dev->dev->speed == USB_SPEED_LOW)
822 urb->interval = ep->desc.bInterval;
823 else
824 urb->interval = 1 << (ep->desc.bInterval - 1);
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300825 urb->complete = isoc_irq;
826 urb->number_of_packets = npkt;
827 for (i = 0; i < npkt; i++) {
828 urb->iso_frame_desc[i].length = psize;
829 urb->iso_frame_desc[i].offset = psize * i;
830 }
831 } else { /* bulk */
832 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
Jean-François Moinea23c1942010-10-29 07:57:03 -0300833 ep->desc.bEndpointAddress);
Frank Zago6c49da72008-09-28 07:43:00 -0300834 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300835 urb->complete = bulk_irq;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300836 }
837 }
838 return 0;
839}
840
841/*
842 * start the USB transfer
843 */
844static int gspca_init_transfer(struct gspca_dev *gspca_dev)
845{
Jean-François Moine27b12022011-08-10 07:38:48 -0300846 struct usb_interface *intf;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300847 struct usb_host_endpoint *ep;
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300848 struct urb *urb;
Jean-François Moine27b12022011-08-10 07:38:48 -0300849 struct ep_tb_s ep_tb[MAX_ALT];
850 int n, ret, xfer, alt, alt_idx;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300851
Hans de Goedee2997a72008-04-23 08:09:12 -0300852 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
853 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300854
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300855 if (!gspca_dev->present) {
856 ret = -ENODEV;
Hans de Goede314b3e02010-10-26 05:15:46 -0300857 goto unlock;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300858 }
859
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300860 /* reset the streaming variables */
861 gspca_dev->image = NULL;
862 gspca_dev->image_len = 0;
863 gspca_dev->last_packet_type = DISCARD_PACKET;
864 gspca_dev->sequence = 0;
865
Jean-Francois Moineab6c8cf2010-01-13 15:34:27 -0300866 gspca_dev->usb_err = 0;
867
Jean-François Moine27b12022011-08-10 07:38:48 -0300868 /* do the specific subdriver stuff before endpoint selection */
Hans de Goede47faa6f2011-12-29 16:43:12 -0300869 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
870 gspca_dev->alt = gspca_dev->cam.bulk ? intf->num_altsetting : 0;
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300871 if (gspca_dev->sd_desc->isoc_init) {
872 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
873 if (ret < 0)
Hans de Goede314b3e02010-10-26 05:15:46 -0300874 goto unlock;
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300875 }
Jean-François Moine27b12022011-08-10 07:38:48 -0300876 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
877 : USB_ENDPOINT_XFER_ISOC;
Hans de Goede314b3e02010-10-26 05:15:46 -0300878
Hans de Goede66957b82011-12-30 07:58:10 -0300879 /* if bulk or the subdriver forced an altsetting, get the endpoint */
Jean-François Moine27b12022011-08-10 07:38:48 -0300880 if (gspca_dev->alt != 0) {
881 gspca_dev->alt--; /* (previous version compatibility) */
882 ep = alt_xfer(&intf->altsetting[gspca_dev->alt], xfer);
883 if (ep == NULL) {
884 pr_err("bad altsetting %d\n", gspca_dev->alt);
885 ret = -EIO;
886 goto out;
887 }
888 ep_tb[0].alt = gspca_dev->alt;
889 alt_idx = 1;
890 } else {
891
892 /* else, compute the minimum bandwidth
893 * and build the endpoint table */
Hans de Goede66957b82011-12-30 07:58:10 -0300894 alt_idx = build_isoc_ep_tb(gspca_dev, intf, ep_tb);
Jean-François Moine27b12022011-08-10 07:38:48 -0300895 if (alt_idx <= 0) {
896 pr_err("no transfer endpoint found\n");
897 ret = -EIO;
898 goto unlock;
899 }
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300900 }
Jean-François Moine27b12022011-08-10 07:38:48 -0300901
902 /* set the highest alternate setting and
903 * loop until urb submit succeeds */
904 gspca_input_destroy_urb(gspca_dev);
905
906 gspca_dev->alt = ep_tb[--alt_idx].alt;
907 alt = -1;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300908 for (;;) {
Jean-François Moine27b12022011-08-10 07:38:48 -0300909 if (alt != gspca_dev->alt) {
910 alt = gspca_dev->alt;
Jean-François Moine6121ca52011-11-30 05:54:16 -0300911 if (intf->num_altsetting > 1) {
Jean-François Moine27b12022011-08-10 07:38:48 -0300912 ret = usb_set_interface(gspca_dev->dev,
913 gspca_dev->iface,
914 alt);
915 if (ret < 0) {
Jean-François Moine4274d842011-08-10 07:40:47 -0300916 if (ret == -ENOSPC)
917 goto retry; /*fixme: ugly*/
Jean-François Moine27b12022011-08-10 07:38:48 -0300918 pr_err("set alt %d err %d\n", alt, ret);
919 goto out;
920 }
921 }
922 }
Jean-Francois Moine47aaca92009-12-15 05:23:04 -0300923 if (!gspca_dev->cam.no_urb_create) {
Jean-François Moine27b12022011-08-10 07:38:48 -0300924 PDEBUG(D_STREAM, "init transfer alt %d", alt);
925 ret = create_urbs(gspca_dev,
926 alt_xfer(&intf->altsetting[alt], xfer));
Jean-Francois Moine47aaca92009-12-15 05:23:04 -0300927 if (ret < 0) {
928 destroy_urbs(gspca_dev);
929 goto out;
930 }
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300931 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300932
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300933 /* clear the bulk endpoint */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300934 if (gspca_dev->cam.bulk)
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300935 usb_clear_halt(gspca_dev->dev,
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300936 gspca_dev->urb[0]->pipe);
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300937
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300938 /* start the cam */
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300939 ret = gspca_dev->sd_desc->start(gspca_dev);
940 if (ret < 0) {
941 destroy_urbs(gspca_dev);
942 goto out;
943 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300944 gspca_dev->streaming = 1;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300945
Antonio Ospitedff369a2008-11-14 07:53:32 -0300946 /* some bulk transfers are started by the subdriver */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300947 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300948 break;
949
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300950 /* submit the URBs */
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300951 for (n = 0; n < MAX_NURBS; n++) {
952 urb = gspca_dev->urb[n];
953 if (urb == NULL)
954 break;
955 ret = usb_submit_urb(urb, GFP_KERNEL);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300956 if (ret < 0)
957 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300958 }
959 if (ret >= 0)
Jean-François Moine27b12022011-08-10 07:38:48 -0300960 break; /* transfer is started */
961
962 /* something when wrong
963 * stop the webcam and free the transfer resources */
Jean-François Moinee4dac282010-04-09 06:11:36 -0300964 gspca_stream_off(gspca_dev);
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300965 if (ret != -ENOSPC) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300966 pr_err("usb_submit_urb alt %d err %d\n",
967 gspca_dev->alt, ret);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300968 goto out;
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300969 }
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300970
971 /* the bandwidth is not wide enough
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300972 * negotiate or try a lower alternate setting */
Jean-François Moine4274d842011-08-10 07:40:47 -0300973retry:
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300974 PDEBUG(D_ERR|D_STREAM,
Jean-François Moine27b12022011-08-10 07:38:48 -0300975 "alt %d - bandwidth not wide enough - trying again",
976 alt);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300977 msleep(20); /* wait for kill complete */
978 if (gspca_dev->sd_desc->isoc_nego) {
979 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
980 if (ret < 0)
981 goto out;
982 } else {
Jean-François Moine27b12022011-08-10 07:38:48 -0300983 if (alt_idx <= 0) {
984 pr_err("no transfer endpoint found\n");
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300985 ret = -EIO;
986 goto out;
987 }
Jean-François Moine27b12022011-08-10 07:38:48 -0300988 alt = ep_tb[--alt_idx].alt;
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300989 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300990 }
991out:
Hans de Goede314b3e02010-10-26 05:15:46 -0300992 gspca_input_create_urb(gspca_dev);
993unlock:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300994 mutex_unlock(&gspca_dev->usb_lock);
995 return ret;
996}
997
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300998static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300999{
Jean-François Moine6a330912010-10-02 04:12:25 -03001000 struct gspca_ctrl *ctrl;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001001 int i;
1002
1003 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
1004 gspca_dev->curr_mode = i;
1005 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
1006 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001007 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-François Moine6a330912010-10-02 04:12:25 -03001008
1009 /* set the current control values to their default values
1010 * which may have changed in sd_init() */
1011 ctrl = gspca_dev->cam.ctrls;
1012 if (ctrl != NULL) {
1013 for (i = 0;
1014 i < gspca_dev->sd_desc->nctrls;
1015 i++, ctrl++)
1016 ctrl->val = ctrl->def;
1017 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001018}
1019
1020static int wxh_to_mode(struct gspca_dev *gspca_dev,
1021 int width, int height)
1022{
1023 int i;
1024
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001025 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
1026 if (width >= gspca_dev->cam.cam_mode[i].width
1027 && height >= gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001028 break;
1029 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001030 return i;
1031}
1032
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001033/*
1034 * search a mode with the right pixel format
1035 */
1036static int gspca_get_mode(struct gspca_dev *gspca_dev,
1037 int mode,
1038 int pixfmt)
1039{
1040 int modeU, modeD;
1041
1042 modeU = modeD = mode;
1043 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
1044 if (--modeD >= 0) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001045 if (gspca_dev->cam.cam_mode[modeD].pixelformat
1046 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001047 return modeD;
1048 }
1049 if (++modeU < gspca_dev->cam.nmodes) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001050 if (gspca_dev->cam.cam_mode[modeU].pixelformat
1051 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001052 return modeU;
1053 }
1054 }
1055 return -EINVAL;
1056}
1057
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03001058#ifdef CONFIG_VIDEO_ADV_DEBUG
1059static int vidioc_g_register(struct file *file, void *priv,
1060 struct v4l2_dbg_register *reg)
1061{
1062 int ret;
1063 struct gspca_dev *gspca_dev = priv;
1064
1065 if (!gspca_dev->sd_desc->get_chip_ident)
1066 return -EINVAL;
1067
1068 if (!gspca_dev->sd_desc->get_register)
1069 return -EINVAL;
1070
1071 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1072 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001073 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03001074 if (gspca_dev->present)
1075 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
1076 else
1077 ret = -ENODEV;
1078 mutex_unlock(&gspca_dev->usb_lock);
1079
1080 return ret;
1081}
1082
1083static int vidioc_s_register(struct file *file, void *priv,
1084 struct v4l2_dbg_register *reg)
1085{
1086 int ret;
1087 struct gspca_dev *gspca_dev = priv;
1088
1089 if (!gspca_dev->sd_desc->get_chip_ident)
1090 return -EINVAL;
1091
1092 if (!gspca_dev->sd_desc->set_register)
1093 return -EINVAL;
1094
1095 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1096 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001097 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03001098 if (gspca_dev->present)
1099 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
1100 else
1101 ret = -ENODEV;
1102 mutex_unlock(&gspca_dev->usb_lock);
1103
1104 return ret;
1105}
1106#endif
1107
1108static int vidioc_g_chip_ident(struct file *file, void *priv,
1109 struct v4l2_dbg_chip_ident *chip)
1110{
1111 int ret;
1112 struct gspca_dev *gspca_dev = priv;
1113
1114 if (!gspca_dev->sd_desc->get_chip_ident)
1115 return -EINVAL;
1116
1117 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1118 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001119 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03001120 if (gspca_dev->present)
1121 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
1122 else
1123 ret = -ENODEV;
1124 mutex_unlock(&gspca_dev->usb_lock);
1125
1126 return ret;
1127}
1128
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001129static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001130 struct v4l2_fmtdesc *fmtdesc)
1131{
1132 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001133 int i, j, index;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001134 __u32 fmt_tb[8];
1135
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001136 /* give an index to each format */
1137 index = 0;
1138 j = 0;
1139 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001140 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001141 j = 0;
1142 for (;;) {
1143 if (fmt_tb[j] == fmt_tb[index])
1144 break;
1145 j++;
1146 }
1147 if (j == index) {
1148 if (fmtdesc->index == index)
1149 break; /* new format */
1150 index++;
Julia Lawall80297122008-11-12 23:18:21 -03001151 if (index >= ARRAY_SIZE(fmt_tb))
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001152 return -EINVAL;
1153 }
1154 }
1155 if (i < 0)
1156 return -EINVAL; /* no more format */
1157
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001158 fmtdesc->pixelformat = fmt_tb[index];
Jean-François Moineb796fb62011-08-10 07:32:57 -03001159 if (gspca_dev->cam.cam_mode[i].sizeimage <
1160 gspca_dev->cam.cam_mode[i].width *
1161 gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001162 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
1163 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
1164 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
1165 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
1166 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
1167 fmtdesc->description[4] = '\0';
1168 return 0;
1169}
1170
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001171static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001172 struct v4l2_format *fmt)
1173{
1174 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001175 int mode;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001176
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001177 mode = gspca_dev->curr_mode;
1178 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1179 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001180 return 0;
1181}
1182
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001183static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001184 struct v4l2_format *fmt)
1185{
Hans de Goede24c530b2008-07-04 17:55:18 -03001186 int w, h, mode, mode2;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001187
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001188 w = fmt->fmt.pix.width;
1189 h = fmt->fmt.pix.height;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001190
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001191#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001192 if (gspca_debug & D_CONF)
1193 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
1194#endif
1195 /* search the closest mode for width and height */
1196 mode = wxh_to_mode(gspca_dev, w, h);
1197
1198 /* OK if right palette */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001199 if (gspca_dev->cam.cam_mode[mode].pixelformat
1200 != fmt->fmt.pix.pixelformat) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001201
1202 /* else, search the closest mode with the same pixel format */
1203 mode2 = gspca_get_mode(gspca_dev, mode,
1204 fmt->fmt.pix.pixelformat);
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001205 if (mode2 >= 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001206 mode = mode2;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001207/* else
1208 ; * no chance, return this mode */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001209 }
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001210 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1211 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001212 return mode; /* used when s_fmt */
1213}
1214
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001215static int vidioc_try_fmt_vid_cap(struct file *file,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001216 void *priv,
1217 struct v4l2_format *fmt)
1218{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001219 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001220 int ret;
1221
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001222 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001223 if (ret < 0)
1224 return ret;
1225 return 0;
1226}
1227
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001228static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001229 struct v4l2_format *fmt)
1230{
1231 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001232 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001233
Hans de Goedee2997a72008-04-23 08:09:12 -03001234 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1235 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001236
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001237 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001238 if (ret < 0)
1239 goto out;
1240
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001241 if (gspca_dev->nframes != 0
1242 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
1243 ret = -EINVAL;
1244 goto out;
1245 }
1246
Jean-Francois Moine50a871f2008-06-30 19:47:33 -03001247 if (ret == gspca_dev->curr_mode) {
1248 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001249 goto out; /* same mode */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -03001250 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001251
1252 if (gspca_dev->streaming) {
1253 ret = -EBUSY;
1254 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -03001255 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001256 gspca_dev->width = fmt->fmt.pix.width;
1257 gspca_dev->height = fmt->fmt.pix.height;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001258 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
1259 gspca_dev->curr_mode = ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001260
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001261 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001262out:
1263 mutex_unlock(&gspca_dev->queue_lock);
1264 return ret;
1265}
1266
Hans de Goede11c635a2009-05-25 15:04:22 -03001267static int vidioc_enum_framesizes(struct file *file, void *priv,
1268 struct v4l2_frmsizeenum *fsize)
1269{
1270 struct gspca_dev *gspca_dev = priv;
1271 int i;
1272 __u32 index = 0;
1273
1274 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
1275 if (fsize->pixel_format !=
1276 gspca_dev->cam.cam_mode[i].pixelformat)
1277 continue;
1278
1279 if (fsize->index == index) {
1280 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1281 fsize->discrete.width =
1282 gspca_dev->cam.cam_mode[i].width;
1283 fsize->discrete.height =
1284 gspca_dev->cam.cam_mode[i].height;
1285 return 0;
1286 }
1287 index++;
1288 }
1289
1290 return -EINVAL;
1291}
1292
Antonio Ospite28ffe772009-12-02 06:18:46 -03001293static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1294 struct v4l2_frmivalenum *fival)
1295{
1296 struct gspca_dev *gspca_dev = priv;
1297 int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
1298 __u32 i;
1299
1300 if (gspca_dev->cam.mode_framerates == NULL ||
1301 gspca_dev->cam.mode_framerates[mode].nrates == 0)
1302 return -EINVAL;
1303
1304 if (fival->pixel_format !=
1305 gspca_dev->cam.cam_mode[mode].pixelformat)
1306 return -EINVAL;
1307
1308 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1309 if (fival->index == i) {
1310 fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1311 fival->discrete.numerator = 1;
1312 fival->discrete.denominator =
1313 gspca_dev->cam.mode_framerates[mode].rates[i];
1314 return 0;
1315 }
1316 }
1317
1318 return -EINVAL;
1319}
1320
Hans de Goeded0182302008-11-19 17:10:47 -03001321static void gspca_release(struct video_device *vfd)
Frank Zago6b060ff2008-09-28 08:12:22 -03001322{
Hans de Goeded0182302008-11-19 17:10:47 -03001323 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
Frank Zago6b060ff2008-09-28 08:12:22 -03001324
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001325 PDEBUG(D_PROBE, "%s released",
1326 video_device_node_name(&gspca_dev->vdev));
Frank Zago6b060ff2008-09-28 08:12:22 -03001327
1328 kfree(gspca_dev->usb_buf);
1329 kfree(gspca_dev);
1330}
1331
Hans Verkuilbec43662008-12-30 06:58:20 -03001332static int dev_open(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001333{
1334 struct gspca_dev *gspca_dev;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001335
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001336 PDEBUG(D_STREAM, "[%s] open", current->comm);
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001337 gspca_dev = (struct gspca_dev *) video_devdata(file);
Hans de Goede7f6eb112010-12-30 20:20:09 -03001338 if (!gspca_dev->present)
1339 return -ENODEV;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03001340
1341 /* protect the subdriver against rmmod */
Hans de Goede7f6eb112010-12-30 20:20:09 -03001342 if (!try_module_get(gspca_dev->module))
1343 return -ENODEV;
Frank Zago6b060ff2008-09-28 08:12:22 -03001344
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001345 file->private_data = gspca_dev;
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001346#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001347 /* activate the v4l2 debug */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001348 if (gspca_debug & D_V4L2)
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001349 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001350 | V4L2_DEBUG_IOCTL_ARG;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001351 else
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001352 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001353 | V4L2_DEBUG_IOCTL_ARG);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001354#endif
Hans de Goede7f6eb112010-12-30 20:20:09 -03001355 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001356}
1357
Hans Verkuilbec43662008-12-30 06:58:20 -03001358static int dev_close(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001359{
1360 struct gspca_dev *gspca_dev = file->private_data;
1361
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001362 PDEBUG(D_STREAM, "[%s] close", current->comm);
Hans de Goedee2997a72008-04-23 08:09:12 -03001363 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1364 return -ERESTARTSYS;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001365
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001366 /* if the file did the capture, free the streaming resources */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001367 if (gspca_dev->capt_file == file) {
Jean-Francois Moinea1e82882008-09-03 17:12:17 -03001368 if (gspca_dev->streaming) {
1369 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001370 gspca_dev->usb_err = 0;
Jean-Francois Moinea1e82882008-09-03 17:12:17 -03001371 gspca_stream_off(gspca_dev);
1372 mutex_unlock(&gspca_dev->usb_lock);
1373 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001374 frame_free(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001375 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001376 file->private_data = NULL;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03001377 module_put(gspca_dev->module);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001378 mutex_unlock(&gspca_dev->queue_lock);
Frank Zago6b060ff2008-09-28 08:12:22 -03001379
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001380 PDEBUG(D_STREAM, "close done");
Frank Zago6b060ff2008-09-28 08:12:22 -03001381
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001382 return 0;
1383}
1384
1385static int vidioc_querycap(struct file *file, void *priv,
1386 struct v4l2_capability *cap)
1387{
1388 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001389 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001390
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001391 /* protect the access to the usb device */
1392 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1393 return -ERESTARTSYS;
1394 if (!gspca_dev->present) {
1395 ret = -ENODEV;
1396 goto out;
1397 }
Hans de Goedee7d712c2011-06-05 14:58:46 -03001398 strlcpy((char *) cap->driver, gspca_dev->sd_desc->name,
Jean-François Moined8518592010-12-28 06:55:01 -03001399 sizeof cap->driver);
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -03001400 if (gspca_dev->dev->product != NULL) {
Hans de Goedee7d712c2011-06-05 14:58:46 -03001401 strlcpy((char *) cap->card, gspca_dev->dev->product,
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -03001402 sizeof cap->card);
1403 } else {
Jean-François Moined8518592010-12-28 06:55:01 -03001404 snprintf((char *) cap->card, sizeof cap->card,
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -03001405 "USB Camera (%04x:%04x)",
1406 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1407 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1408 }
Jean-François Moined8518592010-12-28 06:55:01 -03001409 usb_make_path(gspca_dev->dev, (char *) cap->bus_info,
1410 sizeof(cap->bus_info));
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001411 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1412 | V4L2_CAP_STREAMING
1413 | V4L2_CAP_READWRITE;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001414 ret = 0;
1415out:
1416 mutex_unlock(&gspca_dev->usb_lock);
1417 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001418}
1419
Jean-François Moine6a330912010-10-02 04:12:25 -03001420static int get_ctrl(struct gspca_dev *gspca_dev,
Hans de Goedea0001a22009-06-10 04:52:18 -03001421 int id)
1422{
1423 const struct ctrl *ctrls;
1424 int i;
1425
1426 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1427 i < gspca_dev->sd_desc->nctrls;
1428 i++, ctrls++) {
1429 if (gspca_dev->ctrl_dis & (1 << i))
1430 continue;
1431 if (id == ctrls->qctrl.id)
Jean-François Moine6a330912010-10-02 04:12:25 -03001432 return i;
Hans de Goedea0001a22009-06-10 04:52:18 -03001433 }
Jean-François Moine6a330912010-10-02 04:12:25 -03001434 return -1;
Hans de Goedea0001a22009-06-10 04:52:18 -03001435}
1436
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001437static int vidioc_queryctrl(struct file *file, void *priv,
1438 struct v4l2_queryctrl *q_ctrl)
1439{
1440 struct gspca_dev *gspca_dev = priv;
Hans de Goedea0001a22009-06-10 04:52:18 -03001441 const struct ctrl *ctrls;
Jean-François Moine6a330912010-10-02 04:12:25 -03001442 struct gspca_ctrl *gspca_ctrl;
1443 int i, idx;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001444 u32 id;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001445
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001446 id = q_ctrl->id;
1447 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1448 id &= V4L2_CTRL_ID_MASK;
1449 id++;
Jean-François Moine6a330912010-10-02 04:12:25 -03001450 idx = -1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001451 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001452 if (gspca_dev->ctrl_dis & (1 << i))
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001453 continue;
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001454 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001455 continue;
Jean-François Moine6a330912010-10-02 04:12:25 -03001456 if (idx >= 0
1457 && gspca_dev->sd_desc->ctrls[i].qctrl.id
Jean-François Moine780e3122010-10-19 04:29:10 -03001458 > gspca_dev->sd_desc->ctrls[idx].qctrl.id)
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001459 continue;
Jean-François Moine6a330912010-10-02 04:12:25 -03001460 idx = i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001461 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001462 } else {
Jean-François Moine6a330912010-10-02 04:12:25 -03001463 idx = get_ctrl(gspca_dev, id);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001464 }
Jean-François Moine6a330912010-10-02 04:12:25 -03001465 if (idx < 0)
1466 return -EINVAL;
1467 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1468 memcpy(q_ctrl, &ctrls->qctrl, sizeof *q_ctrl);
1469 if (gspca_dev->cam.ctrls != NULL) {
1470 gspca_ctrl = &gspca_dev->cam.ctrls[idx];
1471 q_ctrl->default_value = gspca_ctrl->def;
1472 q_ctrl->minimum = gspca_ctrl->min;
1473 q_ctrl->maximum = gspca_ctrl->max;
1474 }
1475 if (gspca_dev->ctrl_inac & (1 << idx))
Jean-Francois Moine4af85662009-11-12 15:59:27 -03001476 q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001477 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001478}
1479
1480static int vidioc_s_ctrl(struct file *file, void *priv,
1481 struct v4l2_control *ctrl)
1482{
1483 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001484 const struct ctrl *ctrls;
Jean-François Moine6a330912010-10-02 04:12:25 -03001485 struct gspca_ctrl *gspca_ctrl;
1486 int idx, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001487
Jean-François Moine6a330912010-10-02 04:12:25 -03001488 idx = get_ctrl(gspca_dev, ctrl->id);
1489 if (idx < 0)
Hans de Goedea0001a22009-06-10 04:52:18 -03001490 return -EINVAL;
Jean-François Moine6a330912010-10-02 04:12:25 -03001491 if (gspca_dev->ctrl_inac & (1 << idx))
1492 return -EINVAL;
1493 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1494 if (gspca_dev->cam.ctrls != NULL) {
1495 gspca_ctrl = &gspca_dev->cam.ctrls[idx];
1496 if (ctrl->value < gspca_ctrl->min
1497 || ctrl->value > gspca_ctrl->max)
1498 return -ERANGE;
1499 } else {
1500 gspca_ctrl = NULL;
1501 if (ctrl->value < ctrls->qctrl.minimum
1502 || ctrl->value > ctrls->qctrl.maximum)
1503 return -ERANGE;
1504 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001505 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1506 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1507 return -ERESTARTSYS;
Jean-François Moine6a330912010-10-02 04:12:25 -03001508 if (!gspca_dev->present) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001509 ret = -ENODEV;
Jean-François Moine6a330912010-10-02 04:12:25 -03001510 goto out;
1511 }
1512 gspca_dev->usb_err = 0;
1513 if (ctrls->set != NULL) {
1514 ret = ctrls->set(gspca_dev, ctrl->value);
1515 goto out;
1516 }
1517 if (gspca_ctrl != NULL) {
1518 gspca_ctrl->val = ctrl->value;
1519 if (ctrls->set_control != NULL
1520 && gspca_dev->streaming)
1521 ctrls->set_control(gspca_dev);
1522 }
1523 ret = gspca_dev->usb_err;
1524out:
Hans de Goedea0001a22009-06-10 04:52:18 -03001525 mutex_unlock(&gspca_dev->usb_lock);
1526 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001527}
1528
1529static int vidioc_g_ctrl(struct file *file, void *priv,
1530 struct v4l2_control *ctrl)
1531{
1532 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001533 const struct ctrl *ctrls;
Jean-François Moine6a330912010-10-02 04:12:25 -03001534 int idx, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001535
Jean-François Moine6a330912010-10-02 04:12:25 -03001536 idx = get_ctrl(gspca_dev, ctrl->id);
1537 if (idx < 0)
Hans de Goedea0001a22009-06-10 04:52:18 -03001538 return -EINVAL;
Jean-François Moine6a330912010-10-02 04:12:25 -03001539 ctrls = &gspca_dev->sd_desc->ctrls[idx];
Hans de Goedea0001a22009-06-10 04:52:18 -03001540
1541 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1542 return -ERESTARTSYS;
Jean-François Moine6a330912010-10-02 04:12:25 -03001543 if (!gspca_dev->present) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001544 ret = -ENODEV;
Jean-François Moine6a330912010-10-02 04:12:25 -03001545 goto out;
1546 }
1547 gspca_dev->usb_err = 0;
1548 if (ctrls->get != NULL) {
1549 ret = ctrls->get(gspca_dev, &ctrl->value);
1550 goto out;
1551 }
1552 if (gspca_dev->cam.ctrls != NULL)
1553 ctrl->value = gspca_dev->cam.ctrls[idx].val;
1554 ret = 0;
1555out:
Hans de Goedea0001a22009-06-10 04:52:18 -03001556 mutex_unlock(&gspca_dev->usb_lock);
1557 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001558}
1559
1560static int vidioc_querymenu(struct file *file, void *priv,
1561 struct v4l2_querymenu *qmenu)
1562{
1563 struct gspca_dev *gspca_dev = priv;
1564
1565 if (!gspca_dev->sd_desc->querymenu)
1566 return -EINVAL;
1567 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1568}
1569
1570static int vidioc_enum_input(struct file *file, void *priv,
1571 struct v4l2_input *input)
1572{
1573 struct gspca_dev *gspca_dev = priv;
1574
1575 if (input->index != 0)
1576 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001577 input->type = V4L2_INPUT_TYPE_CAMERA;
Adam Bakerdfa76fa2009-03-29 19:17:10 -03001578 input->status = gspca_dev->cam.input_flags;
Hans de Goedee7d712c2011-06-05 14:58:46 -03001579 strlcpy(input->name, gspca_dev->sd_desc->name,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001580 sizeof input->name);
1581 return 0;
1582}
1583
1584static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1585{
1586 *i = 0;
1587 return 0;
1588}
1589
1590static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1591{
1592 if (i > 0)
1593 return -EINVAL;
1594 return (0);
1595}
1596
1597static int vidioc_reqbufs(struct file *file, void *priv,
1598 struct v4l2_requestbuffers *rb)
1599{
1600 struct gspca_dev *gspca_dev = priv;
Jean-François Moine832d0a92010-04-09 06:07:58 -03001601 int i, ret = 0, streaming;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001602
Jean-François Moinea2ba6f22010-07-14 06:21:16 -03001603 i = rb->memory; /* (avoid compilation warning) */
1604 switch (i) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001605 case GSPCA_MEMORY_READ: /* (internal call) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001606 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001607 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001608 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001609 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001610 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001611 }
Hans de Goedee2997a72008-04-23 08:09:12 -03001612 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1613 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001614
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001615 if (gspca_dev->memory != GSPCA_MEMORY_NO
Hans de Goedece5610b2010-12-31 07:41:54 -03001616 && gspca_dev->memory != GSPCA_MEMORY_READ
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001617 && gspca_dev->memory != rb->memory) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001618 ret = -EBUSY;
1619 goto out;
1620 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001621
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001622 /* only one file may do the capture */
1623 if (gspca_dev->capt_file != NULL
1624 && gspca_dev->capt_file != file) {
1625 ret = -EBUSY;
1626 goto out;
1627 }
1628
1629 /* if allocated, the buffers must not be mapped */
1630 for (i = 0; i < gspca_dev->nframes; i++) {
1631 if (gspca_dev->frame[i].vma_use_count) {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001632 ret = -EBUSY;
1633 goto out;
1634 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001635 }
1636
1637 /* stop streaming */
Jean-François Moine832d0a92010-04-09 06:07:58 -03001638 streaming = gspca_dev->streaming;
1639 if (streaming) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001640 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001641 gspca_dev->usb_err = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001642 gspca_stream_off(gspca_dev);
1643 mutex_unlock(&gspca_dev->usb_lock);
Jean-François Moine59929ce2011-03-14 05:33:01 -03001644
1645 /* Don't restart the stream when switching from read
1646 * to mmap mode */
1647 if (gspca_dev->memory == GSPCA_MEMORY_READ)
1648 streaming = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001649 }
1650
1651 /* free the previous allocated buffers, if any */
Hans de Goeded642de22010-12-31 04:51:36 -03001652 if (gspca_dev->nframes != 0)
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001653 frame_free(gspca_dev);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001654 if (rb->count == 0) /* unrequest */
1655 goto out;
Hans de Goeded642de22010-12-31 04:51:36 -03001656 ret = frame_alloc(gspca_dev, file, rb->memory, rb->count);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001657 if (ret == 0) {
1658 rb->count = gspca_dev->nframes;
Jean-François Moine832d0a92010-04-09 06:07:58 -03001659 if (streaming)
1660 ret = gspca_init_transfer(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001661 }
1662out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001663 mutex_unlock(&gspca_dev->queue_lock);
1664 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1665 return ret;
1666}
1667
1668static int vidioc_querybuf(struct file *file, void *priv,
1669 struct v4l2_buffer *v4l2_buf)
1670{
1671 struct gspca_dev *gspca_dev = priv;
1672 struct gspca_frame *frame;
1673
Jean-Francois Moine13752bd2008-12-19 15:08:51 -03001674 if (v4l2_buf->index < 0
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001675 || v4l2_buf->index >= gspca_dev->nframes)
1676 return -EINVAL;
1677
1678 frame = &gspca_dev->frame[v4l2_buf->index];
1679 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1680 return 0;
1681}
1682
1683static int vidioc_streamon(struct file *file, void *priv,
1684 enum v4l2_buf_type buf_type)
1685{
1686 struct gspca_dev *gspca_dev = priv;
1687 int ret;
1688
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001689 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1690 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001691 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1692 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001693
Jean-François Moine18bf99a2010-04-09 06:06:11 -03001694 /* check the capture file */
1695 if (gspca_dev->capt_file != file) {
1696 ret = -EBUSY;
1697 goto out;
1698 }
1699
Jean-Francois Moine1edabe72008-12-19 15:17:25 -03001700 if (gspca_dev->nframes == 0
1701 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001702 ret = -EINVAL;
1703 goto out;
1704 }
1705 if (!gspca_dev->streaming) {
1706 ret = gspca_init_transfer(gspca_dev);
1707 if (ret < 0)
1708 goto out;
1709 }
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001710#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001711 if (gspca_debug & D_STREAM) {
1712 PDEBUG_MODE("stream on OK",
1713 gspca_dev->pixfmt,
1714 gspca_dev->width,
1715 gspca_dev->height);
1716 }
1717#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001718 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001719out:
1720 mutex_unlock(&gspca_dev->queue_lock);
1721 return ret;
1722}
1723
1724static int vidioc_streamoff(struct file *file, void *priv,
1725 enum v4l2_buf_type buf_type)
1726{
1727 struct gspca_dev *gspca_dev = priv;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001728 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001729
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001730 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1731 return -EINVAL;
Hans de Goede4e770f72010-12-30 13:11:21 -03001732
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001733 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1734 return -ERESTARTSYS;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001735
Hans de Goede4e770f72010-12-30 13:11:21 -03001736 if (!gspca_dev->streaming) {
1737 ret = 0;
1738 goto out;
1739 }
1740
Jean-François Moine18bf99a2010-04-09 06:06:11 -03001741 /* check the capture file */
1742 if (gspca_dev->capt_file != file) {
1743 ret = -EBUSY;
1744 goto out;
1745 }
1746
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001747 /* stop streaming */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001748 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1749 ret = -ERESTARTSYS;
1750 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001751 }
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001752 gspca_dev->usb_err = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001753 gspca_stream_off(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001754 mutex_unlock(&gspca_dev->usb_lock);
Hans de Goede0d0ae152010-12-31 17:17:51 -03001755 /* In case another thread is waiting in dqbuf */
1756 wake_up_interruptible(&gspca_dev->wq);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001757
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001758 /* empty the transfer queues */
1759 atomic_set(&gspca_dev->fr_q, 0);
1760 atomic_set(&gspca_dev->fr_i, 0);
1761 gspca_dev->fr_o = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001762 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001763out:
1764 mutex_unlock(&gspca_dev->queue_lock);
1765 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001766}
1767
1768static int vidioc_g_jpegcomp(struct file *file, void *priv,
1769 struct v4l2_jpegcompression *jpegcomp)
1770{
1771 struct gspca_dev *gspca_dev = priv;
1772 int ret;
1773
1774 if (!gspca_dev->sd_desc->get_jcomp)
1775 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001776 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1777 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001778 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001779 if (gspca_dev->present)
1780 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1781 else
1782 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001783 mutex_unlock(&gspca_dev->usb_lock);
1784 return ret;
1785}
1786
1787static int vidioc_s_jpegcomp(struct file *file, void *priv,
1788 struct v4l2_jpegcompression *jpegcomp)
1789{
1790 struct gspca_dev *gspca_dev = priv;
1791 int ret;
1792
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001793 if (!gspca_dev->sd_desc->set_jcomp)
1794 return -EINVAL;
Jim Parisf86d4a92008-12-10 05:50:44 -03001795 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1796 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001797 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001798 if (gspca_dev->present)
1799 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1800 else
1801 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001802 mutex_unlock(&gspca_dev->usb_lock);
1803 return ret;
1804}
1805
1806static int vidioc_g_parm(struct file *filp, void *priv,
1807 struct v4l2_streamparm *parm)
1808{
1809 struct gspca_dev *gspca_dev = priv;
1810
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001811 parm->parm.capture.readbuffers = gspca_dev->nbufread;
Jim Paris627a5ef2008-12-10 06:02:42 -03001812
1813 if (gspca_dev->sd_desc->get_streamparm) {
1814 int ret;
1815
1816 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1817 return -ERESTARTSYS;
Jean-François Moine668f44a2010-12-25 13:46:14 -03001818 if (gspca_dev->present) {
1819 gspca_dev->usb_err = 0;
1820 gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
1821 ret = gspca_dev->usb_err;
1822 } else {
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001823 ret = -ENODEV;
Jean-François Moine668f44a2010-12-25 13:46:14 -03001824 }
Jim Paris627a5ef2008-12-10 06:02:42 -03001825 mutex_unlock(&gspca_dev->usb_lock);
1826 return ret;
1827 }
1828
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001829 return 0;
1830}
1831
1832static int vidioc_s_parm(struct file *filp, void *priv,
1833 struct v4l2_streamparm *parm)
1834{
1835 struct gspca_dev *gspca_dev = priv;
1836 int n;
1837
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001838 n = parm->parm.capture.readbuffers;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001839 if (n == 0 || n >= GSPCA_MAX_FRAMES)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001840 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1841 else
1842 gspca_dev->nbufread = n;
Jim Paris627a5ef2008-12-10 06:02:42 -03001843
1844 if (gspca_dev->sd_desc->set_streamparm) {
1845 int ret;
1846
1847 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1848 return -ERESTARTSYS;
Jean-François Moine668f44a2010-12-25 13:46:14 -03001849 if (gspca_dev->present) {
1850 gspca_dev->usb_err = 0;
1851 gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
1852 ret = gspca_dev->usb_err;
1853 } else {
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001854 ret = -ENODEV;
Jean-François Moine668f44a2010-12-25 13:46:14 -03001855 }
Jim Paris627a5ef2008-12-10 06:02:42 -03001856 mutex_unlock(&gspca_dev->usb_lock);
1857 return ret;
1858 }
1859
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001860 return 0;
1861}
1862
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001863static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1864{
1865 struct gspca_dev *gspca_dev = file->private_data;
Harvey Harrisonefab8212008-07-05 06:12:47 -03001866 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001867 struct page *page;
1868 unsigned long addr, start, size;
1869 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001870
1871 start = vma->vm_start;
1872 size = vma->vm_end - vma->vm_start;
1873 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1874
Hans de Goedee2997a72008-04-23 08:09:12 -03001875 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1876 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001877 if (!gspca_dev->present) {
1878 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001879 goto out;
1880 }
1881 if (gspca_dev->capt_file != file) {
1882 ret = -EINVAL;
1883 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001884 }
1885
Harvey Harrisonefab8212008-07-05 06:12:47 -03001886 frame = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001887 for (i = 0; i < gspca_dev->nframes; ++i) {
1888 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1889 PDEBUG(D_STREAM, "mmap bad memory type");
1890 break;
1891 }
1892 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1893 == vma->vm_pgoff) {
1894 frame = &gspca_dev->frame[i];
1895 break;
1896 }
1897 }
Harvey Harrisonefab8212008-07-05 06:12:47 -03001898 if (frame == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001899 PDEBUG(D_STREAM, "mmap no frame buffer found");
1900 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001901 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001902 }
Jean-François Moine38bff692010-06-24 05:19:58 -03001903 if (size != frame->v4l2_buf.length) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001904 PDEBUG(D_STREAM, "mmap bad size");
1905 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001906 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001907 }
1908
1909 /*
1910 * - VM_IO marks the area as being a mmaped region for I/O to a
1911 * device. It also prevents the region from being core dumped.
1912 */
1913 vma->vm_flags |= VM_IO;
1914
1915 addr = (unsigned long) frame->data;
1916 while (size > 0) {
1917 page = vmalloc_to_page((void *) addr);
1918 ret = vm_insert_page(vma, start, page);
1919 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001920 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001921 start += PAGE_SIZE;
1922 addr += PAGE_SIZE;
1923 size -= PAGE_SIZE;
1924 }
1925
Jean-Francois Moined50e2572009-11-26 15:36:40 -03001926 vma->vm_ops = &gspca_vm_ops;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001927 vma->vm_private_data = frame;
1928 gspca_vm_open(vma);
Hans de Goedee2997a72008-04-23 08:09:12 -03001929 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001930out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001931 mutex_unlock(&gspca_dev->queue_lock);
1932 return ret;
1933}
1934
Hans de Goede27074ef2010-12-30 19:54:33 -03001935static int frame_ready_nolock(struct gspca_dev *gspca_dev, struct file *file,
1936 enum v4l2_memory memory)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001937{
Hans de Goede27074ef2010-12-30 19:54:33 -03001938 if (!gspca_dev->present)
1939 return -ENODEV;
1940 if (gspca_dev->capt_file != file || gspca_dev->memory != memory ||
1941 !gspca_dev->streaming)
1942 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001943
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001944 /* check if a frame is ready */
Hans de Goede27074ef2010-12-30 19:54:33 -03001945 return gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i);
1946}
Hans de Goede181b7042008-11-19 17:16:26 -03001947
Hans de Goede27074ef2010-12-30 19:54:33 -03001948static int frame_ready(struct gspca_dev *gspca_dev, struct file *file,
1949 enum v4l2_memory memory)
1950{
1951 int ret;
Hans de Goede181b7042008-11-19 17:16:26 -03001952
Hans de Goede27074ef2010-12-30 19:54:33 -03001953 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1954 return -ERESTARTSYS;
1955 ret = frame_ready_nolock(gspca_dev, file, memory);
1956 mutex_unlock(&gspca_dev->queue_lock);
1957 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001958}
1959
1960/*
1961 * dequeue a video buffer
1962 *
1963 * If nonblock_ing is false, block until a buffer is available.
1964 */
1965static int vidioc_dqbuf(struct file *file, void *priv,
1966 struct v4l2_buffer *v4l2_buf)
1967{
1968 struct gspca_dev *gspca_dev = priv;
1969 struct gspca_frame *frame;
Hans de Goede27074ef2010-12-30 19:54:33 -03001970 int i, j, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001971
1972 PDEBUG(D_FRAM, "dqbuf");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001973
Hans de Goede27074ef2010-12-30 19:54:33 -03001974 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001975 return -ERESTARTSYS;
1976
Hans de Goede27074ef2010-12-30 19:54:33 -03001977 for (;;) {
1978 ret = frame_ready_nolock(gspca_dev, file, v4l2_buf->memory);
1979 if (ret < 0)
1980 goto out;
1981 if (ret > 0)
1982 break;
1983
1984 mutex_unlock(&gspca_dev->queue_lock);
1985
1986 if (file->f_flags & O_NONBLOCK)
1987 return -EAGAIN;
1988
1989 /* wait till a frame is ready */
1990 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1991 frame_ready(gspca_dev, file, v4l2_buf->memory),
1992 msecs_to_jiffies(3000));
1993 if (ret < 0)
1994 return ret;
1995 if (ret == 0)
1996 return -EIO;
1997
1998 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1999 return -ERESTARTSYS;
2000 }
2001
2002 i = gspca_dev->fr_o;
2003 j = gspca_dev->fr_queue[i];
2004 frame = &gspca_dev->frame[j];
2005
2006 gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES;
2007
2008 if (gspca_dev->sd_desc->dq_callback) {
2009 mutex_lock(&gspca_dev->usb_lock);
2010 gspca_dev->usb_err = 0;
2011 if (gspca_dev->present)
2012 gspca_dev->sd_desc->dq_callback(gspca_dev);
2013 mutex_unlock(&gspca_dev->usb_lock);
2014 }
2015
Hans de Goede4a82bc62010-12-30 20:00:17 -03002016 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
2017 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
2018 PDEBUG(D_FRAM, "dqbuf %d", j);
2019 ret = 0;
2020
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002021 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002022 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002023 frame->data,
2024 frame->v4l2_buf.bytesused)) {
2025 PDEBUG(D_ERR|D_STREAM,
2026 "dqbuf cp to user failed");
2027 ret = -EFAULT;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002028 }
2029 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002030out:
Hans de Goede27074ef2010-12-30 19:54:33 -03002031 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002032 return ret;
2033}
2034
2035/*
2036 * queue a video buffer
2037 *
2038 * Attempting to queue a buffer that has already been
2039 * queued will return -EINVAL.
2040 */
2041static int vidioc_qbuf(struct file *file, void *priv,
2042 struct v4l2_buffer *v4l2_buf)
2043{
2044 struct gspca_dev *gspca_dev = priv;
2045 struct gspca_frame *frame;
2046 int i, index, ret;
2047
2048 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002049
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002050 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
2051 return -ERESTARTSYS;
2052
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002053 index = v4l2_buf->index;
2054 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002055 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002056 "qbuf idx %d >= %d", index, gspca_dev->nframes);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002057 ret = -EINVAL;
2058 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002059 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002060 if (v4l2_buf->memory != gspca_dev->memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002061 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002062 ret = -EINVAL;
2063 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002064 }
2065
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002066 frame = &gspca_dev->frame[index];
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002067 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
2068 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002069 ret = -EINVAL;
2070 goto out;
2071 }
2072
2073 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002074
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002075 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002076 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
2077 frame->v4l2_buf.length = v4l2_buf->length;
2078 }
2079
2080 /* put the buffer in the 'queued' queue */
Jean-François Moinef7059ea2010-07-06 04:32:27 -03002081 i = atomic_read(&gspca_dev->fr_q);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002082 gspca_dev->fr_queue[i] = index;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03002083 atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002084
2085 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
2086 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
2087 ret = 0;
2088out:
2089 mutex_unlock(&gspca_dev->queue_lock);
2090 return ret;
2091}
2092
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002093/*
2094 * allocate the resources for read()
2095 */
2096static int read_alloc(struct gspca_dev *gspca_dev,
2097 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002098{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002099 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002100 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002101
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002102 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002103 if (gspca_dev->nframes == 0) {
2104 struct v4l2_requestbuffers rb;
2105
2106 memset(&rb, 0, sizeof rb);
2107 rb.count = gspca_dev->nbufread;
2108 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002109 rb.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002110 ret = vidioc_reqbufs(file, gspca_dev, &rb);
2111 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002112 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002113 return ret;
2114 }
2115 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2116 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002117 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002118 for (i = 0; i < gspca_dev->nbufread; i++) {
2119 v4l2_buf.index = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002120 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2121 if (ret != 0) {
2122 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
2123 return ret;
2124 }
2125 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002126 gspca_dev->memory = GSPCA_MEMORY_READ;
2127 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002128
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002129 /* start streaming */
2130 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2131 if (ret != 0)
2132 PDEBUG(D_STREAM, "read streamon err %d", ret);
2133 return ret;
2134}
2135
2136static unsigned int dev_poll(struct file *file, poll_table *wait)
2137{
2138 struct gspca_dev *gspca_dev = file->private_data;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03002139 int ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002140
2141 PDEBUG(D_FRAM, "poll");
2142
2143 poll_wait(file, &gspca_dev->wq, wait);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002144
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002145 /* if reqbufs is not done, the user would use read() */
Hans de Goedeee362992010-12-31 05:05:56 -03002146 if (gspca_dev->memory == GSPCA_MEMORY_NO) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002147 ret = read_alloc(gspca_dev, file);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002148 if (ret != 0)
2149 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002150 }
2151
2152 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
2153 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002154
Jean-François Moinef7059ea2010-07-06 04:32:27 -03002155 /* check if an image has been received */
2156 if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i))
2157 ret = POLLIN | POLLRDNORM; /* yes */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002158 else
2159 ret = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002160 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002161 if (!gspca_dev->present)
2162 return POLLHUP;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002163 return ret;
2164}
2165
2166static ssize_t dev_read(struct file *file, char __user *data,
2167 size_t count, loff_t *ppos)
2168{
2169 struct gspca_dev *gspca_dev = file->private_data;
2170 struct gspca_frame *frame;
2171 struct v4l2_buffer v4l2_buf;
2172 struct timeval timestamp;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002173 int n, ret, ret2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002174
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03002175 PDEBUG(D_FRAM, "read (%zd)", count);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002176 if (!gspca_dev->present)
2177 return -ENODEV;
Hans de Goedeee362992010-12-31 05:05:56 -03002178 if (gspca_dev->memory == GSPCA_MEMORY_NO) { /* first time ? */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002179 ret = read_alloc(gspca_dev, file);
2180 if (ret != 0)
2181 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002182 }
2183
2184 /* get a frame */
Paulo Assis2f51d002010-03-07 06:56:56 -03002185 timestamp = ktime_to_timeval(ktime_get());
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002186 timestamp.tv_sec--;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002187 n = 2;
2188 for (;;) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002189 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2190 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002191 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002192 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
2193 if (ret != 0) {
2194 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
2195 return ret;
2196 }
2197
2198 /* if the process slept for more than 1 second,
Frank Zago219423c2008-09-26 07:43:54 -03002199 * get a newer frame */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002200 frame = &gspca_dev->frame[v4l2_buf.index];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002201 if (--n < 0)
2202 break; /* avoid infinite loop */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002203 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
2204 break;
2205 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2206 if (ret != 0) {
2207 PDEBUG(D_STREAM, "read qbuf err %d", ret);
2208 return ret;
2209 }
2210 }
2211
2212 /* copy the frame */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002213 if (count > frame->v4l2_buf.bytesused)
2214 count = frame->v4l2_buf.bytesused;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002215 ret = copy_to_user(data, frame->data, count);
2216 if (ret != 0) {
2217 PDEBUG(D_ERR|D_STREAM,
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03002218 "read cp to user lack %d / %zd", ret, count);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002219 ret = -EFAULT;
2220 goto out;
2221 }
2222 ret = count;
2223out:
2224 /* in each case, requeue the buffer */
2225 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2226 if (ret2 != 0)
2227 return ret2;
2228 return ret;
2229}
2230
Hans Verkuilbec43662008-12-30 06:58:20 -03002231static struct v4l2_file_operations dev_fops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002232 .owner = THIS_MODULE,
2233 .open = dev_open,
2234 .release = dev_close,
2235 .read = dev_read,
2236 .mmap = dev_mmap,
Hans Verkuilbec43662008-12-30 06:58:20 -03002237 .unlocked_ioctl = video_ioctl2,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002238 .poll = dev_poll,
2239};
2240
Hans Verkuila3998102008-07-21 02:57:38 -03002241static const struct v4l2_ioctl_ops dev_ioctl_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002242 .vidioc_querycap = vidioc_querycap,
2243 .vidioc_dqbuf = vidioc_dqbuf,
2244 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03002245 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2246 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2247 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2248 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002249 .vidioc_streamon = vidioc_streamon,
2250 .vidioc_queryctrl = vidioc_queryctrl,
2251 .vidioc_g_ctrl = vidioc_g_ctrl,
2252 .vidioc_s_ctrl = vidioc_s_ctrl,
2253 .vidioc_querymenu = vidioc_querymenu,
2254 .vidioc_enum_input = vidioc_enum_input,
2255 .vidioc_g_input = vidioc_g_input,
2256 .vidioc_s_input = vidioc_s_input,
2257 .vidioc_reqbufs = vidioc_reqbufs,
2258 .vidioc_querybuf = vidioc_querybuf,
2259 .vidioc_streamoff = vidioc_streamoff,
2260 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
2261 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
2262 .vidioc_g_parm = vidioc_g_parm,
2263 .vidioc_s_parm = vidioc_s_parm,
Hans de Goede11c635a2009-05-25 15:04:22 -03002264 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Antonio Ospite28ffe772009-12-02 06:18:46 -03002265 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03002266#ifdef CONFIG_VIDEO_ADV_DEBUG
2267 .vidioc_g_register = vidioc_g_register,
2268 .vidioc_s_register = vidioc_s_register,
2269#endif
2270 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002271};
2272
Jean-François Moine59929ce2011-03-14 05:33:01 -03002273static const struct video_device gspca_template = {
Hans Verkuila3998102008-07-21 02:57:38 -03002274 .name = "gspca main driver",
Hans Verkuila3998102008-07-21 02:57:38 -03002275 .fops = &dev_fops,
2276 .ioctl_ops = &dev_ioctl_ops,
Hans de Goeded0182302008-11-19 17:10:47 -03002277 .release = gspca_release,
Hans Verkuila3998102008-07-21 02:57:38 -03002278};
2279
Jean-François Moine6a330912010-10-02 04:12:25 -03002280/* initialize the controls */
2281static void ctrls_init(struct gspca_dev *gspca_dev)
2282{
2283 struct gspca_ctrl *ctrl;
2284 int i;
2285
2286 for (i = 0, ctrl = gspca_dev->cam.ctrls;
2287 i < gspca_dev->sd_desc->nctrls;
2288 i++, ctrl++) {
2289 ctrl->def = gspca_dev->sd_desc->ctrls[i].qctrl.default_value;
2290 ctrl->val = ctrl->def;
2291 ctrl->min = gspca_dev->sd_desc->ctrls[i].qctrl.minimum;
2292 ctrl->max = gspca_dev->sd_desc->ctrls[i].qctrl.maximum;
2293 }
2294}
2295
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002296/*
2297 * probe and create a new gspca device
2298 *
2299 * This function must be called by the sub-driver when it is
2300 * called for probing a new device.
2301 */
Jean-François Moine44628642010-06-05 07:47:36 -03002302int gspca_dev_probe2(struct usb_interface *intf,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002303 const struct usb_device_id *id,
2304 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002305 int dev_size,
2306 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002307{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002308 struct gspca_dev *gspca_dev;
2309 struct usb_device *dev = interface_to_usbdev(intf);
2310 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002311
Jean-François Moineee7dd4e2011-09-23 05:25:28 -03002312 pr_info("%s-" GSPCA_VERSION " probing %04x:%04x\n",
2313 sd_desc->name, id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002314
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002315 /* create the device */
2316 if (dev_size < sizeof *gspca_dev)
2317 dev_size = sizeof *gspca_dev;
2318 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
Erik Andrene293e592008-10-03 08:46:50 -03002319 if (!gspca_dev) {
Joe Perches133a9fe2011-08-21 19:56:57 -03002320 pr_err("couldn't kzalloc gspca struct\n");
Erik Andrene293e592008-10-03 08:46:50 -03002321 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002322 }
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002323 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2324 if (!gspca_dev->usb_buf) {
Joe Perches133a9fe2011-08-21 19:56:57 -03002325 pr_err("out of memory\n");
Erik Andrene293e592008-10-03 08:46:50 -03002326 ret = -ENOMEM;
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002327 goto out;
2328 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002329 gspca_dev->dev = dev;
Jean-François Moine44628642010-06-05 07:47:36 -03002330 gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
Jean-François Moine35680ba2010-07-14 06:30:18 -03002331
2332 /* check if any audio device */
Hans de Goede5dae6032012-01-01 17:20:14 -03002333 if (dev->actconfig->desc.bNumInterfaces != 1) {
Jean-François Moine35680ba2010-07-14 06:30:18 -03002334 int i;
2335 struct usb_interface *intf2;
2336
Hans de Goede5dae6032012-01-01 17:20:14 -03002337 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
2338 intf2 = dev->actconfig->interface[i];
Jean-François Moine35680ba2010-07-14 06:30:18 -03002339 if (intf2 != NULL
2340 && intf2->altsetting != NULL
2341 && intf2->altsetting->desc.bInterfaceClass ==
2342 USB_CLASS_AUDIO) {
2343 gspca_dev->audio = 1;
2344 break;
2345 }
2346 }
2347 }
2348
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002349 gspca_dev->sd_desc = sd_desc;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002350 gspca_dev->nbufread = 2;
Jean-Francois Moineff374742008-10-23 07:29:51 -03002351 gspca_dev->empty_packet = -1; /* don't check the empty packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002352
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002353 /* configure the subdriver and initialize the USB device */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002354 ret = sd_desc->config(gspca_dev, id);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002355 if (ret < 0)
2356 goto out;
Jean-François Moine6a330912010-10-02 04:12:25 -03002357 if (gspca_dev->cam.ctrls != NULL)
2358 ctrls_init(gspca_dev);
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002359 ret = sd_desc->init(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002360 if (ret < 0)
2361 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002362 gspca_set_default_mode(gspca_dev);
2363
Hans de Goede10ee2402010-02-19 04:41:40 -03002364 ret = gspca_input_connect(gspca_dev);
2365 if (ret)
2366 goto out;
2367
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002368 mutex_init(&gspca_dev->usb_lock);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002369 mutex_init(&gspca_dev->queue_lock);
2370 init_waitqueue_head(&gspca_dev->wq);
2371
2372 /* init video stuff */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002373 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
Hans de Goede24441632009-03-27 16:08:20 -03002374 gspca_dev->vdev.parent = &intf->dev;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03002375 gspca_dev->module = module;
Oliver Neukumee56a4d2008-07-27 14:01:59 -03002376 gspca_dev->present = 1;
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002377 ret = video_register_device(&gspca_dev->vdev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002378 VFL_TYPE_GRABBER,
Jean-Francois Moine8d584a52009-01-13 06:07:59 -03002379 -1);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002380 if (ret < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -03002381 pr_err("video_register_device err %d\n", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002382 goto out;
2383 }
2384
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002385 usb_set_intfdata(intf, gspca_dev);
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002386 PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
Márton Németh0274d422010-01-28 06:39:49 -03002387
Hans de Goede10ee2402010-02-19 04:41:40 -03002388 gspca_input_create_urb(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002389
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002390 return 0;
2391out:
Randy Dunlapf7628012010-03-30 14:32:04 -03002392#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Hans de Goede10ee2402010-02-19 04:41:40 -03002393 if (gspca_dev->input_dev)
2394 input_unregister_device(gspca_dev->input_dev);
Hans de Goedee7637522010-02-19 05:11:09 -03002395#endif
Jean-Francois Moinee07a1d82008-11-19 06:37:53 -03002396 kfree(gspca_dev->usb_buf);
2397 kfree(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002398 return ret;
2399}
Jean-François Moine44628642010-06-05 07:47:36 -03002400EXPORT_SYMBOL(gspca_dev_probe2);
2401
2402/* same function as the previous one, but check the interface */
2403int gspca_dev_probe(struct usb_interface *intf,
2404 const struct usb_device_id *id,
2405 const struct sd_desc *sd_desc,
2406 int dev_size,
2407 struct module *module)
2408{
2409 struct usb_device *dev = interface_to_usbdev(intf);
2410
2411 /* we don't handle multi-config cameras */
2412 if (dev->descriptor.bNumConfigurations != 1) {
Joe Perches133a9fe2011-08-21 19:56:57 -03002413 pr_err("%04x:%04x too many config\n",
2414 id->idVendor, id->idProduct);
Jean-François Moine44628642010-06-05 07:47:36 -03002415 return -ENODEV;
2416 }
2417
2418 /* the USB video interface must be the first one */
Hans de Goede5dae6032012-01-01 17:20:14 -03002419 if (dev->actconfig->desc.bNumInterfaces != 1
Jean-François Moinec4dc6922010-07-14 06:26:54 -03002420 && intf->cur_altsetting->desc.bInterfaceNumber != 0)
Jean-François Moine44628642010-06-05 07:47:36 -03002421 return -ENODEV;
Jean-François Moine44628642010-06-05 07:47:36 -03002422
2423 return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
2424}
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002425EXPORT_SYMBOL(gspca_dev_probe);
2426
2427/*
2428 * USB disconnection
2429 *
2430 * This function must be called by the sub-driver
2431 * when the device disconnects, after the specific resources are freed.
2432 */
2433void gspca_disconnect(struct usb_interface *intf)
2434{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002435 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Randy Dunlapf7628012010-03-30 14:32:04 -03002436#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -03002437 struct input_dev *input_dev;
Jean-François Moinee070bf92010-02-18 15:02:51 -03002438#endif
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002439
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002440 PDEBUG(D_PROBE, "%s disconnect",
2441 video_device_node_name(&gspca_dev->vdev));
Adam Bakerad281272009-02-04 15:33:21 -03002442 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002443
Hans de Goede4e770f72010-12-30 13:11:21 -03002444 gspca_dev->present = 0;
2445 wake_up_interruptible(&gspca_dev->wq);
2446
2447 destroy_urbs(gspca_dev);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002448
Randy Dunlapf7628012010-03-30 14:32:04 -03002449#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -03002450 gspca_input_destroy_urb(gspca_dev);
2451 input_dev = gspca_dev->input_dev;
2452 if (input_dev) {
2453 gspca_dev->input_dev = NULL;
2454 input_unregister_device(input_dev);
2455 }
Jean-François Moinee070bf92010-02-18 15:02:51 -03002456#endif
Márton Németh0274d422010-01-28 06:39:49 -03002457
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002458 /* the device is freed at exit of this function */
2459 gspca_dev->dev = NULL;
Adam Bakerad281272009-02-04 15:33:21 -03002460 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03002461
Frank Zago6b060ff2008-09-28 08:12:22 -03002462 usb_set_intfdata(intf, NULL);
2463
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002464 /* release the device */
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002465 /* (this will call gspca_release() immediately or on last close) */
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002466 video_unregister_device(&gspca_dev->vdev);
Jean-Francois Moine3145b8c2008-10-05 04:22:10 -03002467
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002468/* PDEBUG(D_PROBE, "disconnect complete"); */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002469}
2470EXPORT_SYMBOL(gspca_disconnect);
2471
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002472#ifdef CONFIG_PM
2473int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2474{
2475 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2476
2477 if (!gspca_dev->streaming)
2478 return 0;
2479 gspca_dev->frozen = 1; /* avoid urb error messages */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002480 if (gspca_dev->sd_desc->stopN)
2481 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002482 destroy_urbs(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002483 gspca_input_destroy_urb(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002484 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002485 if (gspca_dev->sd_desc->stop0)
2486 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002487 return 0;
2488}
2489EXPORT_SYMBOL(gspca_suspend);
2490
2491int gspca_resume(struct usb_interface *intf)
2492{
2493 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2494
2495 gspca_dev->frozen = 0;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002496 gspca_dev->sd_desc->init(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002497 gspca_input_create_urb(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002498 if (gspca_dev->streaming)
2499 return gspca_init_transfer(gspca_dev);
Jean-Francois Moine1e89e2da2008-09-03 17:12:15 -03002500 return 0;
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002501}
2502EXPORT_SYMBOL(gspca_resume);
2503#endif
Hans de Goededcef32372008-07-10 10:40:53 -03002504/* -- cam driver utility functions -- */
2505
2506/* auto gain and exposure algorithm based on the knee algorithm described here:
2507 http://ytse.tricolour.net/docs/LowLightOptimization.html
2508
2509 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2510 where changed. */
2511int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2512 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2513{
2514 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2515 const struct ctrl *gain_ctrl = NULL;
2516 const struct ctrl *exposure_ctrl = NULL;
2517 const struct ctrl *autogain_ctrl = NULL;
2518 int retval = 0;
2519
2520 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goede26984b02010-02-01 13:18:37 -03002521 if (gspca_dev->ctrl_dis & (1 << i))
2522 continue;
Hans de Goededcef32372008-07-10 10:40:53 -03002523 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2524 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2525 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2526 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2527 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2528 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2529 }
2530 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2531 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2532 "on cam without (auto)gain/exposure");
2533 return 0;
2534 }
2535
2536 if (gain_ctrl->get(gspca_dev, &gain) ||
2537 exposure_ctrl->get(gspca_dev, &exposure) ||
2538 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2539 return 0;
2540
2541 orig_gain = gain;
2542 orig_exposure = exposure;
2543
2544 /* If we are of a multiple of deadzone, do multiple steps to reach the
2545 desired lumination fast (with the risc of a slight overshoot) */
2546 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2547
Jean-Francois Moine1c44d812008-11-10 05:56:55 -03002548 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
Hans de Goededcef32372008-07-10 10:40:53 -03002549 avg_lum, desired_avg_lum, steps);
2550
2551 for (i = 0; i < steps; i++) {
2552 if (avg_lum > desired_avg_lum) {
2553 if (gain > gain_knee)
2554 gain--;
2555 else if (exposure > exposure_knee)
2556 exposure--;
2557 else if (gain > gain_ctrl->qctrl.default_value)
2558 gain--;
2559 else if (exposure > exposure_ctrl->qctrl.minimum)
2560 exposure--;
2561 else if (gain > gain_ctrl->qctrl.minimum)
2562 gain--;
2563 else
2564 break;
2565 } else {
2566 if (gain < gain_ctrl->qctrl.default_value)
2567 gain++;
2568 else if (exposure < exposure_knee)
2569 exposure++;
2570 else if (gain < gain_knee)
2571 gain++;
2572 else if (exposure < exposure_ctrl->qctrl.maximum)
2573 exposure++;
2574 else if (gain < gain_ctrl->qctrl.maximum)
2575 gain++;
2576 else
2577 break;
2578 }
2579 }
2580
2581 if (gain != orig_gain) {
2582 gain_ctrl->set(gspca_dev, gain);
2583 retval = 1;
2584 }
2585 if (exposure != orig_exposure) {
2586 exposure_ctrl->set(gspca_dev, exposure);
2587 retval = 1;
2588 }
2589
2590 return retval;
2591}
2592EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2593
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002594/* -- module insert / remove -- */
2595static int __init gspca_init(void)
2596{
Jean-François Moinef22123f2011-09-23 05:17:10 -03002597 pr_info("v" GSPCA_VERSION " registered\n");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002598 return 0;
2599}
2600static void __exit gspca_exit(void)
2601{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002602}
2603
2604module_init(gspca_init);
2605module_exit(gspca_exit);
2606
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03002607#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002608module_param_named(debug, gspca_debug, int, 0644);
2609MODULE_PARM_DESC(debug,
2610 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-François Moinef8e56772011-05-17 04:10:32 -03002611 " 0x08:stream 0x10:frame 0x20:packet"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002612 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002613#endif