blob: a3c2d36da0c860ab5c705a1f89e83425c75bbf56 [file] [log] [blame]
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001/*
2 * Main USB camera driver
3 *
Jean-François Moine59929ce2011-03-14 05:33:01 -03004 * Copyright (C) 2008-2011 Jean-François Moine <http://moinejf.free.fr>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03005 *
Márton Németh0274d422010-01-28 06:39:49 -03006 * Camera button input handling by Márton Németh
7 * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu>
8 *
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03009 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
Joe Perches133a9fe2011-08-21 19:56:57 -030024#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030026#define MODULE_NAME "gspca"
27
28#include <linux/init.h>
29#include <linux/fs.h>
30#include <linux/vmalloc.h>
31#include <linux/sched.h>
32#include <linux/slab.h>
33#include <linux/mm.h>
34#include <linux/string.h>
35#include <linux/pagemap.h>
Jean-Francois Moine956e42d2008-07-01 10:03:42 -030036#include <linux/io.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030037#include <asm/page.h>
Jean-Francois Moine956e42d2008-07-01 10:03:42 -030038#include <linux/uaccess.h>
Paulo Assis2f51d002010-03-07 06:56:56 -030039#include <linux/ktime.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030040#include <media/v4l2-ioctl.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030041
42#include "gspca.h"
43
Randy Dunlapf7628012010-03-30 14:32:04 -030044#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -030045#include <linux/input.h>
46#include <linux/usb/input.h>
Jean-François Moinee070bf92010-02-18 15:02:51 -030047#endif
Márton Németh0274d422010-01-28 06:39:49 -030048
Jean-Francois Moined43fa322008-06-12 10:58:58 -030049/* global values */
Jean-Francois Moine8fee8452009-01-17 04:46:38 -030050#define DEF_NURBS 3 /* default number of URBs */
51#if DEF_NURBS > MAX_NURBS
52#error "DEF_NURBS too big"
53#endif
Jean-Francois Moined43fa322008-06-12 10:58:58 -030054
Mauro Carvalho Chehabb0eaab762011-06-27 22:28:38 -030055#define DRIVER_VERSION_NUMBER "2.13.0"
56
Jean-François Moine27c6f9e2010-04-02 06:59:13 -030057MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030058MODULE_DESCRIPTION("GSPCA USB Camera Driver");
59MODULE_LICENSE("GPL");
Mauro Carvalho Chehabb0eaab762011-06-27 22:28:38 -030060MODULE_VERSION(DRIVER_VERSION_NUMBER);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030061
Jean-Francois Moine335b3f82008-07-30 04:53:02 -030062#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030063int gspca_debug = D_ERR | D_PROBE;
64EXPORT_SYMBOL(gspca_debug);
65
66static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
67{
68 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
69 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
70 txt,
71 pixfmt & 0xff,
72 (pixfmt >> 8) & 0xff,
73 (pixfmt >> 16) & 0xff,
74 pixfmt >> 24,
75 w, h);
76 } else {
77 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
78 txt,
79 pixfmt,
80 w, h);
81 }
82}
83#else
84#define PDEBUG_MODE(txt, pixfmt, w, h)
85#endif
86
Jean-Francois Moine77ee3312009-11-07 15:30:50 -030087/* specific memory types - !! should be different from V4L2_MEMORY_xxx */
Jean-Francois Moined43fa322008-06-12 10:58:58 -030088#define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
89#define GSPCA_MEMORY_READ 7
90
Jean-Francois Moined43fa322008-06-12 10:58:58 -030091#define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
Jean-Francois Moined43fa322008-06-12 10:58:58 -030092
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030093/*
94 * VMA operations.
95 */
96static void gspca_vm_open(struct vm_area_struct *vma)
97{
98 struct gspca_frame *frame = vma->vm_private_data;
99
100 frame->vma_use_count++;
101 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
102}
103
104static void gspca_vm_close(struct vm_area_struct *vma)
105{
106 struct gspca_frame *frame = vma->vm_private_data;
107
108 if (--frame->vma_use_count <= 0)
109 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
110}
111
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400112static const struct vm_operations_struct gspca_vm_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300113 .open = gspca_vm_open,
114 .close = gspca_vm_close,
115};
116
Márton Németh0274d422010-01-28 06:39:49 -0300117/*
118 * Input and interrupt endpoint handling functions
119 */
Randy Dunlapf7628012010-03-30 14:32:04 -0300120#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -0300121static void int_irq(struct urb *urb)
122{
123 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
124 int ret;
125
126 ret = urb->status;
127 switch (ret) {
128 case 0:
129 if (gspca_dev->sd_desc->int_pkt_scan(gspca_dev,
130 urb->transfer_buffer, urb->actual_length) < 0) {
131 PDEBUG(D_ERR, "Unknown packet received");
132 }
133 break;
134
135 case -ENOENT:
136 case -ECONNRESET:
137 case -ENODEV:
138 case -ESHUTDOWN:
139 /* Stop is requested either by software or hardware is gone,
140 * keep the ret value non-zero and don't resubmit later.
141 */
142 break;
143
144 default:
145 PDEBUG(D_ERR, "URB error %i, resubmitting", urb->status);
146 urb->status = 0;
147 ret = 0;
148 }
149
150 if (ret == 0) {
151 ret = usb_submit_urb(urb, GFP_ATOMIC);
152 if (ret < 0)
Joe Perches133a9fe2011-08-21 19:56:57 -0300153 pr_err("Resubmit URB failed with error %i\n", ret);
Márton Németh0274d422010-01-28 06:39:49 -0300154 }
155}
156
157static int gspca_input_connect(struct gspca_dev *dev)
158{
159 struct input_dev *input_dev;
160 int err = 0;
161
162 dev->input_dev = NULL;
Hans de Goedeac82f592010-02-19 04:28:39 -0300163 if (dev->sd_desc->int_pkt_scan || dev->sd_desc->other_input) {
Márton Németh0274d422010-01-28 06:39:49 -0300164 input_dev = input_allocate_device();
165 if (!input_dev)
166 return -ENOMEM;
167
168 usb_make_path(dev->dev, dev->phys, sizeof(dev->phys));
169 strlcat(dev->phys, "/input0", sizeof(dev->phys));
170
171 input_dev->name = dev->sd_desc->name;
172 input_dev->phys = dev->phys;
173
174 usb_to_input_id(dev->dev, &input_dev->id);
175
176 input_dev->evbit[0] = BIT_MASK(EV_KEY);
177 input_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
178 input_dev->dev.parent = &dev->dev->dev;
179
180 err = input_register_device(input_dev);
181 if (err) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300182 pr_err("Input device registration failed with error %i\n",
183 err);
Márton Németh0274d422010-01-28 06:39:49 -0300184 input_dev->dev.parent = NULL;
185 input_free_device(input_dev);
186 } else {
187 dev->input_dev = input_dev;
188 }
Hans de Goede10ee2402010-02-19 04:41:40 -0300189 }
Márton Németh0274d422010-01-28 06:39:49 -0300190
191 return err;
192}
193
194static int alloc_and_submit_int_urb(struct gspca_dev *gspca_dev,
195 struct usb_endpoint_descriptor *ep)
196{
197 unsigned int buffer_len;
198 int interval;
199 struct urb *urb;
200 struct usb_device *dev;
201 void *buffer = NULL;
202 int ret = -EINVAL;
203
Jean-François Moineb26d6e22010-04-29 06:01:53 -0300204 buffer_len = le16_to_cpu(ep->wMaxPacketSize);
Márton Németh0274d422010-01-28 06:39:49 -0300205 interval = ep->bInterval;
Jean-François Moinec4dc6922010-07-14 06:26:54 -0300206 PDEBUG(D_CONF, "found int in endpoint: 0x%x, "
Márton Németh0274d422010-01-28 06:39:49 -0300207 "buffer_len=%u, interval=%u",
208 ep->bEndpointAddress, buffer_len, interval);
209
210 dev = gspca_dev->dev;
211
212 urb = usb_alloc_urb(0, GFP_KERNEL);
213 if (!urb) {
214 ret = -ENOMEM;
215 goto error;
216 }
217
Linus Torvalds7a9b1492010-05-20 21:26:12 -0700218 buffer = usb_alloc_coherent(dev, buffer_len,
Márton Németh0274d422010-01-28 06:39:49 -0300219 GFP_KERNEL, &urb->transfer_dma);
220 if (!buffer) {
221 ret = -ENOMEM;
222 goto error_buffer;
223 }
224 usb_fill_int_urb(urb, dev,
225 usb_rcvintpipe(dev, ep->bEndpointAddress),
226 buffer, buffer_len,
227 int_irq, (void *)gspca_dev, interval);
Jason Wang882787f2010-09-03 06:57:19 -0300228 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Márton Németh0274d422010-01-28 06:39:49 -0300229 ret = usb_submit_urb(urb, GFP_KERNEL);
230 if (ret < 0) {
Jean-François Moinec4dc6922010-07-14 06:26:54 -0300231 PDEBUG(D_ERR, "submit int URB failed with error %i", ret);
Márton Németh0274d422010-01-28 06:39:49 -0300232 goto error_submit;
233 }
Hans de Goede2661da42010-10-26 05:16:32 -0300234 gspca_dev->int_urb = urb;
Márton Németh0274d422010-01-28 06:39:49 -0300235 return ret;
236
237error_submit:
Daniel Mack997ea582010-04-12 13:17:25 +0200238 usb_free_coherent(dev,
239 urb->transfer_buffer_length,
240 urb->transfer_buffer,
241 urb->transfer_dma);
Márton Németh0274d422010-01-28 06:39:49 -0300242error_buffer:
243 usb_free_urb(urb);
244error:
245 return ret;
246}
247
Hans de Goede10ee2402010-02-19 04:41:40 -0300248static void gspca_input_create_urb(struct gspca_dev *gspca_dev)
Márton Németh0274d422010-01-28 06:39:49 -0300249{
Márton Németh0274d422010-01-28 06:39:49 -0300250 struct usb_interface *intf;
251 struct usb_host_interface *intf_desc;
252 struct usb_endpoint_descriptor *ep;
253 int i;
254
255 if (gspca_dev->sd_desc->int_pkt_scan) {
256 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
257 intf_desc = intf->cur_altsetting;
258 for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
259 ep = &intf_desc->endpoint[i].desc;
260 if (usb_endpoint_dir_in(ep) &&
261 usb_endpoint_xfer_int(ep)) {
262
Hans de Goede10ee2402010-02-19 04:41:40 -0300263 alloc_and_submit_int_urb(gspca_dev, ep);
Márton Németh0274d422010-01-28 06:39:49 -0300264 break;
265 }
266 }
267 }
Márton Németh0274d422010-01-28 06:39:49 -0300268}
269
270static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
271{
272 struct urb *urb;
273
274 urb = gspca_dev->int_urb;
275 if (urb) {
276 gspca_dev->int_urb = NULL;
277 usb_kill_urb(urb);
Daniel Mack997ea582010-04-12 13:17:25 +0200278 usb_free_coherent(gspca_dev->dev,
279 urb->transfer_buffer_length,
280 urb->transfer_buffer,
281 urb->transfer_dma);
Márton Németh0274d422010-01-28 06:39:49 -0300282 usb_free_urb(urb);
283 }
284}
Randy Dunlapf7628012010-03-30 14:32:04 -0300285#else
286static inline void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
287{
288}
289
290static inline void gspca_input_create_urb(struct gspca_dev *gspca_dev)
291{
292}
293
294static inline int gspca_input_connect(struct gspca_dev *dev)
295{
296 return 0;
297}
Márton Németh0274d422010-01-28 06:39:49 -0300298#endif
299
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300300/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300301 * fill a video frame from an URB and resubmit
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300302 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300303static void fill_frame(struct gspca_dev *gspca_dev,
304 struct urb *urb)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300305{
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300306 u8 *data; /* address of data in the iso message */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300307 int i, len, st;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300308 cam_pkt_op pkt_scan;
309
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300310 if (urb->status != 0) {
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300311 if (urb->status == -ESHUTDOWN)
312 return; /* disconnection */
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300313#ifdef CONFIG_PM
Hans de Goede6a540bd2009-10-24 15:02:14 -0300314 if (gspca_dev->frozen)
315 return;
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300316#endif
Hans de Goede6a540bd2009-10-24 15:02:14 -0300317 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine81578522009-11-13 07:15:08 -0300318 urb->status = 0;
Hans de Goedebf926ad2009-10-23 06:56:06 -0300319 goto resubmit;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300320 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300321 pkt_scan = gspca_dev->sd_desc->pkt_scan;
322 for (i = 0; i < urb->number_of_packets; i++) {
Mauro Carvalho Chehab95f18be2010-12-27 12:00:03 -0300323 len = urb->iso_frame_desc[i].actual_length;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300324
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300325 /* check the packet status and length */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300326 st = urb->iso_frame_desc[i].status;
327 if (st) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300328 pr_err("ISOC data error: [%d] len=%d, status=%d\n",
329 i, len, st);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300330 gspca_dev->last_packet_type = DISCARD_PACKET;
331 continue;
332 }
Jean-François Moine76ebc162010-12-25 13:22:21 -0300333 if (len == 0) {
334 if (gspca_dev->empty_packet == 0)
335 gspca_dev->empty_packet = 1;
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)
Joe Perches133a9fe2011-08-21 19:56:57 -0300351 pr_err("usb_submit_urb() ret %d\n", st);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300352}
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)
Joe Perches133a9fe2011-08-21 19:56:57 -0300405 pr_err("usb_submit_urb() ret %d\n", st);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300406 }
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.
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300419 */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300420void gspca_frame_add(struct gspca_dev *gspca_dev,
421 enum gspca_packet_type packet_type,
422 const u8 *data,
423 int len)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300424{
Jean-Francois Moine3ec342f2009-11-13 07:38:16 -0300425 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300426 int i, j;
427
Hans de Goedee2997a72008-04-23 08:09:12 -0300428 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300429
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300430 if (packet_type == FIRST_PACKET) {
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300431 i = atomic_read(&gspca_dev->fr_i);
432
433 /* if there are no queued buffer, discard the whole frame */
434 if (i == atomic_read(&gspca_dev->fr_q)) {
435 gspca_dev->last_packet_type = DISCARD_PACKET;
Jean-François Moine2e0544a2010-10-16 13:59:13 -0300436 gspca_dev->sequence++;
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300437 return;
438 }
Jean-François Moineb192ca92010-06-27 03:08:19 -0300439 j = gspca_dev->fr_queue[i];
440 frame = &gspca_dev->frame[j];
Paulo Assis2f51d002010-03-07 06:56:56 -0300441 frame->v4l2_buf.timestamp = ktime_to_timeval(ktime_get());
Jean-François Moine2e0544a2010-10-16 13:59:13 -0300442 frame->v4l2_buf.sequence = gspca_dev->sequence++;
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300443 gspca_dev->image = frame->data;
Jean-François Moineb192ca92010-06-27 03:08:19 -0300444 gspca_dev->image_len = 0;
Jean-François Moinefe988f52010-07-29 02:46:02 -0300445 } else {
446 switch (gspca_dev->last_packet_type) {
447 case DISCARD_PACKET:
Hans de Goedefa4376d2011-06-05 03:44:43 -0300448 if (packet_type == LAST_PACKET) {
Jean-François Moinefe988f52010-07-29 02:46:02 -0300449 gspca_dev->last_packet_type = packet_type;
Hans de Goedefa4376d2011-06-05 03:44:43 -0300450 gspca_dev->image = NULL;
451 gspca_dev->image_len = 0;
452 }
Jean-François Moinefe988f52010-07-29 02:46:02 -0300453 return;
454 case LAST_PACKET:
455 return;
456 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300457 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300458
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300459 /* append the packet to the frame buffer */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300460 if (len > 0) {
Jean-François Moineb192ca92010-06-27 03:08:19 -0300461 if (gspca_dev->image_len + len > gspca_dev->frsz) {
462 PDEBUG(D_ERR|D_PACK, "frame overflow %d > %d",
463 gspca_dev->image_len + len,
464 gspca_dev->frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300465 packet_type = DISCARD_PACKET;
466 } else {
Jean-François Moinefe988f52010-07-29 02:46:02 -0300467/* !! image is NULL only when last pkt is LAST or DISCARD
468 if (gspca_dev->image == NULL) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300469 pr_err("gspca_frame_add() image == NULL\n");
Jean-François Moinefe988f52010-07-29 02:46:02 -0300470 return;
471 }
472 */
Jean-François Moineb192ca92010-06-27 03:08:19 -0300473 memcpy(gspca_dev->image + gspca_dev->image_len,
474 data, len);
475 gspca_dev->image_len += len;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300476 }
477 }
478 gspca_dev->last_packet_type = packet_type;
479
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300480 /* if last packet, invalidate packet concatenation until
481 * next first packet, wake up the application and advance
482 * in the queue */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300483 if (packet_type == LAST_PACKET) {
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300484 i = atomic_read(&gspca_dev->fr_i);
Jean-François Moineb192ca92010-06-27 03:08:19 -0300485 j = gspca_dev->fr_queue[i];
486 frame = &gspca_dev->frame[j];
487 frame->v4l2_buf.bytesused = gspca_dev->image_len;
Jean-François Moine02bbcb92010-07-06 04:16:40 -0300488 frame->v4l2_buf.flags = (frame->v4l2_buf.flags
489 | V4L2_BUF_FLAG_DONE)
490 & ~V4L2_BUF_FLAG_QUEUED;
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300491 i = (i + 1) % GSPCA_MAX_FRAMES;
492 atomic_set(&gspca_dev->fr_i, i);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300493 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300494 PDEBUG(D_FRAM, "frame complete len:%d",
495 frame->v4l2_buf.bytesused);
496 gspca_dev->image = NULL;
497 gspca_dev->image_len = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300498 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300499}
500EXPORT_SYMBOL(gspca_frame_add);
501
502static int gspca_is_compressed(__u32 format)
503{
504 switch (format) {
505 case V4L2_PIX_FMT_MJPEG:
506 case V4L2_PIX_FMT_JPEG:
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300507 case V4L2_PIX_FMT_SPCA561:
Hans de Goedeab8f12c2008-07-04 18:29:32 -0300508 case V4L2_PIX_FMT_PAC207:
Kyle Guinnd661e622009-01-16 05:36:14 -0300509 case V4L2_PIX_FMT_MR97310A:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300510 return 1;
511 }
512 return 0;
513}
514
Hans de Goeded642de22010-12-31 04:51:36 -0300515static int frame_alloc(struct gspca_dev *gspca_dev, struct file *file,
516 enum v4l2_memory memory, unsigned int count)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300517{
518 struct gspca_frame *frame;
519 unsigned int frsz;
520 int i;
521
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300522 i = gspca_dev->curr_mode;
523 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300524 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300525 frsz = PAGE_ALIGN(frsz);
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300526 if (count >= GSPCA_MAX_FRAMES)
527 count = GSPCA_MAX_FRAMES - 1;
Jean-François Moinee795d912010-06-24 05:14:22 -0300528 gspca_dev->frbuf = vmalloc_32(frsz * count);
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300529 if (!gspca_dev->frbuf) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300530 pr_err("frame alloc failed\n");
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300531 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300532 }
Hans de Goeded642de22010-12-31 04:51:36 -0300533 gspca_dev->capt_file = file;
534 gspca_dev->memory = memory;
535 gspca_dev->frsz = frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300536 gspca_dev->nframes = count;
537 for (i = 0; i < count; i++) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300538 frame = &gspca_dev->frame[i];
539 frame->v4l2_buf.index = i;
540 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
541 frame->v4l2_buf.flags = 0;
542 frame->v4l2_buf.field = V4L2_FIELD_NONE;
543 frame->v4l2_buf.length = frsz;
Hans de Goeded642de22010-12-31 04:51:36 -0300544 frame->v4l2_buf.memory = memory;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300545 frame->v4l2_buf.sequence = 0;
Jean-François Moineb192ca92010-06-27 03:08:19 -0300546 frame->data = gspca_dev->frbuf + i * frsz;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300547 frame->v4l2_buf.m.offset = i * frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300548 }
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300549 atomic_set(&gspca_dev->fr_q, 0);
550 atomic_set(&gspca_dev->fr_i, 0);
551 gspca_dev->fr_o = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300552 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300553}
554
555static void frame_free(struct gspca_dev *gspca_dev)
556{
557 int i;
558
559 PDEBUG(D_STREAM, "frame free");
Harvey Harrisonefab8212008-07-05 06:12:47 -0300560 if (gspca_dev->frbuf != NULL) {
Jean-François Moinee795d912010-06-24 05:14:22 -0300561 vfree(gspca_dev->frbuf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300562 gspca_dev->frbuf = NULL;
563 for (i = 0; i < gspca_dev->nframes; i++)
564 gspca_dev->frame[i].data = NULL;
565 }
566 gspca_dev->nframes = 0;
Hans de Goeded642de22010-12-31 04:51:36 -0300567 gspca_dev->frsz = 0;
568 gspca_dev->capt_file = NULL;
569 gspca_dev->memory = GSPCA_MEMORY_NO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300570}
571
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300572static void destroy_urbs(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300573{
574 struct urb *urb;
575 unsigned int i;
576
577 PDEBUG(D_STREAM, "kill transfer");
Erik Andrén44732812008-10-13 15:52:46 -0300578 for (i = 0; i < MAX_NURBS; i++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300579 urb = gspca_dev->urb[i];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300580 if (urb == NULL)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300581 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300582
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300583 gspca_dev->urb[i] = NULL;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300584 usb_kill_urb(urb);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300585 if (urb->transfer_buffer != NULL)
Daniel Mack997ea582010-04-12 13:17:25 +0200586 usb_free_coherent(gspca_dev->dev,
587 urb->transfer_buffer_length,
588 urb->transfer_buffer,
589 urb->transfer_dma);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300590 usb_free_urb(urb);
591 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300592}
593
Jean-François Moinee4dac282010-04-09 06:11:36 -0300594static int gspca_set_alt0(struct gspca_dev *gspca_dev)
595{
596 int ret;
597
598 if (gspca_dev->alt == 0)
599 return 0;
600 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
601 if (ret < 0)
Joe Perches133a9fe2011-08-21 19:56:57 -0300602 pr_err("set alt 0 err %d\n", ret);
Jean-François Moinee4dac282010-04-09 06:11:36 -0300603 return ret;
604}
605
606/* Note: both the queue and the usb locks should be held when calling this */
607static void gspca_stream_off(struct gspca_dev *gspca_dev)
608{
609 gspca_dev->streaming = 0;
610 if (gspca_dev->present) {
611 if (gspca_dev->sd_desc->stopN)
612 gspca_dev->sd_desc->stopN(gspca_dev);
613 destroy_urbs(gspca_dev);
614 gspca_input_destroy_urb(gspca_dev);
615 gspca_set_alt0(gspca_dev);
616 gspca_input_create_urb(gspca_dev);
617 }
618
619 /* always call stop0 to free the subdriver's resources */
620 if (gspca_dev->sd_desc->stop0)
621 gspca_dev->sd_desc->stop0(gspca_dev);
622 PDEBUG(D_STREAM, "stream off OK");
623}
624
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300625/*
Frank Zago219423c2008-09-26 07:43:54 -0300626 * look for an input transfer endpoint in an alternate setting
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300627 */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300628static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300629 int xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300630{
631 struct usb_host_endpoint *ep;
632 int i, attr;
633
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300634 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
635 ep = &alt->endpoint[i];
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300636 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Jean-Francois Moine30d35e42009-04-21 13:57:31 -0300637 if (attr == xfer
Patrice Chotard71062252011-03-21 16:58:56 -0300638 && ep->desc.wMaxPacketSize != 0
639 && usb_endpoint_dir_in(&ep->desc))
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300640 return ep;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300641 }
642 return NULL;
643}
644
645/*
Frank Zago219423c2008-09-26 07:43:54 -0300646 * look for an input (isoc or bulk) endpoint
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300647 *
648 * The endpoint is defined by the subdriver.
649 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
650 * This routine may be called many times when the bandwidth is too small
651 * (the bandwidth is checked on urb submit).
652 */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300653static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300654{
655 struct usb_interface *intf;
656 struct usb_host_endpoint *ep;
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300657 int xfer, i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300658
659 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300660 ep = NULL;
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300661 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
662 : USB_ENDPOINT_XFER_ISOC;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300663 i = gspca_dev->alt; /* previous alt setting */
Hans de Goedea511ba92009-10-16 07:13:07 -0300664 if (gspca_dev->cam.reverse_alts) {
665 while (++i < gspca_dev->nbalt) {
666 ep = alt_xfer(&intf->altsetting[i], xfer);
667 if (ep)
668 break;
669 }
670 } else {
671 while (--i >= 0) {
672 ep = alt_xfer(&intf->altsetting[i], xfer);
673 if (ep)
674 break;
675 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300676 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300677 if (ep == NULL) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300678 pr_err("no transfer endpoint found\n");
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300679 return NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300680 }
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300681 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300682 i, ep->desc.bEndpointAddress);
Jean-Francois Moine1c8336d2009-07-28 04:38:25 -0300683 gspca_dev->alt = i; /* memorize the current alt setting */
Jean-Francois Moine8dd07ef2009-04-21 14:05:44 -0300684 if (gspca_dev->nbalt > 1) {
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300685 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
686 if (ret < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300687 pr_err("set alt %d err %d\n", i, ret);
Márton Németh0274d422010-01-28 06:39:49 -0300688 ep = NULL;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300689 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300690 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300691 return ep;
692}
693
694/*
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300695 * create the URBs for image transfer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300696 */
697static int create_urbs(struct gspca_dev *gspca_dev,
698 struct usb_host_endpoint *ep)
699{
700 struct urb *urb;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300701 int n, nurbs, i, psize, npkt, bsize;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300702
703 /* calculate the packet size and the number of packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300704 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300705
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300706 if (!gspca_dev->cam.bulk) { /* isoc */
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300707
708 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300709 if (gspca_dev->pkt_size == 0)
710 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
711 else
712 psize = gspca_dev->pkt_size;
Jean-Francois Moine49cb6b02009-04-25 13:29:01 -0300713 npkt = gspca_dev->cam.npkt;
714 if (npkt == 0)
715 npkt = 32; /* default value */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300716 bsize = psize * npkt;
717 PDEBUG(D_STREAM,
718 "isoc %d pkts size %d = bsize:%d",
719 npkt, psize, bsize);
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300720 nurbs = DEF_NURBS;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300721 } else { /* bulk */
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300722 npkt = 0;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300723 bsize = gspca_dev->cam.bulk_size;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300724 if (bsize == 0)
725 bsize = psize;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300726 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300727 if (gspca_dev->cam.bulk_nurbs != 0)
728 nurbs = gspca_dev->cam.bulk_nurbs;
729 else
730 nurbs = 1;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300731 }
732
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300733 for (n = 0; n < nurbs; n++) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300734 urb = usb_alloc_urb(npkt, GFP_KERNEL);
735 if (!urb) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300736 pr_err("usb_alloc_urb failed\n");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300737 return -ENOMEM;
738 }
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300739 gspca_dev->urb[n] = urb;
Daniel Mack997ea582010-04-12 13:17:25 +0200740 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300741 bsize,
742 GFP_KERNEL,
743 &urb->transfer_dma);
744
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300745 if (urb->transfer_buffer == NULL) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300746 pr_err("usb_alloc_coherent failed\n");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300747 return -ENOMEM;
748 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300749 urb->dev = gspca_dev->dev;
750 urb->context = gspca_dev;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300751 urb->transfer_buffer_length = bsize;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300752 if (npkt != 0) { /* ISOC */
753 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
754 ep->desc.bEndpointAddress);
Frank Zago6c49da72008-09-28 07:43:00 -0300755 urb->transfer_flags = URB_ISO_ASAP
756 | URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300757 urb->interval = ep->desc.bInterval;
758 urb->complete = isoc_irq;
759 urb->number_of_packets = npkt;
760 for (i = 0; i < npkt; i++) {
761 urb->iso_frame_desc[i].length = psize;
762 urb->iso_frame_desc[i].offset = psize * i;
763 }
764 } else { /* bulk */
765 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
Jean-François Moinea23c1942010-10-29 07:57:03 -0300766 ep->desc.bEndpointAddress);
Frank Zago6c49da72008-09-28 07:43:00 -0300767 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300768 urb->complete = bulk_irq;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300769 }
770 }
771 return 0;
772}
773
774/*
775 * start the USB transfer
776 */
777static int gspca_init_transfer(struct gspca_dev *gspca_dev)
778{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300779 struct usb_host_endpoint *ep;
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300780 struct urb *urb;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300781 int n, ret;
782
Hans de Goedee2997a72008-04-23 08:09:12 -0300783 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
784 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300785
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300786 if (!gspca_dev->present) {
787 ret = -ENODEV;
Hans de Goede314b3e02010-10-26 05:15:46 -0300788 goto unlock;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300789 }
790
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300791 /* reset the streaming variables */
792 gspca_dev->image = NULL;
793 gspca_dev->image_len = 0;
794 gspca_dev->last_packet_type = DISCARD_PACKET;
795 gspca_dev->sequence = 0;
796
Jean-Francois Moineab6c8cf2010-01-13 15:34:27 -0300797 gspca_dev->usb_err = 0;
798
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300799 /* set the higher alternate setting and
800 * loop until urb submit succeeds */
Hans de Goedea511ba92009-10-16 07:13:07 -0300801 if (gspca_dev->cam.reverse_alts)
802 gspca_dev->alt = 0;
803 else
804 gspca_dev->alt = gspca_dev->nbalt;
805
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300806 if (gspca_dev->sd_desc->isoc_init) {
807 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
808 if (ret < 0)
Hans de Goede314b3e02010-10-26 05:15:46 -0300809 goto unlock;
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300810 }
Hans de Goede314b3e02010-10-26 05:15:46 -0300811
812 gspca_input_destroy_urb(gspca_dev);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300813 ep = get_ep(gspca_dev);
814 if (ep == NULL) {
815 ret = -EIO;
816 goto out;
817 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300818 for (;;) {
Jean-Francois Moine47aaca92009-12-15 05:23:04 -0300819 if (!gspca_dev->cam.no_urb_create) {
820 PDEBUG(D_STREAM, "init transfer alt %d",
821 gspca_dev->alt);
822 ret = create_urbs(gspca_dev, ep);
823 if (ret < 0) {
824 destroy_urbs(gspca_dev);
825 goto out;
826 }
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300827 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300828
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300829 /* clear the bulk endpoint */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300830 if (gspca_dev->cam.bulk)
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300831 usb_clear_halt(gspca_dev->dev,
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300832 gspca_dev->urb[0]->pipe);
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300833
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300834 /* start the cam */
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300835 ret = gspca_dev->sd_desc->start(gspca_dev);
836 if (ret < 0) {
837 destroy_urbs(gspca_dev);
838 goto out;
839 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300840 gspca_dev->streaming = 1;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300841
Antonio Ospitedff369a2008-11-14 07:53:32 -0300842 /* some bulk transfers are started by the subdriver */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300843 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300844 break;
845
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300846 /* submit the URBs */
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300847 for (n = 0; n < MAX_NURBS; n++) {
848 urb = gspca_dev->urb[n];
849 if (urb == NULL)
850 break;
851 ret = usb_submit_urb(urb, GFP_KERNEL);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300852 if (ret < 0)
853 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300854 }
855 if (ret >= 0)
856 break;
Jean-François Moinee4dac282010-04-09 06:11:36 -0300857 gspca_stream_off(gspca_dev);
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300858 if (ret != -ENOSPC) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300859 pr_err("usb_submit_urb alt %d err %d\n",
860 gspca_dev->alt, ret);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300861 goto out;
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300862 }
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300863
864 /* the bandwidth is not wide enough
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300865 * negotiate or try a lower alternate setting */
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300866 PDEBUG(D_ERR|D_STREAM,
867 "bandwidth not wide enough - trying again");
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300868 msleep(20); /* wait for kill complete */
869 if (gspca_dev->sd_desc->isoc_nego) {
870 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
871 if (ret < 0)
872 goto out;
873 } else {
874 ep = get_ep(gspca_dev);
875 if (ep == NULL) {
876 ret = -EIO;
877 goto out;
878 }
879 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300880 }
881out:
Hans de Goede314b3e02010-10-26 05:15:46 -0300882 gspca_input_create_urb(gspca_dev);
883unlock:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300884 mutex_unlock(&gspca_dev->usb_lock);
885 return ret;
886}
887
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300888static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300889{
Jean-François Moine6a330912010-10-02 04:12:25 -0300890 struct gspca_ctrl *ctrl;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300891 int i;
892
893 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
894 gspca_dev->curr_mode = i;
895 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
896 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300897 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-François Moine6a330912010-10-02 04:12:25 -0300898
899 /* set the current control values to their default values
900 * which may have changed in sd_init() */
901 ctrl = gspca_dev->cam.ctrls;
902 if (ctrl != NULL) {
903 for (i = 0;
904 i < gspca_dev->sd_desc->nctrls;
905 i++, ctrl++)
906 ctrl->val = ctrl->def;
907 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300908}
909
910static int wxh_to_mode(struct gspca_dev *gspca_dev,
911 int width, int height)
912{
913 int i;
914
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300915 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
916 if (width >= gspca_dev->cam.cam_mode[i].width
917 && height >= gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300918 break;
919 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300920 return i;
921}
922
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300923/*
924 * search a mode with the right pixel format
925 */
926static int gspca_get_mode(struct gspca_dev *gspca_dev,
927 int mode,
928 int pixfmt)
929{
930 int modeU, modeD;
931
932 modeU = modeD = mode;
933 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
934 if (--modeD >= 0) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300935 if (gspca_dev->cam.cam_mode[modeD].pixelformat
936 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300937 return modeD;
938 }
939 if (++modeU < gspca_dev->cam.nmodes) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300940 if (gspca_dev->cam.cam_mode[modeU].pixelformat
941 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300942 return modeU;
943 }
944 }
945 return -EINVAL;
946}
947
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300948#ifdef CONFIG_VIDEO_ADV_DEBUG
949static int vidioc_g_register(struct file *file, void *priv,
950 struct v4l2_dbg_register *reg)
951{
952 int ret;
953 struct gspca_dev *gspca_dev = priv;
954
955 if (!gspca_dev->sd_desc->get_chip_ident)
956 return -EINVAL;
957
958 if (!gspca_dev->sd_desc->get_register)
959 return -EINVAL;
960
961 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
962 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -0300963 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300964 if (gspca_dev->present)
965 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
966 else
967 ret = -ENODEV;
968 mutex_unlock(&gspca_dev->usb_lock);
969
970 return ret;
971}
972
973static int vidioc_s_register(struct file *file, void *priv,
974 struct v4l2_dbg_register *reg)
975{
976 int ret;
977 struct gspca_dev *gspca_dev = priv;
978
979 if (!gspca_dev->sd_desc->get_chip_ident)
980 return -EINVAL;
981
982 if (!gspca_dev->sd_desc->set_register)
983 return -EINVAL;
984
985 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
986 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -0300987 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300988 if (gspca_dev->present)
989 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
990 else
991 ret = -ENODEV;
992 mutex_unlock(&gspca_dev->usb_lock);
993
994 return ret;
995}
996#endif
997
998static int vidioc_g_chip_ident(struct file *file, void *priv,
999 struct v4l2_dbg_chip_ident *chip)
1000{
1001 int ret;
1002 struct gspca_dev *gspca_dev = priv;
1003
1004 if (!gspca_dev->sd_desc->get_chip_ident)
1005 return -EINVAL;
1006
1007 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1008 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001009 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03001010 if (gspca_dev->present)
1011 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
1012 else
1013 ret = -ENODEV;
1014 mutex_unlock(&gspca_dev->usb_lock);
1015
1016 return ret;
1017}
1018
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001019static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001020 struct v4l2_fmtdesc *fmtdesc)
1021{
1022 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001023 int i, j, index;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001024 __u32 fmt_tb[8];
1025
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001026 /* give an index to each format */
1027 index = 0;
1028 j = 0;
1029 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001030 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001031 j = 0;
1032 for (;;) {
1033 if (fmt_tb[j] == fmt_tb[index])
1034 break;
1035 j++;
1036 }
1037 if (j == index) {
1038 if (fmtdesc->index == index)
1039 break; /* new format */
1040 index++;
Julia Lawall80297122008-11-12 23:18:21 -03001041 if (index >= ARRAY_SIZE(fmt_tb))
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001042 return -EINVAL;
1043 }
1044 }
1045 if (i < 0)
1046 return -EINVAL; /* no more format */
1047
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001048 fmtdesc->pixelformat = fmt_tb[index];
1049 if (gspca_is_compressed(fmt_tb[index]))
1050 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
1051 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
1052 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
1053 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
1054 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
1055 fmtdesc->description[4] = '\0';
1056 return 0;
1057}
1058
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001059static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001060 struct v4l2_format *fmt)
1061{
1062 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001063 int mode;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001064
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001065 mode = gspca_dev->curr_mode;
1066 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 0;
1069}
1070
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001071static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001072 struct v4l2_format *fmt)
1073{
Hans de Goede24c530b2008-07-04 17:55:18 -03001074 int w, h, mode, mode2;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001075
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001076 w = fmt->fmt.pix.width;
1077 h = fmt->fmt.pix.height;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001078
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001079#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001080 if (gspca_debug & D_CONF)
1081 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
1082#endif
1083 /* search the closest mode for width and height */
1084 mode = wxh_to_mode(gspca_dev, w, h);
1085
1086 /* OK if right palette */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001087 if (gspca_dev->cam.cam_mode[mode].pixelformat
1088 != fmt->fmt.pix.pixelformat) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001089
1090 /* else, search the closest mode with the same pixel format */
1091 mode2 = gspca_get_mode(gspca_dev, mode,
1092 fmt->fmt.pix.pixelformat);
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001093 if (mode2 >= 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001094 mode = mode2;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001095/* else
1096 ; * no chance, return this mode */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001097 }
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001098 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1099 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001100 return mode; /* used when s_fmt */
1101}
1102
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001103static int vidioc_try_fmt_vid_cap(struct file *file,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001104 void *priv,
1105 struct v4l2_format *fmt)
1106{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001107 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001108 int ret;
1109
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001110 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001111 if (ret < 0)
1112 return ret;
1113 return 0;
1114}
1115
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001116static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001117 struct v4l2_format *fmt)
1118{
1119 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001120 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001121
Hans de Goedee2997a72008-04-23 08:09:12 -03001122 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1123 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001124
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001125 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001126 if (ret < 0)
1127 goto out;
1128
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001129 if (gspca_dev->nframes != 0
1130 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
1131 ret = -EINVAL;
1132 goto out;
1133 }
1134
Jean-Francois Moine50a871f2008-06-30 19:47:33 -03001135 if (ret == gspca_dev->curr_mode) {
1136 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001137 goto out; /* same mode */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -03001138 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001139
1140 if (gspca_dev->streaming) {
1141 ret = -EBUSY;
1142 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -03001143 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001144 gspca_dev->width = fmt->fmt.pix.width;
1145 gspca_dev->height = fmt->fmt.pix.height;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001146 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
1147 gspca_dev->curr_mode = ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001148
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001149 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001150out:
1151 mutex_unlock(&gspca_dev->queue_lock);
1152 return ret;
1153}
1154
Hans de Goede11c635a2009-05-25 15:04:22 -03001155static int vidioc_enum_framesizes(struct file *file, void *priv,
1156 struct v4l2_frmsizeenum *fsize)
1157{
1158 struct gspca_dev *gspca_dev = priv;
1159 int i;
1160 __u32 index = 0;
1161
1162 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
1163 if (fsize->pixel_format !=
1164 gspca_dev->cam.cam_mode[i].pixelformat)
1165 continue;
1166
1167 if (fsize->index == index) {
1168 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1169 fsize->discrete.width =
1170 gspca_dev->cam.cam_mode[i].width;
1171 fsize->discrete.height =
1172 gspca_dev->cam.cam_mode[i].height;
1173 return 0;
1174 }
1175 index++;
1176 }
1177
1178 return -EINVAL;
1179}
1180
Antonio Ospite28ffe772009-12-02 06:18:46 -03001181static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1182 struct v4l2_frmivalenum *fival)
1183{
1184 struct gspca_dev *gspca_dev = priv;
1185 int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
1186 __u32 i;
1187
1188 if (gspca_dev->cam.mode_framerates == NULL ||
1189 gspca_dev->cam.mode_framerates[mode].nrates == 0)
1190 return -EINVAL;
1191
1192 if (fival->pixel_format !=
1193 gspca_dev->cam.cam_mode[mode].pixelformat)
1194 return -EINVAL;
1195
1196 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1197 if (fival->index == i) {
1198 fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1199 fival->discrete.numerator = 1;
1200 fival->discrete.denominator =
1201 gspca_dev->cam.mode_framerates[mode].rates[i];
1202 return 0;
1203 }
1204 }
1205
1206 return -EINVAL;
1207}
1208
Hans de Goeded0182302008-11-19 17:10:47 -03001209static void gspca_release(struct video_device *vfd)
Frank Zago6b060ff2008-09-28 08:12:22 -03001210{
Hans de Goeded0182302008-11-19 17:10:47 -03001211 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
Frank Zago6b060ff2008-09-28 08:12:22 -03001212
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001213 PDEBUG(D_PROBE, "%s released",
1214 video_device_node_name(&gspca_dev->vdev));
Frank Zago6b060ff2008-09-28 08:12:22 -03001215
1216 kfree(gspca_dev->usb_buf);
1217 kfree(gspca_dev);
1218}
1219
Hans Verkuilbec43662008-12-30 06:58:20 -03001220static int dev_open(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001221{
1222 struct gspca_dev *gspca_dev;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001223
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001224 PDEBUG(D_STREAM, "[%s] open", current->comm);
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001225 gspca_dev = (struct gspca_dev *) video_devdata(file);
Hans de Goede7f6eb112010-12-30 20:20:09 -03001226 if (!gspca_dev->present)
1227 return -ENODEV;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03001228
1229 /* protect the subdriver against rmmod */
Hans de Goede7f6eb112010-12-30 20:20:09 -03001230 if (!try_module_get(gspca_dev->module))
1231 return -ENODEV;
Frank Zago6b060ff2008-09-28 08:12:22 -03001232
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001233 file->private_data = gspca_dev;
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001234#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001235 /* activate the v4l2 debug */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001236 if (gspca_debug & D_V4L2)
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001237 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001238 | V4L2_DEBUG_IOCTL_ARG;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001239 else
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001240 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001241 | V4L2_DEBUG_IOCTL_ARG);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001242#endif
Hans de Goede7f6eb112010-12-30 20:20:09 -03001243 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001244}
1245
Hans Verkuilbec43662008-12-30 06:58:20 -03001246static int dev_close(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001247{
1248 struct gspca_dev *gspca_dev = file->private_data;
1249
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001250 PDEBUG(D_STREAM, "[%s] close", current->comm);
Hans de Goedee2997a72008-04-23 08:09:12 -03001251 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1252 return -ERESTARTSYS;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001253
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001254 /* if the file did the capture, free the streaming resources */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001255 if (gspca_dev->capt_file == file) {
Jean-Francois Moinea1e82882008-09-03 17:12:17 -03001256 if (gspca_dev->streaming) {
1257 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001258 gspca_dev->usb_err = 0;
Jean-Francois Moinea1e82882008-09-03 17:12:17 -03001259 gspca_stream_off(gspca_dev);
1260 mutex_unlock(&gspca_dev->usb_lock);
1261 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001262 frame_free(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001263 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001264 file->private_data = NULL;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03001265 module_put(gspca_dev->module);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001266 mutex_unlock(&gspca_dev->queue_lock);
Frank Zago6b060ff2008-09-28 08:12:22 -03001267
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001268 PDEBUG(D_STREAM, "close done");
Frank Zago6b060ff2008-09-28 08:12:22 -03001269
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001270 return 0;
1271}
1272
1273static int vidioc_querycap(struct file *file, void *priv,
1274 struct v4l2_capability *cap)
1275{
1276 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001277 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001278
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001279 /* protect the access to the usb device */
1280 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1281 return -ERESTARTSYS;
1282 if (!gspca_dev->present) {
1283 ret = -ENODEV;
1284 goto out;
1285 }
Hans de Goedee7d712c2011-06-05 14:58:46 -03001286 strlcpy((char *) cap->driver, gspca_dev->sd_desc->name,
Jean-François Moined8518592010-12-28 06:55:01 -03001287 sizeof cap->driver);
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -03001288 if (gspca_dev->dev->product != NULL) {
Hans de Goedee7d712c2011-06-05 14:58:46 -03001289 strlcpy((char *) cap->card, gspca_dev->dev->product,
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -03001290 sizeof cap->card);
1291 } else {
Jean-François Moined8518592010-12-28 06:55:01 -03001292 snprintf((char *) cap->card, sizeof cap->card,
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -03001293 "USB Camera (%04x:%04x)",
1294 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1295 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1296 }
Jean-François Moined8518592010-12-28 06:55:01 -03001297 usb_make_path(gspca_dev->dev, (char *) cap->bus_info,
1298 sizeof(cap->bus_info));
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001299 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1300 | V4L2_CAP_STREAMING
1301 | V4L2_CAP_READWRITE;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001302 ret = 0;
1303out:
1304 mutex_unlock(&gspca_dev->usb_lock);
1305 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001306}
1307
Jean-François Moine6a330912010-10-02 04:12:25 -03001308static int get_ctrl(struct gspca_dev *gspca_dev,
Hans de Goedea0001a22009-06-10 04:52:18 -03001309 int id)
1310{
1311 const struct ctrl *ctrls;
1312 int i;
1313
1314 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1315 i < gspca_dev->sd_desc->nctrls;
1316 i++, ctrls++) {
1317 if (gspca_dev->ctrl_dis & (1 << i))
1318 continue;
1319 if (id == ctrls->qctrl.id)
Jean-François Moine6a330912010-10-02 04:12:25 -03001320 return i;
Hans de Goedea0001a22009-06-10 04:52:18 -03001321 }
Jean-François Moine6a330912010-10-02 04:12:25 -03001322 return -1;
Hans de Goedea0001a22009-06-10 04:52:18 -03001323}
1324
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001325static int vidioc_queryctrl(struct file *file, void *priv,
1326 struct v4l2_queryctrl *q_ctrl)
1327{
1328 struct gspca_dev *gspca_dev = priv;
Hans de Goedea0001a22009-06-10 04:52:18 -03001329 const struct ctrl *ctrls;
Jean-François Moine6a330912010-10-02 04:12:25 -03001330 struct gspca_ctrl *gspca_ctrl;
1331 int i, idx;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001332 u32 id;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001333
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001334 id = q_ctrl->id;
1335 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1336 id &= V4L2_CTRL_ID_MASK;
1337 id++;
Jean-François Moine6a330912010-10-02 04:12:25 -03001338 idx = -1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001339 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001340 if (gspca_dev->ctrl_dis & (1 << i))
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001341 continue;
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001342 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001343 continue;
Jean-François Moine6a330912010-10-02 04:12:25 -03001344 if (idx >= 0
1345 && gspca_dev->sd_desc->ctrls[i].qctrl.id
Jean-François Moine780e3122010-10-19 04:29:10 -03001346 > gspca_dev->sd_desc->ctrls[idx].qctrl.id)
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001347 continue;
Jean-François Moine6a330912010-10-02 04:12:25 -03001348 idx = i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001349 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001350 } else {
Jean-François Moine6a330912010-10-02 04:12:25 -03001351 idx = get_ctrl(gspca_dev, id);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001352 }
Jean-François Moine6a330912010-10-02 04:12:25 -03001353 if (idx < 0)
1354 return -EINVAL;
1355 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1356 memcpy(q_ctrl, &ctrls->qctrl, sizeof *q_ctrl);
1357 if (gspca_dev->cam.ctrls != NULL) {
1358 gspca_ctrl = &gspca_dev->cam.ctrls[idx];
1359 q_ctrl->default_value = gspca_ctrl->def;
1360 q_ctrl->minimum = gspca_ctrl->min;
1361 q_ctrl->maximum = gspca_ctrl->max;
1362 }
1363 if (gspca_dev->ctrl_inac & (1 << idx))
Jean-Francois Moine4af85662009-11-12 15:59:27 -03001364 q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001365 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001366}
1367
1368static int vidioc_s_ctrl(struct file *file, void *priv,
1369 struct v4l2_control *ctrl)
1370{
1371 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001372 const struct ctrl *ctrls;
Jean-François Moine6a330912010-10-02 04:12:25 -03001373 struct gspca_ctrl *gspca_ctrl;
1374 int idx, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001375
Jean-François Moine6a330912010-10-02 04:12:25 -03001376 idx = get_ctrl(gspca_dev, ctrl->id);
1377 if (idx < 0)
Hans de Goedea0001a22009-06-10 04:52:18 -03001378 return -EINVAL;
Jean-François Moine6a330912010-10-02 04:12:25 -03001379 if (gspca_dev->ctrl_inac & (1 << idx))
1380 return -EINVAL;
1381 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1382 if (gspca_dev->cam.ctrls != NULL) {
1383 gspca_ctrl = &gspca_dev->cam.ctrls[idx];
1384 if (ctrl->value < gspca_ctrl->min
1385 || ctrl->value > gspca_ctrl->max)
1386 return -ERANGE;
1387 } else {
1388 gspca_ctrl = NULL;
1389 if (ctrl->value < ctrls->qctrl.minimum
1390 || ctrl->value > ctrls->qctrl.maximum)
1391 return -ERANGE;
1392 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001393 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1394 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1395 return -ERESTARTSYS;
Jean-François Moine6a330912010-10-02 04:12:25 -03001396 if (!gspca_dev->present) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001397 ret = -ENODEV;
Jean-François Moine6a330912010-10-02 04:12:25 -03001398 goto out;
1399 }
1400 gspca_dev->usb_err = 0;
1401 if (ctrls->set != NULL) {
1402 ret = ctrls->set(gspca_dev, ctrl->value);
1403 goto out;
1404 }
1405 if (gspca_ctrl != NULL) {
1406 gspca_ctrl->val = ctrl->value;
1407 if (ctrls->set_control != NULL
1408 && gspca_dev->streaming)
1409 ctrls->set_control(gspca_dev);
1410 }
1411 ret = gspca_dev->usb_err;
1412out:
Hans de Goedea0001a22009-06-10 04:52:18 -03001413 mutex_unlock(&gspca_dev->usb_lock);
1414 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001415}
1416
1417static int vidioc_g_ctrl(struct file *file, void *priv,
1418 struct v4l2_control *ctrl)
1419{
1420 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001421 const struct ctrl *ctrls;
Jean-François Moine6a330912010-10-02 04:12:25 -03001422 int idx, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001423
Jean-François Moine6a330912010-10-02 04:12:25 -03001424 idx = get_ctrl(gspca_dev, ctrl->id);
1425 if (idx < 0)
Hans de Goedea0001a22009-06-10 04:52:18 -03001426 return -EINVAL;
Jean-François Moine6a330912010-10-02 04:12:25 -03001427 ctrls = &gspca_dev->sd_desc->ctrls[idx];
Hans de Goedea0001a22009-06-10 04:52:18 -03001428
1429 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1430 return -ERESTARTSYS;
Jean-François Moine6a330912010-10-02 04:12:25 -03001431 if (!gspca_dev->present) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001432 ret = -ENODEV;
Jean-François Moine6a330912010-10-02 04:12:25 -03001433 goto out;
1434 }
1435 gspca_dev->usb_err = 0;
1436 if (ctrls->get != NULL) {
1437 ret = ctrls->get(gspca_dev, &ctrl->value);
1438 goto out;
1439 }
1440 if (gspca_dev->cam.ctrls != NULL)
1441 ctrl->value = gspca_dev->cam.ctrls[idx].val;
1442 ret = 0;
1443out:
Hans de Goedea0001a22009-06-10 04:52:18 -03001444 mutex_unlock(&gspca_dev->usb_lock);
1445 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001446}
1447
1448static int vidioc_querymenu(struct file *file, void *priv,
1449 struct v4l2_querymenu *qmenu)
1450{
1451 struct gspca_dev *gspca_dev = priv;
1452
1453 if (!gspca_dev->sd_desc->querymenu)
1454 return -EINVAL;
1455 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1456}
1457
1458static int vidioc_enum_input(struct file *file, void *priv,
1459 struct v4l2_input *input)
1460{
1461 struct gspca_dev *gspca_dev = priv;
1462
1463 if (input->index != 0)
1464 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001465 input->type = V4L2_INPUT_TYPE_CAMERA;
Adam Bakerdfa76fa2009-03-29 19:17:10 -03001466 input->status = gspca_dev->cam.input_flags;
Hans de Goedee7d712c2011-06-05 14:58:46 -03001467 strlcpy(input->name, gspca_dev->sd_desc->name,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001468 sizeof input->name);
1469 return 0;
1470}
1471
1472static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1473{
1474 *i = 0;
1475 return 0;
1476}
1477
1478static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1479{
1480 if (i > 0)
1481 return -EINVAL;
1482 return (0);
1483}
1484
1485static int vidioc_reqbufs(struct file *file, void *priv,
1486 struct v4l2_requestbuffers *rb)
1487{
1488 struct gspca_dev *gspca_dev = priv;
Jean-François Moine832d0a92010-04-09 06:07:58 -03001489 int i, ret = 0, streaming;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001490
Jean-François Moinea2ba6f22010-07-14 06:21:16 -03001491 i = rb->memory; /* (avoid compilation warning) */
1492 switch (i) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001493 case GSPCA_MEMORY_READ: /* (internal call) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001494 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001495 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001496 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001497 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001498 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001499 }
Hans de Goedee2997a72008-04-23 08:09:12 -03001500 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1501 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001502
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001503 if (gspca_dev->memory != GSPCA_MEMORY_NO
Hans de Goedece5610b2010-12-31 07:41:54 -03001504 && gspca_dev->memory != GSPCA_MEMORY_READ
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001505 && gspca_dev->memory != rb->memory) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001506 ret = -EBUSY;
1507 goto out;
1508 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001509
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001510 /* only one file may do the capture */
1511 if (gspca_dev->capt_file != NULL
1512 && gspca_dev->capt_file != file) {
1513 ret = -EBUSY;
1514 goto out;
1515 }
1516
1517 /* if allocated, the buffers must not be mapped */
1518 for (i = 0; i < gspca_dev->nframes; i++) {
1519 if (gspca_dev->frame[i].vma_use_count) {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001520 ret = -EBUSY;
1521 goto out;
1522 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001523 }
1524
1525 /* stop streaming */
Jean-François Moine832d0a92010-04-09 06:07:58 -03001526 streaming = gspca_dev->streaming;
1527 if (streaming) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001528 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001529 gspca_dev->usb_err = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001530 gspca_stream_off(gspca_dev);
1531 mutex_unlock(&gspca_dev->usb_lock);
Jean-François Moine59929ce2011-03-14 05:33:01 -03001532
1533 /* Don't restart the stream when switching from read
1534 * to mmap mode */
1535 if (gspca_dev->memory == GSPCA_MEMORY_READ)
1536 streaming = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001537 }
1538
1539 /* free the previous allocated buffers, if any */
Hans de Goeded642de22010-12-31 04:51:36 -03001540 if (gspca_dev->nframes != 0)
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001541 frame_free(gspca_dev);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001542 if (rb->count == 0) /* unrequest */
1543 goto out;
Hans de Goeded642de22010-12-31 04:51:36 -03001544 ret = frame_alloc(gspca_dev, file, rb->memory, rb->count);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001545 if (ret == 0) {
1546 rb->count = gspca_dev->nframes;
Jean-François Moine832d0a92010-04-09 06:07:58 -03001547 if (streaming)
1548 ret = gspca_init_transfer(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001549 }
1550out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001551 mutex_unlock(&gspca_dev->queue_lock);
1552 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1553 return ret;
1554}
1555
1556static int vidioc_querybuf(struct file *file, void *priv,
1557 struct v4l2_buffer *v4l2_buf)
1558{
1559 struct gspca_dev *gspca_dev = priv;
1560 struct gspca_frame *frame;
1561
Jean-Francois Moine13752bd2008-12-19 15:08:51 -03001562 if (v4l2_buf->index < 0
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001563 || v4l2_buf->index >= gspca_dev->nframes)
1564 return -EINVAL;
1565
1566 frame = &gspca_dev->frame[v4l2_buf->index];
1567 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1568 return 0;
1569}
1570
1571static int vidioc_streamon(struct file *file, void *priv,
1572 enum v4l2_buf_type buf_type)
1573{
1574 struct gspca_dev *gspca_dev = priv;
1575 int ret;
1576
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001577 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1578 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001579 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1580 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001581
Jean-François Moine18bf99a2010-04-09 06:06:11 -03001582 /* check the capture file */
1583 if (gspca_dev->capt_file != file) {
1584 ret = -EBUSY;
1585 goto out;
1586 }
1587
Jean-Francois Moine1edabe72008-12-19 15:17:25 -03001588 if (gspca_dev->nframes == 0
1589 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001590 ret = -EINVAL;
1591 goto out;
1592 }
1593 if (!gspca_dev->streaming) {
1594 ret = gspca_init_transfer(gspca_dev);
1595 if (ret < 0)
1596 goto out;
1597 }
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001598#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001599 if (gspca_debug & D_STREAM) {
1600 PDEBUG_MODE("stream on OK",
1601 gspca_dev->pixfmt,
1602 gspca_dev->width,
1603 gspca_dev->height);
1604 }
1605#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001606 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001607out:
1608 mutex_unlock(&gspca_dev->queue_lock);
1609 return ret;
1610}
1611
1612static int vidioc_streamoff(struct file *file, void *priv,
1613 enum v4l2_buf_type buf_type)
1614{
1615 struct gspca_dev *gspca_dev = priv;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001616 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001617
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001618 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1619 return -EINVAL;
Hans de Goede4e770f72010-12-30 13:11:21 -03001620
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001621 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1622 return -ERESTARTSYS;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001623
Hans de Goede4e770f72010-12-30 13:11:21 -03001624 if (!gspca_dev->streaming) {
1625 ret = 0;
1626 goto out;
1627 }
1628
Jean-François Moine18bf99a2010-04-09 06:06:11 -03001629 /* check the capture file */
1630 if (gspca_dev->capt_file != file) {
1631 ret = -EBUSY;
1632 goto out;
1633 }
1634
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001635 /* stop streaming */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001636 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1637 ret = -ERESTARTSYS;
1638 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001639 }
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001640 gspca_dev->usb_err = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001641 gspca_stream_off(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001642 mutex_unlock(&gspca_dev->usb_lock);
Hans de Goede0d0ae152010-12-31 17:17:51 -03001643 /* In case another thread is waiting in dqbuf */
1644 wake_up_interruptible(&gspca_dev->wq);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001645
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001646 /* empty the transfer queues */
1647 atomic_set(&gspca_dev->fr_q, 0);
1648 atomic_set(&gspca_dev->fr_i, 0);
1649 gspca_dev->fr_o = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001650 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001651out:
1652 mutex_unlock(&gspca_dev->queue_lock);
1653 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001654}
1655
1656static int vidioc_g_jpegcomp(struct file *file, void *priv,
1657 struct v4l2_jpegcompression *jpegcomp)
1658{
1659 struct gspca_dev *gspca_dev = priv;
1660 int ret;
1661
1662 if (!gspca_dev->sd_desc->get_jcomp)
1663 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001664 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1665 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001666 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001667 if (gspca_dev->present)
1668 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1669 else
1670 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001671 mutex_unlock(&gspca_dev->usb_lock);
1672 return ret;
1673}
1674
1675static int vidioc_s_jpegcomp(struct file *file, void *priv,
1676 struct v4l2_jpegcompression *jpegcomp)
1677{
1678 struct gspca_dev *gspca_dev = priv;
1679 int ret;
1680
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001681 if (!gspca_dev->sd_desc->set_jcomp)
1682 return -EINVAL;
Jim Parisf86d4a92008-12-10 05:50:44 -03001683 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1684 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001685 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001686 if (gspca_dev->present)
1687 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1688 else
1689 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001690 mutex_unlock(&gspca_dev->usb_lock);
1691 return ret;
1692}
1693
1694static int vidioc_g_parm(struct file *filp, void *priv,
1695 struct v4l2_streamparm *parm)
1696{
1697 struct gspca_dev *gspca_dev = priv;
1698
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001699 parm->parm.capture.readbuffers = gspca_dev->nbufread;
Jim Paris627a5ef2008-12-10 06:02:42 -03001700
1701 if (gspca_dev->sd_desc->get_streamparm) {
1702 int ret;
1703
1704 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1705 return -ERESTARTSYS;
Jean-François Moine668f44a2010-12-25 13:46:14 -03001706 if (gspca_dev->present) {
1707 gspca_dev->usb_err = 0;
1708 gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
1709 ret = gspca_dev->usb_err;
1710 } else {
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001711 ret = -ENODEV;
Jean-François Moine668f44a2010-12-25 13:46:14 -03001712 }
Jim Paris627a5ef2008-12-10 06:02:42 -03001713 mutex_unlock(&gspca_dev->usb_lock);
1714 return ret;
1715 }
1716
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001717 return 0;
1718}
1719
1720static int vidioc_s_parm(struct file *filp, void *priv,
1721 struct v4l2_streamparm *parm)
1722{
1723 struct gspca_dev *gspca_dev = priv;
1724 int n;
1725
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001726 n = parm->parm.capture.readbuffers;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001727 if (n == 0 || n >= GSPCA_MAX_FRAMES)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001728 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1729 else
1730 gspca_dev->nbufread = n;
Jim Paris627a5ef2008-12-10 06:02:42 -03001731
1732 if (gspca_dev->sd_desc->set_streamparm) {
1733 int ret;
1734
1735 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1736 return -ERESTARTSYS;
Jean-François Moine668f44a2010-12-25 13:46:14 -03001737 if (gspca_dev->present) {
1738 gspca_dev->usb_err = 0;
1739 gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
1740 ret = gspca_dev->usb_err;
1741 } else {
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001742 ret = -ENODEV;
Jean-François Moine668f44a2010-12-25 13:46:14 -03001743 }
Jim Paris627a5ef2008-12-10 06:02:42 -03001744 mutex_unlock(&gspca_dev->usb_lock);
1745 return ret;
1746 }
1747
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001748 return 0;
1749}
1750
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001751static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1752{
1753 struct gspca_dev *gspca_dev = file->private_data;
Harvey Harrisonefab8212008-07-05 06:12:47 -03001754 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001755 struct page *page;
1756 unsigned long addr, start, size;
1757 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001758
1759 start = vma->vm_start;
1760 size = vma->vm_end - vma->vm_start;
1761 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1762
Hans de Goedee2997a72008-04-23 08:09:12 -03001763 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1764 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001765 if (!gspca_dev->present) {
1766 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001767 goto out;
1768 }
1769 if (gspca_dev->capt_file != file) {
1770 ret = -EINVAL;
1771 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001772 }
1773
Harvey Harrisonefab8212008-07-05 06:12:47 -03001774 frame = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001775 for (i = 0; i < gspca_dev->nframes; ++i) {
1776 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1777 PDEBUG(D_STREAM, "mmap bad memory type");
1778 break;
1779 }
1780 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1781 == vma->vm_pgoff) {
1782 frame = &gspca_dev->frame[i];
1783 break;
1784 }
1785 }
Harvey Harrisonefab8212008-07-05 06:12:47 -03001786 if (frame == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001787 PDEBUG(D_STREAM, "mmap no frame buffer found");
1788 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001789 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001790 }
Jean-François Moine38bff692010-06-24 05:19:58 -03001791 if (size != frame->v4l2_buf.length) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001792 PDEBUG(D_STREAM, "mmap bad size");
1793 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001794 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001795 }
1796
1797 /*
1798 * - VM_IO marks the area as being a mmaped region for I/O to a
1799 * device. It also prevents the region from being core dumped.
1800 */
1801 vma->vm_flags |= VM_IO;
1802
1803 addr = (unsigned long) frame->data;
1804 while (size > 0) {
1805 page = vmalloc_to_page((void *) addr);
1806 ret = vm_insert_page(vma, start, page);
1807 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001808 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001809 start += PAGE_SIZE;
1810 addr += PAGE_SIZE;
1811 size -= PAGE_SIZE;
1812 }
1813
Jean-Francois Moined50e2572009-11-26 15:36:40 -03001814 vma->vm_ops = &gspca_vm_ops;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001815 vma->vm_private_data = frame;
1816 gspca_vm_open(vma);
Hans de Goedee2997a72008-04-23 08:09:12 -03001817 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001818out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001819 mutex_unlock(&gspca_dev->queue_lock);
1820 return ret;
1821}
1822
Hans de Goede27074ef2010-12-30 19:54:33 -03001823static int frame_ready_nolock(struct gspca_dev *gspca_dev, struct file *file,
1824 enum v4l2_memory memory)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001825{
Hans de Goede27074ef2010-12-30 19:54:33 -03001826 if (!gspca_dev->present)
1827 return -ENODEV;
1828 if (gspca_dev->capt_file != file || gspca_dev->memory != memory ||
1829 !gspca_dev->streaming)
1830 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001831
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001832 /* check if a frame is ready */
Hans de Goede27074ef2010-12-30 19:54:33 -03001833 return gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i);
1834}
Hans de Goede181b7042008-11-19 17:16:26 -03001835
Hans de Goede27074ef2010-12-30 19:54:33 -03001836static int frame_ready(struct gspca_dev *gspca_dev, struct file *file,
1837 enum v4l2_memory memory)
1838{
1839 int ret;
Hans de Goede181b7042008-11-19 17:16:26 -03001840
Hans de Goede27074ef2010-12-30 19:54:33 -03001841 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1842 return -ERESTARTSYS;
1843 ret = frame_ready_nolock(gspca_dev, file, memory);
1844 mutex_unlock(&gspca_dev->queue_lock);
1845 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001846}
1847
1848/*
1849 * dequeue a video buffer
1850 *
1851 * If nonblock_ing is false, block until a buffer is available.
1852 */
1853static int vidioc_dqbuf(struct file *file, void *priv,
1854 struct v4l2_buffer *v4l2_buf)
1855{
1856 struct gspca_dev *gspca_dev = priv;
1857 struct gspca_frame *frame;
Hans de Goede27074ef2010-12-30 19:54:33 -03001858 int i, j, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001859
1860 PDEBUG(D_FRAM, "dqbuf");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001861
Hans de Goede27074ef2010-12-30 19:54:33 -03001862 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001863 return -ERESTARTSYS;
1864
Hans de Goede27074ef2010-12-30 19:54:33 -03001865 for (;;) {
1866 ret = frame_ready_nolock(gspca_dev, file, v4l2_buf->memory);
1867 if (ret < 0)
1868 goto out;
1869 if (ret > 0)
1870 break;
1871
1872 mutex_unlock(&gspca_dev->queue_lock);
1873
1874 if (file->f_flags & O_NONBLOCK)
1875 return -EAGAIN;
1876
1877 /* wait till a frame is ready */
1878 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1879 frame_ready(gspca_dev, file, v4l2_buf->memory),
1880 msecs_to_jiffies(3000));
1881 if (ret < 0)
1882 return ret;
1883 if (ret == 0)
1884 return -EIO;
1885
1886 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1887 return -ERESTARTSYS;
1888 }
1889
1890 i = gspca_dev->fr_o;
1891 j = gspca_dev->fr_queue[i];
1892 frame = &gspca_dev->frame[j];
1893
1894 gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES;
1895
1896 if (gspca_dev->sd_desc->dq_callback) {
1897 mutex_lock(&gspca_dev->usb_lock);
1898 gspca_dev->usb_err = 0;
1899 if (gspca_dev->present)
1900 gspca_dev->sd_desc->dq_callback(gspca_dev);
1901 mutex_unlock(&gspca_dev->usb_lock);
1902 }
1903
Hans de Goede4a82bc62010-12-30 20:00:17 -03001904 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1905 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1906 PDEBUG(D_FRAM, "dqbuf %d", j);
1907 ret = 0;
1908
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001909 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001910 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001911 frame->data,
1912 frame->v4l2_buf.bytesused)) {
1913 PDEBUG(D_ERR|D_STREAM,
1914 "dqbuf cp to user failed");
1915 ret = -EFAULT;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001916 }
1917 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001918out:
Hans de Goede27074ef2010-12-30 19:54:33 -03001919 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001920 return ret;
1921}
1922
1923/*
1924 * queue a video buffer
1925 *
1926 * Attempting to queue a buffer that has already been
1927 * queued will return -EINVAL.
1928 */
1929static int vidioc_qbuf(struct file *file, void *priv,
1930 struct v4l2_buffer *v4l2_buf)
1931{
1932 struct gspca_dev *gspca_dev = priv;
1933 struct gspca_frame *frame;
1934 int i, index, ret;
1935
1936 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001937
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001938 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1939 return -ERESTARTSYS;
1940
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001941 index = v4l2_buf->index;
1942 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001943 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001944 "qbuf idx %d >= %d", index, gspca_dev->nframes);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001945 ret = -EINVAL;
1946 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001947 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001948 if (v4l2_buf->memory != gspca_dev->memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001949 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001950 ret = -EINVAL;
1951 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001952 }
1953
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001954 frame = &gspca_dev->frame[index];
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001955 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1956 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001957 ret = -EINVAL;
1958 goto out;
1959 }
1960
1961 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001962
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001963 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001964 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1965 frame->v4l2_buf.length = v4l2_buf->length;
1966 }
1967
1968 /* put the buffer in the 'queued' queue */
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001969 i = atomic_read(&gspca_dev->fr_q);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001970 gspca_dev->fr_queue[i] = index;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001971 atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001972
1973 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1974 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1975 ret = 0;
1976out:
1977 mutex_unlock(&gspca_dev->queue_lock);
1978 return ret;
1979}
1980
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001981/*
1982 * allocate the resources for read()
1983 */
1984static int read_alloc(struct gspca_dev *gspca_dev,
1985 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001986{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001987 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001988 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001989
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001990 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001991 if (gspca_dev->nframes == 0) {
1992 struct v4l2_requestbuffers rb;
1993
1994 memset(&rb, 0, sizeof rb);
1995 rb.count = gspca_dev->nbufread;
1996 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001997 rb.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001998 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1999 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002000 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002001 return ret;
2002 }
2003 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2004 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002005 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002006 for (i = 0; i < gspca_dev->nbufread; i++) {
2007 v4l2_buf.index = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002008 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2009 if (ret != 0) {
2010 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
2011 return ret;
2012 }
2013 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002014 gspca_dev->memory = GSPCA_MEMORY_READ;
2015 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002016
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002017 /* start streaming */
2018 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2019 if (ret != 0)
2020 PDEBUG(D_STREAM, "read streamon err %d", ret);
2021 return ret;
2022}
2023
2024static unsigned int dev_poll(struct file *file, poll_table *wait)
2025{
2026 struct gspca_dev *gspca_dev = file->private_data;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03002027 int ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002028
2029 PDEBUG(D_FRAM, "poll");
2030
2031 poll_wait(file, &gspca_dev->wq, wait);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002032
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002033 /* if reqbufs is not done, the user would use read() */
Hans de Goedeee362992010-12-31 05:05:56 -03002034 if (gspca_dev->memory == GSPCA_MEMORY_NO) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002035 ret = read_alloc(gspca_dev, file);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002036 if (ret != 0)
2037 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002038 }
2039
2040 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
2041 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002042
Jean-François Moinef7059ea2010-07-06 04:32:27 -03002043 /* check if an image has been received */
2044 if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i))
2045 ret = POLLIN | POLLRDNORM; /* yes */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002046 else
2047 ret = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002048 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002049 if (!gspca_dev->present)
2050 return POLLHUP;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002051 return ret;
2052}
2053
2054static ssize_t dev_read(struct file *file, char __user *data,
2055 size_t count, loff_t *ppos)
2056{
2057 struct gspca_dev *gspca_dev = file->private_data;
2058 struct gspca_frame *frame;
2059 struct v4l2_buffer v4l2_buf;
2060 struct timeval timestamp;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002061 int n, ret, ret2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002062
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03002063 PDEBUG(D_FRAM, "read (%zd)", count);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002064 if (!gspca_dev->present)
2065 return -ENODEV;
Hans de Goedeee362992010-12-31 05:05:56 -03002066 if (gspca_dev->memory == GSPCA_MEMORY_NO) { /* first time ? */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002067 ret = read_alloc(gspca_dev, file);
2068 if (ret != 0)
2069 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002070 }
2071
2072 /* get a frame */
Paulo Assis2f51d002010-03-07 06:56:56 -03002073 timestamp = ktime_to_timeval(ktime_get());
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002074 timestamp.tv_sec--;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002075 n = 2;
2076 for (;;) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002077 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2078 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002079 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002080 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
2081 if (ret != 0) {
2082 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
2083 return ret;
2084 }
2085
2086 /* if the process slept for more than 1 second,
Frank Zago219423c2008-09-26 07:43:54 -03002087 * get a newer frame */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002088 frame = &gspca_dev->frame[v4l2_buf.index];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002089 if (--n < 0)
2090 break; /* avoid infinite loop */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002091 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
2092 break;
2093 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2094 if (ret != 0) {
2095 PDEBUG(D_STREAM, "read qbuf err %d", ret);
2096 return ret;
2097 }
2098 }
2099
2100 /* copy the frame */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002101 if (count > frame->v4l2_buf.bytesused)
2102 count = frame->v4l2_buf.bytesused;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002103 ret = copy_to_user(data, frame->data, count);
2104 if (ret != 0) {
2105 PDEBUG(D_ERR|D_STREAM,
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03002106 "read cp to user lack %d / %zd", ret, count);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002107 ret = -EFAULT;
2108 goto out;
2109 }
2110 ret = count;
2111out:
2112 /* in each case, requeue the buffer */
2113 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2114 if (ret2 != 0)
2115 return ret2;
2116 return ret;
2117}
2118
Hans Verkuilbec43662008-12-30 06:58:20 -03002119static struct v4l2_file_operations dev_fops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002120 .owner = THIS_MODULE,
2121 .open = dev_open,
2122 .release = dev_close,
2123 .read = dev_read,
2124 .mmap = dev_mmap,
Hans Verkuilbec43662008-12-30 06:58:20 -03002125 .unlocked_ioctl = video_ioctl2,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002126 .poll = dev_poll,
2127};
2128
Hans Verkuila3998102008-07-21 02:57:38 -03002129static const struct v4l2_ioctl_ops dev_ioctl_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002130 .vidioc_querycap = vidioc_querycap,
2131 .vidioc_dqbuf = vidioc_dqbuf,
2132 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03002133 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2134 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2135 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2136 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002137 .vidioc_streamon = vidioc_streamon,
2138 .vidioc_queryctrl = vidioc_queryctrl,
2139 .vidioc_g_ctrl = vidioc_g_ctrl,
2140 .vidioc_s_ctrl = vidioc_s_ctrl,
2141 .vidioc_querymenu = vidioc_querymenu,
2142 .vidioc_enum_input = vidioc_enum_input,
2143 .vidioc_g_input = vidioc_g_input,
2144 .vidioc_s_input = vidioc_s_input,
2145 .vidioc_reqbufs = vidioc_reqbufs,
2146 .vidioc_querybuf = vidioc_querybuf,
2147 .vidioc_streamoff = vidioc_streamoff,
2148 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
2149 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
2150 .vidioc_g_parm = vidioc_g_parm,
2151 .vidioc_s_parm = vidioc_s_parm,
Hans de Goede11c635a2009-05-25 15:04:22 -03002152 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Antonio Ospite28ffe772009-12-02 06:18:46 -03002153 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03002154#ifdef CONFIG_VIDEO_ADV_DEBUG
2155 .vidioc_g_register = vidioc_g_register,
2156 .vidioc_s_register = vidioc_s_register,
2157#endif
2158 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002159};
2160
Jean-François Moine59929ce2011-03-14 05:33:01 -03002161static const struct video_device gspca_template = {
Hans Verkuila3998102008-07-21 02:57:38 -03002162 .name = "gspca main driver",
Hans Verkuila3998102008-07-21 02:57:38 -03002163 .fops = &dev_fops,
2164 .ioctl_ops = &dev_ioctl_ops,
Hans de Goeded0182302008-11-19 17:10:47 -03002165 .release = gspca_release,
Hans Verkuila3998102008-07-21 02:57:38 -03002166};
2167
Jean-François Moine6a330912010-10-02 04:12:25 -03002168/* initialize the controls */
2169static void ctrls_init(struct gspca_dev *gspca_dev)
2170{
2171 struct gspca_ctrl *ctrl;
2172 int i;
2173
2174 for (i = 0, ctrl = gspca_dev->cam.ctrls;
2175 i < gspca_dev->sd_desc->nctrls;
2176 i++, ctrl++) {
2177 ctrl->def = gspca_dev->sd_desc->ctrls[i].qctrl.default_value;
2178 ctrl->val = ctrl->def;
2179 ctrl->min = gspca_dev->sd_desc->ctrls[i].qctrl.minimum;
2180 ctrl->max = gspca_dev->sd_desc->ctrls[i].qctrl.maximum;
2181 }
2182}
2183
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002184/*
2185 * probe and create a new gspca device
2186 *
2187 * This function must be called by the sub-driver when it is
2188 * called for probing a new device.
2189 */
Jean-François Moine44628642010-06-05 07:47:36 -03002190int gspca_dev_probe2(struct usb_interface *intf,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002191 const struct usb_device_id *id,
2192 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002193 int dev_size,
2194 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002195{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002196 struct gspca_dev *gspca_dev;
2197 struct usb_device *dev = interface_to_usbdev(intf);
2198 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002199
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002200 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002201
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002202 /* create the device */
2203 if (dev_size < sizeof *gspca_dev)
2204 dev_size = sizeof *gspca_dev;
2205 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
Erik Andrene293e592008-10-03 08:46:50 -03002206 if (!gspca_dev) {
Joe Perches133a9fe2011-08-21 19:56:57 -03002207 pr_err("couldn't kzalloc gspca struct\n");
Erik Andrene293e592008-10-03 08:46:50 -03002208 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002209 }
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002210 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2211 if (!gspca_dev->usb_buf) {
Joe Perches133a9fe2011-08-21 19:56:57 -03002212 pr_err("out of memory\n");
Erik Andrene293e592008-10-03 08:46:50 -03002213 ret = -ENOMEM;
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002214 goto out;
2215 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002216 gspca_dev->dev = dev;
Jean-François Moine44628642010-06-05 07:47:36 -03002217 gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002218 gspca_dev->nbalt = intf->num_altsetting;
Jean-François Moine35680ba2010-07-14 06:30:18 -03002219
2220 /* check if any audio device */
2221 if (dev->config->desc.bNumInterfaces != 1) {
2222 int i;
2223 struct usb_interface *intf2;
2224
2225 for (i = 0; i < dev->config->desc.bNumInterfaces; i++) {
2226 intf2 = dev->config->interface[i];
2227 if (intf2 != NULL
2228 && intf2->altsetting != NULL
2229 && intf2->altsetting->desc.bInterfaceClass ==
2230 USB_CLASS_AUDIO) {
2231 gspca_dev->audio = 1;
2232 break;
2233 }
2234 }
2235 }
2236
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002237 gspca_dev->sd_desc = sd_desc;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002238 gspca_dev->nbufread = 2;
Jean-Francois Moineff374742008-10-23 07:29:51 -03002239 gspca_dev->empty_packet = -1; /* don't check the empty packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002240
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002241 /* configure the subdriver and initialize the USB device */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002242 ret = sd_desc->config(gspca_dev, id);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002243 if (ret < 0)
2244 goto out;
Jean-François Moine6a330912010-10-02 04:12:25 -03002245 if (gspca_dev->cam.ctrls != NULL)
2246 ctrls_init(gspca_dev);
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002247 ret = sd_desc->init(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002248 if (ret < 0)
2249 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002250 gspca_set_default_mode(gspca_dev);
2251
Hans de Goede10ee2402010-02-19 04:41:40 -03002252 ret = gspca_input_connect(gspca_dev);
2253 if (ret)
2254 goto out;
2255
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002256 mutex_init(&gspca_dev->usb_lock);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002257 mutex_init(&gspca_dev->queue_lock);
2258 init_waitqueue_head(&gspca_dev->wq);
2259
2260 /* init video stuff */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002261 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
Hans de Goede24441632009-03-27 16:08:20 -03002262 gspca_dev->vdev.parent = &intf->dev;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03002263 gspca_dev->module = module;
Oliver Neukumee56a4d2008-07-27 14:01:59 -03002264 gspca_dev->present = 1;
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002265 ret = video_register_device(&gspca_dev->vdev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002266 VFL_TYPE_GRABBER,
Jean-Francois Moine8d584a52009-01-13 06:07:59 -03002267 -1);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002268 if (ret < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -03002269 pr_err("video_register_device err %d\n", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002270 goto out;
2271 }
2272
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002273 usb_set_intfdata(intf, gspca_dev);
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002274 PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
Márton Németh0274d422010-01-28 06:39:49 -03002275
Hans de Goede10ee2402010-02-19 04:41:40 -03002276 gspca_input_create_urb(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002277
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002278 return 0;
2279out:
Randy Dunlapf7628012010-03-30 14:32:04 -03002280#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Hans de Goede10ee2402010-02-19 04:41:40 -03002281 if (gspca_dev->input_dev)
2282 input_unregister_device(gspca_dev->input_dev);
Hans de Goedee7637522010-02-19 05:11:09 -03002283#endif
Jean-Francois Moinee07a1d82008-11-19 06:37:53 -03002284 kfree(gspca_dev->usb_buf);
2285 kfree(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002286 return ret;
2287}
Jean-François Moine44628642010-06-05 07:47:36 -03002288EXPORT_SYMBOL(gspca_dev_probe2);
2289
2290/* same function as the previous one, but check the interface */
2291int gspca_dev_probe(struct usb_interface *intf,
2292 const struct usb_device_id *id,
2293 const struct sd_desc *sd_desc,
2294 int dev_size,
2295 struct module *module)
2296{
2297 struct usb_device *dev = interface_to_usbdev(intf);
2298
2299 /* we don't handle multi-config cameras */
2300 if (dev->descriptor.bNumConfigurations != 1) {
Joe Perches133a9fe2011-08-21 19:56:57 -03002301 pr_err("%04x:%04x too many config\n",
2302 id->idVendor, id->idProduct);
Jean-François Moine44628642010-06-05 07:47:36 -03002303 return -ENODEV;
2304 }
2305
2306 /* the USB video interface must be the first one */
2307 if (dev->config->desc.bNumInterfaces != 1
Jean-François Moinec4dc6922010-07-14 06:26:54 -03002308 && intf->cur_altsetting->desc.bInterfaceNumber != 0)
Jean-François Moine44628642010-06-05 07:47:36 -03002309 return -ENODEV;
Jean-François Moine44628642010-06-05 07:47:36 -03002310
2311 return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
2312}
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002313EXPORT_SYMBOL(gspca_dev_probe);
2314
2315/*
2316 * USB disconnection
2317 *
2318 * This function must be called by the sub-driver
2319 * when the device disconnects, after the specific resources are freed.
2320 */
2321void gspca_disconnect(struct usb_interface *intf)
2322{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002323 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Randy Dunlapf7628012010-03-30 14:32:04 -03002324#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -03002325 struct input_dev *input_dev;
Jean-François Moinee070bf92010-02-18 15:02:51 -03002326#endif
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002327
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002328 PDEBUG(D_PROBE, "%s disconnect",
2329 video_device_node_name(&gspca_dev->vdev));
Adam Bakerad281272009-02-04 15:33:21 -03002330 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002331
Hans de Goede4e770f72010-12-30 13:11:21 -03002332 gspca_dev->present = 0;
2333 wake_up_interruptible(&gspca_dev->wq);
2334
2335 destroy_urbs(gspca_dev);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002336
Randy Dunlapf7628012010-03-30 14:32:04 -03002337#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -03002338 gspca_input_destroy_urb(gspca_dev);
2339 input_dev = gspca_dev->input_dev;
2340 if (input_dev) {
2341 gspca_dev->input_dev = NULL;
2342 input_unregister_device(input_dev);
2343 }
Jean-François Moinee070bf92010-02-18 15:02:51 -03002344#endif
Márton Németh0274d422010-01-28 06:39:49 -03002345
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002346 /* the device is freed at exit of this function */
2347 gspca_dev->dev = NULL;
Adam Bakerad281272009-02-04 15:33:21 -03002348 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03002349
Frank Zago6b060ff2008-09-28 08:12:22 -03002350 usb_set_intfdata(intf, NULL);
2351
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002352 /* release the device */
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002353 /* (this will call gspca_release() immediately or on last close) */
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002354 video_unregister_device(&gspca_dev->vdev);
Jean-Francois Moine3145b8c2008-10-05 04:22:10 -03002355
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002356/* PDEBUG(D_PROBE, "disconnect complete"); */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002357}
2358EXPORT_SYMBOL(gspca_disconnect);
2359
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002360#ifdef CONFIG_PM
2361int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2362{
2363 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2364
2365 if (!gspca_dev->streaming)
2366 return 0;
2367 gspca_dev->frozen = 1; /* avoid urb error messages */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002368 if (gspca_dev->sd_desc->stopN)
2369 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002370 destroy_urbs(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002371 gspca_input_destroy_urb(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002372 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002373 if (gspca_dev->sd_desc->stop0)
2374 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002375 return 0;
2376}
2377EXPORT_SYMBOL(gspca_suspend);
2378
2379int gspca_resume(struct usb_interface *intf)
2380{
2381 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2382
2383 gspca_dev->frozen = 0;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002384 gspca_dev->sd_desc->init(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002385 gspca_input_create_urb(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002386 if (gspca_dev->streaming)
2387 return gspca_init_transfer(gspca_dev);
Jean-Francois Moine1e89e2da2008-09-03 17:12:15 -03002388 return 0;
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002389}
2390EXPORT_SYMBOL(gspca_resume);
2391#endif
Hans de Goededcef32372008-07-10 10:40:53 -03002392/* -- cam driver utility functions -- */
2393
2394/* auto gain and exposure algorithm based on the knee algorithm described here:
2395 http://ytse.tricolour.net/docs/LowLightOptimization.html
2396
2397 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2398 where changed. */
2399int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2400 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2401{
2402 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2403 const struct ctrl *gain_ctrl = NULL;
2404 const struct ctrl *exposure_ctrl = NULL;
2405 const struct ctrl *autogain_ctrl = NULL;
2406 int retval = 0;
2407
2408 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goede26984b02010-02-01 13:18:37 -03002409 if (gspca_dev->ctrl_dis & (1 << i))
2410 continue;
Hans de Goededcef32372008-07-10 10:40:53 -03002411 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2412 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2413 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2414 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2415 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2416 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2417 }
2418 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2419 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2420 "on cam without (auto)gain/exposure");
2421 return 0;
2422 }
2423
2424 if (gain_ctrl->get(gspca_dev, &gain) ||
2425 exposure_ctrl->get(gspca_dev, &exposure) ||
2426 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2427 return 0;
2428
2429 orig_gain = gain;
2430 orig_exposure = exposure;
2431
2432 /* If we are of a multiple of deadzone, do multiple steps to reach the
2433 desired lumination fast (with the risc of a slight overshoot) */
2434 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2435
Jean-Francois Moine1c44d812008-11-10 05:56:55 -03002436 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
Hans de Goededcef32372008-07-10 10:40:53 -03002437 avg_lum, desired_avg_lum, steps);
2438
2439 for (i = 0; i < steps; i++) {
2440 if (avg_lum > desired_avg_lum) {
2441 if (gain > gain_knee)
2442 gain--;
2443 else if (exposure > exposure_knee)
2444 exposure--;
2445 else if (gain > gain_ctrl->qctrl.default_value)
2446 gain--;
2447 else if (exposure > exposure_ctrl->qctrl.minimum)
2448 exposure--;
2449 else if (gain > gain_ctrl->qctrl.minimum)
2450 gain--;
2451 else
2452 break;
2453 } else {
2454 if (gain < gain_ctrl->qctrl.default_value)
2455 gain++;
2456 else if (exposure < exposure_knee)
2457 exposure++;
2458 else if (gain < gain_knee)
2459 gain++;
2460 else if (exposure < exposure_ctrl->qctrl.maximum)
2461 exposure++;
2462 else if (gain < gain_ctrl->qctrl.maximum)
2463 gain++;
2464 else
2465 break;
2466 }
2467 }
2468
2469 if (gain != orig_gain) {
2470 gain_ctrl->set(gspca_dev, gain);
2471 retval = 1;
2472 }
2473 if (exposure != orig_exposure) {
2474 exposure_ctrl->set(gspca_dev, exposure);
2475 retval = 1;
2476 }
2477
2478 return retval;
2479}
2480EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2481
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002482/* -- module insert / remove -- */
2483static int __init gspca_init(void)
2484{
Joe Perches133a9fe2011-08-21 19:56:57 -03002485 pr_info("v" DRIVER_VERSION_NUMBER " registered\n");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002486 return 0;
2487}
2488static void __exit gspca_exit(void)
2489{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002490}
2491
2492module_init(gspca_init);
2493module_exit(gspca_exit);
2494
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03002495#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002496module_param_named(debug, gspca_debug, int, 0644);
2497MODULE_PARM_DESC(debug,
2498 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-François Moinef8e56772011-05-17 04:10:32 -03002499 " 0x08:stream 0x10:frame 0x20:packet"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002500 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002501#endif