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