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