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