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