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