1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Fintek F81232 USB to serial adaptor driver
4 * Fintek F81532A/534A/535/536 USB to 2/4/8/12 serial adaptor driver
5 *
6 * Copyright (C) 2012 Greg Kroah-Hartman (gregkh@linuxfoundation.org)
7 * Copyright (C) 2012 Linux Foundation
8 */
9
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/slab.h>
13 #include <linux/tty.h>
14 #include <linux/tty_driver.h>
15 #include <linux/tty_flip.h>
16 #include <linux/serial.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/mutex.h>
20 #include <linux/uaccess.h>
21 #include <linux/usb.h>
22 #include <linux/usb/serial.h>
23 #include <linux/serial_reg.h>
24
25 #define F81232_ID \
26 { USB_DEVICE(0x1934, 0x0706) } /* 1 port UART device */
27
28 #define F81534A_SERIES_ID \
29 { USB_DEVICE(0x2c42, 0x1602) }, /* In-Box 2 port UART device */ \
30 { USB_DEVICE(0x2c42, 0x1604) }, /* In-Box 4 port UART device */ \
31 { USB_DEVICE(0x2c42, 0x1605) }, /* In-Box 8 port UART device */ \
32 { USB_DEVICE(0x2c42, 0x1606) }, /* In-Box 12 port UART device */ \
33 { USB_DEVICE(0x2c42, 0x1608) }, /* Non-Flash type */ \
34 { USB_DEVICE(0x2c42, 0x1632) }, /* 2 port UART device */ \
35 { USB_DEVICE(0x2c42, 0x1634) }, /* 4 port UART device */ \
36 { USB_DEVICE(0x2c42, 0x1635) }, /* 8 port UART device */ \
37 { USB_DEVICE(0x2c42, 0x1636) } /* 12 port UART device */
38
39 #define F81534A_CTRL_ID \
40 { USB_DEVICE(0x2c42, 0x16f8) } /* Global control device */
41
42 static const struct usb_device_id f81232_id_table[] = {
43 F81232_ID,
44 { } /* Terminating entry */
45 };
46
47 static const struct usb_device_id f81534a_id_table[] = {
48 F81534A_SERIES_ID,
49 { } /* Terminating entry */
50 };
51
52 static const struct usb_device_id f81534a_ctrl_id_table[] = {
53 F81534A_CTRL_ID,
54 { } /* Terminating entry */
55 };
56
57 static const struct usb_device_id combined_id_table[] = {
58 F81232_ID,
59 F81534A_SERIES_ID,
60 F81534A_CTRL_ID,
61 { } /* Terminating entry */
62 };
63 MODULE_DEVICE_TABLE(usb, combined_id_table);
64
65 /* Maximum baudrate for F81232 */
66 #define F81232_MAX_BAUDRATE 1500000
67 #define F81232_DEF_BAUDRATE 9600
68
69 /* USB Control EP parameter */
70 #define F81232_REGISTER_REQUEST 0xa0
71 #define F81232_GET_REGISTER 0xc0
72 #define F81232_SET_REGISTER 0x40
73
74 #define SERIAL_BASE_ADDRESS 0x0120
75 #define RECEIVE_BUFFER_REGISTER (0x00 + SERIAL_BASE_ADDRESS)
76 #define INTERRUPT_ENABLE_REGISTER (0x01 + SERIAL_BASE_ADDRESS)
77 #define FIFO_CONTROL_REGISTER (0x02 + SERIAL_BASE_ADDRESS)
78 #define LINE_CONTROL_REGISTER (0x03 + SERIAL_BASE_ADDRESS)
79 #define MODEM_CONTROL_REGISTER (0x04 + SERIAL_BASE_ADDRESS)
80 #define LINE_STATUS_REGISTER (0x05 + SERIAL_BASE_ADDRESS)
81 #define MODEM_STATUS_REGISTER (0x06 + SERIAL_BASE_ADDRESS)
82
83 /*
84 * F81232 Clock registers (106h)
85 *
86 * Bit1-0: Clock source selector
87 * 00: 1.846MHz.
88 * 01: 18.46MHz.
89 * 10: 24MHz.
90 * 11: 14.77MHz.
91 */
92 #define F81232_CLK_REGISTER 0x106
93 #define F81232_CLK_1_846_MHZ 0
94 #define F81232_CLK_18_46_MHZ BIT(0)
95 #define F81232_CLK_24_MHZ BIT(1)
96 #define F81232_CLK_14_77_MHZ (BIT(1) | BIT(0))
97 #define F81232_CLK_MASK GENMASK(1, 0)
98
99 #define F81534A_MODE_REG 0x107
100 #define F81534A_TRIGGER_MASK GENMASK(3, 2)
101 #define F81534A_TRIGGER_MULTIPLE_4X BIT(3)
102 #define F81534A_FIFO_128BYTE (BIT(1) | BIT(0))
103
104 /* Serial port self GPIO control, 2bytes [control&output data][input data] */
105 #define F81534A_GPIO_REG 0x10e
106 #define F81534A_GPIO_MODE2_DIR BIT(6) /* 1: input, 0: output */
107 #define F81534A_GPIO_MODE1_DIR BIT(5)
108 #define F81534A_GPIO_MODE0_DIR BIT(4)
109 #define F81534A_GPIO_MODE2_OUTPUT BIT(2)
110 #define F81534A_GPIO_MODE1_OUTPUT BIT(1)
111 #define F81534A_GPIO_MODE0_OUTPUT BIT(0)
112
113 #define F81534A_CTRL_CMD_ENABLE_PORT 0x116
114
115 struct f81232_private {
116 struct mutex lock;
117 u8 modem_control;
118 u8 modem_status;
119 u8 shadow_lcr;
120 speed_t baud_base;
121 struct work_struct lsr_work;
122 struct work_struct interrupt_work;
123 struct usb_serial_port *port;
124 };
125
126 static u32 const baudrate_table[] = { 115200, 921600, 1152000, 1500000 };
127 static u8 const clock_table[] = { F81232_CLK_1_846_MHZ, F81232_CLK_14_77_MHZ,
128 F81232_CLK_18_46_MHZ, F81232_CLK_24_MHZ };
129
calc_baud_divisor(speed_t baudrate,speed_t clockrate)130 static int calc_baud_divisor(speed_t baudrate, speed_t clockrate)
131 {
132 return DIV_ROUND_CLOSEST(clockrate, baudrate);
133 }
134
f81232_get_register(struct usb_serial_port * port,u16 reg,u8 * val)135 static int f81232_get_register(struct usb_serial_port *port, u16 reg, u8 *val)
136 {
137 int status;
138 struct usb_device *dev = port->serial->dev;
139
140 status = usb_control_msg_recv(dev,
141 0,
142 F81232_REGISTER_REQUEST,
143 F81232_GET_REGISTER,
144 reg,
145 0,
146 val,
147 sizeof(*val),
148 USB_CTRL_GET_TIMEOUT,
149 GFP_KERNEL);
150 if (status) {
151 dev_err(&port->dev, "%s failed status: %d\n", __func__, status);
152 status = usb_translate_errors(status);
153 }
154
155 return status;
156 }
157
f81232_set_register(struct usb_serial_port * port,u16 reg,u8 val)158 static int f81232_set_register(struct usb_serial_port *port, u16 reg, u8 val)
159 {
160 int status;
161 struct usb_device *dev = port->serial->dev;
162
163 status = usb_control_msg_send(dev,
164 0,
165 F81232_REGISTER_REQUEST,
166 F81232_SET_REGISTER,
167 reg,
168 0,
169 &val,
170 sizeof(val),
171 USB_CTRL_SET_TIMEOUT,
172 GFP_KERNEL);
173 if (status) {
174 dev_err(&port->dev, "%s failed status: %d\n", __func__, status);
175 status = usb_translate_errors(status);
176 }
177
178 return status;
179 }
180
f81232_set_mask_register(struct usb_serial_port * port,u16 reg,u8 mask,u8 val)181 static int f81232_set_mask_register(struct usb_serial_port *port, u16 reg,
182 u8 mask, u8 val)
183 {
184 int status;
185 u8 tmp;
186
187 status = f81232_get_register(port, reg, &tmp);
188 if (status)
189 return status;
190
191 tmp = (tmp & ~mask) | (val & mask);
192
193 return f81232_set_register(port, reg, tmp);
194 }
195
f81232_read_msr(struct usb_serial_port * port)196 static void f81232_read_msr(struct usb_serial_port *port)
197 {
198 int status;
199 u8 current_msr;
200 struct tty_struct *tty;
201 struct f81232_private *priv = usb_get_serial_port_data(port);
202
203 mutex_lock(&priv->lock);
204 status = f81232_get_register(port, MODEM_STATUS_REGISTER,
205 ¤t_msr);
206 if (status) {
207 dev_err(&port->dev, "%s fail, status: %d\n", __func__, status);
208 mutex_unlock(&priv->lock);
209 return;
210 }
211
212 if (!(current_msr & UART_MSR_ANY_DELTA)) {
213 mutex_unlock(&priv->lock);
214 return;
215 }
216
217 priv->modem_status = current_msr;
218
219 if (current_msr & UART_MSR_DCTS)
220 port->icount.cts++;
221 if (current_msr & UART_MSR_DDSR)
222 port->icount.dsr++;
223 if (current_msr & UART_MSR_TERI)
224 port->icount.rng++;
225 if (current_msr & UART_MSR_DDCD) {
226 port->icount.dcd++;
227 tty = tty_port_tty_get(&port->port);
228 if (tty) {
229 usb_serial_handle_dcd_change(port, tty,
230 current_msr & UART_MSR_DCD);
231
232 tty_kref_put(tty);
233 }
234 }
235
236 wake_up_interruptible(&port->port.delta_msr_wait);
237 mutex_unlock(&priv->lock);
238 }
239
f81232_set_mctrl(struct usb_serial_port * port,unsigned int set,unsigned int clear)240 static int f81232_set_mctrl(struct usb_serial_port *port,
241 unsigned int set, unsigned int clear)
242 {
243 u8 val;
244 int status;
245 struct f81232_private *priv = usb_get_serial_port_data(port);
246
247 if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0)
248 return 0; /* no change */
249
250 /* 'set' takes precedence over 'clear' */
251 clear &= ~set;
252
253 /* force enable interrupt with OUT2 */
254 mutex_lock(&priv->lock);
255 val = UART_MCR_OUT2 | priv->modem_control;
256
257 if (clear & TIOCM_DTR)
258 val &= ~UART_MCR_DTR;
259
260 if (clear & TIOCM_RTS)
261 val &= ~UART_MCR_RTS;
262
263 if (set & TIOCM_DTR)
264 val |= UART_MCR_DTR;
265
266 if (set & TIOCM_RTS)
267 val |= UART_MCR_RTS;
268
269 dev_dbg(&port->dev, "%s new:%02x old:%02x\n", __func__,
270 val, priv->modem_control);
271
272 status = f81232_set_register(port, MODEM_CONTROL_REGISTER, val);
273 if (status) {
274 dev_err(&port->dev, "%s set MCR status < 0\n", __func__);
275 mutex_unlock(&priv->lock);
276 return status;
277 }
278
279 priv->modem_control = val;
280 mutex_unlock(&priv->lock);
281
282 return 0;
283 }
284
f81232_update_line_status(struct usb_serial_port * port,unsigned char * data,size_t actual_length)285 static void f81232_update_line_status(struct usb_serial_port *port,
286 unsigned char *data,
287 size_t actual_length)
288 {
289 struct f81232_private *priv = usb_get_serial_port_data(port);
290
291 if (!actual_length)
292 return;
293
294 switch (data[0] & 0x07) {
295 case 0x00: /* msr change */
296 dev_dbg(&port->dev, "IIR: MSR Change: %02x\n", data[0]);
297 schedule_work(&priv->interrupt_work);
298 break;
299 case 0x02: /* tx-empty */
300 break;
301 case 0x04: /* rx data available */
302 break;
303 case 0x06: /* lsr change */
304 /* we can forget it. the LSR will read from bulk-in */
305 dev_dbg(&port->dev, "IIR: LSR Change: %02x\n", data[0]);
306 break;
307 }
308 }
309
f81232_read_int_callback(struct urb * urb)310 static void f81232_read_int_callback(struct urb *urb)
311 {
312 struct usb_serial_port *port = urb->context;
313 unsigned char *data = urb->transfer_buffer;
314 unsigned int actual_length = urb->actual_length;
315 int status = urb->status;
316 int retval;
317
318 switch (status) {
319 case 0:
320 /* success */
321 break;
322 case -ECONNRESET:
323 case -ENOENT:
324 case -ESHUTDOWN:
325 /* this urb is terminated, clean up */
326 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
327 __func__, status);
328 return;
329 default:
330 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
331 __func__, status);
332 goto exit;
333 }
334
335 usb_serial_debug_data(&port->dev, __func__,
336 urb->actual_length, urb->transfer_buffer);
337
338 f81232_update_line_status(port, data, actual_length);
339
340 exit:
341 retval = usb_submit_urb(urb, GFP_ATOMIC);
342 if (retval)
343 dev_err(&urb->dev->dev,
344 "%s - usb_submit_urb failed with result %d\n",
345 __func__, retval);
346 }
347
f81232_handle_lsr(struct usb_serial_port * port,u8 lsr)348 static char f81232_handle_lsr(struct usb_serial_port *port, u8 lsr)
349 {
350 struct f81232_private *priv = usb_get_serial_port_data(port);
351 char tty_flag = TTY_NORMAL;
352
353 if (!(lsr & UART_LSR_BRK_ERROR_BITS))
354 return tty_flag;
355
356 if (lsr & UART_LSR_BI) {
357 tty_flag = TTY_BREAK;
358 port->icount.brk++;
359 usb_serial_handle_break(port);
360 } else if (lsr & UART_LSR_PE) {
361 tty_flag = TTY_PARITY;
362 port->icount.parity++;
363 } else if (lsr & UART_LSR_FE) {
364 tty_flag = TTY_FRAME;
365 port->icount.frame++;
366 }
367
368 if (lsr & UART_LSR_OE) {
369 port->icount.overrun++;
370 schedule_work(&priv->lsr_work);
371 tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
372 }
373
374 return tty_flag;
375 }
376
f81232_process_read_urb(struct urb * urb)377 static void f81232_process_read_urb(struct urb *urb)
378 {
379 struct usb_serial_port *port = urb->context;
380 unsigned char *data = urb->transfer_buffer;
381 char tty_flag;
382 unsigned int i;
383 u8 lsr;
384
385 /*
386 * When opening the port we get a 1-byte packet with the current LSR,
387 * which we discard.
388 */
389 if ((urb->actual_length < 2) || (urb->actual_length % 2))
390 return;
391
392 /* bulk-in data: [LSR(1Byte)+DATA(1Byte)][LSR(1Byte)+DATA(1Byte)]... */
393
394 for (i = 0; i < urb->actual_length; i += 2) {
395 lsr = data[i];
396 tty_flag = f81232_handle_lsr(port, lsr);
397
398 if (port->sysrq) {
399 if (usb_serial_handle_sysrq_char(port, data[i + 1]))
400 continue;
401 }
402
403 tty_insert_flip_char(&port->port, data[i + 1], tty_flag);
404 }
405
406 tty_flip_buffer_push(&port->port);
407 }
408
f81534a_process_read_urb(struct urb * urb)409 static void f81534a_process_read_urb(struct urb *urb)
410 {
411 struct usb_serial_port *port = urb->context;
412 unsigned char *data = urb->transfer_buffer;
413 char tty_flag;
414 unsigned int i;
415 u8 lsr;
416 u8 len;
417
418 if (urb->actual_length < 3) {
419 dev_err(&port->dev, "short message received: %d\n",
420 urb->actual_length);
421 return;
422 }
423
424 len = data[0];
425 if (len != urb->actual_length) {
426 dev_err(&port->dev, "malformed message received: %d (%d)\n",
427 urb->actual_length, len);
428 return;
429 }
430
431 /* bulk-in data: [LEN][Data.....][LSR] */
432 lsr = data[len - 1];
433 tty_flag = f81232_handle_lsr(port, lsr);
434
435 if (port->sysrq) {
436 for (i = 1; i < len - 1; ++i) {
437 if (!usb_serial_handle_sysrq_char(port, data[i])) {
438 tty_insert_flip_char(&port->port, data[i],
439 tty_flag);
440 }
441 }
442 } else {
443 tty_insert_flip_string_fixed_flag(&port->port, &data[1],
444 tty_flag, len - 2);
445 }
446
447 tty_flip_buffer_push(&port->port);
448 }
449
f81232_break_ctl(struct tty_struct * tty,int break_state)450 static int f81232_break_ctl(struct tty_struct *tty, int break_state)
451 {
452 struct usb_serial_port *port = tty->driver_data;
453 struct f81232_private *priv = usb_get_serial_port_data(port);
454 int status;
455
456 mutex_lock(&priv->lock);
457
458 if (break_state)
459 priv->shadow_lcr |= UART_LCR_SBC;
460 else
461 priv->shadow_lcr &= ~UART_LCR_SBC;
462
463 status = f81232_set_register(port, LINE_CONTROL_REGISTER,
464 priv->shadow_lcr);
465 if (status)
466 dev_err(&port->dev, "set break failed: %d\n", status);
467
468 mutex_unlock(&priv->lock);
469
470 return status;
471 }
472
f81232_find_clk(speed_t baudrate)473 static int f81232_find_clk(speed_t baudrate)
474 {
475 int idx;
476
477 for (idx = 0; idx < ARRAY_SIZE(baudrate_table); ++idx) {
478 if (baudrate <= baudrate_table[idx] &&
479 baudrate_table[idx] % baudrate == 0)
480 return idx;
481 }
482
483 return -EINVAL;
484 }
485
f81232_set_baudrate(struct tty_struct * tty,struct usb_serial_port * port,speed_t baudrate,speed_t old_baudrate)486 static void f81232_set_baudrate(struct tty_struct *tty,
487 struct usb_serial_port *port, speed_t baudrate,
488 speed_t old_baudrate)
489 {
490 struct f81232_private *priv = usb_get_serial_port_data(port);
491 u8 lcr;
492 int divisor;
493 int status = 0;
494 int i;
495 int idx;
496 speed_t baud_list[] = { baudrate, old_baudrate, F81232_DEF_BAUDRATE };
497
498 for (i = 0; i < ARRAY_SIZE(baud_list); ++i) {
499 baudrate = baud_list[i];
500 if (baudrate == 0) {
501 tty_encode_baud_rate(tty, 0, 0);
502 return;
503 }
504
505 idx = f81232_find_clk(baudrate);
506 if (idx >= 0) {
507 tty_encode_baud_rate(tty, baudrate, baudrate);
508 break;
509 }
510 }
511
512 if (idx < 0)
513 return;
514
515 priv->baud_base = baudrate_table[idx];
516 divisor = calc_baud_divisor(baudrate, priv->baud_base);
517
518 status = f81232_set_mask_register(port, F81232_CLK_REGISTER,
519 F81232_CLK_MASK, clock_table[idx]);
520 if (status) {
521 dev_err(&port->dev, "%s failed to set CLK_REG: %d\n",
522 __func__, status);
523 return;
524 }
525
526 status = f81232_get_register(port, LINE_CONTROL_REGISTER,
527 &lcr); /* get LCR */
528 if (status) {
529 dev_err(&port->dev, "%s failed to get LCR: %d\n",
530 __func__, status);
531 return;
532 }
533
534 status = f81232_set_register(port, LINE_CONTROL_REGISTER,
535 lcr | UART_LCR_DLAB); /* Enable DLAB */
536 if (status) {
537 dev_err(&port->dev, "%s failed to set DLAB: %d\n",
538 __func__, status);
539 return;
540 }
541
542 status = f81232_set_register(port, RECEIVE_BUFFER_REGISTER,
543 divisor & 0x00ff); /* low */
544 if (status) {
545 dev_err(&port->dev, "%s failed to set baudrate MSB: %d\n",
546 __func__, status);
547 goto reapply_lcr;
548 }
549
550 status = f81232_set_register(port, INTERRUPT_ENABLE_REGISTER,
551 (divisor & 0xff00) >> 8); /* high */
552 if (status) {
553 dev_err(&port->dev, "%s failed to set baudrate LSB: %d\n",
554 __func__, status);
555 }
556
557 reapply_lcr:
558 status = f81232_set_register(port, LINE_CONTROL_REGISTER,
559 lcr & ~UART_LCR_DLAB);
560 if (status) {
561 dev_err(&port->dev, "%s failed to set DLAB: %d\n",
562 __func__, status);
563 }
564 }
565
f81232_port_enable(struct usb_serial_port * port)566 static int f81232_port_enable(struct usb_serial_port *port)
567 {
568 u8 val;
569 int status;
570
571 /* fifo on, trigger8, clear TX/RX*/
572 val = UART_FCR_TRIGGER_8 | UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR |
573 UART_FCR_CLEAR_XMIT;
574
575 status = f81232_set_register(port, FIFO_CONTROL_REGISTER, val);
576 if (status) {
577 dev_err(&port->dev, "%s failed to set FCR: %d\n",
578 __func__, status);
579 return status;
580 }
581
582 /* MSR Interrupt only, LSR will read from Bulk-in odd byte */
583 status = f81232_set_register(port, INTERRUPT_ENABLE_REGISTER,
584 UART_IER_MSI);
585 if (status) {
586 dev_err(&port->dev, "%s failed to set IER: %d\n",
587 __func__, status);
588 return status;
589 }
590
591 return 0;
592 }
593
f81232_port_disable(struct usb_serial_port * port)594 static int f81232_port_disable(struct usb_serial_port *port)
595 {
596 int status;
597
598 status = f81232_set_register(port, INTERRUPT_ENABLE_REGISTER, 0);
599 if (status) {
600 dev_err(&port->dev, "%s failed to set IER: %d\n",
601 __func__, status);
602 return status;
603 }
604
605 return 0;
606 }
607
f81232_set_termios(struct tty_struct * tty,struct usb_serial_port * port,const struct ktermios * old_termios)608 static void f81232_set_termios(struct tty_struct *tty,
609 struct usb_serial_port *port,
610 const struct ktermios *old_termios)
611 {
612 struct f81232_private *priv = usb_get_serial_port_data(port);
613 u8 new_lcr = 0;
614 int status = 0;
615 speed_t baudrate;
616 speed_t old_baud;
617
618 /* Don't change anything if nothing has changed */
619 if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
620 return;
621
622 if (C_BAUD(tty) == B0)
623 f81232_set_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
624 else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
625 f81232_set_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
626
627 baudrate = tty_get_baud_rate(tty);
628 if (baudrate > 0) {
629 if (old_termios)
630 old_baud = tty_termios_baud_rate(old_termios);
631 else
632 old_baud = F81232_DEF_BAUDRATE;
633
634 f81232_set_baudrate(tty, port, baudrate, old_baud);
635 }
636
637 if (C_PARENB(tty)) {
638 new_lcr |= UART_LCR_PARITY;
639
640 if (!C_PARODD(tty))
641 new_lcr |= UART_LCR_EPAR;
642
643 if (C_CMSPAR(tty))
644 new_lcr |= UART_LCR_SPAR;
645 }
646
647 if (C_CSTOPB(tty))
648 new_lcr |= UART_LCR_STOP;
649
650 new_lcr |= UART_LCR_WLEN(tty_get_char_size(tty->termios.c_cflag));
651
652 mutex_lock(&priv->lock);
653
654 new_lcr |= (priv->shadow_lcr & UART_LCR_SBC);
655 status = f81232_set_register(port, LINE_CONTROL_REGISTER, new_lcr);
656 if (status) {
657 dev_err(&port->dev, "%s failed to set LCR: %d\n",
658 __func__, status);
659 }
660
661 priv->shadow_lcr = new_lcr;
662
663 mutex_unlock(&priv->lock);
664 }
665
f81232_tiocmget(struct tty_struct * tty)666 static int f81232_tiocmget(struct tty_struct *tty)
667 {
668 int r;
669 struct usb_serial_port *port = tty->driver_data;
670 struct f81232_private *port_priv = usb_get_serial_port_data(port);
671 u8 mcr, msr;
672
673 /* force get current MSR changed state */
674 f81232_read_msr(port);
675
676 mutex_lock(&port_priv->lock);
677 mcr = port_priv->modem_control;
678 msr = port_priv->modem_status;
679 mutex_unlock(&port_priv->lock);
680
681 r = (mcr & UART_MCR_DTR ? TIOCM_DTR : 0) |
682 (mcr & UART_MCR_RTS ? TIOCM_RTS : 0) |
683 (msr & UART_MSR_CTS ? TIOCM_CTS : 0) |
684 (msr & UART_MSR_DCD ? TIOCM_CAR : 0) |
685 (msr & UART_MSR_RI ? TIOCM_RI : 0) |
686 (msr & UART_MSR_DSR ? TIOCM_DSR : 0);
687
688 return r;
689 }
690
f81232_tiocmset(struct tty_struct * tty,unsigned int set,unsigned int clear)691 static int f81232_tiocmset(struct tty_struct *tty,
692 unsigned int set, unsigned int clear)
693 {
694 struct usb_serial_port *port = tty->driver_data;
695
696 return f81232_set_mctrl(port, set, clear);
697 }
698
f81232_open(struct tty_struct * tty,struct usb_serial_port * port)699 static int f81232_open(struct tty_struct *tty, struct usb_serial_port *port)
700 {
701 int result;
702
703 result = f81232_port_enable(port);
704 if (result)
705 return result;
706
707 /* Setup termios */
708 if (tty)
709 f81232_set_termios(tty, port, NULL);
710
711 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
712 if (result) {
713 dev_err(&port->dev, "%s - failed submitting interrupt urb,"
714 " error %d\n", __func__, result);
715 return result;
716 }
717
718 result = usb_serial_generic_open(tty, port);
719 if (result) {
720 usb_kill_urb(port->interrupt_in_urb);
721 return result;
722 }
723
724 return 0;
725 }
726
f81534a_open(struct tty_struct * tty,struct usb_serial_port * port)727 static int f81534a_open(struct tty_struct *tty, struct usb_serial_port *port)
728 {
729 int status;
730 u8 mask;
731 u8 val;
732
733 val = F81534A_TRIGGER_MULTIPLE_4X | F81534A_FIFO_128BYTE;
734 mask = F81534A_TRIGGER_MASK | F81534A_FIFO_128BYTE;
735
736 status = f81232_set_mask_register(port, F81534A_MODE_REG, mask, val);
737 if (status) {
738 dev_err(&port->dev, "failed to set MODE_REG: %d\n", status);
739 return status;
740 }
741
742 return f81232_open(tty, port);
743 }
744
f81232_close(struct usb_serial_port * port)745 static void f81232_close(struct usb_serial_port *port)
746 {
747 struct f81232_private *port_priv = usb_get_serial_port_data(port);
748
749 f81232_port_disable(port);
750 usb_serial_generic_close(port);
751 usb_kill_urb(port->interrupt_in_urb);
752 flush_work(&port_priv->interrupt_work);
753 flush_work(&port_priv->lsr_work);
754 }
755
f81232_dtr_rts(struct usb_serial_port * port,int on)756 static void f81232_dtr_rts(struct usb_serial_port *port, int on)
757 {
758 if (on)
759 f81232_set_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
760 else
761 f81232_set_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
762 }
763
f81232_tx_empty(struct usb_serial_port * port)764 static bool f81232_tx_empty(struct usb_serial_port *port)
765 {
766 int status;
767 u8 tmp;
768
769 status = f81232_get_register(port, LINE_STATUS_REGISTER, &tmp);
770 if (!status) {
771 if ((tmp & UART_LSR_TEMT) != UART_LSR_TEMT)
772 return false;
773 }
774
775 return true;
776 }
777
f81232_carrier_raised(struct usb_serial_port * port)778 static int f81232_carrier_raised(struct usb_serial_port *port)
779 {
780 u8 msr;
781 struct f81232_private *priv = usb_get_serial_port_data(port);
782
783 mutex_lock(&priv->lock);
784 msr = priv->modem_status;
785 mutex_unlock(&priv->lock);
786
787 if (msr & UART_MSR_DCD)
788 return 1;
789 return 0;
790 }
791
f81232_get_serial(struct tty_struct * tty,struct serial_struct * ss)792 static void f81232_get_serial(struct tty_struct *tty, struct serial_struct *ss)
793 {
794 struct usb_serial_port *port = tty->driver_data;
795 struct f81232_private *priv = usb_get_serial_port_data(port);
796
797 ss->baud_base = priv->baud_base;
798 }
799
f81232_interrupt_work(struct work_struct * work)800 static void f81232_interrupt_work(struct work_struct *work)
801 {
802 struct f81232_private *priv =
803 container_of(work, struct f81232_private, interrupt_work);
804
805 f81232_read_msr(priv->port);
806 }
807
f81232_lsr_worker(struct work_struct * work)808 static void f81232_lsr_worker(struct work_struct *work)
809 {
810 struct f81232_private *priv;
811 struct usb_serial_port *port;
812 int status;
813 u8 tmp;
814
815 priv = container_of(work, struct f81232_private, lsr_work);
816 port = priv->port;
817
818 status = f81232_get_register(port, LINE_STATUS_REGISTER, &tmp);
819 if (status)
820 dev_warn(&port->dev, "read LSR failed: %d\n", status);
821 }
822
f81534a_ctrl_set_register(struct usb_interface * intf,u16 reg,u16 size,void * val)823 static int f81534a_ctrl_set_register(struct usb_interface *intf, u16 reg,
824 u16 size, void *val)
825 {
826 return usb_control_msg_send(interface_to_usbdev(intf),
827 0,
828 F81232_REGISTER_REQUEST,
829 F81232_SET_REGISTER,
830 reg,
831 0,
832 val,
833 size,
834 USB_CTRL_SET_TIMEOUT,
835 GFP_KERNEL);
836 }
837
f81534a_ctrl_get_register(struct usb_interface * intf,u16 reg,u16 size,void * val)838 static int f81534a_ctrl_get_register(struct usb_interface *intf, u16 reg,
839 u16 size, void *val)
840 {
841 return usb_control_msg_recv(interface_to_usbdev(intf),
842 0,
843 F81232_REGISTER_REQUEST,
844 F81232_GET_REGISTER,
845 reg,
846 0,
847 val,
848 size,
849 USB_CTRL_GET_TIMEOUT,
850 GFP_KERNEL);
851 }
852
f81534a_ctrl_enable_all_ports(struct usb_interface * intf,bool en)853 static int f81534a_ctrl_enable_all_ports(struct usb_interface *intf, bool en)
854 {
855 unsigned char enable[2] = {0};
856 int status;
857
858 /*
859 * Enable all available serial ports, define as following:
860 * bit 15 : Reset behavior (when HUB got soft reset)
861 * 0: maintain all serial port enabled state.
862 * 1: disable all serial port.
863 * bit 0~11 : Serial port enable bit.
864 */
865 if (en) {
866 /*
867 * The Fintek F81532A/534A/535/536 family relies on the
868 * F81534A_CTRL_CMD_ENABLE_PORT (116h) register during
869 * initialization to both determine serial port status and
870 * control port creation.
871 *
872 * If the driver experiences fast load/unload cycles, the
873 * device state may becomes unstable, resulting in the
874 * incomplete generation of serial ports.
875 *
876 * Performing a dummy read operation on the register prior
877 * to the initial write command resolves the issue.
878 *
879 * This clears the device's stale internal state. Subsequent
880 * write operations will correctly generate all serial ports.
881 */
882 status = f81534a_ctrl_get_register(intf,
883 F81534A_CTRL_CMD_ENABLE_PORT,
884 sizeof(enable),
885 enable);
886 if (status)
887 return status;
888
889 enable[0] = 0xff;
890 enable[1] = 0x8f;
891 }
892
893 status = f81534a_ctrl_set_register(intf, F81534A_CTRL_CMD_ENABLE_PORT,
894 sizeof(enable), enable);
895 if (status)
896 dev_err(&intf->dev, "failed to enable ports: %d\n", status);
897
898 return status;
899 }
900
f81534a_ctrl_probe(struct usb_interface * intf,const struct usb_device_id * id)901 static int f81534a_ctrl_probe(struct usb_interface *intf,
902 const struct usb_device_id *id)
903 {
904 return f81534a_ctrl_enable_all_ports(intf, true);
905 }
906
f81534a_ctrl_disconnect(struct usb_interface * intf)907 static void f81534a_ctrl_disconnect(struct usb_interface *intf)
908 {
909 f81534a_ctrl_enable_all_ports(intf, false);
910 }
911
f81534a_ctrl_resume(struct usb_interface * intf)912 static int f81534a_ctrl_resume(struct usb_interface *intf)
913 {
914 return f81534a_ctrl_enable_all_ports(intf, true);
915 }
916
f81232_port_probe(struct usb_serial_port * port)917 static int f81232_port_probe(struct usb_serial_port *port)
918 {
919 struct f81232_private *priv;
920
921 priv = devm_kzalloc(&port->dev, sizeof(*priv), GFP_KERNEL);
922 if (!priv)
923 return -ENOMEM;
924
925 mutex_init(&priv->lock);
926 INIT_WORK(&priv->interrupt_work, f81232_interrupt_work);
927 INIT_WORK(&priv->lsr_work, f81232_lsr_worker);
928
929 usb_set_serial_port_data(port, priv);
930
931 priv->port = port;
932
933 return 0;
934 }
935
f81534a_port_probe(struct usb_serial_port * port)936 static int f81534a_port_probe(struct usb_serial_port *port)
937 {
938 int status;
939
940 /* tri-state with pull-high, default RS232 Mode */
941 status = f81232_set_register(port, F81534A_GPIO_REG,
942 F81534A_GPIO_MODE2_DIR);
943 if (status)
944 return status;
945
946 return f81232_port_probe(port);
947 }
948
f81232_suspend(struct usb_serial * serial,pm_message_t message)949 static int f81232_suspend(struct usb_serial *serial, pm_message_t message)
950 {
951 struct usb_serial_port *port = serial->port[0];
952 struct f81232_private *port_priv = usb_get_serial_port_data(port);
953 int i;
954
955 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
956 usb_kill_urb(port->read_urbs[i]);
957
958 usb_kill_urb(port->interrupt_in_urb);
959
960 if (port_priv) {
961 flush_work(&port_priv->interrupt_work);
962 flush_work(&port_priv->lsr_work);
963 }
964
965 return 0;
966 }
967
f81232_resume(struct usb_serial * serial)968 static int f81232_resume(struct usb_serial *serial)
969 {
970 struct usb_serial_port *port = serial->port[0];
971 int result;
972
973 if (tty_port_initialized(&port->port)) {
974 result = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
975 if (result) {
976 dev_err(&port->dev, "submit interrupt urb failed: %d\n",
977 result);
978 return result;
979 }
980 }
981
982 return usb_serial_generic_resume(serial);
983 }
984
985 static struct usb_serial_driver f81232_device = {
986 .driver = {
987 .name = "f81232",
988 },
989 .id_table = f81232_id_table,
990 .num_ports = 1,
991 .bulk_in_size = 256,
992 .bulk_out_size = 256,
993 .open = f81232_open,
994 .close = f81232_close,
995 .dtr_rts = f81232_dtr_rts,
996 .carrier_raised = f81232_carrier_raised,
997 .get_serial = f81232_get_serial,
998 .break_ctl = f81232_break_ctl,
999 .set_termios = f81232_set_termios,
1000 .tiocmget = f81232_tiocmget,
1001 .tiocmset = f81232_tiocmset,
1002 .tiocmiwait = usb_serial_generic_tiocmiwait,
1003 .tx_empty = f81232_tx_empty,
1004 .process_read_urb = f81232_process_read_urb,
1005 .read_int_callback = f81232_read_int_callback,
1006 .port_probe = f81232_port_probe,
1007 .suspend = f81232_suspend,
1008 .resume = f81232_resume,
1009 };
1010
1011 static struct usb_serial_driver f81534a_device = {
1012 .driver = {
1013 .name = "f81534a",
1014 },
1015 .id_table = f81534a_id_table,
1016 .num_ports = 1,
1017 .open = f81534a_open,
1018 .close = f81232_close,
1019 .dtr_rts = f81232_dtr_rts,
1020 .carrier_raised = f81232_carrier_raised,
1021 .get_serial = f81232_get_serial,
1022 .break_ctl = f81232_break_ctl,
1023 .set_termios = f81232_set_termios,
1024 .tiocmget = f81232_tiocmget,
1025 .tiocmset = f81232_tiocmset,
1026 .tiocmiwait = usb_serial_generic_tiocmiwait,
1027 .tx_empty = f81232_tx_empty,
1028 .process_read_urb = f81534a_process_read_urb,
1029 .read_int_callback = f81232_read_int_callback,
1030 .port_probe = f81534a_port_probe,
1031 .suspend = f81232_suspend,
1032 .resume = f81232_resume,
1033 };
1034
1035 static struct usb_serial_driver * const serial_drivers[] = {
1036 &f81232_device,
1037 &f81534a_device,
1038 NULL,
1039 };
1040
1041 static struct usb_driver f81534a_ctrl_driver = {
1042 .name = "f81534a_ctrl",
1043 .id_table = f81534a_ctrl_id_table,
1044 .probe = f81534a_ctrl_probe,
1045 .disconnect = f81534a_ctrl_disconnect,
1046 .resume = f81534a_ctrl_resume,
1047 };
1048
f81232_init(void)1049 static int __init f81232_init(void)
1050 {
1051 int status;
1052
1053 status = usb_register_driver(&f81534a_ctrl_driver, THIS_MODULE,
1054 KBUILD_MODNAME);
1055 if (status)
1056 return status;
1057
1058 status = usb_serial_register_drivers(serial_drivers, KBUILD_MODNAME,
1059 combined_id_table);
1060 if (status) {
1061 usb_deregister(&f81534a_ctrl_driver);
1062 return status;
1063 }
1064
1065 return 0;
1066 }
1067
f81232_exit(void)1068 static void __exit f81232_exit(void)
1069 {
1070 usb_serial_deregister_drivers(serial_drivers);
1071 usb_deregister(&f81534a_ctrl_driver);
1072 }
1073
1074 module_init(f81232_init);
1075 module_exit(f81232_exit);
1076
1077 MODULE_DESCRIPTION("Fintek F81232/532A/534A/535/536 USB to serial driver");
1078 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");
1079 MODULE_AUTHOR("Peter Hong <peter_hong@fintek.com.tw>");
1080 MODULE_LICENSE("GPL v2");
1081