[PATCH] V4L/DVB: (3086c) Whitespaces cleanups part 4
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / media / video / em28xx / em28xx-core.c
blob517573221972b2fdd73fa9089078ace9721d368d
1 /*
2 em28xx-core.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
4 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5 Markus Rechberger <mrechberger@gmail.com>
6 Mauro Carvalho Chehab <mchehab@brturbo.com.br>
7 Sascha Sommer <saschasommer@freenet.de>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/init.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/usb.h>
29 #include <linux/vmalloc.h>
31 #include "em28xx.h"
33 /* #define ENABLE_DEBUG_ISOC_FRAMES */
35 static unsigned int core_debug;
36 module_param(core_debug,int,0644);
37 MODULE_PARM_DESC(core_debug,"enable debug messages [core]");
39 #define em28xx_coredbg(fmt, arg...) do {\
40 if (core_debug) \
41 printk(KERN_INFO "%s %s :"fmt, \
42 dev->name, __FUNCTION__, ##arg); } while (0)
44 static unsigned int reg_debug;
45 module_param(reg_debug,int,0644);
46 MODULE_PARM_DESC(reg_debug,"enable debug messages [URB reg]");
48 #define em28xx_regdbg(fmt, arg...) do {\
49 if (reg_debug) \
50 printk(KERN_INFO "%s %s :"fmt, \
51 dev->name, __FUNCTION__, ##arg); } while (0)
53 static unsigned int isoc_debug;
54 module_param(isoc_debug,int,0644);
55 MODULE_PARM_DESC(isoc_debug,"enable debug messages [isoc transfers]");
57 #define em28xx_isocdbg(fmt, arg...) do {\
58 if (isoc_debug) \
59 printk(KERN_INFO "%s %s :"fmt, \
60 dev->name, __FUNCTION__, ##arg); } while (0)
62 static int alt = EM28XX_PINOUT;
63 module_param(alt, int, 0644);
64 MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint");
66 /* ------------------------------------------------------------------ */
67 /* debug help functions */
69 static const char *v4l1_ioctls[] = {
70 "0", "CGAP", "GCHAN", "SCHAN", "GTUNER", "STUNER", "GPICT", "SPICT",
71 "CCAPTURE", "GWIN", "SWIN", "GFBUF", "SFBUF", "KEY", "GFREQ",
72 "SFREQ", "GAUDIO", "SAUDIO", "SYNC", "MCAPTURE", "GMBUF", "GUNIT",
73 "GCAPTURE", "SCAPTURE", "SPLAYMODE", "SWRITEMODE", "GPLAYINFO",
74 "SMICROCODE", "GVBIFMT", "SVBIFMT" };
75 #define V4L1_IOCTLS ARRAY_SIZE(v4l1_ioctls)
77 static const char *v4l2_ioctls[] = {
78 "QUERYCAP", "1", "ENUM_PIXFMT", "ENUM_FBUFFMT", "G_FMT", "S_FMT",
79 "G_COMP", "S_COMP", "REQBUFS", "QUERYBUF", "G_FBUF", "S_FBUF",
80 "G_WIN", "S_WIN", "PREVIEW", "QBUF", "16", "DQBUF", "STREAMON",
81 "STREAMOFF", "G_PERF", "G_PARM", "S_PARM", "G_STD", "S_STD",
82 "ENUMSTD", "ENUMINPUT", "G_CTRL", "S_CTRL", "G_TUNER", "S_TUNER",
83 "G_FREQ", "S_FREQ", "G_AUDIO", "S_AUDIO", "35", "QUERYCTRL",
84 "QUERYMENU", "G_INPUT", "S_INPUT", "ENUMCVT", "41", "42", "43",
85 "44", "45", "G_OUTPUT", "S_OUTPUT", "ENUMOUTPUT", "G_AUDOUT",
86 "S_AUDOUT", "ENUMFX", "G_EFFECT", "S_EFFECT", "G_MODULATOR",
87 "S_MODULATOR"
89 #define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
91 void em28xx_print_ioctl(char *name, unsigned int cmd)
93 char *dir;
95 switch (_IOC_DIR(cmd)) {
96 case _IOC_NONE: dir = "--"; break;
97 case _IOC_READ: dir = "r-"; break;
98 case _IOC_WRITE: dir = "-w"; break;
99 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
100 default: dir = "??"; break;
102 switch (_IOC_TYPE(cmd)) {
103 case 'v':
104 printk(KERN_DEBUG "%s: ioctl 0x%08x (v4l1, %s, VIDIOC%s)\n",
105 name, cmd, dir, (_IOC_NR(cmd) < V4L1_IOCTLS) ?
106 v4l1_ioctls[_IOC_NR(cmd)] : "???");
107 break;
108 case 'V':
109 printk(KERN_DEBUG "%s: ioctl 0x%08x (v4l2, %s, VIDIOC_%s)\n",
110 name, cmd, dir, (_IOC_NR(cmd) < V4L2_IOCTLS) ?
111 v4l2_ioctls[_IOC_NR(cmd)] : "???");
112 break;
113 default:
114 printk(KERN_DEBUG "%s: ioctl 0x%08x (???, %s, #%d)\n",
115 name, cmd, dir, _IOC_NR(cmd));
119 static void *rvmalloc(size_t size)
121 void *mem;
122 unsigned long adr;
124 size = PAGE_ALIGN(size);
126 mem = vmalloc_32((unsigned long)size);
127 if (!mem)
128 return NULL;
130 memset(mem, 0, size);
132 adr = (unsigned long)mem;
133 while (size > 0) {
134 SetPageReserved(vmalloc_to_page((void *)adr));
135 adr += PAGE_SIZE;
136 size -= PAGE_SIZE;
139 return mem;
142 static void rvfree(void *mem, size_t size)
144 unsigned long adr;
146 if (!mem)
147 return;
149 size = PAGE_ALIGN(size);
151 adr = (unsigned long)mem;
152 while (size > 0) {
153 ClearPageReserved(vmalloc_to_page((void *)adr));
154 adr += PAGE_SIZE;
155 size -= PAGE_SIZE;
158 vfree(mem);
163 * em28xx_request_buffers()
164 * allocate a number of buffers
166 u32 em28xx_request_buffers(struct em28xx *dev, u32 count)
168 const size_t imagesize = PAGE_ALIGN(dev->frame_size); /*needs to be page aligned cause the buffers can be mapped individually! */
169 void *buff = NULL;
170 u32 i;
171 em28xx_coredbg("requested %i buffers with size %i", count, imagesize);
172 if (count > EM28XX_NUM_FRAMES)
173 count = EM28XX_NUM_FRAMES;
175 dev->num_frames = count;
176 while (dev->num_frames > 0) {
177 if ((buff = rvmalloc(dev->num_frames * imagesize)))
178 break;
179 dev->num_frames--;
182 for (i = 0; i < dev->num_frames; i++) {
183 dev->frame[i].bufmem = buff + i * imagesize;
184 dev->frame[i].buf.index = i;
185 dev->frame[i].buf.m.offset = i * imagesize;
186 dev->frame[i].buf.length = dev->frame_size;
187 dev->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
188 dev->frame[i].buf.sequence = 0;
189 dev->frame[i].buf.field = V4L2_FIELD_NONE;
190 dev->frame[i].buf.memory = V4L2_MEMORY_MMAP;
191 dev->frame[i].buf.flags = 0;
193 return dev->num_frames;
197 * em28xx_queue_unusedframes()
198 * add all frames that are not currently in use to the inbuffer queue
200 void em28xx_queue_unusedframes(struct em28xx *dev)
202 unsigned long lock_flags;
203 u32 i;
205 for (i = 0; i < dev->num_frames; i++)
206 if (dev->frame[i].state == F_UNUSED) {
207 dev->frame[i].state = F_QUEUED;
208 spin_lock_irqsave(&dev->queue_lock, lock_flags);
209 list_add_tail(&dev->frame[i].frame, &dev->inqueue);
210 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
215 * em28xx_release_buffers()
216 * free frame buffers
218 void em28xx_release_buffers(struct em28xx *dev)
220 if (dev->num_frames) {
221 rvfree(dev->frame[0].bufmem,
222 dev->num_frames * PAGE_ALIGN(dev->frame[0].buf.length));
223 dev->num_frames = 0;
228 * em28xx_read_reg_req()
229 * reads data from the usb device specifying bRequest
231 int em28xx_read_reg_req_len(struct em28xx *dev, u8 req, u16 reg,
232 char *buf, int len)
234 int ret, byte;
236 em28xx_regdbg("req=%02x, reg=%02x ", req, reg);
238 ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0), req,
239 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
240 0x0000, reg, buf, len, HZ);
242 if (reg_debug){
243 printk(ret < 0 ? " failed!\n" : "%02x values: ", ret);
244 for (byte = 0; byte < len; byte++) {
245 printk(" %02x", buf[byte]);
247 printk("\n");
250 return ret;
254 * em28xx_read_reg_req()
255 * reads data from the usb device specifying bRequest
257 int em28xx_read_reg_req(struct em28xx *dev, u8 req, u16 reg)
259 u8 val;
260 int ret;
262 em28xx_regdbg("req=%02x, reg=%02x:", req, reg);
264 ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0), req,
265 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
266 0x0000, reg, &val, 1, HZ);
268 if (reg_debug)
269 printk(ret < 0 ? " failed!\n" : "%02x\n", val);
271 if (ret < 0)
272 return ret;
274 return val;
277 int em28xx_read_reg(struct em28xx *dev, u16 reg)
279 return em28xx_read_reg_req(dev, USB_REQ_GET_STATUS, reg);
283 * em28xx_write_regs_req()
284 * sends data to the usb device, specifying bRequest
286 int em28xx_write_regs_req(struct em28xx *dev, u8 req, u16 reg, char *buf,
287 int len)
289 int ret;
291 /*usb_control_msg seems to expect a kmalloced buffer */
292 unsigned char *bufs = kmalloc(len, GFP_KERNEL);
294 em28xx_regdbg("req=%02x reg=%02x:", req, reg);
296 if (reg_debug) {
297 int i;
298 for (i = 0; i < len; ++i)
299 printk (" %02x", (unsigned char)buf[i]);
300 printk ("\n");
303 if (!bufs)
304 return -ENOMEM;
305 memcpy(bufs, buf, len);
306 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), req,
307 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
308 0x0000, reg, bufs, len, HZ);
309 mdelay(5); /* FIXME: magic number */
310 kfree(bufs);
311 return ret;
314 int em28xx_write_regs(struct em28xx *dev, u16 reg, char *buf, int len)
316 return em28xx_write_regs_req(dev, USB_REQ_GET_STATUS, reg, buf, len);
320 * em28xx_write_reg_bits()
321 * sets only some bits (specified by bitmask) of a register, by first reading
322 * the actual value
324 int em28xx_write_reg_bits(struct em28xx *dev, u16 reg, u8 val,
325 u8 bitmask)
327 int oldval;
328 u8 newval;
329 if ((oldval = em28xx_read_reg(dev, reg)) < 0)
330 return oldval;
331 newval = (((u8) oldval) & ~bitmask) | (val & bitmask);
332 return em28xx_write_regs(dev, reg, &newval, 1);
336 * em28xx_write_ac97()
337 * write a 16 bit value to the specified AC97 address (LSB first!)
339 int em28xx_write_ac97(struct em28xx *dev, u8 reg, u8 * val)
341 int ret;
342 u8 addr = reg & 0x7f;
343 if ((ret = em28xx_write_regs(dev, AC97LSB_REG, val, 2)) < 0)
344 return ret;
345 if ((ret = em28xx_write_regs(dev, AC97ADDR_REG, &addr, 1)) < 0)
346 return ret;
347 if ((ret = em28xx_read_reg(dev, AC97BUSY_REG)) < 0)
348 return ret;
349 else if (((u8) ret) & 0x01) {
350 em28xx_warn ("AC97 command still being exectuted: not handled properly!\n");
352 return 0;
355 int em28xx_audio_analog_set(struct em28xx *dev)
357 char s[2] = { 0x00, 0x00 };
358 s[0] |= 0x1f - dev->volume;
359 s[1] |= 0x1f - dev->volume;
360 if (dev->mute)
361 s[1] |= 0x80;
362 return em28xx_write_ac97(dev, MASTER_AC97, s);
366 int em28xx_colorlevels_set_default(struct em28xx *dev)
368 em28xx_write_regs(dev, YGAIN_REG, "\x10", 1); /* contrast */
369 em28xx_write_regs(dev, YOFFSET_REG, "\x00", 1); /* brightness */
370 em28xx_write_regs(dev, UVGAIN_REG, "\x10", 1); /* saturation */
371 em28xx_write_regs(dev, UOFFSET_REG, "\x00", 1);
372 em28xx_write_regs(dev, VOFFSET_REG, "\x00", 1);
373 em28xx_write_regs(dev, SHARPNESS_REG, "\x00", 1);
375 em28xx_write_regs(dev, GAMMA_REG, "\x20", 1);
376 em28xx_write_regs(dev, RGAIN_REG, "\x20", 1);
377 em28xx_write_regs(dev, GGAIN_REG, "\x20", 1);
378 em28xx_write_regs(dev, BGAIN_REG, "\x20", 1);
379 em28xx_write_regs(dev, ROFFSET_REG, "\x00", 1);
380 em28xx_write_regs(dev, GOFFSET_REG, "\x00", 1);
381 return em28xx_write_regs(dev, BOFFSET_REG, "\x00", 1);
384 int em28xx_capture_start(struct em28xx *dev, int start)
386 int ret;
387 /* FIXME: which is the best order? */
388 /* video registers are sampled by VREF */
389 if ((ret = em28xx_write_reg_bits(dev, USBSUSP_REG, start ? 0x10 : 0x00,
390 0x10)) < 0)
391 return ret;
392 /* enable video capture */
393 return em28xx_write_regs(dev, VINENABLE_REG, start ? "\x67" : "\x27", 1);
396 int em28xx_outfmt_set_yuv422(struct em28xx *dev)
398 em28xx_write_regs(dev, OUTFMT_REG, "\x34", 1);
399 em28xx_write_regs(dev, VINMODE_REG, "\x10", 1);
400 return em28xx_write_regs(dev, VINCTRL_REG, "\x11", 1);
403 int em28xx_accumulator_set(struct em28xx *dev, u8 xmin, u8 xmax, u8 ymin,
404 u8 ymax)
406 em28xx_coredbg("em28xx Scale: (%d,%d)-(%d,%d)\n", xmin, ymin, xmax, ymax);
408 em28xx_write_regs(dev, XMIN_REG, &xmin, 1);
409 em28xx_write_regs(dev, XMAX_REG, &xmax, 1);
410 em28xx_write_regs(dev, YMIN_REG, &ymin, 1);
411 return em28xx_write_regs(dev, YMAX_REG, &ymax, 1);
414 int em28xx_capture_area_set(struct em28xx *dev, u8 hstart, u8 vstart,
415 u16 width, u16 height)
417 u8 cwidth = width;
418 u8 cheight = height;
419 u8 overflow = (height >> 7 & 0x02) | (width >> 8 & 0x01);
421 em28xx_coredbg("em28xx Area Set: (%d,%d)\n", (width | (overflow & 2) << 7),
422 (height | (overflow & 1) << 8));
424 em28xx_write_regs(dev, HSTART_REG, &hstart, 1);
425 em28xx_write_regs(dev, VSTART_REG, &vstart, 1);
426 em28xx_write_regs(dev, CWIDTH_REG, &cwidth, 1);
427 em28xx_write_regs(dev, CHEIGHT_REG, &cheight, 1);
428 return em28xx_write_regs(dev, OFLOW_REG, &overflow, 1);
431 int em28xx_scaler_set(struct em28xx *dev, u16 h, u16 v)
433 u8 mode;
434 /* the em2800 scaler only supports scaling down to 50% */
435 if(dev->is_em2800)
436 mode = (v ? 0x20 : 0x00) | (h ? 0x10 : 0x00);
437 else {
438 u8 buf[2];
439 buf[0] = h;
440 buf[1] = h >> 8;
441 em28xx_write_regs(dev, HSCALELOW_REG, (char *)buf, 2);
442 buf[0] = v;
443 buf[1] = v >> 8;
444 em28xx_write_regs(dev, VSCALELOW_REG, (char *)buf, 2);
445 /* it seems that both H and V scalers must be active to work correctly */
446 mode = (h || v)? 0x30: 0x00;
448 return em28xx_write_reg_bits(dev, COMPR_REG, mode, 0x30);
451 /* FIXME: this only function read values from dev */
452 int em28xx_resolution_set(struct em28xx *dev)
454 int width, height;
455 width = norm_maxw(dev);
456 height = norm_maxh(dev) >> 1;
458 em28xx_outfmt_set_yuv422(dev);
459 em28xx_accumulator_set(dev, 1, (width - 4) >> 2, 1, (height - 4) >> 2);
460 em28xx_capture_area_set(dev, 0, 0, width >> 2, height >> 2);
461 return em28xx_scaler_set(dev, dev->hscale, dev->vscale);
465 /******************* isoc transfer handling ****************************/
467 #ifdef ENABLE_DEBUG_ISOC_FRAMES
468 static void em28xx_isoc_dump(struct urb *urb, struct pt_regs *regs)
470 int len = 0;
471 int ntrans = 0;
472 int i;
474 printk(KERN_DEBUG "isocIrq: sf=%d np=%d ec=%x\n",
475 urb->start_frame, urb->number_of_packets,
476 urb->error_count);
477 for (i = 0; i < urb->number_of_packets; i++) {
478 unsigned char *buf =
479 urb->transfer_buffer +
480 urb->iso_frame_desc[i].offset;
481 int alen = urb->iso_frame_desc[i].actual_length;
482 if (alen > 0) {
483 if (buf[0] == 0x88) {
484 ntrans++;
485 len += alen;
486 } else if (buf[0] == 0x22) {
487 printk(KERN_DEBUG
488 "= l=%d nt=%d bpp=%d\n",
489 len - 4 * ntrans, ntrans,
490 ntrans == 0 ? 0 : len / ntrans);
491 ntrans = 1;
492 len = alen;
493 } else
494 printk(KERN_DEBUG "!\n");
496 printk(KERN_DEBUG " n=%d s=%d al=%d %x\n", i,
497 urb->iso_frame_desc[i].status,
498 urb->iso_frame_desc[i].actual_length,
499 (unsigned int)
500 *((unsigned char *)(urb->transfer_buffer +
501 urb->iso_frame_desc[i].
502 offset)));
505 #endif
507 static inline int em28xx_isoc_video(struct em28xx *dev,struct em28xx_frame_t **f,
508 unsigned long *lock_flags, unsigned char buf)
510 if (!(buf & 0x01)) {
511 if ((*f)->state == F_GRABBING) {
512 /*previous frame is incomplete */
513 if ((*f)->fieldbytesused < dev->field_size) {
514 (*f)->state = F_ERROR;
515 em28xx_isocdbg ("dropping incomplete bottom field (%i missing bytes)",
516 dev->field_size-(*f)->fieldbytesused);
517 } else {
518 (*f)->state = F_DONE;
519 (*f)->buf.bytesused = dev->frame_size;
522 if ((*f)->state == F_DONE || (*f)->state == F_ERROR) {
523 /* move current frame to outqueue and get next free buffer from inqueue */
524 spin_lock_irqsave(&dev-> queue_lock, *lock_flags);
525 list_move_tail(&(*f)->frame, &dev->outqueue);
526 if (!list_empty(&dev->inqueue))
527 (*f) = list_entry(dev-> inqueue.next,
528 struct em28xx_frame_t,frame);
529 else
530 (*f) = NULL;
531 spin_unlock_irqrestore(&dev->queue_lock,*lock_flags);
533 if (!(*f)) {
534 em28xx_isocdbg ("new frame but no buffer is free");
535 return -1;
537 do_gettimeofday(&(*f)->buf.timestamp);
538 (*f)->buf.sequence = ++dev->frame_count;
539 (*f)->buf.field = V4L2_FIELD_INTERLACED;
540 (*f)->state = F_GRABBING;
541 (*f)->buf.bytesused = 0;
542 (*f)->top_field = 1;
543 (*f)->fieldbytesused = 0;
544 } else {
545 /* acquiring bottom field */
546 if ((*f)->state == F_GRABBING) {
547 if (!(*f)->top_field) {
548 (*f)->state = F_ERROR;
549 em28xx_isocdbg ("unexpected begin of bottom field; discarding it");
550 } else if ((*f)-> fieldbytesused < dev->field_size - 172) {
551 (*f)->state = F_ERROR;
552 em28xx_isocdbg ("dropping incomplete top field (%i missing bytes)",
553 dev->field_size-(*f)->fieldbytesused);
554 } else {
555 (*f)->top_field = 0;
556 (*f)->fieldbytesused = 0;
560 return (0);
563 static inline void em28xx_isoc_video_copy(struct em28xx *dev,
564 struct em28xx_frame_t **f, unsigned char *buf, int len)
566 void *fieldstart, *startwrite, *startread;
567 int linesdone, currlinedone, offset, lencopy,remain;
569 if(dev->frame_size != (*f)->buf.length){
570 em28xx_err("frame_size %i and buf.length %i are different!!!\n",dev->frame_size,(*f)->buf.length);
571 return;
574 if ((*f)->fieldbytesused + len > dev->field_size)
575 len =dev->field_size - (*f)->fieldbytesused;
577 if (buf[0] != 0x88 && buf[0] != 0x22) {
578 em28xx_isocdbg("frame is not complete\n");
579 startread = buf;
580 len+=4;
581 } else
582 startread = buf + 4;
584 remain = len;
586 if ((*f)->top_field)
587 fieldstart = (*f)->bufmem;
588 else
589 fieldstart = (*f)->bufmem + dev->bytesperline;
591 linesdone = (*f)->fieldbytesused / dev->bytesperline;
592 currlinedone = (*f)->fieldbytesused % dev->bytesperline;
593 offset = linesdone * dev->bytesperline * 2 + currlinedone;
594 startwrite = fieldstart + offset;
595 lencopy = dev->bytesperline - currlinedone;
596 lencopy = lencopy > remain ? remain : lencopy;
598 memcpy(startwrite, startread, lencopy);
599 remain -= lencopy;
601 while (remain > 0) {
602 startwrite += lencopy + dev->bytesperline;
603 startread += lencopy;
604 if (dev->bytesperline > remain)
605 lencopy = remain;
606 else
607 lencopy = dev->bytesperline;
609 memcpy(startwrite, startread, lencopy);
610 remain -= lencopy;
613 (*f)->fieldbytesused += len;
617 * em28xx_isoIrq()
618 * handles the incoming isoc urbs and fills the frames from our inqueue
620 void em28xx_isocIrq(struct urb *urb, struct pt_regs *regs)
622 struct em28xx *dev = urb->context;
623 int i, status;
624 struct em28xx_frame_t **f;
625 unsigned long lock_flags;
627 if (!dev)
628 return;
629 #ifdef ENABLE_DEBUG_ISOC_FRAMES
630 if (isoc_debug>1)
631 em28xx_isoc_dump(urb, regs);
632 #endif
634 if (urb->status == -ENOENT)
635 return;
637 f = &dev->frame_current;
639 if (dev->stream == STREAM_INTERRUPT) {
640 dev->stream = STREAM_OFF;
641 if ((*f))
642 (*f)->state = F_QUEUED;
643 em28xx_isocdbg("stream interrupted");
644 wake_up_interruptible(&dev->wait_stream);
647 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
648 return;
650 if (dev->stream == STREAM_ON && !list_empty(&dev->inqueue)) {
651 if (!(*f))
652 (*f) = list_entry(dev->inqueue.next,
653 struct em28xx_frame_t, frame);
655 for (i = 0; i < urb->number_of_packets; i++) {
656 unsigned char *buf = urb->transfer_buffer +
657 urb->iso_frame_desc[i].offset;
658 int len = urb->iso_frame_desc[i].actual_length - 4;
660 if (urb->iso_frame_desc[i].status) {
661 em28xx_isocdbg("data error: [%d] len=%d, status=%d", i,
662 urb->iso_frame_desc[i].actual_length,
663 urb->iso_frame_desc[i].status);
664 if (urb->iso_frame_desc[i].status != -EPROTO)
665 continue;
667 if (urb->iso_frame_desc[i].actual_length <= 0) {
668 em28xx_isocdbg("packet %d is empty",i);
669 continue;
671 if (urb->iso_frame_desc[i].actual_length >
672 dev->max_pkt_size) {
673 em28xx_isocdbg("packet bigger than packet size");
674 continue;
676 /*new frame */
677 if (buf[0] == 0x22 && buf[1] == 0x5a) {
678 em28xx_isocdbg("Video frame, length=%i!",len);
680 if (em28xx_isoc_video(dev,f,&lock_flags,buf[2]))
681 break;
682 } else if (buf[0]==0x33 && buf[1]==0x95 && buf[2]==0x00) {
683 em28xx_isocdbg("VBI HEADER!!!");
686 /* actual copying */
687 if ((*f)->state == F_GRABBING) {
688 em28xx_isoc_video_copy(dev,f,buf, len);
693 for (i = 0; i < urb->number_of_packets; i++) {
694 urb->iso_frame_desc[i].status = 0;
695 urb->iso_frame_desc[i].actual_length = 0;
698 urb->status = 0;
699 if ((status = usb_submit_urb(urb, GFP_ATOMIC))) {
700 em28xx_errdev("resubmit of urb failed (error=%i)\n", status);
701 dev->state |= DEV_MISCONFIGURED;
703 wake_up_interruptible(&dev->wait_frame);
704 return;
708 * em28xx_uninit_isoc()
709 * deallocates the buffers and urbs allocated during em28xx_init_iosc()
711 void em28xx_uninit_isoc(struct em28xx *dev)
713 int i;
715 for (i = 0; i < EM28XX_NUM_BUFS; i++) {
716 if (dev->urb[i]) {
717 usb_kill_urb(dev->urb[i]);
718 if (dev->transfer_buffer[i]){
719 usb_buffer_free(dev->udev,(EM28XX_NUM_PACKETS*dev->max_pkt_size),dev->transfer_buffer[i],dev->urb[i]->transfer_dma);
721 usb_free_urb(dev->urb[i]);
723 dev->urb[i] = NULL;
724 dev->transfer_buffer[i] = NULL;
726 em28xx_capture_start(dev, 0);
730 * em28xx_init_isoc()
731 * allocates transfer buffers and submits the urbs for isoc transfer
733 int em28xx_init_isoc(struct em28xx *dev)
735 /* change interface to 3 which allowes the biggest packet sizes */
736 int i, errCode;
737 const int sb_size = EM28XX_NUM_PACKETS * dev->max_pkt_size;
739 /* reset streaming vars */
740 dev->frame_current = NULL;
741 dev->frame_count = 0;
743 /* allocate urbs */
744 for (i = 0; i < EM28XX_NUM_BUFS; i++) {
745 struct urb *urb;
746 int j, k;
747 /* allocate transfer buffer */
748 urb = usb_alloc_urb(EM28XX_NUM_PACKETS, GFP_KERNEL);
749 if (!urb){
750 em28xx_errdev("cannot alloc urb %i\n", i);
751 em28xx_uninit_isoc(dev);
752 return -ENOMEM;
754 dev->transfer_buffer[i] = usb_buffer_alloc(dev->udev, sb_size, GFP_KERNEL,&urb->transfer_dma);
755 if (!dev->transfer_buffer[i]) {
756 em28xx_errdev
757 ("unable to allocate %i bytes for transfer buffer %i\n",
758 sb_size, i);
759 em28xx_uninit_isoc(dev);
760 return -ENOMEM;
762 memset(dev->transfer_buffer[i], 0, sb_size);
763 urb->dev = dev->udev;
764 urb->context = dev;
765 urb->pipe = usb_rcvisocpipe(dev->udev, 0x82);
766 urb->transfer_flags = URB_ISO_ASAP;
767 urb->interval = 1;
768 urb->transfer_buffer = dev->transfer_buffer[i];
769 urb->complete = em28xx_isocIrq;
770 urb->number_of_packets = EM28XX_NUM_PACKETS;
771 urb->transfer_buffer_length = sb_size;
772 for (j = k = 0; j < EM28XX_NUM_PACKETS;
773 j++, k += dev->max_pkt_size) {
774 urb->iso_frame_desc[j].offset = k;
775 urb->iso_frame_desc[j].length =
776 dev->max_pkt_size;
778 dev->urb[i] = urb;
781 /* submit urbs */
782 for (i = 0; i < EM28XX_NUM_BUFS; i++) {
783 errCode = usb_submit_urb(dev->urb[i], GFP_KERNEL);
784 if (errCode) {
785 em28xx_errdev("submit of urb %i failed (error=%i)\n", i,
786 errCode);
787 em28xx_uninit_isoc(dev);
788 return errCode;
792 return 0;
795 int em28xx_set_alternate(struct em28xx *dev)
797 int errCode, prev_alt = dev->alt;
798 dev->alt = alt;
799 if (dev->alt == 0) {
800 int i;
801 for(i=0;i< dev->num_alt; i++)
802 if(dev->alt_max_pkt_size[i]>dev->alt_max_pkt_size[dev->alt])
803 dev->alt=i;
806 if (dev->alt != prev_alt) {
807 dev->max_pkt_size = dev->alt_max_pkt_size[dev->alt];
808 em28xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n", dev->alt,
809 dev->max_pkt_size);
810 errCode = usb_set_interface(dev->udev, 0, dev->alt);
811 if (errCode < 0) {
812 em28xx_errdev ("cannot change alternate number to %d (error=%i)\n",
813 dev->alt, errCode);
814 return errCode;
817 return 0;