Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * TI 3410/5052 USB Serial Driver
0004  *
0005  * Copyright (C) 2004 Texas Instruments
0006  *
0007  * This driver is based on the Linux io_ti driver, which is
0008  *   Copyright (C) 2000-2002 Inside Out Networks
0009  *   Copyright (C) 2001-2002 Greg Kroah-Hartman
0010  *
0011  * For questions or problems with this driver, contact Texas Instruments
0012  * technical support, or Al Borchers <alborchers@steinerpoint.com>, or
0013  * Peter Berger <pberger@brimson.com>.
0014  */
0015 
0016 #include <linux/kernel.h>
0017 #include <linux/errno.h>
0018 #include <linux/firmware.h>
0019 #include <linux/slab.h>
0020 #include <linux/tty.h>
0021 #include <linux/tty_driver.h>
0022 #include <linux/tty_flip.h>
0023 #include <linux/module.h>
0024 #include <linux/spinlock.h>
0025 #include <linux/ioctl.h>
0026 #include <linux/serial.h>
0027 #include <linux/kfifo.h>
0028 #include <linux/mutex.h>
0029 #include <linux/uaccess.h>
0030 #include <linux/usb.h>
0031 #include <linux/usb/serial.h>
0032 
0033 /* Configuration ids */
0034 #define TI_BOOT_CONFIG          1
0035 #define TI_ACTIVE_CONFIG        2
0036 
0037 /* Vendor and product ids */
0038 #define TI_VENDOR_ID            0x0451
0039 #define IBM_VENDOR_ID           0x04b3
0040 #define STARTECH_VENDOR_ID      0x14b0
0041 #define TI_3410_PRODUCT_ID      0x3410
0042 #define IBM_4543_PRODUCT_ID     0x4543
0043 #define IBM_454B_PRODUCT_ID     0x454b
0044 #define IBM_454C_PRODUCT_ID     0x454c
0045 #define TI_3410_EZ430_ID        0xF430  /* TI ez430 development tool */
0046 #define TI_5052_BOOT_PRODUCT_ID     0x5052  /* no EEPROM, no firmware */
0047 #define TI_5152_BOOT_PRODUCT_ID     0x5152  /* no EEPROM, no firmware */
0048 #define TI_5052_EEPROM_PRODUCT_ID   0x505A  /* EEPROM, no firmware */
0049 #define TI_5052_FIRMWARE_PRODUCT_ID 0x505F  /* firmware is running */
0050 #define FRI2_PRODUCT_ID         0x5053  /* Fish River Island II */
0051 
0052 /* Multi-Tech vendor and product ids */
0053 #define MTS_VENDOR_ID           0x06E0
0054 #define MTS_GSM_NO_FW_PRODUCT_ID    0xF108
0055 #define MTS_CDMA_NO_FW_PRODUCT_ID   0xF109
0056 #define MTS_CDMA_PRODUCT_ID     0xF110
0057 #define MTS_GSM_PRODUCT_ID      0xF111
0058 #define MTS_EDGE_PRODUCT_ID     0xF112
0059 #define MTS_MT9234MU_PRODUCT_ID     0xF114
0060 #define MTS_MT9234ZBA_PRODUCT_ID    0xF115
0061 #define MTS_MT9234ZBAOLD_PRODUCT_ID 0x0319
0062 
0063 /* Abbott Diabetics vendor and product ids */
0064 #define ABBOTT_VENDOR_ID        0x1a61
0065 #define ABBOTT_STEREO_PLUG_ID       0x3410
0066 #define ABBOTT_PRODUCT_ID       ABBOTT_STEREO_PLUG_ID
0067 #define ABBOTT_STRIP_PORT_ID        0x3420
0068 
0069 /* Honeywell vendor and product IDs */
0070 #define HONEYWELL_VENDOR_ID     0x10ac
0071 #define HONEYWELL_HGI80_PRODUCT_ID  0x0102  /* Honeywell HGI80 */
0072 
0073 /* Moxa UPORT 11x0 vendor and product IDs */
0074 #define MXU1_VENDOR_ID              0x110a
0075 #define MXU1_1110_PRODUCT_ID            0x1110
0076 #define MXU1_1130_PRODUCT_ID            0x1130
0077 #define MXU1_1150_PRODUCT_ID            0x1150
0078 #define MXU1_1151_PRODUCT_ID            0x1151
0079 #define MXU1_1131_PRODUCT_ID            0x1131
0080 
0081 /* Commands */
0082 #define TI_GET_VERSION          0x01
0083 #define TI_GET_PORT_STATUS      0x02
0084 #define TI_GET_PORT_DEV_INFO        0x03
0085 #define TI_GET_CONFIG           0x04
0086 #define TI_SET_CONFIG           0x05
0087 #define TI_OPEN_PORT            0x06
0088 #define TI_CLOSE_PORT           0x07
0089 #define TI_START_PORT           0x08
0090 #define TI_STOP_PORT            0x09
0091 #define TI_TEST_PORT            0x0A
0092 #define TI_PURGE_PORT           0x0B
0093 #define TI_RESET_EXT_DEVICE     0x0C
0094 #define TI_WRITE_DATA           0x80
0095 #define TI_READ_DATA            0x81
0096 #define TI_REQ_TYPE_CLASS       0x82
0097 
0098 /* Module identifiers */
0099 #define TI_I2C_PORT         0x01
0100 #define TI_IEEE1284_PORT        0x02
0101 #define TI_UART1_PORT           0x03
0102 #define TI_UART2_PORT           0x04
0103 #define TI_RAM_PORT         0x05
0104 
0105 /* Modem status */
0106 #define TI_MSR_DELTA_CTS        0x01
0107 #define TI_MSR_DELTA_DSR        0x02
0108 #define TI_MSR_DELTA_RI         0x04
0109 #define TI_MSR_DELTA_CD         0x08
0110 #define TI_MSR_CTS          0x10
0111 #define TI_MSR_DSR          0x20
0112 #define TI_MSR_RI           0x40
0113 #define TI_MSR_CD           0x80
0114 #define TI_MSR_DELTA_MASK       0x0F
0115 #define TI_MSR_MASK         0xF0
0116 
0117 /* Line status */
0118 #define TI_LSR_OVERRUN_ERROR        0x01
0119 #define TI_LSR_PARITY_ERROR     0x02
0120 #define TI_LSR_FRAMING_ERROR        0x04
0121 #define TI_LSR_BREAK            0x08
0122 #define TI_LSR_ERROR            0x0F
0123 #define TI_LSR_RX_FULL          0x10
0124 #define TI_LSR_TX_EMPTY         0x20
0125 #define TI_LSR_TX_EMPTY_BOTH        0x40
0126 
0127 /* Line control */
0128 #define TI_LCR_BREAK            0x40
0129 
0130 /* Modem control */
0131 #define TI_MCR_LOOP         0x04
0132 #define TI_MCR_DTR          0x10
0133 #define TI_MCR_RTS          0x20
0134 
0135 /* Mask settings */
0136 #define TI_UART_ENABLE_RTS_IN       0x0001
0137 #define TI_UART_DISABLE_RTS     0x0002
0138 #define TI_UART_ENABLE_PARITY_CHECKING  0x0008
0139 #define TI_UART_ENABLE_DSR_OUT      0x0010
0140 #define TI_UART_ENABLE_CTS_OUT      0x0020
0141 #define TI_UART_ENABLE_X_OUT        0x0040
0142 #define TI_UART_ENABLE_XA_OUT       0x0080
0143 #define TI_UART_ENABLE_X_IN     0x0100
0144 #define TI_UART_ENABLE_DTR_IN       0x0800
0145 #define TI_UART_DISABLE_DTR     0x1000
0146 #define TI_UART_ENABLE_MS_INTS      0x2000
0147 #define TI_UART_ENABLE_AUTO_START_DMA   0x4000
0148 
0149 /* Parity */
0150 #define TI_UART_NO_PARITY       0x00
0151 #define TI_UART_ODD_PARITY      0x01
0152 #define TI_UART_EVEN_PARITY     0x02
0153 #define TI_UART_MARK_PARITY     0x03
0154 #define TI_UART_SPACE_PARITY        0x04
0155 
0156 /* Stop bits */
0157 #define TI_UART_1_STOP_BITS     0x00
0158 #define TI_UART_1_5_STOP_BITS       0x01
0159 #define TI_UART_2_STOP_BITS     0x02
0160 
0161 /* Bits per character */
0162 #define TI_UART_5_DATA_BITS     0x00
0163 #define TI_UART_6_DATA_BITS     0x01
0164 #define TI_UART_7_DATA_BITS     0x02
0165 #define TI_UART_8_DATA_BITS     0x03
0166 
0167 /* 232/485 modes */
0168 #define TI_UART_232         0x00
0169 #define TI_UART_485_RECEIVER_DISABLED   0x01
0170 #define TI_UART_485_RECEIVER_ENABLED    0x02
0171 
0172 /* Pipe transfer mode and timeout */
0173 #define TI_PIPE_MODE_CONTINUOUS     0x01
0174 #define TI_PIPE_MODE_MASK       0x03
0175 #define TI_PIPE_TIMEOUT_MASK        0x7C
0176 #define TI_PIPE_TIMEOUT_ENABLE      0x80
0177 
0178 /* Config struct */
0179 struct ti_uart_config {
0180     __be16  wBaudRate;
0181     __be16  wFlags;
0182     u8  bDataBits;
0183     u8  bParity;
0184     u8  bStopBits;
0185     char    cXon;
0186     char    cXoff;
0187     u8  bUartMode;
0188 };
0189 
0190 /* Get port status */
0191 struct ti_port_status {
0192     u8 bCmdCode;
0193     u8 bModuleId;
0194     u8 bErrorCode;
0195     u8 bMSR;
0196     u8 bLSR;
0197 };
0198 
0199 /* Purge modes */
0200 #define TI_PURGE_OUTPUT         0x00
0201 #define TI_PURGE_INPUT          0x80
0202 
0203 /* Read/Write data */
0204 #define TI_RW_DATA_ADDR_SFR     0x10
0205 #define TI_RW_DATA_ADDR_IDATA       0x20
0206 #define TI_RW_DATA_ADDR_XDATA       0x30
0207 #define TI_RW_DATA_ADDR_CODE        0x40
0208 #define TI_RW_DATA_ADDR_GPIO        0x50
0209 #define TI_RW_DATA_ADDR_I2C     0x60
0210 #define TI_RW_DATA_ADDR_FLASH       0x70
0211 #define TI_RW_DATA_ADDR_DSP     0x80
0212 
0213 #define TI_RW_DATA_UNSPECIFIED      0x00
0214 #define TI_RW_DATA_BYTE         0x01
0215 #define TI_RW_DATA_WORD         0x02
0216 #define TI_RW_DATA_DOUBLE_WORD      0x04
0217 
0218 struct ti_write_data_bytes {
0219     u8  bAddrType;
0220     u8  bDataType;
0221     u8  bDataCounter;
0222     __be16  wBaseAddrHi;
0223     __be16  wBaseAddrLo;
0224     u8  bData[];
0225 } __packed;
0226 
0227 struct ti_read_data_request {
0228     u8  bAddrType;
0229     u8  bDataType;
0230     u8  bDataCounter;
0231     __be16  wBaseAddrHi;
0232     __be16  wBaseAddrLo;
0233 } __packed;
0234 
0235 struct ti_read_data_bytes {
0236     u8  bCmdCode;
0237     u8  bModuleId;
0238     u8  bErrorCode;
0239     u8  bData[];
0240 };
0241 
0242 /* Interrupt struct */
0243 struct ti_interrupt {
0244     u8  bICode;
0245     u8  bIInfo;
0246 };
0247 
0248 /* Interrupt codes */
0249 #define TI_CODE_HARDWARE_ERROR      0xFF
0250 #define TI_CODE_DATA_ERROR      0x03
0251 #define TI_CODE_MODEM_STATUS        0x04
0252 
0253 /* Download firmware max packet size */
0254 #define TI_DOWNLOAD_MAX_PACKET_SIZE 64
0255 
0256 /* Firmware image header */
0257 struct ti_firmware_header {
0258     __le16  wLength;
0259     u8  bCheckSum;
0260 } __packed;
0261 
0262 /* UART addresses */
0263 #define TI_UART1_BASE_ADDR      0xFFA0  /* UART 1 base address */
0264 #define TI_UART2_BASE_ADDR      0xFFB0  /* UART 2 base address */
0265 #define TI_UART_OFFSET_LCR      0x0002  /* UART MCR register offset */
0266 #define TI_UART_OFFSET_MCR      0x0004  /* UART MCR register offset */
0267 
0268 #define TI_DRIVER_AUTHOR    "Al Borchers <alborchers@steinerpoint.com>"
0269 #define TI_DRIVER_DESC      "TI USB 3410/5052 Serial Driver"
0270 
0271 #define TI_FIRMWARE_BUF_SIZE    16284
0272 
0273 #define TI_TRANSFER_TIMEOUT 2
0274 
0275 /* read urb states */
0276 #define TI_READ_URB_RUNNING 0
0277 #define TI_READ_URB_STOPPING    1
0278 #define TI_READ_URB_STOPPED 2
0279 
0280 #define TI_EXTRA_VID_PID_COUNT  5
0281 
0282 struct ti_port {
0283     int         tp_is_open;
0284     u8          tp_msr;
0285     u8          tp_shadow_mcr;
0286     u8          tp_uart_mode;   /* 232 or 485 modes */
0287     unsigned int        tp_uart_base_addr;
0288     struct ti_device    *tp_tdev;
0289     struct usb_serial_port  *tp_port;
0290     spinlock_t      tp_lock;
0291     int         tp_read_urb_state;
0292     int         tp_write_urb_in_use;
0293 };
0294 
0295 struct ti_device {
0296     struct mutex        td_open_close_lock;
0297     int         td_open_port_count;
0298     struct usb_serial   *td_serial;
0299     int         td_is_3410;
0300     bool            td_rs485_only;
0301 };
0302 
0303 static int ti_startup(struct usb_serial *serial);
0304 static void ti_release(struct usb_serial *serial);
0305 static int ti_port_probe(struct usb_serial_port *port);
0306 static void ti_port_remove(struct usb_serial_port *port);
0307 static int ti_open(struct tty_struct *tty, struct usb_serial_port *port);
0308 static void ti_close(struct usb_serial_port *port);
0309 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
0310         const unsigned char *data, int count);
0311 static unsigned int ti_write_room(struct tty_struct *tty);
0312 static unsigned int ti_chars_in_buffer(struct tty_struct *tty);
0313 static bool ti_tx_empty(struct usb_serial_port *port);
0314 static void ti_throttle(struct tty_struct *tty);
0315 static void ti_unthrottle(struct tty_struct *tty);
0316 static void ti_set_termios(struct tty_struct *tty,
0317         struct usb_serial_port *port, struct ktermios *old_termios);
0318 static int ti_tiocmget(struct tty_struct *tty);
0319 static int ti_tiocmset(struct tty_struct *tty,
0320         unsigned int set, unsigned int clear);
0321 static void ti_break(struct tty_struct *tty, int break_state);
0322 static void ti_interrupt_callback(struct urb *urb);
0323 static void ti_bulk_in_callback(struct urb *urb);
0324 static void ti_bulk_out_callback(struct urb *urb);
0325 
0326 static void ti_recv(struct usb_serial_port *port, unsigned char *data,
0327         int length);
0328 static void ti_send(struct ti_port *tport);
0329 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr);
0330 static int ti_get_lsr(struct ti_port *tport, u8 *lsr);
0331 static void ti_get_serial_info(struct tty_struct *tty, struct serial_struct *ss);
0332 static void ti_handle_new_msr(struct ti_port *tport, u8 msr);
0333 
0334 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty);
0335 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty);
0336 
0337 static int ti_command_out_sync(struct usb_device *udev, u8 command,
0338         u16 moduleid, u16 value, void *data, int size);
0339 static int ti_command_in_sync(struct usb_device *udev, u8 command,
0340         u16 moduleid, u16 value, void *data, int size);
0341 static int ti_port_cmd_out(struct usb_serial_port *port, u8 command,
0342         u16 value, void *data, int size);
0343 static int ti_port_cmd_in(struct usb_serial_port *port, u8 command,
0344         u16 value, void *data, int size);
0345 
0346 static int ti_write_byte(struct usb_serial_port *port, struct ti_device *tdev,
0347              unsigned long addr, u8 mask, u8 byte);
0348 
0349 static int ti_download_firmware(struct ti_device *tdev);
0350 
0351 static const struct usb_device_id ti_id_table_3410[] = {
0352     { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
0353     { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
0354     { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
0355     { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
0356     { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
0357     { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
0358     { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
0359     { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
0360     { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
0361     { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
0362     { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
0363     { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
0364     { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
0365     { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_STEREO_PLUG_ID) },
0366     { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_STRIP_PORT_ID) },
0367     { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
0368     { USB_DEVICE(HONEYWELL_VENDOR_ID, HONEYWELL_HGI80_PRODUCT_ID) },
0369     { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1110_PRODUCT_ID) },
0370     { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1130_PRODUCT_ID) },
0371     { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1131_PRODUCT_ID) },
0372     { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1150_PRODUCT_ID) },
0373     { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1151_PRODUCT_ID) },
0374     { USB_DEVICE(STARTECH_VENDOR_ID, TI_3410_PRODUCT_ID) },
0375     { } /* terminator */
0376 };
0377 
0378 static const struct usb_device_id ti_id_table_5052[] = {
0379     { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
0380     { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
0381     { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
0382     { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
0383     { }
0384 };
0385 
0386 static const struct usb_device_id ti_id_table_combined[] = {
0387     { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
0388     { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
0389     { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
0390     { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
0391     { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
0392     { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
0393     { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
0394     { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
0395     { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
0396     { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
0397     { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
0398     { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
0399     { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
0400     { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
0401     { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
0402     { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
0403     { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
0404     { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_PRODUCT_ID) },
0405     { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_STRIP_PORT_ID) },
0406     { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
0407     { USB_DEVICE(HONEYWELL_VENDOR_ID, HONEYWELL_HGI80_PRODUCT_ID) },
0408     { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1110_PRODUCT_ID) },
0409     { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1130_PRODUCT_ID) },
0410     { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1131_PRODUCT_ID) },
0411     { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1150_PRODUCT_ID) },
0412     { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1151_PRODUCT_ID) },
0413     { USB_DEVICE(STARTECH_VENDOR_ID, TI_3410_PRODUCT_ID) },
0414     { } /* terminator */
0415 };
0416 
0417 static struct usb_serial_driver ti_1port_device = {
0418     .driver = {
0419         .owner      = THIS_MODULE,
0420         .name       = "ti_usb_3410_5052_1",
0421     },
0422     .description        = "TI USB 3410 1 port adapter",
0423     .id_table       = ti_id_table_3410,
0424     .num_ports      = 1,
0425     .num_bulk_out       = 1,
0426     .attach         = ti_startup,
0427     .release        = ti_release,
0428     .port_probe     = ti_port_probe,
0429     .port_remove        = ti_port_remove,
0430     .open           = ti_open,
0431     .close          = ti_close,
0432     .write          = ti_write,
0433     .write_room     = ti_write_room,
0434     .chars_in_buffer    = ti_chars_in_buffer,
0435     .tx_empty       = ti_tx_empty,
0436     .throttle       = ti_throttle,
0437     .unthrottle     = ti_unthrottle,
0438     .get_serial     = ti_get_serial_info,
0439     .set_termios        = ti_set_termios,
0440     .tiocmget       = ti_tiocmget,
0441     .tiocmset       = ti_tiocmset,
0442     .tiocmiwait     = usb_serial_generic_tiocmiwait,
0443     .get_icount     = usb_serial_generic_get_icount,
0444     .break_ctl      = ti_break,
0445     .read_int_callback  = ti_interrupt_callback,
0446     .read_bulk_callback = ti_bulk_in_callback,
0447     .write_bulk_callback    = ti_bulk_out_callback,
0448 };
0449 
0450 static struct usb_serial_driver ti_2port_device = {
0451     .driver = {
0452         .owner      = THIS_MODULE,
0453         .name       = "ti_usb_3410_5052_2",
0454     },
0455     .description        = "TI USB 5052 2 port adapter",
0456     .id_table       = ti_id_table_5052,
0457     .num_ports      = 2,
0458     .num_bulk_out       = 1,
0459     .attach         = ti_startup,
0460     .release        = ti_release,
0461     .port_probe     = ti_port_probe,
0462     .port_remove        = ti_port_remove,
0463     .open           = ti_open,
0464     .close          = ti_close,
0465     .write          = ti_write,
0466     .write_room     = ti_write_room,
0467     .chars_in_buffer    = ti_chars_in_buffer,
0468     .tx_empty       = ti_tx_empty,
0469     .throttle       = ti_throttle,
0470     .unthrottle     = ti_unthrottle,
0471     .get_serial     = ti_get_serial_info,
0472     .set_termios        = ti_set_termios,
0473     .tiocmget       = ti_tiocmget,
0474     .tiocmset       = ti_tiocmset,
0475     .tiocmiwait     = usb_serial_generic_tiocmiwait,
0476     .get_icount     = usb_serial_generic_get_icount,
0477     .break_ctl      = ti_break,
0478     .read_int_callback  = ti_interrupt_callback,
0479     .read_bulk_callback = ti_bulk_in_callback,
0480     .write_bulk_callback    = ti_bulk_out_callback,
0481 };
0482 
0483 static struct usb_serial_driver * const serial_drivers[] = {
0484     &ti_1port_device, &ti_2port_device, NULL
0485 };
0486 
0487 MODULE_AUTHOR(TI_DRIVER_AUTHOR);
0488 MODULE_DESCRIPTION(TI_DRIVER_DESC);
0489 MODULE_LICENSE("GPL");
0490 
0491 MODULE_FIRMWARE("ti_3410.fw");
0492 MODULE_FIRMWARE("ti_5052.fw");
0493 MODULE_FIRMWARE("mts_cdma.fw");
0494 MODULE_FIRMWARE("mts_gsm.fw");
0495 MODULE_FIRMWARE("mts_edge.fw");
0496 MODULE_FIRMWARE("mts_mt9234mu.fw");
0497 MODULE_FIRMWARE("mts_mt9234zba.fw");
0498 MODULE_FIRMWARE("moxa/moxa-1110.fw");
0499 MODULE_FIRMWARE("moxa/moxa-1130.fw");
0500 MODULE_FIRMWARE("moxa/moxa-1131.fw");
0501 MODULE_FIRMWARE("moxa/moxa-1150.fw");
0502 MODULE_FIRMWARE("moxa/moxa-1151.fw");
0503 
0504 MODULE_DEVICE_TABLE(usb, ti_id_table_combined);
0505 
0506 module_usb_serial_driver(serial_drivers, ti_id_table_combined);
0507 
0508 static int ti_startup(struct usb_serial *serial)
0509 {
0510     struct ti_device *tdev;
0511     struct usb_device *dev = serial->dev;
0512     struct usb_host_interface *cur_altsetting;
0513     int num_endpoints;
0514     u16 vid, pid;
0515     int status;
0516 
0517     dev_dbg(&dev->dev,
0518         "%s - product 0x%4X, num configurations %d, configuration value %d\n",
0519         __func__, le16_to_cpu(dev->descriptor.idProduct),
0520         dev->descriptor.bNumConfigurations,
0521         dev->actconfig->desc.bConfigurationValue);
0522 
0523     tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
0524     if (!tdev)
0525         return -ENOMEM;
0526 
0527     mutex_init(&tdev->td_open_close_lock);
0528     tdev->td_serial = serial;
0529     usb_set_serial_data(serial, tdev);
0530 
0531     /* determine device type */
0532     if (serial->type == &ti_1port_device)
0533         tdev->td_is_3410 = 1;
0534     dev_dbg(&dev->dev, "%s - device type is %s\n", __func__,
0535         tdev->td_is_3410 ? "3410" : "5052");
0536 
0537     vid = le16_to_cpu(dev->descriptor.idVendor);
0538     pid = le16_to_cpu(dev->descriptor.idProduct);
0539     if (vid == MXU1_VENDOR_ID) {
0540         switch (pid) {
0541         case MXU1_1130_PRODUCT_ID:
0542         case MXU1_1131_PRODUCT_ID:
0543             tdev->td_rs485_only = true;
0544             break;
0545         }
0546     }
0547 
0548     cur_altsetting = serial->interface->cur_altsetting;
0549     num_endpoints = cur_altsetting->desc.bNumEndpoints;
0550 
0551     /* if we have only 1 configuration and 1 endpoint, download firmware */
0552     if (dev->descriptor.bNumConfigurations == 1 && num_endpoints == 1) {
0553         status = ti_download_firmware(tdev);
0554 
0555         if (status != 0)
0556             goto free_tdev;
0557 
0558         /* 3410 must be reset, 5052 resets itself */
0559         if (tdev->td_is_3410) {
0560             msleep_interruptible(100);
0561             usb_reset_device(dev);
0562         }
0563 
0564         status = -ENODEV;
0565         goto free_tdev;
0566     }
0567 
0568     /* the second configuration must be set */
0569     if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
0570         status = usb_driver_set_configuration(dev, TI_ACTIVE_CONFIG);
0571         status = status ? status : -ENODEV;
0572         goto free_tdev;
0573     }
0574 
0575     if (serial->num_bulk_in < serial->num_ports ||
0576             serial->num_bulk_out < serial->num_ports) {
0577         dev_err(&serial->interface->dev, "missing endpoints\n");
0578         status = -ENODEV;
0579         goto free_tdev;
0580     }
0581 
0582     return 0;
0583 
0584 free_tdev:
0585     kfree(tdev);
0586     usb_set_serial_data(serial, NULL);
0587     return status;
0588 }
0589 
0590 
0591 static void ti_release(struct usb_serial *serial)
0592 {
0593     struct ti_device *tdev = usb_get_serial_data(serial);
0594 
0595     kfree(tdev);
0596 }
0597 
0598 static int ti_port_probe(struct usb_serial_port *port)
0599 {
0600     struct ti_port *tport;
0601 
0602     tport = kzalloc(sizeof(*tport), GFP_KERNEL);
0603     if (!tport)
0604         return -ENOMEM;
0605 
0606     spin_lock_init(&tport->tp_lock);
0607     if (port == port->serial->port[0])
0608         tport->tp_uart_base_addr = TI_UART1_BASE_ADDR;
0609     else
0610         tport->tp_uart_base_addr = TI_UART2_BASE_ADDR;
0611     tport->tp_port = port;
0612     tport->tp_tdev = usb_get_serial_data(port->serial);
0613 
0614     if (tport->tp_tdev->td_rs485_only)
0615         tport->tp_uart_mode = TI_UART_485_RECEIVER_DISABLED;
0616     else
0617         tport->tp_uart_mode = TI_UART_232;
0618 
0619     usb_set_serial_port_data(port, tport);
0620 
0621     /*
0622      * The TUSB5052 LSR does not tell when the transmitter shift register
0623      * has emptied so add a one-character drain delay.
0624      */
0625     if (!tport->tp_tdev->td_is_3410)
0626         port->port.drain_delay = 1;
0627 
0628     return 0;
0629 }
0630 
0631 static void ti_port_remove(struct usb_serial_port *port)
0632 {
0633     struct ti_port *tport;
0634 
0635     tport = usb_get_serial_port_data(port);
0636     kfree(tport);
0637 }
0638 
0639 static int ti_open(struct tty_struct *tty, struct usb_serial_port *port)
0640 {
0641     struct ti_port *tport = usb_get_serial_port_data(port);
0642     struct ti_device *tdev;
0643     struct usb_device *dev;
0644     struct urb *urb;
0645     int status;
0646     u16 open_settings;
0647 
0648     open_settings = (TI_PIPE_MODE_CONTINUOUS |
0649              TI_PIPE_TIMEOUT_ENABLE |
0650              (TI_TRANSFER_TIMEOUT << 2));
0651 
0652     dev = port->serial->dev;
0653     tdev = tport->tp_tdev;
0654 
0655     /* only one open on any port on a device at a time */
0656     if (mutex_lock_interruptible(&tdev->td_open_close_lock))
0657         return -ERESTARTSYS;
0658 
0659     tport->tp_msr = 0;
0660     tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR);
0661 
0662     /* start interrupt urb the first time a port is opened on this device */
0663     if (tdev->td_open_port_count == 0) {
0664         dev_dbg(&port->dev, "%s - start interrupt in urb\n", __func__);
0665         urb = tdev->td_serial->port[0]->interrupt_in_urb;
0666         if (!urb) {
0667             dev_err(&port->dev, "%s - no interrupt urb\n", __func__);
0668             status = -EINVAL;
0669             goto release_lock;
0670         }
0671         urb->context = tdev;
0672         status = usb_submit_urb(urb, GFP_KERNEL);
0673         if (status) {
0674             dev_err(&port->dev, "%s - submit interrupt urb failed, %d\n", __func__, status);
0675             goto release_lock;
0676         }
0677     }
0678 
0679     if (tty)
0680         ti_set_termios(tty, port, &tty->termios);
0681 
0682     status = ti_port_cmd_out(port, TI_OPEN_PORT, open_settings, NULL, 0);
0683     if (status) {
0684         dev_err(&port->dev, "%s - cannot send open command, %d\n",
0685             __func__, status);
0686         goto unlink_int_urb;
0687     }
0688 
0689     status = ti_port_cmd_out(port, TI_START_PORT, 0, NULL, 0);
0690     if (status) {
0691         dev_err(&port->dev, "%s - cannot send start command, %d\n",
0692                             __func__, status);
0693         goto unlink_int_urb;
0694     }
0695 
0696     status = ti_port_cmd_out(port, TI_PURGE_PORT, TI_PURGE_INPUT, NULL, 0);
0697     if (status) {
0698         dev_err(&port->dev, "%s - cannot clear input buffers, %d\n",
0699                             __func__, status);
0700         goto unlink_int_urb;
0701     }
0702     status = ti_port_cmd_out(port, TI_PURGE_PORT, TI_PURGE_OUTPUT, NULL, 0);
0703     if (status) {
0704         dev_err(&port->dev, "%s - cannot clear output buffers, %d\n",
0705                             __func__, status);
0706         goto unlink_int_urb;
0707     }
0708 
0709     /* reset the data toggle on the bulk endpoints to work around bug in
0710      * host controllers where things get out of sync some times */
0711     usb_clear_halt(dev, port->write_urb->pipe);
0712     usb_clear_halt(dev, port->read_urb->pipe);
0713 
0714     if (tty)
0715         ti_set_termios(tty, port, &tty->termios);
0716 
0717     status = ti_port_cmd_out(port, TI_OPEN_PORT, open_settings, NULL, 0);
0718     if (status) {
0719         dev_err(&port->dev, "%s - cannot send open command (2), %d\n",
0720                             __func__, status);
0721         goto unlink_int_urb;
0722     }
0723 
0724     status = ti_port_cmd_out(port, TI_START_PORT, 0, NULL, 0);
0725     if (status) {
0726         dev_err(&port->dev, "%s - cannot send start command (2), %d\n",
0727                             __func__, status);
0728         goto unlink_int_urb;
0729     }
0730 
0731     /* start read urb */
0732     urb = port->read_urb;
0733     if (!urb) {
0734         dev_err(&port->dev, "%s - no read urb\n", __func__);
0735         status = -EINVAL;
0736         goto unlink_int_urb;
0737     }
0738     tport->tp_read_urb_state = TI_READ_URB_RUNNING;
0739     urb->context = tport;
0740     status = usb_submit_urb(urb, GFP_KERNEL);
0741     if (status) {
0742         dev_err(&port->dev, "%s - submit read urb failed, %d\n",
0743                             __func__, status);
0744         goto unlink_int_urb;
0745     }
0746 
0747     tport->tp_is_open = 1;
0748     ++tdev->td_open_port_count;
0749 
0750     goto release_lock;
0751 
0752 unlink_int_urb:
0753     if (tdev->td_open_port_count == 0)
0754         usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
0755 release_lock:
0756     mutex_unlock(&tdev->td_open_close_lock);
0757     return status;
0758 }
0759 
0760 
0761 static void ti_close(struct usb_serial_port *port)
0762 {
0763     struct ti_device *tdev;
0764     struct ti_port *tport;
0765     int status;
0766     unsigned long flags;
0767 
0768     tdev = usb_get_serial_data(port->serial);
0769     tport = usb_get_serial_port_data(port);
0770 
0771     tport->tp_is_open = 0;
0772 
0773     usb_kill_urb(port->read_urb);
0774     usb_kill_urb(port->write_urb);
0775     tport->tp_write_urb_in_use = 0;
0776     spin_lock_irqsave(&tport->tp_lock, flags);
0777     kfifo_reset_out(&port->write_fifo);
0778     spin_unlock_irqrestore(&tport->tp_lock, flags);
0779 
0780     status = ti_port_cmd_out(port, TI_CLOSE_PORT, 0, NULL, 0);
0781     if (status)
0782         dev_err(&port->dev,
0783             "%s - cannot send close port command, %d\n"
0784                             , __func__, status);
0785 
0786     mutex_lock(&tdev->td_open_close_lock);
0787     --tdev->td_open_port_count;
0788     if (tdev->td_open_port_count == 0) {
0789         /* last port is closed, shut down interrupt urb */
0790         usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
0791     }
0792     mutex_unlock(&tdev->td_open_close_lock);
0793 }
0794 
0795 
0796 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
0797             const unsigned char *data, int count)
0798 {
0799     struct ti_port *tport = usb_get_serial_port_data(port);
0800 
0801     if (count == 0) {
0802         return 0;
0803     }
0804 
0805     if (!tport->tp_is_open)
0806         return -ENODEV;
0807 
0808     count = kfifo_in_locked(&port->write_fifo, data, count,
0809                             &tport->tp_lock);
0810     ti_send(tport);
0811 
0812     return count;
0813 }
0814 
0815 
0816 static unsigned int ti_write_room(struct tty_struct *tty)
0817 {
0818     struct usb_serial_port *port = tty->driver_data;
0819     struct ti_port *tport = usb_get_serial_port_data(port);
0820     unsigned int room;
0821     unsigned long flags;
0822 
0823     spin_lock_irqsave(&tport->tp_lock, flags);
0824     room = kfifo_avail(&port->write_fifo);
0825     spin_unlock_irqrestore(&tport->tp_lock, flags);
0826 
0827     dev_dbg(&port->dev, "%s - returns %u\n", __func__, room);
0828     return room;
0829 }
0830 
0831 
0832 static unsigned int ti_chars_in_buffer(struct tty_struct *tty)
0833 {
0834     struct usb_serial_port *port = tty->driver_data;
0835     struct ti_port *tport = usb_get_serial_port_data(port);
0836     unsigned int chars;
0837     unsigned long flags;
0838 
0839     spin_lock_irqsave(&tport->tp_lock, flags);
0840     chars = kfifo_len(&port->write_fifo);
0841     spin_unlock_irqrestore(&tport->tp_lock, flags);
0842 
0843     dev_dbg(&port->dev, "%s - returns %u\n", __func__, chars);
0844     return chars;
0845 }
0846 
0847 static bool ti_tx_empty(struct usb_serial_port *port)
0848 {
0849     struct ti_port *tport = usb_get_serial_port_data(port);
0850     u8 lsr, mask;
0851     int ret;
0852 
0853     /*
0854      * TUSB5052 does not have the TEMT bit to tell if the shift register
0855      * is empty.
0856      */
0857     if (tport->tp_tdev->td_is_3410)
0858         mask = TI_LSR_TX_EMPTY_BOTH;
0859     else
0860         mask = TI_LSR_TX_EMPTY;
0861 
0862     ret = ti_get_lsr(tport, &lsr);
0863     if (!ret && !(lsr & mask))
0864         return false;
0865 
0866     return true;
0867 }
0868 
0869 static void ti_throttle(struct tty_struct *tty)
0870 {
0871     struct usb_serial_port *port = tty->driver_data;
0872     struct ti_port *tport = usb_get_serial_port_data(port);
0873 
0874     if (I_IXOFF(tty) || C_CRTSCTS(tty))
0875         ti_stop_read(tport, tty);
0876 
0877 }
0878 
0879 
0880 static void ti_unthrottle(struct tty_struct *tty)
0881 {
0882     struct usb_serial_port *port = tty->driver_data;
0883     struct ti_port *tport = usb_get_serial_port_data(port);
0884     int status;
0885 
0886     if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
0887         status = ti_restart_read(tport, tty);
0888         if (status)
0889             dev_err(&port->dev, "%s - cannot restart read, %d\n",
0890                             __func__, status);
0891     }
0892 }
0893 
0894 static void ti_set_termios(struct tty_struct *tty,
0895         struct usb_serial_port *port, struct ktermios *old_termios)
0896 {
0897     struct ti_port *tport = usb_get_serial_port_data(port);
0898     struct ti_uart_config *config;
0899     int baud;
0900     int status;
0901     unsigned int mcr;
0902     u16 wbaudrate;
0903     u16 wflags = 0;
0904 
0905     config = kmalloc(sizeof(*config), GFP_KERNEL);
0906     if (!config)
0907         return;
0908 
0909     /* these flags must be set */
0910     wflags |= TI_UART_ENABLE_MS_INTS;
0911     wflags |= TI_UART_ENABLE_AUTO_START_DMA;
0912     config->bUartMode = tport->tp_uart_mode;
0913 
0914     switch (C_CSIZE(tty)) {
0915     case CS5:
0916         config->bDataBits = TI_UART_5_DATA_BITS;
0917         break;
0918     case CS6:
0919         config->bDataBits = TI_UART_6_DATA_BITS;
0920         break;
0921     case CS7:
0922         config->bDataBits = TI_UART_7_DATA_BITS;
0923         break;
0924     default:
0925     case CS8:
0926         config->bDataBits = TI_UART_8_DATA_BITS;
0927         break;
0928     }
0929 
0930     /* CMSPAR isn't supported by this driver */
0931     tty->termios.c_cflag &= ~CMSPAR;
0932 
0933     if (C_PARENB(tty)) {
0934         if (C_PARODD(tty)) {
0935             wflags |= TI_UART_ENABLE_PARITY_CHECKING;
0936             config->bParity = TI_UART_ODD_PARITY;
0937         } else {
0938             wflags |= TI_UART_ENABLE_PARITY_CHECKING;
0939             config->bParity = TI_UART_EVEN_PARITY;
0940         }
0941     } else {
0942         wflags &= ~TI_UART_ENABLE_PARITY_CHECKING;
0943         config->bParity = TI_UART_NO_PARITY;
0944     }
0945 
0946     if (C_CSTOPB(tty))
0947         config->bStopBits = TI_UART_2_STOP_BITS;
0948     else
0949         config->bStopBits = TI_UART_1_STOP_BITS;
0950 
0951     if (C_CRTSCTS(tty)) {
0952         /* RTS flow control must be off to drop RTS for baud rate B0 */
0953         if ((C_BAUD(tty)) != B0)
0954             wflags |= TI_UART_ENABLE_RTS_IN;
0955         wflags |= TI_UART_ENABLE_CTS_OUT;
0956     } else {
0957         ti_restart_read(tport, tty);
0958     }
0959 
0960     if (I_IXOFF(tty) || I_IXON(tty)) {
0961         config->cXon  = START_CHAR(tty);
0962         config->cXoff = STOP_CHAR(tty);
0963 
0964         if (I_IXOFF(tty))
0965             wflags |= TI_UART_ENABLE_X_IN;
0966         else
0967             ti_restart_read(tport, tty);
0968 
0969         if (I_IXON(tty))
0970             wflags |= TI_UART_ENABLE_X_OUT;
0971     }
0972 
0973     baud = tty_get_baud_rate(tty);
0974     if (!baud)
0975         baud = 9600;
0976     if (tport->tp_tdev->td_is_3410)
0977         wbaudrate = (923077 + baud/2) / baud;
0978     else
0979         wbaudrate = (461538 + baud/2) / baud;
0980 
0981     /* FIXME: Should calculate resulting baud here and report it back */
0982     if ((C_BAUD(tty)) != B0)
0983         tty_encode_baud_rate(tty, baud, baud);
0984 
0985     dev_dbg(&port->dev,
0986         "%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d\n",
0987         __func__, baud, wbaudrate, wflags,
0988         config->bDataBits, config->bParity, config->bStopBits,
0989         config->cXon, config->cXoff, config->bUartMode);
0990 
0991     config->wBaudRate = cpu_to_be16(wbaudrate);
0992     config->wFlags = cpu_to_be16(wflags);
0993 
0994     status = ti_port_cmd_out(port, TI_SET_CONFIG, 0, config,
0995             sizeof(*config));
0996     if (status)
0997         dev_err(&port->dev, "%s - cannot set config on port %d, %d\n",
0998                 __func__, port->port_number, status);
0999 
1000     /* SET_CONFIG asserts RTS and DTR, reset them correctly */
1001     mcr = tport->tp_shadow_mcr;
1002     /* if baud rate is B0, clear RTS and DTR */
1003     if (C_BAUD(tty) == B0)
1004         mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
1005     status = ti_set_mcr(tport, mcr);
1006     if (status)
1007         dev_err(&port->dev, "%s - cannot set modem control on port %d, %d\n",
1008                 __func__, port->port_number, status);
1009 
1010     kfree(config);
1011 }
1012 
1013 
1014 static int ti_tiocmget(struct tty_struct *tty)
1015 {
1016     struct usb_serial_port *port = tty->driver_data;
1017     struct ti_port *tport = usb_get_serial_port_data(port);
1018     unsigned int result;
1019     unsigned int msr;
1020     unsigned int mcr;
1021     unsigned long flags;
1022 
1023     spin_lock_irqsave(&tport->tp_lock, flags);
1024     msr = tport->tp_msr;
1025     mcr = tport->tp_shadow_mcr;
1026     spin_unlock_irqrestore(&tport->tp_lock, flags);
1027 
1028     result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
1029         | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
1030         | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
1031         | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
1032         | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
1033         | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
1034         | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
1035 
1036     dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
1037 
1038     return result;
1039 }
1040 
1041 
1042 static int ti_tiocmset(struct tty_struct *tty,
1043                 unsigned int set, unsigned int clear)
1044 {
1045     struct usb_serial_port *port = tty->driver_data;
1046     struct ti_port *tport = usb_get_serial_port_data(port);
1047     unsigned int mcr;
1048     unsigned long flags;
1049 
1050     spin_lock_irqsave(&tport->tp_lock, flags);
1051     mcr = tport->tp_shadow_mcr;
1052 
1053     if (set & TIOCM_RTS)
1054         mcr |= TI_MCR_RTS;
1055     if (set & TIOCM_DTR)
1056         mcr |= TI_MCR_DTR;
1057     if (set & TIOCM_LOOP)
1058         mcr |= TI_MCR_LOOP;
1059 
1060     if (clear & TIOCM_RTS)
1061         mcr &= ~TI_MCR_RTS;
1062     if (clear & TIOCM_DTR)
1063         mcr &= ~TI_MCR_DTR;
1064     if (clear & TIOCM_LOOP)
1065         mcr &= ~TI_MCR_LOOP;
1066     spin_unlock_irqrestore(&tport->tp_lock, flags);
1067 
1068     return ti_set_mcr(tport, mcr);
1069 }
1070 
1071 
1072 static void ti_break(struct tty_struct *tty, int break_state)
1073 {
1074     struct usb_serial_port *port = tty->driver_data;
1075     struct ti_port *tport = usb_get_serial_port_data(port);
1076     int status;
1077 
1078     dev_dbg(&port->dev, "%s - state = %d\n", __func__, break_state);
1079 
1080     status = ti_write_byte(port, tport->tp_tdev,
1081         tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
1082         TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
1083 
1084     if (status)
1085         dev_dbg(&port->dev, "%s - error setting break, %d\n", __func__, status);
1086 }
1087 
1088 static int ti_get_port_from_code(unsigned char code)
1089 {
1090     return (code >> 6) & 0x01;
1091 }
1092 
1093 static int ti_get_func_from_code(unsigned char code)
1094 {
1095     return code & 0x0f;
1096 }
1097 
1098 static void ti_interrupt_callback(struct urb *urb)
1099 {
1100     struct ti_device *tdev = urb->context;
1101     struct usb_serial_port *port;
1102     struct usb_serial *serial = tdev->td_serial;
1103     struct ti_port *tport;
1104     struct device *dev = &urb->dev->dev;
1105     unsigned char *data = urb->transfer_buffer;
1106     int length = urb->actual_length;
1107     int port_number;
1108     int function;
1109     int status = urb->status;
1110     int retval;
1111     u8 msr;
1112 
1113     switch (status) {
1114     case 0:
1115         break;
1116     case -ECONNRESET:
1117     case -ENOENT:
1118     case -ESHUTDOWN:
1119         dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
1120         return;
1121     default:
1122         dev_err(dev, "%s - nonzero urb status, %d\n", __func__, status);
1123         goto exit;
1124     }
1125 
1126     if (length != 2) {
1127         dev_dbg(dev, "%s - bad packet size, %d\n", __func__, length);
1128         goto exit;
1129     }
1130 
1131     if (data[0] == TI_CODE_HARDWARE_ERROR) {
1132         dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]);
1133         goto exit;
1134     }
1135 
1136     port_number = ti_get_port_from_code(data[0]);
1137     function = ti_get_func_from_code(data[0]);
1138 
1139     dev_dbg(dev, "%s - port_number %d, function %d, data 0x%02X\n",
1140         __func__, port_number, function, data[1]);
1141 
1142     if (port_number >= serial->num_ports) {
1143         dev_err(dev, "%s - bad port number, %d\n",
1144                         __func__, port_number);
1145         goto exit;
1146     }
1147 
1148     port = serial->port[port_number];
1149 
1150     tport = usb_get_serial_port_data(port);
1151     if (!tport)
1152         goto exit;
1153 
1154     switch (function) {
1155     case TI_CODE_DATA_ERROR:
1156         dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n",
1157             __func__, port_number, data[1]);
1158         break;
1159 
1160     case TI_CODE_MODEM_STATUS:
1161         msr = data[1];
1162         dev_dbg(dev, "%s - port %d, msr 0x%02X\n", __func__, port_number, msr);
1163         ti_handle_new_msr(tport, msr);
1164         break;
1165 
1166     default:
1167         dev_err(dev, "%s - unknown interrupt code, 0x%02X\n",
1168                             __func__, data[1]);
1169         break;
1170     }
1171 
1172 exit:
1173     retval = usb_submit_urb(urb, GFP_ATOMIC);
1174     if (retval)
1175         dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
1176             __func__, retval);
1177 }
1178 
1179 
1180 static void ti_bulk_in_callback(struct urb *urb)
1181 {
1182     struct ti_port *tport = urb->context;
1183     struct usb_serial_port *port = tport->tp_port;
1184     struct device *dev = &urb->dev->dev;
1185     int status = urb->status;
1186     unsigned long flags;
1187     int retval = 0;
1188 
1189     switch (status) {
1190     case 0:
1191         break;
1192     case -ECONNRESET:
1193     case -ENOENT:
1194     case -ESHUTDOWN:
1195         dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
1196         return;
1197     default:
1198         dev_err(dev, "%s - nonzero urb status, %d\n",
1199             __func__, status);
1200     }
1201 
1202     if (status == -EPIPE)
1203         goto exit;
1204 
1205     if (status) {
1206         dev_err(dev, "%s - stopping read!\n", __func__);
1207         return;
1208     }
1209 
1210     if (urb->actual_length) {
1211         usb_serial_debug_data(dev, __func__, urb->actual_length,
1212                       urb->transfer_buffer);
1213 
1214         if (!tport->tp_is_open)
1215             dev_dbg(dev, "%s - port closed, dropping data\n",
1216                 __func__);
1217         else
1218             ti_recv(port, urb->transfer_buffer, urb->actual_length);
1219         spin_lock_irqsave(&tport->tp_lock, flags);
1220         port->icount.rx += urb->actual_length;
1221         spin_unlock_irqrestore(&tport->tp_lock, flags);
1222     }
1223 
1224 exit:
1225     /* continue to read unless stopping */
1226     spin_lock_irqsave(&tport->tp_lock, flags);
1227     if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1228         retval = usb_submit_urb(urb, GFP_ATOMIC);
1229     else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING)
1230         tport->tp_read_urb_state = TI_READ_URB_STOPPED;
1231 
1232     spin_unlock_irqrestore(&tport->tp_lock, flags);
1233     if (retval)
1234         dev_err(dev, "%s - resubmit read urb failed, %d\n",
1235             __func__, retval);
1236 }
1237 
1238 
1239 static void ti_bulk_out_callback(struct urb *urb)
1240 {
1241     struct ti_port *tport = urb->context;
1242     struct usb_serial_port *port = tport->tp_port;
1243     int status = urb->status;
1244 
1245     tport->tp_write_urb_in_use = 0;
1246 
1247     switch (status) {
1248     case 0:
1249         break;
1250     case -ECONNRESET:
1251     case -ENOENT:
1252     case -ESHUTDOWN:
1253         dev_dbg(&port->dev, "%s - urb shutting down, %d\n", __func__, status);
1254         return;
1255     default:
1256         dev_err_console(port, "%s - nonzero urb status, %d\n",
1257             __func__, status);
1258     }
1259 
1260     /* send any buffered data */
1261     ti_send(tport);
1262 }
1263 
1264 
1265 static void ti_recv(struct usb_serial_port *port, unsigned char *data,
1266         int length)
1267 {
1268     int cnt;
1269 
1270     do {
1271         cnt = tty_insert_flip_string(&port->port, data, length);
1272         if (cnt < length) {
1273             dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
1274                         __func__, length - cnt);
1275             if (cnt == 0)
1276                 break;
1277         }
1278         tty_flip_buffer_push(&port->port);
1279         data += cnt;
1280         length -= cnt;
1281     } while (length > 0);
1282 }
1283 
1284 
1285 static void ti_send(struct ti_port *tport)
1286 {
1287     int count, result;
1288     struct usb_serial_port *port = tport->tp_port;
1289     unsigned long flags;
1290 
1291     spin_lock_irqsave(&tport->tp_lock, flags);
1292 
1293     if (tport->tp_write_urb_in_use)
1294         goto unlock;
1295 
1296     count = kfifo_out(&port->write_fifo,
1297                 port->write_urb->transfer_buffer,
1298                 port->bulk_out_size);
1299 
1300     if (count == 0)
1301         goto unlock;
1302 
1303     tport->tp_write_urb_in_use = 1;
1304 
1305     spin_unlock_irqrestore(&tport->tp_lock, flags);
1306 
1307     usb_serial_debug_data(&port->dev, __func__, count,
1308                   port->write_urb->transfer_buffer);
1309 
1310     usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1311                usb_sndbulkpipe(port->serial->dev,
1312                         port->bulk_out_endpointAddress),
1313                port->write_urb->transfer_buffer, count,
1314                ti_bulk_out_callback, tport);
1315 
1316     result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1317     if (result) {
1318         dev_err_console(port, "%s - submit write urb failed, %d\n",
1319                             __func__, result);
1320         tport->tp_write_urb_in_use = 0;
1321         /* TODO: reschedule ti_send */
1322     } else {
1323         spin_lock_irqsave(&tport->tp_lock, flags);
1324         port->icount.tx += count;
1325         spin_unlock_irqrestore(&tport->tp_lock, flags);
1326     }
1327 
1328     /* more room in the buffer for new writes, wakeup */
1329     tty_port_tty_wakeup(&port->port);
1330 
1331     return;
1332 unlock:
1333     spin_unlock_irqrestore(&tport->tp_lock, flags);
1334     return;
1335 }
1336 
1337 
1338 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
1339 {
1340     unsigned long flags;
1341     int status;
1342 
1343     status = ti_write_byte(tport->tp_port, tport->tp_tdev,
1344         tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
1345         TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
1346 
1347     spin_lock_irqsave(&tport->tp_lock, flags);
1348     if (!status)
1349         tport->tp_shadow_mcr = mcr;
1350     spin_unlock_irqrestore(&tport->tp_lock, flags);
1351 
1352     return status;
1353 }
1354 
1355 
1356 static int ti_get_lsr(struct ti_port *tport, u8 *lsr)
1357 {
1358     int size, status;
1359     struct usb_serial_port *port = tport->tp_port;
1360     struct ti_port_status *data;
1361 
1362     size = sizeof(struct ti_port_status);
1363     data = kmalloc(size, GFP_KERNEL);
1364     if (!data)
1365         return -ENOMEM;
1366 
1367     status = ti_port_cmd_in(port, TI_GET_PORT_STATUS, 0, data, size);
1368     if (status) {
1369         dev_err(&port->dev,
1370             "%s - get port status command failed, %d\n",
1371                             __func__, status);
1372         goto free_data;
1373     }
1374 
1375     dev_dbg(&port->dev, "%s - lsr 0x%02X\n", __func__, data->bLSR);
1376 
1377     *lsr = data->bLSR;
1378 
1379 free_data:
1380     kfree(data);
1381     return status;
1382 }
1383 
1384 
1385 static void ti_get_serial_info(struct tty_struct *tty, struct serial_struct *ss)
1386 {
1387     struct usb_serial_port *port = tty->driver_data;
1388     struct ti_port *tport = usb_get_serial_port_data(port);
1389 
1390     ss->baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
1391 }
1392 
1393 
1394 static void ti_handle_new_msr(struct ti_port *tport, u8 msr)
1395 {
1396     struct async_icount *icount;
1397     struct tty_struct *tty;
1398     unsigned long flags;
1399 
1400     dev_dbg(&tport->tp_port->dev, "%s - msr 0x%02X\n", __func__, msr);
1401 
1402     if (msr & TI_MSR_DELTA_MASK) {
1403         spin_lock_irqsave(&tport->tp_lock, flags);
1404         icount = &tport->tp_port->icount;
1405         if (msr & TI_MSR_DELTA_CTS)
1406             icount->cts++;
1407         if (msr & TI_MSR_DELTA_DSR)
1408             icount->dsr++;
1409         if (msr & TI_MSR_DELTA_CD)
1410             icount->dcd++;
1411         if (msr & TI_MSR_DELTA_RI)
1412             icount->rng++;
1413         wake_up_interruptible(&tport->tp_port->port.delta_msr_wait);
1414         spin_unlock_irqrestore(&tport->tp_lock, flags);
1415     }
1416 
1417     tport->tp_msr = msr & TI_MSR_MASK;
1418 
1419     /* handle CTS flow control */
1420     tty = tty_port_tty_get(&tport->tp_port->port);
1421     if (tty && C_CRTSCTS(tty)) {
1422         if (msr & TI_MSR_CTS)
1423             tty_wakeup(tty);
1424     }
1425     tty_kref_put(tty);
1426 }
1427 
1428 
1429 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
1430 {
1431     unsigned long flags;
1432 
1433     spin_lock_irqsave(&tport->tp_lock, flags);
1434 
1435     if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1436         tport->tp_read_urb_state = TI_READ_URB_STOPPING;
1437 
1438     spin_unlock_irqrestore(&tport->tp_lock, flags);
1439 }
1440 
1441 
1442 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
1443 {
1444     struct urb *urb;
1445     int status = 0;
1446     unsigned long flags;
1447 
1448     spin_lock_irqsave(&tport->tp_lock, flags);
1449 
1450     if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
1451         tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1452         urb = tport->tp_port->read_urb;
1453         spin_unlock_irqrestore(&tport->tp_lock, flags);
1454         urb->context = tport;
1455         status = usb_submit_urb(urb, GFP_KERNEL);
1456     } else  {
1457         tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1458         spin_unlock_irqrestore(&tport->tp_lock, flags);
1459     }
1460 
1461     return status;
1462 }
1463 
1464 static int ti_command_out_sync(struct usb_device *udev, u8 command,
1465         u16 moduleid, u16 value, void *data, int size)
1466 {
1467     int status;
1468 
1469     status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), command,
1470             USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
1471             value, moduleid, data, size, 1000);
1472     if (status < 0)
1473         return status;
1474 
1475     return 0;
1476 }
1477 
1478 static int ti_command_in_sync(struct usb_device *udev, u8 command,
1479         u16 moduleid, u16 value, void *data, int size)
1480 {
1481     int status;
1482 
1483     status = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), command,
1484             USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
1485             value, moduleid, data, size, 1000);
1486     if (status == size)
1487         status = 0;
1488     else if (status >= 0)
1489         status = -ECOMM;
1490 
1491     return status;
1492 }
1493 
1494 static int ti_port_cmd_out(struct usb_serial_port *port, u8 command,
1495         u16 value, void *data, int size)
1496 {
1497     return ti_command_out_sync(port->serial->dev, command,
1498             TI_UART1_PORT + port->port_number,
1499             value, data, size);
1500 }
1501 
1502 static int ti_port_cmd_in(struct usb_serial_port *port, u8 command,
1503         u16 value, void *data, int size)
1504 {
1505     return ti_command_in_sync(port->serial->dev, command,
1506             TI_UART1_PORT + port->port_number,
1507             value, data, size);
1508 }
1509 
1510 static int ti_write_byte(struct usb_serial_port *port,
1511              struct ti_device *tdev, unsigned long addr,
1512              u8 mask, u8 byte)
1513 {
1514     int status;
1515     unsigned int size;
1516     struct ti_write_data_bytes *data;
1517 
1518     dev_dbg(&port->dev, "%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X\n", __func__,
1519         addr, mask, byte);
1520 
1521     size = sizeof(struct ti_write_data_bytes) + 2;
1522     data = kmalloc(size, GFP_KERNEL);
1523     if (!data)
1524         return -ENOMEM;
1525 
1526     data->bAddrType = TI_RW_DATA_ADDR_XDATA;
1527     data->bDataType = TI_RW_DATA_BYTE;
1528     data->bDataCounter = 1;
1529     data->wBaseAddrHi = cpu_to_be16(addr>>16);
1530     data->wBaseAddrLo = cpu_to_be16(addr);
1531     data->bData[0] = mask;
1532     data->bData[1] = byte;
1533 
1534     status = ti_command_out_sync(port->serial->dev, TI_WRITE_DATA,
1535             TI_RAM_PORT, 0, data, size);
1536     if (status < 0)
1537         dev_err(&port->dev, "%s - failed, %d\n", __func__, status);
1538 
1539     kfree(data);
1540 
1541     return status;
1542 }
1543 
1544 static int ti_do_download(struct usb_device *dev, int pipe,
1545                         u8 *buffer, int size)
1546 {
1547     int pos;
1548     u8 cs = 0;
1549     int done;
1550     struct ti_firmware_header *header;
1551     int status = 0;
1552     int len;
1553 
1554     for (pos = sizeof(struct ti_firmware_header); pos < size; pos++)
1555         cs = (u8)(cs + buffer[pos]);
1556 
1557     header = (struct ti_firmware_header *)buffer;
1558     header->wLength = cpu_to_le16(size - sizeof(*header));
1559     header->bCheckSum = cs;
1560 
1561     dev_dbg(&dev->dev, "%s - downloading firmware\n", __func__);
1562     for (pos = 0; pos < size; pos += done) {
1563         len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
1564         status = usb_bulk_msg(dev, pipe, buffer + pos, len,
1565                                 &done, 1000);
1566         if (status)
1567             break;
1568     }
1569     return status;
1570 }
1571 
1572 static int ti_download_firmware(struct ti_device *tdev)
1573 {
1574     int status;
1575     int buffer_size;
1576     u8 *buffer;
1577     struct usb_device *dev = tdev->td_serial->dev;
1578     unsigned int pipe = usb_sndbulkpipe(dev,
1579         tdev->td_serial->port[0]->bulk_out_endpointAddress);
1580     const struct firmware *fw_p;
1581     char buf[32];
1582 
1583     if (le16_to_cpu(dev->descriptor.idVendor) == MXU1_VENDOR_ID) {
1584         snprintf(buf,
1585             sizeof(buf),
1586             "moxa/moxa-%04x.fw",
1587             le16_to_cpu(dev->descriptor.idProduct));
1588 
1589         status = request_firmware(&fw_p, buf, &dev->dev);
1590         goto check_firmware;
1591     }
1592 
1593     /* try ID specific firmware first, then try generic firmware */
1594     sprintf(buf, "ti_usb-v%04x-p%04x.fw",
1595             le16_to_cpu(dev->descriptor.idVendor),
1596             le16_to_cpu(dev->descriptor.idProduct));
1597     status = request_firmware(&fw_p, buf, &dev->dev);
1598 
1599     if (status != 0) {
1600         buf[0] = '\0';
1601         if (le16_to_cpu(dev->descriptor.idVendor) == MTS_VENDOR_ID) {
1602             switch (le16_to_cpu(dev->descriptor.idProduct)) {
1603             case MTS_CDMA_PRODUCT_ID:
1604                 strcpy(buf, "mts_cdma.fw");
1605                 break;
1606             case MTS_GSM_PRODUCT_ID:
1607                 strcpy(buf, "mts_gsm.fw");
1608                 break;
1609             case MTS_EDGE_PRODUCT_ID:
1610                 strcpy(buf, "mts_edge.fw");
1611                 break;
1612             case MTS_MT9234MU_PRODUCT_ID:
1613                 strcpy(buf, "mts_mt9234mu.fw");
1614                 break;
1615             case MTS_MT9234ZBA_PRODUCT_ID:
1616                 strcpy(buf, "mts_mt9234zba.fw");
1617                 break;
1618             case MTS_MT9234ZBAOLD_PRODUCT_ID:
1619                 strcpy(buf, "mts_mt9234zba.fw");
1620                 break;          }
1621         }
1622         if (buf[0] == '\0') {
1623             if (tdev->td_is_3410)
1624                 strcpy(buf, "ti_3410.fw");
1625             else
1626                 strcpy(buf, "ti_5052.fw");
1627         }
1628         status = request_firmware(&fw_p, buf, &dev->dev);
1629     }
1630 
1631 check_firmware:
1632     if (status) {
1633         dev_err(&dev->dev, "%s - firmware not found\n", __func__);
1634         return -ENOENT;
1635     }
1636     if (fw_p->size > TI_FIRMWARE_BUF_SIZE) {
1637         dev_err(&dev->dev, "%s - firmware too large %zu\n", __func__, fw_p->size);
1638         release_firmware(fw_p);
1639         return -ENOENT;
1640     }
1641 
1642     buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
1643     buffer = kmalloc(buffer_size, GFP_KERNEL);
1644     if (buffer) {
1645         memcpy(buffer, fw_p->data, fw_p->size);
1646         memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
1647         status = ti_do_download(dev, pipe, buffer, fw_p->size);
1648         kfree(buffer);
1649     } else {
1650         status = -ENOMEM;
1651     }
1652     release_firmware(fw_p);
1653     if (status) {
1654         dev_err(&dev->dev, "%s - error downloading firmware, %d\n",
1655                             __func__, status);
1656         return status;
1657     }
1658 
1659     dev_dbg(&dev->dev, "%s - download successful\n", __func__);
1660 
1661     return 0;
1662 }