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