15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+ 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * Edgeport USB Serial Converter driver 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 2000-2002 Inside Out Networks, All rights reserved. 61da177e4SLinus Torvalds * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Supports the following devices: 91da177e4SLinus Torvalds * EP/1 EP/2 EP/4 EP/21 EP/22 EP/221 EP/42 EP/421 WATCHPORT 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * For questions or problems with this driver, contact Inside Out 121da177e4SLinus Torvalds * Networks technical support, or Peter Berger <pberger@brimson.com>, 131da177e4SLinus Torvalds * or Al Borchers <alborchers@steinerpoint.com>. 141da177e4SLinus Torvalds */ 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds #include <linux/kernel.h> 171da177e4SLinus Torvalds #include <linux/jiffies.h> 181da177e4SLinus Torvalds #include <linux/errno.h> 191da177e4SLinus Torvalds #include <linux/slab.h> 201da177e4SLinus Torvalds #include <linux/tty.h> 211da177e4SLinus Torvalds #include <linux/tty_driver.h> 221da177e4SLinus Torvalds #include <linux/tty_flip.h> 231da177e4SLinus Torvalds #include <linux/module.h> 241da177e4SLinus Torvalds #include <linux/spinlock.h> 25241ca64fSMatthias Kaehlcke #include <linux/mutex.h> 261da177e4SLinus Torvalds #include <linux/serial.h> 275509076dSJohan Hovold #include <linux/swab.h> 28d733cec1SJohan Hovold #include <linux/kfifo.h> 291da177e4SLinus Torvalds #include <linux/ioctl.h> 30d12b219aSJaswinder Singh #include <linux/firmware.h> 312742fd88SAlan Cox #include <linux/uaccess.h> 321da177e4SLinus Torvalds #include <linux/usb.h> 33a969888cSGreg Kroah-Hartman #include <linux/usb/serial.h> 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds #include "io_16654.h" 361da177e4SLinus Torvalds #include "io_usbvend.h" 371da177e4SLinus Torvalds #include "io_ti.h" 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli" 401da177e4SLinus Torvalds #define DRIVER_DESC "Edgeport USB Serial Driver" 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds #define EPROM_PAGE_SIZE 64 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds /* different hardware types */ 461da177e4SLinus Torvalds #define HARDWARE_TYPE_930 0 471da177e4SLinus Torvalds #define HARDWARE_TYPE_TIUMP 1 481da177e4SLinus Torvalds 492742fd88SAlan Cox /* IOCTL_PRIVATE_TI_GET_MODE Definitions */ 502742fd88SAlan Cox #define TI_MODE_CONFIGURING 0 /* Device has not entered start device */ 512742fd88SAlan Cox #define TI_MODE_BOOT 1 /* Staying in boot mode */ 522742fd88SAlan Cox #define TI_MODE_DOWNLOAD 2 /* Made it to download mode */ 5375899513SPeter E. Berger #define TI_MODE_TRANSITIONING 3 /* 5475899513SPeter E. Berger * Currently in boot mode but 5575899513SPeter E. Berger * transitioning to download mode 5675899513SPeter E. Berger */ 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds /* read urb state */ 591da177e4SLinus Torvalds #define EDGE_READ_URB_RUNNING 0 601da177e4SLinus Torvalds #define EDGE_READ_URB_STOPPING 1 611da177e4SLinus Torvalds #define EDGE_READ_URB_STOPPED 2 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds /* Product information read from the Edgeport */ 652742fd88SAlan Cox struct product_info { 662742fd88SAlan Cox int TiMode; /* Current TI Mode */ 672742fd88SAlan Cox __u8 hardware_type; /* Type of hardware */ 681da177e4SLinus Torvalds } __attribute__((packed)); 691da177e4SLinus Torvalds 70c0e34831SPeter E. Berger /* 71c0e34831SPeter E. Berger * Edgeport firmware header 72c0e34831SPeter E. Berger * 73c0e34831SPeter E. Berger * "build_number" has been set to 0 in all three of the images I have 74c0e34831SPeter E. Berger * seen, and Digi Tech Support suggests that it is safe to ignore it. 75c0e34831SPeter E. Berger * 76c0e34831SPeter E. Berger * "length" is the number of bytes of actual data following the header. 77c0e34831SPeter E. Berger * 78c0e34831SPeter E. Berger * "checksum" is the low order byte resulting from adding the values of 79c0e34831SPeter E. Berger * all the data bytes. 80c0e34831SPeter E. Berger */ 81c0e34831SPeter E. Berger struct edgeport_fw_hdr { 82c0e34831SPeter E. Berger u8 major_version; 83c0e34831SPeter E. Berger u8 minor_version; 84c0e34831SPeter E. Berger __le16 build_number; 85c0e34831SPeter E. Berger __le16 length; 86c0e34831SPeter E. Berger u8 checksum; 87c0e34831SPeter E. Berger } __packed; 88c0e34831SPeter E. Berger 891da177e4SLinus Torvalds struct edgeport_port { 901da177e4SLinus Torvalds __u16 uart_base; 911da177e4SLinus Torvalds __u16 dma_address; 921da177e4SLinus Torvalds __u8 shadow_msr; 931da177e4SLinus Torvalds __u8 shadow_mcr; 941da177e4SLinus Torvalds __u8 shadow_lsr; 951da177e4SLinus Torvalds __u8 lsr_mask; 9675899513SPeter E. Berger __u32 ump_read_timeout; /* 9775899513SPeter E. Berger * Number of milliseconds the UMP will 9875899513SPeter E. Berger * wait without data before completing 9975899513SPeter E. Berger * a read short 10075899513SPeter E. Berger */ 1011da177e4SLinus Torvalds int baud_rate; 1021da177e4SLinus Torvalds int close_pending; 1031da177e4SLinus Torvalds int lsr_event; 10448ee5801SJohan Hovold 1051da177e4SLinus Torvalds struct edgeport_serial *edge_serial; 1061da177e4SLinus Torvalds struct usb_serial_port *port; 1071da177e4SLinus Torvalds __u8 bUartMode; /* Port type, 0: RS232, etc. */ 1081da177e4SLinus Torvalds spinlock_t ep_lock; 1091da177e4SLinus Torvalds int ep_read_urb_state; 1101da177e4SLinus Torvalds int ep_write_urb_in_use; 1111da177e4SLinus Torvalds }; 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds struct edgeport_serial { 1141da177e4SLinus Torvalds struct product_info product_info; 1152742fd88SAlan Cox u8 TI_I2C_Type; /* Type of I2C in UMP */ 11675899513SPeter E. Berger u8 TiReadI2C; /* 11775899513SPeter E. Berger * Set to TRUE if we have read the 11875899513SPeter E. Berger * I2c in Boot Mode 11975899513SPeter E. Berger */ 120241ca64fSMatthias Kaehlcke struct mutex es_lock; 1211da177e4SLinus Torvalds int num_ports_open; 1221da177e4SLinus Torvalds struct usb_serial *serial; 12326c78daaSPeter E. Berger struct delayed_work heartbeat_work; 124c0e34831SPeter E. Berger int fw_version; 12526c78daaSPeter E. Berger bool use_heartbeat; 1261da177e4SLinus Torvalds }; 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds /* Devices that this driver supports */ 1307d40d7e8SNémeth Márton static const struct usb_device_id edgeport_1port_id_table[] = { 1311da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) }, 1321da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) }, 1331da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) }, 1341da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) }, 1351da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) }, 1361da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) }, 1371da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) }, 1381da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) }, 1391da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) }, 1401da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) }, 1411da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) }, 1421da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) }, 1431da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) }, 1441da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) }, 1451da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) }, 1461da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) }, 1471da177e4SLinus Torvalds { } 1481da177e4SLinus Torvalds }; 1491da177e4SLinus Torvalds 1507d40d7e8SNémeth Márton static const struct usb_device_id edgeport_2port_id_table[] = { 1511da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) }, 1521da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) }, 1531da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) }, 1541da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) }, 1551da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) }, 1561da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) }, 1571da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) }, 1581da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) }, 1591da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) }, 1601da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) }, 1611da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) }, 1621da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) }, 163fc4cbd75SMartin K. Petersen /* The 4, 8 and 16 port devices show up as multiple 2 port devices */ 1641da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) }, 165fc4cbd75SMartin K. Petersen { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) }, 166fc4cbd75SMartin K. Petersen { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) }, 167fc4cbd75SMartin K. Petersen { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) }, 168fc4cbd75SMartin K. Petersen { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) }, 1691da177e4SLinus Torvalds { } 1701da177e4SLinus Torvalds }; 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds /* Devices that this driver supports */ 1737d40d7e8SNémeth Márton static const struct usb_device_id id_table_combined[] = { 1741da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) }, 1751da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) }, 1761da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) }, 1771da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) }, 1781da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) }, 1791da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) }, 1801da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) }, 1811da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) }, 1821da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) }, 1831da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) }, 1841da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) }, 1851da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) }, 1861da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) }, 1871da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) }, 1881da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) }, 1891da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) }, 1901da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) }, 1911da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) }, 1921da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) }, 1931da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) }, 1941da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) }, 1951da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) }, 1961da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) }, 1971da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) }, 1981da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) }, 1991da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) }, 2001da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) }, 2011da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) }, 2021da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) }, 203fc4cbd75SMartin K. Petersen { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) }, 204fc4cbd75SMartin K. Petersen { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) }, 205fc4cbd75SMartin K. Petersen { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) }, 206fc4cbd75SMartin K. Petersen { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) }, 2071da177e4SLinus Torvalds { } 2081da177e4SLinus Torvalds }; 2091da177e4SLinus Torvalds 2101da177e4SLinus Torvalds MODULE_DEVICE_TABLE(usb, id_table_combined); 2111da177e4SLinus Torvalds 21290ab5ee9SRusty Russell static bool ignore_cpu_rev; 2132742fd88SAlan Cox static int default_uart_mode; /* RS232 */ 2141da177e4SLinus Torvalds 2152e124b4aSJiri Slaby static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data, 2162e124b4aSJiri Slaby int length); 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds static void stop_read(struct edgeport_port *edge_port); 2191da177e4SLinus Torvalds static int restart_read(struct edgeport_port *edge_port); 2201da177e4SLinus Torvalds 22195da310eSAlan Cox static void edge_set_termios(struct tty_struct *tty, 22295da310eSAlan Cox struct usb_serial_port *port, struct ktermios *old_termios); 223ae3759c2SJiri Slaby static void edge_send(struct usb_serial_port *port, struct tty_struct *tty); 2241da177e4SLinus Torvalds 225bebf1f18SPeter E. Berger static int do_download_mode(struct edgeport_serial *serial, 226bebf1f18SPeter E. Berger const struct firmware *fw); 227bebf1f18SPeter E. Berger static int do_boot_mode(struct edgeport_serial *serial, 228bebf1f18SPeter E. Berger const struct firmware *fw); 229bebf1f18SPeter E. Berger 230fc4cbd75SMartin K. Petersen /* sysfs attributes */ 231fc4cbd75SMartin K. Petersen static int edge_create_sysfs_attrs(struct usb_serial_port *port); 232fc4cbd75SMartin K. Petersen static int edge_remove_sysfs_attrs(struct usb_serial_port *port); 233fc4cbd75SMartin K. Petersen 23426c78daaSPeter E. Berger /* 23526c78daaSPeter E. Berger * Some release of Edgeport firmware "down3.bin" after version 4.80 23626c78daaSPeter E. Berger * introduced code to automatically disconnect idle devices on some 23726c78daaSPeter E. Berger * Edgeport models after periods of inactivity, typically ~60 seconds. 23826c78daaSPeter E. Berger * This occurs without regard to whether ports on the device are open 23926c78daaSPeter E. Berger * or not. Digi International Tech Support suggested: 24026c78daaSPeter E. Berger * 24126c78daaSPeter E. Berger * 1. Adding driver "heartbeat" code to reset the firmware timer by 24226c78daaSPeter E. Berger * requesting a descriptor record every 15 seconds, which should be 24326c78daaSPeter E. Berger * effective with newer firmware versions that require it, and benign 24426c78daaSPeter E. Berger * with older versions that do not. In practice 40 seconds seems often 24526c78daaSPeter E. Berger * enough. 24626c78daaSPeter E. Berger * 2. The heartbeat code is currently required only on Edgeport/416 models. 24726c78daaSPeter E. Berger */ 24826c78daaSPeter E. Berger #define FW_HEARTBEAT_VERSION_CUTOFF ((4 << 8) + 80) 24926c78daaSPeter E. Berger #define FW_HEARTBEAT_SECS 40 2501da177e4SLinus Torvalds 251c3ece7e7SPeter E. Berger /* Timeouts in msecs: firmware downloads take longer */ 252c3ece7e7SPeter E. Berger #define TI_VSEND_TIMEOUT_DEFAULT 1000 253c3ece7e7SPeter E. Berger #define TI_VSEND_TIMEOUT_FW_DOWNLOAD 10000 254c3ece7e7SPeter E. Berger 255e8d89db0SJohan Hovold static int ti_vread_sync(struct usb_device *dev, u8 request, u16 value, 256e8d89db0SJohan Hovold u16 index, void *data, int size) 2571da177e4SLinus Torvalds { 2581da177e4SLinus Torvalds int status; 2591da177e4SLinus Torvalds 2602742fd88SAlan Cox status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request, 2612742fd88SAlan Cox (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN), 2622742fd88SAlan Cox value, index, data, size, 1000); 2631da177e4SLinus Torvalds if (status < 0) 2641da177e4SLinus Torvalds return status; 2651da177e4SLinus Torvalds if (status != size) { 2660765590fSJohan Hovold dev_dbg(&dev->dev, "%s - wanted to read %d, but only read %d\n", 267441b62c1SHarvey Harrison __func__, size, status); 2681da177e4SLinus Torvalds return -ECOMM; 2691da177e4SLinus Torvalds } 2701da177e4SLinus Torvalds return 0; 2711da177e4SLinus Torvalds } 2721da177e4SLinus Torvalds 273c3ece7e7SPeter E. Berger static int ti_vsend_sync(struct usb_device *dev, u8 request, u16 value, 274e8d89db0SJohan Hovold u16 index, void *data, int size, int timeout) 2751da177e4SLinus Torvalds { 2761da177e4SLinus Torvalds int status; 2771da177e4SLinus Torvalds 2782742fd88SAlan Cox status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request, 2792742fd88SAlan Cox (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT), 280c3ece7e7SPeter E. Berger value, index, data, size, timeout); 2811da177e4SLinus Torvalds if (status < 0) 2821da177e4SLinus Torvalds return status; 28366db9478SJohan Hovold 2841da177e4SLinus Torvalds return 0; 2851da177e4SLinus Torvalds } 2861da177e4SLinus Torvalds 287*13c61339SJohan Hovold static int read_port_cmd(struct usb_serial_port *port, u8 command, u16 value, 288*13c61339SJohan Hovold void *data, int size) 289*13c61339SJohan Hovold { 290*13c61339SJohan Hovold return ti_vread_sync(port->serial->dev, command, value, 291*13c61339SJohan Hovold UMPM_UART1_PORT + port->port_number, 292*13c61339SJohan Hovold data, size); 293*13c61339SJohan Hovold } 294*13c61339SJohan Hovold 2957a14fac0SJohan Hovold static int send_port_cmd(struct usb_serial_port *port, u8 command, u16 value, 2967a14fac0SJohan Hovold void *data, int size) 2971da177e4SLinus Torvalds { 2987a14fac0SJohan Hovold return ti_vsend_sync(port->serial->dev, command, value, 2997a14fac0SJohan Hovold UMPM_UART1_PORT + port->port_number, 3007a14fac0SJohan Hovold data, size, TI_VSEND_TIMEOUT_DEFAULT); 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds /* clear tx/rx buffers and fifo in TI UMP */ 3042742fd88SAlan Cox static int purge_port(struct usb_serial_port *port, __u16 mask) 3051da177e4SLinus Torvalds { 3061143832eSGreg Kroah-Hartman int port_number = port->port_number; 3071da177e4SLinus Torvalds 30867e6da70SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - port %d, mask %x\n", __func__, port_number, mask); 3091da177e4SLinus Torvalds 3107a14fac0SJohan Hovold return send_port_cmd(port, UMPC_PURGE_PORT, mask, NULL, 0); 3111da177e4SLinus Torvalds } 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds /** 3142742fd88SAlan Cox * read_download_mem - Read edgeport memory from TI chip 3151da177e4SLinus Torvalds * @dev: usb device pointer 3161da177e4SLinus Torvalds * @start_address: Device CPU address at which to read 3171da177e4SLinus Torvalds * @length: Length of above data 3181da177e4SLinus Torvalds * @address_type: Can read both XDATA and I2C 3191da177e4SLinus Torvalds * @buffer: pointer to input data buffer 3201da177e4SLinus Torvalds */ 3212742fd88SAlan Cox static int read_download_mem(struct usb_device *dev, int start_address, 3221da177e4SLinus Torvalds int length, __u8 address_type, __u8 *buffer) 3231da177e4SLinus Torvalds { 3241da177e4SLinus Torvalds int status = 0; 3251da177e4SLinus Torvalds __u8 read_length; 3265509076dSJohan Hovold u16 be_start_address; 3271da177e4SLinus Torvalds 32867e6da70SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, length); 3291da177e4SLinus Torvalds 33075899513SPeter E. Berger /* 33175899513SPeter E. Berger * Read in blocks of 64 bytes 3321da177e4SLinus Torvalds * (TI firmware can't handle more than 64 byte reads) 3331da177e4SLinus Torvalds */ 3341da177e4SLinus Torvalds while (length) { 3351da177e4SLinus Torvalds if (length > 64) 3361da177e4SLinus Torvalds read_length = 64; 3371da177e4SLinus Torvalds else 3381da177e4SLinus Torvalds read_length = (__u8)length; 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds if (read_length > 1) { 34167e6da70SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, read_length); 3421da177e4SLinus Torvalds } 3435509076dSJohan Hovold /* 3445509076dSJohan Hovold * NOTE: Must use swab as wIndex is sent in little-endian 3455509076dSJohan Hovold * byte order regardless of host byte order. 3465509076dSJohan Hovold */ 3475509076dSJohan Hovold be_start_address = swab16((u16)start_address); 3482742fd88SAlan Cox status = ti_vread_sync(dev, UMPC_MEMORY_READ, 3492742fd88SAlan Cox (__u16)address_type, 3505509076dSJohan Hovold be_start_address, 3512742fd88SAlan Cox buffer, read_length); 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds if (status) { 35467e6da70SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s - ERROR %x\n", __func__, status); 3551da177e4SLinus Torvalds return status; 3561da177e4SLinus Torvalds } 3571da177e4SLinus Torvalds 3582742fd88SAlan Cox if (read_length > 1) 35959d33f2fSGreg Kroah-Hartman usb_serial_debug_data(&dev->dev, __func__, read_length, buffer); 3601da177e4SLinus Torvalds 3611da177e4SLinus Torvalds /* Update pointers/length */ 3621da177e4SLinus Torvalds start_address += read_length; 3631da177e4SLinus Torvalds buffer += read_length; 3641da177e4SLinus Torvalds length -= read_length; 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds 3671da177e4SLinus Torvalds return status; 3681da177e4SLinus Torvalds } 3691da177e4SLinus Torvalds 3702742fd88SAlan Cox static int read_ram(struct usb_device *dev, int start_address, 3712742fd88SAlan Cox int length, __u8 *buffer) 3721da177e4SLinus Torvalds { 3732742fd88SAlan Cox return read_download_mem(dev, start_address, length, 3742742fd88SAlan Cox DTK_ADDR_SPACE_XDATA, buffer); 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds /* Read edgeport memory to a given block */ 3782742fd88SAlan Cox static int read_boot_mem(struct edgeport_serial *serial, 3792742fd88SAlan Cox int start_address, int length, __u8 *buffer) 3801da177e4SLinus Torvalds { 3811da177e4SLinus Torvalds int status = 0; 3821da177e4SLinus Torvalds int i; 3831da177e4SLinus Torvalds 3841da177e4SLinus Torvalds for (i = 0; i < length; i++) { 3852742fd88SAlan Cox status = ti_vread_sync(serial->serial->dev, 3862742fd88SAlan Cox UMPC_MEMORY_READ, serial->TI_I2C_Type, 3872742fd88SAlan Cox (__u16)(start_address+i), &buffer[i], 0x01); 3881da177e4SLinus Torvalds if (status) { 38967e6da70SGreg Kroah-Hartman dev_dbg(&serial->serial->dev->dev, "%s - ERROR %x\n", __func__, status); 3901da177e4SLinus Torvalds return status; 3911da177e4SLinus Torvalds } 3921da177e4SLinus Torvalds } 3931da177e4SLinus Torvalds 39467e6da70SGreg Kroah-Hartman dev_dbg(&serial->serial->dev->dev, "%s - start_address = %x, length = %d\n", 3952742fd88SAlan Cox __func__, start_address, length); 39659d33f2fSGreg Kroah-Hartman usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer); 3971da177e4SLinus Torvalds 3981da177e4SLinus Torvalds serial->TiReadI2C = 1; 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds return status; 4011da177e4SLinus Torvalds } 4021da177e4SLinus Torvalds 4031da177e4SLinus Torvalds /* Write given block to TI EPROM memory */ 4042742fd88SAlan Cox static int write_boot_mem(struct edgeport_serial *serial, 4052742fd88SAlan Cox int start_address, int length, __u8 *buffer) 4061da177e4SLinus Torvalds { 4071da177e4SLinus Torvalds int status = 0; 4081da177e4SLinus Torvalds int i; 409e9305d2fSJohan Hovold u8 *temp; 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds /* Must do a read before write */ 4121da177e4SLinus Torvalds if (!serial->TiReadI2C) { 413e9305d2fSJohan Hovold temp = kmalloc(1, GFP_KERNEL); 41410c642d0SJohan Hovold if (!temp) 415e9305d2fSJohan Hovold return -ENOMEM; 41610c642d0SJohan Hovold 417e9305d2fSJohan Hovold status = read_boot_mem(serial, 0, 1, temp); 418e9305d2fSJohan Hovold kfree(temp); 4191da177e4SLinus Torvalds if (status) 4201da177e4SLinus Torvalds return status; 4211da177e4SLinus Torvalds } 4221da177e4SLinus Torvalds 4231da177e4SLinus Torvalds for (i = 0; i < length; ++i) { 424c3ece7e7SPeter E. Berger status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE, 425c3ece7e7SPeter E. Berger buffer[i], (u16)(i + start_address), NULL, 426c3ece7e7SPeter E. Berger 0, TI_VSEND_TIMEOUT_DEFAULT); 4271da177e4SLinus Torvalds if (status) 4281da177e4SLinus Torvalds return status; 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds 43167e6da70SGreg Kroah-Hartman dev_dbg(&serial->serial->dev->dev, "%s - start_sddr = %x, length = %d\n", __func__, start_address, length); 43259d33f2fSGreg Kroah-Hartman usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer); 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds return status; 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds /* Write edgeport I2C memory to TI chip */ 4382742fd88SAlan Cox static int write_i2c_mem(struct edgeport_serial *serial, 4392742fd88SAlan Cox int start_address, int length, __u8 address_type, __u8 *buffer) 4401da177e4SLinus Torvalds { 44167e6da70SGreg Kroah-Hartman struct device *dev = &serial->serial->dev->dev; 4421da177e4SLinus Torvalds int status = 0; 4431da177e4SLinus Torvalds int write_length; 4445509076dSJohan Hovold u16 be_start_address; 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds /* We can only send a maximum of 1 aligned byte page at a time */ 4471da177e4SLinus Torvalds 44825985edcSLucas De Marchi /* calculate the number of bytes left in the first page */ 4492742fd88SAlan Cox write_length = EPROM_PAGE_SIZE - 4502742fd88SAlan Cox (start_address & (EPROM_PAGE_SIZE - 1)); 4511da177e4SLinus Torvalds 4521da177e4SLinus Torvalds if (write_length > length) 4531da177e4SLinus Torvalds write_length = length; 4541da177e4SLinus Torvalds 45567e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - BytesInFirstPage Addr = %x, length = %d\n", 4562742fd88SAlan Cox __func__, start_address, write_length); 45759d33f2fSGreg Kroah-Hartman usb_serial_debug_data(dev, __func__, write_length, buffer); 4581da177e4SLinus Torvalds 4595509076dSJohan Hovold /* 4605509076dSJohan Hovold * Write first page. 4615509076dSJohan Hovold * 4625509076dSJohan Hovold * NOTE: Must use swab as wIndex is sent in little-endian byte order 4635509076dSJohan Hovold * regardless of host byte order. 4645509076dSJohan Hovold */ 4655509076dSJohan Hovold be_start_address = swab16((u16)start_address); 466c3ece7e7SPeter E. Berger status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE, 467c3ece7e7SPeter E. Berger (u16)address_type, be_start_address, 468c3ece7e7SPeter E. Berger buffer, write_length, TI_VSEND_TIMEOUT_DEFAULT); 4691da177e4SLinus Torvalds if (status) { 47067e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - ERROR %d\n", __func__, status); 4711da177e4SLinus Torvalds return status; 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds length -= write_length; 4751da177e4SLinus Torvalds start_address += write_length; 4761da177e4SLinus Torvalds buffer += write_length; 4771da177e4SLinus Torvalds 47875899513SPeter E. Berger /* 47975899513SPeter E. Berger * We should be aligned now -- can write max page size bytes at a 48075899513SPeter E. Berger * time. 48175899513SPeter E. Berger */ 4821da177e4SLinus Torvalds while (length) { 4831da177e4SLinus Torvalds if (length > EPROM_PAGE_SIZE) 4841da177e4SLinus Torvalds write_length = EPROM_PAGE_SIZE; 4851da177e4SLinus Torvalds else 4861da177e4SLinus Torvalds write_length = length; 4871da177e4SLinus Torvalds 48867e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - Page Write Addr = %x, length = %d\n", 4892742fd88SAlan Cox __func__, start_address, write_length); 49059d33f2fSGreg Kroah-Hartman usb_serial_debug_data(dev, __func__, write_length, buffer); 4911da177e4SLinus Torvalds 4925509076dSJohan Hovold /* 4935509076dSJohan Hovold * Write next page. 4945509076dSJohan Hovold * 4955509076dSJohan Hovold * NOTE: Must use swab as wIndex is sent in little-endian byte 4965509076dSJohan Hovold * order regardless of host byte order. 4975509076dSJohan Hovold */ 4985509076dSJohan Hovold be_start_address = swab16((u16)start_address); 4992742fd88SAlan Cox status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE, 500c3ece7e7SPeter E. Berger (u16)address_type, be_start_address, buffer, 501c3ece7e7SPeter E. Berger write_length, TI_VSEND_TIMEOUT_DEFAULT); 5021da177e4SLinus Torvalds if (status) { 50367e6da70SGreg Kroah-Hartman dev_err(dev, "%s - ERROR %d\n", __func__, status); 5041da177e4SLinus Torvalds return status; 5051da177e4SLinus Torvalds } 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds length -= write_length; 5081da177e4SLinus Torvalds start_address += write_length; 5091da177e4SLinus Torvalds buffer += write_length; 5101da177e4SLinus Torvalds } 5111da177e4SLinus Torvalds return status; 5121da177e4SLinus Torvalds } 5131da177e4SLinus Torvalds 51475899513SPeter E. Berger /* 51575899513SPeter E. Berger * Examine the UMP DMA registers and LSR 5161da177e4SLinus Torvalds * 5171da177e4SLinus Torvalds * Check the MSBit of the X and Y DMA byte count registers. 5181da177e4SLinus Torvalds * A zero in this bit indicates that the TX DMA buffers are empty 5191da177e4SLinus Torvalds * then check the TX Empty bit in the UART. 5201da177e4SLinus Torvalds */ 5212742fd88SAlan Cox static int tx_active(struct edgeport_port *port) 5221da177e4SLinus Torvalds { 5231da177e4SLinus Torvalds int status; 5241da177e4SLinus Torvalds struct out_endpoint_desc_block *oedb; 5251da177e4SLinus Torvalds __u8 *lsr; 5261da177e4SLinus Torvalds int bytes_left = 0; 5271da177e4SLinus Torvalds 5281da177e4SLinus Torvalds oedb = kmalloc(sizeof(*oedb), GFP_KERNEL); 52910c642d0SJohan Hovold if (!oedb) 5301da177e4SLinus Torvalds return -ENOMEM; 5311da177e4SLinus Torvalds 53275899513SPeter E. Berger /* 53375899513SPeter E. Berger * Sigh, that's right, just one byte, as not all platforms can 53475899513SPeter E. Berger * do DMA from stack 53575899513SPeter E. Berger */ 53675899513SPeter E. Berger lsr = kmalloc(1, GFP_KERNEL); 5371da177e4SLinus Torvalds if (!lsr) { 5381da177e4SLinus Torvalds kfree(oedb); 5391da177e4SLinus Torvalds return -ENOMEM; 5401da177e4SLinus Torvalds } 5411da177e4SLinus Torvalds /* Read the DMA Count Registers */ 5422742fd88SAlan Cox status = read_ram(port->port->serial->dev, port->dma_address, 5432742fd88SAlan Cox sizeof(*oedb), (void *)oedb); 5441da177e4SLinus Torvalds if (status) 5451da177e4SLinus Torvalds goto exit_is_tx_active; 5461da177e4SLinus Torvalds 54767e6da70SGreg Kroah-Hartman dev_dbg(&port->port->dev, "%s - XByteCount 0x%X\n", __func__, oedb->XByteCount); 5481da177e4SLinus Torvalds 5491da177e4SLinus Torvalds /* and the LSR */ 5502742fd88SAlan Cox status = read_ram(port->port->serial->dev, 5512742fd88SAlan Cox port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr); 5521da177e4SLinus Torvalds 5531da177e4SLinus Torvalds if (status) 5541da177e4SLinus Torvalds goto exit_is_tx_active; 55567e6da70SGreg Kroah-Hartman dev_dbg(&port->port->dev, "%s - LSR = 0x%X\n", __func__, *lsr); 5561da177e4SLinus Torvalds 5571da177e4SLinus Torvalds /* If either buffer has data or we are transmitting then return TRUE */ 5581da177e4SLinus Torvalds if ((oedb->XByteCount & 0x80) != 0) 5591da177e4SLinus Torvalds bytes_left += 64; 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds if ((*lsr & UMP_UART_LSR_TX_MASK) == 0) 5621da177e4SLinus Torvalds bytes_left += 1; 5631da177e4SLinus Torvalds 5641da177e4SLinus Torvalds /* We return Not Active if we get any kind of error */ 5651da177e4SLinus Torvalds exit_is_tx_active: 56667e6da70SGreg Kroah-Hartman dev_dbg(&port->port->dev, "%s - return %d\n", __func__, bytes_left); 5671da177e4SLinus Torvalds 5681da177e4SLinus Torvalds kfree(lsr); 5691da177e4SLinus Torvalds kfree(oedb); 5701da177e4SLinus Torvalds return bytes_left; 5711da177e4SLinus Torvalds } 5721da177e4SLinus Torvalds 5732742fd88SAlan Cox static int choose_config(struct usb_device *dev) 5741da177e4SLinus Torvalds { 5752742fd88SAlan Cox /* 5762742fd88SAlan Cox * There may be multiple configurations on this device, in which case 5772742fd88SAlan Cox * we would need to read and parse all of them to find out which one 5782742fd88SAlan Cox * we want. However, we just support one config at this point, 5792742fd88SAlan Cox * configuration # 1, which is Config Descriptor 0. 5802742fd88SAlan Cox */ 5811da177e4SLinus Torvalds 58267e6da70SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s - Number of Interfaces = %d\n", 5832742fd88SAlan Cox __func__, dev->config->desc.bNumInterfaces); 58467e6da70SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s - MAX Power = %d\n", 5852742fd88SAlan Cox __func__, dev->config->desc.bMaxPower * 2); 5861da177e4SLinus Torvalds 5871da177e4SLinus Torvalds if (dev->config->desc.bNumInterfaces != 1) { 58867e6da70SGreg Kroah-Hartman dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __func__); 5891da177e4SLinus Torvalds return -ENODEV; 5901da177e4SLinus Torvalds } 5911da177e4SLinus Torvalds 5921da177e4SLinus Torvalds return 0; 5931da177e4SLinus Torvalds } 5941da177e4SLinus Torvalds 5952742fd88SAlan Cox static int read_rom(struct edgeport_serial *serial, 5962742fd88SAlan Cox int start_address, int length, __u8 *buffer) 5971da177e4SLinus Torvalds { 5981da177e4SLinus Torvalds int status; 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) { 6012742fd88SAlan Cox status = read_download_mem(serial->serial->dev, 6021da177e4SLinus Torvalds start_address, 6031da177e4SLinus Torvalds length, 6041da177e4SLinus Torvalds serial->TI_I2C_Type, 6051da177e4SLinus Torvalds buffer); 6061da177e4SLinus Torvalds } else { 6072742fd88SAlan Cox status = read_boot_mem(serial, start_address, length, 6081da177e4SLinus Torvalds buffer); 6091da177e4SLinus Torvalds } 6101da177e4SLinus Torvalds return status; 6111da177e4SLinus Torvalds } 6121da177e4SLinus Torvalds 6132742fd88SAlan Cox static int write_rom(struct edgeport_serial *serial, int start_address, 6142742fd88SAlan Cox int length, __u8 *buffer) 6151da177e4SLinus Torvalds { 6161da177e4SLinus Torvalds if (serial->product_info.TiMode == TI_MODE_BOOT) 6172742fd88SAlan Cox return write_boot_mem(serial, start_address, length, 6181da177e4SLinus Torvalds buffer); 6191da177e4SLinus Torvalds 6201da177e4SLinus Torvalds if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) 6212742fd88SAlan Cox return write_i2c_mem(serial, start_address, length, 6222742fd88SAlan Cox serial->TI_I2C_Type, buffer); 6231da177e4SLinus Torvalds return -EINVAL; 6241da177e4SLinus Torvalds } 6251da177e4SLinus Torvalds 6261da177e4SLinus Torvalds /* Read a descriptor header from I2C based on type */ 6272742fd88SAlan Cox static int get_descriptor_addr(struct edgeport_serial *serial, 6282742fd88SAlan Cox int desc_type, struct ti_i2c_desc *rom_desc) 6291da177e4SLinus Torvalds { 6301da177e4SLinus Torvalds int start_address; 6311da177e4SLinus Torvalds int status; 6321da177e4SLinus Torvalds 6331da177e4SLinus Torvalds /* Search for requested descriptor in I2C */ 6341da177e4SLinus Torvalds start_address = 2; 6351da177e4SLinus Torvalds do { 6362742fd88SAlan Cox status = read_rom(serial, 6371da177e4SLinus Torvalds start_address, 6381da177e4SLinus Torvalds sizeof(struct ti_i2c_desc), 6391da177e4SLinus Torvalds (__u8 *)rom_desc); 6401da177e4SLinus Torvalds if (status) 6411da177e4SLinus Torvalds return 0; 6421da177e4SLinus Torvalds 6431da177e4SLinus Torvalds if (rom_desc->Type == desc_type) 6441da177e4SLinus Torvalds return start_address; 6451da177e4SLinus Torvalds 6465509076dSJohan Hovold start_address = start_address + sizeof(struct ti_i2c_desc) + 6475509076dSJohan Hovold le16_to_cpu(rom_desc->Size); 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type); 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds return 0; 6521da177e4SLinus Torvalds } 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds /* Validate descriptor checksum */ 6552742fd88SAlan Cox static int valid_csum(struct ti_i2c_desc *rom_desc, __u8 *buffer) 6561da177e4SLinus Torvalds { 6571da177e4SLinus Torvalds __u16 i; 6581da177e4SLinus Torvalds __u8 cs = 0; 6591da177e4SLinus Torvalds 6605509076dSJohan Hovold for (i = 0; i < le16_to_cpu(rom_desc->Size); i++) 6611da177e4SLinus Torvalds cs = (__u8)(cs + buffer[i]); 6622742fd88SAlan Cox 6631da177e4SLinus Torvalds if (cs != rom_desc->CheckSum) { 66467e6da70SGreg Kroah-Hartman pr_debug("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs); 6651da177e4SLinus Torvalds return -EINVAL; 6661da177e4SLinus Torvalds } 6671da177e4SLinus Torvalds return 0; 6681da177e4SLinus Torvalds } 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds /* Make sure that the I2C image is good */ 6712742fd88SAlan Cox static int check_i2c_image(struct edgeport_serial *serial) 6721da177e4SLinus Torvalds { 6731da177e4SLinus Torvalds struct device *dev = &serial->serial->dev->dev; 6741da177e4SLinus Torvalds int status = 0; 6751da177e4SLinus Torvalds struct ti_i2c_desc *rom_desc; 6761da177e4SLinus Torvalds int start_address = 2; 6771da177e4SLinus Torvalds __u8 *buffer; 6781da177e4SLinus Torvalds __u16 ttype; 6791da177e4SLinus Torvalds 6801da177e4SLinus Torvalds rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL); 68110c642d0SJohan Hovold if (!rom_desc) 6821da177e4SLinus Torvalds return -ENOMEM; 68310c642d0SJohan Hovold 6841da177e4SLinus Torvalds buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL); 6851da177e4SLinus Torvalds if (!buffer) { 6861da177e4SLinus Torvalds kfree(rom_desc); 6871da177e4SLinus Torvalds return -ENOMEM; 6881da177e4SLinus Torvalds } 6891da177e4SLinus Torvalds 6902742fd88SAlan Cox /* Read the first byte (Signature0) must be 0x52 or 0x10 */ 6912742fd88SAlan Cox status = read_rom(serial, 0, 1, buffer); 6921da177e4SLinus Torvalds if (status) 6932742fd88SAlan Cox goto out; 6941da177e4SLinus Torvalds 6951da177e4SLinus Torvalds if (*buffer != UMP5152 && *buffer != UMP3410) { 696441b62c1SHarvey Harrison dev_err(dev, "%s - invalid buffer signature\n", __func__); 6971da177e4SLinus Torvalds status = -ENODEV; 6982742fd88SAlan Cox goto out; 6991da177e4SLinus Torvalds } 7001da177e4SLinus Torvalds 7011da177e4SLinus Torvalds do { 7022742fd88SAlan Cox /* Validate the I2C */ 7032742fd88SAlan Cox status = read_rom(serial, 7041da177e4SLinus Torvalds start_address, 7051da177e4SLinus Torvalds sizeof(struct ti_i2c_desc), 7061da177e4SLinus Torvalds (__u8 *)rom_desc); 7071da177e4SLinus Torvalds if (status) 7081da177e4SLinus Torvalds break; 7091da177e4SLinus Torvalds 7102742fd88SAlan Cox if ((start_address + sizeof(struct ti_i2c_desc) + 7115509076dSJohan Hovold le16_to_cpu(rom_desc->Size)) > TI_MAX_I2C_SIZE) { 7121da177e4SLinus Torvalds status = -ENODEV; 71367e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - structure too big, erroring out.\n", __func__); 7141da177e4SLinus Torvalds break; 7151da177e4SLinus Torvalds } 7161da177e4SLinus Torvalds 71767e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s Type = 0x%x\n", __func__, rom_desc->Type); 7181da177e4SLinus Torvalds 7192742fd88SAlan Cox /* Skip type 2 record */ 7201da177e4SLinus Torvalds ttype = rom_desc->Type & 0x0f; 7211da177e4SLinus Torvalds if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC 7221da177e4SLinus Torvalds && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) { 7232742fd88SAlan Cox /* Read the descriptor data */ 7242742fd88SAlan Cox status = read_rom(serial, start_address + 7252742fd88SAlan Cox sizeof(struct ti_i2c_desc), 7265509076dSJohan Hovold le16_to_cpu(rom_desc->Size), 7275509076dSJohan Hovold buffer); 7281da177e4SLinus Torvalds if (status) 7291da177e4SLinus Torvalds break; 7301da177e4SLinus Torvalds 7312742fd88SAlan Cox status = valid_csum(rom_desc, buffer); 7321da177e4SLinus Torvalds if (status) 7331da177e4SLinus Torvalds break; 7341da177e4SLinus Torvalds } 7352742fd88SAlan Cox start_address = start_address + sizeof(struct ti_i2c_desc) + 7365509076dSJohan Hovold le16_to_cpu(rom_desc->Size); 7371da177e4SLinus Torvalds 7382742fd88SAlan Cox } while ((rom_desc->Type != I2C_DESC_TYPE_ION) && 7392742fd88SAlan Cox (start_address < TI_MAX_I2C_SIZE)); 7401da177e4SLinus Torvalds 7412742fd88SAlan Cox if ((rom_desc->Type != I2C_DESC_TYPE_ION) || 7422742fd88SAlan Cox (start_address > TI_MAX_I2C_SIZE)) 7431da177e4SLinus Torvalds status = -ENODEV; 7441da177e4SLinus Torvalds 7452742fd88SAlan Cox out: 7461da177e4SLinus Torvalds kfree(buffer); 7471da177e4SLinus Torvalds kfree(rom_desc); 7481da177e4SLinus Torvalds return status; 7491da177e4SLinus Torvalds } 7501da177e4SLinus Torvalds 7512742fd88SAlan Cox static int get_manuf_info(struct edgeport_serial *serial, __u8 *buffer) 7521da177e4SLinus Torvalds { 7531da177e4SLinus Torvalds int status; 7541da177e4SLinus Torvalds int start_address; 7551da177e4SLinus Torvalds struct ti_i2c_desc *rom_desc; 7561da177e4SLinus Torvalds struct edge_ti_manuf_descriptor *desc; 75767e6da70SGreg Kroah-Hartman struct device *dev = &serial->serial->dev->dev; 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL); 76010c642d0SJohan Hovold if (!rom_desc) 7611da177e4SLinus Torvalds return -ENOMEM; 76210c642d0SJohan Hovold 7632742fd88SAlan Cox start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION, 7642742fd88SAlan Cox rom_desc); 7651da177e4SLinus Torvalds 7661da177e4SLinus Torvalds if (!start_address) { 76767e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - Edge Descriptor not found in I2C\n", __func__); 7681da177e4SLinus Torvalds status = -ENODEV; 7691da177e4SLinus Torvalds goto exit; 7701da177e4SLinus Torvalds } 7711da177e4SLinus Torvalds 7722742fd88SAlan Cox /* Read the descriptor data */ 7732742fd88SAlan Cox status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc), 7745509076dSJohan Hovold le16_to_cpu(rom_desc->Size), buffer); 7751da177e4SLinus Torvalds if (status) 7761da177e4SLinus Torvalds goto exit; 7771da177e4SLinus Torvalds 7782742fd88SAlan Cox status = valid_csum(rom_desc, buffer); 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds desc = (struct edge_ti_manuf_descriptor *)buffer; 78167e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - IonConfig 0x%x\n", __func__, desc->IonConfig); 78267e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - Version %d\n", __func__, desc->Version); 78367e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - Cpu/Board 0x%x\n", __func__, desc->CpuRev_BoardRev); 78467e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - NumPorts %d\n", __func__, desc->NumPorts); 78567e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - NumVirtualPorts %d\n", __func__, desc->NumVirtualPorts); 78667e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - TotalPorts %d\n", __func__, desc->TotalPorts); 7871da177e4SLinus Torvalds 7881da177e4SLinus Torvalds exit: 7891da177e4SLinus Torvalds kfree(rom_desc); 7901da177e4SLinus Torvalds return status; 7911da177e4SLinus Torvalds } 7921da177e4SLinus Torvalds 7931da177e4SLinus Torvalds /* Build firmware header used for firmware update */ 794277bf37bSPeter E. Berger static int build_i2c_fw_hdr(u8 *header, const struct firmware *fw) 7951da177e4SLinus Torvalds { 7961da177e4SLinus Torvalds __u8 *buffer; 7971da177e4SLinus Torvalds int buffer_size; 7981da177e4SLinus Torvalds int i; 7991da177e4SLinus Torvalds __u8 cs = 0; 8001da177e4SLinus Torvalds struct ti_i2c_desc *i2c_header; 8011da177e4SLinus Torvalds struct ti_i2c_image_header *img_header; 8021da177e4SLinus Torvalds struct ti_i2c_firmware_rec *firmware_rec; 803c0e34831SPeter E. Berger struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data; 8041da177e4SLinus Torvalds 80575899513SPeter E. Berger /* 80675899513SPeter E. Berger * In order to update the I2C firmware we must change the type 2 record 8072742fd88SAlan Cox * to type 0xF2. This will force the UMP to come up in Boot Mode. 8082742fd88SAlan Cox * Then while in boot mode, the driver will download the latest 8092742fd88SAlan Cox * firmware (padded to 15.5k) into the UMP ram. And finally when the 8102742fd88SAlan Cox * device comes back up in download mode the driver will cause the new 8112742fd88SAlan Cox * firmware to be copied from the UMP Ram to I2C and the firmware will 8122742fd88SAlan Cox * update the record type from 0xf2 to 0x02. 8132742fd88SAlan Cox */ 8141da177e4SLinus Torvalds 81575899513SPeter E. Berger /* 81675899513SPeter E. Berger * Allocate a 15.5k buffer + 2 bytes for version number (Firmware 81775899513SPeter E. Berger * Record) 81875899513SPeter E. Berger */ 8192742fd88SAlan Cox buffer_size = (((1024 * 16) - 512 ) + 8202742fd88SAlan Cox sizeof(struct ti_i2c_firmware_rec)); 8211da177e4SLinus Torvalds 8221da177e4SLinus Torvalds buffer = kmalloc(buffer_size, GFP_KERNEL); 82310c642d0SJohan Hovold if (!buffer) 8241da177e4SLinus Torvalds return -ENOMEM; 8251da177e4SLinus Torvalds 82675899513SPeter E. Berger /* Set entire image of 0xffs */ 8271da177e4SLinus Torvalds memset(buffer, 0xff, buffer_size); 8281da177e4SLinus Torvalds 8292742fd88SAlan Cox /* Copy version number into firmware record */ 8301da177e4SLinus Torvalds firmware_rec = (struct ti_i2c_firmware_rec *)buffer; 8311da177e4SLinus Torvalds 832c0e34831SPeter E. Berger firmware_rec->Ver_Major = fw_hdr->major_version; 833c0e34831SPeter E. Berger firmware_rec->Ver_Minor = fw_hdr->minor_version; 8341da177e4SLinus Torvalds 8352742fd88SAlan Cox /* Pointer to fw_down memory image */ 836d12b219aSJaswinder Singh img_header = (struct ti_i2c_image_header *)&fw->data[4]; 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds memcpy(buffer + sizeof(struct ti_i2c_firmware_rec), 839d12b219aSJaswinder Singh &fw->data[4 + sizeof(struct ti_i2c_image_header)], 8401da177e4SLinus Torvalds le16_to_cpu(img_header->Length)); 8411da177e4SLinus Torvalds 8421da177e4SLinus Torvalds for (i=0; i < buffer_size; i++) { 8431da177e4SLinus Torvalds cs = (__u8)(cs + buffer[i]); 8441da177e4SLinus Torvalds } 8451da177e4SLinus Torvalds 8461da177e4SLinus Torvalds kfree(buffer); 8471da177e4SLinus Torvalds 8482742fd88SAlan Cox /* Build new header */ 8491da177e4SLinus Torvalds i2c_header = (struct ti_i2c_desc *)header; 8501da177e4SLinus Torvalds firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data; 8511da177e4SLinus Torvalds 8521da177e4SLinus Torvalds i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK; 853c03890ffSJohan Hovold i2c_header->Size = cpu_to_le16(buffer_size); 8541da177e4SLinus Torvalds i2c_header->CheckSum = cs; 855c0e34831SPeter E. Berger firmware_rec->Ver_Major = fw_hdr->major_version; 856c0e34831SPeter E. Berger firmware_rec->Ver_Minor = fw_hdr->minor_version; 8571da177e4SLinus Torvalds 8581da177e4SLinus Torvalds return 0; 8591da177e4SLinus Torvalds } 8601da177e4SLinus Torvalds 8611da177e4SLinus Torvalds /* Try to figure out what type of I2c we have */ 8622742fd88SAlan Cox static int i2c_type_bootmode(struct edgeport_serial *serial) 8631da177e4SLinus Torvalds { 86467e6da70SGreg Kroah-Hartman struct device *dev = &serial->serial->dev->dev; 8651da177e4SLinus Torvalds int status; 866e9305d2fSJohan Hovold u8 *data; 867e9305d2fSJohan Hovold 868e9305d2fSJohan Hovold data = kmalloc(1, GFP_KERNEL); 86910c642d0SJohan Hovold if (!data) 870e9305d2fSJohan Hovold return -ENOMEM; 8711da177e4SLinus Torvalds 8722742fd88SAlan Cox /* Try to read type 2 */ 8732742fd88SAlan Cox status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ, 874e9305d2fSJohan Hovold DTK_ADDR_SPACE_I2C_TYPE_II, 0, data, 0x01); 8751da177e4SLinus Torvalds if (status) 87667e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - read 2 status error = %d\n", __func__, status); 8771da177e4SLinus Torvalds else 87867e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data); 879e9305d2fSJohan Hovold if ((!status) && (*data == UMP5152 || *data == UMP3410)) { 88067e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - ROM_TYPE_II\n", __func__); 8811da177e4SLinus Torvalds serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II; 882e9305d2fSJohan Hovold goto out; 8831da177e4SLinus Torvalds } 8841da177e4SLinus Torvalds 8852742fd88SAlan Cox /* Try to read type 3 */ 8862742fd88SAlan Cox status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ, 887e9305d2fSJohan Hovold DTK_ADDR_SPACE_I2C_TYPE_III, 0, data, 0x01); 8881da177e4SLinus Torvalds if (status) 88967e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - read 3 status error = %d\n", __func__, status); 8901da177e4SLinus Torvalds else 89167e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data); 892e9305d2fSJohan Hovold if ((!status) && (*data == UMP5152 || *data == UMP3410)) { 89367e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - ROM_TYPE_III\n", __func__); 8941da177e4SLinus Torvalds serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III; 895e9305d2fSJohan Hovold goto out; 8961da177e4SLinus Torvalds } 8971da177e4SLinus Torvalds 89867e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - Unknown\n", __func__); 8991da177e4SLinus Torvalds serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II; 900e9305d2fSJohan Hovold status = -ENODEV; 901e9305d2fSJohan Hovold out: 902e9305d2fSJohan Hovold kfree(data); 903e9305d2fSJohan Hovold return status; 9041da177e4SLinus Torvalds } 9051da177e4SLinus Torvalds 9062742fd88SAlan Cox static int bulk_xfer(struct usb_serial *serial, void *buffer, 9072742fd88SAlan Cox int length, int *num_sent) 9081da177e4SLinus Torvalds { 9091da177e4SLinus Torvalds int status; 9101da177e4SLinus Torvalds 9111da177e4SLinus Torvalds status = usb_bulk_msg(serial->dev, 9121da177e4SLinus Torvalds usb_sndbulkpipe(serial->dev, 9131da177e4SLinus Torvalds serial->port[0]->bulk_out_endpointAddress), 9142742fd88SAlan Cox buffer, length, num_sent, 1000); 9151da177e4SLinus Torvalds return status; 9161da177e4SLinus Torvalds } 9171da177e4SLinus Torvalds 9181da177e4SLinus Torvalds /* Download given firmware image to the device (IN BOOT MODE) */ 9192742fd88SAlan Cox static int download_code(struct edgeport_serial *serial, __u8 *image, 9202742fd88SAlan Cox int image_length) 9211da177e4SLinus Torvalds { 9221da177e4SLinus Torvalds int status = 0; 9231da177e4SLinus Torvalds int pos; 9241da177e4SLinus Torvalds int transfer; 9251da177e4SLinus Torvalds int done; 9261da177e4SLinus Torvalds 9272742fd88SAlan Cox /* Transfer firmware image */ 9281da177e4SLinus Torvalds for (pos = 0; pos < image_length; ) { 9292742fd88SAlan Cox /* Read the next buffer from file */ 9301da177e4SLinus Torvalds transfer = image_length - pos; 9311da177e4SLinus Torvalds if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE) 9321da177e4SLinus Torvalds transfer = EDGE_FW_BULK_MAX_PACKET_SIZE; 9331da177e4SLinus Torvalds 9342742fd88SAlan Cox /* Transfer data */ 9352742fd88SAlan Cox status = bulk_xfer(serial->serial, &image[pos], 9362742fd88SAlan Cox transfer, &done); 9371da177e4SLinus Torvalds if (status) 9381da177e4SLinus Torvalds break; 9392742fd88SAlan Cox /* Advance buffer pointer */ 9401da177e4SLinus Torvalds pos += done; 9411da177e4SLinus Torvalds } 9421da177e4SLinus Torvalds 9431da177e4SLinus Torvalds return status; 9441da177e4SLinus Torvalds } 9451da177e4SLinus Torvalds 9462742fd88SAlan Cox /* FIXME!!! */ 9472742fd88SAlan Cox static int config_boot_dev(struct usb_device *dev) 9481da177e4SLinus Torvalds { 9491da177e4SLinus Torvalds return 0; 9501da177e4SLinus Torvalds } 9511da177e4SLinus Torvalds 9522742fd88SAlan Cox static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc) 9532742fd88SAlan Cox { 9542742fd88SAlan Cox return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev); 9552742fd88SAlan Cox } 9562742fd88SAlan Cox 957dcb8e99dSPeter E. Berger static int check_fw_sanity(struct edgeport_serial *serial, 958dcb8e99dSPeter E. Berger const struct firmware *fw) 959dcb8e99dSPeter E. Berger { 960dcb8e99dSPeter E. Berger u16 length_total; 961dcb8e99dSPeter E. Berger u8 checksum = 0; 962dcb8e99dSPeter E. Berger int pos; 963dcb8e99dSPeter E. Berger struct device *dev = &serial->serial->interface->dev; 964dcb8e99dSPeter E. Berger struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data; 965dcb8e99dSPeter E. Berger 966dcb8e99dSPeter E. Berger if (fw->size < sizeof(struct edgeport_fw_hdr)) { 967dcb8e99dSPeter E. Berger dev_err(dev, "incomplete fw header\n"); 968dcb8e99dSPeter E. Berger return -EINVAL; 969dcb8e99dSPeter E. Berger } 970dcb8e99dSPeter E. Berger 971dcb8e99dSPeter E. Berger length_total = le16_to_cpu(fw_hdr->length) + 972dcb8e99dSPeter E. Berger sizeof(struct edgeport_fw_hdr); 973dcb8e99dSPeter E. Berger 974dcb8e99dSPeter E. Berger if (fw->size != length_total) { 975dcb8e99dSPeter E. Berger dev_err(dev, "bad fw size (expected: %u, got: %zu)\n", 976dcb8e99dSPeter E. Berger length_total, fw->size); 977dcb8e99dSPeter E. Berger return -EINVAL; 978dcb8e99dSPeter E. Berger } 979dcb8e99dSPeter E. Berger 980dcb8e99dSPeter E. Berger for (pos = sizeof(struct edgeport_fw_hdr); pos < fw->size; ++pos) 981dcb8e99dSPeter E. Berger checksum += fw->data[pos]; 982dcb8e99dSPeter E. Berger 983dcb8e99dSPeter E. Berger if (checksum != fw_hdr->checksum) { 984dcb8e99dSPeter E. Berger dev_err(dev, "bad fw checksum (expected: 0x%x, got: 0x%x)\n", 985dcb8e99dSPeter E. Berger fw_hdr->checksum, checksum); 986dcb8e99dSPeter E. Berger return -EINVAL; 987dcb8e99dSPeter E. Berger } 988dcb8e99dSPeter E. Berger 989dcb8e99dSPeter E. Berger return 0; 990dcb8e99dSPeter E. Berger } 991dcb8e99dSPeter E. Berger 99275899513SPeter E. Berger /* 9931da177e4SLinus Torvalds * DownloadTIFirmware - Download run-time operating firmware to the TI5052 9941da177e4SLinus Torvalds * 9951da177e4SLinus Torvalds * This routine downloads the main operating code into the TI5052, using the 9961da177e4SLinus Torvalds * boot code already burned into E2PROM or ROM. 9971da177e4SLinus Torvalds */ 998b4146162SPeter E. Berger static int download_fw(struct edgeport_serial *serial) 9991da177e4SLinus Torvalds { 1000ef9324b2SPeter E. Berger struct device *dev = &serial->serial->interface->dev; 10011da177e4SLinus Torvalds int status = 0; 10021da177e4SLinus Torvalds struct usb_interface_descriptor *interface; 1003b4146162SPeter E. Berger const struct firmware *fw; 1004b4146162SPeter E. Berger const char *fw_name = "edgeport/down3.bin"; 1005b4146162SPeter E. Berger struct edgeport_fw_hdr *fw_hdr; 1006c0e34831SPeter E. Berger 1007b4146162SPeter E. Berger status = request_firmware(&fw, fw_name, dev); 1008b4146162SPeter E. Berger if (status) { 1009b4146162SPeter E. Berger dev_err(dev, "Failed to load image \"%s\" err %d\n", 1010b4146162SPeter E. Berger fw_name, status); 1011b4146162SPeter E. Berger return status; 1012b4146162SPeter E. Berger } 1013c0e34831SPeter E. Berger 1014b4146162SPeter E. Berger if (check_fw_sanity(serial, fw)) { 1015b4146162SPeter E. Berger status = -EINVAL; 1016b4146162SPeter E. Berger goto out; 1017b4146162SPeter E. Berger } 1018b4146162SPeter E. Berger 1019b4146162SPeter E. Berger fw_hdr = (struct edgeport_fw_hdr *)fw->data; 1020b4146162SPeter E. Berger 1021b4146162SPeter E. Berger /* If on-board version is newer, "fw_version" will be updated later. */ 1022c0e34831SPeter E. Berger serial->fw_version = (fw_hdr->major_version << 8) + 1023c0e34831SPeter E. Berger fw_hdr->minor_version; 10241da177e4SLinus Torvalds 102575899513SPeter E. Berger /* 102675899513SPeter E. Berger * This routine is entered by both the BOOT mode and the Download mode 10271da177e4SLinus Torvalds * We can determine which code is running by the reading the config 10281da177e4SLinus Torvalds * descriptor and if we have only one bulk pipe it is in boot mode 10291da177e4SLinus Torvalds */ 10301da177e4SLinus Torvalds serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP; 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds /* Default to type 2 i2c */ 10331da177e4SLinus Torvalds serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II; 10341da177e4SLinus Torvalds 10352742fd88SAlan Cox status = choose_config(serial->serial->dev); 10361da177e4SLinus Torvalds if (status) 1037b4146162SPeter E. Berger goto out; 10381da177e4SLinus Torvalds 10391da177e4SLinus Torvalds interface = &serial->serial->interface->cur_altsetting->desc; 10401da177e4SLinus Torvalds if (!interface) { 1041441b62c1SHarvey Harrison dev_err(dev, "%s - no interface set, error!\n", __func__); 1042b4146162SPeter E. Berger status = -ENODEV; 1043b4146162SPeter E. Berger goto out; 10441da177e4SLinus Torvalds } 10451da177e4SLinus Torvalds 10462742fd88SAlan Cox /* 10472742fd88SAlan Cox * Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING 10482742fd88SAlan Cox * if we have more than one endpoint we are definitely in download 10492742fd88SAlan Cox * mode 10502742fd88SAlan Cox */ 1051bebf1f18SPeter E. Berger if (interface->bNumEndpoints > 1) { 10521da177e4SLinus Torvalds serial->product_info.TiMode = TI_MODE_DOWNLOAD; 1053b4146162SPeter E. Berger status = do_download_mode(serial, fw); 1054b4146162SPeter E. Berger } else { 10552742fd88SAlan Cox /* Otherwise we will remain in configuring mode */ 10561da177e4SLinus Torvalds serial->product_info.TiMode = TI_MODE_CONFIGURING; 1057b4146162SPeter E. Berger status = do_boot_mode(serial, fw); 1058b4146162SPeter E. Berger } 10591da177e4SLinus Torvalds 1060b4146162SPeter E. Berger out: 1061b4146162SPeter E. Berger release_firmware(fw); 1062b4146162SPeter E. Berger return status; 1063bebf1f18SPeter E. Berger } 1064bebf1f18SPeter E. Berger 1065bebf1f18SPeter E. Berger static int do_download_mode(struct edgeport_serial *serial, 1066bebf1f18SPeter E. Berger const struct firmware *fw) 1067bebf1f18SPeter E. Berger { 1068bebf1f18SPeter E. Berger struct device *dev = &serial->serial->interface->dev; 1069bebf1f18SPeter E. Berger int status = 0; 1070bebf1f18SPeter E. Berger int start_address; 1071bebf1f18SPeter E. Berger struct edge_ti_manuf_descriptor *ti_manuf_desc; 1072bebf1f18SPeter E. Berger int download_cur_ver; 1073bebf1f18SPeter E. Berger int download_new_ver; 1074bebf1f18SPeter E. Berger struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data; 10751da177e4SLinus Torvalds struct ti_i2c_desc *rom_desc; 10761da177e4SLinus Torvalds 107767e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - RUNNING IN DOWNLOAD MODE\n", __func__); 10781da177e4SLinus Torvalds 10792742fd88SAlan Cox status = check_i2c_image(serial); 10801da177e4SLinus Torvalds if (status) { 108167e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - DOWNLOAD MODE -- BAD I2C\n", __func__); 10821da177e4SLinus Torvalds return status; 10831da177e4SLinus Torvalds } 10841da177e4SLinus Torvalds 108575899513SPeter E. Berger /* 108675899513SPeter E. Berger * Validate Hardware version number 10871da177e4SLinus Torvalds * Read Manufacturing Descriptor from TI Based Edgeport 10881da177e4SLinus Torvalds */ 10891da177e4SLinus Torvalds ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL); 109010c642d0SJohan Hovold if (!ti_manuf_desc) 10911da177e4SLinus Torvalds return -ENOMEM; 109210c642d0SJohan Hovold 10932742fd88SAlan Cox status = get_manuf_info(serial, (__u8 *)ti_manuf_desc); 10941da177e4SLinus Torvalds if (status) { 10951da177e4SLinus Torvalds kfree(ti_manuf_desc); 10961da177e4SLinus Torvalds return status; 10971da177e4SLinus Torvalds } 10981da177e4SLinus Torvalds 10992742fd88SAlan Cox /* Check version number of ION descriptor */ 11002742fd88SAlan Cox if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) { 110167e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n", 11022742fd88SAlan Cox __func__, ti_cpu_rev(ti_manuf_desc)); 11031da177e4SLinus Torvalds kfree(ti_manuf_desc); 11041da177e4SLinus Torvalds return -EINVAL; 11051da177e4SLinus Torvalds } 11061da177e4SLinus Torvalds 11071da177e4SLinus Torvalds rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL); 11081da177e4SLinus Torvalds if (!rom_desc) { 11091da177e4SLinus Torvalds kfree(ti_manuf_desc); 11101da177e4SLinus Torvalds return -ENOMEM; 11111da177e4SLinus Torvalds } 11121da177e4SLinus Torvalds 11132742fd88SAlan Cox /* Search for type 2 record (firmware record) */ 11142742fd88SAlan Cox start_address = get_descriptor_addr(serial, 11152742fd88SAlan Cox I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc); 11162742fd88SAlan Cox if (start_address != 0) { 11171da177e4SLinus Torvalds struct ti_i2c_firmware_rec *firmware_version; 1118e9305d2fSJohan Hovold u8 *record; 11191da177e4SLinus Torvalds 1120bebf1f18SPeter E. Berger dev_dbg(dev, "%s - Found Type FIRMWARE (Type 2) record\n", 1121bebf1f18SPeter E. Berger __func__); 11221da177e4SLinus Torvalds 11232742fd88SAlan Cox firmware_version = kmalloc(sizeof(*firmware_version), 11242742fd88SAlan Cox GFP_KERNEL); 11251da177e4SLinus Torvalds if (!firmware_version) { 11261da177e4SLinus Torvalds kfree(rom_desc); 11271da177e4SLinus Torvalds kfree(ti_manuf_desc); 11281da177e4SLinus Torvalds return -ENOMEM; 11291da177e4SLinus Torvalds } 11301da177e4SLinus Torvalds 113175899513SPeter E. Berger /* 113275899513SPeter E. Berger * Validate version number 11332742fd88SAlan Cox * Read the descriptor data 11342742fd88SAlan Cox */ 11352742fd88SAlan Cox status = read_rom(serial, start_address + 11362742fd88SAlan Cox sizeof(struct ti_i2c_desc), 11371da177e4SLinus Torvalds sizeof(struct ti_i2c_firmware_rec), 11381da177e4SLinus Torvalds (__u8 *)firmware_version); 11391da177e4SLinus Torvalds if (status) { 11401da177e4SLinus Torvalds kfree(firmware_version); 11411da177e4SLinus Torvalds kfree(rom_desc); 11421da177e4SLinus Torvalds kfree(ti_manuf_desc); 11431da177e4SLinus Torvalds return status; 11441da177e4SLinus Torvalds } 11451da177e4SLinus Torvalds 114675899513SPeter E. Berger /* 114775899513SPeter E. Berger * Check version number of download with current 114875899513SPeter E. Berger * version in I2c 114975899513SPeter E. Berger */ 11501da177e4SLinus Torvalds download_cur_ver = (firmware_version->Ver_Major << 8) + 11511da177e4SLinus Torvalds (firmware_version->Ver_Minor); 1152c0e34831SPeter E. Berger download_new_ver = (fw_hdr->major_version << 8) + 1153c0e34831SPeter E. Berger (fw_hdr->minor_version); 11541da177e4SLinus Torvalds 115567e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - >> FW Versions Device %d.%d Driver %d.%d\n", 115667e6da70SGreg Kroah-Hartman __func__, firmware_version->Ver_Major, 11571da177e4SLinus Torvalds firmware_version->Ver_Minor, 1158c0e34831SPeter E. Berger fw_hdr->major_version, fw_hdr->minor_version); 11591da177e4SLinus Torvalds 116075899513SPeter E. Berger /* 116175899513SPeter E. Berger * Check if we have an old version in the I2C and 116275899513SPeter E. Berger * update if necessary 116375899513SPeter E. Berger */ 11640827a9ffSGreg Kroah-Hartman if (download_cur_ver < download_new_ver) { 116567e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - Update I2C dld from %d.%d to %d.%d\n", 1166441b62c1SHarvey Harrison __func__, 11671da177e4SLinus Torvalds firmware_version->Ver_Major, 11681da177e4SLinus Torvalds firmware_version->Ver_Minor, 1169c0e34831SPeter E. Berger fw_hdr->major_version, 1170c0e34831SPeter E. Berger fw_hdr->minor_version); 11711da177e4SLinus Torvalds 1172e9305d2fSJohan Hovold record = kmalloc(1, GFP_KERNEL); 1173e9305d2fSJohan Hovold if (!record) { 1174e9305d2fSJohan Hovold kfree(firmware_version); 1175e9305d2fSJohan Hovold kfree(rom_desc); 1176e9305d2fSJohan Hovold kfree(ti_manuf_desc); 1177e9305d2fSJohan Hovold return -ENOMEM; 1178e9305d2fSJohan Hovold } 1179bebf1f18SPeter E. Berger /* 1180bebf1f18SPeter E. Berger * In order to update the I2C firmware we must 11812742fd88SAlan Cox * change the type 2 record to type 0xF2. This 11822742fd88SAlan Cox * will force the UMP to come up in Boot Mode. 11832742fd88SAlan Cox * Then while in boot mode, the driver will 11842742fd88SAlan Cox * download the latest firmware (padded to 11852742fd88SAlan Cox * 15.5k) into the UMP ram. Finally when the 11862742fd88SAlan Cox * device comes back up in download mode the 11872742fd88SAlan Cox * driver will cause the new firmware to be 11882742fd88SAlan Cox * copied from the UMP Ram to I2C and the 11892742fd88SAlan Cox * firmware will update the record type from 11902742fd88SAlan Cox * 0xf2 to 0x02. 11912742fd88SAlan Cox */ 1192e9305d2fSJohan Hovold *record = I2C_DESC_TYPE_FIRMWARE_BLANK; 11931da177e4SLinus Torvalds 119475899513SPeter E. Berger /* 119575899513SPeter E. Berger * Change the I2C Firmware record type to 119675899513SPeter E. Berger * 0xf2 to trigger an update 119775899513SPeter E. Berger */ 11982742fd88SAlan Cox status = write_rom(serial, start_address, 1199e9305d2fSJohan Hovold sizeof(*record), record); 12001da177e4SLinus Torvalds if (status) { 1201e9305d2fSJohan Hovold kfree(record); 12021da177e4SLinus Torvalds kfree(firmware_version); 12031da177e4SLinus Torvalds kfree(rom_desc); 12041da177e4SLinus Torvalds kfree(ti_manuf_desc); 12051da177e4SLinus Torvalds return status; 12061da177e4SLinus Torvalds } 12071da177e4SLinus Torvalds 120875899513SPeter E. Berger /* 120975899513SPeter E. Berger * verify the write -- must do this in order 12102742fd88SAlan Cox * for write to complete before we do the 12112742fd88SAlan Cox * hardware reset 12122742fd88SAlan Cox */ 12132742fd88SAlan Cox status = read_rom(serial, 12141da177e4SLinus Torvalds start_address, 1215e9305d2fSJohan Hovold sizeof(*record), 1216e9305d2fSJohan Hovold record); 12171da177e4SLinus Torvalds if (status) { 1218e9305d2fSJohan Hovold kfree(record); 12191da177e4SLinus Torvalds kfree(firmware_version); 12201da177e4SLinus Torvalds kfree(rom_desc); 12211da177e4SLinus Torvalds kfree(ti_manuf_desc); 12221da177e4SLinus Torvalds return status; 12231da177e4SLinus Torvalds } 12241da177e4SLinus Torvalds 1225e9305d2fSJohan Hovold if (*record != I2C_DESC_TYPE_FIRMWARE_BLANK) { 1226bebf1f18SPeter E. Berger dev_err(dev, "%s - error resetting device\n", 1227bebf1f18SPeter E. Berger __func__); 1228e9305d2fSJohan Hovold kfree(record); 12291da177e4SLinus Torvalds kfree(firmware_version); 12301da177e4SLinus Torvalds kfree(rom_desc); 12311da177e4SLinus Torvalds kfree(ti_manuf_desc); 12321da177e4SLinus Torvalds return -ENODEV; 12331da177e4SLinus Torvalds } 12341da177e4SLinus Torvalds 123567e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - HARDWARE RESET\n", __func__); 12361da177e4SLinus Torvalds 12372742fd88SAlan Cox /* Reset UMP -- Back to BOOT MODE */ 12382742fd88SAlan Cox status = ti_vsend_sync(serial->serial->dev, 12392742fd88SAlan Cox UMPC_HARDWARE_RESET, 1240c3ece7e7SPeter E. Berger 0, 0, NULL, 0, 1241c3ece7e7SPeter E. Berger TI_VSEND_TIMEOUT_DEFAULT); 12421da177e4SLinus Torvalds 1243bebf1f18SPeter E. Berger dev_dbg(dev, "%s - HARDWARE RESET return %d\n", 1244bebf1f18SPeter E. Berger __func__, status); 12451da177e4SLinus Torvalds 12461da177e4SLinus Torvalds /* return an error on purpose. */ 1247e9305d2fSJohan Hovold kfree(record); 12481da177e4SLinus Torvalds kfree(firmware_version); 12491da177e4SLinus Torvalds kfree(rom_desc); 12501da177e4SLinus Torvalds kfree(ti_manuf_desc); 12511da177e4SLinus Torvalds return -ENODEV; 1252bebf1f18SPeter E. Berger } 1253c0e34831SPeter E. Berger /* Same or newer fw version is already loaded */ 1254c0e34831SPeter E. Berger serial->fw_version = download_cur_ver; 12551da177e4SLinus Torvalds kfree(firmware_version); 12561da177e4SLinus Torvalds } 12572742fd88SAlan Cox /* Search for type 0xF2 record (firmware blank record) */ 1258bebf1f18SPeter E. Berger else { 1259bebf1f18SPeter E. Berger start_address = get_descriptor_addr(serial, 1260bebf1f18SPeter E. Berger I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc); 1261bebf1f18SPeter E. Berger if (start_address != 0) { 12622742fd88SAlan Cox #define HEADER_SIZE (sizeof(struct ti_i2c_desc) + \ 12632742fd88SAlan Cox sizeof(struct ti_i2c_firmware_rec)) 12641da177e4SLinus Torvalds __u8 *header; 12651da177e4SLinus Torvalds __u8 *vheader; 12661da177e4SLinus Torvalds 12671da177e4SLinus Torvalds header = kmalloc(HEADER_SIZE, GFP_KERNEL); 12681da177e4SLinus Torvalds if (!header) { 12691da177e4SLinus Torvalds kfree(rom_desc); 12701da177e4SLinus Torvalds kfree(ti_manuf_desc); 12711da177e4SLinus Torvalds return -ENOMEM; 12721da177e4SLinus Torvalds } 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds vheader = kmalloc(HEADER_SIZE, GFP_KERNEL); 12751da177e4SLinus Torvalds if (!vheader) { 12761da177e4SLinus Torvalds kfree(header); 12771da177e4SLinus Torvalds kfree(rom_desc); 12781da177e4SLinus Torvalds kfree(ti_manuf_desc); 12791da177e4SLinus Torvalds return -ENOMEM; 12801da177e4SLinus Torvalds } 12811da177e4SLinus Torvalds 1282bebf1f18SPeter E. Berger dev_dbg(dev, "%s - Found Type BLANK FIRMWARE (Type F2) record\n", 1283bebf1f18SPeter E. Berger __func__); 12841da177e4SLinus Torvalds 12852742fd88SAlan Cox /* 12862742fd88SAlan Cox * In order to update the I2C firmware we must change 12872742fd88SAlan Cox * the type 2 record to type 0xF2. This will force the 12882742fd88SAlan Cox * UMP to come up in Boot Mode. Then while in boot 12892742fd88SAlan Cox * mode, the driver will download the latest firmware 12902742fd88SAlan Cox * (padded to 15.5k) into the UMP ram. Finally when the 12912742fd88SAlan Cox * device comes back up in download mode the driver 12922742fd88SAlan Cox * will cause the new firmware to be copied from the 12932742fd88SAlan Cox * UMP Ram to I2C and the firmware will update the 12942742fd88SAlan Cox * record type from 0xf2 to 0x02. 12952742fd88SAlan Cox */ 1296277bf37bSPeter E. Berger status = build_i2c_fw_hdr(header, fw); 12971da177e4SLinus Torvalds if (status) { 12981da177e4SLinus Torvalds kfree(vheader); 12991da177e4SLinus Torvalds kfree(header); 13001da177e4SLinus Torvalds kfree(rom_desc); 13011da177e4SLinus Torvalds kfree(ti_manuf_desc); 1302fd6e5bbbSRoel Kluin return -EINVAL; 13031da177e4SLinus Torvalds } 13041da177e4SLinus Torvalds 130575899513SPeter E. Berger /* 130675899513SPeter E. Berger * Update I2C with type 0xf2 record with correct 130775899513SPeter E. Berger * size and checksum 130875899513SPeter E. Berger */ 13092742fd88SAlan Cox status = write_rom(serial, 13101da177e4SLinus Torvalds start_address, 13111da177e4SLinus Torvalds HEADER_SIZE, 13121da177e4SLinus Torvalds header); 13131da177e4SLinus Torvalds if (status) { 13141da177e4SLinus Torvalds kfree(vheader); 13151da177e4SLinus Torvalds kfree(header); 13161da177e4SLinus Torvalds kfree(rom_desc); 13171da177e4SLinus Torvalds kfree(ti_manuf_desc); 13189800eb33SRoel Kluin return -EINVAL; 13191da177e4SLinus Torvalds } 13201da177e4SLinus Torvalds 132175899513SPeter E. Berger /* 132275899513SPeter E. Berger * verify the write -- must do this in order for 132375899513SPeter E. Berger * write to complete before we do the hardware reset 132475899513SPeter E. Berger */ 13252742fd88SAlan Cox status = read_rom(serial, start_address, 13262742fd88SAlan Cox HEADER_SIZE, vheader); 13271da177e4SLinus Torvalds 13281da177e4SLinus Torvalds if (status) { 1329bebf1f18SPeter E. Berger dev_dbg(dev, "%s - can't read header back\n", 1330bebf1f18SPeter E. Berger __func__); 13311da177e4SLinus Torvalds kfree(vheader); 13321da177e4SLinus Torvalds kfree(header); 13331da177e4SLinus Torvalds kfree(rom_desc); 13341da177e4SLinus Torvalds kfree(ti_manuf_desc); 13351da177e4SLinus Torvalds return status; 13361da177e4SLinus Torvalds } 13371da177e4SLinus Torvalds if (memcmp(vheader, header, HEADER_SIZE)) { 1338bebf1f18SPeter E. Berger dev_dbg(dev, "%s - write download record failed\n", 1339bebf1f18SPeter E. Berger __func__); 13401da177e4SLinus Torvalds kfree(vheader); 13411da177e4SLinus Torvalds kfree(header); 13421da177e4SLinus Torvalds kfree(rom_desc); 13431da177e4SLinus Torvalds kfree(ti_manuf_desc); 13441e29709eSRoel Kluin return -EINVAL; 13451da177e4SLinus Torvalds } 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds kfree(vheader); 13481da177e4SLinus Torvalds kfree(header); 13491da177e4SLinus Torvalds 135067e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - Start firmware update\n", __func__); 13511da177e4SLinus Torvalds 13522742fd88SAlan Cox /* Tell firmware to copy download image into I2C */ 13532742fd88SAlan Cox status = ti_vsend_sync(serial->serial->dev, 1354c3ece7e7SPeter E. Berger UMPC_COPY_DNLD_TO_I2C, 1355c3ece7e7SPeter E. Berger 0, 0, NULL, 0, 1356c3ece7e7SPeter E. Berger TI_VSEND_TIMEOUT_FW_DOWNLOAD); 13571da177e4SLinus Torvalds 1358bebf1f18SPeter E. Berger dev_dbg(dev, "%s - Update complete 0x%x\n", __func__, 1359bebf1f18SPeter E. Berger status); 13601da177e4SLinus Torvalds if (status) { 13612742fd88SAlan Cox dev_err(dev, 13622742fd88SAlan Cox "%s - UMPC_COPY_DNLD_TO_I2C failed\n", 13632742fd88SAlan Cox __func__); 13641da177e4SLinus Torvalds kfree(rom_desc); 13651da177e4SLinus Torvalds kfree(ti_manuf_desc); 13661da177e4SLinus Torvalds return status; 13671da177e4SLinus Torvalds } 13681da177e4SLinus Torvalds } 1369bebf1f18SPeter E. Berger } 13701da177e4SLinus Torvalds 1371bebf1f18SPeter E. Berger /* The device is running the download code */ 13721da177e4SLinus Torvalds kfree(rom_desc); 13731da177e4SLinus Torvalds kfree(ti_manuf_desc); 13741da177e4SLinus Torvalds return 0; 13751da177e4SLinus Torvalds } 13761da177e4SLinus Torvalds 1377bebf1f18SPeter E. Berger static int do_boot_mode(struct edgeport_serial *serial, 1378bebf1f18SPeter E. Berger const struct firmware *fw) 1379bebf1f18SPeter E. Berger { 1380bebf1f18SPeter E. Berger struct device *dev = &serial->serial->interface->dev; 1381bebf1f18SPeter E. Berger int status = 0; 1382bebf1f18SPeter E. Berger struct edge_ti_manuf_descriptor *ti_manuf_desc; 1383bebf1f18SPeter E. Berger struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data; 1384bebf1f18SPeter E. Berger 138567e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - RUNNING IN BOOT MODE\n", __func__); 13861da177e4SLinus Torvalds 13872742fd88SAlan Cox /* Configure the TI device so we can use the BULK pipes for download */ 13882742fd88SAlan Cox status = config_boot_dev(serial->serial->dev); 13891da177e4SLinus Torvalds if (status) 13901da177e4SLinus Torvalds return status; 13911da177e4SLinus Torvalds 13922742fd88SAlan Cox if (le16_to_cpu(serial->serial->dev->descriptor.idVendor) 13932742fd88SAlan Cox != USB_VENDOR_ID_ION) { 139467e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - VID = 0x%x\n", __func__, 13951da177e4SLinus Torvalds le16_to_cpu(serial->serial->dev->descriptor.idVendor)); 13961da177e4SLinus Torvalds serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II; 13972742fd88SAlan Cox goto stayinbootmode; 13981da177e4SLinus Torvalds } 13991da177e4SLinus Torvalds 140075899513SPeter E. Berger /* 140175899513SPeter E. Berger * We have an ION device (I2c Must be programmed) 140275899513SPeter E. Berger * Determine I2C image type 140375899513SPeter E. Berger */ 14042742fd88SAlan Cox if (i2c_type_bootmode(serial)) 14052742fd88SAlan Cox goto stayinbootmode; 14061da177e4SLinus Torvalds 14072742fd88SAlan Cox /* Check for ION Vendor ID and that the I2C is valid */ 14082742fd88SAlan Cox if (!check_i2c_image(serial)) { 14091da177e4SLinus Torvalds struct ti_i2c_image_header *header; 14101da177e4SLinus Torvalds int i; 14111da177e4SLinus Torvalds __u8 cs = 0; 14121da177e4SLinus Torvalds __u8 *buffer; 14131da177e4SLinus Torvalds int buffer_size; 14141da177e4SLinus Torvalds 141575899513SPeter E. Berger /* 141675899513SPeter E. Berger * Validate Hardware version number 14171da177e4SLinus Torvalds * Read Manufacturing Descriptor from TI Based Edgeport 14181da177e4SLinus Torvalds */ 14191da177e4SLinus Torvalds ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL); 142010c642d0SJohan Hovold if (!ti_manuf_desc) 14211da177e4SLinus Torvalds return -ENOMEM; 142210c642d0SJohan Hovold 14232742fd88SAlan Cox status = get_manuf_info(serial, (__u8 *)ti_manuf_desc); 14241da177e4SLinus Torvalds if (status) { 14251da177e4SLinus Torvalds kfree(ti_manuf_desc); 14262742fd88SAlan Cox goto stayinbootmode; 14271da177e4SLinus Torvalds } 14281da177e4SLinus Torvalds 14292742fd88SAlan Cox /* Check for version 2 */ 14302742fd88SAlan Cox if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) { 143167e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n", 14322742fd88SAlan Cox __func__, ti_cpu_rev(ti_manuf_desc)); 14331da177e4SLinus Torvalds kfree(ti_manuf_desc); 14342742fd88SAlan Cox goto stayinbootmode; 14351da177e4SLinus Torvalds } 14361da177e4SLinus Torvalds 14371da177e4SLinus Torvalds kfree(ti_manuf_desc); 14381da177e4SLinus Torvalds 14391da177e4SLinus Torvalds /* 14402742fd88SAlan Cox * In order to update the I2C firmware we must change the type 14412742fd88SAlan Cox * 2 record to type 0xF2. This will force the UMP to come up 14422742fd88SAlan Cox * in Boot Mode. Then while in boot mode, the driver will 14432742fd88SAlan Cox * download the latest firmware (padded to 15.5k) into the 14442742fd88SAlan Cox * UMP ram. Finally when the device comes back up in download 14452742fd88SAlan Cox * mode the driver will cause the new firmware to be copied 14462742fd88SAlan Cox * from the UMP Ram to I2C and the firmware will update the 14472742fd88SAlan Cox * record type from 0xf2 to 0x02. 14482742fd88SAlan Cox * 14491da177e4SLinus Torvalds * Do we really have to copy the whole firmware image, 14501da177e4SLinus Torvalds * or could we do this in place! 14511da177e4SLinus Torvalds */ 14521da177e4SLinus Torvalds 14532742fd88SAlan Cox /* Allocate a 15.5k buffer + 3 byte header */ 14542742fd88SAlan Cox buffer_size = (((1024 * 16) - 512) + 14552742fd88SAlan Cox sizeof(struct ti_i2c_image_header)); 14561da177e4SLinus Torvalds buffer = kmalloc(buffer_size, GFP_KERNEL); 145710c642d0SJohan Hovold if (!buffer) 14581da177e4SLinus Torvalds return -ENOMEM; 14591da177e4SLinus Torvalds 14602742fd88SAlan Cox /* Initialize the buffer to 0xff (pad the buffer) */ 14611da177e4SLinus Torvalds memset(buffer, 0xff, buffer_size); 1462d12b219aSJaswinder Singh memcpy(buffer, &fw->data[4], fw->size - 4); 14631da177e4SLinus Torvalds 14642742fd88SAlan Cox for (i = sizeof(struct ti_i2c_image_header); 14652742fd88SAlan Cox i < buffer_size; i++) { 14661da177e4SLinus Torvalds cs = (__u8)(cs + buffer[i]); 14671da177e4SLinus Torvalds } 14681da177e4SLinus Torvalds 14691da177e4SLinus Torvalds header = (struct ti_i2c_image_header *)buffer; 14701da177e4SLinus Torvalds 14712742fd88SAlan Cox /* update length and checksum after padding */ 14722742fd88SAlan Cox header->Length = cpu_to_le16((__u16)(buffer_size - 14732742fd88SAlan Cox sizeof(struct ti_i2c_image_header))); 14741da177e4SLinus Torvalds header->CheckSum = cs; 14751da177e4SLinus Torvalds 14762742fd88SAlan Cox /* Download the operational code */ 1477c0e34831SPeter E. Berger dev_dbg(dev, "%s - Downloading operational code image version %d.%d (TI UMP)\n", 1478c0e34831SPeter E. Berger __func__, 1479c0e34831SPeter E. Berger fw_hdr->major_version, fw_hdr->minor_version); 14802742fd88SAlan Cox status = download_code(serial, buffer, buffer_size); 14811da177e4SLinus Torvalds 14821da177e4SLinus Torvalds kfree(buffer); 14831da177e4SLinus Torvalds 14841da177e4SLinus Torvalds if (status) { 148567e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - Error downloading operational code image\n", __func__); 14861da177e4SLinus Torvalds return status; 14871da177e4SLinus Torvalds } 14881da177e4SLinus Torvalds 14892742fd88SAlan Cox /* Device will reboot */ 14901da177e4SLinus Torvalds serial->product_info.TiMode = TI_MODE_TRANSITIONING; 14911da177e4SLinus Torvalds 149267e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - Download successful -- Device rebooting...\n", __func__); 14931da177e4SLinus Torvalds 1494e35d6d7cSJohan Hovold return 1; 14951da177e4SLinus Torvalds } 14961da177e4SLinus Torvalds 14972742fd88SAlan Cox stayinbootmode: 14982742fd88SAlan Cox /* Eprom is invalid or blank stay in boot mode */ 149967e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - STAYING IN BOOT MODE\n", __func__); 15001da177e4SLinus Torvalds serial->product_info.TiMode = TI_MODE_BOOT; 15011da177e4SLinus Torvalds 15024f9785ccSJohan Hovold return 1; 15031da177e4SLinus Torvalds } 15041da177e4SLinus Torvalds 15052742fd88SAlan Cox static int ti_do_config(struct edgeport_port *port, int feature, int on) 15061da177e4SLinus Torvalds { 15072742fd88SAlan Cox on = !!on; /* 1 or 0 not bitmask */ 15087a14fac0SJohan Hovold 15097a14fac0SJohan Hovold return send_port_cmd(port->port, feature, on, NULL, 0); 15101da177e4SLinus Torvalds } 15111da177e4SLinus Torvalds 15122742fd88SAlan Cox static int restore_mcr(struct edgeport_port *port, __u8 mcr) 15131da177e4SLinus Torvalds { 15141da177e4SLinus Torvalds int status = 0; 15151da177e4SLinus Torvalds 151667e6da70SGreg Kroah-Hartman dev_dbg(&port->port->dev, "%s - %x\n", __func__, mcr); 15171da177e4SLinus Torvalds 15182742fd88SAlan Cox status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR); 15191da177e4SLinus Torvalds if (status) 15201da177e4SLinus Torvalds return status; 15212742fd88SAlan Cox status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS); 15221da177e4SLinus Torvalds if (status) 15231da177e4SLinus Torvalds return status; 15242742fd88SAlan Cox return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK); 15251da177e4SLinus Torvalds } 15261da177e4SLinus Torvalds 15271da177e4SLinus Torvalds /* Convert TI LSR to standard UART flags */ 15282742fd88SAlan Cox static __u8 map_line_status(__u8 ti_lsr) 15291da177e4SLinus Torvalds { 15301da177e4SLinus Torvalds __u8 lsr = 0; 15311da177e4SLinus Torvalds 15321da177e4SLinus Torvalds #define MAP_FLAG(flagUmp, flagUart) \ 15331da177e4SLinus Torvalds if (ti_lsr & flagUmp) \ 15341da177e4SLinus Torvalds lsr |= flagUart; 15351da177e4SLinus Torvalds 15361da177e4SLinus Torvalds MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR) /* overrun */ 15371da177e4SLinus Torvalds MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */ 15381da177e4SLinus Torvalds MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */ 15391da177e4SLinus Torvalds MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */ 15402742fd88SAlan Cox MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* rx data available */ 15412742fd88SAlan Cox MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* tx hold reg empty */ 15421da177e4SLinus Torvalds 15431da177e4SLinus Torvalds #undef MAP_FLAG 15441da177e4SLinus Torvalds 15451da177e4SLinus Torvalds return lsr; 15461da177e4SLinus Torvalds } 15471da177e4SLinus Torvalds 15481da177e4SLinus Torvalds static void handle_new_msr(struct edgeport_port *edge_port, __u8 msr) 15491da177e4SLinus Torvalds { 15501da177e4SLinus Torvalds struct async_icount *icount; 15511da177e4SLinus Torvalds struct tty_struct *tty; 15521da177e4SLinus Torvalds 155367e6da70SGreg Kroah-Hartman dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, msr); 15541da177e4SLinus Torvalds 15552742fd88SAlan Cox if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR | 15562742fd88SAlan Cox EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) { 1557cf9a9d66SJohan Hovold icount = &edge_port->port->icount; 15581da177e4SLinus Torvalds 15591da177e4SLinus Torvalds /* update input line counters */ 15601da177e4SLinus Torvalds if (msr & EDGEPORT_MSR_DELTA_CTS) 15611da177e4SLinus Torvalds icount->cts++; 15621da177e4SLinus Torvalds if (msr & EDGEPORT_MSR_DELTA_DSR) 15631da177e4SLinus Torvalds icount->dsr++; 15641da177e4SLinus Torvalds if (msr & EDGEPORT_MSR_DELTA_CD) 15651da177e4SLinus Torvalds icount->dcd++; 15661da177e4SLinus Torvalds if (msr & EDGEPORT_MSR_DELTA_RI) 15671da177e4SLinus Torvalds icount->rng++; 156848ee5801SJohan Hovold wake_up_interruptible(&edge_port->port->port.delta_msr_wait); 15691da177e4SLinus Torvalds } 15701da177e4SLinus Torvalds 15711da177e4SLinus Torvalds /* Save the new modem status */ 15721da177e4SLinus Torvalds edge_port->shadow_msr = msr & 0xf0; 15731da177e4SLinus Torvalds 15744a90f09bSAlan Cox tty = tty_port_tty_get(&edge_port->port->port); 15751da177e4SLinus Torvalds /* handle CTS flow control */ 15761da177e4SLinus Torvalds if (tty && C_CRTSCTS(tty)) { 1577d95e3caeSPeter Hurley if (msr & EDGEPORT_MSR_CTS) 15781da177e4SLinus Torvalds tty_wakeup(tty); 15791da177e4SLinus Torvalds } 15804a90f09bSAlan Cox tty_kref_put(tty); 15811da177e4SLinus Torvalds } 15821da177e4SLinus Torvalds 15832742fd88SAlan Cox static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data, 15842742fd88SAlan Cox __u8 lsr, __u8 data) 15851da177e4SLinus Torvalds { 15861da177e4SLinus Torvalds struct async_icount *icount; 15872742fd88SAlan Cox __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR | 15882742fd88SAlan Cox LSR_FRM_ERR | LSR_BREAK)); 15891da177e4SLinus Torvalds 159067e6da70SGreg Kroah-Hartman dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, new_lsr); 15911da177e4SLinus Torvalds 15921da177e4SLinus Torvalds edge_port->shadow_lsr = lsr; 15931da177e4SLinus Torvalds 15942742fd88SAlan Cox if (new_lsr & LSR_BREAK) 15951da177e4SLinus Torvalds /* 15961da177e4SLinus Torvalds * Parity and Framing errors only count if they 15971da177e4SLinus Torvalds * occur exclusive of a break being received. 15981da177e4SLinus Torvalds */ 15991da177e4SLinus Torvalds new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK); 16001da177e4SLinus Torvalds 16011da177e4SLinus Torvalds /* Place LSR data byte into Rx buffer */ 16022e124b4aSJiri Slaby if (lsr_data) 16032e124b4aSJiri Slaby edge_tty_recv(edge_port->port, &data, 1); 16041da177e4SLinus Torvalds 16051da177e4SLinus Torvalds /* update input line counters */ 1606cf9a9d66SJohan Hovold icount = &edge_port->port->icount; 16071da177e4SLinus Torvalds if (new_lsr & LSR_BREAK) 16081da177e4SLinus Torvalds icount->brk++; 16091da177e4SLinus Torvalds if (new_lsr & LSR_OVER_ERR) 16101da177e4SLinus Torvalds icount->overrun++; 16111da177e4SLinus Torvalds if (new_lsr & LSR_PAR_ERR) 16121da177e4SLinus Torvalds icount->parity++; 16131da177e4SLinus Torvalds if (new_lsr & LSR_FRM_ERR) 16141da177e4SLinus Torvalds icount->frame++; 16151da177e4SLinus Torvalds } 16161da177e4SLinus Torvalds 16177d12e780SDavid Howells static void edge_interrupt_callback(struct urb *urb) 16181da177e4SLinus Torvalds { 1619cdc97792SMing Lei struct edgeport_serial *edge_serial = urb->context; 16201da177e4SLinus Torvalds struct usb_serial_port *port; 16211da177e4SLinus Torvalds struct edgeport_port *edge_port; 162267e6da70SGreg Kroah-Hartman struct device *dev; 16231da177e4SLinus Torvalds unsigned char *data = urb->transfer_buffer; 16241da177e4SLinus Torvalds int length = urb->actual_length; 16251da177e4SLinus Torvalds int port_number; 16261da177e4SLinus Torvalds int function; 1627ee337c21SGreg Kroah-Hartman int retval; 16281da177e4SLinus Torvalds __u8 lsr; 16291da177e4SLinus Torvalds __u8 msr; 1630ee337c21SGreg Kroah-Hartman int status = urb->status; 16311da177e4SLinus Torvalds 1632ee337c21SGreg Kroah-Hartman switch (status) { 16331da177e4SLinus Torvalds case 0: 16341da177e4SLinus Torvalds /* success */ 16351da177e4SLinus Torvalds break; 16361da177e4SLinus Torvalds case -ECONNRESET: 16371da177e4SLinus Torvalds case -ENOENT: 16381da177e4SLinus Torvalds case -ESHUTDOWN: 16391da177e4SLinus Torvalds /* this urb is terminated, clean up */ 164067e6da70SGreg Kroah-Hartman dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", 1641441b62c1SHarvey Harrison __func__, status); 16421da177e4SLinus Torvalds return; 16431da177e4SLinus Torvalds default: 1644ee337c21SGreg Kroah-Hartman dev_err(&urb->dev->dev, "%s - nonzero urb status received: " 1645441b62c1SHarvey Harrison "%d\n", __func__, status); 16461da177e4SLinus Torvalds goto exit; 16471da177e4SLinus Torvalds } 16481da177e4SLinus Torvalds 16491da177e4SLinus Torvalds if (!length) { 165067e6da70SGreg Kroah-Hartman dev_dbg(&urb->dev->dev, "%s - no data in urb\n", __func__); 16511da177e4SLinus Torvalds goto exit; 16521da177e4SLinus Torvalds } 16531da177e4SLinus Torvalds 165467e6da70SGreg Kroah-Hartman dev = &edge_serial->serial->dev->dev; 165559d33f2fSGreg Kroah-Hartman usb_serial_debug_data(dev, __func__, length, data); 16561da177e4SLinus Torvalds 16571da177e4SLinus Torvalds if (length != 2) { 165867e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - expecting packet of size 2, got %d\n", __func__, length); 16591da177e4SLinus Torvalds goto exit; 16601da177e4SLinus Torvalds } 16611da177e4SLinus Torvalds 16621da177e4SLinus Torvalds port_number = TIUMP_GET_PORT_FROM_CODE(data[0]); 16631da177e4SLinus Torvalds function = TIUMP_GET_FUNC_FROM_CODE(data[0]); 166467e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - port_number %d, function %d, info 0x%x\n", __func__, 166567e6da70SGreg Kroah-Hartman port_number, function, data[1]); 16660b1d250aSJohan Hovold 16670b1d250aSJohan Hovold if (port_number >= edge_serial->serial->num_ports) { 16680b1d250aSJohan Hovold dev_err(dev, "bad port number %d\n", port_number); 16690b1d250aSJohan Hovold goto exit; 16700b1d250aSJohan Hovold } 16710b1d250aSJohan Hovold 16721da177e4SLinus Torvalds port = edge_serial->serial->port[port_number]; 16731da177e4SLinus Torvalds edge_port = usb_get_serial_port_data(port); 16741da177e4SLinus Torvalds if (!edge_port) { 167567e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - edge_port not found\n", __func__); 16761da177e4SLinus Torvalds return; 16771da177e4SLinus Torvalds } 16781da177e4SLinus Torvalds switch (function) { 16791da177e4SLinus Torvalds case TIUMP_INTERRUPT_CODE_LSR: 16802742fd88SAlan Cox lsr = map_line_status(data[1]); 16811da177e4SLinus Torvalds if (lsr & UMP_UART_LSR_DATA_MASK) { 168275899513SPeter E. Berger /* 168375899513SPeter E. Berger * Save the LSR event for bulk read completion routine 168475899513SPeter E. Berger */ 168567e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - LSR Event Port %u LSR Status = %02x\n", 1686441b62c1SHarvey Harrison __func__, port_number, lsr); 16871da177e4SLinus Torvalds edge_port->lsr_event = 1; 16881da177e4SLinus Torvalds edge_port->lsr_mask = lsr; 16891da177e4SLinus Torvalds } else { 169067e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - ===== Port %d LSR Status = %02x ======\n", 1691441b62c1SHarvey Harrison __func__, port_number, lsr); 16921da177e4SLinus Torvalds handle_new_lsr(edge_port, 0, lsr, 0); 16931da177e4SLinus Torvalds } 16941da177e4SLinus Torvalds break; 16951da177e4SLinus Torvalds 16962742fd88SAlan Cox case TIUMP_INTERRUPT_CODE_MSR: /* MSR */ 16971da177e4SLinus Torvalds /* Copy MSR from UMP */ 16981da177e4SLinus Torvalds msr = data[1]; 169967e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - ===== Port %u MSR Status = %02x ======\n", 1700441b62c1SHarvey Harrison __func__, port_number, msr); 17011da177e4SLinus Torvalds handle_new_msr(edge_port, msr); 17021da177e4SLinus Torvalds break; 17031da177e4SLinus Torvalds 17041da177e4SLinus Torvalds default: 17052742fd88SAlan Cox dev_err(&urb->dev->dev, 17062742fd88SAlan Cox "%s - Unknown Interrupt code from UMP %x\n", 1707441b62c1SHarvey Harrison __func__, data[1]); 17081da177e4SLinus Torvalds break; 17091da177e4SLinus Torvalds 17101da177e4SLinus Torvalds } 17111da177e4SLinus Torvalds 17121da177e4SLinus Torvalds exit: 1713ee337c21SGreg Kroah-Hartman retval = usb_submit_urb(urb, GFP_ATOMIC); 1714ee337c21SGreg Kroah-Hartman if (retval) 17152742fd88SAlan Cox dev_err(&urb->dev->dev, 17162742fd88SAlan Cox "%s - usb_submit_urb failed with result %d\n", 1717441b62c1SHarvey Harrison __func__, retval); 17181da177e4SLinus Torvalds } 17191da177e4SLinus Torvalds 17207d12e780SDavid Howells static void edge_bulk_in_callback(struct urb *urb) 17211da177e4SLinus Torvalds { 1722cdc97792SMing Lei struct edgeport_port *edge_port = urb->context; 172367e6da70SGreg Kroah-Hartman struct device *dev = &edge_port->port->dev; 17241da177e4SLinus Torvalds unsigned char *data = urb->transfer_buffer; 17256778b0cbSJohn Ogness unsigned long flags; 1726ee337c21SGreg Kroah-Hartman int retval = 0; 17271da177e4SLinus Torvalds int port_number; 1728ee337c21SGreg Kroah-Hartman int status = urb->status; 17291da177e4SLinus Torvalds 1730ee337c21SGreg Kroah-Hartman switch (status) { 17311da177e4SLinus Torvalds case 0: 17321da177e4SLinus Torvalds /* success */ 17331da177e4SLinus Torvalds break; 17341da177e4SLinus Torvalds case -ECONNRESET: 17351da177e4SLinus Torvalds case -ENOENT: 17361da177e4SLinus Torvalds case -ESHUTDOWN: 17371da177e4SLinus Torvalds /* this urb is terminated, clean up */ 173867e6da70SGreg Kroah-Hartman dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status); 17391da177e4SLinus Torvalds return; 17401da177e4SLinus Torvalds default: 174167e6da70SGreg Kroah-Hartman dev_err(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n", __func__, status); 17421da177e4SLinus Torvalds } 17431da177e4SLinus Torvalds 1744ee337c21SGreg Kroah-Hartman if (status == -EPIPE) 17451da177e4SLinus Torvalds goto exit; 17461da177e4SLinus Torvalds 1747ee337c21SGreg Kroah-Hartman if (status) { 1748441b62c1SHarvey Harrison dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__); 17491da177e4SLinus Torvalds return; 17501da177e4SLinus Torvalds } 17511da177e4SLinus Torvalds 17521143832eSGreg Kroah-Hartman port_number = edge_port->port->port_number; 17531da177e4SLinus Torvalds 1754654b404fSJohan Hovold if (urb->actual_length > 0 && edge_port->lsr_event) { 17551da177e4SLinus Torvalds edge_port->lsr_event = 0; 175667e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s ===== Port %u LSR Status = %02x, Data = %02x ======\n", 1757441b62c1SHarvey Harrison __func__, port_number, edge_port->lsr_mask, *data); 17581da177e4SLinus Torvalds handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data); 17591da177e4SLinus Torvalds /* Adjust buffer length/pointer */ 17601da177e4SLinus Torvalds --urb->actual_length; 17611da177e4SLinus Torvalds ++data; 17621da177e4SLinus Torvalds } 17631da177e4SLinus Torvalds 17642e124b4aSJiri Slaby if (urb->actual_length) { 176559d33f2fSGreg Kroah-Hartman usb_serial_debug_data(dev, __func__, urb->actual_length, data); 17662742fd88SAlan Cox if (edge_port->close_pending) 176767e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - close pending, dropping data on the floor\n", 17682742fd88SAlan Cox __func__); 17692742fd88SAlan Cox else 17702e124b4aSJiri Slaby edge_tty_recv(edge_port->port, data, 177105c7cd39SJiri Slaby urb->actual_length); 1772cf9a9d66SJohan Hovold edge_port->port->icount.rx += urb->actual_length; 17731da177e4SLinus Torvalds } 17741da177e4SLinus Torvalds 17751da177e4SLinus Torvalds exit: 17761da177e4SLinus Torvalds /* continue read unless stopped */ 17776778b0cbSJohn Ogness spin_lock_irqsave(&edge_port->ep_lock, flags); 17785833041fSJohan Hovold if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING) 1779ee337c21SGreg Kroah-Hartman retval = usb_submit_urb(urb, GFP_ATOMIC); 17805833041fSJohan Hovold else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING) 17811da177e4SLinus Torvalds edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED; 17825833041fSJohan Hovold 17836778b0cbSJohn Ogness spin_unlock_irqrestore(&edge_port->ep_lock, flags); 1784ee337c21SGreg Kroah-Hartman if (retval) 178567e6da70SGreg Kroah-Hartman dev_err(dev, "%s - usb_submit_urb failed with result %d\n", __func__, retval); 17861da177e4SLinus Torvalds } 17871da177e4SLinus Torvalds 17882e124b4aSJiri Slaby static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data, 17892e124b4aSJiri Slaby int length) 17901da177e4SLinus Torvalds { 17912742fd88SAlan Cox int queued; 17921da177e4SLinus Torvalds 179305c7cd39SJiri Slaby queued = tty_insert_flip_string(&port->port, data, length); 17942742fd88SAlan Cox if (queued < length) 179505c7cd39SJiri Slaby dev_err(&port->dev, "%s - dropping data, %d bytes lost\n", 17962742fd88SAlan Cox __func__, length - queued); 17972e124b4aSJiri Slaby tty_flip_buffer_push(&port->port); 17981da177e4SLinus Torvalds } 17991da177e4SLinus Torvalds 18007d12e780SDavid Howells static void edge_bulk_out_callback(struct urb *urb) 18011da177e4SLinus Torvalds { 1802cdc97792SMing Lei struct usb_serial_port *port = urb->context; 18031da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 1804ee337c21SGreg Kroah-Hartman int status = urb->status; 18054a90f09bSAlan Cox struct tty_struct *tty; 18061da177e4SLinus Torvalds 18071da177e4SLinus Torvalds edge_port->ep_write_urb_in_use = 0; 18081da177e4SLinus Torvalds 1809ee337c21SGreg Kroah-Hartman switch (status) { 18101da177e4SLinus Torvalds case 0: 18111da177e4SLinus Torvalds /* success */ 18121da177e4SLinus Torvalds break; 18131da177e4SLinus Torvalds case -ECONNRESET: 18141da177e4SLinus Torvalds case -ENOENT: 18151da177e4SLinus Torvalds case -ESHUTDOWN: 18161da177e4SLinus Torvalds /* this urb is terminated, clean up */ 181767e6da70SGreg Kroah-Hartman dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", 1818441b62c1SHarvey Harrison __func__, status); 18191da177e4SLinus Torvalds return; 18201da177e4SLinus Torvalds default: 182122a416c4SJohan Hovold dev_err_console(port, "%s - nonzero write bulk status " 1822441b62c1SHarvey Harrison "received: %d\n", __func__, status); 18231da177e4SLinus Torvalds } 18241da177e4SLinus Torvalds 18251da177e4SLinus Torvalds /* send any buffered data */ 18264a90f09bSAlan Cox tty = tty_port_tty_get(&port->port); 1827ae3759c2SJiri Slaby edge_send(port, tty); 18284a90f09bSAlan Cox tty_kref_put(tty); 18291da177e4SLinus Torvalds } 18301da177e4SLinus Torvalds 1831a509a7e4SAlan Cox static int edge_open(struct tty_struct *tty, struct usb_serial_port *port) 18321da177e4SLinus Torvalds { 18331da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 18341da177e4SLinus Torvalds struct edgeport_serial *edge_serial; 18351da177e4SLinus Torvalds struct usb_device *dev; 18361da177e4SLinus Torvalds struct urb *urb; 18371da177e4SLinus Torvalds int status; 18381da177e4SLinus Torvalds u16 open_settings; 18391da177e4SLinus Torvalds u8 transaction_timeout; 18401da177e4SLinus Torvalds 18411da177e4SLinus Torvalds if (edge_port == NULL) 18421da177e4SLinus Torvalds return -ENODEV; 18431da177e4SLinus Torvalds 18441da177e4SLinus Torvalds dev = port->serial->dev; 18451da177e4SLinus Torvalds 18461da177e4SLinus Torvalds /* turn off loopback */ 18472742fd88SAlan Cox status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0); 18481da177e4SLinus Torvalds if (status) { 18492742fd88SAlan Cox dev_err(&port->dev, 18502742fd88SAlan Cox "%s - cannot send clear loopback command, %d\n", 1851441b62c1SHarvey Harrison __func__, status); 18521da177e4SLinus Torvalds return status; 18531da177e4SLinus Torvalds } 18541da177e4SLinus Torvalds 18551da177e4SLinus Torvalds /* set up the port settings */ 185695da310eSAlan Cox if (tty) 1857adc8d746SAlan Cox edge_set_termios(tty, port, &tty->termios); 18581da177e4SLinus Torvalds 18591da177e4SLinus Torvalds /* open up the port */ 18601da177e4SLinus Torvalds 18611da177e4SLinus Torvalds /* milliseconds to timeout for DMA transfer */ 18621da177e4SLinus Torvalds transaction_timeout = 2; 18631da177e4SLinus Torvalds 18642742fd88SAlan Cox edge_port->ump_read_timeout = 18652742fd88SAlan Cox max(20, ((transaction_timeout * 3) / 2)); 18661da177e4SLinus Torvalds 18672742fd88SAlan Cox /* milliseconds to timeout for DMA transfer */ 18681da177e4SLinus Torvalds open_settings = (u8)(UMP_DMA_MODE_CONTINOUS | 18691da177e4SLinus Torvalds UMP_PIPE_TRANS_TIMEOUT_ENA | 18701da177e4SLinus Torvalds (transaction_timeout << 2)); 18711da177e4SLinus Torvalds 187267e6da70SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - Sending UMPC_OPEN_PORT\n", __func__); 18731da177e4SLinus Torvalds 18741da177e4SLinus Torvalds /* Tell TI to open and start the port */ 18757a14fac0SJohan Hovold status = send_port_cmd(port, UMPC_OPEN_PORT, open_settings, NULL, 0); 18761da177e4SLinus Torvalds if (status) { 18772742fd88SAlan Cox dev_err(&port->dev, "%s - cannot send open command, %d\n", 18782742fd88SAlan Cox __func__, status); 18791da177e4SLinus Torvalds return status; 18801da177e4SLinus Torvalds } 18811da177e4SLinus Torvalds 18821da177e4SLinus Torvalds /* Start the DMA? */ 18837a14fac0SJohan Hovold status = send_port_cmd(port, UMPC_START_PORT, 0, NULL, 0); 18841da177e4SLinus Torvalds if (status) { 18852742fd88SAlan Cox dev_err(&port->dev, "%s - cannot send start DMA command, %d\n", 18862742fd88SAlan Cox __func__, status); 18871da177e4SLinus Torvalds return status; 18881da177e4SLinus Torvalds } 18891da177e4SLinus Torvalds 18901da177e4SLinus Torvalds /* Clear TX and RX buffers in UMP */ 18912742fd88SAlan Cox status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN); 18921da177e4SLinus Torvalds if (status) { 18932742fd88SAlan Cox dev_err(&port->dev, 18942742fd88SAlan Cox "%s - cannot send clear buffers command, %d\n", 18952742fd88SAlan Cox __func__, status); 18961da177e4SLinus Torvalds return status; 18971da177e4SLinus Torvalds } 18981da177e4SLinus Torvalds 18991da177e4SLinus Torvalds /* Read Initial MSR */ 1900*13c61339SJohan Hovold status = read_port_cmd(port, UMPC_READ_MSR, 0, &edge_port->shadow_msr, 1); 19011da177e4SLinus Torvalds if (status) { 19022742fd88SAlan Cox dev_err(&port->dev, "%s - cannot send read MSR command, %d\n", 19032742fd88SAlan Cox __func__, status); 19041da177e4SLinus Torvalds return status; 19051da177e4SLinus Torvalds } 19061da177e4SLinus Torvalds 190767e6da70SGreg Kroah-Hartman dev_dbg(&port->dev, "ShadowMSR 0x%X\n", edge_port->shadow_msr); 19081da177e4SLinus Torvalds 19091da177e4SLinus Torvalds /* Set Initial MCR */ 19101da177e4SLinus Torvalds edge_port->shadow_mcr = MCR_RTS | MCR_DTR; 191167e6da70SGreg Kroah-Hartman dev_dbg(&port->dev, "ShadowMCR 0x%X\n", edge_port->shadow_mcr); 19121da177e4SLinus Torvalds 19131da177e4SLinus Torvalds edge_serial = edge_port->edge_serial; 1914241ca64fSMatthias Kaehlcke if (mutex_lock_interruptible(&edge_serial->es_lock)) 19151da177e4SLinus Torvalds return -ERESTARTSYS; 19161da177e4SLinus Torvalds if (edge_serial->num_ports_open == 0) { 19172742fd88SAlan Cox /* we are the first port to open, post the interrupt urb */ 19181da177e4SLinus Torvalds urb = edge_serial->serial->port[0]->interrupt_in_urb; 19191da177e4SLinus Torvalds urb->context = edge_serial; 19201da177e4SLinus Torvalds status = usb_submit_urb(urb, GFP_KERNEL); 19211da177e4SLinus Torvalds if (status) { 19222742fd88SAlan Cox dev_err(&port->dev, 19232742fd88SAlan Cox "%s - usb_submit_urb failed with value %d\n", 19242742fd88SAlan Cox __func__, status); 1925241ca64fSMatthias Kaehlcke goto release_es_lock; 19261da177e4SLinus Torvalds } 19271da177e4SLinus Torvalds } 19281da177e4SLinus Torvalds 19291da177e4SLinus Torvalds /* 19301da177e4SLinus Torvalds * reset the data toggle on the bulk endpoints to work around bug in 19311da177e4SLinus Torvalds * host controllers where things get out of sync some times 19321da177e4SLinus Torvalds */ 19331da177e4SLinus Torvalds usb_clear_halt(dev, port->write_urb->pipe); 19341da177e4SLinus Torvalds usb_clear_halt(dev, port->read_urb->pipe); 19351da177e4SLinus Torvalds 19361da177e4SLinus Torvalds /* start up our bulk read urb */ 19371da177e4SLinus Torvalds urb = port->read_urb; 19381da177e4SLinus Torvalds edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING; 19391da177e4SLinus Torvalds urb->context = edge_port; 19401da177e4SLinus Torvalds status = usb_submit_urb(urb, GFP_KERNEL); 19411da177e4SLinus Torvalds if (status) { 19422742fd88SAlan Cox dev_err(&port->dev, 19432742fd88SAlan Cox "%s - read bulk usb_submit_urb failed with value %d\n", 19442742fd88SAlan Cox __func__, status); 19451da177e4SLinus Torvalds goto unlink_int_urb; 19461da177e4SLinus Torvalds } 19471da177e4SLinus Torvalds 19481da177e4SLinus Torvalds ++edge_serial->num_ports_open; 19491da177e4SLinus Torvalds 1950241ca64fSMatthias Kaehlcke goto release_es_lock; 19511da177e4SLinus Torvalds 19521da177e4SLinus Torvalds unlink_int_urb: 19531da177e4SLinus Torvalds if (edge_port->edge_serial->num_ports_open == 0) 19541da177e4SLinus Torvalds usb_kill_urb(port->serial->port[0]->interrupt_in_urb); 1955241ca64fSMatthias Kaehlcke release_es_lock: 1956241ca64fSMatthias Kaehlcke mutex_unlock(&edge_serial->es_lock); 19571da177e4SLinus Torvalds return status; 19581da177e4SLinus Torvalds } 19591da177e4SLinus Torvalds 1960335f8514SAlan Cox static void edge_close(struct usb_serial_port *port) 19611da177e4SLinus Torvalds { 19621da177e4SLinus Torvalds struct edgeport_serial *edge_serial; 19631da177e4SLinus Torvalds struct edgeport_port *edge_port; 196477de2518SJohan Hovold unsigned long flags; 19651da177e4SLinus Torvalds 19661da177e4SLinus Torvalds edge_serial = usb_get_serial_data(port->serial); 19671da177e4SLinus Torvalds edge_port = usb_get_serial_port_data(port); 19682742fd88SAlan Cox if (edge_serial == NULL || edge_port == NULL) 19691da177e4SLinus Torvalds return; 19701da177e4SLinus Torvalds 197175899513SPeter E. Berger /* 197275899513SPeter E. Berger * The bulkreadcompletion routine will check 197375899513SPeter E. Berger * this flag and dump add read data 197475899513SPeter E. Berger */ 19751da177e4SLinus Torvalds edge_port->close_pending = 1; 19761da177e4SLinus Torvalds 19771da177e4SLinus Torvalds usb_kill_urb(port->read_urb); 19781da177e4SLinus Torvalds usb_kill_urb(port->write_urb); 19791da177e4SLinus Torvalds edge_port->ep_write_urb_in_use = 0; 198077de2518SJohan Hovold spin_lock_irqsave(&edge_port->ep_lock, flags); 1981ddca16e4SJohan Hovold kfifo_reset_out(&port->write_fifo); 198277de2518SJohan Hovold spin_unlock_irqrestore(&edge_port->ep_lock, flags); 19831da177e4SLinus Torvalds 198467e6da70SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - send umpc_close_port\n", __func__); 19857a14fac0SJohan Hovold send_port_cmd(port, UMPC_CLOSE_PORT, 0, NULL, 0); 1986af581059SJohan Hovold 1987241ca64fSMatthias Kaehlcke mutex_lock(&edge_serial->es_lock); 19881da177e4SLinus Torvalds --edge_port->edge_serial->num_ports_open; 19891da177e4SLinus Torvalds if (edge_port->edge_serial->num_ports_open <= 0) { 19901da177e4SLinus Torvalds /* last port is now closed, let's shut down our interrupt urb */ 19911da177e4SLinus Torvalds usb_kill_urb(port->serial->port[0]->interrupt_in_urb); 19921da177e4SLinus Torvalds edge_port->edge_serial->num_ports_open = 0; 19931da177e4SLinus Torvalds } 1994241ca64fSMatthias Kaehlcke mutex_unlock(&edge_serial->es_lock); 19951da177e4SLinus Torvalds edge_port->close_pending = 0; 19961da177e4SLinus Torvalds } 19971da177e4SLinus Torvalds 199895da310eSAlan Cox static int edge_write(struct tty_struct *tty, struct usb_serial_port *port, 199995da310eSAlan Cox const unsigned char *data, int count) 20001da177e4SLinus Torvalds { 20011da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 20021da177e4SLinus Torvalds 20031da177e4SLinus Torvalds if (count == 0) { 200467e6da70SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__); 20051da177e4SLinus Torvalds return 0; 20061da177e4SLinus Torvalds } 20071da177e4SLinus Torvalds 20081da177e4SLinus Torvalds if (edge_port == NULL) 20091da177e4SLinus Torvalds return -ENODEV; 20101da177e4SLinus Torvalds if (edge_port->close_pending == 1) 20111da177e4SLinus Torvalds return -ENODEV; 20121da177e4SLinus Torvalds 2013ddca16e4SJohan Hovold count = kfifo_in_locked(&port->write_fifo, data, count, 2014d733cec1SJohan Hovold &edge_port->ep_lock); 2015ae3759c2SJiri Slaby edge_send(port, tty); 20161da177e4SLinus Torvalds 20171da177e4SLinus Torvalds return count; 20181da177e4SLinus Torvalds } 20191da177e4SLinus Torvalds 2020ae3759c2SJiri Slaby static void edge_send(struct usb_serial_port *port, struct tty_struct *tty) 20211da177e4SLinus Torvalds { 20221da177e4SLinus Torvalds int count, result; 20231da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 20241da177e4SLinus Torvalds unsigned long flags; 20251da177e4SLinus Torvalds 20261da177e4SLinus Torvalds spin_lock_irqsave(&edge_port->ep_lock, flags); 20271da177e4SLinus Torvalds 20281da177e4SLinus Torvalds if (edge_port->ep_write_urb_in_use) { 20291da177e4SLinus Torvalds spin_unlock_irqrestore(&edge_port->ep_lock, flags); 20301da177e4SLinus Torvalds return; 20311da177e4SLinus Torvalds } 20321da177e4SLinus Torvalds 2033ddca16e4SJohan Hovold count = kfifo_out(&port->write_fifo, 20341da177e4SLinus Torvalds port->write_urb->transfer_buffer, 20351da177e4SLinus Torvalds port->bulk_out_size); 20361da177e4SLinus Torvalds 20371da177e4SLinus Torvalds if (count == 0) { 20381da177e4SLinus Torvalds spin_unlock_irqrestore(&edge_port->ep_lock, flags); 20391da177e4SLinus Torvalds return; 20401da177e4SLinus Torvalds } 20411da177e4SLinus Torvalds 20421da177e4SLinus Torvalds edge_port->ep_write_urb_in_use = 1; 20431da177e4SLinus Torvalds 20441da177e4SLinus Torvalds spin_unlock_irqrestore(&edge_port->ep_lock, flags); 20451da177e4SLinus Torvalds 204659d33f2fSGreg Kroah-Hartman usb_serial_debug_data(&port->dev, __func__, count, port->write_urb->transfer_buffer); 20471da177e4SLinus Torvalds 20481da177e4SLinus Torvalds /* set up our urb */ 2049fd11961aSJohan Hovold port->write_urb->transfer_buffer_length = count; 20501da177e4SLinus Torvalds 20511da177e4SLinus Torvalds /* send the data out the bulk port */ 20521da177e4SLinus Torvalds result = usb_submit_urb(port->write_urb, GFP_ATOMIC); 20531da177e4SLinus Torvalds if (result) { 205422a416c4SJohan Hovold dev_err_console(port, 20552742fd88SAlan Cox "%s - failed submitting write urb, error %d\n", 20562742fd88SAlan Cox __func__, result); 20571da177e4SLinus Torvalds edge_port->ep_write_urb_in_use = 0; 20582742fd88SAlan Cox /* TODO: reschedule edge_send */ 20592742fd88SAlan Cox } else 2060cf9a9d66SJohan Hovold edge_port->port->icount.tx += count; 20611da177e4SLinus Torvalds 206275899513SPeter E. Berger /* 206375899513SPeter E. Berger * wakeup any process waiting for writes to complete 206475899513SPeter E. Berger * there is now more room in the buffer for new writes 206575899513SPeter E. Berger */ 20662742fd88SAlan Cox if (tty) 20671da177e4SLinus Torvalds tty_wakeup(tty); 20681da177e4SLinus Torvalds } 20691da177e4SLinus Torvalds 207095da310eSAlan Cox static int edge_write_room(struct tty_struct *tty) 20711da177e4SLinus Torvalds { 207295da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 20731da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 20741da177e4SLinus Torvalds int room = 0; 20751da177e4SLinus Torvalds unsigned long flags; 20761da177e4SLinus Torvalds 20771da177e4SLinus Torvalds if (edge_port == NULL) 20782742fd88SAlan Cox return 0; 20791da177e4SLinus Torvalds if (edge_port->close_pending == 1) 20802742fd88SAlan Cox return 0; 20811da177e4SLinus Torvalds 20821da177e4SLinus Torvalds spin_lock_irqsave(&edge_port->ep_lock, flags); 2083ddca16e4SJohan Hovold room = kfifo_avail(&port->write_fifo); 20841da177e4SLinus Torvalds spin_unlock_irqrestore(&edge_port->ep_lock, flags); 20851da177e4SLinus Torvalds 208667e6da70SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - returns %d\n", __func__, room); 20871da177e4SLinus Torvalds return room; 20881da177e4SLinus Torvalds } 20891da177e4SLinus Torvalds 209095da310eSAlan Cox static int edge_chars_in_buffer(struct tty_struct *tty) 20911da177e4SLinus Torvalds { 209295da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 20931da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 20941da177e4SLinus Torvalds int chars = 0; 20951da177e4SLinus Torvalds unsigned long flags; 20961da177e4SLinus Torvalds if (edge_port == NULL) 20972742fd88SAlan Cox return 0; 20981da177e4SLinus Torvalds 20991da177e4SLinus Torvalds spin_lock_irqsave(&edge_port->ep_lock, flags); 2100ddca16e4SJohan Hovold chars = kfifo_len(&port->write_fifo); 21011da177e4SLinus Torvalds spin_unlock_irqrestore(&edge_port->ep_lock, flags); 21021da177e4SLinus Torvalds 210367e6da70SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars); 21041da177e4SLinus Torvalds return chars; 21051da177e4SLinus Torvalds } 21061da177e4SLinus Torvalds 2107b16634adSJohan Hovold static bool edge_tx_empty(struct usb_serial_port *port) 2108b16634adSJohan Hovold { 2109b16634adSJohan Hovold struct edgeport_port *edge_port = usb_get_serial_port_data(port); 2110b16634adSJohan Hovold int ret; 2111b16634adSJohan Hovold 2112b16634adSJohan Hovold ret = tx_active(edge_port); 2113b16634adSJohan Hovold if (ret > 0) 2114b16634adSJohan Hovold return false; 2115b16634adSJohan Hovold 2116b16634adSJohan Hovold return true; 2117b16634adSJohan Hovold } 2118b16634adSJohan Hovold 211995da310eSAlan Cox static void edge_throttle(struct tty_struct *tty) 21201da177e4SLinus Torvalds { 212195da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 21221da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 21231da177e4SLinus Torvalds int status; 21241da177e4SLinus Torvalds 21251da177e4SLinus Torvalds if (edge_port == NULL) 21261da177e4SLinus Torvalds return; 21271da177e4SLinus Torvalds 21281da177e4SLinus Torvalds /* if we are implementing XON/XOFF, send the stop character */ 21291da177e4SLinus Torvalds if (I_IXOFF(tty)) { 21301da177e4SLinus Torvalds unsigned char stop_char = STOP_CHAR(tty); 213195da310eSAlan Cox status = edge_write(tty, port, &stop_char, 1); 213295da310eSAlan Cox if (status <= 0) { 213395da310eSAlan Cox dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status); 213495da310eSAlan Cox } 21351da177e4SLinus Torvalds } 21361da177e4SLinus Torvalds 213775899513SPeter E. Berger /* 213875899513SPeter E. Berger * if we are implementing RTS/CTS, stop reads 213975899513SPeter E. Berger * and the Edgeport will clear the RTS line 214075899513SPeter E. Berger */ 21411da177e4SLinus Torvalds if (C_CRTSCTS(tty)) 21421da177e4SLinus Torvalds stop_read(edge_port); 21431da177e4SLinus Torvalds 21441da177e4SLinus Torvalds } 21451da177e4SLinus Torvalds 214695da310eSAlan Cox static void edge_unthrottle(struct tty_struct *tty) 21471da177e4SLinus Torvalds { 214895da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 21491da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 21501da177e4SLinus Torvalds int status; 21511da177e4SLinus Torvalds 21521da177e4SLinus Torvalds if (edge_port == NULL) 21531da177e4SLinus Torvalds return; 21541da177e4SLinus Torvalds 21551da177e4SLinus Torvalds /* if we are implementing XON/XOFF, send the start character */ 21561da177e4SLinus Torvalds if (I_IXOFF(tty)) { 21571da177e4SLinus Torvalds unsigned char start_char = START_CHAR(tty); 215895da310eSAlan Cox status = edge_write(tty, port, &start_char, 1); 215995da310eSAlan Cox if (status <= 0) { 216095da310eSAlan Cox dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status); 216195da310eSAlan Cox } 21621da177e4SLinus Torvalds } 216375899513SPeter E. Berger /* 216475899513SPeter E. Berger * if we are implementing RTS/CTS, restart reads 216575899513SPeter E. Berger * are the Edgeport will assert the RTS line 216675899513SPeter E. Berger */ 21671da177e4SLinus Torvalds if (C_CRTSCTS(tty)) { 21681da177e4SLinus Torvalds status = restart_read(edge_port); 21691da177e4SLinus Torvalds if (status) 21702742fd88SAlan Cox dev_err(&port->dev, 21712742fd88SAlan Cox "%s - read bulk usb_submit_urb failed: %d\n", 21722742fd88SAlan Cox __func__, status); 21731da177e4SLinus Torvalds } 21741da177e4SLinus Torvalds 21751da177e4SLinus Torvalds } 21761da177e4SLinus Torvalds 21771da177e4SLinus Torvalds static void stop_read(struct edgeport_port *edge_port) 21781da177e4SLinus Torvalds { 21791da177e4SLinus Torvalds unsigned long flags; 21801da177e4SLinus Torvalds 21811da177e4SLinus Torvalds spin_lock_irqsave(&edge_port->ep_lock, flags); 21821da177e4SLinus Torvalds 21831da177e4SLinus Torvalds if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING) 21841da177e4SLinus Torvalds edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING; 21851da177e4SLinus Torvalds edge_port->shadow_mcr &= ~MCR_RTS; 21861da177e4SLinus Torvalds 21871da177e4SLinus Torvalds spin_unlock_irqrestore(&edge_port->ep_lock, flags); 21881da177e4SLinus Torvalds } 21891da177e4SLinus Torvalds 21901da177e4SLinus Torvalds static int restart_read(struct edgeport_port *edge_port) 21911da177e4SLinus Torvalds { 21921da177e4SLinus Torvalds struct urb *urb; 21931da177e4SLinus Torvalds int status = 0; 21941da177e4SLinus Torvalds unsigned long flags; 21951da177e4SLinus Torvalds 21961da177e4SLinus Torvalds spin_lock_irqsave(&edge_port->ep_lock, flags); 21971da177e4SLinus Torvalds 21981da177e4SLinus Torvalds if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) { 21991da177e4SLinus Torvalds urb = edge_port->port->read_urb; 2200efdff608SOliver Neukum status = usb_submit_urb(urb, GFP_ATOMIC); 22011da177e4SLinus Torvalds } 22021da177e4SLinus Torvalds edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING; 22031da177e4SLinus Torvalds edge_port->shadow_mcr |= MCR_RTS; 22041da177e4SLinus Torvalds 22051da177e4SLinus Torvalds spin_unlock_irqrestore(&edge_port->ep_lock, flags); 22061da177e4SLinus Torvalds 22071da177e4SLinus Torvalds return status; 22081da177e4SLinus Torvalds } 22091da177e4SLinus Torvalds 221095da310eSAlan Cox static void change_port_settings(struct tty_struct *tty, 221195da310eSAlan Cox struct edgeport_port *edge_port, struct ktermios *old_termios) 22121da177e4SLinus Torvalds { 221367e6da70SGreg Kroah-Hartman struct device *dev = &edge_port->port->dev; 22141da177e4SLinus Torvalds struct ump_uart_config *config; 22151da177e4SLinus Torvalds int baud; 22161da177e4SLinus Torvalds unsigned cflag; 22171da177e4SLinus Torvalds int status; 22181da177e4SLinus Torvalds 22191da177e4SLinus Torvalds config = kmalloc (sizeof (*config), GFP_KERNEL); 22201da177e4SLinus Torvalds if (!config) { 2221adc8d746SAlan Cox tty->termios = *old_termios; 22221da177e4SLinus Torvalds return; 22231da177e4SLinus Torvalds } 22241da177e4SLinus Torvalds 2225adc8d746SAlan Cox cflag = tty->termios.c_cflag; 22261da177e4SLinus Torvalds 22271da177e4SLinus Torvalds config->wFlags = 0; 22281da177e4SLinus Torvalds 22291da177e4SLinus Torvalds /* These flags must be set */ 22301da177e4SLinus Torvalds config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT; 22311da177e4SLinus Torvalds config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR; 22321da177e4SLinus Torvalds config->bUartMode = (__u8)(edge_port->bUartMode); 22331da177e4SLinus Torvalds 22341da177e4SLinus Torvalds switch (cflag & CSIZE) { 22351da177e4SLinus Torvalds case CS5: 22361da177e4SLinus Torvalds config->bDataBits = UMP_UART_CHAR5BITS; 223767e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - data bits = 5\n", __func__); 22381da177e4SLinus Torvalds break; 22391da177e4SLinus Torvalds case CS6: 22401da177e4SLinus Torvalds config->bDataBits = UMP_UART_CHAR6BITS; 224167e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - data bits = 6\n", __func__); 22421da177e4SLinus Torvalds break; 22431da177e4SLinus Torvalds case CS7: 22441da177e4SLinus Torvalds config->bDataBits = UMP_UART_CHAR7BITS; 224567e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - data bits = 7\n", __func__); 22461da177e4SLinus Torvalds break; 22471da177e4SLinus Torvalds default: 22481da177e4SLinus Torvalds case CS8: 22491da177e4SLinus Torvalds config->bDataBits = UMP_UART_CHAR8BITS; 225067e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - data bits = 8\n", __func__); 22511da177e4SLinus Torvalds break; 22521da177e4SLinus Torvalds } 22531da177e4SLinus Torvalds 22541da177e4SLinus Torvalds if (cflag & PARENB) { 22551da177e4SLinus Torvalds if (cflag & PARODD) { 22561da177e4SLinus Torvalds config->wFlags |= UMP_MASK_UART_FLAGS_PARITY; 22571da177e4SLinus Torvalds config->bParity = UMP_UART_ODDPARITY; 225867e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - parity = odd\n", __func__); 22591da177e4SLinus Torvalds } else { 22601da177e4SLinus Torvalds config->wFlags |= UMP_MASK_UART_FLAGS_PARITY; 22611da177e4SLinus Torvalds config->bParity = UMP_UART_EVENPARITY; 226267e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - parity = even\n", __func__); 22631da177e4SLinus Torvalds } 22641da177e4SLinus Torvalds } else { 22651da177e4SLinus Torvalds config->bParity = UMP_UART_NOPARITY; 226667e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - parity = none\n", __func__); 22671da177e4SLinus Torvalds } 22681da177e4SLinus Torvalds 22691da177e4SLinus Torvalds if (cflag & CSTOPB) { 22701da177e4SLinus Torvalds config->bStopBits = UMP_UART_STOPBIT2; 227167e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - stop bits = 2\n", __func__); 22721da177e4SLinus Torvalds } else { 22731da177e4SLinus Torvalds config->bStopBits = UMP_UART_STOPBIT1; 227467e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - stop bits = 1\n", __func__); 22751da177e4SLinus Torvalds } 22761da177e4SLinus Torvalds 22771da177e4SLinus Torvalds /* figure out the flow control settings */ 22781da177e4SLinus Torvalds if (cflag & CRTSCTS) { 22791da177e4SLinus Torvalds config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW; 22801da177e4SLinus Torvalds config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW; 228167e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__); 22821da177e4SLinus Torvalds } else { 228367e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__); 22841da177e4SLinus Torvalds restart_read(edge_port); 22851da177e4SLinus Torvalds } 22861da177e4SLinus Torvalds 228775899513SPeter E. Berger /* 228875899513SPeter E. Berger * if we are implementing XON/XOFF, set the start and stop 228975899513SPeter E. Berger * character in the device 229075899513SPeter E. Berger */ 22911da177e4SLinus Torvalds config->cXon = START_CHAR(tty); 22921da177e4SLinus Torvalds config->cXoff = STOP_CHAR(tty); 22931da177e4SLinus Torvalds 22941da177e4SLinus Torvalds /* if we are implementing INBOUND XON/XOFF */ 22951da177e4SLinus Torvalds if (I_IXOFF(tty)) { 22961da177e4SLinus Torvalds config->wFlags |= UMP_MASK_UART_FLAGS_IN_X; 229767e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n", 2298441b62c1SHarvey Harrison __func__, config->cXon, config->cXoff); 22992742fd88SAlan Cox } else 230067e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__); 23011da177e4SLinus Torvalds 23021da177e4SLinus Torvalds /* if we are implementing OUTBOUND XON/XOFF */ 23031da177e4SLinus Torvalds if (I_IXON(tty)) { 23041da177e4SLinus Torvalds config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X; 230567e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n", 2306441b62c1SHarvey Harrison __func__, config->cXon, config->cXoff); 23072742fd88SAlan Cox } else 230867e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__); 23091da177e4SLinus Torvalds 2310adc8d746SAlan Cox tty->termios.c_cflag &= ~CMSPAR; 2311d5f5bcd4SAlan Cox 23121da177e4SLinus Torvalds /* Round the baud rate */ 23131da177e4SLinus Torvalds baud = tty_get_baud_rate(tty); 23141da177e4SLinus Torvalds if (!baud) { 23151da177e4SLinus Torvalds /* pick a default, any default... */ 23161da177e4SLinus Torvalds baud = 9600; 23176aeb75e6SJohan Hovold } else { 23186aeb75e6SJohan Hovold /* Avoid a zero divisor. */ 23196aeb75e6SJohan Hovold baud = min(baud, 461550); 2320d5f5bcd4SAlan Cox tty_encode_baud_rate(tty, baud, baud); 23216aeb75e6SJohan Hovold } 2322d5f5bcd4SAlan Cox 23231da177e4SLinus Torvalds edge_port->baud_rate = baud; 23241da177e4SLinus Torvalds config->wBaudRate = (__u16)((461550L + baud/2) / baud); 23251da177e4SLinus Torvalds 2326d5f5bcd4SAlan Cox /* FIXME: Recompute actual baud from divisor here */ 2327d5f5bcd4SAlan Cox 232867e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - baud rate = %d, wBaudRate = %d\n", __func__, baud, config->wBaudRate); 23291da177e4SLinus Torvalds 233067e6da70SGreg Kroah-Hartman dev_dbg(dev, "wBaudRate: %d\n", (int)(461550L / config->wBaudRate)); 233167e6da70SGreg Kroah-Hartman dev_dbg(dev, "wFlags: 0x%x\n", config->wFlags); 233267e6da70SGreg Kroah-Hartman dev_dbg(dev, "bDataBits: %d\n", config->bDataBits); 233367e6da70SGreg Kroah-Hartman dev_dbg(dev, "bParity: %d\n", config->bParity); 233467e6da70SGreg Kroah-Hartman dev_dbg(dev, "bStopBits: %d\n", config->bStopBits); 233567e6da70SGreg Kroah-Hartman dev_dbg(dev, "cXon: %d\n", config->cXon); 233667e6da70SGreg Kroah-Hartman dev_dbg(dev, "cXoff: %d\n", config->cXoff); 233767e6da70SGreg Kroah-Hartman dev_dbg(dev, "bUartMode: %d\n", config->bUartMode); 23381da177e4SLinus Torvalds 23391da177e4SLinus Torvalds /* move the word values into big endian mode */ 23401da177e4SLinus Torvalds cpu_to_be16s(&config->wFlags); 23411da177e4SLinus Torvalds cpu_to_be16s(&config->wBaudRate); 23421da177e4SLinus Torvalds 23437a14fac0SJohan Hovold status = send_port_cmd(edge_port->port, UMPC_SET_CONFIG, 0, config, 23447a14fac0SJohan Hovold sizeof(*config)); 23452742fd88SAlan Cox if (status) 234667e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - error %d when trying to write config to device\n", 2347441b62c1SHarvey Harrison __func__, status); 23481da177e4SLinus Torvalds kfree(config); 23491da177e4SLinus Torvalds } 23501da177e4SLinus Torvalds 235195da310eSAlan Cox static void edge_set_termios(struct tty_struct *tty, 235295da310eSAlan Cox struct usb_serial_port *port, struct ktermios *old_termios) 23531da177e4SLinus Torvalds { 23541da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 23551da177e4SLinus Torvalds 23561da177e4SLinus Torvalds if (edge_port == NULL) 23571da177e4SLinus Torvalds return; 23581da177e4SLinus Torvalds /* change the port settings to the new ones specified */ 235995da310eSAlan Cox change_port_settings(tty, edge_port, old_termios); 23601da177e4SLinus Torvalds } 23611da177e4SLinus Torvalds 236220b9d177SAlan Cox static int edge_tiocmset(struct tty_struct *tty, 23632742fd88SAlan Cox unsigned int set, unsigned int clear) 23641da177e4SLinus Torvalds { 236595da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 23661da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 23671da177e4SLinus Torvalds unsigned int mcr; 23683d71fe0bSAlan Cox unsigned long flags; 23691da177e4SLinus Torvalds 23703d71fe0bSAlan Cox spin_lock_irqsave(&edge_port->ep_lock, flags); 23711da177e4SLinus Torvalds mcr = edge_port->shadow_mcr; 23721da177e4SLinus Torvalds if (set & TIOCM_RTS) 23731da177e4SLinus Torvalds mcr |= MCR_RTS; 23741da177e4SLinus Torvalds if (set & TIOCM_DTR) 23751da177e4SLinus Torvalds mcr |= MCR_DTR; 23761da177e4SLinus Torvalds if (set & TIOCM_LOOP) 23771da177e4SLinus Torvalds mcr |= MCR_LOOPBACK; 23781da177e4SLinus Torvalds 23791da177e4SLinus Torvalds if (clear & TIOCM_RTS) 23801da177e4SLinus Torvalds mcr &= ~MCR_RTS; 23811da177e4SLinus Torvalds if (clear & TIOCM_DTR) 23821da177e4SLinus Torvalds mcr &= ~MCR_DTR; 23831da177e4SLinus Torvalds if (clear & TIOCM_LOOP) 23841da177e4SLinus Torvalds mcr &= ~MCR_LOOPBACK; 23851da177e4SLinus Torvalds 23861da177e4SLinus Torvalds edge_port->shadow_mcr = mcr; 23873d71fe0bSAlan Cox spin_unlock_irqrestore(&edge_port->ep_lock, flags); 23881da177e4SLinus Torvalds 23892742fd88SAlan Cox restore_mcr(edge_port, mcr); 23901da177e4SLinus Torvalds return 0; 23911da177e4SLinus Torvalds } 23921da177e4SLinus Torvalds 239360b33c13SAlan Cox static int edge_tiocmget(struct tty_struct *tty) 23941da177e4SLinus Torvalds { 239595da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 23961da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 23971da177e4SLinus Torvalds unsigned int result = 0; 23981da177e4SLinus Torvalds unsigned int msr; 23991da177e4SLinus Torvalds unsigned int mcr; 24003d71fe0bSAlan Cox unsigned long flags; 24011da177e4SLinus Torvalds 24023d71fe0bSAlan Cox spin_lock_irqsave(&edge_port->ep_lock, flags); 24033d71fe0bSAlan Cox 24041da177e4SLinus Torvalds msr = edge_port->shadow_msr; 24051da177e4SLinus Torvalds mcr = edge_port->shadow_mcr; 24061da177e4SLinus Torvalds result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */ 24071da177e4SLinus Torvalds | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */ 24081da177e4SLinus Torvalds | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */ 24091da177e4SLinus Torvalds | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */ 24101da177e4SLinus Torvalds | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */ 24111da177e4SLinus Torvalds | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */ 24121da177e4SLinus Torvalds 24131da177e4SLinus Torvalds 241467e6da70SGreg Kroah-Hartman dev_dbg(&port->dev, "%s -- %x\n", __func__, result); 24153d71fe0bSAlan Cox spin_unlock_irqrestore(&edge_port->ep_lock, flags); 24161da177e4SLinus Torvalds 24171da177e4SLinus Torvalds return result; 24181da177e4SLinus Torvalds } 24191da177e4SLinus Torvalds 242095da310eSAlan Cox static void edge_break(struct tty_struct *tty, int break_state) 24211da177e4SLinus Torvalds { 242295da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 24231da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 24241da177e4SLinus Torvalds int status; 24252742fd88SAlan Cox int bv = 0; /* Off */ 24261da177e4SLinus Torvalds 242795da310eSAlan Cox if (break_state == -1) 24282742fd88SAlan Cox bv = 1; /* On */ 24292742fd88SAlan Cox status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv); 24302742fd88SAlan Cox if (status) 243167e6da70SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - error %d sending break set/clear command.\n", 2432441b62c1SHarvey Harrison __func__, status); 24331da177e4SLinus Torvalds } 24341da177e4SLinus Torvalds 243526c78daaSPeter E. Berger static void edge_heartbeat_schedule(struct edgeport_serial *edge_serial) 243626c78daaSPeter E. Berger { 243726c78daaSPeter E. Berger if (!edge_serial->use_heartbeat) 243826c78daaSPeter E. Berger return; 243926c78daaSPeter E. Berger 244026c78daaSPeter E. Berger schedule_delayed_work(&edge_serial->heartbeat_work, 244126c78daaSPeter E. Berger FW_HEARTBEAT_SECS * HZ); 244226c78daaSPeter E. Berger } 244326c78daaSPeter E. Berger 244426c78daaSPeter E. Berger static void edge_heartbeat_work(struct work_struct *work) 244526c78daaSPeter E. Berger { 244626c78daaSPeter E. Berger struct edgeport_serial *serial; 244726c78daaSPeter E. Berger struct ti_i2c_desc *rom_desc; 244826c78daaSPeter E. Berger 244926c78daaSPeter E. Berger serial = container_of(work, struct edgeport_serial, 245026c78daaSPeter E. Berger heartbeat_work.work); 245126c78daaSPeter E. Berger 245226c78daaSPeter E. Berger rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL); 245326c78daaSPeter E. Berger 245426c78daaSPeter E. Berger /* Descriptor address request is enough to reset the firmware timer */ 245526c78daaSPeter E. Berger if (!rom_desc || !get_descriptor_addr(serial, I2C_DESC_TYPE_ION, 245626c78daaSPeter E. Berger rom_desc)) { 245726c78daaSPeter E. Berger dev_err(&serial->serial->interface->dev, 245826c78daaSPeter E. Berger "%s - Incomplete heartbeat\n", __func__); 245926c78daaSPeter E. Berger } 246026c78daaSPeter E. Berger kfree(rom_desc); 246126c78daaSPeter E. Berger 246226c78daaSPeter E. Berger edge_heartbeat_schedule(serial); 246326c78daaSPeter E. Berger } 246426c78daaSPeter E. Berger 246549f4ff2dSJohan Hovold static int edge_calc_num_ports(struct usb_serial *serial, 246649f4ff2dSJohan Hovold struct usb_serial_endpoints *epds) 246749f4ff2dSJohan Hovold { 246849f4ff2dSJohan Hovold struct device *dev = &serial->interface->dev; 246949f4ff2dSJohan Hovold unsigned char num_ports = serial->type->num_ports; 247049f4ff2dSJohan Hovold 247149f4ff2dSJohan Hovold /* Make sure we have the required endpoints when in download mode. */ 247249f4ff2dSJohan Hovold if (serial->interface->cur_altsetting->desc.bNumEndpoints > 1) { 247349f4ff2dSJohan Hovold if (epds->num_bulk_in < num_ports || 24749c8299b4SJohan Hovold epds->num_bulk_out < num_ports || 24759c8299b4SJohan Hovold epds->num_interrupt_in < 1) { 247649f4ff2dSJohan Hovold dev_err(dev, "required endpoints missing\n"); 247749f4ff2dSJohan Hovold return -ENODEV; 247849f4ff2dSJohan Hovold } 247949f4ff2dSJohan Hovold } 248049f4ff2dSJohan Hovold 248149f4ff2dSJohan Hovold return num_ports; 248249f4ff2dSJohan Hovold } 248349f4ff2dSJohan Hovold 24841da177e4SLinus Torvalds static int edge_startup(struct usb_serial *serial) 24851da177e4SLinus Torvalds { 24861da177e4SLinus Torvalds struct edgeport_serial *edge_serial; 24871da177e4SLinus Torvalds int status; 248826c78daaSPeter E. Berger u16 product_id; 24891da177e4SLinus Torvalds 24901da177e4SLinus Torvalds /* create our private serial structure */ 249180b6ca48SEric Sesterhenn edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL); 249210c642d0SJohan Hovold if (!edge_serial) 24931da177e4SLinus Torvalds return -ENOMEM; 249410c642d0SJohan Hovold 2495241ca64fSMatthias Kaehlcke mutex_init(&edge_serial->es_lock); 24961da177e4SLinus Torvalds edge_serial->serial = serial; 24974f9785ccSJohan Hovold INIT_DELAYED_WORK(&edge_serial->heartbeat_work, edge_heartbeat_work); 24981da177e4SLinus Torvalds usb_set_serial_data(serial, edge_serial); 24991da177e4SLinus Torvalds 2500b4146162SPeter E. Berger status = download_fw(edge_serial); 25014f9785ccSJohan Hovold if (status < 0) { 25021da177e4SLinus Torvalds kfree(edge_serial); 25031da177e4SLinus Torvalds return status; 25041da177e4SLinus Torvalds } 25051da177e4SLinus Torvalds 25064f9785ccSJohan Hovold if (status > 0) 25074f9785ccSJohan Hovold return 1; /* bind but do not register any ports */ 25084f9785ccSJohan Hovold 250926c78daaSPeter E. Berger product_id = le16_to_cpu( 251026c78daaSPeter E. Berger edge_serial->serial->dev->descriptor.idProduct); 251126c78daaSPeter E. Berger 251226c78daaSPeter E. Berger /* Currently only the EP/416 models require heartbeat support */ 251326c78daaSPeter E. Berger if (edge_serial->fw_version > FW_HEARTBEAT_VERSION_CUTOFF) { 251426c78daaSPeter E. Berger if (product_id == ION_DEVICE_ID_TI_EDGEPORT_416 || 251526c78daaSPeter E. Berger product_id == ION_DEVICE_ID_TI_EDGEPORT_416B) { 251626c78daaSPeter E. Berger edge_serial->use_heartbeat = true; 251726c78daaSPeter E. Berger } 251826c78daaSPeter E. Berger } 251926c78daaSPeter E. Berger 252026c78daaSPeter E. Berger edge_heartbeat_schedule(edge_serial); 252126c78daaSPeter E. Berger 25221da177e4SLinus Torvalds return 0; 25231da177e4SLinus Torvalds } 25241da177e4SLinus Torvalds 2525f9c99bb8SAlan Stern static void edge_disconnect(struct usb_serial *serial) 25261da177e4SLinus Torvalds { 25272330d0a8SJohan Hovold struct edgeport_serial *edge_serial = usb_get_serial_data(serial); 25282330d0a8SJohan Hovold 25292330d0a8SJohan Hovold cancel_delayed_work_sync(&edge_serial->heartbeat_work); 2530f9c99bb8SAlan Stern } 2531f9c99bb8SAlan Stern 2532f9c99bb8SAlan Stern static void edge_release(struct usb_serial *serial) 2533f9c99bb8SAlan Stern { 253426c78daaSPeter E. Berger struct edgeport_serial *edge_serial = usb_get_serial_data(serial); 253526c78daaSPeter E. Berger 253626c78daaSPeter E. Berger cancel_delayed_work_sync(&edge_serial->heartbeat_work); 253726c78daaSPeter E. Berger kfree(edge_serial); 25381da177e4SLinus Torvalds } 25391da177e4SLinus Torvalds 254000361530SJohan Hovold static int edge_port_probe(struct usb_serial_port *port) 254100361530SJohan Hovold { 254200361530SJohan Hovold struct edgeport_port *edge_port; 254300361530SJohan Hovold int ret; 254400361530SJohan Hovold 254500361530SJohan Hovold edge_port = kzalloc(sizeof(*edge_port), GFP_KERNEL); 254600361530SJohan Hovold if (!edge_port) 254700361530SJohan Hovold return -ENOMEM; 254800361530SJohan Hovold 254900361530SJohan Hovold spin_lock_init(&edge_port->ep_lock); 255000361530SJohan Hovold edge_port->port = port; 255100361530SJohan Hovold edge_port->edge_serial = usb_get_serial_data(port->serial); 255200361530SJohan Hovold edge_port->bUartMode = default_uart_mode; 255300361530SJohan Hovold 25540fce06daSJohan Hovold switch (port->port_number) { 25550fce06daSJohan Hovold case 0: 25560fce06daSJohan Hovold edge_port->uart_base = UMPMEM_BASE_UART1; 25570fce06daSJohan Hovold edge_port->dma_address = UMPD_OEDB1_ADDRESS; 25580fce06daSJohan Hovold break; 25590fce06daSJohan Hovold case 1: 25600fce06daSJohan Hovold edge_port->uart_base = UMPMEM_BASE_UART2; 25610fce06daSJohan Hovold edge_port->dma_address = UMPD_OEDB2_ADDRESS; 25620fce06daSJohan Hovold break; 25630fce06daSJohan Hovold default: 25640fce06daSJohan Hovold dev_err(&port->dev, "unknown port number\n"); 25650fce06daSJohan Hovold ret = -ENODEV; 25660fce06daSJohan Hovold goto err; 25670fce06daSJohan Hovold } 25680fce06daSJohan Hovold 25690fce06daSJohan Hovold dev_dbg(&port->dev, 25700fce06daSJohan Hovold "%s - port_number = %d, uart_base = %04x, dma_address = %04x\n", 25710fce06daSJohan Hovold __func__, port->port_number, edge_port->uart_base, 25720fce06daSJohan Hovold edge_port->dma_address); 25730fce06daSJohan Hovold 257400361530SJohan Hovold usb_set_serial_port_data(port, edge_port); 257500361530SJohan Hovold 25765d8c61bcSJohan Hovold ret = edge_create_sysfs_attrs(port); 25770fce06daSJohan Hovold if (ret) 25780fce06daSJohan Hovold goto err; 25795d8c61bcSJohan Hovold 25804b8e0795SJohan Hovold /* 25814b8e0795SJohan Hovold * The LSR does not tell when the transmitter shift register has 25824b8e0795SJohan Hovold * emptied so add a one-character drain delay. 25834b8e0795SJohan Hovold */ 2584d7be6221SJohan Hovold port->port.drain_delay = 1; 2585f40d7815SJohan Hovold 258600361530SJohan Hovold return 0; 25870fce06daSJohan Hovold err: 25880fce06daSJohan Hovold kfree(edge_port); 25890fce06daSJohan Hovold 25900fce06daSJohan Hovold return ret; 259100361530SJohan Hovold } 259200361530SJohan Hovold 2593c5d1448fSUwe Kleine-König static void edge_port_remove(struct usb_serial_port *port) 259400361530SJohan Hovold { 259500361530SJohan Hovold struct edgeport_port *edge_port; 259600361530SJohan Hovold 259700361530SJohan Hovold edge_port = usb_get_serial_port_data(port); 259800361530SJohan Hovold edge_remove_sysfs_attrs(port); 259900361530SJohan Hovold kfree(edge_port); 260000361530SJohan Hovold } 26011da177e4SLinus Torvalds 2602fc4cbd75SMartin K. Petersen /* Sysfs Attributes */ 2603fc4cbd75SMartin K. Petersen 2604154547c4SGreg Kroah-Hartman static ssize_t uart_mode_show(struct device *dev, 2605fc4cbd75SMartin K. Petersen struct device_attribute *attr, char *buf) 2606fc4cbd75SMartin K. Petersen { 2607fc4cbd75SMartin K. Petersen struct usb_serial_port *port = to_usb_serial_port(dev); 2608fc4cbd75SMartin K. Petersen struct edgeport_port *edge_port = usb_get_serial_port_data(port); 2609fc4cbd75SMartin K. Petersen 2610fc4cbd75SMartin K. Petersen return sprintf(buf, "%d\n", edge_port->bUartMode); 2611fc4cbd75SMartin K. Petersen } 2612fc4cbd75SMartin K. Petersen 2613154547c4SGreg Kroah-Hartman static ssize_t uart_mode_store(struct device *dev, 2614fc4cbd75SMartin K. Petersen struct device_attribute *attr, const char *valbuf, size_t count) 2615fc4cbd75SMartin K. Petersen { 2616fc4cbd75SMartin K. Petersen struct usb_serial_port *port = to_usb_serial_port(dev); 2617fc4cbd75SMartin K. Petersen struct edgeport_port *edge_port = usb_get_serial_port_data(port); 2618fc4cbd75SMartin K. Petersen unsigned int v = simple_strtoul(valbuf, NULL, 0); 2619fc4cbd75SMartin K. Petersen 262067e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s: setting uart_mode = %d\n", __func__, v); 2621fc4cbd75SMartin K. Petersen 2622fc4cbd75SMartin K. Petersen if (v < 256) 2623fc4cbd75SMartin K. Petersen edge_port->bUartMode = v; 2624fc4cbd75SMartin K. Petersen else 2625441b62c1SHarvey Harrison dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v); 2626fc4cbd75SMartin K. Petersen 2627fc4cbd75SMartin K. Petersen return count; 2628fc4cbd75SMartin K. Petersen } 2629154547c4SGreg Kroah-Hartman static DEVICE_ATTR_RW(uart_mode); 2630fc4cbd75SMartin K. Petersen 2631fc4cbd75SMartin K. Petersen static int edge_create_sysfs_attrs(struct usb_serial_port *port) 2632fc4cbd75SMartin K. Petersen { 2633fc4cbd75SMartin K. Petersen return device_create_file(&port->dev, &dev_attr_uart_mode); 2634fc4cbd75SMartin K. Petersen } 2635fc4cbd75SMartin K. Petersen 2636fc4cbd75SMartin K. Petersen static int edge_remove_sysfs_attrs(struct usb_serial_port *port) 2637fc4cbd75SMartin K. Petersen { 2638fc4cbd75SMartin K. Petersen device_remove_file(&port->dev, &dev_attr_uart_mode); 2639fc4cbd75SMartin K. Petersen return 0; 2640fc4cbd75SMartin K. Petersen } 2641fc4cbd75SMartin K. Petersen 264226c78daaSPeter E. Berger #ifdef CONFIG_PM 264326c78daaSPeter E. Berger static int edge_suspend(struct usb_serial *serial, pm_message_t message) 264426c78daaSPeter E. Berger { 264526c78daaSPeter E. Berger struct edgeport_serial *edge_serial = usb_get_serial_data(serial); 264626c78daaSPeter E. Berger 264726c78daaSPeter E. Berger cancel_delayed_work_sync(&edge_serial->heartbeat_work); 264826c78daaSPeter E. Berger 264926c78daaSPeter E. Berger return 0; 265026c78daaSPeter E. Berger } 265126c78daaSPeter E. Berger 265226c78daaSPeter E. Berger static int edge_resume(struct usb_serial *serial) 265326c78daaSPeter E. Berger { 265426c78daaSPeter E. Berger struct edgeport_serial *edge_serial = usb_get_serial_data(serial); 265526c78daaSPeter E. Berger 265626c78daaSPeter E. Berger edge_heartbeat_schedule(edge_serial); 265726c78daaSPeter E. Berger 265826c78daaSPeter E. Berger return 0; 265926c78daaSPeter E. Berger } 266026c78daaSPeter E. Berger #endif 2661fc4cbd75SMartin K. Petersen 2662ea65370dSGreg Kroah-Hartman static struct usb_serial_driver edgeport_1port_device = { 266318fcac35SGreg Kroah-Hartman .driver = { 26641da177e4SLinus Torvalds .owner = THIS_MODULE, 2665269bda1cSGreg Kroah-Hartman .name = "edgeport_ti_1", 266618fcac35SGreg Kroah-Hartman }, 2667269bda1cSGreg Kroah-Hartman .description = "Edgeport TI 1 port adapter", 26681da177e4SLinus Torvalds .id_table = edgeport_1port_id_table, 26691da177e4SLinus Torvalds .num_ports = 1, 26708d9c4d9eSJohan Hovold .num_bulk_out = 1, 26711da177e4SLinus Torvalds .open = edge_open, 26721da177e4SLinus Torvalds .close = edge_close, 26731da177e4SLinus Torvalds .throttle = edge_throttle, 26741da177e4SLinus Torvalds .unthrottle = edge_unthrottle, 26751da177e4SLinus Torvalds .attach = edge_startup, 267649f4ff2dSJohan Hovold .calc_num_ports = edge_calc_num_ports, 2677f9c99bb8SAlan Stern .disconnect = edge_disconnect, 2678f9c99bb8SAlan Stern .release = edge_release, 267900361530SJohan Hovold .port_probe = edge_port_probe, 268000361530SJohan Hovold .port_remove = edge_port_remove, 26811da177e4SLinus Torvalds .set_termios = edge_set_termios, 26821da177e4SLinus Torvalds .tiocmget = edge_tiocmget, 26831da177e4SLinus Torvalds .tiocmset = edge_tiocmset, 268448ee5801SJohan Hovold .tiocmiwait = usb_serial_generic_tiocmiwait, 2685cf9a9d66SJohan Hovold .get_icount = usb_serial_generic_get_icount, 26861da177e4SLinus Torvalds .write = edge_write, 26871da177e4SLinus Torvalds .write_room = edge_write_room, 26881da177e4SLinus Torvalds .chars_in_buffer = edge_chars_in_buffer, 2689b16634adSJohan Hovold .tx_empty = edge_tx_empty, 26901da177e4SLinus Torvalds .break_ctl = edge_break, 26911da177e4SLinus Torvalds .read_int_callback = edge_interrupt_callback, 26921da177e4SLinus Torvalds .read_bulk_callback = edge_bulk_in_callback, 26931da177e4SLinus Torvalds .write_bulk_callback = edge_bulk_out_callback, 269426c78daaSPeter E. Berger #ifdef CONFIG_PM 269526c78daaSPeter E. Berger .suspend = edge_suspend, 269626c78daaSPeter E. Berger .resume = edge_resume, 269726c78daaSPeter E. Berger #endif 26981da177e4SLinus Torvalds }; 26991da177e4SLinus Torvalds 2700ea65370dSGreg Kroah-Hartman static struct usb_serial_driver edgeport_2port_device = { 270118fcac35SGreg Kroah-Hartman .driver = { 27021da177e4SLinus Torvalds .owner = THIS_MODULE, 2703269bda1cSGreg Kroah-Hartman .name = "edgeport_ti_2", 270418fcac35SGreg Kroah-Hartman }, 2705269bda1cSGreg Kroah-Hartman .description = "Edgeport TI 2 port adapter", 27061da177e4SLinus Torvalds .id_table = edgeport_2port_id_table, 27071da177e4SLinus Torvalds .num_ports = 2, 27088d9c4d9eSJohan Hovold .num_bulk_out = 1, 27091da177e4SLinus Torvalds .open = edge_open, 27101da177e4SLinus Torvalds .close = edge_close, 27111da177e4SLinus Torvalds .throttle = edge_throttle, 27121da177e4SLinus Torvalds .unthrottle = edge_unthrottle, 27131da177e4SLinus Torvalds .attach = edge_startup, 271449f4ff2dSJohan Hovold .calc_num_ports = edge_calc_num_ports, 2715f9c99bb8SAlan Stern .disconnect = edge_disconnect, 2716f9c99bb8SAlan Stern .release = edge_release, 271700361530SJohan Hovold .port_probe = edge_port_probe, 271800361530SJohan Hovold .port_remove = edge_port_remove, 27191da177e4SLinus Torvalds .set_termios = edge_set_termios, 27201da177e4SLinus Torvalds .tiocmget = edge_tiocmget, 27211da177e4SLinus Torvalds .tiocmset = edge_tiocmset, 272248ee5801SJohan Hovold .tiocmiwait = usb_serial_generic_tiocmiwait, 2723cf9a9d66SJohan Hovold .get_icount = usb_serial_generic_get_icount, 27241da177e4SLinus Torvalds .write = edge_write, 27251da177e4SLinus Torvalds .write_room = edge_write_room, 27261da177e4SLinus Torvalds .chars_in_buffer = edge_chars_in_buffer, 2727b16634adSJohan Hovold .tx_empty = edge_tx_empty, 27281da177e4SLinus Torvalds .break_ctl = edge_break, 27291da177e4SLinus Torvalds .read_int_callback = edge_interrupt_callback, 27301da177e4SLinus Torvalds .read_bulk_callback = edge_bulk_in_callback, 27311da177e4SLinus Torvalds .write_bulk_callback = edge_bulk_out_callback, 273226c78daaSPeter E. Berger #ifdef CONFIG_PM 273326c78daaSPeter E. Berger .suspend = edge_suspend, 273426c78daaSPeter E. Berger .resume = edge_resume, 273526c78daaSPeter E. Berger #endif 27361da177e4SLinus Torvalds }; 27371da177e4SLinus Torvalds 27387dbe2460SAlan Stern static struct usb_serial_driver * const serial_drivers[] = { 27397dbe2460SAlan Stern &edgeport_1port_device, &edgeport_2port_device, NULL 27407dbe2460SAlan Stern }; 27417dbe2460SAlan Stern 274268e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table_combined); 27431da177e4SLinus Torvalds 27441da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR); 27451da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC); 27461da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 2747d12b219aSJaswinder Singh MODULE_FIRMWARE("edgeport/down3.bin"); 27481da177e4SLinus Torvalds 27491da177e4SLinus Torvalds module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR); 27502742fd88SAlan Cox MODULE_PARM_DESC(ignore_cpu_rev, 27512742fd88SAlan Cox "Ignore the cpu revision when connecting to a device"); 27521da177e4SLinus Torvalds 2753fc4cbd75SMartin K. Petersen module_param(default_uart_mode, int, S_IRUGO | S_IWUSR); 2754fc4cbd75SMartin K. Petersen MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ..."); 2755