diff options
Diffstat (limited to 'drivers/usb/serial/io_ti.c')
-rw-r--r-- | drivers/usb/serial/io_ti.c | 3091 |
1 files changed, 3091 insertions, 0 deletions
diff --git a/drivers/usb/serial/io_ti.c b/drivers/usb/serial/io_ti.c new file mode 100644 index 00000000000..6c96fdaec36 --- /dev/null +++ b/drivers/usb/serial/io_ti.c @@ -0,0 +1,3091 @@ +/* + * Edgeport USB Serial Converter driver + * + * Copyright (C) 2000-2002 Inside Out Networks, All rights reserved. + * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Supports the following devices: + * EP/1 EP/2 EP/4 EP/21 EP/22 EP/221 EP/42 EP/421 WATCHPORT + * + * For questions or problems with this driver, contact Inside Out + * Networks technical support, or Peter Berger <pberger@brimson.com>, + * or Al Borchers <alborchers@steinerpoint.com>. + * + * Version history: + * + * July 11, 2002 Removed 4 port device structure since all TI UMP + * chips have only 2 ports + * David Iacovelli (davidi@ionetworks.com) + * + */ + +#include <linux/config.h> +#include <linux/kernel.h> +#include <linux/jiffies.h> +#include <linux/errno.h> +#include <linux/init.h> +#include <linux/slab.h> +#include <linux/tty.h> +#include <linux/tty_driver.h> +#include <linux/tty_flip.h> +#include <linux/module.h> +#include <linux/spinlock.h> +#include <linux/serial.h> +#include <linux/ioctl.h> +#include <asm/uaccess.h> +#include <asm/semaphore.h> +#include <linux/usb.h> + +#include "usb-serial.h" +#include "io_16654.h" +#include "io_usbvend.h" +#include "io_ti.h" + +/* + * Version Information + */ +#define DRIVER_VERSION "v0.7" +#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli" +#define DRIVER_DESC "Edgeport USB Serial Driver" + + +/* firmware image code */ +#define IMAGE_VERSION_NAME PagableOperationalCodeImageVersion +#define IMAGE_ARRAY_NAME PagableOperationalCodeImage +#define IMAGE_SIZE PagableOperationalCodeSize +#include "io_fw_down3.h" /* Define array OperationalCodeImage[] */ + +#define EPROM_PAGE_SIZE 64 + + +struct edgeport_uart_buf_desc { + __u32 count; // Number of bytes currently in buffer +}; + +/* different hardware types */ +#define HARDWARE_TYPE_930 0 +#define HARDWARE_TYPE_TIUMP 1 + +// IOCTL_PRIVATE_TI_GET_MODE Definitions +#define TI_MODE_CONFIGURING 0 // Device has not entered start device +#define TI_MODE_BOOT 1 // Staying in boot mode +#define TI_MODE_DOWNLOAD 2 // Made it to download mode +#define TI_MODE_TRANSITIONING 3 // Currently in boot mode but transitioning to download mode + +/* read urb state */ +#define EDGE_READ_URB_RUNNING 0 +#define EDGE_READ_URB_STOPPING 1 +#define EDGE_READ_URB_STOPPED 2 + +#define EDGE_LOW_LATENCY 1 +#define EDGE_CLOSING_WAIT 4000 /* in .01 sec */ + +#define EDGE_OUT_BUF_SIZE 1024 + + +/* Product information read from the Edgeport */ +struct product_info +{ + int TiMode; // Current TI Mode + __u8 hardware_type; // Type of hardware +} __attribute__((packed)); + +/* circular buffer */ +struct edge_buf { + unsigned int buf_size; + char *buf_buf; + char *buf_get; + char *buf_put; +}; + +struct edgeport_port { + __u16 uart_base; + __u16 dma_address; + __u8 shadow_msr; + __u8 shadow_mcr; + __u8 shadow_lsr; + __u8 lsr_mask; + __u32 ump_read_timeout; /* Number of miliseconds the UMP will + wait without data before completing + a read short */ + int baud_rate; + int close_pending; + int lsr_event; + struct edgeport_uart_buf_desc tx; + struct async_icount icount; + wait_queue_head_t delta_msr_wait; /* for handling sleeping while + waiting for msr change to + happen */ + struct edgeport_serial *edge_serial; + struct usb_serial_port *port; + __u8 bUartMode; /* Port type, 0: RS232, etc. */ + spinlock_t ep_lock; + int ep_read_urb_state; + int ep_write_urb_in_use; + struct edge_buf *ep_out_buf; +}; + +struct edgeport_serial { + struct product_info product_info; + u8 TI_I2C_Type; // Type of I2C in UMP + u8 TiReadI2C; // Set to TRUE if we have read the I2c in Boot Mode + struct semaphore es_sem; + int num_ports_open; + struct usb_serial *serial; +}; + + +/* Devices that this driver supports */ +static struct usb_device_id edgeport_1port_id_table [] = { + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) }, + { } +}; + +static struct usb_device_id edgeport_2port_id_table [] = { + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) }, +// The 4-port shows up as two 2-port devices + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) }, + { } +}; + +/* Devices that this driver supports */ +static struct usb_device_id id_table_combined [] = { + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) }, + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) }, + { } +}; + +MODULE_DEVICE_TABLE (usb, id_table_combined); + +static struct usb_driver io_driver = { + .owner = THIS_MODULE, + .name = "io_ti", + .probe = usb_serial_probe, + .disconnect = usb_serial_disconnect, + .id_table = id_table_combined, +}; + + +static struct EDGE_FIRMWARE_VERSION_INFO OperationalCodeImageVersion; + +static int debug; + +static int TIStayInBootMode = 0; +static int low_latency = EDGE_LOW_LATENCY; +static int closing_wait = EDGE_CLOSING_WAIT; +static int ignore_cpu_rev = 0; + + +static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length); + +static void stop_read(struct edgeport_port *edge_port); +static int restart_read(struct edgeport_port *edge_port); + +static void edge_set_termios (struct usb_serial_port *port, struct termios *old_termios); +static void edge_send(struct usb_serial_port *port); + +/* circular buffer */ +static struct edge_buf *edge_buf_alloc(unsigned int size); +static void edge_buf_free(struct edge_buf *eb); +static void edge_buf_clear(struct edge_buf *eb); +static unsigned int edge_buf_data_avail(struct edge_buf *eb); +static unsigned int edge_buf_space_avail(struct edge_buf *eb); +static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf, + unsigned int count); +static unsigned int edge_buf_get(struct edge_buf *eb, char *buf, + unsigned int count); + + +static int TIReadVendorRequestSync (struct usb_device *dev, + __u8 request, + __u16 value, + __u16 index, + u8 *data, + int size) +{ + int status; + + status = usb_control_msg (dev, + usb_rcvctrlpipe(dev, 0), + request, + (USB_TYPE_VENDOR | + USB_RECIP_DEVICE | + USB_DIR_IN), + value, + index, + data, + size, + 1000); + if (status < 0) + return status; + if (status != size) { + dbg ("%s - wanted to write %d, but only wrote %d", + __FUNCTION__, size, status); + return -ECOMM; + } + return 0; +} + +static int TISendVendorRequestSync (struct usb_device *dev, + __u8 request, + __u16 value, + __u16 index, + u8 *data, + int size) +{ + int status; + + status = usb_control_msg (dev, + usb_sndctrlpipe(dev, 0), + request, + (USB_TYPE_VENDOR | + USB_RECIP_DEVICE | + USB_DIR_OUT), + value, + index, + data, + size, + 1000); + if (status < 0) + return status; + if (status != size) { + dbg ("%s - wanted to write %d, but only wrote %d", + __FUNCTION__, size, status); + return -ECOMM; + } + return 0; +} + +static int TIWriteCommandSync (struct usb_device *dev, __u8 command, + __u8 moduleid, __u16 value, u8 *data, + int size) +{ + return TISendVendorRequestSync (dev, + command, // Request + value, // wValue + moduleid, // wIndex + data, // TransferBuffer + size); // TransferBufferLength + +} + +/* clear tx/rx buffers and fifo in TI UMP */ +static int TIPurgeDataSync (struct usb_serial_port *port, __u16 mask) +{ + int port_number = port->number - port->serial->minor; + + dbg ("%s - port %d, mask %x", __FUNCTION__, port_number, mask); + + return TIWriteCommandSync (port->serial->dev, + UMPC_PURGE_PORT, + (__u8)(UMPM_UART1_PORT + port_number), + mask, + NULL, + 0); +} + +/** + * TIReadDownloadMemory - Read edgeport memory from TI chip + * @dev: usb device pointer + * @start_address: Device CPU address at which to read + * @length: Length of above data + * @address_type: Can read both XDATA and I2C + * @buffer: pointer to input data buffer + */ +static int TIReadDownloadMemory(struct usb_device *dev, int start_address, + int length, __u8 address_type, __u8 *buffer) +{ + int status = 0; + __u8 read_length; + __be16 be_start_address; + + dbg ("%s - @ %x for %d", __FUNCTION__, start_address, length); + + /* Read in blocks of 64 bytes + * (TI firmware can't handle more than 64 byte reads) + */ + while (length) { + if (length > 64) + read_length= 64; + else + read_length = (__u8)length; + + if (read_length > 1) { + dbg ("%s - @ %x for %d", __FUNCTION__, + start_address, read_length); + } + be_start_address = cpu_to_be16 (start_address); + status = TIReadVendorRequestSync (dev, + UMPC_MEMORY_READ, // Request + (__u16)address_type, // wValue (Address type) + (__force __u16)be_start_address, // wIndex (Address to read) + buffer, // TransferBuffer + read_length); // TransferBufferLength + + if (status) { + dbg ("%s - ERROR %x", __FUNCTION__, status); + return status; + } + + if (read_length > 1) { + usb_serial_debug_data(debug, &dev->dev, __FUNCTION__, + read_length, buffer); + } + + /* Update pointers/length */ + start_address += read_length; + buffer += read_length; + length -= read_length; + } + + return status; +} + +static int TIReadRam (struct usb_device *dev, int start_address, int length, __u8 *buffer) +{ + return TIReadDownloadMemory (dev, + start_address, + length, + DTK_ADDR_SPACE_XDATA, + buffer); +} + +/* Read edgeport memory to a given block */ +static int TIReadBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 * buffer) +{ + int status = 0; + int i; + + for (i=0; i< length; i++) { + status = TIReadVendorRequestSync (serial->serial->dev, + UMPC_MEMORY_READ, // Request + serial->TI_I2C_Type, // wValue (Address type) + (__u16)(start_address+i), // wIndex + &buffer[i], // TransferBuffer + 0x01); // TransferBufferLength + if (status) { + dbg ("%s - ERROR %x", __FUNCTION__, status); + return status; + } + } + + dbg ("%s - start_address = %x, length = %d", __FUNCTION__, start_address, length); + usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, length, buffer); + + serial->TiReadI2C = 1; + + return status; +} + +/* Write given block to TI EPROM memory */ +static int TIWriteBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer) +{ + int status = 0; + int i; + __u8 temp; + + /* Must do a read before write */ + if (!serial->TiReadI2C) { + status = TIReadBootMemory(serial, 0, 1, &temp); + if (status) + return status; + } + + for (i=0; i < length; ++i) { + status = TISendVendorRequestSync (serial->serial->dev, + UMPC_MEMORY_WRITE, // Request + buffer[i], // wValue + (__u16)(i+start_address), // wIndex + NULL, // TransferBuffer + 0); // TransferBufferLength + if (status) + return status; + } + + dbg ("%s - start_sddr = %x, length = %d", __FUNCTION__, start_address, length); + usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, length, buffer); + + return status; +} + + +/* Write edgeport I2C memory to TI chip */ +static int TIWriteDownloadI2C (struct edgeport_serial *serial, int start_address, int length, __u8 address_type, __u8 *buffer) +{ + int status = 0; + int write_length; + __be16 be_start_address; + + /* We can only send a maximum of 1 aligned byte page at a time */ + + /* calulate the number of bytes left in the first page */ + write_length = EPROM_PAGE_SIZE - (start_address & (EPROM_PAGE_SIZE - 1)); + + if (write_length > length) + write_length = length; + + dbg ("%s - BytesInFirstPage Addr = %x, length = %d", __FUNCTION__, start_address, write_length); + usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, write_length, buffer); + + /* Write first page */ + be_start_address = cpu_to_be16 (start_address); + status = TISendVendorRequestSync (serial->serial->dev, + UMPC_MEMORY_WRITE, // Request + (__u16)address_type, // wValue + (__force __u16)be_start_address, // wIndex + buffer, // TransferBuffer + write_length); + if (status) { + dbg ("%s - ERROR %d", __FUNCTION__, status); + return status; + } + + length -= write_length; + start_address += write_length; + buffer += write_length; + + /* We should be aligned now -- can write max page size bytes at a time */ + while (length) { + if (length > EPROM_PAGE_SIZE) + write_length = EPROM_PAGE_SIZE; + else + write_length = length; + + dbg ("%s - Page Write Addr = %x, length = %d", __FUNCTION__, start_address, write_length); + usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, write_length, buffer); + + /* Write next page */ + be_start_address = cpu_to_be16 (start_address); + status = TISendVendorRequestSync (serial->serial->dev, + UMPC_MEMORY_WRITE, // Request + (__u16)address_type, // wValue + (__force __u16)be_start_address, // wIndex + buffer, // TransferBuffer + write_length); // TransferBufferLength + if (status) { + dev_err (&serial->serial->dev->dev, "%s - ERROR %d\n", __FUNCTION__, status); + return status; + } + + length -= write_length; + start_address += write_length; + buffer += write_length; + } + return status; +} + +/* Examine the UMP DMA registers and LSR + * + * Check the MSBit of the X and Y DMA byte count registers. + * A zero in this bit indicates that the TX DMA buffers are empty + * then check the TX Empty bit in the UART. + */ +static int TIIsTxActive (struct edgeport_port *port) +{ + int status; + struct out_endpoint_desc_block *oedb; + __u8 *lsr; + int bytes_left = 0; + + oedb = kmalloc (sizeof (* oedb), GFP_KERNEL); + if (!oedb) { + dev_err (&port->port->dev, "%s - out of memory\n", __FUNCTION__); + return -ENOMEM; + } + + lsr = kmalloc (1, GFP_KERNEL); /* Sigh, that's right, just one byte, + as not all platforms can do DMA + from stack */ + if (!lsr) { + kfree(oedb); + return -ENOMEM; + } + /* Read the DMA Count Registers */ + status = TIReadRam (port->port->serial->dev, + port->dma_address, + sizeof( *oedb), + (void *)oedb); + + if (status) + goto exit_is_tx_active; + + dbg ("%s - XByteCount 0x%X", __FUNCTION__, oedb->XByteCount); + + /* and the LSR */ + status = TIReadRam (port->port->serial->dev, + port->uart_base + UMPMEM_OFFS_UART_LSR, + 1, + lsr); + + if (status) + goto exit_is_tx_active; + dbg ("%s - LSR = 0x%X", __FUNCTION__, *lsr); + + /* If either buffer has data or we are transmitting then return TRUE */ + if ((oedb->XByteCount & 0x80 ) != 0 ) + bytes_left += 64; + + if ((*lsr & UMP_UART_LSR_TX_MASK ) == 0 ) + bytes_left += 1; + + /* We return Not Active if we get any kind of error */ +exit_is_tx_active: + dbg ("%s - return %d", __FUNCTION__, bytes_left ); + + kfree(lsr); + kfree(oedb); + return bytes_left; +} + +static void TIChasePort(struct edgeport_port *port, unsigned long timeout, int flush) +{ + int baud_rate; + struct tty_struct *tty = port->port->tty; + wait_queue_t wait; + unsigned long flags; + + if (!timeout) + timeout = (HZ*EDGE_CLOSING_WAIT)/100; + + /* wait for data to drain from the buffer */ + spin_lock_irqsave(&port->ep_lock, flags); + init_waitqueue_entry(&wait, current); + add_wait_queue(&tty->write_wait, &wait); + for (;;) { + set_current_state(TASK_INTERRUPTIBLE); + if (edge_buf_data_avail(port->ep_out_buf) == 0 + || timeout == 0 || signal_pending(current) + || !usb_get_intfdata(port->port->serial->interface)) /* disconnect */ + break; + spin_unlock_irqrestore(&port->ep_lock, flags); + timeout = schedule_timeout(timeout); + spin_lock_irqsave(&port->ep_lock, flags); + } + set_current_state(TASK_RUNNING); + remove_wait_queue(&tty->write_wait, &wait); + if (flush) + edge_buf_clear(port->ep_out_buf); + spin_unlock_irqrestore(&port->ep_lock, flags); + + /* wait for data to drain from the device */ + timeout += jiffies; + while ((long)(jiffies - timeout) < 0 && !signal_pending(current) + && usb_get_intfdata(port->port->serial->interface)) { /* not disconnected */ + if (!TIIsTxActive(port)) + break; + msleep(10); + } + + /* disconnected */ + if (!usb_get_intfdata(port->port->serial->interface)) + return; + + /* wait one more character time, based on baud rate */ + /* (TIIsTxActive doesn't seem to wait for the last byte) */ + if ((baud_rate=port->baud_rate) == 0) + baud_rate = 50; + msleep(max(1,(10000+baud_rate-1)/baud_rate)); +} + +static int TIChooseConfiguration (struct usb_device *dev) +{ + // There may be multiple configurations on this device, in which case + // we would need to read and parse all of them to find out which one + // we want. However, we just support one config at this point, + // configuration # 1, which is Config Descriptor 0. + + dbg ("%s - Number of Interfaces = %d", __FUNCTION__, dev->config->desc.bNumInterfaces); + dbg ("%s - MAX Power = %d", __FUNCTION__, dev->config->desc.bMaxPower*2); + + if (dev->config->desc.bNumInterfaces != 1) { + dev_err (&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __FUNCTION__); + return -ENODEV; + } + + return 0; +} + +static int TIReadRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer) +{ + int status; + + if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) { + status = TIReadDownloadMemory (serial->serial->dev, + start_address, + length, + serial->TI_I2C_Type, + buffer); + } else { + status = TIReadBootMemory (serial, + start_address, + length, + buffer); + } + + return status; +} + +static int TIWriteRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer) +{ + if (serial->product_info.TiMode == TI_MODE_BOOT) + return TIWriteBootMemory (serial, + start_address, + length, + buffer); + + if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) + return TIWriteDownloadI2C (serial, + start_address, + length, + serial->TI_I2C_Type, + buffer); + + return -EINVAL; +} + + + +/* Read a descriptor header from I2C based on type */ +static int TIGetDescriptorAddress (struct edgeport_serial *serial, int desc_type, struct ti_i2c_desc *rom_desc) +{ + int start_address; + int status; + + /* Search for requested descriptor in I2C */ + start_address = 2; + do { + status = TIReadRom (serial, + start_address, + sizeof(struct ti_i2c_desc), + (__u8 *)rom_desc ); + if (status) + return 0; + + if (rom_desc->Type == desc_type) + return start_address; + + start_address = start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size; + + } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type); + + return 0; +} + +/* Validate descriptor checksum */ +static int ValidChecksum(struct ti_i2c_desc *rom_desc, __u8 *buffer) +{ + __u16 i; + __u8 cs = 0; + + for (i=0; i < rom_desc->Size; i++) { + cs = (__u8)(cs + buffer[i]); + } + if (cs != rom_desc->CheckSum) { + dbg ("%s - Mismatch %x - %x", __FUNCTION__, rom_desc->CheckSum, cs); + return -EINVAL; + } + return 0; +} + +/* Make sure that the I2C image is good */ +static int TiValidateI2cImage (struct edgeport_serial *serial) +{ + struct device *dev = &serial->serial->dev->dev; + int status = 0; + struct ti_i2c_desc *rom_desc; + int start_address = 2; + __u8 *buffer; + __u16 ttype; + + rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL); + if (!rom_desc) { + dev_err (dev, "%s - out of memory\n", __FUNCTION__); + return -ENOMEM; + } + buffer = kmalloc (TI_MAX_I2C_SIZE, GFP_KERNEL); + if (!buffer) { + dev_err (dev, "%s - out of memory when allocating buffer\n", __FUNCTION__); + kfree (rom_desc); + return -ENOMEM; + } + + // Read the first byte (Signature0) must be 0x52 or 0x10 + status = TIReadRom (serial, 0, 1, buffer); + if (status) + goto ExitTiValidateI2cImage; + + if (*buffer != UMP5152 && *buffer != UMP3410) { + dev_err (dev, "%s - invalid buffer signature\n", __FUNCTION__); + status = -ENODEV; + goto ExitTiValidateI2cImage; + } + + do { + // Validate the I2C + status = TIReadRom (serial, + start_address, + sizeof(struct ti_i2c_desc), + (__u8 *)rom_desc); + if (status) + break; + + if ((start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size) > TI_MAX_I2C_SIZE) { + status = -ENODEV; + dbg ("%s - structure too big, erroring out.", __FUNCTION__); + break; + } + + dbg ("%s Type = 0x%x", __FUNCTION__, rom_desc->Type); + + // Skip type 2 record + ttype = rom_desc->Type & 0x0f; + if ( ttype != I2C_DESC_TYPE_FIRMWARE_BASIC + && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO ) { + // Read the descriptor data + status = TIReadRom(serial, + start_address+sizeof(struct ti_i2c_desc), + rom_desc->Size, + buffer); + if (status) + break; + + status = ValidChecksum(rom_desc, buffer); + if (status) + break; + } + start_address = start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size; + + } while ((rom_desc->Type != I2C_DESC_TYPE_ION) && (start_address < TI_MAX_I2C_SIZE)); + + if ((rom_desc->Type != I2C_DESC_TYPE_ION) || (start_address > TI_MAX_I2C_SIZE)) + status = -ENODEV; + +ExitTiValidateI2cImage: + kfree (buffer); + kfree (rom_desc); + return status; +} + +static int TIReadManufDescriptor (struct edgeport_serial *serial, __u8 *buffer) +{ + int status; + int start_address; + struct ti_i2c_desc *rom_desc; + struct edge_ti_manuf_descriptor *desc; + + rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL); + if (!rom_desc) { + dev_err (&serial->serial->dev->dev, "%s - out of memory\n", __FUNCTION__); + return -ENOMEM; + } + start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_ION, rom_desc); + + if (!start_address) { + dbg ("%s - Edge Descriptor not found in I2C", __FUNCTION__); + status = -ENODEV; + goto exit; + } + + // Read the descriptor data + status = TIReadRom (serial, + start_address+sizeof(struct ti_i2c_desc), + rom_desc->Size, + buffer); + if (status) + goto exit; + + status = ValidChecksum(rom_desc, buffer); + + desc = (struct edge_ti_manuf_descriptor *)buffer; + dbg ( "%s - IonConfig 0x%x", __FUNCTION__, desc->IonConfig ); + dbg ( "%s - Version %d", __FUNCTION__, desc->Version ); + dbg ( "%s - Cpu/Board 0x%x", __FUNCTION__, desc->CpuRev_BoardRev ); + dbg ( "%s - NumPorts %d", __FUNCTION__, desc->NumPorts ); + dbg ( "%s - NumVirtualPorts %d", __FUNCTION__, desc->NumVirtualPorts ); + dbg ( "%s - TotalPorts %d", __FUNCTION__, desc->TotalPorts ); + +exit: + kfree (rom_desc); + return status; +} + +/* Build firmware header used for firmware update */ +static int BuildI2CFirmwareHeader (__u8 *header, struct device *dev) +{ + __u8 *buffer; + int buffer_size; + int i; + __u8 cs = 0; + struct ti_i2c_desc *i2c_header; + struct ti_i2c_image_header *img_header; + struct ti_i2c_firmware_rec *firmware_rec; + + // In order to update the I2C firmware we must change the type 2 record to type 0xF2. + // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver + // will download the latest firmware (padded to 15.5k) into the UMP ram. + // And finally when the device comes back up in download mode the driver will cause + // the new firmware to be copied from the UMP Ram to I2C and the firmware will update + // the record type from 0xf2 to 0x02. + + // Allocate a 15.5k buffer + 2 bytes for version number (Firmware Record) + buffer_size = (((1024 * 16) - 512 )+ sizeof(struct ti_i2c_firmware_rec)); + + buffer = kmalloc (buffer_size, GFP_KERNEL); + if (!buffer) { + dev_err (dev, "%s - out of memory\n", __FUNCTION__); + return -ENOMEM; + } + + // Set entire image of 0xffs + memset (buffer, 0xff, buffer_size); + + // Copy version number into firmware record + firmware_rec = (struct ti_i2c_firmware_rec *)buffer; + + firmware_rec->Ver_Major = OperationalCodeImageVersion.MajorVersion; + firmware_rec->Ver_Minor = OperationalCodeImageVersion.MinorVersion; + + // Pointer to fw_down memory image + img_header = (struct ti_i2c_image_header *)&PagableOperationalCodeImage[0]; + + memcpy (buffer + sizeof(struct ti_i2c_firmware_rec), + &PagableOperationalCodeImage[sizeof(struct ti_i2c_image_header)], + le16_to_cpu(img_header->Length)); + + for (i=0; i < buffer_size; i++) { + cs = (__u8)(cs + buffer[i]); + } + + kfree (buffer); + + // Build new header + i2c_header = (struct ti_i2c_desc *)header; + firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data; + + i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK; + i2c_header->Size = (__u16)buffer_size; + i2c_header->CheckSum = cs; + firmware_rec->Ver_Major = OperationalCodeImageVersion.MajorVersion; + firmware_rec->Ver_Minor = OperationalCodeImageVersion.MinorVersion; + + return 0; +} + +/* Try to figure out what type of I2c we have */ +static int TIGetI2cTypeInBootMode (struct edgeport_serial *serial) +{ + int status; + __u8 data; + + // Try to read type 2 + status = TIReadVendorRequestSync (serial->serial->dev, + UMPC_MEMORY_READ, // Request + DTK_ADDR_SPACE_I2C_TYPE_II, // wValue (Address type) + 0, // wIndex + &data, // TransferBuffer + 0x01); // TransferBufferLength + if (status) + dbg ("%s - read 2 status error = %d", __FUNCTION__, status); + else + dbg ("%s - read 2 data = 0x%x", __FUNCTION__, data); + if ((!status) && (data == UMP5152 || data == UMP3410)) { + dbg ("%s - ROM_TYPE_II", __FUNCTION__); + serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II; + return 0; + } + + // Try to read type 3 + status = TIReadVendorRequestSync (serial->serial->dev, + UMPC_MEMORY_READ, // Request + DTK_ADDR_SPACE_I2C_TYPE_III, // wValue (Address type) + 0, // wIndex + &data, // TransferBuffer + 0x01); // TransferBufferLength + if (status) + dbg ("%s - read 3 status error = %d", __FUNCTION__, status); + else + dbg ("%s - read 2 data = 0x%x", __FUNCTION__, data); + if ((!status) && (data == UMP5152 || data == UMP3410)) { + dbg ("%s - ROM_TYPE_III", __FUNCTION__); + serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III; + return 0; + } + + dbg ("%s - Unknown", __FUNCTION__); + serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II; + return -ENODEV; +} + +static int TISendBulkTransferSync (struct usb_serial *serial, void *buffer, int length, int *num_sent) +{ + int status; + + status = usb_bulk_msg (serial->dev, + usb_sndbulkpipe(serial->dev, + serial->port[0]->bulk_out_endpointAddress), + buffer, + length, + num_sent, + 1000); + return status; +} + +/* Download given firmware image to the device (IN BOOT MODE) */ +static int TIDownloadCodeImage (struct edgeport_serial *serial, __u8 *image, int image_length) +{ + int status = 0; + int pos; + int transfer; + int done; + + // Transfer firmware image + for (pos = 0; pos < image_length; ) { + // Read the next buffer from file + transfer = image_length - pos; + if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE) + transfer = EDGE_FW_BULK_MAX_PACKET_SIZE; + + // Transfer data + status = TISendBulkTransferSync (serial->serial, &image[pos], transfer, &done); + if (status) + break; + // Advance buffer pointer + pos += done; + } + + return status; +} + +// FIXME!!! +static int TIConfigureBootDevice (struct usb_device *dev) +{ + return 0; +} + +/** + * DownloadTIFirmware - Download run-time operating firmware to the TI5052 + * + * This routine downloads the main operating code into the TI5052, using the + * boot code already burned into E2PROM or ROM. + */ +static int TIDownloadFirmware (struct edgeport_serial *serial) +{ + struct device *dev = &serial->serial->dev->dev; + int status = 0; + int start_address; + struct edge_ti_manuf_descriptor *ti_manuf_desc; + struct usb_interface_descriptor *interface; + int download_cur_ver; + int download_new_ver; + + /* This routine is entered by both the BOOT mode and the Download mode + * We can determine which code is running by the reading the config + * descriptor and if we have only one bulk pipe it is in boot mode + */ + serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP; + + /* Default to type 2 i2c */ + serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II; + + status = TIChooseConfiguration (serial->serial->dev); + if (status) + return status; + + interface = &serial->serial->interface->cur_altsetting->desc; + if (!interface) { + dev_err (dev, "%s - no interface set, error!\n", __FUNCTION__); + return -ENODEV; + } + + // Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING + // if we have more than one endpoint we are definitely in download mode + if (interface->bNumEndpoints > 1) + serial->product_info.TiMode = TI_MODE_DOWNLOAD; + else + // Otherwise we will remain in configuring mode + serial->product_info.TiMode = TI_MODE_CONFIGURING; + + // Save Download Version Number + OperationalCodeImageVersion.MajorVersion = PagableOperationalCodeImageVersion.MajorVersion; + OperationalCodeImageVersion.MinorVersion = PagableOperationalCodeImageVersion.MinorVersion; + OperationalCodeImageVersion.BuildNumber = PagableOperationalCodeImageVersion.BuildNumber; + + /********************************************************************/ + /* Download Mode */ + /********************************************************************/ + if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) { + struct ti_i2c_desc *rom_desc; + + dbg ("%s - <<<<<<<<<<<<<<<RUNNING IN DOWNLOAD MODE>>>>>>>>>>", __FUNCTION__); + + status = TiValidateI2cImage (serial); + if (status) { + dbg ("%s - <<<<<<<<<<<<<<<DOWNLOAD MODE -- BAD I2C >>>>>>>>>>", + __FUNCTION__); + return status; + } + + /* Validate Hardware version number + * Read Manufacturing Descriptor from TI Based Edgeport + */ + ti_manuf_desc = kmalloc (sizeof (*ti_manuf_desc), GFP_KERNEL); + if (!ti_manuf_desc) { + dev_err (dev, "%s - out of memory.\n", __FUNCTION__); + return -ENOMEM; + } + status = TIReadManufDescriptor (serial, (__u8 *)ti_manuf_desc); + if (status) { + kfree (ti_manuf_desc); + return status; + } + + // Check version number of ION descriptor + if (!ignore_cpu_rev && TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev) < 2) { + dbg ( "%s - Wrong CPU Rev %d (Must be 2)", __FUNCTION__, + TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev)); + kfree (ti_manuf_desc); + return -EINVAL; + } + + rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL); + if (!rom_desc) { + dev_err (dev, "%s - out of memory.\n", __FUNCTION__); + kfree (ti_manuf_desc); + return -ENOMEM; + } + + // Search for type 2 record (firmware record) |