11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Edgeport USB Serial Converter driver 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2000-2002 Inside Out Networks, All rights reserved. 51da177e4SLinus Torvalds * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com> 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 81da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 91da177e4SLinus Torvalds * the Free Software Foundation; either version 2 of the License, or 101da177e4SLinus Torvalds * (at your option) any later version. 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * Supports the following devices: 131da177e4SLinus Torvalds * EP/1 EP/2 EP/4 EP/21 EP/22 EP/221 EP/42 EP/421 WATCHPORT 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * For questions or problems with this driver, contact Inside Out 161da177e4SLinus Torvalds * Networks technical support, or Peter Berger <pberger@brimson.com>, 171da177e4SLinus Torvalds * or Al Borchers <alborchers@steinerpoint.com>. 181da177e4SLinus Torvalds * 191da177e4SLinus Torvalds * Version history: 201da177e4SLinus Torvalds * 211da177e4SLinus Torvalds * July 11, 2002 Removed 4 port device structure since all TI UMP 221da177e4SLinus Torvalds * chips have only 2 ports 231da177e4SLinus Torvalds * David Iacovelli (davidi@ionetworks.com) 241da177e4SLinus Torvalds * 251da177e4SLinus Torvalds */ 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds #include <linux/kernel.h> 281da177e4SLinus Torvalds #include <linux/jiffies.h> 291da177e4SLinus Torvalds #include <linux/errno.h> 301da177e4SLinus Torvalds #include <linux/init.h> 311da177e4SLinus Torvalds #include <linux/slab.h> 321da177e4SLinus Torvalds #include <linux/tty.h> 331da177e4SLinus Torvalds #include <linux/tty_driver.h> 341da177e4SLinus Torvalds #include <linux/tty_flip.h> 351da177e4SLinus Torvalds #include <linux/module.h> 361da177e4SLinus Torvalds #include <linux/spinlock.h> 37241ca64fSMatthias Kaehlcke #include <linux/mutex.h> 381da177e4SLinus Torvalds #include <linux/serial.h> 39d733cec1SJohan Hovold #include <linux/kfifo.h> 401da177e4SLinus Torvalds #include <linux/ioctl.h> 41d12b219aSJaswinder Singh #include <linux/firmware.h> 422742fd88SAlan Cox #include <linux/uaccess.h> 431da177e4SLinus Torvalds #include <linux/usb.h> 44a969888cSGreg Kroah-Hartman #include <linux/usb/serial.h> 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds #include "io_16654.h" 471da177e4SLinus Torvalds #include "io_usbvend.h" 481da177e4SLinus Torvalds #include "io_ti.h" 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds /* 511da177e4SLinus Torvalds * Version Information 521da177e4SLinus Torvalds */ 53fc4cbd75SMartin K. Petersen #define DRIVER_VERSION "v0.7mode043006" 541da177e4SLinus Torvalds #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli" 551da177e4SLinus Torvalds #define DRIVER_DESC "Edgeport USB Serial Driver" 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds #define EPROM_PAGE_SIZE 64 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds /* different hardware types */ 611da177e4SLinus Torvalds #define HARDWARE_TYPE_930 0 621da177e4SLinus Torvalds #define HARDWARE_TYPE_TIUMP 1 631da177e4SLinus Torvalds 642742fd88SAlan Cox /* IOCTL_PRIVATE_TI_GET_MODE Definitions */ 652742fd88SAlan Cox #define TI_MODE_CONFIGURING 0 /* Device has not entered start device */ 662742fd88SAlan Cox #define TI_MODE_BOOT 1 /* Staying in boot mode */ 672742fd88SAlan Cox #define TI_MODE_DOWNLOAD 2 /* Made it to download mode */ 682742fd88SAlan Cox #define TI_MODE_TRANSITIONING 3 /* Currently in boot mode but 692742fd88SAlan Cox transitioning to download mode */ 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds /* read urb state */ 721da177e4SLinus Torvalds #define EDGE_READ_URB_RUNNING 0 731da177e4SLinus Torvalds #define EDGE_READ_URB_STOPPING 1 741da177e4SLinus Torvalds #define EDGE_READ_URB_STOPPED 2 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds #define EDGE_CLOSING_WAIT 4000 /* in .01 sec */ 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds #define EDGE_OUT_BUF_SIZE 1024 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds /* Product information read from the Edgeport */ 822742fd88SAlan Cox struct product_info { 832742fd88SAlan Cox int TiMode; /* Current TI Mode */ 842742fd88SAlan Cox __u8 hardware_type; /* Type of hardware */ 851da177e4SLinus Torvalds } __attribute__((packed)); 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds struct edgeport_port { 881da177e4SLinus Torvalds __u16 uart_base; 891da177e4SLinus Torvalds __u16 dma_address; 901da177e4SLinus Torvalds __u8 shadow_msr; 911da177e4SLinus Torvalds __u8 shadow_mcr; 921da177e4SLinus Torvalds __u8 shadow_lsr; 931da177e4SLinus Torvalds __u8 lsr_mask; 9419af5cdbSMartin Olsson __u32 ump_read_timeout; /* Number of milliseconds the UMP will 951da177e4SLinus Torvalds wait without data before completing 961da177e4SLinus Torvalds a read short */ 971da177e4SLinus Torvalds int baud_rate; 981da177e4SLinus Torvalds int close_pending; 991da177e4SLinus Torvalds int lsr_event; 1001da177e4SLinus Torvalds struct async_icount icount; 1011da177e4SLinus Torvalds wait_queue_head_t delta_msr_wait; /* for handling sleeping while 1021da177e4SLinus Torvalds waiting for msr change to 1031da177e4SLinus Torvalds happen */ 1041da177e4SLinus Torvalds struct edgeport_serial *edge_serial; 1051da177e4SLinus Torvalds struct usb_serial_port *port; 1061da177e4SLinus Torvalds __u8 bUartMode; /* Port type, 0: RS232, etc. */ 1071da177e4SLinus Torvalds spinlock_t ep_lock; 1081da177e4SLinus Torvalds int ep_read_urb_state; 1091da177e4SLinus Torvalds int ep_write_urb_in_use; 110d733cec1SJohan Hovold struct kfifo write_fifo; 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 */ 1162742fd88SAlan Cox u8 TiReadI2C; /* Set to TRUE if we have read the 1172742fd88SAlan Cox I2c in Boot Mode */ 118241ca64fSMatthias Kaehlcke struct mutex es_lock; 1191da177e4SLinus Torvalds int num_ports_open; 1201da177e4SLinus Torvalds struct usb_serial *serial; 1211da177e4SLinus Torvalds }; 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds /* Devices that this driver supports */ 1257d40d7e8SNémeth Márton static const struct usb_device_id edgeport_1port_id_table[] = { 1261da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) }, 1271da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) }, 1281da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) }, 1291da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) }, 1301da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) }, 1311da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) }, 1321da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) }, 1331da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) }, 1341da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) }, 1351da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) }, 1361da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) }, 1371da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) }, 1381da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) }, 1391da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) }, 1401da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) }, 1411da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) }, 1421da177e4SLinus Torvalds { } 1431da177e4SLinus Torvalds }; 1441da177e4SLinus Torvalds 1457d40d7e8SNémeth Márton static const struct usb_device_id edgeport_2port_id_table[] = { 1461da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) }, 1471da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) }, 1481da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) }, 1491da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) }, 1501da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) }, 1511da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) }, 1521da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) }, 1531da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) }, 1541da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) }, 1551da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) }, 1561da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) }, 1571da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) }, 158fc4cbd75SMartin K. Petersen /* The 4, 8 and 16 port devices show up as multiple 2 port devices */ 1591da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) }, 160fc4cbd75SMartin K. Petersen { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) }, 161fc4cbd75SMartin K. Petersen { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) }, 162fc4cbd75SMartin K. Petersen { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) }, 163fc4cbd75SMartin K. Petersen { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) }, 1641da177e4SLinus Torvalds { } 1651da177e4SLinus Torvalds }; 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds /* Devices that this driver supports */ 1687d40d7e8SNémeth Márton static const struct usb_device_id id_table_combined[] = { 1691da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) }, 1701da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) }, 1711da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) }, 1721da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) }, 1731da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) }, 1741da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) }, 1751da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) }, 1761da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) }, 1771da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) }, 1781da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) }, 1791da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) }, 1801da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) }, 1811da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) }, 1821da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) }, 1831da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) }, 1841da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) }, 1851da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) }, 1861da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) }, 1871da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) }, 1881da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) }, 1891da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) }, 1901da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) }, 1911da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) }, 1921da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) }, 1931da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) }, 1941da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) }, 1951da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) }, 1961da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) }, 1971da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) }, 198fc4cbd75SMartin K. Petersen { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) }, 199fc4cbd75SMartin K. Petersen { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) }, 200fc4cbd75SMartin K. Petersen { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) }, 201fc4cbd75SMartin K. Petersen { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) }, 2021da177e4SLinus Torvalds { } 2031da177e4SLinus Torvalds }; 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds MODULE_DEVICE_TABLE(usb, id_table_combined); 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds static struct usb_driver io_driver = { 2081da177e4SLinus Torvalds .name = "io_ti", 2091da177e4SLinus Torvalds .probe = usb_serial_probe, 2101da177e4SLinus Torvalds .disconnect = usb_serial_disconnect, 2111da177e4SLinus Torvalds .id_table = id_table_combined, 212ba9dc657SGreg Kroah-Hartman .no_dynamic_id = 1, 2131da177e4SLinus Torvalds }; 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds 216d12b219aSJaswinder Singh static unsigned char OperationalMajorVersion; 217d12b219aSJaswinder Singh static unsigned char OperationalMinorVersion; 218d12b219aSJaswinder Singh static unsigned short OperationalBuildNumber; 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds static int debug; 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds static int closing_wait = EDGE_CLOSING_WAIT; 2232742fd88SAlan Cox static int ignore_cpu_rev; 2242742fd88SAlan Cox static int default_uart_mode; /* RS232 */ 2251da177e4SLinus Torvalds 2262742fd88SAlan Cox static void edge_tty_recv(struct device *dev, struct tty_struct *tty, 2272742fd88SAlan Cox unsigned char *data, int length); 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds static void stop_read(struct edgeport_port *edge_port); 2301da177e4SLinus Torvalds static int restart_read(struct edgeport_port *edge_port); 2311da177e4SLinus Torvalds 23295da310eSAlan Cox static void edge_set_termios(struct tty_struct *tty, 23395da310eSAlan Cox struct usb_serial_port *port, struct ktermios *old_termios); 23495da310eSAlan Cox static void edge_send(struct tty_struct *tty); 2351da177e4SLinus Torvalds 236fc4cbd75SMartin K. Petersen /* sysfs attributes */ 237fc4cbd75SMartin K. Petersen static int edge_create_sysfs_attrs(struct usb_serial_port *port); 238fc4cbd75SMartin K. Petersen static int edge_remove_sysfs_attrs(struct usb_serial_port *port); 239fc4cbd75SMartin K. Petersen 2401da177e4SLinus Torvalds 2412742fd88SAlan Cox static int ti_vread_sync(struct usb_device *dev, __u8 request, 2422742fd88SAlan Cox __u16 value, __u16 index, u8 *data, int size) 2431da177e4SLinus Torvalds { 2441da177e4SLinus Torvalds int status; 2451da177e4SLinus Torvalds 2462742fd88SAlan Cox status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request, 2472742fd88SAlan Cox (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN), 2482742fd88SAlan Cox value, index, data, size, 1000); 2491da177e4SLinus Torvalds if (status < 0) 2501da177e4SLinus Torvalds return status; 2511da177e4SLinus Torvalds if (status != size) { 2521da177e4SLinus Torvalds dbg("%s - wanted to write %d, but only wrote %d", 253441b62c1SHarvey Harrison __func__, size, status); 2541da177e4SLinus Torvalds return -ECOMM; 2551da177e4SLinus Torvalds } 2561da177e4SLinus Torvalds return 0; 2571da177e4SLinus Torvalds } 2581da177e4SLinus Torvalds 2592742fd88SAlan Cox static int ti_vsend_sync(struct usb_device *dev, __u8 request, 2602742fd88SAlan Cox __u16 value, __u16 index, u8 *data, int size) 2611da177e4SLinus Torvalds { 2621da177e4SLinus Torvalds int status; 2631da177e4SLinus Torvalds 2642742fd88SAlan Cox status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request, 2652742fd88SAlan Cox (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT), 2662742fd88SAlan Cox value, index, data, size, 1000); 2671da177e4SLinus Torvalds if (status < 0) 2681da177e4SLinus Torvalds return status; 2691da177e4SLinus Torvalds if (status != size) { 2701da177e4SLinus Torvalds dbg("%s - wanted to write %d, but only wrote %d", 271441b62c1SHarvey Harrison __func__, size, status); 2721da177e4SLinus Torvalds return -ECOMM; 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds return 0; 2751da177e4SLinus Torvalds } 2761da177e4SLinus Torvalds 2772742fd88SAlan Cox static int send_cmd(struct usb_device *dev, __u8 command, 2781da177e4SLinus Torvalds __u8 moduleid, __u16 value, u8 *data, 2791da177e4SLinus Torvalds int size) 2801da177e4SLinus Torvalds { 2812742fd88SAlan Cox return ti_vsend_sync(dev, command, value, moduleid, data, size); 2821da177e4SLinus Torvalds } 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds /* clear tx/rx buffers and fifo in TI UMP */ 2852742fd88SAlan Cox static int purge_port(struct usb_serial_port *port, __u16 mask) 2861da177e4SLinus Torvalds { 2871da177e4SLinus Torvalds int port_number = port->number - port->serial->minor; 2881da177e4SLinus Torvalds 289441b62c1SHarvey Harrison dbg("%s - port %d, mask %x", __func__, port_number, mask); 2901da177e4SLinus Torvalds 2912742fd88SAlan Cox return send_cmd(port->serial->dev, 2921da177e4SLinus Torvalds UMPC_PURGE_PORT, 2931da177e4SLinus Torvalds (__u8)(UMPM_UART1_PORT + port_number), 2941da177e4SLinus Torvalds mask, 2951da177e4SLinus Torvalds NULL, 2961da177e4SLinus Torvalds 0); 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds /** 3002742fd88SAlan Cox * read_download_mem - Read edgeport memory from TI chip 3011da177e4SLinus Torvalds * @dev: usb device pointer 3021da177e4SLinus Torvalds * @start_address: Device CPU address at which to read 3031da177e4SLinus Torvalds * @length: Length of above data 3041da177e4SLinus Torvalds * @address_type: Can read both XDATA and I2C 3051da177e4SLinus Torvalds * @buffer: pointer to input data buffer 3061da177e4SLinus Torvalds */ 3072742fd88SAlan Cox static int read_download_mem(struct usb_device *dev, int start_address, 3081da177e4SLinus Torvalds int length, __u8 address_type, __u8 *buffer) 3091da177e4SLinus Torvalds { 3101da177e4SLinus Torvalds int status = 0; 3111da177e4SLinus Torvalds __u8 read_length; 3121da177e4SLinus Torvalds __be16 be_start_address; 3131da177e4SLinus Torvalds 314441b62c1SHarvey Harrison dbg("%s - @ %x for %d", __func__, start_address, length); 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds /* Read in blocks of 64 bytes 3171da177e4SLinus Torvalds * (TI firmware can't handle more than 64 byte reads) 3181da177e4SLinus Torvalds */ 3191da177e4SLinus Torvalds while (length) { 3201da177e4SLinus Torvalds if (length > 64) 3211da177e4SLinus Torvalds read_length = 64; 3221da177e4SLinus Torvalds else 3231da177e4SLinus Torvalds read_length = (__u8)length; 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds if (read_length > 1) { 326441b62c1SHarvey Harrison dbg("%s - @ %x for %d", __func__, 3271da177e4SLinus Torvalds start_address, read_length); 3281da177e4SLinus Torvalds } 3291da177e4SLinus Torvalds be_start_address = cpu_to_be16(start_address); 3302742fd88SAlan Cox status = ti_vread_sync(dev, UMPC_MEMORY_READ, 3312742fd88SAlan Cox (__u16)address_type, 3322742fd88SAlan Cox (__force __u16)be_start_address, 3332742fd88SAlan Cox buffer, read_length); 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds if (status) { 336441b62c1SHarvey Harrison dbg("%s - ERROR %x", __func__, status); 3371da177e4SLinus Torvalds return status; 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds 3402742fd88SAlan Cox if (read_length > 1) 341441b62c1SHarvey Harrison usb_serial_debug_data(debug, &dev->dev, __func__, 3421da177e4SLinus Torvalds read_length, buffer); 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvalds /* Update pointers/length */ 3451da177e4SLinus Torvalds start_address += read_length; 3461da177e4SLinus Torvalds buffer += read_length; 3471da177e4SLinus Torvalds length -= read_length; 3481da177e4SLinus Torvalds } 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds return status; 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds 3532742fd88SAlan Cox static int read_ram(struct usb_device *dev, int start_address, 3542742fd88SAlan Cox int length, __u8 *buffer) 3551da177e4SLinus Torvalds { 3562742fd88SAlan Cox return read_download_mem(dev, start_address, length, 3572742fd88SAlan Cox DTK_ADDR_SPACE_XDATA, buffer); 3581da177e4SLinus Torvalds } 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds /* Read edgeport memory to a given block */ 3612742fd88SAlan Cox static int read_boot_mem(struct edgeport_serial *serial, 3622742fd88SAlan Cox int start_address, int length, __u8 *buffer) 3631da177e4SLinus Torvalds { 3641da177e4SLinus Torvalds int status = 0; 3651da177e4SLinus Torvalds int i; 3661da177e4SLinus Torvalds 3671da177e4SLinus Torvalds for (i = 0; i < length; i++) { 3682742fd88SAlan Cox status = ti_vread_sync(serial->serial->dev, 3692742fd88SAlan Cox UMPC_MEMORY_READ, serial->TI_I2C_Type, 3702742fd88SAlan Cox (__u16)(start_address+i), &buffer[i], 0x01); 3711da177e4SLinus Torvalds if (status) { 372441b62c1SHarvey Harrison dbg("%s - ERROR %x", __func__, status); 3731da177e4SLinus Torvalds return status; 3741da177e4SLinus Torvalds } 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds 3772742fd88SAlan Cox dbg("%s - start_address = %x, length = %d", 3782742fd88SAlan Cox __func__, start_address, length); 3792742fd88SAlan Cox usb_serial_debug_data(debug, &serial->serial->dev->dev, 3802742fd88SAlan Cox __func__, length, buffer); 3811da177e4SLinus Torvalds 3821da177e4SLinus Torvalds serial->TiReadI2C = 1; 3831da177e4SLinus Torvalds 3841da177e4SLinus Torvalds return status; 3851da177e4SLinus Torvalds } 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds /* Write given block to TI EPROM memory */ 3882742fd88SAlan Cox static int write_boot_mem(struct edgeport_serial *serial, 3892742fd88SAlan Cox int start_address, int length, __u8 *buffer) 3901da177e4SLinus Torvalds { 3911da177e4SLinus Torvalds int status = 0; 3921da177e4SLinus Torvalds int i; 393e9305d2fSJohan Hovold u8 *temp; 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds /* Must do a read before write */ 3961da177e4SLinus Torvalds if (!serial->TiReadI2C) { 397e9305d2fSJohan Hovold temp = kmalloc(1, GFP_KERNEL); 398e9305d2fSJohan Hovold if (!temp) { 399e9305d2fSJohan Hovold dev_err(&serial->serial->dev->dev, 400e9305d2fSJohan Hovold "%s - out of memory\n", __func__); 401e9305d2fSJohan Hovold return -ENOMEM; 402e9305d2fSJohan Hovold } 403e9305d2fSJohan Hovold status = read_boot_mem(serial, 0, 1, temp); 404e9305d2fSJohan Hovold kfree(temp); 4051da177e4SLinus Torvalds if (status) 4061da177e4SLinus Torvalds return status; 4071da177e4SLinus Torvalds } 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds for (i = 0; i < length; ++i) { 4102742fd88SAlan Cox status = ti_vsend_sync(serial->serial->dev, 4112742fd88SAlan Cox UMPC_MEMORY_WRITE, buffer[i], 4122742fd88SAlan Cox (__u16)(i + start_address), NULL, 0); 4131da177e4SLinus Torvalds if (status) 4141da177e4SLinus Torvalds return status; 4151da177e4SLinus Torvalds } 4161da177e4SLinus Torvalds 4172742fd88SAlan Cox dbg("%s - start_sddr = %x, length = %d", 4182742fd88SAlan Cox __func__, start_address, length); 4192742fd88SAlan Cox usb_serial_debug_data(debug, &serial->serial->dev->dev, 4202742fd88SAlan Cox __func__, length, buffer); 4211da177e4SLinus Torvalds 4221da177e4SLinus Torvalds return status; 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds /* Write edgeport I2C memory to TI chip */ 4272742fd88SAlan Cox static int write_i2c_mem(struct edgeport_serial *serial, 4282742fd88SAlan Cox int start_address, int length, __u8 address_type, __u8 *buffer) 4291da177e4SLinus Torvalds { 4301da177e4SLinus Torvalds int status = 0; 4311da177e4SLinus Torvalds int write_length; 4321da177e4SLinus Torvalds __be16 be_start_address; 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds /* We can only send a maximum of 1 aligned byte page at a time */ 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds /* calulate the number of bytes left in the first page */ 4372742fd88SAlan Cox write_length = EPROM_PAGE_SIZE - 4382742fd88SAlan Cox (start_address & (EPROM_PAGE_SIZE - 1)); 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds if (write_length > length) 4411da177e4SLinus Torvalds write_length = length; 4421da177e4SLinus Torvalds 4432742fd88SAlan Cox dbg("%s - BytesInFirstPage Addr = %x, length = %d", 4442742fd88SAlan Cox __func__, start_address, write_length); 4452742fd88SAlan Cox usb_serial_debug_data(debug, &serial->serial->dev->dev, 4462742fd88SAlan Cox __func__, write_length, buffer); 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds /* Write first page */ 4491da177e4SLinus Torvalds be_start_address = cpu_to_be16(start_address); 4502742fd88SAlan Cox status = ti_vsend_sync(serial->serial->dev, 4512742fd88SAlan Cox UMPC_MEMORY_WRITE, (__u16)address_type, 4522742fd88SAlan Cox (__force __u16)be_start_address, 4532742fd88SAlan Cox buffer, write_length); 4541da177e4SLinus Torvalds if (status) { 455441b62c1SHarvey Harrison dbg("%s - ERROR %d", __func__, status); 4561da177e4SLinus Torvalds return status; 4571da177e4SLinus Torvalds } 4581da177e4SLinus Torvalds 4591da177e4SLinus Torvalds length -= write_length; 4601da177e4SLinus Torvalds start_address += write_length; 4611da177e4SLinus Torvalds buffer += write_length; 4621da177e4SLinus Torvalds 4632742fd88SAlan Cox /* We should be aligned now -- can write 4642742fd88SAlan Cox max page size bytes at a time */ 4651da177e4SLinus Torvalds while (length) { 4661da177e4SLinus Torvalds if (length > EPROM_PAGE_SIZE) 4671da177e4SLinus Torvalds write_length = EPROM_PAGE_SIZE; 4681da177e4SLinus Torvalds else 4691da177e4SLinus Torvalds write_length = length; 4701da177e4SLinus Torvalds 4712742fd88SAlan Cox dbg("%s - Page Write Addr = %x, length = %d", 4722742fd88SAlan Cox __func__, start_address, write_length); 4732742fd88SAlan Cox usb_serial_debug_data(debug, &serial->serial->dev->dev, 4742742fd88SAlan Cox __func__, write_length, buffer); 4751da177e4SLinus Torvalds 4761da177e4SLinus Torvalds /* Write next page */ 4771da177e4SLinus Torvalds be_start_address = cpu_to_be16(start_address); 4782742fd88SAlan Cox status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE, 4792742fd88SAlan Cox (__u16)address_type, 4802742fd88SAlan Cox (__force __u16)be_start_address, 4812742fd88SAlan Cox buffer, write_length); 4821da177e4SLinus Torvalds if (status) { 4832742fd88SAlan Cox dev_err(&serial->serial->dev->dev, "%s - ERROR %d\n", 4842742fd88SAlan Cox __func__, status); 4851da177e4SLinus Torvalds return status; 4861da177e4SLinus Torvalds } 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds length -= write_length; 4891da177e4SLinus Torvalds start_address += write_length; 4901da177e4SLinus Torvalds buffer += write_length; 4911da177e4SLinus Torvalds } 4921da177e4SLinus Torvalds return status; 4931da177e4SLinus Torvalds } 4941da177e4SLinus Torvalds 4951da177e4SLinus Torvalds /* Examine the UMP DMA registers and LSR 4961da177e4SLinus Torvalds * 4971da177e4SLinus Torvalds * Check the MSBit of the X and Y DMA byte count registers. 4981da177e4SLinus Torvalds * A zero in this bit indicates that the TX DMA buffers are empty 4991da177e4SLinus Torvalds * then check the TX Empty bit in the UART. 5001da177e4SLinus Torvalds */ 5012742fd88SAlan Cox static int tx_active(struct edgeport_port *port) 5021da177e4SLinus Torvalds { 5031da177e4SLinus Torvalds int status; 5041da177e4SLinus Torvalds struct out_endpoint_desc_block *oedb; 5051da177e4SLinus Torvalds __u8 *lsr; 5061da177e4SLinus Torvalds int bytes_left = 0; 5071da177e4SLinus Torvalds 5081da177e4SLinus Torvalds oedb = kmalloc(sizeof(*oedb), GFP_KERNEL); 5091da177e4SLinus Torvalds if (!oedb) { 510441b62c1SHarvey Harrison dev_err(&port->port->dev, "%s - out of memory\n", __func__); 5111da177e4SLinus Torvalds return -ENOMEM; 5121da177e4SLinus Torvalds } 5131da177e4SLinus Torvalds 5141da177e4SLinus Torvalds lsr = kmalloc(1, GFP_KERNEL); /* Sigh, that's right, just one byte, 5151da177e4SLinus Torvalds as not all platforms can do DMA 5161da177e4SLinus Torvalds from stack */ 5171da177e4SLinus Torvalds if (!lsr) { 5181da177e4SLinus Torvalds kfree(oedb); 5191da177e4SLinus Torvalds return -ENOMEM; 5201da177e4SLinus Torvalds } 5211da177e4SLinus Torvalds /* Read the DMA Count Registers */ 5222742fd88SAlan Cox status = read_ram(port->port->serial->dev, port->dma_address, 5232742fd88SAlan Cox sizeof(*oedb), (void *)oedb); 5241da177e4SLinus Torvalds if (status) 5251da177e4SLinus Torvalds goto exit_is_tx_active; 5261da177e4SLinus Torvalds 527441b62c1SHarvey Harrison dbg("%s - XByteCount 0x%X", __func__, oedb->XByteCount); 5281da177e4SLinus Torvalds 5291da177e4SLinus Torvalds /* and the LSR */ 5302742fd88SAlan Cox status = read_ram(port->port->serial->dev, 5312742fd88SAlan Cox port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr); 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds if (status) 5341da177e4SLinus Torvalds goto exit_is_tx_active; 535441b62c1SHarvey Harrison dbg("%s - LSR = 0x%X", __func__, *lsr); 5361da177e4SLinus Torvalds 5371da177e4SLinus Torvalds /* If either buffer has data or we are transmitting then return TRUE */ 5381da177e4SLinus Torvalds if ((oedb->XByteCount & 0x80) != 0) 5391da177e4SLinus Torvalds bytes_left += 64; 5401da177e4SLinus Torvalds 5411da177e4SLinus Torvalds if ((*lsr & UMP_UART_LSR_TX_MASK) == 0) 5421da177e4SLinus Torvalds bytes_left += 1; 5431da177e4SLinus Torvalds 5441da177e4SLinus Torvalds /* We return Not Active if we get any kind of error */ 5451da177e4SLinus Torvalds exit_is_tx_active: 546441b62c1SHarvey Harrison dbg("%s - return %d", __func__, bytes_left); 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds kfree(lsr); 5491da177e4SLinus Torvalds kfree(oedb); 5501da177e4SLinus Torvalds return bytes_left; 5511da177e4SLinus Torvalds } 5521da177e4SLinus Torvalds 5532742fd88SAlan Cox static void chase_port(struct edgeport_port *port, unsigned long timeout, 5542742fd88SAlan Cox int flush) 5551da177e4SLinus Torvalds { 5561da177e4SLinus Torvalds int baud_rate; 5574a90f09bSAlan Cox struct tty_struct *tty = tty_port_tty_get(&port->port->port); 5581da177e4SLinus Torvalds wait_queue_t wait; 5591da177e4SLinus Torvalds unsigned long flags; 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds if (!timeout) 5621da177e4SLinus Torvalds timeout = (HZ * EDGE_CLOSING_WAIT)/100; 5631da177e4SLinus Torvalds 5641da177e4SLinus Torvalds /* wait for data to drain from the buffer */ 5651da177e4SLinus Torvalds spin_lock_irqsave(&port->ep_lock, flags); 5661da177e4SLinus Torvalds init_waitqueue_entry(&wait, current); 5671da177e4SLinus Torvalds add_wait_queue(&tty->write_wait, &wait); 5681da177e4SLinus Torvalds for (;;) { 5691da177e4SLinus Torvalds set_current_state(TASK_INTERRUPTIBLE); 570d733cec1SJohan Hovold if (kfifo_len(&port->write_fifo) == 0 5711da177e4SLinus Torvalds || timeout == 0 || signal_pending(current) 5722742fd88SAlan Cox || !usb_get_intfdata(port->port->serial->interface)) 5732742fd88SAlan Cox /* disconnect */ 5741da177e4SLinus Torvalds break; 5751da177e4SLinus Torvalds spin_unlock_irqrestore(&port->ep_lock, flags); 5761da177e4SLinus Torvalds timeout = schedule_timeout(timeout); 5771da177e4SLinus Torvalds spin_lock_irqsave(&port->ep_lock, flags); 5781da177e4SLinus Torvalds } 5791da177e4SLinus Torvalds set_current_state(TASK_RUNNING); 5801da177e4SLinus Torvalds remove_wait_queue(&tty->write_wait, &wait); 5811da177e4SLinus Torvalds if (flush) 582d733cec1SJohan Hovold kfifo_reset_out(&port->write_fifo); 5831da177e4SLinus Torvalds spin_unlock_irqrestore(&port->ep_lock, flags); 5844a90f09bSAlan Cox tty_kref_put(tty); 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds /* wait for data to drain from the device */ 5871da177e4SLinus Torvalds timeout += jiffies; 5881da177e4SLinus Torvalds while ((long)(jiffies - timeout) < 0 && !signal_pending(current) 5892742fd88SAlan Cox && usb_get_intfdata(port->port->serial->interface)) { 5902742fd88SAlan Cox /* not disconnected */ 5912742fd88SAlan Cox if (!tx_active(port)) 5921da177e4SLinus Torvalds break; 5931da177e4SLinus Torvalds msleep(10); 5941da177e4SLinus Torvalds } 5951da177e4SLinus Torvalds 5961da177e4SLinus Torvalds /* disconnected */ 5971da177e4SLinus Torvalds if (!usb_get_intfdata(port->port->serial->interface)) 5981da177e4SLinus Torvalds return; 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds /* wait one more character time, based on baud rate */ 6012742fd88SAlan Cox /* (tx_active doesn't seem to wait for the last byte) */ 6022742fd88SAlan Cox baud_rate = port->baud_rate; 6032742fd88SAlan Cox if (baud_rate == 0) 6041da177e4SLinus Torvalds baud_rate = 50; 605dfa5ec79SJulia Lawall msleep(max(1, DIV_ROUND_UP(10000, baud_rate))); 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds 6082742fd88SAlan Cox static int choose_config(struct usb_device *dev) 6091da177e4SLinus Torvalds { 6102742fd88SAlan Cox /* 6112742fd88SAlan Cox * There may be multiple configurations on this device, in which case 6122742fd88SAlan Cox * we would need to read and parse all of them to find out which one 6132742fd88SAlan Cox * we want. However, we just support one config at this point, 6142742fd88SAlan Cox * configuration # 1, which is Config Descriptor 0. 6152742fd88SAlan Cox */ 6161da177e4SLinus Torvalds 6172742fd88SAlan Cox dbg("%s - Number of Interfaces = %d", 6182742fd88SAlan Cox __func__, dev->config->desc.bNumInterfaces); 6192742fd88SAlan Cox dbg("%s - MAX Power = %d", 6202742fd88SAlan Cox __func__, dev->config->desc.bMaxPower * 2); 6211da177e4SLinus Torvalds 6221da177e4SLinus Torvalds if (dev->config->desc.bNumInterfaces != 1) { 6232742fd88SAlan Cox dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", 6242742fd88SAlan Cox __func__); 6251da177e4SLinus Torvalds return -ENODEV; 6261da177e4SLinus Torvalds } 6271da177e4SLinus Torvalds 6281da177e4SLinus Torvalds return 0; 6291da177e4SLinus Torvalds } 6301da177e4SLinus Torvalds 6312742fd88SAlan Cox static int read_rom(struct edgeport_serial *serial, 6322742fd88SAlan Cox int start_address, int length, __u8 *buffer) 6331da177e4SLinus Torvalds { 6341da177e4SLinus Torvalds int status; 6351da177e4SLinus Torvalds 6361da177e4SLinus Torvalds if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) { 6372742fd88SAlan Cox status = read_download_mem(serial->serial->dev, 6381da177e4SLinus Torvalds start_address, 6391da177e4SLinus Torvalds length, 6401da177e4SLinus Torvalds serial->TI_I2C_Type, 6411da177e4SLinus Torvalds buffer); 6421da177e4SLinus Torvalds } else { 6432742fd88SAlan Cox status = read_boot_mem(serial, start_address, length, 6441da177e4SLinus Torvalds buffer); 6451da177e4SLinus Torvalds } 6461da177e4SLinus Torvalds return status; 6471da177e4SLinus Torvalds } 6481da177e4SLinus Torvalds 6492742fd88SAlan Cox static int write_rom(struct edgeport_serial *serial, int start_address, 6502742fd88SAlan Cox int length, __u8 *buffer) 6511da177e4SLinus Torvalds { 6521da177e4SLinus Torvalds if (serial->product_info.TiMode == TI_MODE_BOOT) 6532742fd88SAlan Cox return write_boot_mem(serial, start_address, length, 6541da177e4SLinus Torvalds buffer); 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) 6572742fd88SAlan Cox return write_i2c_mem(serial, start_address, length, 6582742fd88SAlan Cox serial->TI_I2C_Type, buffer); 6591da177e4SLinus Torvalds return -EINVAL; 6601da177e4SLinus Torvalds } 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds /* Read a descriptor header from I2C based on type */ 6652742fd88SAlan Cox static int get_descriptor_addr(struct edgeport_serial *serial, 6662742fd88SAlan Cox int desc_type, struct ti_i2c_desc *rom_desc) 6671da177e4SLinus Torvalds { 6681da177e4SLinus Torvalds int start_address; 6691da177e4SLinus Torvalds int status; 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds /* Search for requested descriptor in I2C */ 6721da177e4SLinus Torvalds start_address = 2; 6731da177e4SLinus Torvalds do { 6742742fd88SAlan Cox status = read_rom(serial, 6751da177e4SLinus Torvalds start_address, 6761da177e4SLinus Torvalds sizeof(struct ti_i2c_desc), 6771da177e4SLinus Torvalds (__u8 *)rom_desc); 6781da177e4SLinus Torvalds if (status) 6791da177e4SLinus Torvalds return 0; 6801da177e4SLinus Torvalds 6811da177e4SLinus Torvalds if (rom_desc->Type == desc_type) 6821da177e4SLinus Torvalds return start_address; 6831da177e4SLinus Torvalds 6842742fd88SAlan Cox start_address = start_address + sizeof(struct ti_i2c_desc) 6852742fd88SAlan Cox + rom_desc->Size; 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type); 6881da177e4SLinus Torvalds 6891da177e4SLinus Torvalds return 0; 6901da177e4SLinus Torvalds } 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds /* Validate descriptor checksum */ 6932742fd88SAlan Cox static int valid_csum(struct ti_i2c_desc *rom_desc, __u8 *buffer) 6941da177e4SLinus Torvalds { 6951da177e4SLinus Torvalds __u16 i; 6961da177e4SLinus Torvalds __u8 cs = 0; 6971da177e4SLinus Torvalds 6982742fd88SAlan Cox for (i = 0; i < rom_desc->Size; i++) 6991da177e4SLinus Torvalds cs = (__u8)(cs + buffer[i]); 7002742fd88SAlan Cox 7011da177e4SLinus Torvalds if (cs != rom_desc->CheckSum) { 702441b62c1SHarvey Harrison dbg("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs); 7031da177e4SLinus Torvalds return -EINVAL; 7041da177e4SLinus Torvalds } 7051da177e4SLinus Torvalds return 0; 7061da177e4SLinus Torvalds } 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds /* Make sure that the I2C image is good */ 7092742fd88SAlan Cox static int check_i2c_image(struct edgeport_serial *serial) 7101da177e4SLinus Torvalds { 7111da177e4SLinus Torvalds struct device *dev = &serial->serial->dev->dev; 7121da177e4SLinus Torvalds int status = 0; 7131da177e4SLinus Torvalds struct ti_i2c_desc *rom_desc; 7141da177e4SLinus Torvalds int start_address = 2; 7151da177e4SLinus Torvalds __u8 *buffer; 7161da177e4SLinus Torvalds __u16 ttype; 7171da177e4SLinus Torvalds 7181da177e4SLinus Torvalds rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL); 7191da177e4SLinus Torvalds if (!rom_desc) { 720441b62c1SHarvey Harrison dev_err(dev, "%s - out of memory\n", __func__); 7211da177e4SLinus Torvalds return -ENOMEM; 7221da177e4SLinus Torvalds } 7231da177e4SLinus Torvalds buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL); 7241da177e4SLinus Torvalds if (!buffer) { 7252742fd88SAlan Cox dev_err(dev, "%s - out of memory when allocating buffer\n", 7262742fd88SAlan Cox __func__); 7271da177e4SLinus Torvalds kfree(rom_desc); 7281da177e4SLinus Torvalds return -ENOMEM; 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds 7312742fd88SAlan Cox /* Read the first byte (Signature0) must be 0x52 or 0x10 */ 7322742fd88SAlan Cox status = read_rom(serial, 0, 1, buffer); 7331da177e4SLinus Torvalds if (status) 7342742fd88SAlan Cox goto out; 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds if (*buffer != UMP5152 && *buffer != UMP3410) { 737441b62c1SHarvey Harrison dev_err(dev, "%s - invalid buffer signature\n", __func__); 7381da177e4SLinus Torvalds status = -ENODEV; 7392742fd88SAlan Cox goto out; 7401da177e4SLinus Torvalds } 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds do { 7432742fd88SAlan Cox /* Validate the I2C */ 7442742fd88SAlan Cox status = read_rom(serial, 7451da177e4SLinus Torvalds start_address, 7461da177e4SLinus Torvalds sizeof(struct ti_i2c_desc), 7471da177e4SLinus Torvalds (__u8 *)rom_desc); 7481da177e4SLinus Torvalds if (status) 7491da177e4SLinus Torvalds break; 7501da177e4SLinus Torvalds 7512742fd88SAlan Cox if ((start_address + sizeof(struct ti_i2c_desc) + 7522742fd88SAlan Cox rom_desc->Size) > TI_MAX_I2C_SIZE) { 7531da177e4SLinus Torvalds status = -ENODEV; 754441b62c1SHarvey Harrison dbg("%s - structure too big, erroring out.", __func__); 7551da177e4SLinus Torvalds break; 7561da177e4SLinus Torvalds } 7571da177e4SLinus Torvalds 758441b62c1SHarvey Harrison dbg("%s Type = 0x%x", __func__, rom_desc->Type); 7591da177e4SLinus Torvalds 7602742fd88SAlan Cox /* Skip type 2 record */ 7611da177e4SLinus Torvalds ttype = rom_desc->Type & 0x0f; 7621da177e4SLinus Torvalds if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC 7631da177e4SLinus Torvalds && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) { 7642742fd88SAlan Cox /* Read the descriptor data */ 7652742fd88SAlan Cox status = read_rom(serial, start_address + 7662742fd88SAlan Cox sizeof(struct ti_i2c_desc), 7672742fd88SAlan Cox rom_desc->Size, buffer); 7681da177e4SLinus Torvalds if (status) 7691da177e4SLinus Torvalds break; 7701da177e4SLinus Torvalds 7712742fd88SAlan Cox status = valid_csum(rom_desc, buffer); 7721da177e4SLinus Torvalds if (status) 7731da177e4SLinus Torvalds break; 7741da177e4SLinus Torvalds } 7752742fd88SAlan Cox start_address = start_address + sizeof(struct ti_i2c_desc) + 7762742fd88SAlan Cox rom_desc->Size; 7771da177e4SLinus Torvalds 7782742fd88SAlan Cox } while ((rom_desc->Type != I2C_DESC_TYPE_ION) && 7792742fd88SAlan Cox (start_address < TI_MAX_I2C_SIZE)); 7801da177e4SLinus Torvalds 7812742fd88SAlan Cox if ((rom_desc->Type != I2C_DESC_TYPE_ION) || 7822742fd88SAlan Cox (start_address > TI_MAX_I2C_SIZE)) 7831da177e4SLinus Torvalds status = -ENODEV; 7841da177e4SLinus Torvalds 7852742fd88SAlan Cox out: 7861da177e4SLinus Torvalds kfree(buffer); 7871da177e4SLinus Torvalds kfree(rom_desc); 7881da177e4SLinus Torvalds return status; 7891da177e4SLinus Torvalds } 7901da177e4SLinus Torvalds 7912742fd88SAlan Cox static int get_manuf_info(struct edgeport_serial *serial, __u8 *buffer) 7921da177e4SLinus Torvalds { 7931da177e4SLinus Torvalds int status; 7941da177e4SLinus Torvalds int start_address; 7951da177e4SLinus Torvalds struct ti_i2c_desc *rom_desc; 7961da177e4SLinus Torvalds struct edge_ti_manuf_descriptor *desc; 7971da177e4SLinus Torvalds 7981da177e4SLinus Torvalds rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL); 7991da177e4SLinus Torvalds if (!rom_desc) { 8002742fd88SAlan Cox dev_err(&serial->serial->dev->dev, "%s - out of memory\n", 8012742fd88SAlan Cox __func__); 8021da177e4SLinus Torvalds return -ENOMEM; 8031da177e4SLinus Torvalds } 8042742fd88SAlan Cox start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION, 8052742fd88SAlan Cox rom_desc); 8061da177e4SLinus Torvalds 8071da177e4SLinus Torvalds if (!start_address) { 808441b62c1SHarvey Harrison dbg("%s - Edge Descriptor not found in I2C", __func__); 8091da177e4SLinus Torvalds status = -ENODEV; 8101da177e4SLinus Torvalds goto exit; 8111da177e4SLinus Torvalds } 8121da177e4SLinus Torvalds 8132742fd88SAlan Cox /* Read the descriptor data */ 8142742fd88SAlan Cox status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc), 8152742fd88SAlan Cox rom_desc->Size, buffer); 8161da177e4SLinus Torvalds if (status) 8171da177e4SLinus Torvalds goto exit; 8181da177e4SLinus Torvalds 8192742fd88SAlan Cox status = valid_csum(rom_desc, buffer); 8201da177e4SLinus Torvalds 8211da177e4SLinus Torvalds desc = (struct edge_ti_manuf_descriptor *)buffer; 822441b62c1SHarvey Harrison dbg("%s - IonConfig 0x%x", __func__, desc->IonConfig); 823441b62c1SHarvey Harrison dbg("%s - Version %d", __func__, desc->Version); 824441b62c1SHarvey Harrison dbg("%s - Cpu/Board 0x%x", __func__, desc->CpuRev_BoardRev); 825441b62c1SHarvey Harrison dbg("%s - NumPorts %d", __func__, desc->NumPorts); 826441b62c1SHarvey Harrison dbg("%s - NumVirtualPorts %d", __func__, desc->NumVirtualPorts); 827441b62c1SHarvey Harrison dbg("%s - TotalPorts %d", __func__, desc->TotalPorts); 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds exit: 8301da177e4SLinus Torvalds kfree(rom_desc); 8311da177e4SLinus Torvalds return status; 8321da177e4SLinus Torvalds } 8331da177e4SLinus Torvalds 8341da177e4SLinus Torvalds /* Build firmware header used for firmware update */ 8352742fd88SAlan Cox static int build_i2c_fw_hdr(__u8 *header, struct device *dev) 8361da177e4SLinus Torvalds { 8371da177e4SLinus Torvalds __u8 *buffer; 8381da177e4SLinus Torvalds int buffer_size; 8391da177e4SLinus Torvalds int i; 840d12b219aSJaswinder Singh int err; 8411da177e4SLinus Torvalds __u8 cs = 0; 8421da177e4SLinus Torvalds struct ti_i2c_desc *i2c_header; 8431da177e4SLinus Torvalds struct ti_i2c_image_header *img_header; 8441da177e4SLinus Torvalds struct ti_i2c_firmware_rec *firmware_rec; 845d12b219aSJaswinder Singh const struct firmware *fw; 846d12b219aSJaswinder Singh const char *fw_name = "edgeport/down3.bin"; 8471da177e4SLinus Torvalds 8482742fd88SAlan Cox /* In order to update the I2C firmware we must change the type 2 record 8492742fd88SAlan Cox * to type 0xF2. This will force the UMP to come up in Boot Mode. 8502742fd88SAlan Cox * Then while in boot mode, the driver will download the latest 8512742fd88SAlan Cox * firmware (padded to 15.5k) into the UMP ram. And finally when the 8522742fd88SAlan Cox * device comes back up in download mode the driver will cause the new 8532742fd88SAlan Cox * firmware to be copied from the UMP Ram to I2C and the firmware will 8542742fd88SAlan Cox * update the record type from 0xf2 to 0x02. 8552742fd88SAlan Cox */ 8561da177e4SLinus Torvalds 8572742fd88SAlan Cox /* Allocate a 15.5k buffer + 2 bytes for version number 8582742fd88SAlan Cox * (Firmware Record) */ 8592742fd88SAlan Cox buffer_size = (((1024 * 16) - 512 ) + 8602742fd88SAlan Cox sizeof(struct ti_i2c_firmware_rec)); 8611da177e4SLinus Torvalds 8621da177e4SLinus Torvalds buffer = kmalloc(buffer_size, GFP_KERNEL); 8631da177e4SLinus Torvalds if (!buffer) { 864441b62c1SHarvey Harrison dev_err(dev, "%s - out of memory\n", __func__); 8651da177e4SLinus Torvalds return -ENOMEM; 8661da177e4SLinus Torvalds } 8671da177e4SLinus Torvalds 8681da177e4SLinus Torvalds // Set entire image of 0xffs 8691da177e4SLinus Torvalds memset(buffer, 0xff, buffer_size); 8701da177e4SLinus Torvalds 871d12b219aSJaswinder Singh err = request_firmware(&fw, fw_name, dev); 872d12b219aSJaswinder Singh if (err) { 873d12b219aSJaswinder Singh printk(KERN_ERR "Failed to load image \"%s\" err %d\n", 874d12b219aSJaswinder Singh fw_name, err); 875d12b219aSJaswinder Singh kfree(buffer); 876d12b219aSJaswinder Singh return err; 877d12b219aSJaswinder Singh } 878d12b219aSJaswinder Singh 879d12b219aSJaswinder Singh /* Save Download Version Number */ 880d12b219aSJaswinder Singh OperationalMajorVersion = fw->data[0]; 881d12b219aSJaswinder Singh OperationalMinorVersion = fw->data[1]; 882d12b219aSJaswinder Singh OperationalBuildNumber = fw->data[2] | (fw->data[3] << 8); 883d12b219aSJaswinder Singh 8842742fd88SAlan Cox /* Copy version number into firmware record */ 8851da177e4SLinus Torvalds firmware_rec = (struct ti_i2c_firmware_rec *)buffer; 8861da177e4SLinus Torvalds 887d12b219aSJaswinder Singh firmware_rec->Ver_Major = OperationalMajorVersion; 888d12b219aSJaswinder Singh firmware_rec->Ver_Minor = OperationalMinorVersion; 8891da177e4SLinus Torvalds 8902742fd88SAlan Cox /* Pointer to fw_down memory image */ 891d12b219aSJaswinder Singh img_header = (struct ti_i2c_image_header *)&fw->data[4]; 8921da177e4SLinus Torvalds 8931da177e4SLinus Torvalds memcpy(buffer + sizeof(struct ti_i2c_firmware_rec), 894d12b219aSJaswinder Singh &fw->data[4 + sizeof(struct ti_i2c_image_header)], 8951da177e4SLinus Torvalds le16_to_cpu(img_header->Length)); 8961da177e4SLinus Torvalds 897d12b219aSJaswinder Singh release_firmware(fw); 898d12b219aSJaswinder Singh 8991da177e4SLinus Torvalds for (i=0; i < buffer_size; i++) { 9001da177e4SLinus Torvalds cs = (__u8)(cs + buffer[i]); 9011da177e4SLinus Torvalds } 9021da177e4SLinus Torvalds 9031da177e4SLinus Torvalds kfree(buffer); 9041da177e4SLinus Torvalds 9052742fd88SAlan Cox /* Build new header */ 9061da177e4SLinus Torvalds i2c_header = (struct ti_i2c_desc *)header; 9071da177e4SLinus Torvalds firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data; 9081da177e4SLinus Torvalds 9091da177e4SLinus Torvalds i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK; 9101da177e4SLinus Torvalds i2c_header->Size = (__u16)buffer_size; 9111da177e4SLinus Torvalds i2c_header->CheckSum = cs; 912d12b219aSJaswinder Singh firmware_rec->Ver_Major = OperationalMajorVersion; 913d12b219aSJaswinder Singh firmware_rec->Ver_Minor = OperationalMinorVersion; 9141da177e4SLinus Torvalds 9151da177e4SLinus Torvalds return 0; 9161da177e4SLinus Torvalds } 9171da177e4SLinus Torvalds 9181da177e4SLinus Torvalds /* Try to figure out what type of I2c we have */ 9192742fd88SAlan Cox static int i2c_type_bootmode(struct edgeport_serial *serial) 9201da177e4SLinus Torvalds { 9211da177e4SLinus Torvalds int status; 922e9305d2fSJohan Hovold u8 *data; 923e9305d2fSJohan Hovold 924e9305d2fSJohan Hovold data = kmalloc(1, GFP_KERNEL); 925e9305d2fSJohan Hovold if (!data) { 926e9305d2fSJohan Hovold dev_err(&serial->serial->dev->dev, 927e9305d2fSJohan Hovold "%s - out of memory\n", __func__); 928e9305d2fSJohan Hovold return -ENOMEM; 929e9305d2fSJohan Hovold } 9301da177e4SLinus Torvalds 9312742fd88SAlan Cox /* Try to read type 2 */ 9322742fd88SAlan Cox status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ, 933e9305d2fSJohan Hovold DTK_ADDR_SPACE_I2C_TYPE_II, 0, data, 0x01); 9341da177e4SLinus Torvalds if (status) 935441b62c1SHarvey Harrison dbg("%s - read 2 status error = %d", __func__, status); 9361da177e4SLinus Torvalds else 937e9305d2fSJohan Hovold dbg("%s - read 2 data = 0x%x", __func__, *data); 938e9305d2fSJohan Hovold if ((!status) && (*data == UMP5152 || *data == UMP3410)) { 939441b62c1SHarvey Harrison dbg("%s - ROM_TYPE_II", __func__); 9401da177e4SLinus Torvalds serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II; 941e9305d2fSJohan Hovold goto out; 9421da177e4SLinus Torvalds } 9431da177e4SLinus Torvalds 9442742fd88SAlan Cox /* Try to read type 3 */ 9452742fd88SAlan Cox status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ, 946e9305d2fSJohan Hovold DTK_ADDR_SPACE_I2C_TYPE_III, 0, data, 0x01); 9471da177e4SLinus Torvalds if (status) 948441b62c1SHarvey Harrison dbg("%s - read 3 status error = %d", __func__, status); 9491da177e4SLinus Torvalds else 950e9305d2fSJohan Hovold dbg("%s - read 2 data = 0x%x", __func__, *data); 951e9305d2fSJohan Hovold if ((!status) && (*data == UMP5152 || *data == UMP3410)) { 952441b62c1SHarvey Harrison dbg("%s - ROM_TYPE_III", __func__); 9531da177e4SLinus Torvalds serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III; 954e9305d2fSJohan Hovold goto out; 9551da177e4SLinus Torvalds } 9561da177e4SLinus Torvalds 957441b62c1SHarvey Harrison dbg("%s - Unknown", __func__); 9581da177e4SLinus Torvalds serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II; 959e9305d2fSJohan Hovold status = -ENODEV; 960e9305d2fSJohan Hovold out: 961e9305d2fSJohan Hovold kfree(data); 962e9305d2fSJohan Hovold return status; 9631da177e4SLinus Torvalds } 9641da177e4SLinus Torvalds 9652742fd88SAlan Cox static int bulk_xfer(struct usb_serial *serial, void *buffer, 9662742fd88SAlan Cox int length, int *num_sent) 9671da177e4SLinus Torvalds { 9681da177e4SLinus Torvalds int status; 9691da177e4SLinus Torvalds 9701da177e4SLinus Torvalds status = usb_bulk_msg(serial->dev, 9711da177e4SLinus Torvalds usb_sndbulkpipe(serial->dev, 9721da177e4SLinus Torvalds serial->port[0]->bulk_out_endpointAddress), 9732742fd88SAlan Cox buffer, length, num_sent, 1000); 9741da177e4SLinus Torvalds return status; 9751da177e4SLinus Torvalds } 9761da177e4SLinus Torvalds 9771da177e4SLinus Torvalds /* Download given firmware image to the device (IN BOOT MODE) */ 9782742fd88SAlan Cox static int download_code(struct edgeport_serial *serial, __u8 *image, 9792742fd88SAlan Cox int image_length) 9801da177e4SLinus Torvalds { 9811da177e4SLinus Torvalds int status = 0; 9821da177e4SLinus Torvalds int pos; 9831da177e4SLinus Torvalds int transfer; 9841da177e4SLinus Torvalds int done; 9851da177e4SLinus Torvalds 9862742fd88SAlan Cox /* Transfer firmware image */ 9871da177e4SLinus Torvalds for (pos = 0; pos < image_length; ) { 9882742fd88SAlan Cox /* Read the next buffer from file */ 9891da177e4SLinus Torvalds transfer = image_length - pos; 9901da177e4SLinus Torvalds if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE) 9911da177e4SLinus Torvalds transfer = EDGE_FW_BULK_MAX_PACKET_SIZE; 9921da177e4SLinus Torvalds 9932742fd88SAlan Cox /* Transfer data */ 9942742fd88SAlan Cox status = bulk_xfer(serial->serial, &image[pos], 9952742fd88SAlan Cox transfer, &done); 9961da177e4SLinus Torvalds if (status) 9971da177e4SLinus Torvalds break; 9982742fd88SAlan Cox /* Advance buffer pointer */ 9991da177e4SLinus Torvalds pos += done; 10001da177e4SLinus Torvalds } 10011da177e4SLinus Torvalds 10021da177e4SLinus Torvalds return status; 10031da177e4SLinus Torvalds } 10041da177e4SLinus Torvalds 10052742fd88SAlan Cox /* FIXME!!! */ 10062742fd88SAlan Cox static int config_boot_dev(struct usb_device *dev) 10071da177e4SLinus Torvalds { 10081da177e4SLinus Torvalds return 0; 10091da177e4SLinus Torvalds } 10101da177e4SLinus Torvalds 10112742fd88SAlan Cox static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc) 10122742fd88SAlan Cox { 10132742fd88SAlan Cox return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev); 10142742fd88SAlan Cox } 10152742fd88SAlan Cox 10161da177e4SLinus Torvalds /** 10171da177e4SLinus Torvalds * DownloadTIFirmware - Download run-time operating firmware to the TI5052 10181da177e4SLinus Torvalds * 10191da177e4SLinus Torvalds * This routine downloads the main operating code into the TI5052, using the 10201da177e4SLinus Torvalds * boot code already burned into E2PROM or ROM. 10211da177e4SLinus Torvalds */ 10222742fd88SAlan Cox static int download_fw(struct edgeport_serial *serial) 10231da177e4SLinus Torvalds { 10241da177e4SLinus Torvalds struct device *dev = &serial->serial->dev->dev; 10251da177e4SLinus Torvalds int status = 0; 10261da177e4SLinus Torvalds int start_address; 10271da177e4SLinus Torvalds struct edge_ti_manuf_descriptor *ti_manuf_desc; 10281da177e4SLinus Torvalds struct usb_interface_descriptor *interface; 10291da177e4SLinus Torvalds int download_cur_ver; 10301da177e4SLinus Torvalds int download_new_ver; 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds /* This routine is entered by both the BOOT mode and the Download mode 10331da177e4SLinus Torvalds * We can determine which code is running by the reading the config 10341da177e4SLinus Torvalds * descriptor and if we have only one bulk pipe it is in boot mode 10351da177e4SLinus Torvalds */ 10361da177e4SLinus Torvalds serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP; 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds /* Default to type 2 i2c */ 10391da177e4SLinus Torvalds serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II; 10401da177e4SLinus Torvalds 10412742fd88SAlan Cox status = choose_config(serial->serial->dev); 10421da177e4SLinus Torvalds if (status) 10431da177e4SLinus Torvalds return status; 10441da177e4SLinus Torvalds 10451da177e4SLinus Torvalds interface = &serial->serial->interface->cur_altsetting->desc; 10461da177e4SLinus Torvalds if (!interface) { 1047441b62c1SHarvey Harrison dev_err(dev, "%s - no interface set, error!\n", __func__); 10481da177e4SLinus Torvalds return -ENODEV; 10491da177e4SLinus Torvalds } 10501da177e4SLinus Torvalds 10512742fd88SAlan Cox /* 10522742fd88SAlan Cox * Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING 10532742fd88SAlan Cox * if we have more than one endpoint we are definitely in download 10542742fd88SAlan Cox * mode 10552742fd88SAlan Cox */ 10561da177e4SLinus Torvalds if (interface->bNumEndpoints > 1) 10571da177e4SLinus Torvalds serial->product_info.TiMode = TI_MODE_DOWNLOAD; 10581da177e4SLinus Torvalds else 10592742fd88SAlan Cox /* Otherwise we will remain in configuring mode */ 10601da177e4SLinus Torvalds serial->product_info.TiMode = TI_MODE_CONFIGURING; 10611da177e4SLinus Torvalds 10621da177e4SLinus Torvalds /********************************************************************/ 10631da177e4SLinus Torvalds /* Download Mode */ 10641da177e4SLinus Torvalds /********************************************************************/ 10651da177e4SLinus Torvalds if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) { 10661da177e4SLinus Torvalds struct ti_i2c_desc *rom_desc; 10671da177e4SLinus Torvalds 10689544e833SAndrew Morton dbg("%s - RUNNING IN DOWNLOAD MODE", __func__); 10691da177e4SLinus Torvalds 10702742fd88SAlan Cox status = check_i2c_image(serial); 10711da177e4SLinus Torvalds if (status) { 10729544e833SAndrew Morton dbg("%s - DOWNLOAD MODE -- BAD I2C", __func__); 10731da177e4SLinus Torvalds return status; 10741da177e4SLinus Torvalds } 10751da177e4SLinus Torvalds 10761da177e4SLinus Torvalds /* Validate Hardware version number 10771da177e4SLinus Torvalds * Read Manufacturing Descriptor from TI Based Edgeport 10781da177e4SLinus Torvalds */ 10791da177e4SLinus Torvalds ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL); 10801da177e4SLinus Torvalds if (!ti_manuf_desc) { 1081441b62c1SHarvey Harrison dev_err(dev, "%s - out of memory.\n", __func__); 10821da177e4SLinus Torvalds return -ENOMEM; 10831da177e4SLinus Torvalds } 10842742fd88SAlan Cox status = get_manuf_info(serial, (__u8 *)ti_manuf_desc); 10851da177e4SLinus Torvalds if (status) { 10861da177e4SLinus Torvalds kfree(ti_manuf_desc); 10871da177e4SLinus Torvalds return status; 10881da177e4SLinus Torvalds } 10891da177e4SLinus Torvalds 10902742fd88SAlan Cox /* Check version number of ION descriptor */ 10912742fd88SAlan Cox if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) { 10922742fd88SAlan Cox dbg("%s - Wrong CPU Rev %d (Must be 2)", 10932742fd88SAlan Cox __func__, ti_cpu_rev(ti_manuf_desc)); 10941da177e4SLinus Torvalds kfree(ti_manuf_desc); 10951da177e4SLinus Torvalds return -EINVAL; 10961da177e4SLinus Torvalds } 10971da177e4SLinus Torvalds 10981da177e4SLinus Torvalds rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL); 10991da177e4SLinus Torvalds if (!rom_desc) { 1100441b62c1SHarvey Harrison dev_err(dev, "%s - out of memory.\n", __func__); 11011da177e4SLinus Torvalds kfree(ti_manuf_desc); 11021da177e4SLinus Torvalds return -ENOMEM; 11031da177e4SLinus Torvalds } 11041da177e4SLinus Torvalds 11052742fd88SAlan Cox /* Search for type 2 record (firmware record) */ 11062742fd88SAlan Cox start_address = get_descriptor_addr(serial, 11072742fd88SAlan Cox I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc); 11082742fd88SAlan Cox if (start_address != 0) { 11091da177e4SLinus Torvalds struct ti_i2c_firmware_rec *firmware_version; 1110e9305d2fSJohan Hovold u8 *record; 11111da177e4SLinus Torvalds 11122742fd88SAlan Cox dbg("%s - Found Type FIRMWARE (Type 2) record", 11132742fd88SAlan Cox __func__); 11141da177e4SLinus Torvalds 11152742fd88SAlan Cox firmware_version = kmalloc(sizeof(*firmware_version), 11162742fd88SAlan Cox GFP_KERNEL); 11171da177e4SLinus Torvalds if (!firmware_version) { 1118441b62c1SHarvey Harrison dev_err(dev, "%s - out of memory.\n", __func__); 11191da177e4SLinus Torvalds kfree(rom_desc); 11201da177e4SLinus Torvalds kfree(ti_manuf_desc); 11211da177e4SLinus Torvalds return -ENOMEM; 11221da177e4SLinus Torvalds } 11231da177e4SLinus Torvalds 11242742fd88SAlan Cox /* Validate version number 11252742fd88SAlan Cox * Read the descriptor data 11262742fd88SAlan Cox */ 11272742fd88SAlan Cox status = read_rom(serial, start_address + 11282742fd88SAlan Cox sizeof(struct ti_i2c_desc), 11291da177e4SLinus Torvalds sizeof(struct ti_i2c_firmware_rec), 11301da177e4SLinus Torvalds (__u8 *)firmware_version); 11311da177e4SLinus Torvalds if (status) { 11321da177e4SLinus Torvalds kfree(firmware_version); 11331da177e4SLinus Torvalds kfree(rom_desc); 11341da177e4SLinus Torvalds kfree(ti_manuf_desc); 11351da177e4SLinus Torvalds return status; 11361da177e4SLinus Torvalds } 11371da177e4SLinus Torvalds 11382742fd88SAlan Cox /* Check version number of download with current 11392742fd88SAlan Cox version in I2c */ 11401da177e4SLinus Torvalds download_cur_ver = (firmware_version->Ver_Major << 8) + 11411da177e4SLinus Torvalds (firmware_version->Ver_Minor); 1142d12b219aSJaswinder Singh download_new_ver = (OperationalMajorVersion << 8) + 1143d12b219aSJaswinder Singh (OperationalMinorVersion); 11441da177e4SLinus Torvalds 11452742fd88SAlan Cox dbg("%s - >> FW Versions Device %d.%d Driver %d.%d", 1146441b62c1SHarvey Harrison __func__, 11471da177e4SLinus Torvalds firmware_version->Ver_Major, 11481da177e4SLinus Torvalds firmware_version->Ver_Minor, 1149d12b219aSJaswinder Singh OperationalMajorVersion, 1150d12b219aSJaswinder Singh OperationalMinorVersion); 11511da177e4SLinus Torvalds 11522742fd88SAlan Cox /* Check if we have an old version in the I2C and 11532742fd88SAlan Cox update if necessary */ 11540827a9ffSGreg Kroah-Hartman if (download_cur_ver < download_new_ver) { 11552742fd88SAlan Cox dbg("%s - Update I2C dld from %d.%d to %d.%d", 1156441b62c1SHarvey Harrison __func__, 11571da177e4SLinus Torvalds firmware_version->Ver_Major, 11581da177e4SLinus Torvalds firmware_version->Ver_Minor, 1159d12b219aSJaswinder Singh OperationalMajorVersion, 1160d12b219aSJaswinder Singh OperationalMinorVersion); 11611da177e4SLinus Torvalds 1162e9305d2fSJohan Hovold record = kmalloc(1, GFP_KERNEL); 1163e9305d2fSJohan Hovold if (!record) { 1164e9305d2fSJohan Hovold dev_err(dev, "%s - out of memory.\n", 1165e9305d2fSJohan Hovold __func__); 1166e9305d2fSJohan Hovold kfree(firmware_version); 1167e9305d2fSJohan Hovold kfree(rom_desc); 1168e9305d2fSJohan Hovold kfree(ti_manuf_desc); 1169e9305d2fSJohan Hovold return -ENOMEM; 1170e9305d2fSJohan Hovold } 11712742fd88SAlan Cox /* In order to update the I2C firmware we must 11722742fd88SAlan Cox * change the type 2 record to type 0xF2. This 11732742fd88SAlan Cox * will force the UMP to come up in Boot Mode. 11742742fd88SAlan Cox * Then while in boot mode, the driver will 11752742fd88SAlan Cox * download the latest firmware (padded to 11762742fd88SAlan Cox * 15.5k) into the UMP ram. Finally when the 11772742fd88SAlan Cox * device comes back up in download mode the 11782742fd88SAlan Cox * driver will cause the new firmware to be 11792742fd88SAlan Cox * copied from the UMP Ram to I2C and the 11802742fd88SAlan Cox * firmware will update the record type from 11812742fd88SAlan Cox * 0xf2 to 0x02. 11822742fd88SAlan Cox */ 1183e9305d2fSJohan Hovold *record = I2C_DESC_TYPE_FIRMWARE_BLANK; 11841da177e4SLinus Torvalds 11852742fd88SAlan Cox /* Change the I2C Firmware record type to 11862742fd88SAlan Cox 0xf2 to trigger an update */ 11872742fd88SAlan Cox status = write_rom(serial, start_address, 1188e9305d2fSJohan Hovold sizeof(*record), record); 11891da177e4SLinus Torvalds if (status) { 1190e9305d2fSJohan Hovold kfree(record); 11911da177e4SLinus Torvalds kfree(firmware_version); 11921da177e4SLinus Torvalds kfree(rom_desc); 11931da177e4SLinus Torvalds kfree(ti_manuf_desc); 11941da177e4SLinus Torvalds return status; 11951da177e4SLinus Torvalds } 11961da177e4SLinus Torvalds 11972742fd88SAlan Cox /* verify the write -- must do this in order 11982742fd88SAlan Cox * for write to complete before we do the 11992742fd88SAlan Cox * hardware reset 12002742fd88SAlan Cox */ 12012742fd88SAlan Cox status = read_rom(serial, 12021da177e4SLinus Torvalds start_address, 1203e9305d2fSJohan Hovold sizeof(*record), 1204e9305d2fSJohan Hovold record); 12051da177e4SLinus Torvalds if (status) { 1206e9305d2fSJohan Hovold kfree(record); 12071da177e4SLinus Torvalds kfree(firmware_version); 12081da177e4SLinus Torvalds kfree(rom_desc); 12091da177e4SLinus Torvalds kfree(ti_manuf_desc); 12101da177e4SLinus Torvalds return status; 12111da177e4SLinus Torvalds } 12121da177e4SLinus Torvalds 1213e9305d2fSJohan Hovold if (*record != I2C_DESC_TYPE_FIRMWARE_BLANK) { 12142742fd88SAlan Cox dev_err(dev, 12152742fd88SAlan Cox "%s - error resetting device\n", 12162742fd88SAlan Cox __func__); 1217e9305d2fSJohan Hovold kfree(record); 12181da177e4SLinus Torvalds kfree(firmware_version); 12191da177e4SLinus Torvalds kfree(rom_desc); 12201da177e4SLinus Torvalds kfree(ti_manuf_desc); 12211da177e4SLinus Torvalds return -ENODEV; 12221da177e4SLinus Torvalds } 12231da177e4SLinus Torvalds 1224441b62c1SHarvey Harrison dbg("%s - HARDWARE RESET", __func__); 12251da177e4SLinus Torvalds 12262742fd88SAlan Cox /* Reset UMP -- Back to BOOT MODE */ 12272742fd88SAlan Cox status = ti_vsend_sync(serial->serial->dev, 12282742fd88SAlan Cox UMPC_HARDWARE_RESET, 12292742fd88SAlan Cox 0, 0, NULL, 0); 12301da177e4SLinus Torvalds 12312742fd88SAlan Cox dbg("%s - HARDWARE RESET return %d", 12322742fd88SAlan Cox __func__, status); 12331da177e4SLinus Torvalds 12341da177e4SLinus Torvalds /* return an error on purpose. */ 1235e9305d2fSJohan Hovold kfree(record); 12361da177e4SLinus Torvalds kfree(firmware_version); 12371da177e4SLinus Torvalds kfree(rom_desc); 12381da177e4SLinus Torvalds kfree(ti_manuf_desc); 12391da177e4SLinus Torvalds return -ENODEV; 12401da177e4SLinus Torvalds } 12411da177e4SLinus Torvalds kfree(firmware_version); 12421da177e4SLinus Torvalds } 12432742fd88SAlan Cox /* Search for type 0xF2 record (firmware blank record) */ 12442742fd88SAlan Cox else if ((start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) { 12452742fd88SAlan Cox #define HEADER_SIZE (sizeof(struct ti_i2c_desc) + \ 12462742fd88SAlan Cox sizeof(struct ti_i2c_firmware_rec)) 12471da177e4SLinus Torvalds __u8 *header; 12481da177e4SLinus Torvalds __u8 *vheader; 12491da177e4SLinus Torvalds 12501da177e4SLinus Torvalds header = kmalloc(HEADER_SIZE, GFP_KERNEL); 12511da177e4SLinus Torvalds if (!header) { 1252441b62c1SHarvey Harrison dev_err(dev, "%s - out of memory.\n", __func__); 12531da177e4SLinus Torvalds kfree(rom_desc); 12541da177e4SLinus Torvalds kfree(ti_manuf_desc); 12551da177e4SLinus Torvalds return -ENOMEM; 12561da177e4SLinus Torvalds } 12571da177e4SLinus Torvalds 12581da177e4SLinus Torvalds vheader = kmalloc(HEADER_SIZE, GFP_KERNEL); 12591da177e4SLinus Torvalds if (!vheader) { 1260441b62c1SHarvey Harrison dev_err(dev, "%s - out of memory.\n", __func__); 12611da177e4SLinus Torvalds kfree(header); 12621da177e4SLinus Torvalds kfree(rom_desc); 12631da177e4SLinus Torvalds kfree(ti_manuf_desc); 12641da177e4SLinus Torvalds return -ENOMEM; 12651da177e4SLinus Torvalds } 12661da177e4SLinus Torvalds 12672742fd88SAlan Cox dbg("%s - Found Type BLANK FIRMWARE (Type F2) record", 12682742fd88SAlan Cox __func__); 12691da177e4SLinus Torvalds 12702742fd88SAlan Cox /* 12712742fd88SAlan Cox * In order to update the I2C firmware we must change 12722742fd88SAlan Cox * the type 2 record to type 0xF2. This will force the 12732742fd88SAlan Cox * UMP to come up in Boot Mode. Then while in boot 12742742fd88SAlan Cox * mode, the driver will download the latest firmware 12752742fd88SAlan Cox * (padded to 15.5k) into the UMP ram. Finally when the 12762742fd88SAlan Cox * device comes back up in download mode the driver 12772742fd88SAlan Cox * will cause the new firmware to be copied from the 12782742fd88SAlan Cox * UMP Ram to I2C and the firmware will update the 12792742fd88SAlan Cox * record type from 0xf2 to 0x02. 12802742fd88SAlan Cox */ 12812742fd88SAlan Cox status = build_i2c_fw_hdr(header, dev); 12821da177e4SLinus Torvalds if (status) { 12831da177e4SLinus Torvalds kfree(vheader); 12841da177e4SLinus Torvalds kfree(header); 12851da177e4SLinus Torvalds kfree(rom_desc); 12861da177e4SLinus Torvalds kfree(ti_manuf_desc); 1287*fd6e5bbbSRoel Kluin return -EINVAL; 12881da177e4SLinus Torvalds } 12891da177e4SLinus Torvalds 12902742fd88SAlan Cox /* Update I2C with type 0xf2 record with correct 12912742fd88SAlan Cox size and checksum */ 12922742fd88SAlan Cox status = write_rom(serial, 12931da177e4SLinus Torvalds start_address, 12941da177e4SLinus Torvalds HEADER_SIZE, 12951da177e4SLinus Torvalds header); 12961da177e4SLinus Torvalds if (status) { 12971da177e4SLinus Torvalds kfree(vheader); 12981da177e4SLinus Torvalds kfree(header); 12991da177e4SLinus Torvalds kfree(rom_desc); 13001da177e4SLinus Torvalds kfree(ti_manuf_desc); 13019800eb33SRoel Kluin return -EINVAL; 13021da177e4SLinus Torvalds } 13031da177e4SLinus Torvalds 13042742fd88SAlan Cox /* verify the write -- must do this in order for 13052742fd88SAlan Cox write to complete before we do the hardware reset */ 13062742fd88SAlan Cox status = read_rom(serial, start_address, 13072742fd88SAlan Cox HEADER_SIZE, vheader); 13081da177e4SLinus Torvalds 13091da177e4SLinus Torvalds if (status) { 1310441b62c1SHarvey Harrison dbg("%s - can't read header back", __func__); 13111da177e4SLinus Torvalds kfree(vheader); 13121da177e4SLinus Torvalds kfree(header); 13131da177e4SLinus Torvalds kfree(rom_desc); 13141da177e4SLinus Torvalds kfree(ti_manuf_desc); 13151da177e4SLinus Torvalds return status; 13161da177e4SLinus Torvalds } 13171da177e4SLinus Torvalds if (memcmp(vheader, header, HEADER_SIZE)) { 13182742fd88SAlan Cox dbg("%s - write download record failed", 13192742fd88SAlan Cox __func__); 13201da177e4SLinus Torvalds kfree(vheader); 13211da177e4SLinus Torvalds kfree(header); 13221da177e4SLinus Torvalds kfree(rom_desc); 13231da177e4SLinus Torvalds kfree(ti_manuf_desc); 13241e29709eSRoel Kluin return -EINVAL; 13251da177e4SLinus Torvalds } 13261da177e4SLinus Torvalds 13271da177e4SLinus Torvalds kfree(vheader); 13281da177e4SLinus Torvalds kfree(header); 13291da177e4SLinus Torvalds 1330441b62c1SHarvey Harrison dbg("%s - Start firmware update", __func__); 13311da177e4SLinus Torvalds 13322742fd88SAlan Cox /* Tell firmware to copy download image into I2C */ 13332742fd88SAlan Cox status = ti_vsend_sync(serial->serial->dev, 13342742fd88SAlan Cox UMPC_COPY_DNLD_TO_I2C, 0, 0, NULL, 0); 13351da177e4SLinus Torvalds 1336441b62c1SHarvey Harrison dbg("%s - Update complete 0x%x", __func__, status); 13371da177e4SLinus Torvalds if (status) { 13382742fd88SAlan Cox dev_err(dev, 13392742fd88SAlan Cox "%s - UMPC_COPY_DNLD_TO_I2C failed\n", 13402742fd88SAlan Cox __func__); 13411da177e4SLinus Torvalds kfree(rom_desc); 13421da177e4SLinus Torvalds kfree(ti_manuf_desc); 13431da177e4SLinus Torvalds return status; 13441da177e4SLinus Torvalds } 13451da177e4SLinus Torvalds } 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds // The device is running the download code 13481da177e4SLinus Torvalds kfree(rom_desc); 13491da177e4SLinus Torvalds kfree(ti_manuf_desc); 13501da177e4SLinus Torvalds return 0; 13511da177e4SLinus Torvalds } 13521da177e4SLinus Torvalds 13531da177e4SLinus Torvalds /********************************************************************/ 13541da177e4SLinus Torvalds /* Boot Mode */ 13551da177e4SLinus Torvalds /********************************************************************/ 13569544e833SAndrew Morton dbg("%s - RUNNING IN BOOT MODE", __func__); 13571da177e4SLinus Torvalds 13582742fd88SAlan Cox /* Configure the TI device so we can use the BULK pipes for download */ 13592742fd88SAlan Cox status = config_boot_dev(serial->serial->dev); 13601da177e4SLinus Torvalds if (status) 13611da177e4SLinus Torvalds return status; 13621da177e4SLinus Torvalds 13632742fd88SAlan Cox if (le16_to_cpu(serial->serial->dev->descriptor.idVendor) 13642742fd88SAlan Cox != USB_VENDOR_ID_ION) { 1365441b62c1SHarvey Harrison dbg("%s - VID = 0x%x", __func__, 13661da177e4SLinus Torvalds le16_to_cpu(serial->serial->dev->descriptor.idVendor)); 13671da177e4SLinus Torvalds serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II; 13682742fd88SAlan Cox goto stayinbootmode; 13691da177e4SLinus Torvalds } 13701da177e4SLinus Torvalds 13712742fd88SAlan Cox /* We have an ION device (I2c Must be programmed) 13722742fd88SAlan Cox Determine I2C image type */ 13732742fd88SAlan Cox if (i2c_type_bootmode(serial)) 13742742fd88SAlan Cox goto stayinbootmode; 13751da177e4SLinus Torvalds 13762742fd88SAlan Cox /* Check for ION Vendor ID and that the I2C is valid */ 13772742fd88SAlan Cox if (!check_i2c_image(serial)) { 13781da177e4SLinus Torvalds struct ti_i2c_image_header *header; 13791da177e4SLinus Torvalds int i; 13801da177e4SLinus Torvalds __u8 cs = 0; 13811da177e4SLinus Torvalds __u8 *buffer; 13821da177e4SLinus Torvalds int buffer_size; 1383d12b219aSJaswinder Singh int err; 1384d12b219aSJaswinder Singh const struct firmware *fw; 1385d12b219aSJaswinder Singh const char *fw_name = "edgeport/down3.bin"; 13861da177e4SLinus Torvalds 13871da177e4SLinus Torvalds /* Validate Hardware version number 13881da177e4SLinus Torvalds * Read Manufacturing Descriptor from TI Based Edgeport 13891da177e4SLinus Torvalds */ 13901da177e4SLinus Torvalds ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL); 13911da177e4SLinus Torvalds if (!ti_manuf_desc) { 1392441b62c1SHarvey Harrison dev_err(dev, "%s - out of memory.\n", __func__); 13931da177e4SLinus Torvalds return -ENOMEM; 13941da177e4SLinus Torvalds } 13952742fd88SAlan Cox status = get_manuf_info(serial, (__u8 *)ti_manuf_desc); 13961da177e4SLinus Torvalds if (status) { 13971da177e4SLinus Torvalds kfree(ti_manuf_desc); 13982742fd88SAlan Cox goto stayinbootmode; 13991da177e4SLinus Torvalds } 14001da177e4SLinus Torvalds 14012742fd88SAlan Cox /* Check for version 2 */ 14022742fd88SAlan Cox if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) { 14032742fd88SAlan Cox dbg("%s - Wrong CPU Rev %d (Must be 2)", 14042742fd88SAlan Cox __func__, ti_cpu_rev(ti_manuf_desc)); 14051da177e4SLinus Torvalds kfree(ti_manuf_desc); 14062742fd88SAlan Cox goto stayinbootmode; 14071da177e4SLinus Torvalds } 14081da177e4SLinus Torvalds 14091da177e4SLinus Torvalds kfree(ti_manuf_desc); 14101da177e4SLinus Torvalds 14111da177e4SLinus Torvalds /* 14122742fd88SAlan Cox * In order to update the I2C firmware we must change the type 14132742fd88SAlan Cox * 2 record to type 0xF2. This will force the UMP to come up 14142742fd88SAlan Cox * in Boot Mode. Then while in boot mode, the driver will 14152742fd88SAlan Cox * download the latest firmware (padded to 15.5k) into the 14162742fd88SAlan Cox * UMP ram. Finally when the device comes back up in download 14172742fd88SAlan Cox * mode the driver will cause the new firmware to be copied 14182742fd88SAlan Cox * from the UMP Ram to I2C and the firmware will update the 14192742fd88SAlan Cox * record type from 0xf2 to 0x02. 14202742fd88SAlan Cox * 14211da177e4SLinus Torvalds * Do we really have to copy the whole firmware image, 14221da177e4SLinus Torvalds * or could we do this in place! 14231da177e4SLinus Torvalds */ 14241da177e4SLinus Torvalds 14252742fd88SAlan Cox /* Allocate a 15.5k buffer + 3 byte header */ 14262742fd88SAlan Cox buffer_size = (((1024 * 16) - 512) + 14272742fd88SAlan Cox sizeof(struct ti_i2c_image_header)); 14281da177e4SLinus Torvalds buffer = kmalloc(buffer_size, GFP_KERNEL); 14291da177e4SLinus Torvalds if (!buffer) { 1430441b62c1SHarvey Harrison dev_err(dev, "%s - out of memory\n", __func__); 14311da177e4SLinus Torvalds return -ENOMEM; 14321da177e4SLinus Torvalds } 14331da177e4SLinus Torvalds 14342742fd88SAlan Cox /* Initialize the buffer to 0xff (pad the buffer) */ 14351da177e4SLinus Torvalds memset(buffer, 0xff, buffer_size); 14361da177e4SLinus Torvalds 1437d12b219aSJaswinder Singh err = request_firmware(&fw, fw_name, dev); 1438d12b219aSJaswinder Singh if (err) { 1439d12b219aSJaswinder Singh printk(KERN_ERR "Failed to load image \"%s\" err %d\n", 1440d12b219aSJaswinder Singh fw_name, err); 1441d12b219aSJaswinder Singh kfree(buffer); 1442d12b219aSJaswinder Singh return err; 1443d12b219aSJaswinder Singh } 1444d12b219aSJaswinder Singh memcpy(buffer, &fw->data[4], fw->size - 4); 1445d12b219aSJaswinder Singh release_firmware(fw); 14461da177e4SLinus Torvalds 14472742fd88SAlan Cox for (i = sizeof(struct ti_i2c_image_header); 14482742fd88SAlan Cox i < buffer_size; i++) { 14491da177e4SLinus Torvalds cs = (__u8)(cs + buffer[i]); 14501da177e4SLinus Torvalds } 14511da177e4SLinus Torvalds 14521da177e4SLinus Torvalds header = (struct ti_i2c_image_header *)buffer; 14531da177e4SLinus Torvalds 14542742fd88SAlan Cox /* update length and checksum after padding */ 14552742fd88SAlan Cox header->Length = cpu_to_le16((__u16)(buffer_size - 14562742fd88SAlan Cox sizeof(struct ti_i2c_image_header))); 14571da177e4SLinus Torvalds header->CheckSum = cs; 14581da177e4SLinus Torvalds 14592742fd88SAlan Cox /* Download the operational code */ 14602742fd88SAlan Cox dbg("%s - Downloading operational code image (TI UMP)", 14612742fd88SAlan Cox __func__); 14622742fd88SAlan Cox status = download_code(serial, buffer, buffer_size); 14631da177e4SLinus Torvalds 14641da177e4SLinus Torvalds kfree(buffer); 14651da177e4SLinus Torvalds 14661da177e4SLinus Torvalds if (status) { 14672742fd88SAlan Cox dbg("%s - Error downloading operational code image", 14682742fd88SAlan Cox __func__); 14691da177e4SLinus Torvalds return status; 14701da177e4SLinus Torvalds } 14711da177e4SLinus Torvalds 14722742fd88SAlan Cox /* Device will reboot */ 14731da177e4SLinus Torvalds serial->product_info.TiMode = TI_MODE_TRANSITIONING; 14741da177e4SLinus Torvalds 14752742fd88SAlan Cox dbg("%s - Download successful -- Device rebooting...", 14762742fd88SAlan Cox __func__); 14771da177e4SLinus Torvalds 14781da177e4SLinus Torvalds /* return an error on purpose */ 14791da177e4SLinus Torvalds return -ENODEV; 14801da177e4SLinus Torvalds } 14811da177e4SLinus Torvalds 14822742fd88SAlan Cox stayinbootmode: 14832742fd88SAlan Cox /* Eprom is invalid or blank stay in boot mode */ 14849544e833SAndrew Morton dbg("%s - STAYING IN BOOT MODE", __func__); 14851da177e4SLinus Torvalds serial->product_info.TiMode = TI_MODE_BOOT; 14861da177e4SLinus Torvalds 14871da177e4SLinus Torvalds return 0; 14881da177e4SLinus Torvalds } 14891da177e4SLinus Torvalds 14901da177e4SLinus Torvalds 14912742fd88SAlan Cox static int ti_do_config(struct edgeport_port *port, int feature, int on) 14921da177e4SLinus Torvalds { 14931da177e4SLinus Torvalds int port_number = port->port->number - port->port->serial->minor; 14942742fd88SAlan Cox on = !!on; /* 1 or 0 not bitmask */ 14952742fd88SAlan Cox return send_cmd(port->port->serial->dev, 14962742fd88SAlan Cox feature, (__u8)(UMPM_UART1_PORT + port_number), 14972742fd88SAlan Cox on, NULL, 0); 14981da177e4SLinus Torvalds } 14991da177e4SLinus Torvalds 15001da177e4SLinus Torvalds 15012742fd88SAlan Cox static int restore_mcr(struct edgeport_port *port, __u8 mcr) 15021da177e4SLinus Torvalds { 15031da177e4SLinus Torvalds int status = 0; 15041da177e4SLinus Torvalds 1505441b62c1SHarvey Harrison dbg("%s - %x", __func__, mcr); 15061da177e4SLinus Torvalds 15072742fd88SAlan Cox status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR); 15081da177e4SLinus Torvalds if (status) 15091da177e4SLinus Torvalds return status; 15102742fd88SAlan Cox status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS); 15111da177e4SLinus Torvalds if (status) 15121da177e4SLinus Torvalds return status; 15132742fd88SAlan Cox return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK); 15141da177e4SLinus Torvalds } 15151da177e4SLinus Torvalds 15161da177e4SLinus Torvalds /* Convert TI LSR to standard UART flags */ 15172742fd88SAlan Cox static __u8 map_line_status(__u8 ti_lsr) 15181da177e4SLinus Torvalds { 15191da177e4SLinus Torvalds __u8 lsr = 0; 15201da177e4SLinus Torvalds 15211da177e4SLinus Torvalds #define MAP_FLAG(flagUmp, flagUart) \ 15221da177e4SLinus Torvalds if (ti_lsr & flagUmp) \ 15231da177e4SLinus Torvalds lsr |= flagUart; 15241da177e4SLinus Torvalds 15251da177e4SLinus Torvalds MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR) /* overrun */ 15261da177e4SLinus Torvalds MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */ 15271da177e4SLinus Torvalds MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */ 15281da177e4SLinus Torvalds MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */ 15292742fd88SAlan Cox MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* rx data available */ 15302742fd88SAlan Cox MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* tx hold reg empty */ 15311da177e4SLinus Torvalds 15321da177e4SLinus Torvalds #undef MAP_FLAG 15331da177e4SLinus Torvalds 15341da177e4SLinus Torvalds return lsr; 15351da177e4SLinus Torvalds } 15361da177e4SLinus Torvalds 15371da177e4SLinus Torvalds static void handle_new_msr(struct edgeport_port *edge_port, __u8 msr) 15381da177e4SLinus Torvalds { 15391da177e4SLinus Torvalds struct async_icount *icount; 15401da177e4SLinus Torvalds struct tty_struct *tty; 15411da177e4SLinus Torvalds 1542441b62c1SHarvey Harrison dbg("%s - %02x", __func__, msr); 15431da177e4SLinus Torvalds 15442742fd88SAlan Cox if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR | 15452742fd88SAlan Cox EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) { 15461da177e4SLinus Torvalds icount = &edge_port->icount; 15471da177e4SLinus Torvalds 15481da177e4SLinus Torvalds /* update input line counters */ 15491da177e4SLinus Torvalds if (msr & EDGEPORT_MSR_DELTA_CTS) 15501da177e4SLinus Torvalds icount->cts++; 15511da177e4SLinus Torvalds if (msr & EDGEPORT_MSR_DELTA_DSR) 15521da177e4SLinus Torvalds icount->dsr++; 15531da177e4SLinus Torvalds if (msr & EDGEPORT_MSR_DELTA_CD) 15541da177e4SLinus Torvalds icount->dcd++; 15551da177e4SLinus Torvalds if (msr & EDGEPORT_MSR_DELTA_RI) 15561da177e4SLinus Torvalds icount->rng++; 15571da177e4SLinus Torvalds wake_up_interruptible(&edge_port->delta_msr_wait); 15581da177e4SLinus Torvalds } 15591da177e4SLinus Torvalds 15601da177e4SLinus Torvalds /* Save the new modem status */ 15611da177e4SLinus Torvalds edge_port->shadow_msr = msr & 0xf0; 15621da177e4SLinus Torvalds 15634a90f09bSAlan Cox tty = tty_port_tty_get(&edge_port->port->port); 15641da177e4SLinus Torvalds /* handle CTS flow control */ 15651da177e4SLinus Torvalds if (tty && C_CRTSCTS(tty)) { 15661da177e4SLinus Torvalds if (msr & EDGEPORT_MSR_CTS) { 15671da177e4SLinus Torvalds tty->hw_stopped = 0; 15681da177e4SLinus Torvalds tty_wakeup(tty); 15691da177e4SLinus Torvalds } else { 15701da177e4SLinus Torvalds tty->hw_stopped = 1; 15711da177e4SLinus Torvalds } 15721da177e4SLinus Torvalds } 15734a90f09bSAlan Cox tty_kref_put(tty); 15741da177e4SLinus Torvalds 15751da177e4SLinus Torvalds return; 15761da177e4SLinus Torvalds } 15771da177e4SLinus Torvalds 15782742fd88SAlan Cox static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data, 15792742fd88SAlan Cox __u8 lsr, __u8 data) 15801da177e4SLinus Torvalds { 15811da177e4SLinus Torvalds struct async_icount *icount; 15822742fd88SAlan Cox __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR | 15832742fd88SAlan Cox LSR_FRM_ERR | LSR_BREAK)); 15844a90f09bSAlan Cox struct tty_struct *tty; 15851da177e4SLinus Torvalds 1586441b62c1SHarvey Harrison dbg("%s - %02x", __func__, new_lsr); 15871da177e4SLinus Torvalds 15881da177e4SLinus Torvalds edge_port->shadow_lsr = lsr; 15891da177e4SLinus Torvalds 15902742fd88SAlan Cox if (new_lsr & LSR_BREAK) 15911da177e4SLinus Torvalds /* 15921da177e4SLinus Torvalds * Parity and Framing errors only count if they 15931da177e4SLinus Torvalds * occur exclusive of a break being received. 15941da177e4SLinus Torvalds */ 15951da177e4SLinus Torvalds new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK); 15961da177e4SLinus Torvalds 15971da177e4SLinus Torvalds /* Place LSR data byte into Rx buffer */ 15984a90f09bSAlan Cox if (lsr_data) { 15994a90f09bSAlan Cox tty = tty_port_tty_get(&edge_port->port->port); 16004a90f09bSAlan Cox if (tty) { 16014a90f09bSAlan Cox edge_tty_recv(&edge_port->port->dev, tty, &data, 1); 16024a90f09bSAlan Cox tty_kref_put(tty); 16034a90f09bSAlan Cox } 16044a90f09bSAlan Cox } 16051da177e4SLinus Torvalds 16061da177e4SLinus Torvalds /* update input line counters */ 16071da177e4SLinus Torvalds icount = &edge_port->icount; 16081da177e4SLinus Torvalds if (new_lsr & LSR_BREAK) 16091da177e4SLinus Torvalds icount->brk++; 16101da177e4SLinus Torvalds if (new_lsr & LSR_OVER_ERR) 16111da177e4SLinus Torvalds icount->overrun++; 16121da177e4SLinus Torvalds if (new_lsr & LSR_PAR_ERR) 16131da177e4SLinus Torvalds icount->parity++; 16141da177e4SLinus Torvalds if (new_lsr & LSR_FRM_ERR) 16151da177e4SLinus Torvalds icount->frame++; 16161da177e4SLinus Torvalds } 16171da177e4SLinus Torvalds 16181da177e4SLinus Torvalds 16197d12e780SDavid Howells static void edge_interrupt_callback(struct urb *urb) 16201da177e4SLinus Torvalds { 1621cdc97792SMing Lei struct edgeport_serial *edge_serial = urb->context; 16221da177e4SLinus Torvalds struct usb_serial_port *port; 16231da177e4SLinus Torvalds struct edgeport_port *edge_port; 16241da177e4SLinus Torvalds unsigned char *data = urb->transfer_buffer; 16251da177e4SLinus Torvalds int length = urb->actual_length; 16261da177e4SLinus Torvalds int port_number; 16271da177e4SLinus Torvalds int function; 1628ee337c21SGreg Kroah-Hartman int retval; 16291da177e4SLinus Torvalds __u8 lsr; 16301da177e4SLinus Torvalds __u8 msr; 1631ee337c21SGreg Kroah-Hartman int status = urb->status; 16321da177e4SLinus Torvalds 1633441b62c1SHarvey Harrison dbg("%s", __func__); 16341da177e4SLinus Torvalds 1635ee337c21SGreg Kroah-Hartman switch (status) { 16361da177e4SLinus Torvalds case 0: 16371da177e4SLinus Torvalds /* success */ 16381da177e4SLinus Torvalds break; 16391da177e4SLinus Torvalds case -ECONNRESET: 16401da177e4SLinus Torvalds case -ENOENT: 16411da177e4SLinus Torvalds case -ESHUTDOWN: 16421da177e4SLinus Torvalds /* this urb is terminated, clean up */ 1643ee337c21SGreg Kroah-Hartman dbg("%s - urb shutting down with status: %d", 1644441b62c1SHarvey Harrison __func__, status); 16451da177e4SLinus Torvalds return; 16461da177e4SLinus Torvalds default: 1647ee337c21SGreg Kroah-Hartman dev_err(&urb->dev->dev, "%s - nonzero urb status received: " 1648441b62c1SHarvey Harrison "%d\n", __func__, status); 16491da177e4SLinus Torvalds goto exit; 16501da177e4SLinus Torvalds } 16511da177e4SLinus Torvalds 16521da177e4SLinus Torvalds if (!length) { 1653441b62c1SHarvey Harrison dbg("%s - no data in urb", __func__); 16541da177e4SLinus Torvalds goto exit; 16551da177e4SLinus Torvalds } 16561da177e4SLinus Torvalds 16572742fd88SAlan Cox usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, 16582742fd88SAlan Cox __func__, length, data); 16591da177e4SLinus Torvalds 16601da177e4SLinus Torvalds if (length != 2) { 16612742fd88SAlan Cox dbg("%s - expecting packet of size 2, got %d", 16622742fd88SAlan Cox __func__, length); 16631da177e4SLinus Torvalds goto exit; 16641da177e4SLinus Torvalds } 16651da177e4SLinus Torvalds 16661da177e4SLinus Torvalds port_number = TIUMP_GET_PORT_FROM_CODE(data[0]); 16671da177e4SLinus Torvalds function = TIUMP_GET_FUNC_FROM_CODE(data[0]); 16681da177e4SLinus Torvalds dbg("%s - port_number %d, function %d, info 0x%x", 1669441b62c1SHarvey Harrison __func__, port_number, function, data[1]); 16701da177e4SLinus Torvalds port = edge_serial->serial->port[port_number]; 16711da177e4SLinus Torvalds edge_port = usb_get_serial_port_data(port); 16721da177e4SLinus Torvalds if (!edge_port) { 1673441b62c1SHarvey Harrison dbg("%s - edge_port not found", __func__); 16741da177e4SLinus Torvalds return; 16751da177e4SLinus Torvalds } 16761da177e4SLinus Torvalds switch (function) { 16771da177e4SLinus Torvalds case TIUMP_INTERRUPT_CODE_LSR: 16782742fd88SAlan Cox lsr = map_line_status(data[1]); 16791da177e4SLinus Torvalds if (lsr & UMP_UART_LSR_DATA_MASK) { 16802742fd88SAlan Cox /* Save the LSR event for bulk read 16812742fd88SAlan Cox completion routine */ 16821da177e4SLinus Torvalds dbg("%s - LSR Event Port %u LSR Status = %02x", 1683441b62c1SHarvey Harrison __func__, port_number, lsr); 16841da177e4SLinus Torvalds edge_port->lsr_event = 1; 16851da177e4SLinus Torvalds edge_port->lsr_mask = lsr; 16861da177e4SLinus Torvalds } else { 16871da177e4SLinus Torvalds dbg("%s - ===== Port %d LSR Status = %02x ======", 1688441b62c1SHarvey Harrison __func__, port_number, lsr); 16891da177e4SLinus Torvalds handle_new_lsr(edge_port, 0, lsr, 0); 16901da177e4SLinus Torvalds } 16911da177e4SLinus Torvalds break; 16921da177e4SLinus Torvalds 16932742fd88SAlan Cox case TIUMP_INTERRUPT_CODE_MSR: /* MSR */ 16941da177e4SLinus Torvalds /* Copy MSR from UMP */ 16951da177e4SLinus Torvalds msr = data[1]; 1696759f3634SJoe Perches dbg("%s - ===== Port %u MSR Status = %02x ======", 1697441b62c1SHarvey Harrison __func__, port_number, msr); 16981da177e4SLinus Torvalds handle_new_msr(edge_port, msr); 16991da177e4SLinus Torvalds break; 17001da177e4SLinus Torvalds 17011da177e4SLinus Torvalds default: 17022742fd88SAlan Cox dev_err(&urb->dev->dev, 17032742fd88SAlan Cox "%s - Unknown Interrupt code from UMP %x\n", 1704441b62c1SHarvey Harrison __func__, data[1]); 17051da177e4SLinus Torvalds break; 17061da177e4SLinus Torvalds 17071da177e4SLinus Torvalds } 17081da177e4SLinus Torvalds 17091da177e4SLinus Torvalds exit: 1710ee337c21SGreg Kroah-Hartman retval = usb_submit_urb(urb, GFP_ATOMIC); 1711ee337c21SGreg Kroah-Hartman if (retval) 17122742fd88SAlan Cox dev_err(&urb->dev->dev, 17132742fd88SAlan Cox "%s - usb_submit_urb failed with result %d\n", 1714441b62c1SHarvey Harrison __func__, retval); 17151da177e4SLinus Torvalds } 17161da177e4SLinus Torvalds 17177d12e780SDavid Howells static void edge_bulk_in_callback(struct urb *urb) 17181da177e4SLinus Torvalds { 1719cdc97792SMing Lei struct edgeport_port *edge_port = urb->context; 17201da177e4SLinus Torvalds unsigned char *data = urb->transfer_buffer; 17211da177e4SLinus Torvalds struct tty_struct *tty; 1722ee337c21SGreg Kroah-Hartman int retval = 0; 17231da177e4SLinus Torvalds int port_number; 1724ee337c21SGreg Kroah-Hartman int status = urb->status; 17251da177e4SLinus Torvalds 1726441b62c1SHarvey Harrison dbg("%s", __func__); 17271da177e4SLinus Torvalds 1728ee337c21SGreg Kroah-Hartman switch (status) { 17291da177e4SLinus Torvalds case 0: 17301da177e4SLinus Torvalds /* success */ 17311da177e4SLinus Torvalds break; 17321da177e4SLinus Torvalds case -ECONNRESET: 17331da177e4SLinus Torvalds case -ENOENT: 17341da177e4SLinus Torvalds case -ESHUTDOWN: 17351da177e4SLinus Torvalds /* this urb is terminated, clean up */ 1736ee337c21SGreg Kroah-Hartman dbg("%s - urb shutting down with status: %d", 1737441b62c1SHarvey Harrison __func__, status); 17381da177e4SLinus Torvalds return; 17391da177e4SLinus Torvalds default: 17402742fd88SAlan Cox dev_err(&urb->dev->dev, 17412742fd88SAlan Cox "%s - nonzero read bulk status received: %d\n", 1742441b62c1SHarvey Harrison __func__, status); 17431da177e4SLinus Torvalds } 17441da177e4SLinus Torvalds 1745ee337c21SGreg Kroah-Hartman if (status == -EPIPE) 17461da177e4SLinus Torvalds goto exit; 17471da177e4SLinus Torvalds 1748ee337c21SGreg Kroah-Hartman if (status) { 1749441b62c1SHarvey Harrison dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__); 17501da177e4SLinus Torvalds return; 17511da177e4SLinus Torvalds } 17521da177e4SLinus Torvalds 17531da177e4SLinus Torvalds port_number = edge_port->port->number - edge_port->port->serial->minor; 17541da177e4SLinus Torvalds 17551da177e4SLinus Torvalds if (edge_port->lsr_event) { 17561da177e4SLinus Torvalds edge_port->lsr_event = 0; 17571da177e4SLinus Torvalds dbg("%s ===== Port %u LSR Status = %02x, Data = %02x ======", 1758441b62c1SHarvey Harrison __func__, port_number, edge_port->lsr_mask, *data); 17591da177e4SLinus Torvalds handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data); 17601da177e4SLinus Torvalds /* Adjust buffer length/pointer */ 17611da177e4SLinus Torvalds --urb->actual_length; 17621da177e4SLinus Torvalds ++data; 17631da177e4SLinus Torvalds } 17641da177e4SLinus Torvalds 17654a90f09bSAlan Cox tty = tty_port_tty_get(&edge_port->port->port); 17661da177e4SLinus Torvalds if (tty && urb->actual_length) { 17672742fd88SAlan Cox usb_serial_debug_data(debug, &edge_port->port->dev, 17682742fd88SAlan Cox __func__, urb->actual_length, data); 17692742fd88SAlan Cox if (edge_port->close_pending) 17702742fd88SAlan Cox dbg("%s - close pending, dropping data on the floor", 17712742fd88SAlan Cox __func__); 17722742fd88SAlan Cox else 17732742fd88SAlan Cox edge_tty_recv(&edge_port->port->dev, tty, data, 17742742fd88SAlan Cox urb->actual_length); 17751da177e4SLinus Torvalds edge_port->icount.rx += urb->actual_length; 17761da177e4SLinus Torvalds } 17774a90f09bSAlan Cox tty_kref_put(tty); 17781da177e4SLinus Torvalds 17791da177e4SLinus Torvalds exit: 17801da177e4SLinus Torvalds /* continue read unless stopped */ 17811da177e4SLinus Torvalds spin_lock(&edge_port->ep_lock); 17821da177e4SLinus Torvalds if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING) { 17831da177e4SLinus Torvalds urb->dev = edge_port->port->serial->dev; 1784ee337c21SGreg Kroah-Hartman retval = usb_submit_urb(urb, GFP_ATOMIC); 17851da177e4SLinus Torvalds } else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING) { 17861da177e4SLinus Torvalds edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED; 17871da177e4SLinus Torvalds } 17881da177e4SLinus Torvalds spin_unlock(&edge_port->ep_lock); 1789ee337c21SGreg Kroah-Hartman if (retval) 17902742fd88SAlan Cox dev_err(&urb->dev->dev, 17912742fd88SAlan Cox "%s - usb_submit_urb failed with result %d\n", 1792441b62c1SHarvey Harrison __func__, retval); 17931da177e4SLinus Torvalds } 17941da177e4SLinus Torvalds 17952742fd88SAlan Cox static void edge_tty_recv(struct device *dev, struct tty_struct *tty, 17962742fd88SAlan Cox unsigned char *data, int length) 17971da177e4SLinus Torvalds { 17982742fd88SAlan Cox int queued; 17991da177e4SLinus Torvalds 18002742fd88SAlan Cox queued = tty_insert_flip_string(tty, data, length); 18012742fd88SAlan Cox if (queued < length) 18021da177e4SLinus Torvalds dev_err(dev, "%s - dropping data, %d bytes lost\n", 18032742fd88SAlan Cox __func__, length - queued); 18041da177e4SLinus Torvalds tty_flip_buffer_push(tty); 18051da177e4SLinus Torvalds } 18061da177e4SLinus Torvalds 18077d12e780SDavid Howells static void edge_bulk_out_callback(struct urb *urb) 18081da177e4SLinus Torvalds { 1809cdc97792SMing Lei struct usb_serial_port *port = urb->context; 18101da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 1811ee337c21SGreg Kroah-Hartman int status = urb->status; 18124a90f09bSAlan Cox struct tty_struct *tty; 18131da177e4SLinus Torvalds 1814441b62c1SHarvey Harrison dbg("%s - port %d", __func__, port->number); 18151da177e4SLinus Torvalds 18161da177e4SLinus Torvalds edge_port->ep_write_urb_in_use = 0; 18171da177e4SLinus Torvalds 1818ee337c21SGreg Kroah-Hartman switch (status) { 18191da177e4SLinus Torvalds case 0: 18201da177e4SLinus Torvalds /* success */ 18211da177e4SLinus Torvalds break; 18221da177e4SLinus Torvalds case -ECONNRESET: 18231da177e4SLinus Torvalds case -ENOENT: 18241da177e4SLinus Torvalds case -ESHUTDOWN: 18251da177e4SLinus Torvalds /* this urb is terminated, clean up */ 1826ee337c21SGreg Kroah-Hartman dbg("%s - urb shutting down with status: %d", 1827441b62c1SHarvey Harrison __func__, status); 18281da177e4SLinus Torvalds return; 18291da177e4SLinus Torvalds default: 1830ee337c21SGreg Kroah-Hartman dev_err(&urb->dev->dev, "%s - nonzero write bulk status " 1831441b62c1SHarvey Harrison "received: %d\n", __func__, status); 18321da177e4SLinus Torvalds } 18331da177e4SLinus Torvalds 18341da177e4SLinus Torvalds /* send any buffered data */ 18354a90f09bSAlan Cox tty = tty_port_tty_get(&port->port); 18364a90f09bSAlan Cox edge_send(tty); 18374a90f09bSAlan Cox tty_kref_put(tty); 18381da177e4SLinus Torvalds } 18391da177e4SLinus Torvalds 1840a509a7e4SAlan Cox static int edge_open(struct tty_struct *tty, struct usb_serial_port *port) 18411da177e4SLinus Torvalds { 18421da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 18431da177e4SLinus Torvalds struct edgeport_serial *edge_serial; 18441da177e4SLinus Torvalds struct usb_device *dev; 18451da177e4SLinus Torvalds struct urb *urb; 18461da177e4SLinus Torvalds int port_number; 18471da177e4SLinus Torvalds int status; 18481da177e4SLinus Torvalds u16 open_settings; 18491da177e4SLinus Torvalds u8 transaction_timeout; 18501da177e4SLinus Torvalds 1851441b62c1SHarvey Harrison dbg("%s - port %d", __func__, port->number); 18521da177e4SLinus Torvalds 18531da177e4SLinus Torvalds if (edge_port == NULL) 18541da177e4SLinus Torvalds return -ENODEV; 18551da177e4SLinus Torvalds 18561da177e4SLinus Torvalds port_number = port->number - port->serial->minor; 18571da177e4SLinus Torvalds switch (port_number) { 18581da177e4SLinus Torvalds case 0: 18591da177e4SLinus Torvalds edge_port->uart_base = UMPMEM_BASE_UART1; 18601da177e4SLinus Torvalds edge_port->dma_address = UMPD_OEDB1_ADDRESS; 18611da177e4SLinus Torvalds break; 18621da177e4SLinus Torvalds case 1: 18631da177e4SLinus Torvalds edge_port->uart_base = UMPMEM_BASE_UART2; 18641da177e4SLinus Torvalds edge_port->dma_address = UMPD_OEDB2_ADDRESS; 18651da177e4SLinus Torvalds break; 18661da177e4SLinus Torvalds default: 18671da177e4SLinus Torvalds dev_err(&port->dev, "Unknown port number!!!\n"); 18681da177e4SLinus Torvalds return -ENODEV; 18691da177e4SLinus Torvalds } 18701da177e4SLinus Torvalds 18711da177e4SLinus Torvalds dbg("%s - port_number = %d, uart_base = %04x, dma_address = %04x", 18722742fd88SAlan Cox __func__, port_number, edge_port->uart_base, 18732742fd88SAlan Cox edge_port->dma_address); 18741da177e4SLinus Torvalds 18751da177e4SLinus Torvalds dev = port->serial->dev; 18761da177e4SLinus Torvalds 18771da177e4SLinus Torvalds memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount)); 18781da177e4SLinus Torvalds init_waitqueue_head(&edge_port->delta_msr_wait); 18791da177e4SLinus Torvalds 18801da177e4SLinus Torvalds /* turn off loopback */ 18812742fd88SAlan Cox status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0); 18821da177e4SLinus Torvalds if (status) { 18832742fd88SAlan Cox dev_err(&port->dev, 18842742fd88SAlan Cox "%s - cannot send clear loopback command, %d\n", 1885441b62c1SHarvey Harrison __func__, status); 18861da177e4SLinus Torvalds return status; 18871da177e4SLinus Torvalds } 18881da177e4SLinus Torvalds 18891da177e4SLinus Torvalds /* set up the port settings */ 189095da310eSAlan Cox if (tty) 18914a90f09bSAlan Cox edge_set_termios(tty, port, tty->termios); 18921da177e4SLinus Torvalds 18931da177e4SLinus Torvalds /* open up the port */ 18941da177e4SLinus Torvalds 18951da177e4SLinus Torvalds /* milliseconds to timeout for DMA transfer */ 18961da177e4SLinus Torvalds transaction_timeout = 2; 18971da177e4SLinus Torvalds 18982742fd88SAlan Cox edge_port->ump_read_timeout = 18992742fd88SAlan Cox max(20, ((transaction_timeout * 3) / 2)); 19001da177e4SLinus Torvalds 19012742fd88SAlan Cox /* milliseconds to timeout for DMA transfer */ 19021da177e4SLinus Torvalds open_settings = (u8)(UMP_DMA_MODE_CONTINOUS | 19031da177e4SLinus Torvalds UMP_PIPE_TRANS_TIMEOUT_ENA | 19041da177e4SLinus Torvalds (transaction_timeout << 2)); 19051da177e4SLinus Torvalds 1906441b62c1SHarvey Harrison dbg("%s - Sending UMPC_OPEN_PORT", __func__); 19071da177e4SLinus Torvalds 19081da177e4SLinus Torvalds /* Tell TI to open and start the port */ 19092742fd88SAlan Cox status = send_cmd(dev, UMPC_OPEN_PORT, 19102742fd88SAlan Cox (u8)(UMPM_UART1_PORT + port_number), open_settings, NULL, 0); 19111da177e4SLinus Torvalds if (status) { 19122742fd88SAlan Cox dev_err(&port->dev, "%s - cannot send open command, %d\n", 19132742fd88SAlan Cox __func__, status); 19141da177e4SLinus Torvalds return status; 19151da177e4SLinus Torvalds } 19161da177e4SLinus Torvalds 19171da177e4SLinus Torvalds /* Start the DMA? */ 19182742fd88SAlan Cox status = send_cmd(dev, UMPC_START_PORT, 19192742fd88SAlan Cox (u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0); 19201da177e4SLinus Torvalds if (status) { 19212742fd88SAlan Cox dev_err(&port->dev, "%s - cannot send start DMA command, %d\n", 19222742fd88SAlan Cox __func__, status); 19231da177e4SLinus Torvalds return status; 19241da177e4SLinus Torvalds } 19251da177e4SLinus Torvalds 19261da177e4SLinus Torvalds /* Clear TX and RX buffers in UMP */ 19272742fd88SAlan Cox status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN); 19281da177e4SLinus Torvalds if (status) { 19292742fd88SAlan Cox dev_err(&port->dev, 19302742fd88SAlan Cox "%s - cannot send clear buffers command, %d\n", 19312742fd88SAlan Cox __func__, status); 19321da177e4SLinus Torvalds return status; 19331da177e4SLinus Torvalds } 19341da177e4SLinus Torvalds 19351da177e4SLinus Torvalds /* Read Initial MSR */ 19362742fd88SAlan Cox status = ti_vread_sync(dev, UMPC_READ_MSR, 0, 19372742fd88SAlan Cox (__u16)(UMPM_UART1_PORT + port_number), 19382742fd88SAlan Cox &edge_port->shadow_msr, 1); 19391da177e4SLinus Torvalds if (status) { 19402742fd88SAlan Cox dev_err(&port->dev, "%s - cannot send read MSR command, %d\n", 19412742fd88SAlan Cox __func__, status); 19421da177e4SLinus Torvalds return status; 19431da177e4SLinus Torvalds } 19441da177e4SLinus Torvalds 19451da177e4SLinus Torvalds dbg("ShadowMSR 0x%X", edge_port->shadow_msr); 19461da177e4SLinus Torvalds 19471da177e4SLinus Torvalds /* Set Initial MCR */ 19481da177e4SLinus Torvalds edge_port->shadow_mcr = MCR_RTS | MCR_DTR; 19491da177e4SLinus Torvalds dbg("ShadowMCR 0x%X", edge_port->shadow_mcr); 19501da177e4SLinus Torvalds 19511da177e4SLinus Torvalds edge_serial = edge_port->edge_serial; 1952241ca64fSMatthias Kaehlcke if (mutex_lock_interruptible(&edge_serial->es_lock)) 19531da177e4SLinus Torvalds return -ERESTARTSYS; 19541da177e4SLinus Torvalds if (edge_serial->num_ports_open == 0) { 19552742fd88SAlan Cox /* we are the first port to open, post the interrupt urb */ 19561da177e4SLinus Torvalds urb = edge_serial->serial->port[0]->interrupt_in_urb; 19571da177e4SLinus Torvalds if (!urb) { 19582742fd88SAlan Cox dev_err(&port->dev, 19592742fd88SAlan Cox "%s - no interrupt urb present, exiting\n", 19602742fd88SAlan Cox __func__); 19611da177e4SLinus Torvalds status = -EINVAL; 1962241ca64fSMatthias Kaehlcke goto release_es_lock; 19631da177e4SLinus Torvalds } 19641da177e4SLinus Torvalds urb->complete = edge_interrupt_callback; 19651da177e4SLinus Torvalds urb->context = edge_serial; 19661da177e4SLinus Torvalds urb->dev = dev; 19671da177e4SLinus Torvalds status = usb_submit_urb(urb, GFP_KERNEL); 19681da177e4SLinus Torvalds if (status) { 19692742fd88SAlan Cox dev_err(&port->dev, 19702742fd88SAlan Cox "%s - usb_submit_urb failed with value %d\n", 19712742fd88SAlan Cox __func__, status); 1972241ca64fSMatthias Kaehlcke goto release_es_lock; 19731da177e4SLinus Torvalds } 19741da177e4SLinus Torvalds } 19751da177e4SLinus Torvalds 19761da177e4SLinus Torvalds /* 19771da177e4SLinus Torvalds * reset the data toggle on the bulk endpoints to work around bug in 19781da177e4SLinus Torvalds * host controllers where things get out of sync some times 19791da177e4SLinus Torvalds */ 19801da177e4SLinus Torvalds usb_clear_halt(dev, port->write_urb->pipe); 19811da177e4SLinus Torvalds usb_clear_halt(dev, port->read_urb->pipe); 19821da177e4SLinus Torvalds 19831da177e4SLinus Torvalds /* start up our bulk read urb */ 19841da177e4SLinus Torvalds urb = port->read_urb; 19851da177e4SLinus Torvalds if (!urb) { 19862742fd88SAlan Cox dev_err(&port->dev, "%s - no read urb present, exiting\n", 19872742fd88SAlan Cox __func__); 19881da177e4SLinus Torvalds status = -EINVAL; 19891da177e4SLinus Torvalds goto unlink_int_urb; 19901da177e4SLinus Torvalds } 19911da177e4SLinus Torvalds edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING; 19921da177e4SLinus Torvalds urb->complete = edge_bulk_in_callback; 19931da177e4SLinus Torvalds urb->context = edge_port; 19941da177e4SLinus Torvalds urb->dev = dev; 19951da177e4SLinus Torvalds status = usb_submit_urb(urb, GFP_KERNEL); 19961da177e4SLinus Torvalds if (status) { 19972742fd88SAlan Cox dev_err(&port->dev, 19982742fd88SAlan Cox "%s - read bulk usb_submit_urb failed with value %d\n", 19992742fd88SAlan Cox __func__, status); 20001da177e4SLinus Torvalds goto unlink_int_urb; 20011da177e4SLinus Torvalds } 20021da177e4SLinus Torvalds 20031da177e4SLinus Torvalds ++edge_serial->num_ports_open; 20041da177e4SLinus Torvalds 2005441b62c1SHarvey Harrison dbg("%s - exited", __func__); 20061da177e4SLinus Torvalds 2007241ca64fSMatthias Kaehlcke goto release_es_lock; 20081da177e4SLinus Torvalds 20091da177e4SLinus Torvalds unlink_int_urb: 20101da177e4SLinus Torvalds if (edge_port->edge_serial->num_ports_open == 0) 20111da177e4SLinus Torvalds usb_kill_urb(port->serial->port[0]->interrupt_in_urb); 2012241ca64fSMatthias Kaehlcke release_es_lock: 2013241ca64fSMatthias Kaehlcke mutex_unlock(&edge_serial->es_lock); 20141da177e4SLinus Torvalds return status; 20151da177e4SLinus Torvalds } 20161da177e4SLinus Torvalds 2017335f8514SAlan Cox static void edge_close(struct usb_serial_port *port) 20181da177e4SLinus Torvalds { 20191da177e4SLinus Torvalds struct edgeport_serial *edge_serial; 20201da177e4SLinus Torvalds struct edgeport_port *edge_port; 20211da177e4SLinus Torvalds int port_number; 20221da177e4SLinus Torvalds int status; 20231da177e4SLinus Torvalds 2024441b62c1SHarvey Harrison dbg("%s - port %d", __func__, port->number); 20251da177e4SLinus Torvalds 20261da177e4SLinus Torvalds edge_serial = usb_get_serial_data(port->serial); 20271da177e4SLinus Torvalds edge_port = usb_get_serial_port_data(port); 20282742fd88SAlan Cox if (edge_serial == NULL || edge_port == NULL) 20291da177e4SLinus Torvalds return; 20301da177e4SLinus Torvalds 20311da177e4SLinus Torvalds /* The bulkreadcompletion routine will check 20321da177e4SLinus Torvalds * this flag and dump add read data */ 20331da177e4SLinus Torvalds edge_port->close_pending = 1; 20341da177e4SLinus Torvalds 20351da177e4SLinus Torvalds /* chase the port close and flush */ 20362742fd88SAlan Cox chase_port(edge_port, (HZ * closing_wait) / 100, 1); 20371da177e4SLinus Torvalds 20381da177e4SLinus Torvalds usb_kill_urb(port->read_urb); 20391da177e4SLinus Torvalds usb_kill_urb(port->write_urb); 20401da177e4SLinus Torvalds edge_port->ep_write_urb_in_use = 0; 20411da177e4SLinus Torvalds 20421da177e4SLinus Torvalds /* assuming we can still talk to the device, 20431da177e4SLinus Torvalds * send a close port command to it */ 2044441b62c1SHarvey Harrison dbg("%s - send umpc_close_port", __func__); 20451da177e4SLinus Torvalds port_number = port->number - port->serial->minor; 20462742fd88SAlan Cox status = send_cmd(port->serial->dev, 20471da177e4SLinus Torvalds UMPC_CLOSE_PORT, 20481da177e4SLinus Torvalds (__u8)(UMPM_UART1_PORT + port_number), 20491da177e4SLinus Torvalds 0, 20501da177e4SLinus Torvalds NULL, 20511da177e4SLinus Torvalds 0); 2052241ca64fSMatthias Kaehlcke mutex_lock(&edge_serial->es_lock); 20531da177e4SLinus Torvalds --edge_port->edge_serial->num_ports_open; 20541da177e4SLinus Torvalds if (edge_port->edge_serial->num_ports_open <= 0) { 20551da177e4SLinus Torvalds /* last port is now closed, let's shut down our interrupt urb */ 20561da177e4SLinus Torvalds usb_kill_urb(port->serial->port[0]->interrupt_in_urb); 20571da177e4SLinus Torvalds edge_port->edge_serial->num_ports_open = 0; 20581da177e4SLinus Torvalds } 2059241ca64fSMatthias Kaehlcke mutex_unlock(&edge_serial->es_lock); 20601da177e4SLinus Torvalds edge_port->close_pending = 0; 20611da177e4SLinus Torvalds 2062441b62c1SHarvey Harrison dbg("%s - exited", __func__); 20631da177e4SLinus Torvalds } 20641da177e4SLinus Torvalds 206595da310eSAlan Cox static int edge_write(struct tty_struct *tty, struct usb_serial_port *port, 206695da310eSAlan Cox const unsigned char *data, int count) 20671da177e4SLinus Torvalds { 20681da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 20691da177e4SLinus Torvalds 2070441b62c1SHarvey Harrison dbg("%s - port %d", __func__, port->number); 20711da177e4SLinus Torvalds 20721da177e4SLinus Torvalds if (count == 0) { 2073441b62c1SHarvey Harrison dbg("%s - write request of 0 bytes", __func__); 20741da177e4SLinus Torvalds return 0; 20751da177e4SLinus Torvalds } 20761da177e4SLinus Torvalds 20771da177e4SLinus Torvalds if (edge_port == NULL) 20781da177e4SLinus Torvalds return -ENODEV; 20791da177e4SLinus Torvalds if (edge_port->close_pending == 1) 20801da177e4SLinus Torvalds return -ENODEV; 20811da177e4SLinus Torvalds 2082d733cec1SJohan Hovold count = kfifo_in_locked(&edge_port->write_fifo, data, count, 2083d733cec1SJohan Hovold &edge_port->ep_lock); 208495da310eSAlan Cox edge_send(tty); 20851da177e4SLinus Torvalds 20861da177e4SLinus Torvalds return count; 20871da177e4SLinus Torvalds } 20881da177e4SLinus Torvalds 208995da310eSAlan Cox static void edge_send(struct tty_struct *tty) 20901da177e4SLinus Torvalds { 209195da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 20921da177e4SLinus Torvalds int count, result; 20931da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 20941da177e4SLinus Torvalds unsigned long flags; 20951da177e4SLinus Torvalds 20961da177e4SLinus Torvalds 2097441b62c1SHarvey Harrison dbg("%s - port %d", __func__, port->number); 20981da177e4SLinus Torvalds 20991da177e4SLinus Torvalds spin_lock_irqsave(&edge_port->ep_lock, flags); 21001da177e4SLinus Torvalds 21011da177e4SLinus Torvalds if (edge_port->ep_write_urb_in_use) { 21021da177e4SLinus Torvalds spin_unlock_irqrestore(&edge_port->ep_lock, flags); 21031da177e4SLinus Torvalds return; 21041da177e4SLinus Torvalds } 21051da177e4SLinus Torvalds 2106d733cec1SJohan Hovold count = kfifo_out(&edge_port->write_fifo, 21071da177e4SLinus Torvalds port->write_urb->transfer_buffer, 21081da177e4SLinus Torvalds port->bulk_out_size); 21091da177e4SLinus Torvalds 21101da177e4SLinus Torvalds if (count == 0) { 21111da177e4SLinus Torvalds spin_unlock_irqrestore(&edge_port->ep_lock, flags); 21121da177e4SLinus Torvalds return; 21131da177e4SLinus Torvalds } 21141da177e4SLinus Torvalds 21151da177e4SLinus Torvalds edge_port->ep_write_urb_in_use = 1; 21161da177e4SLinus Torvalds 21171da177e4SLinus Torvalds spin_unlock_irqrestore(&edge_port->ep_lock, flags); 21181da177e4SLinus Torvalds 21192742fd88SAlan Cox usb_serial_debug_data(debug, &port->dev, __func__, count, 21202742fd88SAlan Cox port->write_urb->transfer_buffer); 21211da177e4SLinus Torvalds 21221da177e4SLinus Torvalds /* set up our urb */ 21231da177e4SLinus Torvalds usb_fill_bulk_urb(port->write_urb, port->serial->dev, 21241da177e4SLinus Torvalds usb_sndbulkpipe(port->serial->dev, 21251da177e4SLinus Torvalds port->bulk_out_endpointAddress), 21261da177e4SLinus Torvalds port->write_urb->transfer_buffer, count, 21271da177e4SLinus Torvalds edge_bulk_out_callback, 21281da177e4SLinus Torvalds port); 21291da177e4SLinus Torvalds 21301da177e4SLinus Torvalds /* send the data out the bulk port */ 21311da177e4SLinus Torvalds result = usb_submit_urb(port->write_urb, GFP_ATOMIC); 21321da177e4SLinus Torvalds if (result) { 21332742fd88SAlan Cox dev_err(&port->dev, 21342742fd88SAlan Cox "%s - failed submitting write urb, error %d\n", 21352742fd88SAlan Cox __func__, result); 21361da177e4SLinus Torvalds edge_port->ep_write_urb_in_use = 0; 21372742fd88SAlan Cox /* TODO: reschedule edge_send */ 21382742fd88SAlan Cox } else 21391da177e4SLinus Torvalds edge_port->icount.tx += count; 21401da177e4SLinus Torvalds 21411da177e4SLinus Torvalds /* wakeup any process waiting for writes to complete */ 21421da177e4SLinus Torvalds /* there is now more room in the buffer for new writes */ 21432742fd88SAlan Cox if (tty) 21441da177e4SLinus Torvalds tty_wakeup(tty); 21451da177e4SLinus Torvalds } 21461da177e4SLinus Torvalds 214795da310eSAlan Cox static int edge_write_room(struct tty_struct *tty) 21481da177e4SLinus Torvalds { 214995da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 21501da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 21511da177e4SLinus Torvalds int room = 0; 21521da177e4SLinus Torvalds unsigned long flags; 21531da177e4SLinus Torvalds 2154441b62c1SHarvey Harrison dbg("%s - port %d", __func__, port->number); 21551da177e4SLinus Torvalds 21561da177e4SLinus Torvalds if (edge_port == NULL) 21572742fd88SAlan Cox return 0; 21581da177e4SLinus Torvalds if (edge_port->close_pending == 1) 21592742fd88SAlan Cox return 0; 21601da177e4SLinus Torvalds 21611da177e4SLinus Torvalds spin_lock_irqsave(&edge_port->ep_lock, flags); 2162d733cec1SJohan Hovold room = kfifo_avail(&edge_port->write_fifo); 21631da177e4SLinus Torvalds spin_unlock_irqrestore(&edge_port->ep_lock, flags); 21641da177e4SLinus Torvalds 2165441b62c1SHarvey Harrison dbg("%s - returns %d", __func__, room); 21661da177e4SLinus Torvalds return room; 21671da177e4SLinus Torvalds } 21681da177e4SLinus Torvalds 216995da310eSAlan Cox static int edge_chars_in_buffer(struct tty_struct *tty) 21701da177e4SLinus Torvalds { 217195da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 21721da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 21731da177e4SLinus Torvalds int chars = 0; 21741da177e4SLinus Torvalds unsigned long flags; 21751da177e4SLinus Torvalds 2176441b62c1SHarvey Harrison dbg("%s - port %d", __func__, port->number); 21771da177e4SLinus Torvalds 21781da177e4SLinus Torvalds if (edge_port == NULL) 21792742fd88SAlan Cox return 0; 21801da177e4SLinus Torvalds if (edge_port->close_pending == 1) 21812742fd88SAlan Cox return 0; 21821da177e4SLinus Torvalds 21831da177e4SLinus Torvalds spin_lock_irqsave(&edge_port->ep_lock, flags); 2184d733cec1SJohan Hovold chars = kfifo_len(&edge_port->write_fifo); 21851da177e4SLinus Torvalds spin_unlock_irqrestore(&edge_port->ep_lock, flags); 21861da177e4SLinus Torvalds 2187441b62c1SHarvey Harrison dbg("%s - returns %d", __func__, chars); 21881da177e4SLinus Torvalds return chars; 21891da177e4SLinus Torvalds } 21901da177e4SLinus Torvalds 219195da310eSAlan Cox static void edge_throttle(struct tty_struct *tty) 21921da177e4SLinus Torvalds { 219395da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 21941da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 21951da177e4SLinus Torvalds int status; 21961da177e4SLinus Torvalds 2197441b62c1SHarvey Harrison dbg("%s - port %d", __func__, port->number); 21981da177e4SLinus Torvalds 21991da177e4SLinus Torvalds if (edge_port == NULL) 22001da177e4SLinus Torvalds return; 22011da177e4SLinus Torvalds 22021da177e4SLinus Torvalds /* if we are implementing XON/XOFF, send the stop character */ 22031da177e4SLinus Torvalds if (I_IXOFF(tty)) { 22041da177e4SLinus Torvalds unsigned char stop_char = STOP_CHAR(tty); 220595da310eSAlan Cox status = edge_write(tty, port, &stop_char, 1); 220695da310eSAlan Cox if (status <= 0) { 220795da310eSAlan Cox dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status); 220895da310eSAlan Cox } 22091da177e4SLinus Torvalds } 22101da177e4SLinus Torvalds 22111da177e4SLinus Torvalds /* if we are implementing RTS/CTS, stop reads */ 22121da177e4SLinus Torvalds /* and the Edgeport will clear the RTS line */ 22131da177e4SLinus Torvalds if (C_CRTSCTS(tty)) 22141da177e4SLinus Torvalds stop_read(edge_port); 22151da177e4SLinus Torvalds 22161da177e4SLinus Torvalds } 22171da177e4SLinus Torvalds 221895da310eSAlan Cox static void edge_unthrottle(struct tty_struct *tty) 22191da177e4SLinus Torvalds { 222095da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 22211da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 22221da177e4SLinus Torvalds int status; 22231da177e4SLinus Torvalds 2224441b62c1SHarvey Harrison dbg("%s - port %d", __func__, port->number); 22251da177e4SLinus Torvalds 22261da177e4SLinus Torvalds if (edge_port == NULL) 22271da177e4SLinus Torvalds return; 22281da177e4SLinus Torvalds 22291da177e4SLinus Torvalds /* if we are implementing XON/XOFF, send the start character */ 22301da177e4SLinus Torvalds if (I_IXOFF(tty)) { 22311da177e4SLinus Torvalds unsigned char start_char = START_CHAR(tty); 223295da310eSAlan Cox status = edge_write(tty, port, &start_char, 1); 223395da310eSAlan Cox if (status <= 0) { 223495da310eSAlan Cox dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status); 223595da310eSAlan Cox } 22361da177e4SLinus Torvalds } 22371da177e4SLinus Torvalds /* if we are implementing RTS/CTS, restart reads */ 22381da177e4SLinus Torvalds /* are the Edgeport will assert the RTS line */ 22391da177e4SLinus Torvalds if (C_CRTSCTS(tty)) { 22401da177e4SLinus Torvalds status = restart_read(edge_port); 22411da177e4SLinus Torvalds if (status) 22422742fd88SAlan Cox dev_err(&port->dev, 22432742fd88SAlan Cox "%s - read bulk usb_submit_urb failed: %d\n", 22442742fd88SAlan Cox __func__, status); 22451da177e4SLinus Torvalds } 22461da177e4SLinus Torvalds 22471da177e4SLinus Torvalds } 22481da177e4SLinus Torvalds 22491da177e4SLinus Torvalds static void stop_read(struct edgeport_port *edge_port) 22501da177e4SLinus Torvalds { 22511da177e4SLinus Torvalds unsigned long flags; 22521da177e4SLinus Torvalds 22531da177e4SLinus Torvalds spin_lock_irqsave(&edge_port->ep_lock, flags); 22541da177e4SLinus Torvalds 22551da177e4SLinus Torvalds if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING) 22561da177e4SLinus Torvalds edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING; 22571da177e4SLinus Torvalds edge_port->shadow_mcr &= ~MCR_RTS; 22581da177e4SLinus Torvalds 22591da177e4SLinus Torvalds spin_unlock_irqrestore(&edge_port->ep_lock, flags); 22601da177e4SLinus Torvalds } 22611da177e4SLinus Torvalds 22621da177e4SLinus Torvalds static int restart_read(struct edgeport_port *edge_port) 22631da177e4SLinus Torvalds { 22641da177e4SLinus Torvalds struct urb *urb; 22651da177e4SLinus Torvalds int status = 0; 22661da177e4SLinus Torvalds unsigned long flags; 22671da177e4SLinus Torvalds 22681da177e4SLinus Torvalds spin_lock_irqsave(&edge_port->ep_lock, flags); 22691da177e4SLinus Torvalds 22701da177e4SLinus Torvalds if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) { 22711da177e4SLinus Torvalds urb = edge_port->port->read_urb; 22721da177e4SLinus Torvalds urb->complete = edge_bulk_in_callback; 22731da177e4SLinus Torvalds urb->context = edge_port; 22741da177e4SLinus Torvalds urb->dev = edge_port->port->serial->dev; 2275efdff608SOliver Neukum status = usb_submit_urb(urb, GFP_ATOMIC); 22761da177e4SLinus Torvalds } 22771da177e4SLinus Torvalds edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING; 22781da177e4SLinus Torvalds edge_port->shadow_mcr |= MCR_RTS; 22791da177e4SLinus Torvalds 22801da177e4SLinus Torvalds spin_unlock_irqrestore(&edge_port->ep_lock, flags); 22811da177e4SLinus Torvalds 22821da177e4SLinus Torvalds return status; 22831da177e4SLinus Torvalds } 22841da177e4SLinus Torvalds 228595da310eSAlan Cox static void change_port_settings(struct tty_struct *tty, 228695da310eSAlan Cox struct edgeport_port *edge_port, struct ktermios *old_termios) 22871da177e4SLinus Torvalds { 22881da177e4SLinus Torvalds struct ump_uart_config *config; 22891da177e4SLinus Torvalds int baud; 22901da177e4SLinus Torvalds unsigned cflag; 22911da177e4SLinus Torvalds int status; 22922742fd88SAlan Cox int port_number = edge_port->port->number - 22932742fd88SAlan Cox edge_port->port->serial->minor; 22941da177e4SLinus Torvalds 2295441b62c1SHarvey Harrison dbg("%s - port %d", __func__, edge_port->port->number); 22961da177e4SLinus Torvalds 22971da177e4SLinus Torvalds config = kmalloc (sizeof (*config), GFP_KERNEL); 22981da177e4SLinus Torvalds if (!config) { 22992742fd88SAlan Cox *tty->termios = *old_termios; 23002742fd88SAlan Cox dev_err(&edge_port->port->dev, "%s - out of memory\n", 23012742fd88SAlan Cox __func__); 23021da177e4SLinus Torvalds return; 23031da177e4SLinus Torvalds } 23041da177e4SLinus Torvalds 23051da177e4SLinus Torvalds cflag = tty->termios->c_cflag; 23061da177e4SLinus Torvalds 23071da177e4SLinus Torvalds config->wFlags = 0; 23081da177e4SLinus Torvalds 23091da177e4SLinus Torvalds /* These flags must be set */ 23101da177e4SLinus Torvalds config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT; 23111da177e4SLinus Torvalds config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR; 23121da177e4SLinus Torvalds config->bUartMode = (__u8)(edge_port->bUartMode); 23131da177e4SLinus Torvalds 23141da177e4SLinus Torvalds switch (cflag & CSIZE) { 23151da177e4SLinus Torvalds case CS5: 23161da177e4SLinus Torvalds config->bDataBits = UMP_UART_CHAR5BITS; 2317441b62c1SHarvey Harrison dbg("%s - data bits = 5", __func__); 23181da177e4SLinus Torvalds break; 23191da177e4SLinus Torvalds case CS6: 23201da177e4SLinus Torvalds config->bDataBits = UMP_UART_CHAR6BITS; 2321441b62c1SHarvey Harrison dbg("%s - data bits = 6", __func__); 23221da177e4SLinus Torvalds break; 23231da177e4SLinus Torvalds case CS7: 23241da177e4SLinus Torvalds config->bDataBits = UMP_UART_CHAR7BITS; 2325441b62c1SHarvey Harrison dbg("%s - data bits = 7", __func__); 23261da177e4SLinus Torvalds break; 23271da177e4SLinus Torvalds default: 23281da177e4SLinus Torvalds case CS8: 23291da177e4SLinus Torvalds config->bDataBits = UMP_UART_CHAR8BITS; 2330441b62c1SHarvey Harrison dbg("%s - data bits = 8", __func__); 23311da177e4SLinus Torvalds break; 23321da177e4SLinus Torvalds } 23331da177e4SLinus Torvalds 23341da177e4SLinus Torvalds if (cflag & PARENB) { 23351da177e4SLinus Torvalds if (cflag & PARODD) { 23361da177e4SLinus Torvalds config->wFlags |= UMP_MASK_UART_FLAGS_PARITY; 23371da177e4SLinus Torvalds config->bParity = UMP_UART_ODDPARITY; 2338441b62c1SHarvey Harrison dbg("%s - parity = odd", __func__); 23391da177e4SLinus Torvalds } else { 23401da177e4SLinus Torvalds config->wFlags |= UMP_MASK_UART_FLAGS_PARITY; 23411da177e4SLinus Torvalds config->bParity = UMP_UART_EVENPARITY; 2342441b62c1SHarvey Harrison dbg("%s - parity = even", __func__); 23431da177e4SLinus Torvalds } 23441da177e4SLinus Torvalds } else { 23451da177e4SLinus Torvalds config->bParity = UMP_UART_NOPARITY; 2346441b62c1SHarvey Harrison dbg("%s - parity = none", __func__); 23471da177e4SLinus Torvalds } 23481da177e4SLinus Torvalds 23491da177e4SLinus Torvalds if (cflag & CSTOPB) { 23501da177e4SLinus Torvalds config->bStopBits = UMP_UART_STOPBIT2; 2351441b62c1SHarvey Harrison dbg("%s - stop bits = 2", __func__); 23521da177e4SLinus Torvalds } else { 23531da177e4SLinus Torvalds config->bStopBits = UMP_UART_STOPBIT1; 2354441b62c1SHarvey Harrison dbg("%s - stop bits = 1", __func__); 23551da177e4SLinus Torvalds } 23561da177e4SLinus Torvalds 23571da177e4SLinus Torvalds /* figure out the flow control settings */ 23581da177e4SLinus Torvalds if (cflag & CRTSCTS) { 23591da177e4SLinus Torvalds config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW; 23601da177e4SLinus Torvalds config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW; 2361441b62c1SHarvey Harrison dbg("%s - RTS/CTS is enabled", __func__); 23621da177e4SLinus Torvalds } else { 2363441b62c1SHarvey Harrison dbg("%s - RTS/CTS is disabled", __func__); 23641da177e4SLinus Torvalds tty->hw_stopped = 0; 23651da177e4SLinus Torvalds restart_read(edge_port); 23661da177e4SLinus Torvalds } 23671da177e4SLinus Torvalds 23682742fd88SAlan Cox /* if we are implementing XON/XOFF, set the start and stop 23692742fd88SAlan Cox character in the device */ 23701da177e4SLinus Torvalds config->cXon = START_CHAR(tty); 23711da177e4SLinus Torvalds config->cXoff = STOP_CHAR(tty); 23721da177e4SLinus Torvalds 23731da177e4SLinus Torvalds /* if we are implementing INBOUND XON/XOFF */ 23741da177e4SLinus Torvalds if (I_IXOFF(tty)) { 23751da177e4SLinus Torvalds config->wFlags |= UMP_MASK_UART_FLAGS_IN_X; 23761da177e4SLinus Torvalds dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x", 2377441b62c1SHarvey Harrison __func__, config->cXon, config->cXoff); 23782742fd88SAlan Cox } else 2379441b62c1SHarvey Harrison dbg("%s - INBOUND XON/XOFF is disabled", __func__); 23801da177e4SLinus Torvalds 23811da177e4SLinus Torvalds /* if we are implementing OUTBOUND XON/XOFF */ 23821da177e4SLinus Torvalds if (I_IXON(tty)) { 23831da177e4SLinus Torvalds config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X; 23841da177e4SLinus Torvalds dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x", 2385441b62c1SHarvey Harrison __func__, config->cXon, config->cXoff); 23862742fd88SAlan Cox } else 2387441b62c1SHarvey Harrison dbg("%s - OUTBOUND XON/XOFF is disabled", __func__); 23881da177e4SLinus Torvalds 2389d5f5bcd4SAlan Cox tty->termios->c_cflag &= ~CMSPAR; 2390d5f5bcd4SAlan Cox 23911da177e4SLinus Torvalds /* Round the baud rate */ 23921da177e4SLinus Torvalds baud = tty_get_baud_rate(tty); 23931da177e4SLinus Torvalds if (!baud) { 23941da177e4SLinus Torvalds /* pick a default, any default... */ 23951da177e4SLinus Torvalds baud = 9600; 2396d5f5bcd4SAlan Cox } else 2397d5f5bcd4SAlan Cox tty_encode_baud_rate(tty, baud, baud); 2398d5f5bcd4SAlan Cox 23991da177e4SLinus Torvalds edge_port->baud_rate = baud; 24001da177e4SLinus Torvalds config->wBaudRate = (__u16)((461550L + baud/2) / baud); 24011da177e4SLinus Torvalds 2402d5f5bcd4SAlan Cox /* FIXME: Recompute actual baud from divisor here */ 2403d5f5bcd4SAlan Cox 24042742fd88SAlan Cox dbg("%s - baud rate = %d, wBaudRate = %d", __func__, baud, 24052742fd88SAlan Cox config->wBaudRate); 24061da177e4SLinus Torvalds 24071da177e4SLinus Torvalds dbg("wBaudRate: %d", (int)(461550L / config->wBaudRate)); 24081da177e4SLinus Torvalds dbg("wFlags: 0x%x", config->wFlags); 24091da177e4SLinus Torvalds dbg("bDataBits: %d", config->bDataBits); 24101da177e4SLinus Torvalds dbg("bParity: %d", config->bParity); 24111da177e4SLinus Torvalds dbg("bStopBits: %d", config->bStopBits); 24121da177e4SLinus Torvalds dbg("cXon: %d", config->cXon); 24131da177e4SLinus Torvalds dbg("cXoff: %d", config->cXoff); 24141da177e4SLinus Torvalds dbg("bUartMode: %d", config->bUartMode); 24151da177e4SLinus Torvalds 24161da177e4SLinus Torvalds /* move the word values into big endian mode */ 24171da177e4SLinus Torvalds cpu_to_be16s(&config->wFlags); 24181da177e4SLinus Torvalds cpu_to_be16s(&config->wBaudRate); 24191da177e4SLinus Torvalds 24202742fd88SAlan Cox status = send_cmd(edge_port->port->serial->dev, UMPC_SET_CONFIG, 24211da177e4SLinus Torvalds (__u8)(UMPM_UART1_PORT + port_number), 24222742fd88SAlan Cox 0, (__u8 *)config, sizeof(*config)); 24232742fd88SAlan Cox if (status) 24241da177e4SLinus Torvalds dbg("%s - error %d when trying to write config to device", 2425441b62c1SHarvey Harrison __func__, status); 24261da177e4SLinus Torvalds kfree(config); 24271da177e4SLinus Torvalds return; 24281da177e4SLinus Torvalds } 24291da177e4SLinus Torvalds 243095da310eSAlan Cox static void edge_set_termios(struct tty_struct *tty, 243195da310eSAlan Cox struct usb_serial_port *port, struct ktermios *old_termios) 24321da177e4SLinus Torvalds { 24331da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 243495da310eSAlan Cox unsigned int cflag; 243595da310eSAlan Cox 243695da310eSAlan Cox cflag = tty->termios->c_cflag; 24371da177e4SLinus Torvalds 2438441b62c1SHarvey Harrison dbg("%s - clfag %08x iflag %08x", __func__, 24391da177e4SLinus Torvalds tty->termios->c_cflag, tty->termios->c_iflag); 2440441b62c1SHarvey Harrison dbg("%s - old clfag %08x old iflag %08x", __func__, 24411da177e4SLinus Torvalds old_termios->c_cflag, old_termios->c_iflag); 2442441b62c1SHarvey Harrison dbg("%s - port %d", __func__, port->number); 24431da177e4SLinus Torvalds 24441da177e4SLinus Torvalds if (edge_port == NULL) 24451da177e4SLinus Torvalds return; 24461da177e4SLinus Torvalds /* change the port settings to the new ones specified */ 244795da310eSAlan Cox change_port_settings(tty, edge_port, old_termios); 244895da310eSAlan Cox return; 24491da177e4SLinus Torvalds } 24501da177e4SLinus Torvalds 245195da310eSAlan Cox static int edge_tiocmset(struct tty_struct *tty, struct file *file, 24522742fd88SAlan Cox unsigned int set, unsigned int clear) 24531da177e4SLinus Torvalds { 245495da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 24551da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 24561da177e4SLinus Torvalds unsigned int mcr; 24573d71fe0bSAlan Cox unsigned long flags; 24581da177e4SLinus Torvalds 2459441b62c1SHarvey Harrison dbg("%s - port %d", __func__, port->number); 24601da177e4SLinus Torvalds 24613d71fe0bSAlan Cox spin_lock_irqsave(&edge_port->ep_lock, flags); 24621da177e4SLinus Torvalds mcr = edge_port->shadow_mcr; 24631da177e4SLinus Torvalds if (set & TIOCM_RTS) 24641da177e4SLinus Torvalds mcr |= MCR_RTS; 24651da177e4SLinus Torvalds if (set & TIOCM_DTR) 24661da177e4SLinus Torvalds mcr |= MCR_DTR; 24671da177e4SLinus Torvalds if (set & TIOCM_LOOP) 24681da177e4SLinus Torvalds mcr |= MCR_LOOPBACK; 24691da177e4SLinus Torvalds 24701da177e4SLinus Torvalds if (clear & TIOCM_RTS) 24711da177e4SLinus Torvalds mcr &= ~MCR_RTS; 24721da177e4SLinus Torvalds if (clear & TIOCM_DTR) 24731da177e4SLinus Torvalds mcr &= ~MCR_DTR; 24741da177e4SLinus Torvalds if (clear & TIOCM_LOOP) 24751da177e4SLinus Torvalds mcr &= ~MCR_LOOPBACK; 24761da177e4SLinus Torvalds 24771da177e4SLinus Torvalds edge_port->shadow_mcr = mcr; 24783d71fe0bSAlan Cox spin_unlock_irqrestore(&edge_port->ep_lock, flags); 24791da177e4SLinus Torvalds 24802742fd88SAlan Cox restore_mcr(edge_port, mcr); 24811da177e4SLinus Torvalds return 0; 24821da177e4SLinus Torvalds } 24831da177e4SLinus Torvalds 248495da310eSAlan Cox static int edge_tiocmget(struct tty_struct *tty, struct file *file) 24851da177e4SLinus Torvalds { 248695da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 24871da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 24881da177e4SLinus Torvalds unsigned int result = 0; 24891da177e4SLinus Torvalds unsigned int msr; 24901da177e4SLinus Torvalds unsigned int mcr; 24913d71fe0bSAlan Cox unsigned long flags; 24921da177e4SLinus Torvalds 2493441b62c1SHarvey Harrison dbg("%s - port %d", __func__, port->number); 24941da177e4SLinus Torvalds 24953d71fe0bSAlan Cox spin_lock_irqsave(&edge_port->ep_lock, flags); 24963d71fe0bSAlan Cox 24971da177e4SLinus Torvalds msr = edge_port->shadow_msr; 24981da177e4SLinus Torvalds mcr = edge_port->shadow_mcr; 24991da177e4SLinus Torvalds result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */ 25001da177e4SLinus Torvalds | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */ 25011da177e4SLinus Torvalds | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */ 25021da177e4SLinus Torvalds | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */ 25031da177e4SLinus Torvalds | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */ 25041da177e4SLinus Torvalds | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */ 25051da177e4SLinus Torvalds 25061da177e4SLinus Torvalds 2507441b62c1SHarvey Harrison dbg("%s -- %x", __func__, result); 25083d71fe0bSAlan Cox spin_unlock_irqrestore(&edge_port->ep_lock, flags); 25091da177e4SLinus Torvalds 25101da177e4SLinus Torvalds return result; 25111da177e4SLinus Torvalds } 25121da177e4SLinus Torvalds 25132742fd88SAlan Cox static int get_serial_info(struct edgeport_port *edge_port, 25142742fd88SAlan Cox struct serial_struct __user *retinfo) 25151da177e4SLinus Torvalds { 25161da177e4SLinus Torvalds struct serial_struct tmp; 25171da177e4SLinus Torvalds 25181da177e4SLinus Torvalds if (!retinfo) 25191da177e4SLinus Torvalds return -EFAULT; 25201da177e4SLinus Torvalds 25211da177e4SLinus Torvalds memset(&tmp, 0, sizeof(tmp)); 25221da177e4SLinus Torvalds 25231da177e4SLinus Torvalds tmp.type = PORT_16550A; 25241da177e4SLinus Torvalds tmp.line = edge_port->port->serial->minor; 25251da177e4SLinus Torvalds tmp.port = edge_port->port->number; 25261da177e4SLinus Torvalds tmp.irq = 0; 25271da177e4SLinus Torvalds tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; 25281da177e4SLinus Torvalds tmp.xmit_fifo_size = edge_port->port->bulk_out_size; 25291da177e4SLinus Torvalds tmp.baud_base = 9600; 25301da177e4SLinus Torvalds tmp.close_delay = 5*HZ; 25311da177e4SLinus Torvalds tmp.closing_wait = closing_wait; 25321da177e4SLinus Torvalds 25331da177e4SLinus Torvalds if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) 25341da177e4SLinus Torvalds return -EFAULT; 25351da177e4SLinus Torvalds return 0; 25361da177e4SLinus Torvalds } 25371da177e4SLinus Torvalds 253895da310eSAlan Cox static int edge_ioctl(struct tty_struct *tty, struct file *file, 25392742fd88SAlan Cox unsigned int cmd, unsigned long arg) 25401da177e4SLinus Torvalds { 254195da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 25421da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 25431da177e4SLinus Torvalds struct async_icount cnow; 25441da177e4SLinus Torvalds struct async_icount cprev; 25451da177e4SLinus Torvalds 2546441b62c1SHarvey Harrison dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd); 25471da177e4SLinus Torvalds 25481da177e4SLinus Torvalds switch (cmd) { 25491da177e4SLinus Torvalds case TIOCGSERIAL: 2550441b62c1SHarvey Harrison dbg("%s - (%d) TIOCGSERIAL", __func__, port->number); 25512742fd88SAlan Cox return get_serial_info(edge_port, 25522742fd88SAlan Cox (struct serial_struct __user *) arg); 25531da177e4SLinus Torvalds case TIOCMIWAIT: 2554441b62c1SHarvey Harrison dbg("%s - (%d) TIOCMIWAIT", __func__, port->number); 25551da177e4SLinus Torvalds cprev = edge_port->icount; 25561da177e4SLinus Torvalds while (1) { 25571da177e4SLinus Torvalds interruptible_sleep_on(&edge_port->delta_msr_wait); 25581da177e4SLinus Torvalds /* see if a signal did it */ 25591da177e4SLinus Torvalds if (signal_pending(current)) 25601da177e4SLinus Torvalds return -ERESTARTSYS; 25611da177e4SLinus Torvalds cnow = edge_port->icount; 25621da177e4SLinus Torvalds if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr && 25631da177e4SLinus Torvalds cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) 25641da177e4SLinus Torvalds return -EIO; /* no change => error */ 25651da177e4SLinus Torvalds if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || 25661da177e4SLinus Torvalds ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || 25671da177e4SLinus Torvalds ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) || 25681da177e4SLinus Torvalds ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) { 25691da177e4SLinus Torvalds return 0; 25701da177e4SLinus Torvalds } 25711da177e4SLinus Torvalds cprev = cnow; 25721da177e4SLinus Torvalds } 25731da177e4SLinus Torvalds /* not reached */ 25741da177e4SLinus Torvalds break; 25751da177e4SLinus Torvalds case TIOCGICOUNT: 2576441b62c1SHarvey Harrison dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __func__, 25771da177e4SLinus Torvalds port->number, edge_port->icount.rx, edge_port->icount.tx); 25782742fd88SAlan Cox if (copy_to_user((void __user *)arg, &edge_port->icount, 25792742fd88SAlan Cox sizeof(edge_port->icount))) 25801da177e4SLinus Torvalds return -EFAULT; 25811da177e4SLinus Torvalds return 0; 25821da177e4SLinus Torvalds } 25831da177e4SLinus Torvalds return -ENOIOCTLCMD; 25841da177e4SLinus Torvalds } 25851da177e4SLinus Torvalds 258695da310eSAlan Cox static void edge_break(struct tty_struct *tty, int break_state) 25871da177e4SLinus Torvalds { 258895da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 25891da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 25901da177e4SLinus Torvalds int status; 25912742fd88SAlan Cox int bv = 0; /* Off */ 25921da177e4SLinus Torvalds 259395da310eSAlan Cox dbg("%s - state = %d", __func__, break_state); 25941da177e4SLinus Torvalds 25951da177e4SLinus Torvalds /* chase the port close */ 25962742fd88SAlan Cox chase_port(edge_port, 0, 0); 25971da177e4SLinus Torvalds 259895da310eSAlan Cox if (break_state == -1) 25992742fd88SAlan Cox bv = 1; /* On */ 26002742fd88SAlan Cox status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv); 26012742fd88SAlan Cox if (status) 26021da177e4SLinus Torvalds dbg("%s - error %d sending break set/clear command.", 2603441b62c1SHarvey Harrison __func__, status); 26041da177e4SLinus Torvalds } 26051da177e4SLinus Torvalds 26061da177e4SLinus Torvalds static int edge_startup(struct usb_serial *serial) 26071da177e4SLinus Torvalds { 26081da177e4SLinus Torvalds struct edgeport_serial *edge_serial; 26091da177e4SLinus Torvalds struct edgeport_port *edge_port; 26101da177e4SLinus Torvalds struct usb_device *dev; 26111da177e4SLinus Torvalds int status; 26121da177e4SLinus Torvalds int i; 26131da177e4SLinus Torvalds 26141da177e4SLinus Torvalds dev = serial->dev; 26151da177e4SLinus Torvalds 26161da177e4SLinus Torvalds /* create our private serial structure */ 261780b6ca48SEric Sesterhenn edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL); 26181da177e4SLinus Torvalds if (edge_serial == NULL) { 2619441b62c1SHarvey Harrison dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__); 26201da177e4SLinus Torvalds return -ENOMEM; 26211da177e4SLinus Torvalds } 2622241ca64fSMatthias Kaehlcke mutex_init(&edge_serial->es_lock); 26231da177e4SLinus Torvalds edge_serial->serial = serial; 26241da177e4SLinus Torvalds usb_set_serial_data(serial, edge_serial); 26251da177e4SLinus Torvalds 26262742fd88SAlan Cox status = download_fw(edge_serial); 26271da177e4SLinus Torvalds if (status) { 26281da177e4SLinus Torvalds kfree(edge_serial); 26291da177e4SLinus Torvalds return status; 26301da177e4SLinus Torvalds } 26311da177e4SLinus Torvalds 26321da177e4SLinus Torvalds /* set up our port private structures */ 26331da177e4SLinus Torvalds for (i = 0; i < serial->num_ports; ++i) { 263480b6ca48SEric Sesterhenn edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL); 26351da177e4SLinus Torvalds if (edge_port == NULL) { 26362742fd88SAlan Cox dev_err(&serial->dev->dev, "%s - Out of memory\n", 26372742fd88SAlan Cox __func__); 26381da177e4SLinus Torvalds goto cleanup; 26391da177e4SLinus Torvalds } 26401da177e4SLinus Torvalds spin_lock_init(&edge_port->ep_lock); 2641d733cec1SJohan Hovold if (kfifo_alloc(&edge_port->write_fifo, EDGE_OUT_BUF_SIZE, 2642d733cec1SJohan Hovold GFP_KERNEL)) { 26432742fd88SAlan Cox dev_err(&serial->dev->dev, "%s - Out of memory\n", 26442742fd88SAlan Cox __func__); 26451da177e4SLinus Torvalds kfree(edge_port); 26461da177e4SLinus Torvalds goto cleanup; 26471da177e4SLinus Torvalds } 26481da177e4SLinus Torvalds edge_port->port = serial->port[i]; 26491da177e4SLinus Torvalds edge_port->edge_serial = edge_serial; 26501da177e4SLinus Torvalds usb_set_serial_port_data(serial->port[i], edge_port); 2651fc4cbd75SMartin K. Petersen edge_port->bUartMode = default_uart_mode; 26521da177e4SLinus Torvalds } 26531da177e4SLinus Torvalds 26541da177e4SLinus Torvalds return 0; 26551da177e4SLinus Torvalds 26561da177e4SLinus Torvalds cleanup: 26571da177e4SLinus Torvalds for (--i; i >= 0; --i) { 26581da177e4SLinus Torvalds edge_port = usb_get_serial_port_data(serial->port[i]); 2659d733cec1SJohan Hovold kfifo_free(&edge_port->write_fifo); 26601da177e4SLinus Torvalds kfree(edge_port); 26611da177e4SLinus Torvalds usb_set_serial_port_data(serial->port[i], NULL); 26621da177e4SLinus Torvalds } 26631da177e4SLinus Torvalds kfree(edge_serial); 26641da177e4SLinus Torvalds usb_set_serial_data(serial, NULL); 26651da177e4SLinus Torvalds return -ENOMEM; 26661da177e4SLinus Torvalds } 26671da177e4SLinus Torvalds 2668f9c99bb8SAlan Stern static void edge_disconnect(struct usb_serial *serial) 26691da177e4SLinus Torvalds { 26701da177e4SLinus Torvalds int i; 26711da177e4SLinus Torvalds struct edgeport_port *edge_port; 26721da177e4SLinus Torvalds 2673441b62c1SHarvey Harrison dbg("%s", __func__); 26741da177e4SLinus Torvalds 26751da177e4SLinus Torvalds for (i = 0; i < serial->num_ports; ++i) { 26761da177e4SLinus Torvalds edge_port = usb_get_serial_port_data(serial->port[i]); 2677fc4cbd75SMartin K. Petersen edge_remove_sysfs_attrs(edge_port->port); 2678f9c99bb8SAlan Stern } 2679f9c99bb8SAlan Stern } 2680f9c99bb8SAlan Stern 2681f9c99bb8SAlan Stern static void edge_release(struct usb_serial *serial) 2682f9c99bb8SAlan Stern { 2683f9c99bb8SAlan Stern int i; 2684f9c99bb8SAlan Stern struct edgeport_port *edge_port; 2685f9c99bb8SAlan Stern 2686f9c99bb8SAlan Stern dbg("%s", __func__); 2687f9c99bb8SAlan Stern 2688f9c99bb8SAlan Stern for (i = 0; i < serial->num_ports; ++i) { 2689f9c99bb8SAlan Stern edge_port = usb_get_serial_port_data(serial->port[i]); 2690d733cec1SJohan Hovold kfifo_free(&edge_port->write_fifo); 26911da177e4SLinus Torvalds kfree(edge_port); 26921da177e4SLinus Torvalds } 26931da177e4SLinus Torvalds kfree(usb_get_serial_data(serial)); 26941da177e4SLinus Torvalds } 26951da177e4SLinus Torvalds 26961da177e4SLinus Torvalds 2697fc4cbd75SMartin K. Petersen /* Sysfs Attributes */ 2698fc4cbd75SMartin K. Petersen 2699fc4cbd75SMartin K. Petersen static ssize_t show_uart_mode(struct device *dev, 2700fc4cbd75SMartin K. Petersen struct device_attribute *attr, char *buf) 2701fc4cbd75SMartin K. Petersen { 2702fc4cbd75SMartin K. Petersen struct usb_serial_port *port = to_usb_serial_port(dev); 2703fc4cbd75SMartin K. Petersen struct edgeport_port *edge_port = usb_get_serial_port_data(port); 2704fc4cbd75SMartin K. Petersen 2705fc4cbd75SMartin K. Petersen return sprintf(buf, "%d\n", edge_port->bUartMode); 2706fc4cbd75SMartin K. Petersen } 2707fc4cbd75SMartin K. Petersen 2708fc4cbd75SMartin K. Petersen static ssize_t store_uart_mode(struct device *dev, 2709fc4cbd75SMartin K. Petersen struct device_attribute *attr, const char *valbuf, size_t count) 2710fc4cbd75SMartin K. Petersen { 2711fc4cbd75SMartin K. Petersen struct usb_serial_port *port = to_usb_serial_port(dev); 2712fc4cbd75SMartin K. Petersen struct edgeport_port *edge_port = usb_get_serial_port_data(port); 2713fc4cbd75SMartin K. Petersen unsigned int v = simple_strtoul(valbuf, NULL, 0); 2714fc4cbd75SMartin K. Petersen 2715441b62c1SHarvey Harrison dbg("%s: setting uart_mode = %d", __func__, v); 2716fc4cbd75SMartin K. Petersen 2717fc4cbd75SMartin K. Petersen if (v < 256) 2718fc4cbd75SMartin K. Petersen edge_port->bUartMode = v; 2719fc4cbd75SMartin K. Petersen else 2720441b62c1SHarvey Harrison dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v); 2721fc4cbd75SMartin K. Petersen 2722fc4cbd75SMartin K. Petersen return count; 2723fc4cbd75SMartin K. Petersen } 2724fc4cbd75SMartin K. Petersen 27252742fd88SAlan Cox static DEVICE_ATTR(uart_mode, S_IWUSR | S_IRUGO, show_uart_mode, 27262742fd88SAlan Cox store_uart_mode); 2727fc4cbd75SMartin K. Petersen 2728fc4cbd75SMartin K. Petersen static int edge_create_sysfs_attrs(struct usb_serial_port *port) 2729fc4cbd75SMartin K. Petersen { 2730fc4cbd75SMartin K. Petersen return device_create_file(&port->dev, &dev_attr_uart_mode); 2731fc4cbd75SMartin K. Petersen } 2732fc4cbd75SMartin K. Petersen 2733fc4cbd75SMartin K. Petersen static int edge_remove_sysfs_attrs(struct usb_serial_port *port) 2734fc4cbd75SMartin K. Petersen { 2735fc4cbd75SMartin K. Petersen device_remove_file(&port->dev, &dev_attr_uart_mode); 2736fc4cbd75SMartin K. Petersen return 0; 2737fc4cbd75SMartin K. Petersen } 2738fc4cbd75SMartin K. Petersen 2739fc4cbd75SMartin K. Petersen 2740ea65370dSGreg Kroah-Hartman static struct usb_serial_driver edgeport_1port_device = { 274118fcac35SGreg Kroah-Hartman .driver = { 27421da177e4SLinus Torvalds .owner = THIS_MODULE, 2743269bda1cSGreg Kroah-Hartman .name = "edgeport_ti_1", 274418fcac35SGreg Kroah-Hartman }, 2745269bda1cSGreg Kroah-Hartman .description = "Edgeport TI 1 port adapter", 2746d9b1b787SJohannes Hölzl .usb_driver = &io_driver, 27471da177e4SLinus Torvalds .id_table = edgeport_1port_id_table, 27481da177e4SLinus Torvalds .num_ports = 1, 27491da177e4SLinus Torvalds .open = edge_open, 27501da177e4SLinus Torvalds .close = edge_close, 27511da177e4SLinus Torvalds .throttle = edge_throttle, 27521da177e4SLinus Torvalds .unthrottle = edge_unthrottle, 27531da177e4SLinus Torvalds .attach = edge_startup, 2754f9c99bb8SAlan Stern .disconnect = edge_disconnect, 2755f9c99bb8SAlan Stern .release = edge_release, 2756fc4cbd75SMartin K. Petersen .port_probe = edge_create_sysfs_attrs, 27571da177e4SLinus Torvalds .ioctl = edge_ioctl, 27581da177e4SLinus Torvalds .set_termios = edge_set_termios, 27591da177e4SLinus Torvalds .tiocmget = edge_tiocmget, 27601da177e4SLinus Torvalds .tiocmset = edge_tiocmset, 27611da177e4SLinus Torvalds .write = edge_write, 27621da177e4SLinus Torvalds .write_room = edge_write_room, 27631da177e4SLinus Torvalds .chars_in_buffer = edge_chars_in_buffer, 27641da177e4SLinus Torvalds .break_ctl = edge_break, 27651da177e4SLinus Torvalds .read_int_callback = edge_interrupt_callback, 27661da177e4SLinus Torvalds .read_bulk_callback = edge_bulk_in_callback, 27671da177e4SLinus Torvalds .write_bulk_callback = edge_bulk_out_callback, 27681da177e4SLinus Torvalds }; 27691da177e4SLinus Torvalds 2770ea65370dSGreg Kroah-Hartman static struct usb_serial_driver edgeport_2port_device = { 277118fcac35SGreg Kroah-Hartman .driver = { 27721da177e4SLinus Torvalds .owner = THIS_MODULE, 2773269bda1cSGreg Kroah-Hartman .name = "edgeport_ti_2", 277418fcac35SGreg Kroah-Hartman }, 2775269bda1cSGreg Kroah-Hartman .description = "Edgeport TI 2 port adapter", 2776d9b1b787SJohannes Hölzl .usb_driver = &io_driver, 27771da177e4SLinus Torvalds .id_table = edgeport_2port_id_table, 27781da177e4SLinus Torvalds .num_ports = 2, 27791da177e4SLinus Torvalds .open = edge_open, 27801da177e4SLinus Torvalds .close = edge_close, 27811da177e4SLinus Torvalds .throttle = edge_throttle, 27821da177e4SLinus Torvalds .unthrottle = edge_unthrottle, 27831da177e4SLinus Torvalds .attach = edge_startup, 2784f9c99bb8SAlan Stern .disconnect = edge_disconnect, 2785f9c99bb8SAlan Stern .release = edge_release, 2786fc4cbd75SMartin K. Petersen .port_probe = edge_create_sysfs_attrs, 27871da177e4SLinus Torvalds .ioctl = edge_ioctl, 27881da177e4SLinus Torvalds .set_termios = edge_set_termios, 27891da177e4SLinus Torvalds .tiocmget = edge_tiocmget, 27901da177e4SLinus Torvalds .tiocmset = edge_tiocmset, 27911da177e4SLinus Torvalds .write = edge_write, 27921da177e4SLinus Torvalds .write_room = edge_write_room, 27931da177e4SLinus Torvalds .chars_in_buffer = edge_chars_in_buffer, 27941da177e4SLinus Torvalds .break_ctl = edge_break, 27951da177e4SLinus Torvalds .read_int_callback = edge_interrupt_callback, 27961da177e4SLinus Torvalds .read_bulk_callback = edge_bulk_in_callback, 27971da177e4SLinus Torvalds .write_bulk_callback = edge_bulk_out_callback, 27981da177e4SLinus Torvalds }; 27991da177e4SLinus Torvalds 28001da177e4SLinus Torvalds 28011da177e4SLinus Torvalds static int __init edgeport_init(void) 28021da177e4SLinus Torvalds { 28031da177e4SLinus Torvalds int retval; 28041da177e4SLinus Torvalds retval = usb_serial_register(&edgeport_1port_device); 28051da177e4SLinus Torvalds if (retval) 28061da177e4SLinus Torvalds goto failed_1port_device_register; 28071da177e4SLinus Torvalds retval = usb_serial_register(&edgeport_2port_device); 28081da177e4SLinus Torvalds if (retval) 28091da177e4SLinus Torvalds goto failed_2port_device_register; 28101da177e4SLinus Torvalds retval = usb_register(&io_driver); 28111da177e4SLinus Torvalds if (retval) 28121da177e4SLinus Torvalds goto failed_usb_register; 2813c197a8dbSGreg Kroah-Hartman printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":" 2814c197a8dbSGreg Kroah-Hartman DRIVER_DESC "\n"); 28151da177e4SLinus Torvalds return 0; 28161da177e4SLinus Torvalds failed_usb_register: 28171da177e4SLinus Torvalds usb_serial_deregister(&edgeport_2port_device); 28181da177e4SLinus Torvalds failed_2port_device_register: 28191da177e4SLinus Torvalds usb_serial_deregister(&edgeport_1port_device); 28201da177e4SLinus Torvalds failed_1port_device_register: 28211da177e4SLinus Torvalds return retval; 28221da177e4SLinus Torvalds } 28231da177e4SLinus Torvalds 28241da177e4SLinus Torvalds static void __exit edgeport_exit(void) 28251da177e4SLinus Torvalds { 28261da177e4SLinus Torvalds usb_deregister(&io_driver); 28271da177e4SLinus Torvalds usb_serial_deregister(&edgeport_1port_device); 28281da177e4SLinus Torvalds usb_serial_deregister(&edgeport_2port_device); 28291da177e4SLinus Torvalds } 28301da177e4SLinus Torvalds 28311da177e4SLinus Torvalds module_init(edgeport_init); 28321da177e4SLinus Torvalds module_exit(edgeport_exit); 28331da177e4SLinus Torvalds 28341da177e4SLinus Torvalds /* Module information */ 28351da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR); 28361da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC); 28371da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 2838d12b219aSJaswinder Singh MODULE_FIRMWARE("edgeport/down3.bin"); 28391da177e4SLinus Torvalds 28401da177e4SLinus Torvalds module_param(debug, bool, S_IRUGO | S_IWUSR); 28411da177e4SLinus Torvalds MODULE_PARM_DESC(debug, "Debug enabled or not"); 28421da177e4SLinus Torvalds 28431da177e4SLinus Torvalds module_param(closing_wait, int, S_IRUGO | S_IWUSR); 28441da177e4SLinus Torvalds MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs"); 28451da177e4SLinus Torvalds 28461da177e4SLinus Torvalds module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR); 28472742fd88SAlan Cox MODULE_PARM_DESC(ignore_cpu_rev, 28482742fd88SAlan Cox "Ignore the cpu revision when connecting to a device"); 28491da177e4SLinus Torvalds 2850fc4cbd75SMartin K. Petersen module_param(default_uart_mode, int, S_IRUGO | S_IWUSR); 2851fc4cbd75SMartin K. Petersen MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ..."); 2852