blob: ee9544894e8b4ce98339617a90b6cdf0dddb1a41 [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 Dobriyanf0f37e22009-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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 Moine0be01002008-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
Hans de Goeded642de22010-12-31 04:51:36 -0300502static int frame_alloc(struct gspca_dev *gspca_dev, struct file *file,
503 enum v4l2_memory memory, unsigned int count)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300504{
505 struct gspca_frame *frame;
506 unsigned int frsz;
507 int i;
508
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300509 i = gspca_dev->curr_mode;
510 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300511 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300512 frsz = PAGE_ALIGN(frsz);
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300513 if (count >= GSPCA_MAX_FRAMES)
514 count = GSPCA_MAX_FRAMES - 1;
Jean-François Moinee795d912010-06-24 05:14:22 -0300515 gspca_dev->frbuf = vmalloc_32(frsz * count);
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300516 if (!gspca_dev->frbuf) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300517 pr_err("frame alloc failed\n");
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300518 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300519 }
Hans de Goeded642de22010-12-31 04:51:36 -0300520 gspca_dev->capt_file = file;
521 gspca_dev->memory = memory;
522 gspca_dev->frsz = frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300523 gspca_dev->nframes = count;
524 for (i = 0; i < count; i++) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300525 frame = &gspca_dev->frame[i];
526 frame->v4l2_buf.index = i;
527 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
528 frame->v4l2_buf.flags = 0;
529 frame->v4l2_buf.field = V4L2_FIELD_NONE;
530 frame->v4l2_buf.length = frsz;
Hans de Goeded642de22010-12-31 04:51:36 -0300531 frame->v4l2_buf.memory = memory;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300532 frame->v4l2_buf.sequence = 0;
Jean-François Moineb192ca92010-06-27 03:08:19 -0300533 frame->data = gspca_dev->frbuf + i * frsz;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300534 frame->v4l2_buf.m.offset = i * frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300535 }
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300536 atomic_set(&gspca_dev->fr_q, 0);
537 atomic_set(&gspca_dev->fr_i, 0);
538 gspca_dev->fr_o = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300539 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300540}
541
542static void frame_free(struct gspca_dev *gspca_dev)
543{
544 int i;
545
546 PDEBUG(D_STREAM, "frame free");
Harvey Harrisonefab8212008-07-05 06:12:47 -0300547 if (gspca_dev->frbuf != NULL) {
Jean-François Moinee795d912010-06-24 05:14:22 -0300548 vfree(gspca_dev->frbuf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300549 gspca_dev->frbuf = NULL;
550 for (i = 0; i < gspca_dev->nframes; i++)
551 gspca_dev->frame[i].data = NULL;
552 }
553 gspca_dev->nframes = 0;
Hans de Goeded642de22010-12-31 04:51:36 -0300554 gspca_dev->frsz = 0;
555 gspca_dev->capt_file = NULL;
556 gspca_dev->memory = GSPCA_MEMORY_NO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300557}
558
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300559static void destroy_urbs(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300560{
561 struct urb *urb;
562 unsigned int i;
563
564 PDEBUG(D_STREAM, "kill transfer");
Erik Andrén44732812008-10-13 15:52:46 -0300565 for (i = 0; i < MAX_NURBS; i++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300566 urb = gspca_dev->urb[i];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300567 if (urb == NULL)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300568 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300569
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300570 gspca_dev->urb[i] = NULL;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300571 usb_kill_urb(urb);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300572 if (urb->transfer_buffer != NULL)
Daniel Mack997ea582010-04-12 13:17:25 +0200573 usb_free_coherent(gspca_dev->dev,
574 urb->transfer_buffer_length,
575 urb->transfer_buffer,
576 urb->transfer_dma);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300577 usb_free_urb(urb);
578 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300579}
580
Jean-François Moinee4dac282010-04-09 06:11:36 -0300581static int gspca_set_alt0(struct gspca_dev *gspca_dev)
582{
583 int ret;
584
585 if (gspca_dev->alt == 0)
586 return 0;
587 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
588 if (ret < 0)
Joe Perches133a9fe2011-08-21 19:56:57 -0300589 pr_err("set alt 0 err %d\n", ret);
Jean-François Moinee4dac282010-04-09 06:11:36 -0300590 return ret;
591}
592
593/* Note: both the queue and the usb locks should be held when calling this */
594static void gspca_stream_off(struct gspca_dev *gspca_dev)
595{
596 gspca_dev->streaming = 0;
597 if (gspca_dev->present) {
598 if (gspca_dev->sd_desc->stopN)
599 gspca_dev->sd_desc->stopN(gspca_dev);
600 destroy_urbs(gspca_dev);
601 gspca_input_destroy_urb(gspca_dev);
602 gspca_set_alt0(gspca_dev);
603 gspca_input_create_urb(gspca_dev);
604 }
605
606 /* always call stop0 to free the subdriver's resources */
607 if (gspca_dev->sd_desc->stop0)
608 gspca_dev->sd_desc->stop0(gspca_dev);
609 PDEBUG(D_STREAM, "stream off OK");
610}
611
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300612/*
Frank Zago219423c2008-09-26 07:43:54 -0300613 * look for an input transfer endpoint in an alternate setting
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300614 */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300615static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300616 int xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300617{
618 struct usb_host_endpoint *ep;
619 int i, attr;
620
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300621 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
622 ep = &alt->endpoint[i];
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300623 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Jean-Francois Moine30d35e42009-04-21 13:57:31 -0300624 if (attr == xfer
Patrice Chotard71062252011-03-21 16:58:56 -0300625 && ep->desc.wMaxPacketSize != 0
626 && usb_endpoint_dir_in(&ep->desc))
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300627 return ep;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300628 }
629 return NULL;
630}
631
Jean-François Moine27b12022011-08-10 07:38:48 -0300632/* compute the minimum bandwidth for the current transfer */
633static u32 which_bandwidth(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300634{
Jean-François Moine27b12022011-08-10 07:38:48 -0300635 u32 bandwidth;
636 int i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300637
Jean-François Moine27b12022011-08-10 07:38:48 -0300638 i = gspca_dev->curr_mode;
639 bandwidth = gspca_dev->cam.cam_mode[i].sizeimage;
640
641 /* if the image is compressed, estimate the mean image size */
642 if (bandwidth < gspca_dev->cam.cam_mode[i].width *
643 gspca_dev->cam.cam_mode[i].height)
644 bandwidth /= 3;
645
646 /* estimate the frame rate */
647 if (gspca_dev->sd_desc->get_streamparm) {
648 struct v4l2_streamparm parm;
649
650 parm.parm.capture.timeperframe.denominator = 15;
651 gspca_dev->sd_desc->get_streamparm(gspca_dev, &parm);
652 bandwidth *= parm.parm.capture.timeperframe.denominator;
Hans de Goedea511ba92009-10-16 07:13:07 -0300653 } else {
Jean-François Moine27b12022011-08-10 07:38:48 -0300654 bandwidth *= 15; /* 15 fps */
655 }
656
657 PDEBUG(D_STREAM, "min bandwidth: %d", bandwidth);
658 return bandwidth;
659}
660
661/* endpoint table */
662#define MAX_ALT 16
663struct ep_tb_s {
664 u32 alt;
665 u32 bandwidth;
666};
667
668/*
669 * build the table of the endpoints
670 * and compute the minimum bandwidth for the image transfer
671 */
672static int build_ep_tb(struct gspca_dev *gspca_dev,
673 struct usb_interface *intf,
674 int xfer,
675 struct ep_tb_s *ep_tb)
676{
677 struct usb_host_endpoint *ep;
678 int i, j, nbalt, psize, found;
679 u32 bandwidth, last_bw;
680
681 nbalt = intf->num_altsetting;
682 if (nbalt > MAX_ALT)
683 nbalt = MAX_ALT; /* fixme: should warn */
684
685 /* build the endpoint table */
686 i = 0;
687 last_bw = 0;
688 for (;;) {
689 ep_tb->bandwidth = 2000 * 2000 * 120;
690 found = 0;
691 for (j = 0; j < nbalt; j++) {
692 ep = alt_xfer(&intf->altsetting[j], xfer);
693 if (ep == NULL)
694 continue;
695 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
696 if (!gspca_dev->cam.bulk) /* isoc */
697 psize = (psize & 0x07ff) *
698 (1 + ((psize >> 11) & 3));
699 bandwidth = psize * ep->desc.bInterval * 1000;
700 if (gspca_dev->dev->speed == USB_SPEED_HIGH
701 || gspca_dev->dev->speed == USB_SPEED_SUPER)
702 bandwidth *= 8;
703 if (bandwidth <= last_bw)
704 continue;
705 if (bandwidth < ep_tb->bandwidth) {
706 ep_tb->bandwidth = bandwidth;
707 ep_tb->alt = j;
708 found = 1;
709 }
Hans de Goedea511ba92009-10-16 07:13:07 -0300710 }
Jean-François Moine27b12022011-08-10 07:38:48 -0300711 if (!found)
712 break;
713 PDEBUG(D_STREAM, "alt %d bandwidth %d",
714 ep_tb->alt, ep_tb->bandwidth);
715 last_bw = ep_tb->bandwidth;
716 i++;
717 ep_tb++;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300718 }
Jean-François Moine27b12022011-08-10 07:38:48 -0300719
720 /* get the requested bandwidth and start at the highest atlsetting */
721 bandwidth = which_bandwidth(gspca_dev);
722 ep_tb--;
723 while (i > 1) {
724 ep_tb--;
725 if (ep_tb->bandwidth < bandwidth)
726 break;
727 i--;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300728 }
Jean-François Moine27b12022011-08-10 07:38:48 -0300729 return i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300730}
731
732/*
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300733 * create the URBs for image transfer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300734 */
735static int create_urbs(struct gspca_dev *gspca_dev,
736 struct usb_host_endpoint *ep)
737{
738 struct urb *urb;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300739 int n, nurbs, i, psize, npkt, bsize;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300740
741 /* calculate the packet size and the number of packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300742 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300743
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300744 if (!gspca_dev->cam.bulk) { /* isoc */
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300745
746 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300747 if (gspca_dev->pkt_size == 0)
748 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
749 else
750 psize = gspca_dev->pkt_size;
Jean-Francois Moine49cb6b02009-04-25 13:29:01 -0300751 npkt = gspca_dev->cam.npkt;
752 if (npkt == 0)
753 npkt = 32; /* default value */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300754 bsize = psize * npkt;
755 PDEBUG(D_STREAM,
756 "isoc %d pkts size %d = bsize:%d",
757 npkt, psize, bsize);
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300758 nurbs = DEF_NURBS;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300759 } else { /* bulk */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300760 npkt = 0;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300761 bsize = gspca_dev->cam.bulk_size;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300762 if (bsize == 0)
763 bsize = psize;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300764 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300765 if (gspca_dev->cam.bulk_nurbs != 0)
766 nurbs = gspca_dev->cam.bulk_nurbs;
767 else
768 nurbs = 1;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300769 }
770
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300771 for (n = 0; n < nurbs; n++) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300772 urb = usb_alloc_urb(npkt, GFP_KERNEL);
773 if (!urb) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300774 pr_err("usb_alloc_urb failed\n");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300775 return -ENOMEM;
776 }
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300777 gspca_dev->urb[n] = urb;
Daniel Mack997ea582010-04-12 13:17:25 +0200778 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300779 bsize,
780 GFP_KERNEL,
781 &urb->transfer_dma);
782
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300783 if (urb->transfer_buffer == NULL) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300784 pr_err("usb_alloc_coherent failed\n");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300785 return -ENOMEM;
786 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300787 urb->dev = gspca_dev->dev;
788 urb->context = gspca_dev;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300789 urb->transfer_buffer_length = bsize;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300790 if (npkt != 0) { /* ISOC */
791 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
792 ep->desc.bEndpointAddress);
Frank Zago6c49da72008-09-28 07:43:00 -0300793 urb->transfer_flags = URB_ISO_ASAP
794 | URB_NO_TRANSFER_DMA_MAP;
Jean-François Moine480992b2011-08-10 07:28:44 -0300795 if (gspca_dev->dev->speed == USB_SPEED_LOW)
796 urb->interval = ep->desc.bInterval;
797 else
798 urb->interval = 1 << (ep->desc.bInterval - 1);
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300799 urb->complete = isoc_irq;
800 urb->number_of_packets = npkt;
801 for (i = 0; i < npkt; i++) {
802 urb->iso_frame_desc[i].length = psize;
803 urb->iso_frame_desc[i].offset = psize * i;
804 }
805 } else { /* bulk */
806 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
Jean-François Moinea23c1942010-10-29 07:57:03 -0300807 ep->desc.bEndpointAddress);
Frank Zago6c49da72008-09-28 07:43:00 -0300808 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300809 urb->complete = bulk_irq;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300810 }
811 }
812 return 0;
813}
814
815/*
816 * start the USB transfer
817 */
818static int gspca_init_transfer(struct gspca_dev *gspca_dev)
819{
Jean-François Moine27b12022011-08-10 07:38:48 -0300820 struct usb_interface *intf;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300821 struct usb_host_endpoint *ep;
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300822 struct urb *urb;
Jean-François Moine27b12022011-08-10 07:38:48 -0300823 struct ep_tb_s ep_tb[MAX_ALT];
824 int n, ret, xfer, alt, alt_idx;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300825
Hans de Goedee2997a72008-04-23 08:09:12 -0300826 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
827 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300828
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300829 if (!gspca_dev->present) {
830 ret = -ENODEV;
Hans de Goede314b3e02010-10-26 05:15:46 -0300831 goto unlock;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300832 }
833
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300834 /* reset the streaming variables */
835 gspca_dev->image = NULL;
836 gspca_dev->image_len = 0;
837 gspca_dev->last_packet_type = DISCARD_PACKET;
838 gspca_dev->sequence = 0;
839
Jean-Francois Moineab6c8cf2010-01-13 15:34:27 -0300840 gspca_dev->usb_err = 0;
841
Jean-François Moine27b12022011-08-10 07:38:48 -0300842 /* do the specific subdriver stuff before endpoint selection */
843 gspca_dev->alt = 0;
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300844 if (gspca_dev->sd_desc->isoc_init) {
845 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
846 if (ret < 0)
Hans de Goede314b3e02010-10-26 05:15:46 -0300847 goto unlock;
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300848 }
Jean-François Moine27b12022011-08-10 07:38:48 -0300849 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
850 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
851 : USB_ENDPOINT_XFER_ISOC;
Hans de Goede314b3e02010-10-26 05:15:46 -0300852
Jean-François Moine27b12022011-08-10 07:38:48 -0300853 /* if the subdriver forced an altsetting, get the endpoint */
854 if (gspca_dev->alt != 0) {
855 gspca_dev->alt--; /* (previous version compatibility) */
856 ep = alt_xfer(&intf->altsetting[gspca_dev->alt], xfer);
857 if (ep == NULL) {
858 pr_err("bad altsetting %d\n", gspca_dev->alt);
859 ret = -EIO;
860 goto out;
861 }
862 ep_tb[0].alt = gspca_dev->alt;
863 alt_idx = 1;
864 } else {
865
866 /* else, compute the minimum bandwidth
867 * and build the endpoint table */
868 alt_idx = build_ep_tb(gspca_dev, intf, xfer, ep_tb);
869 if (alt_idx <= 0) {
870 pr_err("no transfer endpoint found\n");
871 ret = -EIO;
872 goto unlock;
873 }
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300874 }
Jean-François Moine27b12022011-08-10 07:38:48 -0300875
876 /* set the highest alternate setting and
877 * loop until urb submit succeeds */
878 gspca_input_destroy_urb(gspca_dev);
879
880 gspca_dev->alt = ep_tb[--alt_idx].alt;
881 alt = -1;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300882 for (;;) {
Jean-François Moine27b12022011-08-10 07:38:48 -0300883 if (alt != gspca_dev->alt) {
884 alt = gspca_dev->alt;
885 if (gspca_dev->nbalt > 1) {
886 ret = usb_set_interface(gspca_dev->dev,
887 gspca_dev->iface,
888 alt);
889 if (ret < 0) {
890 pr_err("set alt %d err %d\n", alt, ret);
891 goto out;
892 }
893 }
894 }
Jean-Francois Moine47aaca92009-12-15 05:23:04 -0300895 if (!gspca_dev->cam.no_urb_create) {
Jean-François Moine27b12022011-08-10 07:38:48 -0300896 PDEBUG(D_STREAM, "init transfer alt %d", alt);
897 ret = create_urbs(gspca_dev,
898 alt_xfer(&intf->altsetting[alt], xfer));
Jean-Francois Moine47aaca92009-12-15 05:23:04 -0300899 if (ret < 0) {
900 destroy_urbs(gspca_dev);
901 goto out;
902 }
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300903 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300904
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300905 /* clear the bulk endpoint */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300906 if (gspca_dev->cam.bulk)
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300907 usb_clear_halt(gspca_dev->dev,
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300908 gspca_dev->urb[0]->pipe);
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300909
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300910 /* start the cam */
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300911 ret = gspca_dev->sd_desc->start(gspca_dev);
912 if (ret < 0) {
913 destroy_urbs(gspca_dev);
914 goto out;
915 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300916 gspca_dev->streaming = 1;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300917
Antonio Ospitedff369a2008-11-14 07:53:32 -0300918 /* some bulk transfers are started by the subdriver */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300919 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300920 break;
921
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300922 /* submit the URBs */
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300923 for (n = 0; n < MAX_NURBS; n++) {
924 urb = gspca_dev->urb[n];
925 if (urb == NULL)
926 break;
927 ret = usb_submit_urb(urb, GFP_KERNEL);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300928 if (ret < 0)
929 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300930 }
931 if (ret >= 0)
Jean-François Moine27b12022011-08-10 07:38:48 -0300932 break; /* transfer is started */
933
934 /* something when wrong
935 * stop the webcam and free the transfer resources */
Jean-François Moinee4dac282010-04-09 06:11:36 -0300936 gspca_stream_off(gspca_dev);
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300937 if (ret != -ENOSPC) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300938 pr_err("usb_submit_urb alt %d err %d\n",
939 gspca_dev->alt, ret);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300940 goto out;
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300941 }
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300942
943 /* the bandwidth is not wide enough
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300944 * negotiate or try a lower alternate setting */
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300945 PDEBUG(D_ERR|D_STREAM,
Jean-François Moine27b12022011-08-10 07:38:48 -0300946 "alt %d - bandwidth not wide enough - trying again",
947 alt);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300948 msleep(20); /* wait for kill complete */
949 if (gspca_dev->sd_desc->isoc_nego) {
950 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
951 if (ret < 0)
952 goto out;
953 } else {
Jean-François Moine27b12022011-08-10 07:38:48 -0300954 if (alt_idx <= 0) {
955 pr_err("no transfer endpoint found\n");
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300956 ret = -EIO;
957 goto out;
958 }
Jean-François Moine27b12022011-08-10 07:38:48 -0300959 alt = ep_tb[--alt_idx].alt;
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300960 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300961 }
962out:
Hans de Goede314b3e02010-10-26 05:15:46 -0300963 gspca_input_create_urb(gspca_dev);
964unlock:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300965 mutex_unlock(&gspca_dev->usb_lock);
966 return ret;
967}
968
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300969static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300970{
Jean-François Moine6a330912010-10-02 04:12:25 -0300971 struct gspca_ctrl *ctrl;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300972 int i;
973
974 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
975 gspca_dev->curr_mode = i;
976 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
977 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300978 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-François Moine6a330912010-10-02 04:12:25 -0300979
980 /* set the current control values to their default values
981 * which may have changed in sd_init() */
982 ctrl = gspca_dev->cam.ctrls;
983 if (ctrl != NULL) {
984 for (i = 0;
985 i < gspca_dev->sd_desc->nctrls;
986 i++, ctrl++)
987 ctrl->val = ctrl->def;
988 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300989}
990
991static int wxh_to_mode(struct gspca_dev *gspca_dev,
992 int width, int height)
993{
994 int i;
995
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300996 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
997 if (width >= gspca_dev->cam.cam_mode[i].width
998 && height >= gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300999 break;
1000 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001001 return i;
1002}
1003
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001004/*
1005 * search a mode with the right pixel format
1006 */
1007static int gspca_get_mode(struct gspca_dev *gspca_dev,
1008 int mode,
1009 int pixfmt)
1010{
1011 int modeU, modeD;
1012
1013 modeU = modeD = mode;
1014 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
1015 if (--modeD >= 0) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001016 if (gspca_dev->cam.cam_mode[modeD].pixelformat
1017 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001018 return modeD;
1019 }
1020 if (++modeU < gspca_dev->cam.nmodes) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001021 if (gspca_dev->cam.cam_mode[modeU].pixelformat
1022 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001023 return modeU;
1024 }
1025 }
1026 return -EINVAL;
1027}
1028
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03001029#ifdef CONFIG_VIDEO_ADV_DEBUG
1030static int vidioc_g_register(struct file *file, void *priv,
1031 struct v4l2_dbg_register *reg)
1032{
1033 int ret;
1034 struct gspca_dev *gspca_dev = priv;
1035
1036 if (!gspca_dev->sd_desc->get_chip_ident)
1037 return -EINVAL;
1038
1039 if (!gspca_dev->sd_desc->get_register)
1040 return -EINVAL;
1041
1042 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1043 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001044 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03001045 if (gspca_dev->present)
1046 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
1047 else
1048 ret = -ENODEV;
1049 mutex_unlock(&gspca_dev->usb_lock);
1050
1051 return ret;
1052}
1053
1054static int vidioc_s_register(struct file *file, void *priv,
1055 struct v4l2_dbg_register *reg)
1056{
1057 int ret;
1058 struct gspca_dev *gspca_dev = priv;
1059
1060 if (!gspca_dev->sd_desc->get_chip_ident)
1061 return -EINVAL;
1062
1063 if (!gspca_dev->sd_desc->set_register)
1064 return -EINVAL;
1065
1066 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1067 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001068 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03001069 if (gspca_dev->present)
1070 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
1071 else
1072 ret = -ENODEV;
1073 mutex_unlock(&gspca_dev->usb_lock);
1074
1075 return ret;
1076}
1077#endif
1078
1079static int vidioc_g_chip_ident(struct file *file, void *priv,
1080 struct v4l2_dbg_chip_ident *chip)
1081{
1082 int ret;
1083 struct gspca_dev *gspca_dev = priv;
1084
1085 if (!gspca_dev->sd_desc->get_chip_ident)
1086 return -EINVAL;
1087
1088 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1089 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001090 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03001091 if (gspca_dev->present)
1092 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
1093 else
1094 ret = -ENODEV;
1095 mutex_unlock(&gspca_dev->usb_lock);
1096
1097 return ret;
1098}
1099
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001100static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001101 struct v4l2_fmtdesc *fmtdesc)
1102{
1103 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001104 int i, j, index;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001105 __u32 fmt_tb[8];
1106
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001107 /* give an index to each format */
1108 index = 0;
1109 j = 0;
1110 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001111 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001112 j = 0;
1113 for (;;) {
1114 if (fmt_tb[j] == fmt_tb[index])
1115 break;
1116 j++;
1117 }
1118 if (j == index) {
1119 if (fmtdesc->index == index)
1120 break; /* new format */
1121 index++;
Julia Lawall80297122008-11-12 23:18:21 -03001122 if (index >= ARRAY_SIZE(fmt_tb))
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001123 return -EINVAL;
1124 }
1125 }
1126 if (i < 0)
1127 return -EINVAL; /* no more format */
1128
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001129 fmtdesc->pixelformat = fmt_tb[index];
Jean-François Moineb796fb62011-08-10 07:32:57 -03001130 if (gspca_dev->cam.cam_mode[i].sizeimage <
1131 gspca_dev->cam.cam_mode[i].width *
1132 gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001133 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
1134 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
1135 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
1136 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
1137 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
1138 fmtdesc->description[4] = '\0';
1139 return 0;
1140}
1141
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001142static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001143 struct v4l2_format *fmt)
1144{
1145 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001146 int mode;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001147
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001148 mode = gspca_dev->curr_mode;
1149 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1150 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001151 return 0;
1152}
1153
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001154static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001155 struct v4l2_format *fmt)
1156{
Hans de Goede24c530b2008-07-04 17:55:18 -03001157 int w, h, mode, mode2;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001158
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001159 w = fmt->fmt.pix.width;
1160 h = fmt->fmt.pix.height;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001161
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001162#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001163 if (gspca_debug & D_CONF)
1164 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
1165#endif
1166 /* search the closest mode for width and height */
1167 mode = wxh_to_mode(gspca_dev, w, h);
1168
1169 /* OK if right palette */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001170 if (gspca_dev->cam.cam_mode[mode].pixelformat
1171 != fmt->fmt.pix.pixelformat) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001172
1173 /* else, search the closest mode with the same pixel format */
1174 mode2 = gspca_get_mode(gspca_dev, mode,
1175 fmt->fmt.pix.pixelformat);
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001176 if (mode2 >= 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001177 mode = mode2;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001178/* else
1179 ; * no chance, return this mode */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001180 }
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001181 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1182 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001183 return mode; /* used when s_fmt */
1184}
1185
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001186static int vidioc_try_fmt_vid_cap(struct file *file,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001187 void *priv,
1188 struct v4l2_format *fmt)
1189{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001190 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001191 int ret;
1192
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001193 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001194 if (ret < 0)
1195 return ret;
1196 return 0;
1197}
1198
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001199static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001200 struct v4l2_format *fmt)
1201{
1202 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001203 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001204
Hans de Goedee2997a72008-04-23 08:09:12 -03001205 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1206 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001207
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001208 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001209 if (ret < 0)
1210 goto out;
1211
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001212 if (gspca_dev->nframes != 0
1213 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
1214 ret = -EINVAL;
1215 goto out;
1216 }
1217
Jean-Francois Moine50a871f2008-06-30 19:47:33 -03001218 if (ret == gspca_dev->curr_mode) {
1219 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001220 goto out; /* same mode */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -03001221 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001222
1223 if (gspca_dev->streaming) {
1224 ret = -EBUSY;
1225 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -03001226 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001227 gspca_dev->width = fmt->fmt.pix.width;
1228 gspca_dev->height = fmt->fmt.pix.height;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001229 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
1230 gspca_dev->curr_mode = ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001231
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001232 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001233out:
1234 mutex_unlock(&gspca_dev->queue_lock);
1235 return ret;
1236}
1237
Hans de Goede11c635a2009-05-25 15:04:22 -03001238static int vidioc_enum_framesizes(struct file *file, void *priv,
1239 struct v4l2_frmsizeenum *fsize)
1240{
1241 struct gspca_dev *gspca_dev = priv;
1242 int i;
1243 __u32 index = 0;
1244
1245 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
1246 if (fsize->pixel_format !=
1247 gspca_dev->cam.cam_mode[i].pixelformat)
1248 continue;
1249
1250 if (fsize->index == index) {
1251 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1252 fsize->discrete.width =
1253 gspca_dev->cam.cam_mode[i].width;
1254 fsize->discrete.height =
1255 gspca_dev->cam.cam_mode[i].height;
1256 return 0;
1257 }
1258 index++;
1259 }
1260
1261 return -EINVAL;
1262}
1263
Antonio Ospite28ffe772009-12-02 06:18:46 -03001264static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1265 struct v4l2_frmivalenum *fival)
1266{
1267 struct gspca_dev *gspca_dev = priv;
1268 int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
1269 __u32 i;
1270
1271 if (gspca_dev->cam.mode_framerates == NULL ||
1272 gspca_dev->cam.mode_framerates[mode].nrates == 0)
1273 return -EINVAL;
1274
1275 if (fival->pixel_format !=
1276 gspca_dev->cam.cam_mode[mode].pixelformat)
1277 return -EINVAL;
1278
1279 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1280 if (fival->index == i) {
1281 fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1282 fival->discrete.numerator = 1;
1283 fival->discrete.denominator =
1284 gspca_dev->cam.mode_framerates[mode].rates[i];
1285 return 0;
1286 }
1287 }
1288
1289 return -EINVAL;
1290}
1291
Hans de Goeded0182302008-11-19 17:10:47 -03001292static void gspca_release(struct video_device *vfd)
Frank Zago6b060ff2008-09-28 08:12:22 -03001293{
Hans de Goeded0182302008-11-19 17:10:47 -03001294 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
Frank Zago6b060ff2008-09-28 08:12:22 -03001295
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001296 PDEBUG(D_PROBE, "%s released",
1297 video_device_node_name(&gspca_dev->vdev));
Frank Zago6b060ff2008-09-28 08:12:22 -03001298
1299 kfree(gspca_dev->usb_buf);
1300 kfree(gspca_dev);
1301}
1302
Hans Verkuilbec43662008-12-30 06:58:20 -03001303static int dev_open(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001304{
1305 struct gspca_dev *gspca_dev;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001306
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001307 PDEBUG(D_STREAM, "[%s] open", current->comm);
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001308 gspca_dev = (struct gspca_dev *) video_devdata(file);
Hans de Goede7f6eb112010-12-30 20:20:09 -03001309 if (!gspca_dev->present)
1310 return -ENODEV;
Jean-Francois Moine5c4fa002008-11-18 15:52:31 -03001311
1312 /* protect the subdriver against rmmod */
Hans de Goede7f6eb112010-12-30 20:20:09 -03001313 if (!try_module_get(gspca_dev->module))
1314 return -ENODEV;
Frank Zago6b060ff2008-09-28 08:12:22 -03001315
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001316 file->private_data = gspca_dev;
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001317#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001318 /* activate the v4l2 debug */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001319 if (gspca_debug & D_V4L2)
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001320 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001321 | V4L2_DEBUG_IOCTL_ARG;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001322 else
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001323 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001324 | V4L2_DEBUG_IOCTL_ARG);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001325#endif
Hans de Goede7f6eb112010-12-30 20:20:09 -03001326 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001327}
1328
Hans Verkuilbec43662008-12-30 06:58:20 -03001329static int dev_close(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001330{
1331 struct gspca_dev *gspca_dev = file->private_data;
1332
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001333 PDEBUG(D_STREAM, "[%s] close", current->comm);
Hans de Goedee2997a72008-04-23 08:09:12 -03001334 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1335 return -ERESTARTSYS;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001336
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001337 /* if the file did the capture, free the streaming resources */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001338 if (gspca_dev->capt_file == file) {
Jean-Francois Moinea1e82882008-09-03 17:12:17 -03001339 if (gspca_dev->streaming) {
1340 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001341 gspca_dev->usb_err = 0;
Jean-Francois Moinea1e82882008-09-03 17:12:17 -03001342 gspca_stream_off(gspca_dev);
1343 mutex_unlock(&gspca_dev->usb_lock);
1344 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001345 frame_free(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001346 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001347 file->private_data = NULL;
Jean-Francois Moine5c4fa002008-11-18 15:52:31 -03001348 module_put(gspca_dev->module);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001349 mutex_unlock(&gspca_dev->queue_lock);
Frank Zago6b060ff2008-09-28 08:12:22 -03001350
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001351 PDEBUG(D_STREAM, "close done");
Frank Zago6b060ff2008-09-28 08:12:22 -03001352
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001353 return 0;
1354}
1355
1356static int vidioc_querycap(struct file *file, void *priv,
1357 struct v4l2_capability *cap)
1358{
1359 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001360 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001361
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001362 /* protect the access to the usb device */
1363 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1364 return -ERESTARTSYS;
1365 if (!gspca_dev->present) {
1366 ret = -ENODEV;
1367 goto out;
1368 }
Hans de Goedee7d712c2011-06-05 14:58:46 -03001369 strlcpy((char *) cap->driver, gspca_dev->sd_desc->name,
Jean-François Moined8518592010-12-28 06:55:01 -03001370 sizeof cap->driver);
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -03001371 if (gspca_dev->dev->product != NULL) {
Hans de Goedee7d712c2011-06-05 14:58:46 -03001372 strlcpy((char *) cap->card, gspca_dev->dev->product,
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -03001373 sizeof cap->card);
1374 } else {
Jean-François Moined8518592010-12-28 06:55:01 -03001375 snprintf((char *) cap->card, sizeof cap->card,
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -03001376 "USB Camera (%04x:%04x)",
1377 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1378 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1379 }
Jean-François Moined8518592010-12-28 06:55:01 -03001380 usb_make_path(gspca_dev->dev, (char *) cap->bus_info,
1381 sizeof(cap->bus_info));
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001382 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1383 | V4L2_CAP_STREAMING
1384 | V4L2_CAP_READWRITE;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001385 ret = 0;
1386out:
1387 mutex_unlock(&gspca_dev->usb_lock);
1388 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001389}
1390
Jean-François Moine6a330912010-10-02 04:12:25 -03001391static int get_ctrl(struct gspca_dev *gspca_dev,
Hans de Goedea0001a22009-06-10 04:52:18 -03001392 int id)
1393{
1394 const struct ctrl *ctrls;
1395 int i;
1396
1397 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1398 i < gspca_dev->sd_desc->nctrls;
1399 i++, ctrls++) {
1400 if (gspca_dev->ctrl_dis & (1 << i))
1401 continue;
1402 if (id == ctrls->qctrl.id)
Jean-François Moine6a330912010-10-02 04:12:25 -03001403 return i;
Hans de Goedea0001a22009-06-10 04:52:18 -03001404 }
Jean-François Moine6a330912010-10-02 04:12:25 -03001405 return -1;
Hans de Goedea0001a22009-06-10 04:52:18 -03001406}
1407
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001408static int vidioc_queryctrl(struct file *file, void *priv,
1409 struct v4l2_queryctrl *q_ctrl)
1410{
1411 struct gspca_dev *gspca_dev = priv;
Hans de Goedea0001a22009-06-10 04:52:18 -03001412 const struct ctrl *ctrls;
Jean-François Moine6a330912010-10-02 04:12:25 -03001413 struct gspca_ctrl *gspca_ctrl;
1414 int i, idx;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001415 u32 id;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001416
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001417 id = q_ctrl->id;
1418 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1419 id &= V4L2_CTRL_ID_MASK;
1420 id++;
Jean-François Moine6a330912010-10-02 04:12:25 -03001421 idx = -1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001422 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001423 if (gspca_dev->ctrl_dis & (1 << i))
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001424 continue;
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001425 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001426 continue;
Jean-François Moine6a330912010-10-02 04:12:25 -03001427 if (idx >= 0
1428 && gspca_dev->sd_desc->ctrls[i].qctrl.id
Jean-François Moine780e3122010-10-19 04:29:10 -03001429 > gspca_dev->sd_desc->ctrls[idx].qctrl.id)
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001430 continue;
Jean-François Moine6a330912010-10-02 04:12:25 -03001431 idx = i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001432 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001433 } else {
Jean-François Moine6a330912010-10-02 04:12:25 -03001434 idx = get_ctrl(gspca_dev, id);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001435 }
Jean-François Moine6a330912010-10-02 04:12:25 -03001436 if (idx < 0)
1437 return -EINVAL;
1438 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1439 memcpy(q_ctrl, &ctrls->qctrl, sizeof *q_ctrl);
1440 if (gspca_dev->cam.ctrls != NULL) {
1441 gspca_ctrl = &gspca_dev->cam.ctrls[idx];
1442 q_ctrl->default_value = gspca_ctrl->def;
1443 q_ctrl->minimum = gspca_ctrl->min;
1444 q_ctrl->maximum = gspca_ctrl->max;
1445 }
1446 if (gspca_dev->ctrl_inac & (1 << idx))
Jean-Francois Moine4af85662009-11-12 15:59:27 -03001447 q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001448 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001449}
1450
1451static int vidioc_s_ctrl(struct file *file, void *priv,
1452 struct v4l2_control *ctrl)
1453{
1454 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001455 const struct ctrl *ctrls;
Jean-François Moine6a330912010-10-02 04:12:25 -03001456 struct gspca_ctrl *gspca_ctrl;
1457 int idx, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001458
Jean-François Moine6a330912010-10-02 04:12:25 -03001459 idx = get_ctrl(gspca_dev, ctrl->id);
1460 if (idx < 0)
Hans de Goedea0001a22009-06-10 04:52:18 -03001461 return -EINVAL;
Jean-François Moine6a330912010-10-02 04:12:25 -03001462 if (gspca_dev->ctrl_inac & (1 << idx))
1463 return -EINVAL;
1464 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1465 if (gspca_dev->cam.ctrls != NULL) {
1466 gspca_ctrl = &gspca_dev->cam.ctrls[idx];
1467 if (ctrl->value < gspca_ctrl->min
1468 || ctrl->value > gspca_ctrl->max)
1469 return -ERANGE;
1470 } else {
1471 gspca_ctrl = NULL;
1472 if (ctrl->value < ctrls->qctrl.minimum
1473 || ctrl->value > ctrls->qctrl.maximum)
1474 return -ERANGE;
1475 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001476 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1477 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1478 return -ERESTARTSYS;
Jean-François Moine6a330912010-10-02 04:12:25 -03001479 if (!gspca_dev->present) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001480 ret = -ENODEV;
Jean-François Moine6a330912010-10-02 04:12:25 -03001481 goto out;
1482 }
1483 gspca_dev->usb_err = 0;
1484 if (ctrls->set != NULL) {
1485 ret = ctrls->set(gspca_dev, ctrl->value);
1486 goto out;
1487 }
1488 if (gspca_ctrl != NULL) {
1489 gspca_ctrl->val = ctrl->value;
1490 if (ctrls->set_control != NULL
1491 && gspca_dev->streaming)
1492 ctrls->set_control(gspca_dev);
1493 }
1494 ret = gspca_dev->usb_err;
1495out:
Hans de Goedea0001a22009-06-10 04:52:18 -03001496 mutex_unlock(&gspca_dev->usb_lock);
1497 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001498}
1499
1500static int vidioc_g_ctrl(struct file *file, void *priv,
1501 struct v4l2_control *ctrl)
1502{
1503 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001504 const struct ctrl *ctrls;
Jean-François Moine6a330912010-10-02 04:12:25 -03001505 int idx, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001506
Jean-François Moine6a330912010-10-02 04:12:25 -03001507 idx = get_ctrl(gspca_dev, ctrl->id);
1508 if (idx < 0)
Hans de Goedea0001a22009-06-10 04:52:18 -03001509 return -EINVAL;
Jean-François Moine6a330912010-10-02 04:12:25 -03001510 ctrls = &gspca_dev->sd_desc->ctrls[idx];
Hans de Goedea0001a22009-06-10 04:52:18 -03001511
1512 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1513 return -ERESTARTSYS;
Jean-François Moine6a330912010-10-02 04:12:25 -03001514 if (!gspca_dev->present) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001515 ret = -ENODEV;
Jean-François Moine6a330912010-10-02 04:12:25 -03001516 goto out;
1517 }
1518 gspca_dev->usb_err = 0;
1519 if (ctrls->get != NULL) {
1520 ret = ctrls->get(gspca_dev, &ctrl->value);
1521 goto out;
1522 }
1523 if (gspca_dev->cam.ctrls != NULL)
1524 ctrl->value = gspca_dev->cam.ctrls[idx].val;
1525 ret = 0;
1526out:
Hans de Goedea0001a22009-06-10 04:52:18 -03001527 mutex_unlock(&gspca_dev->usb_lock);
1528 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001529}
1530
1531static int vidioc_querymenu(struct file *file, void *priv,
1532 struct v4l2_querymenu *qmenu)
1533{
1534 struct gspca_dev *gspca_dev = priv;
1535
1536 if (!gspca_dev->sd_desc->querymenu)
1537 return -EINVAL;
1538 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1539}
1540
1541static int vidioc_enum_input(struct file *file, void *priv,
1542 struct v4l2_input *input)
1543{
1544 struct gspca_dev *gspca_dev = priv;
1545
1546 if (input->index != 0)
1547 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001548 input->type = V4L2_INPUT_TYPE_CAMERA;
Adam Bakerdfa76fa2009-03-29 19:17:10 -03001549 input->status = gspca_dev->cam.input_flags;
Hans de Goedee7d712c2011-06-05 14:58:46 -03001550 strlcpy(input->name, gspca_dev->sd_desc->name,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001551 sizeof input->name);
1552 return 0;
1553}
1554
1555static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1556{
1557 *i = 0;
1558 return 0;
1559}
1560
1561static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1562{
1563 if (i > 0)
1564 return -EINVAL;
1565 return (0);
1566}
1567
1568static int vidioc_reqbufs(struct file *file, void *priv,
1569 struct v4l2_requestbuffers *rb)
1570{
1571 struct gspca_dev *gspca_dev = priv;
Jean-François Moine832d0a92010-04-09 06:07:58 -03001572 int i, ret = 0, streaming;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001573
Jean-François Moinea2ba6f22010-07-14 06:21:16 -03001574 i = rb->memory; /* (avoid compilation warning) */
1575 switch (i) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001576 case GSPCA_MEMORY_READ: /* (internal call) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001577 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001578 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001579 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001580 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001581 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001582 }
Hans de Goedee2997a72008-04-23 08:09:12 -03001583 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1584 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001585
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001586 if (gspca_dev->memory != GSPCA_MEMORY_NO
Hans de Goedece5610b2010-12-31 07:41:54 -03001587 && gspca_dev->memory != GSPCA_MEMORY_READ
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001588 && gspca_dev->memory != rb->memory) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001589 ret = -EBUSY;
1590 goto out;
1591 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001592
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001593 /* only one file may do the capture */
1594 if (gspca_dev->capt_file != NULL
1595 && gspca_dev->capt_file != file) {
1596 ret = -EBUSY;
1597 goto out;
1598 }
1599
1600 /* if allocated, the buffers must not be mapped */
1601 for (i = 0; i < gspca_dev->nframes; i++) {
1602 if (gspca_dev->frame[i].vma_use_count) {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001603 ret = -EBUSY;
1604 goto out;
1605 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001606 }
1607
1608 /* stop streaming */
Jean-François Moine832d0a92010-04-09 06:07:58 -03001609 streaming = gspca_dev->streaming;
1610 if (streaming) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001611 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001612 gspca_dev->usb_err = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001613 gspca_stream_off(gspca_dev);
1614 mutex_unlock(&gspca_dev->usb_lock);
Jean-François Moine59929ce2011-03-14 05:33:01 -03001615
1616 /* Don't restart the stream when switching from read
1617 * to mmap mode */
1618 if (gspca_dev->memory == GSPCA_MEMORY_READ)
1619 streaming = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001620 }
1621
1622 /* free the previous allocated buffers, if any */
Hans de Goeded642de22010-12-31 04:51:36 -03001623 if (gspca_dev->nframes != 0)
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001624 frame_free(gspca_dev);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001625 if (rb->count == 0) /* unrequest */
1626 goto out;
Hans de Goeded642de22010-12-31 04:51:36 -03001627 ret = frame_alloc(gspca_dev, file, rb->memory, rb->count);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001628 if (ret == 0) {
1629 rb->count = gspca_dev->nframes;
Jean-François Moine832d0a92010-04-09 06:07:58 -03001630 if (streaming)
1631 ret = gspca_init_transfer(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001632 }
1633out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001634 mutex_unlock(&gspca_dev->queue_lock);
1635 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1636 return ret;
1637}
1638
1639static int vidioc_querybuf(struct file *file, void *priv,
1640 struct v4l2_buffer *v4l2_buf)
1641{
1642 struct gspca_dev *gspca_dev = priv;
1643 struct gspca_frame *frame;
1644
Jean-Francois Moine13752bd2008-12-19 15:08:51 -03001645 if (v4l2_buf->index < 0
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001646 || v4l2_buf->index >= gspca_dev->nframes)
1647 return -EINVAL;
1648
1649 frame = &gspca_dev->frame[v4l2_buf->index];
1650 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1651 return 0;
1652}
1653
1654static int vidioc_streamon(struct file *file, void *priv,
1655 enum v4l2_buf_type buf_type)
1656{
1657 struct gspca_dev *gspca_dev = priv;
1658 int ret;
1659
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001660 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1661 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001662 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1663 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001664
Jean-François Moine18bf99a2010-04-09 06:06:11 -03001665 /* check the capture file */
1666 if (gspca_dev->capt_file != file) {
1667 ret = -EBUSY;
1668 goto out;
1669 }
1670
Jean-Francois Moine1edabe72008-12-19 15:17:25 -03001671 if (gspca_dev->nframes == 0
1672 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001673 ret = -EINVAL;
1674 goto out;
1675 }
1676 if (!gspca_dev->streaming) {
1677 ret = gspca_init_transfer(gspca_dev);
1678 if (ret < 0)
1679 goto out;
1680 }
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001681#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001682 if (gspca_debug & D_STREAM) {
1683 PDEBUG_MODE("stream on OK",
1684 gspca_dev->pixfmt,
1685 gspca_dev->width,
1686 gspca_dev->height);
1687 }
1688#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001689 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001690out:
1691 mutex_unlock(&gspca_dev->queue_lock);
1692 return ret;
1693}
1694
1695static int vidioc_streamoff(struct file *file, void *priv,
1696 enum v4l2_buf_type buf_type)
1697{
1698 struct gspca_dev *gspca_dev = priv;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001699 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001700
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001701 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1702 return -EINVAL;
Hans de Goede4e770f72010-12-30 13:11:21 -03001703
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001704 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1705 return -ERESTARTSYS;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001706
Hans de Goede4e770f72010-12-30 13:11:21 -03001707 if (!gspca_dev->streaming) {
1708 ret = 0;
1709 goto out;
1710 }
1711
Jean-François Moine18bf99a2010-04-09 06:06:11 -03001712 /* check the capture file */
1713 if (gspca_dev->capt_file != file) {
1714 ret = -EBUSY;
1715 goto out;
1716 }
1717
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001718 /* stop streaming */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001719 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1720 ret = -ERESTARTSYS;
1721 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001722 }
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001723 gspca_dev->usb_err = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001724 gspca_stream_off(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001725 mutex_unlock(&gspca_dev->usb_lock);
Hans de Goede0d0ae152010-12-31 17:17:51 -03001726 /* In case another thread is waiting in dqbuf */
1727 wake_up_interruptible(&gspca_dev->wq);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001728
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001729 /* empty the transfer queues */
1730 atomic_set(&gspca_dev->fr_q, 0);
1731 atomic_set(&gspca_dev->fr_i, 0);
1732 gspca_dev->fr_o = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001733 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001734out:
1735 mutex_unlock(&gspca_dev->queue_lock);
1736 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001737}
1738
1739static int vidioc_g_jpegcomp(struct file *file, void *priv,
1740 struct v4l2_jpegcompression *jpegcomp)
1741{
1742 struct gspca_dev *gspca_dev = priv;
1743 int ret;
1744
1745 if (!gspca_dev->sd_desc->get_jcomp)
1746 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001747 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1748 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001749 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001750 if (gspca_dev->present)
1751 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1752 else
1753 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001754 mutex_unlock(&gspca_dev->usb_lock);
1755 return ret;
1756}
1757
1758static int vidioc_s_jpegcomp(struct file *file, void *priv,
1759 struct v4l2_jpegcompression *jpegcomp)
1760{
1761 struct gspca_dev *gspca_dev = priv;
1762 int ret;
1763
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001764 if (!gspca_dev->sd_desc->set_jcomp)
1765 return -EINVAL;
Jim Parisf86d4a92008-12-10 05:50:44 -03001766 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1767 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001768 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001769 if (gspca_dev->present)
1770 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1771 else
1772 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001773 mutex_unlock(&gspca_dev->usb_lock);
1774 return ret;
1775}
1776
1777static int vidioc_g_parm(struct file *filp, void *priv,
1778 struct v4l2_streamparm *parm)
1779{
1780 struct gspca_dev *gspca_dev = priv;
1781
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001782 parm->parm.capture.readbuffers = gspca_dev->nbufread;
Jim Paris627a5ef2008-12-10 06:02:42 -03001783
1784 if (gspca_dev->sd_desc->get_streamparm) {
1785 int ret;
1786
1787 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1788 return -ERESTARTSYS;
Jean-François Moine668f44a2010-12-25 13:46:14 -03001789 if (gspca_dev->present) {
1790 gspca_dev->usb_err = 0;
1791 gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
1792 ret = gspca_dev->usb_err;
1793 } else {
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001794 ret = -ENODEV;
Jean-François Moine668f44a2010-12-25 13:46:14 -03001795 }
Jim Paris627a5ef2008-12-10 06:02:42 -03001796 mutex_unlock(&gspca_dev->usb_lock);
1797 return ret;
1798 }
1799
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001800 return 0;
1801}
1802
1803static int vidioc_s_parm(struct file *filp, void *priv,
1804 struct v4l2_streamparm *parm)
1805{
1806 struct gspca_dev *gspca_dev = priv;
1807 int n;
1808
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001809 n = parm->parm.capture.readbuffers;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001810 if (n == 0 || n >= GSPCA_MAX_FRAMES)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001811 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1812 else
1813 gspca_dev->nbufread = n;
Jim Paris627a5ef2008-12-10 06:02:42 -03001814
1815 if (gspca_dev->sd_desc->set_streamparm) {
1816 int ret;
1817
1818 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1819 return -ERESTARTSYS;
Jean-François Moine668f44a2010-12-25 13:46:14 -03001820 if (gspca_dev->present) {
1821 gspca_dev->usb_err = 0;
1822 gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
1823 ret = gspca_dev->usb_err;
1824 } else {
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001825 ret = -ENODEV;
Jean-François Moine668f44a2010-12-25 13:46:14 -03001826 }
Jim Paris627a5ef2008-12-10 06:02:42 -03001827 mutex_unlock(&gspca_dev->usb_lock);
1828 return ret;
1829 }
1830
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001831 return 0;
1832}
1833
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001834static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1835{
1836 struct gspca_dev *gspca_dev = file->private_data;
Harvey Harrisonefab8212008-07-05 06:12:47 -03001837 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001838 struct page *page;
1839 unsigned long addr, start, size;
1840 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001841
1842 start = vma->vm_start;
1843 size = vma->vm_end - vma->vm_start;
1844 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1845
Hans de Goedee2997a72008-04-23 08:09:12 -03001846 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1847 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001848 if (!gspca_dev->present) {
1849 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001850 goto out;
1851 }
1852 if (gspca_dev->capt_file != file) {
1853 ret = -EINVAL;
1854 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001855 }
1856
Harvey Harrisonefab8212008-07-05 06:12:47 -03001857 frame = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001858 for (i = 0; i < gspca_dev->nframes; ++i) {
1859 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1860 PDEBUG(D_STREAM, "mmap bad memory type");
1861 break;
1862 }
1863 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1864 == vma->vm_pgoff) {
1865 frame = &gspca_dev->frame[i];
1866 break;
1867 }
1868 }
Harvey Harrisonefab8212008-07-05 06:12:47 -03001869 if (frame == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001870 PDEBUG(D_STREAM, "mmap no frame buffer found");
1871 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001872 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001873 }
Jean-François Moine38bff692010-06-24 05:19:58 -03001874 if (size != frame->v4l2_buf.length) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001875 PDEBUG(D_STREAM, "mmap bad size");
1876 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001877 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001878 }
1879
1880 /*
1881 * - VM_IO marks the area as being a mmaped region for I/O to a
1882 * device. It also prevents the region from being core dumped.
1883 */
1884 vma->vm_flags |= VM_IO;
1885
1886 addr = (unsigned long) frame->data;
1887 while (size > 0) {
1888 page = vmalloc_to_page((void *) addr);
1889 ret = vm_insert_page(vma, start, page);
1890 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001891 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001892 start += PAGE_SIZE;
1893 addr += PAGE_SIZE;
1894 size -= PAGE_SIZE;
1895 }
1896
Jean-Francois Moined50e2572009-11-26 15:36:40 -03001897 vma->vm_ops = &gspca_vm_ops;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001898 vma->vm_private_data = frame;
1899 gspca_vm_open(vma);
Hans de Goedee2997a72008-04-23 08:09:12 -03001900 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001901out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001902 mutex_unlock(&gspca_dev->queue_lock);
1903 return ret;
1904}
1905
Hans de Goede27074ef2010-12-30 19:54:33 -03001906static int frame_ready_nolock(struct gspca_dev *gspca_dev, struct file *file,
1907 enum v4l2_memory memory)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001908{
Hans de Goede27074ef2010-12-30 19:54:33 -03001909 if (!gspca_dev->present)
1910 return -ENODEV;
1911 if (gspca_dev->capt_file != file || gspca_dev->memory != memory ||
1912 !gspca_dev->streaming)
1913 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001914
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001915 /* check if a frame is ready */
Hans de Goede27074ef2010-12-30 19:54:33 -03001916 return gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i);
1917}
Hans de Goede181b7042008-11-19 17:16:26 -03001918
Hans de Goede27074ef2010-12-30 19:54:33 -03001919static int frame_ready(struct gspca_dev *gspca_dev, struct file *file,
1920 enum v4l2_memory memory)
1921{
1922 int ret;
Hans de Goede181b7042008-11-19 17:16:26 -03001923
Hans de Goede27074ef2010-12-30 19:54:33 -03001924 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1925 return -ERESTARTSYS;
1926 ret = frame_ready_nolock(gspca_dev, file, memory);
1927 mutex_unlock(&gspca_dev->queue_lock);
1928 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001929}
1930
1931/*
1932 * dequeue a video buffer
1933 *
1934 * If nonblock_ing is false, block until a buffer is available.
1935 */
1936static int vidioc_dqbuf(struct file *file, void *priv,
1937 struct v4l2_buffer *v4l2_buf)
1938{
1939 struct gspca_dev *gspca_dev = priv;
1940 struct gspca_frame *frame;
Hans de Goede27074ef2010-12-30 19:54:33 -03001941 int i, j, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001942
1943 PDEBUG(D_FRAM, "dqbuf");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001944
Hans de Goede27074ef2010-12-30 19:54:33 -03001945 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001946 return -ERESTARTSYS;
1947
Hans de Goede27074ef2010-12-30 19:54:33 -03001948 for (;;) {
1949 ret = frame_ready_nolock(gspca_dev, file, v4l2_buf->memory);
1950 if (ret < 0)
1951 goto out;
1952 if (ret > 0)
1953 break;
1954
1955 mutex_unlock(&gspca_dev->queue_lock);
1956
1957 if (file->f_flags & O_NONBLOCK)
1958 return -EAGAIN;
1959
1960 /* wait till a frame is ready */
1961 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1962 frame_ready(gspca_dev, file, v4l2_buf->memory),
1963 msecs_to_jiffies(3000));
1964 if (ret < 0)
1965 return ret;
1966 if (ret == 0)
1967 return -EIO;
1968
1969 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1970 return -ERESTARTSYS;
1971 }
1972
1973 i = gspca_dev->fr_o;
1974 j = gspca_dev->fr_queue[i];
1975 frame = &gspca_dev->frame[j];
1976
1977 gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES;
1978
1979 if (gspca_dev->sd_desc->dq_callback) {
1980 mutex_lock(&gspca_dev->usb_lock);
1981 gspca_dev->usb_err = 0;
1982 if (gspca_dev->present)
1983 gspca_dev->sd_desc->dq_callback(gspca_dev);
1984 mutex_unlock(&gspca_dev->usb_lock);
1985 }
1986
Hans de Goede4a82bc62010-12-30 20:00:17 -03001987 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1988 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1989 PDEBUG(D_FRAM, "dqbuf %d", j);
1990 ret = 0;
1991
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001992 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001993 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001994 frame->data,
1995 frame->v4l2_buf.bytesused)) {
1996 PDEBUG(D_ERR|D_STREAM,
1997 "dqbuf cp to user failed");
1998 ret = -EFAULT;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001999 }
2000 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002001out:
Hans de Goede27074ef2010-12-30 19:54:33 -03002002 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002003 return ret;
2004}
2005
2006/*
2007 * queue a video buffer
2008 *
2009 * Attempting to queue a buffer that has already been
2010 * queued will return -EINVAL.
2011 */
2012static int vidioc_qbuf(struct file *file, void *priv,
2013 struct v4l2_buffer *v4l2_buf)
2014{
2015 struct gspca_dev *gspca_dev = priv;
2016 struct gspca_frame *frame;
2017 int i, index, ret;
2018
2019 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002020
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002021 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
2022 return -ERESTARTSYS;
2023
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002024 index = v4l2_buf->index;
2025 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002026 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002027 "qbuf idx %d >= %d", index, gspca_dev->nframes);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002028 ret = -EINVAL;
2029 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002030 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002031 if (v4l2_buf->memory != gspca_dev->memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002032 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002033 ret = -EINVAL;
2034 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002035 }
2036
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002037 frame = &gspca_dev->frame[index];
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002038 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
2039 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002040 ret = -EINVAL;
2041 goto out;
2042 }
2043
2044 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002045
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002046 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002047 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
2048 frame->v4l2_buf.length = v4l2_buf->length;
2049 }
2050
2051 /* put the buffer in the 'queued' queue */
Jean-François Moinef7059ea2010-07-06 04:32:27 -03002052 i = atomic_read(&gspca_dev->fr_q);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002053 gspca_dev->fr_queue[i] = index;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03002054 atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002055
2056 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
2057 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
2058 ret = 0;
2059out:
2060 mutex_unlock(&gspca_dev->queue_lock);
2061 return ret;
2062}
2063
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002064/*
2065 * allocate the resources for read()
2066 */
2067static int read_alloc(struct gspca_dev *gspca_dev,
2068 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002069{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002070 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002071 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002072
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002073 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002074 if (gspca_dev->nframes == 0) {
2075 struct v4l2_requestbuffers rb;
2076
2077 memset(&rb, 0, sizeof rb);
2078 rb.count = gspca_dev->nbufread;
2079 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002080 rb.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002081 ret = vidioc_reqbufs(file, gspca_dev, &rb);
2082 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002083 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002084 return ret;
2085 }
2086 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2087 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002088 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002089 for (i = 0; i < gspca_dev->nbufread; i++) {
2090 v4l2_buf.index = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002091 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2092 if (ret != 0) {
2093 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
2094 return ret;
2095 }
2096 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002097 gspca_dev->memory = GSPCA_MEMORY_READ;
2098 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002099
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002100 /* start streaming */
2101 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2102 if (ret != 0)
2103 PDEBUG(D_STREAM, "read streamon err %d", ret);
2104 return ret;
2105}
2106
2107static unsigned int dev_poll(struct file *file, poll_table *wait)
2108{
2109 struct gspca_dev *gspca_dev = file->private_data;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03002110 int ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002111
2112 PDEBUG(D_FRAM, "poll");
2113
2114 poll_wait(file, &gspca_dev->wq, wait);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002115
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002116 /* if reqbufs is not done, the user would use read() */
Hans de Goedeee362992010-12-31 05:05:56 -03002117 if (gspca_dev->memory == GSPCA_MEMORY_NO) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002118 ret = read_alloc(gspca_dev, file);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002119 if (ret != 0)
2120 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002121 }
2122
2123 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
2124 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002125
Jean-François Moinef7059ea2010-07-06 04:32:27 -03002126 /* check if an image has been received */
2127 if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i))
2128 ret = POLLIN | POLLRDNORM; /* yes */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002129 else
2130 ret = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002131 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002132 if (!gspca_dev->present)
2133 return POLLHUP;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002134 return ret;
2135}
2136
2137static ssize_t dev_read(struct file *file, char __user *data,
2138 size_t count, loff_t *ppos)
2139{
2140 struct gspca_dev *gspca_dev = file->private_data;
2141 struct gspca_frame *frame;
2142 struct v4l2_buffer v4l2_buf;
2143 struct timeval timestamp;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002144 int n, ret, ret2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002145
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03002146 PDEBUG(D_FRAM, "read (%zd)", count);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002147 if (!gspca_dev->present)
2148 return -ENODEV;
Hans de Goedeee362992010-12-31 05:05:56 -03002149 if (gspca_dev->memory == GSPCA_MEMORY_NO) { /* first time ? */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002150 ret = read_alloc(gspca_dev, file);
2151 if (ret != 0)
2152 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002153 }
2154
2155 /* get a frame */
Paulo Assis2f51d002010-03-07 06:56:56 -03002156 timestamp = ktime_to_timeval(ktime_get());
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002157 timestamp.tv_sec--;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002158 n = 2;
2159 for (;;) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002160 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2161 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002162 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002163 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
2164 if (ret != 0) {
2165 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
2166 return ret;
2167 }
2168
2169 /* if the process slept for more than 1 second,
Frank Zago219423c2008-09-26 07:43:54 -03002170 * get a newer frame */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002171 frame = &gspca_dev->frame[v4l2_buf.index];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002172 if (--n < 0)
2173 break; /* avoid infinite loop */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002174 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
2175 break;
2176 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2177 if (ret != 0) {
2178 PDEBUG(D_STREAM, "read qbuf err %d", ret);
2179 return ret;
2180 }
2181 }
2182
2183 /* copy the frame */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002184 if (count > frame->v4l2_buf.bytesused)
2185 count = frame->v4l2_buf.bytesused;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002186 ret = copy_to_user(data, frame->data, count);
2187 if (ret != 0) {
2188 PDEBUG(D_ERR|D_STREAM,
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03002189 "read cp to user lack %d / %zd", ret, count);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002190 ret = -EFAULT;
2191 goto out;
2192 }
2193 ret = count;
2194out:
2195 /* in each case, requeue the buffer */
2196 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2197 if (ret2 != 0)
2198 return ret2;
2199 return ret;
2200}
2201
Hans Verkuilbec43662008-12-30 06:58:20 -03002202static struct v4l2_file_operations dev_fops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002203 .owner = THIS_MODULE,
2204 .open = dev_open,
2205 .release = dev_close,
2206 .read = dev_read,
2207 .mmap = dev_mmap,
Hans Verkuilbec43662008-12-30 06:58:20 -03002208 .unlocked_ioctl = video_ioctl2,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002209 .poll = dev_poll,
2210};
2211
Hans Verkuila3998102008-07-21 02:57:38 -03002212static const struct v4l2_ioctl_ops dev_ioctl_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002213 .vidioc_querycap = vidioc_querycap,
2214 .vidioc_dqbuf = vidioc_dqbuf,
2215 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03002216 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2217 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2218 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2219 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002220 .vidioc_streamon = vidioc_streamon,
2221 .vidioc_queryctrl = vidioc_queryctrl,
2222 .vidioc_g_ctrl = vidioc_g_ctrl,
2223 .vidioc_s_ctrl = vidioc_s_ctrl,
2224 .vidioc_querymenu = vidioc_querymenu,
2225 .vidioc_enum_input = vidioc_enum_input,
2226 .vidioc_g_input = vidioc_g_input,
2227 .vidioc_s_input = vidioc_s_input,
2228 .vidioc_reqbufs = vidioc_reqbufs,
2229 .vidioc_querybuf = vidioc_querybuf,
2230 .vidioc_streamoff = vidioc_streamoff,
2231 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
2232 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
2233 .vidioc_g_parm = vidioc_g_parm,
2234 .vidioc_s_parm = vidioc_s_parm,
Hans de Goede11c635a2009-05-25 15:04:22 -03002235 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Antonio Ospite28ffe772009-12-02 06:18:46 -03002236 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03002237#ifdef CONFIG_VIDEO_ADV_DEBUG
2238 .vidioc_g_register = vidioc_g_register,
2239 .vidioc_s_register = vidioc_s_register,
2240#endif
2241 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002242};
2243
Jean-François Moine59929ce2011-03-14 05:33:01 -03002244static const struct video_device gspca_template = {
Hans Verkuila3998102008-07-21 02:57:38 -03002245 .name = "gspca main driver",
Hans Verkuila3998102008-07-21 02:57:38 -03002246 .fops = &dev_fops,
2247 .ioctl_ops = &dev_ioctl_ops,
Hans de Goeded0182302008-11-19 17:10:47 -03002248 .release = gspca_release,
Hans Verkuila3998102008-07-21 02:57:38 -03002249};
2250
Jean-François Moine6a330912010-10-02 04:12:25 -03002251/* initialize the controls */
2252static void ctrls_init(struct gspca_dev *gspca_dev)
2253{
2254 struct gspca_ctrl *ctrl;
2255 int i;
2256
2257 for (i = 0, ctrl = gspca_dev->cam.ctrls;
2258 i < gspca_dev->sd_desc->nctrls;
2259 i++, ctrl++) {
2260 ctrl->def = gspca_dev->sd_desc->ctrls[i].qctrl.default_value;
2261 ctrl->val = ctrl->def;
2262 ctrl->min = gspca_dev->sd_desc->ctrls[i].qctrl.minimum;
2263 ctrl->max = gspca_dev->sd_desc->ctrls[i].qctrl.maximum;
2264 }
2265}
2266
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002267/*
2268 * probe and create a new gspca device
2269 *
2270 * This function must be called by the sub-driver when it is
2271 * called for probing a new device.
2272 */
Jean-François Moine44628642010-06-05 07:47:36 -03002273int gspca_dev_probe2(struct usb_interface *intf,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002274 const struct usb_device_id *id,
2275 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002276 int dev_size,
2277 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002278{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002279 struct gspca_dev *gspca_dev;
2280 struct usb_device *dev = interface_to_usbdev(intf);
2281 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002282
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002283 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002284
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002285 /* create the device */
2286 if (dev_size < sizeof *gspca_dev)
2287 dev_size = sizeof *gspca_dev;
2288 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
Erik Andrene293e592008-10-03 08:46:50 -03002289 if (!gspca_dev) {
Joe Perches133a9fe2011-08-21 19:56:57 -03002290 pr_err("couldn't kzalloc gspca struct\n");
Erik Andrene293e592008-10-03 08:46:50 -03002291 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002292 }
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002293 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2294 if (!gspca_dev->usb_buf) {
Joe Perches133a9fe2011-08-21 19:56:57 -03002295 pr_err("out of memory\n");
Erik Andrene293e592008-10-03 08:46:50 -03002296 ret = -ENOMEM;
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002297 goto out;
2298 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002299 gspca_dev->dev = dev;
Jean-François Moine44628642010-06-05 07:47:36 -03002300 gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002301 gspca_dev->nbalt = intf->num_altsetting;
Jean-François Moine35680ba2010-07-14 06:30:18 -03002302
2303 /* check if any audio device */
2304 if (dev->config->desc.bNumInterfaces != 1) {
2305 int i;
2306 struct usb_interface *intf2;
2307
2308 for (i = 0; i < dev->config->desc.bNumInterfaces; i++) {
2309 intf2 = dev->config->interface[i];
2310 if (intf2 != NULL
2311 && intf2->altsetting != NULL
2312 && intf2->altsetting->desc.bInterfaceClass ==
2313 USB_CLASS_AUDIO) {
2314 gspca_dev->audio = 1;
2315 break;
2316 }
2317 }
2318 }
2319
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002320 gspca_dev->sd_desc = sd_desc;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002321 gspca_dev->nbufread = 2;
Jean-Francois Moineff374742008-10-23 07:29:51 -03002322 gspca_dev->empty_packet = -1; /* don't check the empty packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002323
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002324 /* configure the subdriver and initialize the USB device */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002325 ret = sd_desc->config(gspca_dev, id);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002326 if (ret < 0)
2327 goto out;
Jean-François Moine6a330912010-10-02 04:12:25 -03002328 if (gspca_dev->cam.ctrls != NULL)
2329 ctrls_init(gspca_dev);
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002330 ret = sd_desc->init(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002331 if (ret < 0)
2332 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002333 gspca_set_default_mode(gspca_dev);
2334
Hans de Goede10ee2402010-02-19 04:41:40 -03002335 ret = gspca_input_connect(gspca_dev);
2336 if (ret)
2337 goto out;
2338
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002339 mutex_init(&gspca_dev->usb_lock);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002340 mutex_init(&gspca_dev->queue_lock);
2341 init_waitqueue_head(&gspca_dev->wq);
2342
2343 /* init video stuff */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002344 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
Hans de Goede24441632009-03-27 16:08:20 -03002345 gspca_dev->vdev.parent = &intf->dev;
Jean-Francois Moine5c4fa002008-11-18 15:52:31 -03002346 gspca_dev->module = module;
Oliver Neukumee56a4d2008-07-27 14:01:59 -03002347 gspca_dev->present = 1;
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002348 ret = video_register_device(&gspca_dev->vdev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002349 VFL_TYPE_GRABBER,
Jean-Francois Moine8d584a52009-01-13 06:07:59 -03002350 -1);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002351 if (ret < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -03002352 pr_err("video_register_device err %d\n", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002353 goto out;
2354 }
2355
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002356 usb_set_intfdata(intf, gspca_dev);
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002357 PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
Márton Németh0274d422010-01-28 06:39:49 -03002358
Hans de Goede10ee2402010-02-19 04:41:40 -03002359 gspca_input_create_urb(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002360
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002361 return 0;
2362out:
Randy Dunlapf7628012010-03-30 14:32:04 -03002363#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Hans de Goede10ee2402010-02-19 04:41:40 -03002364 if (gspca_dev->input_dev)
2365 input_unregister_device(gspca_dev->input_dev);
Hans de Goedee7637522010-02-19 05:11:09 -03002366#endif
Jean-Francois Moinee07a1d82008-11-19 06:37:53 -03002367 kfree(gspca_dev->usb_buf);
2368 kfree(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002369 return ret;
2370}
Jean-François Moine44628642010-06-05 07:47:36 -03002371EXPORT_SYMBOL(gspca_dev_probe2);
2372
2373/* same function as the previous one, but check the interface */
2374int gspca_dev_probe(struct usb_interface *intf,
2375 const struct usb_device_id *id,
2376 const struct sd_desc *sd_desc,
2377 int dev_size,
2378 struct module *module)
2379{
2380 struct usb_device *dev = interface_to_usbdev(intf);
2381
2382 /* we don't handle multi-config cameras */
2383 if (dev->descriptor.bNumConfigurations != 1) {
Joe Perches133a9fe2011-08-21 19:56:57 -03002384 pr_err("%04x:%04x too many config\n",
2385 id->idVendor, id->idProduct);
Jean-François Moine44628642010-06-05 07:47:36 -03002386 return -ENODEV;
2387 }
2388
2389 /* the USB video interface must be the first one */
2390 if (dev->config->desc.bNumInterfaces != 1
Jean-François Moinec4dc6922010-07-14 06:26:54 -03002391 && intf->cur_altsetting->desc.bInterfaceNumber != 0)
Jean-François Moine44628642010-06-05 07:47:36 -03002392 return -ENODEV;
Jean-François Moine44628642010-06-05 07:47:36 -03002393
2394 return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
2395}
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002396EXPORT_SYMBOL(gspca_dev_probe);
2397
2398/*
2399 * USB disconnection
2400 *
2401 * This function must be called by the sub-driver
2402 * when the device disconnects, after the specific resources are freed.
2403 */
2404void gspca_disconnect(struct usb_interface *intf)
2405{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002406 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Randy Dunlapf7628012010-03-30 14:32:04 -03002407#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -03002408 struct input_dev *input_dev;
Jean-François Moinee070bf92010-02-18 15:02:51 -03002409#endif
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002410
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002411 PDEBUG(D_PROBE, "%s disconnect",
2412 video_device_node_name(&gspca_dev->vdev));
Adam Bakerad281272009-02-04 15:33:21 -03002413 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002414
Hans de Goede4e770f72010-12-30 13:11:21 -03002415 gspca_dev->present = 0;
2416 wake_up_interruptible(&gspca_dev->wq);
2417
2418 destroy_urbs(gspca_dev);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002419
Randy Dunlapf7628012010-03-30 14:32:04 -03002420#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -03002421 gspca_input_destroy_urb(gspca_dev);
2422 input_dev = gspca_dev->input_dev;
2423 if (input_dev) {
2424 gspca_dev->input_dev = NULL;
2425 input_unregister_device(input_dev);
2426 }
Jean-François Moinee070bf92010-02-18 15:02:51 -03002427#endif
Márton Németh0274d422010-01-28 06:39:49 -03002428
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002429 /* the device is freed at exit of this function */
2430 gspca_dev->dev = NULL;
Adam Bakerad281272009-02-04 15:33:21 -03002431 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03002432
Frank Zago6b060ff2008-09-28 08:12:22 -03002433 usb_set_intfdata(intf, NULL);
2434
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002435 /* release the device */
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002436 /* (this will call gspca_release() immediately or on last close) */
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002437 video_unregister_device(&gspca_dev->vdev);
Jean-Francois Moine3145b8c2008-10-05 04:22:10 -03002438
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002439/* PDEBUG(D_PROBE, "disconnect complete"); */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002440}
2441EXPORT_SYMBOL(gspca_disconnect);
2442
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002443#ifdef CONFIG_PM
2444int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2445{
2446 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2447
2448 if (!gspca_dev->streaming)
2449 return 0;
2450 gspca_dev->frozen = 1; /* avoid urb error messages */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002451 if (gspca_dev->sd_desc->stopN)
2452 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002453 destroy_urbs(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002454 gspca_input_destroy_urb(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002455 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002456 if (gspca_dev->sd_desc->stop0)
2457 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002458 return 0;
2459}
2460EXPORT_SYMBOL(gspca_suspend);
2461
2462int gspca_resume(struct usb_interface *intf)
2463{
2464 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2465
2466 gspca_dev->frozen = 0;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002467 gspca_dev->sd_desc->init(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002468 gspca_input_create_urb(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002469 if (gspca_dev->streaming)
2470 return gspca_init_transfer(gspca_dev);
Jean-Francois Moine1e89e2d2008-09-03 17:12:15 -03002471 return 0;
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002472}
2473EXPORT_SYMBOL(gspca_resume);
2474#endif
Hans de Goededcef3232008-07-10 10:40:53 -03002475/* -- cam driver utility functions -- */
2476
2477/* auto gain and exposure algorithm based on the knee algorithm described here:
2478 http://ytse.tricolour.net/docs/LowLightOptimization.html
2479
2480 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2481 where changed. */
2482int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2483 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2484{
2485 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2486 const struct ctrl *gain_ctrl = NULL;
2487 const struct ctrl *exposure_ctrl = NULL;
2488 const struct ctrl *autogain_ctrl = NULL;
2489 int retval = 0;
2490
2491 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goede26984b02010-02-01 13:18:37 -03002492 if (gspca_dev->ctrl_dis & (1 << i))
2493 continue;
Hans de Goededcef3232008-07-10 10:40:53 -03002494 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2495 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2496 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2497 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2498 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2499 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2500 }
2501 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2502 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2503 "on cam without (auto)gain/exposure");
2504 return 0;
2505 }
2506
2507 if (gain_ctrl->get(gspca_dev, &gain) ||
2508 exposure_ctrl->get(gspca_dev, &exposure) ||
2509 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2510 return 0;
2511
2512 orig_gain = gain;
2513 orig_exposure = exposure;
2514
2515 /* If we are of a multiple of deadzone, do multiple steps to reach the
2516 desired lumination fast (with the risc of a slight overshoot) */
2517 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2518
Jean-Francois Moine1c44d812008-11-10 05:56:55 -03002519 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
Hans de Goededcef3232008-07-10 10:40:53 -03002520 avg_lum, desired_avg_lum, steps);
2521
2522 for (i = 0; i < steps; i++) {
2523 if (avg_lum > desired_avg_lum) {
2524 if (gain > gain_knee)
2525 gain--;
2526 else if (exposure > exposure_knee)
2527 exposure--;
2528 else if (gain > gain_ctrl->qctrl.default_value)
2529 gain--;
2530 else if (exposure > exposure_ctrl->qctrl.minimum)
2531 exposure--;
2532 else if (gain > gain_ctrl->qctrl.minimum)
2533 gain--;
2534 else
2535 break;
2536 } else {
2537 if (gain < gain_ctrl->qctrl.default_value)
2538 gain++;
2539 else if (exposure < exposure_knee)
2540 exposure++;
2541 else if (gain < gain_knee)
2542 gain++;
2543 else if (exposure < exposure_ctrl->qctrl.maximum)
2544 exposure++;
2545 else if (gain < gain_ctrl->qctrl.maximum)
2546 gain++;
2547 else
2548 break;
2549 }
2550 }
2551
2552 if (gain != orig_gain) {
2553 gain_ctrl->set(gspca_dev, gain);
2554 retval = 1;
2555 }
2556 if (exposure != orig_exposure) {
2557 exposure_ctrl->set(gspca_dev, exposure);
2558 retval = 1;
2559 }
2560
2561 return retval;
2562}
2563EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2564
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002565/* -- module insert / remove -- */
2566static int __init gspca_init(void)
2567{
Joe Perches133a9fe2011-08-21 19:56:57 -03002568 pr_info("v" DRIVER_VERSION_NUMBER " registered\n");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002569 return 0;
2570}
2571static void __exit gspca_exit(void)
2572{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002573}
2574
2575module_init(gspca_init);
2576module_exit(gspca_exit);
2577
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03002578#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002579module_param_named(debug, gspca_debug, int, 0644);
2580MODULE_PARM_DESC(debug,
2581 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-François Moinef8e56772011-05-17 04:10:32 -03002582 " 0x08:stream 0x10:frame 0x20:packet"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002583 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002584#endif