Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 2 | /* |
| 3 | * usb-serial driver for Quatech SSU-100 |
| 4 | * |
| 5 | * based on ftdi_sio.c and the original serqt_usb.c from Quatech |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #include <linux/errno.h> |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 10 | #include <linux/slab.h> |
| 11 | #include <linux/tty.h> |
| 12 | #include <linux/tty_driver.h> |
| 13 | #include <linux/tty_flip.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/serial.h> |
| 16 | #include <linux/usb.h> |
| 17 | #include <linux/usb/serial.h> |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 18 | #include <linux/serial_reg.h> |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 19 | #include <linux/uaccess.h> |
| 20 | |
| 21 | #define QT_OPEN_CLOSE_CHANNEL 0xca |
| 22 | #define QT_SET_GET_DEVICE 0xc2 |
| 23 | #define QT_SET_GET_REGISTER 0xc0 |
| 24 | #define QT_GET_SET_PREBUF_TRIG_LVL 0xcc |
| 25 | #define QT_SET_ATF 0xcd |
| 26 | #define QT_GET_SET_UART 0xc1 |
| 27 | #define QT_TRANSFER_IN 0xc0 |
| 28 | #define QT_HW_FLOW_CONTROL_MASK 0xc5 |
| 29 | #define QT_SW_FLOW_CONTROL_MASK 0xc6 |
| 30 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 31 | #define SERIAL_MSR_MASK 0xf0 |
| 32 | |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 33 | #define SERIAL_CRTSCTS ((UART_MCR_RTS << 8) | UART_MSR_CTS) |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 34 | |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 35 | #define SERIAL_EVEN_PARITY (UART_LCR_PARITY | UART_LCR_EPAR) |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 36 | |
| 37 | #define MAX_BAUD_RATE 460800 |
| 38 | |
| 39 | #define ATC_DISABLED 0x00 |
| 40 | #define DUPMODE_BITS 0xc0 |
| 41 | #define RR_BITS 0x03 |
| 42 | #define LOOPMODE_BITS 0x41 |
| 43 | #define RS232_MODE 0x00 |
| 44 | #define RTSCTS_TO_CONNECTOR 0x40 |
| 45 | #define CLKS_X4 0x02 |
| 46 | #define FULLPWRBIT 0x00000080 |
| 47 | #define NEXT_BOARD_POWER_BIT 0x00000004 |
| 48 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 49 | #define DRIVER_DESC "Quatech SSU-100 USB to Serial Driver" |
| 50 | |
| 51 | #define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */ |
| 52 | #define QUATECH_SSU100 0xC020 /* SSU100 */ |
| 53 | |
| 54 | static const struct usb_device_id id_table[] = { |
| 55 | {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU100)}, |
| 56 | {} /* Terminating entry */ |
| 57 | }; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 58 | MODULE_DEVICE_TABLE(usb, id_table); |
| 59 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 60 | struct ssu100_port_private { |
Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 61 | spinlock_t status_lock; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 62 | u8 shadowLSR; |
| 63 | u8 shadowMSR; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 64 | }; |
| 65 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 66 | static inline int ssu100_control_msg(struct usb_device *dev, |
| 67 | u8 request, u16 data, u16 index) |
| 68 | { |
| 69 | return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 70 | request, 0x40, data, index, |
| 71 | NULL, 0, 300); |
| 72 | } |
| 73 | |
| 74 | static inline int ssu100_setdevice(struct usb_device *dev, u8 *data) |
| 75 | { |
| 76 | u16 x = ((u16)(data[1] << 8) | (u16)(data[0])); |
| 77 | |
| 78 | return ssu100_control_msg(dev, QT_SET_GET_DEVICE, x, 0); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | static inline int ssu100_getdevice(struct usb_device *dev, u8 *data) |
| 83 | { |
Johan Hovold | 1eac5c2 | 2017-01-12 14:56:22 +0100 | [diff] [blame] | 84 | int ret; |
| 85 | |
| 86 | ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 87 | QT_SET_GET_DEVICE, 0xc0, 0, 0, |
| 88 | data, 3, 300); |
| 89 | if (ret < 3) { |
| 90 | if (ret >= 0) |
| 91 | ret = -EIO; |
| 92 | } |
| 93 | |
| 94 | return ret; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static inline int ssu100_getregister(struct usb_device *dev, |
| 98 | unsigned short uart, |
| 99 | unsigned short reg, |
| 100 | u8 *data) |
| 101 | { |
Johan Hovold | 1eac5c2 | 2017-01-12 14:56:22 +0100 | [diff] [blame] | 102 | int ret; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 103 | |
Johan Hovold | 1eac5c2 | 2017-01-12 14:56:22 +0100 | [diff] [blame] | 104 | ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 105 | QT_SET_GET_REGISTER, 0xc0, reg, |
| 106 | uart, data, sizeof(*data), 300); |
Chengguang Xu | 3391ca1 | 2018-06-25 15:35:18 +0800 | [diff] [blame] | 107 | if (ret < (int)sizeof(*data)) { |
Johan Hovold | 1eac5c2 | 2017-01-12 14:56:22 +0100 | [diff] [blame] | 108 | if (ret >= 0) |
| 109 | ret = -EIO; |
| 110 | } |
| 111 | |
| 112 | return ret; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | |
| 116 | static inline int ssu100_setregister(struct usb_device *dev, |
| 117 | unsigned short uart, |
Bill Pemberton | 556f1a0 | 2010-08-05 17:01:08 -0400 | [diff] [blame] | 118 | unsigned short reg, |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 119 | u16 data) |
| 120 | { |
Bill Pemberton | 556f1a0 | 2010-08-05 17:01:08 -0400 | [diff] [blame] | 121 | u16 value = (data << 8) | reg; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 122 | |
| 123 | return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 124 | QT_SET_GET_REGISTER, 0x40, value, uart, |
| 125 | NULL, 0, 300); |
| 126 | |
| 127 | } |
| 128 | |
| 129 | #define set_mctrl(dev, set) update_mctrl((dev), (set), 0) |
| 130 | #define clear_mctrl(dev, clear) update_mctrl((dev), 0, (clear)) |
| 131 | |
| 132 | /* these do not deal with device that have more than 1 port */ |
| 133 | static inline int update_mctrl(struct usb_device *dev, unsigned int set, |
| 134 | unsigned int clear) |
| 135 | { |
| 136 | unsigned urb_value; |
| 137 | int result; |
| 138 | |
| 139 | if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) { |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 140 | dev_dbg(&dev->dev, "%s - DTR|RTS not being set|cleared\n", __func__); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 141 | return 0; /* no change */ |
| 142 | } |
| 143 | |
| 144 | clear &= ~set; /* 'set' takes precedence over 'clear' */ |
| 145 | urb_value = 0; |
| 146 | if (set & TIOCM_DTR) |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 147 | urb_value |= UART_MCR_DTR; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 148 | if (set & TIOCM_RTS) |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 149 | urb_value |= UART_MCR_RTS; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 150 | |
Bill Pemberton | 556f1a0 | 2010-08-05 17:01:08 -0400 | [diff] [blame] | 151 | result = ssu100_setregister(dev, 0, UART_MCR, urb_value); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 152 | if (result < 0) |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 153 | dev_dbg(&dev->dev, "%s Error from MODEM_CTRL urb\n", __func__); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 154 | |
| 155 | return result; |
| 156 | } |
| 157 | |
| 158 | static int ssu100_initdevice(struct usb_device *dev) |
| 159 | { |
| 160 | u8 *data; |
| 161 | int result = 0; |
| 162 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 163 | data = kzalloc(3, GFP_KERNEL); |
| 164 | if (!data) |
| 165 | return -ENOMEM; |
| 166 | |
| 167 | result = ssu100_getdevice(dev, data); |
| 168 | if (result < 0) { |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 169 | dev_dbg(&dev->dev, "%s - get_device failed %i\n", __func__, result); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 170 | goto out; |
| 171 | } |
| 172 | |
| 173 | data[1] &= ~FULLPWRBIT; |
| 174 | |
| 175 | result = ssu100_setdevice(dev, data); |
| 176 | if (result < 0) { |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 177 | dev_dbg(&dev->dev, "%s - setdevice failed %i\n", __func__, result); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 178 | goto out; |
| 179 | } |
| 180 | |
| 181 | result = ssu100_control_msg(dev, QT_GET_SET_PREBUF_TRIG_LVL, 128, 0); |
| 182 | if (result < 0) { |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 183 | dev_dbg(&dev->dev, "%s - set prebuffer level failed %i\n", __func__, result); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 184 | goto out; |
| 185 | } |
| 186 | |
| 187 | result = ssu100_control_msg(dev, QT_SET_ATF, ATC_DISABLED, 0); |
| 188 | if (result < 0) { |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 189 | dev_dbg(&dev->dev, "%s - set ATFprebuffer level failed %i\n", __func__, result); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 190 | goto out; |
| 191 | } |
| 192 | |
| 193 | result = ssu100_getdevice(dev, data); |
| 194 | if (result < 0) { |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 195 | dev_dbg(&dev->dev, "%s - get_device failed %i\n", __func__, result); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 196 | goto out; |
| 197 | } |
| 198 | |
| 199 | data[0] &= ~(RR_BITS | DUPMODE_BITS); |
| 200 | data[0] |= CLKS_X4; |
| 201 | data[1] &= ~(LOOPMODE_BITS); |
| 202 | data[1] |= RS232_MODE; |
| 203 | |
| 204 | result = ssu100_setdevice(dev, data); |
| 205 | if (result < 0) { |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 206 | dev_dbg(&dev->dev, "%s - setdevice failed %i\n", __func__, result); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 207 | goto out; |
| 208 | } |
| 209 | |
| 210 | out: kfree(data); |
| 211 | return result; |
| 212 | |
| 213 | } |
| 214 | |
| 215 | |
| 216 | static void ssu100_set_termios(struct tty_struct *tty, |
| 217 | struct usb_serial_port *port, |
| 218 | struct ktermios *old_termios) |
| 219 | { |
| 220 | struct usb_device *dev = port->serial->dev; |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 221 | struct ktermios *termios = &tty->termios; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 222 | u16 baud, divisor, remainder; |
| 223 | unsigned int cflag = termios->c_cflag; |
| 224 | u16 urb_value = 0; /* will hold the new flags */ |
| 225 | int result; |
| 226 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 227 | if (cflag & PARENB) { |
| 228 | if (cflag & PARODD) |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 229 | urb_value |= UART_LCR_PARITY; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 230 | else |
| 231 | urb_value |= SERIAL_EVEN_PARITY; |
| 232 | } |
| 233 | |
| 234 | switch (cflag & CSIZE) { |
| 235 | case CS5: |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 236 | urb_value |= UART_LCR_WLEN5; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 237 | break; |
| 238 | case CS6: |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 239 | urb_value |= UART_LCR_WLEN6; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 240 | break; |
| 241 | case CS7: |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 242 | urb_value |= UART_LCR_WLEN7; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 243 | break; |
| 244 | default: |
| 245 | case CS8: |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 246 | urb_value |= UART_LCR_WLEN8; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 247 | break; |
| 248 | } |
| 249 | |
| 250 | baud = tty_get_baud_rate(tty); |
| 251 | if (!baud) |
| 252 | baud = 9600; |
| 253 | |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 254 | dev_dbg(&port->dev, "%s - got baud = %d\n", __func__, baud); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 255 | |
| 256 | |
| 257 | divisor = MAX_BAUD_RATE / baud; |
| 258 | remainder = MAX_BAUD_RATE % baud; |
| 259 | if (((remainder * 2) >= baud) && (baud != 110)) |
| 260 | divisor++; |
| 261 | |
| 262 | urb_value = urb_value << 8; |
| 263 | |
| 264 | result = ssu100_control_msg(dev, QT_GET_SET_UART, divisor, urb_value); |
| 265 | if (result < 0) |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 266 | dev_dbg(&port->dev, "%s - set uart failed\n", __func__); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 267 | |
| 268 | if (cflag & CRTSCTS) |
| 269 | result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK, |
| 270 | SERIAL_CRTSCTS, 0); |
| 271 | else |
| 272 | result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK, |
| 273 | 0, 0); |
| 274 | if (result < 0) |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 275 | dev_dbg(&port->dev, "%s - set HW flow control failed\n", __func__); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 276 | |
| 277 | if (I_IXOFF(tty) || I_IXON(tty)) { |
| 278 | u16 x = ((u16)(START_CHAR(tty) << 8) | (u16)(STOP_CHAR(tty))); |
| 279 | |
| 280 | result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK, |
| 281 | x, 0); |
| 282 | } else |
| 283 | result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK, |
| 284 | 0, 0); |
| 285 | |
| 286 | if (result < 0) |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 287 | dev_dbg(&port->dev, "%s - set SW flow control failed\n", __func__); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 288 | |
| 289 | } |
| 290 | |
| 291 | |
| 292 | static int ssu100_open(struct tty_struct *tty, struct usb_serial_port *port) |
| 293 | { |
| 294 | struct usb_device *dev = port->serial->dev; |
| 295 | struct ssu100_port_private *priv = usb_get_serial_port_data(port); |
| 296 | u8 *data; |
| 297 | int result; |
Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 298 | unsigned long flags; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 299 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 300 | data = kzalloc(2, GFP_KERNEL); |
| 301 | if (!data) |
| 302 | return -ENOMEM; |
| 303 | |
| 304 | result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 305 | QT_OPEN_CLOSE_CHANNEL, |
| 306 | QT_TRANSFER_IN, 0x01, |
| 307 | 0, data, 2, 300); |
Johan Hovold | 1eac5c2 | 2017-01-12 14:56:22 +0100 | [diff] [blame] | 308 | if (result < 2) { |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 309 | dev_dbg(&port->dev, "%s - open failed %i\n", __func__, result); |
Johan Hovold | 1eac5c2 | 2017-01-12 14:56:22 +0100 | [diff] [blame] | 310 | if (result >= 0) |
| 311 | result = -EIO; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 312 | kfree(data); |
| 313 | return result; |
| 314 | } |
| 315 | |
Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 316 | spin_lock_irqsave(&priv->status_lock, flags); |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 317 | priv->shadowLSR = data[0]; |
| 318 | priv->shadowMSR = data[1]; |
Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 319 | spin_unlock_irqrestore(&priv->status_lock, flags); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 320 | |
| 321 | kfree(data); |
| 322 | |
| 323 | /* set to 9600 */ |
| 324 | result = ssu100_control_msg(dev, QT_GET_SET_UART, 0x30, 0x0300); |
| 325 | if (result < 0) |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 326 | dev_dbg(&port->dev, "%s - set uart failed\n", __func__); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 327 | |
| 328 | if (tty) |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 329 | ssu100_set_termios(tty, port, &tty->termios); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 330 | |
| 331 | return usb_serial_generic_open(tty, port); |
| 332 | } |
| 333 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 334 | static int ssu100_attach(struct usb_serial *serial) |
| 335 | { |
Johan Hovold | 638b9e1 | 2012-10-17 16:31:34 +0200 | [diff] [blame] | 336 | return ssu100_initdevice(serial->dev); |
| 337 | } |
| 338 | |
| 339 | static int ssu100_port_probe(struct usb_serial_port *port) |
| 340 | { |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 341 | struct ssu100_port_private *priv; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 342 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 343 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
Johan Hovold | 638b9e1 | 2012-10-17 16:31:34 +0200 | [diff] [blame] | 344 | if (!priv) |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 345 | return -ENOMEM; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 346 | |
Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 347 | spin_lock_init(&priv->status_lock); |
Johan Hovold | 638b9e1 | 2012-10-17 16:31:34 +0200 | [diff] [blame] | 348 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 349 | usb_set_serial_port_data(port, priv); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 350 | |
Johan Hovold | 638b9e1 | 2012-10-17 16:31:34 +0200 | [diff] [blame] | 351 | return 0; |
| 352 | } |
| 353 | |
Uwe Kleine-König | c5d1448 | 2021-02-08 15:31:49 +0100 | [diff] [blame] | 354 | static void ssu100_port_remove(struct usb_serial_port *port) |
Johan Hovold | 638b9e1 | 2012-10-17 16:31:34 +0200 | [diff] [blame] | 355 | { |
| 356 | struct ssu100_port_private *priv; |
| 357 | |
| 358 | priv = usb_get_serial_port_data(port); |
| 359 | kfree(priv); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 360 | } |
| 361 | |
Alan Cox | 60b33c1 | 2011-02-14 16:26:14 +0000 | [diff] [blame] | 362 | static int ssu100_tiocmget(struct tty_struct *tty) |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 363 | { |
| 364 | struct usb_serial_port *port = tty->driver_data; |
| 365 | struct usb_device *dev = port->serial->dev; |
| 366 | u8 *d; |
| 367 | int r; |
| 368 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 369 | d = kzalloc(2, GFP_KERNEL); |
| 370 | if (!d) |
| 371 | return -ENOMEM; |
| 372 | |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 373 | r = ssu100_getregister(dev, 0, UART_MCR, d); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 374 | if (r < 0) |
| 375 | goto mget_out; |
| 376 | |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 377 | r = ssu100_getregister(dev, 0, UART_MSR, d+1); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 378 | if (r < 0) |
| 379 | goto mget_out; |
| 380 | |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 381 | r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) | |
| 382 | (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) | |
| 383 | (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) | |
| 384 | (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) | |
| 385 | (d[1] & UART_MSR_RI ? TIOCM_RI : 0) | |
| 386 | (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 387 | |
| 388 | mget_out: |
| 389 | kfree(d); |
| 390 | return r; |
| 391 | } |
| 392 | |
Alan Cox | 20b9d17 | 2011-02-14 16:26:50 +0000 | [diff] [blame] | 393 | static int ssu100_tiocmset(struct tty_struct *tty, |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 394 | unsigned int set, unsigned int clear) |
| 395 | { |
| 396 | struct usb_serial_port *port = tty->driver_data; |
| 397 | struct usb_device *dev = port->serial->dev; |
| 398 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 399 | return update_mctrl(dev, set, clear); |
| 400 | } |
| 401 | |
| 402 | static void ssu100_dtr_rts(struct usb_serial_port *port, int on) |
| 403 | { |
| 404 | struct usb_device *dev = port->serial->dev; |
| 405 | |
Johan Hovold | b2ca699 | 2013-02-13 17:53:28 +0100 | [diff] [blame] | 406 | /* Disable flow control */ |
| 407 | if (!on) { |
| 408 | if (ssu100_setregister(dev, 0, UART_MCR, 0) < 0) |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 409 | dev_err(&port->dev, "error from flowcontrol urb\n"); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 410 | } |
Johan Hovold | b2ca699 | 2013-02-13 17:53:28 +0100 | [diff] [blame] | 411 | /* drop RTS and DTR */ |
| 412 | if (on) |
| 413 | set_mctrl(dev, TIOCM_DTR | TIOCM_RTS); |
| 414 | else |
| 415 | clear_mctrl(dev, TIOCM_DTR | TIOCM_RTS); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 416 | } |
| 417 | |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 418 | static void ssu100_update_msr(struct usb_serial_port *port, u8 msr) |
| 419 | { |
| 420 | struct ssu100_port_private *priv = usb_get_serial_port_data(port); |
| 421 | unsigned long flags; |
| 422 | |
| 423 | spin_lock_irqsave(&priv->status_lock, flags); |
| 424 | priv->shadowMSR = msr; |
| 425 | spin_unlock_irqrestore(&priv->status_lock, flags); |
| 426 | |
| 427 | if (msr & UART_MSR_ANY_DELTA) { |
| 428 | /* update input line counters */ |
| 429 | if (msr & UART_MSR_DCTS) |
Johan Hovold | 31ecdb6b | 2013-03-21 12:37:31 +0100 | [diff] [blame] | 430 | port->icount.cts++; |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 431 | if (msr & UART_MSR_DDSR) |
Johan Hovold | 31ecdb6b | 2013-03-21 12:37:31 +0100 | [diff] [blame] | 432 | port->icount.dsr++; |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 433 | if (msr & UART_MSR_DDCD) |
Johan Hovold | 31ecdb6b | 2013-03-21 12:37:31 +0100 | [diff] [blame] | 434 | port->icount.dcd++; |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 435 | if (msr & UART_MSR_TERI) |
Johan Hovold | 31ecdb6b | 2013-03-21 12:37:31 +0100 | [diff] [blame] | 436 | port->icount.rng++; |
Johan Hovold | c24c838 | 2013-03-21 12:37:32 +0100 | [diff] [blame] | 437 | wake_up_interruptible(&port->port.delta_msr_wait); |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 441 | static void ssu100_update_lsr(struct usb_serial_port *port, u8 lsr, |
| 442 | char *tty_flag) |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 443 | { |
| 444 | struct ssu100_port_private *priv = usb_get_serial_port_data(port); |
| 445 | unsigned long flags; |
| 446 | |
| 447 | spin_lock_irqsave(&priv->status_lock, flags); |
| 448 | priv->shadowLSR = lsr; |
| 449 | spin_unlock_irqrestore(&priv->status_lock, flags); |
| 450 | |
Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 451 | *tty_flag = TTY_NORMAL; |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 452 | if (lsr & UART_LSR_BRK_ERROR_BITS) { |
Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 453 | /* we always want to update icount, but we only want to |
| 454 | * update tty_flag for one case */ |
| 455 | if (lsr & UART_LSR_BI) { |
Johan Hovold | 31ecdb6b | 2013-03-21 12:37:31 +0100 | [diff] [blame] | 456 | port->icount.brk++; |
Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 457 | *tty_flag = TTY_BREAK; |
| 458 | usb_serial_handle_break(port); |
| 459 | } |
| 460 | if (lsr & UART_LSR_PE) { |
Johan Hovold | 31ecdb6b | 2013-03-21 12:37:31 +0100 | [diff] [blame] | 461 | port->icount.parity++; |
Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 462 | if (*tty_flag == TTY_NORMAL) |
| 463 | *tty_flag = TTY_PARITY; |
| 464 | } |
| 465 | if (lsr & UART_LSR_FE) { |
Johan Hovold | 31ecdb6b | 2013-03-21 12:37:31 +0100 | [diff] [blame] | 466 | port->icount.frame++; |
Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 467 | if (*tty_flag == TTY_NORMAL) |
| 468 | *tty_flag = TTY_FRAME; |
| 469 | } |
Johan Hovold | 75bcbf2 | 2014-11-18 11:25:21 +0100 | [diff] [blame] | 470 | if (lsr & UART_LSR_OE) { |
Johan Hovold | 31ecdb6b | 2013-03-21 12:37:31 +0100 | [diff] [blame] | 471 | port->icount.overrun++; |
Johan Hovold | 75bcbf2 | 2014-11-18 11:25:21 +0100 | [diff] [blame] | 472 | tty_insert_flip_char(&port->port, 0, TTY_OVERRUN); |
Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 473 | } |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 474 | } |
Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 475 | |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 476 | } |
| 477 | |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 478 | static void ssu100_process_read_urb(struct urb *urb) |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 479 | { |
Bill Pemberton | f7043ec | 2010-10-21 14:43:05 -0400 | [diff] [blame] | 480 | struct usb_serial_port *port = urb->context; |
Johan Hovold | eb0c68e | 2020-07-08 14:50:00 +0200 | [diff] [blame] | 481 | char *packet = urb->transfer_buffer; |
Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 482 | char flag = TTY_NORMAL; |
Bill Pemberton | f7043ec | 2010-10-21 14:43:05 -0400 | [diff] [blame] | 483 | u32 len = urb->actual_length; |
| 484 | int i; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 485 | char *ch; |
| 486 | |
Bill Pemberton | 9b2cef3 | 2010-08-05 17:01:06 -0400 | [diff] [blame] | 487 | if ((len >= 4) && |
| 488 | (packet[0] == 0x1b) && (packet[1] == 0x1b) && |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 489 | ((packet[2] == 0x00) || (packet[2] == 0x01))) { |
Johan Hovold | 75bcbf2 | 2014-11-18 11:25:21 +0100 | [diff] [blame] | 490 | if (packet[2] == 0x00) |
Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 491 | ssu100_update_lsr(port, packet[3], &flag); |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 492 | if (packet[2] == 0x01) |
| 493 | ssu100_update_msr(port, packet[3]); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 494 | |
| 495 | len -= 4; |
| 496 | ch = packet + 4; |
| 497 | } else |
| 498 | ch = packet; |
| 499 | |
| 500 | if (!len) |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 501 | return; /* status only */ |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 502 | |
Johan Hovold | 37ae231 | 2020-07-08 14:49:54 +0200 | [diff] [blame] | 503 | if (port->sysrq) { |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 504 | for (i = 0; i < len; i++, ch++) { |
Dmitry Torokhov | 6ee9f4b | 2010-08-17 21:15:47 -0700 | [diff] [blame] | 505 | if (!usb_serial_handle_sysrq_char(port, *ch)) |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 506 | tty_insert_flip_char(&port->port, *ch, flag); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 507 | } |
Johan Hovold | 37ae231 | 2020-07-08 14:49:54 +0200 | [diff] [blame] | 508 | } else { |
Jiri Slaby | 2f69335 | 2013-01-03 15:53:02 +0100 | [diff] [blame] | 509 | tty_insert_flip_string_fixed_flag(&port->port, ch, flag, len); |
Johan Hovold | 37ae231 | 2020-07-08 14:49:54 +0200 | [diff] [blame] | 510 | } |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 511 | |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 512 | tty_flip_buffer_push(&port->port); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 513 | } |
| 514 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 515 | static struct usb_serial_driver ssu100_device = { |
| 516 | .driver = { |
| 517 | .owner = THIS_MODULE, |
| 518 | .name = "ssu100", |
| 519 | }, |
| 520 | .description = DRIVER_DESC, |
| 521 | .id_table = id_table, |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 522 | .num_ports = 1, |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 523 | .open = ssu100_open, |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 524 | .attach = ssu100_attach, |
Johan Hovold | 638b9e1 | 2012-10-17 16:31:34 +0200 | [diff] [blame] | 525 | .port_probe = ssu100_port_probe, |
| 526 | .port_remove = ssu100_port_remove, |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 527 | .dtr_rts = ssu100_dtr_rts, |
| 528 | .process_read_urb = ssu100_process_read_urb, |
| 529 | .tiocmget = ssu100_tiocmget, |
| 530 | .tiocmset = ssu100_tiocmset, |
Johan Hovold | c24c838 | 2013-03-21 12:37:32 +0100 | [diff] [blame] | 531 | .tiocmiwait = usb_serial_generic_tiocmiwait, |
Johan Hovold | 31ecdb6b | 2013-03-21 12:37:31 +0100 | [diff] [blame] | 532 | .get_icount = usb_serial_generic_get_icount, |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 533 | .set_termios = ssu100_set_termios, |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 534 | }; |
| 535 | |
Alan Stern | d860322 | 2012-02-23 14:57:25 -0500 | [diff] [blame] | 536 | static struct usb_serial_driver * const serial_drivers[] = { |
| 537 | &ssu100_device, NULL |
| 538 | }; |
| 539 | |
Greg Kroah-Hartman | 68e2411 | 2012-05-08 15:46:14 -0700 | [diff] [blame] | 540 | module_usb_serial_driver(serial_drivers, id_table); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 541 | |
| 542 | MODULE_DESCRIPTION(DRIVER_DESC); |
Johan Hovold | 627cfa8 | 2017-11-03 18:12:08 +0100 | [diff] [blame] | 543 | MODULE_LICENSE("GPL v2"); |