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