Merge branch 'topic/aoa' into for-linus
[linux-2.6] / drivers / staging / go7007 / go7007-usb.c
1 /*
2  * Copyright (C) 2005-2006 Micronas USA Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License (Version 2) as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software Foundation,
15  * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
16  */
17
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/wait.h>
22 #include <linux/list.h>
23 #include <linux/slab.h>
24 #include <linux/time.h>
25 #include <linux/mm.h>
26 #include <linux/usb.h>
27 #include <linux/i2c.h>
28 #include <asm/byteorder.h>
29 #include <media/tvaudio.h>
30
31 #include "go7007-priv.h"
32 #include "wis-i2c.h"
33
34 static unsigned int assume_endura;
35 module_param(assume_endura, int, 0644);
36 MODULE_PARM_DESC(assume_endura, "when probing fails, hardware is a Pelco Endura");
37
38 /* #define GO7007_USB_DEBUG */
39 /* #define GO7007_I2C_DEBUG */ /* for debugging the EZ-USB I2C adapter */
40
41 #define HPI_STATUS_ADDR 0xFFF4
42 #define INT_PARAM_ADDR  0xFFF6
43 #define INT_INDEX_ADDR  0xFFF8
44
45 /*
46  * Pipes on EZ-USB interface:
47  *      0 snd - Control
48  *      0 rcv - Control
49  *      2 snd - Download firmware (control)
50  *      4 rcv - Read Interrupt (interrupt)
51  *      6 rcv - Read Video (bulk)
52  *      8 rcv - Read Audio (bulk)
53  */
54
55 #define GO7007_USB_EZUSB                (1<<0)
56 #define GO7007_USB_EZUSB_I2C            (1<<1)
57
58 struct go7007_usb_board {
59         unsigned int flags;
60         struct go7007_board_info main_info;
61 };
62
63 struct go7007_usb {
64         struct go7007_usb_board *board;
65         struct semaphore i2c_lock;
66         struct usb_device *usbdev;
67         struct urb *video_urbs[8];
68         struct urb *audio_urbs[8];
69         struct urb *intr_urb;
70 };
71
72 /*********************** Product specification data ***********************/
73
74 static struct go7007_usb_board board_matrix_ii = {
75         .flags          = GO7007_USB_EZUSB,
76         .main_info      = {
77                 .firmware        = "go7007tv.bin",
78                 .flags           = GO7007_BOARD_HAS_AUDIO |
79                                         GO7007_BOARD_USE_ONBOARD_I2C,
80                 .audio_flags     = GO7007_AUDIO_I2S_MODE_1 |
81                                         GO7007_AUDIO_WORD_16,
82                 .audio_rate      = 48000,
83                 .audio_bclk_div  = 8,
84                 .audio_main_div  = 2,
85                 .hpi_buffer_cap  = 7,
86                 .sensor_flags    = GO7007_SENSOR_656 |
87                                         GO7007_SENSOR_VALID_ENABLE |
88                                         GO7007_SENSOR_TV |
89                                         GO7007_SENSOR_VBI |
90                                         GO7007_SENSOR_SCALING,
91                 .num_i2c_devs    = 1,
92                 .i2c_devs        = {
93                         {
94                                 .id     = I2C_DRIVERID_WIS_SAA7115,
95                                 .addr   = 0x20,
96                         },
97                 },
98                 .num_inputs      = 2,
99                 .inputs          = {
100                         {
101                                 .video_input    = 0,
102                                 .name           = "Composite",
103                         },
104                         {
105                                 .video_input    = 9,
106                                 .name           = "S-Video",
107                         },
108                 },
109         },
110 };
111
112 static struct go7007_usb_board board_matrix_reload = {
113         .flags          = GO7007_USB_EZUSB,
114         .main_info      = {
115                 .firmware        = "go7007tv.bin",
116                 .flags           = GO7007_BOARD_HAS_AUDIO |
117                                         GO7007_BOARD_USE_ONBOARD_I2C,
118                 .audio_flags     = GO7007_AUDIO_I2S_MODE_1 |
119                                         GO7007_AUDIO_I2S_MASTER |
120                                         GO7007_AUDIO_WORD_16,
121                 .audio_rate      = 48000,
122                 .audio_bclk_div  = 8,
123                 .audio_main_div  = 2,
124                 .hpi_buffer_cap  = 7,
125                 .sensor_flags    = GO7007_SENSOR_656 |
126                                         GO7007_SENSOR_TV,
127                 .num_i2c_devs    = 1,
128                 .i2c_devs        = {
129                         {
130                                 .id     = I2C_DRIVERID_WIS_SAA7113,
131                                 .addr   = 0x25,
132                         },
133                 },
134                 .num_inputs      = 2,
135                 .inputs          = {
136                         {
137                                 .video_input    = 0,
138                                 .name           = "Composite",
139                         },
140                         {
141                                 .video_input    = 9,
142                                 .name           = "S-Video",
143                         },
144                 },
145         },
146 };
147
148 static struct go7007_usb_board board_star_trek = {
149         .flags          = GO7007_USB_EZUSB | GO7007_USB_EZUSB_I2C,
150         .main_info      = {
151                 .firmware        = "go7007tv.bin",
152                 .flags           = GO7007_BOARD_HAS_AUDIO, /* |
153                                         GO7007_BOARD_HAS_TUNER, */
154                 .sensor_flags    = GO7007_SENSOR_656 |
155                                         GO7007_SENSOR_VALID_ENABLE |
156                                         GO7007_SENSOR_TV |
157                                         GO7007_SENSOR_VBI |
158                                         GO7007_SENSOR_SCALING,
159                 .audio_flags     = GO7007_AUDIO_I2S_MODE_1 |
160                                         GO7007_AUDIO_WORD_16,
161                 .audio_bclk_div  = 8,
162                 .audio_main_div  = 2,
163                 .hpi_buffer_cap  = 7,
164                 .num_i2c_devs    = 1,
165                 .i2c_devs        = {
166                         {
167                                 .id     = I2C_DRIVERID_WIS_SAA7115,
168                                 .addr   = 0x20,
169                         },
170                 },
171                 .num_inputs      = 2,
172                 .inputs          = {
173                         {
174                                 .video_input    = 1,
175                         /*      .audio_input    = AUDIO_EXTERN, */
176                                 .name           = "Composite",
177                         },
178                         {
179                                 .video_input    = 8,
180                         /*      .audio_input    = AUDIO_EXTERN, */
181                                 .name           = "S-Video",
182                         },
183                 /*      {
184                  *              .video_input    = 3,
185                  *              .audio_input    = AUDIO_TUNER,
186                  *              .name           = "Tuner",
187                  *      },
188                  */
189                 },
190         },
191 };
192
193 static struct go7007_usb_board board_px_tv402u = {
194         .flags          = GO7007_USB_EZUSB | GO7007_USB_EZUSB_I2C,
195         .main_info      = {
196                 .firmware        = "go7007tv.bin",
197                 .flags           = GO7007_BOARD_HAS_AUDIO |
198                                         GO7007_BOARD_HAS_TUNER,
199                 .sensor_flags    = GO7007_SENSOR_656 |
200                                         GO7007_SENSOR_VALID_ENABLE |
201                                         GO7007_SENSOR_TV |
202                                         GO7007_SENSOR_VBI |
203                                         GO7007_SENSOR_SCALING,
204                 .audio_flags     = GO7007_AUDIO_I2S_MODE_1 |
205                                         GO7007_AUDIO_WORD_16,
206                 .audio_bclk_div  = 8,
207                 .audio_main_div  = 2,
208                 .hpi_buffer_cap  = 7,
209                 .num_i2c_devs    = 3,
210                 .i2c_devs        = {
211                         {
212                                 .id     = I2C_DRIVERID_WIS_SAA7115,
213                                 .addr   = 0x20,
214                         },
215                         {
216                                 .id     = I2C_DRIVERID_WIS_UDA1342,
217                                 .addr   = 0x1a,
218                         },
219                         {
220                                 .id     = I2C_DRIVERID_WIS_SONY_TUNER,
221                                 .addr   = 0x60,
222                         },
223                 },
224                 .num_inputs      = 3,
225                 .inputs          = {
226                         {
227                                 .video_input    = 1,
228                                 .audio_input    = TVAUDIO_INPUT_EXTERN,
229                                 .name           = "Composite",
230                         },
231                         {
232                                 .video_input    = 8,
233                                 .audio_input    = TVAUDIO_INPUT_EXTERN,
234                                 .name           = "S-Video",
235                         },
236                         {
237                                 .video_input    = 3,
238                                 .audio_input    = TVAUDIO_INPUT_TUNER,
239                                 .name           = "Tuner",
240                         },
241                 },
242         },
243 };
244
245 static struct go7007_usb_board board_xmen = {
246         .flags          = 0,
247         .main_info      = {
248                 .firmware         = "go7007tv.bin",
249                 .flags            = GO7007_BOARD_USE_ONBOARD_I2C,
250                 .hpi_buffer_cap   = 0,
251                 .sensor_flags     = GO7007_SENSOR_VREF_POLAR,
252                 .sensor_width     = 320,
253                 .sensor_height    = 240,
254                 .sensor_framerate = 30030,
255                 .audio_flags      = GO7007_AUDIO_ONE_CHANNEL |
256                                         GO7007_AUDIO_I2S_MODE_3 |
257                                         GO7007_AUDIO_WORD_14 |
258                                         GO7007_AUDIO_I2S_MASTER |
259                                         GO7007_AUDIO_BCLK_POLAR |
260                                         GO7007_AUDIO_OKI_MODE,
261                 .audio_rate       = 8000,
262                 .audio_bclk_div   = 48,
263                 .audio_main_div   = 1,
264                 .num_i2c_devs     = 1,
265                 .i2c_devs         = {
266                         {
267                                 .id     = I2C_DRIVERID_WIS_OV7640,
268                                 .addr   = 0x21,
269                         },
270                 },
271                 .num_inputs       = 1,
272                 .inputs           = {
273                         {
274                                 .name           = "Camera",
275                         },
276                 },
277         },
278 };
279
280 static struct go7007_usb_board board_matrix_revolution = {
281         .flags          = GO7007_USB_EZUSB,
282         .main_info      = {
283                 .firmware        = "go7007tv.bin",
284                 .flags           = GO7007_BOARD_HAS_AUDIO |
285                                         GO7007_BOARD_USE_ONBOARD_I2C,
286                 .audio_flags     = GO7007_AUDIO_I2S_MODE_1 |
287                                         GO7007_AUDIO_I2S_MASTER |
288                                         GO7007_AUDIO_WORD_16,
289                 .audio_rate      = 48000,
290                 .audio_bclk_div  = 8,
291                 .audio_main_div  = 2,
292                 .hpi_buffer_cap  = 7,
293                 .sensor_flags    = GO7007_SENSOR_656 |
294                                         GO7007_SENSOR_TV |
295                                         GO7007_SENSOR_VBI,
296                 .num_i2c_devs    = 1,
297                 .i2c_devs        = {
298                         {
299                                 .id     = I2C_DRIVERID_WIS_TW9903,
300                                 .addr   = 0x44,
301                         },
302                 },
303                 .num_inputs      = 2,
304                 .inputs          = {
305                         {
306                                 .video_input    = 2,
307                                 .name           = "Composite",
308                         },
309                         {
310                                 .video_input    = 8,
311                                 .name           = "S-Video",
312                         },
313                 },
314         },
315 };
316
317 static struct go7007_usb_board board_lifeview_lr192 = {
318         .flags          = GO7007_USB_EZUSB,
319         .main_info      = {
320                 .firmware        = "go7007tv.bin",
321                 .flags           = GO7007_BOARD_HAS_AUDIO |
322                                         GO7007_BOARD_USE_ONBOARD_I2C,
323                 .audio_flags     = GO7007_AUDIO_I2S_MODE_1 |
324                                         GO7007_AUDIO_WORD_16,
325                 .audio_rate      = 48000,
326                 .audio_bclk_div  = 8,
327                 .audio_main_div  = 2,
328                 .hpi_buffer_cap  = 7,
329                 .sensor_flags    = GO7007_SENSOR_656 |
330                                         GO7007_SENSOR_VALID_ENABLE |
331                                         GO7007_SENSOR_TV |
332                                         GO7007_SENSOR_VBI |
333                                         GO7007_SENSOR_SCALING,
334                 .num_i2c_devs    = 0,
335                 .num_inputs      = 1,
336                 .inputs          = {
337                         {
338                                 .video_input    = 0,
339                                 .name           = "Composite",
340                         },
341                 },
342         },
343 };
344
345 static struct go7007_usb_board board_endura = {
346         .flags          = 0,
347         .main_info      = {
348                 .firmware        = "go7007tv.bin",
349                 .flags           = 0,
350                 .audio_flags     = GO7007_AUDIO_I2S_MODE_1 |
351                                         GO7007_AUDIO_I2S_MASTER |
352                                         GO7007_AUDIO_WORD_16,
353                 .audio_rate      = 8000,
354                 .audio_bclk_div  = 48,
355                 .audio_main_div  = 8,
356                 .hpi_buffer_cap  = 0,
357                 .sensor_flags    = GO7007_SENSOR_656 |
358                                         GO7007_SENSOR_TV,
359                 .sensor_h_offset = 8,
360                 .num_i2c_devs    = 0,
361                 .num_inputs      = 1,
362                 .inputs          = {
363                         {
364                                 .name           = "Camera",
365                         },
366                 },
367         },
368 };
369
370 static struct go7007_usb_board board_adlink_mpg24 = {
371         .flags          = 0,
372         .main_info      = {
373                 .firmware        = "go7007tv.bin",
374                 .flags           = GO7007_BOARD_USE_ONBOARD_I2C,
375                 .audio_flags     = GO7007_AUDIO_I2S_MODE_1 |
376                                         GO7007_AUDIO_I2S_MASTER |
377                                         GO7007_AUDIO_WORD_16,
378                 .audio_rate      = 48000,
379                 .audio_bclk_div  = 8,
380                 .audio_main_div  = 2,
381                 .hpi_buffer_cap  = 0,
382                 .sensor_flags    = GO7007_SENSOR_656 |
383                                         GO7007_SENSOR_TV |
384                                         GO7007_SENSOR_VBI,
385                 .num_i2c_devs    = 1,
386                 .i2c_devs        = {
387                         {
388                                 .id     = I2C_DRIVERID_WIS_TW2804,
389                                 .addr   = 0x00, /* yes, really */
390                         },
391                 },
392                 .num_inputs      = 1,
393                 .inputs          = {
394                         {
395                                 .name           = "Composite",
396                         },
397                 },
398         },
399 };
400
401 static struct go7007_usb_board board_sensoray_2250 = {
402         .flags          = GO7007_USB_EZUSB | GO7007_USB_EZUSB_I2C,
403         .main_info      = {
404                 .firmware        = "go7007tv.bin",
405                 .audio_flags     = GO7007_AUDIO_I2S_MODE_1 |
406                                         GO7007_AUDIO_I2S_MASTER |
407                                         GO7007_AUDIO_WORD_16,
408                 .flags           = GO7007_BOARD_HAS_AUDIO,
409                 .audio_rate      = 48000,
410                 .audio_bclk_div  = 8,
411                 .audio_main_div  = 2,
412                 .hpi_buffer_cap  = 7,
413                 .sensor_flags    = GO7007_SENSOR_656 |
414                                         GO7007_SENSOR_TV,
415                 .num_i2c_devs    = 1,
416                 .i2c_devs        = {
417                         {
418                                 .id     = I2C_DRIVERID_S2250,
419                                 .addr   = 0x34,
420                         },
421                 },
422                 .num_inputs      = 2,
423                 .inputs          = {
424                         {
425                                 .video_input    = 0,
426                                 .name           = "Composite",
427                         },
428                         {
429                                 .video_input    = 1,
430                                 .name           = "S-Video",
431                         },
432                 },
433         },
434 };
435
436 static struct usb_device_id go7007_usb_id_table[] = {
437         {
438                 .match_flags    = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION |
439                                         USB_DEVICE_ID_MATCH_INT_INFO,
440                 .idVendor       = 0x0eb1,  /* Vendor ID of WIS Technologies */
441                 .idProduct      = 0x7007,  /* Product ID of GO7007SB chip */
442                 .bcdDevice_lo   = 0x200,   /* Revision number of XMen */
443                 .bcdDevice_hi   = 0x200,
444                 .bInterfaceClass        = 255,
445                 .bInterfaceSubClass     = 0,
446                 .bInterfaceProtocol     = 255,
447                 .driver_info    = (kernel_ulong_t)GO7007_BOARDID_XMEN,
448         },
449         {
450                 .match_flags    = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
451                 .idVendor       = 0x0eb1,  /* Vendor ID of WIS Technologies */
452                 .idProduct      = 0x7007,  /* Product ID of GO7007SB chip */
453                 .bcdDevice_lo   = 0x202,   /* Revision number of Matrix II */
454                 .bcdDevice_hi   = 0x202,
455                 .driver_info    = (kernel_ulong_t)GO7007_BOARDID_MATRIX_II,
456         },
457         {
458                 .match_flags    = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
459                 .idVendor       = 0x0eb1,  /* Vendor ID of WIS Technologies */
460                 .idProduct      = 0x7007,  /* Product ID of GO7007SB chip */
461                 .bcdDevice_lo   = 0x204,   /* Revision number of Matrix */
462                 .bcdDevice_hi   = 0x204,   /*     Reloaded */
463                 .driver_info    = (kernel_ulong_t)GO7007_BOARDID_MATRIX_RELOAD,
464         },
465         {
466                 .match_flags    = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION |
467                                         USB_DEVICE_ID_MATCH_INT_INFO,
468                 .idVendor       = 0x0eb1,  /* Vendor ID of WIS Technologies */
469                 .idProduct      = 0x7007,  /* Product ID of GO7007SB chip */
470                 .bcdDevice_lo   = 0x205,   /* Revision number of XMen-II */
471                 .bcdDevice_hi   = 0x205,
472                 .bInterfaceClass        = 255,
473                 .bInterfaceSubClass     = 0,
474                 .bInterfaceProtocol     = 255,
475                 .driver_info    = (kernel_ulong_t)GO7007_BOARDID_XMEN_II,
476         },
477         {
478                 .match_flags    = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
479                 .idVendor       = 0x0eb1,  /* Vendor ID of WIS Technologies */
480                 .idProduct      = 0x7007,  /* Product ID of GO7007SB chip */
481                 .bcdDevice_lo   = 0x208,   /* Revision number of Star Trek */
482                 .bcdDevice_hi   = 0x208,
483                 .driver_info    = (kernel_ulong_t)GO7007_BOARDID_STAR_TREK,
484         },
485         {
486                 .match_flags    = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION |
487                                         USB_DEVICE_ID_MATCH_INT_INFO,
488                 .idVendor       = 0x0eb1,  /* Vendor ID of WIS Technologies */
489                 .idProduct      = 0x7007,  /* Product ID of GO7007SB chip */
490                 .bcdDevice_lo   = 0x209,   /* Revision number of XMen-III */
491                 .bcdDevice_hi   = 0x209,
492                 .bInterfaceClass        = 255,
493                 .bInterfaceSubClass     = 0,
494                 .bInterfaceProtocol     = 255,
495                 .driver_info    = (kernel_ulong_t)GO7007_BOARDID_XMEN_III,
496         },
497         {
498                 .match_flags    = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
499                 .idVendor       = 0x0eb1,  /* Vendor ID of WIS Technologies */
500                 .idProduct      = 0x7007,  /* Product ID of GO7007SB chip */
501                 .bcdDevice_lo   = 0x210,   /* Revision number of Matrix */
502                 .bcdDevice_hi   = 0x210,   /*     Revolution */
503                 .driver_info    = (kernel_ulong_t)GO7007_BOARDID_MATRIX_REV,
504         },
505         {
506                 .match_flags    = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
507                 .idVendor       = 0x093b,  /* Vendor ID of Plextor */
508                 .idProduct      = 0xa102,  /* Product ID of M402U */
509                 .bcdDevice_lo   = 0x1,     /* revision number of Blueberry */
510                 .bcdDevice_hi   = 0x1,
511                 .driver_info    = (kernel_ulong_t)GO7007_BOARDID_PX_M402U,
512         },
513         {
514                 .match_flags    = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
515                 .idVendor       = 0x093b,  /* Vendor ID of Plextor */
516                 .idProduct      = 0xa104,  /* Product ID of TV402U */
517                 .bcdDevice_lo   = 0x1,
518                 .bcdDevice_hi   = 0x1,
519                 .driver_info    = (kernel_ulong_t)GO7007_BOARDID_PX_TV402U_ANY,
520         },
521         {
522                 .match_flags    = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
523                 .idVendor       = 0x10fd,  /* Vendor ID of Anubis Electronics */
524                 .idProduct      = 0xde00,  /* Product ID of Lifeview LR192 */
525                 .bcdDevice_lo   = 0x1,
526                 .bcdDevice_hi   = 0x1,
527                 .driver_info    = (kernel_ulong_t)GO7007_BOARDID_LIFEVIEW_LR192,
528         },
529         {
530                 .match_flags    = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
531                 .idVendor       = 0x1943,  /* Vendor ID Sensoray */
532                 .idProduct      = 0x2250,  /* Product ID of 2250/2251 */
533                 .bcdDevice_lo   = 0x1,
534                 .bcdDevice_hi   = 0x1,
535                 .driver_info    = (kernel_ulong_t)GO7007_BOARDID_SENSORAY_2250,
536         },
537         { }                                     /* Terminating entry */
538 };
539
540 MODULE_DEVICE_TABLE(usb, go7007_usb_id_table);
541
542 /********************* Driver for EZ-USB HPI interface *********************/
543
544 static int go7007_usb_vendor_request(struct go7007 *go, int request,
545                 int value, int index, void *transfer_buffer, int length, int in)
546 {
547         struct go7007_usb *usb = go->hpi_context;
548         int timeout = 5000;
549
550         if (in) {
551                 return usb_control_msg(usb->usbdev,
552                                 usb_rcvctrlpipe(usb->usbdev, 0), request,
553                                 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
554                                 value, index, transfer_buffer, length, timeout);
555         } else {
556                 return usb_control_msg(usb->usbdev,
557                                 usb_sndctrlpipe(usb->usbdev, 0), request,
558                                 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
559                                 value, index, transfer_buffer, length, timeout);
560         }
561 }
562
563 static int go7007_usb_interface_reset(struct go7007 *go)
564 {
565         struct go7007_usb *usb = go->hpi_context;
566         u16 intr_val, intr_data;
567
568         /* Reset encoder */
569         if (go7007_write_interrupt(go, 0x0001, 0x0001) < 0)
570                 return -1;
571         msleep(100);
572
573         if (usb->board->flags & GO7007_USB_EZUSB) {
574                 /* Reset buffer in EZ-USB */
575 #ifdef GO7007_USB_DEBUG
576                 printk(KERN_DEBUG "go7007-usb: resetting EZ-USB buffers\n");
577 #endif
578                 if (go7007_usb_vendor_request(go, 0x10, 0, 0, NULL, 0, 0) < 0 ||
579                     go7007_usb_vendor_request(go, 0x10, 0, 0, NULL, 0, 0) < 0)
580                         return -1;
581
582                 /* Reset encoder again */
583                 if (go7007_write_interrupt(go, 0x0001, 0x0001) < 0)
584                         return -1;
585                 msleep(100);
586         }
587
588         /* Wait for an interrupt to indicate successful hardware reset */
589         if (go7007_read_interrupt(go, &intr_val, &intr_data) < 0 ||
590                         (intr_val & ~0x1) != 0x55aa) {
591                 printk(KERN_ERR
592                         "go7007-usb: unable to reset the USB interface\n");
593                 return -1;
594         }
595         return 0;
596 }
597
598 static int go7007_usb_ezusb_write_interrupt(struct go7007 *go,
599                                                 int addr, int data)
600 {
601         struct go7007_usb *usb = go->hpi_context;
602         int i, r;
603         u16 status_reg;
604         int timeout = 500;
605
606 #ifdef GO7007_USB_DEBUG
607         printk(KERN_DEBUG
608                 "go7007-usb: WriteInterrupt: %04x %04x\n", addr, data);
609 #endif
610
611         for (i = 0; i < 100; ++i) {
612                 r = usb_control_msg(usb->usbdev,
613                                 usb_rcvctrlpipe(usb->usbdev, 0), 0x14,
614                                 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
615                                 0, HPI_STATUS_ADDR, &status_reg,
616                                 sizeof(status_reg), timeout);
617                 if (r < 0)
618                         goto write_int_error;
619                 __le16_to_cpus(&status_reg);
620                 if (!(status_reg & 0x0010))
621                         break;
622                 msleep(10);
623         }
624         if (i == 100) {
625                 printk(KERN_ERR
626                         "go7007-usb: device is hung, status reg = 0x%04x\n",
627                         status_reg);
628                 return -1;
629         }
630         r = usb_control_msg(usb->usbdev, usb_sndctrlpipe(usb->usbdev, 0), 0x12,
631                         USB_TYPE_VENDOR | USB_RECIP_DEVICE, data,
632                         INT_PARAM_ADDR, NULL, 0, timeout);
633         if (r < 0)
634                 goto write_int_error;
635         r = usb_control_msg(usb->usbdev, usb_sndctrlpipe(usb->usbdev, 0),
636                         0x12, USB_TYPE_VENDOR | USB_RECIP_DEVICE, addr,
637                         INT_INDEX_ADDR, NULL, 0, timeout);
638         if (r < 0)
639                 goto write_int_error;
640         return 0;
641
642 write_int_error:
643         printk(KERN_ERR "go7007-usb: error in WriteInterrupt: %d\n", r);
644         return r;
645 }
646
647 static int go7007_usb_onboard_write_interrupt(struct go7007 *go,
648                                                 int addr, int data)
649 {
650         struct go7007_usb *usb = go->hpi_context;
651         u8 *tbuf;
652         int r;
653         int timeout = 500;
654
655 #ifdef GO7007_USB_DEBUG
656         printk(KERN_DEBUG
657                 "go7007-usb: WriteInterrupt: %04x %04x\n", addr, data);
658 #endif
659
660         tbuf = kmalloc(8, GFP_KERNEL);
661         if (tbuf == NULL)
662                 return -ENOMEM;
663         memset(tbuf, 0, 8);
664         tbuf[0] = data & 0xff;
665         tbuf[1] = data >> 8;
666         tbuf[2] = addr & 0xff;
667         tbuf[3] = addr >> 8;
668         r = usb_control_msg(usb->usbdev, usb_sndctrlpipe(usb->usbdev, 2), 0x00,
669                         USB_TYPE_VENDOR | USB_RECIP_ENDPOINT, 0x55aa,
670                         0xf0f0, tbuf, 8, timeout);
671         kfree(tbuf);
672         if (r < 0) {
673                 printk(KERN_ERR "go7007-usb: error in WriteInterrupt: %d\n", r);
674                 return r;
675         }
676         return 0;
677 }
678
679 static void go7007_usb_readinterrupt_complete(struct urb *urb)
680 {
681         struct go7007 *go = (struct go7007 *)urb->context;
682         u16 *regs = (u16 *)urb->transfer_buffer;
683         int status = urb->status;
684
685         if (status) {
686                 if (status != -ESHUTDOWN &&
687                                 go->status != STATUS_SHUTDOWN) {
688                         printk(KERN_ERR
689                                 "go7007-usb: error in read interrupt: %d\n",
690                                 urb->status);
691                 } else {
692                         wake_up(&go->interrupt_waitq);
693                         return;
694                 }
695         } else if (urb->actual_length != urb->transfer_buffer_length) {
696                 printk(KERN_ERR "go7007-usb: short read in interrupt pipe!\n");
697         } else {
698                 go->interrupt_available = 1;
699                 go->interrupt_data = __le16_to_cpu(regs[0]);
700                 go->interrupt_value = __le16_to_cpu(regs[1]);
701 #ifdef GO7007_USB_DEBUG
702                 printk(KERN_DEBUG "go7007-usb: ReadInterrupt: %04x %04x\n",
703                                 go->interrupt_value, go->interrupt_data);
704 #endif
705         }
706
707         wake_up(&go->interrupt_waitq);
708 }
709
710 static int go7007_usb_read_interrupt(struct go7007 *go)
711 {
712         struct go7007_usb *usb = go->hpi_context;
713         int r;
714
715         r = usb_submit_urb(usb->intr_urb, GFP_KERNEL);
716         if (r < 0) {
717                 printk(KERN_ERR
718                         "go7007-usb: unable to submit interrupt urb: %d\n", r);
719                 return r;
720         }
721         return 0;
722 }
723
724 static void go7007_usb_read_video_pipe_complete(struct urb *urb)
725 {
726         struct go7007 *go = (struct go7007 *)urb->context;
727         int r, status = urb-> status;
728
729         if (!go->streaming) {
730                 wake_up_interruptible(&go->frame_waitq);
731                 return;
732         }
733         if (status) {
734                 printk(KERN_ERR "go7007-usb: error in video pipe: %d\n", status);
735                 return;
736         }
737         if (urb->actual_length != urb->transfer_buffer_length) {
738                 printk(KERN_ERR "go7007-usb: short read in video pipe!\n");
739                 return;
740         }
741         go7007_parse_video_stream(go, urb->transfer_buffer, urb->actual_length);
742         r = usb_submit_urb(urb, GFP_ATOMIC);
743         if (r < 0)
744                 printk(KERN_ERR "go7007-usb: error in video pipe: %d\n", r);
745 }
746
747 static void go7007_usb_read_audio_pipe_complete(struct urb *urb)
748 {
749         struct go7007 *go = (struct go7007 *)urb->context;
750         int r, status = urb->status;
751
752         if (!go->streaming)
753                 return;
754         if (status) {
755                 printk(KERN_ERR "go7007-usb: error in audio pipe: %d\n", status);
756                 return;
757         }
758         if (urb->actual_length != urb->transfer_buffer_length) {
759                 printk(KERN_ERR "go7007-usb: short read in audio pipe!\n");
760                 return;
761         }
762         if (go->audio_deliver != NULL)
763                 go->audio_deliver(go, urb->transfer_buffer, urb->actual_length);
764         r = usb_submit_urb(urb, GFP_ATOMIC);
765         if (r < 0)
766                 printk(KERN_ERR "go7007-usb: error in audio pipe: %d\n", r);
767 }
768
769 static int go7007_usb_stream_start(struct go7007 *go)
770 {
771         struct go7007_usb *usb = go->hpi_context;
772         int i, r;
773
774         for (i = 0; i < 8; ++i) {
775                 r = usb_submit_urb(usb->video_urbs[i], GFP_KERNEL);
776                 if (r < 0) {
777                         printk(KERN_ERR "go7007-usb: error submitting video "
778                                         "urb %d: %d\n", i, r);
779                         goto video_submit_failed;
780                 }
781         }
782         if (!go->audio_enabled)
783                 return 0;
784
785         for (i = 0; i < 8; ++i) {
786                 r = usb_submit_urb(usb->audio_urbs[i], GFP_KERNEL);
787                 if (r < 0) {
788                         printk(KERN_ERR "go7007-usb: error submitting audio "
789                                         "urb %d: %d\n", i, r);
790                         goto audio_submit_failed;
791                 }
792         }
793         return 0;
794
795 audio_submit_failed:
796         for (i = 0; i < 7; ++i)
797                 usb_kill_urb(usb->audio_urbs[i]);
798 video_submit_failed:
799         for (i = 0; i < 8; ++i)
800                 usb_kill_urb(usb->video_urbs[i]);
801         return -1;
802 }
803
804 static int go7007_usb_stream_stop(struct go7007 *go)
805 {
806         struct go7007_usb *usb = go->hpi_context;
807         int i;
808
809         if (go->status == STATUS_SHUTDOWN)
810                 return 0;
811         for (i = 0; i < 8; ++i)
812                 usb_kill_urb(usb->video_urbs[i]);
813         if (go->audio_enabled)
814                 for (i = 0; i < 8; ++i)
815                         usb_kill_urb(usb->audio_urbs[i]);
816         return 0;
817 }
818
819 static int go7007_usb_send_firmware(struct go7007 *go, u8 *data, int len)
820 {
821         struct go7007_usb *usb = go->hpi_context;
822         int transferred, pipe;
823         int timeout = 500;
824
825 #ifdef GO7007_USB_DEBUG
826         printk(KERN_DEBUG "go7007-usb: DownloadBuffer sending %d bytes\n", len);
827 #endif
828
829         if (usb->board->flags & GO7007_USB_EZUSB)
830                 pipe = usb_sndbulkpipe(usb->usbdev, 2);
831         else
832                 pipe = usb_sndbulkpipe(usb->usbdev, 3);
833
834         return usb_bulk_msg(usb->usbdev, pipe, data, len,
835                                         &transferred, timeout);
836 }
837
838 static struct go7007_hpi_ops go7007_usb_ezusb_hpi_ops = {
839         .interface_reset        = go7007_usb_interface_reset,
840         .write_interrupt        = go7007_usb_ezusb_write_interrupt,
841         .read_interrupt         = go7007_usb_read_interrupt,
842         .stream_start           = go7007_usb_stream_start,
843         .stream_stop            = go7007_usb_stream_stop,
844         .send_firmware          = go7007_usb_send_firmware,
845 };
846
847 static struct go7007_hpi_ops go7007_usb_onboard_hpi_ops = {
848         .interface_reset        = go7007_usb_interface_reset,
849         .write_interrupt        = go7007_usb_onboard_write_interrupt,
850         .read_interrupt         = go7007_usb_read_interrupt,
851         .stream_start           = go7007_usb_stream_start,
852         .stream_stop            = go7007_usb_stream_stop,
853         .send_firmware          = go7007_usb_send_firmware,
854 };
855
856 /********************* Driver for EZ-USB I2C adapter *********************/
857
858 static int go7007_usb_i2c_master_xfer(struct i2c_adapter *adapter,
859                                         struct i2c_msg msgs[], int num)
860 {
861         struct go7007 *go = i2c_get_adapdata(adapter);
862         struct go7007_usb *usb = go->hpi_context;
863         u8 buf[16];
864         int buf_len, i;
865         int ret = -1;
866
867         if (go->status == STATUS_SHUTDOWN)
868                 return -1;
869
870         down(&usb->i2c_lock);
871
872         for (i = 0; i < num; ++i) {
873                 /* The hardware command is "write some bytes then read some
874                  * bytes", so we try to coalesce a write followed by a read
875                  * into a single USB transaction */
876                 if (i + 1 < num && msgs[i].addr == msgs[i + 1].addr &&
877                                 !(msgs[i].flags & I2C_M_RD) &&
878                                 (msgs[i + 1].flags & I2C_M_RD)) {
879 #ifdef GO7007_I2C_DEBUG
880                         printk(KERN_DEBUG "go7007-usb: i2c write/read %d/%d "
881                                         "bytes on %02x\n", msgs[i].len,
882                                         msgs[i + 1].len, msgs[i].addr);
883 #endif
884                         buf[0] = 0x01;
885                         buf[1] = msgs[i].len + 1;
886                         buf[2] = msgs[i].addr << 1;
887                         memcpy(&buf[3], msgs[i].buf, msgs[i].len);
888                         buf_len = msgs[i].len + 3;
889                         buf[buf_len++] = msgs[++i].len;
890                 } else if (msgs[i].flags & I2C_M_RD) {
891 #ifdef GO7007_I2C_DEBUG
892                         printk(KERN_DEBUG "go7007-usb: i2c read %d "
893                                         "bytes on %02x\n", msgs[i].len,
894                                         msgs[i].addr);
895 #endif
896                         buf[0] = 0x01;
897                         buf[1] = 1;
898                         buf[2] = msgs[i].addr << 1;
899                         buf[3] = msgs[i].len;
900                         buf_len = 4;
901                 } else {
902 #ifdef GO7007_I2C_DEBUG
903                         printk(KERN_DEBUG "go7007-usb: i2c write %d "
904                                         "bytes on %02x\n", msgs[i].len,
905                                         msgs[i].addr);
906 #endif
907                         buf[0] = 0x00;
908                         buf[1] = msgs[i].len + 1;
909                         buf[2] = msgs[i].addr << 1;
910                         memcpy(&buf[3], msgs[i].buf, msgs[i].len);
911                         buf_len = msgs[i].len + 3;
912                         buf[buf_len++] = 0;
913                 }
914                 if (go7007_usb_vendor_request(go, 0x24, 0, 0,
915                                                 buf, buf_len, 0) < 0)
916                         goto i2c_done;
917                 if (msgs[i].flags & I2C_M_RD) {
918                         memset(buf, 0, sizeof(buf));
919                         if (go7007_usb_vendor_request(go, 0x25, 0, 0, buf,
920                                                 msgs[i].len + 1, 1) < 0)
921                                 goto i2c_done;
922                         memcpy(msgs[i].buf, buf + 1, msgs[i].len);
923                 }
924         }
925         ret = 0;
926
927 i2c_done:
928         up(&usb->i2c_lock);
929         return ret;
930 }
931
932 static u32 go7007_usb_functionality(struct i2c_adapter *adapter)
933 {
934         /* No errors are reported by the hardware, so we don't bother
935          * supporting quick writes to avoid confusing probing */
936         return (I2C_FUNC_SMBUS_EMUL) & ~I2C_FUNC_SMBUS_QUICK;
937 }
938
939 static struct i2c_algorithm go7007_usb_algo = {
940         .master_xfer    = go7007_usb_i2c_master_xfer,
941         .functionality  = go7007_usb_functionality,
942 };
943
944 static struct i2c_adapter go7007_usb_adap_templ = {
945         .owner                  = THIS_MODULE,
946         .class                  = I2C_CLASS_TV_ANALOG,
947         .name                   = "WIS GO7007SB EZ-USB",
948         .id                     = I2C_ALGO_GO7007_USB,
949         .algo                   = &go7007_usb_algo,
950 };
951
952 /********************* USB add/remove functions *********************/
953
954 static int go7007_usb_probe(struct usb_interface *intf,
955                 const struct usb_device_id *id)
956 {
957         struct go7007 *go;
958         struct go7007_usb *usb;
959         struct go7007_usb_board *board;
960         struct usb_device *usbdev = interface_to_usbdev(intf);
961         char *name;
962         int video_pipe, i, v_urb_len;
963
964         printk(KERN_DEBUG "go7007-usb: probing new GO7007 USB board\n");
965
966         switch (id->driver_info) {
967         case GO7007_BOARDID_MATRIX_II:
968                 name = "WIS Matrix II or compatible";
969                 board = &board_matrix_ii;
970                 break;
971         case GO7007_BOARDID_MATRIX_RELOAD:
972                 name = "WIS Matrix Reloaded or compatible";
973                 board = &board_matrix_reload;
974                 break;
975         case GO7007_BOARDID_MATRIX_REV:
976                 name = "WIS Matrix Revolution or compatible";
977                 board = &board_matrix_revolution;
978                 break;
979         case GO7007_BOARDID_STAR_TREK:
980                 name = "WIS Star Trek or compatible";
981                 board = &board_star_trek;
982                 break;
983         case GO7007_BOARDID_XMEN:
984                 name = "WIS XMen or compatible";
985                 board = &board_xmen;
986                 break;
987         case GO7007_BOARDID_XMEN_II:
988                 name = "WIS XMen II or compatible";
989                 board = &board_xmen;
990                 break;
991         case GO7007_BOARDID_XMEN_III:
992                 name = "WIS XMen III or compatible";
993                 board = &board_xmen;
994                 break;
995         case GO7007_BOARDID_PX_M402U:
996                 name = "Plextor PX-M402U";
997                 board = &board_matrix_ii;
998                 break;
999         case GO7007_BOARDID_PX_TV402U_ANY:
1000                 name = "Plextor PX-TV402U (unknown tuner)";
1001                 board = &board_px_tv402u;
1002                 break;
1003         case GO7007_BOARDID_LIFEVIEW_LR192:
1004                 printk(KERN_ERR "go7007-usb: The Lifeview TV Walker Ultra "
1005                                 "is not supported.  Sorry!\n");
1006                 return 0;
1007                 name = "Lifeview TV Walker Ultra";
1008                 board = &board_lifeview_lr192;
1009                 break;
1010         case GO7007_BOARDID_SENSORAY_2250:
1011                 printk(KERN_INFO "Sensoray 2250 found\n");
1012                 name = "Sensoray 2250/2251\n";
1013                 board = &board_sensoray_2250;
1014                 break;
1015         default:
1016                 printk(KERN_ERR "go7007-usb: unknown board ID %d!\n",
1017                                 (unsigned int)id->driver_info);
1018                 return 0;
1019         }
1020
1021         usb = kzalloc(sizeof(struct go7007_usb), GFP_KERNEL);
1022         if (usb == NULL)
1023                 return -ENOMEM;
1024
1025         /* Allocate the URB and buffer for receiving incoming interrupts */
1026         usb->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1027         if (usb->intr_urb == NULL)
1028                 goto allocfail;
1029         usb->intr_urb->transfer_buffer = kmalloc(2*sizeof(u16), GFP_KERNEL);
1030         if (usb->intr_urb->transfer_buffer == NULL)
1031                 goto allocfail;
1032
1033         go = go7007_alloc(&board->main_info, &intf->dev);
1034         if (go == NULL)
1035                 goto allocfail;
1036         usb->board = board;
1037         usb->usbdev = usbdev;
1038         go->board_id = id->driver_info;
1039         strncpy(go->name, name, sizeof(go->name));
1040         if (board->flags & GO7007_USB_EZUSB)
1041                 go->hpi_ops = &go7007_usb_ezusb_hpi_ops;
1042         else
1043                 go->hpi_ops = &go7007_usb_onboard_hpi_ops;
1044         go->hpi_context = usb;
1045         usb_fill_int_urb(usb->intr_urb, usb->usbdev,
1046                         usb_rcvintpipe(usb->usbdev, 4),
1047                         usb->intr_urb->transfer_buffer, 2*sizeof(u16),
1048                         go7007_usb_readinterrupt_complete, go, 8);
1049         usb_set_intfdata(intf, go);
1050
1051         /* Boot the GO7007 */
1052         if (go7007_boot_encoder(go, go->board_info->flags &
1053                                         GO7007_BOARD_USE_ONBOARD_I2C) < 0)
1054                 goto initfail;
1055
1056         /* Register the EZ-USB I2C adapter, if we're using it */
1057         if (board->flags & GO7007_USB_EZUSB_I2C) {
1058                 memcpy(&go->i2c_adapter, &go7007_usb_adap_templ,
1059                                 sizeof(go7007_usb_adap_templ));
1060                 init_MUTEX(&usb->i2c_lock);
1061                 go->i2c_adapter.dev.parent = go->dev;
1062                 i2c_set_adapdata(&go->i2c_adapter, go);
1063                 if (i2c_add_adapter(&go->i2c_adapter) < 0) {
1064                         printk(KERN_ERR
1065                                 "go7007-usb: error: i2c_add_adapter failed\n");
1066                         goto initfail;
1067                 }
1068                 go->i2c_adapter_online = 1;
1069         }
1070
1071         /* Pelco and Adlink reused the XMen and XMen-III vendor and product
1072          * IDs for their own incompatible designs.  We can detect XMen boards
1073          * by probing the sensor, but there is no way to probe the sensors on
1074          * the Pelco and Adlink designs so we default to the Adlink.  If it
1075          * is actually a Pelco, the user must set the assume_endura module
1076          * parameter. */
1077         if ((go->board_id == GO7007_BOARDID_XMEN ||
1078                                 go->board_id == GO7007_BOARDID_XMEN_III) &&
1079                         go->i2c_adapter_online) {
1080                 union i2c_smbus_data data;
1081
1082                 /* Check to see if register 0x0A is 0x76 */
1083                 i2c_smbus_xfer(&go->i2c_adapter, 0x21, I2C_CLIENT_SCCB,
1084                         I2C_SMBUS_READ, 0x0A, I2C_SMBUS_BYTE_DATA, &data);
1085                 if (data.byte != 0x76) {
1086                         if (assume_endura) {
1087                                 go->board_id = GO7007_BOARDID_ENDURA;
1088                                 usb->board = board = &board_endura;
1089                                 go->board_info = &board->main_info;
1090                                 strncpy(go->name, "Pelco Endura",
1091                                                 sizeof(go->name));
1092                         } else {
1093                                 u16 channel;
1094
1095                                 /* set GPIO5 to be an output, currently low */
1096                                 go7007_write_addr(go, 0x3c82, 0x0000);
1097                                 go7007_write_addr(go, 0x3c80, 0x00df);
1098                                 /* read channel number from GPIO[1:0] */
1099                                 go7007_read_addr(go, 0x3c81, &channel);
1100                                 channel &= 0x3;
1101                                 go->board_id = GO7007_BOARDID_ADLINK_MPG24;
1102                                 usb->board = board = &board_adlink_mpg24;
1103                                 go->board_info = &board->main_info;
1104                                 go->channel_number = channel;
1105                                 snprintf(go->name, sizeof(go->name),
1106                                         "Adlink PCI-MPG24, channel #%d",
1107                                         channel);
1108                         }
1109                 }
1110         }
1111
1112         /* Probe the tuner model on the TV402U */
1113         if (go->board_id == GO7007_BOARDID_PX_TV402U_ANY) {
1114                 u8 data[3];
1115
1116                 /* Board strapping indicates tuner model */
1117                 if (go7007_usb_vendor_request(go, 0x41, 0, 0, data, 3, 1) < 0) {
1118                         printk(KERN_ERR "go7007-usb: GPIO read failed!\n");
1119                         goto initfail;
1120                 }
1121                 switch (data[0] >> 6) {
1122                 case 1:
1123                         go->board_id = GO7007_BOARDID_PX_TV402U_EU;
1124                         go->tuner_type = TUNER_SONY_BTF_PG472Z;
1125                         strncpy(go->name, "Plextor PX-TV402U-EU",
1126                                         sizeof(go->name));
1127                         break;
1128                 case 2:
1129                         go->board_id = GO7007_BOARDID_PX_TV402U_JP;
1130                         go->tuner_type = TUNER_SONY_BTF_PK467Z;
1131                         strncpy(go->name, "Plextor PX-TV402U-JP",
1132                                         sizeof(go->name));
1133                         break;
1134                 case 3:
1135                         go->board_id = GO7007_BOARDID_PX_TV402U_NA;
1136                         go->tuner_type = TUNER_SONY_BTF_PB463Z;
1137                         strncpy(go->name, "Plextor PX-TV402U-NA",
1138                                         sizeof(go->name));
1139                         break;
1140                 default:
1141                         printk(KERN_DEBUG "go7007-usb: unable to detect "
1142                                                 "tuner type!\n");
1143                         break;
1144                 }
1145                 /* Configure tuner mode selection inputs connected
1146                  * to the EZ-USB GPIO output pins */
1147                 if (go7007_usb_vendor_request(go, 0x40, 0x7f02, 0,
1148                                         NULL, 0, 0) < 0) {
1149                         printk(KERN_ERR
1150                                 "go7007-usb: GPIO write failed!\n");
1151                         goto initfail;
1152                 }
1153         }
1154
1155         /* Print a nasty message if the user attempts to use a USB2.0 device in
1156          * a USB1.1 port.  There will be silent corruption of the stream. */
1157         if ((board->flags & GO7007_USB_EZUSB) &&
1158                         usbdev->speed != USB_SPEED_HIGH)
1159                 printk(KERN_ERR "go7007-usb: *** WARNING ***  This device "
1160                                 "must be connected to a USB 2.0 port!  "
1161                                 "Attempting to capture video through a USB 1.1 "
1162                                 "port will result in stream corruption, even "
1163                                 "at low bitrates!\n");
1164
1165         /* Do any final GO7007 initialization, then register the
1166          * V4L2 and ALSA interfaces */
1167         if (go7007_register_encoder(go) < 0)
1168                 goto initfail;
1169
1170         /* Allocate the URBs and buffers for receiving the video stream */
1171         if (board->flags & GO7007_USB_EZUSB) {
1172                 v_urb_len = 1024;
1173                 video_pipe = usb_rcvbulkpipe(usb->usbdev, 6);
1174         } else {
1175                 v_urb_len = 512;
1176                 video_pipe = usb_rcvbulkpipe(usb->usbdev, 1);
1177         }
1178         for (i = 0; i < 8; ++i) {
1179                 usb->video_urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1180                 if (usb->video_urbs[i] == NULL)
1181                         goto initfail;
1182                 usb->video_urbs[i]->transfer_buffer =
1183                                                 kmalloc(v_urb_len, GFP_KERNEL);
1184                 if (usb->video_urbs[i]->transfer_buffer == NULL)
1185                         goto initfail;
1186                 usb_fill_bulk_urb(usb->video_urbs[i], usb->usbdev, video_pipe,
1187                                 usb->video_urbs[i]->transfer_buffer, v_urb_len,
1188                                 go7007_usb_read_video_pipe_complete, go);
1189         }
1190
1191         /* Allocate the URBs and buffers for receiving the audio stream */
1192         if ((board->flags & GO7007_USB_EZUSB) && go->audio_enabled)
1193                 for (i = 0; i < 8; ++i) {
1194                         usb->audio_urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1195                         if (usb->audio_urbs[i] == NULL)
1196                                 goto initfail;
1197                         usb->audio_urbs[i]->transfer_buffer = kmalloc(4096,
1198                                                                 GFP_KERNEL);
1199                         if (usb->audio_urbs[i]->transfer_buffer == NULL)
1200                                 goto initfail;
1201                         usb_fill_bulk_urb(usb->audio_urbs[i], usb->usbdev,
1202                                 usb_rcvbulkpipe(usb->usbdev, 8),
1203                                 usb->audio_urbs[i]->transfer_buffer, 4096,
1204                                 go7007_usb_read_audio_pipe_complete, go);
1205                 }
1206
1207
1208         go->status = STATUS_ONLINE;
1209         return 0;
1210
1211 initfail:
1212         go->status = STATUS_SHUTDOWN;
1213         return 0;
1214
1215 allocfail:
1216         if (usb->intr_urb) {
1217                 kfree(usb->intr_urb->transfer_buffer);
1218                 usb_free_urb(usb->intr_urb);
1219         }
1220         kfree(usb);
1221         return -ENOMEM;
1222 }
1223
1224 static void go7007_usb_disconnect(struct usb_interface *intf)
1225 {
1226         struct go7007 *go = usb_get_intfdata(intf);
1227         struct go7007_usb *usb = go->hpi_context;
1228         struct urb *vurb, *aurb;
1229         int i;
1230
1231         go->status = STATUS_SHUTDOWN;
1232         usb_kill_urb(usb->intr_urb);
1233
1234         /* Free USB-related structs */
1235         for (i = 0; i < 8; ++i) {
1236                 vurb = usb->video_urbs[i];
1237                 if (vurb) {
1238                         usb_kill_urb(vurb);
1239                         if (vurb->transfer_buffer)
1240                                 kfree(vurb->transfer_buffer);
1241                         usb_free_urb(vurb);
1242                 }
1243                 aurb = usb->audio_urbs[i];
1244                 if (aurb) {
1245                         usb_kill_urb(aurb);
1246                         if (aurb->transfer_buffer)
1247                                 kfree(aurb->transfer_buffer);
1248                         usb_free_urb(aurb);
1249                 }
1250         }
1251         kfree(usb->intr_urb->transfer_buffer);
1252         usb_free_urb(usb->intr_urb);
1253
1254         kfree(go->hpi_context);
1255
1256         go7007_remove(go);
1257 }
1258
1259 static struct usb_driver go7007_usb_driver = {
1260         .name           = "go7007",
1261         .probe          = go7007_usb_probe,
1262         .disconnect     = go7007_usb_disconnect,
1263         .id_table       = go7007_usb_id_table,
1264 };
1265
1266 static int __init go7007_usb_init(void)
1267 {
1268         return usb_register(&go7007_usb_driver);
1269 }
1270
1271 static void __exit go7007_usb_cleanup(void)
1272 {
1273         usb_deregister(&go7007_usb_driver);
1274 }
1275
1276 module_init(go7007_usb_init);
1277 module_exit(go7007_usb_cleanup);
1278
1279 MODULE_LICENSE("GPL v2");