blob: 1da7a5c6a9a38c94341c75c55ffb28f43a7504b4 [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>
Jean-Francois Moinecf0fe082008-10-17 04:36:47 -030024#include <linux/version.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030025#include <linux/fs.h>
26#include <linux/vmalloc.h>
27#include <linux/sched.h>
28#include <linux/slab.h>
29#include <linux/mm.h>
30#include <linux/string.h>
31#include <linux/pagemap.h>
Jean-Francois Moine956e42d2008-07-01 10:03:42 -030032#include <linux/io.h>
Frank Zago6b060ff2008-09-28 08:12:22 -030033#include <linux/kref.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030034#include <asm/page.h>
Jean-Francois Moine956e42d2008-07-01 10:03:42 -030035#include <linux/uaccess.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030036#include <linux/jiffies.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030037#include <media/v4l2-ioctl.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030038
39#include "gspca.h"
40
Jean-Francois Moined43fa322008-06-12 10:58:58 -030041/* global values */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -030042#define DEF_NURBS 2 /* default number of URBs */
Jean-Francois Moined43fa322008-06-12 10:58:58 -030043
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030044MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
45MODULE_DESCRIPTION("GSPCA USB Camera Driver");
46MODULE_LICENSE("GPL");
47
Jean-Francois Moine240d3ad2008-10-24 05:02:58 -030048#define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 4, 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030049
50static int video_nr = -1;
51
Jean-Francois Moine335b3f82008-07-30 04:53:02 -030052#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030053int gspca_debug = D_ERR | D_PROBE;
54EXPORT_SYMBOL(gspca_debug);
55
56static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
57{
58 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
59 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
60 txt,
61 pixfmt & 0xff,
62 (pixfmt >> 8) & 0xff,
63 (pixfmt >> 16) & 0xff,
64 pixfmt >> 24,
65 w, h);
66 } else {
67 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
68 txt,
69 pixfmt,
70 w, h);
71 }
72}
73#else
74#define PDEBUG_MODE(txt, pixfmt, w, h)
75#endif
76
Jean-Francois Moined43fa322008-06-12 10:58:58 -030077/* specific memory types - !! should different from V4L2_MEMORY_xxx */
78#define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
79#define GSPCA_MEMORY_READ 7
80
Jean-Francois Moined43fa322008-06-12 10:58:58 -030081#define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
Jean-Francois Moined43fa322008-06-12 10:58:58 -030082
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030083/*
84 * VMA operations.
85 */
86static void gspca_vm_open(struct vm_area_struct *vma)
87{
88 struct gspca_frame *frame = vma->vm_private_data;
89
90 frame->vma_use_count++;
91 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
92}
93
94static void gspca_vm_close(struct vm_area_struct *vma)
95{
96 struct gspca_frame *frame = vma->vm_private_data;
97
98 if (--frame->vma_use_count <= 0)
99 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
100}
101
102static struct vm_operations_struct gspca_vm_ops = {
103 .open = gspca_vm_open,
104 .close = gspca_vm_close,
105};
106
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300107/* get the current input frame buffer */
108struct gspca_frame *gspca_get_i_frame(struct gspca_dev *gspca_dev)
109{
110 struct gspca_frame *frame;
111 int i;
112
113 i = gspca_dev->fr_i;
114 i = gspca_dev->fr_queue[i];
115 frame = &gspca_dev->frame[i];
116 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
117 != V4L2_BUF_FLAG_QUEUED)
118 return NULL;
119 return frame;
120}
121EXPORT_SYMBOL(gspca_get_i_frame);
122
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300123/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300124 * fill a video frame from an URB and resubmit
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300125 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300126static void fill_frame(struct gspca_dev *gspca_dev,
127 struct urb *urb)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300128{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300129 struct gspca_frame *frame;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300130 __u8 *data; /* address of data in the iso message */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300131 int i, len, st;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300132 cam_pkt_op pkt_scan;
133
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300134 if (urb->status != 0) {
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300135#ifdef CONFIG_PM
Jean-Francois Moine6a709742008-09-03 16:48:10 -0300136 if (!gspca_dev->frozen)
Jean-Francois Moine62ee8ee2008-09-03 17:12:13 -0300137#endif
Jean-Francois Moine6a709742008-09-03 16:48:10 -0300138 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300139 return; /* disconnection ? */
140 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300141 pkt_scan = gspca_dev->sd_desc->pkt_scan;
142 for (i = 0; i < urb->number_of_packets; i++) {
143
144 /* check the availability of the frame buffer */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300145 frame = gspca_get_i_frame(gspca_dev);
146 if (!frame) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300147 gspca_dev->last_packet_type = DISCARD_PACKET;
148 break;
149 }
150
151 /* check the packet status and length */
152 len = urb->iso_frame_desc[i].actual_length;
Jean-Francois Moineff374742008-10-23 07:29:51 -0300153 if (len == 0) {
154 if (gspca_dev->empty_packet == 0)
155 gspca_dev->empty_packet = 1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300156 continue;
Jean-Francois Moineff374742008-10-23 07:29:51 -0300157 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300158 st = urb->iso_frame_desc[i].status;
159 if (st) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300160 PDEBUG(D_ERR,
161 "ISOC data error: [%d] len=%d, status=%d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300162 i, len, st);
163 gspca_dev->last_packet_type = DISCARD_PACKET;
164 continue;
165 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300166
167 /* let the packet be analyzed by the subdriver */
168 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
169 i, urb->iso_frame_desc[i].offset, len);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300170 data = (__u8 *) urb->transfer_buffer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300171 + urb->iso_frame_desc[i].offset;
172 pkt_scan(gspca_dev, frame, data, len);
173 }
174
175 /* resubmit the URB */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300176 st = usb_submit_urb(urb, GFP_ATOMIC);
177 if (st < 0)
178 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
179}
180
181/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300182 * ISOC message interrupt from the USB device
183 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300184 * Analyse each packet and call the subdriver for copy to the frame buffer.
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300185 */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300186static void isoc_irq(struct urb *urb
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300187)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300188{
189 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
190
Hans de Goede3647fea2008-07-15 05:36:30 -0300191 PDEBUG(D_PACK, "isoc irq");
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300192 if (!gspca_dev->streaming)
193 return;
194 fill_frame(gspca_dev, urb);
195}
196
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300197/*
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300198 * bulk message interrupt from the USB device
199 */
200static void bulk_irq(struct urb *urb
201)
202{
203 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
204 struct gspca_frame *frame;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300205 int st;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300206
207 PDEBUG(D_PACK, "bulk irq");
208 if (!gspca_dev->streaming)
209 return;
Jean-Francois Moine880ae242008-11-18 04:19:52 -0300210 switch (urb->status) {
211 case 0:
212 break;
213 case -ECONNRESET:
214 urb->status = 0;
215 break;
216 default:
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300217#ifdef CONFIG_PM
218 if (!gspca_dev->frozen)
219#endif
220 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
221 return; /* disconnection ? */
222 }
223
224 /* check the availability of the frame buffer */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300225 frame = gspca_get_i_frame(gspca_dev);
226 if (!frame) {
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300227 gspca_dev->last_packet_type = DISCARD_PACKET;
228 } else {
229 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
230 gspca_dev->sd_desc->pkt_scan(gspca_dev,
231 frame,
232 urb->transfer_buffer,
233 urb->actual_length);
234 }
Antonio Ospitedff369a2008-11-14 07:53:32 -0300235
236 /* resubmit the URB */
237 if (gspca_dev->cam.bulk_nurbs != 0) {
238 st = usb_submit_urb(urb, GFP_ATOMIC);
239 if (st < 0)
240 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
241 }
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300242}
243
244/*
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300245 * add data to the current frame
246 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300247 * This function is called by the subdrivers at interrupt level.
248 *
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300249 * To build a frame, these ones must add
250 * - one FIRST_PACKET
251 * - 0 or many INTER_PACKETs
252 * - one LAST_PACKET
253 * DISCARD_PACKET invalidates the whole frame.
254 * On LAST_PACKET, a new frame is returned.
255 */
256struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
Erik Andrene293e592008-10-03 08:46:50 -0300257 enum gspca_packet_type packet_type,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300258 struct gspca_frame *frame,
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300259 const __u8 *data,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300260 int len)
261{
262 int i, j;
263
Hans de Goedee2997a72008-04-23 08:09:12 -0300264 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300265
266 /* when start of a new frame, if the current frame buffer
267 * is not queued, discard the whole frame */
268 if (packet_type == FIRST_PACKET) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300269 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
270 != V4L2_BUF_FLAG_QUEUED) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300271 gspca_dev->last_packet_type = DISCARD_PACKET;
272 return frame;
273 }
274 frame->data_end = frame->data;
275 jiffies_to_timeval(get_jiffies_64(),
276 &frame->v4l2_buf.timestamp);
277 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300278 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
Jean-Francois Moine06ca78f2008-07-22 03:45:08 -0300279 if (packet_type == LAST_PACKET)
280 gspca_dev->last_packet_type = packet_type;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300281 return frame;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300282 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300283
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300284 /* append the packet to the frame buffer */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300285 if (len > 0) {
286 if (frame->data_end - frame->data + len
287 > frame->v4l2_buf.length) {
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -0300288 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300289 frame->data_end - frame->data + len,
290 frame->v4l2_buf.length);
291 packet_type = DISCARD_PACKET;
292 } else {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300293 memcpy(frame->data_end, data, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300294 frame->data_end += len;
295 }
296 }
297 gspca_dev->last_packet_type = packet_type;
298
Frank Zago219423c2008-09-26 07:43:54 -0300299 /* if last packet, wake up the application and advance in the queue */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300300 if (packet_type == LAST_PACKET) {
301 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
302 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
303 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
304 atomic_inc(&gspca_dev->nevent);
305 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300306 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
307 gspca_dev->fr_i = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300308 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
309 frame->v4l2_buf.bytesused,
310 gspca_dev->fr_q,
311 i,
312 gspca_dev->fr_o);
313 j = gspca_dev->fr_queue[i];
314 frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300315 }
316 return frame;
317}
318EXPORT_SYMBOL(gspca_frame_add);
319
320static int gspca_is_compressed(__u32 format)
321{
322 switch (format) {
323 case V4L2_PIX_FMT_MJPEG:
324 case V4L2_PIX_FMT_JPEG:
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300325 case V4L2_PIX_FMT_SPCA561:
Hans de Goedeab8f12c2008-07-04 18:29:32 -0300326 case V4L2_PIX_FMT_PAC207:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300327 return 1;
328 }
329 return 0;
330}
331
332static void *rvmalloc(unsigned long size)
333{
334 void *mem;
335 unsigned long adr;
336
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300337 mem = vmalloc_32(size);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300338 if (mem != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300339 adr = (unsigned long) mem;
340 while ((long) size > 0) {
341 SetPageReserved(vmalloc_to_page((void *) adr));
342 adr += PAGE_SIZE;
343 size -= PAGE_SIZE;
344 }
345 }
346 return mem;
347}
348
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300349static void rvfree(void *mem, long size)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300350{
351 unsigned long adr;
352
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300353 adr = (unsigned long) mem;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300354 while (size > 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300355 ClearPageReserved(vmalloc_to_page((void *) adr));
356 adr += PAGE_SIZE;
357 size -= PAGE_SIZE;
358 }
359 vfree(mem);
360}
361
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300362static int frame_alloc(struct gspca_dev *gspca_dev,
363 unsigned int count)
364{
365 struct gspca_frame *frame;
366 unsigned int frsz;
367 int i;
368
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300369 i = gspca_dev->curr_mode;
370 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300371 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300372 frsz = PAGE_ALIGN(frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300373 gspca_dev->frsz = frsz;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300374 if (count > GSPCA_MAX_FRAMES)
375 count = GSPCA_MAX_FRAMES;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300376 gspca_dev->frbuf = rvmalloc(frsz * count);
377 if (!gspca_dev->frbuf) {
378 err("frame alloc failed");
379 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300380 }
381 gspca_dev->nframes = count;
382 for (i = 0; i < count; i++) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300383 frame = &gspca_dev->frame[i];
384 frame->v4l2_buf.index = i;
385 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
386 frame->v4l2_buf.flags = 0;
387 frame->v4l2_buf.field = V4L2_FIELD_NONE;
388 frame->v4l2_buf.length = frsz;
389 frame->v4l2_buf.memory = gspca_dev->memory;
390 frame->v4l2_buf.sequence = 0;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300391 frame->data = frame->data_end =
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300392 gspca_dev->frbuf + i * frsz;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300393 frame->v4l2_buf.m.offset = i * frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300394 }
395 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
396 gspca_dev->last_packet_type = DISCARD_PACKET;
397 gspca_dev->sequence = 0;
398 atomic_set(&gspca_dev->nevent, 0);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300399 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300400}
401
402static void frame_free(struct gspca_dev *gspca_dev)
403{
404 int i;
405
406 PDEBUG(D_STREAM, "frame free");
Harvey Harrisonefab8212008-07-05 06:12:47 -0300407 if (gspca_dev->frbuf != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300408 rvfree(gspca_dev->frbuf,
409 gspca_dev->nframes * gspca_dev->frsz);
410 gspca_dev->frbuf = NULL;
411 for (i = 0; i < gspca_dev->nframes; i++)
412 gspca_dev->frame[i].data = NULL;
413 }
414 gspca_dev->nframes = 0;
415}
416
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300417static void destroy_urbs(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300418{
419 struct urb *urb;
420 unsigned int i;
421
422 PDEBUG(D_STREAM, "kill transfer");
Erik Andrén44732812008-10-13 15:52:46 -0300423 for (i = 0; i < MAX_NURBS; i++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300424 urb = gspca_dev->urb[i];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300425 if (urb == NULL)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300426 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300427
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300428 gspca_dev->urb[i] = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300429 usb_kill_urb(urb);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300430 if (urb->transfer_buffer != NULL)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300431 usb_buffer_free(gspca_dev->dev,
432 urb->transfer_buffer_length,
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300433 urb->transfer_buffer,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300434 urb->transfer_dma);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300435 usb_free_urb(urb);
436 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300437}
438
439/*
Frank Zago219423c2008-09-26 07:43:54 -0300440 * look for an input transfer endpoint in an alternate setting
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300441 */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300442static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
443 __u8 epaddr,
444 __u8 xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300445{
446 struct usb_host_endpoint *ep;
447 int i, attr;
448
449 epaddr |= USB_DIR_IN;
450 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
451 ep = &alt->endpoint[i];
452 if (ep->desc.bEndpointAddress == epaddr) {
453 attr = ep->desc.bmAttributes
454 & USB_ENDPOINT_XFERTYPE_MASK;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300455 if (attr == xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300456 return ep;
457 break;
458 }
459 }
460 return NULL;
461}
462
463/*
Frank Zago219423c2008-09-26 07:43:54 -0300464 * look for an input (isoc or bulk) endpoint
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300465 *
466 * The endpoint is defined by the subdriver.
467 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
468 * This routine may be called many times when the bandwidth is too small
469 * (the bandwidth is checked on urb submit).
470 */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300471static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300472{
473 struct usb_interface *intf;
474 struct usb_host_endpoint *ep;
475 int i, ret;
476
477 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300478 ep = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300479 i = gspca_dev->alt; /* previous alt setting */
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300480
481 /* try isoc */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300482 while (--i > 0) { /* alt 0 is unusable */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300483 ep = alt_xfer(&intf->altsetting[i],
484 gspca_dev->cam.epaddr,
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300485 USB_ENDPOINT_XFER_ISOC);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300486 if (ep)
487 break;
488 }
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300489
490 /* if no isoc, try bulk */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300491 if (ep == NULL) {
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300492 ep = alt_xfer(&intf->altsetting[0],
493 gspca_dev->cam.epaddr,
494 USB_ENDPOINT_XFER_BULK);
495 if (ep == NULL) {
496 err("no transfer endpoint found");
497 return NULL;
498 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300499 }
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300500 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300501 i, ep->desc.bEndpointAddress);
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300502 if (i > 0) {
503 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
504 if (ret < 0) {
505 err("set interface err %d", ret);
506 return NULL;
507 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300508 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300509 gspca_dev->alt = i; /* memorize the current alt setting */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300510 return ep;
511}
512
513/*
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300514 * create the URBs for image transfer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300515 */
516static int create_urbs(struct gspca_dev *gspca_dev,
517 struct usb_host_endpoint *ep)
518{
519 struct urb *urb;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300520 int n, nurbs, i, psize, npkt, bsize;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300521
522 /* calculate the packet size and the number of packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300523 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300524
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300525 if (gspca_dev->alt != 0) { /* isoc */
526
527 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
528 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300529 npkt = ISO_MAX_SIZE / psize;
530 if (npkt > ISO_MAX_PKT)
531 npkt = ISO_MAX_PKT;
532 bsize = psize * npkt;
533 PDEBUG(D_STREAM,
534 "isoc %d pkts size %d = bsize:%d",
535 npkt, psize, bsize);
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300536 nurbs = DEF_NURBS;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300537 } else { /* bulk */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300538 npkt = 0;
Antonio Ospitedff369a2008-11-14 07:53:32 -0300539 bsize = gspca_dev->cam.bulk_size;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300540 if (bsize == 0)
541 bsize = psize;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300542 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
Antonio Ospitedff369a2008-11-14 07:53:32 -0300543 if (gspca_dev->cam.bulk_nurbs != 0)
544 nurbs = gspca_dev->cam.bulk_nurbs;
545 else
546 nurbs = 1;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300547 }
548
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300549 gspca_dev->nurbs = nurbs;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300550 for (n = 0; n < nurbs; n++) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300551 urb = usb_alloc_urb(npkt, GFP_KERNEL);
552 if (!urb) {
553 err("usb_alloc_urb failed");
Jean-Francois Moine0d0548e2008-09-05 12:49:54 -0300554 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300555 return -ENOMEM;
556 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300557 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300558 bsize,
559 GFP_KERNEL,
560 &urb->transfer_dma);
561
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300562 if (urb->transfer_buffer == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300563 usb_free_urb(urb);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300564 err("usb_buffer_urb failed");
Jean-Francois Moine0d0548e2008-09-05 12:49:54 -0300565 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300566 return -ENOMEM;
567 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300568 gspca_dev->urb[n] = urb;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300569 urb->dev = gspca_dev->dev;
570 urb->context = gspca_dev;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300571 urb->transfer_buffer_length = bsize;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300572 if (npkt != 0) { /* ISOC */
573 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
574 ep->desc.bEndpointAddress);
Frank Zago6c49da72008-09-28 07:43:00 -0300575 urb->transfer_flags = URB_ISO_ASAP
576 | URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300577 urb->interval = ep->desc.bInterval;
578 urb->complete = isoc_irq;
579 urb->number_of_packets = npkt;
580 for (i = 0; i < npkt; i++) {
581 urb->iso_frame_desc[i].length = psize;
582 urb->iso_frame_desc[i].offset = psize * i;
583 }
584 } else { /* bulk */
585 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
586 ep->desc.bEndpointAddress),
Frank Zago6c49da72008-09-28 07:43:00 -0300587 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300588 urb->complete = bulk_irq;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300589 }
590 }
591 return 0;
592}
593
594/*
595 * start the USB transfer
596 */
597static int gspca_init_transfer(struct gspca_dev *gspca_dev)
598{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300599 struct usb_host_endpoint *ep;
600 int n, ret;
601
Hans de Goedee2997a72008-04-23 08:09:12 -0300602 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
603 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300604
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300605 /* set the higher alternate setting and
606 * loop until urb submit succeeds */
607 gspca_dev->alt = gspca_dev->nbalt;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300608 for (;;) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300609 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300610 ep = get_ep(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300611 if (ep == NULL) {
612 ret = -EIO;
613 goto out;
614 }
615 ret = create_urbs(gspca_dev, ep);
616 if (ret < 0)
617 goto out;
618
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300619 /* clear the bulk endpoint */
620 if (gspca_dev->alt == 0) /* if bulk transfer */
621 usb_clear_halt(gspca_dev->dev,
622 usb_rcvintpipe(gspca_dev->dev,
623 gspca_dev->cam.epaddr));
624
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300625 /* start the cam */
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300626 ret = gspca_dev->sd_desc->start(gspca_dev);
627 if (ret < 0) {
628 destroy_urbs(gspca_dev);
629 goto out;
630 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300631 gspca_dev->streaming = 1;
632 atomic_set(&gspca_dev->nevent, 0);
633
Antonio Ospitedff369a2008-11-14 07:53:32 -0300634 /* some bulk transfers are started by the subdriver */
635 if (gspca_dev->alt == 0 && gspca_dev->cam.bulk_nurbs == 0)
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300636 break;
637
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300638 /* submit the URBs */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300639 for (n = 0; n < gspca_dev->nurbs; n++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300640 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300641 if (ret < 0) {
642 PDEBUG(D_ERR|D_STREAM,
643 "usb_submit_urb [%d] err %d", n, ret);
Hans de Goedee2997a72008-04-23 08:09:12 -0300644 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300645 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300646 if (ret == -ENOSPC)
647 break; /* try the previous alt */
648 goto out;
649 }
650 }
651 if (ret >= 0)
652 break;
653 }
654out:
655 mutex_unlock(&gspca_dev->usb_lock);
656 return ret;
657}
658
659static int gspca_set_alt0(struct gspca_dev *gspca_dev)
660{
661 int ret;
662
663 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
664 if (ret < 0)
665 PDEBUG(D_ERR|D_STREAM, "set interface 0 err %d", ret);
666 return ret;
667}
668
Frank Zago219423c2008-09-26 07:43:54 -0300669/* Note: both the queue and the usb locks should be held when calling this */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300670static void gspca_stream_off(struct gspca_dev *gspca_dev)
671{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300672 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300673 atomic_set(&gspca_dev->nevent, 0);
Jean-Francois Moine98522a72008-11-18 06:33:08 -0300674 if (gspca_dev->present
675 && gspca_dev->sd_desc->stopN)
676 gspca_dev->sd_desc->stopN(gspca_dev);
677 destroy_urbs(gspca_dev);
678 gspca_set_alt0(gspca_dev);
679 if (gspca_dev->sd_desc->stop0)
680 gspca_dev->sd_desc->stop0(gspca_dev);
681 PDEBUG(D_STREAM, "stream off OK");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300682}
683
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300684static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300685{
686 int i;
687
688 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
689 gspca_dev->curr_mode = i;
690 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
691 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300692 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300693}
694
695static int wxh_to_mode(struct gspca_dev *gspca_dev,
696 int width, int height)
697{
698 int i;
699
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300700 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
701 if (width >= gspca_dev->cam.cam_mode[i].width
702 && height >= gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300703 break;
704 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300705 return i;
706}
707
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300708/*
709 * search a mode with the right pixel format
710 */
711static int gspca_get_mode(struct gspca_dev *gspca_dev,
712 int mode,
713 int pixfmt)
714{
715 int modeU, modeD;
716
717 modeU = modeD = mode;
718 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
719 if (--modeD >= 0) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300720 if (gspca_dev->cam.cam_mode[modeD].pixelformat
721 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300722 return modeD;
723 }
724 if (++modeU < gspca_dev->cam.nmodes) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300725 if (gspca_dev->cam.cam_mode[modeU].pixelformat
726 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300727 return modeU;
728 }
729 }
730 return -EINVAL;
731}
732
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300733static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300734 struct v4l2_fmtdesc *fmtdesc)
735{
736 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300737 int i, j, index;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300738 __u32 fmt_tb[8];
739
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300740 /* give an index to each format */
741 index = 0;
742 j = 0;
743 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300744 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300745 j = 0;
746 for (;;) {
747 if (fmt_tb[j] == fmt_tb[index])
748 break;
749 j++;
750 }
751 if (j == index) {
752 if (fmtdesc->index == index)
753 break; /* new format */
754 index++;
Julia Lawall80297122008-11-12 23:18:21 -0300755 if (index >= ARRAY_SIZE(fmt_tb))
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300756 return -EINVAL;
757 }
758 }
759 if (i < 0)
760 return -EINVAL; /* no more format */
761
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300762 fmtdesc->pixelformat = fmt_tb[index];
763 if (gspca_is_compressed(fmt_tb[index]))
764 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300765 fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300766 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
767 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
768 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
769 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
770 fmtdesc->description[4] = '\0';
771 return 0;
772}
773
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300774static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300775 struct v4l2_format *fmt)
776{
777 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300778 int mode;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300779
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300780 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
781 return -EINVAL;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300782 mode = gspca_dev->curr_mode;
783 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
784 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300785 return 0;
786}
787
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300788static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300789 struct v4l2_format *fmt)
790{
Hans de Goede24c530b2008-07-04 17:55:18 -0300791 int w, h, mode, mode2;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300792
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300793 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
794 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300795 w = fmt->fmt.pix.width;
796 h = fmt->fmt.pix.height;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300797
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300798#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300799 if (gspca_debug & D_CONF)
800 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
801#endif
802 /* search the closest mode for width and height */
803 mode = wxh_to_mode(gspca_dev, w, h);
804
805 /* OK if right palette */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300806 if (gspca_dev->cam.cam_mode[mode].pixelformat
807 != fmt->fmt.pix.pixelformat) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300808
809 /* else, search the closest mode with the same pixel format */
810 mode2 = gspca_get_mode(gspca_dev, mode,
811 fmt->fmt.pix.pixelformat);
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300812 if (mode2 >= 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300813 mode = mode2;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300814/* else
815 ; * no chance, return this mode */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300816 }
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300817 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
818 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300819 return mode; /* used when s_fmt */
820}
821
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300822static int vidioc_try_fmt_vid_cap(struct file *file,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300823 void *priv,
824 struct v4l2_format *fmt)
825{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300826 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300827 int ret;
828
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300829 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300830 if (ret < 0)
831 return ret;
832 return 0;
833}
834
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300835static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300836 struct v4l2_format *fmt)
837{
838 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300839 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300840
Hans de Goedee2997a72008-04-23 08:09:12 -0300841 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
842 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300843
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300844 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300845 if (ret < 0)
846 goto out;
847
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300848 if (gspca_dev->nframes != 0
849 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
850 ret = -EINVAL;
851 goto out;
852 }
853
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300854 if (ret == gspca_dev->curr_mode) {
855 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300856 goto out; /* same mode */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300857 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300858
859 if (gspca_dev->streaming) {
860 ret = -EBUSY;
861 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -0300862 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300863 gspca_dev->width = fmt->fmt.pix.width;
864 gspca_dev->height = fmt->fmt.pix.height;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300865 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
866 gspca_dev->curr_mode = ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300867
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300868 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300869out:
870 mutex_unlock(&gspca_dev->queue_lock);
871 return ret;
872}
873
Frank Zago6b060ff2008-09-28 08:12:22 -0300874static void gspca_delete(struct kref *kref)
875{
876 struct gspca_dev *gspca_dev = container_of(kref, struct gspca_dev, kref);
877
878 PDEBUG(D_STREAM, "device deleted");
879
880 kfree(gspca_dev->usb_buf);
881 kfree(gspca_dev);
882}
883
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300884static int dev_open(struct inode *inode, struct file *file)
885{
886 struct gspca_dev *gspca_dev;
887 int ret;
888
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300889 PDEBUG(D_STREAM, "%s open", current->comm);
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -0300890 gspca_dev = (struct gspca_dev *) video_devdata(file);
Hans de Goedee2997a72008-04-23 08:09:12 -0300891 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
892 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300893 if (!gspca_dev->present) {
894 ret = -ENODEV;
895 goto out;
896 }
897
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300898 if (gspca_dev->users > 4) { /* (arbitrary value) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300899 ret = -EBUSY;
900 goto out;
901 }
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -0300902
903 /* protect the subdriver against rmmod */
904 if (!try_module_get(gspca_dev->module)) {
905 ret = -ENODEV;
906 goto out;
907 }
908
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300909 gspca_dev->users++;
Frank Zago6b060ff2008-09-28 08:12:22 -0300910
911 /* one more user */
912 kref_get(&gspca_dev->kref);
913
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300914 file->private_data = gspca_dev;
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300915#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300916 /* activate the v4l2 debug */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300917 if (gspca_debug & D_V4L2)
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -0300918 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -0300919 | V4L2_DEBUG_IOCTL_ARG;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300920 else
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -0300921 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -0300922 | V4L2_DEBUG_IOCTL_ARG);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300923#endif
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300924 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300925out:
926 mutex_unlock(&gspca_dev->queue_lock);
927 if (ret != 0)
928 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
929 else
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300930 PDEBUG(D_STREAM, "open done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300931 return ret;
932}
933
934static int dev_close(struct inode *inode, struct file *file)
935{
936 struct gspca_dev *gspca_dev = file->private_data;
937
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300938 PDEBUG(D_STREAM, "%s close", current->comm);
Hans de Goedee2997a72008-04-23 08:09:12 -0300939 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
940 return -ERESTARTSYS;
941 gspca_dev->users--;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300942
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -0300943 /* if the file did the capture, free the streaming resources */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300944 if (gspca_dev->capt_file == file) {
Jean-Francois Moinea1e82882008-09-03 17:12:17 -0300945 if (gspca_dev->streaming) {
946 mutex_lock(&gspca_dev->usb_lock);
947 gspca_stream_off(gspca_dev);
948 mutex_unlock(&gspca_dev->usb_lock);
949 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300950 frame_free(gspca_dev);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300951 gspca_dev->capt_file = NULL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300952 gspca_dev->memory = GSPCA_MEMORY_NO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300953 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300954 file->private_data = NULL;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -0300955 module_put(gspca_dev->module);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300956 mutex_unlock(&gspca_dev->queue_lock);
Frank Zago6b060ff2008-09-28 08:12:22 -0300957
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300958 PDEBUG(D_STREAM, "close done");
Frank Zago6b060ff2008-09-28 08:12:22 -0300959
960 kref_put(&gspca_dev->kref, gspca_delete);
961
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300962 return 0;
963}
964
965static int vidioc_querycap(struct file *file, void *priv,
966 struct v4l2_capability *cap)
967{
968 struct gspca_dev *gspca_dev = priv;
969
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300970 memset(cap, 0, sizeof *cap);
971 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -0300972 if (gspca_dev->dev->product != NULL) {
973 strncpy(cap->card, gspca_dev->dev->product,
974 sizeof cap->card);
975 } else {
976 snprintf(cap->card, sizeof cap->card,
977 "USB Camera (%04x:%04x)",
978 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
979 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
980 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300981 strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name,
982 sizeof cap->bus_info);
983 cap->version = DRIVER_VERSION_NUMBER;
984 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
985 | V4L2_CAP_STREAMING
986 | V4L2_CAP_READWRITE;
987 return 0;
988}
989
990static int vidioc_queryctrl(struct file *file, void *priv,
991 struct v4l2_queryctrl *q_ctrl)
992{
993 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300994 int i, ix;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300995 u32 id;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300996
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300997 ix = -1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300998 id = q_ctrl->id;
999 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1000 id &= V4L2_CTRL_ID_MASK;
1001 id++;
1002 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goede0f523c22008-09-03 17:12:14 -03001003 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001004 continue;
1005 if (ix < 0) {
1006 ix = i;
1007 continue;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001008 }
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001009 if (gspca_dev->sd_desc->ctrls[i].qctrl.id
1010 > gspca_dev->sd_desc->ctrls[ix].qctrl.id)
1011 continue;
1012 ix = i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001013 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001014 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001015 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001016 if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) {
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001017 ix = i;
1018 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001019 }
1020 }
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001021 if (ix < 0)
1022 return -EINVAL;
1023 memcpy(q_ctrl, &gspca_dev->sd_desc->ctrls[ix].qctrl,
1024 sizeof *q_ctrl);
1025 if (gspca_dev->ctrl_dis & (1 << ix))
1026 q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
1027 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001028}
1029
1030static int vidioc_s_ctrl(struct file *file, void *priv,
1031 struct v4l2_control *ctrl)
1032{
1033 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001034 const struct ctrl *ctrls;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001035 int i, ret;
1036
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001037 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1038 i < gspca_dev->sd_desc->nctrls;
1039 i++, ctrls++) {
1040 if (ctrl->id != ctrls->qctrl.id)
1041 continue;
Hans de Goede0f523c22008-09-03 17:12:14 -03001042 if (gspca_dev->ctrl_dis & (1 << i))
1043 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001044 if (ctrl->value < ctrls->qctrl.minimum
Rabin Vincentfcf5cb22008-08-03 07:58:54 -03001045 || ctrl->value > ctrls->qctrl.maximum)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001046 return -ERANGE;
1047 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
Hans de Goedee2997a72008-04-23 08:09:12 -03001048 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1049 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001050 ret = ctrls->set(gspca_dev, ctrl->value);
1051 mutex_unlock(&gspca_dev->usb_lock);
1052 return ret;
1053 }
1054 return -EINVAL;
1055}
1056
1057static int vidioc_g_ctrl(struct file *file, void *priv,
1058 struct v4l2_control *ctrl)
1059{
1060 struct gspca_dev *gspca_dev = priv;
1061
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001062 const struct ctrl *ctrls;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001063 int i, ret;
1064
1065 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1066 i < gspca_dev->sd_desc->nctrls;
1067 i++, ctrls++) {
1068 if (ctrl->id != ctrls->qctrl.id)
1069 continue;
Hans de Goede0f523c22008-09-03 17:12:14 -03001070 if (gspca_dev->ctrl_dis & (1 << i))
1071 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001072 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1073 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001074 ret = ctrls->get(gspca_dev, &ctrl->value);
1075 mutex_unlock(&gspca_dev->usb_lock);
1076 return ret;
1077 }
1078 return -EINVAL;
1079}
1080
1081static int vidioc_querymenu(struct file *file, void *priv,
1082 struct v4l2_querymenu *qmenu)
1083{
1084 struct gspca_dev *gspca_dev = priv;
1085
1086 if (!gspca_dev->sd_desc->querymenu)
1087 return -EINVAL;
1088 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1089}
1090
1091static int vidioc_enum_input(struct file *file, void *priv,
1092 struct v4l2_input *input)
1093{
1094 struct gspca_dev *gspca_dev = priv;
1095
1096 if (input->index != 0)
1097 return -EINVAL;
1098 memset(input, 0, sizeof *input);
1099 input->type = V4L2_INPUT_TYPE_CAMERA;
1100 strncpy(input->name, gspca_dev->sd_desc->name,
1101 sizeof input->name);
1102 return 0;
1103}
1104
1105static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1106{
1107 *i = 0;
1108 return 0;
1109}
1110
1111static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1112{
1113 if (i > 0)
1114 return -EINVAL;
1115 return (0);
1116}
1117
1118static int vidioc_reqbufs(struct file *file, void *priv,
1119 struct v4l2_requestbuffers *rb)
1120{
1121 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001122 int i, ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001123
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001124 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1125 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001126 switch (rb->memory) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001127 case GSPCA_MEMORY_READ: /* (internal call) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001128 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001129 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001130 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001131 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001132 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001133 }
Hans de Goedee2997a72008-04-23 08:09:12 -03001134 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1135 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001136
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001137 if (gspca_dev->memory != GSPCA_MEMORY_NO
1138 && gspca_dev->memory != rb->memory) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001139 ret = -EBUSY;
1140 goto out;
1141 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001142
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001143 /* only one file may do the capture */
1144 if (gspca_dev->capt_file != NULL
1145 && gspca_dev->capt_file != file) {
1146 ret = -EBUSY;
1147 goto out;
1148 }
1149
1150 /* if allocated, the buffers must not be mapped */
1151 for (i = 0; i < gspca_dev->nframes; i++) {
1152 if (gspca_dev->frame[i].vma_use_count) {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001153 ret = -EBUSY;
1154 goto out;
1155 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001156 }
1157
1158 /* stop streaming */
1159 if (gspca_dev->streaming) {
1160 mutex_lock(&gspca_dev->usb_lock);
1161 gspca_stream_off(gspca_dev);
1162 mutex_unlock(&gspca_dev->usb_lock);
1163 }
1164
1165 /* free the previous allocated buffers, if any */
1166 if (gspca_dev->nframes != 0) {
1167 frame_free(gspca_dev);
1168 gspca_dev->capt_file = NULL;
1169 }
1170 if (rb->count == 0) /* unrequest */
1171 goto out;
1172 gspca_dev->memory = rb->memory;
1173 ret = frame_alloc(gspca_dev, rb->count);
1174 if (ret == 0) {
1175 rb->count = gspca_dev->nframes;
1176 gspca_dev->capt_file = file;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001177 }
1178out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001179 mutex_unlock(&gspca_dev->queue_lock);
1180 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1181 return ret;
1182}
1183
1184static int vidioc_querybuf(struct file *file, void *priv,
1185 struct v4l2_buffer *v4l2_buf)
1186{
1187 struct gspca_dev *gspca_dev = priv;
1188 struct gspca_frame *frame;
1189
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001190 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1191 || v4l2_buf->index < 0
1192 || v4l2_buf->index >= gspca_dev->nframes)
1193 return -EINVAL;
1194
1195 frame = &gspca_dev->frame[v4l2_buf->index];
1196 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1197 return 0;
1198}
1199
1200static int vidioc_streamon(struct file *file, void *priv,
1201 enum v4l2_buf_type buf_type)
1202{
1203 struct gspca_dev *gspca_dev = priv;
1204 int ret;
1205
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001206 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1207 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001208 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1209 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001210 if (!gspca_dev->present) {
1211 ret = -ENODEV;
1212 goto out;
1213 }
1214 if (gspca_dev->nframes == 0) {
1215 ret = -EINVAL;
1216 goto out;
1217 }
1218 if (!gspca_dev->streaming) {
1219 ret = gspca_init_transfer(gspca_dev);
1220 if (ret < 0)
1221 goto out;
1222 }
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001223#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001224 if (gspca_debug & D_STREAM) {
1225 PDEBUG_MODE("stream on OK",
1226 gspca_dev->pixfmt,
1227 gspca_dev->width,
1228 gspca_dev->height);
1229 }
1230#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001231 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001232out:
1233 mutex_unlock(&gspca_dev->queue_lock);
1234 return ret;
1235}
1236
1237static int vidioc_streamoff(struct file *file, void *priv,
1238 enum v4l2_buf_type buf_type)
1239{
1240 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001241 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001242
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001243 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1244 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001245 if (!gspca_dev->streaming)
1246 return 0;
1247 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1248 return -ERESTARTSYS;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001249
1250 /* stop streaming */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001251 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1252 ret = -ERESTARTSYS;
1253 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001254 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001255 gspca_stream_off(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001256 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001257
1258 /* empty the application queues */
1259 for (i = 0; i < gspca_dev->nframes; i++)
1260 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1261 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1262 gspca_dev->last_packet_type = DISCARD_PACKET;
1263 gspca_dev->sequence = 0;
1264 atomic_set(&gspca_dev->nevent, 0);
1265 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001266out:
1267 mutex_unlock(&gspca_dev->queue_lock);
1268 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001269}
1270
1271static int vidioc_g_jpegcomp(struct file *file, void *priv,
1272 struct v4l2_jpegcompression *jpegcomp)
1273{
1274 struct gspca_dev *gspca_dev = priv;
1275 int ret;
1276
1277 if (!gspca_dev->sd_desc->get_jcomp)
1278 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001279 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1280 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001281 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1282 mutex_unlock(&gspca_dev->usb_lock);
1283 return ret;
1284}
1285
1286static int vidioc_s_jpegcomp(struct file *file, void *priv,
1287 struct v4l2_jpegcompression *jpegcomp)
1288{
1289 struct gspca_dev *gspca_dev = priv;
1290 int ret;
1291
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001292 if (!gspca_dev->sd_desc->set_jcomp)
1293 return -EINVAL;
Jim Parisf86d4a92008-12-10 05:50:44 -03001294 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1295 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001296 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1297 mutex_unlock(&gspca_dev->usb_lock);
1298 return ret;
1299}
1300
1301static int vidioc_g_parm(struct file *filp, void *priv,
1302 struct v4l2_streamparm *parm)
1303{
1304 struct gspca_dev *gspca_dev = priv;
1305
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001306 memset(parm, 0, sizeof *parm);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001307 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1308 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1309 return 0;
1310}
1311
1312static int vidioc_s_parm(struct file *filp, void *priv,
1313 struct v4l2_streamparm *parm)
1314{
1315 struct gspca_dev *gspca_dev = priv;
1316 int n;
1317
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001318 n = parm->parm.capture.readbuffers;
1319 if (n == 0 || n > GSPCA_MAX_FRAMES)
1320 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1321 else
1322 gspca_dev->nbufread = n;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001323 return 0;
1324}
1325
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001326static int vidioc_s_std(struct file *filp, void *priv,
1327 v4l2_std_id *parm)
1328{
1329 return 0;
1330}
1331
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001332#ifdef CONFIG_VIDEO_V4L1_COMPAT
1333static int vidiocgmbuf(struct file *file, void *priv,
1334 struct video_mbuf *mbuf)
1335{
1336 struct gspca_dev *gspca_dev = file->private_data;
1337 int i;
1338
1339 PDEBUG(D_STREAM, "cgmbuf");
1340 if (gspca_dev->nframes == 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001341 int ret;
1342
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001343 {
1344 struct v4l2_format fmt;
1345
1346 memset(&fmt, 0, sizeof fmt);
1347 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1348 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1349 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1350 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1351 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001352 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001353 if (ret != 0)
1354 return ret;
1355 }
1356 {
1357 struct v4l2_requestbuffers rb;
1358
1359 memset(&rb, 0, sizeof rb);
1360 rb.count = 4;
1361 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1362 rb.memory = V4L2_MEMORY_MMAP;
1363 ret = vidioc_reqbufs(file, priv, &rb);
1364 if (ret != 0)
1365 return ret;
1366 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001367 }
1368 mbuf->frames = gspca_dev->nframes;
1369 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1370 for (i = 0; i < mbuf->frames; i++)
1371 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1372 return 0;
1373}
1374#endif
1375
1376static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1377{
1378 struct gspca_dev *gspca_dev = file->private_data;
Harvey Harrisonefab8212008-07-05 06:12:47 -03001379 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001380 struct page *page;
1381 unsigned long addr, start, size;
1382 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001383
1384 start = vma->vm_start;
1385 size = vma->vm_end - vma->vm_start;
1386 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1387
Hans de Goedee2997a72008-04-23 08:09:12 -03001388 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1389 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001390 if (!gspca_dev->present) {
1391 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001392 goto out;
1393 }
1394 if (gspca_dev->capt_file != file) {
1395 ret = -EINVAL;
1396 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001397 }
1398
Harvey Harrisonefab8212008-07-05 06:12:47 -03001399 frame = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001400 for (i = 0; i < gspca_dev->nframes; ++i) {
1401 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1402 PDEBUG(D_STREAM, "mmap bad memory type");
1403 break;
1404 }
1405 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1406 == vma->vm_pgoff) {
1407 frame = &gspca_dev->frame[i];
1408 break;
1409 }
1410 }
Harvey Harrisonefab8212008-07-05 06:12:47 -03001411 if (frame == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001412 PDEBUG(D_STREAM, "mmap no frame buffer found");
1413 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001414 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001415 }
1416#ifdef CONFIG_VIDEO_V4L1_COMPAT
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001417 /* v4l1 maps all the buffers */
1418 if (i != 0
1419 || size != frame->v4l2_buf.length * gspca_dev->nframes)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001420#endif
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001421 if (size != frame->v4l2_buf.length) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001422 PDEBUG(D_STREAM, "mmap bad size");
1423 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001424 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001425 }
1426
1427 /*
1428 * - VM_IO marks the area as being a mmaped region for I/O to a
1429 * device. It also prevents the region from being core dumped.
1430 */
1431 vma->vm_flags |= VM_IO;
1432
1433 addr = (unsigned long) frame->data;
1434 while (size > 0) {
1435 page = vmalloc_to_page((void *) addr);
1436 ret = vm_insert_page(vma, start, page);
1437 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001438 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001439 start += PAGE_SIZE;
1440 addr += PAGE_SIZE;
1441 size -= PAGE_SIZE;
1442 }
1443
1444 vma->vm_ops = &gspca_vm_ops;
1445 vma->vm_private_data = frame;
1446 gspca_vm_open(vma);
Hans de Goedee2997a72008-04-23 08:09:12 -03001447 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001448out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001449 mutex_unlock(&gspca_dev->queue_lock);
1450 return ret;
1451}
1452
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001453/*
1454 * wait for a video frame
1455 *
1456 * If a frame is ready, its index is returned.
1457 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001458static int frame_wait(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001459 int nonblock_ing)
1460{
1461 struct gspca_frame *frame;
1462 int i, j, ret;
1463
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001464 /* check if a frame is ready */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001465 i = gspca_dev->fr_o;
1466 j = gspca_dev->fr_queue[i];
1467 frame = &gspca_dev->frame[j];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001468 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) {
1469 atomic_dec(&gspca_dev->nevent);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001470 goto ok;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001471 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001472 if (nonblock_ing) /* no frame yet */
1473 return -EAGAIN;
1474
1475 /* wait till a frame is ready */
1476 for (;;) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001477 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1478 atomic_read(&gspca_dev->nevent) > 0,
1479 msecs_to_jiffies(3000));
1480 if (ret <= 0) {
1481 if (ret < 0)
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001482 return ret; /* interrupt */
1483 return -EIO; /* timeout */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001484 }
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001485 atomic_dec(&gspca_dev->nevent);
Hans de Goedee2997a72008-04-23 08:09:12 -03001486 if (!gspca_dev->streaming || !gspca_dev->present)
1487 return -EIO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001488 i = gspca_dev->fr_o;
1489 j = gspca_dev->fr_queue[i];
1490 frame = &gspca_dev->frame[j];
1491 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1492 break;
1493 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001494ok:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001495 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1496 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1497 gspca_dev->fr_q,
1498 gspca_dev->fr_i,
1499 gspca_dev->fr_o);
Hans de Goedee2997a72008-04-23 08:09:12 -03001500
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001501 if (gspca_dev->sd_desc->dq_callback) {
1502 mutex_lock(&gspca_dev->usb_lock);
Hans de Goedee2997a72008-04-23 08:09:12 -03001503 gspca_dev->sd_desc->dq_callback(gspca_dev);
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001504 mutex_unlock(&gspca_dev->usb_lock);
1505 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001506 return j;
1507}
1508
1509/*
1510 * dequeue a video buffer
1511 *
1512 * If nonblock_ing is false, block until a buffer is available.
1513 */
1514static int vidioc_dqbuf(struct file *file, void *priv,
1515 struct v4l2_buffer *v4l2_buf)
1516{
1517 struct gspca_dev *gspca_dev = priv;
1518 struct gspca_frame *frame;
1519 int i, ret;
1520
1521 PDEBUG(D_FRAM, "dqbuf");
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001522 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1523 return -EINVAL;
1524 if (v4l2_buf->memory != gspca_dev->memory)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001525 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001526
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001527 /* if not streaming, be sure the application will not loop forever */
1528 if (!(file->f_flags & O_NONBLOCK)
1529 && !gspca_dev->streaming && gspca_dev->users == 1)
1530 return -EINVAL;
1531
1532 /* only the capturing file may dequeue */
1533 if (gspca_dev->capt_file != file)
1534 return -EINVAL;
1535
1536 /* only one dequeue / read at a time */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001537 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1538 return -ERESTARTSYS;
1539
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001540 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001541 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001542 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001543 i = ret; /* frame index */
1544 frame = &gspca_dev->frame[i];
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001545 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001546 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001547 frame->data,
1548 frame->v4l2_buf.bytesused)) {
1549 PDEBUG(D_ERR|D_STREAM,
1550 "dqbuf cp to user failed");
1551 ret = -EFAULT;
1552 goto out;
1553 }
1554 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001555 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1556 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1557 PDEBUG(D_FRAM, "dqbuf %d", i);
1558 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001559out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001560 mutex_unlock(&gspca_dev->read_lock);
1561 return ret;
1562}
1563
1564/*
1565 * queue a video buffer
1566 *
1567 * Attempting to queue a buffer that has already been
1568 * queued will return -EINVAL.
1569 */
1570static int vidioc_qbuf(struct file *file, void *priv,
1571 struct v4l2_buffer *v4l2_buf)
1572{
1573 struct gspca_dev *gspca_dev = priv;
1574 struct gspca_frame *frame;
1575 int i, index, ret;
1576
1577 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1578 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1579 return -EINVAL;
1580
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001581 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1582 return -ERESTARTSYS;
1583
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001584 index = v4l2_buf->index;
1585 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001586 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001587 "qbuf idx %d >= %d", index, gspca_dev->nframes);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001588 ret = -EINVAL;
1589 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001590 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001591 if (v4l2_buf->memory != gspca_dev->memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001592 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001593 ret = -EINVAL;
1594 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001595 }
1596
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001597 frame = &gspca_dev->frame[index];
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001598 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1599 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001600 ret = -EINVAL;
1601 goto out;
1602 }
1603
1604 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001605
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001606 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001607 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1608 frame->v4l2_buf.length = v4l2_buf->length;
1609 }
1610
1611 /* put the buffer in the 'queued' queue */
1612 i = gspca_dev->fr_q;
1613 gspca_dev->fr_queue[i] = index;
1614 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1615 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1616 gspca_dev->fr_q,
1617 gspca_dev->fr_i,
1618 gspca_dev->fr_o);
1619
1620 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1621 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1622 ret = 0;
1623out:
1624 mutex_unlock(&gspca_dev->queue_lock);
1625 return ret;
1626}
1627
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001628/*
1629 * allocate the resources for read()
1630 */
1631static int read_alloc(struct gspca_dev *gspca_dev,
1632 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001633{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001634 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001635 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001636
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001637 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001638 if (gspca_dev->nframes == 0) {
1639 struct v4l2_requestbuffers rb;
1640
1641 memset(&rb, 0, sizeof rb);
1642 rb.count = gspca_dev->nbufread;
1643 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001644 rb.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001645 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1646 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001647 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001648 return ret;
1649 }
1650 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1651 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001652 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001653 for (i = 0; i < gspca_dev->nbufread; i++) {
1654 v4l2_buf.index = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001655 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1656 if (ret != 0) {
1657 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1658 return ret;
1659 }
1660 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001661 gspca_dev->memory = GSPCA_MEMORY_READ;
1662 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001663
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001664 /* start streaming */
1665 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1666 if (ret != 0)
1667 PDEBUG(D_STREAM, "read streamon err %d", ret);
1668 return ret;
1669}
1670
1671static unsigned int dev_poll(struct file *file, poll_table *wait)
1672{
1673 struct gspca_dev *gspca_dev = file->private_data;
1674 int i, ret;
1675
1676 PDEBUG(D_FRAM, "poll");
1677
1678 poll_wait(file, &gspca_dev->wq, wait);
1679 if (!gspca_dev->present)
1680 return POLLERR;
1681
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001682 /* if reqbufs is not done, the user would use read() */
1683 if (gspca_dev->nframes == 0) {
1684 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1685 return POLLERR; /* not the 1st time */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001686 ret = read_alloc(gspca_dev, file);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001687 if (ret != 0)
1688 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001689 }
1690
1691 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1692 return POLLERR;
1693 if (!gspca_dev->present) {
1694 ret = POLLERR;
1695 goto out;
1696 }
1697
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001698 /* check the next incoming buffer */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001699 i = gspca_dev->fr_o;
1700 i = gspca_dev->fr_queue[i];
1701 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1702 ret = POLLIN | POLLRDNORM; /* something to read */
1703 else
1704 ret = 0;
1705out:
1706 mutex_unlock(&gspca_dev->queue_lock);
1707 return ret;
1708}
1709
1710static ssize_t dev_read(struct file *file, char __user *data,
1711 size_t count, loff_t *ppos)
1712{
1713 struct gspca_dev *gspca_dev = file->private_data;
1714 struct gspca_frame *frame;
1715 struct v4l2_buffer v4l2_buf;
1716 struct timeval timestamp;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001717 int n, ret, ret2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001718
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001719 PDEBUG(D_FRAM, "read (%zd)", count);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001720 if (!gspca_dev->present)
1721 return -ENODEV;
1722 switch (gspca_dev->memory) {
1723 case GSPCA_MEMORY_NO: /* first time */
1724 ret = read_alloc(gspca_dev, file);
1725 if (ret != 0)
1726 return ret;
1727 break;
1728 case GSPCA_MEMORY_READ:
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001729 if (gspca_dev->capt_file == file)
1730 break;
1731 /* fall thru */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001732 default:
1733 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001734 }
1735
1736 /* get a frame */
1737 jiffies_to_timeval(get_jiffies_64(), &timestamp);
1738 timestamp.tv_sec--;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001739 n = 2;
1740 for (;;) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001741 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1742 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001743 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001744 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1745 if (ret != 0) {
1746 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1747 return ret;
1748 }
1749
1750 /* if the process slept for more than 1 second,
Frank Zago219423c2008-09-26 07:43:54 -03001751 * get a newer frame */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001752 frame = &gspca_dev->frame[v4l2_buf.index];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001753 if (--n < 0)
1754 break; /* avoid infinite loop */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001755 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1756 break;
1757 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1758 if (ret != 0) {
1759 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1760 return ret;
1761 }
1762 }
1763
1764 /* copy the frame */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001765 if (count > frame->v4l2_buf.bytesused)
1766 count = frame->v4l2_buf.bytesused;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001767 ret = copy_to_user(data, frame->data, count);
1768 if (ret != 0) {
1769 PDEBUG(D_ERR|D_STREAM,
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001770 "read cp to user lack %d / %zd", ret, count);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001771 ret = -EFAULT;
1772 goto out;
1773 }
1774 ret = count;
1775out:
1776 /* in each case, requeue the buffer */
1777 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1778 if (ret2 != 0)
1779 return ret2;
1780 return ret;
1781}
1782
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001783static struct file_operations dev_fops = {
1784 .owner = THIS_MODULE,
1785 .open = dev_open,
1786 .release = dev_close,
1787 .read = dev_read,
1788 .mmap = dev_mmap,
1789 .ioctl = video_ioctl2,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001790#ifdef CONFIG_COMPAT
1791 .compat_ioctl = v4l_compat_ioctl32,
1792#endif
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001793 .llseek = no_llseek,
1794 .poll = dev_poll,
1795};
1796
Hans Verkuila3998102008-07-21 02:57:38 -03001797static const struct v4l2_ioctl_ops dev_ioctl_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001798 .vidioc_querycap = vidioc_querycap,
1799 .vidioc_dqbuf = vidioc_dqbuf,
1800 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001801 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1802 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1803 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1804 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001805 .vidioc_streamon = vidioc_streamon,
1806 .vidioc_queryctrl = vidioc_queryctrl,
1807 .vidioc_g_ctrl = vidioc_g_ctrl,
1808 .vidioc_s_ctrl = vidioc_s_ctrl,
1809 .vidioc_querymenu = vidioc_querymenu,
1810 .vidioc_enum_input = vidioc_enum_input,
1811 .vidioc_g_input = vidioc_g_input,
1812 .vidioc_s_input = vidioc_s_input,
1813 .vidioc_reqbufs = vidioc_reqbufs,
1814 .vidioc_querybuf = vidioc_querybuf,
1815 .vidioc_streamoff = vidioc_streamoff,
1816 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1817 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1818 .vidioc_g_parm = vidioc_g_parm,
1819 .vidioc_s_parm = vidioc_s_parm,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001820 .vidioc_s_std = vidioc_s_std,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001821#ifdef CONFIG_VIDEO_V4L1_COMPAT
1822 .vidiocgmbuf = vidiocgmbuf,
1823#endif
1824};
1825
Hans Verkuila3998102008-07-21 02:57:38 -03001826static struct video_device gspca_template = {
1827 .name = "gspca main driver",
Hans Verkuila3998102008-07-21 02:57:38 -03001828 .fops = &dev_fops,
1829 .ioctl_ops = &dev_ioctl_ops,
Jean-Francois Moinee07a1d82008-11-19 06:37:53 -03001830 .release = video_device_release,
Hans Verkuila3998102008-07-21 02:57:38 -03001831 .minor = -1,
1832};
1833
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001834/*
1835 * probe and create a new gspca device
1836 *
1837 * This function must be called by the sub-driver when it is
1838 * called for probing a new device.
1839 */
1840int gspca_dev_probe(struct usb_interface *intf,
1841 const struct usb_device_id *id,
1842 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001843 int dev_size,
1844 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001845{
1846 struct usb_interface_descriptor *interface;
1847 struct gspca_dev *gspca_dev;
1848 struct usb_device *dev = interface_to_usbdev(intf);
1849 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001850
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001851 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001852
1853 /* we don't handle multi-config cameras */
1854 if (dev->descriptor.bNumConfigurations != 1)
1855 return -ENODEV;
1856 interface = &intf->cur_altsetting->desc;
1857 if (interface->bInterfaceNumber > 0)
1858 return -ENODEV;
1859
1860 /* create the device */
1861 if (dev_size < sizeof *gspca_dev)
1862 dev_size = sizeof *gspca_dev;
1863 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
Erik Andrene293e592008-10-03 08:46:50 -03001864 if (!gspca_dev) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001865 err("couldn't kzalloc gspca struct");
Erik Andrene293e592008-10-03 08:46:50 -03001866 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001867 }
Frank Zago6b060ff2008-09-28 08:12:22 -03001868 kref_init(&gspca_dev->kref);
Jean-Francois Moine8295d992008-09-03 17:12:19 -03001869 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
1870 if (!gspca_dev->usb_buf) {
1871 err("out of memory");
Erik Andrene293e592008-10-03 08:46:50 -03001872 ret = -ENOMEM;
Jean-Francois Moine8295d992008-09-03 17:12:19 -03001873 goto out;
1874 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001875 gspca_dev->dev = dev;
1876 gspca_dev->iface = interface->bInterfaceNumber;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001877 gspca_dev->nbalt = intf->num_altsetting;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001878 gspca_dev->sd_desc = sd_desc;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001879 gspca_dev->nbufread = 2;
Jean-Francois Moineff374742008-10-23 07:29:51 -03001880 gspca_dev->empty_packet = -1; /* don't check the empty packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001881
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001882 /* configure the subdriver and initialize the USB device */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001883 ret = gspca_dev->sd_desc->config(gspca_dev, id);
1884 if (ret < 0)
1885 goto out;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001886 ret = gspca_dev->sd_desc->init(gspca_dev);
1887 if (ret < 0)
1888 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001889 ret = gspca_set_alt0(gspca_dev);
1890 if (ret < 0)
1891 goto out;
1892 gspca_set_default_mode(gspca_dev);
1893
1894 mutex_init(&gspca_dev->usb_lock);
1895 mutex_init(&gspca_dev->read_lock);
1896 mutex_init(&gspca_dev->queue_lock);
1897 init_waitqueue_head(&gspca_dev->wq);
1898
1899 /* init video stuff */
Jean-Francois Moinee07a1d82008-11-19 06:37:53 -03001900 gspca_dev->vdev = video_device_alloc();
1901 memcpy(gspca_dev->vdev, &gspca_template, sizeof gspca_template);
1902 gspca_dev->vdev->parent = &dev->dev;
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -03001903 gspca_dev->module = module;
Oliver Neukumee56a4d2008-07-27 14:01:59 -03001904 gspca_dev->present = 1;
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001905 ret = video_register_device(&gspca_dev->vdev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001906 VFL_TYPE_GRABBER,
1907 video_nr);
1908 if (ret < 0) {
1909 err("video_register_device err %d", ret);
1910 goto out;
1911 }
1912
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001913 usb_set_intfdata(intf, gspca_dev);
1914 PDEBUG(D_PROBE, "probe ok");
1915 return 0;
1916out:
Jean-Francois Moinee07a1d82008-11-19 06:37:53 -03001917 kfree(gspca_dev->usb_buf);
1918 kfree(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001919 return ret;
1920}
1921EXPORT_SYMBOL(gspca_dev_probe);
1922
1923/*
1924 * USB disconnection
1925 *
1926 * This function must be called by the sub-driver
1927 * when the device disconnects, after the specific resources are freed.
1928 */
1929void gspca_disconnect(struct usb_interface *intf)
1930{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001931 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001932
Frank Zago6b060ff2008-09-28 08:12:22 -03001933 usb_set_intfdata(intf, NULL);
1934
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001935 /* release the device */
1936 /* (this will call gspca_release() immediatly or on last close) */
1937 video_unregister_device(&gspca_dev->vdev);
Jean-Francois Moine3145b8c2008-10-05 04:22:10 -03001938
Frank Zago6b060ff2008-09-28 08:12:22 -03001939 kref_put(&gspca_dev->kref, gspca_delete);
1940
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001941 PDEBUG(D_PROBE, "disconnect complete");
1942}
1943EXPORT_SYMBOL(gspca_disconnect);
1944
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001945#ifdef CONFIG_PM
1946int gspca_suspend(struct usb_interface *intf, pm_message_t message)
1947{
1948 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1949
1950 if (!gspca_dev->streaming)
1951 return 0;
1952 gspca_dev->frozen = 1; /* avoid urb error messages */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001953 if (gspca_dev->sd_desc->stopN)
1954 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001955 destroy_urbs(gspca_dev);
1956 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001957 if (gspca_dev->sd_desc->stop0)
1958 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001959 return 0;
1960}
1961EXPORT_SYMBOL(gspca_suspend);
1962
1963int gspca_resume(struct usb_interface *intf)
1964{
1965 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1966
1967 gspca_dev->frozen = 0;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001968 gspca_dev->sd_desc->init(gspca_dev);
1969 if (gspca_dev->streaming)
1970 return gspca_init_transfer(gspca_dev);
Jean-Francois Moine1e89e2d2008-09-03 17:12:15 -03001971 return 0;
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001972}
1973EXPORT_SYMBOL(gspca_resume);
1974#endif
Hans de Goededcef3232008-07-10 10:40:53 -03001975/* -- cam driver utility functions -- */
1976
1977/* auto gain and exposure algorithm based on the knee algorithm described here:
1978 http://ytse.tricolour.net/docs/LowLightOptimization.html
1979
1980 Returns 0 if no changes were made, 1 if the gain and or exposure settings
1981 where changed. */
1982int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
1983 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
1984{
1985 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
1986 const struct ctrl *gain_ctrl = NULL;
1987 const struct ctrl *exposure_ctrl = NULL;
1988 const struct ctrl *autogain_ctrl = NULL;
1989 int retval = 0;
1990
1991 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1992 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
1993 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1994 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
1995 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
1996 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
1997 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1998 }
1999 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2000 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2001 "on cam without (auto)gain/exposure");
2002 return 0;
2003 }
2004
2005 if (gain_ctrl->get(gspca_dev, &gain) ||
2006 exposure_ctrl->get(gspca_dev, &exposure) ||
2007 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2008 return 0;
2009
2010 orig_gain = gain;
2011 orig_exposure = exposure;
2012
2013 /* If we are of a multiple of deadzone, do multiple steps to reach the
2014 desired lumination fast (with the risc of a slight overshoot) */
2015 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2016
Jean-Francois Moine1c44d812008-11-10 05:56:55 -03002017 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
Hans de Goededcef3232008-07-10 10:40:53 -03002018 avg_lum, desired_avg_lum, steps);
2019
2020 for (i = 0; i < steps; i++) {
2021 if (avg_lum > desired_avg_lum) {
2022 if (gain > gain_knee)
2023 gain--;
2024 else if (exposure > exposure_knee)
2025 exposure--;
2026 else if (gain > gain_ctrl->qctrl.default_value)
2027 gain--;
2028 else if (exposure > exposure_ctrl->qctrl.minimum)
2029 exposure--;
2030 else if (gain > gain_ctrl->qctrl.minimum)
2031 gain--;
2032 else
2033 break;
2034 } else {
2035 if (gain < gain_ctrl->qctrl.default_value)
2036 gain++;
2037 else if (exposure < exposure_knee)
2038 exposure++;
2039 else if (gain < gain_knee)
2040 gain++;
2041 else if (exposure < exposure_ctrl->qctrl.maximum)
2042 exposure++;
2043 else if (gain < gain_ctrl->qctrl.maximum)
2044 gain++;
2045 else
2046 break;
2047 }
2048 }
2049
2050 if (gain != orig_gain) {
2051 gain_ctrl->set(gspca_dev, gain);
2052 retval = 1;
2053 }
2054 if (exposure != orig_exposure) {
2055 exposure_ctrl->set(gspca_dev, exposure);
2056 retval = 1;
2057 }
2058
2059 return retval;
2060}
2061EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2062
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002063/* -- module insert / remove -- */
2064static int __init gspca_init(void)
2065{
Jean-Francois Moinebb64e862008-07-19 06:49:28 -03002066 info("main v%d.%d.%d registered",
2067 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2068 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2069 DRIVER_VERSION_NUMBER & 0xff);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002070 return 0;
2071}
2072static void __exit gspca_exit(void)
2073{
2074 info("main deregistered");
2075}
2076
2077module_init(gspca_init);
2078module_exit(gspca_exit);
2079
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03002080#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002081module_param_named(debug, gspca_debug, int, 0644);
2082MODULE_PARM_DESC(debug,
2083 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002084 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
2085 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002086#endif