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