linux/drivers/media/video/em28xx/em28xx-core.c
author Markus Rechberger <mrechberger@gmail.com>
Fri Feb 03 02:12:18 2006 +0100 (2006-02-03)
changeset 3306 db0f68f60150
parent 3232 691edd57dab7
child 3715 1683316b4b14
permissions -rw-r--r--
fixed i2c return value, conversion mdelay to msleep

From: Markus Rechberger <mrechberger@gmail.com>

fixed i2c return value, conversion mdelay to msleep

Signed-off-by: Markus Rechberger <mrechberger@gmail.com>
     1 /*
     2    em28xx-core.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
     3 
     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>
     8 
     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.
    13 
    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.
    18 
    19    You should have received a copy of the GNU General Public License
    20    along with this program; if not, write to the Free Software
    21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    22  */
    23 
    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>
    30 
    31 #include "em28xx.h"
    32 
    33 /* #define ENABLE_DEBUG_ISOC_FRAMES */
    34 
    35 static unsigned int core_debug = 0;
    36 module_param(core_debug,int,0644);
    37 MODULE_PARM_DESC(core_debug,"enable debug messages [core]");
    38 
    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)
    43 
    44 static unsigned int reg_debug = 0;
    45 module_param(reg_debug,int,0644);
    46 MODULE_PARM_DESC(reg_debug,"enable debug messages [URB reg]");
    47 
    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)
    52 
    53 static unsigned int isoc_debug = 0;
    54 module_param(isoc_debug,int,0644);
    55 MODULE_PARM_DESC(isoc_debug,"enable debug messages [isoc transfers]");
    56 
    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)
    61 
    62 static int alt = EM28XX_PINOUT;
    63 module_param(alt, int, 0644);
    64 MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint");
    65 
    66 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
    67 static void *rvmalloc(size_t size)
    68 {
    69 	void *mem;
    70 	unsigned long adr;
    71 
    72 	size = PAGE_ALIGN(size);
    73 
    74 	mem = vmalloc_32((unsigned long)size);
    75 	if (!mem)
    76 		return NULL;
    77 
    78 	adr = (unsigned long)mem;
    79 	while (size > 0) {
    80 		SetPageReserved(vmalloc_to_page((void *)adr));
    81 		adr += PAGE_SIZE;
    82 		size -= PAGE_SIZE;
    83 	}
    84 
    85 	return mem;
    86 }
    87 
    88 static void rvfree(void *mem, size_t size)
    89 {
    90 	unsigned long adr;
    91 
    92 	if (!mem)
    93 		return;
    94 
    95 	size = PAGE_ALIGN(size);
    96 
    97 	adr = (unsigned long)mem;
    98 	while (size > 0) {
    99 		ClearPageReserved(vmalloc_to_page((void *)adr));
   100 		adr += PAGE_SIZE;
   101 		size -= PAGE_SIZE;
   102 	}
   103 
   104 	vfree(mem);
   105 }
   106 #endif
   107 
   108 /*
   109  * em28xx_request_buffers()
   110  * allocate a number of buffers
   111  */
   112 u32 em28xx_request_buffers(struct em28xx *dev, u32 count)
   113 {
   114 	const size_t imagesize = PAGE_ALIGN(dev->frame_size);	/*needs to be page aligned cause the buffers can be mapped individually! */
   115 	void *buff = NULL;
   116 	u32 i;
   117 	em28xx_coredbg("requested %i buffers with size %zi", count, imagesize);
   118 	if (count > EM28XX_NUM_FRAMES)
   119 		count = EM28XX_NUM_FRAMES;
   120 
   121 	dev->num_frames = count;
   122 	while (dev->num_frames > 0) {
   123 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
   124 		if ((buff = rvmalloc(dev->num_frames * imagesize))) {
   125 #else
   126 		if ((buff = vmalloc_32(dev->num_frames * imagesize))) {
   127 #endif
   128 			memset(buff, 0, dev->num_frames * imagesize);
   129 			break;
   130 		}
   131 		dev->num_frames--;
   132 	}
   133 
   134 	for (i = 0; i < dev->num_frames; i++) {
   135 		dev->frame[i].bufmem = buff + i * imagesize;
   136 		dev->frame[i].buf.index = i;
   137 		dev->frame[i].buf.m.offset = i * imagesize;
   138 		dev->frame[i].buf.length = dev->frame_size;
   139 		dev->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
   140 		dev->frame[i].buf.sequence = 0;
   141 		dev->frame[i].buf.field = V4L2_FIELD_NONE;
   142 		dev->frame[i].buf.memory = V4L2_MEMORY_MMAP;
   143 		dev->frame[i].buf.flags = 0;
   144 	}
   145 	return dev->num_frames;
   146 }
   147 
   148 /*
   149  * em28xx_queue_unusedframes()
   150  * add all frames that are not currently in use to the inbuffer queue
   151  */
   152 void em28xx_queue_unusedframes(struct em28xx *dev)
   153 {
   154 	unsigned long lock_flags;
   155 	u32 i;
   156 
   157 	for (i = 0; i < dev->num_frames; i++)
   158 		if (dev->frame[i].state == F_UNUSED) {
   159 			dev->frame[i].state = F_QUEUED;
   160 			spin_lock_irqsave(&dev->queue_lock, lock_flags);
   161 			list_add_tail(&dev->frame[i].frame, &dev->inqueue);
   162 			spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
   163 		}
   164 }
   165 
   166 /*
   167  * em28xx_release_buffers()
   168  * free frame buffers
   169  */
   170 void em28xx_release_buffers(struct em28xx *dev)
   171 {
   172 	if (dev->num_frames) {
   173 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
   174 		rvfree(dev->frame[0].bufmem,
   175 		       dev->num_frames * PAGE_ALIGN(dev->frame[0].buf.length));
   176 #else
   177 		vfree(dev->frame[0].bufmem);
   178 #endif
   179 		dev->num_frames = 0;
   180 	}
   181 }
   182 
   183 /*
   184  * em28xx_read_reg_req()
   185  * reads data from the usb device specifying bRequest
   186  */
   187 int em28xx_read_reg_req_len(struct em28xx *dev, u8 req, u16 reg,
   188 				   char *buf, int len)
   189 {
   190 	int ret, byte;
   191 
   192 	if (dev->state & DEV_DISCONNECTED)
   193 		return(-ENODEV);
   194 
   195 	em28xx_regdbg("req=%02x, reg=%02x ", req, reg);
   196 
   197 	ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0), req,
   198 			      USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
   199 			      0x0000, reg, buf, len, HZ);
   200 
   201 	if (reg_debug){
   202 		printk(ret < 0 ? " failed!\n" : "%02x values: ", ret);
   203 		for (byte = 0; byte < len; byte++) {
   204 			printk(" %02x", buf[byte]);
   205 		}
   206 		printk("\n");
   207 	}
   208 
   209 	return ret;
   210 }
   211 
   212 /*
   213  * em28xx_read_reg_req()
   214  * reads data from the usb device specifying bRequest
   215  */
   216 int em28xx_read_reg_req(struct em28xx *dev, u8 req, u16 reg)
   217 {
   218 	u8 val;
   219 	int ret;
   220 
   221 	if (dev->state & DEV_DISCONNECTED)
   222 		return(-ENODEV);
   223 
   224 	em28xx_regdbg("req=%02x, reg=%02x:", req, reg);
   225 
   226 	ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0), req,
   227 			      USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
   228 			      0x0000, reg, &val, 1, HZ);
   229 
   230 	if (reg_debug)
   231 		printk(ret < 0 ? " failed!\n" : "%02x\n", val);
   232 
   233 	if (ret < 0)
   234 		return ret;
   235 
   236 	return val;
   237 }
   238 
   239 int em28xx_read_reg(struct em28xx *dev, u16 reg)
   240 {
   241 	return em28xx_read_reg_req(dev, USB_REQ_GET_STATUS, reg);
   242 }
   243 
   244 /*
   245  * em28xx_write_regs_req()
   246  * sends data to the usb device, specifying bRequest
   247  */
   248 int em28xx_write_regs_req(struct em28xx *dev, u8 req, u16 reg, char *buf,
   249 				 int len)
   250 {
   251 	int ret;
   252 
   253 	/*usb_control_msg seems to expect a kmalloced buffer */
   254 	unsigned char *bufs;
   255 
   256 	if (dev->state & DEV_DISCONNECTED)
   257 		return(-ENODEV);
   258 
   259 	bufs = kmalloc(len, GFP_KERNEL);
   260 
   261 	em28xx_regdbg("req=%02x reg=%02x:", req, reg);
   262 
   263 	if (reg_debug) {
   264 		int i;
   265 		for (i = 0; i < len; ++i)
   266 			printk (" %02x", (unsigned char)buf[i]);
   267 		printk ("\n");
   268 	}
   269 
   270 	if (!bufs)
   271 		return -ENOMEM;
   272 	memcpy(bufs, buf, len);
   273 	ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), req,
   274 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
   275 			      0x0000, reg, bufs, len, HZ);
   276 	msleep(5);		/* FIXME: magic number */
   277 	kfree(bufs);
   278 	return ret;
   279 }
   280 
   281 int em28xx_write_regs(struct em28xx *dev, u16 reg, char *buf, int len)
   282 {
   283 	return em28xx_write_regs_req(dev, USB_REQ_GET_STATUS, reg, buf, len);
   284 }
   285 
   286 /*
   287  * em28xx_write_reg_bits()
   288  * sets only some bits (specified by bitmask) of a register, by first reading
   289  * the actual value
   290  */
   291 int em28xx_write_reg_bits(struct em28xx *dev, u16 reg, u8 val,
   292 				 u8 bitmask)
   293 {
   294 	int oldval;
   295 	u8 newval;
   296 	if ((oldval = em28xx_read_reg(dev, reg)) < 0)
   297 		return oldval;
   298 	newval = (((u8) oldval) & ~bitmask) | (val & bitmask);
   299 	return em28xx_write_regs(dev, reg, &newval, 1);
   300 }
   301 
   302 /*
   303  * em28xx_write_ac97()
   304  * write a 16 bit value to the specified AC97 address (LSB first!)
   305  */
   306 int em28xx_write_ac97(struct em28xx *dev, u8 reg, u8 * val)
   307 {
   308 	int ret;
   309 	u8 addr = reg & 0x7f;
   310 	if ((ret = em28xx_write_regs(dev, AC97LSB_REG, val, 2)) < 0)
   311 		return ret;
   312 	if ((ret = em28xx_write_regs(dev, AC97ADDR_REG, &addr, 1)) < 0)
   313 		return ret;
   314 	if ((ret = em28xx_read_reg(dev, AC97BUSY_REG)) < 0)
   315 		return ret;
   316 	else if (((u8) ret) & 0x01) {
   317 		em28xx_warn ("AC97 command still being executed: not handled properly!\n");
   318 	}
   319 	return 0;
   320 }
   321 
   322 int em28xx_audio_analog_set(struct em28xx *dev)
   323 {
   324 	char s[2] = { 0x00, 0x00 };
   325 	s[0] |= 0x1f - dev->volume;
   326 	s[1] |= 0x1f - dev->volume;
   327 	if (dev->mute)
   328 		s[1] |= 0x80;
   329 	return em28xx_write_ac97(dev, MASTER_AC97, s);
   330 }
   331 
   332 #if 0
   333 int em28xx_audio_analog_mute(struct em28xx *dev, int mute)
   334 {
   335 	/* (un)mute master mixer with maximum volume level */
   336 	return em28xx_write_ac97(dev, MASTER_AC97,
   337 				 mute ? "\x00\x80" : "\x00\x00");
   338 }
   339 #endif
   340 
   341 int em28xx_colorlevels_set_default(struct em28xx *dev)
   342 {
   343 	em28xx_write_regs(dev, YGAIN_REG, "\x10", 1);	/* contrast */
   344 	em28xx_write_regs(dev, YOFFSET_REG, "\x00", 1);	/* brightness */
   345 	em28xx_write_regs(dev, UVGAIN_REG, "\x10", 1);	/* saturation */
   346 	em28xx_write_regs(dev, UOFFSET_REG, "\x00", 1);
   347 	em28xx_write_regs(dev, VOFFSET_REG, "\x00", 1);
   348 	em28xx_write_regs(dev, SHARPNESS_REG, "\x00", 1);
   349 
   350 	em28xx_write_regs(dev, GAMMA_REG, "\x20", 1);
   351 	em28xx_write_regs(dev, RGAIN_REG, "\x20", 1);
   352 	em28xx_write_regs(dev, GGAIN_REG, "\x20", 1);
   353 	em28xx_write_regs(dev, BGAIN_REG, "\x20", 1);
   354 	em28xx_write_regs(dev, ROFFSET_REG, "\x00", 1);
   355 	em28xx_write_regs(dev, GOFFSET_REG, "\x00", 1);
   356 	return em28xx_write_regs(dev, BOFFSET_REG, "\x00", 1);
   357 }
   358 
   359 int em28xx_capture_start(struct em28xx *dev, int start)
   360 {
   361 	int ret;
   362 	/* FIXME: which is the best order? */
   363 	/* video registers are sampled by VREF */
   364 	if ((ret = em28xx_write_reg_bits(dev, USBSUSP_REG, start ? 0x10 : 0x00,
   365 					  0x10)) < 0)
   366 		return ret;
   367 	/* enable video capture */
   368 	return em28xx_write_regs(dev, VINENABLE_REG, start ? "\x67" : "\x27", 1);
   369 }
   370 
   371 int em28xx_outfmt_set_yuv422(struct em28xx *dev)
   372 {
   373 	em28xx_write_regs(dev, OUTFMT_REG, "\x34", 1);
   374 	em28xx_write_regs(dev, VINMODE_REG, "\x10", 1);
   375 	return em28xx_write_regs(dev, VINCTRL_REG, "\x11", 1);
   376 }
   377 
   378 int em28xx_accumulator_set(struct em28xx *dev, u8 xmin, u8 xmax, u8 ymin,
   379 				  u8 ymax)
   380 {
   381 	em28xx_coredbg("em28xx Scale: (%d,%d)-(%d,%d)\n", xmin, ymin, xmax, ymax);
   382 
   383 	em28xx_write_regs(dev, XMIN_REG, &xmin, 1);
   384 	em28xx_write_regs(dev, XMAX_REG, &xmax, 1);
   385 	em28xx_write_regs(dev, YMIN_REG, &ymin, 1);
   386 	return em28xx_write_regs(dev, YMAX_REG, &ymax, 1);
   387 }
   388 
   389 int em28xx_capture_area_set(struct em28xx *dev, u8 hstart, u8 vstart,
   390 				   u16 width, u16 height)
   391 {
   392 	u8 cwidth = width;
   393 	u8 cheight = height;
   394 	u8 overflow = (height >> 7 & 0x02) | (width >> 8 & 0x01);
   395 
   396 	em28xx_coredbg("em28xx Area Set: (%d,%d)\n", (width | (overflow & 2) << 7),
   397 			(height | (overflow & 1) << 8));
   398 
   399 	em28xx_write_regs(dev, HSTART_REG, &hstart, 1);
   400 	em28xx_write_regs(dev, VSTART_REG, &vstart, 1);
   401 	em28xx_write_regs(dev, CWIDTH_REG, &cwidth, 1);
   402 	em28xx_write_regs(dev, CHEIGHT_REG, &cheight, 1);
   403 	return em28xx_write_regs(dev, OFLOW_REG, &overflow, 1);
   404 }
   405 
   406 int em28xx_scaler_set(struct em28xx *dev, u16 h, u16 v)
   407 {
   408 	u8 mode;
   409 	/* the em2800 scaler only supports scaling down to 50% */
   410 	if(dev->is_em2800)
   411 		mode = (v ? 0x20 : 0x00) | (h ? 0x10 : 0x00);
   412 	else {
   413 		u8 buf[2];
   414 		buf[0] = h;
   415 		buf[1] = h >> 8;
   416 		em28xx_write_regs(dev, HSCALELOW_REG, (char *)buf, 2);
   417 		buf[0] = v;
   418 		buf[1] = v >> 8;
   419 		em28xx_write_regs(dev, VSCALELOW_REG, (char *)buf, 2);
   420 		/* it seems that both H and V scalers must be active to work correctly */
   421 		mode = (h || v)? 0x30: 0x00;
   422 	}
   423 	return em28xx_write_reg_bits(dev, COMPR_REG, mode, 0x30);
   424 }
   425 
   426 /* FIXME: this only function read values from dev */
   427 int em28xx_resolution_set(struct em28xx *dev)
   428 {
   429 	int width, height;
   430 	width = norm_maxw(dev);
   431 	height = norm_maxh(dev) >> 1;
   432 
   433 	em28xx_outfmt_set_yuv422(dev);
   434 	em28xx_accumulator_set(dev, 1, (width - 4) >> 2, 1, (height - 4) >> 2);
   435 	em28xx_capture_area_set(dev, 0, 0, width >> 2, height >> 2);
   436 	return em28xx_scaler_set(dev, dev->hscale, dev->vscale);
   437 }
   438 
   439 
   440 /******************* isoc transfer handling ****************************/
   441 
   442 #ifdef ENABLE_DEBUG_ISOC_FRAMES
   443 static void em28xx_isoc_dump(struct urb *urb, struct pt_regs *regs)
   444 {
   445 	int len = 0;
   446 	int ntrans = 0;
   447 	int i;
   448 
   449 	printk(KERN_DEBUG "isocIrq: sf=%d np=%d ec=%x\n",
   450 	       urb->start_frame, urb->number_of_packets,
   451 	       urb->error_count);
   452 	for (i = 0; i < urb->number_of_packets; i++) {
   453 		unsigned char *buf =
   454 				urb->transfer_buffer +
   455 				urb->iso_frame_desc[i].offset;
   456 		int alen = urb->iso_frame_desc[i].actual_length;
   457 		if (alen > 0) {
   458 			if (buf[0] == 0x88) {
   459 				ntrans++;
   460 				len += alen;
   461 			} else if (buf[0] == 0x22) {
   462 				printk(KERN_DEBUG
   463 						"= l=%d nt=%d bpp=%d\n",
   464 				len - 4 * ntrans, ntrans,
   465 				ntrans == 0 ? 0 : len / ntrans);
   466 				ntrans = 1;
   467 				len = alen;
   468 			} else
   469 				printk(KERN_DEBUG "!\n");
   470 		}
   471 		printk(KERN_DEBUG "   n=%d s=%d al=%d %x\n", i,
   472 		       urb->iso_frame_desc[i].status,
   473 		       urb->iso_frame_desc[i].actual_length,
   474 		       (unsigned int)
   475 				       *((unsigned char *)(urb->transfer_buffer +
   476 				       urb->iso_frame_desc[i].
   477 				       offset)));
   478 	}
   479 }
   480 #endif
   481 
   482 static inline int em28xx_isoc_video(struct em28xx *dev,struct em28xx_frame_t **f,
   483 				    unsigned long *lock_flags, unsigned char buf)
   484 {
   485 	if (!(buf & 0x01)) {
   486 		if ((*f)->state == F_GRABBING) {
   487 			/*previous frame is incomplete */
   488 			if ((*f)->fieldbytesused < dev->field_size) {
   489 				(*f)->state = F_ERROR;
   490 				em28xx_isocdbg ("dropping incomplete bottom field (%i missing bytes)",
   491 					 dev->field_size-(*f)->fieldbytesused);
   492 			} else {
   493 				(*f)->state = F_DONE;
   494 				(*f)->buf.bytesused = dev->frame_size;
   495 			}
   496 		}
   497 		if ((*f)->state == F_DONE || (*f)->state == F_ERROR) {
   498 			/* move current frame to outqueue and get next free buffer from inqueue */
   499 			spin_lock_irqsave(&dev-> queue_lock, *lock_flags);
   500 			list_move_tail(&(*f)->frame, &dev->outqueue);
   501 			if (!list_empty(&dev->inqueue))
   502 				(*f) = list_entry(dev-> inqueue.next,
   503 			struct em28xx_frame_t,frame);
   504 			else
   505 				(*f) = NULL;
   506 			spin_unlock_irqrestore(&dev->queue_lock,*lock_flags);
   507 		}
   508 		if (!(*f)) {
   509 			em28xx_isocdbg ("new frame but no buffer is free");
   510 			return -1;
   511 		}
   512 		do_gettimeofday(&(*f)->buf.timestamp);
   513 		(*f)->buf.sequence = ++dev->frame_count;
   514 		(*f)->buf.field = V4L2_FIELD_INTERLACED;
   515 		(*f)->state = F_GRABBING;
   516 		(*f)->buf.bytesused = 0;
   517 		(*f)->top_field = 1;
   518 		(*f)->fieldbytesused = 0;
   519 	} else {
   520 					/* acquiring bottom field */
   521 		if ((*f)->state == F_GRABBING) {
   522 			if (!(*f)->top_field) {
   523 				(*f)->state = F_ERROR;
   524 				em28xx_isocdbg ("unexpected begin of bottom field; discarding it");
   525 			} else if ((*f)-> fieldbytesused < dev->field_size - 172) {
   526 				(*f)->state = F_ERROR;
   527 				em28xx_isocdbg ("dropping incomplete top field (%i missing bytes)",
   528 					 dev->field_size-(*f)->fieldbytesused);
   529 			} else {
   530 				(*f)->top_field = 0;
   531 				(*f)->fieldbytesused = 0;
   532 			}
   533 		}
   534 	}
   535 	return (0);
   536 }
   537 
   538 static inline void em28xx_isoc_video_copy(struct em28xx *dev,
   539 					  struct em28xx_frame_t **f, unsigned char *buf, int len)
   540 {
   541 	void *fieldstart, *startwrite, *startread;
   542 	int linesdone, currlinedone, offset, lencopy,remain;
   543 
   544 	if(dev->frame_size != (*f)->buf.length){
   545 		em28xx_err("frame_size %i and buf.length %i are different!!!\n",dev->frame_size,(*f)->buf.length);
   546 		return;
   547 	}
   548 
   549 	if ((*f)->fieldbytesused + len > dev->field_size)
   550 		len =dev->field_size - (*f)->fieldbytesused;
   551 
   552 	if (buf[0] != 0x88 && buf[0] != 0x22) {
   553 		em28xx_isocdbg("frame is not complete\n");
   554 		startread = buf;
   555 		len+=4;
   556 	} else
   557 		startread = buf + 4;
   558 
   559 	remain = len;
   560 
   561 	if ((*f)->top_field)
   562 		fieldstart = (*f)->bufmem;
   563 	else
   564 		fieldstart = (*f)->bufmem + dev->bytesperline;
   565 
   566 	linesdone = (*f)->fieldbytesused / dev->bytesperline;
   567 	currlinedone = (*f)->fieldbytesused % dev->bytesperline;
   568 	offset = linesdone * dev->bytesperline * 2 + currlinedone;
   569 	startwrite = fieldstart + offset;
   570 	lencopy = dev->bytesperline - currlinedone;
   571 	lencopy = lencopy > remain ? remain : lencopy;
   572 
   573 	memcpy(startwrite, startread, lencopy);
   574 	remain -= lencopy;
   575 
   576 	while (remain > 0) {
   577 		startwrite += lencopy + dev->bytesperline;
   578 		startread += lencopy;
   579 		if (dev->bytesperline > remain)
   580 			lencopy = remain;
   581 		else
   582 			lencopy = dev->bytesperline;
   583 
   584 		memcpy(startwrite, startread, lencopy);
   585 		remain -= lencopy;
   586 	}
   587 
   588 	(*f)->fieldbytesused += len;
   589 }
   590 
   591 /*
   592  * em28xx_isoIrq()
   593  * handles the incoming isoc urbs and fills the frames from our inqueue
   594  */
   595 void em28xx_isocIrq(struct urb *urb, struct pt_regs *regs)
   596 {
   597 	struct em28xx *dev = urb->context;
   598 	int i, status;
   599 	struct em28xx_frame_t **f;
   600 	unsigned long lock_flags;
   601 
   602 	if (!dev)
   603 		return;
   604 #ifdef ENABLE_DEBUG_ISOC_FRAMES
   605 	if (isoc_debug>1)
   606 		em28xx_isoc_dump(urb, regs);
   607 #endif
   608 
   609 	if (urb->status == -ENOENT)
   610 		return;
   611 
   612 	f = &dev->frame_current;
   613 
   614 	if (dev->stream == STREAM_INTERRUPT) {
   615 		dev->stream = STREAM_OFF;
   616 		if ((*f))
   617 			(*f)->state = F_QUEUED;
   618 		em28xx_isocdbg("stream interrupted");
   619 		wake_up_interruptible(&dev->wait_stream);
   620 	}
   621 
   622 	if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
   623 		return;
   624 
   625 	if (dev->stream == STREAM_ON && !list_empty(&dev->inqueue)) {
   626 		if (!(*f))
   627 			(*f) = list_entry(dev->inqueue.next,
   628 		struct em28xx_frame_t, frame);
   629 
   630 		for (i = 0; i < urb->number_of_packets; i++) {
   631 			unsigned char *buf = urb->transfer_buffer +
   632 					urb->iso_frame_desc[i].offset;
   633 			int len = urb->iso_frame_desc[i].actual_length - 4;
   634 
   635 			if (urb->iso_frame_desc[i].status) {
   636 				em28xx_isocdbg("data error: [%d] len=%d, status=%d", i,
   637 					urb->iso_frame_desc[i].actual_length,
   638 					urb->iso_frame_desc[i].status);
   639 				if (urb->iso_frame_desc[i].status != -EPROTO)
   640 					continue;
   641 			}
   642 			if (urb->iso_frame_desc[i].actual_length <= 0) {
   643 				em28xx_isocdbg("packet %d is empty",i);
   644 				continue;
   645 			}
   646 			if (urb->iso_frame_desc[i].actual_length >
   647 						 dev->max_pkt_size) {
   648 				em28xx_isocdbg("packet bigger than packet size");
   649 				continue;
   650 			}
   651 			/*new frame */
   652 			if (buf[0] == 0x22 && buf[1] == 0x5a) {
   653 				em28xx_isocdbg("Video frame, length=%i!",len);
   654 
   655 				if (em28xx_isoc_video(dev,f,&lock_flags,buf[2]))
   656 				break;
   657 			} else if (buf[0]==0x33 && buf[1]==0x95 && buf[2]==0x00) {
   658 				em28xx_isocdbg("VBI HEADER!!!");
   659 			}
   660 
   661 			/* actual copying */
   662 			if ((*f)->state == F_GRABBING) {
   663 				em28xx_isoc_video_copy(dev,f,buf, len);
   664 			}
   665 		}
   666 	}
   667 
   668 	for (i = 0; i < urb->number_of_packets; i++) {
   669 		urb->iso_frame_desc[i].status = 0;
   670 		urb->iso_frame_desc[i].actual_length = 0;
   671 	}
   672 
   673 	urb->status = 0;
   674 	if ((status = usb_submit_urb(urb, GFP_ATOMIC))) {
   675 		em28xx_errdev("resubmit of urb failed (error=%i)\n", status);
   676 		dev->state |= DEV_MISCONFIGURED;
   677 	}
   678 	wake_up_interruptible(&dev->wait_frame);
   679 	return;
   680 }
   681 
   682 /*
   683  * em28xx_uninit_isoc()
   684  * deallocates the buffers and urbs allocated during em28xx_init_iosc()
   685  */
   686 void em28xx_uninit_isoc(struct em28xx *dev)
   687 {
   688 	int i;
   689 
   690 	for (i = 0; i < EM28XX_NUM_BUFS; i++) {
   691 		if (dev->urb[i]) {
   692 			usb_kill_urb(dev->urb[i]);
   693 			if (dev->transfer_buffer[i]){
   694 				usb_buffer_free(dev->udev,(EM28XX_NUM_PACKETS*dev->max_pkt_size),dev->transfer_buffer[i],dev->urb[i]->transfer_dma);
   695 			}
   696 			usb_free_urb(dev->urb[i]);
   697 		}
   698 		dev->urb[i] = NULL;
   699 		dev->transfer_buffer[i] = NULL;
   700 	}
   701 	em28xx_capture_start(dev, 0);
   702 }
   703 
   704 /*
   705  * em28xx_init_isoc()
   706  * allocates transfer buffers and submits the urbs for isoc transfer
   707  */
   708 int em28xx_init_isoc(struct em28xx *dev)
   709 {
   710 	/* change interface to 3 which allowes the biggest packet sizes */
   711 	int i, errCode;
   712 	const int sb_size = EM28XX_NUM_PACKETS * dev->max_pkt_size;
   713 
   714 	/* reset streaming vars */
   715 	dev->frame_current = NULL;
   716 	dev->frame_count = 0;
   717 
   718 	/* allocate urbs */
   719 	for (i = 0; i < EM28XX_NUM_BUFS; i++) {
   720 		struct urb *urb;
   721 		int j, k;
   722 		/* allocate transfer buffer */
   723 		urb = usb_alloc_urb(EM28XX_NUM_PACKETS, GFP_KERNEL);
   724 		if (!urb){
   725 			em28xx_errdev("cannot alloc urb %i\n", i);
   726 			em28xx_uninit_isoc(dev);
   727 			return -ENOMEM;
   728 		}
   729 		dev->transfer_buffer[i] = usb_buffer_alloc(dev->udev, sb_size, GFP_KERNEL,&urb->transfer_dma);
   730 		if (!dev->transfer_buffer[i]) {
   731 			em28xx_errdev
   732 					("unable to allocate %i bytes for transfer buffer %i\n",
   733 					 sb_size, i);
   734 			em28xx_uninit_isoc(dev);
   735 			return -ENOMEM;
   736 		}
   737 		memset(dev->transfer_buffer[i], 0, sb_size);
   738 		urb->dev = dev->udev;
   739 		urb->context = dev;
   740 		urb->pipe = usb_rcvisocpipe(dev->udev, 0x82);
   741 		urb->transfer_flags = URB_ISO_ASAP;
   742 		urb->interval = 1;
   743 		urb->transfer_buffer = dev->transfer_buffer[i];
   744 		urb->complete = em28xx_isocIrq;
   745 		urb->number_of_packets = EM28XX_NUM_PACKETS;
   746 		urb->transfer_buffer_length = sb_size;
   747 		for (j = k = 0; j < EM28XX_NUM_PACKETS;
   748 				j++, k += dev->max_pkt_size) {
   749 			urb->iso_frame_desc[j].offset = k;
   750 			urb->iso_frame_desc[j].length =
   751 				dev->max_pkt_size;
   752 		}
   753 		dev->urb[i] = urb;
   754 	}
   755 
   756 	/* submit urbs */
   757 	for (i = 0; i < EM28XX_NUM_BUFS; i++) {
   758 		errCode = usb_submit_urb(dev->urb[i], GFP_KERNEL);
   759 		if (errCode) {
   760 			em28xx_errdev("submit of urb %i failed (error=%i)\n", i,
   761 				      errCode);
   762 			em28xx_uninit_isoc(dev);
   763 			return errCode;
   764 		}
   765 	}
   766 
   767 	return 0;
   768 }
   769 
   770 int em28xx_set_alternate(struct em28xx *dev)
   771 {
   772 	int errCode, prev_alt = dev->alt;
   773 	dev->alt = alt;
   774 	if (dev->alt == 0) {
   775 		int i;
   776 #if 1 /* Always try to get the maximum size value */
   777 		for(i=0;i< dev->num_alt; i++)
   778 			if(dev->alt_max_pkt_size[i]>dev->alt_max_pkt_size[dev->alt])
   779 				dev->alt=i;
   780 #endif
   781 #if 0 /* Should be dependent of horizontal size */
   782 		if(dev->is_em2800){ /* always use the max packet size for em2800 based devices */
   783 			for(i=0;i< dev->num_alt; i++)
   784 				if(dev->alt_max_pkt_size[i]>dev->alt_max_pkt_size[dev->alt])
   785 					dev->alt=i;
   786 		}else{
   787 		unsigned int min_pkt_size = dev->field_size / 137;	/* FIXME: empiric magic number */
   788 		em28xx_coredbg("minimum isoc packet size: %u", min_pkt_size);
   789 		dev->alt = 7;
   790 		for (i = 0; i < dev->num_alt; i ++)
   791 			if (dev->alt_max_pkt_size[i] >= min_pkt_size) {
   792 			dev->alt = i;
   793 			break;
   794 			}
   795 		}
   796 #endif
   797 	}
   798 
   799 	if (dev->alt != prev_alt) {
   800 		dev->max_pkt_size = dev->alt_max_pkt_size[dev->alt];
   801 		em28xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n", dev->alt,
   802 		       dev->max_pkt_size);
   803 		errCode = usb_set_interface(dev->udev, 0, dev->alt);
   804 		if (errCode < 0) {
   805 			em28xx_errdev ("cannot change alternate number to %d (error=%i)\n",
   806 							dev->alt, errCode);
   807 			return errCode;
   808 		}
   809 	}
   810 	return 0;
   811 }