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 201da177e4SLinus Torvalds #include <linux/kernel.h> 211da177e4SLinus Torvalds #include <linux/jiffies.h> 221da177e4SLinus Torvalds #include <linux/errno.h> 231da177e4SLinus Torvalds #include <linux/init.h> 241da177e4SLinus Torvalds #include <linux/slab.h> 251da177e4SLinus Torvalds #include <linux/tty.h> 261da177e4SLinus Torvalds #include <linux/tty_driver.h> 271da177e4SLinus Torvalds #include <linux/tty_flip.h> 281da177e4SLinus Torvalds #include <linux/module.h> 291da177e4SLinus Torvalds #include <linux/spinlock.h> 30241ca64fSMatthias Kaehlcke #include <linux/mutex.h> 311da177e4SLinus Torvalds #include <linux/serial.h> 32d733cec1SJohan Hovold #include <linux/kfifo.h> 331da177e4SLinus Torvalds #include <linux/ioctl.h> 34d12b219aSJaswinder Singh #include <linux/firmware.h> 352742fd88SAlan Cox #include <linux/uaccess.h> 361da177e4SLinus Torvalds #include <linux/usb.h> 37a969888cSGreg Kroah-Hartman #include <linux/usb/serial.h> 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds #include "io_16654.h" 401da177e4SLinus Torvalds #include "io_usbvend.h" 411da177e4SLinus Torvalds #include "io_ti.h" 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds /* 441da177e4SLinus Torvalds * Version Information 451da177e4SLinus Torvalds */ 46fc4cbd75SMartin K. Petersen #define DRIVER_VERSION "v0.7mode043006" 471da177e4SLinus Torvalds #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli" 481da177e4SLinus Torvalds #define DRIVER_DESC "Edgeport USB Serial Driver" 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds #define EPROM_PAGE_SIZE 64 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds /* different hardware types */ 541da177e4SLinus Torvalds #define HARDWARE_TYPE_930 0 551da177e4SLinus Torvalds #define HARDWARE_TYPE_TIUMP 1 561da177e4SLinus Torvalds 572742fd88SAlan Cox /* IOCTL_PRIVATE_TI_GET_MODE Definitions */ 582742fd88SAlan Cox #define TI_MODE_CONFIGURING 0 /* Device has not entered start device */ 592742fd88SAlan Cox #define TI_MODE_BOOT 1 /* Staying in boot mode */ 602742fd88SAlan Cox #define TI_MODE_DOWNLOAD 2 /* Made it to download mode */ 612742fd88SAlan Cox #define TI_MODE_TRANSITIONING 3 /* Currently in boot mode but 622742fd88SAlan Cox transitioning to download mode */ 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds /* read urb state */ 651da177e4SLinus Torvalds #define EDGE_READ_URB_RUNNING 0 661da177e4SLinus Torvalds #define EDGE_READ_URB_STOPPING 1 671da177e4SLinus Torvalds #define EDGE_READ_URB_STOPPED 2 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds #define EDGE_CLOSING_WAIT 4000 /* in .01 sec */ 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds #define EDGE_OUT_BUF_SIZE 1024 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds /* Product information read from the Edgeport */ 752742fd88SAlan Cox struct product_info { 762742fd88SAlan Cox int TiMode; /* Current TI Mode */ 772742fd88SAlan Cox __u8 hardware_type; /* Type of hardware */ 781da177e4SLinus Torvalds } __attribute__((packed)); 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds struct edgeport_port { 811da177e4SLinus Torvalds __u16 uart_base; 821da177e4SLinus Torvalds __u16 dma_address; 831da177e4SLinus Torvalds __u8 shadow_msr; 841da177e4SLinus Torvalds __u8 shadow_mcr; 851da177e4SLinus Torvalds __u8 shadow_lsr; 861da177e4SLinus Torvalds __u8 lsr_mask; 8719af5cdbSMartin Olsson __u32 ump_read_timeout; /* Number of milliseconds the UMP will 881da177e4SLinus Torvalds wait without data before completing 891da177e4SLinus Torvalds a read short */ 901da177e4SLinus Torvalds int baud_rate; 911da177e4SLinus Torvalds int close_pending; 921da177e4SLinus Torvalds int lsr_event; 931da177e4SLinus Torvalds struct async_icount icount; 941da177e4SLinus Torvalds wait_queue_head_t delta_msr_wait; /* for handling sleeping while 951da177e4SLinus Torvalds waiting for msr change to 961da177e4SLinus Torvalds happen */ 971da177e4SLinus Torvalds struct edgeport_serial *edge_serial; 981da177e4SLinus Torvalds struct usb_serial_port *port; 991da177e4SLinus Torvalds __u8 bUartMode; /* Port type, 0: RS232, etc. */ 1001da177e4SLinus Torvalds spinlock_t ep_lock; 1011da177e4SLinus Torvalds int ep_read_urb_state; 1021da177e4SLinus Torvalds int ep_write_urb_in_use; 103d733cec1SJohan Hovold struct kfifo write_fifo; 1041da177e4SLinus Torvalds }; 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds struct edgeport_serial { 1071da177e4SLinus Torvalds struct product_info product_info; 1082742fd88SAlan Cox u8 TI_I2C_Type; /* Type of I2C in UMP */ 1092742fd88SAlan Cox u8 TiReadI2C; /* Set to TRUE if we have read the 1102742fd88SAlan Cox I2c in Boot Mode */ 111241ca64fSMatthias Kaehlcke struct mutex es_lock; 1121da177e4SLinus Torvalds int num_ports_open; 1131da177e4SLinus Torvalds struct usb_serial *serial; 1141da177e4SLinus Torvalds }; 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds /* Devices that this driver supports */ 1187d40d7e8SNémeth Márton static const struct usb_device_id edgeport_1port_id_table[] = { 1191da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) }, 1201da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) }, 1211da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) }, 1221da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) }, 1231da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) }, 1241da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) }, 1251da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) }, 1261da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) }, 1271da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) }, 1281da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) }, 1291da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) }, 1301da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) }, 1311da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) }, 1321da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) }, 1331da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) }, 1341da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) }, 1351da177e4SLinus Torvalds { } 1361da177e4SLinus Torvalds }; 1371da177e4SLinus Torvalds 1387d40d7e8SNémeth Márton static const struct usb_device_id edgeport_2port_id_table[] = { 1391da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) }, 1401da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) }, 1411da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) }, 1421da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) }, 1431da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) }, 1441da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) }, 1451da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) }, 1461da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) }, 1471da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) }, 1481da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) }, 1491da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) }, 1501da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) }, 151fc4cbd75SMartin K. Petersen /* The 4, 8 and 16 port devices show up as multiple 2 port devices */ 1521da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) }, 153fc4cbd75SMartin K. Petersen { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) }, 154fc4cbd75SMartin K. Petersen { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) }, 155fc4cbd75SMartin K. Petersen { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) }, 156fc4cbd75SMartin K. Petersen { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) }, 1571da177e4SLinus Torvalds { } 1581da177e4SLinus Torvalds }; 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds /* Devices that this driver supports */ 1617d40d7e8SNémeth Márton static const struct usb_device_id id_table_combined[] = { 1621da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) }, 1631da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) }, 1641da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) }, 1651da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) }, 1661da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) }, 1671da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) }, 1681da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) }, 1691da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) }, 1701da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) }, 1711da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) }, 1721da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) }, 1731da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) }, 1741da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) }, 1751da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) }, 1761da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) }, 1771da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) }, 1781da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) }, 1791da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) }, 1801da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) }, 1811da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) }, 1821da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) }, 1831da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) }, 1841da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) }, 1851da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) }, 1861da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) }, 1871da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) }, 1881da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) }, 1891da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) }, 1901da177e4SLinus Torvalds { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) }, 191fc4cbd75SMartin K. Petersen { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) }, 192fc4cbd75SMartin K. Petersen { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) }, 193fc4cbd75SMartin K. Petersen { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) }, 194fc4cbd75SMartin K. Petersen { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) }, 1951da177e4SLinus Torvalds { } 1961da177e4SLinus Torvalds }; 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds MODULE_DEVICE_TABLE(usb, id_table_combined); 1991da177e4SLinus Torvalds 200d12b219aSJaswinder Singh static unsigned char OperationalMajorVersion; 201d12b219aSJaswinder Singh static unsigned char OperationalMinorVersion; 202d12b219aSJaswinder Singh static unsigned short OperationalBuildNumber; 2031da177e4SLinus Torvalds 20490ab5ee9SRusty Russell static bool debug; 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds static int closing_wait = EDGE_CLOSING_WAIT; 20790ab5ee9SRusty Russell static bool ignore_cpu_rev; 2082742fd88SAlan Cox static int default_uart_mode; /* RS232 */ 2091da177e4SLinus Torvalds 2102742fd88SAlan Cox static void edge_tty_recv(struct device *dev, struct tty_struct *tty, 2112742fd88SAlan Cox unsigned char *data, int length); 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds static void stop_read(struct edgeport_port *edge_port); 2141da177e4SLinus Torvalds static int restart_read(struct edgeport_port *edge_port); 2151da177e4SLinus Torvalds 21695da310eSAlan Cox static void edge_set_termios(struct tty_struct *tty, 21795da310eSAlan Cox struct usb_serial_port *port, struct ktermios *old_termios); 21895da310eSAlan Cox static void edge_send(struct tty_struct *tty); 2191da177e4SLinus Torvalds 220fc4cbd75SMartin K. Petersen /* sysfs attributes */ 221fc4cbd75SMartin K. Petersen static int edge_create_sysfs_attrs(struct usb_serial_port *port); 222fc4cbd75SMartin K. Petersen static int edge_remove_sysfs_attrs(struct usb_serial_port *port); 223fc4cbd75SMartin K. Petersen 2241da177e4SLinus Torvalds 2252742fd88SAlan Cox static int ti_vread_sync(struct usb_device *dev, __u8 request, 2262742fd88SAlan Cox __u16 value, __u16 index, u8 *data, int size) 2271da177e4SLinus Torvalds { 2281da177e4SLinus Torvalds int status; 2291da177e4SLinus Torvalds 2302742fd88SAlan Cox status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request, 2312742fd88SAlan Cox (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN), 2322742fd88SAlan Cox value, index, data, size, 1000); 2331da177e4SLinus Torvalds if (status < 0) 2341da177e4SLinus Torvalds return status; 2351da177e4SLinus Torvalds if (status != size) { 236*67e6da70SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s - wanted to write %d, but only wrote %d\n", 237441b62c1SHarvey Harrison __func__, size, status); 2381da177e4SLinus Torvalds return -ECOMM; 2391da177e4SLinus Torvalds } 2401da177e4SLinus Torvalds return 0; 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds 2432742fd88SAlan Cox static int ti_vsend_sync(struct usb_device *dev, __u8 request, 2442742fd88SAlan Cox __u16 value, __u16 index, u8 *data, int size) 2451da177e4SLinus Torvalds { 2461da177e4SLinus Torvalds int status; 2471da177e4SLinus Torvalds 2482742fd88SAlan Cox status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request, 2492742fd88SAlan Cox (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT), 2502742fd88SAlan Cox value, index, data, size, 1000); 2511da177e4SLinus Torvalds if (status < 0) 2521da177e4SLinus Torvalds return status; 2531da177e4SLinus Torvalds if (status != size) { 254*67e6da70SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s - wanted to write %d, but only wrote %d\n", 255441b62c1SHarvey Harrison __func__, size, status); 2561da177e4SLinus Torvalds return -ECOMM; 2571da177e4SLinus Torvalds } 2581da177e4SLinus Torvalds return 0; 2591da177e4SLinus Torvalds } 2601da177e4SLinus Torvalds 2612742fd88SAlan Cox static int send_cmd(struct usb_device *dev, __u8 command, 2621da177e4SLinus Torvalds __u8 moduleid, __u16 value, u8 *data, 2631da177e4SLinus Torvalds int size) 2641da177e4SLinus Torvalds { 2652742fd88SAlan Cox return ti_vsend_sync(dev, command, value, moduleid, data, size); 2661da177e4SLinus Torvalds } 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds /* clear tx/rx buffers and fifo in TI UMP */ 2692742fd88SAlan Cox static int purge_port(struct usb_serial_port *port, __u16 mask) 2701da177e4SLinus Torvalds { 2711da177e4SLinus Torvalds int port_number = port->number - port->serial->minor; 2721da177e4SLinus Torvalds 273*67e6da70SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - port %d, mask %x\n", __func__, port_number, mask); 2741da177e4SLinus Torvalds 2752742fd88SAlan Cox return send_cmd(port->serial->dev, 2761da177e4SLinus Torvalds UMPC_PURGE_PORT, 2771da177e4SLinus Torvalds (__u8)(UMPM_UART1_PORT + port_number), 2781da177e4SLinus Torvalds mask, 2791da177e4SLinus Torvalds NULL, 2801da177e4SLinus Torvalds 0); 2811da177e4SLinus Torvalds } 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds /** 2842742fd88SAlan Cox * read_download_mem - Read edgeport memory from TI chip 2851da177e4SLinus Torvalds * @dev: usb device pointer 2861da177e4SLinus Torvalds * @start_address: Device CPU address at which to read 2871da177e4SLinus Torvalds * @length: Length of above data 2881da177e4SLinus Torvalds * @address_type: Can read both XDATA and I2C 2891da177e4SLinus Torvalds * @buffer: pointer to input data buffer 2901da177e4SLinus Torvalds */ 2912742fd88SAlan Cox static int read_download_mem(struct usb_device *dev, int start_address, 2921da177e4SLinus Torvalds int length, __u8 address_type, __u8 *buffer) 2931da177e4SLinus Torvalds { 2941da177e4SLinus Torvalds int status = 0; 2951da177e4SLinus Torvalds __u8 read_length; 2961da177e4SLinus Torvalds __be16 be_start_address; 2971da177e4SLinus Torvalds 298*67e6da70SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, length); 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds /* Read in blocks of 64 bytes 3011da177e4SLinus Torvalds * (TI firmware can't handle more than 64 byte reads) 3021da177e4SLinus Torvalds */ 3031da177e4SLinus Torvalds while (length) { 3041da177e4SLinus Torvalds if (length > 64) 3051da177e4SLinus Torvalds read_length = 64; 3061da177e4SLinus Torvalds else 3071da177e4SLinus Torvalds read_length = (__u8)length; 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds if (read_length > 1) { 310*67e6da70SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, read_length); 3111da177e4SLinus Torvalds } 3121da177e4SLinus Torvalds be_start_address = cpu_to_be16(start_address); 3132742fd88SAlan Cox status = ti_vread_sync(dev, UMPC_MEMORY_READ, 3142742fd88SAlan Cox (__u16)address_type, 3152742fd88SAlan Cox (__force __u16)be_start_address, 3162742fd88SAlan Cox buffer, read_length); 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds if (status) { 319*67e6da70SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s - ERROR %x\n", __func__, status); 3201da177e4SLinus Torvalds return status; 3211da177e4SLinus Torvalds } 3221da177e4SLinus Torvalds 3232742fd88SAlan Cox if (read_length > 1) 324441b62c1SHarvey Harrison usb_serial_debug_data(debug, &dev->dev, __func__, 3251da177e4SLinus Torvalds read_length, buffer); 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds /* Update pointers/length */ 3281da177e4SLinus Torvalds start_address += read_length; 3291da177e4SLinus Torvalds buffer += read_length; 3301da177e4SLinus Torvalds length -= read_length; 3311da177e4SLinus Torvalds } 3321da177e4SLinus Torvalds 3331da177e4SLinus Torvalds return status; 3341da177e4SLinus Torvalds } 3351da177e4SLinus Torvalds 3362742fd88SAlan Cox static int read_ram(struct usb_device *dev, int start_address, 3372742fd88SAlan Cox int length, __u8 *buffer) 3381da177e4SLinus Torvalds { 3392742fd88SAlan Cox return read_download_mem(dev, start_address, length, 3402742fd88SAlan Cox DTK_ADDR_SPACE_XDATA, buffer); 3411da177e4SLinus Torvalds } 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds /* Read edgeport memory to a given block */ 3442742fd88SAlan Cox static int read_boot_mem(struct edgeport_serial *serial, 3452742fd88SAlan Cox int start_address, int length, __u8 *buffer) 3461da177e4SLinus Torvalds { 3471da177e4SLinus Torvalds int status = 0; 3481da177e4SLinus Torvalds int i; 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds for (i = 0; i < length; i++) { 3512742fd88SAlan Cox status = ti_vread_sync(serial->serial->dev, 3522742fd88SAlan Cox UMPC_MEMORY_READ, serial->TI_I2C_Type, 3532742fd88SAlan Cox (__u16)(start_address+i), &buffer[i], 0x01); 3541da177e4SLinus Torvalds if (status) { 355*67e6da70SGreg Kroah-Hartman dev_dbg(&serial->serial->dev->dev, "%s - ERROR %x\n", __func__, status); 3561da177e4SLinus Torvalds return status; 3571da177e4SLinus Torvalds } 3581da177e4SLinus Torvalds } 3591da177e4SLinus Torvalds 360*67e6da70SGreg Kroah-Hartman dev_dbg(&serial->serial->dev->dev, "%s - start_address = %x, length = %d\n", 3612742fd88SAlan Cox __func__, start_address, length); 3622742fd88SAlan Cox usb_serial_debug_data(debug, &serial->serial->dev->dev, 3632742fd88SAlan Cox __func__, length, buffer); 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds serial->TiReadI2C = 1; 3661da177e4SLinus Torvalds 3671da177e4SLinus Torvalds return status; 3681da177e4SLinus Torvalds } 3691da177e4SLinus Torvalds 3701da177e4SLinus Torvalds /* Write given block to TI EPROM memory */ 3712742fd88SAlan Cox static int write_boot_mem(struct edgeport_serial *serial, 3722742fd88SAlan Cox int start_address, int length, __u8 *buffer) 3731da177e4SLinus Torvalds { 3741da177e4SLinus Torvalds int status = 0; 3751da177e4SLinus Torvalds int i; 376e9305d2fSJohan Hovold u8 *temp; 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds /* Must do a read before write */ 3791da177e4SLinus Torvalds if (!serial->TiReadI2C) { 380e9305d2fSJohan Hovold temp = kmalloc(1, GFP_KERNEL); 381e9305d2fSJohan Hovold if (!temp) { 382e9305d2fSJohan Hovold dev_err(&serial->serial->dev->dev, 383e9305d2fSJohan Hovold "%s - out of memory\n", __func__); 384e9305d2fSJohan Hovold return -ENOMEM; 385e9305d2fSJohan Hovold } 386e9305d2fSJohan Hovold status = read_boot_mem(serial, 0, 1, temp); 387e9305d2fSJohan Hovold kfree(temp); 3881da177e4SLinus Torvalds if (status) 3891da177e4SLinus Torvalds return status; 3901da177e4SLinus Torvalds } 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds for (i = 0; i < length; ++i) { 3932742fd88SAlan Cox status = ti_vsend_sync(serial->serial->dev, 3942742fd88SAlan Cox UMPC_MEMORY_WRITE, buffer[i], 3952742fd88SAlan Cox (__u16)(i + start_address), NULL, 0); 3961da177e4SLinus Torvalds if (status) 3971da177e4SLinus Torvalds return status; 3981da177e4SLinus Torvalds } 3991da177e4SLinus Torvalds 400*67e6da70SGreg Kroah-Hartman dev_dbg(&serial->serial->dev->dev, "%s - start_sddr = %x, length = %d\n", __func__, start_address, length); 4012742fd88SAlan Cox usb_serial_debug_data(debug, &serial->serial->dev->dev, 4022742fd88SAlan Cox __func__, length, buffer); 4031da177e4SLinus Torvalds 4041da177e4SLinus Torvalds return status; 4051da177e4SLinus Torvalds } 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds /* Write edgeport I2C memory to TI chip */ 4092742fd88SAlan Cox static int write_i2c_mem(struct edgeport_serial *serial, 4102742fd88SAlan Cox int start_address, int length, __u8 address_type, __u8 *buffer) 4111da177e4SLinus Torvalds { 412*67e6da70SGreg Kroah-Hartman struct device *dev = &serial->serial->dev->dev; 4131da177e4SLinus Torvalds int status = 0; 4141da177e4SLinus Torvalds int write_length; 4151da177e4SLinus Torvalds __be16 be_start_address; 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds /* We can only send a maximum of 1 aligned byte page at a time */ 4181da177e4SLinus Torvalds 41925985edcSLucas De Marchi /* calculate the number of bytes left in the first page */ 4202742fd88SAlan Cox write_length = EPROM_PAGE_SIZE - 4212742fd88SAlan Cox (start_address & (EPROM_PAGE_SIZE - 1)); 4221da177e4SLinus Torvalds 4231da177e4SLinus Torvalds if (write_length > length) 4241da177e4SLinus Torvalds write_length = length; 4251da177e4SLinus Torvalds 426*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - BytesInFirstPage Addr = %x, length = %d\n", 4272742fd88SAlan Cox __func__, start_address, write_length); 428*67e6da70SGreg Kroah-Hartman usb_serial_debug_data(debug, dev, __func__, write_length, buffer); 4291da177e4SLinus Torvalds 4301da177e4SLinus Torvalds /* Write first page */ 4311da177e4SLinus Torvalds be_start_address = cpu_to_be16(start_address); 4322742fd88SAlan Cox status = ti_vsend_sync(serial->serial->dev, 4332742fd88SAlan Cox UMPC_MEMORY_WRITE, (__u16)address_type, 4342742fd88SAlan Cox (__force __u16)be_start_address, 4352742fd88SAlan Cox buffer, write_length); 4361da177e4SLinus Torvalds if (status) { 437*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - ERROR %d\n", __func__, status); 4381da177e4SLinus Torvalds return status; 4391da177e4SLinus Torvalds } 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds length -= write_length; 4421da177e4SLinus Torvalds start_address += write_length; 4431da177e4SLinus Torvalds buffer += write_length; 4441da177e4SLinus Torvalds 4452742fd88SAlan Cox /* We should be aligned now -- can write 4462742fd88SAlan Cox max page size bytes at a time */ 4471da177e4SLinus Torvalds while (length) { 4481da177e4SLinus Torvalds if (length > EPROM_PAGE_SIZE) 4491da177e4SLinus Torvalds write_length = EPROM_PAGE_SIZE; 4501da177e4SLinus Torvalds else 4511da177e4SLinus Torvalds write_length = length; 4521da177e4SLinus Torvalds 453*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - Page Write Addr = %x, length = %d\n", 4542742fd88SAlan Cox __func__, start_address, write_length); 455*67e6da70SGreg Kroah-Hartman usb_serial_debug_data(debug, dev, __func__, write_length, buffer); 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds /* Write next page */ 4581da177e4SLinus Torvalds be_start_address = cpu_to_be16(start_address); 4592742fd88SAlan Cox status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE, 4602742fd88SAlan Cox (__u16)address_type, 4612742fd88SAlan Cox (__force __u16)be_start_address, 4622742fd88SAlan Cox buffer, write_length); 4631da177e4SLinus Torvalds if (status) { 464*67e6da70SGreg Kroah-Hartman dev_err(dev, "%s - ERROR %d\n", __func__, status); 4651da177e4SLinus Torvalds return status; 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds 4681da177e4SLinus Torvalds length -= write_length; 4691da177e4SLinus Torvalds start_address += write_length; 4701da177e4SLinus Torvalds buffer += write_length; 4711da177e4SLinus Torvalds } 4721da177e4SLinus Torvalds return status; 4731da177e4SLinus Torvalds } 4741da177e4SLinus Torvalds 4751da177e4SLinus Torvalds /* Examine the UMP DMA registers and LSR 4761da177e4SLinus Torvalds * 4771da177e4SLinus Torvalds * Check the MSBit of the X and Y DMA byte count registers. 4781da177e4SLinus Torvalds * A zero in this bit indicates that the TX DMA buffers are empty 4791da177e4SLinus Torvalds * then check the TX Empty bit in the UART. 4801da177e4SLinus Torvalds */ 4812742fd88SAlan Cox static int tx_active(struct edgeport_port *port) 4821da177e4SLinus Torvalds { 4831da177e4SLinus Torvalds int status; 4841da177e4SLinus Torvalds struct out_endpoint_desc_block *oedb; 4851da177e4SLinus Torvalds __u8 *lsr; 4861da177e4SLinus Torvalds int bytes_left = 0; 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds oedb = kmalloc(sizeof(*oedb), GFP_KERNEL); 4891da177e4SLinus Torvalds if (!oedb) { 490441b62c1SHarvey Harrison dev_err(&port->port->dev, "%s - out of memory\n", __func__); 4911da177e4SLinus Torvalds return -ENOMEM; 4921da177e4SLinus Torvalds } 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds lsr = kmalloc(1, GFP_KERNEL); /* Sigh, that's right, just one byte, 4951da177e4SLinus Torvalds as not all platforms can do DMA 4961da177e4SLinus Torvalds from stack */ 4971da177e4SLinus Torvalds if (!lsr) { 4981da177e4SLinus Torvalds kfree(oedb); 4991da177e4SLinus Torvalds return -ENOMEM; 5001da177e4SLinus Torvalds } 5011da177e4SLinus Torvalds /* Read the DMA Count Registers */ 5022742fd88SAlan Cox status = read_ram(port->port->serial->dev, port->dma_address, 5032742fd88SAlan Cox sizeof(*oedb), (void *)oedb); 5041da177e4SLinus Torvalds if (status) 5051da177e4SLinus Torvalds goto exit_is_tx_active; 5061da177e4SLinus Torvalds 507*67e6da70SGreg Kroah-Hartman dev_dbg(&port->port->dev, "%s - XByteCount 0x%X\n", __func__, oedb->XByteCount); 5081da177e4SLinus Torvalds 5091da177e4SLinus Torvalds /* and the LSR */ 5102742fd88SAlan Cox status = read_ram(port->port->serial->dev, 5112742fd88SAlan Cox port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr); 5121da177e4SLinus Torvalds 5131da177e4SLinus Torvalds if (status) 5141da177e4SLinus Torvalds goto exit_is_tx_active; 515*67e6da70SGreg Kroah-Hartman dev_dbg(&port->port->dev, "%s - LSR = 0x%X\n", __func__, *lsr); 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds /* If either buffer has data or we are transmitting then return TRUE */ 5181da177e4SLinus Torvalds if ((oedb->XByteCount & 0x80) != 0) 5191da177e4SLinus Torvalds bytes_left += 64; 5201da177e4SLinus Torvalds 5211da177e4SLinus Torvalds if ((*lsr & UMP_UART_LSR_TX_MASK) == 0) 5221da177e4SLinus Torvalds bytes_left += 1; 5231da177e4SLinus Torvalds 5241da177e4SLinus Torvalds /* We return Not Active if we get any kind of error */ 5251da177e4SLinus Torvalds exit_is_tx_active: 526*67e6da70SGreg Kroah-Hartman dev_dbg(&port->port->dev, "%s - return %d\n", __func__, bytes_left); 5271da177e4SLinus Torvalds 5281da177e4SLinus Torvalds kfree(lsr); 5291da177e4SLinus Torvalds kfree(oedb); 5301da177e4SLinus Torvalds return bytes_left; 5311da177e4SLinus Torvalds } 5321da177e4SLinus Torvalds 5332742fd88SAlan Cox static void chase_port(struct edgeport_port *port, unsigned long timeout, 5342742fd88SAlan Cox int flush) 5351da177e4SLinus Torvalds { 5361da177e4SLinus Torvalds int baud_rate; 5374a90f09bSAlan Cox struct tty_struct *tty = tty_port_tty_get(&port->port->port); 538af581059SJohan Hovold struct usb_serial *serial = port->port->serial; 5391da177e4SLinus Torvalds wait_queue_t wait; 5401da177e4SLinus Torvalds unsigned long flags; 5411da177e4SLinus Torvalds 5421da177e4SLinus Torvalds if (!timeout) 5431da177e4SLinus Torvalds timeout = (HZ * EDGE_CLOSING_WAIT)/100; 5441da177e4SLinus Torvalds 5451da177e4SLinus Torvalds /* wait for data to drain from the buffer */ 5461da177e4SLinus Torvalds spin_lock_irqsave(&port->ep_lock, flags); 5471da177e4SLinus Torvalds init_waitqueue_entry(&wait, current); 5481da177e4SLinus Torvalds add_wait_queue(&tty->write_wait, &wait); 5491da177e4SLinus Torvalds for (;;) { 5501da177e4SLinus Torvalds set_current_state(TASK_INTERRUPTIBLE); 551d733cec1SJohan Hovold if (kfifo_len(&port->write_fifo) == 0 5521da177e4SLinus Torvalds || timeout == 0 || signal_pending(current) 553af581059SJohan Hovold || serial->disconnected) 5542742fd88SAlan Cox /* disconnect */ 5551da177e4SLinus Torvalds break; 5561da177e4SLinus Torvalds spin_unlock_irqrestore(&port->ep_lock, flags); 5571da177e4SLinus Torvalds timeout = schedule_timeout(timeout); 5581da177e4SLinus Torvalds spin_lock_irqsave(&port->ep_lock, flags); 5591da177e4SLinus Torvalds } 5601da177e4SLinus Torvalds set_current_state(TASK_RUNNING); 5611da177e4SLinus Torvalds remove_wait_queue(&tty->write_wait, &wait); 5621da177e4SLinus Torvalds if (flush) 563d733cec1SJohan Hovold kfifo_reset_out(&port->write_fifo); 5641da177e4SLinus Torvalds spin_unlock_irqrestore(&port->ep_lock, flags); 5654a90f09bSAlan Cox tty_kref_put(tty); 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds /* wait for data to drain from the device */ 5681da177e4SLinus Torvalds timeout += jiffies; 5691da177e4SLinus Torvalds while ((long)(jiffies - timeout) < 0 && !signal_pending(current) 570af581059SJohan Hovold && !serial->disconnected) { 5712742fd88SAlan Cox /* not disconnected */ 5722742fd88SAlan Cox if (!tx_active(port)) 5731da177e4SLinus Torvalds break; 5741da177e4SLinus Torvalds msleep(10); 5751da177e4SLinus Torvalds } 5761da177e4SLinus Torvalds 5771da177e4SLinus Torvalds /* disconnected */ 578af581059SJohan Hovold if (serial->disconnected) 5791da177e4SLinus Torvalds return; 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds /* wait one more character time, based on baud rate */ 5822742fd88SAlan Cox /* (tx_active doesn't seem to wait for the last byte) */ 5832742fd88SAlan Cox baud_rate = port->baud_rate; 5842742fd88SAlan Cox if (baud_rate == 0) 5851da177e4SLinus Torvalds baud_rate = 50; 586dfa5ec79SJulia Lawall msleep(max(1, DIV_ROUND_UP(10000, baud_rate))); 5871da177e4SLinus Torvalds } 5881da177e4SLinus Torvalds 5892742fd88SAlan Cox static int choose_config(struct usb_device *dev) 5901da177e4SLinus Torvalds { 5912742fd88SAlan Cox /* 5922742fd88SAlan Cox * There may be multiple configurations on this device, in which case 5932742fd88SAlan Cox * we would need to read and parse all of them to find out which one 5942742fd88SAlan Cox * we want. However, we just support one config at this point, 5952742fd88SAlan Cox * configuration # 1, which is Config Descriptor 0. 5962742fd88SAlan Cox */ 5971da177e4SLinus Torvalds 598*67e6da70SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s - Number of Interfaces = %d\n", 5992742fd88SAlan Cox __func__, dev->config->desc.bNumInterfaces); 600*67e6da70SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s - MAX Power = %d\n", 6012742fd88SAlan Cox __func__, dev->config->desc.bMaxPower * 2); 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds if (dev->config->desc.bNumInterfaces != 1) { 604*67e6da70SGreg Kroah-Hartman dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __func__); 6051da177e4SLinus Torvalds return -ENODEV; 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds 6081da177e4SLinus Torvalds return 0; 6091da177e4SLinus Torvalds } 6101da177e4SLinus Torvalds 6112742fd88SAlan Cox static int read_rom(struct edgeport_serial *serial, 6122742fd88SAlan Cox int start_address, int length, __u8 *buffer) 6131da177e4SLinus Torvalds { 6141da177e4SLinus Torvalds int status; 6151da177e4SLinus Torvalds 6161da177e4SLinus Torvalds if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) { 6172742fd88SAlan Cox status = read_download_mem(serial->serial->dev, 6181da177e4SLinus Torvalds start_address, 6191da177e4SLinus Torvalds length, 6201da177e4SLinus Torvalds serial->TI_I2C_Type, 6211da177e4SLinus Torvalds buffer); 6221da177e4SLinus Torvalds } else { 6232742fd88SAlan Cox status = read_boot_mem(serial, start_address, length, 6241da177e4SLinus Torvalds buffer); 6251da177e4SLinus Torvalds } 6261da177e4SLinus Torvalds return status; 6271da177e4SLinus Torvalds } 6281da177e4SLinus Torvalds 6292742fd88SAlan Cox static int write_rom(struct edgeport_serial *serial, int start_address, 6302742fd88SAlan Cox int length, __u8 *buffer) 6311da177e4SLinus Torvalds { 6321da177e4SLinus Torvalds if (serial->product_info.TiMode == TI_MODE_BOOT) 6332742fd88SAlan Cox return write_boot_mem(serial, start_address, length, 6341da177e4SLinus Torvalds buffer); 6351da177e4SLinus Torvalds 6361da177e4SLinus Torvalds if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) 6372742fd88SAlan Cox return write_i2c_mem(serial, start_address, length, 6382742fd88SAlan Cox serial->TI_I2C_Type, buffer); 6391da177e4SLinus Torvalds return -EINVAL; 6401da177e4SLinus Torvalds } 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds 6431da177e4SLinus Torvalds 6441da177e4SLinus Torvalds /* Read a descriptor header from I2C based on type */ 6452742fd88SAlan Cox static int get_descriptor_addr(struct edgeport_serial *serial, 6462742fd88SAlan Cox int desc_type, struct ti_i2c_desc *rom_desc) 6471da177e4SLinus Torvalds { 6481da177e4SLinus Torvalds int start_address; 6491da177e4SLinus Torvalds int status; 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds /* Search for requested descriptor in I2C */ 6521da177e4SLinus Torvalds start_address = 2; 6531da177e4SLinus Torvalds do { 6542742fd88SAlan Cox status = read_rom(serial, 6551da177e4SLinus Torvalds start_address, 6561da177e4SLinus Torvalds sizeof(struct ti_i2c_desc), 6571da177e4SLinus Torvalds (__u8 *)rom_desc); 6581da177e4SLinus Torvalds if (status) 6591da177e4SLinus Torvalds return 0; 6601da177e4SLinus Torvalds 6611da177e4SLinus Torvalds if (rom_desc->Type == desc_type) 6621da177e4SLinus Torvalds return start_address; 6631da177e4SLinus Torvalds 6642742fd88SAlan Cox start_address = start_address + sizeof(struct ti_i2c_desc) 6652742fd88SAlan Cox + rom_desc->Size; 6661da177e4SLinus Torvalds 6671da177e4SLinus Torvalds } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type); 6681da177e4SLinus Torvalds 6691da177e4SLinus Torvalds return 0; 6701da177e4SLinus Torvalds } 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds /* Validate descriptor checksum */ 6732742fd88SAlan Cox static int valid_csum(struct ti_i2c_desc *rom_desc, __u8 *buffer) 6741da177e4SLinus Torvalds { 6751da177e4SLinus Torvalds __u16 i; 6761da177e4SLinus Torvalds __u8 cs = 0; 6771da177e4SLinus Torvalds 6782742fd88SAlan Cox for (i = 0; i < rom_desc->Size; i++) 6791da177e4SLinus Torvalds cs = (__u8)(cs + buffer[i]); 6802742fd88SAlan Cox 6811da177e4SLinus Torvalds if (cs != rom_desc->CheckSum) { 682*67e6da70SGreg Kroah-Hartman pr_debug("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs); 6831da177e4SLinus Torvalds return -EINVAL; 6841da177e4SLinus Torvalds } 6851da177e4SLinus Torvalds return 0; 6861da177e4SLinus Torvalds } 6871da177e4SLinus Torvalds 6881da177e4SLinus Torvalds /* Make sure that the I2C image is good */ 6892742fd88SAlan Cox static int check_i2c_image(struct edgeport_serial *serial) 6901da177e4SLinus Torvalds { 6911da177e4SLinus Torvalds struct device *dev = &serial->serial->dev->dev; 6921da177e4SLinus Torvalds int status = 0; 6931da177e4SLinus Torvalds struct ti_i2c_desc *rom_desc; 6941da177e4SLinus Torvalds int start_address = 2; 6951da177e4SLinus Torvalds __u8 *buffer; 6961da177e4SLinus Torvalds __u16 ttype; 6971da177e4SLinus Torvalds 6981da177e4SLinus Torvalds rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL); 6991da177e4SLinus Torvalds if (!rom_desc) { 700441b62c1SHarvey Harrison dev_err(dev, "%s - out of memory\n", __func__); 7011da177e4SLinus Torvalds return -ENOMEM; 7021da177e4SLinus Torvalds } 7031da177e4SLinus Torvalds buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL); 7041da177e4SLinus Torvalds if (!buffer) { 7052742fd88SAlan Cox dev_err(dev, "%s - out of memory when allocating buffer\n", 7062742fd88SAlan Cox __func__); 7071da177e4SLinus Torvalds kfree(rom_desc); 7081da177e4SLinus Torvalds return -ENOMEM; 7091da177e4SLinus Torvalds } 7101da177e4SLinus Torvalds 7112742fd88SAlan Cox /* Read the first byte (Signature0) must be 0x52 or 0x10 */ 7122742fd88SAlan Cox status = read_rom(serial, 0, 1, buffer); 7131da177e4SLinus Torvalds if (status) 7142742fd88SAlan Cox goto out; 7151da177e4SLinus Torvalds 7161da177e4SLinus Torvalds if (*buffer != UMP5152 && *buffer != UMP3410) { 717441b62c1SHarvey Harrison dev_err(dev, "%s - invalid buffer signature\n", __func__); 7181da177e4SLinus Torvalds status = -ENODEV; 7192742fd88SAlan Cox goto out; 7201da177e4SLinus Torvalds } 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds do { 7232742fd88SAlan Cox /* Validate the I2C */ 7242742fd88SAlan Cox status = read_rom(serial, 7251da177e4SLinus Torvalds start_address, 7261da177e4SLinus Torvalds sizeof(struct ti_i2c_desc), 7271da177e4SLinus Torvalds (__u8 *)rom_desc); 7281da177e4SLinus Torvalds if (status) 7291da177e4SLinus Torvalds break; 7301da177e4SLinus Torvalds 7312742fd88SAlan Cox if ((start_address + sizeof(struct ti_i2c_desc) + 7322742fd88SAlan Cox rom_desc->Size) > TI_MAX_I2C_SIZE) { 7331da177e4SLinus Torvalds status = -ENODEV; 734*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - structure too big, erroring out.\n", __func__); 7351da177e4SLinus Torvalds break; 7361da177e4SLinus Torvalds } 7371da177e4SLinus Torvalds 738*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s Type = 0x%x\n", __func__, rom_desc->Type); 7391da177e4SLinus Torvalds 7402742fd88SAlan Cox /* Skip type 2 record */ 7411da177e4SLinus Torvalds ttype = rom_desc->Type & 0x0f; 7421da177e4SLinus Torvalds if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC 7431da177e4SLinus Torvalds && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) { 7442742fd88SAlan Cox /* Read the descriptor data */ 7452742fd88SAlan Cox status = read_rom(serial, start_address + 7462742fd88SAlan Cox sizeof(struct ti_i2c_desc), 7472742fd88SAlan Cox rom_desc->Size, buffer); 7481da177e4SLinus Torvalds if (status) 7491da177e4SLinus Torvalds break; 7501da177e4SLinus Torvalds 7512742fd88SAlan Cox status = valid_csum(rom_desc, buffer); 7521da177e4SLinus Torvalds if (status) 7531da177e4SLinus Torvalds break; 7541da177e4SLinus Torvalds } 7552742fd88SAlan Cox start_address = start_address + sizeof(struct ti_i2c_desc) + 7562742fd88SAlan Cox rom_desc->Size; 7571da177e4SLinus Torvalds 7582742fd88SAlan Cox } while ((rom_desc->Type != I2C_DESC_TYPE_ION) && 7592742fd88SAlan Cox (start_address < TI_MAX_I2C_SIZE)); 7601da177e4SLinus Torvalds 7612742fd88SAlan Cox if ((rom_desc->Type != I2C_DESC_TYPE_ION) || 7622742fd88SAlan Cox (start_address > TI_MAX_I2C_SIZE)) 7631da177e4SLinus Torvalds status = -ENODEV; 7641da177e4SLinus Torvalds 7652742fd88SAlan Cox out: 7661da177e4SLinus Torvalds kfree(buffer); 7671da177e4SLinus Torvalds kfree(rom_desc); 7681da177e4SLinus Torvalds return status; 7691da177e4SLinus Torvalds } 7701da177e4SLinus Torvalds 7712742fd88SAlan Cox static int get_manuf_info(struct edgeport_serial *serial, __u8 *buffer) 7721da177e4SLinus Torvalds { 7731da177e4SLinus Torvalds int status; 7741da177e4SLinus Torvalds int start_address; 7751da177e4SLinus Torvalds struct ti_i2c_desc *rom_desc; 7761da177e4SLinus Torvalds struct edge_ti_manuf_descriptor *desc; 777*67e6da70SGreg Kroah-Hartman struct device *dev = &serial->serial->dev->dev; 7781da177e4SLinus Torvalds 7791da177e4SLinus Torvalds rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL); 7801da177e4SLinus Torvalds if (!rom_desc) { 781*67e6da70SGreg Kroah-Hartman dev_err(dev, "%s - out of memory\n", __func__); 7821da177e4SLinus Torvalds return -ENOMEM; 7831da177e4SLinus Torvalds } 7842742fd88SAlan Cox start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION, 7852742fd88SAlan Cox rom_desc); 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds if (!start_address) { 788*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - Edge Descriptor not found in I2C\n", __func__); 7891da177e4SLinus Torvalds status = -ENODEV; 7901da177e4SLinus Torvalds goto exit; 7911da177e4SLinus Torvalds } 7921da177e4SLinus Torvalds 7932742fd88SAlan Cox /* Read the descriptor data */ 7942742fd88SAlan Cox status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc), 7952742fd88SAlan Cox rom_desc->Size, buffer); 7961da177e4SLinus Torvalds if (status) 7971da177e4SLinus Torvalds goto exit; 7981da177e4SLinus Torvalds 7992742fd88SAlan Cox status = valid_csum(rom_desc, buffer); 8001da177e4SLinus Torvalds 8011da177e4SLinus Torvalds desc = (struct edge_ti_manuf_descriptor *)buffer; 802*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - IonConfig 0x%x\n", __func__, desc->IonConfig); 803*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - Version %d\n", __func__, desc->Version); 804*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - Cpu/Board 0x%x\n", __func__, desc->CpuRev_BoardRev); 805*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - NumPorts %d\n", __func__, desc->NumPorts); 806*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - NumVirtualPorts %d\n", __func__, desc->NumVirtualPorts); 807*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - TotalPorts %d\n", __func__, desc->TotalPorts); 8081da177e4SLinus Torvalds 8091da177e4SLinus Torvalds exit: 8101da177e4SLinus Torvalds kfree(rom_desc); 8111da177e4SLinus Torvalds return status; 8121da177e4SLinus Torvalds } 8131da177e4SLinus Torvalds 8141da177e4SLinus Torvalds /* Build firmware header used for firmware update */ 8152742fd88SAlan Cox static int build_i2c_fw_hdr(__u8 *header, struct device *dev) 8161da177e4SLinus Torvalds { 8171da177e4SLinus Torvalds __u8 *buffer; 8181da177e4SLinus Torvalds int buffer_size; 8191da177e4SLinus Torvalds int i; 820d12b219aSJaswinder Singh int err; 8211da177e4SLinus Torvalds __u8 cs = 0; 8221da177e4SLinus Torvalds struct ti_i2c_desc *i2c_header; 8231da177e4SLinus Torvalds struct ti_i2c_image_header *img_header; 8241da177e4SLinus Torvalds struct ti_i2c_firmware_rec *firmware_rec; 825d12b219aSJaswinder Singh const struct firmware *fw; 826d12b219aSJaswinder Singh const char *fw_name = "edgeport/down3.bin"; 8271da177e4SLinus Torvalds 8282742fd88SAlan Cox /* In order to update the I2C firmware we must change the type 2 record 8292742fd88SAlan Cox * to type 0xF2. This will force the UMP to come up in Boot Mode. 8302742fd88SAlan Cox * Then while in boot mode, the driver will download the latest 8312742fd88SAlan Cox * firmware (padded to 15.5k) into the UMP ram. And finally when the 8322742fd88SAlan Cox * device comes back up in download mode the driver will cause the new 8332742fd88SAlan Cox * firmware to be copied from the UMP Ram to I2C and the firmware will 8342742fd88SAlan Cox * update the record type from 0xf2 to 0x02. 8352742fd88SAlan Cox */ 8361da177e4SLinus Torvalds 8372742fd88SAlan Cox /* Allocate a 15.5k buffer + 2 bytes for version number 8382742fd88SAlan Cox * (Firmware Record) */ 8392742fd88SAlan Cox buffer_size = (((1024 * 16) - 512 ) + 8402742fd88SAlan Cox sizeof(struct ti_i2c_firmware_rec)); 8411da177e4SLinus Torvalds 8421da177e4SLinus Torvalds buffer = kmalloc(buffer_size, GFP_KERNEL); 8431da177e4SLinus Torvalds if (!buffer) { 844441b62c1SHarvey Harrison dev_err(dev, "%s - out of memory\n", __func__); 8451da177e4SLinus Torvalds return -ENOMEM; 8461da177e4SLinus Torvalds } 8471da177e4SLinus Torvalds 8481da177e4SLinus Torvalds // Set entire image of 0xffs 8491da177e4SLinus Torvalds memset(buffer, 0xff, buffer_size); 8501da177e4SLinus Torvalds 851d12b219aSJaswinder Singh err = request_firmware(&fw, fw_name, dev); 852d12b219aSJaswinder Singh if (err) { 853d12b219aSJaswinder Singh printk(KERN_ERR "Failed to load image \"%s\" err %d\n", 854d12b219aSJaswinder Singh fw_name, err); 855d12b219aSJaswinder Singh kfree(buffer); 856d12b219aSJaswinder Singh return err; 857d12b219aSJaswinder Singh } 858d12b219aSJaswinder Singh 859d12b219aSJaswinder Singh /* Save Download Version Number */ 860d12b219aSJaswinder Singh OperationalMajorVersion = fw->data[0]; 861d12b219aSJaswinder Singh OperationalMinorVersion = fw->data[1]; 862d12b219aSJaswinder Singh OperationalBuildNumber = fw->data[2] | (fw->data[3] << 8); 863d12b219aSJaswinder Singh 8642742fd88SAlan Cox /* Copy version number into firmware record */ 8651da177e4SLinus Torvalds firmware_rec = (struct ti_i2c_firmware_rec *)buffer; 8661da177e4SLinus Torvalds 867d12b219aSJaswinder Singh firmware_rec->Ver_Major = OperationalMajorVersion; 868d12b219aSJaswinder Singh firmware_rec->Ver_Minor = OperationalMinorVersion; 8691da177e4SLinus Torvalds 8702742fd88SAlan Cox /* Pointer to fw_down memory image */ 871d12b219aSJaswinder Singh img_header = (struct ti_i2c_image_header *)&fw->data[4]; 8721da177e4SLinus Torvalds 8731da177e4SLinus Torvalds memcpy(buffer + sizeof(struct ti_i2c_firmware_rec), 874d12b219aSJaswinder Singh &fw->data[4 + sizeof(struct ti_i2c_image_header)], 8751da177e4SLinus Torvalds le16_to_cpu(img_header->Length)); 8761da177e4SLinus Torvalds 877d12b219aSJaswinder Singh release_firmware(fw); 878d12b219aSJaswinder Singh 8791da177e4SLinus Torvalds for (i=0; i < buffer_size; i++) { 8801da177e4SLinus Torvalds cs = (__u8)(cs + buffer[i]); 8811da177e4SLinus Torvalds } 8821da177e4SLinus Torvalds 8831da177e4SLinus Torvalds kfree(buffer); 8841da177e4SLinus Torvalds 8852742fd88SAlan Cox /* Build new header */ 8861da177e4SLinus Torvalds i2c_header = (struct ti_i2c_desc *)header; 8871da177e4SLinus Torvalds firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data; 8881da177e4SLinus Torvalds 8891da177e4SLinus Torvalds i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK; 8901da177e4SLinus Torvalds i2c_header->Size = (__u16)buffer_size; 8911da177e4SLinus Torvalds i2c_header->CheckSum = cs; 892d12b219aSJaswinder Singh firmware_rec->Ver_Major = OperationalMajorVersion; 893d12b219aSJaswinder Singh firmware_rec->Ver_Minor = OperationalMinorVersion; 8941da177e4SLinus Torvalds 8951da177e4SLinus Torvalds return 0; 8961da177e4SLinus Torvalds } 8971da177e4SLinus Torvalds 8981da177e4SLinus Torvalds /* Try to figure out what type of I2c we have */ 8992742fd88SAlan Cox static int i2c_type_bootmode(struct edgeport_serial *serial) 9001da177e4SLinus Torvalds { 901*67e6da70SGreg Kroah-Hartman struct device *dev = &serial->serial->dev->dev; 9021da177e4SLinus Torvalds int status; 903e9305d2fSJohan Hovold u8 *data; 904e9305d2fSJohan Hovold 905e9305d2fSJohan Hovold data = kmalloc(1, GFP_KERNEL); 906e9305d2fSJohan Hovold if (!data) { 907*67e6da70SGreg Kroah-Hartman dev_err(dev, "%s - out of memory\n", __func__); 908e9305d2fSJohan Hovold return -ENOMEM; 909e9305d2fSJohan Hovold } 9101da177e4SLinus Torvalds 9112742fd88SAlan Cox /* Try to read type 2 */ 9122742fd88SAlan Cox status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ, 913e9305d2fSJohan Hovold DTK_ADDR_SPACE_I2C_TYPE_II, 0, data, 0x01); 9141da177e4SLinus Torvalds if (status) 915*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - read 2 status error = %d\n", __func__, status); 9161da177e4SLinus Torvalds else 917*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data); 918e9305d2fSJohan Hovold if ((!status) && (*data == UMP5152 || *data == UMP3410)) { 919*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - ROM_TYPE_II\n", __func__); 9201da177e4SLinus Torvalds serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II; 921e9305d2fSJohan Hovold goto out; 9221da177e4SLinus Torvalds } 9231da177e4SLinus Torvalds 9242742fd88SAlan Cox /* Try to read type 3 */ 9252742fd88SAlan Cox status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ, 926e9305d2fSJohan Hovold DTK_ADDR_SPACE_I2C_TYPE_III, 0, data, 0x01); 9271da177e4SLinus Torvalds if (status) 928*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - read 3 status error = %d\n", __func__, status); 9291da177e4SLinus Torvalds else 930*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data); 931e9305d2fSJohan Hovold if ((!status) && (*data == UMP5152 || *data == UMP3410)) { 932*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - ROM_TYPE_III\n", __func__); 9331da177e4SLinus Torvalds serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III; 934e9305d2fSJohan Hovold goto out; 9351da177e4SLinus Torvalds } 9361da177e4SLinus Torvalds 937*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - Unknown\n", __func__); 9381da177e4SLinus Torvalds serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II; 939e9305d2fSJohan Hovold status = -ENODEV; 940e9305d2fSJohan Hovold out: 941e9305d2fSJohan Hovold kfree(data); 942e9305d2fSJohan Hovold return status; 9431da177e4SLinus Torvalds } 9441da177e4SLinus Torvalds 9452742fd88SAlan Cox static int bulk_xfer(struct usb_serial *serial, void *buffer, 9462742fd88SAlan Cox int length, int *num_sent) 9471da177e4SLinus Torvalds { 9481da177e4SLinus Torvalds int status; 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds status = usb_bulk_msg(serial->dev, 9511da177e4SLinus Torvalds usb_sndbulkpipe(serial->dev, 9521da177e4SLinus Torvalds serial->port[0]->bulk_out_endpointAddress), 9532742fd88SAlan Cox buffer, length, num_sent, 1000); 9541da177e4SLinus Torvalds return status; 9551da177e4SLinus Torvalds } 9561da177e4SLinus Torvalds 9571da177e4SLinus Torvalds /* Download given firmware image to the device (IN BOOT MODE) */ 9582742fd88SAlan Cox static int download_code(struct edgeport_serial *serial, __u8 *image, 9592742fd88SAlan Cox int image_length) 9601da177e4SLinus Torvalds { 9611da177e4SLinus Torvalds int status = 0; 9621da177e4SLinus Torvalds int pos; 9631da177e4SLinus Torvalds int transfer; 9641da177e4SLinus Torvalds int done; 9651da177e4SLinus Torvalds 9662742fd88SAlan Cox /* Transfer firmware image */ 9671da177e4SLinus Torvalds for (pos = 0; pos < image_length; ) { 9682742fd88SAlan Cox /* Read the next buffer from file */ 9691da177e4SLinus Torvalds transfer = image_length - pos; 9701da177e4SLinus Torvalds if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE) 9711da177e4SLinus Torvalds transfer = EDGE_FW_BULK_MAX_PACKET_SIZE; 9721da177e4SLinus Torvalds 9732742fd88SAlan Cox /* Transfer data */ 9742742fd88SAlan Cox status = bulk_xfer(serial->serial, &image[pos], 9752742fd88SAlan Cox transfer, &done); 9761da177e4SLinus Torvalds if (status) 9771da177e4SLinus Torvalds break; 9782742fd88SAlan Cox /* Advance buffer pointer */ 9791da177e4SLinus Torvalds pos += done; 9801da177e4SLinus Torvalds } 9811da177e4SLinus Torvalds 9821da177e4SLinus Torvalds return status; 9831da177e4SLinus Torvalds } 9841da177e4SLinus Torvalds 9852742fd88SAlan Cox /* FIXME!!! */ 9862742fd88SAlan Cox static int config_boot_dev(struct usb_device *dev) 9871da177e4SLinus Torvalds { 9881da177e4SLinus Torvalds return 0; 9891da177e4SLinus Torvalds } 9901da177e4SLinus Torvalds 9912742fd88SAlan Cox static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc) 9922742fd88SAlan Cox { 9932742fd88SAlan Cox return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev); 9942742fd88SAlan Cox } 9952742fd88SAlan Cox 9961da177e4SLinus Torvalds /** 9971da177e4SLinus Torvalds * DownloadTIFirmware - Download run-time operating firmware to the TI5052 9981da177e4SLinus Torvalds * 9991da177e4SLinus Torvalds * This routine downloads the main operating code into the TI5052, using the 10001da177e4SLinus Torvalds * boot code already burned into E2PROM or ROM. 10011da177e4SLinus Torvalds */ 10022742fd88SAlan Cox static int download_fw(struct edgeport_serial *serial) 10031da177e4SLinus Torvalds { 10041da177e4SLinus Torvalds struct device *dev = &serial->serial->dev->dev; 10051da177e4SLinus Torvalds int status = 0; 10061da177e4SLinus Torvalds int start_address; 10071da177e4SLinus Torvalds struct edge_ti_manuf_descriptor *ti_manuf_desc; 10081da177e4SLinus Torvalds struct usb_interface_descriptor *interface; 10091da177e4SLinus Torvalds int download_cur_ver; 10101da177e4SLinus Torvalds int download_new_ver; 10111da177e4SLinus Torvalds 10121da177e4SLinus Torvalds /* This routine is entered by both the BOOT mode and the Download mode 10131da177e4SLinus Torvalds * We can determine which code is running by the reading the config 10141da177e4SLinus Torvalds * descriptor and if we have only one bulk pipe it is in boot mode 10151da177e4SLinus Torvalds */ 10161da177e4SLinus Torvalds serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP; 10171da177e4SLinus Torvalds 10181da177e4SLinus Torvalds /* Default to type 2 i2c */ 10191da177e4SLinus Torvalds serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II; 10201da177e4SLinus Torvalds 10212742fd88SAlan Cox status = choose_config(serial->serial->dev); 10221da177e4SLinus Torvalds if (status) 10231da177e4SLinus Torvalds return status; 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds interface = &serial->serial->interface->cur_altsetting->desc; 10261da177e4SLinus Torvalds if (!interface) { 1027441b62c1SHarvey Harrison dev_err(dev, "%s - no interface set, error!\n", __func__); 10281da177e4SLinus Torvalds return -ENODEV; 10291da177e4SLinus Torvalds } 10301da177e4SLinus Torvalds 10312742fd88SAlan Cox /* 10322742fd88SAlan Cox * Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING 10332742fd88SAlan Cox * if we have more than one endpoint we are definitely in download 10342742fd88SAlan Cox * mode 10352742fd88SAlan Cox */ 10361da177e4SLinus Torvalds if (interface->bNumEndpoints > 1) 10371da177e4SLinus Torvalds serial->product_info.TiMode = TI_MODE_DOWNLOAD; 10381da177e4SLinus Torvalds else 10392742fd88SAlan Cox /* Otherwise we will remain in configuring mode */ 10401da177e4SLinus Torvalds serial->product_info.TiMode = TI_MODE_CONFIGURING; 10411da177e4SLinus Torvalds 10421da177e4SLinus Torvalds /********************************************************************/ 10431da177e4SLinus Torvalds /* Download Mode */ 10441da177e4SLinus Torvalds /********************************************************************/ 10451da177e4SLinus Torvalds if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) { 10461da177e4SLinus Torvalds struct ti_i2c_desc *rom_desc; 10471da177e4SLinus Torvalds 1048*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - RUNNING IN DOWNLOAD MODE\n", __func__); 10491da177e4SLinus Torvalds 10502742fd88SAlan Cox status = check_i2c_image(serial); 10511da177e4SLinus Torvalds if (status) { 1052*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - DOWNLOAD MODE -- BAD I2C\n", __func__); 10531da177e4SLinus Torvalds return status; 10541da177e4SLinus Torvalds } 10551da177e4SLinus Torvalds 10561da177e4SLinus Torvalds /* Validate Hardware version number 10571da177e4SLinus Torvalds * Read Manufacturing Descriptor from TI Based Edgeport 10581da177e4SLinus Torvalds */ 10591da177e4SLinus Torvalds ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL); 10601da177e4SLinus Torvalds if (!ti_manuf_desc) { 1061441b62c1SHarvey Harrison dev_err(dev, "%s - out of memory.\n", __func__); 10621da177e4SLinus Torvalds return -ENOMEM; 10631da177e4SLinus Torvalds } 10642742fd88SAlan Cox status = get_manuf_info(serial, (__u8 *)ti_manuf_desc); 10651da177e4SLinus Torvalds if (status) { 10661da177e4SLinus Torvalds kfree(ti_manuf_desc); 10671da177e4SLinus Torvalds return status; 10681da177e4SLinus Torvalds } 10691da177e4SLinus Torvalds 10702742fd88SAlan Cox /* Check version number of ION descriptor */ 10712742fd88SAlan Cox if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) { 1072*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n", 10732742fd88SAlan Cox __func__, ti_cpu_rev(ti_manuf_desc)); 10741da177e4SLinus Torvalds kfree(ti_manuf_desc); 10751da177e4SLinus Torvalds return -EINVAL; 10761da177e4SLinus Torvalds } 10771da177e4SLinus Torvalds 10781da177e4SLinus Torvalds rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL); 10791da177e4SLinus Torvalds if (!rom_desc) { 1080441b62c1SHarvey Harrison dev_err(dev, "%s - out of memory.\n", __func__); 10811da177e4SLinus Torvalds kfree(ti_manuf_desc); 10821da177e4SLinus Torvalds return -ENOMEM; 10831da177e4SLinus Torvalds } 10841da177e4SLinus Torvalds 10852742fd88SAlan Cox /* Search for type 2 record (firmware record) */ 10862742fd88SAlan Cox start_address = get_descriptor_addr(serial, 10872742fd88SAlan Cox I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc); 10882742fd88SAlan Cox if (start_address != 0) { 10891da177e4SLinus Torvalds struct ti_i2c_firmware_rec *firmware_version; 1090e9305d2fSJohan Hovold u8 *record; 10911da177e4SLinus Torvalds 1092*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - Found Type FIRMWARE (Type 2) record\n", __func__); 10931da177e4SLinus Torvalds 10942742fd88SAlan Cox firmware_version = kmalloc(sizeof(*firmware_version), 10952742fd88SAlan Cox GFP_KERNEL); 10961da177e4SLinus Torvalds if (!firmware_version) { 1097441b62c1SHarvey Harrison dev_err(dev, "%s - out of memory.\n", __func__); 10981da177e4SLinus Torvalds kfree(rom_desc); 10991da177e4SLinus Torvalds kfree(ti_manuf_desc); 11001da177e4SLinus Torvalds return -ENOMEM; 11011da177e4SLinus Torvalds } 11021da177e4SLinus Torvalds 11032742fd88SAlan Cox /* Validate version number 11042742fd88SAlan Cox * Read the descriptor data 11052742fd88SAlan Cox */ 11062742fd88SAlan Cox status = read_rom(serial, start_address + 11072742fd88SAlan Cox sizeof(struct ti_i2c_desc), 11081da177e4SLinus Torvalds sizeof(struct ti_i2c_firmware_rec), 11091da177e4SLinus Torvalds (__u8 *)firmware_version); 11101da177e4SLinus Torvalds if (status) { 11111da177e4SLinus Torvalds kfree(firmware_version); 11121da177e4SLinus Torvalds kfree(rom_desc); 11131da177e4SLinus Torvalds kfree(ti_manuf_desc); 11141da177e4SLinus Torvalds return status; 11151da177e4SLinus Torvalds } 11161da177e4SLinus Torvalds 11172742fd88SAlan Cox /* Check version number of download with current 11182742fd88SAlan Cox version in I2c */ 11191da177e4SLinus Torvalds download_cur_ver = (firmware_version->Ver_Major << 8) + 11201da177e4SLinus Torvalds (firmware_version->Ver_Minor); 1121d12b219aSJaswinder Singh download_new_ver = (OperationalMajorVersion << 8) + 1122d12b219aSJaswinder Singh (OperationalMinorVersion); 11231da177e4SLinus Torvalds 1124*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - >> FW Versions Device %d.%d Driver %d.%d\n", 1125*67e6da70SGreg Kroah-Hartman __func__, firmware_version->Ver_Major, 11261da177e4SLinus Torvalds firmware_version->Ver_Minor, 1127d12b219aSJaswinder Singh OperationalMajorVersion, 1128d12b219aSJaswinder Singh OperationalMinorVersion); 11291da177e4SLinus Torvalds 11302742fd88SAlan Cox /* Check if we have an old version in the I2C and 11312742fd88SAlan Cox update if necessary */ 11320827a9ffSGreg Kroah-Hartman if (download_cur_ver < download_new_ver) { 1133*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - Update I2C dld from %d.%d to %d.%d\n", 1134441b62c1SHarvey Harrison __func__, 11351da177e4SLinus Torvalds firmware_version->Ver_Major, 11361da177e4SLinus Torvalds firmware_version->Ver_Minor, 1137d12b219aSJaswinder Singh OperationalMajorVersion, 1138d12b219aSJaswinder Singh OperationalMinorVersion); 11391da177e4SLinus Torvalds 1140e9305d2fSJohan Hovold record = kmalloc(1, GFP_KERNEL); 1141e9305d2fSJohan Hovold if (!record) { 1142e9305d2fSJohan Hovold dev_err(dev, "%s - out of memory.\n", 1143e9305d2fSJohan Hovold __func__); 1144e9305d2fSJohan Hovold kfree(firmware_version); 1145e9305d2fSJohan Hovold kfree(rom_desc); 1146e9305d2fSJohan Hovold kfree(ti_manuf_desc); 1147e9305d2fSJohan Hovold return -ENOMEM; 1148e9305d2fSJohan Hovold } 11492742fd88SAlan Cox /* In order to update the I2C firmware we must 11502742fd88SAlan Cox * change the type 2 record to type 0xF2. This 11512742fd88SAlan Cox * will force the UMP to come up in Boot Mode. 11522742fd88SAlan Cox * Then while in boot mode, the driver will 11532742fd88SAlan Cox * download the latest firmware (padded to 11542742fd88SAlan Cox * 15.5k) into the UMP ram. Finally when the 11552742fd88SAlan Cox * device comes back up in download mode the 11562742fd88SAlan Cox * driver will cause the new firmware to be 11572742fd88SAlan Cox * copied from the UMP Ram to I2C and the 11582742fd88SAlan Cox * firmware will update the record type from 11592742fd88SAlan Cox * 0xf2 to 0x02. 11602742fd88SAlan Cox */ 1161e9305d2fSJohan Hovold *record = I2C_DESC_TYPE_FIRMWARE_BLANK; 11621da177e4SLinus Torvalds 11632742fd88SAlan Cox /* Change the I2C Firmware record type to 11642742fd88SAlan Cox 0xf2 to trigger an update */ 11652742fd88SAlan Cox status = write_rom(serial, start_address, 1166e9305d2fSJohan Hovold sizeof(*record), record); 11671da177e4SLinus Torvalds if (status) { 1168e9305d2fSJohan Hovold kfree(record); 11691da177e4SLinus Torvalds kfree(firmware_version); 11701da177e4SLinus Torvalds kfree(rom_desc); 11711da177e4SLinus Torvalds kfree(ti_manuf_desc); 11721da177e4SLinus Torvalds return status; 11731da177e4SLinus Torvalds } 11741da177e4SLinus Torvalds 11752742fd88SAlan Cox /* verify the write -- must do this in order 11762742fd88SAlan Cox * for write to complete before we do the 11772742fd88SAlan Cox * hardware reset 11782742fd88SAlan Cox */ 11792742fd88SAlan Cox status = read_rom(serial, 11801da177e4SLinus Torvalds start_address, 1181e9305d2fSJohan Hovold sizeof(*record), 1182e9305d2fSJohan Hovold record); 11831da177e4SLinus Torvalds if (status) { 1184e9305d2fSJohan Hovold kfree(record); 11851da177e4SLinus Torvalds kfree(firmware_version); 11861da177e4SLinus Torvalds kfree(rom_desc); 11871da177e4SLinus Torvalds kfree(ti_manuf_desc); 11881da177e4SLinus Torvalds return status; 11891da177e4SLinus Torvalds } 11901da177e4SLinus Torvalds 1191e9305d2fSJohan Hovold if (*record != I2C_DESC_TYPE_FIRMWARE_BLANK) { 1192*67e6da70SGreg Kroah-Hartman dev_err(dev, "%s - error resetting device\n", __func__); 1193e9305d2fSJohan Hovold kfree(record); 11941da177e4SLinus Torvalds kfree(firmware_version); 11951da177e4SLinus Torvalds kfree(rom_desc); 11961da177e4SLinus Torvalds kfree(ti_manuf_desc); 11971da177e4SLinus Torvalds return -ENODEV; 11981da177e4SLinus Torvalds } 11991da177e4SLinus Torvalds 1200*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - HARDWARE RESET\n", __func__); 12011da177e4SLinus Torvalds 12022742fd88SAlan Cox /* Reset UMP -- Back to BOOT MODE */ 12032742fd88SAlan Cox status = ti_vsend_sync(serial->serial->dev, 12042742fd88SAlan Cox UMPC_HARDWARE_RESET, 12052742fd88SAlan Cox 0, 0, NULL, 0); 12061da177e4SLinus Torvalds 1207*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - HARDWARE RESET return %d\n", __func__, status); 12081da177e4SLinus Torvalds 12091da177e4SLinus Torvalds /* return an error on purpose. */ 1210e9305d2fSJohan Hovold kfree(record); 12111da177e4SLinus Torvalds kfree(firmware_version); 12121da177e4SLinus Torvalds kfree(rom_desc); 12131da177e4SLinus Torvalds kfree(ti_manuf_desc); 12141da177e4SLinus Torvalds return -ENODEV; 12151da177e4SLinus Torvalds } 12161da177e4SLinus Torvalds kfree(firmware_version); 12171da177e4SLinus Torvalds } 12182742fd88SAlan Cox /* Search for type 0xF2 record (firmware blank record) */ 12192742fd88SAlan Cox else if ((start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) { 12202742fd88SAlan Cox #define HEADER_SIZE (sizeof(struct ti_i2c_desc) + \ 12212742fd88SAlan Cox sizeof(struct ti_i2c_firmware_rec)) 12221da177e4SLinus Torvalds __u8 *header; 12231da177e4SLinus Torvalds __u8 *vheader; 12241da177e4SLinus Torvalds 12251da177e4SLinus Torvalds header = kmalloc(HEADER_SIZE, GFP_KERNEL); 12261da177e4SLinus Torvalds if (!header) { 1227441b62c1SHarvey Harrison dev_err(dev, "%s - out of memory.\n", __func__); 12281da177e4SLinus Torvalds kfree(rom_desc); 12291da177e4SLinus Torvalds kfree(ti_manuf_desc); 12301da177e4SLinus Torvalds return -ENOMEM; 12311da177e4SLinus Torvalds } 12321da177e4SLinus Torvalds 12331da177e4SLinus Torvalds vheader = kmalloc(HEADER_SIZE, GFP_KERNEL); 12341da177e4SLinus Torvalds if (!vheader) { 1235441b62c1SHarvey Harrison dev_err(dev, "%s - out of memory.\n", __func__); 12361da177e4SLinus Torvalds kfree(header); 12371da177e4SLinus Torvalds kfree(rom_desc); 12381da177e4SLinus Torvalds kfree(ti_manuf_desc); 12391da177e4SLinus Torvalds return -ENOMEM; 12401da177e4SLinus Torvalds } 12411da177e4SLinus Torvalds 1242*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - Found Type BLANK FIRMWARE (Type F2) record\n", __func__); 12431da177e4SLinus Torvalds 12442742fd88SAlan Cox /* 12452742fd88SAlan Cox * In order to update the I2C firmware we must change 12462742fd88SAlan Cox * the type 2 record to type 0xF2. This will force the 12472742fd88SAlan Cox * UMP to come up in Boot Mode. Then while in boot 12482742fd88SAlan Cox * mode, the driver will download the latest firmware 12492742fd88SAlan Cox * (padded to 15.5k) into the UMP ram. Finally when the 12502742fd88SAlan Cox * device comes back up in download mode the driver 12512742fd88SAlan Cox * will cause the new firmware to be copied from the 12522742fd88SAlan Cox * UMP Ram to I2C and the firmware will update the 12532742fd88SAlan Cox * record type from 0xf2 to 0x02. 12542742fd88SAlan Cox */ 12552742fd88SAlan Cox status = build_i2c_fw_hdr(header, dev); 12561da177e4SLinus Torvalds if (status) { 12571da177e4SLinus Torvalds kfree(vheader); 12581da177e4SLinus Torvalds kfree(header); 12591da177e4SLinus Torvalds kfree(rom_desc); 12601da177e4SLinus Torvalds kfree(ti_manuf_desc); 1261fd6e5bbbSRoel Kluin return -EINVAL; 12621da177e4SLinus Torvalds } 12631da177e4SLinus Torvalds 12642742fd88SAlan Cox /* Update I2C with type 0xf2 record with correct 12652742fd88SAlan Cox size and checksum */ 12662742fd88SAlan Cox status = write_rom(serial, 12671da177e4SLinus Torvalds start_address, 12681da177e4SLinus Torvalds HEADER_SIZE, 12691da177e4SLinus Torvalds header); 12701da177e4SLinus Torvalds if (status) { 12711da177e4SLinus Torvalds kfree(vheader); 12721da177e4SLinus Torvalds kfree(header); 12731da177e4SLinus Torvalds kfree(rom_desc); 12741da177e4SLinus Torvalds kfree(ti_manuf_desc); 12759800eb33SRoel Kluin return -EINVAL; 12761da177e4SLinus Torvalds } 12771da177e4SLinus Torvalds 12782742fd88SAlan Cox /* verify the write -- must do this in order for 12792742fd88SAlan Cox write to complete before we do the hardware reset */ 12802742fd88SAlan Cox status = read_rom(serial, start_address, 12812742fd88SAlan Cox HEADER_SIZE, vheader); 12821da177e4SLinus Torvalds 12831da177e4SLinus Torvalds if (status) { 1284*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - can't read header back\n", __func__); 12851da177e4SLinus Torvalds kfree(vheader); 12861da177e4SLinus Torvalds kfree(header); 12871da177e4SLinus Torvalds kfree(rom_desc); 12881da177e4SLinus Torvalds kfree(ti_manuf_desc); 12891da177e4SLinus Torvalds return status; 12901da177e4SLinus Torvalds } 12911da177e4SLinus Torvalds if (memcmp(vheader, header, HEADER_SIZE)) { 1292*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - write download record failed\n", __func__); 12931da177e4SLinus Torvalds kfree(vheader); 12941da177e4SLinus Torvalds kfree(header); 12951da177e4SLinus Torvalds kfree(rom_desc); 12961da177e4SLinus Torvalds kfree(ti_manuf_desc); 12971e29709eSRoel Kluin return -EINVAL; 12981da177e4SLinus Torvalds } 12991da177e4SLinus Torvalds 13001da177e4SLinus Torvalds kfree(vheader); 13011da177e4SLinus Torvalds kfree(header); 13021da177e4SLinus Torvalds 1303*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - Start firmware update\n", __func__); 13041da177e4SLinus Torvalds 13052742fd88SAlan Cox /* Tell firmware to copy download image into I2C */ 13062742fd88SAlan Cox status = ti_vsend_sync(serial->serial->dev, 13072742fd88SAlan Cox UMPC_COPY_DNLD_TO_I2C, 0, 0, NULL, 0); 13081da177e4SLinus Torvalds 1309*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - Update complete 0x%x\n", __func__, status); 13101da177e4SLinus Torvalds if (status) { 13112742fd88SAlan Cox dev_err(dev, 13122742fd88SAlan Cox "%s - UMPC_COPY_DNLD_TO_I2C failed\n", 13132742fd88SAlan Cox __func__); 13141da177e4SLinus Torvalds kfree(rom_desc); 13151da177e4SLinus Torvalds kfree(ti_manuf_desc); 13161da177e4SLinus Torvalds return status; 13171da177e4SLinus Torvalds } 13181da177e4SLinus Torvalds } 13191da177e4SLinus Torvalds 13201da177e4SLinus Torvalds // The device is running the download code 13211da177e4SLinus Torvalds kfree(rom_desc); 13221da177e4SLinus Torvalds kfree(ti_manuf_desc); 13231da177e4SLinus Torvalds return 0; 13241da177e4SLinus Torvalds } 13251da177e4SLinus Torvalds 13261da177e4SLinus Torvalds /********************************************************************/ 13271da177e4SLinus Torvalds /* Boot Mode */ 13281da177e4SLinus Torvalds /********************************************************************/ 1329*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - RUNNING IN BOOT MODE\n", __func__); 13301da177e4SLinus Torvalds 13312742fd88SAlan Cox /* Configure the TI device so we can use the BULK pipes for download */ 13322742fd88SAlan Cox status = config_boot_dev(serial->serial->dev); 13331da177e4SLinus Torvalds if (status) 13341da177e4SLinus Torvalds return status; 13351da177e4SLinus Torvalds 13362742fd88SAlan Cox if (le16_to_cpu(serial->serial->dev->descriptor.idVendor) 13372742fd88SAlan Cox != USB_VENDOR_ID_ION) { 1338*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - VID = 0x%x\n", __func__, 13391da177e4SLinus Torvalds le16_to_cpu(serial->serial->dev->descriptor.idVendor)); 13401da177e4SLinus Torvalds serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II; 13412742fd88SAlan Cox goto stayinbootmode; 13421da177e4SLinus Torvalds } 13431da177e4SLinus Torvalds 13442742fd88SAlan Cox /* We have an ION device (I2c Must be programmed) 13452742fd88SAlan Cox Determine I2C image type */ 13462742fd88SAlan Cox if (i2c_type_bootmode(serial)) 13472742fd88SAlan Cox goto stayinbootmode; 13481da177e4SLinus Torvalds 13492742fd88SAlan Cox /* Check for ION Vendor ID and that the I2C is valid */ 13502742fd88SAlan Cox if (!check_i2c_image(serial)) { 13511da177e4SLinus Torvalds struct ti_i2c_image_header *header; 13521da177e4SLinus Torvalds int i; 13531da177e4SLinus Torvalds __u8 cs = 0; 13541da177e4SLinus Torvalds __u8 *buffer; 13551da177e4SLinus Torvalds int buffer_size; 1356d12b219aSJaswinder Singh int err; 1357d12b219aSJaswinder Singh const struct firmware *fw; 1358d12b219aSJaswinder Singh const char *fw_name = "edgeport/down3.bin"; 13591da177e4SLinus Torvalds 13601da177e4SLinus Torvalds /* Validate Hardware version number 13611da177e4SLinus Torvalds * Read Manufacturing Descriptor from TI Based Edgeport 13621da177e4SLinus Torvalds */ 13631da177e4SLinus Torvalds ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL); 13641da177e4SLinus Torvalds if (!ti_manuf_desc) { 1365441b62c1SHarvey Harrison dev_err(dev, "%s - out of memory.\n", __func__); 13661da177e4SLinus Torvalds return -ENOMEM; 13671da177e4SLinus Torvalds } 13682742fd88SAlan Cox status = get_manuf_info(serial, (__u8 *)ti_manuf_desc); 13691da177e4SLinus Torvalds if (status) { 13701da177e4SLinus Torvalds kfree(ti_manuf_desc); 13712742fd88SAlan Cox goto stayinbootmode; 13721da177e4SLinus Torvalds } 13731da177e4SLinus Torvalds 13742742fd88SAlan Cox /* Check for version 2 */ 13752742fd88SAlan Cox if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) { 1376*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n", 13772742fd88SAlan Cox __func__, ti_cpu_rev(ti_manuf_desc)); 13781da177e4SLinus Torvalds kfree(ti_manuf_desc); 13792742fd88SAlan Cox goto stayinbootmode; 13801da177e4SLinus Torvalds } 13811da177e4SLinus Torvalds 13821da177e4SLinus Torvalds kfree(ti_manuf_desc); 13831da177e4SLinus Torvalds 13841da177e4SLinus Torvalds /* 13852742fd88SAlan Cox * In order to update the I2C firmware we must change the type 13862742fd88SAlan Cox * 2 record to type 0xF2. This will force the UMP to come up 13872742fd88SAlan Cox * in Boot Mode. Then while in boot mode, the driver will 13882742fd88SAlan Cox * download the latest firmware (padded to 15.5k) into the 13892742fd88SAlan Cox * UMP ram. Finally when the device comes back up in download 13902742fd88SAlan Cox * mode the driver will cause the new firmware to be copied 13912742fd88SAlan Cox * from the UMP Ram to I2C and the firmware will update the 13922742fd88SAlan Cox * record type from 0xf2 to 0x02. 13932742fd88SAlan Cox * 13941da177e4SLinus Torvalds * Do we really have to copy the whole firmware image, 13951da177e4SLinus Torvalds * or could we do this in place! 13961da177e4SLinus Torvalds */ 13971da177e4SLinus Torvalds 13982742fd88SAlan Cox /* Allocate a 15.5k buffer + 3 byte header */ 13992742fd88SAlan Cox buffer_size = (((1024 * 16) - 512) + 14002742fd88SAlan Cox sizeof(struct ti_i2c_image_header)); 14011da177e4SLinus Torvalds buffer = kmalloc(buffer_size, GFP_KERNEL); 14021da177e4SLinus Torvalds if (!buffer) { 1403441b62c1SHarvey Harrison dev_err(dev, "%s - out of memory\n", __func__); 14041da177e4SLinus Torvalds return -ENOMEM; 14051da177e4SLinus Torvalds } 14061da177e4SLinus Torvalds 14072742fd88SAlan Cox /* Initialize the buffer to 0xff (pad the buffer) */ 14081da177e4SLinus Torvalds memset(buffer, 0xff, buffer_size); 14091da177e4SLinus Torvalds 1410d12b219aSJaswinder Singh err = request_firmware(&fw, fw_name, dev); 1411d12b219aSJaswinder Singh if (err) { 1412d12b219aSJaswinder Singh printk(KERN_ERR "Failed to load image \"%s\" err %d\n", 1413d12b219aSJaswinder Singh fw_name, err); 1414d12b219aSJaswinder Singh kfree(buffer); 1415d12b219aSJaswinder Singh return err; 1416d12b219aSJaswinder Singh } 1417d12b219aSJaswinder Singh memcpy(buffer, &fw->data[4], fw->size - 4); 1418d12b219aSJaswinder Singh release_firmware(fw); 14191da177e4SLinus Torvalds 14202742fd88SAlan Cox for (i = sizeof(struct ti_i2c_image_header); 14212742fd88SAlan Cox i < buffer_size; i++) { 14221da177e4SLinus Torvalds cs = (__u8)(cs + buffer[i]); 14231da177e4SLinus Torvalds } 14241da177e4SLinus Torvalds 14251da177e4SLinus Torvalds header = (struct ti_i2c_image_header *)buffer; 14261da177e4SLinus Torvalds 14272742fd88SAlan Cox /* update length and checksum after padding */ 14282742fd88SAlan Cox header->Length = cpu_to_le16((__u16)(buffer_size - 14292742fd88SAlan Cox sizeof(struct ti_i2c_image_header))); 14301da177e4SLinus Torvalds header->CheckSum = cs; 14311da177e4SLinus Torvalds 14322742fd88SAlan Cox /* Download the operational code */ 1433*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - Downloading operational code image (TI UMP)\n", __func__); 14342742fd88SAlan Cox status = download_code(serial, buffer, buffer_size); 14351da177e4SLinus Torvalds 14361da177e4SLinus Torvalds kfree(buffer); 14371da177e4SLinus Torvalds 14381da177e4SLinus Torvalds if (status) { 1439*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - Error downloading operational code image\n", __func__); 14401da177e4SLinus Torvalds return status; 14411da177e4SLinus Torvalds } 14421da177e4SLinus Torvalds 14432742fd88SAlan Cox /* Device will reboot */ 14441da177e4SLinus Torvalds serial->product_info.TiMode = TI_MODE_TRANSITIONING; 14451da177e4SLinus Torvalds 1446*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - Download successful -- Device rebooting...\n", __func__); 14471da177e4SLinus Torvalds 14481da177e4SLinus Torvalds /* return an error on purpose */ 14491da177e4SLinus Torvalds return -ENODEV; 14501da177e4SLinus Torvalds } 14511da177e4SLinus Torvalds 14522742fd88SAlan Cox stayinbootmode: 14532742fd88SAlan Cox /* Eprom is invalid or blank stay in boot mode */ 1454*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - STAYING IN BOOT MODE\n", __func__); 14551da177e4SLinus Torvalds serial->product_info.TiMode = TI_MODE_BOOT; 14561da177e4SLinus Torvalds 14571da177e4SLinus Torvalds return 0; 14581da177e4SLinus Torvalds } 14591da177e4SLinus Torvalds 14601da177e4SLinus Torvalds 14612742fd88SAlan Cox static int ti_do_config(struct edgeport_port *port, int feature, int on) 14621da177e4SLinus Torvalds { 14631da177e4SLinus Torvalds int port_number = port->port->number - port->port->serial->minor; 14642742fd88SAlan Cox on = !!on; /* 1 or 0 not bitmask */ 14652742fd88SAlan Cox return send_cmd(port->port->serial->dev, 14662742fd88SAlan Cox feature, (__u8)(UMPM_UART1_PORT + port_number), 14672742fd88SAlan Cox on, NULL, 0); 14681da177e4SLinus Torvalds } 14691da177e4SLinus Torvalds 14701da177e4SLinus Torvalds 14712742fd88SAlan Cox static int restore_mcr(struct edgeport_port *port, __u8 mcr) 14721da177e4SLinus Torvalds { 14731da177e4SLinus Torvalds int status = 0; 14741da177e4SLinus Torvalds 1475*67e6da70SGreg Kroah-Hartman dev_dbg(&port->port->dev, "%s - %x\n", __func__, mcr); 14761da177e4SLinus Torvalds 14772742fd88SAlan Cox status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR); 14781da177e4SLinus Torvalds if (status) 14791da177e4SLinus Torvalds return status; 14802742fd88SAlan Cox status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS); 14811da177e4SLinus Torvalds if (status) 14821da177e4SLinus Torvalds return status; 14832742fd88SAlan Cox return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK); 14841da177e4SLinus Torvalds } 14851da177e4SLinus Torvalds 14861da177e4SLinus Torvalds /* Convert TI LSR to standard UART flags */ 14872742fd88SAlan Cox static __u8 map_line_status(__u8 ti_lsr) 14881da177e4SLinus Torvalds { 14891da177e4SLinus Torvalds __u8 lsr = 0; 14901da177e4SLinus Torvalds 14911da177e4SLinus Torvalds #define MAP_FLAG(flagUmp, flagUart) \ 14921da177e4SLinus Torvalds if (ti_lsr & flagUmp) \ 14931da177e4SLinus Torvalds lsr |= flagUart; 14941da177e4SLinus Torvalds 14951da177e4SLinus Torvalds MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR) /* overrun */ 14961da177e4SLinus Torvalds MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */ 14971da177e4SLinus Torvalds MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */ 14981da177e4SLinus Torvalds MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */ 14992742fd88SAlan Cox MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* rx data available */ 15002742fd88SAlan Cox MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* tx hold reg empty */ 15011da177e4SLinus Torvalds 15021da177e4SLinus Torvalds #undef MAP_FLAG 15031da177e4SLinus Torvalds 15041da177e4SLinus Torvalds return lsr; 15051da177e4SLinus Torvalds } 15061da177e4SLinus Torvalds 15071da177e4SLinus Torvalds static void handle_new_msr(struct edgeport_port *edge_port, __u8 msr) 15081da177e4SLinus Torvalds { 15091da177e4SLinus Torvalds struct async_icount *icount; 15101da177e4SLinus Torvalds struct tty_struct *tty; 15111da177e4SLinus Torvalds 1512*67e6da70SGreg Kroah-Hartman dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, msr); 15131da177e4SLinus Torvalds 15142742fd88SAlan Cox if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR | 15152742fd88SAlan Cox EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) { 15161da177e4SLinus Torvalds icount = &edge_port->icount; 15171da177e4SLinus Torvalds 15181da177e4SLinus Torvalds /* update input line counters */ 15191da177e4SLinus Torvalds if (msr & EDGEPORT_MSR_DELTA_CTS) 15201da177e4SLinus Torvalds icount->cts++; 15211da177e4SLinus Torvalds if (msr & EDGEPORT_MSR_DELTA_DSR) 15221da177e4SLinus Torvalds icount->dsr++; 15231da177e4SLinus Torvalds if (msr & EDGEPORT_MSR_DELTA_CD) 15241da177e4SLinus Torvalds icount->dcd++; 15251da177e4SLinus Torvalds if (msr & EDGEPORT_MSR_DELTA_RI) 15261da177e4SLinus Torvalds icount->rng++; 15271da177e4SLinus Torvalds wake_up_interruptible(&edge_port->delta_msr_wait); 15281da177e4SLinus Torvalds } 15291da177e4SLinus Torvalds 15301da177e4SLinus Torvalds /* Save the new modem status */ 15311da177e4SLinus Torvalds edge_port->shadow_msr = msr & 0xf0; 15321da177e4SLinus Torvalds 15334a90f09bSAlan Cox tty = tty_port_tty_get(&edge_port->port->port); 15341da177e4SLinus Torvalds /* handle CTS flow control */ 15351da177e4SLinus Torvalds if (tty && C_CRTSCTS(tty)) { 15361da177e4SLinus Torvalds if (msr & EDGEPORT_MSR_CTS) { 15371da177e4SLinus Torvalds tty->hw_stopped = 0; 15381da177e4SLinus Torvalds tty_wakeup(tty); 15391da177e4SLinus Torvalds } else { 15401da177e4SLinus Torvalds tty->hw_stopped = 1; 15411da177e4SLinus Torvalds } 15421da177e4SLinus Torvalds } 15434a90f09bSAlan Cox tty_kref_put(tty); 15441da177e4SLinus Torvalds } 15451da177e4SLinus Torvalds 15462742fd88SAlan Cox static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data, 15472742fd88SAlan Cox __u8 lsr, __u8 data) 15481da177e4SLinus Torvalds { 15491da177e4SLinus Torvalds struct async_icount *icount; 15502742fd88SAlan Cox __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR | 15512742fd88SAlan Cox LSR_FRM_ERR | LSR_BREAK)); 15524a90f09bSAlan Cox struct tty_struct *tty; 15531da177e4SLinus Torvalds 1554*67e6da70SGreg Kroah-Hartman dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, new_lsr); 15551da177e4SLinus Torvalds 15561da177e4SLinus Torvalds edge_port->shadow_lsr = lsr; 15571da177e4SLinus Torvalds 15582742fd88SAlan Cox if (new_lsr & LSR_BREAK) 15591da177e4SLinus Torvalds /* 15601da177e4SLinus Torvalds * Parity and Framing errors only count if they 15611da177e4SLinus Torvalds * occur exclusive of a break being received. 15621da177e4SLinus Torvalds */ 15631da177e4SLinus Torvalds new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK); 15641da177e4SLinus Torvalds 15651da177e4SLinus Torvalds /* Place LSR data byte into Rx buffer */ 15664a90f09bSAlan Cox if (lsr_data) { 15674a90f09bSAlan Cox tty = tty_port_tty_get(&edge_port->port->port); 15684a90f09bSAlan Cox if (tty) { 15694a90f09bSAlan Cox edge_tty_recv(&edge_port->port->dev, tty, &data, 1); 15704a90f09bSAlan Cox tty_kref_put(tty); 15714a90f09bSAlan Cox } 15724a90f09bSAlan Cox } 15731da177e4SLinus Torvalds 15741da177e4SLinus Torvalds /* update input line counters */ 15751da177e4SLinus Torvalds icount = &edge_port->icount; 15761da177e4SLinus Torvalds if (new_lsr & LSR_BREAK) 15771da177e4SLinus Torvalds icount->brk++; 15781da177e4SLinus Torvalds if (new_lsr & LSR_OVER_ERR) 15791da177e4SLinus Torvalds icount->overrun++; 15801da177e4SLinus Torvalds if (new_lsr & LSR_PAR_ERR) 15811da177e4SLinus Torvalds icount->parity++; 15821da177e4SLinus Torvalds if (new_lsr & LSR_FRM_ERR) 15831da177e4SLinus Torvalds icount->frame++; 15841da177e4SLinus Torvalds } 15851da177e4SLinus Torvalds 15861da177e4SLinus Torvalds 15877d12e780SDavid Howells static void edge_interrupt_callback(struct urb *urb) 15881da177e4SLinus Torvalds { 1589cdc97792SMing Lei struct edgeport_serial *edge_serial = urb->context; 15901da177e4SLinus Torvalds struct usb_serial_port *port; 15911da177e4SLinus Torvalds struct edgeport_port *edge_port; 1592*67e6da70SGreg Kroah-Hartman struct device *dev; 15931da177e4SLinus Torvalds unsigned char *data = urb->transfer_buffer; 15941da177e4SLinus Torvalds int length = urb->actual_length; 15951da177e4SLinus Torvalds int port_number; 15961da177e4SLinus Torvalds int function; 1597ee337c21SGreg Kroah-Hartman int retval; 15981da177e4SLinus Torvalds __u8 lsr; 15991da177e4SLinus Torvalds __u8 msr; 1600ee337c21SGreg Kroah-Hartman int status = urb->status; 16011da177e4SLinus Torvalds 1602ee337c21SGreg Kroah-Hartman switch (status) { 16031da177e4SLinus Torvalds case 0: 16041da177e4SLinus Torvalds /* success */ 16051da177e4SLinus Torvalds break; 16061da177e4SLinus Torvalds case -ECONNRESET: 16071da177e4SLinus Torvalds case -ENOENT: 16081da177e4SLinus Torvalds case -ESHUTDOWN: 16091da177e4SLinus Torvalds /* this urb is terminated, clean up */ 1610*67e6da70SGreg Kroah-Hartman dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", 1611441b62c1SHarvey Harrison __func__, status); 16121da177e4SLinus Torvalds return; 16131da177e4SLinus Torvalds default: 1614ee337c21SGreg Kroah-Hartman dev_err(&urb->dev->dev, "%s - nonzero urb status received: " 1615441b62c1SHarvey Harrison "%d\n", __func__, status); 16161da177e4SLinus Torvalds goto exit; 16171da177e4SLinus Torvalds } 16181da177e4SLinus Torvalds 16191da177e4SLinus Torvalds if (!length) { 1620*67e6da70SGreg Kroah-Hartman dev_dbg(&urb->dev->dev, "%s - no data in urb\n", __func__); 16211da177e4SLinus Torvalds goto exit; 16221da177e4SLinus Torvalds } 16231da177e4SLinus Torvalds 1624*67e6da70SGreg Kroah-Hartman dev = &edge_serial->serial->dev->dev; 1625*67e6da70SGreg Kroah-Hartman usb_serial_debug_data(debug, dev, __func__, length, data); 16261da177e4SLinus Torvalds 16271da177e4SLinus Torvalds if (length != 2) { 1628*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - expecting packet of size 2, got %d\n", __func__, length); 16291da177e4SLinus Torvalds goto exit; 16301da177e4SLinus Torvalds } 16311da177e4SLinus Torvalds 16321da177e4SLinus Torvalds port_number = TIUMP_GET_PORT_FROM_CODE(data[0]); 16331da177e4SLinus Torvalds function = TIUMP_GET_FUNC_FROM_CODE(data[0]); 1634*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - port_number %d, function %d, info 0x%x\n", __func__, 1635*67e6da70SGreg Kroah-Hartman port_number, function, data[1]); 16361da177e4SLinus Torvalds port = edge_serial->serial->port[port_number]; 16371da177e4SLinus Torvalds edge_port = usb_get_serial_port_data(port); 16381da177e4SLinus Torvalds if (!edge_port) { 1639*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - edge_port not found\n", __func__); 16401da177e4SLinus Torvalds return; 16411da177e4SLinus Torvalds } 16421da177e4SLinus Torvalds switch (function) { 16431da177e4SLinus Torvalds case TIUMP_INTERRUPT_CODE_LSR: 16442742fd88SAlan Cox lsr = map_line_status(data[1]); 16451da177e4SLinus Torvalds if (lsr & UMP_UART_LSR_DATA_MASK) { 16462742fd88SAlan Cox /* Save the LSR event for bulk read 16472742fd88SAlan Cox completion routine */ 1648*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - LSR Event Port %u LSR Status = %02x\n", 1649441b62c1SHarvey Harrison __func__, port_number, lsr); 16501da177e4SLinus Torvalds edge_port->lsr_event = 1; 16511da177e4SLinus Torvalds edge_port->lsr_mask = lsr; 16521da177e4SLinus Torvalds } else { 1653*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - ===== Port %d LSR Status = %02x ======\n", 1654441b62c1SHarvey Harrison __func__, port_number, lsr); 16551da177e4SLinus Torvalds handle_new_lsr(edge_port, 0, lsr, 0); 16561da177e4SLinus Torvalds } 16571da177e4SLinus Torvalds break; 16581da177e4SLinus Torvalds 16592742fd88SAlan Cox case TIUMP_INTERRUPT_CODE_MSR: /* MSR */ 16601da177e4SLinus Torvalds /* Copy MSR from UMP */ 16611da177e4SLinus Torvalds msr = data[1]; 1662*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - ===== Port %u MSR Status = %02x ======\n", 1663441b62c1SHarvey Harrison __func__, port_number, msr); 16641da177e4SLinus Torvalds handle_new_msr(edge_port, msr); 16651da177e4SLinus Torvalds break; 16661da177e4SLinus Torvalds 16671da177e4SLinus Torvalds default: 16682742fd88SAlan Cox dev_err(&urb->dev->dev, 16692742fd88SAlan Cox "%s - Unknown Interrupt code from UMP %x\n", 1670441b62c1SHarvey Harrison __func__, data[1]); 16711da177e4SLinus Torvalds break; 16721da177e4SLinus Torvalds 16731da177e4SLinus Torvalds } 16741da177e4SLinus Torvalds 16751da177e4SLinus Torvalds exit: 1676ee337c21SGreg Kroah-Hartman retval = usb_submit_urb(urb, GFP_ATOMIC); 1677ee337c21SGreg Kroah-Hartman if (retval) 16782742fd88SAlan Cox dev_err(&urb->dev->dev, 16792742fd88SAlan Cox "%s - usb_submit_urb failed with result %d\n", 1680441b62c1SHarvey Harrison __func__, retval); 16811da177e4SLinus Torvalds } 16821da177e4SLinus Torvalds 16837d12e780SDavid Howells static void edge_bulk_in_callback(struct urb *urb) 16841da177e4SLinus Torvalds { 1685cdc97792SMing Lei struct edgeport_port *edge_port = urb->context; 1686*67e6da70SGreg Kroah-Hartman struct device *dev = &edge_port->port->dev; 16871da177e4SLinus Torvalds unsigned char *data = urb->transfer_buffer; 16881da177e4SLinus Torvalds struct tty_struct *tty; 1689ee337c21SGreg Kroah-Hartman int retval = 0; 16901da177e4SLinus Torvalds int port_number; 1691ee337c21SGreg Kroah-Hartman int status = urb->status; 16921da177e4SLinus Torvalds 1693ee337c21SGreg Kroah-Hartman switch (status) { 16941da177e4SLinus Torvalds case 0: 16951da177e4SLinus Torvalds /* success */ 16961da177e4SLinus Torvalds break; 16971da177e4SLinus Torvalds case -ECONNRESET: 16981da177e4SLinus Torvalds case -ENOENT: 16991da177e4SLinus Torvalds case -ESHUTDOWN: 17001da177e4SLinus Torvalds /* this urb is terminated, clean up */ 1701*67e6da70SGreg Kroah-Hartman dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status); 17021da177e4SLinus Torvalds return; 17031da177e4SLinus Torvalds default: 1704*67e6da70SGreg Kroah-Hartman dev_err(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n", __func__, status); 17051da177e4SLinus Torvalds } 17061da177e4SLinus Torvalds 1707ee337c21SGreg Kroah-Hartman if (status == -EPIPE) 17081da177e4SLinus Torvalds goto exit; 17091da177e4SLinus Torvalds 1710ee337c21SGreg Kroah-Hartman if (status) { 1711441b62c1SHarvey Harrison dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__); 17121da177e4SLinus Torvalds return; 17131da177e4SLinus Torvalds } 17141da177e4SLinus Torvalds 17151da177e4SLinus Torvalds port_number = edge_port->port->number - edge_port->port->serial->minor; 17161da177e4SLinus Torvalds 17171da177e4SLinus Torvalds if (edge_port->lsr_event) { 17181da177e4SLinus Torvalds edge_port->lsr_event = 0; 1719*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s ===== Port %u LSR Status = %02x, Data = %02x ======\n", 1720441b62c1SHarvey Harrison __func__, port_number, edge_port->lsr_mask, *data); 17211da177e4SLinus Torvalds handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data); 17221da177e4SLinus Torvalds /* Adjust buffer length/pointer */ 17231da177e4SLinus Torvalds --urb->actual_length; 17241da177e4SLinus Torvalds ++data; 17251da177e4SLinus Torvalds } 17261da177e4SLinus Torvalds 17274a90f09bSAlan Cox tty = tty_port_tty_get(&edge_port->port->port); 17281da177e4SLinus Torvalds if (tty && urb->actual_length) { 1729*67e6da70SGreg Kroah-Hartman usb_serial_debug_data(debug, dev, __func__, urb->actual_length, data); 17302742fd88SAlan Cox if (edge_port->close_pending) 1731*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - close pending, dropping data on the floor\n", 17322742fd88SAlan Cox __func__); 17332742fd88SAlan Cox else 1734*67e6da70SGreg Kroah-Hartman edge_tty_recv(dev, tty, data, urb->actual_length); 17351da177e4SLinus Torvalds edge_port->icount.rx += urb->actual_length; 17361da177e4SLinus Torvalds } 17374a90f09bSAlan Cox tty_kref_put(tty); 17381da177e4SLinus Torvalds 17391da177e4SLinus Torvalds exit: 17401da177e4SLinus Torvalds /* continue read unless stopped */ 17411da177e4SLinus Torvalds spin_lock(&edge_port->ep_lock); 17425833041fSJohan Hovold if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING) 1743ee337c21SGreg Kroah-Hartman retval = usb_submit_urb(urb, GFP_ATOMIC); 17445833041fSJohan Hovold else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING) 17451da177e4SLinus Torvalds edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED; 17465833041fSJohan Hovold 17471da177e4SLinus Torvalds spin_unlock(&edge_port->ep_lock); 1748ee337c21SGreg Kroah-Hartman if (retval) 1749*67e6da70SGreg Kroah-Hartman dev_err(dev, "%s - usb_submit_urb failed with result %d\n", __func__, retval); 17501da177e4SLinus Torvalds } 17511da177e4SLinus Torvalds 17522742fd88SAlan Cox static void edge_tty_recv(struct device *dev, struct tty_struct *tty, 17532742fd88SAlan Cox unsigned char *data, int length) 17541da177e4SLinus Torvalds { 17552742fd88SAlan Cox int queued; 17561da177e4SLinus Torvalds 17572742fd88SAlan Cox queued = tty_insert_flip_string(tty, data, length); 17582742fd88SAlan Cox if (queued < length) 17591da177e4SLinus Torvalds dev_err(dev, "%s - dropping data, %d bytes lost\n", 17602742fd88SAlan Cox __func__, length - queued); 17611da177e4SLinus Torvalds tty_flip_buffer_push(tty); 17621da177e4SLinus Torvalds } 17631da177e4SLinus Torvalds 17647d12e780SDavid Howells static void edge_bulk_out_callback(struct urb *urb) 17651da177e4SLinus Torvalds { 1766cdc97792SMing Lei struct usb_serial_port *port = urb->context; 17671da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 1768ee337c21SGreg Kroah-Hartman int status = urb->status; 17694a90f09bSAlan Cox struct tty_struct *tty; 17701da177e4SLinus Torvalds 17711da177e4SLinus Torvalds edge_port->ep_write_urb_in_use = 0; 17721da177e4SLinus Torvalds 1773ee337c21SGreg Kroah-Hartman switch (status) { 17741da177e4SLinus Torvalds case 0: 17751da177e4SLinus Torvalds /* success */ 17761da177e4SLinus Torvalds break; 17771da177e4SLinus Torvalds case -ECONNRESET: 17781da177e4SLinus Torvalds case -ENOENT: 17791da177e4SLinus Torvalds case -ESHUTDOWN: 17801da177e4SLinus Torvalds /* this urb is terminated, clean up */ 1781*67e6da70SGreg Kroah-Hartman dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", 1782441b62c1SHarvey Harrison __func__, status); 17831da177e4SLinus Torvalds return; 17841da177e4SLinus Torvalds default: 178522a416c4SJohan Hovold dev_err_console(port, "%s - nonzero write bulk status " 1786441b62c1SHarvey Harrison "received: %d\n", __func__, status); 17871da177e4SLinus Torvalds } 17881da177e4SLinus Torvalds 17891da177e4SLinus Torvalds /* send any buffered data */ 17904a90f09bSAlan Cox tty = tty_port_tty_get(&port->port); 17914a90f09bSAlan Cox edge_send(tty); 17924a90f09bSAlan Cox tty_kref_put(tty); 17931da177e4SLinus Torvalds } 17941da177e4SLinus Torvalds 1795a509a7e4SAlan Cox static int edge_open(struct tty_struct *tty, struct usb_serial_port *port) 17961da177e4SLinus Torvalds { 17971da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 17981da177e4SLinus Torvalds struct edgeport_serial *edge_serial; 17991da177e4SLinus Torvalds struct usb_device *dev; 18001da177e4SLinus Torvalds struct urb *urb; 18011da177e4SLinus Torvalds int port_number; 18021da177e4SLinus Torvalds int status; 18031da177e4SLinus Torvalds u16 open_settings; 18041da177e4SLinus Torvalds u8 transaction_timeout; 18051da177e4SLinus Torvalds 18061da177e4SLinus Torvalds if (edge_port == NULL) 18071da177e4SLinus Torvalds return -ENODEV; 18081da177e4SLinus Torvalds 18091da177e4SLinus Torvalds port_number = port->number - port->serial->minor; 18101da177e4SLinus Torvalds switch (port_number) { 18111da177e4SLinus Torvalds case 0: 18121da177e4SLinus Torvalds edge_port->uart_base = UMPMEM_BASE_UART1; 18131da177e4SLinus Torvalds edge_port->dma_address = UMPD_OEDB1_ADDRESS; 18141da177e4SLinus Torvalds break; 18151da177e4SLinus Torvalds case 1: 18161da177e4SLinus Torvalds edge_port->uart_base = UMPMEM_BASE_UART2; 18171da177e4SLinus Torvalds edge_port->dma_address = UMPD_OEDB2_ADDRESS; 18181da177e4SLinus Torvalds break; 18191da177e4SLinus Torvalds default: 18201da177e4SLinus Torvalds dev_err(&port->dev, "Unknown port number!!!\n"); 18211da177e4SLinus Torvalds return -ENODEV; 18221da177e4SLinus Torvalds } 18231da177e4SLinus Torvalds 1824*67e6da70SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - port_number = %d, uart_base = %04x, dma_address = %04x\n", 1825*67e6da70SGreg Kroah-Hartman __func__, port_number, edge_port->uart_base, edge_port->dma_address); 18261da177e4SLinus Torvalds 18271da177e4SLinus Torvalds dev = port->serial->dev; 18281da177e4SLinus Torvalds 18291da177e4SLinus Torvalds memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount)); 18301da177e4SLinus Torvalds init_waitqueue_head(&edge_port->delta_msr_wait); 18311da177e4SLinus Torvalds 18321da177e4SLinus Torvalds /* turn off loopback */ 18332742fd88SAlan Cox status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0); 18341da177e4SLinus Torvalds if (status) { 18352742fd88SAlan Cox dev_err(&port->dev, 18362742fd88SAlan Cox "%s - cannot send clear loopback command, %d\n", 1837441b62c1SHarvey Harrison __func__, status); 18381da177e4SLinus Torvalds return status; 18391da177e4SLinus Torvalds } 18401da177e4SLinus Torvalds 18411da177e4SLinus Torvalds /* set up the port settings */ 184295da310eSAlan Cox if (tty) 18434a90f09bSAlan Cox edge_set_termios(tty, port, tty->termios); 18441da177e4SLinus Torvalds 18451da177e4SLinus Torvalds /* open up the port */ 18461da177e4SLinus Torvalds 18471da177e4SLinus Torvalds /* milliseconds to timeout for DMA transfer */ 18481da177e4SLinus Torvalds transaction_timeout = 2; 18491da177e4SLinus Torvalds 18502742fd88SAlan Cox edge_port->ump_read_timeout = 18512742fd88SAlan Cox max(20, ((transaction_timeout * 3) / 2)); 18521da177e4SLinus Torvalds 18532742fd88SAlan Cox /* milliseconds to timeout for DMA transfer */ 18541da177e4SLinus Torvalds open_settings = (u8)(UMP_DMA_MODE_CONTINOUS | 18551da177e4SLinus Torvalds UMP_PIPE_TRANS_TIMEOUT_ENA | 18561da177e4SLinus Torvalds (transaction_timeout << 2)); 18571da177e4SLinus Torvalds 1858*67e6da70SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - Sending UMPC_OPEN_PORT\n", __func__); 18591da177e4SLinus Torvalds 18601da177e4SLinus Torvalds /* Tell TI to open and start the port */ 18612742fd88SAlan Cox status = send_cmd(dev, UMPC_OPEN_PORT, 18622742fd88SAlan Cox (u8)(UMPM_UART1_PORT + port_number), open_settings, NULL, 0); 18631da177e4SLinus Torvalds if (status) { 18642742fd88SAlan Cox dev_err(&port->dev, "%s - cannot send open command, %d\n", 18652742fd88SAlan Cox __func__, status); 18661da177e4SLinus Torvalds return status; 18671da177e4SLinus Torvalds } 18681da177e4SLinus Torvalds 18691da177e4SLinus Torvalds /* Start the DMA? */ 18702742fd88SAlan Cox status = send_cmd(dev, UMPC_START_PORT, 18712742fd88SAlan Cox (u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0); 18721da177e4SLinus Torvalds if (status) { 18732742fd88SAlan Cox dev_err(&port->dev, "%s - cannot send start DMA command, %d\n", 18742742fd88SAlan Cox __func__, status); 18751da177e4SLinus Torvalds return status; 18761da177e4SLinus Torvalds } 18771da177e4SLinus Torvalds 18781da177e4SLinus Torvalds /* Clear TX and RX buffers in UMP */ 18792742fd88SAlan Cox status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN); 18801da177e4SLinus Torvalds if (status) { 18812742fd88SAlan Cox dev_err(&port->dev, 18822742fd88SAlan Cox "%s - cannot send clear buffers command, %d\n", 18832742fd88SAlan Cox __func__, status); 18841da177e4SLinus Torvalds return status; 18851da177e4SLinus Torvalds } 18861da177e4SLinus Torvalds 18871da177e4SLinus Torvalds /* Read Initial MSR */ 18882742fd88SAlan Cox status = ti_vread_sync(dev, UMPC_READ_MSR, 0, 18892742fd88SAlan Cox (__u16)(UMPM_UART1_PORT + port_number), 18902742fd88SAlan Cox &edge_port->shadow_msr, 1); 18911da177e4SLinus Torvalds if (status) { 18922742fd88SAlan Cox dev_err(&port->dev, "%s - cannot send read MSR command, %d\n", 18932742fd88SAlan Cox __func__, status); 18941da177e4SLinus Torvalds return status; 18951da177e4SLinus Torvalds } 18961da177e4SLinus Torvalds 1897*67e6da70SGreg Kroah-Hartman dev_dbg(&port->dev, "ShadowMSR 0x%X\n", edge_port->shadow_msr); 18981da177e4SLinus Torvalds 18991da177e4SLinus Torvalds /* Set Initial MCR */ 19001da177e4SLinus Torvalds edge_port->shadow_mcr = MCR_RTS | MCR_DTR; 1901*67e6da70SGreg Kroah-Hartman dev_dbg(&port->dev, "ShadowMCR 0x%X\n", edge_port->shadow_mcr); 19021da177e4SLinus Torvalds 19031da177e4SLinus Torvalds edge_serial = edge_port->edge_serial; 1904241ca64fSMatthias Kaehlcke if (mutex_lock_interruptible(&edge_serial->es_lock)) 19051da177e4SLinus Torvalds return -ERESTARTSYS; 19061da177e4SLinus Torvalds if (edge_serial->num_ports_open == 0) { 19072742fd88SAlan Cox /* we are the first port to open, post the interrupt urb */ 19081da177e4SLinus Torvalds urb = edge_serial->serial->port[0]->interrupt_in_urb; 19091da177e4SLinus Torvalds if (!urb) { 19102742fd88SAlan Cox dev_err(&port->dev, 19112742fd88SAlan Cox "%s - no interrupt urb present, exiting\n", 19122742fd88SAlan Cox __func__); 19131da177e4SLinus Torvalds status = -EINVAL; 1914241ca64fSMatthias Kaehlcke goto release_es_lock; 19151da177e4SLinus Torvalds } 19161da177e4SLinus Torvalds urb->context = edge_serial; 19171da177e4SLinus Torvalds status = usb_submit_urb(urb, GFP_KERNEL); 19181da177e4SLinus Torvalds if (status) { 19192742fd88SAlan Cox dev_err(&port->dev, 19202742fd88SAlan Cox "%s - usb_submit_urb failed with value %d\n", 19212742fd88SAlan Cox __func__, status); 1922241ca64fSMatthias Kaehlcke goto release_es_lock; 19231da177e4SLinus Torvalds } 19241da177e4SLinus Torvalds } 19251da177e4SLinus Torvalds 19261da177e4SLinus Torvalds /* 19271da177e4SLinus Torvalds * reset the data toggle on the bulk endpoints to work around bug in 19281da177e4SLinus Torvalds * host controllers where things get out of sync some times 19291da177e4SLinus Torvalds */ 19301da177e4SLinus Torvalds usb_clear_halt(dev, port->write_urb->pipe); 19311da177e4SLinus Torvalds usb_clear_halt(dev, port->read_urb->pipe); 19321da177e4SLinus Torvalds 19331da177e4SLinus Torvalds /* start up our bulk read urb */ 19341da177e4SLinus Torvalds urb = port->read_urb; 19351da177e4SLinus Torvalds if (!urb) { 19362742fd88SAlan Cox dev_err(&port->dev, "%s - no read urb present, exiting\n", 19372742fd88SAlan Cox __func__); 19381da177e4SLinus Torvalds status = -EINVAL; 19391da177e4SLinus Torvalds goto unlink_int_urb; 19401da177e4SLinus Torvalds } 19411da177e4SLinus Torvalds edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING; 19421da177e4SLinus Torvalds urb->context = edge_port; 19431da177e4SLinus Torvalds status = usb_submit_urb(urb, GFP_KERNEL); 19441da177e4SLinus Torvalds if (status) { 19452742fd88SAlan Cox dev_err(&port->dev, 19462742fd88SAlan Cox "%s - read bulk usb_submit_urb failed with value %d\n", 19472742fd88SAlan Cox __func__, status); 19481da177e4SLinus Torvalds goto unlink_int_urb; 19491da177e4SLinus Torvalds } 19501da177e4SLinus Torvalds 19511da177e4SLinus Torvalds ++edge_serial->num_ports_open; 19521da177e4SLinus Torvalds 1953241ca64fSMatthias Kaehlcke goto release_es_lock; 19541da177e4SLinus Torvalds 19551da177e4SLinus Torvalds unlink_int_urb: 19561da177e4SLinus Torvalds if (edge_port->edge_serial->num_ports_open == 0) 19571da177e4SLinus Torvalds usb_kill_urb(port->serial->port[0]->interrupt_in_urb); 1958241ca64fSMatthias Kaehlcke release_es_lock: 1959241ca64fSMatthias Kaehlcke mutex_unlock(&edge_serial->es_lock); 19601da177e4SLinus Torvalds return status; 19611da177e4SLinus Torvalds } 19621da177e4SLinus Torvalds 1963335f8514SAlan Cox static void edge_close(struct usb_serial_port *port) 19641da177e4SLinus Torvalds { 19651da177e4SLinus Torvalds struct edgeport_serial *edge_serial; 19661da177e4SLinus Torvalds struct edgeport_port *edge_port; 1967af581059SJohan Hovold struct usb_serial *serial = port->serial; 19681da177e4SLinus Torvalds int port_number; 19691da177e4SLinus Torvalds 19701da177e4SLinus Torvalds edge_serial = usb_get_serial_data(port->serial); 19711da177e4SLinus Torvalds edge_port = usb_get_serial_port_data(port); 19722742fd88SAlan Cox if (edge_serial == NULL || edge_port == NULL) 19731da177e4SLinus Torvalds return; 19741da177e4SLinus Torvalds 19751da177e4SLinus Torvalds /* The bulkreadcompletion routine will check 19761da177e4SLinus Torvalds * this flag and dump add read data */ 19771da177e4SLinus Torvalds edge_port->close_pending = 1; 19781da177e4SLinus Torvalds 19791da177e4SLinus Torvalds /* chase the port close and flush */ 19802742fd88SAlan Cox chase_port(edge_port, (HZ * closing_wait) / 100, 1); 19811da177e4SLinus Torvalds 19821da177e4SLinus Torvalds usb_kill_urb(port->read_urb); 19831da177e4SLinus Torvalds usb_kill_urb(port->write_urb); 19841da177e4SLinus Torvalds edge_port->ep_write_urb_in_use = 0; 19851da177e4SLinus Torvalds 19861da177e4SLinus Torvalds /* assuming we can still talk to the device, 19871da177e4SLinus Torvalds * send a close port command to it */ 1988*67e6da70SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - send umpc_close_port\n", __func__); 19891da177e4SLinus Torvalds port_number = port->number - port->serial->minor; 1990af581059SJohan Hovold 1991af581059SJohan Hovold mutex_lock(&serial->disc_mutex); 1992af581059SJohan Hovold if (!serial->disconnected) { 1993af581059SJohan Hovold send_cmd(serial->dev, 19941da177e4SLinus Torvalds UMPC_CLOSE_PORT, 19951da177e4SLinus Torvalds (__u8)(UMPM_UART1_PORT + port_number), 19961da177e4SLinus Torvalds 0, 19971da177e4SLinus Torvalds NULL, 19981da177e4SLinus Torvalds 0); 1999af581059SJohan Hovold } 2000af581059SJohan Hovold mutex_unlock(&serial->disc_mutex); 2001af581059SJohan Hovold 2002241ca64fSMatthias Kaehlcke mutex_lock(&edge_serial->es_lock); 20031da177e4SLinus Torvalds --edge_port->edge_serial->num_ports_open; 20041da177e4SLinus Torvalds if (edge_port->edge_serial->num_ports_open <= 0) { 20051da177e4SLinus Torvalds /* last port is now closed, let's shut down our interrupt urb */ 20061da177e4SLinus Torvalds usb_kill_urb(port->serial->port[0]->interrupt_in_urb); 20071da177e4SLinus Torvalds edge_port->edge_serial->num_ports_open = 0; 20081da177e4SLinus Torvalds } 2009241ca64fSMatthias Kaehlcke mutex_unlock(&edge_serial->es_lock); 20101da177e4SLinus Torvalds edge_port->close_pending = 0; 20111da177e4SLinus Torvalds } 20121da177e4SLinus Torvalds 201395da310eSAlan Cox static int edge_write(struct tty_struct *tty, struct usb_serial_port *port, 201495da310eSAlan Cox const unsigned char *data, int count) 20151da177e4SLinus Torvalds { 20161da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 20171da177e4SLinus Torvalds 20181da177e4SLinus Torvalds if (count == 0) { 2019*67e6da70SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__); 20201da177e4SLinus Torvalds return 0; 20211da177e4SLinus Torvalds } 20221da177e4SLinus Torvalds 20231da177e4SLinus Torvalds if (edge_port == NULL) 20241da177e4SLinus Torvalds return -ENODEV; 20251da177e4SLinus Torvalds if (edge_port->close_pending == 1) 20261da177e4SLinus Torvalds return -ENODEV; 20271da177e4SLinus Torvalds 2028d733cec1SJohan Hovold count = kfifo_in_locked(&edge_port->write_fifo, data, count, 2029d733cec1SJohan Hovold &edge_port->ep_lock); 203095da310eSAlan Cox edge_send(tty); 20311da177e4SLinus Torvalds 20321da177e4SLinus Torvalds return count; 20331da177e4SLinus Torvalds } 20341da177e4SLinus Torvalds 203595da310eSAlan Cox static void edge_send(struct tty_struct *tty) 20361da177e4SLinus Torvalds { 203795da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 20381da177e4SLinus Torvalds int count, result; 20391da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 20401da177e4SLinus Torvalds unsigned long flags; 20411da177e4SLinus Torvalds 20421da177e4SLinus Torvalds spin_lock_irqsave(&edge_port->ep_lock, flags); 20431da177e4SLinus Torvalds 20441da177e4SLinus Torvalds if (edge_port->ep_write_urb_in_use) { 20451da177e4SLinus Torvalds spin_unlock_irqrestore(&edge_port->ep_lock, flags); 20461da177e4SLinus Torvalds return; 20471da177e4SLinus Torvalds } 20481da177e4SLinus Torvalds 2049d733cec1SJohan Hovold count = kfifo_out(&edge_port->write_fifo, 20501da177e4SLinus Torvalds port->write_urb->transfer_buffer, 20511da177e4SLinus Torvalds port->bulk_out_size); 20521da177e4SLinus Torvalds 20531da177e4SLinus Torvalds if (count == 0) { 20541da177e4SLinus Torvalds spin_unlock_irqrestore(&edge_port->ep_lock, flags); 20551da177e4SLinus Torvalds return; 20561da177e4SLinus Torvalds } 20571da177e4SLinus Torvalds 20581da177e4SLinus Torvalds edge_port->ep_write_urb_in_use = 1; 20591da177e4SLinus Torvalds 20601da177e4SLinus Torvalds spin_unlock_irqrestore(&edge_port->ep_lock, flags); 20611da177e4SLinus Torvalds 20622742fd88SAlan Cox usb_serial_debug_data(debug, &port->dev, __func__, count, 20632742fd88SAlan Cox port->write_urb->transfer_buffer); 20641da177e4SLinus Torvalds 20651da177e4SLinus Torvalds /* set up our urb */ 2066fd11961aSJohan Hovold port->write_urb->transfer_buffer_length = count; 20671da177e4SLinus Torvalds 20681da177e4SLinus Torvalds /* send the data out the bulk port */ 20691da177e4SLinus Torvalds result = usb_submit_urb(port->write_urb, GFP_ATOMIC); 20701da177e4SLinus Torvalds if (result) { 207122a416c4SJohan Hovold dev_err_console(port, 20722742fd88SAlan Cox "%s - failed submitting write urb, error %d\n", 20732742fd88SAlan Cox __func__, result); 20741da177e4SLinus Torvalds edge_port->ep_write_urb_in_use = 0; 20752742fd88SAlan Cox /* TODO: reschedule edge_send */ 20762742fd88SAlan Cox } else 20771da177e4SLinus Torvalds edge_port->icount.tx += count; 20781da177e4SLinus Torvalds 20791da177e4SLinus Torvalds /* wakeup any process waiting for writes to complete */ 20801da177e4SLinus Torvalds /* there is now more room in the buffer for new writes */ 20812742fd88SAlan Cox if (tty) 20821da177e4SLinus Torvalds tty_wakeup(tty); 20831da177e4SLinus Torvalds } 20841da177e4SLinus Torvalds 208595da310eSAlan Cox static int edge_write_room(struct tty_struct *tty) 20861da177e4SLinus Torvalds { 208795da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 20881da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 20891da177e4SLinus Torvalds int room = 0; 20901da177e4SLinus Torvalds unsigned long flags; 20911da177e4SLinus Torvalds 20921da177e4SLinus Torvalds if (edge_port == NULL) 20932742fd88SAlan Cox return 0; 20941da177e4SLinus Torvalds if (edge_port->close_pending == 1) 20952742fd88SAlan Cox return 0; 20961da177e4SLinus Torvalds 20971da177e4SLinus Torvalds spin_lock_irqsave(&edge_port->ep_lock, flags); 2098d733cec1SJohan Hovold room = kfifo_avail(&edge_port->write_fifo); 20991da177e4SLinus Torvalds spin_unlock_irqrestore(&edge_port->ep_lock, flags); 21001da177e4SLinus Torvalds 2101*67e6da70SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - returns %d\n", __func__, room); 21021da177e4SLinus Torvalds return room; 21031da177e4SLinus Torvalds } 21041da177e4SLinus Torvalds 210595da310eSAlan Cox static int edge_chars_in_buffer(struct tty_struct *tty) 21061da177e4SLinus Torvalds { 210795da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 21081da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 21091da177e4SLinus Torvalds int chars = 0; 21101da177e4SLinus Torvalds unsigned long flags; 21111da177e4SLinus Torvalds 21121da177e4SLinus Torvalds if (edge_port == NULL) 21132742fd88SAlan Cox return 0; 21141da177e4SLinus Torvalds if (edge_port->close_pending == 1) 21152742fd88SAlan Cox return 0; 21161da177e4SLinus Torvalds 21171da177e4SLinus Torvalds spin_lock_irqsave(&edge_port->ep_lock, flags); 2118d733cec1SJohan Hovold chars = kfifo_len(&edge_port->write_fifo); 21191da177e4SLinus Torvalds spin_unlock_irqrestore(&edge_port->ep_lock, flags); 21201da177e4SLinus Torvalds 2121*67e6da70SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars); 21221da177e4SLinus Torvalds return chars; 21231da177e4SLinus Torvalds } 21241da177e4SLinus Torvalds 212595da310eSAlan Cox static void edge_throttle(struct tty_struct *tty) 21261da177e4SLinus Torvalds { 212795da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 21281da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 21291da177e4SLinus Torvalds int status; 21301da177e4SLinus Torvalds 21311da177e4SLinus Torvalds if (edge_port == NULL) 21321da177e4SLinus Torvalds return; 21331da177e4SLinus Torvalds 21341da177e4SLinus Torvalds /* if we are implementing XON/XOFF, send the stop character */ 21351da177e4SLinus Torvalds if (I_IXOFF(tty)) { 21361da177e4SLinus Torvalds unsigned char stop_char = STOP_CHAR(tty); 213795da310eSAlan Cox status = edge_write(tty, port, &stop_char, 1); 213895da310eSAlan Cox if (status <= 0) { 213995da310eSAlan Cox dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status); 214095da310eSAlan Cox } 21411da177e4SLinus Torvalds } 21421da177e4SLinus Torvalds 21431da177e4SLinus Torvalds /* if we are implementing RTS/CTS, stop reads */ 21441da177e4SLinus Torvalds /* and the Edgeport will clear the RTS line */ 21451da177e4SLinus Torvalds if (C_CRTSCTS(tty)) 21461da177e4SLinus Torvalds stop_read(edge_port); 21471da177e4SLinus Torvalds 21481da177e4SLinus Torvalds } 21491da177e4SLinus Torvalds 215095da310eSAlan Cox static void edge_unthrottle(struct tty_struct *tty) 21511da177e4SLinus Torvalds { 215295da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 21531da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 21541da177e4SLinus Torvalds int status; 21551da177e4SLinus Torvalds 21561da177e4SLinus Torvalds if (edge_port == NULL) 21571da177e4SLinus Torvalds return; 21581da177e4SLinus Torvalds 21591da177e4SLinus Torvalds /* if we are implementing XON/XOFF, send the start character */ 21601da177e4SLinus Torvalds if (I_IXOFF(tty)) { 21611da177e4SLinus Torvalds unsigned char start_char = START_CHAR(tty); 216295da310eSAlan Cox status = edge_write(tty, port, &start_char, 1); 216395da310eSAlan Cox if (status <= 0) { 216495da310eSAlan Cox dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status); 216595da310eSAlan Cox } 21661da177e4SLinus Torvalds } 21671da177e4SLinus Torvalds /* if we are implementing RTS/CTS, restart reads */ 21681da177e4SLinus Torvalds /* are the Edgeport will assert the RTS line */ 21691da177e4SLinus Torvalds if (C_CRTSCTS(tty)) { 21701da177e4SLinus Torvalds status = restart_read(edge_port); 21711da177e4SLinus Torvalds if (status) 21722742fd88SAlan Cox dev_err(&port->dev, 21732742fd88SAlan Cox "%s - read bulk usb_submit_urb failed: %d\n", 21742742fd88SAlan Cox __func__, status); 21751da177e4SLinus Torvalds } 21761da177e4SLinus Torvalds 21771da177e4SLinus Torvalds } 21781da177e4SLinus Torvalds 21791da177e4SLinus Torvalds static void stop_read(struct edgeport_port *edge_port) 21801da177e4SLinus Torvalds { 21811da177e4SLinus Torvalds unsigned long flags; 21821da177e4SLinus Torvalds 21831da177e4SLinus Torvalds spin_lock_irqsave(&edge_port->ep_lock, flags); 21841da177e4SLinus Torvalds 21851da177e4SLinus Torvalds if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING) 21861da177e4SLinus Torvalds edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING; 21871da177e4SLinus Torvalds edge_port->shadow_mcr &= ~MCR_RTS; 21881da177e4SLinus Torvalds 21891da177e4SLinus Torvalds spin_unlock_irqrestore(&edge_port->ep_lock, flags); 21901da177e4SLinus Torvalds } 21911da177e4SLinus Torvalds 21921da177e4SLinus Torvalds static int restart_read(struct edgeport_port *edge_port) 21931da177e4SLinus Torvalds { 21941da177e4SLinus Torvalds struct urb *urb; 21951da177e4SLinus Torvalds int status = 0; 21961da177e4SLinus Torvalds unsigned long flags; 21971da177e4SLinus Torvalds 21981da177e4SLinus Torvalds spin_lock_irqsave(&edge_port->ep_lock, flags); 21991da177e4SLinus Torvalds 22001da177e4SLinus Torvalds if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) { 22011da177e4SLinus Torvalds urb = edge_port->port->read_urb; 2202efdff608SOliver Neukum status = usb_submit_urb(urb, GFP_ATOMIC); 22031da177e4SLinus Torvalds } 22041da177e4SLinus Torvalds edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING; 22051da177e4SLinus Torvalds edge_port->shadow_mcr |= MCR_RTS; 22061da177e4SLinus Torvalds 22071da177e4SLinus Torvalds spin_unlock_irqrestore(&edge_port->ep_lock, flags); 22081da177e4SLinus Torvalds 22091da177e4SLinus Torvalds return status; 22101da177e4SLinus Torvalds } 22111da177e4SLinus Torvalds 221295da310eSAlan Cox static void change_port_settings(struct tty_struct *tty, 221395da310eSAlan Cox struct edgeport_port *edge_port, struct ktermios *old_termios) 22141da177e4SLinus Torvalds { 2215*67e6da70SGreg Kroah-Hartman struct device *dev = &edge_port->port->dev; 22161da177e4SLinus Torvalds struct ump_uart_config *config; 22171da177e4SLinus Torvalds int baud; 22181da177e4SLinus Torvalds unsigned cflag; 22191da177e4SLinus Torvalds int status; 22202742fd88SAlan Cox int port_number = edge_port->port->number - 22212742fd88SAlan Cox edge_port->port->serial->minor; 22221da177e4SLinus Torvalds 2223*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - port %d\n", __func__, edge_port->port->number); 22241da177e4SLinus Torvalds 22251da177e4SLinus Torvalds config = kmalloc (sizeof (*config), GFP_KERNEL); 22261da177e4SLinus Torvalds if (!config) { 22272742fd88SAlan Cox *tty->termios = *old_termios; 2228*67e6da70SGreg Kroah-Hartman dev_err(dev, "%s - out of memory\n", __func__); 22291da177e4SLinus Torvalds return; 22301da177e4SLinus Torvalds } 22311da177e4SLinus Torvalds 22321da177e4SLinus Torvalds cflag = tty->termios->c_cflag; 22331da177e4SLinus Torvalds 22341da177e4SLinus Torvalds config->wFlags = 0; 22351da177e4SLinus Torvalds 22361da177e4SLinus Torvalds /* These flags must be set */ 22371da177e4SLinus Torvalds config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT; 22381da177e4SLinus Torvalds config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR; 22391da177e4SLinus Torvalds config->bUartMode = (__u8)(edge_port->bUartMode); 22401da177e4SLinus Torvalds 22411da177e4SLinus Torvalds switch (cflag & CSIZE) { 22421da177e4SLinus Torvalds case CS5: 22431da177e4SLinus Torvalds config->bDataBits = UMP_UART_CHAR5BITS; 2244*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - data bits = 5\n", __func__); 22451da177e4SLinus Torvalds break; 22461da177e4SLinus Torvalds case CS6: 22471da177e4SLinus Torvalds config->bDataBits = UMP_UART_CHAR6BITS; 2248*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - data bits = 6\n", __func__); 22491da177e4SLinus Torvalds break; 22501da177e4SLinus Torvalds case CS7: 22511da177e4SLinus Torvalds config->bDataBits = UMP_UART_CHAR7BITS; 2252*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - data bits = 7\n", __func__); 22531da177e4SLinus Torvalds break; 22541da177e4SLinus Torvalds default: 22551da177e4SLinus Torvalds case CS8: 22561da177e4SLinus Torvalds config->bDataBits = UMP_UART_CHAR8BITS; 2257*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - data bits = 8\n", __func__); 22581da177e4SLinus Torvalds break; 22591da177e4SLinus Torvalds } 22601da177e4SLinus Torvalds 22611da177e4SLinus Torvalds if (cflag & PARENB) { 22621da177e4SLinus Torvalds if (cflag & PARODD) { 22631da177e4SLinus Torvalds config->wFlags |= UMP_MASK_UART_FLAGS_PARITY; 22641da177e4SLinus Torvalds config->bParity = UMP_UART_ODDPARITY; 2265*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - parity = odd\n", __func__); 22661da177e4SLinus Torvalds } else { 22671da177e4SLinus Torvalds config->wFlags |= UMP_MASK_UART_FLAGS_PARITY; 22681da177e4SLinus Torvalds config->bParity = UMP_UART_EVENPARITY; 2269*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - parity = even\n", __func__); 22701da177e4SLinus Torvalds } 22711da177e4SLinus Torvalds } else { 22721da177e4SLinus Torvalds config->bParity = UMP_UART_NOPARITY; 2273*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - parity = none\n", __func__); 22741da177e4SLinus Torvalds } 22751da177e4SLinus Torvalds 22761da177e4SLinus Torvalds if (cflag & CSTOPB) { 22771da177e4SLinus Torvalds config->bStopBits = UMP_UART_STOPBIT2; 2278*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - stop bits = 2\n", __func__); 22791da177e4SLinus Torvalds } else { 22801da177e4SLinus Torvalds config->bStopBits = UMP_UART_STOPBIT1; 2281*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - stop bits = 1\n", __func__); 22821da177e4SLinus Torvalds } 22831da177e4SLinus Torvalds 22841da177e4SLinus Torvalds /* figure out the flow control settings */ 22851da177e4SLinus Torvalds if (cflag & CRTSCTS) { 22861da177e4SLinus Torvalds config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW; 22871da177e4SLinus Torvalds config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW; 2288*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__); 22891da177e4SLinus Torvalds } else { 2290*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__); 22911da177e4SLinus Torvalds tty->hw_stopped = 0; 22921da177e4SLinus Torvalds restart_read(edge_port); 22931da177e4SLinus Torvalds } 22941da177e4SLinus Torvalds 22952742fd88SAlan Cox /* if we are implementing XON/XOFF, set the start and stop 22962742fd88SAlan Cox character in the device */ 22971da177e4SLinus Torvalds config->cXon = START_CHAR(tty); 22981da177e4SLinus Torvalds config->cXoff = STOP_CHAR(tty); 22991da177e4SLinus Torvalds 23001da177e4SLinus Torvalds /* if we are implementing INBOUND XON/XOFF */ 23011da177e4SLinus Torvalds if (I_IXOFF(tty)) { 23021da177e4SLinus Torvalds config->wFlags |= UMP_MASK_UART_FLAGS_IN_X; 2303*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n", 2304441b62c1SHarvey Harrison __func__, config->cXon, config->cXoff); 23052742fd88SAlan Cox } else 2306*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__); 23071da177e4SLinus Torvalds 23081da177e4SLinus Torvalds /* if we are implementing OUTBOUND XON/XOFF */ 23091da177e4SLinus Torvalds if (I_IXON(tty)) { 23101da177e4SLinus Torvalds config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X; 2311*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n", 2312441b62c1SHarvey Harrison __func__, config->cXon, config->cXoff); 23132742fd88SAlan Cox } else 2314*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__); 23151da177e4SLinus Torvalds 2316d5f5bcd4SAlan Cox tty->termios->c_cflag &= ~CMSPAR; 2317d5f5bcd4SAlan Cox 23181da177e4SLinus Torvalds /* Round the baud rate */ 23191da177e4SLinus Torvalds baud = tty_get_baud_rate(tty); 23201da177e4SLinus Torvalds if (!baud) { 23211da177e4SLinus Torvalds /* pick a default, any default... */ 23221da177e4SLinus Torvalds baud = 9600; 2323d5f5bcd4SAlan Cox } else 2324d5f5bcd4SAlan Cox tty_encode_baud_rate(tty, baud, baud); 2325d5f5bcd4SAlan Cox 23261da177e4SLinus Torvalds edge_port->baud_rate = baud; 23271da177e4SLinus Torvalds config->wBaudRate = (__u16)((461550L + baud/2) / baud); 23281da177e4SLinus Torvalds 2329d5f5bcd4SAlan Cox /* FIXME: Recompute actual baud from divisor here */ 2330d5f5bcd4SAlan Cox 2331*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - baud rate = %d, wBaudRate = %d\n", __func__, baud, config->wBaudRate); 23321da177e4SLinus Torvalds 2333*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "wBaudRate: %d\n", (int)(461550L / config->wBaudRate)); 2334*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "wFlags: 0x%x\n", config->wFlags); 2335*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "bDataBits: %d\n", config->bDataBits); 2336*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "bParity: %d\n", config->bParity); 2337*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "bStopBits: %d\n", config->bStopBits); 2338*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "cXon: %d\n", config->cXon); 2339*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "cXoff: %d\n", config->cXoff); 2340*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "bUartMode: %d\n", config->bUartMode); 23411da177e4SLinus Torvalds 23421da177e4SLinus Torvalds /* move the word values into big endian mode */ 23431da177e4SLinus Torvalds cpu_to_be16s(&config->wFlags); 23441da177e4SLinus Torvalds cpu_to_be16s(&config->wBaudRate); 23451da177e4SLinus Torvalds 23462742fd88SAlan Cox status = send_cmd(edge_port->port->serial->dev, UMPC_SET_CONFIG, 23471da177e4SLinus Torvalds (__u8)(UMPM_UART1_PORT + port_number), 23482742fd88SAlan Cox 0, (__u8 *)config, sizeof(*config)); 23492742fd88SAlan Cox if (status) 2350*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s - error %d when trying to write config to device\n", 2351441b62c1SHarvey Harrison __func__, status); 23521da177e4SLinus Torvalds kfree(config); 23531da177e4SLinus Torvalds } 23541da177e4SLinus Torvalds 235595da310eSAlan Cox static void edge_set_termios(struct tty_struct *tty, 235695da310eSAlan Cox struct usb_serial_port *port, struct ktermios *old_termios) 23571da177e4SLinus Torvalds { 23581da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 235995da310eSAlan Cox unsigned int cflag; 236095da310eSAlan Cox 236195da310eSAlan Cox cflag = tty->termios->c_cflag; 23621da177e4SLinus Torvalds 2363*67e6da70SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__, 23641da177e4SLinus Torvalds tty->termios->c_cflag, tty->termios->c_iflag); 2365*67e6da70SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__, 23661da177e4SLinus Torvalds old_termios->c_cflag, old_termios->c_iflag); 2367*67e6da70SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number); 23681da177e4SLinus Torvalds 23691da177e4SLinus Torvalds if (edge_port == NULL) 23701da177e4SLinus Torvalds return; 23711da177e4SLinus Torvalds /* change the port settings to the new ones specified */ 237295da310eSAlan Cox change_port_settings(tty, edge_port, old_termios); 23731da177e4SLinus Torvalds } 23741da177e4SLinus Torvalds 237520b9d177SAlan Cox static int edge_tiocmset(struct tty_struct *tty, 23762742fd88SAlan Cox unsigned int set, unsigned int clear) 23771da177e4SLinus Torvalds { 237895da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 23791da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 23801da177e4SLinus Torvalds unsigned int mcr; 23813d71fe0bSAlan Cox unsigned long flags; 23821da177e4SLinus Torvalds 23833d71fe0bSAlan Cox spin_lock_irqsave(&edge_port->ep_lock, flags); 23841da177e4SLinus Torvalds mcr = edge_port->shadow_mcr; 23851da177e4SLinus Torvalds if (set & TIOCM_RTS) 23861da177e4SLinus Torvalds mcr |= MCR_RTS; 23871da177e4SLinus Torvalds if (set & TIOCM_DTR) 23881da177e4SLinus Torvalds mcr |= MCR_DTR; 23891da177e4SLinus Torvalds if (set & TIOCM_LOOP) 23901da177e4SLinus Torvalds mcr |= MCR_LOOPBACK; 23911da177e4SLinus Torvalds 23921da177e4SLinus Torvalds if (clear & TIOCM_RTS) 23931da177e4SLinus Torvalds mcr &= ~MCR_RTS; 23941da177e4SLinus Torvalds if (clear & TIOCM_DTR) 23951da177e4SLinus Torvalds mcr &= ~MCR_DTR; 23961da177e4SLinus Torvalds if (clear & TIOCM_LOOP) 23971da177e4SLinus Torvalds mcr &= ~MCR_LOOPBACK; 23981da177e4SLinus Torvalds 23991da177e4SLinus Torvalds edge_port->shadow_mcr = mcr; 24003d71fe0bSAlan Cox spin_unlock_irqrestore(&edge_port->ep_lock, flags); 24011da177e4SLinus Torvalds 24022742fd88SAlan Cox restore_mcr(edge_port, mcr); 24031da177e4SLinus Torvalds return 0; 24041da177e4SLinus Torvalds } 24051da177e4SLinus Torvalds 240660b33c13SAlan Cox static int edge_tiocmget(struct tty_struct *tty) 24071da177e4SLinus Torvalds { 240895da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 24091da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 24101da177e4SLinus Torvalds unsigned int result = 0; 24111da177e4SLinus Torvalds unsigned int msr; 24121da177e4SLinus Torvalds unsigned int mcr; 24133d71fe0bSAlan Cox unsigned long flags; 24141da177e4SLinus Torvalds 24153d71fe0bSAlan Cox spin_lock_irqsave(&edge_port->ep_lock, flags); 24163d71fe0bSAlan Cox 24171da177e4SLinus Torvalds msr = edge_port->shadow_msr; 24181da177e4SLinus Torvalds mcr = edge_port->shadow_mcr; 24191da177e4SLinus Torvalds result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */ 24201da177e4SLinus Torvalds | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */ 24211da177e4SLinus Torvalds | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */ 24221da177e4SLinus Torvalds | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */ 24231da177e4SLinus Torvalds | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */ 24241da177e4SLinus Torvalds | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */ 24251da177e4SLinus Torvalds 24261da177e4SLinus Torvalds 2427*67e6da70SGreg Kroah-Hartman dev_dbg(&port->dev, "%s -- %x\n", __func__, result); 24283d71fe0bSAlan Cox spin_unlock_irqrestore(&edge_port->ep_lock, flags); 24291da177e4SLinus Torvalds 24301da177e4SLinus Torvalds return result; 24311da177e4SLinus Torvalds } 24321da177e4SLinus Torvalds 24330bca1b91SAlan Cox static int edge_get_icount(struct tty_struct *tty, 24340bca1b91SAlan Cox struct serial_icounter_struct *icount) 24350bca1b91SAlan Cox { 24360bca1b91SAlan Cox struct usb_serial_port *port = tty->driver_data; 24370bca1b91SAlan Cox struct edgeport_port *edge_port = usb_get_serial_port_data(port); 24380bca1b91SAlan Cox struct async_icount *ic = &edge_port->icount; 24390bca1b91SAlan Cox 24400bca1b91SAlan Cox icount->cts = ic->cts; 24410bca1b91SAlan Cox icount->dsr = ic->dsr; 24420bca1b91SAlan Cox icount->rng = ic->rng; 24430bca1b91SAlan Cox icount->dcd = ic->dcd; 24440bca1b91SAlan Cox icount->tx = ic->tx; 24450bca1b91SAlan Cox icount->rx = ic->rx; 24460bca1b91SAlan Cox icount->frame = ic->frame; 24470bca1b91SAlan Cox icount->parity = ic->parity; 24480bca1b91SAlan Cox icount->overrun = ic->overrun; 24490bca1b91SAlan Cox icount->brk = ic->brk; 24500bca1b91SAlan Cox icount->buf_overrun = ic->buf_overrun; 24510bca1b91SAlan Cox return 0; 24520bca1b91SAlan Cox } 24530bca1b91SAlan Cox 24542742fd88SAlan Cox static int get_serial_info(struct edgeport_port *edge_port, 24552742fd88SAlan Cox struct serial_struct __user *retinfo) 24561da177e4SLinus Torvalds { 24571da177e4SLinus Torvalds struct serial_struct tmp; 24581da177e4SLinus Torvalds 24591da177e4SLinus Torvalds if (!retinfo) 24601da177e4SLinus Torvalds return -EFAULT; 24611da177e4SLinus Torvalds 24621da177e4SLinus Torvalds memset(&tmp, 0, sizeof(tmp)); 24631da177e4SLinus Torvalds 24641da177e4SLinus Torvalds tmp.type = PORT_16550A; 24651da177e4SLinus Torvalds tmp.line = edge_port->port->serial->minor; 24661da177e4SLinus Torvalds tmp.port = edge_port->port->number; 24671da177e4SLinus Torvalds tmp.irq = 0; 24681da177e4SLinus Torvalds tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; 24691da177e4SLinus Torvalds tmp.xmit_fifo_size = edge_port->port->bulk_out_size; 24701da177e4SLinus Torvalds tmp.baud_base = 9600; 24711da177e4SLinus Torvalds tmp.close_delay = 5*HZ; 24721da177e4SLinus Torvalds tmp.closing_wait = closing_wait; 24731da177e4SLinus Torvalds 24741da177e4SLinus Torvalds if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) 24751da177e4SLinus Torvalds return -EFAULT; 24761da177e4SLinus Torvalds return 0; 24771da177e4SLinus Torvalds } 24781da177e4SLinus Torvalds 247900a0d0d6SAlan Cox static int edge_ioctl(struct tty_struct *tty, 24802742fd88SAlan Cox unsigned int cmd, unsigned long arg) 24811da177e4SLinus Torvalds { 248295da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 24831da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 24841da177e4SLinus Torvalds struct async_icount cnow; 24851da177e4SLinus Torvalds struct async_icount cprev; 24861da177e4SLinus Torvalds 2487*67e6da70SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd); 24881da177e4SLinus Torvalds 24891da177e4SLinus Torvalds switch (cmd) { 24901da177e4SLinus Torvalds case TIOCGSERIAL: 2491*67e6da70SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - TIOCGSERIAL\n", __func__); 24922742fd88SAlan Cox return get_serial_info(edge_port, 24932742fd88SAlan Cox (struct serial_struct __user *) arg); 24941da177e4SLinus Torvalds case TIOCMIWAIT: 2495*67e6da70SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - TIOCMIWAIT\n", __func__); 24961da177e4SLinus Torvalds cprev = edge_port->icount; 24971da177e4SLinus Torvalds while (1) { 24981da177e4SLinus Torvalds interruptible_sleep_on(&edge_port->delta_msr_wait); 24991da177e4SLinus Torvalds /* see if a signal did it */ 25001da177e4SLinus Torvalds if (signal_pending(current)) 25011da177e4SLinus Torvalds return -ERESTARTSYS; 25021da177e4SLinus Torvalds cnow = edge_port->icount; 25031da177e4SLinus Torvalds if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr && 25041da177e4SLinus Torvalds cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) 25051da177e4SLinus Torvalds return -EIO; /* no change => error */ 25061da177e4SLinus Torvalds if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || 25071da177e4SLinus Torvalds ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || 25081da177e4SLinus Torvalds ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) || 25091da177e4SLinus Torvalds ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) { 25101da177e4SLinus Torvalds return 0; 25111da177e4SLinus Torvalds } 25121da177e4SLinus Torvalds cprev = cnow; 25131da177e4SLinus Torvalds } 25141da177e4SLinus Torvalds /* not reached */ 25151da177e4SLinus Torvalds break; 25161da177e4SLinus Torvalds } 25171da177e4SLinus Torvalds return -ENOIOCTLCMD; 25181da177e4SLinus Torvalds } 25191da177e4SLinus Torvalds 252095da310eSAlan Cox static void edge_break(struct tty_struct *tty, int break_state) 25211da177e4SLinus Torvalds { 252295da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 25231da177e4SLinus Torvalds struct edgeport_port *edge_port = usb_get_serial_port_data(port); 25241da177e4SLinus Torvalds int status; 25252742fd88SAlan Cox int bv = 0; /* Off */ 25261da177e4SLinus Torvalds 25271da177e4SLinus Torvalds /* chase the port close */ 25282742fd88SAlan Cox chase_port(edge_port, 0, 0); 25291da177e4SLinus Torvalds 253095da310eSAlan Cox if (break_state == -1) 25312742fd88SAlan Cox bv = 1; /* On */ 25322742fd88SAlan Cox status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv); 25332742fd88SAlan Cox if (status) 2534*67e6da70SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - error %d sending break set/clear command.\n", 2535441b62c1SHarvey Harrison __func__, status); 25361da177e4SLinus Torvalds } 25371da177e4SLinus Torvalds 25381da177e4SLinus Torvalds static int edge_startup(struct usb_serial *serial) 25391da177e4SLinus Torvalds { 25401da177e4SLinus Torvalds struct edgeport_serial *edge_serial; 25411da177e4SLinus Torvalds struct edgeport_port *edge_port; 25421da177e4SLinus Torvalds struct usb_device *dev; 25431da177e4SLinus Torvalds int status; 25441da177e4SLinus Torvalds int i; 25451da177e4SLinus Torvalds 25461da177e4SLinus Torvalds dev = serial->dev; 25471da177e4SLinus Torvalds 25481da177e4SLinus Torvalds /* create our private serial structure */ 254980b6ca48SEric Sesterhenn edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL); 25501da177e4SLinus Torvalds if (edge_serial == NULL) { 2551441b62c1SHarvey Harrison dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__); 25521da177e4SLinus Torvalds return -ENOMEM; 25531da177e4SLinus Torvalds } 2554241ca64fSMatthias Kaehlcke mutex_init(&edge_serial->es_lock); 25551da177e4SLinus Torvalds edge_serial->serial = serial; 25561da177e4SLinus Torvalds usb_set_serial_data(serial, edge_serial); 25571da177e4SLinus Torvalds 25582742fd88SAlan Cox status = download_fw(edge_serial); 25591da177e4SLinus Torvalds if (status) { 25601da177e4SLinus Torvalds kfree(edge_serial); 25611da177e4SLinus Torvalds return status; 25621da177e4SLinus Torvalds } 25631da177e4SLinus Torvalds 25641da177e4SLinus Torvalds /* set up our port private structures */ 25651da177e4SLinus Torvalds for (i = 0; i < serial->num_ports; ++i) { 256680b6ca48SEric Sesterhenn edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL); 25671da177e4SLinus Torvalds if (edge_port == NULL) { 25682742fd88SAlan Cox dev_err(&serial->dev->dev, "%s - Out of memory\n", 25692742fd88SAlan Cox __func__); 25701da177e4SLinus Torvalds goto cleanup; 25711da177e4SLinus Torvalds } 25721da177e4SLinus Torvalds spin_lock_init(&edge_port->ep_lock); 2573d733cec1SJohan Hovold if (kfifo_alloc(&edge_port->write_fifo, EDGE_OUT_BUF_SIZE, 2574d733cec1SJohan Hovold GFP_KERNEL)) { 25752742fd88SAlan Cox dev_err(&serial->dev->dev, "%s - Out of memory\n", 25762742fd88SAlan Cox __func__); 25771da177e4SLinus Torvalds kfree(edge_port); 25781da177e4SLinus Torvalds goto cleanup; 25791da177e4SLinus Torvalds } 25801da177e4SLinus Torvalds edge_port->port = serial->port[i]; 25811da177e4SLinus Torvalds edge_port->edge_serial = edge_serial; 25821da177e4SLinus Torvalds usb_set_serial_port_data(serial->port[i], edge_port); 2583fc4cbd75SMartin K. Petersen edge_port->bUartMode = default_uart_mode; 25841da177e4SLinus Torvalds } 25851da177e4SLinus Torvalds 25861da177e4SLinus Torvalds return 0; 25871da177e4SLinus Torvalds 25881da177e4SLinus Torvalds cleanup: 25891da177e4SLinus Torvalds for (--i; i >= 0; --i) { 25901da177e4SLinus Torvalds edge_port = usb_get_serial_port_data(serial->port[i]); 2591d733cec1SJohan Hovold kfifo_free(&edge_port->write_fifo); 25921da177e4SLinus Torvalds kfree(edge_port); 25931da177e4SLinus Torvalds usb_set_serial_port_data(serial->port[i], NULL); 25941da177e4SLinus Torvalds } 25951da177e4SLinus Torvalds kfree(edge_serial); 25961da177e4SLinus Torvalds usb_set_serial_data(serial, NULL); 25971da177e4SLinus Torvalds return -ENOMEM; 25981da177e4SLinus Torvalds } 25991da177e4SLinus Torvalds 2600f9c99bb8SAlan Stern static void edge_disconnect(struct usb_serial *serial) 26011da177e4SLinus Torvalds { 2602f9c99bb8SAlan Stern } 2603f9c99bb8SAlan Stern 2604f9c99bb8SAlan Stern static void edge_release(struct usb_serial *serial) 2605f9c99bb8SAlan Stern { 2606f9c99bb8SAlan Stern int i; 2607f9c99bb8SAlan Stern struct edgeport_port *edge_port; 2608f9c99bb8SAlan Stern 2609f9c99bb8SAlan Stern for (i = 0; i < serial->num_ports; ++i) { 2610f9c99bb8SAlan Stern edge_port = usb_get_serial_port_data(serial->port[i]); 2611d733cec1SJohan Hovold kfifo_free(&edge_port->write_fifo); 26121da177e4SLinus Torvalds kfree(edge_port); 26131da177e4SLinus Torvalds } 26141da177e4SLinus Torvalds kfree(usb_get_serial_data(serial)); 26151da177e4SLinus Torvalds } 26161da177e4SLinus Torvalds 26171da177e4SLinus Torvalds 2618fc4cbd75SMartin K. Petersen /* Sysfs Attributes */ 2619fc4cbd75SMartin K. Petersen 2620fc4cbd75SMartin K. Petersen static ssize_t show_uart_mode(struct device *dev, 2621fc4cbd75SMartin K. Petersen struct device_attribute *attr, char *buf) 2622fc4cbd75SMartin K. Petersen { 2623fc4cbd75SMartin K. Petersen struct usb_serial_port *port = to_usb_serial_port(dev); 2624fc4cbd75SMartin K. Petersen struct edgeport_port *edge_port = usb_get_serial_port_data(port); 2625fc4cbd75SMartin K. Petersen 2626fc4cbd75SMartin K. Petersen return sprintf(buf, "%d\n", edge_port->bUartMode); 2627fc4cbd75SMartin K. Petersen } 2628fc4cbd75SMartin K. Petersen 2629fc4cbd75SMartin K. Petersen static ssize_t store_uart_mode(struct device *dev, 2630fc4cbd75SMartin K. Petersen struct device_attribute *attr, const char *valbuf, size_t count) 2631fc4cbd75SMartin K. Petersen { 2632fc4cbd75SMartin K. Petersen struct usb_serial_port *port = to_usb_serial_port(dev); 2633fc4cbd75SMartin K. Petersen struct edgeport_port *edge_port = usb_get_serial_port_data(port); 2634fc4cbd75SMartin K. Petersen unsigned int v = simple_strtoul(valbuf, NULL, 0); 2635fc4cbd75SMartin K. Petersen 2636*67e6da70SGreg Kroah-Hartman dev_dbg(dev, "%s: setting uart_mode = %d\n", __func__, v); 2637fc4cbd75SMartin K. Petersen 2638fc4cbd75SMartin K. Petersen if (v < 256) 2639fc4cbd75SMartin K. Petersen edge_port->bUartMode = v; 2640fc4cbd75SMartin K. Petersen else 2641441b62c1SHarvey Harrison dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v); 2642fc4cbd75SMartin K. Petersen 2643fc4cbd75SMartin K. Petersen return count; 2644fc4cbd75SMartin K. Petersen } 2645fc4cbd75SMartin K. Petersen 26462742fd88SAlan Cox static DEVICE_ATTR(uart_mode, S_IWUSR | S_IRUGO, show_uart_mode, 26472742fd88SAlan Cox store_uart_mode); 2648fc4cbd75SMartin K. Petersen 2649fc4cbd75SMartin K. Petersen static int edge_create_sysfs_attrs(struct usb_serial_port *port) 2650fc4cbd75SMartin K. Petersen { 2651fc4cbd75SMartin K. Petersen return device_create_file(&port->dev, &dev_attr_uart_mode); 2652fc4cbd75SMartin K. Petersen } 2653fc4cbd75SMartin K. Petersen 2654fc4cbd75SMartin K. Petersen static int edge_remove_sysfs_attrs(struct usb_serial_port *port) 2655fc4cbd75SMartin K. Petersen { 2656fc4cbd75SMartin K. Petersen device_remove_file(&port->dev, &dev_attr_uart_mode); 2657fc4cbd75SMartin K. Petersen return 0; 2658fc4cbd75SMartin K. Petersen } 2659fc4cbd75SMartin K. Petersen 2660fc4cbd75SMartin K. Petersen 2661ea65370dSGreg Kroah-Hartman static struct usb_serial_driver edgeport_1port_device = { 266218fcac35SGreg Kroah-Hartman .driver = { 26631da177e4SLinus Torvalds .owner = THIS_MODULE, 2664269bda1cSGreg Kroah-Hartman .name = "edgeport_ti_1", 266518fcac35SGreg Kroah-Hartman }, 2666269bda1cSGreg Kroah-Hartman .description = "Edgeport TI 1 port adapter", 26671da177e4SLinus Torvalds .id_table = edgeport_1port_id_table, 26681da177e4SLinus Torvalds .num_ports = 1, 26691da177e4SLinus Torvalds .open = edge_open, 26701da177e4SLinus Torvalds .close = edge_close, 26711da177e4SLinus Torvalds .throttle = edge_throttle, 26721da177e4SLinus Torvalds .unthrottle = edge_unthrottle, 26731da177e4SLinus Torvalds .attach = edge_startup, 2674f9c99bb8SAlan Stern .disconnect = edge_disconnect, 2675f9c99bb8SAlan Stern .release = edge_release, 2676fc4cbd75SMartin K. Petersen .port_probe = edge_create_sysfs_attrs, 26776d443d84SEric W. Biederman .port_remove = edge_remove_sysfs_attrs, 26781da177e4SLinus Torvalds .ioctl = edge_ioctl, 26791da177e4SLinus Torvalds .set_termios = edge_set_termios, 26801da177e4SLinus Torvalds .tiocmget = edge_tiocmget, 26811da177e4SLinus Torvalds .tiocmset = edge_tiocmset, 26820bca1b91SAlan Cox .get_icount = edge_get_icount, 26831da177e4SLinus Torvalds .write = edge_write, 26841da177e4SLinus Torvalds .write_room = edge_write_room, 26851da177e4SLinus Torvalds .chars_in_buffer = edge_chars_in_buffer, 26861da177e4SLinus Torvalds .break_ctl = edge_break, 26871da177e4SLinus Torvalds .read_int_callback = edge_interrupt_callback, 26881da177e4SLinus Torvalds .read_bulk_callback = edge_bulk_in_callback, 26891da177e4SLinus Torvalds .write_bulk_callback = edge_bulk_out_callback, 26901da177e4SLinus Torvalds }; 26911da177e4SLinus Torvalds 2692ea65370dSGreg Kroah-Hartman static struct usb_serial_driver edgeport_2port_device = { 269318fcac35SGreg Kroah-Hartman .driver = { 26941da177e4SLinus Torvalds .owner = THIS_MODULE, 2695269bda1cSGreg Kroah-Hartman .name = "edgeport_ti_2", 269618fcac35SGreg Kroah-Hartman }, 2697269bda1cSGreg Kroah-Hartman .description = "Edgeport TI 2 port adapter", 26981da177e4SLinus Torvalds .id_table = edgeport_2port_id_table, 26991da177e4SLinus Torvalds .num_ports = 2, 27001da177e4SLinus Torvalds .open = edge_open, 27011da177e4SLinus Torvalds .close = edge_close, 27021da177e4SLinus Torvalds .throttle = edge_throttle, 27031da177e4SLinus Torvalds .unthrottle = edge_unthrottle, 27041da177e4SLinus Torvalds .attach = edge_startup, 2705f9c99bb8SAlan Stern .disconnect = edge_disconnect, 2706f9c99bb8SAlan Stern .release = edge_release, 2707fc4cbd75SMartin K. Petersen .port_probe = edge_create_sysfs_attrs, 27086d443d84SEric W. Biederman .port_remove = edge_remove_sysfs_attrs, 27091da177e4SLinus Torvalds .ioctl = edge_ioctl, 27101da177e4SLinus Torvalds .set_termios = edge_set_termios, 27111da177e4SLinus Torvalds .tiocmget = edge_tiocmget, 27121da177e4SLinus Torvalds .tiocmset = edge_tiocmset, 27131da177e4SLinus Torvalds .write = edge_write, 27141da177e4SLinus Torvalds .write_room = edge_write_room, 27151da177e4SLinus Torvalds .chars_in_buffer = edge_chars_in_buffer, 27161da177e4SLinus Torvalds .break_ctl = edge_break, 27171da177e4SLinus Torvalds .read_int_callback = edge_interrupt_callback, 27181da177e4SLinus Torvalds .read_bulk_callback = edge_bulk_in_callback, 27191da177e4SLinus Torvalds .write_bulk_callback = edge_bulk_out_callback, 27201da177e4SLinus Torvalds }; 27211da177e4SLinus Torvalds 27227dbe2460SAlan Stern static struct usb_serial_driver * const serial_drivers[] = { 27237dbe2460SAlan Stern &edgeport_1port_device, &edgeport_2port_device, NULL 27247dbe2460SAlan Stern }; 27257dbe2460SAlan Stern 272668e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table_combined); 27271da177e4SLinus Torvalds 27281da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR); 27291da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC); 27301da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 2731d12b219aSJaswinder Singh MODULE_FIRMWARE("edgeport/down3.bin"); 27321da177e4SLinus Torvalds 27331da177e4SLinus Torvalds module_param(debug, bool, S_IRUGO | S_IWUSR); 27341da177e4SLinus Torvalds MODULE_PARM_DESC(debug, "Debug enabled or not"); 27351da177e4SLinus Torvalds 27361da177e4SLinus Torvalds module_param(closing_wait, int, S_IRUGO | S_IWUSR); 27371da177e4SLinus Torvalds MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs"); 27381da177e4SLinus Torvalds 27391da177e4SLinus Torvalds module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR); 27402742fd88SAlan Cox MODULE_PARM_DESC(ignore_cpu_rev, 27412742fd88SAlan Cox "Ignore the cpu revision when connecting to a device"); 27421da177e4SLinus Torvalds 2743fc4cbd75SMartin K. Petersen module_param(default_uart_mode, int, S_IRUGO | S_IWUSR); 2744fc4cbd75SMartin K. Petersen MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ..."); 2745