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.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
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>
30 #include "go7007-priv.h"
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");
37 /* #define GO7007_I2C_DEBUG */ /* for debugging the EZ-USB I2C adapter */
39 #define HPI_STATUS_ADDR 0xFFF4
40 #define INT_PARAM_ADDR 0xFFF6
41 #define INT_INDEX_ADDR 0xFFF8
44 * Pipes on EZ-USB interface:
47 * 2 snd - Download firmware (control)
48 * 4 rcv - Read Interrupt (interrupt)
49 * 6 rcv - Read Video (bulk)
50 * 8 rcv - Read Audio (bulk)
53 #define GO7007_USB_EZUSB (1<<0)
54 #define GO7007_USB_EZUSB_I2C (1<<1)
56 struct go7007_usb_board {
58 struct go7007_board_info main_info;
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];
70 /*********************** Product specification data ***********************/
72 static const struct go7007_usb_board board_matrix_ii = {
73 .flags = GO7007_USB_EZUSB,
75 .flags = GO7007_BOARD_HAS_AUDIO |
76 GO7007_BOARD_USE_ONBOARD_I2C,
77 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
83 .sensor_flags = GO7007_SENSOR_656 |
84 GO7007_SENSOR_VALID_ENABLE |
86 GO7007_SENSOR_SAA7115 |
88 GO7007_SENSOR_SCALING,
108 .video_config = SAA7115_IDQ_IS_DEFAULT,
112 static const struct go7007_usb_board board_matrix_reload = {
113 .flags = GO7007_USB_EZUSB,
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,
124 .sensor_flags = GO7007_SENSOR_656 |
145 .video_config = SAA7115_IDQ_IS_DEFAULT,
149 static const struct go7007_usb_board board_star_trek = {
150 .flags = GO7007_USB_EZUSB | GO7007_USB_EZUSB_I2C,
152 .flags = GO7007_BOARD_HAS_AUDIO, /* |
153 GO7007_BOARD_HAS_TUNER, */
154 .sensor_flags = GO7007_SENSOR_656 |
155 GO7007_SENSOR_VALID_ENABLE |
157 GO7007_SENSOR_SAA7115 |
159 GO7007_SENSOR_SCALING,
160 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
161 GO7007_AUDIO_WORD_16,
177 * .audio_index = AUDIO_TUNER,
183 /* .audio_index = AUDIO_EXTERN, */
188 /* .audio_index = AUDIO_EXTERN, */
192 .video_config = SAA7115_IDQ_IS_DEFAULT,
196 static const struct go7007_usb_board board_px_tv402u = {
197 .flags = GO7007_USB_EZUSB | GO7007_USB_EZUSB_I2C,
199 .flags = GO7007_BOARD_HAS_AUDIO |
200 GO7007_BOARD_HAS_TUNER,
201 .sensor_flags = GO7007_SENSOR_656 |
202 GO7007_SENSOR_VALID_ENABLE |
204 GO7007_SENSOR_SAA7115 |
206 GO7007_SENSOR_SCALING,
207 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
208 GO7007_AUDIO_WORD_16,
233 .type = "sony-btf-mpx",
255 .video_config = SAA7115_IDQ_IS_DEFAULT,
259 .audio_input = UDA1342_IN2,
263 .audio_input = UDA1342_IN1,
270 static const struct go7007_usb_board board_xmen = {
273 .flags = GO7007_BOARD_USE_ONBOARD_I2C,
275 .sensor_flags = GO7007_SENSOR_VREF_POLAR,
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,
286 .audio_bclk_div = 48,
304 static const struct go7007_usb_board board_matrix_revolution = {
305 .flags = GO7007_USB_EZUSB,
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,
316 .sensor_flags = GO7007_SENSOR_656 |
341 static const struct go7007_usb_board board_lifeview_lr192 = {
342 .flags = GO7007_USB_EZUSB,
344 .flags = GO7007_BOARD_HAS_AUDIO |
345 GO7007_BOARD_USE_ONBOARD_I2C,
346 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
347 GO7007_AUDIO_WORD_16,
352 .sensor_flags = GO7007_SENSOR_656 |
353 GO7007_SENSOR_VALID_ENABLE |
356 GO7007_SENSOR_SCALING,
368 static const struct go7007_usb_board board_endura = {
372 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
373 GO7007_AUDIO_I2S_MASTER |
374 GO7007_AUDIO_WORD_16,
376 .audio_bclk_div = 48,
379 .sensor_flags = GO7007_SENSOR_656 |
381 .sensor_h_offset = 8,
392 static const struct go7007_usb_board board_adlink_mpg24 = {
395 .flags = GO7007_BOARD_USE_ONBOARD_I2C,
396 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
397 GO7007_AUDIO_I2S_MASTER |
398 GO7007_AUDIO_WORD_16,
403 .sensor_flags = GO7007_SENSOR_656 |
410 .addr = 0x00, /* yes, really */
411 .flags = I2C_CLIENT_TEN,
424 static const struct go7007_usb_board board_sensoray_2250 = {
425 .flags = GO7007_USB_EZUSB | GO7007_USB_EZUSB_I2C,
427 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
428 GO7007_AUDIO_I2S_MASTER |
429 GO7007_AUDIO_WORD_16,
430 .flags = GO7007_BOARD_HAS_AUDIO,
435 .sensor_flags = GO7007_SENSOR_656 |
475 static const struct go7007_usb_board board_ads_usbav_709 = {
476 .flags = GO7007_USB_EZUSB,
478 .flags = GO7007_BOARD_HAS_AUDIO |
479 GO7007_BOARD_USE_ONBOARD_I2C,
480 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
481 GO7007_AUDIO_I2S_MASTER |
482 GO7007_AUDIO_WORD_16,
487 .sensor_flags = GO7007_SENSOR_656 |
512 static const struct usb_device_id go7007_usb_id_table[] = {
514 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION |
515 USB_DEVICE_ID_MATCH_INT_INFO,
516 .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
517 .idProduct = 0x7007, /* Product ID of GO7007SB chip */
518 .bcdDevice_lo = 0x200, /* Revision number of XMen */
519 .bcdDevice_hi = 0x200,
520 .bInterfaceClass = 255,
521 .bInterfaceSubClass = 0,
522 .bInterfaceProtocol = 255,
523 .driver_info = (kernel_ulong_t)GO7007_BOARDID_XMEN,
526 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
527 .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
528 .idProduct = 0x7007, /* Product ID of GO7007SB chip */
529 .bcdDevice_lo = 0x202, /* Revision number of Matrix II */
530 .bcdDevice_hi = 0x202,
531 .driver_info = (kernel_ulong_t)GO7007_BOARDID_MATRIX_II,
534 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
535 .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
536 .idProduct = 0x7007, /* Product ID of GO7007SB chip */
537 .bcdDevice_lo = 0x204, /* Revision number of Matrix */
538 .bcdDevice_hi = 0x204, /* Reloaded */
539 .driver_info = (kernel_ulong_t)GO7007_BOARDID_MATRIX_RELOAD,
542 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION |
543 USB_DEVICE_ID_MATCH_INT_INFO,
544 .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
545 .idProduct = 0x7007, /* Product ID of GO7007SB chip */
546 .bcdDevice_lo = 0x205, /* Revision number of XMen-II */
547 .bcdDevice_hi = 0x205,
548 .bInterfaceClass = 255,
549 .bInterfaceSubClass = 0,
550 .bInterfaceProtocol = 255,
551 .driver_info = (kernel_ulong_t)GO7007_BOARDID_XMEN_II,
554 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
555 .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
556 .idProduct = 0x7007, /* Product ID of GO7007SB chip */
557 .bcdDevice_lo = 0x208, /* Revision number of Star Trek */
558 .bcdDevice_hi = 0x208,
559 .driver_info = (kernel_ulong_t)GO7007_BOARDID_STAR_TREK,
562 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION |
563 USB_DEVICE_ID_MATCH_INT_INFO,
564 .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
565 .idProduct = 0x7007, /* Product ID of GO7007SB chip */
566 .bcdDevice_lo = 0x209, /* Revision number of XMen-III */
567 .bcdDevice_hi = 0x209,
568 .bInterfaceClass = 255,
569 .bInterfaceSubClass = 0,
570 .bInterfaceProtocol = 255,
571 .driver_info = (kernel_ulong_t)GO7007_BOARDID_XMEN_III,
574 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
575 .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
576 .idProduct = 0x7007, /* Product ID of GO7007SB chip */
577 .bcdDevice_lo = 0x210, /* Revision number of Matrix */
578 .bcdDevice_hi = 0x210, /* Revolution */
579 .driver_info = (kernel_ulong_t)GO7007_BOARDID_MATRIX_REV,
582 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
583 .idVendor = 0x093b, /* Vendor ID of Plextor */
584 .idProduct = 0xa102, /* Product ID of M402U */
585 .bcdDevice_lo = 0x1, /* revision number of Blueberry */
587 .driver_info = (kernel_ulong_t)GO7007_BOARDID_PX_M402U,
590 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
591 .idVendor = 0x093b, /* Vendor ID of Plextor */
592 .idProduct = 0xa104, /* Product ID of TV402U */
595 .driver_info = (kernel_ulong_t)GO7007_BOARDID_PX_TV402U,
598 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
599 .idVendor = 0x10fd, /* Vendor ID of Anubis Electronics */
600 .idProduct = 0xde00, /* Product ID of Lifeview LR192 */
603 .driver_info = (kernel_ulong_t)GO7007_BOARDID_LIFEVIEW_LR192,
606 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
607 .idVendor = 0x1943, /* Vendor ID Sensoray */
608 .idProduct = 0x2250, /* Product ID of 2250/2251 */
611 .driver_info = (kernel_ulong_t)GO7007_BOARDID_SENSORAY_2250,
614 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
615 .idVendor = 0x06e1, /* Vendor ID of ADS Technologies */
616 .idProduct = 0x0709, /* Product ID of DVD Xpress DX2 */
617 .bcdDevice_lo = 0x204,
618 .bcdDevice_hi = 0x204,
619 .driver_info = (kernel_ulong_t)GO7007_BOARDID_ADS_USBAV_709,
621 { } /* Terminating entry */
624 MODULE_DEVICE_TABLE(usb, go7007_usb_id_table);
626 /********************* Driver for EZ-USB HPI interface *********************/
628 static int go7007_usb_vendor_request(struct go7007 *go, int request,
629 int value, int index, void *transfer_buffer, int length, int in)
631 struct go7007_usb *usb = go->hpi_context;
635 return usb_control_msg(usb->usbdev,
636 usb_rcvctrlpipe(usb->usbdev, 0), request,
637 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
638 value, index, transfer_buffer, length, timeout);
640 return usb_control_msg(usb->usbdev,
641 usb_sndctrlpipe(usb->usbdev, 0), request,
642 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
643 value, index, transfer_buffer, length, timeout);
647 static int go7007_usb_interface_reset(struct go7007 *go)
649 struct go7007_usb *usb = go->hpi_context;
650 u16 intr_val, intr_data;
652 if (go->status == STATUS_SHUTDOWN)
655 if (go7007_write_interrupt(go, 0x0001, 0x0001) < 0)
659 if (usb->board->flags & GO7007_USB_EZUSB) {
660 /* Reset buffer in EZ-USB */
661 pr_debug("resetting EZ-USB buffers\n");
662 if (go7007_usb_vendor_request(go, 0x10, 0, 0, NULL, 0, 0) < 0 ||
663 go7007_usb_vendor_request(go, 0x10, 0, 0, NULL, 0, 0) < 0)
666 /* Reset encoder again */
667 if (go7007_write_interrupt(go, 0x0001, 0x0001) < 0)
672 /* Wait for an interrupt to indicate successful hardware reset */
673 if (go7007_read_interrupt(go, &intr_val, &intr_data) < 0 ||
674 (intr_val & ~0x1) != 0x55aa) {
675 dev_err(go->dev, "unable to reset the USB interface\n");
681 static int go7007_usb_ezusb_write_interrupt(struct go7007 *go,
684 struct go7007_usb *usb = go->hpi_context;
689 pr_debug("WriteInterrupt: %04x %04x\n", addr, data);
691 for (i = 0; i < 100; ++i) {
692 r = usb_control_msg(usb->usbdev,
693 usb_rcvctrlpipe(usb->usbdev, 0), 0x14,
694 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
695 0, HPI_STATUS_ADDR, go->usb_buf,
696 sizeof(status_reg), timeout);
699 status_reg = le16_to_cpu(*((__le16 *)go->usb_buf));
700 if (!(status_reg & 0x0010))
705 goto write_int_error;
707 dev_err(go->dev, "device is hung, status reg = 0x%04x\n", status_reg);
710 r = usb_control_msg(usb->usbdev, usb_sndctrlpipe(usb->usbdev, 0), 0x12,
711 USB_TYPE_VENDOR | USB_RECIP_DEVICE, data,
712 INT_PARAM_ADDR, NULL, 0, timeout);
714 goto write_int_error;
715 r = usb_control_msg(usb->usbdev, usb_sndctrlpipe(usb->usbdev, 0),
716 0x12, USB_TYPE_VENDOR | USB_RECIP_DEVICE, addr,
717 INT_INDEX_ADDR, NULL, 0, timeout);
719 goto write_int_error;
723 dev_err(go->dev, "error in WriteInterrupt: %d\n", r);
727 static int go7007_usb_onboard_write_interrupt(struct go7007 *go,
730 struct go7007_usb *usb = go->hpi_context;
734 pr_debug("WriteInterrupt: %04x %04x\n", addr, data);
736 go->usb_buf[0] = data & 0xff;
737 go->usb_buf[1] = data >> 8;
738 go->usb_buf[2] = addr & 0xff;
739 go->usb_buf[3] = addr >> 8;
740 go->usb_buf[4] = go->usb_buf[5] = go->usb_buf[6] = go->usb_buf[7] = 0;
741 r = usb_control_msg(usb->usbdev, usb_sndctrlpipe(usb->usbdev, 2), 0x00,
742 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT, 0x55aa,
743 0xf0f0, go->usb_buf, 8, timeout);
745 dev_err(go->dev, "error in WriteInterrupt: %d\n", r);
751 static void go7007_usb_readinterrupt_complete(struct urb *urb)
753 struct go7007 *go = (struct go7007 *)urb->context;
754 __le16 *regs = (__le16 *)urb->transfer_buffer;
755 int status = urb->status;
758 if (status != -ESHUTDOWN &&
759 go->status != STATUS_SHUTDOWN) {
760 dev_err(go->dev, "error in read interrupt: %d\n", urb->status);
762 wake_up(&go->interrupt_waitq);
765 } else if (urb->actual_length != urb->transfer_buffer_length) {
766 dev_err(go->dev, "short read in interrupt pipe!\n");
768 go->interrupt_available = 1;
769 go->interrupt_data = __le16_to_cpu(regs[0]);
770 go->interrupt_value = __le16_to_cpu(regs[1]);
771 pr_debug("ReadInterrupt: %04x %04x\n",
772 go->interrupt_value, go->interrupt_data);
775 wake_up(&go->interrupt_waitq);
778 static int go7007_usb_read_interrupt(struct go7007 *go)
780 struct go7007_usb *usb = go->hpi_context;
783 r = usb_submit_urb(usb->intr_urb, GFP_KERNEL);
785 dev_err(go->dev, "unable to submit interrupt urb: %d\n", r);
791 static void go7007_usb_read_video_pipe_complete(struct urb *urb)
793 struct go7007 *go = (struct go7007 *)urb->context;
794 int r, status = urb->status;
796 if (!vb2_is_streaming(&go->vidq)) {
797 wake_up_interruptible(&go->frame_waitq);
801 dev_err(go->dev, "error in video pipe: %d\n", status);
804 if (urb->actual_length != urb->transfer_buffer_length) {
805 dev_err(go->dev, "short read in video pipe!\n");
808 go7007_parse_video_stream(go, urb->transfer_buffer, urb->actual_length);
809 r = usb_submit_urb(urb, GFP_ATOMIC);
811 dev_err(go->dev, "error in video pipe: %d\n", r);
814 static void go7007_usb_read_audio_pipe_complete(struct urb *urb)
816 struct go7007 *go = (struct go7007 *)urb->context;
817 int r, status = urb->status;
819 if (!vb2_is_streaming(&go->vidq))
822 dev_err(go->dev, "error in audio pipe: %d\n",
826 if (urb->actual_length != urb->transfer_buffer_length) {
827 dev_err(go->dev, "short read in audio pipe!\n");
830 if (go->audio_deliver != NULL)
831 go->audio_deliver(go, urb->transfer_buffer, urb->actual_length);
832 r = usb_submit_urb(urb, GFP_ATOMIC);
834 dev_err(go->dev, "error in audio pipe: %d\n", r);
837 static int go7007_usb_stream_start(struct go7007 *go)
839 struct go7007_usb *usb = go->hpi_context;
842 for (i = 0; i < 8; ++i) {
843 r = usb_submit_urb(usb->video_urbs[i], GFP_KERNEL);
845 dev_err(go->dev, "error submitting video urb %d: %d\n", i, r);
846 goto video_submit_failed;
849 if (!go->audio_enabled)
852 for (i = 0; i < 8; ++i) {
853 r = usb_submit_urb(usb->audio_urbs[i], GFP_KERNEL);
855 dev_err(go->dev, "error submitting audio urb %d: %d\n", i, r);
856 goto audio_submit_failed;
862 for (i = 0; i < 7; ++i)
863 usb_kill_urb(usb->audio_urbs[i]);
865 for (i = 0; i < 8; ++i)
866 usb_kill_urb(usb->video_urbs[i]);
870 static int go7007_usb_stream_stop(struct go7007 *go)
872 struct go7007_usb *usb = go->hpi_context;
875 if (go->status == STATUS_SHUTDOWN)
877 for (i = 0; i < 8; ++i)
878 usb_kill_urb(usb->video_urbs[i]);
879 if (go->audio_enabled)
880 for (i = 0; i < 8; ++i)
881 usb_kill_urb(usb->audio_urbs[i]);
885 static int go7007_usb_send_firmware(struct go7007 *go, u8 *data, int len)
887 struct go7007_usb *usb = go->hpi_context;
888 int transferred, pipe;
891 pr_debug("DownloadBuffer sending %d bytes\n", len);
893 if (usb->board->flags & GO7007_USB_EZUSB)
894 pipe = usb_sndbulkpipe(usb->usbdev, 2);
896 pipe = usb_sndbulkpipe(usb->usbdev, 3);
898 return usb_bulk_msg(usb->usbdev, pipe, data, len,
899 &transferred, timeout);
902 static void go7007_usb_release(struct go7007 *go)
904 struct go7007_usb *usb = go->hpi_context;
905 struct urb *vurb, *aurb;
909 usb_kill_urb(usb->intr_urb);
910 kfree(usb->intr_urb->transfer_buffer);
911 usb_free_urb(usb->intr_urb);
914 /* Free USB-related structs */
915 for (i = 0; i < 8; ++i) {
916 vurb = usb->video_urbs[i];
919 kfree(vurb->transfer_buffer);
922 aurb = usb->audio_urbs[i];
925 kfree(aurb->transfer_buffer);
930 kfree(go->hpi_context);
933 static struct go7007_hpi_ops go7007_usb_ezusb_hpi_ops = {
934 .interface_reset = go7007_usb_interface_reset,
935 .write_interrupt = go7007_usb_ezusb_write_interrupt,
936 .read_interrupt = go7007_usb_read_interrupt,
937 .stream_start = go7007_usb_stream_start,
938 .stream_stop = go7007_usb_stream_stop,
939 .send_firmware = go7007_usb_send_firmware,
940 .release = go7007_usb_release,
943 static struct go7007_hpi_ops go7007_usb_onboard_hpi_ops = {
944 .interface_reset = go7007_usb_interface_reset,
945 .write_interrupt = go7007_usb_onboard_write_interrupt,
946 .read_interrupt = go7007_usb_read_interrupt,
947 .stream_start = go7007_usb_stream_start,
948 .stream_stop = go7007_usb_stream_stop,
949 .send_firmware = go7007_usb_send_firmware,
950 .release = go7007_usb_release,
953 /********************* Driver for EZ-USB I2C adapter *********************/
955 static int go7007_usb_i2c_master_xfer(struct i2c_adapter *adapter,
956 struct i2c_msg msgs[], int num)
958 struct go7007 *go = i2c_get_adapdata(adapter);
959 struct go7007_usb *usb = go->hpi_context;
960 u8 *buf = go->usb_buf;
964 if (go->status == STATUS_SHUTDOWN)
967 mutex_lock(&usb->i2c_lock);
969 for (i = 0; i < num; ++i) {
970 /* The hardware command is "write some bytes then read some
971 * bytes", so we try to coalesce a write followed by a read
972 * into a single USB transaction */
973 if (i + 1 < num && msgs[i].addr == msgs[i + 1].addr &&
974 !(msgs[i].flags & I2C_M_RD) &&
975 (msgs[i + 1].flags & I2C_M_RD)) {
976 #ifdef GO7007_I2C_DEBUG
977 pr_debug("i2c write/read %d/%d bytes on %02x\n",
978 msgs[i].len, msgs[i + 1].len, msgs[i].addr);
981 buf[1] = msgs[i].len + 1;
982 buf[2] = msgs[i].addr << 1;
983 memcpy(&buf[3], msgs[i].buf, msgs[i].len);
984 buf_len = msgs[i].len + 3;
985 buf[buf_len++] = msgs[++i].len;
986 } else if (msgs[i].flags & I2C_M_RD) {
987 #ifdef GO7007_I2C_DEBUG
988 pr_debug("i2c read %d bytes on %02x\n",
989 msgs[i].len, msgs[i].addr);
993 buf[2] = msgs[i].addr << 1;
994 buf[3] = msgs[i].len;
997 #ifdef GO7007_I2C_DEBUG
998 pr_debug("i2c write %d bytes on %02x\n",
999 msgs[i].len, msgs[i].addr);
1002 buf[1] = msgs[i].len + 1;
1003 buf[2] = msgs[i].addr << 1;
1004 memcpy(&buf[3], msgs[i].buf, msgs[i].len);
1005 buf_len = msgs[i].len + 3;
1008 if (go7007_usb_vendor_request(go, 0x24, 0, 0,
1009 buf, buf_len, 0) < 0)
1011 if (msgs[i].flags & I2C_M_RD) {
1012 memset(buf, 0, msgs[i].len + 1);
1013 if (go7007_usb_vendor_request(go, 0x25, 0, 0, buf,
1014 msgs[i].len + 1, 1) < 0)
1016 memcpy(msgs[i].buf, buf + 1, msgs[i].len);
1022 mutex_unlock(&usb->i2c_lock);
1026 static u32 go7007_usb_functionality(struct i2c_adapter *adapter)
1028 /* No errors are reported by the hardware, so we don't bother
1029 * supporting quick writes to avoid confusing probing */
1030 return (I2C_FUNC_SMBUS_EMUL) & ~I2C_FUNC_SMBUS_QUICK;
1033 static struct i2c_algorithm go7007_usb_algo = {
1034 .master_xfer = go7007_usb_i2c_master_xfer,
1035 .functionality = go7007_usb_functionality,
1038 static struct i2c_adapter go7007_usb_adap_templ = {
1039 .owner = THIS_MODULE,
1040 .name = "WIS GO7007SB EZ-USB",
1041 .algo = &go7007_usb_algo,
1044 /********************* USB add/remove functions *********************/
1046 static int go7007_usb_probe(struct usb_interface *intf,
1047 const struct usb_device_id *id)
1050 struct go7007_usb *usb;
1051 const struct go7007_usb_board *board;
1052 struct usb_device *usbdev = interface_to_usbdev(intf);
1053 unsigned num_i2c_devs;
1055 int video_pipe, i, v_urb_len;
1057 pr_debug("probing new GO7007 USB board\n");
1059 switch (id->driver_info) {
1060 case GO7007_BOARDID_MATRIX_II:
1061 name = "WIS Matrix II or compatible";
1062 board = &board_matrix_ii;
1064 case GO7007_BOARDID_MATRIX_RELOAD:
1065 name = "WIS Matrix Reloaded or compatible";
1066 board = &board_matrix_reload;
1068 case GO7007_BOARDID_MATRIX_REV:
1069 name = "WIS Matrix Revolution or compatible";
1070 board = &board_matrix_revolution;
1072 case GO7007_BOARDID_STAR_TREK:
1073 name = "WIS Star Trek or compatible";
1074 board = &board_star_trek;
1076 case GO7007_BOARDID_XMEN:
1077 name = "WIS XMen or compatible";
1078 board = &board_xmen;
1080 case GO7007_BOARDID_XMEN_II:
1081 name = "WIS XMen II or compatible";
1082 board = &board_xmen;
1084 case GO7007_BOARDID_XMEN_III:
1085 name = "WIS XMen III or compatible";
1086 board = &board_xmen;
1088 case GO7007_BOARDID_PX_M402U:
1089 name = "Plextor PX-M402U";
1090 board = &board_matrix_ii;
1092 case GO7007_BOARDID_PX_TV402U:
1093 name = "Plextor PX-TV402U (unknown tuner)";
1094 board = &board_px_tv402u;
1096 case GO7007_BOARDID_LIFEVIEW_LR192:
1097 dev_err(&intf->dev, "The Lifeview TV Walker Ultra is not supported. Sorry!\n");
1099 name = "Lifeview TV Walker Ultra";
1100 board = &board_lifeview_lr192;
1102 case GO7007_BOARDID_SENSORAY_2250:
1103 dev_info(&intf->dev, "Sensoray 2250 found\n");
1104 name = "Sensoray 2250/2251";
1105 board = &board_sensoray_2250;
1107 case GO7007_BOARDID_ADS_USBAV_709:
1108 name = "ADS Tech DVD Xpress DX2";
1109 board = &board_ads_usbav_709;
1112 dev_err(&intf->dev, "unknown board ID %d!\n",
1113 (unsigned int)id->driver_info);
1117 go = go7007_alloc(&board->main_info, &intf->dev);
1121 usb = kzalloc(sizeof(struct go7007_usb), GFP_KERNEL);
1128 usb->usbdev = usbdev;
1129 usb_make_path(usbdev, go->bus_info, sizeof(go->bus_info));
1130 go->board_id = id->driver_info;
1131 strncpy(go->name, name, sizeof(go->name));
1132 if (board->flags & GO7007_USB_EZUSB)
1133 go->hpi_ops = &go7007_usb_ezusb_hpi_ops;
1135 go->hpi_ops = &go7007_usb_onboard_hpi_ops;
1136 go->hpi_context = usb;
1138 /* Allocate the URB and buffer for receiving incoming interrupts */
1139 usb->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1140 if (usb->intr_urb == NULL)
1142 usb->intr_urb->transfer_buffer = kmalloc(2*sizeof(u16), GFP_KERNEL);
1143 if (usb->intr_urb->transfer_buffer == NULL)
1146 if (go->board_id == GO7007_BOARDID_SENSORAY_2250)
1147 usb_fill_bulk_urb(usb->intr_urb, usb->usbdev,
1148 usb_rcvbulkpipe(usb->usbdev, 4),
1149 usb->intr_urb->transfer_buffer, 2*sizeof(u16),
1150 go7007_usb_readinterrupt_complete, go);
1152 usb_fill_int_urb(usb->intr_urb, usb->usbdev,
1153 usb_rcvintpipe(usb->usbdev, 4),
1154 usb->intr_urb->transfer_buffer, 2*sizeof(u16),
1155 go7007_usb_readinterrupt_complete, go, 8);
1156 usb_set_intfdata(intf, &go->v4l2_dev);
1158 /* Boot the GO7007 */
1159 if (go7007_boot_encoder(go, go->board_info->flags &
1160 GO7007_BOARD_USE_ONBOARD_I2C) < 0)
1163 /* Register the EZ-USB I2C adapter, if we're using it */
1164 if (board->flags & GO7007_USB_EZUSB_I2C) {
1165 memcpy(&go->i2c_adapter, &go7007_usb_adap_templ,
1166 sizeof(go7007_usb_adap_templ));
1167 mutex_init(&usb->i2c_lock);
1168 go->i2c_adapter.dev.parent = go->dev;
1169 i2c_set_adapdata(&go->i2c_adapter, go);
1170 if (i2c_add_adapter(&go->i2c_adapter) < 0) {
1171 dev_err(go->dev, "error: i2c_add_adapter failed\n");
1174 go->i2c_adapter_online = 1;
1177 /* Pelco and Adlink reused the XMen and XMen-III vendor and product
1178 * IDs for their own incompatible designs. We can detect XMen boards
1179 * by probing the sensor, but there is no way to probe the sensors on
1180 * the Pelco and Adlink designs so we default to the Adlink. If it
1181 * is actually a Pelco, the user must set the assume_endura module
1183 if ((go->board_id == GO7007_BOARDID_XMEN ||
1184 go->board_id == GO7007_BOARDID_XMEN_III) &&
1185 go->i2c_adapter_online) {
1186 union i2c_smbus_data data;
1188 /* Check to see if register 0x0A is 0x76 */
1189 i2c_smbus_xfer(&go->i2c_adapter, 0x21, I2C_CLIENT_SCCB,
1190 I2C_SMBUS_READ, 0x0A, I2C_SMBUS_BYTE_DATA, &data);
1191 if (data.byte != 0x76) {
1192 if (assume_endura) {
1193 go->board_id = GO7007_BOARDID_ENDURA;
1194 usb->board = board = &board_endura;
1195 go->board_info = &board->main_info;
1196 strncpy(go->name, "Pelco Endura",
1201 /* read channel number from GPIO[1:0] */
1202 go7007_read_addr(go, 0x3c81, &channel);
1204 go->board_id = GO7007_BOARDID_ADLINK_MPG24;
1205 usb->board = board = &board_adlink_mpg24;
1206 go->board_info = &board->main_info;
1207 go->channel_number = channel;
1208 snprintf(go->name, sizeof(go->name),
1209 "Adlink PCI-MPG24, channel #%d",
1212 go7007_update_board(go);
1216 num_i2c_devs = go->board_info->num_i2c_devs;
1218 /* Probe the tuner model on the TV402U */
1219 if (go->board_id == GO7007_BOARDID_PX_TV402U) {
1220 /* Board strapping indicates tuner model */
1221 if (go7007_usb_vendor_request(go, 0x41, 0, 0, go->usb_buf, 3,
1223 dev_err(go->dev, "GPIO read failed!\n");
1226 switch (go->usb_buf[0] >> 6) {
1228 go->tuner_type = TUNER_SONY_BTF_PG472Z;
1229 go->std = V4L2_STD_PAL;
1230 strncpy(go->name, "Plextor PX-TV402U-EU",
1234 go->tuner_type = TUNER_SONY_BTF_PK467Z;
1235 go->std = V4L2_STD_NTSC_M_JP;
1237 strncpy(go->name, "Plextor PX-TV402U-JP",
1241 go->tuner_type = TUNER_SONY_BTF_PB463Z;
1243 strncpy(go->name, "Plextor PX-TV402U-NA",
1247 pr_debug("unable to detect tuner type!\n");
1250 /* Configure tuner mode selection inputs connected
1251 * to the EZ-USB GPIO output pins */
1252 if (go7007_usb_vendor_request(go, 0x40, 0x7f02, 0,
1254 dev_err(go->dev, "GPIO write failed!\n");
1259 /* Print a nasty message if the user attempts to use a USB2.0 device in
1260 * a USB1.1 port. There will be silent corruption of the stream. */
1261 if ((board->flags & GO7007_USB_EZUSB) &&
1262 usbdev->speed != USB_SPEED_HIGH)
1263 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");
1265 /* Allocate the URBs and buffers for receiving the video stream */
1266 if (board->flags & GO7007_USB_EZUSB) {
1268 video_pipe = usb_rcvbulkpipe(usb->usbdev, 6);
1271 video_pipe = usb_rcvbulkpipe(usb->usbdev, 1);
1273 for (i = 0; i < 8; ++i) {
1274 usb->video_urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1275 if (usb->video_urbs[i] == NULL)
1277 usb->video_urbs[i]->transfer_buffer =
1278 kmalloc(v_urb_len, GFP_KERNEL);
1279 if (usb->video_urbs[i]->transfer_buffer == NULL)
1281 usb_fill_bulk_urb(usb->video_urbs[i], usb->usbdev, video_pipe,
1282 usb->video_urbs[i]->transfer_buffer, v_urb_len,
1283 go7007_usb_read_video_pipe_complete, go);
1286 /* Allocate the URBs and buffers for receiving the audio stream */
1287 if ((board->flags & GO7007_USB_EZUSB) &&
1288 (board->flags & GO7007_BOARD_HAS_AUDIO)) {
1289 for (i = 0; i < 8; ++i) {
1290 usb->audio_urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1291 if (usb->audio_urbs[i] == NULL)
1293 usb->audio_urbs[i]->transfer_buffer = kmalloc(4096,
1295 if (usb->audio_urbs[i]->transfer_buffer == NULL)
1297 usb_fill_bulk_urb(usb->audio_urbs[i], usb->usbdev,
1298 usb_rcvbulkpipe(usb->usbdev, 8),
1299 usb->audio_urbs[i]->transfer_buffer, 4096,
1300 go7007_usb_read_audio_pipe_complete, go);
1304 /* Do any final GO7007 initialization, then register the
1305 * V4L2 and ALSA interfaces */
1306 if (go7007_register_encoder(go, num_i2c_devs) < 0)
1309 go->status = STATUS_ONLINE;
1313 go7007_usb_release(go);
1318 static void go7007_usb_disconnect(struct usb_interface *intf)
1320 struct go7007 *go = to_go7007(usb_get_intfdata(intf));
1322 mutex_lock(&go->queue_lock);
1323 mutex_lock(&go->serialize_lock);
1325 if (go->audio_enabled)
1326 go7007_snd_remove(go);
1328 go->status = STATUS_SHUTDOWN;
1329 v4l2_device_disconnect(&go->v4l2_dev);
1330 video_unregister_device(&go->vdev);
1331 mutex_unlock(&go->serialize_lock);
1332 mutex_unlock(&go->queue_lock);
1334 v4l2_device_put(&go->v4l2_dev);
1337 static struct usb_driver go7007_usb_driver = {
1339 .probe = go7007_usb_probe,
1340 .disconnect = go7007_usb_disconnect,
1341 .id_table = go7007_usb_id_table,
1344 module_usb_driver(go7007_usb_driver);
1345 MODULE_LICENSE("GPL v2");