blob: e4c998965ab2c6af4bbe9ac892b3bfee0347a65d [file] [log] [blame]
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001/*
2 * Main USB camera driver
3 *
4 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#define MODULE_NAME "gspca"
22
23#include <linux/init.h>
24#include <linux/fs.h>
25#include <linux/vmalloc.h>
26#include <linux/sched.h>
27#include <linux/slab.h>
28#include <linux/mm.h>
29#include <linux/string.h>
30#include <linux/pagemap.h>
Jean-Francois Moine956e42d2008-07-01 10:03:42 -030031#include <linux/io.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030032#include <asm/page.h>
Jean-Francois Moine956e42d2008-07-01 10:03:42 -030033#include <linux/uaccess.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030034#include <linux/jiffies.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030035#include <media/v4l2-ioctl.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030036
37#include "gspca.h"
38
Jean-Francois Moined43fa322008-06-12 10:58:58 -030039/* global values */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -030040#define DEF_NURBS 2 /* default number of URBs */
Jean-Francois Moined43fa322008-06-12 10:58:58 -030041
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030042MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
43MODULE_DESCRIPTION("GSPCA USB Camera Driver");
44MODULE_LICENSE("GPL");
45
Jean-Francois Moine3371bcc2008-09-06 04:21:08 -030046#define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 3, 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030047
48static int video_nr = -1;
49
Jean-Francois Moine335b3f82008-07-30 04:53:02 -030050#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030051int gspca_debug = D_ERR | D_PROBE;
52EXPORT_SYMBOL(gspca_debug);
53
54static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
55{
56 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
57 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
58 txt,
59 pixfmt & 0xff,
60 (pixfmt >> 8) & 0xff,
61 (pixfmt >> 16) & 0xff,
62 pixfmt >> 24,
63 w, h);
64 } else {
65 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
66 txt,
67 pixfmt,
68 w, h);
69 }
70}
71#else
72#define PDEBUG_MODE(txt, pixfmt, w, h)
73#endif
74
Jean-Francois Moined43fa322008-06-12 10:58:58 -030075/* specific memory types - !! should different from V4L2_MEMORY_xxx */
76#define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
77#define GSPCA_MEMORY_READ 7
78
Jean-Francois Moined43fa322008-06-12 10:58:58 -030079#define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
Jean-Francois Moined43fa322008-06-12 10:58:58 -030080
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030081/*
82 * VMA operations.
83 */
84static void gspca_vm_open(struct vm_area_struct *vma)
85{
86 struct gspca_frame *frame = vma->vm_private_data;
87
88 frame->vma_use_count++;
89 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
90}
91
92static void gspca_vm_close(struct vm_area_struct *vma)
93{
94 struct gspca_frame *frame = vma->vm_private_data;
95
96 if (--frame->vma_use_count <= 0)
97 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
98}
99
100static struct vm_operations_struct gspca_vm_ops = {
101 .open = gspca_vm_open,
102 .close = gspca_vm_close,
103};
104
105/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300106 * fill a video frame from an URB and resubmit
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300107 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300108static void fill_frame(struct gspca_dev *gspca_dev,
109 struct urb *urb)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300110{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300111 struct gspca_frame *frame;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300112 __u8 *data; /* address of data in the iso message */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300113 int i, j, len, st;
114 cam_pkt_op pkt_scan;
115
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300116 if (urb->status != 0) {
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300117#ifdef CONFIG_PM
Jean-Francois Moine6a709742008-09-03 16:48:10 -0300118 if (!gspca_dev->frozen)
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300119#endif
Jean-Francois Moine6a709742008-09-03 16:48:10 -0300120 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300121 return; /* disconnection ? */
122 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300123 pkt_scan = gspca_dev->sd_desc->pkt_scan;
124 for (i = 0; i < urb->number_of_packets; i++) {
125
126 /* check the availability of the frame buffer */
127 j = gspca_dev->fr_i;
128 j = gspca_dev->fr_queue[j];
129 frame = &gspca_dev->frame[j];
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300130 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300131 != V4L2_BUF_FLAG_QUEUED) {
132 gspca_dev->last_packet_type = DISCARD_PACKET;
133 break;
134 }
135
136 /* check the packet status and length */
137 len = urb->iso_frame_desc[i].actual_length;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300138 if (len == 0)
139 continue;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300140 st = urb->iso_frame_desc[i].status;
141 if (st) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300142 PDEBUG(D_ERR,
143 "ISOC data error: [%d] len=%d, status=%d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300144 i, len, st);
145 gspca_dev->last_packet_type = DISCARD_PACKET;
146 continue;
147 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300148
149 /* let the packet be analyzed by the subdriver */
150 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
151 i, urb->iso_frame_desc[i].offset, len);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300152 data = (__u8 *) urb->transfer_buffer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300153 + urb->iso_frame_desc[i].offset;
154 pkt_scan(gspca_dev, frame, data, len);
155 }
156
157 /* resubmit the URB */
158 urb->status = 0;
159 st = usb_submit_urb(urb, GFP_ATOMIC);
160 if (st < 0)
161 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
162}
163
164/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300165 * ISOC message interrupt from the USB device
166 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300167 * Analyse each packet and call the subdriver for copy to the frame buffer.
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300168 */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300169static void isoc_irq(struct urb *urb
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300170)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300171{
172 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
173
Hans de Goede3647fea2008-07-15 05:36:30 -0300174 PDEBUG(D_PACK, "isoc irq");
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300175 if (!gspca_dev->streaming)
176 return;
177 fill_frame(gspca_dev, urb);
178}
179
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300180/*
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300181 * bulk message interrupt from the USB device
182 */
183static void bulk_irq(struct urb *urb
184)
185{
186 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
187 struct gspca_frame *frame;
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300188 int j;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300189
190 PDEBUG(D_PACK, "bulk irq");
191 if (!gspca_dev->streaming)
192 return;
193 if (urb->status != 0 && urb->status != -ECONNRESET) {
194#ifdef CONFIG_PM
195 if (!gspca_dev->frozen)
196#endif
197 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
198 return; /* disconnection ? */
199 }
200
201 /* check the availability of the frame buffer */
202 j = gspca_dev->fr_i;
203 j = gspca_dev->fr_queue[j];
204 frame = &gspca_dev->frame[j];
205 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
206 != V4L2_BUF_FLAG_QUEUED) {
207 gspca_dev->last_packet_type = DISCARD_PACKET;
208 } else {
209 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
210 gspca_dev->sd_desc->pkt_scan(gspca_dev,
211 frame,
212 urb->transfer_buffer,
213 urb->actual_length);
214 }
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300215}
216
217/*
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300218 * add data to the current frame
219 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300220 * This function is called by the subdrivers at interrupt level.
221 *
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300222 * To build a frame, these ones must add
223 * - one FIRST_PACKET
224 * - 0 or many INTER_PACKETs
225 * - one LAST_PACKET
226 * DISCARD_PACKET invalidates the whole frame.
227 * On LAST_PACKET, a new frame is returned.
228 */
229struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
230 int packet_type,
231 struct gspca_frame *frame,
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300232 const __u8 *data,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300233 int len)
234{
235 int i, j;
236
Hans de Goedee2997a72008-04-23 08:09:12 -0300237 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300238
239 /* when start of a new frame, if the current frame buffer
240 * is not queued, discard the whole frame */
241 if (packet_type == FIRST_PACKET) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300242 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
243 != V4L2_BUF_FLAG_QUEUED) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300244 gspca_dev->last_packet_type = DISCARD_PACKET;
245 return frame;
246 }
247 frame->data_end = frame->data;
248 jiffies_to_timeval(get_jiffies_64(),
249 &frame->v4l2_buf.timestamp);
250 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300251 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
Jean-Francois Moine06ca78f2008-07-22 03:45:08 -0300252 if (packet_type == LAST_PACKET)
253 gspca_dev->last_packet_type = packet_type;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300254 return frame;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300255 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300256
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300257 /* append the packet to the frame buffer */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300258 if (len > 0) {
259 if (frame->data_end - frame->data + len
260 > frame->v4l2_buf.length) {
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -0300261 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300262 frame->data_end - frame->data + len,
263 frame->v4l2_buf.length);
264 packet_type = DISCARD_PACKET;
265 } else {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300266 memcpy(frame->data_end, data, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300267 frame->data_end += len;
268 }
269 }
270 gspca_dev->last_packet_type = packet_type;
271
272 /* if last packet, wake the application and advance in the queue */
273 if (packet_type == LAST_PACKET) {
274 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
275 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
276 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
277 atomic_inc(&gspca_dev->nevent);
278 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300279 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
280 gspca_dev->fr_i = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300281 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
282 frame->v4l2_buf.bytesused,
283 gspca_dev->fr_q,
284 i,
285 gspca_dev->fr_o);
286 j = gspca_dev->fr_queue[i];
287 frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300288 }
289 return frame;
290}
291EXPORT_SYMBOL(gspca_frame_add);
292
293static int gspca_is_compressed(__u32 format)
294{
295 switch (format) {
296 case V4L2_PIX_FMT_MJPEG:
297 case V4L2_PIX_FMT_JPEG:
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300298 case V4L2_PIX_FMT_SPCA561:
Hans de Goedeab8f12c2008-07-04 18:29:32 -0300299 case V4L2_PIX_FMT_PAC207:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300300 return 1;
301 }
302 return 0;
303}
304
305static void *rvmalloc(unsigned long size)
306{
307 void *mem;
308 unsigned long adr;
309
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300310/* size = PAGE_ALIGN(size); (already done) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300311 mem = vmalloc_32(size);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300312 if (mem != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300313 adr = (unsigned long) mem;
314 while ((long) size > 0) {
315 SetPageReserved(vmalloc_to_page((void *) adr));
316 adr += PAGE_SIZE;
317 size -= PAGE_SIZE;
318 }
319 }
320 return mem;
321}
322
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300323static void rvfree(void *mem, long size)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300324{
325 unsigned long adr;
326
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300327 adr = (unsigned long) mem;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300328 while (size > 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300329 ClearPageReserved(vmalloc_to_page((void *) adr));
330 adr += PAGE_SIZE;
331 size -= PAGE_SIZE;
332 }
333 vfree(mem);
334}
335
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300336static int frame_alloc(struct gspca_dev *gspca_dev,
337 unsigned int count)
338{
339 struct gspca_frame *frame;
340 unsigned int frsz;
341 int i;
342
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300343 i = gspca_dev->curr_mode;
344 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300345 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300346 frsz = PAGE_ALIGN(frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300347 gspca_dev->frsz = frsz;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300348 if (count > GSPCA_MAX_FRAMES)
349 count = GSPCA_MAX_FRAMES;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300350 gspca_dev->frbuf = rvmalloc(frsz * count);
351 if (!gspca_dev->frbuf) {
352 err("frame alloc failed");
353 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300354 }
355 gspca_dev->nframes = count;
356 for (i = 0; i < count; i++) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300357 frame = &gspca_dev->frame[i];
358 frame->v4l2_buf.index = i;
359 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
360 frame->v4l2_buf.flags = 0;
361 frame->v4l2_buf.field = V4L2_FIELD_NONE;
362 frame->v4l2_buf.length = frsz;
363 frame->v4l2_buf.memory = gspca_dev->memory;
364 frame->v4l2_buf.sequence = 0;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300365 frame->data = frame->data_end =
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300366 gspca_dev->frbuf + i * frsz;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300367 frame->v4l2_buf.m.offset = i * frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300368 }
369 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
370 gspca_dev->last_packet_type = DISCARD_PACKET;
371 gspca_dev->sequence = 0;
372 atomic_set(&gspca_dev->nevent, 0);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300373 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300374}
375
376static void frame_free(struct gspca_dev *gspca_dev)
377{
378 int i;
379
380 PDEBUG(D_STREAM, "frame free");
Harvey Harrisonefab8212008-07-05 06:12:47 -0300381 if (gspca_dev->frbuf != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300382 rvfree(gspca_dev->frbuf,
383 gspca_dev->nframes * gspca_dev->frsz);
384 gspca_dev->frbuf = NULL;
385 for (i = 0; i < gspca_dev->nframes; i++)
386 gspca_dev->frame[i].data = NULL;
387 }
388 gspca_dev->nframes = 0;
389}
390
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300391static void destroy_urbs(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300392{
393 struct urb *urb;
394 unsigned int i;
395
396 PDEBUG(D_STREAM, "kill transfer");
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300397 for (i = 0; i < MAX_NURBS; ++i) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300398 urb = gspca_dev->urb[i];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300399 if (urb == NULL)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300400 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300401
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300402 gspca_dev->urb[i] = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300403 usb_kill_urb(urb);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300404 if (urb->transfer_buffer != NULL)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300405 usb_buffer_free(gspca_dev->dev,
406 urb->transfer_buffer_length,
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300407 urb->transfer_buffer,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300408 urb->transfer_dma);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300409 usb_free_urb(urb);
410 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300411}
412
413/*
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300414 * search an input transfer endpoint in an alternate setting
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300415 */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300416static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
417 __u8 epaddr,
418 __u8 xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300419{
420 struct usb_host_endpoint *ep;
421 int i, attr;
422
423 epaddr |= USB_DIR_IN;
424 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
425 ep = &alt->endpoint[i];
426 if (ep->desc.bEndpointAddress == epaddr) {
427 attr = ep->desc.bmAttributes
428 & USB_ENDPOINT_XFERTYPE_MASK;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300429 if (attr == xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300430 return ep;
431 break;
432 }
433 }
434 return NULL;
435}
436
437/*
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300438 * search an input (isoc or bulk) endpoint
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300439 *
440 * The endpoint is defined by the subdriver.
441 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
442 * This routine may be called many times when the bandwidth is too small
443 * (the bandwidth is checked on urb submit).
444 */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300445static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300446{
447 struct usb_interface *intf;
448 struct usb_host_endpoint *ep;
449 int i, ret;
450
451 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300452 ep = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300453 i = gspca_dev->alt; /* previous alt setting */
454 while (--i > 0) { /* alt 0 is unusable */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300455 ep = alt_xfer(&intf->altsetting[i],
456 gspca_dev->cam.epaddr,
457 gspca_dev->bulk
458 ? USB_ENDPOINT_XFER_BULK
459 : USB_ENDPOINT_XFER_ISOC);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300460 if (ep)
461 break;
462 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300463 if (ep == NULL) {
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300464 err("no transfer endpoint found");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300465 return NULL;
466 }
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300467 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300468 i, ep->desc.bEndpointAddress);
469 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
470 if (ret < 0) {
471 err("set interface err %d", ret);
472 return NULL;
473 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300474 gspca_dev->alt = i; /* memorize the current alt setting */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300475 return ep;
476}
477
478/*
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300479 * create the URBs for image transfer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300480 */
481static int create_urbs(struct gspca_dev *gspca_dev,
482 struct usb_host_endpoint *ep)
483{
484 struct urb *urb;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300485 int n, nurbs, i, psize, npkt, bsize;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300486
487 /* calculate the packet size and the number of packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300488 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300489
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300490 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
491 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300492 if (!gspca_dev->bulk) {
493 npkt = ISO_MAX_SIZE / psize;
494 if (npkt > ISO_MAX_PKT)
495 npkt = ISO_MAX_PKT;
496 bsize = psize * npkt;
497 PDEBUG(D_STREAM,
498 "isoc %d pkts size %d = bsize:%d",
499 npkt, psize, bsize);
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300500 nurbs = DEF_NURBS;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300501 } else {
502 npkt = 0;
503 bsize = psize;
504 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300505 nurbs = 1;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300506 }
507
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300508 gspca_dev->nurbs = nurbs;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300509 for (n = 0; n < nurbs; n++) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300510 urb = usb_alloc_urb(npkt, GFP_KERNEL);
511 if (!urb) {
512 err("usb_alloc_urb failed");
Jean-Francois Moine0d0548e2008-09-05 12:49:54 -0300513 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300514 return -ENOMEM;
515 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300516 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300517 bsize,
518 GFP_KERNEL,
519 &urb->transfer_dma);
520
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300521 if (urb->transfer_buffer == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300522 usb_free_urb(urb);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300523 err("usb_buffer_urb failed");
Jean-Francois Moine0d0548e2008-09-05 12:49:54 -0300524 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300525 return -ENOMEM;
526 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300527 gspca_dev->urb[n] = urb;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300528 urb->dev = gspca_dev->dev;
529 urb->context = gspca_dev;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300530 urb->transfer_buffer_length = bsize;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300531 if (npkt != 0) { /* ISOC */
532 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
533 ep->desc.bEndpointAddress);
534 urb->transfer_flags = URB_ISO_ASAP
535 | URB_NO_TRANSFER_DMA_MAP;
536 urb->interval = ep->desc.bInterval;
537 urb->complete = isoc_irq;
538 urb->number_of_packets = npkt;
539 for (i = 0; i < npkt; i++) {
540 urb->iso_frame_desc[i].length = psize;
541 urb->iso_frame_desc[i].offset = psize * i;
542 }
543 } else { /* bulk */
544 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
545 ep->desc.bEndpointAddress),
546 urb->complete = bulk_irq;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300547 }
548 }
549 return 0;
550}
551
552/*
553 * start the USB transfer
554 */
555static int gspca_init_transfer(struct gspca_dev *gspca_dev)
556{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300557 struct usb_host_endpoint *ep;
558 int n, ret;
559
Hans de Goedee2997a72008-04-23 08:09:12 -0300560 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
561 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300562
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300563 /* set the higher alternate setting and
564 * loop until urb submit succeeds */
565 gspca_dev->alt = gspca_dev->nbalt;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300566 for (;;) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300567 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300568 ep = get_ep(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300569 if (ep == NULL) {
570 ret = -EIO;
571 goto out;
572 }
573 ret = create_urbs(gspca_dev, ep);
574 if (ret < 0)
575 goto out;
576
577 /* start the cam */
578 gspca_dev->sd_desc->start(gspca_dev);
579 gspca_dev->streaming = 1;
580 atomic_set(&gspca_dev->nevent, 0);
581
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300582 /* start the bulk transfer is done by the subdriver */
583 if (gspca_dev->bulk)
584 break;
585
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300586 /* submit the URBs */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300587 for (n = 0; n < gspca_dev->nurbs; n++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300588 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300589 if (ret < 0) {
590 PDEBUG(D_ERR|D_STREAM,
591 "usb_submit_urb [%d] err %d", n, ret);
Hans de Goedee2997a72008-04-23 08:09:12 -0300592 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300593 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300594 if (ret == -ENOSPC)
595 break; /* try the previous alt */
596 goto out;
597 }
598 }
599 if (ret >= 0)
600 break;
601 }
602out:
603 mutex_unlock(&gspca_dev->usb_lock);
604 return ret;
605}
606
607static int gspca_set_alt0(struct gspca_dev *gspca_dev)
608{
609 int ret;
610
611 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
612 if (ret < 0)
613 PDEBUG(D_ERR|D_STREAM, "set interface 0 err %d", ret);
614 return ret;
615}
616
Hans de Goedee2997a72008-04-23 08:09:12 -0300617/* Note both the queue and the usb lock should be hold when calling this */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300618static void gspca_stream_off(struct gspca_dev *gspca_dev)
619{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300620 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300621 atomic_set(&gspca_dev->nevent, 0);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300622 if (gspca_dev->present) {
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300623 if (gspca_dev->sd_desc->stopN)
624 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300625 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300626 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300627 if (gspca_dev->sd_desc->stop0)
628 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300629 PDEBUG(D_STREAM, "stream off OK");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300630 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300631}
632
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300633static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300634{
635 int i;
636
637 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
638 gspca_dev->curr_mode = i;
639 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
640 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300641 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300642}
643
644static int wxh_to_mode(struct gspca_dev *gspca_dev,
645 int width, int height)
646{
647 int i;
648
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300649 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
650 if (width >= gspca_dev->cam.cam_mode[i].width
651 && height >= gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300652 break;
653 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300654 return i;
655}
656
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300657/*
658 * search a mode with the right pixel format
659 */
660static int gspca_get_mode(struct gspca_dev *gspca_dev,
661 int mode,
662 int pixfmt)
663{
664 int modeU, modeD;
665
666 modeU = modeD = mode;
667 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
668 if (--modeD >= 0) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300669 if (gspca_dev->cam.cam_mode[modeD].pixelformat
670 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300671 return modeD;
672 }
673 if (++modeU < gspca_dev->cam.nmodes) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300674 if (gspca_dev->cam.cam_mode[modeU].pixelformat
675 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300676 return modeU;
677 }
678 }
679 return -EINVAL;
680}
681
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300682static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300683 struct v4l2_fmtdesc *fmtdesc)
684{
685 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300686 int i, j, index;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300687 __u32 fmt_tb[8];
688
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300689 /* give an index to each format */
690 index = 0;
691 j = 0;
692 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300693 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300694 j = 0;
695 for (;;) {
696 if (fmt_tb[j] == fmt_tb[index])
697 break;
698 j++;
699 }
700 if (j == index) {
701 if (fmtdesc->index == index)
702 break; /* new format */
703 index++;
704 if (index >= sizeof fmt_tb / sizeof fmt_tb[0])
705 return -EINVAL;
706 }
707 }
708 if (i < 0)
709 return -EINVAL; /* no more format */
710
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300711 fmtdesc->pixelformat = fmt_tb[index];
712 if (gspca_is_compressed(fmt_tb[index]))
713 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300714 fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300715 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
716 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
717 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
718 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
719 fmtdesc->description[4] = '\0';
720 return 0;
721}
722
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300723static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300724 struct v4l2_format *fmt)
725{
726 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300727 int mode;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300728
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300729 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
730 return -EINVAL;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300731 mode = gspca_dev->curr_mode;
732 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
733 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300734 return 0;
735}
736
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300737static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300738 struct v4l2_format *fmt)
739{
Hans de Goede24c530b2008-07-04 17:55:18 -0300740 int w, h, mode, mode2;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300741
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300742 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
743 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300744 w = fmt->fmt.pix.width;
745 h = fmt->fmt.pix.height;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300746
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300747#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300748 if (gspca_debug & D_CONF)
749 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
750#endif
751 /* search the closest mode for width and height */
752 mode = wxh_to_mode(gspca_dev, w, h);
753
754 /* OK if right palette */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300755 if (gspca_dev->cam.cam_mode[mode].pixelformat
756 != fmt->fmt.pix.pixelformat) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300757
758 /* else, search the closest mode with the same pixel format */
759 mode2 = gspca_get_mode(gspca_dev, mode,
760 fmt->fmt.pix.pixelformat);
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300761 if (mode2 >= 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300762 mode = mode2;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300763/* else
764 ; * no chance, return this mode */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300765 }
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300766 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
767 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300768 return mode; /* used when s_fmt */
769}
770
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300771static int vidioc_try_fmt_vid_cap(struct file *file,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300772 void *priv,
773 struct v4l2_format *fmt)
774{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300775 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300776 int ret;
777
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300778 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300779 if (ret < 0)
780 return ret;
781 return 0;
782}
783
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300784static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300785 struct v4l2_format *fmt)
786{
787 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300788 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300789
Hans de Goedee2997a72008-04-23 08:09:12 -0300790 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
791 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300792
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300793 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300794 if (ret < 0)
795 goto out;
796
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300797 if (gspca_dev->nframes != 0
798 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
799 ret = -EINVAL;
800 goto out;
801 }
802
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300803 if (ret == gspca_dev->curr_mode) {
804 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300805 goto out; /* same mode */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300806 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300807
808 if (gspca_dev->streaming) {
809 ret = -EBUSY;
810 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -0300811 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300812 gspca_dev->width = fmt->fmt.pix.width;
813 gspca_dev->height = fmt->fmt.pix.height;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300814 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
815 gspca_dev->curr_mode = ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300816
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300817 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300818out:
819 mutex_unlock(&gspca_dev->queue_lock);
820 return ret;
821}
822
823static int dev_open(struct inode *inode, struct file *file)
824{
825 struct gspca_dev *gspca_dev;
826 int ret;
827
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300828 PDEBUG(D_STREAM, "%s open", current->comm);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300829 gspca_dev = (struct gspca_dev *) video_devdata(file);
Hans de Goedee2997a72008-04-23 08:09:12 -0300830 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
831 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300832 if (!gspca_dev->present) {
833 ret = -ENODEV;
834 goto out;
835 }
836
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300837 if (gspca_dev->users > 4) { /* (arbitrary value) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300838 ret = -EBUSY;
839 goto out;
840 }
841 gspca_dev->users++;
842 file->private_data = gspca_dev;
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300843#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300844 /* activate the v4l2 debug */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300845 if (gspca_debug & D_V4L2)
Frank Zago9de436c2008-09-15 05:20:38 -0300846 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
847 | V4L2_DEBUG_IOCTL_ARG;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300848 else
Frank Zago9de436c2008-09-15 05:20:38 -0300849 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
850 | V4L2_DEBUG_IOCTL_ARG);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300851#endif
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300852 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300853out:
854 mutex_unlock(&gspca_dev->queue_lock);
855 if (ret != 0)
856 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
857 else
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300858 PDEBUG(D_STREAM, "open done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300859 return ret;
860}
861
862static int dev_close(struct inode *inode, struct file *file)
863{
864 struct gspca_dev *gspca_dev = file->private_data;
865
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300866 PDEBUG(D_STREAM, "%s close", current->comm);
Hans de Goedee2997a72008-04-23 08:09:12 -0300867 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
868 return -ERESTARTSYS;
869 gspca_dev->users--;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300870
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -0300871 /* if the file did the capture, free the streaming resources */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300872 if (gspca_dev->capt_file == file) {
Jean-Francois Moinea1e82882008-09-03 17:12:17 -0300873 if (gspca_dev->streaming) {
874 mutex_lock(&gspca_dev->usb_lock);
875 gspca_stream_off(gspca_dev);
876 mutex_unlock(&gspca_dev->usb_lock);
877 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300878 frame_free(gspca_dev);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300879 gspca_dev->capt_file = NULL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300880 gspca_dev->memory = GSPCA_MEMORY_NO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300881 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300882 file->private_data = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300883 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300884 PDEBUG(D_STREAM, "close done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300885 return 0;
886}
887
888static int vidioc_querycap(struct file *file, void *priv,
889 struct v4l2_capability *cap)
890{
891 struct gspca_dev *gspca_dev = priv;
892
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300893 memset(cap, 0, sizeof *cap);
894 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -0300895/* strncpy(cap->card, gspca_dev->cam.dev_name, sizeof cap->card); */
896 if (gspca_dev->dev->product != NULL) {
897 strncpy(cap->card, gspca_dev->dev->product,
898 sizeof cap->card);
899 } else {
900 snprintf(cap->card, sizeof cap->card,
901 "USB Camera (%04x:%04x)",
902 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
903 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
904 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300905 strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name,
906 sizeof cap->bus_info);
907 cap->version = DRIVER_VERSION_NUMBER;
908 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
909 | V4L2_CAP_STREAMING
910 | V4L2_CAP_READWRITE;
911 return 0;
912}
913
914static int vidioc_queryctrl(struct file *file, void *priv,
915 struct v4l2_queryctrl *q_ctrl)
916{
917 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300918 int i, ix;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300919 u32 id;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300920
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300921 ix = -1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300922 id = q_ctrl->id;
923 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
924 id &= V4L2_CTRL_ID_MASK;
925 id++;
926 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goede0f523c22008-09-03 17:12:14 -0300927 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300928 continue;
929 if (ix < 0) {
930 ix = i;
931 continue;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300932 }
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300933 if (gspca_dev->sd_desc->ctrls[i].qctrl.id
934 > gspca_dev->sd_desc->ctrls[ix].qctrl.id)
935 continue;
936 ix = i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300937 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300938 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300939 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300940 if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) {
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300941 ix = i;
942 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300943 }
944 }
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300945 if (ix < 0)
946 return -EINVAL;
947 memcpy(q_ctrl, &gspca_dev->sd_desc->ctrls[ix].qctrl,
948 sizeof *q_ctrl);
949 if (gspca_dev->ctrl_dis & (1 << ix))
950 q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
951 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300952}
953
954static int vidioc_s_ctrl(struct file *file, void *priv,
955 struct v4l2_control *ctrl)
956{
957 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300958 const struct ctrl *ctrls;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300959 int i, ret;
960
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300961 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
962 i < gspca_dev->sd_desc->nctrls;
963 i++, ctrls++) {
964 if (ctrl->id != ctrls->qctrl.id)
965 continue;
Hans de Goede0f523c22008-09-03 17:12:14 -0300966 if (gspca_dev->ctrl_dis & (1 << i))
967 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300968 if (ctrl->value < ctrls->qctrl.minimum
Rabin Vincentfcf5cb22008-08-03 07:58:54 -0300969 || ctrl->value > ctrls->qctrl.maximum)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300970 return -ERANGE;
971 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
Hans de Goedee2997a72008-04-23 08:09:12 -0300972 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
973 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300974 ret = ctrls->set(gspca_dev, ctrl->value);
975 mutex_unlock(&gspca_dev->usb_lock);
976 return ret;
977 }
978 return -EINVAL;
979}
980
981static int vidioc_g_ctrl(struct file *file, void *priv,
982 struct v4l2_control *ctrl)
983{
984 struct gspca_dev *gspca_dev = priv;
985
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300986 const struct ctrl *ctrls;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300987 int i, ret;
988
989 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
990 i < gspca_dev->sd_desc->nctrls;
991 i++, ctrls++) {
992 if (ctrl->id != ctrls->qctrl.id)
993 continue;
Hans de Goede0f523c22008-09-03 17:12:14 -0300994 if (gspca_dev->ctrl_dis & (1 << i))
995 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -0300996 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
997 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300998 ret = ctrls->get(gspca_dev, &ctrl->value);
999 mutex_unlock(&gspca_dev->usb_lock);
1000 return ret;
1001 }
1002 return -EINVAL;
1003}
1004
1005static int vidioc_querymenu(struct file *file, void *priv,
1006 struct v4l2_querymenu *qmenu)
1007{
1008 struct gspca_dev *gspca_dev = priv;
1009
1010 if (!gspca_dev->sd_desc->querymenu)
1011 return -EINVAL;
1012 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1013}
1014
1015static int vidioc_enum_input(struct file *file, void *priv,
1016 struct v4l2_input *input)
1017{
1018 struct gspca_dev *gspca_dev = priv;
1019
1020 if (input->index != 0)
1021 return -EINVAL;
1022 memset(input, 0, sizeof *input);
1023 input->type = V4L2_INPUT_TYPE_CAMERA;
1024 strncpy(input->name, gspca_dev->sd_desc->name,
1025 sizeof input->name);
1026 return 0;
1027}
1028
1029static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1030{
1031 *i = 0;
1032 return 0;
1033}
1034
1035static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1036{
1037 if (i > 0)
1038 return -EINVAL;
1039 return (0);
1040}
1041
1042static int vidioc_reqbufs(struct file *file, void *priv,
1043 struct v4l2_requestbuffers *rb)
1044{
1045 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001046 int i, ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001047
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001048 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1049 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001050 switch (rb->memory) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001051 case GSPCA_MEMORY_READ: /* (internal call) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001052 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001053 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001054 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001055 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001056 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001057 }
Hans de Goedee2997a72008-04-23 08:09:12 -03001058 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1059 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001060
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001061 if (gspca_dev->memory != GSPCA_MEMORY_NO
1062 && gspca_dev->memory != rb->memory) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001063 ret = -EBUSY;
1064 goto out;
1065 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001066
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001067 /* only one file may do the capture */
1068 if (gspca_dev->capt_file != NULL
1069 && gspca_dev->capt_file != file) {
1070 ret = -EBUSY;
1071 goto out;
1072 }
1073
1074 /* if allocated, the buffers must not be mapped */
1075 for (i = 0; i < gspca_dev->nframes; i++) {
1076 if (gspca_dev->frame[i].vma_use_count) {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001077 ret = -EBUSY;
1078 goto out;
1079 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001080 }
1081
1082 /* stop streaming */
1083 if (gspca_dev->streaming) {
1084 mutex_lock(&gspca_dev->usb_lock);
1085 gspca_stream_off(gspca_dev);
1086 mutex_unlock(&gspca_dev->usb_lock);
1087 }
1088
1089 /* free the previous allocated buffers, if any */
1090 if (gspca_dev->nframes != 0) {
1091 frame_free(gspca_dev);
1092 gspca_dev->capt_file = NULL;
1093 }
1094 if (rb->count == 0) /* unrequest */
1095 goto out;
1096 gspca_dev->memory = rb->memory;
1097 ret = frame_alloc(gspca_dev, rb->count);
1098 if (ret == 0) {
1099 rb->count = gspca_dev->nframes;
1100 gspca_dev->capt_file = file;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001101 }
1102out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001103 mutex_unlock(&gspca_dev->queue_lock);
1104 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1105 return ret;
1106}
1107
1108static int vidioc_querybuf(struct file *file, void *priv,
1109 struct v4l2_buffer *v4l2_buf)
1110{
1111 struct gspca_dev *gspca_dev = priv;
1112 struct gspca_frame *frame;
1113
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001114 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1115 || v4l2_buf->index < 0
1116 || v4l2_buf->index >= gspca_dev->nframes)
1117 return -EINVAL;
1118
1119 frame = &gspca_dev->frame[v4l2_buf->index];
1120 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1121 return 0;
1122}
1123
1124static int vidioc_streamon(struct file *file, void *priv,
1125 enum v4l2_buf_type buf_type)
1126{
1127 struct gspca_dev *gspca_dev = priv;
1128 int ret;
1129
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001130 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1131 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001132 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1133 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001134 if (!gspca_dev->present) {
1135 ret = -ENODEV;
1136 goto out;
1137 }
1138 if (gspca_dev->nframes == 0) {
1139 ret = -EINVAL;
1140 goto out;
1141 }
1142 if (!gspca_dev->streaming) {
1143 ret = gspca_init_transfer(gspca_dev);
1144 if (ret < 0)
1145 goto out;
1146 }
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001147#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001148 if (gspca_debug & D_STREAM) {
1149 PDEBUG_MODE("stream on OK",
1150 gspca_dev->pixfmt,
1151 gspca_dev->width,
1152 gspca_dev->height);
1153 }
1154#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001155 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001156out:
1157 mutex_unlock(&gspca_dev->queue_lock);
1158 return ret;
1159}
1160
1161static int vidioc_streamoff(struct file *file, void *priv,
1162 enum v4l2_buf_type buf_type)
1163{
1164 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001165 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001166
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001167 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1168 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001169 if (!gspca_dev->streaming)
1170 return 0;
1171 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1172 return -ERESTARTSYS;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001173
1174 /* stop streaming */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001175 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1176 ret = -ERESTARTSYS;
1177 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001178 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001179 gspca_stream_off(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001180 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001181
1182 /* empty the application queues */
1183 for (i = 0; i < gspca_dev->nframes; i++)
1184 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1185 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1186 gspca_dev->last_packet_type = DISCARD_PACKET;
1187 gspca_dev->sequence = 0;
1188 atomic_set(&gspca_dev->nevent, 0);
1189 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001190out:
1191 mutex_unlock(&gspca_dev->queue_lock);
1192 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001193}
1194
1195static int vidioc_g_jpegcomp(struct file *file, void *priv,
1196 struct v4l2_jpegcompression *jpegcomp)
1197{
1198 struct gspca_dev *gspca_dev = priv;
1199 int ret;
1200
1201 if (!gspca_dev->sd_desc->get_jcomp)
1202 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001203 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1204 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001205 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1206 mutex_unlock(&gspca_dev->usb_lock);
1207 return ret;
1208}
1209
1210static int vidioc_s_jpegcomp(struct file *file, void *priv,
1211 struct v4l2_jpegcompression *jpegcomp)
1212{
1213 struct gspca_dev *gspca_dev = priv;
1214 int ret;
1215
Hans de Goedee2997a72008-04-23 08:09:12 -03001216 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1217 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001218 if (!gspca_dev->sd_desc->set_jcomp)
1219 return -EINVAL;
1220 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1221 mutex_unlock(&gspca_dev->usb_lock);
1222 return ret;
1223}
1224
1225static int vidioc_g_parm(struct file *filp, void *priv,
1226 struct v4l2_streamparm *parm)
1227{
1228 struct gspca_dev *gspca_dev = priv;
1229
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001230 memset(parm, 0, sizeof *parm);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001231 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1232 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1233 return 0;
1234}
1235
1236static int vidioc_s_parm(struct file *filp, void *priv,
1237 struct v4l2_streamparm *parm)
1238{
1239 struct gspca_dev *gspca_dev = priv;
1240 int n;
1241
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001242 n = parm->parm.capture.readbuffers;
1243 if (n == 0 || n > GSPCA_MAX_FRAMES)
1244 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1245 else
1246 gspca_dev->nbufread = n;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001247 return 0;
1248}
1249
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001250static int vidioc_s_std(struct file *filp, void *priv,
1251 v4l2_std_id *parm)
1252{
1253 return 0;
1254}
1255
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001256#ifdef CONFIG_VIDEO_V4L1_COMPAT
1257static int vidiocgmbuf(struct file *file, void *priv,
1258 struct video_mbuf *mbuf)
1259{
1260 struct gspca_dev *gspca_dev = file->private_data;
1261 int i;
1262
1263 PDEBUG(D_STREAM, "cgmbuf");
1264 if (gspca_dev->nframes == 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001265 int ret;
1266
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001267 {
1268 struct v4l2_format fmt;
1269
1270 memset(&fmt, 0, sizeof fmt);
1271 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1272 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1273 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1274 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1275 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001276 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001277 if (ret != 0)
1278 return ret;
1279 }
1280 {
1281 struct v4l2_requestbuffers rb;
1282
1283 memset(&rb, 0, sizeof rb);
1284 rb.count = 4;
1285 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1286 rb.memory = V4L2_MEMORY_MMAP;
1287 ret = vidioc_reqbufs(file, priv, &rb);
1288 if (ret != 0)
1289 return ret;
1290 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001291 }
1292 mbuf->frames = gspca_dev->nframes;
1293 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1294 for (i = 0; i < mbuf->frames; i++)
1295 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1296 return 0;
1297}
1298#endif
1299
1300static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1301{
1302 struct gspca_dev *gspca_dev = file->private_data;
Harvey Harrisonefab8212008-07-05 06:12:47 -03001303 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001304 struct page *page;
1305 unsigned long addr, start, size;
1306 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001307
1308 start = vma->vm_start;
1309 size = vma->vm_end - vma->vm_start;
1310 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1311
Hans de Goedee2997a72008-04-23 08:09:12 -03001312 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1313 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001314 if (!gspca_dev->present) {
1315 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001316 goto out;
1317 }
1318 if (gspca_dev->capt_file != file) {
1319 ret = -EINVAL;
1320 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001321 }
1322
Harvey Harrisonefab8212008-07-05 06:12:47 -03001323 frame = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001324 for (i = 0; i < gspca_dev->nframes; ++i) {
1325 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1326 PDEBUG(D_STREAM, "mmap bad memory type");
1327 break;
1328 }
1329 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1330 == vma->vm_pgoff) {
1331 frame = &gspca_dev->frame[i];
1332 break;
1333 }
1334 }
Harvey Harrisonefab8212008-07-05 06:12:47 -03001335 if (frame == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001336 PDEBUG(D_STREAM, "mmap no frame buffer found");
1337 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001338 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001339 }
1340#ifdef CONFIG_VIDEO_V4L1_COMPAT
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001341 /* v4l1 maps all the buffers */
1342 if (i != 0
1343 || size != frame->v4l2_buf.length * gspca_dev->nframes)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001344#endif
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001345 if (size != frame->v4l2_buf.length) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001346 PDEBUG(D_STREAM, "mmap bad size");
1347 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001348 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001349 }
1350
1351 /*
1352 * - VM_IO marks the area as being a mmaped region for I/O to a
1353 * device. It also prevents the region from being core dumped.
1354 */
1355 vma->vm_flags |= VM_IO;
1356
1357 addr = (unsigned long) frame->data;
1358 while (size > 0) {
1359 page = vmalloc_to_page((void *) addr);
1360 ret = vm_insert_page(vma, start, page);
1361 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001362 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001363 start += PAGE_SIZE;
1364 addr += PAGE_SIZE;
1365 size -= PAGE_SIZE;
1366 }
1367
1368 vma->vm_ops = &gspca_vm_ops;
1369 vma->vm_private_data = frame;
1370 gspca_vm_open(vma);
Hans de Goedee2997a72008-04-23 08:09:12 -03001371 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001372out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001373 mutex_unlock(&gspca_dev->queue_lock);
1374 return ret;
1375}
1376
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001377/*
1378 * wait for a video frame
1379 *
1380 * If a frame is ready, its index is returned.
1381 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001382static int frame_wait(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001383 int nonblock_ing)
1384{
1385 struct gspca_frame *frame;
1386 int i, j, ret;
1387
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001388 /* check if a frame is ready */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001389 i = gspca_dev->fr_o;
1390 j = gspca_dev->fr_queue[i];
1391 frame = &gspca_dev->frame[j];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001392 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) {
1393 atomic_dec(&gspca_dev->nevent);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001394 goto ok;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001395 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001396 if (nonblock_ing) /* no frame yet */
1397 return -EAGAIN;
1398
1399 /* wait till a frame is ready */
1400 for (;;) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001401 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1402 atomic_read(&gspca_dev->nevent) > 0,
1403 msecs_to_jiffies(3000));
1404 if (ret <= 0) {
1405 if (ret < 0)
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001406 return ret; /* interrupt */
1407 return -EIO; /* timeout */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001408 }
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001409 atomic_dec(&gspca_dev->nevent);
Hans de Goedee2997a72008-04-23 08:09:12 -03001410 if (!gspca_dev->streaming || !gspca_dev->present)
1411 return -EIO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001412 i = gspca_dev->fr_o;
1413 j = gspca_dev->fr_queue[i];
1414 frame = &gspca_dev->frame[j];
1415 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1416 break;
1417 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001418ok:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001419 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1420 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1421 gspca_dev->fr_q,
1422 gspca_dev->fr_i,
1423 gspca_dev->fr_o);
Hans de Goedee2997a72008-04-23 08:09:12 -03001424
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001425 if (gspca_dev->sd_desc->dq_callback) {
1426 mutex_lock(&gspca_dev->usb_lock);
Hans de Goedee2997a72008-04-23 08:09:12 -03001427 gspca_dev->sd_desc->dq_callback(gspca_dev);
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001428 mutex_unlock(&gspca_dev->usb_lock);
1429 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001430 return j;
1431}
1432
1433/*
1434 * dequeue a video buffer
1435 *
1436 * If nonblock_ing is false, block until a buffer is available.
1437 */
1438static int vidioc_dqbuf(struct file *file, void *priv,
1439 struct v4l2_buffer *v4l2_buf)
1440{
1441 struct gspca_dev *gspca_dev = priv;
1442 struct gspca_frame *frame;
1443 int i, ret;
1444
1445 PDEBUG(D_FRAM, "dqbuf");
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001446 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1447 return -EINVAL;
1448 if (v4l2_buf->memory != gspca_dev->memory)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001449 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001450
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001451 /* if not streaming, be sure the application will not loop forever */
1452 if (!(file->f_flags & O_NONBLOCK)
1453 && !gspca_dev->streaming && gspca_dev->users == 1)
1454 return -EINVAL;
1455
1456 /* only the capturing file may dequeue */
1457 if (gspca_dev->capt_file != file)
1458 return -EINVAL;
1459
1460 /* only one dequeue / read at a time */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001461 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1462 return -ERESTARTSYS;
1463
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001464 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001465 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001466 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001467 i = ret; /* frame index */
1468 frame = &gspca_dev->frame[i];
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001469 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001470 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001471 frame->data,
1472 frame->v4l2_buf.bytesused)) {
1473 PDEBUG(D_ERR|D_STREAM,
1474 "dqbuf cp to user failed");
1475 ret = -EFAULT;
1476 goto out;
1477 }
1478 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001479 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1480 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1481 PDEBUG(D_FRAM, "dqbuf %d", i);
1482 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001483out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001484 mutex_unlock(&gspca_dev->read_lock);
1485 return ret;
1486}
1487
1488/*
1489 * queue a video buffer
1490 *
1491 * Attempting to queue a buffer that has already been
1492 * queued will return -EINVAL.
1493 */
1494static int vidioc_qbuf(struct file *file, void *priv,
1495 struct v4l2_buffer *v4l2_buf)
1496{
1497 struct gspca_dev *gspca_dev = priv;
1498 struct gspca_frame *frame;
1499 int i, index, ret;
1500
1501 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1502 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1503 return -EINVAL;
1504
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001505 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1506 return -ERESTARTSYS;
1507
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001508 index = v4l2_buf->index;
1509 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001510 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001511 "qbuf idx %d >= %d", index, gspca_dev->nframes);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001512 ret = -EINVAL;
1513 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001514 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001515 if (v4l2_buf->memory != gspca_dev->memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001516 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001517 ret = -EINVAL;
1518 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001519 }
1520
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001521 frame = &gspca_dev->frame[index];
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001522 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1523 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001524 ret = -EINVAL;
1525 goto out;
1526 }
1527
1528 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001529/* frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001530
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001531 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001532 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1533 frame->v4l2_buf.length = v4l2_buf->length;
1534 }
1535
1536 /* put the buffer in the 'queued' queue */
1537 i = gspca_dev->fr_q;
1538 gspca_dev->fr_queue[i] = index;
1539 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1540 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1541 gspca_dev->fr_q,
1542 gspca_dev->fr_i,
1543 gspca_dev->fr_o);
1544
1545 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1546 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1547 ret = 0;
1548out:
1549 mutex_unlock(&gspca_dev->queue_lock);
1550 return ret;
1551}
1552
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001553/*
1554 * allocate the resources for read()
1555 */
1556static int read_alloc(struct gspca_dev *gspca_dev,
1557 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001558{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001559 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001560 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001561
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001562 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001563 if (gspca_dev->nframes == 0) {
1564 struct v4l2_requestbuffers rb;
1565
1566 memset(&rb, 0, sizeof rb);
1567 rb.count = gspca_dev->nbufread;
1568 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001569 rb.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001570 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1571 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001572 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001573 return ret;
1574 }
1575 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1576 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001577 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001578 for (i = 0; i < gspca_dev->nbufread; i++) {
1579 v4l2_buf.index = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001580 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1581 if (ret != 0) {
1582 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1583 return ret;
1584 }
1585 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001586 gspca_dev->memory = GSPCA_MEMORY_READ;
1587 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001588
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001589 /* start streaming */
1590 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1591 if (ret != 0)
1592 PDEBUG(D_STREAM, "read streamon err %d", ret);
1593 return ret;
1594}
1595
1596static unsigned int dev_poll(struct file *file, poll_table *wait)
1597{
1598 struct gspca_dev *gspca_dev = file->private_data;
1599 int i, ret;
1600
1601 PDEBUG(D_FRAM, "poll");
1602
1603 poll_wait(file, &gspca_dev->wq, wait);
1604 if (!gspca_dev->present)
1605 return POLLERR;
1606
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001607 /* if reqbufs is not done, the user would use read() */
1608 if (gspca_dev->nframes == 0) {
1609 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1610 return POLLERR; /* not the 1st time */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001611 ret = read_alloc(gspca_dev, file);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001612 if (ret != 0)
1613 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001614 }
1615
1616 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1617 return POLLERR;
1618 if (!gspca_dev->present) {
1619 ret = POLLERR;
1620 goto out;
1621 }
1622
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001623 /* check the next incoming buffer */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001624 i = gspca_dev->fr_o;
1625 i = gspca_dev->fr_queue[i];
1626 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1627 ret = POLLIN | POLLRDNORM; /* something to read */
1628 else
1629 ret = 0;
1630out:
1631 mutex_unlock(&gspca_dev->queue_lock);
1632 return ret;
1633}
1634
1635static ssize_t dev_read(struct file *file, char __user *data,
1636 size_t count, loff_t *ppos)
1637{
1638 struct gspca_dev *gspca_dev = file->private_data;
1639 struct gspca_frame *frame;
1640 struct v4l2_buffer v4l2_buf;
1641 struct timeval timestamp;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001642 int n, ret, ret2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001643
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001644 PDEBUG(D_FRAM, "read (%zd)", count);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001645 if (!gspca_dev->present)
1646 return -ENODEV;
1647 switch (gspca_dev->memory) {
1648 case GSPCA_MEMORY_NO: /* first time */
1649 ret = read_alloc(gspca_dev, file);
1650 if (ret != 0)
1651 return ret;
1652 break;
1653 case GSPCA_MEMORY_READ:
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001654 if (gspca_dev->capt_file == file)
1655 break;
1656 /* fall thru */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001657 default:
1658 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001659 }
1660
1661 /* get a frame */
1662 jiffies_to_timeval(get_jiffies_64(), &timestamp);
1663 timestamp.tv_sec--;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001664 n = 2;
1665 for (;;) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001666 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1667 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001668 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001669 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1670 if (ret != 0) {
1671 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1672 return ret;
1673 }
1674
1675 /* if the process slept for more than 1 second,
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001676 * get anewer frame */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001677 frame = &gspca_dev->frame[v4l2_buf.index];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001678 if (--n < 0)
1679 break; /* avoid infinite loop */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001680 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1681 break;
1682 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1683 if (ret != 0) {
1684 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1685 return ret;
1686 }
1687 }
1688
1689 /* copy the frame */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001690 if (count > frame->v4l2_buf.bytesused)
1691 count = frame->v4l2_buf.bytesused;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001692 ret = copy_to_user(data, frame->data, count);
1693 if (ret != 0) {
1694 PDEBUG(D_ERR|D_STREAM,
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001695 "read cp to user lack %d / %zd", ret, count);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001696 ret = -EFAULT;
1697 goto out;
1698 }
1699 ret = count;
1700out:
1701 /* in each case, requeue the buffer */
1702 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1703 if (ret2 != 0)
1704 return ret2;
1705 return ret;
1706}
1707
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001708static void dev_release(struct video_device *vfd)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001709{
1710 /* nothing */
1711}
1712
1713static struct file_operations dev_fops = {
1714 .owner = THIS_MODULE,
1715 .open = dev_open,
1716 .release = dev_close,
1717 .read = dev_read,
1718 .mmap = dev_mmap,
1719 .ioctl = video_ioctl2,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001720#ifdef CONFIG_COMPAT
1721 .compat_ioctl = v4l_compat_ioctl32,
1722#endif
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001723 .llseek = no_llseek,
1724 .poll = dev_poll,
1725};
1726
Hans Verkuila3998102008-07-21 02:57:38 -03001727static const struct v4l2_ioctl_ops dev_ioctl_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001728 .vidioc_querycap = vidioc_querycap,
1729 .vidioc_dqbuf = vidioc_dqbuf,
1730 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001731 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1732 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1733 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1734 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001735 .vidioc_streamon = vidioc_streamon,
1736 .vidioc_queryctrl = vidioc_queryctrl,
1737 .vidioc_g_ctrl = vidioc_g_ctrl,
1738 .vidioc_s_ctrl = vidioc_s_ctrl,
1739 .vidioc_querymenu = vidioc_querymenu,
1740 .vidioc_enum_input = vidioc_enum_input,
1741 .vidioc_g_input = vidioc_g_input,
1742 .vidioc_s_input = vidioc_s_input,
1743 .vidioc_reqbufs = vidioc_reqbufs,
1744 .vidioc_querybuf = vidioc_querybuf,
1745 .vidioc_streamoff = vidioc_streamoff,
1746 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1747 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1748 .vidioc_g_parm = vidioc_g_parm,
1749 .vidioc_s_parm = vidioc_s_parm,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001750 .vidioc_s_std = vidioc_s_std,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001751#ifdef CONFIG_VIDEO_V4L1_COMPAT
1752 .vidiocgmbuf = vidiocgmbuf,
1753#endif
1754};
1755
Hans Verkuila3998102008-07-21 02:57:38 -03001756static struct video_device gspca_template = {
1757 .name = "gspca main driver",
Hans Verkuila3998102008-07-21 02:57:38 -03001758 .fops = &dev_fops,
1759 .ioctl_ops = &dev_ioctl_ops,
1760 .release = dev_release, /* mandatory */
1761 .minor = -1,
1762};
1763
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001764/*
1765 * probe and create a new gspca device
1766 *
1767 * This function must be called by the sub-driver when it is
1768 * called for probing a new device.
1769 */
1770int gspca_dev_probe(struct usb_interface *intf,
1771 const struct usb_device_id *id,
1772 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001773 int dev_size,
1774 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001775{
1776 struct usb_interface_descriptor *interface;
1777 struct gspca_dev *gspca_dev;
1778 struct usb_device *dev = interface_to_usbdev(intf);
1779 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001780
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001781 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001782
1783 /* we don't handle multi-config cameras */
1784 if (dev->descriptor.bNumConfigurations != 1)
1785 return -ENODEV;
1786 interface = &intf->cur_altsetting->desc;
1787 if (interface->bInterfaceNumber > 0)
1788 return -ENODEV;
1789
1790 /* create the device */
1791 if (dev_size < sizeof *gspca_dev)
1792 dev_size = sizeof *gspca_dev;
1793 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
1794 if (gspca_dev == NULL) {
1795 err("couldn't kzalloc gspca struct");
1796 return -EIO;
1797 }
Jean-Francois Moine8295d992008-09-03 17:12:19 -03001798 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
1799 if (!gspca_dev->usb_buf) {
1800 err("out of memory");
1801 ret = -EIO;
1802 goto out;
1803 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001804 gspca_dev->dev = dev;
1805 gspca_dev->iface = interface->bInterfaceNumber;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001806 gspca_dev->nbalt = intf->num_altsetting;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001807 gspca_dev->sd_desc = sd_desc;
1808/* gspca_dev->users = 0; (done by kzalloc) */
1809 gspca_dev->nbufread = 2;
1810
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001811 /* configure the subdriver and initialize the USB device */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001812 ret = gspca_dev->sd_desc->config(gspca_dev, id);
1813 if (ret < 0)
1814 goto out;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001815 ret = gspca_dev->sd_desc->init(gspca_dev);
1816 if (ret < 0)
1817 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001818 ret = gspca_set_alt0(gspca_dev);
1819 if (ret < 0)
1820 goto out;
1821 gspca_set_default_mode(gspca_dev);
1822
1823 mutex_init(&gspca_dev->usb_lock);
1824 mutex_init(&gspca_dev->read_lock);
1825 mutex_init(&gspca_dev->queue_lock);
1826 init_waitqueue_head(&gspca_dev->wq);
1827
1828 /* init video stuff */
1829 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
Hans Verkuil5e85e732008-07-20 06:31:39 -03001830 gspca_dev->vdev.parent = &dev->dev;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001831 memcpy(&gspca_dev->fops, &dev_fops, sizeof gspca_dev->fops);
1832 gspca_dev->vdev.fops = &gspca_dev->fops;
1833 gspca_dev->fops.owner = module; /* module protection */
Oliver Neukumee56a4d2008-07-27 14:01:59 -03001834 gspca_dev->present = 1;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001835 ret = video_register_device(&gspca_dev->vdev,
1836 VFL_TYPE_GRABBER,
1837 video_nr);
1838 if (ret < 0) {
1839 err("video_register_device err %d", ret);
1840 goto out;
1841 }
1842
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001843 usb_set_intfdata(intf, gspca_dev);
1844 PDEBUG(D_PROBE, "probe ok");
1845 return 0;
1846out:
Jean-Francois Moine8295d992008-09-03 17:12:19 -03001847 kfree(gspca_dev->usb_buf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001848 kfree(gspca_dev);
1849 return ret;
1850}
1851EXPORT_SYMBOL(gspca_dev_probe);
1852
1853/*
1854 * USB disconnection
1855 *
1856 * This function must be called by the sub-driver
1857 * when the device disconnects, after the specific resources are freed.
1858 */
1859void gspca_disconnect(struct usb_interface *intf)
1860{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001861 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001862
1863 if (!gspca_dev)
1864 return;
1865 gspca_dev->present = 0;
Hans de Goedee2997a72008-04-23 08:09:12 -03001866 mutex_lock(&gspca_dev->queue_lock);
1867 mutex_lock(&gspca_dev->usb_lock);
1868 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001869 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001870 mutex_unlock(&gspca_dev->usb_lock);
Hans de Goedee2997a72008-04-23 08:09:12 -03001871 mutex_unlock(&gspca_dev->queue_lock);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001872 while (gspca_dev->users != 0) { /* wait until fully closed */
1873 atomic_inc(&gspca_dev->nevent);
1874 wake_up_interruptible(&gspca_dev->wq); /* wake processes */
1875 schedule();
1876 }
1877/* We don't want people trying to open up the device */
1878 video_unregister_device(&gspca_dev->vdev);
1879/* Free the memory */
Jean-Francois Moine8295d992008-09-03 17:12:19 -03001880 kfree(gspca_dev->usb_buf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001881 kfree(gspca_dev);
1882 PDEBUG(D_PROBE, "disconnect complete");
1883}
1884EXPORT_SYMBOL(gspca_disconnect);
1885
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001886#ifdef CONFIG_PM
1887int gspca_suspend(struct usb_interface *intf, pm_message_t message)
1888{
1889 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1890
1891 if (!gspca_dev->streaming)
1892 return 0;
1893 gspca_dev->frozen = 1; /* avoid urb error messages */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001894 if (gspca_dev->sd_desc->stopN)
1895 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001896 destroy_urbs(gspca_dev);
1897 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001898 if (gspca_dev->sd_desc->stop0)
1899 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001900 return 0;
1901}
1902EXPORT_SYMBOL(gspca_suspend);
1903
1904int gspca_resume(struct usb_interface *intf)
1905{
1906 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1907
1908 gspca_dev->frozen = 0;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001909 gspca_dev->sd_desc->init(gspca_dev);
1910 if (gspca_dev->streaming)
1911 return gspca_init_transfer(gspca_dev);
Jean-Francois Moine1e89e2d2008-09-03 17:12:15 -03001912 return 0;
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001913}
1914EXPORT_SYMBOL(gspca_resume);
1915#endif
Hans de Goededcef3232008-07-10 10:40:53 -03001916/* -- cam driver utility functions -- */
1917
1918/* auto gain and exposure algorithm based on the knee algorithm described here:
1919 http://ytse.tricolour.net/docs/LowLightOptimization.html
1920
1921 Returns 0 if no changes were made, 1 if the gain and or exposure settings
1922 where changed. */
1923int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
1924 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
1925{
1926 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
1927 const struct ctrl *gain_ctrl = NULL;
1928 const struct ctrl *exposure_ctrl = NULL;
1929 const struct ctrl *autogain_ctrl = NULL;
1930 int retval = 0;
1931
1932 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1933 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
1934 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1935 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
1936 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
1937 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
1938 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1939 }
1940 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
1941 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
1942 "on cam without (auto)gain/exposure");
1943 return 0;
1944 }
1945
1946 if (gain_ctrl->get(gspca_dev, &gain) ||
1947 exposure_ctrl->get(gspca_dev, &exposure) ||
1948 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
1949 return 0;
1950
1951 orig_gain = gain;
1952 orig_exposure = exposure;
1953
1954 /* If we are of a multiple of deadzone, do multiple steps to reach the
1955 desired lumination fast (with the risc of a slight overshoot) */
1956 steps = abs(desired_avg_lum - avg_lum) / deadzone;
1957
1958 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d\n",
1959 avg_lum, desired_avg_lum, steps);
1960
1961 for (i = 0; i < steps; i++) {
1962 if (avg_lum > desired_avg_lum) {
1963 if (gain > gain_knee)
1964 gain--;
1965 else if (exposure > exposure_knee)
1966 exposure--;
1967 else if (gain > gain_ctrl->qctrl.default_value)
1968 gain--;
1969 else if (exposure > exposure_ctrl->qctrl.minimum)
1970 exposure--;
1971 else if (gain > gain_ctrl->qctrl.minimum)
1972 gain--;
1973 else
1974 break;
1975 } else {
1976 if (gain < gain_ctrl->qctrl.default_value)
1977 gain++;
1978 else if (exposure < exposure_knee)
1979 exposure++;
1980 else if (gain < gain_knee)
1981 gain++;
1982 else if (exposure < exposure_ctrl->qctrl.maximum)
1983 exposure++;
1984 else if (gain < gain_ctrl->qctrl.maximum)
1985 gain++;
1986 else
1987 break;
1988 }
1989 }
1990
1991 if (gain != orig_gain) {
1992 gain_ctrl->set(gspca_dev, gain);
1993 retval = 1;
1994 }
1995 if (exposure != orig_exposure) {
1996 exposure_ctrl->set(gspca_dev, exposure);
1997 retval = 1;
1998 }
1999
2000 return retval;
2001}
2002EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2003
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002004/* -- module insert / remove -- */
2005static int __init gspca_init(void)
2006{
Jean-Francois Moinebb64e862008-07-19 06:49:28 -03002007 info("main v%d.%d.%d registered",
2008 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2009 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2010 DRIVER_VERSION_NUMBER & 0xff);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002011 return 0;
2012}
2013static void __exit gspca_exit(void)
2014{
2015 info("main deregistered");
2016}
2017
2018module_init(gspca_init);
2019module_exit(gspca_exit);
2020
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03002021#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002022module_param_named(debug, gspca_debug, int, 0644);
2023MODULE_PARM_DESC(debug,
2024 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002025 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
2026 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002027#endif