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, "
37 "hardware is a Pelco Endura");
39 /* #define GO7007_USB_DEBUG */
40 /* #define GO7007_I2C_DEBUG */ /* for debugging the EZ-USB I2C adapter */
42 #define HPI_STATUS_ADDR 0xFFF4
43 #define INT_PARAM_ADDR 0xFFF6
44 #define INT_INDEX_ADDR 0xFFF8
47 * Pipes on EZ-USB interface:
50 * 2 snd - Download firmware (control)
51 * 4 rcv - Read Interrupt (interrupt)
52 * 6 rcv - Read Video (bulk)
53 * 8 rcv - Read Audio (bulk)
56 #define GO7007_USB_EZUSB (1<<0)
57 #define GO7007_USB_EZUSB_I2C (1<<1)
59 struct go7007_usb_board {
61 struct go7007_board_info main_info;
65 struct go7007_usb_board *board;
66 struct mutex i2c_lock;
67 struct usb_device *usbdev;
68 struct urb *video_urbs[8];
69 struct urb *audio_urbs[8];
73 /*********************** Product specification data ***********************/
75 static struct go7007_usb_board board_matrix_ii = {
76 .flags = GO7007_USB_EZUSB,
78 .firmware = "go7007tv.bin",
79 .flags = GO7007_BOARD_HAS_AUDIO |
80 GO7007_BOARD_USE_ONBOARD_I2C,
81 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
87 .sensor_flags = GO7007_SENSOR_656 |
88 GO7007_SENSOR_VALID_ENABLE |
91 GO7007_SENSOR_SCALING,
95 .type = "wis_saa7115",
96 .id = I2C_DRIVERID_WIS_SAA7115,
114 static struct go7007_usb_board board_matrix_reload = {
115 .flags = GO7007_USB_EZUSB,
117 .firmware = "go7007tv.bin",
118 .flags = GO7007_BOARD_HAS_AUDIO |
119 GO7007_BOARD_USE_ONBOARD_I2C,
120 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
121 GO7007_AUDIO_I2S_MASTER |
122 GO7007_AUDIO_WORD_16,
127 .sensor_flags = GO7007_SENSOR_656 |
132 .type = "wis_saa7113",
133 .id = I2C_DRIVERID_WIS_SAA7113,
151 static struct go7007_usb_board board_star_trek = {
152 .flags = GO7007_USB_EZUSB | GO7007_USB_EZUSB_I2C,
154 .firmware = "go7007tv.bin",
155 .flags = GO7007_BOARD_HAS_AUDIO, /* |
156 GO7007_BOARD_HAS_TUNER, */
157 .sensor_flags = GO7007_SENSOR_656 |
158 GO7007_SENSOR_VALID_ENABLE |
161 GO7007_SENSOR_SCALING,
162 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
163 GO7007_AUDIO_WORD_16,
170 .type = "wis_saa7115",
171 .id = I2C_DRIVERID_WIS_SAA7115,
179 /* .audio_input = AUDIO_EXTERN, */
184 /* .audio_input = AUDIO_EXTERN, */
189 * .audio_input = AUDIO_TUNER,
197 static struct go7007_usb_board board_px_tv402u = {
198 .flags = GO7007_USB_EZUSB | GO7007_USB_EZUSB_I2C,
200 .firmware = "go7007tv.bin",
201 .flags = GO7007_BOARD_HAS_AUDIO |
202 GO7007_BOARD_HAS_TUNER,
203 .sensor_flags = GO7007_SENSOR_656 |
204 GO7007_SENSOR_VALID_ENABLE |
207 GO7007_SENSOR_SCALING,
208 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
209 GO7007_AUDIO_WORD_16,
216 .type = "wis_saa7115",
217 .id = I2C_DRIVERID_WIS_SAA7115,
221 .type = "wis_uda1342",
222 .id = I2C_DRIVERID_WIS_UDA1342,
226 .type = "wis_sony_tuner",
227 .id = I2C_DRIVERID_WIS_SONY_TUNER,
235 .audio_input = TVAUDIO_INPUT_EXTERN,
240 .audio_input = TVAUDIO_INPUT_EXTERN,
245 .audio_input = TVAUDIO_INPUT_TUNER,
252 static struct go7007_usb_board board_xmen = {
255 .firmware = "go7007tv.bin",
256 .flags = GO7007_BOARD_USE_ONBOARD_I2C,
258 .sensor_flags = GO7007_SENSOR_VREF_POLAR,
260 .sensor_height = 240,
261 .sensor_framerate = 30030,
262 .audio_flags = GO7007_AUDIO_ONE_CHANNEL |
263 GO7007_AUDIO_I2S_MODE_3 |
264 GO7007_AUDIO_WORD_14 |
265 GO7007_AUDIO_I2S_MASTER |
266 GO7007_AUDIO_BCLK_POLAR |
267 GO7007_AUDIO_OKI_MODE,
269 .audio_bclk_div = 48,
274 .type = "wis_ov7640",
275 .id = I2C_DRIVERID_WIS_OV7640,
288 static struct go7007_usb_board board_matrix_revolution = {
289 .flags = GO7007_USB_EZUSB,
291 .firmware = "go7007tv.bin",
292 .flags = GO7007_BOARD_HAS_AUDIO |
293 GO7007_BOARD_USE_ONBOARD_I2C,
294 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
295 GO7007_AUDIO_I2S_MASTER |
296 GO7007_AUDIO_WORD_16,
301 .sensor_flags = GO7007_SENSOR_656 |
307 .type = "wis_tw9903",
308 .id = I2C_DRIVERID_WIS_TW9903,
326 static struct go7007_usb_board board_lifeview_lr192 = {
327 .flags = GO7007_USB_EZUSB,
329 .firmware = "go7007tv.bin",
330 .flags = GO7007_BOARD_HAS_AUDIO |
331 GO7007_BOARD_USE_ONBOARD_I2C,
332 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
333 GO7007_AUDIO_WORD_16,
338 .sensor_flags = GO7007_SENSOR_656 |
339 GO7007_SENSOR_VALID_ENABLE |
342 GO7007_SENSOR_SCALING,
354 static struct go7007_usb_board board_endura = {
357 .firmware = "go7007tv.bin",
359 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
360 GO7007_AUDIO_I2S_MASTER |
361 GO7007_AUDIO_WORD_16,
363 .audio_bclk_div = 48,
366 .sensor_flags = GO7007_SENSOR_656 |
368 .sensor_h_offset = 8,
379 static struct go7007_usb_board board_adlink_mpg24 = {
382 .firmware = "go7007tv.bin",
383 .flags = GO7007_BOARD_USE_ONBOARD_I2C,
384 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
385 GO7007_AUDIO_I2S_MASTER |
386 GO7007_AUDIO_WORD_16,
391 .sensor_flags = GO7007_SENSOR_656 |
397 .type = "wis_twTW2804",
398 .id = I2C_DRIVERID_WIS_TW2804,
399 .addr = 0x00, /* yes, really */
411 static struct go7007_usb_board board_sensoray_2250 = {
412 .flags = GO7007_USB_EZUSB | GO7007_USB_EZUSB_I2C,
414 .firmware = "go7007tv.bin",
415 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
416 GO7007_AUDIO_I2S_MASTER |
417 GO7007_AUDIO_WORD_16,
418 .flags = GO7007_BOARD_HAS_AUDIO,
423 .sensor_flags = GO7007_SENSOR_656 |
429 .id = I2C_DRIVERID_S2250,
447 MODULE_FIRMWARE("go7007tv.bin");
449 static const struct usb_device_id go7007_usb_id_table[] = {
451 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION |
452 USB_DEVICE_ID_MATCH_INT_INFO,
453 .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
454 .idProduct = 0x7007, /* Product ID of GO7007SB chip */
455 .bcdDevice_lo = 0x200, /* Revision number of XMen */
456 .bcdDevice_hi = 0x200,
457 .bInterfaceClass = 255,
458 .bInterfaceSubClass = 0,
459 .bInterfaceProtocol = 255,
460 .driver_info = (kernel_ulong_t)GO7007_BOARDID_XMEN,
463 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
464 .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
465 .idProduct = 0x7007, /* Product ID of GO7007SB chip */
466 .bcdDevice_lo = 0x202, /* Revision number of Matrix II */
467 .bcdDevice_hi = 0x202,
468 .driver_info = (kernel_ulong_t)GO7007_BOARDID_MATRIX_II,
471 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
472 .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
473 .idProduct = 0x7007, /* Product ID of GO7007SB chip */
474 .bcdDevice_lo = 0x204, /* Revision number of Matrix */
475 .bcdDevice_hi = 0x204, /* Reloaded */
476 .driver_info = (kernel_ulong_t)GO7007_BOARDID_MATRIX_RELOAD,
479 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION |
480 USB_DEVICE_ID_MATCH_INT_INFO,
481 .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
482 .idProduct = 0x7007, /* Product ID of GO7007SB chip */
483 .bcdDevice_lo = 0x205, /* Revision number of XMen-II */
484 .bcdDevice_hi = 0x205,
485 .bInterfaceClass = 255,
486 .bInterfaceSubClass = 0,
487 .bInterfaceProtocol = 255,
488 .driver_info = (kernel_ulong_t)GO7007_BOARDID_XMEN_II,
491 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
492 .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
493 .idProduct = 0x7007, /* Product ID of GO7007SB chip */
494 .bcdDevice_lo = 0x208, /* Revision number of Star Trek */
495 .bcdDevice_hi = 0x208,
496 .driver_info = (kernel_ulong_t)GO7007_BOARDID_STAR_TREK,
499 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION |
500 USB_DEVICE_ID_MATCH_INT_INFO,
501 .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
502 .idProduct = 0x7007, /* Product ID of GO7007SB chip */
503 .bcdDevice_lo = 0x209, /* Revision number of XMen-III */
504 .bcdDevice_hi = 0x209,
505 .bInterfaceClass = 255,
506 .bInterfaceSubClass = 0,
507 .bInterfaceProtocol = 255,
508 .driver_info = (kernel_ulong_t)GO7007_BOARDID_XMEN_III,
511 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
512 .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
513 .idProduct = 0x7007, /* Product ID of GO7007SB chip */
514 .bcdDevice_lo = 0x210, /* Revision number of Matrix */
515 .bcdDevice_hi = 0x210, /* Revolution */
516 .driver_info = (kernel_ulong_t)GO7007_BOARDID_MATRIX_REV,
519 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
520 .idVendor = 0x093b, /* Vendor ID of Plextor */
521 .idProduct = 0xa102, /* Product ID of M402U */
522 .bcdDevice_lo = 0x1, /* revision number of Blueberry */
524 .driver_info = (kernel_ulong_t)GO7007_BOARDID_PX_M402U,
527 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
528 .idVendor = 0x093b, /* Vendor ID of Plextor */
529 .idProduct = 0xa104, /* Product ID of TV402U */
532 .driver_info = (kernel_ulong_t)GO7007_BOARDID_PX_TV402U_ANY,
535 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
536 .idVendor = 0x10fd, /* Vendor ID of Anubis Electronics */
537 .idProduct = 0xde00, /* Product ID of Lifeview LR192 */
540 .driver_info = (kernel_ulong_t)GO7007_BOARDID_LIFEVIEW_LR192,
543 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
544 .idVendor = 0x1943, /* Vendor ID Sensoray */
545 .idProduct = 0x2250, /* Product ID of 2250/2251 */
548 .driver_info = (kernel_ulong_t)GO7007_BOARDID_SENSORAY_2250,
550 { } /* Terminating entry */
553 MODULE_DEVICE_TABLE(usb, go7007_usb_id_table);
555 /********************* Driver for EZ-USB HPI interface *********************/
557 static int go7007_usb_vendor_request(struct go7007 *go, int request,
558 int value, int index, void *transfer_buffer, int length, int in)
560 struct go7007_usb *usb = go->hpi_context;
564 return usb_control_msg(usb->usbdev,
565 usb_rcvctrlpipe(usb->usbdev, 0), request,
566 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
567 value, index, transfer_buffer, length, timeout);
569 return usb_control_msg(usb->usbdev,
570 usb_sndctrlpipe(usb->usbdev, 0), request,
571 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
572 value, index, transfer_buffer, length, timeout);
576 static int go7007_usb_interface_reset(struct go7007 *go)
578 struct go7007_usb *usb = go->hpi_context;
579 u16 intr_val, intr_data;
582 if (go7007_write_interrupt(go, 0x0001, 0x0001) < 0)
586 if (usb->board->flags & GO7007_USB_EZUSB) {
587 /* Reset buffer in EZ-USB */
588 #ifdef GO7007_USB_DEBUG
589 printk(KERN_DEBUG "go7007-usb: resetting EZ-USB buffers\n");
591 if (go7007_usb_vendor_request(go, 0x10, 0, 0, NULL, 0, 0) < 0 ||
592 go7007_usb_vendor_request(go, 0x10, 0, 0, NULL, 0, 0) < 0)
595 /* Reset encoder again */
596 if (go7007_write_interrupt(go, 0x0001, 0x0001) < 0)
601 /* Wait for an interrupt to indicate successful hardware reset */
602 if (go7007_read_interrupt(go, &intr_val, &intr_data) < 0 ||
603 (intr_val & ~0x1) != 0x55aa) {
605 "go7007-usb: unable to reset the USB interface\n");
611 static int go7007_usb_ezusb_write_interrupt(struct go7007 *go,
614 struct go7007_usb *usb = go->hpi_context;
619 #ifdef GO7007_USB_DEBUG
621 "go7007-usb: WriteInterrupt: %04x %04x\n", addr, data);
624 for (i = 0; i < 100; ++i) {
625 r = usb_control_msg(usb->usbdev,
626 usb_rcvctrlpipe(usb->usbdev, 0), 0x14,
627 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
628 0, HPI_STATUS_ADDR, &status_reg,
629 sizeof(status_reg), timeout);
631 goto write_int_error;
632 __le16_to_cpus(&status_reg);
633 if (!(status_reg & 0x0010))
639 "go7007-usb: device is hung, status reg = 0x%04x\n",
643 r = usb_control_msg(usb->usbdev, usb_sndctrlpipe(usb->usbdev, 0), 0x12,
644 USB_TYPE_VENDOR | USB_RECIP_DEVICE, data,
645 INT_PARAM_ADDR, NULL, 0, timeout);
647 goto write_int_error;
648 r = usb_control_msg(usb->usbdev, usb_sndctrlpipe(usb->usbdev, 0),
649 0x12, USB_TYPE_VENDOR | USB_RECIP_DEVICE, addr,
650 INT_INDEX_ADDR, NULL, 0, timeout);
652 goto write_int_error;
656 printk(KERN_ERR "go7007-usb: error in WriteInterrupt: %d\n", r);
660 static int go7007_usb_onboard_write_interrupt(struct go7007 *go,
663 struct go7007_usb *usb = go->hpi_context;
668 #ifdef GO7007_USB_DEBUG
670 "go7007-usb: WriteInterrupt: %04x %04x\n", addr, data);
673 tbuf = kmalloc(8, GFP_KERNEL);
677 tbuf[0] = data & 0xff;
679 tbuf[2] = addr & 0xff;
681 r = usb_control_msg(usb->usbdev, usb_sndctrlpipe(usb->usbdev, 2), 0x00,
682 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT, 0x55aa,
683 0xf0f0, tbuf, 8, timeout);
686 printk(KERN_ERR "go7007-usb: error in WriteInterrupt: %d\n", r);
692 static void go7007_usb_readinterrupt_complete(struct urb *urb)
694 struct go7007 *go = (struct go7007 *)urb->context;
695 u16 *regs = (u16 *)urb->transfer_buffer;
696 int status = urb->status;
699 if (status != -ESHUTDOWN &&
700 go->status != STATUS_SHUTDOWN) {
702 "go7007-usb: error in read interrupt: %d\n",
705 wake_up(&go->interrupt_waitq);
708 } else if (urb->actual_length != urb->transfer_buffer_length) {
709 printk(KERN_ERR "go7007-usb: short read in interrupt pipe!\n");
711 go->interrupt_available = 1;
712 go->interrupt_data = __le16_to_cpu(regs[0]);
713 go->interrupt_value = __le16_to_cpu(regs[1]);
714 #ifdef GO7007_USB_DEBUG
715 printk(KERN_DEBUG "go7007-usb: ReadInterrupt: %04x %04x\n",
716 go->interrupt_value, go->interrupt_data);
720 wake_up(&go->interrupt_waitq);
723 static int go7007_usb_read_interrupt(struct go7007 *go)
725 struct go7007_usb *usb = go->hpi_context;
728 r = usb_submit_urb(usb->intr_urb, GFP_KERNEL);
731 "go7007-usb: unable to submit interrupt urb: %d\n", r);
737 static void go7007_usb_read_video_pipe_complete(struct urb *urb)
739 struct go7007 *go = (struct go7007 *)urb->context;
740 int r, status = urb->status;
742 if (!go->streaming) {
743 wake_up_interruptible(&go->frame_waitq);
747 printk(KERN_ERR "go7007-usb: error in video pipe: %d\n",
751 if (urb->actual_length != urb->transfer_buffer_length) {
752 printk(KERN_ERR "go7007-usb: short read in video pipe!\n");
755 go7007_parse_video_stream(go, urb->transfer_buffer, urb->actual_length);
756 r = usb_submit_urb(urb, GFP_ATOMIC);
758 printk(KERN_ERR "go7007-usb: error in video pipe: %d\n", r);
761 static void go7007_usb_read_audio_pipe_complete(struct urb *urb)
763 struct go7007 *go = (struct go7007 *)urb->context;
764 int r, status = urb->status;
769 printk(KERN_ERR "go7007-usb: error in audio pipe: %d\n",
773 if (urb->actual_length != urb->transfer_buffer_length) {
774 printk(KERN_ERR "go7007-usb: short read in audio pipe!\n");
777 if (go->audio_deliver != NULL)
778 go->audio_deliver(go, urb->transfer_buffer, urb->actual_length);
779 r = usb_submit_urb(urb, GFP_ATOMIC);
781 printk(KERN_ERR "go7007-usb: error in audio pipe: %d\n", r);
784 static int go7007_usb_stream_start(struct go7007 *go)
786 struct go7007_usb *usb = go->hpi_context;
789 for (i = 0; i < 8; ++i) {
790 r = usb_submit_urb(usb->video_urbs[i], GFP_KERNEL);
792 printk(KERN_ERR "go7007-usb: error submitting video "
793 "urb %d: %d\n", i, r);
794 goto video_submit_failed;
797 if (!go->audio_enabled)
800 for (i = 0; i < 8; ++i) {
801 r = usb_submit_urb(usb->audio_urbs[i], GFP_KERNEL);
803 printk(KERN_ERR "go7007-usb: error submitting audio "
804 "urb %d: %d\n", i, r);
805 goto audio_submit_failed;
811 for (i = 0; i < 7; ++i)
812 usb_kill_urb(usb->audio_urbs[i]);
814 for (i = 0; i < 8; ++i)
815 usb_kill_urb(usb->video_urbs[i]);
819 static int go7007_usb_stream_stop(struct go7007 *go)
821 struct go7007_usb *usb = go->hpi_context;
824 if (go->status == STATUS_SHUTDOWN)
826 for (i = 0; i < 8; ++i)
827 usb_kill_urb(usb->video_urbs[i]);
828 if (go->audio_enabled)
829 for (i = 0; i < 8; ++i)
830 usb_kill_urb(usb->audio_urbs[i]);
834 static int go7007_usb_send_firmware(struct go7007 *go, u8 *data, int len)
836 struct go7007_usb *usb = go->hpi_context;
837 int transferred, pipe;
840 #ifdef GO7007_USB_DEBUG
841 printk(KERN_DEBUG "go7007-usb: DownloadBuffer sending %d bytes\n", len);
844 if (usb->board->flags & GO7007_USB_EZUSB)
845 pipe = usb_sndbulkpipe(usb->usbdev, 2);
847 pipe = usb_sndbulkpipe(usb->usbdev, 3);
849 return usb_bulk_msg(usb->usbdev, pipe, data, len,
850 &transferred, timeout);
853 static struct go7007_hpi_ops go7007_usb_ezusb_hpi_ops = {
854 .interface_reset = go7007_usb_interface_reset,
855 .write_interrupt = go7007_usb_ezusb_write_interrupt,
856 .read_interrupt = go7007_usb_read_interrupt,
857 .stream_start = go7007_usb_stream_start,
858 .stream_stop = go7007_usb_stream_stop,
859 .send_firmware = go7007_usb_send_firmware,
862 static struct go7007_hpi_ops go7007_usb_onboard_hpi_ops = {
863 .interface_reset = go7007_usb_interface_reset,
864 .write_interrupt = go7007_usb_onboard_write_interrupt,
865 .read_interrupt = go7007_usb_read_interrupt,
866 .stream_start = go7007_usb_stream_start,
867 .stream_stop = go7007_usb_stream_stop,
868 .send_firmware = go7007_usb_send_firmware,
871 /********************* Driver for EZ-USB I2C adapter *********************/
873 static int go7007_usb_i2c_master_xfer(struct i2c_adapter *adapter,
874 struct i2c_msg msgs[], int num)
876 struct go7007 *go = i2c_get_adapdata(adapter);
877 struct go7007_usb *usb = go->hpi_context;
882 if (go->status == STATUS_SHUTDOWN)
885 mutex_lock(&usb->i2c_lock);
887 for (i = 0; i < num; ++i) {
888 /* The hardware command is "write some bytes then read some
889 * bytes", so we try to coalesce a write followed by a read
890 * into a single USB transaction */
891 if (i + 1 < num && msgs[i].addr == msgs[i + 1].addr &&
892 !(msgs[i].flags & I2C_M_RD) &&
893 (msgs[i + 1].flags & I2C_M_RD)) {
894 #ifdef GO7007_I2C_DEBUG
895 printk(KERN_DEBUG "go7007-usb: i2c write/read %d/%d "
896 "bytes on %02x\n", msgs[i].len,
897 msgs[i + 1].len, msgs[i].addr);
900 buf[1] = msgs[i].len + 1;
901 buf[2] = msgs[i].addr << 1;
902 memcpy(&buf[3], msgs[i].buf, msgs[i].len);
903 buf_len = msgs[i].len + 3;
904 buf[buf_len++] = msgs[++i].len;
905 } else if (msgs[i].flags & I2C_M_RD) {
906 #ifdef GO7007_I2C_DEBUG
907 printk(KERN_DEBUG "go7007-usb: i2c read %d "
908 "bytes on %02x\n", msgs[i].len,
913 buf[2] = msgs[i].addr << 1;
914 buf[3] = msgs[i].len;
917 #ifdef GO7007_I2C_DEBUG
918 printk(KERN_DEBUG "go7007-usb: i2c write %d "
919 "bytes on %02x\n", msgs[i].len,
923 buf[1] = msgs[i].len + 1;
924 buf[2] = msgs[i].addr << 1;
925 memcpy(&buf[3], msgs[i].buf, msgs[i].len);
926 buf_len = msgs[i].len + 3;
929 if (go7007_usb_vendor_request(go, 0x24, 0, 0,
930 buf, buf_len, 0) < 0)
932 if (msgs[i].flags & I2C_M_RD) {
933 memset(buf, 0, sizeof(buf));
934 if (go7007_usb_vendor_request(go, 0x25, 0, 0, buf,
935 msgs[i].len + 1, 1) < 0)
937 memcpy(msgs[i].buf, buf + 1, msgs[i].len);
943 mutex_unlock(&usb->i2c_lock);
947 static u32 go7007_usb_functionality(struct i2c_adapter *adapter)
949 /* No errors are reported by the hardware, so we don't bother
950 * supporting quick writes to avoid confusing probing */
951 return (I2C_FUNC_SMBUS_EMUL) & ~I2C_FUNC_SMBUS_QUICK;
954 static struct i2c_algorithm go7007_usb_algo = {
955 .master_xfer = go7007_usb_i2c_master_xfer,
956 .functionality = go7007_usb_functionality,
959 static struct i2c_adapter go7007_usb_adap_templ = {
960 .owner = THIS_MODULE,
961 .name = "WIS GO7007SB EZ-USB",
962 .algo = &go7007_usb_algo,
965 /********************* USB add/remove functions *********************/
967 static int go7007_usb_probe(struct usb_interface *intf,
968 const struct usb_device_id *id)
971 struct go7007_usb *usb;
972 struct go7007_usb_board *board;
973 struct usb_device *usbdev = interface_to_usbdev(intf);
975 int video_pipe, i, v_urb_len;
977 printk(KERN_DEBUG "go7007-usb: probing new GO7007 USB board\n");
979 switch (id->driver_info) {
980 case GO7007_BOARDID_MATRIX_II:
981 name = "WIS Matrix II or compatible";
982 board = &board_matrix_ii;
984 case GO7007_BOARDID_MATRIX_RELOAD:
985 name = "WIS Matrix Reloaded or compatible";
986 board = &board_matrix_reload;
988 case GO7007_BOARDID_MATRIX_REV:
989 name = "WIS Matrix Revolution or compatible";
990 board = &board_matrix_revolution;
992 case GO7007_BOARDID_STAR_TREK:
993 name = "WIS Star Trek or compatible";
994 board = &board_star_trek;
996 case GO7007_BOARDID_XMEN:
997 name = "WIS XMen or compatible";
1000 case GO7007_BOARDID_XMEN_II:
1001 name = "WIS XMen II or compatible";
1002 board = &board_xmen;
1004 case GO7007_BOARDID_XMEN_III:
1005 name = "WIS XMen III or compatible";
1006 board = &board_xmen;
1008 case GO7007_BOARDID_PX_M402U:
1009 name = "Plextor PX-M402U";
1010 board = &board_matrix_ii;
1012 case GO7007_BOARDID_PX_TV402U_ANY:
1013 name = "Plextor PX-TV402U (unknown tuner)";
1014 board = &board_px_tv402u;
1016 case GO7007_BOARDID_LIFEVIEW_LR192:
1017 printk(KERN_ERR "go7007-usb: The Lifeview TV Walker Ultra "
1018 "is not supported. Sorry!\n");
1020 name = "Lifeview TV Walker Ultra";
1021 board = &board_lifeview_lr192;
1023 case GO7007_BOARDID_SENSORAY_2250:
1024 printk(KERN_INFO "Sensoray 2250 found\n");
1025 name = "Sensoray 2250/2251";
1026 board = &board_sensoray_2250;
1029 printk(KERN_ERR "go7007-usb: unknown board ID %d!\n",
1030 (unsigned int)id->driver_info);
1034 usb = kzalloc(sizeof(struct go7007_usb), GFP_KERNEL);
1038 /* Allocate the URB and buffer for receiving incoming interrupts */
1039 usb->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1040 if (usb->intr_urb == NULL)
1042 usb->intr_urb->transfer_buffer = kmalloc(2*sizeof(u16), GFP_KERNEL);
1043 if (usb->intr_urb->transfer_buffer == NULL)
1046 go = go7007_alloc(&board->main_info, &intf->dev);
1050 usb->usbdev = usbdev;
1051 go->board_id = id->driver_info;
1052 strncpy(go->name, name, sizeof(go->name));
1053 if (board->flags & GO7007_USB_EZUSB)
1054 go->hpi_ops = &go7007_usb_ezusb_hpi_ops;
1056 go->hpi_ops = &go7007_usb_onboard_hpi_ops;
1057 go->hpi_context = usb;
1058 usb_fill_int_urb(usb->intr_urb, usb->usbdev,
1059 usb_rcvintpipe(usb->usbdev, 4),
1060 usb->intr_urb->transfer_buffer, 2*sizeof(u16),
1061 go7007_usb_readinterrupt_complete, go, 8);
1062 usb_set_intfdata(intf, &go->v4l2_dev);
1064 /* Boot the GO7007 */
1065 if (go7007_boot_encoder(go, go->board_info->flags &
1066 GO7007_BOARD_USE_ONBOARD_I2C) < 0)
1069 /* Register the EZ-USB I2C adapter, if we're using it */
1070 if (board->flags & GO7007_USB_EZUSB_I2C) {
1071 memcpy(&go->i2c_adapter, &go7007_usb_adap_templ,
1072 sizeof(go7007_usb_adap_templ));
1073 mutex_init(&usb->i2c_lock);
1074 go->i2c_adapter.dev.parent = go->dev;
1075 i2c_set_adapdata(&go->i2c_adapter, go);
1076 if (i2c_add_adapter(&go->i2c_adapter) < 0) {
1078 "go7007-usb: error: i2c_add_adapter failed\n");
1081 go->i2c_adapter_online = 1;
1084 /* Pelco and Adlink reused the XMen and XMen-III vendor and product
1085 * IDs for their own incompatible designs. We can detect XMen boards
1086 * by probing the sensor, but there is no way to probe the sensors on
1087 * the Pelco and Adlink designs so we default to the Adlink. If it
1088 * is actually a Pelco, the user must set the assume_endura module
1090 if ((go->board_id == GO7007_BOARDID_XMEN ||
1091 go->board_id == GO7007_BOARDID_XMEN_III) &&
1092 go->i2c_adapter_online) {
1093 union i2c_smbus_data data;
1095 /* Check to see if register 0x0A is 0x76 */
1096 i2c_smbus_xfer(&go->i2c_adapter, 0x21, I2C_CLIENT_SCCB,
1097 I2C_SMBUS_READ, 0x0A, I2C_SMBUS_BYTE_DATA, &data);
1098 if (data.byte != 0x76) {
1099 if (assume_endura) {
1100 go->board_id = GO7007_BOARDID_ENDURA;
1101 usb->board = board = &board_endura;
1102 go->board_info = &board->main_info;
1103 strncpy(go->name, "Pelco Endura",
1108 /* set GPIO5 to be an output, currently low */
1109 go7007_write_addr(go, 0x3c82, 0x0000);
1110 go7007_write_addr(go, 0x3c80, 0x00df);
1111 /* read channel number from GPIO[1:0] */
1112 go7007_read_addr(go, 0x3c81, &channel);
1114 go->board_id = GO7007_BOARDID_ADLINK_MPG24;
1115 usb->board = board = &board_adlink_mpg24;
1116 go->board_info = &board->main_info;
1117 go->channel_number = channel;
1118 snprintf(go->name, sizeof(go->name),
1119 "Adlink PCI-MPG24, channel #%d",
1125 /* Probe the tuner model on the TV402U */
1126 if (go->board_id == GO7007_BOARDID_PX_TV402U_ANY) {
1129 /* Board strapping indicates tuner model */
1130 if (go7007_usb_vendor_request(go, 0x41, 0, 0, data, 3, 1) < 0) {
1131 printk(KERN_ERR "go7007-usb: GPIO read failed!\n");
1134 switch (data[0] >> 6) {
1136 go->board_id = GO7007_BOARDID_PX_TV402U_EU;
1137 go->tuner_type = TUNER_SONY_BTF_PG472Z;
1138 strncpy(go->name, "Plextor PX-TV402U-EU",
1142 go->board_id = GO7007_BOARDID_PX_TV402U_JP;
1143 go->tuner_type = TUNER_SONY_BTF_PK467Z;
1144 strncpy(go->name, "Plextor PX-TV402U-JP",
1148 go->board_id = GO7007_BOARDID_PX_TV402U_NA;
1149 go->tuner_type = TUNER_SONY_BTF_PB463Z;
1150 strncpy(go->name, "Plextor PX-TV402U-NA",
1154 printk(KERN_DEBUG "go7007-usb: unable to detect "
1158 /* Configure tuner mode selection inputs connected
1159 * to the EZ-USB GPIO output pins */
1160 if (go7007_usb_vendor_request(go, 0x40, 0x7f02, 0,
1162 printk(KERN_ERR "go7007-usb: GPIO write failed!\n");
1167 /* Print a nasty message if the user attempts to use a USB2.0 device in
1168 * a USB1.1 port. There will be silent corruption of the stream. */
1169 if ((board->flags & GO7007_USB_EZUSB) &&
1170 usbdev->speed != USB_SPEED_HIGH)
1171 printk(KERN_ERR "go7007-usb: *** WARNING *** This device "
1172 "must be connected to a USB 2.0 port! "
1173 "Attempting to capture video through a USB 1.1 "
1174 "port will result in stream corruption, even "
1175 "at low bitrates!\n");
1177 /* Do any final GO7007 initialization, then register the
1178 * V4L2 and ALSA interfaces */
1179 if (go7007_register_encoder(go) < 0)
1182 /* Allocate the URBs and buffers for receiving the video stream */
1183 if (board->flags & GO7007_USB_EZUSB) {
1185 video_pipe = usb_rcvbulkpipe(usb->usbdev, 6);
1188 video_pipe = usb_rcvbulkpipe(usb->usbdev, 1);
1190 for (i = 0; i < 8; ++i) {
1191 usb->video_urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1192 if (usb->video_urbs[i] == NULL)
1194 usb->video_urbs[i]->transfer_buffer =
1195 kmalloc(v_urb_len, GFP_KERNEL);
1196 if (usb->video_urbs[i]->transfer_buffer == NULL)
1198 usb_fill_bulk_urb(usb->video_urbs[i], usb->usbdev, video_pipe,
1199 usb->video_urbs[i]->transfer_buffer, v_urb_len,
1200 go7007_usb_read_video_pipe_complete, go);
1203 /* Allocate the URBs and buffers for receiving the audio stream */
1204 if ((board->flags & GO7007_USB_EZUSB) && go->audio_enabled)
1205 for (i = 0; i < 8; ++i) {
1206 usb->audio_urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1207 if (usb->audio_urbs[i] == NULL)
1209 usb->audio_urbs[i]->transfer_buffer = kmalloc(4096,
1211 if (usb->audio_urbs[i]->transfer_buffer == NULL)
1213 usb_fill_bulk_urb(usb->audio_urbs[i], usb->usbdev,
1214 usb_rcvbulkpipe(usb->usbdev, 8),
1215 usb->audio_urbs[i]->transfer_buffer, 4096,
1216 go7007_usb_read_audio_pipe_complete, go);
1220 go->status = STATUS_ONLINE;
1224 go->status = STATUS_SHUTDOWN;
1228 if (usb->intr_urb) {
1229 kfree(usb->intr_urb->transfer_buffer);
1230 usb_free_urb(usb->intr_urb);
1236 static void go7007_usb_disconnect(struct usb_interface *intf)
1238 struct go7007 *go = to_go7007(usb_get_intfdata(intf));
1239 struct go7007_usb *usb = go->hpi_context;
1240 struct urb *vurb, *aurb;
1243 go->status = STATUS_SHUTDOWN;
1244 usb_kill_urb(usb->intr_urb);
1246 /* Free USB-related structs */
1247 for (i = 0; i < 8; ++i) {
1248 vurb = usb->video_urbs[i];
1251 if (vurb->transfer_buffer)
1252 kfree(vurb->transfer_buffer);
1255 aurb = usb->audio_urbs[i];
1258 if (aurb->transfer_buffer)
1259 kfree(aurb->transfer_buffer);
1263 kfree(usb->intr_urb->transfer_buffer);
1264 usb_free_urb(usb->intr_urb);
1266 kfree(go->hpi_context);
1271 static struct usb_driver go7007_usb_driver = {
1273 .probe = go7007_usb_probe,
1274 .disconnect = go7007_usb_disconnect,
1275 .id_table = go7007_usb_id_table,
1278 static int __init go7007_usb_init(void)
1280 return usb_register(&go7007_usb_driver);
1283 static void __exit go7007_usb_cleanup(void)
1285 usb_deregister(&go7007_usb_driver);
1288 module_init(go7007_usb_init);
1289 module_exit(go7007_usb_cleanup);
1291 MODULE_LICENSE("GPL v2");