Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* vi: ts=8 sw=8 |
| 2 | * |
| 3 | * TI 3410/5052 USB Serial Driver |
| 4 | * |
| 5 | * Copyright (C) 2004 Texas Instruments |
| 6 | * |
| 7 | * This driver is based on the Linux io_ti driver, which is |
| 8 | * Copyright (C) 2000-2002 Inside Out Networks |
| 9 | * Copyright (C) 2001-2002 Greg Kroah-Hartman |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * For questions or problems with this driver, contact Texas Instruments |
| 17 | * technical support, or Al Borchers <alborchers@steinerpoint.com>, or |
| 18 | * Peter Berger <pberger@brimson.com>. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | */ |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/kernel.h> |
| 22 | #include <linux/errno.h> |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 23 | #include <linux/firmware.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/init.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/tty.h> |
| 27 | #include <linux/tty_driver.h> |
| 28 | #include <linux/tty_flip.h> |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/spinlock.h> |
| 31 | #include <linux/ioctl.h> |
| 32 | #include <linux/serial.h> |
| 33 | #include <linux/circ_buf.h> |
Matthias Kaehlcke | 9da0068 | 2007-11-21 15:13:16 -0800 | [diff] [blame] | 34 | #include <linux/mutex.h> |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 35 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/usb.h> |
Greg Kroah-Hartman | a969888 | 2006-07-11 21:22:58 -0700 | [diff] [blame] | 37 | #include <linux/usb/serial.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include "ti_usb_3410_5052.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | /* Defines */ |
| 42 | |
| 43 | #define TI_DRIVER_VERSION "v0.9" |
| 44 | #define TI_DRIVER_AUTHOR "Al Borchers <alborchers@steinerpoint.com>" |
| 45 | #define TI_DRIVER_DESC "TI USB 3410/5052 Serial Driver" |
| 46 | |
| 47 | #define TI_FIRMWARE_BUF_SIZE 16284 |
| 48 | |
| 49 | #define TI_WRITE_BUF_SIZE 1024 |
| 50 | |
| 51 | #define TI_TRANSFER_TIMEOUT 2 |
| 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #define TI_DEFAULT_CLOSING_WAIT 4000 /* in .01 secs */ |
| 54 | |
| 55 | /* supported setserial flags */ |
Oliver Neukum | 2400a2b | 2009-04-20 17:28:53 +0200 | [diff] [blame] | 56 | #define TI_SET_SERIAL_FLAGS 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | /* read urb states */ |
| 59 | #define TI_READ_URB_RUNNING 0 |
| 60 | #define TI_READ_URB_STOPPING 1 |
| 61 | #define TI_READ_URB_STOPPED 2 |
| 62 | |
| 63 | #define TI_EXTRA_VID_PID_COUNT 5 |
| 64 | |
| 65 | |
| 66 | /* Structures */ |
| 67 | |
| 68 | struct ti_port { |
| 69 | int tp_is_open; |
| 70 | __u8 tp_msr; |
| 71 | __u8 tp_lsr; |
| 72 | __u8 tp_shadow_mcr; |
| 73 | __u8 tp_uart_mode; /* 232 or 485 modes */ |
| 74 | unsigned int tp_uart_base_addr; |
| 75 | int tp_flags; |
| 76 | int tp_closing_wait;/* in .01 secs */ |
| 77 | struct async_icount tp_icount; |
| 78 | wait_queue_head_t tp_msr_wait; /* wait for msr change */ |
| 79 | wait_queue_head_t tp_write_wait; |
| 80 | struct ti_device *tp_tdev; |
| 81 | struct usb_serial_port *tp_port; |
| 82 | spinlock_t tp_lock; |
| 83 | int tp_read_urb_state; |
| 84 | int tp_write_urb_in_use; |
| 85 | struct circ_buf *tp_write_buf; |
| 86 | }; |
| 87 | |
| 88 | struct ti_device { |
Matthias Kaehlcke | 9da0068 | 2007-11-21 15:13:16 -0800 | [diff] [blame] | 89 | struct mutex td_open_close_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | int td_open_port_count; |
| 91 | struct usb_serial *td_serial; |
| 92 | int td_is_3410; |
| 93 | int td_urb_error; |
| 94 | }; |
| 95 | |
| 96 | |
| 97 | /* Function Declarations */ |
| 98 | |
| 99 | static int ti_startup(struct usb_serial *serial); |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 100 | static void ti_release(struct usb_serial *serial); |
Alan Cox | a509a7e | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 101 | static int ti_open(struct tty_struct *tty, struct usb_serial_port *port); |
Alan Cox | 335f851 | 2009-06-11 12:26:29 +0100 | [diff] [blame] | 102 | static void ti_close(struct usb_serial_port *port); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 103 | static int ti_write(struct tty_struct *tty, struct usb_serial_port *port, |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 104 | const unsigned char *data, int count); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 105 | static int ti_write_room(struct tty_struct *tty); |
| 106 | static int ti_chars_in_buffer(struct tty_struct *tty); |
| 107 | static void ti_throttle(struct tty_struct *tty); |
| 108 | static void ti_unthrottle(struct tty_struct *tty); |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 109 | static int ti_ioctl(struct tty_struct *tty, struct file *file, |
| 110 | unsigned int cmd, unsigned long arg); |
| 111 | static void ti_set_termios(struct tty_struct *tty, |
| 112 | struct usb_serial_port *port, struct ktermios *old_termios); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 113 | static int ti_tiocmget(struct tty_struct *tty, struct file *file); |
| 114 | static int ti_tiocmset(struct tty_struct *tty, struct file *file, |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 115 | unsigned int set, unsigned int clear); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 116 | static void ti_break(struct tty_struct *tty, int break_state); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 117 | static void ti_interrupt_callback(struct urb *urb); |
| 118 | static void ti_bulk_in_callback(struct urb *urb); |
| 119 | static void ti_bulk_out_callback(struct urb *urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
| 121 | static void ti_recv(struct device *dev, struct tty_struct *tty, |
| 122 | unsigned char *data, int length); |
| 123 | static void ti_send(struct ti_port *tport); |
| 124 | static int ti_set_mcr(struct ti_port *tport, unsigned int mcr); |
| 125 | static int ti_get_lsr(struct ti_port *tport); |
| 126 | static int ti_get_serial_info(struct ti_port *tport, |
| 127 | struct serial_struct __user *ret_arg); |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 128 | static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | struct serial_struct __user *new_arg); |
| 130 | static void ti_handle_new_msr(struct ti_port *tport, __u8 msr); |
| 131 | |
| 132 | static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush); |
| 133 | |
| 134 | static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty); |
| 135 | static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty); |
| 136 | |
| 137 | static int ti_command_out_sync(struct ti_device *tdev, __u8 command, |
| 138 | __u16 moduleid, __u16 value, __u8 *data, int size); |
| 139 | static int ti_command_in_sync(struct ti_device *tdev, __u8 command, |
| 140 | __u16 moduleid, __u16 value, __u8 *data, int size); |
| 141 | |
| 142 | static int ti_write_byte(struct ti_device *tdev, unsigned long addr, |
| 143 | __u8 mask, __u8 byte); |
| 144 | |
Chris Adams | 05a3d90 | 2009-01-11 19:48:53 +0000 | [diff] [blame] | 145 | static int ti_download_firmware(struct ti_device *tdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | /* circular buffer */ |
| 148 | static struct circ_buf *ti_buf_alloc(void); |
| 149 | static void ti_buf_free(struct circ_buf *cb); |
| 150 | static void ti_buf_clear(struct circ_buf *cb); |
| 151 | static int ti_buf_data_avail(struct circ_buf *cb); |
| 152 | static int ti_buf_space_avail(struct circ_buf *cb); |
| 153 | static int ti_buf_put(struct circ_buf *cb, const char *buf, int count); |
| 154 | static int ti_buf_get(struct circ_buf *cb, char *buf, int count); |
| 155 | |
| 156 | |
| 157 | /* Data */ |
| 158 | |
| 159 | /* module parameters */ |
| 160 | static int debug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | static int closing_wait = TI_DEFAULT_CLOSING_WAIT; |
| 162 | static ushort vendor_3410[TI_EXTRA_VID_PID_COUNT]; |
Al Viro | 64a6f95 | 2007-10-14 19:35:30 +0100 | [diff] [blame] | 163 | static unsigned int vendor_3410_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | static ushort product_3410[TI_EXTRA_VID_PID_COUNT]; |
Al Viro | 64a6f95 | 2007-10-14 19:35:30 +0100 | [diff] [blame] | 165 | static unsigned int product_3410_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | static ushort vendor_5052[TI_EXTRA_VID_PID_COUNT]; |
Al Viro | 64a6f95 | 2007-10-14 19:35:30 +0100 | [diff] [blame] | 167 | static unsigned int vendor_5052_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | static ushort product_5052[TI_EXTRA_VID_PID_COUNT]; |
Al Viro | 64a6f95 | 2007-10-14 19:35:30 +0100 | [diff] [blame] | 169 | static unsigned int product_5052_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
| 171 | /* supported devices */ |
| 172 | /* the array dimension is the number of default entries plus */ |
| 173 | /* TI_EXTRA_VID_PID_COUNT user defined entries plus 1 terminating */ |
| 174 | /* null entry */ |
Oliver Neukum | 97dcf04 | 2009-02-04 16:38:33 +0100 | [diff] [blame] | 175 | static struct usb_device_id ti_id_table_3410[10+TI_EXTRA_VID_PID_COUNT+1] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) }, |
Oleg Verych | 1f54a6a | 2006-11-17 08:21:27 +0000 | [diff] [blame] | 177 | { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) }, |
Chris Adams | cb7a7c6 | 2009-01-11 19:49:00 +0000 | [diff] [blame] | 178 | { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) }, |
| 179 | { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) }, |
| 180 | { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) }, |
| 181 | { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) }, |
| 182 | { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) }, |
Oliver Neukum | 1a1fab51 | 2009-01-12 13:31:16 +0100 | [diff] [blame] | 183 | { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) }, |
Oliver Neukum | 97dcf04 | 2009-02-04 16:38:33 +0100 | [diff] [blame] | 184 | { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) }, |
| 185 | { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | }; |
| 187 | |
Oliver Neukum | 97dcf04 | 2009-02-04 16:38:33 +0100 | [diff] [blame] | 188 | static struct usb_device_id ti_id_table_5052[5+TI_EXTRA_VID_PID_COUNT+1] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) }, |
| 190 | { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) }, |
| 191 | { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) }, |
| 192 | { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) }, |
| 193 | }; |
| 194 | |
Oliver Neukum | 97dcf04 | 2009-02-04 16:38:33 +0100 | [diff] [blame] | 195 | static struct usb_device_id ti_id_table_combined[14+2*TI_EXTRA_VID_PID_COUNT+1] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) }, |
Oleg Verych | 1f54a6a | 2006-11-17 08:21:27 +0000 | [diff] [blame] | 197 | { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) }, |
Chris Adams | cb7a7c6 | 2009-01-11 19:49:00 +0000 | [diff] [blame] | 198 | { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) }, |
| 199 | { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) }, |
| 200 | { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) }, |
| 201 | { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) }, |
| 202 | { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) }, |
| 204 | { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) }, |
| 205 | { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) }, |
| 206 | { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) }, |
Oliver Neukum | 1a1fab51 | 2009-01-12 13:31:16 +0100 | [diff] [blame] | 207 | { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) }, |
Oliver Neukum | 97dcf04 | 2009-02-04 16:38:33 +0100 | [diff] [blame] | 208 | { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) }, |
| 209 | { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | { } |
| 211 | }; |
| 212 | |
| 213 | static struct usb_driver ti_usb_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | .name = "ti_usb_3410_5052", |
| 215 | .probe = usb_serial_probe, |
| 216 | .disconnect = usb_serial_disconnect, |
| 217 | .id_table = ti_id_table_combined, |
Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 218 | .no_dynamic_id = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | }; |
| 220 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 221 | static struct usb_serial_driver ti_1port_device = { |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 222 | .driver = { |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 223 | .owner = THIS_MODULE, |
| 224 | .name = "ti_usb_3410_5052_1", |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 225 | }, |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 226 | .description = "TI USB 3410 1 port adapter", |
Johannes Hölzl | d9b1b78 | 2006-12-17 21:50:24 +0100 | [diff] [blame] | 227 | .usb_driver = &ti_usb_driver, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | .id_table = ti_id_table_3410, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | .num_ports = 1, |
| 230 | .attach = ti_startup, |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 231 | .release = ti_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | .open = ti_open, |
| 233 | .close = ti_close, |
| 234 | .write = ti_write, |
| 235 | .write_room = ti_write_room, |
| 236 | .chars_in_buffer = ti_chars_in_buffer, |
| 237 | .throttle = ti_throttle, |
| 238 | .unthrottle = ti_unthrottle, |
| 239 | .ioctl = ti_ioctl, |
| 240 | .set_termios = ti_set_termios, |
| 241 | .tiocmget = ti_tiocmget, |
| 242 | .tiocmset = ti_tiocmset, |
| 243 | .break_ctl = ti_break, |
| 244 | .read_int_callback = ti_interrupt_callback, |
| 245 | .read_bulk_callback = ti_bulk_in_callback, |
| 246 | .write_bulk_callback = ti_bulk_out_callback, |
| 247 | }; |
| 248 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 249 | static struct usb_serial_driver ti_2port_device = { |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 250 | .driver = { |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 251 | .owner = THIS_MODULE, |
| 252 | .name = "ti_usb_3410_5052_2", |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 253 | }, |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 254 | .description = "TI USB 5052 2 port adapter", |
Johannes Hölzl | d9b1b78 | 2006-12-17 21:50:24 +0100 | [diff] [blame] | 255 | .usb_driver = &ti_usb_driver, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | .id_table = ti_id_table_5052, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | .num_ports = 2, |
| 258 | .attach = ti_startup, |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 259 | .release = ti_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | .open = ti_open, |
| 261 | .close = ti_close, |
| 262 | .write = ti_write, |
| 263 | .write_room = ti_write_room, |
| 264 | .chars_in_buffer = ti_chars_in_buffer, |
| 265 | .throttle = ti_throttle, |
| 266 | .unthrottle = ti_unthrottle, |
| 267 | .ioctl = ti_ioctl, |
| 268 | .set_termios = ti_set_termios, |
| 269 | .tiocmget = ti_tiocmget, |
| 270 | .tiocmset = ti_tiocmset, |
| 271 | .break_ctl = ti_break, |
| 272 | .read_int_callback = ti_interrupt_callback, |
| 273 | .read_bulk_callback = ti_bulk_in_callback, |
| 274 | .write_bulk_callback = ti_bulk_out_callback, |
| 275 | }; |
| 276 | |
| 277 | |
| 278 | /* Module */ |
| 279 | |
| 280 | MODULE_AUTHOR(TI_DRIVER_AUTHOR); |
| 281 | MODULE_DESCRIPTION(TI_DRIVER_DESC); |
| 282 | MODULE_VERSION(TI_DRIVER_VERSION); |
| 283 | MODULE_LICENSE("GPL"); |
| 284 | |
David Woodhouse | 5f24e2d | 2008-05-30 18:49:51 +0300 | [diff] [blame] | 285 | MODULE_FIRMWARE("ti_3410.fw"); |
| 286 | MODULE_FIRMWARE("ti_5052.fw"); |
Chris Adams | 7df5231 | 2009-01-11 19:49:11 +0000 | [diff] [blame] | 287 | MODULE_FIRMWARE("mts_cdma.fw"); |
| 288 | MODULE_FIRMWARE("mts_gsm.fw"); |
| 289 | MODULE_FIRMWARE("mts_edge.fw"); |
David Woodhouse | 5f24e2d | 2008-05-30 18:49:51 +0300 | [diff] [blame] | 290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | module_param(debug, bool, S_IRUGO | S_IWUSR); |
| 292 | MODULE_PARM_DESC(debug, "Enable debugging, 0=no, 1=yes"); |
| 293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | module_param(closing_wait, int, S_IRUGO | S_IWUSR); |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 295 | MODULE_PARM_DESC(closing_wait, |
| 296 | "Maximum wait for data to drain in close, in .01 secs, default is 4000"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
| 298 | module_param_array(vendor_3410, ushort, &vendor_3410_count, S_IRUGO); |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 299 | MODULE_PARM_DESC(vendor_3410, |
| 300 | "Vendor ids for 3410 based devices, 1-5 short integers"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | module_param_array(product_3410, ushort, &product_3410_count, S_IRUGO); |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 302 | MODULE_PARM_DESC(product_3410, |
| 303 | "Product ids for 3410 based devices, 1-5 short integers"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | module_param_array(vendor_5052, ushort, &vendor_5052_count, S_IRUGO); |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 305 | MODULE_PARM_DESC(vendor_5052, |
| 306 | "Vendor ids for 5052 based devices, 1-5 short integers"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | module_param_array(product_5052, ushort, &product_5052_count, S_IRUGO); |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 308 | MODULE_PARM_DESC(product_5052, |
| 309 | "Product ids for 5052 based devices, 1-5 short integers"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
| 311 | MODULE_DEVICE_TABLE(usb, ti_id_table_combined); |
| 312 | |
| 313 | |
| 314 | /* Functions */ |
| 315 | |
| 316 | static int __init ti_init(void) |
| 317 | { |
Chris Adams | 05a3d90 | 2009-01-11 19:48:53 +0000 | [diff] [blame] | 318 | int i, j, c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | int ret; |
| 320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | /* insert extra vendor and product ids */ |
Chris Adams | 05a3d90 | 2009-01-11 19:48:53 +0000 | [diff] [blame] | 322 | c = ARRAY_SIZE(ti_id_table_combined) - 2 * TI_EXTRA_VID_PID_COUNT - 1; |
Tobias Klauser | 52950ed | 2005-12-11 16:20:08 +0100 | [diff] [blame] | 323 | j = ARRAY_SIZE(ti_id_table_3410) - TI_EXTRA_VID_PID_COUNT - 1; |
Chris Adams | 05a3d90 | 2009-01-11 19:48:53 +0000 | [diff] [blame] | 324 | for (i = 0; i < min(vendor_3410_count, product_3410_count); i++, j++, c++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | ti_id_table_3410[j].idVendor = vendor_3410[i]; |
| 326 | ti_id_table_3410[j].idProduct = product_3410[i]; |
| 327 | ti_id_table_3410[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE; |
Chris Adams | 05a3d90 | 2009-01-11 19:48:53 +0000 | [diff] [blame] | 328 | ti_id_table_combined[c].idVendor = vendor_3410[i]; |
| 329 | ti_id_table_combined[c].idProduct = product_3410[i]; |
| 330 | ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | } |
Tobias Klauser | 52950ed | 2005-12-11 16:20:08 +0100 | [diff] [blame] | 332 | j = ARRAY_SIZE(ti_id_table_5052) - TI_EXTRA_VID_PID_COUNT - 1; |
Chris Adams | 05a3d90 | 2009-01-11 19:48:53 +0000 | [diff] [blame] | 333 | for (i = 0; i < min(vendor_5052_count, product_5052_count); i++, j++, c++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | ti_id_table_5052[j].idVendor = vendor_5052[i]; |
| 335 | ti_id_table_5052[j].idProduct = product_5052[i]; |
| 336 | ti_id_table_5052[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE; |
Chris Adams | 05a3d90 | 2009-01-11 19:48:53 +0000 | [diff] [blame] | 337 | ti_id_table_combined[c].idVendor = vendor_5052[i]; |
| 338 | ti_id_table_combined[c].idProduct = product_5052[i]; |
| 339 | ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | ret = usb_serial_register(&ti_1port_device); |
| 343 | if (ret) |
| 344 | goto failed_1port; |
| 345 | ret = usb_serial_register(&ti_2port_device); |
| 346 | if (ret) |
| 347 | goto failed_2port; |
| 348 | |
| 349 | ret = usb_register(&ti_usb_driver); |
| 350 | if (ret) |
| 351 | goto failed_usb; |
| 352 | |
Greg Kroah-Hartman | c197a8d | 2008-08-18 13:21:04 -0700 | [diff] [blame] | 353 | printk(KERN_INFO KBUILD_MODNAME ": " TI_DRIVER_VERSION ":" |
| 354 | TI_DRIVER_DESC "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
| 356 | return 0; |
| 357 | |
| 358 | failed_usb: |
| 359 | usb_serial_deregister(&ti_2port_device); |
| 360 | failed_2port: |
| 361 | usb_serial_deregister(&ti_1port_device); |
| 362 | failed_1port: |
| 363 | return ret; |
| 364 | } |
| 365 | |
| 366 | |
| 367 | static void __exit ti_exit(void) |
| 368 | { |
| 369 | usb_serial_deregister(&ti_1port_device); |
| 370 | usb_serial_deregister(&ti_2port_device); |
| 371 | usb_deregister(&ti_usb_driver); |
| 372 | } |
| 373 | |
| 374 | |
| 375 | module_init(ti_init); |
| 376 | module_exit(ti_exit); |
| 377 | |
| 378 | |
| 379 | static int ti_startup(struct usb_serial *serial) |
| 380 | { |
| 381 | struct ti_device *tdev; |
| 382 | struct ti_port *tport; |
| 383 | struct usb_device *dev = serial->dev; |
| 384 | int status; |
| 385 | int i; |
| 386 | |
| 387 | |
| 388 | dbg("%s - product 0x%4X, num configurations %d, configuration value %d", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 389 | __func__, le16_to_cpu(dev->descriptor.idProduct), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | dev->descriptor.bNumConfigurations, |
| 391 | dev->actconfig->desc.bConfigurationValue); |
| 392 | |
| 393 | /* create device structure */ |
Eric Sesterhenn | 80b6ca4 | 2006-02-27 21:29:43 +0100 | [diff] [blame] | 394 | tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | if (tdev == NULL) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 396 | dev_err(&dev->dev, "%s - out of memory\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | return -ENOMEM; |
| 398 | } |
Matthias Kaehlcke | 9da0068 | 2007-11-21 15:13:16 -0800 | [diff] [blame] | 399 | mutex_init(&tdev->td_open_close_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | tdev->td_serial = serial; |
| 401 | usb_set_serial_data(serial, tdev); |
| 402 | |
| 403 | /* determine device type */ |
| 404 | if (usb_match_id(serial->interface, ti_id_table_3410)) |
| 405 | tdev->td_is_3410 = 1; |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 406 | dbg("%s - device type is %s", __func__, |
| 407 | tdev->td_is_3410 ? "3410" : "5052"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | |
| 409 | /* if we have only 1 configuration, download firmware */ |
| 410 | if (dev->descriptor.bNumConfigurations == 1) { |
Chris Adams | 05a3d90 | 2009-01-11 19:48:53 +0000 | [diff] [blame] | 411 | if ((status = ti_download_firmware(tdev)) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | goto free_tdev; |
| 413 | |
| 414 | /* 3410 must be reset, 5052 resets itself */ |
| 415 | if (tdev->td_is_3410) { |
| 416 | msleep_interruptible(100); |
| 417 | usb_reset_device(dev); |
| 418 | } |
| 419 | |
| 420 | status = -ENODEV; |
| 421 | goto free_tdev; |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 422 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | |
Oliver Neukum | 413ba6f | 2008-12-16 12:25:55 +0100 | [diff] [blame] | 424 | /* the second configuration must be set */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) { |
Oliver Neukum | 413ba6f | 2008-12-16 12:25:55 +0100 | [diff] [blame] | 426 | status = usb_driver_set_configuration(dev, TI_ACTIVE_CONFIG); |
| 427 | status = status ? status : -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | goto free_tdev; |
| 429 | } |
| 430 | |
| 431 | /* set up port structures */ |
| 432 | for (i = 0; i < serial->num_ports; ++i) { |
Burman Yan | 7ac9da1 | 2006-11-22 20:54:38 +0200 | [diff] [blame] | 433 | tport = kzalloc(sizeof(struct ti_port), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | if (tport == NULL) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 435 | dev_err(&dev->dev, "%s - out of memory\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | status = -ENOMEM; |
| 437 | goto free_tports; |
| 438 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | spin_lock_init(&tport->tp_lock); |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 440 | tport->tp_uart_base_addr = (i == 0 ? |
| 441 | TI_UART1_BASE_ADDR : TI_UART2_BASE_ADDR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | tport->tp_closing_wait = closing_wait; |
| 443 | init_waitqueue_head(&tport->tp_msr_wait); |
| 444 | init_waitqueue_head(&tport->tp_write_wait); |
| 445 | tport->tp_write_buf = ti_buf_alloc(); |
| 446 | if (tport->tp_write_buf == NULL) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 447 | dev_err(&dev->dev, "%s - out of memory\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | kfree(tport); |
| 449 | status = -ENOMEM; |
| 450 | goto free_tports; |
| 451 | } |
| 452 | tport->tp_port = serial->port[i]; |
| 453 | tport->tp_tdev = tdev; |
| 454 | usb_set_serial_port_data(serial->port[i], tport); |
| 455 | tport->tp_uart_mode = 0; /* default is RS232 */ |
| 456 | } |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 457 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | return 0; |
| 459 | |
| 460 | free_tports: |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 461 | for (--i; i >= 0; --i) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | tport = usb_get_serial_port_data(serial->port[i]); |
| 463 | ti_buf_free(tport->tp_write_buf); |
| 464 | kfree(tport); |
| 465 | usb_set_serial_port_data(serial->port[i], NULL); |
| 466 | } |
| 467 | free_tdev: |
| 468 | kfree(tdev); |
| 469 | usb_set_serial_data(serial, NULL); |
| 470 | return status; |
| 471 | } |
| 472 | |
| 473 | |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 474 | static void ti_release(struct usb_serial *serial) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | { |
| 476 | int i; |
| 477 | struct ti_device *tdev = usb_get_serial_data(serial); |
| 478 | struct ti_port *tport; |
| 479 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 480 | dbg("%s", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 482 | for (i = 0; i < serial->num_ports; ++i) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | tport = usb_get_serial_port_data(serial->port[i]); |
| 484 | if (tport) { |
| 485 | ti_buf_free(tport->tp_write_buf); |
| 486 | kfree(tport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | |
Jesper Juhl | 1bc3c9e | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 490 | kfree(tdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | |
Alan Cox | a509a7e | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 494 | static int ti_open(struct tty_struct *tty, struct usb_serial_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | { |
| 496 | struct ti_port *tport = usb_get_serial_port_data(port); |
| 497 | struct ti_device *tdev; |
| 498 | struct usb_device *dev; |
| 499 | struct urb *urb; |
| 500 | int port_number; |
| 501 | int status; |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 502 | __u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS | |
| 503 | TI_PIPE_TIMEOUT_ENABLE | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | (TI_TRANSFER_TIMEOUT << 2)); |
| 505 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 506 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
| 508 | if (tport == NULL) |
| 509 | return -ENODEV; |
| 510 | |
| 511 | dev = port->serial->dev; |
| 512 | tdev = tport->tp_tdev; |
| 513 | |
| 514 | /* only one open on any port on a device at a time */ |
Matthias Kaehlcke | 9da0068 | 2007-11-21 15:13:16 -0800 | [diff] [blame] | 515 | if (mutex_lock_interruptible(&tdev->td_open_close_lock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | return -ERESTARTSYS; |
| 517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | port_number = port->number - port->serial->minor; |
| 519 | |
| 520 | memset(&(tport->tp_icount), 0x00, sizeof(tport->tp_icount)); |
| 521 | |
| 522 | tport->tp_msr = 0; |
| 523 | tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR); |
| 524 | |
| 525 | /* start interrupt urb the first time a port is opened on this device */ |
| 526 | if (tdev->td_open_port_count == 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 527 | dbg("%s - start interrupt in urb", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | urb = tdev->td_serial->port[0]->interrupt_in_urb; |
| 529 | if (!urb) { |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 530 | dev_err(&port->dev, "%s - no interrupt urb\n", |
| 531 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | status = -EINVAL; |
Matthias Kaehlcke | 9da0068 | 2007-11-21 15:13:16 -0800 | [diff] [blame] | 533 | goto release_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | } |
| 535 | urb->complete = ti_interrupt_callback; |
| 536 | urb->context = tdev; |
| 537 | urb->dev = dev; |
| 538 | status = usb_submit_urb(urb, GFP_KERNEL); |
| 539 | if (status) { |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 540 | dev_err(&port->dev, |
| 541 | "%s - submit interrupt urb failed, %d\n", |
| 542 | __func__, status); |
Matthias Kaehlcke | 9da0068 | 2007-11-21 15:13:16 -0800 | [diff] [blame] | 543 | goto release_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 547 | if (tty) |
| 548 | ti_set_termios(tty, port, tty->termios); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 550 | dbg("%s - sending TI_OPEN_PORT", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | status = ti_command_out_sync(tdev, TI_OPEN_PORT, |
| 552 | (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0); |
| 553 | if (status) { |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 554 | dev_err(&port->dev, "%s - cannot send open command, %d\n", |
| 555 | __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | goto unlink_int_urb; |
| 557 | } |
| 558 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 559 | dbg("%s - sending TI_START_PORT", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | status = ti_command_out_sync(tdev, TI_START_PORT, |
| 561 | (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0); |
| 562 | if (status) { |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 563 | dev_err(&port->dev, "%s - cannot send start command, %d\n", |
| 564 | __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | goto unlink_int_urb; |
| 566 | } |
| 567 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 568 | dbg("%s - sending TI_PURGE_PORT", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | status = ti_command_out_sync(tdev, TI_PURGE_PORT, |
| 570 | (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0); |
| 571 | if (status) { |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 572 | dev_err(&port->dev, "%s - cannot clear input buffers, %d\n", |
| 573 | __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | goto unlink_int_urb; |
| 575 | } |
| 576 | status = ti_command_out_sync(tdev, TI_PURGE_PORT, |
| 577 | (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0); |
| 578 | if (status) { |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 579 | dev_err(&port->dev, "%s - cannot clear output buffers, %d\n", |
| 580 | __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | goto unlink_int_urb; |
| 582 | } |
| 583 | |
| 584 | /* reset the data toggle on the bulk endpoints to work around bug in |
| 585 | * host controllers where things get out of sync some times */ |
| 586 | usb_clear_halt(dev, port->write_urb->pipe); |
| 587 | usb_clear_halt(dev, port->read_urb->pipe); |
| 588 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 589 | if (tty) |
| 590 | ti_set_termios(tty, port, tty->termios); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 592 | dbg("%s - sending TI_OPEN_PORT (2)", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | status = ti_command_out_sync(tdev, TI_OPEN_PORT, |
| 594 | (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0); |
| 595 | if (status) { |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 596 | dev_err(&port->dev, "%s - cannot send open command (2), %d\n", |
| 597 | __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | goto unlink_int_urb; |
| 599 | } |
| 600 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 601 | dbg("%s - sending TI_START_PORT (2)", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | status = ti_command_out_sync(tdev, TI_START_PORT, |
| 603 | (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0); |
| 604 | if (status) { |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 605 | dev_err(&port->dev, "%s - cannot send start command (2), %d\n", |
| 606 | __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | goto unlink_int_urb; |
| 608 | } |
| 609 | |
| 610 | /* start read urb */ |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 611 | dbg("%s - start read urb", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | urb = port->read_urb; |
| 613 | if (!urb) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 614 | dev_err(&port->dev, "%s - no read urb\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | status = -EINVAL; |
| 616 | goto unlink_int_urb; |
| 617 | } |
| 618 | tport->tp_read_urb_state = TI_READ_URB_RUNNING; |
| 619 | urb->complete = ti_bulk_in_callback; |
| 620 | urb->context = tport; |
| 621 | urb->dev = dev; |
| 622 | status = usb_submit_urb(urb, GFP_KERNEL); |
| 623 | if (status) { |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 624 | dev_err(&port->dev, "%s - submit read urb failed, %d\n", |
| 625 | __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | goto unlink_int_urb; |
| 627 | } |
| 628 | |
| 629 | tport->tp_is_open = 1; |
| 630 | ++tdev->td_open_port_count; |
| 631 | |
Matthias Kaehlcke | 9da0068 | 2007-11-21 15:13:16 -0800 | [diff] [blame] | 632 | goto release_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | |
| 634 | unlink_int_urb: |
| 635 | if (tdev->td_open_port_count == 0) |
| 636 | usb_kill_urb(port->serial->port[0]->interrupt_in_urb); |
Matthias Kaehlcke | 9da0068 | 2007-11-21 15:13:16 -0800 | [diff] [blame] | 637 | release_lock: |
| 638 | mutex_unlock(&tdev->td_open_close_lock); |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 639 | dbg("%s - exit %d", __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | return status; |
| 641 | } |
| 642 | |
| 643 | |
Alan Cox | 335f851 | 2009-06-11 12:26:29 +0100 | [diff] [blame] | 644 | static void ti_close(struct usb_serial_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | { |
| 646 | struct ti_device *tdev; |
| 647 | struct ti_port *tport; |
| 648 | int port_number; |
| 649 | int status; |
Matthias Kaehlcke | 9da0068 | 2007-11-21 15:13:16 -0800 | [diff] [blame] | 650 | int do_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 652 | dbg("%s - port %d", __func__, port->number); |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 653 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | tdev = usb_get_serial_data(port->serial); |
| 655 | tport = usb_get_serial_port_data(port); |
| 656 | if (tdev == NULL || tport == NULL) |
| 657 | return; |
| 658 | |
| 659 | tport->tp_is_open = 0; |
| 660 | |
| 661 | ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 1); |
| 662 | |
| 663 | usb_kill_urb(port->read_urb); |
| 664 | usb_kill_urb(port->write_urb); |
| 665 | tport->tp_write_urb_in_use = 0; |
| 666 | |
| 667 | port_number = port->number - port->serial->minor; |
| 668 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 669 | dbg("%s - sending TI_CLOSE_PORT", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | status = ti_command_out_sync(tdev, TI_CLOSE_PORT, |
| 671 | (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0); |
| 672 | if (status) |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 673 | dev_err(&port->dev, |
| 674 | "%s - cannot send close port command, %d\n" |
| 675 | , __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | |
Matthias Kaehlcke | 9da0068 | 2007-11-21 15:13:16 -0800 | [diff] [blame] | 677 | /* if mutex_lock is interrupted, continue anyway */ |
| 678 | do_unlock = !mutex_lock_interruptible(&tdev->td_open_close_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | --tport->tp_tdev->td_open_port_count; |
| 680 | if (tport->tp_tdev->td_open_port_count <= 0) { |
| 681 | /* last port is closed, shut down interrupt urb */ |
| 682 | usb_kill_urb(port->serial->port[0]->interrupt_in_urb); |
| 683 | tport->tp_tdev->td_open_port_count = 0; |
| 684 | } |
Matthias Kaehlcke | 9da0068 | 2007-11-21 15:13:16 -0800 | [diff] [blame] | 685 | if (do_unlock) |
| 686 | mutex_unlock(&tdev->td_open_close_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 688 | dbg("%s - exit", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 692 | static int ti_write(struct tty_struct *tty, struct usb_serial_port *port, |
| 693 | const unsigned char *data, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | { |
| 695 | struct ti_port *tport = usb_get_serial_port_data(port); |
| 696 | unsigned long flags; |
| 697 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 698 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | |
| 700 | if (count == 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 701 | dbg("%s - write request of 0 bytes", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | if (tport == NULL || !tport->tp_is_open) |
| 706 | return -ENODEV; |
| 707 | |
| 708 | spin_lock_irqsave(&tport->tp_lock, flags); |
| 709 | count = ti_buf_put(tport->tp_write_buf, data, count); |
| 710 | spin_unlock_irqrestore(&tport->tp_lock, flags); |
| 711 | |
| 712 | ti_send(tport); |
| 713 | |
| 714 | return count; |
| 715 | } |
| 716 | |
| 717 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 718 | static int ti_write_room(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 720 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | struct ti_port *tport = usb_get_serial_port_data(port); |
| 722 | int room = 0; |
| 723 | unsigned long flags; |
| 724 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 725 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | |
| 727 | if (tport == NULL) |
Alan Cox | 23198fd | 2009-07-20 16:05:27 +0100 | [diff] [blame] | 728 | return 0; |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | spin_lock_irqsave(&tport->tp_lock, flags); |
| 731 | room = ti_buf_space_avail(tport->tp_write_buf); |
| 732 | spin_unlock_irqrestore(&tport->tp_lock, flags); |
| 733 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 734 | dbg("%s - returns %d", __func__, room); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | return room; |
| 736 | } |
| 737 | |
| 738 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 739 | static int ti_chars_in_buffer(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 741 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | struct ti_port *tport = usb_get_serial_port_data(port); |
| 743 | int chars = 0; |
| 744 | unsigned long flags; |
| 745 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 746 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | |
| 748 | if (tport == NULL) |
Alan Cox | 23198fd | 2009-07-20 16:05:27 +0100 | [diff] [blame] | 749 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | |
| 751 | spin_lock_irqsave(&tport->tp_lock, flags); |
| 752 | chars = ti_buf_data_avail(tport->tp_write_buf); |
| 753 | spin_unlock_irqrestore(&tport->tp_lock, flags); |
| 754 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 755 | dbg("%s - returns %d", __func__, chars); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | return chars; |
| 757 | } |
| 758 | |
| 759 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 760 | static void ti_throttle(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 762 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | struct ti_port *tport = usb_get_serial_port_data(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 765 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | |
| 767 | if (tport == NULL) |
| 768 | return; |
| 769 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | if (I_IXOFF(tty) || C_CRTSCTS(tty)) |
| 771 | ti_stop_read(tport, tty); |
| 772 | |
| 773 | } |
| 774 | |
| 775 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 776 | static void ti_unthrottle(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 778 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | struct ti_port *tport = usb_get_serial_port_data(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | int status; |
| 781 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 782 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | |
| 784 | if (tport == NULL) |
| 785 | return; |
| 786 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | if (I_IXOFF(tty) || C_CRTSCTS(tty)) { |
| 788 | status = ti_restart_read(tport, tty); |
| 789 | if (status) |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 790 | dev_err(&port->dev, "%s - cannot restart read, %d\n", |
| 791 | __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | |
| 795 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 796 | static int ti_ioctl(struct tty_struct *tty, struct file *file, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | unsigned int cmd, unsigned long arg) |
| 798 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 799 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | struct ti_port *tport = usb_get_serial_port_data(port); |
| 801 | struct async_icount cnow; |
| 802 | struct async_icount cprev; |
| 803 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 804 | dbg("%s - port %d, cmd = 0x%04X", __func__, port->number, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | |
| 806 | if (tport == NULL) |
| 807 | return -ENODEV; |
| 808 | |
| 809 | switch (cmd) { |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 810 | case TIOCGSERIAL: |
| 811 | dbg("%s - (%d) TIOCGSERIAL", __func__, port->number); |
| 812 | return ti_get_serial_info(tport, |
| 813 | (struct serial_struct __user *)arg); |
| 814 | case TIOCSSERIAL: |
| 815 | dbg("%s - (%d) TIOCSSERIAL", __func__, port->number); |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 816 | return ti_set_serial_info(tty, tport, |
| 817 | (struct serial_struct __user *)arg); |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 818 | case TIOCMIWAIT: |
| 819 | dbg("%s - (%d) TIOCMIWAIT", __func__, port->number); |
| 820 | cprev = tport->tp_icount; |
| 821 | while (1) { |
| 822 | interruptible_sleep_on(&tport->tp_msr_wait); |
| 823 | if (signal_pending(current)) |
| 824 | return -ERESTARTSYS; |
| 825 | cnow = tport->tp_icount; |
| 826 | if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr && |
| 827 | cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) |
| 828 | return -EIO; /* no change => error */ |
| 829 | if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || |
| 830 | ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || |
| 831 | ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) || |
| 832 | ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) |
| 833 | return 0; |
| 834 | cprev = cnow; |
| 835 | } |
| 836 | break; |
| 837 | case TIOCGICOUNT: |
| 838 | dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", |
| 839 | __func__, port->number, |
| 840 | tport->tp_icount.rx, tport->tp_icount.tx); |
| 841 | if (copy_to_user((void __user *)arg, &tport->tp_icount, |
| 842 | sizeof(tport->tp_icount))) |
| 843 | return -EFAULT; |
| 844 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | return -ENOIOCTLCMD; |
| 847 | } |
| 848 | |
| 849 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 850 | static void ti_set_termios(struct tty_struct *tty, |
| 851 | struct usb_serial_port *port, struct ktermios *old_termios) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | { |
| 853 | struct ti_port *tport = usb_get_serial_port_data(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | struct ti_uart_config *config; |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 855 | tcflag_t cflag, iflag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | int baud; |
| 857 | int status; |
| 858 | int port_number = port->number - port->serial->minor; |
| 859 | unsigned int mcr; |
| 860 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 861 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | cflag = tty->termios->c_cflag; |
| 864 | iflag = tty->termios->c_iflag; |
| 865 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 866 | dbg("%s - cflag %08x, iflag %08x", __func__, cflag, iflag); |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 867 | dbg("%s - old clfag %08x, old iflag %08x", __func__, |
| 868 | old_termios->c_cflag, old_termios->c_iflag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | |
| 870 | if (tport == NULL) |
| 871 | return; |
| 872 | |
| 873 | config = kmalloc(sizeof(*config), GFP_KERNEL); |
| 874 | if (!config) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 875 | dev_err(&port->dev, "%s - out of memory\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | return; |
| 877 | } |
| 878 | |
| 879 | config->wFlags = 0; |
| 880 | |
| 881 | /* these flags must be set */ |
| 882 | config->wFlags |= TI_UART_ENABLE_MS_INTS; |
| 883 | config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA; |
| 884 | config->bUartMode = (__u8)(tport->tp_uart_mode); |
| 885 | |
| 886 | switch (cflag & CSIZE) { |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 887 | case CS5: |
| 888 | config->bDataBits = TI_UART_5_DATA_BITS; |
| 889 | break; |
| 890 | case CS6: |
| 891 | config->bDataBits = TI_UART_6_DATA_BITS; |
| 892 | break; |
| 893 | case CS7: |
| 894 | config->bDataBits = TI_UART_7_DATA_BITS; |
| 895 | break; |
| 896 | default: |
| 897 | case CS8: |
| 898 | config->bDataBits = TI_UART_8_DATA_BITS; |
| 899 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | } |
| 901 | |
Alan Cox | 537699e | 2008-01-03 17:03:11 +0000 | [diff] [blame] | 902 | /* CMSPAR isn't supported by this driver */ |
| 903 | tty->termios->c_cflag &= ~CMSPAR; |
| 904 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | if (cflag & PARENB) { |
| 906 | if (cflag & PARODD) { |
| 907 | config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING; |
| 908 | config->bParity = TI_UART_ODD_PARITY; |
| 909 | } else { |
| 910 | config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING; |
| 911 | config->bParity = TI_UART_EVEN_PARITY; |
| 912 | } |
| 913 | } else { |
| 914 | config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING; |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 915 | config->bParity = TI_UART_NO_PARITY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | if (cflag & CSTOPB) |
| 919 | config->bStopBits = TI_UART_2_STOP_BITS; |
| 920 | else |
| 921 | config->bStopBits = TI_UART_1_STOP_BITS; |
| 922 | |
| 923 | if (cflag & CRTSCTS) { |
| 924 | /* RTS flow control must be off to drop RTS for baud rate B0 */ |
| 925 | if ((cflag & CBAUD) != B0) |
| 926 | config->wFlags |= TI_UART_ENABLE_RTS_IN; |
| 927 | config->wFlags |= TI_UART_ENABLE_CTS_OUT; |
| 928 | } else { |
| 929 | tty->hw_stopped = 0; |
| 930 | ti_restart_read(tport, tty); |
| 931 | } |
| 932 | |
| 933 | if (I_IXOFF(tty) || I_IXON(tty)) { |
| 934 | config->cXon = START_CHAR(tty); |
| 935 | config->cXoff = STOP_CHAR(tty); |
| 936 | |
| 937 | if (I_IXOFF(tty)) |
| 938 | config->wFlags |= TI_UART_ENABLE_X_IN; |
| 939 | else |
| 940 | ti_restart_read(tport, tty); |
| 941 | |
| 942 | if (I_IXON(tty)) |
| 943 | config->wFlags |= TI_UART_ENABLE_X_OUT; |
| 944 | } |
| 945 | |
| 946 | baud = tty_get_baud_rate(tty); |
Alan Cox | 537699e | 2008-01-03 17:03:11 +0000 | [diff] [blame] | 947 | if (!baud) |
| 948 | baud = 9600; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | if (tport->tp_tdev->td_is_3410) |
| 950 | config->wBaudRate = (__u16)((923077 + baud/2) / baud); |
| 951 | else |
| 952 | config->wBaudRate = (__u16)((461538 + baud/2) / baud); |
| 953 | |
Alan Cox | 537699e | 2008-01-03 17:03:11 +0000 | [diff] [blame] | 954 | /* FIXME: Should calculate resulting baud here and report it back */ |
| 955 | if ((cflag & CBAUD) != B0) |
| 956 | tty_encode_baud_rate(tty, baud, baud); |
| 957 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | dbg("%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 959 | __func__, baud, config->wBaudRate, config->wFlags, config->bDataBits, config->bParity, config->bStopBits, config->cXon, config->cXoff, config->bUartMode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | |
| 961 | cpu_to_be16s(&config->wBaudRate); |
| 962 | cpu_to_be16s(&config->wFlags); |
| 963 | |
| 964 | status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG, |
| 965 | (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config, |
| 966 | sizeof(*config)); |
| 967 | if (status) |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 968 | dev_err(&port->dev, "%s - cannot set config on port %d, %d\n", |
| 969 | __func__, port_number, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | |
| 971 | /* SET_CONFIG asserts RTS and DTR, reset them correctly */ |
| 972 | mcr = tport->tp_shadow_mcr; |
| 973 | /* if baud rate is B0, clear RTS and DTR */ |
| 974 | if ((cflag & CBAUD) == B0) |
| 975 | mcr &= ~(TI_MCR_DTR | TI_MCR_RTS); |
| 976 | status = ti_set_mcr(tport, mcr); |
| 977 | if (status) |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 978 | dev_err(&port->dev, |
| 979 | "%s - cannot set modem control on port %d, %d\n", |
| 980 | __func__, port_number, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | |
| 982 | kfree(config); |
| 983 | } |
| 984 | |
| 985 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 986 | static int ti_tiocmget(struct tty_struct *tty, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 988 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | struct ti_port *tport = usb_get_serial_port_data(port); |
| 990 | unsigned int result; |
| 991 | unsigned int msr; |
| 992 | unsigned int mcr; |
Alan Cox | 04ca89d | 2008-02-20 21:41:40 +0000 | [diff] [blame] | 993 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 995 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | |
| 997 | if (tport == NULL) |
| 998 | return -ENODEV; |
| 999 | |
Alan Cox | 04ca89d | 2008-02-20 21:41:40 +0000 | [diff] [blame] | 1000 | spin_lock_irqsave(&tport->tp_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | msr = tport->tp_msr; |
| 1002 | mcr = tport->tp_shadow_mcr; |
Alan Cox | 04ca89d | 2008-02-20 21:41:40 +0000 | [diff] [blame] | 1003 | spin_unlock_irqrestore(&tport->tp_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | |
| 1005 | result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0) |
| 1006 | | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0) |
| 1007 | | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0) |
| 1008 | | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0) |
| 1009 | | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0) |
| 1010 | | ((msr & TI_MSR_RI) ? TIOCM_RI : 0) |
| 1011 | | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0); |
| 1012 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1013 | dbg("%s - 0x%04X", __func__, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | |
| 1015 | return result; |
| 1016 | } |
| 1017 | |
| 1018 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1019 | static int ti_tiocmset(struct tty_struct *tty, struct file *file, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | unsigned int set, unsigned int clear) |
| 1021 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1022 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | struct ti_port *tport = usb_get_serial_port_data(port); |
| 1024 | unsigned int mcr; |
Alan Cox | 04ca89d | 2008-02-20 21:41:40 +0000 | [diff] [blame] | 1025 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1027 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | |
| 1029 | if (tport == NULL) |
| 1030 | return -ENODEV; |
| 1031 | |
Alan Cox | 04ca89d | 2008-02-20 21:41:40 +0000 | [diff] [blame] | 1032 | spin_lock_irqsave(&tport->tp_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | mcr = tport->tp_shadow_mcr; |
| 1034 | |
| 1035 | if (set & TIOCM_RTS) |
| 1036 | mcr |= TI_MCR_RTS; |
| 1037 | if (set & TIOCM_DTR) |
| 1038 | mcr |= TI_MCR_DTR; |
| 1039 | if (set & TIOCM_LOOP) |
| 1040 | mcr |= TI_MCR_LOOP; |
| 1041 | |
| 1042 | if (clear & TIOCM_RTS) |
| 1043 | mcr &= ~TI_MCR_RTS; |
| 1044 | if (clear & TIOCM_DTR) |
| 1045 | mcr &= ~TI_MCR_DTR; |
| 1046 | if (clear & TIOCM_LOOP) |
| 1047 | mcr &= ~TI_MCR_LOOP; |
Alan Cox | 04ca89d | 2008-02-20 21:41:40 +0000 | [diff] [blame] | 1048 | spin_unlock_irqrestore(&tport->tp_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | |
| 1050 | return ti_set_mcr(tport, mcr); |
| 1051 | } |
| 1052 | |
| 1053 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1054 | static void ti_break(struct tty_struct *tty, int break_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1056 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | struct ti_port *tport = usb_get_serial_port_data(port); |
| 1058 | int status; |
| 1059 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1060 | dbg("%s - state = %d", __func__, break_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | |
| 1062 | if (tport == NULL) |
| 1063 | return; |
| 1064 | |
| 1065 | ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 0); |
| 1066 | |
| 1067 | status = ti_write_byte(tport->tp_tdev, |
| 1068 | tport->tp_uart_base_addr + TI_UART_OFFSET_LCR, |
| 1069 | TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0); |
| 1070 | |
| 1071 | if (status) |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1072 | dbg("%s - error setting break, %d", __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1076 | static void ti_interrupt_callback(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | { |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 1078 | struct ti_device *tdev = urb->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | struct usb_serial_port *port; |
| 1080 | struct usb_serial *serial = tdev->td_serial; |
| 1081 | struct ti_port *tport; |
| 1082 | struct device *dev = &urb->dev->dev; |
| 1083 | unsigned char *data = urb->transfer_buffer; |
| 1084 | int length = urb->actual_length; |
| 1085 | int port_number; |
| 1086 | int function; |
Greg Kroah-Hartman | 52171b4 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 1087 | int status = urb->status; |
| 1088 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | __u8 msr; |
| 1090 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1091 | dbg("%s", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | |
Greg Kroah-Hartman | 52171b4 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 1093 | switch (status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | case 0: |
| 1095 | break; |
| 1096 | case -ECONNRESET: |
| 1097 | case -ENOENT: |
| 1098 | case -ESHUTDOWN: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1099 | dbg("%s - urb shutting down, %d", __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | tdev->td_urb_error = 1; |
| 1101 | return; |
| 1102 | default: |
Greg Kroah-Hartman | 52171b4 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 1103 | dev_err(dev, "%s - nonzero urb status, %d\n", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1104 | __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | tdev->td_urb_error = 1; |
| 1106 | goto exit; |
| 1107 | } |
| 1108 | |
| 1109 | if (length != 2) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1110 | dbg("%s - bad packet size, %d", __func__, length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | goto exit; |
| 1112 | } |
| 1113 | |
| 1114 | if (data[0] == TI_CODE_HARDWARE_ERROR) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1115 | dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | goto exit; |
| 1117 | } |
| 1118 | |
| 1119 | port_number = TI_GET_PORT_FROM_CODE(data[0]); |
| 1120 | function = TI_GET_FUNC_FROM_CODE(data[0]); |
| 1121 | |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 1122 | dbg("%s - port_number %d, function %d, data 0x%02X", |
| 1123 | __func__, port_number, function, data[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | |
| 1125 | if (port_number >= serial->num_ports) { |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 1126 | dev_err(dev, "%s - bad port number, %d\n", |
| 1127 | __func__, port_number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | goto exit; |
| 1129 | } |
| 1130 | |
| 1131 | port = serial->port[port_number]; |
| 1132 | |
| 1133 | tport = usb_get_serial_port_data(port); |
| 1134 | if (!tport) |
| 1135 | goto exit; |
| 1136 | |
| 1137 | switch (function) { |
| 1138 | case TI_CODE_DATA_ERROR: |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 1139 | dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n", |
| 1140 | __func__, port_number, data[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | break; |
| 1142 | |
| 1143 | case TI_CODE_MODEM_STATUS: |
| 1144 | msr = data[1]; |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1145 | dbg("%s - port %d, msr 0x%02X", __func__, port_number, msr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | ti_handle_new_msr(tport, msr); |
| 1147 | break; |
| 1148 | |
| 1149 | default: |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 1150 | dev_err(dev, "%s - unknown interrupt code, 0x%02X\n", |
| 1151 | __func__, data[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | break; |
| 1153 | } |
| 1154 | |
| 1155 | exit: |
Greg Kroah-Hartman | 52171b4 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 1156 | retval = usb_submit_urb(urb, GFP_ATOMIC); |
| 1157 | if (retval) |
| 1158 | dev_err(dev, "%s - resubmit interrupt urb failed, %d\n", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1159 | __func__, retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1163 | static void ti_bulk_in_callback(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | { |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 1165 | struct ti_port *tport = urb->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | struct usb_serial_port *port = tport->tp_port; |
| 1167 | struct device *dev = &urb->dev->dev; |
Greg Kroah-Hartman | 52171b4 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 1168 | int status = urb->status; |
| 1169 | int retval = 0; |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 1170 | struct tty_struct *tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1172 | dbg("%s", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | |
Greg Kroah-Hartman | 52171b4 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 1174 | switch (status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | case 0: |
| 1176 | break; |
| 1177 | case -ECONNRESET: |
| 1178 | case -ENOENT: |
| 1179 | case -ESHUTDOWN: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1180 | dbg("%s - urb shutting down, %d", __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | tport->tp_tdev->td_urb_error = 1; |
| 1182 | wake_up_interruptible(&tport->tp_write_wait); |
| 1183 | return; |
| 1184 | default: |
Greg Kroah-Hartman | 52171b4 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 1185 | dev_err(dev, "%s - nonzero urb status, %d\n", |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 1186 | __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | tport->tp_tdev->td_urb_error = 1; |
| 1188 | wake_up_interruptible(&tport->tp_write_wait); |
| 1189 | } |
| 1190 | |
Greg Kroah-Hartman | 52171b4 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 1191 | if (status == -EPIPE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | goto exit; |
| 1193 | |
Greg Kroah-Hartman | 52171b4 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 1194 | if (status) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1195 | dev_err(dev, "%s - stopping read!\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | return; |
| 1197 | } |
| 1198 | |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 1199 | tty = tty_port_tty_get(&port->port); |
Alan Cox | cf54509 | 2009-04-14 14:58:11 +0100 | [diff] [blame] | 1200 | if (tty) { |
| 1201 | if (urb->actual_length) { |
| 1202 | usb_serial_debug_data(debug, dev, __func__, |
| 1203 | urb->actual_length, urb->transfer_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | |
Alan Cox | cf54509 | 2009-04-14 14:58:11 +0100 | [diff] [blame] | 1205 | if (!tport->tp_is_open) |
| 1206 | dbg("%s - port closed, dropping data", |
| 1207 | __func__); |
| 1208 | else |
| 1209 | ti_recv(&urb->dev->dev, tty, |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 1210 | urb->transfer_buffer, |
| 1211 | urb->actual_length); |
Alan Cox | cf54509 | 2009-04-14 14:58:11 +0100 | [diff] [blame] | 1212 | spin_lock(&tport->tp_lock); |
| 1213 | tport->tp_icount.rx += urb->actual_length; |
| 1214 | spin_unlock(&tport->tp_lock); |
| 1215 | } |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 1216 | tty_kref_put(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | exit: |
| 1220 | /* continue to read unless stopping */ |
| 1221 | spin_lock(&tport->tp_lock); |
| 1222 | if (tport->tp_read_urb_state == TI_READ_URB_RUNNING) { |
| 1223 | urb->dev = port->serial->dev; |
Greg Kroah-Hartman | 52171b4 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 1224 | retval = usb_submit_urb(urb, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | } else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING) { |
| 1226 | tport->tp_read_urb_state = TI_READ_URB_STOPPED; |
| 1227 | } |
| 1228 | spin_unlock(&tport->tp_lock); |
Greg Kroah-Hartman | 52171b4 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 1229 | if (retval) |
| 1230 | dev_err(dev, "%s - resubmit read urb failed, %d\n", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1231 | __func__, retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1235 | static void ti_bulk_out_callback(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | { |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 1237 | struct ti_port *tport = urb->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | struct usb_serial_port *port = tport->tp_port; |
| 1239 | struct device *dev = &urb->dev->dev; |
Greg Kroah-Hartman | 52171b4 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 1240 | int status = urb->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1242 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | |
| 1244 | tport->tp_write_urb_in_use = 0; |
| 1245 | |
Greg Kroah-Hartman | 52171b4 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 1246 | switch (status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | case 0: |
| 1248 | break; |
| 1249 | case -ECONNRESET: |
| 1250 | case -ENOENT: |
| 1251 | case -ESHUTDOWN: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1252 | dbg("%s - urb shutting down, %d", __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | tport->tp_tdev->td_urb_error = 1; |
| 1254 | wake_up_interruptible(&tport->tp_write_wait); |
| 1255 | return; |
| 1256 | default: |
Greg Kroah-Hartman | 52171b4 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 1257 | dev_err(dev, "%s - nonzero urb status, %d\n", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1258 | __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | tport->tp_tdev->td_urb_error = 1; |
| 1260 | wake_up_interruptible(&tport->tp_write_wait); |
| 1261 | } |
| 1262 | |
| 1263 | /* send any buffered data */ |
| 1264 | ti_send(tport); |
| 1265 | } |
| 1266 | |
| 1267 | |
| 1268 | static void ti_recv(struct device *dev, struct tty_struct *tty, |
| 1269 | unsigned char *data, int length) |
| 1270 | { |
| 1271 | int cnt; |
| 1272 | |
| 1273 | do { |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 1274 | cnt = tty_buffer_request_room(tty, length); |
| 1275 | if (cnt < length) { |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 1276 | dev_err(dev, "%s - dropping data, %d bytes lost\n", |
| 1277 | __func__, length - cnt); |
| 1278 | if (cnt == 0) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 1279 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | } |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 1281 | tty_insert_flip_string(tty, data, cnt); |
| 1282 | tty_flip_buffer_push(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | data += cnt; |
| 1284 | length -= cnt; |
| 1285 | } while (length > 0); |
| 1286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | |
| 1290 | static void ti_send(struct ti_port *tport) |
| 1291 | { |
| 1292 | int count, result; |
| 1293 | struct usb_serial_port *port = tport->tp_port; |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 1294 | struct tty_struct *tty = tty_port_tty_get(&port->port); /* FIXME */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | unsigned long flags; |
| 1296 | |
| 1297 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1298 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | |
| 1300 | spin_lock_irqsave(&tport->tp_lock, flags); |
| 1301 | |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 1302 | if (tport->tp_write_urb_in_use) |
| 1303 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | |
| 1305 | count = ti_buf_get(tport->tp_write_buf, |
| 1306 | port->write_urb->transfer_buffer, |
| 1307 | port->bulk_out_size); |
| 1308 | |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 1309 | if (count == 0) |
| 1310 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | |
| 1312 | tport->tp_write_urb_in_use = 1; |
| 1313 | |
| 1314 | spin_unlock_irqrestore(&tport->tp_lock, flags); |
| 1315 | |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 1316 | usb_serial_debug_data(debug, &port->dev, __func__, count, |
| 1317 | port->write_urb->transfer_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | |
| 1319 | usb_fill_bulk_urb(port->write_urb, port->serial->dev, |
| 1320 | usb_sndbulkpipe(port->serial->dev, |
| 1321 | port->bulk_out_endpointAddress), |
| 1322 | port->write_urb->transfer_buffer, count, |
| 1323 | ti_bulk_out_callback, tport); |
| 1324 | |
| 1325 | result = usb_submit_urb(port->write_urb, GFP_ATOMIC); |
| 1326 | if (result) { |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 1327 | dev_err(&port->dev, "%s - submit write urb failed, %d\n", |
| 1328 | __func__, result); |
| 1329 | tport->tp_write_urb_in_use = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | /* TODO: reschedule ti_send */ |
| 1331 | } else { |
| 1332 | spin_lock_irqsave(&tport->tp_lock, flags); |
| 1333 | tport->tp_icount.tx += count; |
| 1334 | spin_unlock_irqrestore(&tport->tp_lock, flags); |
| 1335 | } |
| 1336 | |
| 1337 | /* more room in the buffer for new writes, wakeup */ |
| 1338 | if (tty) |
| 1339 | tty_wakeup(tty); |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 1340 | tty_kref_put(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | wake_up_interruptible(&tport->tp_write_wait); |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 1342 | return; |
| 1343 | unlock: |
| 1344 | spin_unlock_irqrestore(&tport->tp_lock, flags); |
| 1345 | tty_kref_put(tty); |
| 1346 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | |
| 1350 | static int ti_set_mcr(struct ti_port *tport, unsigned int mcr) |
| 1351 | { |
Alan Cox | 04ca89d | 2008-02-20 21:41:40 +0000 | [diff] [blame] | 1352 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | int status; |
| 1354 | |
| 1355 | status = ti_write_byte(tport->tp_tdev, |
| 1356 | tport->tp_uart_base_addr + TI_UART_OFFSET_MCR, |
| 1357 | TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr); |
| 1358 | |
Alan Cox | 04ca89d | 2008-02-20 21:41:40 +0000 | [diff] [blame] | 1359 | spin_lock_irqsave(&tport->tp_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | if (!status) |
| 1361 | tport->tp_shadow_mcr = mcr; |
Alan Cox | 04ca89d | 2008-02-20 21:41:40 +0000 | [diff] [blame] | 1362 | spin_unlock_irqrestore(&tport->tp_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | |
| 1364 | return status; |
| 1365 | } |
| 1366 | |
| 1367 | |
| 1368 | static int ti_get_lsr(struct ti_port *tport) |
| 1369 | { |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 1370 | int size, status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | struct ti_device *tdev = tport->tp_tdev; |
| 1372 | struct usb_serial_port *port = tport->tp_port; |
| 1373 | int port_number = port->number - port->serial->minor; |
| 1374 | struct ti_port_status *data; |
| 1375 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1376 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | |
| 1378 | size = sizeof(struct ti_port_status); |
| 1379 | data = kmalloc(size, GFP_KERNEL); |
| 1380 | if (!data) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1381 | dev_err(&port->dev, "%s - out of memory\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | return -ENOMEM; |
| 1383 | } |
| 1384 | |
| 1385 | status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS, |
| 1386 | (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size); |
| 1387 | if (status) { |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 1388 | dev_err(&port->dev, |
| 1389 | "%s - get port status command failed, %d\n", |
| 1390 | __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | goto free_data; |
| 1392 | } |
| 1393 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1394 | dbg("%s - lsr 0x%02X", __func__, data->bLSR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | |
| 1396 | tport->tp_lsr = data->bLSR; |
| 1397 | |
| 1398 | free_data: |
| 1399 | kfree(data); |
| 1400 | return status; |
| 1401 | } |
| 1402 | |
| 1403 | |
| 1404 | static int ti_get_serial_info(struct ti_port *tport, |
| 1405 | struct serial_struct __user *ret_arg) |
| 1406 | { |
| 1407 | struct usb_serial_port *port = tport->tp_port; |
| 1408 | struct serial_struct ret_serial; |
| 1409 | |
| 1410 | if (!ret_arg) |
| 1411 | return -EFAULT; |
| 1412 | |
| 1413 | memset(&ret_serial, 0, sizeof(ret_serial)); |
| 1414 | |
| 1415 | ret_serial.type = PORT_16550A; |
| 1416 | ret_serial.line = port->serial->minor; |
| 1417 | ret_serial.port = port->number - port->serial->minor; |
| 1418 | ret_serial.flags = tport->tp_flags; |
| 1419 | ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE; |
| 1420 | ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800; |
| 1421 | ret_serial.closing_wait = tport->tp_closing_wait; |
| 1422 | |
| 1423 | if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg))) |
| 1424 | return -EFAULT; |
| 1425 | |
| 1426 | return 0; |
| 1427 | } |
| 1428 | |
| 1429 | |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 1430 | static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | struct serial_struct __user *new_arg) |
| 1432 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | struct serial_struct new_serial; |
| 1434 | |
| 1435 | if (copy_from_user(&new_serial, new_arg, sizeof(new_serial))) |
| 1436 | return -EFAULT; |
| 1437 | |
| 1438 | tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | tport->tp_closing_wait = new_serial.closing_wait; |
| 1440 | |
| 1441 | return 0; |
| 1442 | } |
| 1443 | |
| 1444 | |
| 1445 | static void ti_handle_new_msr(struct ti_port *tport, __u8 msr) |
| 1446 | { |
| 1447 | struct async_icount *icount; |
| 1448 | struct tty_struct *tty; |
| 1449 | unsigned long flags; |
| 1450 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1451 | dbg("%s - msr 0x%02X", __func__, msr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | |
| 1453 | if (msr & TI_MSR_DELTA_MASK) { |
| 1454 | spin_lock_irqsave(&tport->tp_lock, flags); |
| 1455 | icount = &tport->tp_icount; |
| 1456 | if (msr & TI_MSR_DELTA_CTS) |
| 1457 | icount->cts++; |
| 1458 | if (msr & TI_MSR_DELTA_DSR) |
| 1459 | icount->dsr++; |
| 1460 | if (msr & TI_MSR_DELTA_CD) |
| 1461 | icount->dcd++; |
| 1462 | if (msr & TI_MSR_DELTA_RI) |
| 1463 | icount->rng++; |
| 1464 | wake_up_interruptible(&tport->tp_msr_wait); |
| 1465 | spin_unlock_irqrestore(&tport->tp_lock, flags); |
| 1466 | } |
| 1467 | |
| 1468 | tport->tp_msr = msr & TI_MSR_MASK; |
| 1469 | |
| 1470 | /* handle CTS flow control */ |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 1471 | tty = tty_port_tty_get(&tport->tp_port->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | if (tty && C_CRTSCTS(tty)) { |
| 1473 | if (msr & TI_MSR_CTS) { |
| 1474 | tty->hw_stopped = 0; |
| 1475 | tty_wakeup(tty); |
| 1476 | } else { |
| 1477 | tty->hw_stopped = 1; |
| 1478 | } |
| 1479 | } |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 1480 | tty_kref_put(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | |
| 1484 | static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush) |
| 1485 | { |
| 1486 | struct ti_device *tdev = tport->tp_tdev; |
| 1487 | struct usb_serial_port *port = tport->tp_port; |
| 1488 | wait_queue_t wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1490 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | |
Oliver Neukum | 0915f49 | 2008-01-23 12:28:45 +0100 | [diff] [blame] | 1492 | spin_lock_irq(&tport->tp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | |
| 1494 | /* wait for data to drain from the buffer */ |
| 1495 | tdev->td_urb_error = 0; |
| 1496 | init_waitqueue_entry(&wait, current); |
| 1497 | add_wait_queue(&tport->tp_write_wait, &wait); |
| 1498 | for (;;) { |
| 1499 | set_current_state(TASK_INTERRUPTIBLE); |
| 1500 | if (ti_buf_data_avail(tport->tp_write_buf) == 0 |
| 1501 | || timeout == 0 || signal_pending(current) |
| 1502 | || tdev->td_urb_error |
Oliver Neukum | 0915f49 | 2008-01-23 12:28:45 +0100 | [diff] [blame] | 1503 | || port->serial->disconnected) /* disconnect */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | break; |
Oliver Neukum | 0915f49 | 2008-01-23 12:28:45 +0100 | [diff] [blame] | 1505 | spin_unlock_irq(&tport->tp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | timeout = schedule_timeout(timeout); |
Oliver Neukum | 0915f49 | 2008-01-23 12:28:45 +0100 | [diff] [blame] | 1507 | spin_lock_irq(&tport->tp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | } |
| 1509 | set_current_state(TASK_RUNNING); |
| 1510 | remove_wait_queue(&tport->tp_write_wait, &wait); |
| 1511 | |
| 1512 | /* flush any remaining data in the buffer */ |
| 1513 | if (flush) |
| 1514 | ti_buf_clear(tport->tp_write_buf); |
| 1515 | |
Oliver Neukum | 0915f49 | 2008-01-23 12:28:45 +0100 | [diff] [blame] | 1516 | spin_unlock_irq(&tport->tp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | |
Oliver Neukum | 0915f49 | 2008-01-23 12:28:45 +0100 | [diff] [blame] | 1518 | mutex_lock(&port->serial->disc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | /* wait for data to drain from the device */ |
| 1520 | /* wait for empty tx register, plus 20 ms */ |
| 1521 | timeout += jiffies; |
| 1522 | tport->tp_lsr &= ~TI_LSR_TX_EMPTY; |
| 1523 | while ((long)(jiffies - timeout) < 0 && !signal_pending(current) |
| 1524 | && !(tport->tp_lsr&TI_LSR_TX_EMPTY) && !tdev->td_urb_error |
Oliver Neukum | 0915f49 | 2008-01-23 12:28:45 +0100 | [diff] [blame] | 1525 | && !port->serial->disconnected) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | if (ti_get_lsr(tport)) |
| 1527 | break; |
Oliver Neukum | 0915f49 | 2008-01-23 12:28:45 +0100 | [diff] [blame] | 1528 | mutex_unlock(&port->serial->disc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | msleep_interruptible(20); |
Oliver Neukum | 0915f49 | 2008-01-23 12:28:45 +0100 | [diff] [blame] | 1530 | mutex_lock(&port->serial->disc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | } |
Oliver Neukum | 0915f49 | 2008-01-23 12:28:45 +0100 | [diff] [blame] | 1532 | mutex_unlock(&port->serial->disc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | |
| 1536 | static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty) |
| 1537 | { |
| 1538 | unsigned long flags; |
| 1539 | |
| 1540 | spin_lock_irqsave(&tport->tp_lock, flags); |
| 1541 | |
| 1542 | if (tport->tp_read_urb_state == TI_READ_URB_RUNNING) |
| 1543 | tport->tp_read_urb_state = TI_READ_URB_STOPPING; |
| 1544 | |
| 1545 | spin_unlock_irqrestore(&tport->tp_lock, flags); |
| 1546 | } |
| 1547 | |
| 1548 | |
| 1549 | static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty) |
| 1550 | { |
| 1551 | struct urb *urb; |
| 1552 | int status = 0; |
| 1553 | unsigned long flags; |
| 1554 | |
| 1555 | spin_lock_irqsave(&tport->tp_lock, flags); |
| 1556 | |
| 1557 | if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) { |
Oliver Neukum | 944dc18 | 2007-05-07 08:33:18 +0200 | [diff] [blame] | 1558 | tport->tp_read_urb_state = TI_READ_URB_RUNNING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | urb = tport->tp_port->read_urb; |
Oliver Neukum | 944dc18 | 2007-05-07 08:33:18 +0200 | [diff] [blame] | 1560 | spin_unlock_irqrestore(&tport->tp_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | urb->complete = ti_bulk_in_callback; |
| 1562 | urb->context = tport; |
| 1563 | urb->dev = tport->tp_port->serial->dev; |
| 1564 | status = usb_submit_urb(urb, GFP_KERNEL); |
Oliver Neukum | 944dc18 | 2007-05-07 08:33:18 +0200 | [diff] [blame] | 1565 | } else { |
| 1566 | tport->tp_read_urb_state = TI_READ_URB_RUNNING; |
| 1567 | spin_unlock_irqrestore(&tport->tp_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | |
| 1570 | return status; |
| 1571 | } |
| 1572 | |
| 1573 | |
| 1574 | static int ti_command_out_sync(struct ti_device *tdev, __u8 command, |
| 1575 | __u16 moduleid, __u16 value, __u8 *data, int size) |
| 1576 | { |
| 1577 | int status; |
| 1578 | |
| 1579 | status = usb_control_msg(tdev->td_serial->dev, |
| 1580 | usb_sndctrlpipe(tdev->td_serial->dev, 0), command, |
| 1581 | (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT), |
| 1582 | value, moduleid, data, size, 1000); |
| 1583 | |
| 1584 | if (status == size) |
| 1585 | status = 0; |
| 1586 | |
| 1587 | if (status > 0) |
| 1588 | status = -ECOMM; |
| 1589 | |
| 1590 | return status; |
| 1591 | } |
| 1592 | |
| 1593 | |
| 1594 | static int ti_command_in_sync(struct ti_device *tdev, __u8 command, |
| 1595 | __u16 moduleid, __u16 value, __u8 *data, int size) |
| 1596 | { |
| 1597 | int status; |
| 1598 | |
| 1599 | status = usb_control_msg(tdev->td_serial->dev, |
| 1600 | usb_rcvctrlpipe(tdev->td_serial->dev, 0), command, |
| 1601 | (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN), |
| 1602 | value, moduleid, data, size, 1000); |
| 1603 | |
| 1604 | if (status == size) |
| 1605 | status = 0; |
| 1606 | |
| 1607 | if (status > 0) |
| 1608 | status = -ECOMM; |
| 1609 | |
| 1610 | return status; |
| 1611 | } |
| 1612 | |
| 1613 | |
| 1614 | static int ti_write_byte(struct ti_device *tdev, unsigned long addr, |
| 1615 | __u8 mask, __u8 byte) |
| 1616 | { |
| 1617 | int status; |
| 1618 | unsigned int size; |
| 1619 | struct ti_write_data_bytes *data; |
| 1620 | struct device *dev = &tdev->td_serial->dev->dev; |
| 1621 | |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 1622 | dbg("%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X", |
| 1623 | __func__, addr, mask, byte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | |
| 1625 | size = sizeof(struct ti_write_data_bytes) + 2; |
| 1626 | data = kmalloc(size, GFP_KERNEL); |
| 1627 | if (!data) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1628 | dev_err(dev, "%s - out of memory\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | return -ENOMEM; |
| 1630 | } |
| 1631 | |
| 1632 | data->bAddrType = TI_RW_DATA_ADDR_XDATA; |
| 1633 | data->bDataType = TI_RW_DATA_BYTE; |
| 1634 | data->bDataCounter = 1; |
| 1635 | data->wBaseAddrHi = cpu_to_be16(addr>>16); |
| 1636 | data->wBaseAddrLo = cpu_to_be16(addr); |
| 1637 | data->bData[0] = mask; |
| 1638 | data->bData[1] = byte; |
| 1639 | |
| 1640 | status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0, |
| 1641 | (__u8 *)data, size); |
| 1642 | |
| 1643 | if (status < 0) |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1644 | dev_err(dev, "%s - failed, %d\n", __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | |
| 1646 | kfree(data); |
| 1647 | |
| 1648 | return status; |
| 1649 | } |
| 1650 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1651 | static int ti_do_download(struct usb_device *dev, int pipe, |
| 1652 | u8 *buffer, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | int pos; |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1655 | u8 cs = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | int done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | struct ti_firmware_header *header; |
Oliver Neukum | 87ea8c8 | 2009-06-30 09:44:24 +0200 | [diff] [blame] | 1658 | int status = 0; |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1659 | int len; |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 1660 | |
| 1661 | for (pos = sizeof(struct ti_firmware_header); pos < size; pos++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | cs = (__u8)(cs + buffer[pos]); |
| 1663 | |
| 1664 | header = (struct ti_firmware_header *)buffer; |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 1665 | header->wLength = cpu_to_le16((__u16)(size |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1666 | - sizeof(struct ti_firmware_header))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | header->bCheckSum = cs; |
| 1668 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1669 | dbg("%s - downloading firmware", __func__); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1670 | for (pos = 0; pos < size; pos += done) { |
| 1671 | len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE); |
| 1672 | status = usb_bulk_msg(dev, pipe, buffer + pos, len, |
| 1673 | &done, 1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | if (status) |
| 1675 | break; |
| 1676 | } |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1677 | return status; |
| 1678 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | |
Chris Adams | 05a3d90 | 2009-01-11 19:48:53 +0000 | [diff] [blame] | 1680 | static int ti_download_firmware(struct ti_device *tdev) |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1681 | { |
Chris Adams | 05a3d90 | 2009-01-11 19:48:53 +0000 | [diff] [blame] | 1682 | int status; |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1683 | int buffer_size; |
| 1684 | __u8 *buffer; |
| 1685 | struct usb_device *dev = tdev->td_serial->dev; |
| 1686 | unsigned int pipe = usb_sndbulkpipe(dev, |
| 1687 | tdev->td_serial->port[0]->bulk_out_endpointAddress); |
| 1688 | const struct firmware *fw_p; |
| 1689 | char buf[32]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | |
Chris Adams | 05a3d90 | 2009-01-11 19:48:53 +0000 | [diff] [blame] | 1691 | /* try ID specific firmware first, then try generic firmware */ |
| 1692 | sprintf(buf, "ti_usb-v%04x-p%04x.fw", dev->descriptor.idVendor, |
| 1693 | dev->descriptor.idProduct); |
| 1694 | if ((status = request_firmware(&fw_p, buf, &dev->dev)) != 0) { |
Chris Adams | cb7a7c6 | 2009-01-11 19:49:00 +0000 | [diff] [blame] | 1695 | buf[0] = '\0'; |
| 1696 | if (dev->descriptor.idVendor == MTS_VENDOR_ID) { |
| 1697 | switch (dev->descriptor.idProduct) { |
| 1698 | case MTS_CDMA_PRODUCT_ID: |
| 1699 | strcpy(buf, "mts_cdma.fw"); |
| 1700 | break; |
| 1701 | case MTS_GSM_PRODUCT_ID: |
| 1702 | strcpy(buf, "mts_gsm.fw"); |
| 1703 | break; |
| 1704 | case MTS_EDGE_PRODUCT_ID: |
| 1705 | strcpy(buf, "mts_edge.fw"); |
| 1706 | break; |
| 1707 | } |
| 1708 | } |
| 1709 | if (buf[0] == '\0') { |
| 1710 | if (tdev->td_is_3410) |
| 1711 | strcpy(buf, "ti_3410.fw"); |
| 1712 | else |
| 1713 | strcpy(buf, "ti_5052.fw"); |
| 1714 | } |
Chris Adams | 05a3d90 | 2009-01-11 19:48:53 +0000 | [diff] [blame] | 1715 | status = request_firmware(&fw_p, buf, &dev->dev); |
| 1716 | } |
| 1717 | if (status) { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1718 | dev_err(&dev->dev, "%s - firmware not found\n", __func__); |
| 1719 | return -ENOENT; |
| 1720 | } |
| 1721 | if (fw_p->size > TI_FIRMWARE_BUF_SIZE) { |
| 1722 | dev_err(&dev->dev, "%s - firmware too large\n", __func__); |
| 1723 | return -ENOENT; |
| 1724 | } |
| 1725 | |
| 1726 | buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header); |
| 1727 | buffer = kmalloc(buffer_size, GFP_KERNEL); |
| 1728 | if (buffer) { |
| 1729 | memcpy(buffer, fw_p->data, fw_p->size); |
| 1730 | memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size); |
Chris Adams | 2bcbe4c | 2008-09-10 14:11:38 -0700 | [diff] [blame] | 1731 | status = ti_do_download(dev, pipe, buffer, fw_p->size); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1732 | kfree(buffer); |
Chris Adams | 05a3d90 | 2009-01-11 19:48:53 +0000 | [diff] [blame] | 1733 | } else { |
| 1734 | status = -ENOMEM; |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1735 | } |
| 1736 | release_firmware(fw_p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | if (status) { |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 1738 | dev_err(&dev->dev, "%s - error downloading firmware, %d\n", |
| 1739 | __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | return status; |
| 1741 | } |
| 1742 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1743 | dbg("%s - download successful", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | |
| 1745 | return 0; |
| 1746 | } |
| 1747 | |
| 1748 | |
| 1749 | /* Circular Buffer Functions */ |
| 1750 | |
| 1751 | /* |
| 1752 | * ti_buf_alloc |
| 1753 | * |
| 1754 | * Allocate a circular buffer and all associated memory. |
| 1755 | */ |
| 1756 | |
| 1757 | static struct circ_buf *ti_buf_alloc(void) |
| 1758 | { |
| 1759 | struct circ_buf *cb; |
| 1760 | |
Robert P. J. Day | 5cbded5 | 2006-12-13 00:35:56 -0800 | [diff] [blame] | 1761 | cb = kmalloc(sizeof(struct circ_buf), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | if (cb == NULL) |
| 1763 | return NULL; |
| 1764 | |
| 1765 | cb->buf = kmalloc(TI_WRITE_BUF_SIZE, GFP_KERNEL); |
| 1766 | if (cb->buf == NULL) { |
| 1767 | kfree(cb); |
| 1768 | return NULL; |
| 1769 | } |
| 1770 | |
| 1771 | ti_buf_clear(cb); |
| 1772 | |
| 1773 | return cb; |
| 1774 | } |
| 1775 | |
| 1776 | |
| 1777 | /* |
| 1778 | * ti_buf_free |
| 1779 | * |
| 1780 | * Free the buffer and all associated memory. |
| 1781 | */ |
| 1782 | |
| 1783 | static void ti_buf_free(struct circ_buf *cb) |
| 1784 | { |
| 1785 | kfree(cb->buf); |
| 1786 | kfree(cb); |
| 1787 | } |
| 1788 | |
| 1789 | |
| 1790 | /* |
| 1791 | * ti_buf_clear |
| 1792 | * |
| 1793 | * Clear out all data in the circular buffer. |
| 1794 | */ |
| 1795 | |
| 1796 | static void ti_buf_clear(struct circ_buf *cb) |
| 1797 | { |
| 1798 | cb->head = cb->tail = 0; |
| 1799 | } |
| 1800 | |
| 1801 | |
| 1802 | /* |
| 1803 | * ti_buf_data_avail |
| 1804 | * |
| 1805 | * Return the number of bytes of data available in the circular |
| 1806 | * buffer. |
| 1807 | */ |
| 1808 | |
| 1809 | static int ti_buf_data_avail(struct circ_buf *cb) |
| 1810 | { |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 1811 | return CIRC_CNT(cb->head, cb->tail, TI_WRITE_BUF_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | } |
| 1813 | |
| 1814 | |
| 1815 | /* |
| 1816 | * ti_buf_space_avail |
| 1817 | * |
| 1818 | * Return the number of bytes of space available in the circular |
| 1819 | * buffer. |
| 1820 | */ |
| 1821 | |
| 1822 | static int ti_buf_space_avail(struct circ_buf *cb) |
| 1823 | { |
Alan Cox | a30fa79 | 2008-07-22 11:15:17 +0100 | [diff] [blame] | 1824 | return CIRC_SPACE(cb->head, cb->tail, TI_WRITE_BUF_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | } |
| 1826 | |
| 1827 | |
| 1828 | /* |
| 1829 | * ti_buf_put |
| 1830 | * |
| 1831 | * Copy data data from a user buffer and put it into the circular buffer. |
| 1832 | * Restrict to the amount of space available. |
| 1833 | * |
| 1834 | * Return the number of bytes copied. |
| 1835 | */ |
| 1836 | |
| 1837 | static int ti_buf_put(struct circ_buf *cb, const char *buf, int count) |
| 1838 | { |
| 1839 | int c, ret = 0; |
| 1840 | |
| 1841 | while (1) { |
| 1842 | c = CIRC_SPACE_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE); |
| 1843 | if (count < c) |
| 1844 | c = count; |
| 1845 | if (c <= 0) |
| 1846 | break; |
| 1847 | memcpy(cb->buf + cb->head, buf, c); |
| 1848 | cb->head = (cb->head + c) & (TI_WRITE_BUF_SIZE-1); |
| 1849 | buf += c; |
| 1850 | count -= c; |
| 1851 | ret += c; |
| 1852 | } |
| 1853 | |
| 1854 | return ret; |
| 1855 | } |
| 1856 | |
| 1857 | |
| 1858 | /* |
| 1859 | * ti_buf_get |
| 1860 | * |
| 1861 | * Get data from the circular buffer and copy to the given buffer. |
| 1862 | * Restrict to the amount of data available. |
| 1863 | * |
| 1864 | * Return the number of bytes copied. |
| 1865 | */ |
| 1866 | |
| 1867 | static int ti_buf_get(struct circ_buf *cb, char *buf, int count) |
| 1868 | { |
| 1869 | int c, ret = 0; |
| 1870 | |
| 1871 | while (1) { |
| 1872 | c = CIRC_CNT_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE); |
| 1873 | if (count < c) |
| 1874 | c = count; |
| 1875 | if (c <= 0) |
| 1876 | break; |
| 1877 | memcpy(buf, cb->buf + cb->tail, c); |
| 1878 | cb->tail = (cb->tail + c) & (TI_WRITE_BUF_SIZE-1); |
| 1879 | buf += c; |
| 1880 | count -= c; |
| 1881 | ret += c; |
| 1882 | } |
| 1883 | |
| 1884 | return ret; |
| 1885 | } |