1 // SPDX-License-Identifier: GPL-2.0
3 * Fintek F81232 USB to serial adaptor driver
6 * Copyright (C) 2012 Linux Foundation
9 #include <linux/kernel.h>
10 #include <linux/errno.h>
11 #include <linux/slab.h>
12 #include <linux/tty.h>
13 #include <linux/tty_driver.h>
14 #include <linux/tty_flip.h>
15 #include <linux/serial.h>
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/mutex.h>
19 #include <linux/uaccess.h>
20 #include <linux/usb.h>
21 #include <linux/usb/serial.h>
22 #include <linux/serial_reg.h>
24 static const struct usb_device_id id_table[] = {
25 { USB_DEVICE(0x1934, 0x0706) },
26 { } /* Terminating entry */
28 MODULE_DEVICE_TABLE(usb, id_table);
30 /* Maximum baudrate for F81232 */
31 #define F81232_MAX_BAUDRATE 1500000
32 #define F81232_DEF_BAUDRATE 9600
34 /* USB Control EP parameter */
35 #define F81232_REGISTER_REQUEST 0xa0
36 #define F81232_GET_REGISTER 0xc0
37 #define F81232_SET_REGISTER 0x40
39 #define SERIAL_BASE_ADDRESS 0x0120
40 #define RECEIVE_BUFFER_REGISTER (0x00 + SERIAL_BASE_ADDRESS)
41 #define INTERRUPT_ENABLE_REGISTER (0x01 + SERIAL_BASE_ADDRESS)
42 #define FIFO_CONTROL_REGISTER (0x02 + SERIAL_BASE_ADDRESS)
43 #define LINE_CONTROL_REGISTER (0x03 + SERIAL_BASE_ADDRESS)
44 #define MODEM_CONTROL_REGISTER (0x04 + SERIAL_BASE_ADDRESS)
45 #define LINE_STATUS_REGISTER (0x05 + SERIAL_BASE_ADDRESS)
46 #define MODEM_STATUS_REGISTER (0x06 + SERIAL_BASE_ADDRESS)
49 * F81232 Clock registers (106h)
51 * Bit1-0: Clock source selector
57 #define F81232_CLK_REGISTER 0x106
58 #define F81232_CLK_1_846_MHZ 0
59 #define F81232_CLK_18_46_MHZ BIT(0)
60 #define F81232_CLK_24_MHZ BIT(1)
61 #define F81232_CLK_14_77_MHZ (BIT(1) | BIT(0))
62 #define F81232_CLK_MASK GENMASK(1, 0)
64 struct f81232_private {
70 struct work_struct lsr_work;
71 struct work_struct interrupt_work;
72 struct usb_serial_port *port;
75 static u32 const baudrate_table[] = { 115200, 921600, 1152000, 1500000 };
76 static u8 const clock_table[] = { F81232_CLK_1_846_MHZ, F81232_CLK_14_77_MHZ,
77 F81232_CLK_18_46_MHZ, F81232_CLK_24_MHZ };
79 static int calc_baud_divisor(speed_t baudrate, speed_t clockrate)
84 return DIV_ROUND_CLOSEST(clockrate, baudrate);
87 static int f81232_get_register(struct usb_serial_port *port, u16 reg, u8 *val)
91 struct usb_device *dev = port->serial->dev;
93 tmp = kmalloc(sizeof(*val), GFP_KERNEL);
97 status = usb_control_msg(dev,
98 usb_rcvctrlpipe(dev, 0),
99 F81232_REGISTER_REQUEST,
105 USB_CTRL_GET_TIMEOUT);
106 if (status != sizeof(*val)) {
107 dev_err(&port->dev, "%s failed status: %d\n", __func__, status);
110 status = usb_translate_errors(status);
122 static int f81232_set_register(struct usb_serial_port *port, u16 reg, u8 val)
126 struct usb_device *dev = port->serial->dev;
128 tmp = kmalloc(sizeof(val), GFP_KERNEL);
134 status = usb_control_msg(dev,
135 usb_sndctrlpipe(dev, 0),
136 F81232_REGISTER_REQUEST,
142 USB_CTRL_SET_TIMEOUT);
143 if (status != sizeof(val)) {
144 dev_err(&port->dev, "%s failed status: %d\n", __func__, status);
147 status = usb_translate_errors(status);
158 static int f81232_set_mask_register(struct usb_serial_port *port, u16 reg,
164 status = f81232_get_register(port, reg, &tmp);
168 tmp = (tmp & ~mask) | (val & mask);
170 return f81232_set_register(port, reg, tmp);
173 static void f81232_read_msr(struct usb_serial_port *port)
177 struct tty_struct *tty;
178 struct f81232_private *priv = usb_get_serial_port_data(port);
180 mutex_lock(&priv->lock);
181 status = f81232_get_register(port, MODEM_STATUS_REGISTER,
184 dev_err(&port->dev, "%s fail, status: %d\n", __func__, status);
185 mutex_unlock(&priv->lock);
189 if (!(current_msr & UART_MSR_ANY_DELTA)) {
190 mutex_unlock(&priv->lock);
194 priv->modem_status = current_msr;
196 if (current_msr & UART_MSR_DCTS)
198 if (current_msr & UART_MSR_DDSR)
200 if (current_msr & UART_MSR_TERI)
202 if (current_msr & UART_MSR_DDCD) {
204 tty = tty_port_tty_get(&port->port);
206 usb_serial_handle_dcd_change(port, tty,
207 current_msr & UART_MSR_DCD);
213 wake_up_interruptible(&port->port.delta_msr_wait);
214 mutex_unlock(&priv->lock);
217 static int f81232_set_mctrl(struct usb_serial_port *port,
218 unsigned int set, unsigned int clear)
222 struct f81232_private *priv = usb_get_serial_port_data(port);
224 if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0)
225 return 0; /* no change */
227 /* 'set' takes precedence over 'clear' */
230 /* force enable interrupt with OUT2 */
231 mutex_lock(&priv->lock);
232 val = UART_MCR_OUT2 | priv->modem_control;
234 if (clear & TIOCM_DTR)
235 val &= ~UART_MCR_DTR;
237 if (clear & TIOCM_RTS)
238 val &= ~UART_MCR_RTS;
246 dev_dbg(&port->dev, "%s new:%02x old:%02x\n", __func__,
247 val, priv->modem_control);
249 status = f81232_set_register(port, MODEM_CONTROL_REGISTER, val);
251 dev_err(&port->dev, "%s set MCR status < 0\n", __func__);
252 mutex_unlock(&priv->lock);
256 priv->modem_control = val;
257 mutex_unlock(&priv->lock);
262 static void f81232_update_line_status(struct usb_serial_port *port,
264 size_t actual_length)
266 struct f81232_private *priv = usb_get_serial_port_data(port);
271 switch (data[0] & 0x07) {
272 case 0x00: /* msr change */
273 dev_dbg(&port->dev, "IIR: MSR Change: %02x\n", data[0]);
274 schedule_work(&priv->interrupt_work);
276 case 0x02: /* tx-empty */
278 case 0x04: /* rx data available */
280 case 0x06: /* lsr change */
281 /* we can forget it. the LSR will read from bulk-in */
282 dev_dbg(&port->dev, "IIR: LSR Change: %02x\n", data[0]);
287 static void f81232_read_int_callback(struct urb *urb)
289 struct usb_serial_port *port = urb->context;
290 unsigned char *data = urb->transfer_buffer;
291 unsigned int actual_length = urb->actual_length;
292 int status = urb->status;
302 /* this urb is terminated, clean up */
303 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
307 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
312 usb_serial_debug_data(&port->dev, __func__,
313 urb->actual_length, urb->transfer_buffer);
315 f81232_update_line_status(port, data, actual_length);
318 retval = usb_submit_urb(urb, GFP_ATOMIC);
320 dev_err(&urb->dev->dev,
321 "%s - usb_submit_urb failed with result %d\n",
325 static void f81232_process_read_urb(struct urb *urb)
327 struct usb_serial_port *port = urb->context;
328 struct f81232_private *priv = usb_get_serial_port_data(port);
329 unsigned char *data = urb->transfer_buffer;
335 * When opening the port we get a 1-byte packet with the current LSR,
338 if ((urb->actual_length < 2) || (urb->actual_length % 2))
341 /* bulk-in data: [LSR(1Byte)+DATA(1Byte)][LSR(1Byte)+DATA(1Byte)]... */
343 for (i = 0; i < urb->actual_length; i += 2) {
344 tty_flag = TTY_NORMAL;
347 if (lsr & UART_LSR_BRK_ERROR_BITS) {
348 if (lsr & UART_LSR_BI) {
349 tty_flag = TTY_BREAK;
351 usb_serial_handle_break(port);
352 } else if (lsr & UART_LSR_PE) {
353 tty_flag = TTY_PARITY;
354 port->icount.parity++;
355 } else if (lsr & UART_LSR_FE) {
356 tty_flag = TTY_FRAME;
357 port->icount.frame++;
360 if (lsr & UART_LSR_OE) {
361 port->icount.overrun++;
362 schedule_work(&priv->lsr_work);
363 tty_insert_flip_char(&port->port, 0,
368 if (port->port.console && port->sysrq) {
369 if (usb_serial_handle_sysrq_char(port, data[i + 1]))
373 tty_insert_flip_char(&port->port, data[i + 1], tty_flag);
376 tty_flip_buffer_push(&port->port);
379 static void f81232_break_ctl(struct tty_struct *tty, int break_state)
381 struct usb_serial_port *port = tty->driver_data;
382 struct f81232_private *priv = usb_get_serial_port_data(port);
385 mutex_lock(&priv->lock);
388 priv->shadow_lcr |= UART_LCR_SBC;
390 priv->shadow_lcr &= ~UART_LCR_SBC;
392 status = f81232_set_register(port, LINE_CONTROL_REGISTER,
395 dev_err(&port->dev, "set break failed: %d\n", status);
397 mutex_unlock(&priv->lock);
400 static int f81232_find_clk(speed_t baudrate)
404 for (idx = 0; idx < ARRAY_SIZE(baudrate_table); ++idx) {
405 if (baudrate <= baudrate_table[idx] &&
406 baudrate_table[idx] % baudrate == 0)
413 static void f81232_set_baudrate(struct tty_struct *tty,
414 struct usb_serial_port *port, speed_t baudrate,
415 speed_t old_baudrate)
417 struct f81232_private *priv = usb_get_serial_port_data(port);
423 speed_t baud_list[] = { baudrate, old_baudrate, F81232_DEF_BAUDRATE };
425 for (i = 0; i < ARRAY_SIZE(baud_list); ++i) {
426 idx = f81232_find_clk(baud_list[i]);
428 baudrate = baud_list[i];
429 tty_encode_baud_rate(tty, baudrate, baudrate);
437 priv->baud_base = baudrate_table[idx];
438 divisor = calc_baud_divisor(baudrate, priv->baud_base);
440 status = f81232_set_mask_register(port, F81232_CLK_REGISTER,
441 F81232_CLK_MASK, clock_table[idx]);
443 dev_err(&port->dev, "%s failed to set CLK_REG: %d\n",
448 status = f81232_get_register(port, LINE_CONTROL_REGISTER,
451 dev_err(&port->dev, "%s failed to get LCR: %d\n",
456 status = f81232_set_register(port, LINE_CONTROL_REGISTER,
457 lcr | UART_LCR_DLAB); /* Enable DLAB */
459 dev_err(&port->dev, "%s failed to set DLAB: %d\n",
464 status = f81232_set_register(port, RECEIVE_BUFFER_REGISTER,
465 divisor & 0x00ff); /* low */
467 dev_err(&port->dev, "%s failed to set baudrate MSB: %d\n",
472 status = f81232_set_register(port, INTERRUPT_ENABLE_REGISTER,
473 (divisor & 0xff00) >> 8); /* high */
475 dev_err(&port->dev, "%s failed to set baudrate LSB: %d\n",
480 status = f81232_set_register(port, LINE_CONTROL_REGISTER,
481 lcr & ~UART_LCR_DLAB);
483 dev_err(&port->dev, "%s failed to set DLAB: %d\n",
488 static int f81232_port_enable(struct usb_serial_port *port)
493 /* fifo on, trigger8, clear TX/RX*/
494 val = UART_FCR_TRIGGER_8 | UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR |
497 status = f81232_set_register(port, FIFO_CONTROL_REGISTER, val);
499 dev_err(&port->dev, "%s failed to set FCR: %d\n",
504 /* MSR Interrupt only, LSR will read from Bulk-in odd byte */
505 status = f81232_set_register(port, INTERRUPT_ENABLE_REGISTER,
508 dev_err(&port->dev, "%s failed to set IER: %d\n",
516 static int f81232_port_disable(struct usb_serial_port *port)
520 status = f81232_set_register(port, INTERRUPT_ENABLE_REGISTER, 0);
522 dev_err(&port->dev, "%s failed to set IER: %d\n",
530 static void f81232_set_termios(struct tty_struct *tty,
531 struct usb_serial_port *port, struct ktermios *old_termios)
533 struct f81232_private *priv = usb_get_serial_port_data(port);
539 /* Don't change anything if nothing has changed */
540 if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
543 if (C_BAUD(tty) == B0)
544 f81232_set_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
545 else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
546 f81232_set_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
548 baudrate = tty_get_baud_rate(tty);
551 old_baud = tty_termios_baud_rate(old_termios);
553 old_baud = F81232_DEF_BAUDRATE;
555 f81232_set_baudrate(tty, port, baudrate, old_baud);
559 new_lcr |= UART_LCR_PARITY;
562 new_lcr |= UART_LCR_EPAR;
565 new_lcr |= UART_LCR_SPAR;
569 new_lcr |= UART_LCR_STOP;
571 switch (C_CSIZE(tty)) {
573 new_lcr |= UART_LCR_WLEN5;
576 new_lcr |= UART_LCR_WLEN6;
579 new_lcr |= UART_LCR_WLEN7;
583 new_lcr |= UART_LCR_WLEN8;
587 mutex_lock(&priv->lock);
589 new_lcr |= (priv->shadow_lcr & UART_LCR_SBC);
590 status = f81232_set_register(port, LINE_CONTROL_REGISTER, new_lcr);
592 dev_err(&port->dev, "%s failed to set LCR: %d\n",
596 priv->shadow_lcr = new_lcr;
598 mutex_unlock(&priv->lock);
601 static int f81232_tiocmget(struct tty_struct *tty)
604 struct usb_serial_port *port = tty->driver_data;
605 struct f81232_private *port_priv = usb_get_serial_port_data(port);
608 /* force get current MSR changed state */
609 f81232_read_msr(port);
611 mutex_lock(&port_priv->lock);
612 mcr = port_priv->modem_control;
613 msr = port_priv->modem_status;
614 mutex_unlock(&port_priv->lock);
616 r = (mcr & UART_MCR_DTR ? TIOCM_DTR : 0) |
617 (mcr & UART_MCR_RTS ? TIOCM_RTS : 0) |
618 (msr & UART_MSR_CTS ? TIOCM_CTS : 0) |
619 (msr & UART_MSR_DCD ? TIOCM_CAR : 0) |
620 (msr & UART_MSR_RI ? TIOCM_RI : 0) |
621 (msr & UART_MSR_DSR ? TIOCM_DSR : 0);
626 static int f81232_tiocmset(struct tty_struct *tty,
627 unsigned int set, unsigned int clear)
629 struct usb_serial_port *port = tty->driver_data;
631 return f81232_set_mctrl(port, set, clear);
634 static int f81232_open(struct tty_struct *tty, struct usb_serial_port *port)
638 result = f81232_port_enable(port);
644 f81232_set_termios(tty, port, NULL);
646 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
648 dev_err(&port->dev, "%s - failed submitting interrupt urb,"
649 " error %d\n", __func__, result);
653 result = usb_serial_generic_open(tty, port);
655 usb_kill_urb(port->interrupt_in_urb);
662 static void f81232_close(struct usb_serial_port *port)
664 struct f81232_private *port_priv = usb_get_serial_port_data(port);
666 f81232_port_disable(port);
667 usb_serial_generic_close(port);
668 usb_kill_urb(port->interrupt_in_urb);
669 flush_work(&port_priv->interrupt_work);
670 flush_work(&port_priv->lsr_work);
673 static void f81232_dtr_rts(struct usb_serial_port *port, int on)
676 f81232_set_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
678 f81232_set_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
681 static int f81232_carrier_raised(struct usb_serial_port *port)
684 struct f81232_private *priv = usb_get_serial_port_data(port);
686 mutex_lock(&priv->lock);
687 msr = priv->modem_status;
688 mutex_unlock(&priv->lock);
690 if (msr & UART_MSR_DCD)
695 static int f81232_get_serial_info(struct tty_struct *tty,
696 struct serial_struct *ss)
698 struct usb_serial_port *port = tty->driver_data;
699 struct f81232_private *priv = usb_get_serial_port_data(port);
701 ss->type = PORT_16550A;
702 ss->line = port->minor;
703 ss->port = port->port_number;
704 ss->baud_base = priv->baud_base;
708 static void f81232_interrupt_work(struct work_struct *work)
710 struct f81232_private *priv =
711 container_of(work, struct f81232_private, interrupt_work);
713 f81232_read_msr(priv->port);
716 static void f81232_lsr_worker(struct work_struct *work)
718 struct f81232_private *priv;
719 struct usb_serial_port *port;
723 priv = container_of(work, struct f81232_private, lsr_work);
726 status = f81232_get_register(port, LINE_STATUS_REGISTER, &tmp);
728 dev_warn(&port->dev, "read LSR failed: %d\n", status);
731 static int f81232_port_probe(struct usb_serial_port *port)
733 struct f81232_private *priv;
735 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
739 mutex_init(&priv->lock);
740 INIT_WORK(&priv->interrupt_work, f81232_interrupt_work);
741 INIT_WORK(&priv->lsr_work, f81232_lsr_worker);
743 usb_set_serial_port_data(port, priv);
745 port->port.drain_delay = 256;
751 static int f81232_port_remove(struct usb_serial_port *port)
753 struct f81232_private *priv;
755 priv = usb_get_serial_port_data(port);
761 static int f81232_suspend(struct usb_serial *serial, pm_message_t message)
763 struct usb_serial_port *port = serial->port[0];
764 struct f81232_private *port_priv = usb_get_serial_port_data(port);
767 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
768 usb_kill_urb(port->read_urbs[i]);
770 usb_kill_urb(port->interrupt_in_urb);
773 flush_work(&port_priv->interrupt_work);
774 flush_work(&port_priv->lsr_work);
780 static int f81232_resume(struct usb_serial *serial)
782 struct usb_serial_port *port = serial->port[0];
785 if (tty_port_initialized(&port->port)) {
786 result = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
788 dev_err(&port->dev, "submit interrupt urb failed: %d\n",
794 return usb_serial_generic_resume(serial);
797 static struct usb_serial_driver f81232_device = {
799 .owner = THIS_MODULE,
802 .id_table = id_table,
805 .bulk_out_size = 256,
807 .close = f81232_close,
808 .dtr_rts = f81232_dtr_rts,
809 .carrier_raised = f81232_carrier_raised,
810 .get_serial = f81232_get_serial_info,
811 .break_ctl = f81232_break_ctl,
812 .set_termios = f81232_set_termios,
813 .tiocmget = f81232_tiocmget,
814 .tiocmset = f81232_tiocmset,
815 .tiocmiwait = usb_serial_generic_tiocmiwait,
816 .process_read_urb = f81232_process_read_urb,
817 .read_int_callback = f81232_read_int_callback,
818 .port_probe = f81232_port_probe,
819 .port_remove = f81232_port_remove,
820 .suspend = f81232_suspend,
821 .resume = f81232_resume,
824 static struct usb_serial_driver * const serial_drivers[] = {
829 module_usb_serial_driver(serial_drivers, id_table);
831 MODULE_DESCRIPTION("Fintek F81232 USB to serial adaptor driver");
834 MODULE_LICENSE("GPL v2");