| 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> | 
|  | 9 | #include <linux/init.h> | 
|  | 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 | 3c35b00 | 2010-08-25 18:21:23 -0400 | [diff] [blame] | 49 | static int debug; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 50 |  | 
|  | 51 | /* Version Information */ | 
|  | 52 | #define DRIVER_VERSION "v0.1" | 
|  | 53 | #define DRIVER_DESC "Quatech SSU-100 USB to Serial Driver" | 
|  | 54 |  | 
|  | 55 | #define	USB_VENDOR_ID_QUATECH	0x061d	/* Quatech VID */ | 
|  | 56 | #define QUATECH_SSU100	0xC020	/* SSU100 */ | 
|  | 57 |  | 
|  | 58 | static const struct usb_device_id id_table[] = { | 
|  | 59 | {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU100)}, | 
|  | 60 | {}			/* Terminating entry */ | 
|  | 61 | }; | 
|  | 62 |  | 
|  | 63 | MODULE_DEVICE_TABLE(usb, id_table); | 
|  | 64 |  | 
|  | 65 |  | 
|  | 66 | static struct usb_driver ssu100_driver = { | 
|  | 67 | .name			       = "ssu100", | 
|  | 68 | .probe			       = usb_serial_probe, | 
|  | 69 | .disconnect		       = usb_serial_disconnect, | 
|  | 70 | .id_table		       = id_table, | 
|  | 71 | .suspend		       = usb_serial_suspend, | 
|  | 72 | .resume			       = usb_serial_resume, | 
|  | 73 | .no_dynamic_id		       = 1, | 
|  | 74 | .supports_autosuspend	       = 1, | 
|  | 75 | }; | 
|  | 76 |  | 
|  | 77 | struct ssu100_port_private { | 
| Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 78 | spinlock_t status_lock; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 79 | u8 shadowLSR; | 
|  | 80 | u8 shadowMSR; | 
|  | 81 | wait_queue_head_t delta_msr_wait; /* Used for TIOCMIWAIT */ | 
| Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 82 | struct async_icount icount; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 83 | }; | 
|  | 84 |  | 
|  | 85 | static void ssu100_release(struct usb_serial *serial) | 
|  | 86 | { | 
|  | 87 | struct ssu100_port_private *priv = usb_get_serial_port_data(*serial->port); | 
|  | 88 |  | 
|  | 89 | dbg("%s", __func__); | 
|  | 90 | kfree(priv); | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | static inline int ssu100_control_msg(struct usb_device *dev, | 
|  | 94 | u8 request, u16 data, u16 index) | 
|  | 95 | { | 
|  | 96 | return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), | 
|  | 97 | request, 0x40, data, index, | 
|  | 98 | NULL, 0, 300); | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | static inline int ssu100_setdevice(struct usb_device *dev, u8 *data) | 
|  | 102 | { | 
|  | 103 | u16 x = ((u16)(data[1] << 8) | (u16)(data[0])); | 
|  | 104 |  | 
|  | 105 | return ssu100_control_msg(dev, QT_SET_GET_DEVICE, x, 0); | 
|  | 106 | } | 
|  | 107 |  | 
|  | 108 |  | 
|  | 109 | static inline int ssu100_getdevice(struct usb_device *dev, u8 *data) | 
|  | 110 | { | 
|  | 111 | return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), | 
|  | 112 | QT_SET_GET_DEVICE, 0xc0, 0, 0, | 
|  | 113 | data, 3, 300); | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | static inline int ssu100_getregister(struct usb_device *dev, | 
|  | 117 | unsigned short uart, | 
|  | 118 | unsigned short reg, | 
|  | 119 | u8 *data) | 
|  | 120 | { | 
|  | 121 | return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), | 
|  | 122 | QT_SET_GET_REGISTER, 0xc0, reg, | 
|  | 123 | uart, data, sizeof(*data), 300); | 
|  | 124 |  | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 |  | 
|  | 128 | static inline int ssu100_setregister(struct usb_device *dev, | 
|  | 129 | unsigned short uart, | 
| Bill Pemberton | 556f1a0 | 2010-08-05 17:01:08 -0400 | [diff] [blame] | 130 | unsigned short reg, | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 131 | u16 data) | 
|  | 132 | { | 
| Bill Pemberton | 556f1a0 | 2010-08-05 17:01:08 -0400 | [diff] [blame] | 133 | u16 value = (data << 8) | reg; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 134 |  | 
|  | 135 | return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), | 
|  | 136 | QT_SET_GET_REGISTER, 0x40, value, uart, | 
|  | 137 | NULL, 0, 300); | 
|  | 138 |  | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | #define set_mctrl(dev, set)		update_mctrl((dev), (set), 0) | 
|  | 142 | #define clear_mctrl(dev, clear)	update_mctrl((dev), 0, (clear)) | 
|  | 143 |  | 
|  | 144 | /* these do not deal with device that have more than 1 port */ | 
|  | 145 | static inline int update_mctrl(struct usb_device *dev, unsigned int set, | 
|  | 146 | unsigned int clear) | 
|  | 147 | { | 
|  | 148 | unsigned urb_value; | 
|  | 149 | int result; | 
|  | 150 |  | 
|  | 151 | if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) { | 
|  | 152 | dbg("%s - DTR|RTS not being set|cleared", __func__); | 
|  | 153 | return 0;	/* no change */ | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | clear &= ~set;	/* 'set' takes precedence over 'clear' */ | 
|  | 157 | urb_value = 0; | 
|  | 158 | if (set & TIOCM_DTR) | 
| Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 159 | urb_value |= UART_MCR_DTR; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 160 | if (set & TIOCM_RTS) | 
| Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 161 | urb_value |= UART_MCR_RTS; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 162 |  | 
| Bill Pemberton | 556f1a0 | 2010-08-05 17:01:08 -0400 | [diff] [blame] | 163 | result = ssu100_setregister(dev, 0, UART_MCR, urb_value); | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 164 | if (result < 0) | 
|  | 165 | dbg("%s Error from MODEM_CTRL urb", __func__); | 
|  | 166 |  | 
|  | 167 | return result; | 
|  | 168 | } | 
|  | 169 |  | 
|  | 170 | static int ssu100_initdevice(struct usb_device *dev) | 
|  | 171 | { | 
|  | 172 | u8 *data; | 
|  | 173 | int result = 0; | 
|  | 174 |  | 
|  | 175 | dbg("%s", __func__); | 
|  | 176 |  | 
|  | 177 | data = kzalloc(3, GFP_KERNEL); | 
|  | 178 | if (!data) | 
|  | 179 | return -ENOMEM; | 
|  | 180 |  | 
|  | 181 | result = ssu100_getdevice(dev, data); | 
|  | 182 | if (result < 0) { | 
|  | 183 | dbg("%s - get_device failed %i", __func__, result); | 
|  | 184 | goto out; | 
|  | 185 | } | 
|  | 186 |  | 
|  | 187 | data[1] &= ~FULLPWRBIT; | 
|  | 188 |  | 
|  | 189 | result = ssu100_setdevice(dev, data); | 
|  | 190 | if (result < 0) { | 
|  | 191 | dbg("%s - setdevice failed %i", __func__, result); | 
|  | 192 | goto out; | 
|  | 193 | } | 
|  | 194 |  | 
|  | 195 | result = ssu100_control_msg(dev, QT_GET_SET_PREBUF_TRIG_LVL, 128, 0); | 
|  | 196 | if (result < 0) { | 
|  | 197 | dbg("%s - set prebuffer level failed %i", __func__, result); | 
|  | 198 | goto out; | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | result = ssu100_control_msg(dev, QT_SET_ATF, ATC_DISABLED, 0); | 
|  | 202 | if (result < 0) { | 
|  | 203 | dbg("%s - set ATFprebuffer level failed %i", __func__, result); | 
|  | 204 | goto out; | 
|  | 205 | } | 
|  | 206 |  | 
|  | 207 | result = ssu100_getdevice(dev, data); | 
|  | 208 | if (result < 0) { | 
|  | 209 | dbg("%s - get_device failed %i", __func__, result); | 
|  | 210 | goto out; | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | data[0] &= ~(RR_BITS | DUPMODE_BITS); | 
|  | 214 | data[0] |= CLKS_X4; | 
|  | 215 | data[1] &= ~(LOOPMODE_BITS); | 
|  | 216 | data[1] |= RS232_MODE; | 
|  | 217 |  | 
|  | 218 | result = ssu100_setdevice(dev, data); | 
|  | 219 | if (result < 0) { | 
|  | 220 | dbg("%s - setdevice failed %i", __func__, result); | 
|  | 221 | goto out; | 
|  | 222 | } | 
|  | 223 |  | 
|  | 224 | out:	kfree(data); | 
|  | 225 | return result; | 
|  | 226 |  | 
|  | 227 | } | 
|  | 228 |  | 
|  | 229 |  | 
|  | 230 | static void ssu100_set_termios(struct tty_struct *tty, | 
|  | 231 | struct usb_serial_port *port, | 
|  | 232 | struct ktermios *old_termios) | 
|  | 233 | { | 
|  | 234 | struct usb_device *dev = port->serial->dev; | 
|  | 235 | struct ktermios *termios = tty->termios; | 
|  | 236 | u16 baud, divisor, remainder; | 
|  | 237 | unsigned int cflag = termios->c_cflag; | 
|  | 238 | u16 urb_value = 0; /* will hold the new flags */ | 
|  | 239 | int result; | 
|  | 240 |  | 
|  | 241 | dbg("%s", __func__); | 
|  | 242 |  | 
|  | 243 | if (cflag & PARENB) { | 
|  | 244 | if (cflag & PARODD) | 
| Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 245 | urb_value |= UART_LCR_PARITY; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 246 | else | 
|  | 247 | urb_value |= SERIAL_EVEN_PARITY; | 
|  | 248 | } | 
|  | 249 |  | 
|  | 250 | switch (cflag & CSIZE) { | 
|  | 251 | case CS5: | 
| Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 252 | urb_value |= UART_LCR_WLEN5; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 253 | break; | 
|  | 254 | case CS6: | 
| Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 255 | urb_value |= UART_LCR_WLEN6; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 256 | break; | 
|  | 257 | case CS7: | 
| Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 258 | urb_value |= UART_LCR_WLEN7; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 259 | break; | 
|  | 260 | default: | 
|  | 261 | case CS8: | 
| Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 262 | urb_value |= UART_LCR_WLEN8; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 263 | break; | 
|  | 264 | } | 
|  | 265 |  | 
|  | 266 | baud = tty_get_baud_rate(tty); | 
|  | 267 | if (!baud) | 
|  | 268 | baud = 9600; | 
|  | 269 |  | 
|  | 270 | dbg("%s - got baud = %d\n", __func__, baud); | 
|  | 271 |  | 
|  | 272 |  | 
|  | 273 | divisor = MAX_BAUD_RATE / baud; | 
|  | 274 | remainder = MAX_BAUD_RATE % baud; | 
|  | 275 | if (((remainder * 2) >= baud) && (baud != 110)) | 
|  | 276 | divisor++; | 
|  | 277 |  | 
|  | 278 | urb_value = urb_value << 8; | 
|  | 279 |  | 
|  | 280 | result = ssu100_control_msg(dev, QT_GET_SET_UART, divisor, urb_value); | 
|  | 281 | if (result < 0) | 
|  | 282 | dbg("%s - set uart failed", __func__); | 
|  | 283 |  | 
|  | 284 | if (cflag & CRTSCTS) | 
|  | 285 | result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK, | 
|  | 286 | SERIAL_CRTSCTS, 0); | 
|  | 287 | else | 
|  | 288 | result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK, | 
|  | 289 | 0, 0); | 
|  | 290 | if (result < 0) | 
|  | 291 | dbg("%s - set HW flow control failed", __func__); | 
|  | 292 |  | 
|  | 293 | if (I_IXOFF(tty) || I_IXON(tty)) { | 
|  | 294 | u16 x = ((u16)(START_CHAR(tty) << 8) | (u16)(STOP_CHAR(tty))); | 
|  | 295 |  | 
|  | 296 | result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK, | 
|  | 297 | x, 0); | 
|  | 298 | } else | 
|  | 299 | result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK, | 
|  | 300 | 0, 0); | 
|  | 301 |  | 
|  | 302 | if (result < 0) | 
|  | 303 | dbg("%s - set SW flow control failed", __func__); | 
|  | 304 |  | 
|  | 305 | } | 
|  | 306 |  | 
|  | 307 |  | 
|  | 308 | static int ssu100_open(struct tty_struct *tty, struct usb_serial_port *port) | 
|  | 309 | { | 
|  | 310 | struct usb_device *dev = port->serial->dev; | 
|  | 311 | struct ssu100_port_private *priv = usb_get_serial_port_data(port); | 
|  | 312 | u8 *data; | 
|  | 313 | int result; | 
| Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 314 | unsigned long flags; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 315 |  | 
|  | 316 | dbg("%s - port %d", __func__, port->number); | 
|  | 317 |  | 
|  | 318 | data = kzalloc(2, GFP_KERNEL); | 
|  | 319 | if (!data) | 
|  | 320 | return -ENOMEM; | 
|  | 321 |  | 
|  | 322 | result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), | 
|  | 323 | QT_OPEN_CLOSE_CHANNEL, | 
|  | 324 | QT_TRANSFER_IN, 0x01, | 
|  | 325 | 0, data, 2, 300); | 
|  | 326 | if (result < 0) { | 
|  | 327 | dbg("%s - open failed %i", __func__, result); | 
|  | 328 | kfree(data); | 
|  | 329 | return result; | 
|  | 330 | } | 
|  | 331 |  | 
| Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 332 | spin_lock_irqsave(&priv->status_lock, flags); | 
| Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 333 | priv->shadowLSR = data[0]; | 
|  | 334 | priv->shadowMSR = data[1]; | 
| Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 335 | spin_unlock_irqrestore(&priv->status_lock, flags); | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 336 |  | 
|  | 337 | kfree(data); | 
|  | 338 |  | 
|  | 339 | /* set to 9600 */ | 
|  | 340 | result = ssu100_control_msg(dev, QT_GET_SET_UART, 0x30, 0x0300); | 
|  | 341 | if (result < 0) | 
|  | 342 | dbg("%s - set uart failed", __func__); | 
|  | 343 |  | 
|  | 344 | if (tty) | 
|  | 345 | ssu100_set_termios(tty, port, tty->termios); | 
|  | 346 |  | 
|  | 347 | return usb_serial_generic_open(tty, port); | 
|  | 348 | } | 
|  | 349 |  | 
|  | 350 | static void ssu100_close(struct usb_serial_port *port) | 
|  | 351 | { | 
|  | 352 | dbg("%s", __func__); | 
|  | 353 | usb_serial_generic_close(port); | 
|  | 354 | } | 
|  | 355 |  | 
|  | 356 | static int get_serial_info(struct usb_serial_port *port, | 
|  | 357 | struct serial_struct __user *retinfo) | 
|  | 358 | { | 
|  | 359 | struct serial_struct tmp; | 
|  | 360 |  | 
|  | 361 | if (!retinfo) | 
|  | 362 | return -EFAULT; | 
|  | 363 |  | 
|  | 364 | memset(&tmp, 0, sizeof(tmp)); | 
|  | 365 | tmp.line		= port->serial->minor; | 
|  | 366 | tmp.port		= 0; | 
|  | 367 | tmp.irq			= 0; | 
|  | 368 | tmp.flags		= ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; | 
|  | 369 | tmp.xmit_fifo_size	= port->bulk_out_size; | 
|  | 370 | tmp.baud_base		= 9600; | 
|  | 371 | tmp.close_delay		= 5*HZ; | 
|  | 372 | tmp.closing_wait	= 30*HZ; | 
|  | 373 |  | 
|  | 374 | if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) | 
|  | 375 | return -EFAULT; | 
|  | 376 | return 0; | 
|  | 377 | } | 
|  | 378 |  | 
| Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 379 | static int wait_modem_info(struct usb_serial_port *port, unsigned int arg) | 
|  | 380 | { | 
|  | 381 | struct ssu100_port_private *priv = usb_get_serial_port_data(port); | 
|  | 382 | struct async_icount prev, cur; | 
|  | 383 | unsigned long flags; | 
|  | 384 |  | 
|  | 385 | spin_lock_irqsave(&priv->status_lock, flags); | 
|  | 386 | prev = priv->icount; | 
|  | 387 | spin_unlock_irqrestore(&priv->status_lock, flags); | 
|  | 388 |  | 
|  | 389 | while (1) { | 
|  | 390 | wait_event_interruptible(priv->delta_msr_wait, | 
|  | 391 | ((priv->icount.rng != prev.rng) || | 
|  | 392 | (priv->icount.dsr != prev.dsr) || | 
|  | 393 | (priv->icount.dcd != prev.dcd) || | 
|  | 394 | (priv->icount.cts != prev.cts))); | 
|  | 395 |  | 
|  | 396 | if (signal_pending(current)) | 
|  | 397 | return -ERESTARTSYS; | 
|  | 398 |  | 
|  | 399 | spin_lock_irqsave(&priv->status_lock, flags); | 
|  | 400 | cur = priv->icount; | 
|  | 401 | spin_unlock_irqrestore(&priv->status_lock, flags); | 
|  | 402 |  | 
|  | 403 | if ((prev.rng == cur.rng) && | 
|  | 404 | (prev.dsr == cur.dsr) && | 
|  | 405 | (prev.dcd == cur.dcd) && | 
|  | 406 | (prev.cts == cur.cts)) | 
|  | 407 | return -EIO; | 
|  | 408 |  | 
|  | 409 | if ((arg & TIOCM_RNG && (prev.rng != cur.rng)) || | 
|  | 410 | (arg & TIOCM_DSR && (prev.dsr != cur.dsr)) || | 
|  | 411 | (arg & TIOCM_CD  && (prev.dcd != cur.dcd)) || | 
|  | 412 | (arg & TIOCM_CTS && (prev.cts != cur.cts))) | 
|  | 413 | return 0; | 
|  | 414 | } | 
|  | 415 | return 0; | 
|  | 416 | } | 
|  | 417 |  | 
| Alan Cox | 0bca1b9 | 2010-09-16 18:21:40 +0100 | [diff] [blame] | 418 | static int ssu100_get_icount(struct tty_struct *tty, | 
|  | 419 | struct serial_icounter_struct *icount) | 
|  | 420 | { | 
|  | 421 | struct usb_serial_port *port = tty->driver_data; | 
|  | 422 | struct ssu100_port_private *priv = usb_get_serial_port_data(port); | 
|  | 423 | struct async_icount cnow = priv->icount; | 
|  | 424 |  | 
|  | 425 | icount->cts = cnow.cts; | 
|  | 426 | icount->dsr = cnow.dsr; | 
|  | 427 | icount->rng = cnow.rng; | 
|  | 428 | icount->dcd = cnow.dcd; | 
|  | 429 | icount->rx = cnow.rx; | 
|  | 430 | icount->tx = cnow.tx; | 
|  | 431 | icount->frame = cnow.frame; | 
|  | 432 | icount->overrun = cnow.overrun; | 
|  | 433 | icount->parity = cnow.parity; | 
|  | 434 | icount->brk = cnow.brk; | 
|  | 435 | icount->buf_overrun = cnow.buf_overrun; | 
|  | 436 |  | 
|  | 437 | return 0; | 
|  | 438 | } | 
|  | 439 |  | 
|  | 440 |  | 
|  | 441 |  | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 442 | static int ssu100_ioctl(struct tty_struct *tty, struct file *file, | 
|  | 443 | unsigned int cmd, unsigned long arg) | 
|  | 444 | { | 
|  | 445 | struct usb_serial_port *port = tty->driver_data; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 446 |  | 
|  | 447 | dbg("%s cmd 0x%04x", __func__, cmd); | 
|  | 448 |  | 
|  | 449 | switch (cmd) { | 
|  | 450 | case TIOCGSERIAL: | 
|  | 451 | return get_serial_info(port, | 
|  | 452 | (struct serial_struct __user *) arg); | 
|  | 453 |  | 
|  | 454 | case TIOCMIWAIT: | 
| Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 455 | return wait_modem_info(port, arg); | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 456 |  | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 457 | default: | 
|  | 458 | break; | 
|  | 459 | } | 
|  | 460 |  | 
|  | 461 | dbg("%s arg not supported", __func__); | 
|  | 462 |  | 
|  | 463 | return -ENOIOCTLCMD; | 
|  | 464 | } | 
|  | 465 |  | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 466 | static int ssu100_attach(struct usb_serial *serial) | 
|  | 467 | { | 
|  | 468 | struct ssu100_port_private *priv; | 
|  | 469 | struct usb_serial_port *port = *serial->port; | 
|  | 470 |  | 
|  | 471 | dbg("%s", __func__); | 
|  | 472 |  | 
|  | 473 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); | 
|  | 474 | if (!priv) { | 
|  | 475 | dev_err(&port->dev, "%s- kmalloc(%Zd) failed.\n", __func__, | 
|  | 476 | sizeof(*priv)); | 
|  | 477 | return -ENOMEM; | 
|  | 478 | } | 
|  | 479 |  | 
| Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 480 | spin_lock_init(&priv->status_lock); | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 481 | init_waitqueue_head(&priv->delta_msr_wait); | 
|  | 482 | usb_set_serial_port_data(port, priv); | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 483 |  | 
|  | 484 | return ssu100_initdevice(serial->dev); | 
|  | 485 | } | 
|  | 486 |  | 
|  | 487 | static int ssu100_tiocmget(struct tty_struct *tty, struct file *file) | 
|  | 488 | { | 
|  | 489 | struct usb_serial_port *port = tty->driver_data; | 
|  | 490 | struct usb_device *dev = port->serial->dev; | 
|  | 491 | u8 *d; | 
|  | 492 | int r; | 
|  | 493 |  | 
|  | 494 | dbg("%s\n", __func__); | 
|  | 495 |  | 
|  | 496 | d = kzalloc(2, GFP_KERNEL); | 
|  | 497 | if (!d) | 
|  | 498 | return -ENOMEM; | 
|  | 499 |  | 
| Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 500 | r = ssu100_getregister(dev, 0, UART_MCR, d); | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 501 | if (r < 0) | 
|  | 502 | goto mget_out; | 
|  | 503 |  | 
| Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 504 | r = ssu100_getregister(dev, 0, UART_MSR, d+1); | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 505 | if (r < 0) | 
|  | 506 | goto mget_out; | 
|  | 507 |  | 
| Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 508 | r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) | | 
|  | 509 | (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) | | 
|  | 510 | (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) | | 
|  | 511 | (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) | | 
|  | 512 | (d[1] & UART_MSR_RI ? TIOCM_RI : 0) | | 
|  | 513 | (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0); | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 514 |  | 
|  | 515 | mget_out: | 
|  | 516 | kfree(d); | 
|  | 517 | return r; | 
|  | 518 | } | 
|  | 519 |  | 
|  | 520 | static int ssu100_tiocmset(struct tty_struct *tty, struct file *file, | 
|  | 521 | unsigned int set, unsigned int clear) | 
|  | 522 | { | 
|  | 523 | struct usb_serial_port *port = tty->driver_data; | 
|  | 524 | struct usb_device *dev = port->serial->dev; | 
|  | 525 |  | 
|  | 526 | dbg("%s\n", __func__); | 
|  | 527 | return update_mctrl(dev, set, clear); | 
|  | 528 | } | 
|  | 529 |  | 
|  | 530 | static void ssu100_dtr_rts(struct usb_serial_port *port, int on) | 
|  | 531 | { | 
|  | 532 | struct usb_device *dev = port->serial->dev; | 
|  | 533 |  | 
|  | 534 | dbg("%s\n", __func__); | 
|  | 535 |  | 
|  | 536 | mutex_lock(&port->serial->disc_mutex); | 
|  | 537 | if (!port->serial->disconnected) { | 
|  | 538 | /* Disable flow control */ | 
|  | 539 | if (!on && | 
| Bill Pemberton | 556f1a0 | 2010-08-05 17:01:08 -0400 | [diff] [blame] | 540 | ssu100_setregister(dev, 0, UART_MCR, 0) < 0) | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 541 | dev_err(&port->dev, "error from flowcontrol urb\n"); | 
|  | 542 | /* drop RTS and DTR */ | 
|  | 543 | if (on) | 
|  | 544 | set_mctrl(dev, TIOCM_DTR | TIOCM_RTS); | 
|  | 545 | else | 
|  | 546 | clear_mctrl(dev, TIOCM_DTR | TIOCM_RTS); | 
|  | 547 | } | 
|  | 548 | mutex_unlock(&port->serial->disc_mutex); | 
|  | 549 | } | 
|  | 550 |  | 
| Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 551 | static void ssu100_update_msr(struct usb_serial_port *port, u8 msr) | 
|  | 552 | { | 
|  | 553 | struct ssu100_port_private *priv = usb_get_serial_port_data(port); | 
|  | 554 | unsigned long flags; | 
|  | 555 |  | 
|  | 556 | spin_lock_irqsave(&priv->status_lock, flags); | 
|  | 557 | priv->shadowMSR = msr; | 
|  | 558 | spin_unlock_irqrestore(&priv->status_lock, flags); | 
|  | 559 |  | 
|  | 560 | if (msr & UART_MSR_ANY_DELTA) { | 
|  | 561 | /* update input line counters */ | 
|  | 562 | if (msr & UART_MSR_DCTS) | 
|  | 563 | priv->icount.cts++; | 
|  | 564 | if (msr & UART_MSR_DDSR) | 
|  | 565 | priv->icount.dsr++; | 
|  | 566 | if (msr & UART_MSR_DDCD) | 
|  | 567 | priv->icount.dcd++; | 
|  | 568 | if (msr & UART_MSR_TERI) | 
|  | 569 | priv->icount.rng++; | 
|  | 570 | wake_up_interruptible(&priv->delta_msr_wait); | 
|  | 571 | } | 
|  | 572 | } | 
|  | 573 |  | 
| Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 574 | static void ssu100_update_lsr(struct usb_serial_port *port, u8 lsr, | 
|  | 575 | char *tty_flag) | 
| Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 576 | { | 
|  | 577 | struct ssu100_port_private *priv = usb_get_serial_port_data(port); | 
|  | 578 | unsigned long flags; | 
|  | 579 |  | 
|  | 580 | spin_lock_irqsave(&priv->status_lock, flags); | 
|  | 581 | priv->shadowLSR = lsr; | 
|  | 582 | spin_unlock_irqrestore(&priv->status_lock, flags); | 
|  | 583 |  | 
| Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 584 | *tty_flag = TTY_NORMAL; | 
| Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 585 | if (lsr & UART_LSR_BRK_ERROR_BITS) { | 
| Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 586 | /* we always want to update icount, but we only want to | 
|  | 587 | * update tty_flag for one case */ | 
|  | 588 | if (lsr & UART_LSR_BI) { | 
| Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 589 | priv->icount.brk++; | 
| Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 590 | *tty_flag = TTY_BREAK; | 
|  | 591 | usb_serial_handle_break(port); | 
|  | 592 | } | 
|  | 593 | if (lsr & UART_LSR_PE) { | 
| Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 594 | priv->icount.parity++; | 
| Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 595 | if (*tty_flag == TTY_NORMAL) | 
|  | 596 | *tty_flag = TTY_PARITY; | 
|  | 597 | } | 
|  | 598 | if (lsr & UART_LSR_FE) { | 
|  | 599 | priv->icount.frame++; | 
|  | 600 | if (*tty_flag == TTY_NORMAL) | 
|  | 601 | *tty_flag = TTY_FRAME; | 
|  | 602 | } | 
|  | 603 | if (lsr & UART_LSR_OE){ | 
| Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 604 | priv->icount.overrun++; | 
| Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 605 | if (*tty_flag == TTY_NORMAL) | 
|  | 606 | *tty_flag = TTY_OVERRUN; | 
|  | 607 | } | 
| Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 608 | } | 
| Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 609 |  | 
| Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 610 | } | 
|  | 611 |  | 
| Bill Pemberton | f7043ec | 2010-10-21 14:43:05 -0400 | [diff] [blame] | 612 | static int ssu100_process_packet(struct urb *urb, | 
|  | 613 | struct tty_struct *tty) | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 614 | { | 
| Bill Pemberton | f7043ec | 2010-10-21 14:43:05 -0400 | [diff] [blame] | 615 | struct usb_serial_port *port = urb->context; | 
|  | 616 | char *packet = (char *)urb->transfer_buffer; | 
| Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 617 | char flag = TTY_NORMAL; | 
| Bill Pemberton | f7043ec | 2010-10-21 14:43:05 -0400 | [diff] [blame] | 618 | u32 len = urb->actual_length; | 
|  | 619 | int i; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 620 | char *ch; | 
|  | 621 |  | 
|  | 622 | dbg("%s - port %d", __func__, port->number); | 
|  | 623 |  | 
| Bill Pemberton | 9b2cef3 | 2010-08-05 17:01:06 -0400 | [diff] [blame] | 624 | if ((len >= 4) && | 
|  | 625 | (packet[0] == 0x1b) && (packet[1] == 0x1b) && | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 626 | ((packet[2] == 0x00) || (packet[2] == 0x01))) { | 
| Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 627 | if (packet[2] == 0x00) { | 
|  | 628 | ssu100_update_lsr(port, packet[3], &flag); | 
|  | 629 | if (flag == TTY_OVERRUN) | 
|  | 630 | tty_insert_flip_char(tty, 0, TTY_OVERRUN); | 
|  | 631 | } | 
| Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 632 | if (packet[2] == 0x01) | 
|  | 633 | ssu100_update_msr(port, packet[3]); | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 634 |  | 
|  | 635 | len -= 4; | 
|  | 636 | ch = packet + 4; | 
|  | 637 | } else | 
|  | 638 | ch = packet; | 
|  | 639 |  | 
|  | 640 | if (!len) | 
|  | 641 | return 0;	/* status only */ | 
|  | 642 |  | 
|  | 643 | if (port->port.console && port->sysrq) { | 
|  | 644 | for (i = 0; i < len; i++, ch++) { | 
| Dmitry Torokhov | 6ee9f4b | 2010-08-17 21:15:47 -0700 | [diff] [blame] | 645 | if (!usb_serial_handle_sysrq_char(port, *ch)) | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 646 | tty_insert_flip_char(tty, *ch, flag); | 
|  | 647 | } | 
|  | 648 | } else | 
|  | 649 | tty_insert_flip_string_fixed_flag(tty, ch, flag, len); | 
|  | 650 |  | 
|  | 651 | return len; | 
|  | 652 | } | 
|  | 653 |  | 
|  | 654 | static void ssu100_process_read_urb(struct urb *urb) | 
|  | 655 | { | 
|  | 656 | struct usb_serial_port *port = urb->context; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 657 | struct tty_struct *tty; | 
| Bill Pemberton | f7043ec | 2010-10-21 14:43:05 -0400 | [diff] [blame] | 658 | int count; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 659 |  | 
|  | 660 | dbg("%s", __func__); | 
|  | 661 |  | 
|  | 662 | tty = tty_port_tty_get(&port->port); | 
|  | 663 | if (!tty) | 
|  | 664 | return; | 
|  | 665 |  | 
| Bill Pemberton | f7043ec | 2010-10-21 14:43:05 -0400 | [diff] [blame] | 666 | count = ssu100_process_packet(urb, tty); | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 667 |  | 
|  | 668 | if (count) | 
|  | 669 | tty_flip_buffer_push(tty); | 
|  | 670 | tty_kref_put(tty); | 
|  | 671 | } | 
|  | 672 |  | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 673 | static struct usb_serial_driver ssu100_device = { | 
|  | 674 | .driver = { | 
|  | 675 | .owner = THIS_MODULE, | 
|  | 676 | .name = "ssu100", | 
|  | 677 | }, | 
|  | 678 | .description	     = DRIVER_DESC, | 
|  | 679 | .id_table	     = id_table, | 
|  | 680 | .usb_driver	     = &ssu100_driver, | 
|  | 681 | .num_ports	     = 1, | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 682 | .open		     = ssu100_open, | 
|  | 683 | .close		     = ssu100_close, | 
|  | 684 | .attach              = ssu100_attach, | 
|  | 685 | .release             = ssu100_release, | 
|  | 686 | .dtr_rts             = ssu100_dtr_rts, | 
|  | 687 | .process_read_urb    = ssu100_process_read_urb, | 
|  | 688 | .tiocmget            = ssu100_tiocmget, | 
|  | 689 | .tiocmset            = ssu100_tiocmset, | 
| Alan Cox | 0bca1b9 | 2010-09-16 18:21:40 +0100 | [diff] [blame] | 690 | .get_icount	     = ssu100_get_icount, | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 691 | .ioctl               = ssu100_ioctl, | 
|  | 692 | .set_termios         = ssu100_set_termios, | 
| Bill Pemberton | 85dee13 | 2010-08-05 17:01:11 -0400 | [diff] [blame] | 693 | .disconnect          = usb_serial_generic_disconnect, | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 694 | }; | 
|  | 695 |  | 
|  | 696 | static int __init ssu100_init(void) | 
|  | 697 | { | 
|  | 698 | int retval; | 
|  | 699 |  | 
|  | 700 | dbg("%s", __func__); | 
|  | 701 |  | 
|  | 702 | /* register with usb-serial */ | 
|  | 703 | retval = usb_serial_register(&ssu100_device); | 
|  | 704 |  | 
|  | 705 | if (retval) | 
|  | 706 | goto failed_usb_sio_register; | 
|  | 707 |  | 
|  | 708 | retval = usb_register(&ssu100_driver); | 
|  | 709 | if (retval) | 
|  | 710 | goto failed_usb_register; | 
|  | 711 |  | 
|  | 712 | printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":" | 
|  | 713 | DRIVER_DESC "\n"); | 
|  | 714 |  | 
|  | 715 | return 0; | 
|  | 716 |  | 
|  | 717 | failed_usb_register: | 
|  | 718 | usb_serial_deregister(&ssu100_device); | 
|  | 719 | failed_usb_sio_register: | 
|  | 720 | return retval; | 
|  | 721 | } | 
|  | 722 |  | 
|  | 723 | static void __exit ssu100_exit(void) | 
|  | 724 | { | 
|  | 725 | usb_deregister(&ssu100_driver); | 
|  | 726 | usb_serial_deregister(&ssu100_device); | 
|  | 727 | } | 
|  | 728 |  | 
|  | 729 | module_init(ssu100_init); | 
|  | 730 | module_exit(ssu100_exit); | 
|  | 731 |  | 
|  | 732 | MODULE_DESCRIPTION(DRIVER_DESC); | 
|  | 733 | MODULE_LICENSE("GPL"); | 
|  | 734 |  | 
|  | 735 | module_param(debug, bool, S_IRUGO | S_IWUSR); | 
|  | 736 | MODULE_PARM_DESC(debug, "Debug enabled or not"); |