]> err.no Git - linux-2.6/blob - drivers/media/video/em28xx/em28xx-core.c
[PATCH] v4l: 739: created make changelog to make easier to generate patches
[linux-2.6] / drivers / media / video / em28xx / em28xx-core.c
1 /*
2    em2820-core.c - driver for Empia EM2820/2840 USB video capture devices
3
4    Copyright (C) 2005 Markus Rechberger <mrechberger@gmail.com>
5                       Ludovico Cavedon <cavedon@sssup.it>
6                       Mauro Carvalho Chehab <mchehab@brturbo.com.br>
7
8    Based on the em2800 driver from Sascha Sommer <saschasommer@freenet.de>
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #include <linux/init.h>
26 #include <linux/list.h>
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/usb.h>
30 #include <linux/vmalloc.h>
31
32 #include "em2820.h"
33
34 /* #define ENABLE_DEBUG_ISOC_FRAMES */
35
36 unsigned int core_debug = 0;
37 module_param(core_debug,int,0644);
38 MODULE_PARM_DESC(core_debug,"enable debug messages [core]");
39
40 #define em2820_coredbg(fmt, arg...) do {\
41         if (core_debug) \
42                 printk(KERN_INFO "%s %s :"fmt, \
43                          dev->name, __FUNCTION__ , ##arg); } while (0)
44
45 unsigned int reg_debug = 0;
46 module_param(reg_debug,int,0644);
47 MODULE_PARM_DESC(reg_debug,"enable debug messages [URB reg]");
48
49 #define em2820_regdbg(fmt, arg...) do {\
50         if (reg_debug) \
51                 printk(KERN_INFO "%s %s :"fmt, \
52                          dev->name, __FUNCTION__ , ##arg); } while (0)
53
54 unsigned int isoc_debug = 0;
55 module_param(isoc_debug,int,0644);
56 MODULE_PARM_DESC(core_debug,"enable debug messages [isoc transfers]");
57
58 #define em2820_isocdbg(fmt, arg...) do {\
59         if (isoc_debug) \
60                 printk(KERN_INFO "%s %s :"fmt, \
61                          dev->name, __FUNCTION__ , ##arg); } while (0)
62
63 static int alt = EM2820_PINOUT;
64 module_param(alt, int, 0644);
65 MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint");
66
67 /* ------------------------------------------------------------------ */
68 /* debug help functions                                               */
69
70 static const char *v4l1_ioctls[] = {
71         "0", "CGAP", "GCHAN", "SCHAN", "GTUNER", "STUNER", "GPICT", "SPICT",
72         "CCAPTURE", "GWIN", "SWIN", "GFBUF", "SFBUF", "KEY", "GFREQ",
73         "SFREQ", "GAUDIO", "SAUDIO", "SYNC", "MCAPTURE", "GMBUF", "GUNIT",
74         "GCAPTURE", "SCAPTURE", "SPLAYMODE", "SWRITEMODE", "GPLAYINFO",
75         "SMICROCODE", "GVBIFMT", "SVBIFMT" };
76 #define V4L1_IOCTLS ARRAY_SIZE(v4l1_ioctls)
77
78 static const char *v4l2_ioctls[] = {
79         "QUERYCAP", "1", "ENUM_PIXFMT", "ENUM_FBUFFMT", "G_FMT", "S_FMT",
80         "G_COMP", "S_COMP", "REQBUFS", "QUERYBUF", "G_FBUF", "S_FBUF",
81         "G_WIN", "S_WIN", "PREVIEW", "QBUF", "16", "DQBUF", "STREAMON",
82         "STREAMOFF", "G_PERF", "G_PARM", "S_PARM", "G_STD", "S_STD",
83         "ENUMSTD", "ENUMINPUT", "G_CTRL", "S_CTRL", "G_TUNER", "S_TUNER",
84         "G_FREQ", "S_FREQ", "G_AUDIO", "S_AUDIO", "35", "QUERYCTRL",
85         "QUERYMENU", "G_INPUT", "S_INPUT", "ENUMCVT", "41", "42", "43",
86         "44", "45",  "G_OUTPUT", "S_OUTPUT", "ENUMOUTPUT", "G_AUDOUT",
87         "S_AUDOUT", "ENUMFX", "G_EFFECT", "S_EFFECT", "G_MODULATOR",
88         "S_MODULATOR"
89 };
90 #define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
91
92 void em2820_print_ioctl(char *name, unsigned int cmd)
93 {
94         char *dir;
95
96         switch (_IOC_DIR(cmd)) {
97         case _IOC_NONE:              dir = "--"; break;
98         case _IOC_READ:              dir = "r-"; break;
99         case _IOC_WRITE:             dir = "-w"; break;
100         case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
101         default:                     dir = "??"; break;
102         }
103         switch (_IOC_TYPE(cmd)) {
104         case 'v':
105                 printk(KERN_DEBUG "%s: ioctl 0x%08x (v4l1, %s, VIDIOC%s)\n",
106                        name, cmd, dir, (_IOC_NR(cmd) < V4L1_IOCTLS) ?
107                        v4l1_ioctls[_IOC_NR(cmd)] : "???");
108                 break;
109         case 'V':
110                 printk(KERN_DEBUG "%s: ioctl 0x%08x (v4l2, %s, VIDIOC_%s)\n",
111                        name, cmd, dir, (_IOC_NR(cmd) < V4L2_IOCTLS) ?
112                        v4l2_ioctls[_IOC_NR(cmd)] : "???");
113                 break;
114         default:
115                 printk(KERN_DEBUG "%s: ioctl 0x%08x (???, %s, #%d)\n",
116                        name, cmd, dir, _IOC_NR(cmd));
117         }
118 }
119
120 static void *rvmalloc(size_t size)
121 {
122         void *mem;
123         unsigned long adr;
124
125         size = PAGE_ALIGN(size);
126
127         mem = vmalloc_32((unsigned long)size);
128         if (!mem)
129                 return NULL;
130
131         memset(mem, 0, size);
132
133         adr = (unsigned long)mem;
134         while (size > 0) {
135                 SetPageReserved(vmalloc_to_page((void *)adr));
136                 adr += PAGE_SIZE;
137                 size -= PAGE_SIZE;
138         }
139
140         return mem;
141 }
142
143 static void rvfree(void *mem, size_t size)
144 {
145         unsigned long adr;
146
147         if (!mem)
148                 return;
149
150         size = PAGE_ALIGN(size);
151
152         adr = (unsigned long)mem;
153         while (size > 0) {
154                 ClearPageReserved(vmalloc_to_page((void *)adr));
155                 adr += PAGE_SIZE;
156                 size -= PAGE_SIZE;
157         }
158
159         vfree(mem);
160 }
161
162 /*
163  * em2820_request_buffers()
164  * allocate a number of buffers
165  */
166 u32 em2820_request_buffers(struct em2820 *dev, u32 count)
167 {
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         em2820_coredbg("requested %i buffers with size %i", count, imagesize);
172         if (count > EM2820_NUM_FRAMES)
173                 count = EM2820_NUM_FRAMES;
174
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--;
180         }
181
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;
192         }
193         return dev->num_frames;
194 }
195
196 /*
197  * em2820_queue_unusedframes()
198  * add all frames that are not currently in use to the inbuffer queue
199  */
200 void em2820_queue_unusedframes(struct em2820 *dev)
201 {
202         unsigned long lock_flags;
203         u32 i;
204
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);
211                 }
212 }
213
214 /*
215  * em2820_release_buffers()
216  * free frame buffers
217  */
218 void em2820_release_buffers(struct em2820 *dev)
219 {
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;
224         }
225 }
226
227 /*
228  * em2820_read_reg_req()
229  * reads data from the usb device specifying bRequest
230  */
231 int em2820_read_reg_req_len(struct em2820 *dev, u8 req, u16 reg,
232                                    char *buf, int len)
233 {
234         int ret, byte;
235
236         em2820_regdbg("req=%02x, reg=%02x ", req, reg);
237
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);
241
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]);
246                 }
247                 printk("\n");
248         }
249
250         return ret;
251 }
252
253 /*
254  * em2820_read_reg_req()
255  * reads data from the usb device specifying bRequest
256  */
257 int em2820_read_reg_req(struct em2820 *dev, u8 req, u16 reg)
258 {
259         u8 val;
260         int ret;
261
262         em2820_regdbg("req=%02x, reg=%02x:", req, reg);
263
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);
267
268         if (reg_debug)
269                 printk(ret < 0 ? " failed!\n" : "%02x\n", val);
270
271         if (ret < 0)
272                 return ret;
273
274         return val;
275 }
276
277 int em2820_read_reg(struct em2820 *dev, u16 reg)
278 {
279         return em2820_read_reg_req(dev, USB_REQ_GET_STATUS, reg);
280 }
281
282 /*
283  * em2820_write_regs_req()
284  * sends data to the usb device, specifying bRequest
285  */
286 int em2820_write_regs_req(struct em2820 *dev, u8 req, u16 reg, char *buf,
287                                  int len)
288 {
289         int ret;
290
291         /*usb_control_msg seems to expect a kmalloced buffer */
292         unsigned char *bufs = kmalloc(len, GFP_KERNEL);
293
294         em2820_regdbg("req=%02x reg=%02x:", req, reg);
295
296         if (reg_debug) {
297                 int i;
298                 for (i = 0; i < len; ++i)
299                         printk (" %02x", (unsigned char)buf[i]);
300                 printk ("\n");
301         }
302
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;
312 }
313
314 int em2820_write_regs(struct em2820 *dev, u16 reg, char *buf, int len)
315 {
316         return em2820_write_regs_req(dev, USB_REQ_GET_STATUS, reg, buf, len);
317 }
318
319 /*
320  * em2820_write_reg_bits()
321  * sets only some bits (specified by bitmask) of a register, by first reading
322  * the actual value
323  */
324 int em2820_write_reg_bits(struct em2820 *dev, u16 reg, u8 val,
325                                  u8 bitmask)
326 {
327         int oldval;
328         u8 newval;
329         if ((oldval = em2820_read_reg(dev, reg)) < 0)
330                 return oldval;
331         newval = (((u8) oldval) & ~bitmask) | (val & bitmask);
332         return em2820_write_regs(dev, reg, &newval, 1);
333 }
334
335 /*
336  * em2820_write_ac97()
337  * write a 16 bit value to the specified AC97 address (LSB first!)
338  */
339 int em2820_write_ac97(struct em2820 *dev, u8 reg, u8 * val)
340 {
341         int ret;
342         u8 addr = reg & 0x7f;
343         if ((ret = em2820_write_regs(dev, AC97LSB_REG, val, 2)) < 0)
344                 return ret;
345         if ((ret = em2820_write_regs(dev, AC97ADDR_REG, &addr, 1)) < 0)
346                 return ret;
347         if ((ret = em2820_read_reg(dev, AC97BUSY_REG)) < 0)
348                 return ret;
349         else if (((u8) ret) & 0x01) {
350                 em2820_warn ("AC97 command still being exectuted: not handled properly!\n");
351         }
352         return 0;
353 }
354
355 int em2820_audio_analog_set(struct em2820 *dev)
356 {
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 em2820_write_ac97(dev, MASTER_AC97, s);
363 }
364
365
366 int em2820_colorlevels_set_default(struct em2820 *dev)
367 {
368         em2820_write_regs(dev, YGAIN_REG, "\x10", 1);   /* contrast */
369         em2820_write_regs(dev, YOFFSET_REG, "\x00", 1); /* brightness */
370         em2820_write_regs(dev, UVGAIN_REG, "\x10", 1);  /* saturation */
371         em2820_write_regs(dev, UOFFSET_REG, "\x00", 1);
372         em2820_write_regs(dev, VOFFSET_REG, "\x00", 1);
373         em2820_write_regs(dev, SHARPNESS_REG, "\x00", 1);
374
375         em2820_write_regs(dev, GAMMA_REG, "\x20", 1);
376         em2820_write_regs(dev, RGAIN_REG, "\x20", 1);
377         em2820_write_regs(dev, GGAIN_REG, "\x20", 1);
378         em2820_write_regs(dev, BGAIN_REG, "\x20", 1);
379         em2820_write_regs(dev, ROFFSET_REG, "\x00", 1);
380         em2820_write_regs(dev, GOFFSET_REG, "\x00", 1);
381         return em2820_write_regs(dev, BOFFSET_REG, "\x00", 1);
382 }
383
384 int em2820_capture_start(struct em2820 *dev, int start)
385 {
386         int ret;
387         /* FIXME: which is the best order? */
388         /* video registers are sampled by VREF */
389         if ((ret = em2820_write_reg_bits(dev, USBSUSP_REG, start ? 0x10 : 0x00,
390                                           0x10)) < 0)
391                 return ret;
392         /* enable video capture */
393         return em2820_write_regs(dev, VINENABLE_REG, start ? "\x67" : "\x27", 1);
394 }
395
396 int em2820_outfmt_set_yuv422(struct em2820 *dev)
397 {
398         em2820_write_regs(dev, OUTFMT_REG, "\x34", 1);
399         em2820_write_regs(dev, VINMODE_REG, "\x10", 1);
400         return em2820_write_regs(dev, VINCTRL_REG, "\x11", 1);
401 }
402
403 int em2820_accumulator_set(struct em2820 *dev, u8 xmin, u8 xmax, u8 ymin,
404                                   u8 ymax)
405 {
406         em2820_coredbg("em2820 Scale: (%d,%d)-(%d,%d)\n", xmin, ymin, xmax, ymax);
407
408         em2820_write_regs(dev, XMIN_REG, &xmin, 1);
409         em2820_write_regs(dev, XMAX_REG, &xmax, 1);
410         em2820_write_regs(dev, YMIN_REG, &ymin, 1);
411         return em2820_write_regs(dev, YMAX_REG, &ymax, 1);
412 }
413
414 int em2820_capture_area_set(struct em2820 *dev, u8 hstart, u8 vstart,
415                                    u16 width, u16 height)
416 {
417         u8 cwidth = width;
418         u8 cheight = height;
419         u8 overflow = (height >> 7 & 0x02) | (width >> 8 & 0x01);
420
421         em2820_coredbg("em2820 Area Set: (%d,%d)\n", (width | (overflow & 2) << 7),
422                         (height | (overflow & 1) << 8));
423
424         em2820_write_regs(dev, HSTART_REG, &hstart, 1);
425         em2820_write_regs(dev, VSTART_REG, &vstart, 1);
426         em2820_write_regs(dev, CWIDTH_REG, &cwidth, 1);
427         em2820_write_regs(dev, CHEIGHT_REG, &cheight, 1);
428         return em2820_write_regs(dev, OFLOW_REG, &overflow, 1);
429 }
430
431 int em2820_scaler_set(struct em2820 *dev, u16 h, u16 v)
432 {
433         u8 buf[2];
434         buf[0] = h;
435         buf[1] = h >> 8;
436         em2820_write_regs(dev, HSCALELOW_REG, (char *)buf, 2);
437         buf[0] = v;
438         buf[1] = v >> 8;
439         em2820_write_regs(dev, VSCALELOW_REG, (char *)buf, 2);
440         /* when H and V mixershould be used? */
441         /*      return em2820_write_reg_bits(dev, COMPR_REG, (h ? 0x20 : 0x00) | (v ? 0x10 : 0x00), 0x30); */
442         /* it seems that both H and V scalers must be active to work correctly */
443         return em2820_write_reg_bits(dev, COMPR_REG, h
444                         || v ? 0x30 : 0x00, 0x30);
445 }
446
447 /* FIXME: this only function read values from dev */
448 int em2820_resolution_set(struct em2820 *dev)
449 {
450         int width, height;
451         width = norm_maxw(dev);
452         height = norm_maxh(dev) >> 1;
453
454         em2820_outfmt_set_yuv422(dev);
455         em2820_accumulator_set(dev, 1, (width - 4) >> 2, 1, (height - 4) >> 2);
456         em2820_capture_area_set(dev, 0, 0, width >> 2, height >> 2);
457         return em2820_scaler_set(dev, dev->hscale, dev->vscale);
458 }
459
460
461 /******************* isoc transfer handling ****************************/
462
463 #ifdef ENABLE_DEBUG_ISOC_FRAMES
464 static void em2820_isoc_dump(struct urb *urb, struct pt_regs *regs)
465 {
466         int len = 0;
467         int ntrans = 0;
468         int i;
469
470         printk(KERN_DEBUG "isocIrq: sf=%d np=%d ec=%x\n",
471                urb->start_frame, urb->number_of_packets,
472                urb->error_count);
473         for (i = 0; i < urb->number_of_packets; i++) {
474                 unsigned char *buf =
475                                 urb->transfer_buffer +
476                                 urb->iso_frame_desc[i].offset;
477                 int alen = urb->iso_frame_desc[i].actual_length;
478                 if (alen > 0) {
479                         if (buf[0] == 0x88) {
480                                 ntrans++;
481                                 len += alen;
482                         } else if (buf[0] == 0x22) {
483                                 printk(KERN_DEBUG
484                                                 "= l=%d nt=%d bpp=%d\n",
485                                 len - 4 * ntrans, ntrans,
486                                 ntrans == 0 ? 0 : len / ntrans);
487                                 ntrans = 1;
488                                 len = alen;
489                         } else
490                                 printk(KERN_DEBUG "!\n");
491                 }
492                 printk(KERN_DEBUG "   n=%d s=%d al=%d %x\n", i,
493                        urb->iso_frame_desc[i].status,
494                        urb->iso_frame_desc[i].actual_length,
495                        (unsigned int)
496                                        *((unsigned char *)(urb->transfer_buffer +
497                                        urb->iso_frame_desc[i].
498                                        offset)));
499         }
500 }
501 #endif
502
503 static inline int em2820_isoc_video(struct em2820 *dev,struct em2820_frame_t **f,
504                                     unsigned long *lock_flags, unsigned char buf)
505 {
506         if (!(buf & 0x01)) {
507                 if ((*f)->state == F_GRABBING) {
508                         /*previous frame is incomplete */
509                         if ((*f)->fieldbytesused < dev->field_size) {
510                                 (*f)->state = F_ERROR;
511                                 em2820_isocdbg ("dropping incomplete bottom field (%i missing bytes)",
512                                          dev->field_size-(*f)->fieldbytesused);
513                         } else {
514                                 (*f)->state = F_DONE;
515                                 (*f)->buf.bytesused = dev->frame_size;
516                         }
517                 }
518                 if ((*f)->state == F_DONE || (*f)->state == F_ERROR) {
519                         /* move current frame to outqueue and get next free buffer from inqueue */
520                         spin_lock_irqsave(&dev-> queue_lock, *lock_flags);
521                         list_move_tail(&(*f)->frame, &dev->outqueue);
522                         if (!list_empty(&dev->inqueue))
523                                 (*f) = list_entry(dev-> inqueue.next,
524                         struct em2820_frame_t,frame);
525                         else
526                                 (*f) = NULL;
527                         spin_unlock_irqrestore(&dev->queue_lock,*lock_flags);
528                 }
529                 if (!(*f)) {
530                         em2820_isocdbg ("new frame but no buffer is free");
531                         return -1;
532                 }
533                 do_gettimeofday(&(*f)->buf.timestamp);
534                 (*f)->buf.sequence = ++dev->frame_count;
535                 (*f)->buf.field = V4L2_FIELD_INTERLACED;
536                 (*f)->state = F_GRABBING;
537                 (*f)->buf.bytesused = 0;
538                 (*f)->top_field = 1;
539                 (*f)->fieldbytesused = 0;
540         } else {
541                                         /* acquiring bottom field */
542                 if ((*f)->state == F_GRABBING) {
543                         if (!(*f)->top_field) {
544                                 (*f)->state = F_ERROR;
545                                 em2820_isocdbg ("unexpected begin of bottom field; discarding it");
546                         } else if ((*f)-> fieldbytesused < dev->field_size - 172) {
547                                 (*f)->state = F_ERROR;
548                                 em2820_isocdbg ("dropping incomplete top field (%i missing bytes)",
549                                          dev->field_size-(*f)->fieldbytesused);
550                         } else {
551                                 (*f)->top_field = 0;
552                                 (*f)->fieldbytesused = 0;
553                         }
554                 }
555         }
556         return (0);
557 }
558
559 static inline void em2820_isoc_video_copy(struct em2820 *dev,
560                                           struct em2820_frame_t **f, unsigned char *buf, int len)
561 {
562         void *fieldstart, *startwrite, *startread;
563         int linesdone, currlinedone, offset, lencopy,remain;
564
565         if ((*f)->fieldbytesused + len > dev->field_size)
566                 len =dev->field_size - (*f)->fieldbytesused;
567         remain = len;
568         startread = buf + 4;
569         if ((*f)->top_field)
570                 fieldstart = (*f)->bufmem;
571         else
572                 fieldstart = (*f)->bufmem + dev->bytesperline;
573
574         linesdone = (*f)->fieldbytesused / dev->bytesperline;
575         currlinedone = (*f)->fieldbytesused % dev->bytesperline;
576         offset = linesdone * dev->bytesperline * 2 + currlinedone;
577         startwrite = fieldstart + offset;
578         lencopy = dev->bytesperline - currlinedone;
579         lencopy = lencopy > remain ? remain : lencopy;
580
581         memcpy(startwrite, startread, lencopy);
582         remain -= lencopy;
583
584         while (remain > 0) {
585                 startwrite += lencopy + dev->bytesperline;
586                 startread += lencopy;
587                 if (dev->bytesperline > remain)
588                         lencopy = remain;
589                 else
590                         lencopy = dev->bytesperline;
591
592                 memcpy(startwrite, startread, lencopy);
593                 remain -= lencopy;
594         }
595
596         (*f)->fieldbytesused += len;
597 }
598
599 /*
600  * em2820_isoIrq()
601  * handles the incoming isoc urbs and fills the frames from our inqueue
602  */
603 void em2820_isocIrq(struct urb *urb, struct pt_regs *regs)
604 {
605         struct em2820 *dev = urb->context;
606         int i, status;
607         struct em2820_frame_t **f;
608         unsigned long lock_flags;
609
610         if (!dev)
611                 return;
612 #ifdef ENABLE_DEBUG_ISOC_FRAMES
613         if (isoc_debug>1)
614                 em2820_isoc_dump(urb, regs);
615 #endif
616
617         if (urb->status == -ENOENT)
618                 return;
619
620         f = &dev->frame_current;
621
622         if (dev->stream == STREAM_INTERRUPT) {
623                 dev->stream = STREAM_OFF;
624                 if ((*f))
625                         (*f)->state = F_QUEUED;
626                 em2820_isocdbg("stream interrupted");
627                 wake_up_interruptible(&dev->wait_stream);
628         }
629
630         if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
631                 return;
632
633         if (dev->stream == STREAM_ON && !list_empty(&dev->inqueue)) {
634                 if (!(*f))
635                         (*f) = list_entry(dev->inqueue.next,
636                 struct em2820_frame_t, frame);
637
638                 for (i = 0; i < urb->number_of_packets; i++) {
639                         unsigned char *buf = urb->transfer_buffer +
640                                         urb->iso_frame_desc[i].offset;
641                         int len = urb->iso_frame_desc[i].actual_length - 4;
642
643                         if (urb->iso_frame_desc[i].status) {
644                                 em2820_isocdbg("data error: [%d] len=%d, status=%d", i,
645                                         urb->iso_frame_desc[i].actual_length,
646                                         urb->iso_frame_desc[i].status);
647                                 continue;
648                         }
649                         if (urb->iso_frame_desc[i].actual_length <= 0) {
650                                 em2820_isocdbg("packet %d is empty",i);
651                                 continue;
652                         }
653                         if (urb->iso_frame_desc[i].actual_length >
654                                                  dev->max_pkt_size) {
655                                 em2820_isocdbg("packet bigger than packet size");
656                                 continue;
657                         }
658                         /*new frame */
659                         if (buf[0] == 0x22 && buf[1] == 0x5a) {
660                                 em2820_isocdbg("Video frame, length=%i!",len);
661
662                                 if (em2820_isoc_video(dev,f,&lock_flags,buf[2]))
663                                 break;
664                         } else if (buf[0]==0x33 && buf[1]==0x95 && buf[2]==0x00) {
665                                 em2820_isocdbg("VBI HEADER!!!");
666                         }
667
668                         /* actual copying */
669                         if ((*f)->state == F_GRABBING) {
670                                 em2820_isoc_video_copy(dev,f,buf, len);
671                         }
672                 }
673         }
674
675         for (i = 0; i < urb->number_of_packets; i++) {
676                 urb->iso_frame_desc[i].status = 0;
677                 urb->iso_frame_desc[i].actual_length = 0;
678         }
679
680         urb->status = 0;
681         if ((status = usb_submit_urb(urb, GFP_ATOMIC))) {
682                 em2820_errdev("resubmit of urb failed (error=%i)\n", status);
683                 dev->state |= DEV_MISCONFIGURED;
684         }
685         wake_up_interruptible(&dev->wait_frame);
686         return;
687 }
688
689 /*
690  * em2820_uninit_isoc()
691  * deallocates the buffers and urbs allocated during em2820_init_iosc()
692  */
693 void em2820_uninit_isoc(struct em2820 *dev)
694 {
695         int i;
696
697         for (i = 0; i < EM2820_NUM_BUFS; i++) {
698                 if (dev->urb[i]) {
699                         usb_kill_urb(dev->urb[i]);
700                         usb_free_urb(dev->urb[i]);
701                 }
702                 dev->urb[i] = NULL;
703                 if (dev->transfer_buffer[i])
704                         kfree(dev->transfer_buffer[i]);
705                 dev->transfer_buffer[i] = NULL;
706         }
707         em2820_capture_start(dev, 0);
708 }
709
710 /*
711  * em2820_init_isoc()
712  * allocates transfer buffers and submits the urbs for isoc transfer
713  */
714 int em2820_init_isoc(struct em2820 *dev)
715 {
716         /* change interface to 3 which allowes the biggest packet sizes */
717         int i, errCode;
718         const int sb_size = EM2820_NUM_PACKETS * dev->max_pkt_size;
719
720         /* reset streaming vars */
721         dev->frame_current = NULL;
722         dev->frame_count = 0;
723
724         /* allocate urbs */
725         for (i = 0; i < EM2820_NUM_BUFS; i++) {
726                 struct urb *urb;
727                 int j, k;
728                 /* allocate transfer buffer */
729                 dev->transfer_buffer[i] = kmalloc(sb_size, GFP_KERNEL);
730                 if (!dev->transfer_buffer[i]) {
731                         em2820_errdev
732                                         ("unable to allocate %i bytes for transfer buffer %i\n",
733                                          sb_size, i);
734                         em2820_uninit_isoc(dev);
735                         return -ENOMEM;
736                 }
737                 memset(dev->transfer_buffer[i], 0, sb_size);
738                 urb = usb_alloc_urb(EM2820_NUM_PACKETS, GFP_KERNEL);
739                 if (urb) {
740                         urb->dev = dev->udev;
741                         urb->context = dev;
742                         urb->pipe = usb_rcvisocpipe(dev->udev, 0x82);
743                         urb->transfer_flags = URB_ISO_ASAP;
744                         urb->interval = 1;
745                         urb->transfer_buffer = dev->transfer_buffer[i];
746                         urb->complete = em2820_isocIrq;
747                         urb->number_of_packets = EM2820_NUM_PACKETS;
748                         urb->transfer_buffer_length = sb_size;
749                         for (j = k = 0; j < EM2820_NUM_PACKETS;
750                                                   j++, k += dev->max_pkt_size) {
751                                                           urb->iso_frame_desc[j].offset = k;
752                                                           urb->iso_frame_desc[j].length =
753                                                                           dev->max_pkt_size;
754                                                   }
755                                                   dev->urb[i] = urb;
756                 } else {
757                         em2820_errdev("cannot alloc urb %i\n", i);
758                         em2820_uninit_isoc(dev);
759                         return -ENOMEM;
760                 }
761         }
762
763         /* submit urbs */
764         for (i = 0; i < EM2820_NUM_BUFS; i++) {
765                 errCode = usb_submit_urb(dev->urb[i], GFP_KERNEL);
766                 if (errCode) {
767                         em2820_errdev("submit of urb %i failed (error=%i)\n", i,
768                                       errCode);
769                         em2820_uninit_isoc(dev);
770                         return errCode;
771                 }
772         }
773
774         return 0;
775 }
776
777 int em2820_set_alternate(struct em2820 *dev)
778 {
779         int errCode, prev_alt = dev->alt;
780         dev->alt = alt;
781         if (dev->alt == 0) {
782                 int i;
783                 unsigned int min_pkt_size = dev->field_size / 137;      /* FIXME: empiric magic number */
784                 em2820_coredbg("minimum isoc packet size: %u", min_pkt_size);
785                 dev->alt = 7;
786                 for (i = 1; i < EM2820_MAX_ALT; i += 2) /* FIXME: skip even alternate: why do they not work? */
787                         if (dev->alt_max_pkt_size[i] >= min_pkt_size) {
788                         dev->alt = i;
789                         break;
790                         }
791         }
792
793         if (dev->alt != prev_alt) {
794                 dev->max_pkt_size = dev->alt_max_pkt_size[dev->alt];
795                 em2820_coredbg("setting alternate %d with wMaxPacketSize=%u", dev->alt,
796                        dev->max_pkt_size);
797                 errCode = usb_set_interface(dev->udev, 0, dev->alt);
798                 if (errCode < 0) {
799                         em2820_errdev
800                                         ("cannot change alternate number to %d (error=%i)\n",
801                                          dev->alt, errCode);
802                         return errCode;
803                 }
804         }
805         return 0;
806 }