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