blob: 02d6a48ad1b276072d77de6054c91851b5f4a8cb [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 */
176 urb->status = 0;
177 st = usb_submit_urb(urb, GFP_ATOMIC);
178 if (st < 0)
179 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
180}
181
182/*
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300183 * ISOC message interrupt from the USB device
184 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300185 * Analyse each packet and call the subdriver for copy to the frame buffer.
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300186 */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300187static void isoc_irq(struct urb *urb
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300188)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300189{
190 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
191
Hans de Goede3647fea2008-07-15 05:36:30 -0300192 PDEBUG(D_PACK, "isoc irq");
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300193 if (!gspca_dev->streaming)
194 return;
195 fill_frame(gspca_dev, urb);
196}
197
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300198/*
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300199 * bulk message interrupt from the USB device
200 */
201static void bulk_irq(struct urb *urb
202)
203{
204 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
205 struct gspca_frame *frame;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300206
207 PDEBUG(D_PACK, "bulk irq");
208 if (!gspca_dev->streaming)
209 return;
210 if (urb->status != 0 && urb->status != -ECONNRESET) {
211#ifdef CONFIG_PM
212 if (!gspca_dev->frozen)
213#endif
214 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
215 return; /* disconnection ? */
216 }
217
218 /* check the availability of the frame buffer */
Jean-Francois Moine1b60e1a2008-09-20 05:44:21 -0300219 frame = gspca_get_i_frame(gspca_dev);
220 if (!frame) {
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300221 gspca_dev->last_packet_type = DISCARD_PACKET;
222 } else {
223 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
224 gspca_dev->sd_desc->pkt_scan(gspca_dev,
225 frame,
226 urb->transfer_buffer,
227 urb->actual_length);
228 }
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300229}
230
231/*
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300232 * add data to the current frame
233 *
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300234 * This function is called by the subdrivers at interrupt level.
235 *
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300236 * To build a frame, these ones must add
237 * - one FIRST_PACKET
238 * - 0 or many INTER_PACKETs
239 * - one LAST_PACKET
240 * DISCARD_PACKET invalidates the whole frame.
241 * On LAST_PACKET, a new frame is returned.
242 */
243struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
Erik Andrene293e592008-10-03 08:46:50 -0300244 enum gspca_packet_type packet_type,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300245 struct gspca_frame *frame,
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300246 const __u8 *data,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300247 int len)
248{
249 int i, j;
250
Hans de Goedee2997a72008-04-23 08:09:12 -0300251 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300252
253 /* when start of a new frame, if the current frame buffer
254 * is not queued, discard the whole frame */
255 if (packet_type == FIRST_PACKET) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300256 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
257 != V4L2_BUF_FLAG_QUEUED) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300258 gspca_dev->last_packet_type = DISCARD_PACKET;
259 return frame;
260 }
261 frame->data_end = frame->data;
262 jiffies_to_timeval(get_jiffies_64(),
263 &frame->v4l2_buf.timestamp);
264 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300265 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
Jean-Francois Moine06ca78f2008-07-22 03:45:08 -0300266 if (packet_type == LAST_PACKET)
267 gspca_dev->last_packet_type = packet_type;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300268 return frame;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300269 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300270
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300271 /* append the packet to the frame buffer */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300272 if (len > 0) {
273 if (frame->data_end - frame->data + len
274 > frame->v4l2_buf.length) {
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -0300275 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300276 frame->data_end - frame->data + len,
277 frame->v4l2_buf.length);
278 packet_type = DISCARD_PACKET;
279 } else {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300280 memcpy(frame->data_end, data, len);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300281 frame->data_end += len;
282 }
283 }
284 gspca_dev->last_packet_type = packet_type;
285
Frank Zago219423c2008-09-26 07:43:54 -0300286 /* if last packet, wake up the application and advance in the queue */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300287 if (packet_type == LAST_PACKET) {
288 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
289 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
290 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
291 atomic_inc(&gspca_dev->nevent);
292 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300293 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
294 gspca_dev->fr_i = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300295 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
296 frame->v4l2_buf.bytesused,
297 gspca_dev->fr_q,
298 i,
299 gspca_dev->fr_o);
300 j = gspca_dev->fr_queue[i];
301 frame = &gspca_dev->frame[j];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300302 }
303 return frame;
304}
305EXPORT_SYMBOL(gspca_frame_add);
306
307static int gspca_is_compressed(__u32 format)
308{
309 switch (format) {
310 case V4L2_PIX_FMT_MJPEG:
311 case V4L2_PIX_FMT_JPEG:
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300312 case V4L2_PIX_FMT_SPCA561:
Hans de Goedeab8f12c2008-07-04 18:29:32 -0300313 case V4L2_PIX_FMT_PAC207:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300314 return 1;
315 }
316 return 0;
317}
318
319static void *rvmalloc(unsigned long size)
320{
321 void *mem;
322 unsigned long adr;
323
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300324 mem = vmalloc_32(size);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300325 if (mem != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300326 adr = (unsigned long) mem;
327 while ((long) size > 0) {
328 SetPageReserved(vmalloc_to_page((void *) adr));
329 adr += PAGE_SIZE;
330 size -= PAGE_SIZE;
331 }
332 }
333 return mem;
334}
335
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300336static void rvfree(void *mem, long size)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300337{
338 unsigned long adr;
339
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300340 adr = (unsigned long) mem;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300341 while (size > 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300342 ClearPageReserved(vmalloc_to_page((void *) adr));
343 adr += PAGE_SIZE;
344 size -= PAGE_SIZE;
345 }
346 vfree(mem);
347}
348
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300349static int frame_alloc(struct gspca_dev *gspca_dev,
350 unsigned int count)
351{
352 struct gspca_frame *frame;
353 unsigned int frsz;
354 int i;
355
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300356 i = gspca_dev->curr_mode;
357 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300358 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300359 frsz = PAGE_ALIGN(frsz);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300360 gspca_dev->frsz = frsz;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300361 if (count > GSPCA_MAX_FRAMES)
362 count = GSPCA_MAX_FRAMES;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300363 gspca_dev->frbuf = rvmalloc(frsz * count);
364 if (!gspca_dev->frbuf) {
365 err("frame alloc failed");
366 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300367 }
368 gspca_dev->nframes = count;
369 for (i = 0; i < count; i++) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300370 frame = &gspca_dev->frame[i];
371 frame->v4l2_buf.index = i;
372 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
373 frame->v4l2_buf.flags = 0;
374 frame->v4l2_buf.field = V4L2_FIELD_NONE;
375 frame->v4l2_buf.length = frsz;
376 frame->v4l2_buf.memory = gspca_dev->memory;
377 frame->v4l2_buf.sequence = 0;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300378 frame->data = frame->data_end =
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300379 gspca_dev->frbuf + i * frsz;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -0300380 frame->v4l2_buf.m.offset = i * frsz;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300381 }
382 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
383 gspca_dev->last_packet_type = DISCARD_PACKET;
384 gspca_dev->sequence = 0;
385 atomic_set(&gspca_dev->nevent, 0);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300386 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300387}
388
389static void frame_free(struct gspca_dev *gspca_dev)
390{
391 int i;
392
393 PDEBUG(D_STREAM, "frame free");
Harvey Harrisonefab8212008-07-05 06:12:47 -0300394 if (gspca_dev->frbuf != NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300395 rvfree(gspca_dev->frbuf,
396 gspca_dev->nframes * gspca_dev->frsz);
397 gspca_dev->frbuf = NULL;
398 for (i = 0; i < gspca_dev->nframes; i++)
399 gspca_dev->frame[i].data = NULL;
400 }
401 gspca_dev->nframes = 0;
402}
403
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300404static void destroy_urbs(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300405{
406 struct urb *urb;
407 unsigned int i;
408
409 PDEBUG(D_STREAM, "kill transfer");
Erik Andrén44732812008-10-13 15:52:46 -0300410 for (i = 0; i < MAX_NURBS; i++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300411 urb = gspca_dev->urb[i];
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300412 if (urb == NULL)
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300413 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300414
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300415 gspca_dev->urb[i] = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300416 usb_kill_urb(urb);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300417 if (urb->transfer_buffer != NULL)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300418 usb_buffer_free(gspca_dev->dev,
419 urb->transfer_buffer_length,
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300420 urb->transfer_buffer,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300421 urb->transfer_dma);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300422 usb_free_urb(urb);
423 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300424}
425
426/*
Frank Zago219423c2008-09-26 07:43:54 -0300427 * look for an input transfer endpoint in an alternate setting
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300428 */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300429static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
430 __u8 epaddr,
431 __u8 xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300432{
433 struct usb_host_endpoint *ep;
434 int i, attr;
435
436 epaddr |= USB_DIR_IN;
437 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
438 ep = &alt->endpoint[i];
439 if (ep->desc.bEndpointAddress == epaddr) {
440 attr = ep->desc.bmAttributes
441 & USB_ENDPOINT_XFERTYPE_MASK;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300442 if (attr == xfer)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300443 return ep;
444 break;
445 }
446 }
447 return NULL;
448}
449
450/*
Frank Zago219423c2008-09-26 07:43:54 -0300451 * look for an input (isoc or bulk) endpoint
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300452 *
453 * The endpoint is defined by the subdriver.
454 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
455 * This routine may be called many times when the bandwidth is too small
456 * (the bandwidth is checked on urb submit).
457 */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300458static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300459{
460 struct usb_interface *intf;
461 struct usb_host_endpoint *ep;
462 int i, ret;
463
464 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300465 ep = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300466 i = gspca_dev->alt; /* previous alt setting */
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300467
468 /* try isoc */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300469 while (--i > 0) { /* alt 0 is unusable */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300470 ep = alt_xfer(&intf->altsetting[i],
471 gspca_dev->cam.epaddr,
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300472 USB_ENDPOINT_XFER_ISOC);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300473 if (ep)
474 break;
475 }
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300476
477 /* if no isoc, try bulk */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300478 if (ep == NULL) {
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300479 ep = alt_xfer(&intf->altsetting[0],
480 gspca_dev->cam.epaddr,
481 USB_ENDPOINT_XFER_BULK);
482 if (ep == NULL) {
483 err("no transfer endpoint found");
484 return NULL;
485 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300486 }
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300487 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300488 i, ep->desc.bEndpointAddress);
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300489 if (i > 0) {
490 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
491 if (ret < 0) {
492 err("set interface err %d", ret);
493 return NULL;
494 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300495 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300496 gspca_dev->alt = i; /* memorize the current alt setting */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300497 return ep;
498}
499
500/*
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300501 * create the URBs for image transfer
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300502 */
503static int create_urbs(struct gspca_dev *gspca_dev,
504 struct usb_host_endpoint *ep)
505{
506 struct urb *urb;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300507 int n, nurbs, i, psize, npkt, bsize;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300508
509 /* calculate the packet size and the number of packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300510 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300511
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300512 if (gspca_dev->alt != 0) { /* isoc */
513
514 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
515 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300516 npkt = ISO_MAX_SIZE / psize;
517 if (npkt > ISO_MAX_PKT)
518 npkt = ISO_MAX_PKT;
519 bsize = psize * npkt;
520 PDEBUG(D_STREAM,
521 "isoc %d pkts size %d = bsize:%d",
522 npkt, psize, bsize);
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300523 nurbs = DEF_NURBS;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300524 } else { /* bulk */
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300525 npkt = 0;
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300526 bsize = gspca_dev->cam. bulk_size;
527 if (bsize == 0)
528 bsize = psize;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300529 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300530 nurbs = 1;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300531 }
532
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300533 gspca_dev->nurbs = nurbs;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300534 for (n = 0; n < nurbs; n++) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300535 urb = usb_alloc_urb(npkt, GFP_KERNEL);
536 if (!urb) {
537 err("usb_alloc_urb failed");
Jean-Francois Moine0d0548e2008-09-05 12:49:54 -0300538 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300539 return -ENOMEM;
540 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300541 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300542 bsize,
543 GFP_KERNEL,
544 &urb->transfer_dma);
545
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300546 if (urb->transfer_buffer == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300547 usb_free_urb(urb);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300548 err("usb_buffer_urb failed");
Jean-Francois Moine0d0548e2008-09-05 12:49:54 -0300549 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300550 return -ENOMEM;
551 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300552 gspca_dev->urb[n] = urb;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300553 urb->dev = gspca_dev->dev;
554 urb->context = gspca_dev;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300555 urb->transfer_buffer_length = bsize;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300556 if (npkt != 0) { /* ISOC */
557 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
558 ep->desc.bEndpointAddress);
Frank Zago6c49da72008-09-28 07:43:00 -0300559 urb->transfer_flags = URB_ISO_ASAP
560 | URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300561 urb->interval = ep->desc.bInterval;
562 urb->complete = isoc_irq;
563 urb->number_of_packets = npkt;
564 for (i = 0; i < npkt; i++) {
565 urb->iso_frame_desc[i].length = psize;
566 urb->iso_frame_desc[i].offset = psize * i;
567 }
568 } else { /* bulk */
569 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
570 ep->desc.bEndpointAddress),
Frank Zago6c49da72008-09-28 07:43:00 -0300571 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300572 urb->complete = bulk_irq;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300573 }
574 }
575 return 0;
576}
577
578/*
579 * start the USB transfer
580 */
581static int gspca_init_transfer(struct gspca_dev *gspca_dev)
582{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300583 struct usb_host_endpoint *ep;
584 int n, ret;
585
Hans de Goedee2997a72008-04-23 08:09:12 -0300586 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
587 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300588
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300589 /* set the higher alternate setting and
590 * loop until urb submit succeeds */
591 gspca_dev->alt = gspca_dev->nbalt;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300592 for (;;) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300593 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
Jean-Francois Moine0be01002008-09-04 07:01:50 -0300594 ep = get_ep(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300595 if (ep == NULL) {
596 ret = -EIO;
597 goto out;
598 }
599 ret = create_urbs(gspca_dev, ep);
600 if (ret < 0)
601 goto out;
602
Jean-Francois Moine8fe2f1d2008-10-22 14:51:56 -0300603 /* clear the bulk endpoint */
604 if (gspca_dev->alt == 0) /* if bulk transfer */
605 usb_clear_halt(gspca_dev->dev,
606 usb_rcvintpipe(gspca_dev->dev,
607 gspca_dev->cam.epaddr));
608
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300609 /* start the cam */
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300610 ret = gspca_dev->sd_desc->start(gspca_dev);
611 if (ret < 0) {
612 destroy_urbs(gspca_dev);
613 goto out;
614 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300615 gspca_dev->streaming = 1;
616 atomic_set(&gspca_dev->nevent, 0);
617
Frank Zago219423c2008-09-26 07:43:54 -0300618 /* bulk transfers are started by the subdriver */
Jean-Francois Moine95d91422008-09-29 05:57:32 -0300619 if (gspca_dev->alt == 0)
Jean-Francois Moine51977a82008-09-08 03:22:42 -0300620 break;
621
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300622 /* submit the URBs */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300623 for (n = 0; n < gspca_dev->nurbs; n++) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300624 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300625 if (ret < 0) {
626 PDEBUG(D_ERR|D_STREAM,
627 "usb_submit_urb [%d] err %d", n, ret);
Hans de Goedee2997a72008-04-23 08:09:12 -0300628 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300629 destroy_urbs(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300630 if (ret == -ENOSPC)
631 break; /* try the previous alt */
632 goto out;
633 }
634 }
635 if (ret >= 0)
636 break;
637 }
638out:
639 mutex_unlock(&gspca_dev->usb_lock);
640 return ret;
641}
642
643static int gspca_set_alt0(struct gspca_dev *gspca_dev)
644{
645 int ret;
646
647 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
648 if (ret < 0)
649 PDEBUG(D_ERR|D_STREAM, "set interface 0 err %d", ret);
650 return ret;
651}
652
Frank Zago219423c2008-09-26 07:43:54 -0300653/* Note: both the queue and the usb locks should be held when calling this */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300654static void gspca_stream_off(struct gspca_dev *gspca_dev)
655{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300656 gspca_dev->streaming = 0;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300657 atomic_set(&gspca_dev->nevent, 0);
Jean-Francois Moine98522a72008-11-18 06:33:08 -0300658 if (gspca_dev->present
659 && gspca_dev->sd_desc->stopN)
660 gspca_dev->sd_desc->stopN(gspca_dev);
661 destroy_urbs(gspca_dev);
662 gspca_set_alt0(gspca_dev);
663 if (gspca_dev->sd_desc->stop0)
664 gspca_dev->sd_desc->stop0(gspca_dev);
665 PDEBUG(D_STREAM, "stream off OK");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300666}
667
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300668static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300669{
670 int i;
671
672 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
673 gspca_dev->curr_mode = i;
674 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
675 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300676 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300677}
678
679static int wxh_to_mode(struct gspca_dev *gspca_dev,
680 int width, int height)
681{
682 int i;
683
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300684 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
685 if (width >= gspca_dev->cam.cam_mode[i].width
686 && height >= gspca_dev->cam.cam_mode[i].height)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300687 break;
688 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300689 return i;
690}
691
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300692/*
693 * search a mode with the right pixel format
694 */
695static int gspca_get_mode(struct gspca_dev *gspca_dev,
696 int mode,
697 int pixfmt)
698{
699 int modeU, modeD;
700
701 modeU = modeD = mode;
702 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
703 if (--modeD >= 0) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300704 if (gspca_dev->cam.cam_mode[modeD].pixelformat
705 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300706 return modeD;
707 }
708 if (++modeU < gspca_dev->cam.nmodes) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300709 if (gspca_dev->cam.cam_mode[modeU].pixelformat
710 == pixfmt)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300711 return modeU;
712 }
713 }
714 return -EINVAL;
715}
716
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300717static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300718 struct v4l2_fmtdesc *fmtdesc)
719{
720 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300721 int i, j, index;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300722 __u32 fmt_tb[8];
723
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300724 /* give an index to each format */
725 index = 0;
726 j = 0;
727 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300728 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300729 j = 0;
730 for (;;) {
731 if (fmt_tb[j] == fmt_tb[index])
732 break;
733 j++;
734 }
735 if (j == index) {
736 if (fmtdesc->index == index)
737 break; /* new format */
738 index++;
Julia Lawall80297122008-11-12 23:18:21 -0300739 if (index >= ARRAY_SIZE(fmt_tb))
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300740 return -EINVAL;
741 }
742 }
743 if (i < 0)
744 return -EINVAL; /* no more format */
745
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300746 fmtdesc->pixelformat = fmt_tb[index];
747 if (gspca_is_compressed(fmt_tb[index]))
748 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300749 fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300750 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
751 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
752 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
753 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
754 fmtdesc->description[4] = '\0';
755 return 0;
756}
757
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300758static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300759 struct v4l2_format *fmt)
760{
761 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300762 int mode;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300763
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300764 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
765 return -EINVAL;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300766 mode = gspca_dev->curr_mode;
767 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
768 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300769 return 0;
770}
771
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300772static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300773 struct v4l2_format *fmt)
774{
Hans de Goede24c530b2008-07-04 17:55:18 -0300775 int w, h, mode, mode2;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300776
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300777 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
778 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300779 w = fmt->fmt.pix.width;
780 h = fmt->fmt.pix.height;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300781
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300782#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300783 if (gspca_debug & D_CONF)
784 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
785#endif
786 /* search the closest mode for width and height */
787 mode = wxh_to_mode(gspca_dev, w, h);
788
789 /* OK if right palette */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300790 if (gspca_dev->cam.cam_mode[mode].pixelformat
791 != fmt->fmt.pix.pixelformat) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300792
793 /* else, search the closest mode with the same pixel format */
794 mode2 = gspca_get_mode(gspca_dev, mode,
795 fmt->fmt.pix.pixelformat);
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300796 if (mode2 >= 0)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300797 mode = mode2;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300798/* else
799 ; * no chance, return this mode */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300800 }
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300801 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
802 sizeof fmt->fmt.pix);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300803 return mode; /* used when s_fmt */
804}
805
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300806static int vidioc_try_fmt_vid_cap(struct file *file,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300807 void *priv,
808 struct v4l2_format *fmt)
809{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300810 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300811 int ret;
812
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300813 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300814 if (ret < 0)
815 return ret;
816 return 0;
817}
818
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300819static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300820 struct v4l2_format *fmt)
821{
822 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300823 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300824
Hans de Goedee2997a72008-04-23 08:09:12 -0300825 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
826 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300827
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -0300828 ret = try_fmt_vid_cap(gspca_dev, fmt);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300829 if (ret < 0)
830 goto out;
831
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300832 if (gspca_dev->nframes != 0
833 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
834 ret = -EINVAL;
835 goto out;
836 }
837
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300838 if (ret == gspca_dev->curr_mode) {
839 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300840 goto out; /* same mode */
Jean-Francois Moine50a871f2008-06-30 19:47:33 -0300841 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300842
843 if (gspca_dev->streaming) {
844 ret = -EBUSY;
845 goto out;
Hans de Goedee2997a72008-04-23 08:09:12 -0300846 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300847 gspca_dev->width = fmt->fmt.pix.width;
848 gspca_dev->height = fmt->fmt.pix.height;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300849 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
850 gspca_dev->curr_mode = ret;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300851
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300852 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300853out:
854 mutex_unlock(&gspca_dev->queue_lock);
855 return ret;
856}
857
Frank Zago6b060ff2008-09-28 08:12:22 -0300858static void gspca_delete(struct kref *kref)
859{
860 struct gspca_dev *gspca_dev = container_of(kref, struct gspca_dev, kref);
861
862 PDEBUG(D_STREAM, "device deleted");
863
864 kfree(gspca_dev->usb_buf);
865 kfree(gspca_dev);
866}
867
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300868static int dev_open(struct inode *inode, struct file *file)
869{
870 struct gspca_dev *gspca_dev;
871 int ret;
872
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300873 PDEBUG(D_STREAM, "%s open", current->comm);
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -0300874 gspca_dev = (struct gspca_dev *) video_devdata(file);
Hans de Goedee2997a72008-04-23 08:09:12 -0300875 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
876 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300877 if (!gspca_dev->present) {
878 ret = -ENODEV;
879 goto out;
880 }
881
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300882 if (gspca_dev->users > 4) { /* (arbitrary value) */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300883 ret = -EBUSY;
884 goto out;
885 }
Jean-Francois Moine5c4fa002008-11-18 15:52:31 -0300886
887 /* protect the subdriver against rmmod */
888 if (!try_module_get(gspca_dev->module)) {
889 ret = -ENODEV;
890 goto out;
891 }
892
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300893 gspca_dev->users++;
Frank Zago6b060ff2008-09-28 08:12:22 -0300894
895 /* one more user */
896 kref_get(&gspca_dev->kref);
897
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300898 file->private_data = gspca_dev;
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300899#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300900 /* activate the v4l2 debug */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300901 if (gspca_debug & D_V4L2)
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -0300902 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -0300903 | V4L2_DEBUG_IOCTL_ARG;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300904 else
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -0300905 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
Frank Zago9de436c2008-09-15 05:20:38 -0300906 | V4L2_DEBUG_IOCTL_ARG);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300907#endif
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300908 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300909out:
910 mutex_unlock(&gspca_dev->queue_lock);
911 if (ret != 0)
912 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
913 else
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300914 PDEBUG(D_STREAM, "open done");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300915 return ret;
916}
917
918static int dev_close(struct inode *inode, struct file *file)
919{
920 struct gspca_dev *gspca_dev = file->private_data;
921
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300922 PDEBUG(D_STREAM, "%s close", current->comm);
Hans de Goedee2997a72008-04-23 08:09:12 -0300923 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
924 return -ERESTARTSYS;
925 gspca_dev->users--;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300926
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -0300927 /* if the file did the capture, free the streaming resources */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300928 if (gspca_dev->capt_file == file) {
Jean-Francois Moinea1e82882008-09-03 17:12:17 -0300929 if (gspca_dev->streaming) {
930 mutex_lock(&gspca_dev->usb_lock);
931 gspca_stream_off(gspca_dev);
932 mutex_unlock(&gspca_dev->usb_lock);
933 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -0300934 frame_free(gspca_dev);
Harvey Harrisonefab8212008-07-05 06:12:47 -0300935 gspca_dev->capt_file = NULL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300936 gspca_dev->memory = GSPCA_MEMORY_NO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300937 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300938 file->private_data = NULL;
Jean-Francois Moine5c4fa002008-11-18 15:52:31 -0300939 module_put(gspca_dev->module);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300940 mutex_unlock(&gspca_dev->queue_lock);
Frank Zago6b060ff2008-09-28 08:12:22 -0300941
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300942 PDEBUG(D_STREAM, "close done");
Frank Zago6b060ff2008-09-28 08:12:22 -0300943
944 kref_put(&gspca_dev->kref, gspca_delete);
945
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300946 return 0;
947}
948
949static int vidioc_querycap(struct file *file, void *priv,
950 struct v4l2_capability *cap)
951{
952 struct gspca_dev *gspca_dev = priv;
953
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300954 memset(cap, 0, sizeof *cap);
955 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
Jean-Francois Moinec6edaf12008-07-23 03:24:06 -0300956 if (gspca_dev->dev->product != NULL) {
957 strncpy(cap->card, gspca_dev->dev->product,
958 sizeof cap->card);
959 } else {
960 snprintf(cap->card, sizeof cap->card,
961 "USB Camera (%04x:%04x)",
962 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
963 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
964 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300965 strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name,
966 sizeof cap->bus_info);
967 cap->version = DRIVER_VERSION_NUMBER;
968 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
969 | V4L2_CAP_STREAMING
970 | V4L2_CAP_READWRITE;
971 return 0;
972}
973
974static int vidioc_queryctrl(struct file *file, void *priv,
975 struct v4l2_queryctrl *q_ctrl)
976{
977 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300978 int i, ix;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300979 u32 id;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300980
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300981 ix = -1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300982 id = q_ctrl->id;
983 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
984 id &= V4L2_CTRL_ID_MASK;
985 id++;
986 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Hans de Goede0f523c22008-09-03 17:12:14 -0300987 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300988 continue;
989 if (ix < 0) {
990 ix = i;
991 continue;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300992 }
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -0300993 if (gspca_dev->sd_desc->ctrls[i].qctrl.id
994 > gspca_dev->sd_desc->ctrls[ix].qctrl.id)
995 continue;
996 ix = i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300997 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300998 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300999 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001000 if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) {
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001001 ix = i;
1002 break;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001003 }
1004 }
Jean-Francois Moinef50ba1b2008-09-03 17:12:14 -03001005 if (ix < 0)
1006 return -EINVAL;
1007 memcpy(q_ctrl, &gspca_dev->sd_desc->ctrls[ix].qctrl,
1008 sizeof *q_ctrl);
1009 if (gspca_dev->ctrl_dis & (1 << ix))
1010 q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
1011 return 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001012}
1013
1014static int vidioc_s_ctrl(struct file *file, void *priv,
1015 struct v4l2_control *ctrl)
1016{
1017 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001018 const struct ctrl *ctrls;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001019 int i, ret;
1020
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001021 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1022 i < gspca_dev->sd_desc->nctrls;
1023 i++, ctrls++) {
1024 if (ctrl->id != ctrls->qctrl.id)
1025 continue;
Hans de Goede0f523c22008-09-03 17:12:14 -03001026 if (gspca_dev->ctrl_dis & (1 << i))
1027 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001028 if (ctrl->value < ctrls->qctrl.minimum
Rabin Vincentfcf5cb22008-08-03 07:58:54 -03001029 || ctrl->value > ctrls->qctrl.maximum)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001030 return -ERANGE;
1031 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
Hans de Goedee2997a72008-04-23 08:09:12 -03001032 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1033 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001034 ret = ctrls->set(gspca_dev, ctrl->value);
1035 mutex_unlock(&gspca_dev->usb_lock);
1036 return ret;
1037 }
1038 return -EINVAL;
1039}
1040
1041static int vidioc_g_ctrl(struct file *file, void *priv,
1042 struct v4l2_control *ctrl)
1043{
1044 struct gspca_dev *gspca_dev = priv;
1045
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001046 const struct ctrl *ctrls;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001047 int i, ret;
1048
1049 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1050 i < gspca_dev->sd_desc->nctrls;
1051 i++, ctrls++) {
1052 if (ctrl->id != ctrls->qctrl.id)
1053 continue;
Hans de Goede0f523c22008-09-03 17:12:14 -03001054 if (gspca_dev->ctrl_dis & (1 << i))
1055 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001056 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1057 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001058 ret = ctrls->get(gspca_dev, &ctrl->value);
1059 mutex_unlock(&gspca_dev->usb_lock);
1060 return ret;
1061 }
1062 return -EINVAL;
1063}
1064
1065static int vidioc_querymenu(struct file *file, void *priv,
1066 struct v4l2_querymenu *qmenu)
1067{
1068 struct gspca_dev *gspca_dev = priv;
1069
1070 if (!gspca_dev->sd_desc->querymenu)
1071 return -EINVAL;
1072 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1073}
1074
1075static int vidioc_enum_input(struct file *file, void *priv,
1076 struct v4l2_input *input)
1077{
1078 struct gspca_dev *gspca_dev = priv;
1079
1080 if (input->index != 0)
1081 return -EINVAL;
1082 memset(input, 0, sizeof *input);
1083 input->type = V4L2_INPUT_TYPE_CAMERA;
1084 strncpy(input->name, gspca_dev->sd_desc->name,
1085 sizeof input->name);
1086 return 0;
1087}
1088
1089static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1090{
1091 *i = 0;
1092 return 0;
1093}
1094
1095static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1096{
1097 if (i > 0)
1098 return -EINVAL;
1099 return (0);
1100}
1101
1102static int vidioc_reqbufs(struct file *file, void *priv,
1103 struct v4l2_requestbuffers *rb)
1104{
1105 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001106 int i, ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001107
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001108 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1109 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001110 switch (rb->memory) {
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001111 case GSPCA_MEMORY_READ: /* (internal call) */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001112 case V4L2_MEMORY_MMAP:
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001113 case V4L2_MEMORY_USERPTR:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001114 break;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001115 default:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001116 return -EINVAL;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001117 }
Hans de Goedee2997a72008-04-23 08:09:12 -03001118 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1119 return -ERESTARTSYS;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001120
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001121 if (gspca_dev->memory != GSPCA_MEMORY_NO
1122 && gspca_dev->memory != rb->memory) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001123 ret = -EBUSY;
1124 goto out;
1125 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001126
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001127 /* only one file may do the capture */
1128 if (gspca_dev->capt_file != NULL
1129 && gspca_dev->capt_file != file) {
1130 ret = -EBUSY;
1131 goto out;
1132 }
1133
1134 /* if allocated, the buffers must not be mapped */
1135 for (i = 0; i < gspca_dev->nframes; i++) {
1136 if (gspca_dev->frame[i].vma_use_count) {
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001137 ret = -EBUSY;
1138 goto out;
1139 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001140 }
1141
1142 /* stop streaming */
1143 if (gspca_dev->streaming) {
1144 mutex_lock(&gspca_dev->usb_lock);
1145 gspca_stream_off(gspca_dev);
1146 mutex_unlock(&gspca_dev->usb_lock);
1147 }
1148
1149 /* free the previous allocated buffers, if any */
1150 if (gspca_dev->nframes != 0) {
1151 frame_free(gspca_dev);
1152 gspca_dev->capt_file = NULL;
1153 }
1154 if (rb->count == 0) /* unrequest */
1155 goto out;
1156 gspca_dev->memory = rb->memory;
1157 ret = frame_alloc(gspca_dev, rb->count);
1158 if (ret == 0) {
1159 rb->count = gspca_dev->nframes;
1160 gspca_dev->capt_file = file;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001161 }
1162out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001163 mutex_unlock(&gspca_dev->queue_lock);
1164 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1165 return ret;
1166}
1167
1168static int vidioc_querybuf(struct file *file, void *priv,
1169 struct v4l2_buffer *v4l2_buf)
1170{
1171 struct gspca_dev *gspca_dev = priv;
1172 struct gspca_frame *frame;
1173
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001174 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1175 || v4l2_buf->index < 0
1176 || v4l2_buf->index >= gspca_dev->nframes)
1177 return -EINVAL;
1178
1179 frame = &gspca_dev->frame[v4l2_buf->index];
1180 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1181 return 0;
1182}
1183
1184static int vidioc_streamon(struct file *file, void *priv,
1185 enum v4l2_buf_type buf_type)
1186{
1187 struct gspca_dev *gspca_dev = priv;
1188 int ret;
1189
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001190 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1191 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001192 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1193 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001194 if (!gspca_dev->present) {
1195 ret = -ENODEV;
1196 goto out;
1197 }
1198 if (gspca_dev->nframes == 0) {
1199 ret = -EINVAL;
1200 goto out;
1201 }
1202 if (!gspca_dev->streaming) {
1203 ret = gspca_init_transfer(gspca_dev);
1204 if (ret < 0)
1205 goto out;
1206 }
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03001207#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001208 if (gspca_debug & D_STREAM) {
1209 PDEBUG_MODE("stream on OK",
1210 gspca_dev->pixfmt,
1211 gspca_dev->width,
1212 gspca_dev->height);
1213 }
1214#endif
Hans de Goedee2997a72008-04-23 08:09:12 -03001215 ret = 0;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001216out:
1217 mutex_unlock(&gspca_dev->queue_lock);
1218 return ret;
1219}
1220
1221static int vidioc_streamoff(struct file *file, void *priv,
1222 enum v4l2_buf_type buf_type)
1223{
1224 struct gspca_dev *gspca_dev = priv;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001225 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001226
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001227 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1228 return -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001229 if (!gspca_dev->streaming)
1230 return 0;
1231 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1232 return -ERESTARTSYS;
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001233
1234 /* stop streaming */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001235 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1236 ret = -ERESTARTSYS;
1237 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001238 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001239 gspca_stream_off(gspca_dev);
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001240 mutex_unlock(&gspca_dev->usb_lock);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001241
1242 /* empty the application queues */
1243 for (i = 0; i < gspca_dev->nframes; i++)
1244 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1245 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1246 gspca_dev->last_packet_type = DISCARD_PACKET;
1247 gspca_dev->sequence = 0;
1248 atomic_set(&gspca_dev->nevent, 0);
1249 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001250out:
1251 mutex_unlock(&gspca_dev->queue_lock);
1252 return ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001253}
1254
1255static int vidioc_g_jpegcomp(struct file *file, void *priv,
1256 struct v4l2_jpegcompression *jpegcomp)
1257{
1258 struct gspca_dev *gspca_dev = priv;
1259 int ret;
1260
1261 if (!gspca_dev->sd_desc->get_jcomp)
1262 return -EINVAL;
Hans de Goedee2997a72008-04-23 08:09:12 -03001263 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1264 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001265 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1266 mutex_unlock(&gspca_dev->usb_lock);
1267 return ret;
1268}
1269
1270static int vidioc_s_jpegcomp(struct file *file, void *priv,
1271 struct v4l2_jpegcompression *jpegcomp)
1272{
1273 struct gspca_dev *gspca_dev = priv;
1274 int ret;
1275
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001276 if (!gspca_dev->sd_desc->set_jcomp)
1277 return -EINVAL;
Jim Parisf86d4a92008-12-10 05:50:44 -03001278 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1279 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001280 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1281 mutex_unlock(&gspca_dev->usb_lock);
1282 return ret;
1283}
1284
1285static int vidioc_g_parm(struct file *filp, void *priv,
1286 struct v4l2_streamparm *parm)
1287{
1288 struct gspca_dev *gspca_dev = priv;
1289
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001290 memset(parm, 0, sizeof *parm);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001291 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1292 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1293 return 0;
1294}
1295
1296static int vidioc_s_parm(struct file *filp, void *priv,
1297 struct v4l2_streamparm *parm)
1298{
1299 struct gspca_dev *gspca_dev = priv;
1300 int n;
1301
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001302 n = parm->parm.capture.readbuffers;
1303 if (n == 0 || n > GSPCA_MAX_FRAMES)
1304 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1305 else
1306 gspca_dev->nbufread = n;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001307 return 0;
1308}
1309
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001310static int vidioc_s_std(struct file *filp, void *priv,
1311 v4l2_std_id *parm)
1312{
1313 return 0;
1314}
1315
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001316#ifdef CONFIG_VIDEO_V4L1_COMPAT
1317static int vidiocgmbuf(struct file *file, void *priv,
1318 struct video_mbuf *mbuf)
1319{
1320 struct gspca_dev *gspca_dev = file->private_data;
1321 int i;
1322
1323 PDEBUG(D_STREAM, "cgmbuf");
1324 if (gspca_dev->nframes == 0) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001325 int ret;
1326
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001327 {
1328 struct v4l2_format fmt;
1329
1330 memset(&fmt, 0, sizeof fmt);
1331 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1332 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1333 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1334 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1335 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001336 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001337 if (ret != 0)
1338 return ret;
1339 }
1340 {
1341 struct v4l2_requestbuffers rb;
1342
1343 memset(&rb, 0, sizeof rb);
1344 rb.count = 4;
1345 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1346 rb.memory = V4L2_MEMORY_MMAP;
1347 ret = vidioc_reqbufs(file, priv, &rb);
1348 if (ret != 0)
1349 return ret;
1350 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001351 }
1352 mbuf->frames = gspca_dev->nframes;
1353 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1354 for (i = 0; i < mbuf->frames; i++)
1355 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1356 return 0;
1357}
1358#endif
1359
1360static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1361{
1362 struct gspca_dev *gspca_dev = file->private_data;
Harvey Harrisonefab8212008-07-05 06:12:47 -03001363 struct gspca_frame *frame;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001364 struct page *page;
1365 unsigned long addr, start, size;
1366 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001367
1368 start = vma->vm_start;
1369 size = vma->vm_end - vma->vm_start;
1370 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1371
Hans de Goedee2997a72008-04-23 08:09:12 -03001372 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1373 return -ERESTARTSYS;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001374 if (!gspca_dev->present) {
1375 ret = -ENODEV;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001376 goto out;
1377 }
1378 if (gspca_dev->capt_file != file) {
1379 ret = -EINVAL;
1380 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001381 }
1382
Harvey Harrisonefab8212008-07-05 06:12:47 -03001383 frame = NULL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001384 for (i = 0; i < gspca_dev->nframes; ++i) {
1385 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1386 PDEBUG(D_STREAM, "mmap bad memory type");
1387 break;
1388 }
1389 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1390 == vma->vm_pgoff) {
1391 frame = &gspca_dev->frame[i];
1392 break;
1393 }
1394 }
Harvey Harrisonefab8212008-07-05 06:12:47 -03001395 if (frame == NULL) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001396 PDEBUG(D_STREAM, "mmap no frame buffer found");
1397 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001398 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001399 }
1400#ifdef CONFIG_VIDEO_V4L1_COMPAT
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001401 /* v4l1 maps all the buffers */
1402 if (i != 0
1403 || size != frame->v4l2_buf.length * gspca_dev->nframes)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001404#endif
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001405 if (size != frame->v4l2_buf.length) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001406 PDEBUG(D_STREAM, "mmap bad size");
1407 ret = -EINVAL;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001408 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001409 }
1410
1411 /*
1412 * - VM_IO marks the area as being a mmaped region for I/O to a
1413 * device. It also prevents the region from being core dumped.
1414 */
1415 vma->vm_flags |= VM_IO;
1416
1417 addr = (unsigned long) frame->data;
1418 while (size > 0) {
1419 page = vmalloc_to_page((void *) addr);
1420 ret = vm_insert_page(vma, start, page);
1421 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001422 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001423 start += PAGE_SIZE;
1424 addr += PAGE_SIZE;
1425 size -= PAGE_SIZE;
1426 }
1427
1428 vma->vm_ops = &gspca_vm_ops;
1429 vma->vm_private_data = frame;
1430 gspca_vm_open(vma);
Hans de Goedee2997a72008-04-23 08:09:12 -03001431 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001432out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001433 mutex_unlock(&gspca_dev->queue_lock);
1434 return ret;
1435}
1436
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001437/*
1438 * wait for a video frame
1439 *
1440 * If a frame is ready, its index is returned.
1441 */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001442static int frame_wait(struct gspca_dev *gspca_dev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001443 int nonblock_ing)
1444{
1445 struct gspca_frame *frame;
1446 int i, j, ret;
1447
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001448 /* check if a frame is ready */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001449 i = gspca_dev->fr_o;
1450 j = gspca_dev->fr_queue[i];
1451 frame = &gspca_dev->frame[j];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001452 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) {
1453 atomic_dec(&gspca_dev->nevent);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001454 goto ok;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001455 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001456 if (nonblock_ing) /* no frame yet */
1457 return -EAGAIN;
1458
1459 /* wait till a frame is ready */
1460 for (;;) {
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001461 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1462 atomic_read(&gspca_dev->nevent) > 0,
1463 msecs_to_jiffies(3000));
1464 if (ret <= 0) {
1465 if (ret < 0)
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001466 return ret; /* interrupt */
1467 return -EIO; /* timeout */
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001468 }
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001469 atomic_dec(&gspca_dev->nevent);
Hans de Goedee2997a72008-04-23 08:09:12 -03001470 if (!gspca_dev->streaming || !gspca_dev->present)
1471 return -EIO;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001472 i = gspca_dev->fr_o;
1473 j = gspca_dev->fr_queue[i];
1474 frame = &gspca_dev->frame[j];
1475 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1476 break;
1477 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001478ok:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001479 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1480 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1481 gspca_dev->fr_q,
1482 gspca_dev->fr_i,
1483 gspca_dev->fr_o);
Hans de Goedee2997a72008-04-23 08:09:12 -03001484
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001485 if (gspca_dev->sd_desc->dq_callback) {
1486 mutex_lock(&gspca_dev->usb_lock);
Hans de Goedee2997a72008-04-23 08:09:12 -03001487 gspca_dev->sd_desc->dq_callback(gspca_dev);
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001488 mutex_unlock(&gspca_dev->usb_lock);
1489 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001490 return j;
1491}
1492
1493/*
1494 * dequeue a video buffer
1495 *
1496 * If nonblock_ing is false, block until a buffer is available.
1497 */
1498static int vidioc_dqbuf(struct file *file, void *priv,
1499 struct v4l2_buffer *v4l2_buf)
1500{
1501 struct gspca_dev *gspca_dev = priv;
1502 struct gspca_frame *frame;
1503 int i, ret;
1504
1505 PDEBUG(D_FRAM, "dqbuf");
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001506 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1507 return -EINVAL;
1508 if (v4l2_buf->memory != gspca_dev->memory)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001509 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001510
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001511 /* if not streaming, be sure the application will not loop forever */
1512 if (!(file->f_flags & O_NONBLOCK)
1513 && !gspca_dev->streaming && gspca_dev->users == 1)
1514 return -EINVAL;
1515
1516 /* only the capturing file may dequeue */
1517 if (gspca_dev->capt_file != file)
1518 return -EINVAL;
1519
1520 /* only one dequeue / read at a time */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001521 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1522 return -ERESTARTSYS;
1523
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001524 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001525 if (ret < 0)
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001526 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001527 i = ret; /* frame index */
1528 frame = &gspca_dev->frame[i];
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001529 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001530 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001531 frame->data,
1532 frame->v4l2_buf.bytesused)) {
1533 PDEBUG(D_ERR|D_STREAM,
1534 "dqbuf cp to user failed");
1535 ret = -EFAULT;
1536 goto out;
1537 }
1538 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001539 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1540 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1541 PDEBUG(D_FRAM, "dqbuf %d", i);
1542 ret = 0;
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001543out:
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001544 mutex_unlock(&gspca_dev->read_lock);
1545 return ret;
1546}
1547
1548/*
1549 * queue a video buffer
1550 *
1551 * Attempting to queue a buffer that has already been
1552 * queued will return -EINVAL.
1553 */
1554static int vidioc_qbuf(struct file *file, void *priv,
1555 struct v4l2_buffer *v4l2_buf)
1556{
1557 struct gspca_dev *gspca_dev = priv;
1558 struct gspca_frame *frame;
1559 int i, index, ret;
1560
1561 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1562 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1563 return -EINVAL;
1564
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001565 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1566 return -ERESTARTSYS;
1567
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001568 index = v4l2_buf->index;
1569 if ((unsigned) index >= gspca_dev->nframes) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001570 PDEBUG(D_FRAM,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001571 "qbuf idx %d >= %d", index, gspca_dev->nframes);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001572 ret = -EINVAL;
1573 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001574 }
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001575 if (v4l2_buf->memory != gspca_dev->memory) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001576 PDEBUG(D_FRAM, "qbuf bad memory type");
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001577 ret = -EINVAL;
1578 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001579 }
1580
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001581 frame = &gspca_dev->frame[index];
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001582 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1583 PDEBUG(D_FRAM, "qbuf bad state");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001584 ret = -EINVAL;
1585 goto out;
1586 }
1587
1588 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001589
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001590 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001591 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1592 frame->v4l2_buf.length = v4l2_buf->length;
1593 }
1594
1595 /* put the buffer in the 'queued' queue */
1596 i = gspca_dev->fr_q;
1597 gspca_dev->fr_queue[i] = index;
1598 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1599 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1600 gspca_dev->fr_q,
1601 gspca_dev->fr_i,
1602 gspca_dev->fr_o);
1603
1604 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1605 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1606 ret = 0;
1607out:
1608 mutex_unlock(&gspca_dev->queue_lock);
1609 return ret;
1610}
1611
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001612/*
1613 * allocate the resources for read()
1614 */
1615static int read_alloc(struct gspca_dev *gspca_dev,
1616 struct file *file)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001617{
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001618 struct v4l2_buffer v4l2_buf;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001619 int i, ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001620
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001621 PDEBUG(D_STREAM, "read alloc");
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001622 if (gspca_dev->nframes == 0) {
1623 struct v4l2_requestbuffers rb;
1624
1625 memset(&rb, 0, sizeof rb);
1626 rb.count = gspca_dev->nbufread;
1627 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001628 rb.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001629 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1630 if (ret != 0) {
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001631 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001632 return ret;
1633 }
1634 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1635 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001636 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001637 for (i = 0; i < gspca_dev->nbufread; i++) {
1638 v4l2_buf.index = i;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001639 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1640 if (ret != 0) {
1641 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1642 return ret;
1643 }
1644 }
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001645 gspca_dev->memory = GSPCA_MEMORY_READ;
1646 }
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001647
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001648 /* start streaming */
1649 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1650 if (ret != 0)
1651 PDEBUG(D_STREAM, "read streamon err %d", ret);
1652 return ret;
1653}
1654
1655static unsigned int dev_poll(struct file *file, poll_table *wait)
1656{
1657 struct gspca_dev *gspca_dev = file->private_data;
1658 int i, ret;
1659
1660 PDEBUG(D_FRAM, "poll");
1661
1662 poll_wait(file, &gspca_dev->wq, wait);
1663 if (!gspca_dev->present)
1664 return POLLERR;
1665
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001666 /* if reqbufs is not done, the user would use read() */
1667 if (gspca_dev->nframes == 0) {
1668 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1669 return POLLERR; /* not the 1st time */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001670 ret = read_alloc(gspca_dev, file);
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001671 if (ret != 0)
1672 return POLLERR;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001673 }
1674
1675 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1676 return POLLERR;
1677 if (!gspca_dev->present) {
1678 ret = POLLERR;
1679 goto out;
1680 }
1681
Jean-Francois Moine5b77ae72008-07-12 09:28:04 -03001682 /* check the next incoming buffer */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001683 i = gspca_dev->fr_o;
1684 i = gspca_dev->fr_queue[i];
1685 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1686 ret = POLLIN | POLLRDNORM; /* something to read */
1687 else
1688 ret = 0;
1689out:
1690 mutex_unlock(&gspca_dev->queue_lock);
1691 return ret;
1692}
1693
1694static ssize_t dev_read(struct file *file, char __user *data,
1695 size_t count, loff_t *ppos)
1696{
1697 struct gspca_dev *gspca_dev = file->private_data;
1698 struct gspca_frame *frame;
1699 struct v4l2_buffer v4l2_buf;
1700 struct timeval timestamp;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001701 int n, ret, ret2;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001702
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001703 PDEBUG(D_FRAM, "read (%zd)", count);
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001704 if (!gspca_dev->present)
1705 return -ENODEV;
1706 switch (gspca_dev->memory) {
1707 case GSPCA_MEMORY_NO: /* first time */
1708 ret = read_alloc(gspca_dev, file);
1709 if (ret != 0)
1710 return ret;
1711 break;
1712 case GSPCA_MEMORY_READ:
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001713 if (gspca_dev->capt_file == file)
1714 break;
1715 /* fall thru */
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001716 default:
1717 return -EINVAL;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001718 }
1719
1720 /* get a frame */
1721 jiffies_to_timeval(get_jiffies_64(), &timestamp);
1722 timestamp.tv_sec--;
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001723 n = 2;
1724 for (;;) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001725 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1726 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001727 v4l2_buf.memory = GSPCA_MEMORY_READ;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001728 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1729 if (ret != 0) {
1730 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1731 return ret;
1732 }
1733
1734 /* if the process slept for more than 1 second,
Frank Zago219423c2008-09-26 07:43:54 -03001735 * get a newer frame */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001736 frame = &gspca_dev->frame[v4l2_buf.index];
Jean-Francois Moine82643d02008-07-02 09:14:56 -03001737 if (--n < 0)
1738 break; /* avoid infinite loop */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001739 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1740 break;
1741 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1742 if (ret != 0) {
1743 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1744 return ret;
1745 }
1746 }
1747
1748 /* copy the frame */
Jean-Francois Moineac0d6df2008-07-08 06:58:15 -03001749 if (count > frame->v4l2_buf.bytesused)
1750 count = frame->v4l2_buf.bytesused;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001751 ret = copy_to_user(data, frame->data, count);
1752 if (ret != 0) {
1753 PDEBUG(D_ERR|D_STREAM,
Mauro Carvalho Chehaba9e11132008-07-09 05:26:26 -03001754 "read cp to user lack %d / %zd", ret, count);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001755 ret = -EFAULT;
1756 goto out;
1757 }
1758 ret = count;
1759out:
1760 /* in each case, requeue the buffer */
1761 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1762 if (ret2 != 0)
1763 return ret2;
1764 return ret;
1765}
1766
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001767static struct file_operations dev_fops = {
1768 .owner = THIS_MODULE,
1769 .open = dev_open,
1770 .release = dev_close,
1771 .read = dev_read,
1772 .mmap = dev_mmap,
1773 .ioctl = video_ioctl2,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001774#ifdef CONFIG_COMPAT
1775 .compat_ioctl = v4l_compat_ioctl32,
1776#endif
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001777 .llseek = no_llseek,
1778 .poll = dev_poll,
1779};
1780
Hans Verkuila3998102008-07-21 02:57:38 -03001781static const struct v4l2_ioctl_ops dev_ioctl_ops = {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001782 .vidioc_querycap = vidioc_querycap,
1783 .vidioc_dqbuf = vidioc_dqbuf,
1784 .vidioc_qbuf = vidioc_qbuf,
Mauro Carvalho Chehabe0787702008-07-01 04:06:22 -03001785 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1786 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1787 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1788 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001789 .vidioc_streamon = vidioc_streamon,
1790 .vidioc_queryctrl = vidioc_queryctrl,
1791 .vidioc_g_ctrl = vidioc_g_ctrl,
1792 .vidioc_s_ctrl = vidioc_s_ctrl,
1793 .vidioc_querymenu = vidioc_querymenu,
1794 .vidioc_enum_input = vidioc_enum_input,
1795 .vidioc_g_input = vidioc_g_input,
1796 .vidioc_s_input = vidioc_s_input,
1797 .vidioc_reqbufs = vidioc_reqbufs,
1798 .vidioc_querybuf = vidioc_querybuf,
1799 .vidioc_streamoff = vidioc_streamoff,
1800 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1801 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1802 .vidioc_g_parm = vidioc_g_parm,
1803 .vidioc_s_parm = vidioc_s_parm,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001804 .vidioc_s_std = vidioc_s_std,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001805#ifdef CONFIG_VIDEO_V4L1_COMPAT
1806 .vidiocgmbuf = vidiocgmbuf,
1807#endif
1808};
1809
Hans Verkuila3998102008-07-21 02:57:38 -03001810static struct video_device gspca_template = {
1811 .name = "gspca main driver",
Hans Verkuila3998102008-07-21 02:57:38 -03001812 .fops = &dev_fops,
1813 .ioctl_ops = &dev_ioctl_ops,
Jean-Francois Moinee07a1d82008-11-19 06:37:53 -03001814 .release = video_device_release,
Hans Verkuila3998102008-07-21 02:57:38 -03001815 .minor = -1,
1816};
1817
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001818/*
1819 * probe and create a new gspca device
1820 *
1821 * This function must be called by the sub-driver when it is
1822 * called for probing a new device.
1823 */
1824int gspca_dev_probe(struct usb_interface *intf,
1825 const struct usb_device_id *id,
1826 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001827 int dev_size,
1828 struct module *module)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001829{
1830 struct usb_interface_descriptor *interface;
1831 struct gspca_dev *gspca_dev;
1832 struct usb_device *dev = interface_to_usbdev(intf);
1833 int ret;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001834
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001835 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001836
1837 /* we don't handle multi-config cameras */
1838 if (dev->descriptor.bNumConfigurations != 1)
1839 return -ENODEV;
1840 interface = &intf->cur_altsetting->desc;
1841 if (interface->bInterfaceNumber > 0)
1842 return -ENODEV;
1843
1844 /* create the device */
1845 if (dev_size < sizeof *gspca_dev)
1846 dev_size = sizeof *gspca_dev;
1847 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
Erik Andrene293e592008-10-03 08:46:50 -03001848 if (!gspca_dev) {
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001849 err("couldn't kzalloc gspca struct");
Erik Andrene293e592008-10-03 08:46:50 -03001850 return -ENOMEM;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001851 }
Frank Zago6b060ff2008-09-28 08:12:22 -03001852 kref_init(&gspca_dev->kref);
Jean-Francois Moine8295d992008-09-03 17:12:19 -03001853 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
1854 if (!gspca_dev->usb_buf) {
1855 err("out of memory");
Erik Andrene293e592008-10-03 08:46:50 -03001856 ret = -ENOMEM;
Jean-Francois Moine8295d992008-09-03 17:12:19 -03001857 goto out;
1858 }
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001859 gspca_dev->dev = dev;
1860 gspca_dev->iface = interface->bInterfaceNumber;
Jean-Francois Moined43fa322008-06-12 10:58:58 -03001861 gspca_dev->nbalt = intf->num_altsetting;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001862 gspca_dev->sd_desc = sd_desc;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001863 gspca_dev->nbufread = 2;
Jean-Francois Moineff374742008-10-23 07:29:51 -03001864 gspca_dev->empty_packet = -1; /* don't check the empty packets */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001865
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001866 /* configure the subdriver and initialize the USB device */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001867 ret = gspca_dev->sd_desc->config(gspca_dev, id);
1868 if (ret < 0)
1869 goto out;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001870 ret = gspca_dev->sd_desc->init(gspca_dev);
1871 if (ret < 0)
1872 goto out;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001873 ret = gspca_set_alt0(gspca_dev);
1874 if (ret < 0)
1875 goto out;
1876 gspca_set_default_mode(gspca_dev);
1877
1878 mutex_init(&gspca_dev->usb_lock);
1879 mutex_init(&gspca_dev->read_lock);
1880 mutex_init(&gspca_dev->queue_lock);
1881 init_waitqueue_head(&gspca_dev->wq);
1882
1883 /* init video stuff */
Jean-Francois Moinee07a1d82008-11-19 06:37:53 -03001884 gspca_dev->vdev = video_device_alloc();
1885 memcpy(gspca_dev->vdev, &gspca_template, sizeof gspca_template);
1886 gspca_dev->vdev->parent = &dev->dev;
Jean-Francois Moine5c4fa002008-11-18 15:52:31 -03001887 gspca_dev->module = module;
Oliver Neukumee56a4d2008-07-27 14:01:59 -03001888 gspca_dev->present = 1;
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001889 ret = video_register_device(&gspca_dev->vdev,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001890 VFL_TYPE_GRABBER,
1891 video_nr);
1892 if (ret < 0) {
1893 err("video_register_device err %d", ret);
1894 goto out;
1895 }
1896
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001897 usb_set_intfdata(intf, gspca_dev);
1898 PDEBUG(D_PROBE, "probe ok");
1899 return 0;
1900out:
Jean-Francois Moinee07a1d82008-11-19 06:37:53 -03001901 kfree(gspca_dev->usb_buf);
1902 kfree(gspca_dev);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001903 return ret;
1904}
1905EXPORT_SYMBOL(gspca_dev_probe);
1906
1907/*
1908 * USB disconnection
1909 *
1910 * This function must be called by the sub-driver
1911 * when the device disconnects, after the specific resources are freed.
1912 */
1913void gspca_disconnect(struct usb_interface *intf)
1914{
Jean-Francois Moine4aa0d032008-05-04 06:46:21 -03001915 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001916
Frank Zago6b060ff2008-09-28 08:12:22 -03001917 usb_set_intfdata(intf, NULL);
1918
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -03001919 /* release the device */
1920 /* (this will call gspca_release() immediatly or on last close) */
1921 video_unregister_device(&gspca_dev->vdev);
Jean-Francois Moine3145b8c2008-10-05 04:22:10 -03001922
Frank Zago6b060ff2008-09-28 08:12:22 -03001923 kref_put(&gspca_dev->kref, gspca_delete);
1924
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03001925 PDEBUG(D_PROBE, "disconnect complete");
1926}
1927EXPORT_SYMBOL(gspca_disconnect);
1928
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001929#ifdef CONFIG_PM
1930int gspca_suspend(struct usb_interface *intf, pm_message_t message)
1931{
1932 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1933
1934 if (!gspca_dev->streaming)
1935 return 0;
1936 gspca_dev->frozen = 1; /* avoid urb error messages */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001937 if (gspca_dev->sd_desc->stopN)
1938 gspca_dev->sd_desc->stopN(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001939 destroy_urbs(gspca_dev);
1940 gspca_set_alt0(gspca_dev);
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001941 if (gspca_dev->sd_desc->stop0)
1942 gspca_dev->sd_desc->stop0(gspca_dev);
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001943 return 0;
1944}
1945EXPORT_SYMBOL(gspca_suspend);
1946
1947int gspca_resume(struct usb_interface *intf)
1948{
1949 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1950
1951 gspca_dev->frozen = 0;
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001952 gspca_dev->sd_desc->init(gspca_dev);
1953 if (gspca_dev->streaming)
1954 return gspca_init_transfer(gspca_dev);
Jean-Francois Moine1e89e2d2008-09-03 17:12:15 -03001955 return 0;
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001956}
1957EXPORT_SYMBOL(gspca_resume);
1958#endif
Hans de Goededcef3232008-07-10 10:40:53 -03001959/* -- cam driver utility functions -- */
1960
1961/* auto gain and exposure algorithm based on the knee algorithm described here:
1962 http://ytse.tricolour.net/docs/LowLightOptimization.html
1963
1964 Returns 0 if no changes were made, 1 if the gain and or exposure settings
1965 where changed. */
1966int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
1967 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
1968{
1969 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
1970 const struct ctrl *gain_ctrl = NULL;
1971 const struct ctrl *exposure_ctrl = NULL;
1972 const struct ctrl *autogain_ctrl = NULL;
1973 int retval = 0;
1974
1975 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1976 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
1977 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1978 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
1979 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
1980 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
1981 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1982 }
1983 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
1984 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
1985 "on cam without (auto)gain/exposure");
1986 return 0;
1987 }
1988
1989 if (gain_ctrl->get(gspca_dev, &gain) ||
1990 exposure_ctrl->get(gspca_dev, &exposure) ||
1991 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
1992 return 0;
1993
1994 orig_gain = gain;
1995 orig_exposure = exposure;
1996
1997 /* If we are of a multiple of deadzone, do multiple steps to reach the
1998 desired lumination fast (with the risc of a slight overshoot) */
1999 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2000
Jean-Francois Moine1c44d812008-11-10 05:56:55 -03002001 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
Hans de Goededcef3232008-07-10 10:40:53 -03002002 avg_lum, desired_avg_lum, steps);
2003
2004 for (i = 0; i < steps; i++) {
2005 if (avg_lum > desired_avg_lum) {
2006 if (gain > gain_knee)
2007 gain--;
2008 else if (exposure > exposure_knee)
2009 exposure--;
2010 else if (gain > gain_ctrl->qctrl.default_value)
2011 gain--;
2012 else if (exposure > exposure_ctrl->qctrl.minimum)
2013 exposure--;
2014 else if (gain > gain_ctrl->qctrl.minimum)
2015 gain--;
2016 else
2017 break;
2018 } else {
2019 if (gain < gain_ctrl->qctrl.default_value)
2020 gain++;
2021 else if (exposure < exposure_knee)
2022 exposure++;
2023 else if (gain < gain_knee)
2024 gain++;
2025 else if (exposure < exposure_ctrl->qctrl.maximum)
2026 exposure++;
2027 else if (gain < gain_ctrl->qctrl.maximum)
2028 gain++;
2029 else
2030 break;
2031 }
2032 }
2033
2034 if (gain != orig_gain) {
2035 gain_ctrl->set(gspca_dev, gain);
2036 retval = 1;
2037 }
2038 if (exposure != orig_exposure) {
2039 exposure_ctrl->set(gspca_dev, exposure);
2040 retval = 1;
2041 }
2042
2043 return retval;
2044}
2045EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2046
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002047/* -- module insert / remove -- */
2048static int __init gspca_init(void)
2049{
Jean-Francois Moinebb64e862008-07-19 06:49:28 -03002050 info("main v%d.%d.%d registered",
2051 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2052 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2053 DRIVER_VERSION_NUMBER & 0xff);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002054 return 0;
2055}
2056static void __exit gspca_exit(void)
2057{
2058 info("main deregistered");
2059}
2060
2061module_init(gspca_init);
2062module_exit(gspca_exit);
2063
Jean-Francois Moine335b3f82008-07-30 04:53:02 -03002064#ifdef GSPCA_DEBUG
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002065module_param_named(debug, gspca_debug, int, 0644);
2066MODULE_PARM_DESC(debug,
2067 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
Jean-Francois Moined43fa322008-06-12 10:58:58 -03002068 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
2069 " 0x0100: v4l2");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002070#endif