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