]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/staging/go7007/go7007-usb.c
Staging: go7007: add sensoray 2250/2251 support
[linux-2.6-omap-h63xx.git] / 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
684         if (urb->status != 0) {
685                 if (urb->status != -ESHUTDOWN &&
686                                 go->status != STATUS_SHUTDOWN) {
687                         printk(KERN_ERR
688                                 "go7007-usb: error in read interrupt: %d\n",
689                                 urb->status);
690                 } else {
691                         wake_up(&go->interrupt_waitq);
692                         return;
693                 }
694         } else if (urb->actual_length != urb->transfer_buffer_length) {
695                 printk(KERN_ERR "go7007-usb: short read in interrupt pipe!\n");
696         } else {
697                 go->interrupt_available = 1;
698                 go->interrupt_data = __le16_to_cpu(regs[0]);
699                 go->interrupt_value = __le16_to_cpu(regs[1]);
700 #ifdef GO7007_USB_DEBUG
701                 printk(KERN_DEBUG "go7007-usb: ReadInterrupt: %04x %04x\n",
702                                 go->interrupt_value, go->interrupt_data);
703 #endif
704         }
705
706         wake_up(&go->interrupt_waitq);
707 }
708
709 static int go7007_usb_read_interrupt(struct go7007 *go)
710 {
711         struct go7007_usb *usb = go->hpi_context;
712         int r;
713
714         r = usb_submit_urb(usb->intr_urb, GFP_KERNEL);
715         if (r < 0) {
716                 printk(KERN_ERR
717                         "go7007-usb: unable to submit interrupt urb: %d\n", r);
718                 return r;
719         }
720         return 0;
721 }
722
723 static void go7007_usb_read_video_pipe_complete(struct urb *urb)
724 {
725         struct go7007 *go = (struct go7007 *)urb->context;
726         int r;
727
728         if (!go->streaming) {
729                 wake_up_interruptible(&go->frame_waitq);
730                 return;
731         }
732         if (urb->status != 0) {
733                 printk(KERN_ERR "go7007-usb: error in video pipe: %d\n",
734                                 urb->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;
751
752         if (!go->streaming)
753                 return;
754         if (urb->status != 0) {
755                 printk(KERN_ERR "go7007-usb: error in audio pipe: %d\n",
756                                 urb->status);
757                 return;
758         }
759         if (urb->actual_length != urb->transfer_buffer_length) {
760                 printk(KERN_ERR "go7007-usb: short read in audio pipe!\n");
761                 return;
762         }
763         if (go->audio_deliver != NULL)
764                 go->audio_deliver(go, urb->transfer_buffer, urb->actual_length);
765         r = usb_submit_urb(urb, GFP_ATOMIC);
766         if (r < 0)
767                 printk(KERN_ERR "go7007-usb: error in audio pipe: %d\n", r);
768 }
769
770 static int go7007_usb_stream_start(struct go7007 *go)
771 {
772         struct go7007_usb *usb = go->hpi_context;
773         int i, r;
774
775         for (i = 0; i < 8; ++i) {
776                 r = usb_submit_urb(usb->video_urbs[i], GFP_KERNEL);
777                 if (r < 0) {
778                         printk(KERN_ERR "go7007-usb: error submitting video "
779                                         "urb %d: %d\n", i, r);
780                         goto video_submit_failed;
781                 }
782         }
783         if (!go->audio_enabled)
784                 return 0;
785
786         for (i = 0; i < 8; ++i) {
787                 r = usb_submit_urb(usb->audio_urbs[i], GFP_KERNEL);
788                 if (r < 0) {
789                         printk(KERN_ERR "go7007-usb: error submitting audio "
790                                         "urb %d: %d\n", i, r);
791                         goto audio_submit_failed;
792                 }
793         }
794         return 0;
795
796 audio_submit_failed:
797         for (i = 0; i < 8; ++i)
798                 usb_kill_urb(usb->audio_urbs[i]);
799 video_submit_failed:
800         for (i = 0; i < 8; ++i)
801                 usb_kill_urb(usb->video_urbs[i]);
802         return -1;
803 }
804
805 static int go7007_usb_stream_stop(struct go7007 *go)
806 {
807         struct go7007_usb *usb = go->hpi_context;
808         int i;
809
810         if (go->status == STATUS_SHUTDOWN)
811                 return 0;
812         for (i = 0; i < 8; ++i)
813                 usb_kill_urb(usb->video_urbs[i]);
814         if (go->audio_enabled)
815                 for (i = 0; i < 8; ++i)
816                         usb_kill_urb(usb->audio_urbs[i]);
817         return 0;
818 }
819
820 static int go7007_usb_send_firmware(struct go7007 *go, u8 *data, int len)
821 {
822         struct go7007_usb *usb = go->hpi_context;
823         int transferred, pipe;
824         int timeout = 500;
825
826 #ifdef GO7007_USB_DEBUG
827         printk(KERN_DEBUG "go7007-usb: DownloadBuffer sending %d bytes\n", len);
828 #endif
829
830         if (usb->board->flags & GO7007_USB_EZUSB)
831                 pipe = usb_sndbulkpipe(usb->usbdev, 2);
832         else
833                 pipe = usb_sndbulkpipe(usb->usbdev, 3);
834
835         return usb_bulk_msg(usb->usbdev, pipe, data, len,
836                                         &transferred, timeout);
837 }
838
839 static struct go7007_hpi_ops go7007_usb_ezusb_hpi_ops = {
840         .interface_reset        = go7007_usb_interface_reset,
841         .write_interrupt        = go7007_usb_ezusb_write_interrupt,
842         .read_interrupt         = go7007_usb_read_interrupt,
843         .stream_start           = go7007_usb_stream_start,
844         .stream_stop            = go7007_usb_stream_stop,
845         .send_firmware          = go7007_usb_send_firmware,
846 };
847
848 static struct go7007_hpi_ops go7007_usb_onboard_hpi_ops = {
849         .interface_reset        = go7007_usb_interface_reset,
850         .write_interrupt        = go7007_usb_onboard_write_interrupt,
851         .read_interrupt         = go7007_usb_read_interrupt,
852         .stream_start           = go7007_usb_stream_start,
853         .stream_stop            = go7007_usb_stream_stop,
854         .send_firmware          = go7007_usb_send_firmware,
855 };
856
857 /********************* Driver for EZ-USB I2C adapter *********************/
858
859 static int go7007_usb_i2c_master_xfer(struct i2c_adapter *adapter,
860                                         struct i2c_msg msgs[], int num)
861 {
862         struct go7007 *go = i2c_get_adapdata(adapter);
863         struct go7007_usb *usb = go->hpi_context;
864         u8 buf[16];
865         int buf_len, i;
866         int ret = -1;
867
868         if (go->status == STATUS_SHUTDOWN)
869                 return -1;
870
871         down(&usb->i2c_lock);
872
873         for (i = 0; i < num; ++i) {
874                 /* The hardware command is "write some bytes then read some
875                  * bytes", so we try to coalesce a write followed by a read
876                  * into a single USB transaction */
877                 if (i + 1 < num && msgs[i].addr == msgs[i + 1].addr &&
878                                 !(msgs[i].flags & I2C_M_RD) &&
879                                 (msgs[i + 1].flags & I2C_M_RD)) {
880 #ifdef GO7007_I2C_DEBUG
881                         printk(KERN_DEBUG "go7007-usb: i2c write/read %d/%d "
882                                         "bytes on %02x\n", msgs[i].len,
883                                         msgs[i + 1].len, msgs[i].addr);
884 #endif
885                         buf[0] = 0x01;
886                         buf[1] = msgs[i].len + 1;
887                         buf[2] = msgs[i].addr << 1;
888                         memcpy(&buf[3], msgs[i].buf, msgs[i].len);
889                         buf_len = msgs[i].len + 3;
890                         buf[buf_len++] = msgs[++i].len;
891                 } else if (msgs[i].flags & I2C_M_RD) {
892 #ifdef GO7007_I2C_DEBUG
893                         printk(KERN_DEBUG "go7007-usb: i2c read %d "
894                                         "bytes on %02x\n", msgs[i].len,
895                                         msgs[i].addr);
896 #endif
897                         buf[0] = 0x01;
898                         buf[1] = 1;
899                         buf[2] = msgs[i].addr << 1;
900                         buf[3] = msgs[i].len;
901                         buf_len = 4;
902                 } else {
903 #ifdef GO7007_I2C_DEBUG
904                         printk(KERN_DEBUG "go7007-usb: i2c write %d "
905                                         "bytes on %02x\n", msgs[i].len,
906                                         msgs[i].addr);
907 #endif
908                         buf[0] = 0x00;
909                         buf[1] = msgs[i].len + 1;
910                         buf[2] = msgs[i].addr << 1;
911                         memcpy(&buf[3], msgs[i].buf, msgs[i].len);
912                         buf_len = msgs[i].len + 3;
913                         buf[buf_len++] = 0;
914                 }
915                 if (go7007_usb_vendor_request(go, 0x24, 0, 0,
916                                                 buf, buf_len, 0) < 0)
917                         goto i2c_done;
918                 if (msgs[i].flags & I2C_M_RD) {
919                         memset(buf, 0, sizeof(buf));
920                         if (go7007_usb_vendor_request(go, 0x25, 0, 0, buf,
921                                                 msgs[i].len + 1, 1) < 0)
922                                 goto i2c_done;
923                         memcpy(msgs[i].buf, buf + 1, msgs[i].len);
924                 }
925         }
926         ret = 0;
927
928 i2c_done:
929         up(&usb->i2c_lock);
930         return ret;
931 }
932
933 static u32 go7007_usb_functionality(struct i2c_adapter *adapter)
934 {
935         /* No errors are reported by the hardware, so we don't bother
936          * supporting quick writes to avoid confusing probing */
937         return (I2C_FUNC_SMBUS_EMUL) & ~I2C_FUNC_SMBUS_QUICK;
938 }
939
940 static struct i2c_algorithm go7007_usb_algo = {
941         .master_xfer    = go7007_usb_i2c_master_xfer,
942         .functionality  = go7007_usb_functionality,
943 };
944
945 static struct i2c_adapter go7007_usb_adap_templ = {
946         .owner                  = THIS_MODULE,
947         .class                  = I2C_CLASS_TV_ANALOG,
948         .name                   = "WIS GO7007SB EZ-USB",
949         .id                     = I2C_ALGO_GO7007_USB,
950         .algo                   = &go7007_usb_algo,
951 };
952
953 /********************* USB add/remove functions *********************/
954
955 static int go7007_usb_probe(struct usb_interface *intf,
956                 const struct usb_device_id *id)
957 {
958         struct go7007 *go;
959         struct go7007_usb *usb;
960         struct go7007_usb_board *board;
961         struct usb_device *usbdev = interface_to_usbdev(intf);
962         char *name;
963         int video_pipe, i, v_urb_len;
964
965         printk(KERN_DEBUG "go7007-usb: probing new GO7007 USB board\n");
966
967         switch (id->driver_info) {
968         case GO7007_BOARDID_MATRIX_II:
969                 name = "WIS Matrix II or compatible";
970                 board = &board_matrix_ii;
971                 break;
972         case GO7007_BOARDID_MATRIX_RELOAD:
973                 name = "WIS Matrix Reloaded or compatible";
974                 board = &board_matrix_reload;
975                 break;
976         case GO7007_BOARDID_MATRIX_REV:
977                 name = "WIS Matrix Revolution or compatible";
978                 board = &board_matrix_revolution;
979                 break;
980         case GO7007_BOARDID_STAR_TREK:
981                 name = "WIS Star Trek or compatible";
982                 board = &board_star_trek;
983                 break;
984         case GO7007_BOARDID_XMEN:
985                 name = "WIS XMen or compatible";
986                 board = &board_xmen;
987                 break;
988         case GO7007_BOARDID_XMEN_II:
989                 name = "WIS XMen II or compatible";
990                 board = &board_xmen;
991                 break;
992         case GO7007_BOARDID_XMEN_III:
993                 name = "WIS XMen III or compatible";
994                 board = &board_xmen;
995                 break;
996         case GO7007_BOARDID_PX_M402U:
997                 name = "Plextor PX-M402U";
998                 board = &board_matrix_ii;
999                 break;
1000         case GO7007_BOARDID_PX_TV402U_ANY:
1001                 name = "Plextor PX-TV402U (unknown tuner)";
1002                 board = &board_px_tv402u;
1003                 break;
1004         case GO7007_BOARDID_LIFEVIEW_LR192:
1005                 printk(KERN_ERR "go7007-usb: The Lifeview TV Walker Ultra "
1006                                 "is not supported.  Sorry!\n");
1007                 return 0;
1008                 name = "Lifeview TV Walker Ultra";
1009                 board = &board_lifeview_lr192;
1010                 break;
1011         case GO7007_BOARDID_SENSORAY_2250:
1012                 printk(KERN_INFO "Sensoray 2250 found\n");
1013                 name = "Sensoray 2250/2251\n";
1014                 board = &board_sensoray_2250;
1015                 break;
1016         default:
1017                 printk(KERN_ERR "go7007-usb: unknown board ID %d!\n",
1018                                 (unsigned int)id->driver_info);
1019                 return 0;
1020         }
1021
1022         usb = kmalloc(sizeof(struct go7007_usb), GFP_KERNEL);
1023         if (usb == NULL)
1024                 return -ENOMEM;
1025         memset(usb, 0, sizeof(struct go7007_usb));
1026
1027         /* Allocate the URB and buffer for receiving incoming interrupts */
1028         usb->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1029         if (usb->intr_urb == NULL)
1030                 goto allocfail;
1031         usb->intr_urb->transfer_buffer = kmalloc(2*sizeof(u16), GFP_KERNEL);
1032         if (usb->intr_urb->transfer_buffer == NULL)
1033                 goto allocfail;
1034
1035         go = go7007_alloc(&board->main_info, &intf->dev);
1036         if (go == NULL)
1037                 goto allocfail;
1038         usb->board = board;
1039         usb->usbdev = usbdev;
1040         go->board_id = id->driver_info;
1041         strncpy(go->name, name, sizeof(go->name));
1042         if (board->flags & GO7007_USB_EZUSB)
1043                 go->hpi_ops = &go7007_usb_ezusb_hpi_ops;
1044         else
1045                 go->hpi_ops = &go7007_usb_onboard_hpi_ops;
1046         go->hpi_context = usb;
1047         usb_fill_int_urb(usb->intr_urb, usb->usbdev,
1048                         usb_rcvintpipe(usb->usbdev, 4),
1049                         usb->intr_urb->transfer_buffer, 2*sizeof(u16),
1050                         go7007_usb_readinterrupt_complete, go, 8);
1051         usb_set_intfdata(intf, go);
1052
1053         /* Boot the GO7007 */
1054         if (go7007_boot_encoder(go, go->board_info->flags &
1055                                         GO7007_BOARD_USE_ONBOARD_I2C) < 0)
1056                 goto initfail;
1057
1058         /* Register the EZ-USB I2C adapter, if we're using it */
1059         if (board->flags & GO7007_USB_EZUSB_I2C) {
1060                 memcpy(&go->i2c_adapter, &go7007_usb_adap_templ,
1061                                 sizeof(go7007_usb_adap_templ));
1062                 init_MUTEX(&usb->i2c_lock);
1063                 go->i2c_adapter.dev.parent = go->dev;
1064                 i2c_set_adapdata(&go->i2c_adapter, go);
1065                 if (i2c_add_adapter(&go->i2c_adapter) < 0) {
1066                         printk(KERN_ERR
1067                                 "go7007-usb: error: i2c_add_adapter failed\n");
1068                         goto initfail;
1069                 }
1070                 go->i2c_adapter_online = 1;
1071         }
1072
1073         /* Pelco and Adlink reused the XMen and XMen-III vendor and product
1074          * IDs for their own incompatible designs.  We can detect XMen boards
1075          * by probing the sensor, but there is no way to probe the sensors on
1076          * the Pelco and Adlink designs so we default to the Adlink.  If it
1077          * is actually a Pelco, the user must set the assume_endura module
1078          * parameter. */
1079         if ((go->board_id == GO7007_BOARDID_XMEN ||
1080                                 go->board_id == GO7007_BOARDID_XMEN_III) &&
1081                         go->i2c_adapter_online) {
1082                 union i2c_smbus_data data;
1083
1084                 /* Check to see if register 0x0A is 0x76 */
1085                 i2c_smbus_xfer(&go->i2c_adapter, 0x21, I2C_CLIENT_SCCB,
1086                         I2C_SMBUS_READ, 0x0A, I2C_SMBUS_BYTE_DATA, &data);
1087                 if (data.byte != 0x76) {
1088                         if (assume_endura) {
1089                                 go->board_id = GO7007_BOARDID_ENDURA;
1090                                 usb->board = board = &board_endura;
1091                                 go->board_info = &board->main_info;
1092                                 strncpy(go->name, "Pelco Endura",
1093                                                 sizeof(go->name));
1094                         } else {
1095                                 u16 channel;
1096
1097                                 /* set GPIO5 to be an output, currently low */
1098                                 go7007_write_addr(go, 0x3c82, 0x0000);
1099                                 go7007_write_addr(go, 0x3c80, 0x00df);
1100                                 /* read channel number from GPIO[1:0] */
1101                                 go7007_read_addr(go, 0x3c81, &channel);
1102                                 channel &= 0x3;
1103                                 go->board_id = GO7007_BOARDID_ADLINK_MPG24;
1104                                 usb->board = board = &board_adlink_mpg24;
1105                                 go->board_info = &board->main_info;
1106                                 go->channel_number = channel;
1107                                 snprintf(go->name, sizeof(go->name),
1108                                         "Adlink PCI-MPG24, channel #%d",
1109                                         channel);
1110                         }
1111                 }
1112         }
1113
1114         /* Probe the tuner model on the TV402U */
1115         if (go->board_id == GO7007_BOARDID_PX_TV402U_ANY) {
1116                 u8 data[3];
1117
1118                 /* Board strapping indicates tuner model */
1119                 if (go7007_usb_vendor_request(go, 0x41, 0, 0, data, 3, 1) < 0) {
1120                         printk(KERN_ERR "go7007-usb: GPIO read failed!\n");
1121                         goto initfail;
1122                 }
1123                 switch (data[0] >> 6) {
1124                 case 1:
1125                         go->board_id = GO7007_BOARDID_PX_TV402U_EU;
1126                         go->tuner_type = TUNER_SONY_BTF_PG472Z;
1127                         strncpy(go->name, "Plextor PX-TV402U-EU",
1128                                         sizeof(go->name));
1129                         break;
1130                 case 2:
1131                         go->board_id = GO7007_BOARDID_PX_TV402U_JP;
1132                         go->tuner_type = TUNER_SONY_BTF_PK467Z;
1133                         strncpy(go->name, "Plextor PX-TV402U-JP",
1134                                         sizeof(go->name));
1135                         break;
1136                 case 3:
1137                         go->board_id = GO7007_BOARDID_PX_TV402U_NA;
1138                         go->tuner_type = TUNER_SONY_BTF_PB463Z;
1139                         strncpy(go->name, "Plextor PX-TV402U-NA",
1140                                         sizeof(go->name));
1141                         break;
1142                 default:
1143                         printk(KERN_DEBUG "go7007-usb: unable to detect "
1144                                                 "tuner type!\n");
1145                         break;
1146                 }
1147                 /* Configure tuner mode selection inputs connected
1148                  * to the EZ-USB GPIO output pins */
1149                 if (go7007_usb_vendor_request(go, 0x40, 0x7f02, 0,
1150                                         NULL, 0, 0) < 0) {
1151                         printk(KERN_ERR
1152                                 "go7007-usb: GPIO write failed!\n");
1153                         goto initfail;
1154                 }
1155         }
1156
1157         /* Print a nasty message if the user attempts to use a USB2.0 device in
1158          * a USB1.1 port.  There will be silent corruption of the stream. */
1159         if ((board->flags & GO7007_USB_EZUSB) &&
1160                         usbdev->speed != USB_SPEED_HIGH)
1161                 printk(KERN_ERR "go7007-usb: *** WARNING ***  This device "
1162                                 "must be connected to a USB 2.0 port!  "
1163                                 "Attempting to capture video through a USB 1.1 "
1164                                 "port will result in stream corruption, even "
1165                                 "at low bitrates!\n");
1166
1167         /* Do any final GO7007 initialization, then register the
1168          * V4L2 and ALSA interfaces */
1169         if (go7007_register_encoder(go) < 0)
1170                 goto initfail;
1171
1172         /* Allocate the URBs and buffers for receiving the video stream */
1173         if (board->flags & GO7007_USB_EZUSB) {
1174                 v_urb_len = 1024;
1175                 video_pipe = usb_rcvbulkpipe(usb->usbdev, 6);
1176         } else {
1177                 v_urb_len = 512;
1178                 video_pipe = usb_rcvbulkpipe(usb->usbdev, 1);
1179         }
1180         for (i = 0; i < 8; ++i) {
1181                 usb->video_urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1182                 if (usb->video_urbs[i] == NULL)
1183                         goto initfail;
1184                 usb->video_urbs[i]->transfer_buffer =
1185                                                 kmalloc(v_urb_len, GFP_KERNEL);
1186                 if (usb->video_urbs[i]->transfer_buffer == NULL)
1187                         goto initfail;
1188                 usb_fill_bulk_urb(usb->video_urbs[i], usb->usbdev, video_pipe,
1189                                 usb->video_urbs[i]->transfer_buffer, v_urb_len,
1190                                 go7007_usb_read_video_pipe_complete, go);
1191         }
1192
1193         /* Allocate the URBs and buffers for receiving the audio stream */
1194         if ((board->flags & GO7007_USB_EZUSB) && go->audio_enabled)
1195                 for (i = 0; i < 8; ++i) {
1196                         usb->audio_urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1197                         if (usb->audio_urbs[i] == NULL)
1198                                 goto initfail;
1199                         usb->audio_urbs[i]->transfer_buffer = kmalloc(4096,
1200                                                                 GFP_KERNEL);
1201                         if (usb->audio_urbs[i]->transfer_buffer == NULL)
1202                                 goto initfail;
1203                         usb_fill_bulk_urb(usb->audio_urbs[i], usb->usbdev,
1204                                 usb_rcvbulkpipe(usb->usbdev, 8),
1205                                 usb->audio_urbs[i]->transfer_buffer, 4096,
1206                                 go7007_usb_read_audio_pipe_complete, go);
1207                 }
1208
1209
1210         go->status = STATUS_ONLINE;
1211         return 0;
1212
1213 initfail:
1214         go->status = STATUS_SHUTDOWN;
1215         return 0;
1216
1217 allocfail:
1218         if (usb->intr_urb) {
1219                 kfree(usb->intr_urb->transfer_buffer);
1220                 usb_free_urb(usb->intr_urb);
1221         }
1222         kfree(usb);
1223         return -ENOMEM;
1224 }
1225
1226 static void go7007_usb_disconnect(struct usb_interface *intf)
1227 {
1228         struct go7007 *go = usb_get_intfdata(intf);
1229         struct go7007_usb *usb = go->hpi_context;
1230         int i;
1231
1232         go->status = STATUS_SHUTDOWN;
1233         usb_kill_urb(usb->intr_urb);
1234
1235         /* Free USB-related structs */
1236         for (i = 0; i < 8; ++i) {
1237                 if (usb->video_urbs[i] != NULL) {
1238                         if (usb->video_urbs[i]->transfer_buffer != NULL)
1239                                 kfree(usb->video_urbs[i]->transfer_buffer);
1240                         usb_free_urb(usb->video_urbs[i]);
1241                 }
1242                 if (usb->audio_urbs[i] != NULL) {
1243                         if (usb->audio_urbs[i]->transfer_buffer != NULL)
1244                                 kfree(usb->audio_urbs[i]->transfer_buffer);
1245                         usb_free_urb(usb->audio_urbs[i]);
1246                 }
1247         }
1248         kfree(usb->intr_urb->transfer_buffer);
1249         usb_free_urb(usb->intr_urb);
1250
1251         kfree(go->hpi_context);
1252
1253         go7007_remove(go);
1254 }
1255
1256 static struct usb_driver go7007_usb_driver = {
1257         .name           = "go7007",
1258         .probe          = go7007_usb_probe,
1259         .disconnect     = go7007_usb_disconnect,
1260         .id_table       = go7007_usb_id_table,
1261 };
1262
1263 static int __init go7007_usb_init(void)
1264 {
1265         return usb_register(&go7007_usb_driver);
1266 }
1267
1268 static void __exit go7007_usb_cleanup(void)
1269 {
1270         usb_deregister(&go7007_usb_driver);
1271 }
1272
1273 module_init(go7007_usb_init);
1274 module_exit(go7007_usb_cleanup);
1275
1276 MODULE_LICENSE("GPL v2");