blob: e41dfe2548039eb817fc72562f894ae3562b4a7f [file] [log] [blame]
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001/*
2 * Main USB camera driver
3 *
Jean-François Moine59929ce2011-03-14 05:33:01 -03004 * Copyright (C) 2008-2011 Jean-François Moine <http://moinejf.free.fr>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03005 *
Márton Németh0274d422010-01-28 06:39:49 -03006 * Camera button input handling by Márton Németh
7 * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu>
8 *
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03009 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
Joe Perches133a9fe2011-08-21 19:56:57 -030024#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030026#define MODULE_NAME "gspca"
27
28#include <linux/init.h>
29#include <linux/fs.h>
30#include <linux/vmalloc.h>
31#include <linux/sched.h>
32#include <linux/slab.h>
33#include <linux/mm.h>
34#include <linux/string.h>
35#include <linux/pagemap.h>
Jean-Francois Moine956e42d2008-07-01 10:03:42 -030036#include <linux/io.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030037#include <asm/page.h>
Jean-Francois Moine956e42d2008-07-01 10:03:42 -030038#include <linux/uaccess.h>
Paulo Assis2f51d002010-03-07 06:56:56 -030039#include <linux/ktime.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030040#include <media/v4l2-ioctl.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030041
42#include "gspca.h"
43
Randy Dunlapf7628012010-03-30 14:32:04 -030044#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -030045#include <linux/input.h>
46#include <linux/usb/input.h>
Jean-François Moinee070bf92010-02-18 15:02:51 -030047#endif
Márton Németh0274d422010-01-28 06:39:49 -030048
Jean-Francois Moined43fa322008-06-12 10:58:58 -030049/* global values */
Jean-Francois Moine8fee8452009-01-17 04:46:38 -030050#define DEF_NURBS 3 /* default number of URBs */
51#if DEF_NURBS > MAX_NURBS
52#error "DEF_NURBS too big"
53#endif
Jean-Francois Moined43fa322008-06-12 10:58:58 -030054
Mauro Carvalho Chehabb0eaab762011-06-27 22:28:38 -030055#define DRIVER_VERSION_NUMBER "2.13.0"
56
Jean-François Moine27c6f9e2010-04-02 06:59:13 -030057MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030058MODULE_DESCRIPTION("GSPCA USB Camera Driver");
59MODULE_LICENSE("GPL");
Mauro Carvalho Chehabb0eaab762011-06-27 22:28:38 -030060MODULE_VERSION(DRIVER_VERSION_NUMBER);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030061
Jean-Francois Moine335b3f82008-07-30 04:53:02 -030062#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030063int gspca_debug = D_ERR | D_PROBE;
64EXPORT_SYMBOL(gspca_debug);
65
66static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
67{
68 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
69 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
70 txt,
71 pixfmt & 0xff,
72 (pixfmt >> 8) & 0xff,
73 (pixfmt >> 16) & 0xff,
74 pixfmt >> 24,
75 w, h);
76 } else {
77 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
78 txt,
79 pixfmt,
80 w, h);
81 }
82}
83#else
84#define PDEBUG_MODE(txt, pixfmt, w, h)
85#endif
86
Jean-Francois Moine77ee3312009-11-07 15:30:50 -030087/* specific memory types - !! should be different from V4L2_MEMORY_xxx */
Jean-Francois Moined43fa322008-06-12 10:58:58 -030088#define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
89#define GSPCA_MEMORY_READ 7
90
Jean-Francois Moined43fa322008-06-12 10:58:58 -030091#define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
Jean-Francois Moined43fa322008-06-12 10:58:58 -030092
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030093/*
94 * VMA operations.
95 */
96static void gspca_vm_open(struct vm_area_struct *vma)
97{
98 struct gspca_frame *frame = vma->vm_private_data;
99
100 frame->vma_use_count++;
101 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
102}
103
104static void gspca_vm_close(struct vm_area_struct *vma)
105{
106 struct gspca_frame *frame = vma->vm_private_data;
107
108 if (--frame->vma_use_count <= 0)
109 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
110}
111
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400112static const struct vm_operations_struct gspca_vm_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300113 .open = gspca_vm_open,
114 .close = gspca_vm_close,
115};
116
Márton Németh0274d422010-01-28 06:39:49 -0300117/*
118 * Input and interrupt endpoint handling functions
119 */
Randy Dunlapf7628012010-03-30 14:32:04 -0300120#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -0300121static void int_irq(struct urb *urb)
122{
123 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
124 int ret;
125
126 ret = urb->status;
127 switch (ret) {
128 case 0:
129 if (gspca_dev->sd_desc->int_pkt_scan(gspca_dev,
130 urb->transfer_buffer, urb->actual_length) < 0) {
131 PDEBUG(D_ERR, "Unknown packet received");
132 }
133 break;
134
135 case -ENOENT:
136 case -ECONNRESET:
137 case -ENODEV:
138 case -ESHUTDOWN:
139 /* Stop is requested either by software or hardware is gone,
140 * keep the ret value non-zero and don't resubmit later.
141 */
142 break;
143
144 default:
145 PDEBUG(D_ERR, "URB error %i, resubmitting", urb->status);
146 urb->status = 0;
147 ret = 0;
148 }
149
150 if (ret == 0) {
151 ret = usb_submit_urb(urb, GFP_ATOMIC);
152 if (ret < 0)
Joe Perches133a9fe2011-08-21 19:56:57 -0300153 pr_err("Resubmit URB failed with error %i\n", ret);
Márton Németh0274d422010-01-28 06:39:49 -0300154 }
155}
156
157static int gspca_input_connect(struct gspca_dev *dev)
158{
159 struct input_dev *input_dev;
160 int err = 0;
161
162 dev->input_dev = NULL;
Hans de Goedeac82f592010-02-19 04:28:39 -0300163 if (dev->sd_desc->int_pkt_scan || dev->sd_desc->other_input) {
Márton Németh0274d422010-01-28 06:39:49 -0300164 input_dev = input_allocate_device();
165 if (!input_dev)
166 return -ENOMEM;
167
168 usb_make_path(dev->dev, dev->phys, sizeof(dev->phys));
169 strlcat(dev->phys, "/input0", sizeof(dev->phys));
170
171 input_dev->name = dev->sd_desc->name;
172 input_dev->phys = dev->phys;
173
174 usb_to_input_id(dev->dev, &input_dev->id);
175
176 input_dev->evbit[0] = BIT_MASK(EV_KEY);
177 input_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
178 input_dev->dev.parent = &dev->dev->dev;
179
180 err = input_register_device(input_dev);
181 if (err) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300182 pr_err("Input device registration failed with error %i\n",
183 err);
Márton Németh0274d422010-01-28 06:39:49 -0300184 input_dev->dev.parent = NULL;
185 input_free_device(input_dev);
186 } else {
187 dev->input_dev = input_dev;
188 }
Hans de Goede10ee2402010-02-19 04:41:40 -0300189 }
Márton Németh0274d422010-01-28 06:39:49 -0300190
191 return err;
192}
193
194static int alloc_and_submit_int_urb(struct gspca_dev *gspca_dev,
195 struct usb_endpoint_descriptor *ep)
196{
197 unsigned int buffer_len;
198 int interval;
199 struct urb *urb;
200 struct usb_device *dev;
201 void *buffer = NULL;
202 int ret = -EINVAL;
203
Jean-François Moineb26d6e22010-04-29 06:01:53 -0300204 buffer_len = le16_to_cpu(ep->wMaxPacketSize);
Márton Németh0274d422010-01-28 06:39:49 -0300205 interval = ep->bInterval;
Jean-François Moinec4dc6922010-07-14 06:26:54 -0300206 PDEBUG(D_CONF, "found int in endpoint: 0x%x, "
Márton Németh0274d422010-01-28 06:39:49 -0300207 "buffer_len=%u, interval=%u",
208 ep->bEndpointAddress, buffer_len, interval);
209
210 dev = gspca_dev->dev;
211
212 urb = usb_alloc_urb(0, GFP_KERNEL);
213 if (!urb) {
214 ret = -ENOMEM;
215 goto error;
216 }
217
Linus Torvalds7a9b1492010-05-20 21:26:12 -0700218 buffer = usb_alloc_coherent(dev, buffer_len,
Márton Németh0274d422010-01-28 06:39:49 -0300219 GFP_KERNEL, &urb->transfer_dma);
220 if (!buffer) {
221 ret = -ENOMEM;
222 goto error_buffer;
223 }
224 usb_fill_int_urb(urb, dev,
225 usb_rcvintpipe(dev, ep->bEndpointAddress),
226 buffer, buffer_len,
227 int_irq, (void *)gspca_dev, interval);
Jason Wang882787f2010-09-03 06:57:19 -0300228 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Márton Németh0274d422010-01-28 06:39:49 -0300229 ret = usb_submit_urb(urb, GFP_KERNEL);
230 if (ret < 0) {
Jean-François Moinec4dc6922010-07-14 06:26:54 -0300231 PDEBUG(D_ERR, "submit int URB failed with error %i", ret);
Márton Németh0274d422010-01-28 06:39:49 -0300232 goto error_submit;
233 }
Hans de Goede2661da42010-10-26 05:16:32 -0300234 gspca_dev->int_urb = urb;
Márton Németh0274d422010-01-28 06:39:49 -0300235 return ret;
236
237error_submit:
Daniel Mack997ea582010-04-12 13:17:25 +0200238 usb_free_coherent(dev,
239 urb->transfer_buffer_length,
240 urb->transfer_buffer,
241 urb->transfer_dma);
Márton Németh0274d422010-01-28 06:39:49 -0300242error_buffer:
243 usb_free_urb(urb);
244error:
245 return ret;
246}
247
Hans de Goede10ee2402010-02-19 04:41:40 -0300248static void gspca_input_create_urb(struct gspca_dev *gspca_dev)
Márton Németh0274d422010-01-28 06:39:49 -0300249{
Márton Németh0274d422010-01-28 06:39:49 -0300250 struct usb_interface *intf;
251 struct usb_host_interface *intf_desc;
252 struct usb_endpoint_descriptor *ep;
253 int i;
254
255 if (gspca_dev->sd_desc->int_pkt_scan) {
256 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
257 intf_desc = intf->cur_altsetting;
258 for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
259 ep = &intf_desc->endpoint[i].desc;
260 if (usb_endpoint_dir_in(ep) &&
261 usb_endpoint_xfer_int(ep)) {
262
Hans de Goede10ee2402010-02-19 04:41:40 -0300263 alloc_and_submit_int_urb(gspca_dev, ep);
Márton Németh0274d422010-01-28 06:39:49 -0300264 break;
265 }
266 }
267 }
Márton Németh0274d422010-01-28 06:39:49 -0300268}
269
270static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
271{
272 struct urb *urb;
273
274 urb = gspca_dev->int_urb;
275 if (urb) {
276 gspca_dev->int_urb = NULL;
277 usb_kill_urb(urb);
Daniel Mack997ea582010-04-12 13:17:25 +0200278 usb_free_coherent(gspca_dev->dev,
279 urb->transfer_buffer_length,
280 urb->transfer_buffer,
281 urb->transfer_dma);
Márton Németh0274d422010-01-28 06:39:49 -0300282 usb_free_urb(urb);
283 }
284}
Randy Dunlapf7628012010-03-30 14:32:04 -0300285#else
286static inline void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
287{
288}
289
290static inline void gspca_input_create_urb(struct gspca_dev *gspca_dev)
291{
292}
293
294static inline int gspca_input_connect(struct gspca_dev *dev)
295{
296 return 0;
297}
Márton Németh0274d422010-01-28 06:39:49 -0300298#endif
299
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300300/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300301 * fill a video frame from an URB and resubmit
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300302 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300303static void fill_frame(struct gspca_dev *gspca_dev,
304 struct urb *urb)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300305{
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300306 u8 *data; /* address of data in the iso message */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300307 int i, len, st;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300308 cam_pkt_op pkt_scan;
309
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300310 if (urb->status != 0) {
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300311 if (urb->status == -ESHUTDOWN)
312 return; /* disconnection */
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300313#ifdef CONFIG_PM
Hans de Goede6a540bd2009-10-24 15:02:14 -0300314 if (gspca_dev->frozen)
315 return;
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300316#endif
Hans de Goede6a540bd2009-10-24 15:02:14 -0300317 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine81578522009-11-13 07:15:08 -0300318 urb->status = 0;
Hans de Goedebf926ad2009-10-23 06:56:06 -0300319 goto resubmit;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300320 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300321 pkt_scan = gspca_dev->sd_desc->pkt_scan;
322 for (i = 0; i < urb->number_of_packets; i++) {
Mauro Carvalho Chehab95f18be2010-12-27 12:00:03 -0300323 len = urb->iso_frame_desc[i].actual_length;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300324
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300325 /* check the packet status and length */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300326 st = urb->iso_frame_desc[i].status;
327 if (st) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300328 pr_err("ISOC data error: [%d] len=%d, status=%d\n",
329 i, len, st);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300330 gspca_dev->last_packet_type = DISCARD_PACKET;
331 continue;
332 }
Jean-François Moine76ebc162010-12-25 13:22:21 -0300333 if (len == 0) {
334 if (gspca_dev->empty_packet == 0)
335 gspca_dev->empty_packet = 1;
336 continue;
337 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300338
339 /* let the packet be analyzed by the subdriver */
340 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
341 i, urb->iso_frame_desc[i].offset, len);
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300342 data = (u8 *) urb->transfer_buffer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300343 + urb->iso_frame_desc[i].offset;
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300344 pkt_scan(gspca_dev, data, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300345 }
346
Hans de Goedebf926ad2009-10-23 06:56:06 -0300347resubmit:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300348 /* resubmit the URB */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300349 st = usb_submit_urb(urb, GFP_ATOMIC);
350 if (st < 0)
Joe Perches133a9fe2011-08-21 19:56:57 -0300351 pr_err("usb_submit_urb() ret %d\n", st);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300352}
353
354/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300355 * ISOC message interrupt from the USB device
356 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300357 * Analyse each packet and call the subdriver for copy to the frame buffer.
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300358 */
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300359static void isoc_irq(struct urb *urb)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300360{
361 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
362
Hans de Goede3647fea2008-07-15 05:36:30 -0300363 PDEBUG(D_PACK, "isoc irq");
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300364 if (!gspca_dev->streaming)
365 return;
366 fill_frame(gspca_dev, urb);
367}
368
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300369/*
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300370 * bulk message interrupt from the USB device
371 */
Jean-Francois Moine8d584a52009-01-13 06:07:59 -0300372static void bulk_irq(struct urb *urb)
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300373{
374 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300375 int st;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300376
377 PDEBUG(D_PACK, "bulk irq");
378 if (!gspca_dev->streaming)
379 return;
Jean-Francois Moine880ae242008-11-18 04:19:52 -0300380 switch (urb->status) {
381 case 0:
382 break;
Jean-Francois Moine090264e2009-02-12 08:05:45 -0300383 case -ESHUTDOWN:
384 return; /* disconnection */
Jean-Francois Moine880ae242008-11-18 04:19:52 -0300385 default:
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300386#ifdef CONFIG_PM
Hans de Goede6a540bd2009-10-24 15:02:14 -0300387 if (gspca_dev->frozen)
388 return;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300389#endif
Hans de Goede6a540bd2009-10-24 15:02:14 -0300390 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine81578522009-11-13 07:15:08 -0300391 urb->status = 0;
Hans de Goedebf926ad2009-10-23 06:56:06 -0300392 goto resubmit;
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300393 }
394
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300395 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
396 gspca_dev->sd_desc->pkt_scan(gspca_dev,
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300397 urb->transfer_buffer,
398 urb->actual_length);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300399
Hans de Goedebf926ad2009-10-23 06:56:06 -0300400resubmit:
Antonio Ospitedff369a2008-11-14 07:53:32 -0300401 /* resubmit the URB */
402 if (gspca_dev->cam.bulk_nurbs != 0) {
403 st = usb_submit_urb(urb, GFP_ATOMIC);
404 if (st < 0)
Joe Perches133a9fe2011-08-21 19:56:57 -0300405 pr_err("usb_submit_urb() ret %d\n", st);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300406 }
Jean-Francois Moine0be010042008-09-04 07:01:50 -0300407}
408
409/*
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300410 * add data to the current frame
411 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300412 * This function is called by the subdrivers at interrupt level.
413 *
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300414 * To build a frame, these ones must add
415 * - one FIRST_PACKET
416 * - 0 or many INTER_PACKETs
417 * - one LAST_PACKET
418 * DISCARD_PACKET invalidates the whole frame.
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300419 */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300420void gspca_frame_add(struct gspca_dev *gspca_dev,
421 enum gspca_packet_type packet_type,
422 const u8 *data,
423 int len)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300424{
Jean-Francois Moine3ec342f2009-11-13 07:38:16 -0300425 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300426 int i, j;
427
Hans de Goedee2997a72008-04-23 08:09:12 -0300428 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
Jean-Francois Moined131c3c2009-11-13 08:16:02 -0300429
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300430 if (packet_type == FIRST_PACKET) {
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300431 i = atomic_read(&gspca_dev->fr_i);
432
433 /* if there are no queued buffer, discard the whole frame */
434 if (i == atomic_read(&gspca_dev->fr_q)) {
435 gspca_dev->last_packet_type = DISCARD_PACKET;
Jean-François Moine2e0544a2010-10-16 13:59:13 -0300436 gspca_dev->sequence++;
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300437 return;
438 }
Jean-François Moineb192ca92010-06-27 03:08:19 -0300439 j = gspca_dev->fr_queue[i];
440 frame = &gspca_dev->frame[j];
Paulo Assis2f51d002010-03-07 06:56:56 -0300441 frame->v4l2_buf.timestamp = ktime_to_timeval(ktime_get());
Jean-François Moine2e0544a2010-10-16 13:59:13 -0300442 frame->v4l2_buf.sequence = gspca_dev->sequence++;
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300443 gspca_dev->image = frame->data;
Jean-François Moineb192ca92010-06-27 03:08:19 -0300444 gspca_dev->image_len = 0;
Jean-François Moinefe988f52010-07-29 02:46:02 -0300445 } else {
446 switch (gspca_dev->last_packet_type) {
447 case DISCARD_PACKET:
Hans de Goedefa4376d2011-06-05 03:44:43 -0300448 if (packet_type == LAST_PACKET) {
Jean-François Moinefe988f52010-07-29 02:46:02 -0300449 gspca_dev->last_packet_type = packet_type;
Hans de Goedefa4376d2011-06-05 03:44:43 -0300450 gspca_dev->image = NULL;
451 gspca_dev->image_len = 0;
452 }
Jean-François Moinefe988f52010-07-29 02:46:02 -0300453 return;
454 case LAST_PACKET:
455 return;
456 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300457 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300458
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300459 /* append the packet to the frame buffer */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300460 if (len > 0) {
Jean-François Moineb192ca92010-06-27 03:08:19 -0300461 if (gspca_dev->image_len + len > gspca_dev->frsz) {
462 PDEBUG(D_ERR|D_PACK, "frame overflow %d > %d",
463 gspca_dev->image_len + len,
464 gspca_dev->frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300465 packet_type = DISCARD_PACKET;
466 } else {
Jean-François Moinefe988f52010-07-29 02:46:02 -0300467/* !! image is NULL only when last pkt is LAST or DISCARD
468 if (gspca_dev->image == NULL) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300469 pr_err("gspca_frame_add() image == NULL\n");
Jean-François Moinefe988f52010-07-29 02:46:02 -0300470 return;
471 }
472 */
Jean-François Moineb192ca92010-06-27 03:08:19 -0300473 memcpy(gspca_dev->image + gspca_dev->image_len,
474 data, len);
475 gspca_dev->image_len += len;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300476 }
477 }
478 gspca_dev->last_packet_type = packet_type;
479
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300480 /* if last packet, invalidate packet concatenation until
481 * next first packet, wake up the application and advance
482 * in the queue */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300483 if (packet_type == LAST_PACKET) {
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300484 i = atomic_read(&gspca_dev->fr_i);
Jean-François Moineb192ca92010-06-27 03:08:19 -0300485 j = gspca_dev->fr_queue[i];
486 frame = &gspca_dev->frame[j];
487 frame->v4l2_buf.bytesused = gspca_dev->image_len;
Jean-François Moine02bbcb92010-07-06 04:16:40 -0300488 frame->v4l2_buf.flags = (frame->v4l2_buf.flags
489 | V4L2_BUF_FLAG_DONE)
490 & ~V4L2_BUF_FLAG_QUEUED;
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300491 i = (i + 1) % GSPCA_MAX_FRAMES;
492 atomic_set(&gspca_dev->fr_i, i);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300493 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300494 PDEBUG(D_FRAM, "frame complete len:%d",
495 frame->v4l2_buf.bytesused);
496 gspca_dev->image = NULL;
497 gspca_dev->image_len = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300498 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300499}
500EXPORT_SYMBOL(gspca_frame_add);
501
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 Moine0be010042008-09-04 07:01:50 -0300615static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300616 int xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300617{
618 struct usb_host_endpoint *ep;
619 int i, attr;
620
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300621 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
622 ep = &alt->endpoint[i];
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300623 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Jean-Francois Moine30d35e42009-04-21 13:57:31 -0300624 if (attr == xfer
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 Moine0be010042008-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 Moine0be010042008-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 Moine0be010042008-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 Moine0be010042008-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 Moine0be010042008-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 Moine0be010042008-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 Moine0be010042008-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 Moine0be010042008-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) {
Jean-François Moine4274d842011-08-10 07:40:47 -0300890 if (ret == -ENOSPC)
891 goto retry; /*fixme: ugly*/
Jean-François Moine27b12022011-08-10 07:38:48 -0300892 pr_err("set alt %d err %d\n", alt, ret);
893 goto out;
894 }
895 }
896 }
Jean-Francois Moine47aaca92009-12-15 05:23:04 -0300897 if (!gspca_dev->cam.no_urb_create) {
Jean-François Moine27b12022011-08-10 07:38:48 -0300898 PDEBUG(D_STREAM, "init transfer alt %d", alt);
899 ret = create_urbs(gspca_dev,
900 alt_xfer(&intf->altsetting[alt], xfer));
Jean-Francois Moine47aaca92009-12-15 05:23:04 -0300901 if (ret < 0) {
902 destroy_urbs(gspca_dev);
903 goto out;
904 }
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300905 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300906
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300907 /* clear the bulk endpoint */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300908 if (gspca_dev->cam.bulk)
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300909 usb_clear_halt(gspca_dev->dev,
Jean-Francois Moine50e06de2008-12-31 08:13:46 -0300910 gspca_dev->urb[0]->pipe);
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300911
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300912 /* start the cam */
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300913 ret = gspca_dev->sd_desc->start(gspca_dev);
914 if (ret < 0) {
915 destroy_urbs(gspca_dev);
916 goto out;
917 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300918 gspca_dev->streaming = 1;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300919
Antonio Ospitedff369a2008-11-14 07:53:32 -0300920 /* some bulk transfers are started by the subdriver */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300921 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300922 break;
923
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300924 /* submit the URBs */
Jean-Francois Moinedb870872009-12-15 05:16:04 -0300925 for (n = 0; n < MAX_NURBS; n++) {
926 urb = gspca_dev->urb[n];
927 if (urb == NULL)
928 break;
929 ret = usb_submit_urb(urb, GFP_KERNEL);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300930 if (ret < 0)
931 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300932 }
933 if (ret >= 0)
Jean-François Moine27b12022011-08-10 07:38:48 -0300934 break; /* transfer is started */
935
936 /* something when wrong
937 * stop the webcam and free the transfer resources */
Jean-François Moinee4dac282010-04-09 06:11:36 -0300938 gspca_stream_off(gspca_dev);
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300939 if (ret != -ENOSPC) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300940 pr_err("usb_submit_urb alt %d err %d\n",
941 gspca_dev->alt, ret);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300942 goto out;
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300943 }
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300944
945 /* the bandwidth is not wide enough
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300946 * negotiate or try a lower alternate setting */
Jean-François Moine4274d842011-08-10 07:40:47 -0300947retry:
Jean-Francois Moine4bdf4a82009-11-10 14:49:43 -0300948 PDEBUG(D_ERR|D_STREAM,
Jean-François Moine27b12022011-08-10 07:38:48 -0300949 "alt %d - bandwidth not wide enough - trying again",
950 alt);
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300951 msleep(20); /* wait for kill complete */
952 if (gspca_dev->sd_desc->isoc_nego) {
953 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
954 if (ret < 0)
955 goto out;
956 } else {
Jean-François Moine27b12022011-08-10 07:38:48 -0300957 if (alt_idx <= 0) {
958 pr_err("no transfer endpoint found\n");
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300959 ret = -EIO;
960 goto out;
961 }
Jean-François Moine27b12022011-08-10 07:38:48 -0300962 alt = ep_tb[--alt_idx].alt;
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300963 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300964 }
965out:
Hans de Goede314b3e02010-10-26 05:15:46 -0300966 gspca_input_create_urb(gspca_dev);
967unlock:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300968 mutex_unlock(&gspca_dev->usb_lock);
969 return ret;
970}
971
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300972static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300973{
Jean-François Moine6a330912010-10-02 04:12:25 -0300974 struct gspca_ctrl *ctrl;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300975 int i;
976
977 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
978 gspca_dev->curr_mode = i;
979 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
980 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300981 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-François Moine6a330912010-10-02 04:12:25 -0300982
983 /* set the current control values to their default values
984 * which may have changed in sd_init() */
985 ctrl = gspca_dev->cam.ctrls;
986 if (ctrl != NULL) {
987 for (i = 0;
988 i < gspca_dev->sd_desc->nctrls;
989 i++, ctrl++)
990 ctrl->val = ctrl->def;
991 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300992}
993
994static int wxh_to_mode(struct gspca_dev *gspca_dev,
995 int width, int height)
996{
997 int i;
998
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300999 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
1000 if (width >= gspca_dev->cam.cam_mode[i].width
1001 && height >= gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001002 break;
1003 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001004 return i;
1005}
1006
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001007/*
1008 * search a mode with the right pixel format
1009 */
1010static int gspca_get_mode(struct gspca_dev *gspca_dev,
1011 int mode,
1012 int pixfmt)
1013{
1014 int modeU, modeD;
1015
1016 modeU = modeD = mode;
1017 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
1018 if (--modeD >= 0) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001019 if (gspca_dev->cam.cam_mode[modeD].pixelformat
1020 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001021 return modeD;
1022 }
1023 if (++modeU < gspca_dev->cam.nmodes) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001024 if (gspca_dev->cam.cam_mode[modeU].pixelformat
1025 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001026 return modeU;
1027 }
1028 }
1029 return -EINVAL;
1030}
1031
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03001032#ifdef CONFIG_VIDEO_ADV_DEBUG
1033static int vidioc_g_register(struct file *file, void *priv,
1034 struct v4l2_dbg_register *reg)
1035{
1036 int ret;
1037 struct gspca_dev *gspca_dev = priv;
1038
1039 if (!gspca_dev->sd_desc->get_chip_ident)
1040 return -EINVAL;
1041
1042 if (!gspca_dev->sd_desc->get_register)
1043 return -EINVAL;
1044
1045 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1046 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001047 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03001048 if (gspca_dev->present)
1049 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
1050 else
1051 ret = -ENODEV;
1052 mutex_unlock(&gspca_dev->usb_lock);
1053
1054 return ret;
1055}
1056
1057static int vidioc_s_register(struct file *file, void *priv,
1058 struct v4l2_dbg_register *reg)
1059{
1060 int ret;
1061 struct gspca_dev *gspca_dev = priv;
1062
1063 if (!gspca_dev->sd_desc->get_chip_ident)
1064 return -EINVAL;
1065
1066 if (!gspca_dev->sd_desc->set_register)
1067 return -EINVAL;
1068
1069 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1070 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001071 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03001072 if (gspca_dev->present)
1073 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
1074 else
1075 ret = -ENODEV;
1076 mutex_unlock(&gspca_dev->usb_lock);
1077
1078 return ret;
1079}
1080#endif
1081
1082static int vidioc_g_chip_ident(struct file *file, void *priv,
1083 struct v4l2_dbg_chip_ident *chip)
1084{
1085 int ret;
1086 struct gspca_dev *gspca_dev = priv;
1087
1088 if (!gspca_dev->sd_desc->get_chip_ident)
1089 return -EINVAL;
1090
1091 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1092 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001093 gspca_dev->usb_err = 0;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03001094 if (gspca_dev->present)
1095 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
1096 else
1097 ret = -ENODEV;
1098 mutex_unlock(&gspca_dev->usb_lock);
1099
1100 return ret;
1101}
1102
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001103static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001104 struct v4l2_fmtdesc *fmtdesc)
1105{
1106 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001107 int i, j, index;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001108 __u32 fmt_tb[8];
1109
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001110 /* give an index to each format */
1111 index = 0;
1112 j = 0;
1113 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001114 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001115 j = 0;
1116 for (;;) {
1117 if (fmt_tb[j] == fmt_tb[index])
1118 break;
1119 j++;
1120 }
1121 if (j == index) {
1122 if (fmtdesc->index == index)
1123 break; /* new format */
1124 index++;
Julia Lawall80297122008-11-12 23:18:21 -03001125 if (index >= ARRAY_SIZE(fmt_tb))
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001126 return -EINVAL;
1127 }
1128 }
1129 if (i < 0)
1130 return -EINVAL; /* no more format */
1131
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001132 fmtdesc->pixelformat = fmt_tb[index];
Jean-François Moineb796fb62011-08-10 07:32:57 -03001133 if (gspca_dev->cam.cam_mode[i].sizeimage <
1134 gspca_dev->cam.cam_mode[i].width *
1135 gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001136 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
1137 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
1138 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
1139 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
1140 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
1141 fmtdesc->description[4] = '\0';
1142 return 0;
1143}
1144
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001145static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001146 struct v4l2_format *fmt)
1147{
1148 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001149 int mode;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001150
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001151 mode = gspca_dev->curr_mode;
1152 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1153 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001154 return 0;
1155}
1156
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001157static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001158 struct v4l2_format *fmt)
1159{
Hans de Goede24c530b2008-07-04 17:55:18 -03001160 int w, h, mode, mode2;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001161
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001162 w = fmt->fmt.pix.width;
1163 h = fmt->fmt.pix.height;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001164
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001165#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001166 if (gspca_debug & D_CONF)
1167 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
1168#endif
1169 /* search the closest mode for width and height */
1170 mode = wxh_to_mode(gspca_dev, w, h);
1171
1172 /* OK if right palette */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001173 if (gspca_dev->cam.cam_mode[mode].pixelformat
1174 != fmt->fmt.pix.pixelformat) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001175
1176 /* else, search the closest mode with the same pixel format */
1177 mode2 = gspca_get_mode(gspca_dev, mode,
1178 fmt->fmt.pix.pixelformat);
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001179 if (mode2 >= 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001180 mode = mode2;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001181/* else
1182 ; * no chance, return this mode */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001183 }
Jean-Francois Moinec2446b32008-07-05 11:49:20 -03001184 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1185 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001186 return mode; /* used when s_fmt */
1187}
1188
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001189static int vidioc_try_fmt_vid_cap(struct file *file,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001190 void *priv,
1191 struct v4l2_format *fmt)
1192{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001193 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001194 int ret;
1195
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001196 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001197 if (ret < 0)
1198 return ret;
1199 return 0;
1200}
1201
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001202static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001203 struct v4l2_format *fmt)
1204{
1205 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001206 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001207
Hans de Goedee2997a72008-04-23 08:09:12 -03001208 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1209 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001210
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001211 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001212 if (ret < 0)
1213 goto out;
1214
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001215 if (gspca_dev->nframes != 0
1216 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
1217 ret = -EINVAL;
1218 goto out;
1219 }
1220
Jean-Francois Moine50a871f2008-06-30 19:47:33 -03001221 if (ret == gspca_dev->curr_mode) {
1222 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001223 goto out; /* same mode */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -03001224 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001225
1226 if (gspca_dev->streaming) {
1227 ret = -EBUSY;
1228 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -03001229 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001230 gspca_dev->width = fmt->fmt.pix.width;
1231 gspca_dev->height = fmt->fmt.pix.height;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001232 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
1233 gspca_dev->curr_mode = ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001234
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001235 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001236out:
1237 mutex_unlock(&gspca_dev->queue_lock);
1238 return ret;
1239}
1240
Hans de Goede11c635a2009-05-25 15:04:22 -03001241static int vidioc_enum_framesizes(struct file *file, void *priv,
1242 struct v4l2_frmsizeenum *fsize)
1243{
1244 struct gspca_dev *gspca_dev = priv;
1245 int i;
1246 __u32 index = 0;
1247
1248 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
1249 if (fsize->pixel_format !=
1250 gspca_dev->cam.cam_mode[i].pixelformat)
1251 continue;
1252
1253 if (fsize->index == index) {
1254 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1255 fsize->discrete.width =
1256 gspca_dev->cam.cam_mode[i].width;
1257 fsize->discrete.height =
1258 gspca_dev->cam.cam_mode[i].height;
1259 return 0;
1260 }
1261 index++;
1262 }
1263
1264 return -EINVAL;
1265}
1266
Antonio Ospite28ffe772009-12-02 06:18:46 -03001267static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1268 struct v4l2_frmivalenum *fival)
1269{
1270 struct gspca_dev *gspca_dev = priv;
1271 int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
1272 __u32 i;
1273
1274 if (gspca_dev->cam.mode_framerates == NULL ||
1275 gspca_dev->cam.mode_framerates[mode].nrates == 0)
1276 return -EINVAL;
1277
1278 if (fival->pixel_format !=
1279 gspca_dev->cam.cam_mode[mode].pixelformat)
1280 return -EINVAL;
1281
1282 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1283 if (fival->index == i) {
1284 fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1285 fival->discrete.numerator = 1;
1286 fival->discrete.denominator =
1287 gspca_dev->cam.mode_framerates[mode].rates[i];
1288 return 0;
1289 }
1290 }
1291
1292 return -EINVAL;
1293}
1294
Hans de Goeded0182302008-11-19 17:10:47 -03001295static void gspca_release(struct video_device *vfd)
Frank Zago6b060ff2008-09-28 08:12:22 -03001296{
Hans de Goeded0182302008-11-19 17:10:47 -03001297 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
Frank Zago6b060ff2008-09-28 08:12:22 -03001298
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001299 PDEBUG(D_PROBE, "%s released",
1300 video_device_node_name(&gspca_dev->vdev));
Frank Zago6b060ff2008-09-28 08:12:22 -03001301
1302 kfree(gspca_dev->usb_buf);
1303 kfree(gspca_dev);
1304}
1305
Hans Verkuilbec43662008-12-30 06:58:20 -03001306static int dev_open(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001307{
1308 struct gspca_dev *gspca_dev;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001309
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001310 PDEBUG(D_STREAM, "[%s] open", current->comm);
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001311 gspca_dev = (struct gspca_dev *) video_devdata(file);
Hans de Goede7f6eb112010-12-30 20:20:09 -03001312 if (!gspca_dev->present)
1313 return -ENODEV;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03001314
1315 /* protect the subdriver against rmmod */
Hans de Goede7f6eb112010-12-30 20:20:09 -03001316 if (!try_module_get(gspca_dev->module))
1317 return -ENODEV;
Frank Zago6b060ff2008-09-28 08:12:22 -03001318
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001319 file->private_data = gspca_dev;
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001320#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001321 /* activate the v4l2 debug */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001322 if (gspca_debug & D_V4L2)
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 else
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001326 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -03001327 | V4L2_DEBUG_IOCTL_ARG);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001328#endif
Hans de Goede7f6eb112010-12-30 20:20:09 -03001329 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001330}
1331
Hans Verkuilbec43662008-12-30 06:58:20 -03001332static int dev_close(struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001333{
1334 struct gspca_dev *gspca_dev = file->private_data;
1335
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03001336 PDEBUG(D_STREAM, "[%s] close", current->comm);
Hans de Goedee2997a72008-04-23 08:09:12 -03001337 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1338 return -ERESTARTSYS;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001339
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001340 /* if the file did the capture, free the streaming resources */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001341 if (gspca_dev->capt_file == file) {
Jean-Francois Moinea1e82882008-09-03 17:12:17 -03001342 if (gspca_dev->streaming) {
1343 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001344 gspca_dev->usb_err = 0;
Jean-Francois Moinea1e82882008-09-03 17:12:17 -03001345 gspca_stream_off(gspca_dev);
1346 mutex_unlock(&gspca_dev->usb_lock);
1347 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001348 frame_free(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001349 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001350 file->private_data = NULL;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03001351 module_put(gspca_dev->module);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001352 mutex_unlock(&gspca_dev->queue_lock);
Frank Zago6b060ff2008-09-28 08:12:22 -03001353
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001354 PDEBUG(D_STREAM, "close done");
Frank Zago6b060ff2008-09-28 08:12:22 -03001355
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001356 return 0;
1357}
1358
1359static int vidioc_querycap(struct file *file, void *priv,
1360 struct v4l2_capability *cap)
1361{
1362 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001363 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001364
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001365 /* protect the access to the usb device */
1366 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1367 return -ERESTARTSYS;
1368 if (!gspca_dev->present) {
1369 ret = -ENODEV;
1370 goto out;
1371 }
Hans de Goedee7d712c2011-06-05 14:58:46 -03001372 strlcpy((char *) cap->driver, gspca_dev->sd_desc->name,
Jean-François Moined8518592010-12-28 06:55:01 -03001373 sizeof cap->driver);
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -03001374 if (gspca_dev->dev->product != NULL) {
Hans de Goedee7d712c2011-06-05 14:58:46 -03001375 strlcpy((char *) cap->card, gspca_dev->dev->product,
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -03001376 sizeof cap->card);
1377 } else {
Jean-François Moined8518592010-12-28 06:55:01 -03001378 snprintf((char *) cap->card, sizeof cap->card,
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -03001379 "USB Camera (%04x:%04x)",
1380 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1381 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1382 }
Jean-François Moined8518592010-12-28 06:55:01 -03001383 usb_make_path(gspca_dev->dev, (char *) cap->bus_info,
1384 sizeof(cap->bus_info));
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001385 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1386 | V4L2_CAP_STREAMING
1387 | V4L2_CAP_READWRITE;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001388 ret = 0;
1389out:
1390 mutex_unlock(&gspca_dev->usb_lock);
1391 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001392}
1393
Jean-François Moine6a330912010-10-02 04:12:25 -03001394static int get_ctrl(struct gspca_dev *gspca_dev,
Hans de Goedea0001a22009-06-10 04:52:18 -03001395 int id)
1396{
1397 const struct ctrl *ctrls;
1398 int i;
1399
1400 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1401 i < gspca_dev->sd_desc->nctrls;
1402 i++, ctrls++) {
1403 if (gspca_dev->ctrl_dis & (1 << i))
1404 continue;
1405 if (id == ctrls->qctrl.id)
Jean-François Moine6a330912010-10-02 04:12:25 -03001406 return i;
Hans de Goedea0001a22009-06-10 04:52:18 -03001407 }
Jean-François Moine6a330912010-10-02 04:12:25 -03001408 return -1;
Hans de Goedea0001a22009-06-10 04:52:18 -03001409}
1410
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001411static int vidioc_queryctrl(struct file *file, void *priv,
1412 struct v4l2_queryctrl *q_ctrl)
1413{
1414 struct gspca_dev *gspca_dev = priv;
Hans de Goedea0001a22009-06-10 04:52:18 -03001415 const struct ctrl *ctrls;
Jean-François Moine6a330912010-10-02 04:12:25 -03001416 struct gspca_ctrl *gspca_ctrl;
1417 int i, idx;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001418 u32 id;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001419
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001420 id = q_ctrl->id;
1421 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1422 id &= V4L2_CTRL_ID_MASK;
1423 id++;
Jean-François Moine6a330912010-10-02 04:12:25 -03001424 idx = -1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001425 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001426 if (gspca_dev->ctrl_dis & (1 << i))
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001427 continue;
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001428 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001429 continue;
Jean-François Moine6a330912010-10-02 04:12:25 -03001430 if (idx >= 0
1431 && gspca_dev->sd_desc->ctrls[i].qctrl.id
Jean-François Moine780e3122010-10-19 04:29:10 -03001432 > gspca_dev->sd_desc->ctrls[idx].qctrl.id)
Hans de Goedeb8bfb5f2009-06-13 18:56:22 -03001433 continue;
Jean-François Moine6a330912010-10-02 04:12:25 -03001434 idx = i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001435 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001436 } else {
Jean-François Moine6a330912010-10-02 04:12:25 -03001437 idx = get_ctrl(gspca_dev, id);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001438 }
Jean-François Moine6a330912010-10-02 04:12:25 -03001439 if (idx < 0)
1440 return -EINVAL;
1441 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1442 memcpy(q_ctrl, &ctrls->qctrl, sizeof *q_ctrl);
1443 if (gspca_dev->cam.ctrls != NULL) {
1444 gspca_ctrl = &gspca_dev->cam.ctrls[idx];
1445 q_ctrl->default_value = gspca_ctrl->def;
1446 q_ctrl->minimum = gspca_ctrl->min;
1447 q_ctrl->maximum = gspca_ctrl->max;
1448 }
1449 if (gspca_dev->ctrl_inac & (1 << idx))
Jean-Francois Moine4af85662009-11-12 15:59:27 -03001450 q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001451 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001452}
1453
1454static int vidioc_s_ctrl(struct file *file, void *priv,
1455 struct v4l2_control *ctrl)
1456{
1457 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001458 const struct ctrl *ctrls;
Jean-François Moine6a330912010-10-02 04:12:25 -03001459 struct gspca_ctrl *gspca_ctrl;
1460 int idx, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001461
Jean-François Moine6a330912010-10-02 04:12:25 -03001462 idx = get_ctrl(gspca_dev, ctrl->id);
1463 if (idx < 0)
Hans de Goedea0001a22009-06-10 04:52:18 -03001464 return -EINVAL;
Jean-François Moine6a330912010-10-02 04:12:25 -03001465 if (gspca_dev->ctrl_inac & (1 << idx))
1466 return -EINVAL;
1467 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1468 if (gspca_dev->cam.ctrls != NULL) {
1469 gspca_ctrl = &gspca_dev->cam.ctrls[idx];
1470 if (ctrl->value < gspca_ctrl->min
1471 || ctrl->value > gspca_ctrl->max)
1472 return -ERANGE;
1473 } else {
1474 gspca_ctrl = NULL;
1475 if (ctrl->value < ctrls->qctrl.minimum
1476 || ctrl->value > ctrls->qctrl.maximum)
1477 return -ERANGE;
1478 }
Hans de Goedea0001a22009-06-10 04:52:18 -03001479 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1480 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1481 return -ERESTARTSYS;
Jean-François Moine6a330912010-10-02 04:12:25 -03001482 if (!gspca_dev->present) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001483 ret = -ENODEV;
Jean-François Moine6a330912010-10-02 04:12:25 -03001484 goto out;
1485 }
1486 gspca_dev->usb_err = 0;
1487 if (ctrls->set != NULL) {
1488 ret = ctrls->set(gspca_dev, ctrl->value);
1489 goto out;
1490 }
1491 if (gspca_ctrl != NULL) {
1492 gspca_ctrl->val = ctrl->value;
1493 if (ctrls->set_control != NULL
1494 && gspca_dev->streaming)
1495 ctrls->set_control(gspca_dev);
1496 }
1497 ret = gspca_dev->usb_err;
1498out:
Hans de Goedea0001a22009-06-10 04:52:18 -03001499 mutex_unlock(&gspca_dev->usb_lock);
1500 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001501}
1502
1503static int vidioc_g_ctrl(struct file *file, void *priv,
1504 struct v4l2_control *ctrl)
1505{
1506 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001507 const struct ctrl *ctrls;
Jean-François Moine6a330912010-10-02 04:12:25 -03001508 int idx, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001509
Jean-François Moine6a330912010-10-02 04:12:25 -03001510 idx = get_ctrl(gspca_dev, ctrl->id);
1511 if (idx < 0)
Hans de Goedea0001a22009-06-10 04:52:18 -03001512 return -EINVAL;
Jean-François Moine6a330912010-10-02 04:12:25 -03001513 ctrls = &gspca_dev->sd_desc->ctrls[idx];
Hans de Goedea0001a22009-06-10 04:52:18 -03001514
1515 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1516 return -ERESTARTSYS;
Jean-François Moine6a330912010-10-02 04:12:25 -03001517 if (!gspca_dev->present) {
Hans de Goedea0001a22009-06-10 04:52:18 -03001518 ret = -ENODEV;
Jean-François Moine6a330912010-10-02 04:12:25 -03001519 goto out;
1520 }
1521 gspca_dev->usb_err = 0;
1522 if (ctrls->get != NULL) {
1523 ret = ctrls->get(gspca_dev, &ctrl->value);
1524 goto out;
1525 }
1526 if (gspca_dev->cam.ctrls != NULL)
1527 ctrl->value = gspca_dev->cam.ctrls[idx].val;
1528 ret = 0;
1529out:
Hans de Goedea0001a22009-06-10 04:52:18 -03001530 mutex_unlock(&gspca_dev->usb_lock);
1531 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001532}
1533
1534static int vidioc_querymenu(struct file *file, void *priv,
1535 struct v4l2_querymenu *qmenu)
1536{
1537 struct gspca_dev *gspca_dev = priv;
1538
1539 if (!gspca_dev->sd_desc->querymenu)
1540 return -EINVAL;
1541 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1542}
1543
1544static int vidioc_enum_input(struct file *file, void *priv,
1545 struct v4l2_input *input)
1546{
1547 struct gspca_dev *gspca_dev = priv;
1548
1549 if (input->index != 0)
1550 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001551 input->type = V4L2_INPUT_TYPE_CAMERA;
Adam Bakerdfa76fa2009-03-29 19:17:10 -03001552 input->status = gspca_dev->cam.input_flags;
Hans de Goedee7d712c2011-06-05 14:58:46 -03001553 strlcpy(input->name, gspca_dev->sd_desc->name,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001554 sizeof input->name);
1555 return 0;
1556}
1557
1558static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1559{
1560 *i = 0;
1561 return 0;
1562}
1563
1564static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1565{
1566 if (i > 0)
1567 return -EINVAL;
1568 return (0);
1569}
1570
1571static int vidioc_reqbufs(struct file *file, void *priv,
1572 struct v4l2_requestbuffers *rb)
1573{
1574 struct gspca_dev *gspca_dev = priv;
Jean-François Moine832d0a92010-04-09 06:07:58 -03001575 int i, ret = 0, streaming;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001576
Jean-François Moinea2ba6f22010-07-14 06:21:16 -03001577 i = rb->memory; /* (avoid compilation warning) */
1578 switch (i) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001579 case GSPCA_MEMORY_READ: /* (internal call) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001580 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001581 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001582 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001583 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001584 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001585 }
Hans de Goedee2997a72008-04-23 08:09:12 -03001586 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1587 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001588
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001589 if (gspca_dev->memory != GSPCA_MEMORY_NO
Hans de Goedece5610b2010-12-31 07:41:54 -03001590 && gspca_dev->memory != GSPCA_MEMORY_READ
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001591 && gspca_dev->memory != rb->memory) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001592 ret = -EBUSY;
1593 goto out;
1594 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001595
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001596 /* only one file may do the capture */
1597 if (gspca_dev->capt_file != NULL
1598 && gspca_dev->capt_file != file) {
1599 ret = -EBUSY;
1600 goto out;
1601 }
1602
1603 /* if allocated, the buffers must not be mapped */
1604 for (i = 0; i < gspca_dev->nframes; i++) {
1605 if (gspca_dev->frame[i].vma_use_count) {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001606 ret = -EBUSY;
1607 goto out;
1608 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001609 }
1610
1611 /* stop streaming */
Jean-François Moine832d0a92010-04-09 06:07:58 -03001612 streaming = gspca_dev->streaming;
1613 if (streaming) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001614 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001615 gspca_dev->usb_err = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001616 gspca_stream_off(gspca_dev);
1617 mutex_unlock(&gspca_dev->usb_lock);
Jean-François Moine59929ce2011-03-14 05:33:01 -03001618
1619 /* Don't restart the stream when switching from read
1620 * to mmap mode */
1621 if (gspca_dev->memory == GSPCA_MEMORY_READ)
1622 streaming = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001623 }
1624
1625 /* free the previous allocated buffers, if any */
Hans de Goeded642de22010-12-31 04:51:36 -03001626 if (gspca_dev->nframes != 0)
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001627 frame_free(gspca_dev);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001628 if (rb->count == 0) /* unrequest */
1629 goto out;
Hans de Goeded642de22010-12-31 04:51:36 -03001630 ret = frame_alloc(gspca_dev, file, rb->memory, rb->count);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001631 if (ret == 0) {
1632 rb->count = gspca_dev->nframes;
Jean-François Moine832d0a92010-04-09 06:07:58 -03001633 if (streaming)
1634 ret = gspca_init_transfer(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001635 }
1636out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001637 mutex_unlock(&gspca_dev->queue_lock);
1638 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1639 return ret;
1640}
1641
1642static int vidioc_querybuf(struct file *file, void *priv,
1643 struct v4l2_buffer *v4l2_buf)
1644{
1645 struct gspca_dev *gspca_dev = priv;
1646 struct gspca_frame *frame;
1647
Jean-Francois Moine13752bd2008-12-19 15:08:51 -03001648 if (v4l2_buf->index < 0
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001649 || v4l2_buf->index >= gspca_dev->nframes)
1650 return -EINVAL;
1651
1652 frame = &gspca_dev->frame[v4l2_buf->index];
1653 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1654 return 0;
1655}
1656
1657static int vidioc_streamon(struct file *file, void *priv,
1658 enum v4l2_buf_type buf_type)
1659{
1660 struct gspca_dev *gspca_dev = priv;
1661 int ret;
1662
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001663 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1664 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001665 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1666 return -ERESTARTSYS;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001667
Jean-François Moine18bf99a2010-04-09 06:06:11 -03001668 /* check the capture file */
1669 if (gspca_dev->capt_file != file) {
1670 ret = -EBUSY;
1671 goto out;
1672 }
1673
Jean-Francois Moine1edabe72008-12-19 15:17:25 -03001674 if (gspca_dev->nframes == 0
1675 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001676 ret = -EINVAL;
1677 goto out;
1678 }
1679 if (!gspca_dev->streaming) {
1680 ret = gspca_init_transfer(gspca_dev);
1681 if (ret < 0)
1682 goto out;
1683 }
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001684#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001685 if (gspca_debug & D_STREAM) {
1686 PDEBUG_MODE("stream on OK",
1687 gspca_dev->pixfmt,
1688 gspca_dev->width,
1689 gspca_dev->height);
1690 }
1691#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001692 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001693out:
1694 mutex_unlock(&gspca_dev->queue_lock);
1695 return ret;
1696}
1697
1698static int vidioc_streamoff(struct file *file, void *priv,
1699 enum v4l2_buf_type buf_type)
1700{
1701 struct gspca_dev *gspca_dev = priv;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001702 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001703
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001704 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1705 return -EINVAL;
Hans de Goede4e770f72010-12-30 13:11:21 -03001706
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001707 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1708 return -ERESTARTSYS;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001709
Hans de Goede4e770f72010-12-30 13:11:21 -03001710 if (!gspca_dev->streaming) {
1711 ret = 0;
1712 goto out;
1713 }
1714
Jean-François Moine18bf99a2010-04-09 06:06:11 -03001715 /* check the capture file */
1716 if (gspca_dev->capt_file != file) {
1717 ret = -EBUSY;
1718 goto out;
1719 }
1720
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001721 /* stop streaming */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001722 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1723 ret = -ERESTARTSYS;
1724 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001725 }
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001726 gspca_dev->usb_err = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001727 gspca_stream_off(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001728 mutex_unlock(&gspca_dev->usb_lock);
Hans de Goede0d0ae152010-12-31 17:17:51 -03001729 /* In case another thread is waiting in dqbuf */
1730 wake_up_interruptible(&gspca_dev->wq);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001731
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001732 /* empty the transfer queues */
1733 atomic_set(&gspca_dev->fr_q, 0);
1734 atomic_set(&gspca_dev->fr_i, 0);
1735 gspca_dev->fr_o = 0;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001736 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001737out:
1738 mutex_unlock(&gspca_dev->queue_lock);
1739 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001740}
1741
1742static int vidioc_g_jpegcomp(struct file *file, void *priv,
1743 struct v4l2_jpegcompression *jpegcomp)
1744{
1745 struct gspca_dev *gspca_dev = priv;
1746 int ret;
1747
1748 if (!gspca_dev->sd_desc->get_jcomp)
1749 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001750 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1751 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001752 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001753 if (gspca_dev->present)
1754 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1755 else
1756 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001757 mutex_unlock(&gspca_dev->usb_lock);
1758 return ret;
1759}
1760
1761static int vidioc_s_jpegcomp(struct file *file, void *priv,
1762 struct v4l2_jpegcompression *jpegcomp)
1763{
1764 struct gspca_dev *gspca_dev = priv;
1765 int ret;
1766
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001767 if (!gspca_dev->sd_desc->set_jcomp)
1768 return -EINVAL;
Jim Parisf86d4a92008-12-10 05:50:44 -03001769 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1770 return -ERESTARTSYS;
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -03001771 gspca_dev->usb_err = 0;
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001772 if (gspca_dev->present)
1773 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1774 else
1775 ret = -ENODEV;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001776 mutex_unlock(&gspca_dev->usb_lock);
1777 return ret;
1778}
1779
1780static int vidioc_g_parm(struct file *filp, void *priv,
1781 struct v4l2_streamparm *parm)
1782{
1783 struct gspca_dev *gspca_dev = priv;
1784
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001785 parm->parm.capture.readbuffers = gspca_dev->nbufread;
Jim Paris627a5ef2008-12-10 06:02:42 -03001786
1787 if (gspca_dev->sd_desc->get_streamparm) {
1788 int ret;
1789
1790 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1791 return -ERESTARTSYS;
Jean-François Moine668f44a2010-12-25 13:46:14 -03001792 if (gspca_dev->present) {
1793 gspca_dev->usb_err = 0;
1794 gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
1795 ret = gspca_dev->usb_err;
1796 } else {
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001797 ret = -ENODEV;
Jean-François Moine668f44a2010-12-25 13:46:14 -03001798 }
Jim Paris627a5ef2008-12-10 06:02:42 -03001799 mutex_unlock(&gspca_dev->usb_lock);
1800 return ret;
1801 }
1802
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001803 return 0;
1804}
1805
1806static int vidioc_s_parm(struct file *filp, void *priv,
1807 struct v4l2_streamparm *parm)
1808{
1809 struct gspca_dev *gspca_dev = priv;
1810 int n;
1811
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001812 n = parm->parm.capture.readbuffers;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03001813 if (n == 0 || n >= GSPCA_MAX_FRAMES)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001814 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1815 else
1816 gspca_dev->nbufread = n;
Jim Paris627a5ef2008-12-10 06:02:42 -03001817
1818 if (gspca_dev->sd_desc->set_streamparm) {
1819 int ret;
1820
1821 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1822 return -ERESTARTSYS;
Jean-François Moine668f44a2010-12-25 13:46:14 -03001823 if (gspca_dev->present) {
1824 gspca_dev->usb_err = 0;
1825 gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
1826 ret = gspca_dev->usb_err;
1827 } else {
Jean-Francois Moine090264e2009-02-12 08:05:45 -03001828 ret = -ENODEV;
Jean-François Moine668f44a2010-12-25 13:46:14 -03001829 }
Jim Paris627a5ef2008-12-10 06:02:42 -03001830 mutex_unlock(&gspca_dev->usb_lock);
1831 return ret;
1832 }
1833
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001834 return 0;
1835}
1836
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001837static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1838{
1839 struct gspca_dev *gspca_dev = file->private_data;
Harvey Harrisonefab8212008-07-05 06:12:47 -03001840 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001841 struct page *page;
1842 unsigned long addr, start, size;
1843 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001844
1845 start = vma->vm_start;
1846 size = vma->vm_end - vma->vm_start;
1847 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1848
Hans de Goedee2997a72008-04-23 08:09:12 -03001849 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1850 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001851 if (!gspca_dev->present) {
1852 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001853 goto out;
1854 }
1855 if (gspca_dev->capt_file != file) {
1856 ret = -EINVAL;
1857 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001858 }
1859
Harvey Harrisonefab8212008-07-05 06:12:47 -03001860 frame = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001861 for (i = 0; i < gspca_dev->nframes; ++i) {
1862 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1863 PDEBUG(D_STREAM, "mmap bad memory type");
1864 break;
1865 }
1866 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1867 == vma->vm_pgoff) {
1868 frame = &gspca_dev->frame[i];
1869 break;
1870 }
1871 }
Harvey Harrisonefab8212008-07-05 06:12:47 -03001872 if (frame == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001873 PDEBUG(D_STREAM, "mmap no frame buffer found");
1874 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001875 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001876 }
Jean-François Moine38bff692010-06-24 05:19:58 -03001877 if (size != frame->v4l2_buf.length) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001878 PDEBUG(D_STREAM, "mmap bad size");
1879 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001880 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001881 }
1882
1883 /*
1884 * - VM_IO marks the area as being a mmaped region for I/O to a
1885 * device. It also prevents the region from being core dumped.
1886 */
1887 vma->vm_flags |= VM_IO;
1888
1889 addr = (unsigned long) frame->data;
1890 while (size > 0) {
1891 page = vmalloc_to_page((void *) addr);
1892 ret = vm_insert_page(vma, start, page);
1893 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001894 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001895 start += PAGE_SIZE;
1896 addr += PAGE_SIZE;
1897 size -= PAGE_SIZE;
1898 }
1899
Jean-Francois Moined50e2572009-11-26 15:36:40 -03001900 vma->vm_ops = &gspca_vm_ops;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001901 vma->vm_private_data = frame;
1902 gspca_vm_open(vma);
Hans de Goedee2997a72008-04-23 08:09:12 -03001903 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001904out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001905 mutex_unlock(&gspca_dev->queue_lock);
1906 return ret;
1907}
1908
Hans de Goede27074ef2010-12-30 19:54:33 -03001909static int frame_ready_nolock(struct gspca_dev *gspca_dev, struct file *file,
1910 enum v4l2_memory memory)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001911{
Hans de Goede27074ef2010-12-30 19:54:33 -03001912 if (!gspca_dev->present)
1913 return -ENODEV;
1914 if (gspca_dev->capt_file != file || gspca_dev->memory != memory ||
1915 !gspca_dev->streaming)
1916 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001917
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001918 /* check if a frame is ready */
Hans de Goede27074ef2010-12-30 19:54:33 -03001919 return gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i);
1920}
Hans de Goede181b7042008-11-19 17:16:26 -03001921
Hans de Goede27074ef2010-12-30 19:54:33 -03001922static int frame_ready(struct gspca_dev *gspca_dev, struct file *file,
1923 enum v4l2_memory memory)
1924{
1925 int ret;
Hans de Goede181b7042008-11-19 17:16:26 -03001926
Hans de Goede27074ef2010-12-30 19:54:33 -03001927 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1928 return -ERESTARTSYS;
1929 ret = frame_ready_nolock(gspca_dev, file, memory);
1930 mutex_unlock(&gspca_dev->queue_lock);
1931 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001932}
1933
1934/*
1935 * dequeue a video buffer
1936 *
1937 * If nonblock_ing is false, block until a buffer is available.
1938 */
1939static int vidioc_dqbuf(struct file *file, void *priv,
1940 struct v4l2_buffer *v4l2_buf)
1941{
1942 struct gspca_dev *gspca_dev = priv;
1943 struct gspca_frame *frame;
Hans de Goede27074ef2010-12-30 19:54:33 -03001944 int i, j, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001945
1946 PDEBUG(D_FRAM, "dqbuf");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001947
Hans de Goede27074ef2010-12-30 19:54:33 -03001948 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001949 return -ERESTARTSYS;
1950
Hans de Goede27074ef2010-12-30 19:54:33 -03001951 for (;;) {
1952 ret = frame_ready_nolock(gspca_dev, file, v4l2_buf->memory);
1953 if (ret < 0)
1954 goto out;
1955 if (ret > 0)
1956 break;
1957
1958 mutex_unlock(&gspca_dev->queue_lock);
1959
1960 if (file->f_flags & O_NONBLOCK)
1961 return -EAGAIN;
1962
1963 /* wait till a frame is ready */
1964 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1965 frame_ready(gspca_dev, file, v4l2_buf->memory),
1966 msecs_to_jiffies(3000));
1967 if (ret < 0)
1968 return ret;
1969 if (ret == 0)
1970 return -EIO;
1971
1972 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1973 return -ERESTARTSYS;
1974 }
1975
1976 i = gspca_dev->fr_o;
1977 j = gspca_dev->fr_queue[i];
1978 frame = &gspca_dev->frame[j];
1979
1980 gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES;
1981
1982 if (gspca_dev->sd_desc->dq_callback) {
1983 mutex_lock(&gspca_dev->usb_lock);
1984 gspca_dev->usb_err = 0;
1985 if (gspca_dev->present)
1986 gspca_dev->sd_desc->dq_callback(gspca_dev);
1987 mutex_unlock(&gspca_dev->usb_lock);
1988 }
1989
Hans de Goede4a82bc62010-12-30 20:00:17 -03001990 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1991 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1992 PDEBUG(D_FRAM, "dqbuf %d", j);
1993 ret = 0;
1994
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001995 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001996 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001997 frame->data,
1998 frame->v4l2_buf.bytesused)) {
1999 PDEBUG(D_ERR|D_STREAM,
2000 "dqbuf cp to user failed");
2001 ret = -EFAULT;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002002 }
2003 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002004out:
Hans de Goede27074ef2010-12-30 19:54:33 -03002005 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002006 return ret;
2007}
2008
2009/*
2010 * queue a video buffer
2011 *
2012 * Attempting to queue a buffer that has already been
2013 * queued will return -EINVAL.
2014 */
2015static int vidioc_qbuf(struct file *file, void *priv,
2016 struct v4l2_buffer *v4l2_buf)
2017{
2018 struct gspca_dev *gspca_dev = priv;
2019 struct gspca_frame *frame;
2020 int i, index, ret;
2021
2022 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002023
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002024 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
2025 return -ERESTARTSYS;
2026
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002027 index = v4l2_buf->index;
2028 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002029 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002030 "qbuf idx %d >= %d", index, gspca_dev->nframes);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002031 ret = -EINVAL;
2032 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002033 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002034 if (v4l2_buf->memory != gspca_dev->memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002035 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002036 ret = -EINVAL;
2037 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002038 }
2039
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002040 frame = &gspca_dev->frame[index];
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002041 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
2042 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002043 ret = -EINVAL;
2044 goto out;
2045 }
2046
2047 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002048
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002049 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002050 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
2051 frame->v4l2_buf.length = v4l2_buf->length;
2052 }
2053
2054 /* put the buffer in the 'queued' queue */
Jean-François Moinef7059ea2010-07-06 04:32:27 -03002055 i = atomic_read(&gspca_dev->fr_q);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002056 gspca_dev->fr_queue[i] = index;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03002057 atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002058
2059 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
2060 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
2061 ret = 0;
2062out:
2063 mutex_unlock(&gspca_dev->queue_lock);
2064 return ret;
2065}
2066
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002067/*
2068 * allocate the resources for read()
2069 */
2070static int read_alloc(struct gspca_dev *gspca_dev,
2071 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002072{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002073 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002074 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002075
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002076 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002077 if (gspca_dev->nframes == 0) {
2078 struct v4l2_requestbuffers rb;
2079
2080 memset(&rb, 0, sizeof rb);
2081 rb.count = gspca_dev->nbufread;
2082 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002083 rb.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002084 ret = vidioc_reqbufs(file, gspca_dev, &rb);
2085 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002086 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002087 return ret;
2088 }
2089 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2090 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002091 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002092 for (i = 0; i < gspca_dev->nbufread; i++) {
2093 v4l2_buf.index = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002094 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2095 if (ret != 0) {
2096 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
2097 return ret;
2098 }
2099 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002100 gspca_dev->memory = GSPCA_MEMORY_READ;
2101 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002102
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002103 /* start streaming */
2104 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2105 if (ret != 0)
2106 PDEBUG(D_STREAM, "read streamon err %d", ret);
2107 return ret;
2108}
2109
2110static unsigned int dev_poll(struct file *file, poll_table *wait)
2111{
2112 struct gspca_dev *gspca_dev = file->private_data;
Jean-François Moinef7059ea2010-07-06 04:32:27 -03002113 int ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002114
2115 PDEBUG(D_FRAM, "poll");
2116
2117 poll_wait(file, &gspca_dev->wq, wait);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002118
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002119 /* if reqbufs is not done, the user would use read() */
Hans de Goedeee362992010-12-31 05:05:56 -03002120 if (gspca_dev->memory == GSPCA_MEMORY_NO) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002121 ret = read_alloc(gspca_dev, file);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03002122 if (ret != 0)
2123 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002124 }
2125
2126 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
2127 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002128
Jean-François Moinef7059ea2010-07-06 04:32:27 -03002129 /* check if an image has been received */
2130 if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i))
2131 ret = POLLIN | POLLRDNORM; /* yes */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002132 else
2133 ret = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002134 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002135 if (!gspca_dev->present)
2136 return POLLHUP;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002137 return ret;
2138}
2139
2140static ssize_t dev_read(struct file *file, char __user *data,
2141 size_t count, loff_t *ppos)
2142{
2143 struct gspca_dev *gspca_dev = file->private_data;
2144 struct gspca_frame *frame;
2145 struct v4l2_buffer v4l2_buf;
2146 struct timeval timestamp;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002147 int n, ret, ret2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002148
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03002149 PDEBUG(D_FRAM, "read (%zd)", count);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002150 if (!gspca_dev->present)
2151 return -ENODEV;
Hans de Goedeee362992010-12-31 05:05:56 -03002152 if (gspca_dev->memory == GSPCA_MEMORY_NO) { /* first time ? */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002153 ret = read_alloc(gspca_dev, file);
2154 if (ret != 0)
2155 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002156 }
2157
2158 /* get a frame */
Paulo Assis2f51d002010-03-07 06:56:56 -03002159 timestamp = ktime_to_timeval(ktime_get());
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002160 timestamp.tv_sec--;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002161 n = 2;
2162 for (;;) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002163 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2164 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002165 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002166 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
2167 if (ret != 0) {
2168 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
2169 return ret;
2170 }
2171
2172 /* if the process slept for more than 1 second,
Frank Zago219423c2008-09-26 07:43:54 -03002173 * get a newer frame */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002174 frame = &gspca_dev->frame[v4l2_buf.index];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03002175 if (--n < 0)
2176 break; /* avoid infinite loop */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002177 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
2178 break;
2179 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2180 if (ret != 0) {
2181 PDEBUG(D_STREAM, "read qbuf err %d", ret);
2182 return ret;
2183 }
2184 }
2185
2186 /* copy the frame */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03002187 if (count > frame->v4l2_buf.bytesused)
2188 count = frame->v4l2_buf.bytesused;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002189 ret = copy_to_user(data, frame->data, count);
2190 if (ret != 0) {
2191 PDEBUG(D_ERR|D_STREAM,
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03002192 "read cp to user lack %d / %zd", ret, count);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002193 ret = -EFAULT;
2194 goto out;
2195 }
2196 ret = count;
2197out:
2198 /* in each case, requeue the buffer */
2199 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2200 if (ret2 != 0)
2201 return ret2;
2202 return ret;
2203}
2204
Hans Verkuilbec43662008-12-30 06:58:20 -03002205static struct v4l2_file_operations dev_fops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002206 .owner = THIS_MODULE,
2207 .open = dev_open,
2208 .release = dev_close,
2209 .read = dev_read,
2210 .mmap = dev_mmap,
Hans Verkuilbec43662008-12-30 06:58:20 -03002211 .unlocked_ioctl = video_ioctl2,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002212 .poll = dev_poll,
2213};
2214
Hans Verkuila3998102008-07-21 02:57:38 -03002215static const struct v4l2_ioctl_ops dev_ioctl_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002216 .vidioc_querycap = vidioc_querycap,
2217 .vidioc_dqbuf = vidioc_dqbuf,
2218 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03002219 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2220 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2221 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2222 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002223 .vidioc_streamon = vidioc_streamon,
2224 .vidioc_queryctrl = vidioc_queryctrl,
2225 .vidioc_g_ctrl = vidioc_g_ctrl,
2226 .vidioc_s_ctrl = vidioc_s_ctrl,
2227 .vidioc_querymenu = vidioc_querymenu,
2228 .vidioc_enum_input = vidioc_enum_input,
2229 .vidioc_g_input = vidioc_g_input,
2230 .vidioc_s_input = vidioc_s_input,
2231 .vidioc_reqbufs = vidioc_reqbufs,
2232 .vidioc_querybuf = vidioc_querybuf,
2233 .vidioc_streamoff = vidioc_streamoff,
2234 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
2235 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
2236 .vidioc_g_parm = vidioc_g_parm,
2237 .vidioc_s_parm = vidioc_s_parm,
Hans de Goede11c635a2009-05-25 15:04:22 -03002238 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Antonio Ospite28ffe772009-12-02 06:18:46 -03002239 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
Brian Johnsonaf1d9af2009-07-19 05:29:20 -03002240#ifdef CONFIG_VIDEO_ADV_DEBUG
2241 .vidioc_g_register = vidioc_g_register,
2242 .vidioc_s_register = vidioc_s_register,
2243#endif
2244 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002245};
2246
Jean-François Moine59929ce2011-03-14 05:33:01 -03002247static const struct video_device gspca_template = {
Hans Verkuila3998102008-07-21 02:57:38 -03002248 .name = "gspca main driver",
Hans Verkuila3998102008-07-21 02:57:38 -03002249 .fops = &dev_fops,
2250 .ioctl_ops = &dev_ioctl_ops,
Hans de Goeded0182302008-11-19 17:10:47 -03002251 .release = gspca_release,
Hans Verkuila3998102008-07-21 02:57:38 -03002252};
2253
Jean-François Moine6a330912010-10-02 04:12:25 -03002254/* initialize the controls */
2255static void ctrls_init(struct gspca_dev *gspca_dev)
2256{
2257 struct gspca_ctrl *ctrl;
2258 int i;
2259
2260 for (i = 0, ctrl = gspca_dev->cam.ctrls;
2261 i < gspca_dev->sd_desc->nctrls;
2262 i++, ctrl++) {
2263 ctrl->def = gspca_dev->sd_desc->ctrls[i].qctrl.default_value;
2264 ctrl->val = ctrl->def;
2265 ctrl->min = gspca_dev->sd_desc->ctrls[i].qctrl.minimum;
2266 ctrl->max = gspca_dev->sd_desc->ctrls[i].qctrl.maximum;
2267 }
2268}
2269
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002270/*
2271 * probe and create a new gspca device
2272 *
2273 * This function must be called by the sub-driver when it is
2274 * called for probing a new device.
2275 */
Jean-François Moine44628642010-06-05 07:47:36 -03002276int gspca_dev_probe2(struct usb_interface *intf,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002277 const struct usb_device_id *id,
2278 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002279 int dev_size,
2280 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002281{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002282 struct gspca_dev *gspca_dev;
2283 struct usb_device *dev = interface_to_usbdev(intf);
2284 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002285
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002286 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002287
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002288 /* create the device */
2289 if (dev_size < sizeof *gspca_dev)
2290 dev_size = sizeof *gspca_dev;
2291 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
Erik Andrene293e592008-10-03 08:46:50 -03002292 if (!gspca_dev) {
Joe Perches133a9fe2011-08-21 19:56:57 -03002293 pr_err("couldn't kzalloc gspca struct\n");
Erik Andrene293e592008-10-03 08:46:50 -03002294 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002295 }
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002296 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2297 if (!gspca_dev->usb_buf) {
Joe Perches133a9fe2011-08-21 19:56:57 -03002298 pr_err("out of memory\n");
Erik Andrene293e592008-10-03 08:46:50 -03002299 ret = -ENOMEM;
Jean-Francois Moine8295d992008-09-03 17:12:19 -03002300 goto out;
2301 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002302 gspca_dev->dev = dev;
Jean-François Moine44628642010-06-05 07:47:36 -03002303 gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002304 gspca_dev->nbalt = intf->num_altsetting;
Jean-François Moine35680ba2010-07-14 06:30:18 -03002305
2306 /* check if any audio device */
2307 if (dev->config->desc.bNumInterfaces != 1) {
2308 int i;
2309 struct usb_interface *intf2;
2310
2311 for (i = 0; i < dev->config->desc.bNumInterfaces; i++) {
2312 intf2 = dev->config->interface[i];
2313 if (intf2 != NULL
2314 && intf2->altsetting != NULL
2315 && intf2->altsetting->desc.bInterfaceClass ==
2316 USB_CLASS_AUDIO) {
2317 gspca_dev->audio = 1;
2318 break;
2319 }
2320 }
2321 }
2322
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002323 gspca_dev->sd_desc = sd_desc;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002324 gspca_dev->nbufread = 2;
Jean-Francois Moineff374742008-10-23 07:29:51 -03002325 gspca_dev->empty_packet = -1; /* don't check the empty packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002326
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002327 /* configure the subdriver and initialize the USB device */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002328 ret = sd_desc->config(gspca_dev, id);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002329 if (ret < 0)
2330 goto out;
Jean-François Moine6a330912010-10-02 04:12:25 -03002331 if (gspca_dev->cam.ctrls != NULL)
2332 ctrls_init(gspca_dev);
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002333 ret = sd_desc->init(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002334 if (ret < 0)
2335 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002336 gspca_set_default_mode(gspca_dev);
2337
Hans de Goede10ee2402010-02-19 04:41:40 -03002338 ret = gspca_input_connect(gspca_dev);
2339 if (ret)
2340 goto out;
2341
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002342 mutex_init(&gspca_dev->usb_lock);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002343 mutex_init(&gspca_dev->queue_lock);
2344 init_waitqueue_head(&gspca_dev->wq);
2345
2346 /* init video stuff */
Jean-Francois Moineba38ee82008-11-22 04:27:34 -03002347 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
Hans de Goede24441632009-03-27 16:08:20 -03002348 gspca_dev->vdev.parent = &intf->dev;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03002349 gspca_dev->module = module;
Oliver Neukumee56a4d2008-07-27 14:01:59 -03002350 gspca_dev->present = 1;
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002351 ret = video_register_device(&gspca_dev->vdev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002352 VFL_TYPE_GRABBER,
Jean-Francois Moine8d584a52009-01-13 06:07:59 -03002353 -1);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002354 if (ret < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -03002355 pr_err("video_register_device err %d\n", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002356 goto out;
2357 }
2358
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002359 usb_set_intfdata(intf, gspca_dev);
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002360 PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
Márton Németh0274d422010-01-28 06:39:49 -03002361
Hans de Goede10ee2402010-02-19 04:41:40 -03002362 gspca_input_create_urb(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002363
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002364 return 0;
2365out:
Randy Dunlapf7628012010-03-30 14:32:04 -03002366#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Hans de Goede10ee2402010-02-19 04:41:40 -03002367 if (gspca_dev->input_dev)
2368 input_unregister_device(gspca_dev->input_dev);
Hans de Goedee7637522010-02-19 05:11:09 -03002369#endif
Jean-Francois Moinee07a1d82008-11-19 06:37:53 -03002370 kfree(gspca_dev->usb_buf);
2371 kfree(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002372 return ret;
2373}
Jean-François Moine44628642010-06-05 07:47:36 -03002374EXPORT_SYMBOL(gspca_dev_probe2);
2375
2376/* same function as the previous one, but check the interface */
2377int gspca_dev_probe(struct usb_interface *intf,
2378 const struct usb_device_id *id,
2379 const struct sd_desc *sd_desc,
2380 int dev_size,
2381 struct module *module)
2382{
2383 struct usb_device *dev = interface_to_usbdev(intf);
2384
2385 /* we don't handle multi-config cameras */
2386 if (dev->descriptor.bNumConfigurations != 1) {
Joe Perches133a9fe2011-08-21 19:56:57 -03002387 pr_err("%04x:%04x too many config\n",
2388 id->idVendor, id->idProduct);
Jean-François Moine44628642010-06-05 07:47:36 -03002389 return -ENODEV;
2390 }
2391
2392 /* the USB video interface must be the first one */
2393 if (dev->config->desc.bNumInterfaces != 1
Jean-François Moinec4dc6922010-07-14 06:26:54 -03002394 && intf->cur_altsetting->desc.bInterfaceNumber != 0)
Jean-François Moine44628642010-06-05 07:47:36 -03002395 return -ENODEV;
Jean-François Moine44628642010-06-05 07:47:36 -03002396
2397 return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
2398}
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002399EXPORT_SYMBOL(gspca_dev_probe);
2400
2401/*
2402 * USB disconnection
2403 *
2404 * This function must be called by the sub-driver
2405 * when the device disconnects, after the specific resources are freed.
2406 */
2407void gspca_disconnect(struct usb_interface *intf)
2408{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03002409 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Randy Dunlapf7628012010-03-30 14:32:04 -03002410#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -03002411 struct input_dev *input_dev;
Jean-François Moinee070bf92010-02-18 15:02:51 -03002412#endif
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002413
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002414 PDEBUG(D_PROBE, "%s disconnect",
2415 video_device_node_name(&gspca_dev->vdev));
Adam Bakerad281272009-02-04 15:33:21 -03002416 mutex_lock(&gspca_dev->usb_lock);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002417
Hans de Goede4e770f72010-12-30 13:11:21 -03002418 gspca_dev->present = 0;
2419 wake_up_interruptible(&gspca_dev->wq);
2420
2421 destroy_urbs(gspca_dev);
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002422
Randy Dunlapf7628012010-03-30 14:32:04 -03002423#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Németh0274d422010-01-28 06:39:49 -03002424 gspca_input_destroy_urb(gspca_dev);
2425 input_dev = gspca_dev->input_dev;
2426 if (input_dev) {
2427 gspca_dev->input_dev = NULL;
2428 input_unregister_device(input_dev);
2429 }
Jean-François Moinee070bf92010-02-18 15:02:51 -03002430#endif
Márton Németh0274d422010-01-28 06:39:49 -03002431
Jean-Francois Moine090264e2009-02-12 08:05:45 -03002432 /* the device is freed at exit of this function */
2433 gspca_dev->dev = NULL;
Adam Bakerad281272009-02-04 15:33:21 -03002434 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine6e4ae872008-11-22 05:35:49 -03002435
Frank Zago6b060ff2008-09-28 08:12:22 -03002436 usb_set_intfdata(intf, NULL);
2437
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002438 /* release the device */
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002439 /* (this will call gspca_release() immediately or on last close) */
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03002440 video_unregister_device(&gspca_dev->vdev);
Jean-Francois Moine3145b8c2008-10-05 04:22:10 -03002441
Jean-Francois Moine0d07e242009-10-01 13:44:49 -03002442/* PDEBUG(D_PROBE, "disconnect complete"); */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002443}
2444EXPORT_SYMBOL(gspca_disconnect);
2445
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002446#ifdef CONFIG_PM
2447int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2448{
2449 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2450
2451 if (!gspca_dev->streaming)
2452 return 0;
2453 gspca_dev->frozen = 1; /* avoid urb error messages */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002454 if (gspca_dev->sd_desc->stopN)
2455 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002456 destroy_urbs(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002457 gspca_input_destroy_urb(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002458 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002459 if (gspca_dev->sd_desc->stop0)
2460 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002461 return 0;
2462}
2463EXPORT_SYMBOL(gspca_suspend);
2464
2465int gspca_resume(struct usb_interface *intf)
2466{
2467 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2468
2469 gspca_dev->frozen = 0;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002470 gspca_dev->sd_desc->init(gspca_dev);
Márton Németh0274d422010-01-28 06:39:49 -03002471 gspca_input_create_urb(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002472 if (gspca_dev->streaming)
2473 return gspca_init_transfer(gspca_dev);
Jean-Francois Moine1e89e2da2008-09-03 17:12:15 -03002474 return 0;
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002475}
2476EXPORT_SYMBOL(gspca_resume);
2477#endif
Hans de Goededcef32372008-07-10 10:40:53 -03002478/* -- cam driver utility functions -- */
2479
2480/* auto gain and exposure algorithm based on the knee algorithm described here:
2481 http://ytse.tricolour.net/docs/LowLightOptimization.html
2482
2483 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2484 where changed. */
2485int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2486 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2487{
2488 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2489 const struct ctrl *gain_ctrl = NULL;
2490 const struct ctrl *exposure_ctrl = NULL;
2491 const struct ctrl *autogain_ctrl = NULL;
2492 int retval = 0;
2493
2494 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goede26984b02010-02-01 13:18:37 -03002495 if (gspca_dev->ctrl_dis & (1 << i))
2496 continue;
Hans de Goededcef32372008-07-10 10:40:53 -03002497 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2498 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2499 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2500 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2501 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2502 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2503 }
2504 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2505 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2506 "on cam without (auto)gain/exposure");
2507 return 0;
2508 }
2509
2510 if (gain_ctrl->get(gspca_dev, &gain) ||
2511 exposure_ctrl->get(gspca_dev, &exposure) ||
2512 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2513 return 0;
2514
2515 orig_gain = gain;
2516 orig_exposure = exposure;
2517
2518 /* If we are of a multiple of deadzone, do multiple steps to reach the
2519 desired lumination fast (with the risc of a slight overshoot) */
2520 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2521
Jean-Francois Moine1c44d812008-11-10 05:56:55 -03002522 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
Hans de Goededcef32372008-07-10 10:40:53 -03002523 avg_lum, desired_avg_lum, steps);
2524
2525 for (i = 0; i < steps; i++) {
2526 if (avg_lum > desired_avg_lum) {
2527 if (gain > gain_knee)
2528 gain--;
2529 else if (exposure > exposure_knee)
2530 exposure--;
2531 else if (gain > gain_ctrl->qctrl.default_value)
2532 gain--;
2533 else if (exposure > exposure_ctrl->qctrl.minimum)
2534 exposure--;
2535 else if (gain > gain_ctrl->qctrl.minimum)
2536 gain--;
2537 else
2538 break;
2539 } else {
2540 if (gain < gain_ctrl->qctrl.default_value)
2541 gain++;
2542 else if (exposure < exposure_knee)
2543 exposure++;
2544 else if (gain < gain_knee)
2545 gain++;
2546 else if (exposure < exposure_ctrl->qctrl.maximum)
2547 exposure++;
2548 else if (gain < gain_ctrl->qctrl.maximum)
2549 gain++;
2550 else
2551 break;
2552 }
2553 }
2554
2555 if (gain != orig_gain) {
2556 gain_ctrl->set(gspca_dev, gain);
2557 retval = 1;
2558 }
2559 if (exposure != orig_exposure) {
2560 exposure_ctrl->set(gspca_dev, exposure);
2561 retval = 1;
2562 }
2563
2564 return retval;
2565}
2566EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2567
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002568/* -- module insert / remove -- */
2569static int __init gspca_init(void)
2570{
Joe Perches133a9fe2011-08-21 19:56:57 -03002571 pr_info("v" DRIVER_VERSION_NUMBER " registered\n");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002572 return 0;
2573}
2574static void __exit gspca_exit(void)
2575{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002576}
2577
2578module_init(gspca_init);
2579module_exit(gspca_exit);
2580
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03002581#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002582module_param_named(debug, gspca_debug, int, 0644);
2583MODULE_PARM_DESC(debug,
2584 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-François Moinef8e56772011-05-17 04:10:32 -03002585 " 0x08:stream 0x10:frame 0x20:packet"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002586 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002587#endif