2 * Copyright (C) 2005-2006 Micronas USA Inc.
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.
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.
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.
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/wait.h>
22 #include <linux/list.h>
23 #include <linux/slab.h>
24 #include <linux/time.h>
26 #include <linux/usb.h>
27 #include <linux/i2c.h>
28 #include <asm/byteorder.h>
29 #include <media/tvaudio.h>
31 #include "go7007-priv.h"
34 static unsigned int assume_endura;
35 module_param(assume_endura, int, 0644);
36 MODULE_PARM_DESC(assume_endura, "when probing fails, hardware is a Pelco Endura");
38 /* #define GO7007_USB_DEBUG */
39 /* #define GO7007_I2C_DEBUG */ /* for debugging the EZ-USB I2C adapter */
41 #define HPI_STATUS_ADDR 0xFFF4
42 #define INT_PARAM_ADDR 0xFFF6
43 #define INT_INDEX_ADDR 0xFFF8
46 * Pipes on EZ-USB interface:
49 * 2 snd - Download firmware (control)
50 * 4 rcv - Read Interrupt (interrupt)
51 * 6 rcv - Read Video (bulk)
52 * 8 rcv - Read Audio (bulk)
55 #define GO7007_USB_EZUSB (1<<0)
56 #define GO7007_USB_EZUSB_I2C (1<<1)
58 struct go7007_usb_board {
60 struct go7007_board_info main_info;
64 struct go7007_usb_board *board;
65 struct semaphore i2c_lock;
66 struct usb_device *usbdev;
67 struct urb *video_urbs[8];
68 struct urb *audio_urbs[8];
72 /*********************** Product specification data ***********************/
74 static struct go7007_usb_board board_matrix_ii = {
75 .flags = GO7007_USB_EZUSB,
77 .firmware = "go7007tv.bin",
78 .flags = GO7007_BOARD_HAS_AUDIO |
79 GO7007_BOARD_USE_ONBOARD_I2C,
80 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
86 .sensor_flags = GO7007_SENSOR_656 |
87 GO7007_SENSOR_VALID_ENABLE |
90 GO7007_SENSOR_SCALING,
94 .type = "wis_saa7115",
95 .id = I2C_DRIVERID_WIS_SAA7115,
113 static struct go7007_usb_board board_matrix_reload = {
114 .flags = GO7007_USB_EZUSB,
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,
126 .sensor_flags = GO7007_SENSOR_656 |
131 .type = "wis_saa7113",
132 .id = I2C_DRIVERID_WIS_SAA7113,
150 static struct go7007_usb_board board_star_trek = {
151 .flags = GO7007_USB_EZUSB | GO7007_USB_EZUSB_I2C,
153 .firmware = "go7007tv.bin",
154 .flags = GO7007_BOARD_HAS_AUDIO, /* |
155 GO7007_BOARD_HAS_TUNER, */
156 .sensor_flags = GO7007_SENSOR_656 |
157 GO7007_SENSOR_VALID_ENABLE |
160 GO7007_SENSOR_SCALING,
161 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
162 GO7007_AUDIO_WORD_16,
169 .type = "wis_saa7115",
170 .id = I2C_DRIVERID_WIS_SAA7115,
178 /* .audio_input = AUDIO_EXTERN, */
183 /* .audio_input = AUDIO_EXTERN, */
188 * .audio_input = AUDIO_TUNER,
196 static struct go7007_usb_board board_px_tv402u = {
197 .flags = GO7007_USB_EZUSB | GO7007_USB_EZUSB_I2C,
199 .firmware = "go7007tv.bin",
200 .flags = GO7007_BOARD_HAS_AUDIO |
201 GO7007_BOARD_HAS_TUNER,
202 .sensor_flags = GO7007_SENSOR_656 |
203 GO7007_SENSOR_VALID_ENABLE |
206 GO7007_SENSOR_SCALING,
207 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
208 GO7007_AUDIO_WORD_16,
215 .type = "wis_saa7115",
216 .id = I2C_DRIVERID_WIS_SAA7115,
220 .type = "wis_uda1342",
221 .id = I2C_DRIVERID_WIS_UDA1342,
225 .type = "wis_sony_tuner",
226 .id = I2C_DRIVERID_WIS_SONY_TUNER,
234 .audio_input = TVAUDIO_INPUT_EXTERN,
239 .audio_input = TVAUDIO_INPUT_EXTERN,
244 .audio_input = TVAUDIO_INPUT_TUNER,
251 static struct go7007_usb_board board_xmen = {
254 .firmware = "go7007tv.bin",
255 .flags = GO7007_BOARD_USE_ONBOARD_I2C,
257 .sensor_flags = GO7007_SENSOR_VREF_POLAR,
259 .sensor_height = 240,
260 .sensor_framerate = 30030,
261 .audio_flags = GO7007_AUDIO_ONE_CHANNEL |
262 GO7007_AUDIO_I2S_MODE_3 |
263 GO7007_AUDIO_WORD_14 |
264 GO7007_AUDIO_I2S_MASTER |
265 GO7007_AUDIO_BCLK_POLAR |
266 GO7007_AUDIO_OKI_MODE,
268 .audio_bclk_div = 48,
273 .type = "wis_ov7640",
274 .id = I2C_DRIVERID_WIS_OV7640,
287 static struct go7007_usb_board board_matrix_revolution = {
288 .flags = GO7007_USB_EZUSB,
290 .firmware = "go7007tv.bin",
291 .flags = GO7007_BOARD_HAS_AUDIO |
292 GO7007_BOARD_USE_ONBOARD_I2C,
293 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
294 GO7007_AUDIO_I2S_MASTER |
295 GO7007_AUDIO_WORD_16,
300 .sensor_flags = GO7007_SENSOR_656 |
306 .type = "wis_tw9903",
307 .id = I2C_DRIVERID_WIS_TW9903,
325 static struct go7007_usb_board board_lifeview_lr192 = {
326 .flags = GO7007_USB_EZUSB,
328 .firmware = "go7007tv.bin",
329 .flags = GO7007_BOARD_HAS_AUDIO |
330 GO7007_BOARD_USE_ONBOARD_I2C,
331 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
332 GO7007_AUDIO_WORD_16,
337 .sensor_flags = GO7007_SENSOR_656 |
338 GO7007_SENSOR_VALID_ENABLE |
341 GO7007_SENSOR_SCALING,
353 static struct go7007_usb_board board_endura = {
356 .firmware = "go7007tv.bin",
358 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
359 GO7007_AUDIO_I2S_MASTER |
360 GO7007_AUDIO_WORD_16,
362 .audio_bclk_div = 48,
365 .sensor_flags = GO7007_SENSOR_656 |
367 .sensor_h_offset = 8,
378 static struct go7007_usb_board board_adlink_mpg24 = {
381 .firmware = "go7007tv.bin",
382 .flags = GO7007_BOARD_USE_ONBOARD_I2C,
383 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
384 GO7007_AUDIO_I2S_MASTER |
385 GO7007_AUDIO_WORD_16,
390 .sensor_flags = GO7007_SENSOR_656 |
396 .type = "wis_twTW2804",
397 .id = I2C_DRIVERID_WIS_TW2804,
398 .addr = 0x00, /* yes, really */
410 static struct go7007_usb_board board_sensoray_2250 = {
411 .flags = GO7007_USB_EZUSB | GO7007_USB_EZUSB_I2C,
413 .firmware = "go7007tv.bin",
414 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
415 GO7007_AUDIO_I2S_MASTER |
416 GO7007_AUDIO_WORD_16,
417 .flags = GO7007_BOARD_HAS_AUDIO,
422 .sensor_flags = GO7007_SENSOR_656 |
427 .type = "s2250_board",
428 .id = I2C_DRIVERID_S2250,
446 static struct usb_device_id go7007_usb_id_table[] = {
448 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION |
449 USB_DEVICE_ID_MATCH_INT_INFO,
450 .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
451 .idProduct = 0x7007, /* Product ID of GO7007SB chip */
452 .bcdDevice_lo = 0x200, /* Revision number of XMen */
453 .bcdDevice_hi = 0x200,
454 .bInterfaceClass = 255,
455 .bInterfaceSubClass = 0,
456 .bInterfaceProtocol = 255,
457 .driver_info = (kernel_ulong_t)GO7007_BOARDID_XMEN,
460 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
461 .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
462 .idProduct = 0x7007, /* Product ID of GO7007SB chip */
463 .bcdDevice_lo = 0x202, /* Revision number of Matrix II */
464 .bcdDevice_hi = 0x202,
465 .driver_info = (kernel_ulong_t)GO7007_BOARDID_MATRIX_II,
468 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
469 .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
470 .idProduct = 0x7007, /* Product ID of GO7007SB chip */
471 .bcdDevice_lo = 0x204, /* Revision number of Matrix */
472 .bcdDevice_hi = 0x204, /* Reloaded */
473 .driver_info = (kernel_ulong_t)GO7007_BOARDID_MATRIX_RELOAD,
476 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION |
477 USB_DEVICE_ID_MATCH_INT_INFO,
478 .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
479 .idProduct = 0x7007, /* Product ID of GO7007SB chip */
480 .bcdDevice_lo = 0x205, /* Revision number of XMen-II */
481 .bcdDevice_hi = 0x205,
482 .bInterfaceClass = 255,
483 .bInterfaceSubClass = 0,
484 .bInterfaceProtocol = 255,
485 .driver_info = (kernel_ulong_t)GO7007_BOARDID_XMEN_II,
488 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
489 .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
490 .idProduct = 0x7007, /* Product ID of GO7007SB chip */
491 .bcdDevice_lo = 0x208, /* Revision number of Star Trek */
492 .bcdDevice_hi = 0x208,
493 .driver_info = (kernel_ulong_t)GO7007_BOARDID_STAR_TREK,
496 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION |
497 USB_DEVICE_ID_MATCH_INT_INFO,
498 .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
499 .idProduct = 0x7007, /* Product ID of GO7007SB chip */
500 .bcdDevice_lo = 0x209, /* Revision number of XMen-III */
501 .bcdDevice_hi = 0x209,
502 .bInterfaceClass = 255,
503 .bInterfaceSubClass = 0,
504 .bInterfaceProtocol = 255,
505 .driver_info = (kernel_ulong_t)GO7007_BOARDID_XMEN_III,
508 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
509 .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
510 .idProduct = 0x7007, /* Product ID of GO7007SB chip */
511 .bcdDevice_lo = 0x210, /* Revision number of Matrix */
512 .bcdDevice_hi = 0x210, /* Revolution */
513 .driver_info = (kernel_ulong_t)GO7007_BOARDID_MATRIX_REV,
516 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
517 .idVendor = 0x093b, /* Vendor ID of Plextor */
518 .idProduct = 0xa102, /* Product ID of M402U */
519 .bcdDevice_lo = 0x1, /* revision number of Blueberry */
521 .driver_info = (kernel_ulong_t)GO7007_BOARDID_PX_M402U,
524 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
525 .idVendor = 0x093b, /* Vendor ID of Plextor */
526 .idProduct = 0xa104, /* Product ID of TV402U */
529 .driver_info = (kernel_ulong_t)GO7007_BOARDID_PX_TV402U_ANY,
532 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
533 .idVendor = 0x10fd, /* Vendor ID of Anubis Electronics */
534 .idProduct = 0xde00, /* Product ID of Lifeview LR192 */
537 .driver_info = (kernel_ulong_t)GO7007_BOARDID_LIFEVIEW_LR192,
540 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
541 .idVendor = 0x1943, /* Vendor ID Sensoray */
542 .idProduct = 0x2250, /* Product ID of 2250/2251 */
545 .driver_info = (kernel_ulong_t)GO7007_BOARDID_SENSORAY_2250,
547 { } /* Terminating entry */
550 MODULE_DEVICE_TABLE(usb, go7007_usb_id_table);
552 /********************* Driver for EZ-USB HPI interface *********************/
554 static int go7007_usb_vendor_request(struct go7007 *go, int request,
555 int value, int index, void *transfer_buffer, int length, int in)
557 struct go7007_usb *usb = go->hpi_context;
561 return usb_control_msg(usb->usbdev,
562 usb_rcvctrlpipe(usb->usbdev, 0), request,
563 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
564 value, index, transfer_buffer, length, timeout);
566 return usb_control_msg(usb->usbdev,
567 usb_sndctrlpipe(usb->usbdev, 0), request,
568 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
569 value, index, transfer_buffer, length, timeout);
573 static int go7007_usb_interface_reset(struct go7007 *go)
575 struct go7007_usb *usb = go->hpi_context;
576 u16 intr_val, intr_data;
579 if (go7007_write_interrupt(go, 0x0001, 0x0001) < 0)
583 if (usb->board->flags & GO7007_USB_EZUSB) {
584 /* Reset buffer in EZ-USB */
585 #ifdef GO7007_USB_DEBUG
586 printk(KERN_DEBUG "go7007-usb: resetting EZ-USB buffers\n");
588 if (go7007_usb_vendor_request(go, 0x10, 0, 0, NULL, 0, 0) < 0 ||
589 go7007_usb_vendor_request(go, 0x10, 0, 0, NULL, 0, 0) < 0)
592 /* Reset encoder again */
593 if (go7007_write_interrupt(go, 0x0001, 0x0001) < 0)
598 /* Wait for an interrupt to indicate successful hardware reset */
599 if (go7007_read_interrupt(go, &intr_val, &intr_data) < 0 ||
600 (intr_val & ~0x1) != 0x55aa) {
602 "go7007-usb: unable to reset the USB interface\n");
608 static int go7007_usb_ezusb_write_interrupt(struct go7007 *go,
611 struct go7007_usb *usb = go->hpi_context;
616 #ifdef GO7007_USB_DEBUG
618 "go7007-usb: WriteInterrupt: %04x %04x\n", addr, data);
621 for (i = 0; i < 100; ++i) {
622 r = usb_control_msg(usb->usbdev,
623 usb_rcvctrlpipe(usb->usbdev, 0), 0x14,
624 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
625 0, HPI_STATUS_ADDR, &status_reg,
626 sizeof(status_reg), timeout);
628 goto write_int_error;
629 __le16_to_cpus(&status_reg);
630 if (!(status_reg & 0x0010))
636 "go7007-usb: device is hung, status reg = 0x%04x\n",
640 r = usb_control_msg(usb->usbdev, usb_sndctrlpipe(usb->usbdev, 0), 0x12,
641 USB_TYPE_VENDOR | USB_RECIP_DEVICE, data,
642 INT_PARAM_ADDR, NULL, 0, timeout);
644 goto write_int_error;
645 r = usb_control_msg(usb->usbdev, usb_sndctrlpipe(usb->usbdev, 0),
646 0x12, USB_TYPE_VENDOR | USB_RECIP_DEVICE, addr,
647 INT_INDEX_ADDR, NULL, 0, timeout);
649 goto write_int_error;
653 printk(KERN_ERR "go7007-usb: error in WriteInterrupt: %d\n", r);
657 static int go7007_usb_onboard_write_interrupt(struct go7007 *go,
660 struct go7007_usb *usb = go->hpi_context;
665 #ifdef GO7007_USB_DEBUG
667 "go7007-usb: WriteInterrupt: %04x %04x\n", addr, data);
670 tbuf = kmalloc(8, GFP_KERNEL);
674 tbuf[0] = data & 0xff;
676 tbuf[2] = addr & 0xff;
678 r = usb_control_msg(usb->usbdev, usb_sndctrlpipe(usb->usbdev, 2), 0x00,
679 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT, 0x55aa,
680 0xf0f0, tbuf, 8, timeout);
683 printk(KERN_ERR "go7007-usb: error in WriteInterrupt: %d\n", r);
689 static void go7007_usb_readinterrupt_complete(struct urb *urb)
691 struct go7007 *go = (struct go7007 *)urb->context;
692 u16 *regs = (u16 *)urb->transfer_buffer;
693 int status = urb->status;
696 if (status != -ESHUTDOWN &&
697 go->status != STATUS_SHUTDOWN) {
699 "go7007-usb: error in read interrupt: %d\n",
702 wake_up(&go->interrupt_waitq);
705 } else if (urb->actual_length != urb->transfer_buffer_length) {
706 printk(KERN_ERR "go7007-usb: short read in interrupt pipe!\n");
708 go->interrupt_available = 1;
709 go->interrupt_data = __le16_to_cpu(regs[0]);
710 go->interrupt_value = __le16_to_cpu(regs[1]);
711 #ifdef GO7007_USB_DEBUG
712 printk(KERN_DEBUG "go7007-usb: ReadInterrupt: %04x %04x\n",
713 go->interrupt_value, go->interrupt_data);
717 wake_up(&go->interrupt_waitq);
720 static int go7007_usb_read_interrupt(struct go7007 *go)
722 struct go7007_usb *usb = go->hpi_context;
725 r = usb_submit_urb(usb->intr_urb, GFP_KERNEL);
728 "go7007-usb: unable to submit interrupt urb: %d\n", r);
734 static void go7007_usb_read_video_pipe_complete(struct urb *urb)
736 struct go7007 *go = (struct go7007 *)urb->context;
737 int r, status = urb-> status;
739 if (!go->streaming) {
740 wake_up_interruptible(&go->frame_waitq);
744 printk(KERN_ERR "go7007-usb: error in video pipe: %d\n", status);
747 if (urb->actual_length != urb->transfer_buffer_length) {
748 printk(KERN_ERR "go7007-usb: short read in video pipe!\n");
751 go7007_parse_video_stream(go, urb->transfer_buffer, urb->actual_length);
752 r = usb_submit_urb(urb, GFP_ATOMIC);
754 printk(KERN_ERR "go7007-usb: error in video pipe: %d\n", r);
757 static void go7007_usb_read_audio_pipe_complete(struct urb *urb)
759 struct go7007 *go = (struct go7007 *)urb->context;
760 int r, status = urb->status;
765 printk(KERN_ERR "go7007-usb: error in audio pipe: %d\n", status);
768 if (urb->actual_length != urb->transfer_buffer_length) {
769 printk(KERN_ERR "go7007-usb: short read in audio pipe!\n");
772 if (go->audio_deliver != NULL)
773 go->audio_deliver(go, urb->transfer_buffer, urb->actual_length);
774 r = usb_submit_urb(urb, GFP_ATOMIC);
776 printk(KERN_ERR "go7007-usb: error in audio pipe: %d\n", r);
779 static int go7007_usb_stream_start(struct go7007 *go)
781 struct go7007_usb *usb = go->hpi_context;
784 for (i = 0; i < 8; ++i) {
785 r = usb_submit_urb(usb->video_urbs[i], GFP_KERNEL);
787 printk(KERN_ERR "go7007-usb: error submitting video "
788 "urb %d: %d\n", i, r);
789 goto video_submit_failed;
792 if (!go->audio_enabled)
795 for (i = 0; i < 8; ++i) {
796 r = usb_submit_urb(usb->audio_urbs[i], GFP_KERNEL);
798 printk(KERN_ERR "go7007-usb: error submitting audio "
799 "urb %d: %d\n", i, r);
800 goto audio_submit_failed;
806 for (i = 0; i < 7; ++i)
807 usb_kill_urb(usb->audio_urbs[i]);
809 for (i = 0; i < 8; ++i)
810 usb_kill_urb(usb->video_urbs[i]);
814 static int go7007_usb_stream_stop(struct go7007 *go)
816 struct go7007_usb *usb = go->hpi_context;
819 if (go->status == STATUS_SHUTDOWN)
821 for (i = 0; i < 8; ++i)
822 usb_kill_urb(usb->video_urbs[i]);
823 if (go->audio_enabled)
824 for (i = 0; i < 8; ++i)
825 usb_kill_urb(usb->audio_urbs[i]);
829 static int go7007_usb_send_firmware(struct go7007 *go, u8 *data, int len)
831 struct go7007_usb *usb = go->hpi_context;
832 int transferred, pipe;
835 #ifdef GO7007_USB_DEBUG
836 printk(KERN_DEBUG "go7007-usb: DownloadBuffer sending %d bytes\n", len);
839 if (usb->board->flags & GO7007_USB_EZUSB)
840 pipe = usb_sndbulkpipe(usb->usbdev, 2);
842 pipe = usb_sndbulkpipe(usb->usbdev, 3);
844 return usb_bulk_msg(usb->usbdev, pipe, data, len,
845 &transferred, timeout);
848 static struct go7007_hpi_ops go7007_usb_ezusb_hpi_ops = {
849 .interface_reset = go7007_usb_interface_reset,
850 .write_interrupt = go7007_usb_ezusb_write_interrupt,
851 .read_interrupt = go7007_usb_read_interrupt,
852 .stream_start = go7007_usb_stream_start,
853 .stream_stop = go7007_usb_stream_stop,
854 .send_firmware = go7007_usb_send_firmware,
857 static struct go7007_hpi_ops go7007_usb_onboard_hpi_ops = {
858 .interface_reset = go7007_usb_interface_reset,
859 .write_interrupt = go7007_usb_onboard_write_interrupt,
860 .read_interrupt = go7007_usb_read_interrupt,
861 .stream_start = go7007_usb_stream_start,
862 .stream_stop = go7007_usb_stream_stop,
863 .send_firmware = go7007_usb_send_firmware,
866 /********************* Driver for EZ-USB I2C adapter *********************/
868 static int go7007_usb_i2c_master_xfer(struct i2c_adapter *adapter,
869 struct i2c_msg msgs[], int num)
871 struct go7007 *go = i2c_get_adapdata(adapter);
872 struct go7007_usb *usb = go->hpi_context;
877 if (go->status == STATUS_SHUTDOWN)
880 down(&usb->i2c_lock);
882 for (i = 0; i < num; ++i) {
883 /* The hardware command is "write some bytes then read some
884 * bytes", so we try to coalesce a write followed by a read
885 * into a single USB transaction */
886 if (i + 1 < num && msgs[i].addr == msgs[i + 1].addr &&
887 !(msgs[i].flags & I2C_M_RD) &&
888 (msgs[i + 1].flags & I2C_M_RD)) {
889 #ifdef GO7007_I2C_DEBUG
890 printk(KERN_DEBUG "go7007-usb: i2c write/read %d/%d "
891 "bytes on %02x\n", msgs[i].len,
892 msgs[i + 1].len, msgs[i].addr);
895 buf[1] = msgs[i].len + 1;
896 buf[2] = msgs[i].addr << 1;
897 memcpy(&buf[3], msgs[i].buf, msgs[i].len);
898 buf_len = msgs[i].len + 3;
899 buf[buf_len++] = msgs[++i].len;
900 } else if (msgs[i].flags & I2C_M_RD) {
901 #ifdef GO7007_I2C_DEBUG
902 printk(KERN_DEBUG "go7007-usb: i2c read %d "
903 "bytes on %02x\n", msgs[i].len,
908 buf[2] = msgs[i].addr << 1;
909 buf[3] = msgs[i].len;
912 #ifdef GO7007_I2C_DEBUG
913 printk(KERN_DEBUG "go7007-usb: i2c write %d "
914 "bytes on %02x\n", msgs[i].len,
918 buf[1] = msgs[i].len + 1;
919 buf[2] = msgs[i].addr << 1;
920 memcpy(&buf[3], msgs[i].buf, msgs[i].len);
921 buf_len = msgs[i].len + 3;
924 if (go7007_usb_vendor_request(go, 0x24, 0, 0,
925 buf, buf_len, 0) < 0)
927 if (msgs[i].flags & I2C_M_RD) {
928 memset(buf, 0, sizeof(buf));
929 if (go7007_usb_vendor_request(go, 0x25, 0, 0, buf,
930 msgs[i].len + 1, 1) < 0)
932 memcpy(msgs[i].buf, buf + 1, msgs[i].len);
942 static u32 go7007_usb_functionality(struct i2c_adapter *adapter)
944 /* No errors are reported by the hardware, so we don't bother
945 * supporting quick writes to avoid confusing probing */
946 return (I2C_FUNC_SMBUS_EMUL) & ~I2C_FUNC_SMBUS_QUICK;
949 static struct i2c_algorithm go7007_usb_algo = {
950 .master_xfer = go7007_usb_i2c_master_xfer,
951 .functionality = go7007_usb_functionality,
954 static struct i2c_adapter go7007_usb_adap_templ = {
955 .owner = THIS_MODULE,
956 .name = "WIS GO7007SB EZ-USB",
957 .algo = &go7007_usb_algo,
960 /********************* USB add/remove functions *********************/
962 static int go7007_usb_probe(struct usb_interface *intf,
963 const struct usb_device_id *id)
966 struct go7007_usb *usb;
967 struct go7007_usb_board *board;
968 struct usb_device *usbdev = interface_to_usbdev(intf);
970 int video_pipe, i, v_urb_len;
972 printk(KERN_DEBUG "go7007-usb: probing new GO7007 USB board\n");
974 switch (id->driver_info) {
975 case GO7007_BOARDID_MATRIX_II:
976 name = "WIS Matrix II or compatible";
977 board = &board_matrix_ii;
979 case GO7007_BOARDID_MATRIX_RELOAD:
980 name = "WIS Matrix Reloaded or compatible";
981 board = &board_matrix_reload;
983 case GO7007_BOARDID_MATRIX_REV:
984 name = "WIS Matrix Revolution or compatible";
985 board = &board_matrix_revolution;
987 case GO7007_BOARDID_STAR_TREK:
988 name = "WIS Star Trek or compatible";
989 board = &board_star_trek;
991 case GO7007_BOARDID_XMEN:
992 name = "WIS XMen or compatible";
995 case GO7007_BOARDID_XMEN_II:
996 name = "WIS XMen II or compatible";
999 case GO7007_BOARDID_XMEN_III:
1000 name = "WIS XMen III or compatible";
1001 board = &board_xmen;
1003 case GO7007_BOARDID_PX_M402U:
1004 name = "Plextor PX-M402U";
1005 board = &board_matrix_ii;
1007 case GO7007_BOARDID_PX_TV402U_ANY:
1008 name = "Plextor PX-TV402U (unknown tuner)";
1009 board = &board_px_tv402u;
1011 case GO7007_BOARDID_LIFEVIEW_LR192:
1012 printk(KERN_ERR "go7007-usb: The Lifeview TV Walker Ultra "
1013 "is not supported. Sorry!\n");
1015 name = "Lifeview TV Walker Ultra";
1016 board = &board_lifeview_lr192;
1018 case GO7007_BOARDID_SENSORAY_2250:
1019 printk(KERN_INFO "Sensoray 2250 found\n");
1020 name = "Sensoray 2250/2251\n";
1021 board = &board_sensoray_2250;
1024 printk(KERN_ERR "go7007-usb: unknown board ID %d!\n",
1025 (unsigned int)id->driver_info);
1029 usb = kzalloc(sizeof(struct go7007_usb), GFP_KERNEL);
1033 /* Allocate the URB and buffer for receiving incoming interrupts */
1034 usb->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1035 if (usb->intr_urb == NULL)
1037 usb->intr_urb->transfer_buffer = kmalloc(2*sizeof(u16), GFP_KERNEL);
1038 if (usb->intr_urb->transfer_buffer == NULL)
1041 go = go7007_alloc(&board->main_info, &intf->dev);
1045 usb->usbdev = usbdev;
1046 go->board_id = id->driver_info;
1047 strncpy(go->name, name, sizeof(go->name));
1048 if (board->flags & GO7007_USB_EZUSB)
1049 go->hpi_ops = &go7007_usb_ezusb_hpi_ops;
1051 go->hpi_ops = &go7007_usb_onboard_hpi_ops;
1052 go->hpi_context = usb;
1053 usb_fill_int_urb(usb->intr_urb, usb->usbdev,
1054 usb_rcvintpipe(usb->usbdev, 4),
1055 usb->intr_urb->transfer_buffer, 2*sizeof(u16),
1056 go7007_usb_readinterrupt_complete, go, 8);
1057 usb_set_intfdata(intf, go);
1059 /* Boot the GO7007 */
1060 if (go7007_boot_encoder(go, go->board_info->flags &
1061 GO7007_BOARD_USE_ONBOARD_I2C) < 0)
1064 /* Register the EZ-USB I2C adapter, if we're using it */
1065 if (board->flags & GO7007_USB_EZUSB_I2C) {
1066 memcpy(&go->i2c_adapter, &go7007_usb_adap_templ,
1067 sizeof(go7007_usb_adap_templ));
1068 init_MUTEX(&usb->i2c_lock);
1069 go->i2c_adapter.dev.parent = go->dev;
1070 i2c_set_adapdata(&go->i2c_adapter, go);
1071 if (i2c_add_adapter(&go->i2c_adapter) < 0) {
1073 "go7007-usb: error: i2c_add_adapter failed\n");
1076 go->i2c_adapter_online = 1;
1079 /* Pelco and Adlink reused the XMen and XMen-III vendor and product
1080 * IDs for their own incompatible designs. We can detect XMen boards
1081 * by probing the sensor, but there is no way to probe the sensors on
1082 * the Pelco and Adlink designs so we default to the Adlink. If it
1083 * is actually a Pelco, the user must set the assume_endura module
1085 if ((go->board_id == GO7007_BOARDID_XMEN ||
1086 go->board_id == GO7007_BOARDID_XMEN_III) &&
1087 go->i2c_adapter_online) {
1088 union i2c_smbus_data data;
1090 /* Check to see if register 0x0A is 0x76 */
1091 i2c_smbus_xfer(&go->i2c_adapter, 0x21, I2C_CLIENT_SCCB,
1092 I2C_SMBUS_READ, 0x0A, I2C_SMBUS_BYTE_DATA, &data);
1093 if (data.byte != 0x76) {
1094 if (assume_endura) {
1095 go->board_id = GO7007_BOARDID_ENDURA;
1096 usb->board = board = &board_endura;
1097 go->board_info = &board->main_info;
1098 strncpy(go->name, "Pelco Endura",
1103 /* set GPIO5 to be an output, currently low */
1104 go7007_write_addr(go, 0x3c82, 0x0000);
1105 go7007_write_addr(go, 0x3c80, 0x00df);
1106 /* read channel number from GPIO[1:0] */
1107 go7007_read_addr(go, 0x3c81, &channel);
1109 go->board_id = GO7007_BOARDID_ADLINK_MPG24;
1110 usb->board = board = &board_adlink_mpg24;
1111 go->board_info = &board->main_info;
1112 go->channel_number = channel;
1113 snprintf(go->name, sizeof(go->name),
1114 "Adlink PCI-MPG24, channel #%d",
1120 /* Probe the tuner model on the TV402U */
1121 if (go->board_id == GO7007_BOARDID_PX_TV402U_ANY) {
1124 /* Board strapping indicates tuner model */
1125 if (go7007_usb_vendor_request(go, 0x41, 0, 0, data, 3, 1) < 0) {
1126 printk(KERN_ERR "go7007-usb: GPIO read failed!\n");
1129 switch (data[0] >> 6) {
1131 go->board_id = GO7007_BOARDID_PX_TV402U_EU;
1132 go->tuner_type = TUNER_SONY_BTF_PG472Z;
1133 strncpy(go->name, "Plextor PX-TV402U-EU",
1137 go->board_id = GO7007_BOARDID_PX_TV402U_JP;
1138 go->tuner_type = TUNER_SONY_BTF_PK467Z;
1139 strncpy(go->name, "Plextor PX-TV402U-JP",
1143 go->board_id = GO7007_BOARDID_PX_TV402U_NA;
1144 go->tuner_type = TUNER_SONY_BTF_PB463Z;
1145 strncpy(go->name, "Plextor PX-TV402U-NA",
1149 printk(KERN_DEBUG "go7007-usb: unable to detect "
1153 /* Configure tuner mode selection inputs connected
1154 * to the EZ-USB GPIO output pins */
1155 if (go7007_usb_vendor_request(go, 0x40, 0x7f02, 0,
1158 "go7007-usb: GPIO write failed!\n");
1163 /* Print a nasty message if the user attempts to use a USB2.0 device in
1164 * a USB1.1 port. There will be silent corruption of the stream. */
1165 if ((board->flags & GO7007_USB_EZUSB) &&
1166 usbdev->speed != USB_SPEED_HIGH)
1167 printk(KERN_ERR "go7007-usb: *** WARNING *** This device "
1168 "must be connected to a USB 2.0 port! "
1169 "Attempting to capture video through a USB 1.1 "
1170 "port will result in stream corruption, even "
1171 "at low bitrates!\n");
1173 /* Do any final GO7007 initialization, then register the
1174 * V4L2 and ALSA interfaces */
1175 if (go7007_register_encoder(go) < 0)
1178 /* Allocate the URBs and buffers for receiving the video stream */
1179 if (board->flags & GO7007_USB_EZUSB) {
1181 video_pipe = usb_rcvbulkpipe(usb->usbdev, 6);
1184 video_pipe = usb_rcvbulkpipe(usb->usbdev, 1);
1186 for (i = 0; i < 8; ++i) {
1187 usb->video_urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1188 if (usb->video_urbs[i] == NULL)
1190 usb->video_urbs[i]->transfer_buffer =
1191 kmalloc(v_urb_len, GFP_KERNEL);
1192 if (usb->video_urbs[i]->transfer_buffer == NULL)
1194 usb_fill_bulk_urb(usb->video_urbs[i], usb->usbdev, video_pipe,
1195 usb->video_urbs[i]->transfer_buffer, v_urb_len,
1196 go7007_usb_read_video_pipe_complete, go);
1199 /* Allocate the URBs and buffers for receiving the audio stream */
1200 if ((board->flags & GO7007_USB_EZUSB) && go->audio_enabled)
1201 for (i = 0; i < 8; ++i) {
1202 usb->audio_urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1203 if (usb->audio_urbs[i] == NULL)
1205 usb->audio_urbs[i]->transfer_buffer = kmalloc(4096,
1207 if (usb->audio_urbs[i]->transfer_buffer == NULL)
1209 usb_fill_bulk_urb(usb->audio_urbs[i], usb->usbdev,
1210 usb_rcvbulkpipe(usb->usbdev, 8),
1211 usb->audio_urbs[i]->transfer_buffer, 4096,
1212 go7007_usb_read_audio_pipe_complete, go);
1216 go->status = STATUS_ONLINE;
1220 go->status = STATUS_SHUTDOWN;
1224 if (usb->intr_urb) {
1225 kfree(usb->intr_urb->transfer_buffer);
1226 usb_free_urb(usb->intr_urb);
1232 static void go7007_usb_disconnect(struct usb_interface *intf)
1234 struct go7007 *go = usb_get_intfdata(intf);
1235 struct go7007_usb *usb = go->hpi_context;
1236 struct urb *vurb, *aurb;
1239 go->status = STATUS_SHUTDOWN;
1240 usb_kill_urb(usb->intr_urb);
1242 /* Free USB-related structs */
1243 for (i = 0; i < 8; ++i) {
1244 vurb = usb->video_urbs[i];
1247 if (vurb->transfer_buffer)
1248 kfree(vurb->transfer_buffer);
1251 aurb = usb->audio_urbs[i];
1254 if (aurb->transfer_buffer)
1255 kfree(aurb->transfer_buffer);
1259 kfree(usb->intr_urb->transfer_buffer);
1260 usb_free_urb(usb->intr_urb);
1262 kfree(go->hpi_context);
1267 static struct usb_driver go7007_usb_driver = {
1269 .probe = go7007_usb_probe,
1270 .disconnect = go7007_usb_disconnect,
1271 .id_table = go7007_usb_id_table,
1274 static int __init go7007_usb_init(void)
1276 return usb_register(&go7007_usb_driver);
1279 static void __exit go7007_usb_cleanup(void)
1281 usb_deregister(&go7007_usb_driver);
1284 module_init(go7007_usb_init);
1285 module_exit(go7007_usb_cleanup);
1287 MODULE_LICENSE("GPL v2");