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