1 // SPDX-License-Identifier: GPL-2.0
3 * Fintek F81232 USB to serial adaptor driver
5 * Copyright (C) 2012 Greg Kroah-Hartman (gregkh@linuxfoundation.org)
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 115200
33 /* USB Control EP parameter */
34 #define F81232_REGISTER_REQUEST 0xa0
35 #define F81232_GET_REGISTER 0xc0
36 #define F81232_SET_REGISTER 0x40
38 #define SERIAL_BASE_ADDRESS 0x0120
39 #define RECEIVE_BUFFER_REGISTER (0x00 + SERIAL_BASE_ADDRESS)
40 #define INTERRUPT_ENABLE_REGISTER (0x01 + SERIAL_BASE_ADDRESS)
41 #define FIFO_CONTROL_REGISTER (0x02 + SERIAL_BASE_ADDRESS)
42 #define LINE_CONTROL_REGISTER (0x03 + SERIAL_BASE_ADDRESS)
43 #define MODEM_CONTROL_REGISTER (0x04 + SERIAL_BASE_ADDRESS)
44 #define MODEM_STATUS_REGISTER (0x06 + SERIAL_BASE_ADDRESS)
46 struct f81232_private
{
50 struct work_struct interrupt_work
;
51 struct usb_serial_port
*port
;
54 static int calc_baud_divisor(speed_t baudrate
)
56 return DIV_ROUND_CLOSEST(F81232_MAX_BAUDRATE
, baudrate
);
59 static int f81232_get_register(struct usb_serial_port
*port
, u16 reg
, u8
*val
)
63 struct usb_device
*dev
= port
->serial
->dev
;
65 tmp
= kmalloc(sizeof(*val
), GFP_KERNEL
);
69 status
= usb_control_msg(dev
,
70 usb_rcvctrlpipe(dev
, 0),
71 F81232_REGISTER_REQUEST
,
77 USB_CTRL_GET_TIMEOUT
);
78 if (status
!= sizeof(*val
)) {
79 dev_err(&port
->dev
, "%s failed status: %d\n", __func__
, status
);
82 status
= usb_translate_errors(status
);
94 static int f81232_set_register(struct usb_serial_port
*port
, u16 reg
, u8 val
)
98 struct usb_device
*dev
= port
->serial
->dev
;
100 tmp
= kmalloc(sizeof(val
), GFP_KERNEL
);
106 status
= usb_control_msg(dev
,
107 usb_sndctrlpipe(dev
, 0),
108 F81232_REGISTER_REQUEST
,
114 USB_CTRL_SET_TIMEOUT
);
115 if (status
!= sizeof(val
)) {
116 dev_err(&port
->dev
, "%s failed status: %d\n", __func__
, status
);
119 status
= usb_translate_errors(status
);
130 static void f81232_read_msr(struct usb_serial_port
*port
)
134 struct tty_struct
*tty
;
135 struct f81232_private
*priv
= usb_get_serial_port_data(port
);
137 mutex_lock(&priv
->lock
);
138 status
= f81232_get_register(port
, MODEM_STATUS_REGISTER
,
141 dev_err(&port
->dev
, "%s fail, status: %d\n", __func__
, status
);
142 mutex_unlock(&priv
->lock
);
146 if (!(current_msr
& UART_MSR_ANY_DELTA
)) {
147 mutex_unlock(&priv
->lock
);
151 priv
->modem_status
= current_msr
;
153 if (current_msr
& UART_MSR_DCTS
)
155 if (current_msr
& UART_MSR_DDSR
)
157 if (current_msr
& UART_MSR_TERI
)
159 if (current_msr
& UART_MSR_DDCD
) {
161 tty
= tty_port_tty_get(&port
->port
);
163 usb_serial_handle_dcd_change(port
, tty
,
164 current_msr
& UART_MSR_DCD
);
170 wake_up_interruptible(&port
->port
.delta_msr_wait
);
171 mutex_unlock(&priv
->lock
);
174 static int f81232_set_mctrl(struct usb_serial_port
*port
,
175 unsigned int set
, unsigned int clear
)
179 struct f81232_private
*priv
= usb_get_serial_port_data(port
);
181 if (((set
| clear
) & (TIOCM_DTR
| TIOCM_RTS
)) == 0)
182 return 0; /* no change */
184 /* 'set' takes precedence over 'clear' */
187 /* force enable interrupt with OUT2 */
188 mutex_lock(&priv
->lock
);
189 val
= UART_MCR_OUT2
| priv
->modem_control
;
191 if (clear
& TIOCM_DTR
)
192 val
&= ~UART_MCR_DTR
;
194 if (clear
& TIOCM_RTS
)
195 val
&= ~UART_MCR_RTS
;
203 dev_dbg(&port
->dev
, "%s new:%02x old:%02x\n", __func__
,
204 val
, priv
->modem_control
);
206 status
= f81232_set_register(port
, MODEM_CONTROL_REGISTER
, val
);
208 dev_err(&port
->dev
, "%s set MCR status < 0\n", __func__
);
209 mutex_unlock(&priv
->lock
);
213 priv
->modem_control
= val
;
214 mutex_unlock(&priv
->lock
);
219 static void f81232_update_line_status(struct usb_serial_port
*port
,
221 size_t actual_length
)
223 struct f81232_private
*priv
= usb_get_serial_port_data(port
);
228 switch (data
[0] & 0x07) {
229 case 0x00: /* msr change */
230 dev_dbg(&port
->dev
, "IIR: MSR Change: %02x\n", data
[0]);
231 schedule_work(&priv
->interrupt_work
);
233 case 0x02: /* tx-empty */
235 case 0x04: /* rx data available */
237 case 0x06: /* lsr change */
238 /* we can forget it. the LSR will read from bulk-in */
239 dev_dbg(&port
->dev
, "IIR: LSR Change: %02x\n", data
[0]);
244 static void f81232_read_int_callback(struct urb
*urb
)
246 struct usb_serial_port
*port
= urb
->context
;
247 unsigned char *data
= urb
->transfer_buffer
;
248 unsigned int actual_length
= urb
->actual_length
;
249 int status
= urb
->status
;
259 /* this urb is terminated, clean up */
260 dev_dbg(&port
->dev
, "%s - urb shutting down with status: %d\n",
264 dev_dbg(&port
->dev
, "%s - nonzero urb status received: %d\n",
269 usb_serial_debug_data(&port
->dev
, __func__
,
270 urb
->actual_length
, urb
->transfer_buffer
);
272 f81232_update_line_status(port
, data
, actual_length
);
275 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
277 dev_err(&urb
->dev
->dev
,
278 "%s - usb_submit_urb failed with result %d\n",
282 static void f81232_process_read_urb(struct urb
*urb
)
284 struct usb_serial_port
*port
= urb
->context
;
285 unsigned char *data
= urb
->transfer_buffer
;
291 * When opening the port we get a 1-byte packet with the current LSR,
294 if ((urb
->actual_length
< 2) || (urb
->actual_length
% 2))
297 /* bulk-in data: [LSR(1Byte)+DATA(1Byte)][LSR(1Byte)+DATA(1Byte)]... */
299 for (i
= 0; i
< urb
->actual_length
; i
+= 2) {
300 tty_flag
= TTY_NORMAL
;
303 if (lsr
& UART_LSR_BRK_ERROR_BITS
) {
304 if (lsr
& UART_LSR_BI
) {
305 tty_flag
= TTY_BREAK
;
307 usb_serial_handle_break(port
);
308 } else if (lsr
& UART_LSR_PE
) {
309 tty_flag
= TTY_PARITY
;
310 port
->icount
.parity
++;
311 } else if (lsr
& UART_LSR_FE
) {
312 tty_flag
= TTY_FRAME
;
313 port
->icount
.frame
++;
316 if (lsr
& UART_LSR_OE
) {
317 port
->icount
.overrun
++;
318 tty_insert_flip_char(&port
->port
, 0,
323 if (port
->port
.console
&& port
->sysrq
) {
324 if (usb_serial_handle_sysrq_char(port
, data
[i
+ 1]))
328 tty_insert_flip_char(&port
->port
, data
[i
+ 1], tty_flag
);
331 tty_flip_buffer_push(&port
->port
);
334 static void f81232_break_ctl(struct tty_struct
*tty
, int break_state
)
336 /* FIXME - Stubbed out for now */
339 * break_state = -1 to turn on break, and 0 to turn off break
340 * see drivers/char/tty_io.c to see it used.
341 * last_set_data_urb_value NEVER has the break bit set in it.
345 static void f81232_set_baudrate(struct usb_serial_port
*port
, speed_t baudrate
)
351 divisor
= calc_baud_divisor(baudrate
);
353 status
= f81232_get_register(port
, LINE_CONTROL_REGISTER
,
356 dev_err(&port
->dev
, "%s failed to get LCR: %d\n",
361 status
= f81232_set_register(port
, LINE_CONTROL_REGISTER
,
362 lcr
| UART_LCR_DLAB
); /* Enable DLAB */
364 dev_err(&port
->dev
, "%s failed to set DLAB: %d\n",
369 status
= f81232_set_register(port
, RECEIVE_BUFFER_REGISTER
,
370 divisor
& 0x00ff); /* low */
372 dev_err(&port
->dev
, "%s failed to set baudrate MSB: %d\n",
377 status
= f81232_set_register(port
, INTERRUPT_ENABLE_REGISTER
,
378 (divisor
& 0xff00) >> 8); /* high */
380 dev_err(&port
->dev
, "%s failed to set baudrate LSB: %d\n",
385 status
= f81232_set_register(port
, LINE_CONTROL_REGISTER
,
386 lcr
& ~UART_LCR_DLAB
);
388 dev_err(&port
->dev
, "%s failed to set DLAB: %d\n",
393 static int f81232_port_enable(struct usb_serial_port
*port
)
398 /* fifo on, trigger8, clear TX/RX*/
399 val
= UART_FCR_TRIGGER_8
| UART_FCR_ENABLE_FIFO
| UART_FCR_CLEAR_RCVR
|
402 status
= f81232_set_register(port
, FIFO_CONTROL_REGISTER
, val
);
404 dev_err(&port
->dev
, "%s failed to set FCR: %d\n",
409 /* MSR Interrupt only, LSR will read from Bulk-in odd byte */
410 status
= f81232_set_register(port
, INTERRUPT_ENABLE_REGISTER
,
413 dev_err(&port
->dev
, "%s failed to set IER: %d\n",
421 static int f81232_port_disable(struct usb_serial_port
*port
)
425 status
= f81232_set_register(port
, INTERRUPT_ENABLE_REGISTER
, 0);
427 dev_err(&port
->dev
, "%s failed to set IER: %d\n",
435 static void f81232_set_termios(struct tty_struct
*tty
,
436 struct usb_serial_port
*port
, struct ktermios
*old_termios
)
442 /* Don't change anything if nothing has changed */
443 if (old_termios
&& !tty_termios_hw_change(&tty
->termios
, old_termios
))
446 if (C_BAUD(tty
) == B0
)
447 f81232_set_mctrl(port
, 0, TIOCM_DTR
| TIOCM_RTS
);
448 else if (old_termios
&& (old_termios
->c_cflag
& CBAUD
) == B0
)
449 f81232_set_mctrl(port
, TIOCM_DTR
| TIOCM_RTS
, 0);
451 baudrate
= tty_get_baud_rate(tty
);
453 if (baudrate
> F81232_MAX_BAUDRATE
) {
454 baudrate
= F81232_MAX_BAUDRATE
;
455 tty_encode_baud_rate(tty
, baudrate
, baudrate
);
457 f81232_set_baudrate(port
, baudrate
);
461 new_lcr
|= UART_LCR_PARITY
;
464 new_lcr
|= UART_LCR_EPAR
;
467 new_lcr
|= UART_LCR_SPAR
;
471 new_lcr
|= UART_LCR_STOP
;
473 switch (C_CSIZE(tty
)) {
475 new_lcr
|= UART_LCR_WLEN5
;
478 new_lcr
|= UART_LCR_WLEN6
;
481 new_lcr
|= UART_LCR_WLEN7
;
485 new_lcr
|= UART_LCR_WLEN8
;
489 status
= f81232_set_register(port
, LINE_CONTROL_REGISTER
, new_lcr
);
491 dev_err(&port
->dev
, "%s failed to set LCR: %d\n",
496 static int f81232_tiocmget(struct tty_struct
*tty
)
499 struct usb_serial_port
*port
= tty
->driver_data
;
500 struct f81232_private
*port_priv
= usb_get_serial_port_data(port
);
503 /* force get current MSR changed state */
504 f81232_read_msr(port
);
506 mutex_lock(&port_priv
->lock
);
507 mcr
= port_priv
->modem_control
;
508 msr
= port_priv
->modem_status
;
509 mutex_unlock(&port_priv
->lock
);
511 r
= (mcr
& UART_MCR_DTR
? TIOCM_DTR
: 0) |
512 (mcr
& UART_MCR_RTS
? TIOCM_RTS
: 0) |
513 (msr
& UART_MSR_CTS
? TIOCM_CTS
: 0) |
514 (msr
& UART_MSR_DCD
? TIOCM_CAR
: 0) |
515 (msr
& UART_MSR_RI
? TIOCM_RI
: 0) |
516 (msr
& UART_MSR_DSR
? TIOCM_DSR
: 0);
521 static int f81232_tiocmset(struct tty_struct
*tty
,
522 unsigned int set
, unsigned int clear
)
524 struct usb_serial_port
*port
= tty
->driver_data
;
526 return f81232_set_mctrl(port
, set
, clear
);
529 static int f81232_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
533 result
= f81232_port_enable(port
);
539 f81232_set_termios(tty
, port
, NULL
);
541 result
= usb_submit_urb(port
->interrupt_in_urb
, GFP_KERNEL
);
543 dev_err(&port
->dev
, "%s - failed submitting interrupt urb,"
544 " error %d\n", __func__
, result
);
548 result
= usb_serial_generic_open(tty
, port
);
550 usb_kill_urb(port
->interrupt_in_urb
);
557 static void f81232_close(struct usb_serial_port
*port
)
559 f81232_port_disable(port
);
560 usb_serial_generic_close(port
);
561 usb_kill_urb(port
->interrupt_in_urb
);
564 static void f81232_dtr_rts(struct usb_serial_port
*port
, int on
)
567 f81232_set_mctrl(port
, TIOCM_DTR
| TIOCM_RTS
, 0);
569 f81232_set_mctrl(port
, 0, TIOCM_DTR
| TIOCM_RTS
);
572 static int f81232_carrier_raised(struct usb_serial_port
*port
)
575 struct f81232_private
*priv
= usb_get_serial_port_data(port
);
577 mutex_lock(&priv
->lock
);
578 msr
= priv
->modem_status
;
579 mutex_unlock(&priv
->lock
);
581 if (msr
& UART_MSR_DCD
)
586 static int f81232_get_serial_info(struct usb_serial_port
*port
,
589 struct serial_struct ser
;
591 memset(&ser
, 0, sizeof(ser
));
593 ser
.type
= PORT_16550A
;
594 ser
.line
= port
->minor
;
595 ser
.port
= port
->port_number
;
596 ser
.baud_base
= F81232_MAX_BAUDRATE
;
598 if (copy_to_user((void __user
*)arg
, &ser
, sizeof(ser
)))
604 static int f81232_ioctl(struct tty_struct
*tty
,
605 unsigned int cmd
, unsigned long arg
)
607 struct usb_serial_port
*port
= tty
->driver_data
;
611 return f81232_get_serial_info(port
, arg
);
618 static void f81232_interrupt_work(struct work_struct
*work
)
620 struct f81232_private
*priv
=
621 container_of(work
, struct f81232_private
, interrupt_work
);
623 f81232_read_msr(priv
->port
);
626 static int f81232_port_probe(struct usb_serial_port
*port
)
628 struct f81232_private
*priv
;
630 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
634 mutex_init(&priv
->lock
);
635 INIT_WORK(&priv
->interrupt_work
, f81232_interrupt_work
);
637 usb_set_serial_port_data(port
, priv
);
639 port
->port
.drain_delay
= 256;
645 static int f81232_port_remove(struct usb_serial_port
*port
)
647 struct f81232_private
*priv
;
649 priv
= usb_get_serial_port_data(port
);
655 static struct usb_serial_driver f81232_device
= {
657 .owner
= THIS_MODULE
,
660 .id_table
= id_table
,
663 .bulk_out_size
= 256,
665 .close
= f81232_close
,
666 .dtr_rts
= f81232_dtr_rts
,
667 .carrier_raised
= f81232_carrier_raised
,
668 .ioctl
= f81232_ioctl
,
669 .break_ctl
= f81232_break_ctl
,
670 .set_termios
= f81232_set_termios
,
671 .tiocmget
= f81232_tiocmget
,
672 .tiocmset
= f81232_tiocmset
,
673 .tiocmiwait
= usb_serial_generic_tiocmiwait
,
674 .process_read_urb
= f81232_process_read_urb
,
675 .read_int_callback
= f81232_read_int_callback
,
676 .port_probe
= f81232_port_probe
,
677 .port_remove
= f81232_port_remove
,
680 static struct usb_serial_driver
* const serial_drivers
[] = {
685 module_usb_serial_driver(serial_drivers
, id_table
);
687 MODULE_DESCRIPTION("Fintek F81232 USB to serial adaptor driver");
688 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");
689 MODULE_AUTHOR("Peter Hong <peter_hong@fintek.com.tw>");
690 MODULE_LICENSE("GPL v2");