blob: 2dc7270722f39fd2b584eac85948ae95bcb23482 [file] [log] [blame]
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001/*
2 * Main USB camera driver
3 *
Jean-François Moine27c6f9e2010-04-02 06:59:13 -03004 * Copyright (C) 2008-2010 Jean-François Moine <http://moinejf.free.fr>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03005 *
Márton Németh0274d422010-01-28 06:39:49 -03006 * Camera button input handling by Márton Németh
7 * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu>
8 *
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03009 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#define MODULE_NAME "gspca"
25
26#include <linux/init.h>
Jean-Francois Moinecf0fe082008-10-17 04:36:47 -030027#include <linux/version.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030028#include <linux/fs.h>
29#include <linux/vmalloc.h>
30#include <linux/sched.h>
31#include <linux/slab.h>
32#include <linux/mm.h>
33#include <linux/string.h>
34#include <linux/pagemap.h>
Jean-Francois Moine956e42d2008-07-01 10:03:42 -030035#include <linux/io.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030036#include <asm/page.h>
Jean-Francois Moine956e42d2008-07-01 10:03:42 -030037#include <linux/uaccess.h>
Paulo Assis2f51d002010-03-07 06:56:56 -030038#include <linux/ktime.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030039#include <media/v4l2-ioctl.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030040
41#include "gspca.h"
42
Randy Dunlapf7628012010-03-30 14:32:04 -030043#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -030044#include <linux/input.h>
45#include <linux/usb/input.h>
Jean-François Moinee070bf92010-02-18 15:02:51 -030046#endif
Márton Németh0274d422010-01-28 06:39:49 -030047
Jean-Francois Moined43fa322008-06-12 10:58:58 -030048/* global values */
Jean-Francois Moine8fee8452009-01-17 04:46:38 -030049#define DEF_NURBS 3 /* default number of URBs */
50#if DEF_NURBS > MAX_NURBS
51#error "DEF_NURBS too big"
52#endif
Jean-Francois Moined43fa322008-06-12 10:58:58 -030053
Jean-François Moine27c6f9e2010-04-02 06:59:13 -030054MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030055MODULE_DESCRIPTION("GSPCA USB Camera Driver");
56MODULE_LICENSE("GPL");
57
Jean-François Moine55461f42010-01-30 16:03:02 -030058#define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 9, 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030059
Jean-Francois Moine335b3f82008-07-30 04:53:02 -030060#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030061int gspca_debug = D_ERR | D_PROBE;
62EXPORT_SYMBOL(gspca_debug);
63
64static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
65{
66 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
67 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
68 txt,
69 pixfmt & 0xff,
70 (pixfmt >> 8) & 0xff,
71 (pixfmt >> 16) & 0xff,
72 pixfmt >> 24,
73 w, h);
74 } else {
75 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
76 txt,
77 pixfmt,
78 w, h);
79 }
80}
81#else
82#define PDEBUG_MODE(txt, pixfmt, w, h)
83#endif
84
Jean-Francois Moine77ee3312009-11-07 15:30:50 -030085/* specific memory types - !! should be different from V4L2_MEMORY_xxx */
Jean-Francois Moined43fa322008-06-12 10:58:58 -030086#define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
87#define GSPCA_MEMORY_READ 7
88
Jean-Francois Moined43fa322008-06-12 10:58:58 -030089#define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
Jean-Francois Moined43fa322008-06-12 10:58:58 -030090
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030091/*
92 * VMA operations.
93 */
94static void gspca_vm_open(struct vm_area_struct *vma)
95{
96 struct gspca_frame *frame = vma->vm_private_data;
97
98 frame->vma_use_count++;
99 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
100}
101
102static void gspca_vm_close(struct vm_area_struct *vma)
103{
104 struct gspca_frame *frame = vma->vm_private_data;
105
106 if (--frame->vma_use_count <= 0)
107 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
108}
109
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400110static const struct vm_operations_struct gspca_vm_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300111 .open = gspca_vm_open,
112 .close = gspca_vm_close,
113};
114
Márton Németh0274d422010-01-28 06:39:49 -0300115/*
116 * Input and interrupt endpoint handling functions
117 */
Randy Dunlapf7628012010-03-30 14:32:04 -0300118#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -0300119static void int_irq(struct urb *urb)
120{
121 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
122 int ret;
123
124 ret = urb->status;
125 switch (ret) {
126 case 0:
127 if (gspca_dev->sd_desc->int_pkt_scan(gspca_dev,
128 urb->transfer_buffer, urb->actual_length) < 0) {
129 PDEBUG(D_ERR, "Unknown packet received");
130 }
131 break;
132
133 case -ENOENT:
134 case -ECONNRESET:
135 case -ENODEV:
136 case -ESHUTDOWN:
137 /* Stop is requested either by software or hardware is gone,
138 * keep the ret value non-zero and don't resubmit later.
139 */
140 break;
141
142 default:
143 PDEBUG(D_ERR, "URB error %i, resubmitting", urb->status);
144 urb->status = 0;
145 ret = 0;
146 }
147
148 if (ret == 0) {
149 ret = usb_submit_urb(urb, GFP_ATOMIC);
150 if (ret < 0)
151 PDEBUG(D_ERR, "Resubmit URB failed with error %i", ret);
152 }
153}
154
155static int gspca_input_connect(struct gspca_dev *dev)
156{
157 struct input_dev *input_dev;
158 int err = 0;
159
160 dev->input_dev = NULL;
Hans de Goedeac82f592010-02-19 04:28:39 -0300161 if (dev->sd_desc->int_pkt_scan || dev->sd_desc->other_input) {
Márton Németh0274d422010-01-28 06:39:49 -0300162 input_dev = input_allocate_device();
163 if (!input_dev)
164 return -ENOMEM;
165
166 usb_make_path(dev->dev, dev->phys, sizeof(dev->phys));
167 strlcat(dev->phys, "/input0", sizeof(dev->phys));
168
169 input_dev->name = dev->sd_desc->name;
170 input_dev->phys = dev->phys;
171
172 usb_to_input_id(dev->dev, &input_dev->id);
173
174 input_dev->evbit[0] = BIT_MASK(EV_KEY);
175 input_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
176 input_dev->dev.parent = &dev->dev->dev;
177
178 err = input_register_device(input_dev);
179 if (err) {
180 PDEBUG(D_ERR, "Input device registration failed "
181 "with error %i", err);
182 input_dev->dev.parent = NULL;
183 input_free_device(input_dev);
184 } else {
185 dev->input_dev = input_dev;
186 }
Hans de Goede10ee2402010-02-19 04:41:40 -0300187 }
Márton Németh0274d422010-01-28 06:39:49 -0300188
189 return err;
190}
191
192static int alloc_and_submit_int_urb(struct gspca_dev *gspca_dev,
193 struct usb_endpoint_descriptor *ep)
194{
195 unsigned int buffer_len;
196 int interval;
197 struct urb *urb;
198 struct usb_device *dev;
199 void *buffer = NULL;
200 int ret = -EINVAL;
201
Jean-François Moineb26d6e22010-04-29 06:01:53 -0300202 buffer_len = le16_to_cpu(ep->wMaxPacketSize);
Márton Németh0274d422010-01-28 06:39:49 -0300203 interval = ep->bInterval;
204 PDEBUG(D_PROBE, "found int in endpoint: 0x%x, "
205 "buffer_len=%u, interval=%u",
206 ep->bEndpointAddress, buffer_len, interval);
207
208 dev = gspca_dev->dev;
209
210 urb = usb_alloc_urb(0, GFP_KERNEL);
211 if (!urb) {
212 ret = -ENOMEM;
213 goto error;
214 }
215
Linus Torvalds7a9b1492010-05-20 21:26:12 -0700216 buffer = usb_alloc_coherent(dev, buffer_len,
Márton Németh0274d422010-01-28 06:39:49 -0300217 GFP_KERNEL, &urb->transfer_dma);
218 if (!buffer) {
219 ret = -ENOMEM;
220 goto error_buffer;
221 }
222 usb_fill_int_urb(urb, dev,
223 usb_rcvintpipe(dev, ep->bEndpointAddress),
224 buffer, buffer_len,
225 int_irq, (void *)gspca_dev, interval);
226 gspca_dev->int_urb = urb;
227 ret = usb_submit_urb(urb, GFP_KERNEL);
228 if (ret < 0) {
229 PDEBUG(D_ERR, "submit URB failed with error %i", ret);
230 goto error_submit;
231 }
232 return ret;
233
234error_submit:
Daniel Mack997ea582010-04-12 13:17:25 +0200235 usb_free_coherent(dev,
236 urb->transfer_buffer_length,
237 urb->transfer_buffer,
238 urb->transfer_dma);
Márton Németh0274d422010-01-28 06:39:49 -0300239error_buffer:
240 usb_free_urb(urb);
241error:
242 return ret;
243}
244
Hans de Goede10ee2402010-02-19 04:41:40 -0300245static void gspca_input_create_urb(struct gspca_dev *gspca_dev)
Márton Németh0274d422010-01-28 06:39:49 -0300246{
Márton Németh0274d422010-01-28 06:39:49 -0300247 struct usb_interface *intf;
248 struct usb_host_interface *intf_desc;
249 struct usb_endpoint_descriptor *ep;
250 int i;
251
252 if (gspca_dev->sd_desc->int_pkt_scan) {
253 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
254 intf_desc = intf->cur_altsetting;
255 for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
256 ep = &intf_desc->endpoint[i].desc;
257 if (usb_endpoint_dir_in(ep) &&
258 usb_endpoint_xfer_int(ep)) {
259
Hans de Goede10ee2402010-02-19 04:41:40 -0300260 alloc_and_submit_int_urb(gspca_dev, ep);
Márton Németh0274d422010-01-28 06:39:49 -0300261 break;
262 }
263 }
264 }
Márton Németh0274d422010-01-28 06:39:49 -0300265}
266
267static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
268{
269 struct urb *urb;
270
271 urb = gspca_dev->int_urb;
272 if (urb) {
273 gspca_dev->int_urb = NULL;
274 usb_kill_urb(urb);
Daniel Mack997ea582010-04-12 13:17:25 +0200275 usb_free_coherent(gspca_dev->dev,
276 urb->transfer_buffer_length,
277 urb->transfer_buffer,
278 urb->transfer_dma);
Márton Németh0274d422010-01-28 06:39:49 -0300279 usb_free_urb(urb);
280 }
281}
Randy Dunlapf7628012010-03-30 14:32:04 -0300282#else
283static inline void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
284{
285}
286
287static inline void gspca_input_create_urb(struct gspca_dev *gspca_dev)
288{
289}
290
291static inline int gspca_input_connect(struct gspca_dev *dev)
292{
293 return 0;
294}
Márton Németh0274d422010-01-28 06:39:49 -0300295#endif
296
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300297/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300298 * fill a video frame from an URB and resubmit
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300299 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300300static void fill_frame(struct gspca_dev *gspca_dev,
301 struct urb *urb)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300302{
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300303 u8 *data; /* address of data in the iso message */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300304 int i, len, st;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300305 cam_pkt_op pkt_scan;
306
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300307 if (urb->status != 0) {
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300308 if (urb->status == -ESHUTDOWN)
309 return; /* disconnection */
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300310#ifdef CONFIG_PM
Hans de Goede6a540bd2009-10-24 15:02:14 -0300311 if (gspca_dev->frozen)
312 return;
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300313#endif
Hans de Goede6a540bd2009-10-24 15:02:14 -0300314 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine81578522009-11-13 07:15:08 -0300315 urb->status = 0;
Hans de Goedebf926ad2009-10-23 06:56:06 -0300316 goto resubmit;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300317 }
Jean-François Moineb192ca92010-06-27 03:08:19 -0300318 if (gspca_dev->image == NULL)
319 gspca_dev->last_packet_type = DISCARD_PACKET;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300320 pkt_scan = gspca_dev->sd_desc->pkt_scan;
321 for (i = 0; i < urb->number_of_packets; i++) {
322
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300323 /* check the packet status and length */
324 len = urb->iso_frame_desc[i].actual_length;
Jean-Francois Moineff374742008-10-23 07:29:51 -0300325 if (len == 0) {
326 if (gspca_dev->empty_packet == 0)
327 gspca_dev->empty_packet = 1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300328 continue;
Jean-Francois Moineff374742008-10-23 07:29:51 -0300329 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300330 st = urb->iso_frame_desc[i].status;
331 if (st) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300332 PDEBUG(D_ERR,
333 "ISOC data error: [%d] len=%d, status=%d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300334 i, len, st);
335 gspca_dev->last_packet_type = DISCARD_PACKET;
336 continue;
337 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300338
339 /* let the packet be analyzed by the subdriver */
340 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
341 i, urb->iso_frame_desc[i].offset, len);
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300342 data = (u8 *) urb->transfer_buffer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300343 + urb->iso_frame_desc[i].offset;
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300344 pkt_scan(gspca_dev, data, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300345 }
346
Hans de Goedebf926ad2009-10-23 06:56:06 -0300347resubmit:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300348 /* resubmit the URB */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300349 st = usb_submit_urb(urb, GFP_ATOMIC);
350 if (st < 0)
351 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
352}
353
354/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300355 * ISOC message interrupt from the USB device
356 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300357 * Analyse each packet and call the subdriver for copy to the frame buffer.
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300358 */
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300359static void isoc_irq(struct urb *urb)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300360{
361 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
362
Hans de Goede3647fea2008-07-15 05:36:30 -0300363 PDEBUG(D_PACK, "isoc irq");
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300364 if (!gspca_dev->streaming)
365 return;
366 fill_frame(gspca_dev, urb);
367}
368
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300369/*
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300370 * bulk message interrupt from the USB device
371 */
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300372static void bulk_irq(struct urb *urb)
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300373{
374 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300375 int st;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300376
377 PDEBUG(D_PACK, "bulk irq");
378 if (!gspca_dev->streaming)
379 return;
Jean-Francois Moine880ae242008-11-18 04:19:52 -0300380 switch (urb->status) {
381 case 0:
382 break;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300383 case -ESHUTDOWN:
384 return; /* disconnection */
Jean-Francois Moine880ae242008-11-18 04:19:52 -0300385 default:
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300386#ifdef CONFIG_PM
Hans de Goede6a540bd2009-10-24 15:02:14 -0300387 if (gspca_dev->frozen)
388 return;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300389#endif
Hans de Goede6a540bd2009-10-24 15:02:14 -0300390 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine81578522009-11-13 07:15:08 -0300391 urb->status = 0;
Hans de Goedebf926ad2009-10-23 06:56:06 -0300392 goto resubmit;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300393 }
394
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300395 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
396 gspca_dev->sd_desc->pkt_scan(gspca_dev,
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300397 urb->transfer_buffer,
398 urb->actual_length);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300399
Hans de Goedebf926ad2009-10-23 06:56:06 -0300400resubmit:
Antonio Ospitedff369a2008-11-14 07:53:32 -0300401 /* resubmit the URB */
402 if (gspca_dev->cam.bulk_nurbs != 0) {
403 st = usb_submit_urb(urb, GFP_ATOMIC);
404 if (st < 0)
405 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
406 }
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300407}
408
409/*
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300410 * add data to the current frame
411 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300412 * This function is called by the subdrivers at interrupt level.
413 *
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300414 * To build a frame, these ones must add
415 * - one FIRST_PACKET
416 * - 0 or many INTER_PACKETs
417 * - one LAST_PACKET
418 * DISCARD_PACKET invalidates the whole frame.
419 * On LAST_PACKET, a new frame is returned.
420 */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300421void gspca_frame_add(struct gspca_dev *gspca_dev,
422 enum gspca_packet_type packet_type,
423 const u8 *data,
424 int len)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300425{
Jean-Francois Moine3ec342f2009-11-13 07:38:16 -0300426 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300427 int i, j;
428
Hans de Goedee2997a72008-04-23 08:09:12 -0300429 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300430
431 /* check the availability of the frame buffer */
Jean-François Moineb192ca92010-06-27 03:08:19 -0300432 if (gspca_dev->image == NULL)
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300433 return;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300434
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300435 if (packet_type == FIRST_PACKET) {
Jean-François Moineb192ca92010-06-27 03:08:19 -0300436 i = gspca_dev->fr_i;
437 j = gspca_dev->fr_queue[i];
438 frame = &gspca_dev->frame[j];
Paulo Assis2f51d002010-03-07 06:56:56 -0300439 frame->v4l2_buf.timestamp = ktime_to_timeval(ktime_get());
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300440 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
Jean-François Moineb192ca92010-06-27 03:08:19 -0300441 gspca_dev->image_len = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300442 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
Jean-Francois Moine06ca78f2008-07-22 03:45:08 -0300443 if (packet_type == LAST_PACKET)
444 gspca_dev->last_packet_type = packet_type;
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300445 return;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300446 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300447
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300448 /* append the packet to the frame buffer */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300449 if (len > 0) {
Jean-François Moineb192ca92010-06-27 03:08:19 -0300450 if (gspca_dev->image_len + len > gspca_dev->frsz) {
451 PDEBUG(D_ERR|D_PACK, "frame overflow %d > %d",
452 gspca_dev->image_len + len,
453 gspca_dev->frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300454 packet_type = DISCARD_PACKET;
455 } else {
Jean-François Moineb192ca92010-06-27 03:08:19 -0300456 memcpy(gspca_dev->image + gspca_dev->image_len,
457 data, len);
458 gspca_dev->image_len += len;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300459 }
460 }
461 gspca_dev->last_packet_type = packet_type;
462
Frank Zago219423c2008-09-26 07:43:54 -0300463 /* if last packet, wake up the application and advance in the queue */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300464 if (packet_type == LAST_PACKET) {
Jean-François Moineb192ca92010-06-27 03:08:19 -0300465 i = gspca_dev->fr_i;
466 j = gspca_dev->fr_queue[i];
467 frame = &gspca_dev->frame[j];
468 frame->v4l2_buf.bytesused = gspca_dev->image_len;
Jean-François Moine02bbcb92010-07-06 04:16:40 -0300469 frame->v4l2_buf.flags = (frame->v4l2_buf.flags
470 | V4L2_BUF_FLAG_DONE)
471 & ~V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300472 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
Jean-François Moineb192ca92010-06-27 03:08:19 -0300473 i = (i + 1) % gspca_dev->nframes;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300474 gspca_dev->fr_i = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300475 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
476 frame->v4l2_buf.bytesused,
477 gspca_dev->fr_q,
478 i,
479 gspca_dev->fr_o);
480 j = gspca_dev->fr_queue[i];
Jean-François Moineb192ca92010-06-27 03:08:19 -0300481 frame = &gspca_dev->frame[j];
482 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
483 == V4L2_BUF_FLAG_QUEUED) {
484 gspca_dev->image = frame->data;
485 } else {
486 gspca_dev->image = NULL;
487 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300488 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300489}
490EXPORT_SYMBOL(gspca_frame_add);
491
492static int gspca_is_compressed(__u32 format)
493{
494 switch (format) {
495 case V4L2_PIX_FMT_MJPEG:
496 case V4L2_PIX_FMT_JPEG:
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300497 case V4L2_PIX_FMT_SPCA561:
Hans de Goedeab8f12c2008-07-04 18:29:32 -0300498 case V4L2_PIX_FMT_PAC207:
Kyle Guinnd661e622009-01-16 05:36:14 -0300499 case V4L2_PIX_FMT_MR97310A:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300500 return 1;
501 }
502 return 0;
503}
504
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300505static int frame_alloc(struct gspca_dev *gspca_dev,
506 unsigned int count)
507{
508 struct gspca_frame *frame;
509 unsigned int frsz;
510 int i;
511
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300512 i = gspca_dev->curr_mode;
513 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300514 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300515 frsz = PAGE_ALIGN(frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300516 gspca_dev->frsz = frsz;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300517 if (count > GSPCA_MAX_FRAMES)
518 count = GSPCA_MAX_FRAMES;
Jean-François Moinee795d912010-06-24 05:14:22 -0300519 gspca_dev->frbuf = vmalloc_32(frsz * count);
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300520 if (!gspca_dev->frbuf) {
521 err("frame alloc failed");
522 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300523 }
524 gspca_dev->nframes = count;
525 for (i = 0; i < count; i++) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300526 frame = &gspca_dev->frame[i];
527 frame->v4l2_buf.index = i;
528 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
529 frame->v4l2_buf.flags = 0;
530 frame->v4l2_buf.field = V4L2_FIELD_NONE;
531 frame->v4l2_buf.length = frsz;
532 frame->v4l2_buf.memory = gspca_dev->memory;
533 frame->v4l2_buf.sequence = 0;
Jean-François Moineb192ca92010-06-27 03:08:19 -0300534 frame->data = gspca_dev->frbuf + i * frsz;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300535 frame->v4l2_buf.m.offset = i * frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300536 }
537 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
Jean-François Moineb192ca92010-06-27 03:08:19 -0300538 gspca_dev->image = NULL;
539 gspca_dev->image_len = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300540 gspca_dev->last_packet_type = DISCARD_PACKET;
541 gspca_dev->sequence = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300542 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300543}
544
545static void frame_free(struct gspca_dev *gspca_dev)
546{
547 int i;
548
549 PDEBUG(D_STREAM, "frame free");
Harvey Harrisonefab8212008-07-05 06:12:47 -0300550 if (gspca_dev->frbuf != NULL) {
Jean-François Moinee795d912010-06-24 05:14:22 -0300551 vfree(gspca_dev->frbuf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300552 gspca_dev->frbuf = NULL;
553 for (i = 0; i < gspca_dev->nframes; i++)
554 gspca_dev->frame[i].data = NULL;
555 }
556 gspca_dev->nframes = 0;
557}
558
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300559static void destroy_urbs(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300560{
561 struct urb *urb;
562 unsigned int i;
563
564 PDEBUG(D_STREAM, "kill transfer");
Erik Andrén44732812008-10-13 15:52:46 -0300565 for (i = 0; i < MAX_NURBS; i++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300566 urb = gspca_dev->urb[i];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300567 if (urb == NULL)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300568 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300569
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300570 gspca_dev->urb[i] = NULL;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300571 usb_kill_urb(urb);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300572 if (urb->transfer_buffer != NULL)
Daniel Mack997ea582010-04-12 13:17:25 +0200573 usb_free_coherent(gspca_dev->dev,
574 urb->transfer_buffer_length,
575 urb->transfer_buffer,
576 urb->transfer_dma);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300577 usb_free_urb(urb);
578 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300579}
580
Jean-François Moinee4dac282010-04-09 06:11:36 -0300581static int gspca_set_alt0(struct gspca_dev *gspca_dev)
582{
583 int ret;
584
585 if (gspca_dev->alt == 0)
586 return 0;
587 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
588 if (ret < 0)
589 PDEBUG(D_ERR|D_STREAM, "set alt 0 err %d", ret);
590 return ret;
591}
592
593/* Note: both the queue and the usb locks should be held when calling this */
594static void gspca_stream_off(struct gspca_dev *gspca_dev)
595{
596 gspca_dev->streaming = 0;
597 if (gspca_dev->present) {
598 if (gspca_dev->sd_desc->stopN)
599 gspca_dev->sd_desc->stopN(gspca_dev);
600 destroy_urbs(gspca_dev);
601 gspca_input_destroy_urb(gspca_dev);
602 gspca_set_alt0(gspca_dev);
603 gspca_input_create_urb(gspca_dev);
604 }
605
606 /* always call stop0 to free the subdriver's resources */
607 if (gspca_dev->sd_desc->stop0)
608 gspca_dev->sd_desc->stop0(gspca_dev);
609 PDEBUG(D_STREAM, "stream off OK");
610}
611
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300612/*
Frank Zago219423c2008-09-26 07:43:54 -0300613 * look for an input transfer endpoint in an alternate setting
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300614 */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300615static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300616 int xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300617{
618 struct usb_host_endpoint *ep;
619 int i, attr;
620
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300621 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
622 ep = &alt->endpoint[i];
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300623 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Jean-Francois Moine30d35e42009-04-21 13:57:31 -0300624 if (attr == xfer
625 && ep->desc.wMaxPacketSize != 0)
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300626 return ep;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300627 }
628 return NULL;
629}
630
631/*
Frank Zago219423c2008-09-26 07:43:54 -0300632 * look for an input (isoc or bulk) endpoint
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300633 *
634 * The endpoint is defined by the subdriver.
635 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
636 * This routine may be called many times when the bandwidth is too small
637 * (the bandwidth is checked on urb submit).
638 */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300639static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300640{
641 struct usb_interface *intf;
642 struct usb_host_endpoint *ep;
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300643 int xfer, i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300644
645 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300646 ep = NULL;
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300647 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
648 : USB_ENDPOINT_XFER_ISOC;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300649 i = gspca_dev->alt; /* previous alt setting */
Hans de Goedea511ba92009-10-16 07:13:07 -0300650 if (gspca_dev->cam.reverse_alts) {
651 while (++i < gspca_dev->nbalt) {
652 ep = alt_xfer(&intf->altsetting[i], xfer);
653 if (ep)
654 break;
655 }
656 } else {
657 while (--i >= 0) {
658 ep = alt_xfer(&intf->altsetting[i], xfer);
659 if (ep)
660 break;
661 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300662 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300663 if (ep == NULL) {
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300664 err("no transfer endpoint found");
665 return NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300666 }
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300667 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300668 i, ep->desc.bEndpointAddress);
Jean-Francois Moine1c8336d2009-07-28 04:38:25 -0300669 gspca_dev->alt = i; /* memorize the current alt setting */
Jean-Francois Moine8dd07ef2009-04-21 14:05:44 -0300670 if (gspca_dev->nbalt > 1) {
Márton Németh0274d422010-01-28 06:39:49 -0300671 gspca_input_destroy_urb(gspca_dev);
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300672 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
673 if (ret < 0) {
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300674 err("set alt %d err %d", i, ret);
Márton Németh0274d422010-01-28 06:39:49 -0300675 ep = NULL;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300676 }
Márton Németh0274d422010-01-28 06:39:49 -0300677 gspca_input_create_urb(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300678 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300679 return ep;
680}
681
682/*
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300683 * create the URBs for image transfer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300684 */
685static int create_urbs(struct gspca_dev *gspca_dev,
686 struct usb_host_endpoint *ep)
687{
688 struct urb *urb;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300689 int n, nurbs, i, psize, npkt, bsize;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300690
691 /* calculate the packet size and the number of packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300692 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300693
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300694 if (!gspca_dev->cam.bulk) { /* isoc */
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300695
696 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300697 if (gspca_dev->pkt_size == 0)
698 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
699 else
700 psize = gspca_dev->pkt_size;
Jean-Francois Moine49cb6b02009-04-25 13:29:01 -0300701 npkt = gspca_dev->cam.npkt;
702 if (npkt == 0)
703 npkt = 32; /* default value */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300704 bsize = psize * npkt;
705 PDEBUG(D_STREAM,
706 "isoc %d pkts size %d = bsize:%d",
707 npkt, psize, bsize);
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300708 nurbs = DEF_NURBS;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300709 } else { /* bulk */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300710 npkt = 0;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300711 bsize = gspca_dev->cam.bulk_size;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300712 if (bsize == 0)
713 bsize = psize;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300714 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300715 if (gspca_dev->cam.bulk_nurbs != 0)
716 nurbs = gspca_dev->cam.bulk_nurbs;
717 else
718 nurbs = 1;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300719 }
720
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300721 for (n = 0; n < nurbs; n++) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300722 urb = usb_alloc_urb(npkt, GFP_KERNEL);
723 if (!urb) {
724 err("usb_alloc_urb failed");
725 return -ENOMEM;
726 }
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300727 gspca_dev->urb[n] = urb;
Daniel Mack997ea582010-04-12 13:17:25 +0200728 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300729 bsize,
730 GFP_KERNEL,
731 &urb->transfer_dma);
732
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300733 if (urb->transfer_buffer == NULL) {
Daniel Mack997ea582010-04-12 13:17:25 +0200734 err("usb_alloc_coherent failed");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300735 return -ENOMEM;
736 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300737 urb->dev = gspca_dev->dev;
738 urb->context = gspca_dev;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300739 urb->transfer_buffer_length = bsize;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300740 if (npkt != 0) { /* ISOC */
741 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
742 ep->desc.bEndpointAddress);
Frank Zago6c49da72008-09-28 07:43:00 -0300743 urb->transfer_flags = URB_ISO_ASAP
744 | URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300745 urb->interval = ep->desc.bInterval;
746 urb->complete = isoc_irq;
747 urb->number_of_packets = npkt;
748 for (i = 0; i < npkt; i++) {
749 urb->iso_frame_desc[i].length = psize;
750 urb->iso_frame_desc[i].offset = psize * i;
751 }
752 } else { /* bulk */
753 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
754 ep->desc.bEndpointAddress),
Frank Zago6c49da72008-09-28 07:43:00 -0300755 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300756 urb->complete = bulk_irq;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300757 }
758 }
759 return 0;
760}
761
762/*
763 * start the USB transfer
764 */
765static int gspca_init_transfer(struct gspca_dev *gspca_dev)
766{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300767 struct usb_host_endpoint *ep;
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300768 struct urb *urb;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300769 int n, ret;
770
Hans de Goedee2997a72008-04-23 08:09:12 -0300771 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
772 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300773
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300774 if (!gspca_dev->present) {
775 ret = -ENODEV;
776 goto out;
777 }
778
Jean-Francois Moineab6c8cf2010-01-13 15:34:27 -0300779 gspca_dev->usb_err = 0;
780
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300781 /* set the higher alternate setting and
782 * loop until urb submit succeeds */
Hans de Goedea511ba92009-10-16 07:13:07 -0300783 if (gspca_dev->cam.reverse_alts)
784 gspca_dev->alt = 0;
785 else
786 gspca_dev->alt = gspca_dev->nbalt;
787
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300788 if (gspca_dev->sd_desc->isoc_init) {
789 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
790 if (ret < 0)
791 goto out;
792 }
793 ep = get_ep(gspca_dev);
794 if (ep == NULL) {
795 ret = -EIO;
796 goto out;
797 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300798 for (;;) {
Jean-Francois Moine47aaca92009-12-15 05:23:04 -0300799 if (!gspca_dev->cam.no_urb_create) {
800 PDEBUG(D_STREAM, "init transfer alt %d",
801 gspca_dev->alt);
802 ret = create_urbs(gspca_dev, ep);
803 if (ret < 0) {
804 destroy_urbs(gspca_dev);
805 goto out;
806 }
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300807 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300808
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300809 /* clear the bulk endpoint */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300810 if (gspca_dev->cam.bulk)
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300811 usb_clear_halt(gspca_dev->dev,
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300812 gspca_dev->urb[0]->pipe);
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300813
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300814 /* start the cam */
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300815 ret = gspca_dev->sd_desc->start(gspca_dev);
816 if (ret < 0) {
817 destroy_urbs(gspca_dev);
818 goto out;
819 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300820 gspca_dev->streaming = 1;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300821
Antonio Ospitedff369a2008-11-14 07:53:32 -0300822 /* some bulk transfers are started by the subdriver */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300823 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300824 break;
825
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300826 /* submit the URBs */
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300827 for (n = 0; n < MAX_NURBS; n++) {
828 urb = gspca_dev->urb[n];
829 if (urb == NULL)
830 break;
831 ret = usb_submit_urb(urb, GFP_KERNEL);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300832 if (ret < 0)
833 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300834 }
835 if (ret >= 0)
836 break;
Jean-François Moinee4dac282010-04-09 06:11:36 -0300837 gspca_stream_off(gspca_dev);
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300838 if (ret != -ENOSPC) {
839 PDEBUG(D_ERR|D_STREAM,
840 "usb_submit_urb alt %d err %d",
841 gspca_dev->alt, ret);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300842 goto out;
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300843 }
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300844
845 /* the bandwidth is not wide enough
846 * negociate or try a lower alternate setting */
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300847 PDEBUG(D_ERR|D_STREAM,
848 "bandwidth not wide enough - trying again");
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300849 msleep(20); /* wait for kill complete */
850 if (gspca_dev->sd_desc->isoc_nego) {
851 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
852 if (ret < 0)
853 goto out;
854 } else {
855 ep = get_ep(gspca_dev);
856 if (ep == NULL) {
857 ret = -EIO;
858 goto out;
859 }
860 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300861 }
862out:
863 mutex_unlock(&gspca_dev->usb_lock);
864 return ret;
865}
866
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300867static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300868{
869 int i;
870
871 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
872 gspca_dev->curr_mode = i;
873 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
874 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300875 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300876}
877
878static int wxh_to_mode(struct gspca_dev *gspca_dev,
879 int width, int height)
880{
881 int i;
882
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300883 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
884 if (width >= gspca_dev->cam.cam_mode[i].width
885 && height >= gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300886 break;
887 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300888 return i;
889}
890
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300891/*
892 * search a mode with the right pixel format
893 */
894static int gspca_get_mode(struct gspca_dev *gspca_dev,
895 int mode,
896 int pixfmt)
897{
898 int modeU, modeD;
899
900 modeU = modeD = mode;
901 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
902 if (--modeD >= 0) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300903 if (gspca_dev->cam.cam_mode[modeD].pixelformat
904 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300905 return modeD;
906 }
907 if (++modeU < gspca_dev->cam.nmodes) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300908 if (gspca_dev->cam.cam_mode[modeU].pixelformat
909 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300910 return modeU;
911 }
912 }
913 return -EINVAL;
914}
915
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300916#ifdef CONFIG_VIDEO_ADV_DEBUG
917static int vidioc_g_register(struct file *file, void *priv,
918 struct v4l2_dbg_register *reg)
919{
920 int ret;
921 struct gspca_dev *gspca_dev = priv;
922
923 if (!gspca_dev->sd_desc->get_chip_ident)
924 return -EINVAL;
925
926 if (!gspca_dev->sd_desc->get_register)
927 return -EINVAL;
928
929 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
930 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -0300931 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300932 if (gspca_dev->present)
933 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
934 else
935 ret = -ENODEV;
936 mutex_unlock(&gspca_dev->usb_lock);
937
938 return ret;
939}
940
941static int vidioc_s_register(struct file *file, void *priv,
942 struct v4l2_dbg_register *reg)
943{
944 int ret;
945 struct gspca_dev *gspca_dev = priv;
946
947 if (!gspca_dev->sd_desc->get_chip_ident)
948 return -EINVAL;
949
950 if (!gspca_dev->sd_desc->set_register)
951 return -EINVAL;
952
953 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
954 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -0300955 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300956 if (gspca_dev->present)
957 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
958 else
959 ret = -ENODEV;
960 mutex_unlock(&gspca_dev->usb_lock);
961
962 return ret;
963}
964#endif
965
966static int vidioc_g_chip_ident(struct file *file, void *priv,
967 struct v4l2_dbg_chip_ident *chip)
968{
969 int ret;
970 struct gspca_dev *gspca_dev = priv;
971
972 if (!gspca_dev->sd_desc->get_chip_ident)
973 return -EINVAL;
974
975 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
976 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -0300977 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300978 if (gspca_dev->present)
979 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
980 else
981 ret = -ENODEV;
982 mutex_unlock(&gspca_dev->usb_lock);
983
984 return ret;
985}
986
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300987static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300988 struct v4l2_fmtdesc *fmtdesc)
989{
990 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300991 int i, j, index;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300992 __u32 fmt_tb[8];
993
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300994 /* give an index to each format */
995 index = 0;
996 j = 0;
997 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300998 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300999 j = 0;
1000 for (;;) {
1001 if (fmt_tb[j] == fmt_tb[index])
1002 break;
1003 j++;
1004 }
1005 if (j == index) {
1006 if (fmtdesc->index == index)
1007 break; /* new format */
1008 index++;
Julia Lawall80297122008-11-12 23:18:21 -03001009 if (index >= ARRAY_SIZE(fmt_tb))
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001010 return -EINVAL;
1011 }
1012 }
1013 if (i < 0)
1014 return -EINVAL; /* no more format */
1015
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001016 fmtdesc->pixelformat = fmt_tb[index];
1017 if (gspca_is_compressed(fmt_tb[index]))
1018 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
1019 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
1020 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
1021 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
1022 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
1023 fmtdesc->description[4] = '\0';
1024 return 0;
1025}
1026
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001027static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001028 struct v4l2_format *fmt)
1029{
1030 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001031 int mode;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001032
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001033 mode = gspca_dev->curr_mode;
1034 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1035 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001036 return 0;
1037}
1038
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001039static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001040 struct v4l2_format *fmt)
1041{
Hans de Goede24c530b2008-07-04 17:55:18 -03001042 int w, h, mode, mode2;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001043
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001044 w = fmt->fmt.pix.width;
1045 h = fmt->fmt.pix.height;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001046
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001047#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001048 if (gspca_debug & D_CONF)
1049 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
1050#endif
1051 /* search the closest mode for width and height */
1052 mode = wxh_to_mode(gspca_dev, w, h);
1053
1054 /* OK if right palette */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001055 if (gspca_dev->cam.cam_mode[mode].pixelformat
1056 != fmt->fmt.pix.pixelformat) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001057
1058 /* else, search the closest mode with the same pixel format */
1059 mode2 = gspca_get_mode(gspca_dev, mode,
1060 fmt->fmt.pix.pixelformat);
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001061 if (mode2 >= 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001062 mode = mode2;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001063/* else
1064 ; * no chance, return this mode */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001065 }
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001066 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1067 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001068 return mode; /* used when s_fmt */
1069}
1070
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001071static int vidioc_try_fmt_vid_cap(struct file *file,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001072 void *priv,
1073 struct v4l2_format *fmt)
1074{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001075 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001076 int ret;
1077
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001078 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001079 if (ret < 0)
1080 return ret;
1081 return 0;
1082}
1083
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001084static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001085 struct v4l2_format *fmt)
1086{
1087 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001088 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001089
Hans de Goedee2997a72008-04-23 08:09:12 -03001090 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1091 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001092
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001093 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001094 if (ret < 0)
1095 goto out;
1096
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001097 if (gspca_dev->nframes != 0
1098 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
1099 ret = -EINVAL;
1100 goto out;
1101 }
1102
Jean-Francois Moine50a871f2008-06-30 19:47:33 -03001103 if (ret == gspca_dev->curr_mode) {
1104 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001105 goto out; /* same mode */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -03001106 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001107
1108 if (gspca_dev->streaming) {
1109 ret = -EBUSY;
1110 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -03001111 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001112 gspca_dev->width = fmt->fmt.pix.width;
1113 gspca_dev->height = fmt->fmt.pix.height;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001114 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
1115 gspca_dev->curr_mode = ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001116
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001117 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001118out:
1119 mutex_unlock(&gspca_dev->queue_lock);
1120 return ret;
1121}
1122
Hans de Goede11c635a2009-05-25 15:04:22 -03001123static int vidioc_enum_framesizes(struct file *file, void *priv,
1124 struct v4l2_frmsizeenum *fsize)
1125{
1126 struct gspca_dev *gspca_dev = priv;
1127 int i;
1128 __u32 index = 0;
1129
1130 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
1131 if (fsize->pixel_format !=
1132 gspca_dev->cam.cam_mode[i].pixelformat)
1133 continue;
1134
1135 if (fsize->index == index) {
1136 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1137 fsize->discrete.width =
1138 gspca_dev->cam.cam_mode[i].width;
1139 fsize->discrete.height =
1140 gspca_dev->cam.cam_mode[i].height;
1141 return 0;
1142 }
1143 index++;
1144 }
1145
1146 return -EINVAL;
1147}
1148
Antonio Ospite28ffe772009-12-02 06:18:46 -03001149static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1150 struct v4l2_frmivalenum *fival)
1151{
1152 struct gspca_dev *gspca_dev = priv;
1153 int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
1154 __u32 i;
1155
1156 if (gspca_dev->cam.mode_framerates == NULL ||
1157 gspca_dev->cam.mode_framerates[mode].nrates == 0)
1158 return -EINVAL;
1159
1160 if (fival->pixel_format !=
1161 gspca_dev->cam.cam_mode[mode].pixelformat)
1162 return -EINVAL;
1163
1164 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1165 if (fival->index == i) {
1166 fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1167 fival->discrete.numerator = 1;
1168 fival->discrete.denominator =
1169 gspca_dev->cam.mode_framerates[mode].rates[i];
1170 return 0;
1171 }
1172 }
1173
1174 return -EINVAL;
1175}
1176
Hans de Goeded0182302008-11-19 17:10:47 -03001177static void gspca_release(struct video_device *vfd)
Frank Zago6b060ff2008-09-28 08:12:22 -03001178{
Hans de Goeded0182302008-11-19 17:10:47 -03001179 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
Frank Zago6b060ff2008-09-28 08:12:22 -03001180
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001181 PDEBUG(D_PROBE, "%s released",
1182 video_device_node_name(&gspca_dev->vdev));
Frank Zago6b060ff2008-09-28 08:12:22 -03001183
1184 kfree(gspca_dev->usb_buf);
1185 kfree(gspca_dev);
1186}
1187
Hans Verkuilbec43662008-12-30 06:58:20 -03001188static int dev_open(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001189{
1190 struct gspca_dev *gspca_dev;
1191 int ret;
1192
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001193 PDEBUG(D_STREAM, "[%s] open", current->comm);
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001194 gspca_dev = (struct gspca_dev *) video_devdata(file);
Hans de Goedee2997a72008-04-23 08:09:12 -03001195 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1196 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001197 if (!gspca_dev->present) {
1198 ret = -ENODEV;
1199 goto out;
1200 }
1201
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001202 if (gspca_dev->users > 4) { /* (arbitrary value) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001203 ret = -EBUSY;
1204 goto out;
1205 }
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03001206
1207 /* protect the subdriver against rmmod */
1208 if (!try_module_get(gspca_dev->module)) {
1209 ret = -ENODEV;
1210 goto out;
1211 }
1212
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001213 gspca_dev->users++;
Frank Zago6b060ff2008-09-28 08:12:22 -03001214
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001215 file->private_data = gspca_dev;
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001216#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001217 /* activate the v4l2 debug */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001218 if (gspca_debug & D_V4L2)
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001219 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001220 | V4L2_DEBUG_IOCTL_ARG;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001221 else
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001222 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001223 | V4L2_DEBUG_IOCTL_ARG);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001224#endif
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001225 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001226out:
1227 mutex_unlock(&gspca_dev->queue_lock);
1228 if (ret != 0)
1229 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
1230 else
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001231 PDEBUG(D_STREAM, "open done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001232 return ret;
1233}
1234
Hans Verkuilbec43662008-12-30 06:58:20 -03001235static int dev_close(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001236{
1237 struct gspca_dev *gspca_dev = file->private_data;
1238
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001239 PDEBUG(D_STREAM, "[%s] close", current->comm);
Hans de Goedee2997a72008-04-23 08:09:12 -03001240 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1241 return -ERESTARTSYS;
1242 gspca_dev->users--;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001243
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001244 /* if the file did the capture, free the streaming resources */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001245 if (gspca_dev->capt_file == file) {
Jean-Francois Moinea1e82882008-09-03 17:12:17 -03001246 if (gspca_dev->streaming) {
1247 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001248 gspca_dev->usb_err = 0;
Jean-Francois Moinea1e82882008-09-03 17:12:17 -03001249 gspca_stream_off(gspca_dev);
1250 mutex_unlock(&gspca_dev->usb_lock);
1251 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001252 frame_free(gspca_dev);
Harvey Harrisonefab8212008-07-05 06:12:47 -03001253 gspca_dev->capt_file = NULL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001254 gspca_dev->memory = GSPCA_MEMORY_NO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001255 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001256 file->private_data = NULL;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03001257 module_put(gspca_dev->module);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001258 mutex_unlock(&gspca_dev->queue_lock);
Frank Zago6b060ff2008-09-28 08:12:22 -03001259
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001260 PDEBUG(D_STREAM, "close done");
Frank Zago6b060ff2008-09-28 08:12:22 -03001261
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001262 return 0;
1263}
1264
1265static int vidioc_querycap(struct file *file, void *priv,
1266 struct v4l2_capability *cap)
1267{
1268 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001269 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001270
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001271 /* protect the access to the usb device */
1272 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1273 return -ERESTARTSYS;
1274 if (!gspca_dev->present) {
1275 ret = -ENODEV;
1276 goto out;
1277 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001278 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -03001279 if (gspca_dev->dev->product != NULL) {
1280 strncpy(cap->card, gspca_dev->dev->product,
1281 sizeof cap->card);
1282 } else {
1283 snprintf(cap->card, sizeof cap->card,
1284 "USB Camera (%04x:%04x)",
1285 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1286 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1287 }
Thierry MERLE59320282009-01-21 15:32:26 -03001288 usb_make_path(gspca_dev->dev, cap->bus_info, sizeof(cap->bus_info));
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001289 cap->version = DRIVER_VERSION_NUMBER;
1290 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1291 | V4L2_CAP_STREAMING
1292 | V4L2_CAP_READWRITE;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001293 ret = 0;
1294out:
1295 mutex_unlock(&gspca_dev->usb_lock);
1296 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001297}
1298
Hans de Goedea0001a22009-06-10 04:52:18 -03001299static const struct ctrl *get_ctrl(struct gspca_dev *gspca_dev,
1300 int id)
1301{
1302 const struct ctrl *ctrls;
1303 int i;
1304
1305 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1306 i < gspca_dev->sd_desc->nctrls;
1307 i++, ctrls++) {
1308 if (gspca_dev->ctrl_dis & (1 << i))
1309 continue;
1310 if (id == ctrls->qctrl.id)
1311 return ctrls;
1312 }
1313 return NULL;
1314}
1315
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001316static int vidioc_queryctrl(struct file *file, void *priv,
1317 struct v4l2_queryctrl *q_ctrl)
1318{
1319 struct gspca_dev *gspca_dev = priv;
Hans de Goedea0001a22009-06-10 04:52:18 -03001320 const struct ctrl *ctrls;
1321 int i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001322 u32 id;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001323
Hans de Goedea0001a22009-06-10 04:52:18 -03001324 ctrls = NULL;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001325 id = q_ctrl->id;
1326 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1327 id &= V4L2_CTRL_ID_MASK;
1328 id++;
1329 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001330 if (gspca_dev->ctrl_dis & (1 << i))
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001331 continue;
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001332 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001333 continue;
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001334 if (ctrls && gspca_dev->sd_desc->ctrls[i].qctrl.id
Hans de Goedea0001a22009-06-10 04:52:18 -03001335 > ctrls->qctrl.id)
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001336 continue;
Hans de Goedea0001a22009-06-10 04:52:18 -03001337 ctrls = &gspca_dev->sd_desc->ctrls[i];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001338 }
Jean-Francois Moine83955552009-12-12 06:58:01 -03001339 if (ctrls == NULL)
1340 return -EINVAL;
Hans de Goedea0001a22009-06-10 04:52:18 -03001341 } else {
1342 ctrls = get_ctrl(gspca_dev, id);
Jean-Francois Moine83955552009-12-12 06:58:01 -03001343 if (ctrls == NULL)
1344 return -EINVAL;
Jean-Francois Moine4af85662009-11-12 15:59:27 -03001345 i = ctrls - gspca_dev->sd_desc->ctrls;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001346 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001347 memcpy(q_ctrl, ctrls, sizeof *q_ctrl);
Jean-Francois Moine4af85662009-11-12 15:59:27 -03001348 if (gspca_dev->ctrl_inac & (1 << i))
1349 q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001350 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001351}
1352
1353static int vidioc_s_ctrl(struct file *file, void *priv,
1354 struct v4l2_control *ctrl)
1355{
1356 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001357 const struct ctrl *ctrls;
Hans de Goedea0001a22009-06-10 04:52:18 -03001358 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001359
Hans de Goedea0001a22009-06-10 04:52:18 -03001360 ctrls = get_ctrl(gspca_dev, ctrl->id);
1361 if (ctrls == NULL)
1362 return -EINVAL;
1363
1364 if (ctrl->value < ctrls->qctrl.minimum
1365 || ctrl->value > ctrls->qctrl.maximum)
1366 return -ERANGE;
1367 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1368 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1369 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001370 gspca_dev->usb_err = 0;
Hans de Goedea0001a22009-06-10 04:52:18 -03001371 if (gspca_dev->present)
1372 ret = ctrls->set(gspca_dev, ctrl->value);
1373 else
1374 ret = -ENODEV;
1375 mutex_unlock(&gspca_dev->usb_lock);
1376 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001377}
1378
1379static int vidioc_g_ctrl(struct file *file, void *priv,
1380 struct v4l2_control *ctrl)
1381{
1382 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001383 const struct ctrl *ctrls;
Hans de Goedea0001a22009-06-10 04:52:18 -03001384 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001385
Hans de Goedea0001a22009-06-10 04:52:18 -03001386 ctrls = get_ctrl(gspca_dev, ctrl->id);
1387 if (ctrls == NULL)
1388 return -EINVAL;
1389
1390 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1391 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001392 gspca_dev->usb_err = 0;
Hans de Goedea0001a22009-06-10 04:52:18 -03001393 if (gspca_dev->present)
1394 ret = ctrls->get(gspca_dev, &ctrl->value);
1395 else
1396 ret = -ENODEV;
1397 mutex_unlock(&gspca_dev->usb_lock);
1398 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001399}
1400
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03001401/*fixme: have an audio flag in gspca_dev?*/
1402static int vidioc_s_audio(struct file *file, void *priv,
1403 struct v4l2_audio *audio)
1404{
1405 if (audio->index != 0)
1406 return -EINVAL;
1407 return 0;
1408}
1409
1410static int vidioc_g_audio(struct file *file, void *priv,
1411 struct v4l2_audio *audio)
1412{
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03001413 strcpy(audio->name, "Microphone");
1414 return 0;
1415}
1416
1417static int vidioc_enumaudio(struct file *file, void *priv,
1418 struct v4l2_audio *audio)
1419{
1420 if (audio->index != 0)
1421 return -EINVAL;
1422
1423 strcpy(audio->name, "Microphone");
1424 audio->capability = 0;
1425 audio->mode = 0;
1426 return 0;
1427}
1428
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001429static int vidioc_querymenu(struct file *file, void *priv,
1430 struct v4l2_querymenu *qmenu)
1431{
1432 struct gspca_dev *gspca_dev = priv;
1433
1434 if (!gspca_dev->sd_desc->querymenu)
1435 return -EINVAL;
1436 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1437}
1438
1439static int vidioc_enum_input(struct file *file, void *priv,
1440 struct v4l2_input *input)
1441{
1442 struct gspca_dev *gspca_dev = priv;
1443
1444 if (input->index != 0)
1445 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001446 input->type = V4L2_INPUT_TYPE_CAMERA;
Adam Bakerdfa76fa2009-03-29 19:17:10 -03001447 input->status = gspca_dev->cam.input_flags;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001448 strncpy(input->name, gspca_dev->sd_desc->name,
1449 sizeof input->name);
1450 return 0;
1451}
1452
1453static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1454{
1455 *i = 0;
1456 return 0;
1457}
1458
1459static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1460{
1461 if (i > 0)
1462 return -EINVAL;
1463 return (0);
1464}
1465
1466static int vidioc_reqbufs(struct file *file, void *priv,
1467 struct v4l2_requestbuffers *rb)
1468{
1469 struct gspca_dev *gspca_dev = priv;
Jean-François Moine832d0a92010-04-09 06:07:58 -03001470 int i, ret = 0, streaming;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001471
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001472 switch (rb->memory) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001473 case GSPCA_MEMORY_READ: /* (internal call) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001474 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001475 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001476 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001477 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001478 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001479 }
Hans de Goedee2997a72008-04-23 08:09:12 -03001480 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1481 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001482
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001483 if (gspca_dev->memory != GSPCA_MEMORY_NO
1484 && gspca_dev->memory != rb->memory) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001485 ret = -EBUSY;
1486 goto out;
1487 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001488
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001489 /* only one file may do the capture */
1490 if (gspca_dev->capt_file != NULL
1491 && gspca_dev->capt_file != file) {
1492 ret = -EBUSY;
1493 goto out;
1494 }
1495
1496 /* if allocated, the buffers must not be mapped */
1497 for (i = 0; i < gspca_dev->nframes; i++) {
1498 if (gspca_dev->frame[i].vma_use_count) {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001499 ret = -EBUSY;
1500 goto out;
1501 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001502 }
1503
1504 /* stop streaming */
Jean-François Moine832d0a92010-04-09 06:07:58 -03001505 streaming = gspca_dev->streaming;
1506 if (streaming) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001507 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001508 gspca_dev->usb_err = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001509 gspca_stream_off(gspca_dev);
1510 mutex_unlock(&gspca_dev->usb_lock);
1511 }
1512
1513 /* free the previous allocated buffers, if any */
1514 if (gspca_dev->nframes != 0) {
1515 frame_free(gspca_dev);
1516 gspca_dev->capt_file = NULL;
1517 }
1518 if (rb->count == 0) /* unrequest */
1519 goto out;
1520 gspca_dev->memory = rb->memory;
1521 ret = frame_alloc(gspca_dev, rb->count);
1522 if (ret == 0) {
1523 rb->count = gspca_dev->nframes;
1524 gspca_dev->capt_file = file;
Jean-François Moine832d0a92010-04-09 06:07:58 -03001525 if (streaming)
1526 ret = gspca_init_transfer(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001527 }
1528out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001529 mutex_unlock(&gspca_dev->queue_lock);
1530 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1531 return ret;
1532}
1533
1534static int vidioc_querybuf(struct file *file, void *priv,
1535 struct v4l2_buffer *v4l2_buf)
1536{
1537 struct gspca_dev *gspca_dev = priv;
1538 struct gspca_frame *frame;
1539
Jean-Francois Moine13752bd2008-12-19 15:08:51 -03001540 if (v4l2_buf->index < 0
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001541 || v4l2_buf->index >= gspca_dev->nframes)
1542 return -EINVAL;
1543
1544 frame = &gspca_dev->frame[v4l2_buf->index];
1545 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1546 return 0;
1547}
1548
1549static int vidioc_streamon(struct file *file, void *priv,
1550 enum v4l2_buf_type buf_type)
1551{
1552 struct gspca_dev *gspca_dev = priv;
1553 int ret;
1554
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001555 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1556 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001557 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1558 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001559
Jean-François Moine18bf99a2010-04-09 06:06:11 -03001560 /* check the capture file */
1561 if (gspca_dev->capt_file != file) {
1562 ret = -EBUSY;
1563 goto out;
1564 }
1565
Jean-Francois Moine1edabe72008-12-19 15:17:25 -03001566 if (gspca_dev->nframes == 0
1567 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001568 ret = -EINVAL;
1569 goto out;
1570 }
1571 if (!gspca_dev->streaming) {
1572 ret = gspca_init_transfer(gspca_dev);
1573 if (ret < 0)
1574 goto out;
1575 }
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001576#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001577 if (gspca_debug & D_STREAM) {
1578 PDEBUG_MODE("stream on OK",
1579 gspca_dev->pixfmt,
1580 gspca_dev->width,
1581 gspca_dev->height);
1582 }
1583#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001584 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001585out:
1586 mutex_unlock(&gspca_dev->queue_lock);
1587 return ret;
1588}
1589
1590static int vidioc_streamoff(struct file *file, void *priv,
1591 enum v4l2_buf_type buf_type)
1592{
1593 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001594 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001595
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001596 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1597 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001598 if (!gspca_dev->streaming)
1599 return 0;
1600 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1601 return -ERESTARTSYS;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001602
Jean-François Moine18bf99a2010-04-09 06:06:11 -03001603 /* check the capture file */
1604 if (gspca_dev->capt_file != file) {
1605 ret = -EBUSY;
1606 goto out;
1607 }
1608
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001609 /* stop streaming */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001610 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1611 ret = -ERESTARTSYS;
1612 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001613 }
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001614 gspca_dev->usb_err = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001615 gspca_stream_off(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001616 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001617
1618 /* empty the application queues */
1619 for (i = 0; i < gspca_dev->nframes; i++)
1620 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1621 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1622 gspca_dev->last_packet_type = DISCARD_PACKET;
1623 gspca_dev->sequence = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001624 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001625out:
1626 mutex_unlock(&gspca_dev->queue_lock);
1627 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001628}
1629
1630static int vidioc_g_jpegcomp(struct file *file, void *priv,
1631 struct v4l2_jpegcompression *jpegcomp)
1632{
1633 struct gspca_dev *gspca_dev = priv;
1634 int ret;
1635
1636 if (!gspca_dev->sd_desc->get_jcomp)
1637 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001638 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1639 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001640 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001641 if (gspca_dev->present)
1642 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1643 else
1644 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001645 mutex_unlock(&gspca_dev->usb_lock);
1646 return ret;
1647}
1648
1649static int vidioc_s_jpegcomp(struct file *file, void *priv,
1650 struct v4l2_jpegcompression *jpegcomp)
1651{
1652 struct gspca_dev *gspca_dev = priv;
1653 int ret;
1654
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001655 if (!gspca_dev->sd_desc->set_jcomp)
1656 return -EINVAL;
Jim Parisf86d4a92008-12-10 05:50:44 -03001657 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1658 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001659 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001660 if (gspca_dev->present)
1661 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1662 else
1663 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001664 mutex_unlock(&gspca_dev->usb_lock);
1665 return ret;
1666}
1667
1668static int vidioc_g_parm(struct file *filp, void *priv,
1669 struct v4l2_streamparm *parm)
1670{
1671 struct gspca_dev *gspca_dev = priv;
1672
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001673 parm->parm.capture.readbuffers = gspca_dev->nbufread;
Jim Paris627a5ef2008-12-10 06:02:42 -03001674
1675 if (gspca_dev->sd_desc->get_streamparm) {
1676 int ret;
1677
1678 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1679 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001680 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001681 if (gspca_dev->present)
1682 ret = gspca_dev->sd_desc->get_streamparm(gspca_dev,
1683 parm);
1684 else
1685 ret = -ENODEV;
Jim Paris627a5ef2008-12-10 06:02:42 -03001686 mutex_unlock(&gspca_dev->usb_lock);
1687 return ret;
1688 }
1689
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001690 return 0;
1691}
1692
1693static int vidioc_s_parm(struct file *filp, void *priv,
1694 struct v4l2_streamparm *parm)
1695{
1696 struct gspca_dev *gspca_dev = priv;
1697 int n;
1698
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001699 n = parm->parm.capture.readbuffers;
1700 if (n == 0 || n > GSPCA_MAX_FRAMES)
1701 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1702 else
1703 gspca_dev->nbufread = n;
Jim Paris627a5ef2008-12-10 06:02:42 -03001704
1705 if (gspca_dev->sd_desc->set_streamparm) {
1706 int ret;
1707
1708 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1709 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001710 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001711 if (gspca_dev->present)
1712 ret = gspca_dev->sd_desc->set_streamparm(gspca_dev,
1713 parm);
1714 else
1715 ret = -ENODEV;
Jim Paris627a5ef2008-12-10 06:02:42 -03001716 mutex_unlock(&gspca_dev->usb_lock);
1717 return ret;
1718 }
1719
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001720 return 0;
1721}
1722
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001723static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1724{
1725 struct gspca_dev *gspca_dev = file->private_data;
Harvey Harrisonefab8212008-07-05 06:12:47 -03001726 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001727 struct page *page;
1728 unsigned long addr, start, size;
1729 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001730
1731 start = vma->vm_start;
1732 size = vma->vm_end - vma->vm_start;
1733 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1734
Hans de Goedee2997a72008-04-23 08:09:12 -03001735 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1736 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001737 if (!gspca_dev->present) {
1738 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001739 goto out;
1740 }
1741 if (gspca_dev->capt_file != file) {
1742 ret = -EINVAL;
1743 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001744 }
1745
Harvey Harrisonefab8212008-07-05 06:12:47 -03001746 frame = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001747 for (i = 0; i < gspca_dev->nframes; ++i) {
1748 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1749 PDEBUG(D_STREAM, "mmap bad memory type");
1750 break;
1751 }
1752 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1753 == vma->vm_pgoff) {
1754 frame = &gspca_dev->frame[i];
1755 break;
1756 }
1757 }
Harvey Harrisonefab8212008-07-05 06:12:47 -03001758 if (frame == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001759 PDEBUG(D_STREAM, "mmap no frame buffer found");
1760 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001761 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001762 }
Jean-François Moine38bff692010-06-24 05:19:58 -03001763 if (size != frame->v4l2_buf.length) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001764 PDEBUG(D_STREAM, "mmap bad size");
1765 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001766 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001767 }
1768
1769 /*
1770 * - VM_IO marks the area as being a mmaped region for I/O to a
1771 * device. It also prevents the region from being core dumped.
1772 */
1773 vma->vm_flags |= VM_IO;
1774
1775 addr = (unsigned long) frame->data;
1776 while (size > 0) {
1777 page = vmalloc_to_page((void *) addr);
1778 ret = vm_insert_page(vma, start, page);
1779 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001780 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001781 start += PAGE_SIZE;
1782 addr += PAGE_SIZE;
1783 size -= PAGE_SIZE;
1784 }
1785
Jean-Francois Moined50e2572009-11-26 15:36:40 -03001786 vma->vm_ops = &gspca_vm_ops;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001787 vma->vm_private_data = frame;
1788 gspca_vm_open(vma);
Hans de Goedee2997a72008-04-23 08:09:12 -03001789 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001790out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001791 mutex_unlock(&gspca_dev->queue_lock);
1792 return ret;
1793}
1794
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001795/*
1796 * wait for a video frame
1797 *
1798 * If a frame is ready, its index is returned.
1799 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001800static int frame_wait(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001801 int nonblock_ing)
1802{
1803 struct gspca_frame *frame;
1804 int i, j, ret;
1805
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001806 /* check if a frame is ready */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001807 i = gspca_dev->fr_o;
1808 j = gspca_dev->fr_queue[i];
1809 frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001810
Hans de Goede181b7042008-11-19 17:16:26 -03001811 if (!(frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)) {
1812 if (nonblock_ing)
1813 return -EAGAIN;
1814
1815 /* wait till a frame is ready */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001816 ret = wait_event_interruptible_timeout(gspca_dev->wq,
Hans de Goede181b7042008-11-19 17:16:26 -03001817 (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) ||
1818 !gspca_dev->streaming || !gspca_dev->present,
1819 msecs_to_jiffies(3000));
1820 if (ret < 0)
1821 return ret;
1822 if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present)
Hans de Goedee2997a72008-04-23 08:09:12 -03001823 return -EIO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001824 }
Hans de Goede181b7042008-11-19 17:16:26 -03001825
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001826 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1827 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1828 gspca_dev->fr_q,
1829 gspca_dev->fr_i,
1830 gspca_dev->fr_o);
Hans de Goedee2997a72008-04-23 08:09:12 -03001831
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001832 if (gspca_dev->sd_desc->dq_callback) {
1833 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001834 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001835 if (gspca_dev->present)
1836 gspca_dev->sd_desc->dq_callback(gspca_dev);
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001837 mutex_unlock(&gspca_dev->usb_lock);
1838 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001839 return j;
1840}
1841
1842/*
1843 * dequeue a video buffer
1844 *
1845 * If nonblock_ing is false, block until a buffer is available.
1846 */
1847static int vidioc_dqbuf(struct file *file, void *priv,
1848 struct v4l2_buffer *v4l2_buf)
1849{
1850 struct gspca_dev *gspca_dev = priv;
1851 struct gspca_frame *frame;
1852 int i, ret;
1853
1854 PDEBUG(D_FRAM, "dqbuf");
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001855 if (v4l2_buf->memory != gspca_dev->memory)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001856 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001857
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001858 if (!gspca_dev->present)
1859 return -ENODEV;
1860
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001861 /* if not streaming, be sure the application will not loop forever */
1862 if (!(file->f_flags & O_NONBLOCK)
1863 && !gspca_dev->streaming && gspca_dev->users == 1)
1864 return -EINVAL;
1865
1866 /* only the capturing file may dequeue */
1867 if (gspca_dev->capt_file != file)
1868 return -EINVAL;
1869
1870 /* only one dequeue / read at a time */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001871 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1872 return -ERESTARTSYS;
1873
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001874 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001875 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001876 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001877 i = ret; /* frame index */
1878 frame = &gspca_dev->frame[i];
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001879 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001880 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001881 frame->data,
1882 frame->v4l2_buf.bytesused)) {
1883 PDEBUG(D_ERR|D_STREAM,
1884 "dqbuf cp to user failed");
1885 ret = -EFAULT;
1886 goto out;
1887 }
1888 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001889 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1890 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1891 PDEBUG(D_FRAM, "dqbuf %d", i);
1892 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001893out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001894 mutex_unlock(&gspca_dev->read_lock);
1895 return ret;
1896}
1897
1898/*
1899 * queue a video buffer
1900 *
1901 * Attempting to queue a buffer that has already been
1902 * queued will return -EINVAL.
1903 */
1904static int vidioc_qbuf(struct file *file, void *priv,
1905 struct v4l2_buffer *v4l2_buf)
1906{
1907 struct gspca_dev *gspca_dev = priv;
1908 struct gspca_frame *frame;
1909 int i, index, ret;
1910
1911 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001912
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001913 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1914 return -ERESTARTSYS;
1915
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001916 index = v4l2_buf->index;
1917 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001918 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001919 "qbuf idx %d >= %d", index, gspca_dev->nframes);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001920 ret = -EINVAL;
1921 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001922 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001923 if (v4l2_buf->memory != gspca_dev->memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001924 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001925 ret = -EINVAL;
1926 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001927 }
1928
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001929 frame = &gspca_dev->frame[index];
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001930 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1931 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001932 ret = -EINVAL;
1933 goto out;
1934 }
1935
1936 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001937
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001938 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001939 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1940 frame->v4l2_buf.length = v4l2_buf->length;
1941 }
1942
1943 /* put the buffer in the 'queued' queue */
1944 i = gspca_dev->fr_q;
1945 gspca_dev->fr_queue[i] = index;
Jean-Francois Moine8c32aa52009-12-14 14:57:49 -03001946 if (gspca_dev->fr_i == i)
Jean-François Moineb192ca92010-06-27 03:08:19 -03001947 gspca_dev->image = frame->data;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001948 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1949 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1950 gspca_dev->fr_q,
1951 gspca_dev->fr_i,
1952 gspca_dev->fr_o);
1953
1954 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1955 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1956 ret = 0;
1957out:
1958 mutex_unlock(&gspca_dev->queue_lock);
1959 return ret;
1960}
1961
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001962/*
1963 * allocate the resources for read()
1964 */
1965static int read_alloc(struct gspca_dev *gspca_dev,
1966 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001967{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001968 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001969 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001970
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001971 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001972 if (gspca_dev->nframes == 0) {
1973 struct v4l2_requestbuffers rb;
1974
1975 memset(&rb, 0, sizeof rb);
1976 rb.count = gspca_dev->nbufread;
1977 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001978 rb.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001979 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1980 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001981 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001982 return ret;
1983 }
1984 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1985 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001986 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001987 for (i = 0; i < gspca_dev->nbufread; i++) {
1988 v4l2_buf.index = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001989 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1990 if (ret != 0) {
1991 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1992 return ret;
1993 }
1994 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001995 gspca_dev->memory = GSPCA_MEMORY_READ;
1996 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001997
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001998 /* start streaming */
1999 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2000 if (ret != 0)
2001 PDEBUG(D_STREAM, "read streamon err %d", ret);
2002 return ret;
2003}
2004
2005static unsigned int dev_poll(struct file *file, poll_table *wait)
2006{
2007 struct gspca_dev *gspca_dev = file->private_data;
2008 int i, ret;
2009
2010 PDEBUG(D_FRAM, "poll");
2011
2012 poll_wait(file, &gspca_dev->wq, wait);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002013
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002014 /* if reqbufs is not done, the user would use read() */
2015 if (gspca_dev->nframes == 0) {
2016 if (gspca_dev->memory != GSPCA_MEMORY_NO)
2017 return POLLERR; /* not the 1st time */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002018 ret = read_alloc(gspca_dev, file);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002019 if (ret != 0)
2020 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002021 }
2022
2023 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
2024 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002025
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002026 /* check the next incoming buffer */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002027 i = gspca_dev->fr_o;
2028 i = gspca_dev->fr_queue[i];
2029 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
2030 ret = POLLIN | POLLRDNORM; /* something to read */
2031 else
2032 ret = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002033 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002034 if (!gspca_dev->present)
2035 return POLLHUP;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002036 return ret;
2037}
2038
2039static ssize_t dev_read(struct file *file, char __user *data,
2040 size_t count, loff_t *ppos)
2041{
2042 struct gspca_dev *gspca_dev = file->private_data;
2043 struct gspca_frame *frame;
2044 struct v4l2_buffer v4l2_buf;
2045 struct timeval timestamp;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002046 int n, ret, ret2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002047
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03002048 PDEBUG(D_FRAM, "read (%zd)", count);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002049 if (!gspca_dev->present)
2050 return -ENODEV;
2051 switch (gspca_dev->memory) {
2052 case GSPCA_MEMORY_NO: /* first time */
2053 ret = read_alloc(gspca_dev, file);
2054 if (ret != 0)
2055 return ret;
2056 break;
2057 case GSPCA_MEMORY_READ:
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002058 if (gspca_dev->capt_file == file)
2059 break;
2060 /* fall thru */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002061 default:
2062 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002063 }
2064
2065 /* get a frame */
Paulo Assis2f51d002010-03-07 06:56:56 -03002066 timestamp = ktime_to_timeval(ktime_get());
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002067 timestamp.tv_sec--;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002068 n = 2;
2069 for (;;) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002070 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2071 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002072 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002073 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
2074 if (ret != 0) {
2075 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
2076 return ret;
2077 }
2078
2079 /* if the process slept for more than 1 second,
Frank Zago219423c2008-09-26 07:43:54 -03002080 * get a newer frame */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002081 frame = &gspca_dev->frame[v4l2_buf.index];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002082 if (--n < 0)
2083 break; /* avoid infinite loop */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002084 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
2085 break;
2086 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2087 if (ret != 0) {
2088 PDEBUG(D_STREAM, "read qbuf err %d", ret);
2089 return ret;
2090 }
2091 }
2092
2093 /* copy the frame */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002094 if (count > frame->v4l2_buf.bytesused)
2095 count = frame->v4l2_buf.bytesused;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002096 ret = copy_to_user(data, frame->data, count);
2097 if (ret != 0) {
2098 PDEBUG(D_ERR|D_STREAM,
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03002099 "read cp to user lack %d / %zd", ret, count);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002100 ret = -EFAULT;
2101 goto out;
2102 }
2103 ret = count;
2104out:
2105 /* in each case, requeue the buffer */
2106 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2107 if (ret2 != 0)
2108 return ret2;
2109 return ret;
2110}
2111
Hans Verkuilbec43662008-12-30 06:58:20 -03002112static struct v4l2_file_operations dev_fops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002113 .owner = THIS_MODULE,
2114 .open = dev_open,
2115 .release = dev_close,
2116 .read = dev_read,
2117 .mmap = dev_mmap,
Hans Verkuilbec43662008-12-30 06:58:20 -03002118 .unlocked_ioctl = video_ioctl2,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002119 .poll = dev_poll,
2120};
2121
Hans Verkuila3998102008-07-21 02:57:38 -03002122static const struct v4l2_ioctl_ops dev_ioctl_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002123 .vidioc_querycap = vidioc_querycap,
2124 .vidioc_dqbuf = vidioc_dqbuf,
2125 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03002126 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2127 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2128 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2129 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002130 .vidioc_streamon = vidioc_streamon,
2131 .vidioc_queryctrl = vidioc_queryctrl,
2132 .vidioc_g_ctrl = vidioc_g_ctrl,
2133 .vidioc_s_ctrl = vidioc_s_ctrl,
Jean-Francois Moinef955c4f2008-11-28 08:51:50 -03002134 .vidioc_g_audio = vidioc_g_audio,
2135 .vidioc_s_audio = vidioc_s_audio,
2136 .vidioc_enumaudio = vidioc_enumaudio,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002137 .vidioc_querymenu = vidioc_querymenu,
2138 .vidioc_enum_input = vidioc_enum_input,
2139 .vidioc_g_input = vidioc_g_input,
2140 .vidioc_s_input = vidioc_s_input,
2141 .vidioc_reqbufs = vidioc_reqbufs,
2142 .vidioc_querybuf = vidioc_querybuf,
2143 .vidioc_streamoff = vidioc_streamoff,
2144 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
2145 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
2146 .vidioc_g_parm = vidioc_g_parm,
2147 .vidioc_s_parm = vidioc_s_parm,
Hans de Goede11c635a2009-05-25 15:04:22 -03002148 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Antonio Ospite28ffe772009-12-02 06:18:46 -03002149 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03002150#ifdef CONFIG_VIDEO_ADV_DEBUG
2151 .vidioc_g_register = vidioc_g_register,
2152 .vidioc_s_register = vidioc_s_register,
2153#endif
2154 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002155};
2156
Hans Verkuila3998102008-07-21 02:57:38 -03002157static struct video_device gspca_template = {
2158 .name = "gspca main driver",
Hans Verkuila3998102008-07-21 02:57:38 -03002159 .fops = &dev_fops,
2160 .ioctl_ops = &dev_ioctl_ops,
Hans de Goeded0182302008-11-19 17:10:47 -03002161 .release = gspca_release,
Hans Verkuila3998102008-07-21 02:57:38 -03002162};
2163
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002164/*
2165 * probe and create a new gspca device
2166 *
2167 * This function must be called by the sub-driver when it is
2168 * called for probing a new device.
2169 */
Jean-François Moine44628642010-06-05 07:47:36 -03002170int gspca_dev_probe2(struct usb_interface *intf,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002171 const struct usb_device_id *id,
2172 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002173 int dev_size,
2174 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002175{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002176 struct gspca_dev *gspca_dev;
2177 struct usb_device *dev = interface_to_usbdev(intf);
2178 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002179
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002180 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002181
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002182 /* create the device */
2183 if (dev_size < sizeof *gspca_dev)
2184 dev_size = sizeof *gspca_dev;
2185 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
Erik Andrene293e592008-10-03 08:46:50 -03002186 if (!gspca_dev) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002187 err("couldn't kzalloc gspca struct");
Erik Andrene293e592008-10-03 08:46:50 -03002188 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002189 }
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002190 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2191 if (!gspca_dev->usb_buf) {
2192 err("out of memory");
Erik Andrene293e592008-10-03 08:46:50 -03002193 ret = -ENOMEM;
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002194 goto out;
2195 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002196 gspca_dev->dev = dev;
Jean-François Moine44628642010-06-05 07:47:36 -03002197 gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002198 gspca_dev->nbalt = intf->num_altsetting;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002199 gspca_dev->sd_desc = sd_desc;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002200 gspca_dev->nbufread = 2;
Jean-Francois Moineff374742008-10-23 07:29:51 -03002201 gspca_dev->empty_packet = -1; /* don't check the empty packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002202
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002203 /* configure the subdriver and initialize the USB device */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002204 ret = sd_desc->config(gspca_dev, id);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002205 if (ret < 0)
2206 goto out;
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002207 ret = sd_desc->init(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002208 if (ret < 0)
2209 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002210 gspca_set_default_mode(gspca_dev);
2211
Hans de Goede10ee2402010-02-19 04:41:40 -03002212 ret = gspca_input_connect(gspca_dev);
2213 if (ret)
2214 goto out;
2215
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002216 mutex_init(&gspca_dev->usb_lock);
2217 mutex_init(&gspca_dev->read_lock);
2218 mutex_init(&gspca_dev->queue_lock);
2219 init_waitqueue_head(&gspca_dev->wq);
2220
2221 /* init video stuff */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002222 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
Hans de Goede24441632009-03-27 16:08:20 -03002223 gspca_dev->vdev.parent = &intf->dev;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03002224 gspca_dev->module = module;
Oliver Neukumee56a4d2008-07-27 14:01:59 -03002225 gspca_dev->present = 1;
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002226 ret = video_register_device(&gspca_dev->vdev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002227 VFL_TYPE_GRABBER,
Jean-Francois Moine8d584a52009-01-13 06:07:59 -03002228 -1);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002229 if (ret < 0) {
2230 err("video_register_device err %d", ret);
2231 goto out;
2232 }
2233
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002234 usb_set_intfdata(intf, gspca_dev);
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002235 PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
Márton Németh0274d422010-01-28 06:39:49 -03002236
Hans de Goede10ee2402010-02-19 04:41:40 -03002237 gspca_input_create_urb(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002238
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002239 return 0;
2240out:
Randy Dunlapf7628012010-03-30 14:32:04 -03002241#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Hans de Goede10ee2402010-02-19 04:41:40 -03002242 if (gspca_dev->input_dev)
2243 input_unregister_device(gspca_dev->input_dev);
Hans de Goedee7637522010-02-19 05:11:09 -03002244#endif
Jean-Francois Moinee07a1d82008-11-19 06:37:53 -03002245 kfree(gspca_dev->usb_buf);
2246 kfree(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002247 return ret;
2248}
Jean-François Moine44628642010-06-05 07:47:36 -03002249EXPORT_SYMBOL(gspca_dev_probe2);
2250
2251/* same function as the previous one, but check the interface */
2252int gspca_dev_probe(struct usb_interface *intf,
2253 const struct usb_device_id *id,
2254 const struct sd_desc *sd_desc,
2255 int dev_size,
2256 struct module *module)
2257{
2258 struct usb_device *dev = interface_to_usbdev(intf);
2259
2260 /* we don't handle multi-config cameras */
2261 if (dev->descriptor.bNumConfigurations != 1) {
2262 PDEBUG(D_ERR, "%04x:%04x too many config",
2263 id->idVendor, id->idProduct);
2264 return -ENODEV;
2265 }
2266
2267 /* the USB video interface must be the first one */
2268 if (dev->config->desc.bNumInterfaces != 1
2269 && intf->cur_altsetting->desc.bInterfaceNumber != 0) {
2270 PDEBUG(D_ERR, "%04x:%04x bad interface %d",
2271 id->idVendor, id->idProduct,
2272 intf->cur_altsetting->desc.bInterfaceNumber);
2273 return -ENODEV;
2274 }
2275
2276 return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
2277}
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002278EXPORT_SYMBOL(gspca_dev_probe);
2279
2280/*
2281 * USB disconnection
2282 *
2283 * This function must be called by the sub-driver
2284 * when the device disconnects, after the specific resources are freed.
2285 */
2286void gspca_disconnect(struct usb_interface *intf)
2287{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002288 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Randy Dunlapf7628012010-03-30 14:32:04 -03002289#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -03002290 struct input_dev *input_dev;
Jean-François Moinee070bf92010-02-18 15:02:51 -03002291#endif
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002292
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002293 PDEBUG(D_PROBE, "%s disconnect",
2294 video_device_node_name(&gspca_dev->vdev));
Adam Bakerad281272009-02-04 15:33:21 -03002295 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03002296 gspca_dev->present = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002297
2298 if (gspca_dev->streaming) {
2299 destroy_urbs(gspca_dev);
2300 wake_up_interruptible(&gspca_dev->wq);
2301 }
2302
Randy Dunlapf7628012010-03-30 14:32:04 -03002303#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -03002304 gspca_input_destroy_urb(gspca_dev);
2305 input_dev = gspca_dev->input_dev;
2306 if (input_dev) {
2307 gspca_dev->input_dev = NULL;
2308 input_unregister_device(input_dev);
2309 }
Jean-François Moinee070bf92010-02-18 15:02:51 -03002310#endif
Márton Németh0274d422010-01-28 06:39:49 -03002311
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002312 /* the device is freed at exit of this function */
2313 gspca_dev->dev = NULL;
Adam Bakerad281272009-02-04 15:33:21 -03002314 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03002315
Frank Zago6b060ff2008-09-28 08:12:22 -03002316 usb_set_intfdata(intf, NULL);
2317
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002318 /* release the device */
2319 /* (this will call gspca_release() immediatly or on last close) */
2320 video_unregister_device(&gspca_dev->vdev);
Jean-Francois Moine3145b8c2008-10-05 04:22:10 -03002321
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002322/* PDEBUG(D_PROBE, "disconnect complete"); */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002323}
2324EXPORT_SYMBOL(gspca_disconnect);
2325
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002326#ifdef CONFIG_PM
2327int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2328{
2329 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2330
2331 if (!gspca_dev->streaming)
2332 return 0;
2333 gspca_dev->frozen = 1; /* avoid urb error messages */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002334 if (gspca_dev->sd_desc->stopN)
2335 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002336 destroy_urbs(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002337 gspca_input_destroy_urb(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002338 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002339 if (gspca_dev->sd_desc->stop0)
2340 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002341 return 0;
2342}
2343EXPORT_SYMBOL(gspca_suspend);
2344
2345int gspca_resume(struct usb_interface *intf)
2346{
2347 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2348
2349 gspca_dev->frozen = 0;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002350 gspca_dev->sd_desc->init(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002351 gspca_input_create_urb(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002352 if (gspca_dev->streaming)
2353 return gspca_init_transfer(gspca_dev);
Jean-Francois Moine1e89e2da2008-09-03 17:12:15 -03002354 return 0;
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002355}
2356EXPORT_SYMBOL(gspca_resume);
2357#endif
Hans de Goededcef32372008-07-10 10:40:53 -03002358/* -- cam driver utility functions -- */
2359
2360/* auto gain and exposure algorithm based on the knee algorithm described here:
2361 http://ytse.tricolour.net/docs/LowLightOptimization.html
2362
2363 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2364 where changed. */
2365int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2366 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2367{
2368 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2369 const struct ctrl *gain_ctrl = NULL;
2370 const struct ctrl *exposure_ctrl = NULL;
2371 const struct ctrl *autogain_ctrl = NULL;
2372 int retval = 0;
2373
2374 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goede26984b02010-02-01 13:18:37 -03002375 if (gspca_dev->ctrl_dis & (1 << i))
2376 continue;
Hans de Goededcef32372008-07-10 10:40:53 -03002377 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2378 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2379 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2380 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2381 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2382 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2383 }
2384 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2385 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2386 "on cam without (auto)gain/exposure");
2387 return 0;
2388 }
2389
2390 if (gain_ctrl->get(gspca_dev, &gain) ||
2391 exposure_ctrl->get(gspca_dev, &exposure) ||
2392 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2393 return 0;
2394
2395 orig_gain = gain;
2396 orig_exposure = exposure;
2397
2398 /* If we are of a multiple of deadzone, do multiple steps to reach the
2399 desired lumination fast (with the risc of a slight overshoot) */
2400 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2401
Jean-Francois Moine1c44d812008-11-10 05:56:55 -03002402 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
Hans de Goededcef32372008-07-10 10:40:53 -03002403 avg_lum, desired_avg_lum, steps);
2404
2405 for (i = 0; i < steps; i++) {
2406 if (avg_lum > desired_avg_lum) {
2407 if (gain > gain_knee)
2408 gain--;
2409 else if (exposure > exposure_knee)
2410 exposure--;
2411 else if (gain > gain_ctrl->qctrl.default_value)
2412 gain--;
2413 else if (exposure > exposure_ctrl->qctrl.minimum)
2414 exposure--;
2415 else if (gain > gain_ctrl->qctrl.minimum)
2416 gain--;
2417 else
2418 break;
2419 } else {
2420 if (gain < gain_ctrl->qctrl.default_value)
2421 gain++;
2422 else if (exposure < exposure_knee)
2423 exposure++;
2424 else if (gain < gain_knee)
2425 gain++;
2426 else if (exposure < exposure_ctrl->qctrl.maximum)
2427 exposure++;
2428 else if (gain < gain_ctrl->qctrl.maximum)
2429 gain++;
2430 else
2431 break;
2432 }
2433 }
2434
2435 if (gain != orig_gain) {
2436 gain_ctrl->set(gspca_dev, gain);
2437 retval = 1;
2438 }
2439 if (exposure != orig_exposure) {
2440 exposure_ctrl->set(gspca_dev, exposure);
2441 retval = 1;
2442 }
2443
2444 return retval;
2445}
2446EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2447
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002448/* -- module insert / remove -- */
2449static int __init gspca_init(void)
2450{
Jean-Francois Moinebb64e862008-07-19 06:49:28 -03002451 info("main v%d.%d.%d registered",
2452 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2453 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2454 DRIVER_VERSION_NUMBER & 0xff);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002455 return 0;
2456}
2457static void __exit gspca_exit(void)
2458{
2459 info("main deregistered");
2460}
2461
2462module_init(gspca_init);
2463module_exit(gspca_exit);
2464
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03002465#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002466module_param_named(debug, gspca_debug, int, 0644);
2467MODULE_PARM_DESC(debug,
2468 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002469 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
2470 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002471#endif