2 * Fintek F81232 USB to serial adaptor driver
4 * Copyright (C) 2012 Greg Kroah-Hartman (gregkh@linuxfoundation.org)
5 * Copyright (C) 2012 Linux Foundation
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16 #include <linux/tty.h>
17 #include <linux/tty_driver.h>
18 #include <linux/tty_flip.h>
19 #include <linux/serial.h>
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/mutex.h>
23 #include <linux/uaccess.h>
24 #include <linux/usb.h>
25 #include <linux/usb/serial.h>
26 #include <linux/serial_reg.h>
28 static const struct usb_device_id id_table
[] = {
29 { USB_DEVICE(0x1934, 0x0706) },
30 { } /* Terminating entry */
32 MODULE_DEVICE_TABLE(usb
, id_table
);
34 /* Maximum baudrate for F81232 */
35 #define F81232_MAX_BAUDRATE 115200
37 /* USB Control EP parameter */
38 #define F81232_REGISTER_REQUEST 0xa0
39 #define F81232_GET_REGISTER 0xc0
40 #define F81232_SET_REGISTER 0x40
42 #define SERIAL_BASE_ADDRESS 0x0120
43 #define RECEIVE_BUFFER_REGISTER (0x00 + SERIAL_BASE_ADDRESS)
44 #define INTERRUPT_ENABLE_REGISTER (0x01 + SERIAL_BASE_ADDRESS)
45 #define FIFO_CONTROL_REGISTER (0x02 + SERIAL_BASE_ADDRESS)
46 #define LINE_CONTROL_REGISTER (0x03 + SERIAL_BASE_ADDRESS)
47 #define MODEM_CONTROL_REGISTER (0x04 + SERIAL_BASE_ADDRESS)
48 #define MODEM_STATUS_REGISTER (0x06 + SERIAL_BASE_ADDRESS)
50 struct f81232_private
{
54 struct work_struct interrupt_work
;
55 struct usb_serial_port
*port
;
58 static int calc_baud_divisor(speed_t baudrate
)
60 return DIV_ROUND_CLOSEST(F81232_MAX_BAUDRATE
, baudrate
);
63 static int f81232_get_register(struct usb_serial_port
*port
, u16 reg
, u8
*val
)
67 struct usb_device
*dev
= port
->serial
->dev
;
69 tmp
= kmalloc(sizeof(*val
), GFP_KERNEL
);
73 status
= usb_control_msg(dev
,
74 usb_rcvctrlpipe(dev
, 0),
75 F81232_REGISTER_REQUEST
,
81 USB_CTRL_GET_TIMEOUT
);
82 if (status
!= sizeof(*val
)) {
83 dev_err(&port
->dev
, "%s failed status: %d\n", __func__
, status
);
86 status
= usb_translate_errors(status
);
98 static int f81232_set_register(struct usb_serial_port
*port
, u16 reg
, u8 val
)
102 struct usb_device
*dev
= port
->serial
->dev
;
104 tmp
= kmalloc(sizeof(val
), GFP_KERNEL
);
110 status
= usb_control_msg(dev
,
111 usb_sndctrlpipe(dev
, 0),
112 F81232_REGISTER_REQUEST
,
118 USB_CTRL_SET_TIMEOUT
);
119 if (status
!= sizeof(val
)) {
120 dev_err(&port
->dev
, "%s failed status: %d\n", __func__
, status
);
123 status
= usb_translate_errors(status
);
134 static void f81232_read_msr(struct usb_serial_port
*port
)
138 struct tty_struct
*tty
;
139 struct f81232_private
*priv
= usb_get_serial_port_data(port
);
141 mutex_lock(&priv
->lock
);
142 status
= f81232_get_register(port
, MODEM_STATUS_REGISTER
,
145 dev_err(&port
->dev
, "%s fail, status: %d\n", __func__
, status
);
146 mutex_unlock(&priv
->lock
);
150 if (!(current_msr
& UART_MSR_ANY_DELTA
)) {
151 mutex_unlock(&priv
->lock
);
155 priv
->modem_status
= current_msr
;
157 if (current_msr
& UART_MSR_DCTS
)
159 if (current_msr
& UART_MSR_DDSR
)
161 if (current_msr
& UART_MSR_TERI
)
163 if (current_msr
& UART_MSR_DDCD
) {
165 tty
= tty_port_tty_get(&port
->port
);
167 usb_serial_handle_dcd_change(port
, tty
,
168 current_msr
& UART_MSR_DCD
);
174 wake_up_interruptible(&port
->port
.delta_msr_wait
);
175 mutex_unlock(&priv
->lock
);
178 static int f81232_set_mctrl(struct usb_serial_port
*port
,
179 unsigned int set
, unsigned int clear
)
183 struct f81232_private
*priv
= usb_get_serial_port_data(port
);
185 if (((set
| clear
) & (TIOCM_DTR
| TIOCM_RTS
)) == 0)
186 return 0; /* no change */
188 /* 'set' takes precedence over 'clear' */
191 /* force enable interrupt with OUT2 */
192 mutex_lock(&priv
->lock
);
193 val
= UART_MCR_OUT2
| priv
->modem_control
;
195 if (clear
& TIOCM_DTR
)
196 val
&= ~UART_MCR_DTR
;
198 if (clear
& TIOCM_RTS
)
199 val
&= ~UART_MCR_RTS
;
207 dev_dbg(&port
->dev
, "%s new:%02x old:%02x\n", __func__
,
208 val
, priv
->modem_control
);
210 status
= f81232_set_register(port
, MODEM_CONTROL_REGISTER
, val
);
212 dev_err(&port
->dev
, "%s set MCR status < 0\n", __func__
);
213 mutex_unlock(&priv
->lock
);
217 priv
->modem_control
= val
;
218 mutex_unlock(&priv
->lock
);
223 static void f81232_update_line_status(struct usb_serial_port
*port
,
225 size_t actual_length
)
227 struct f81232_private
*priv
= usb_get_serial_port_data(port
);
232 switch (data
[0] & 0x07) {
233 case 0x00: /* msr change */
234 dev_dbg(&port
->dev
, "IIR: MSR Change: %02x\n", data
[0]);
235 schedule_work(&priv
->interrupt_work
);
237 case 0x02: /* tx-empty */
239 case 0x04: /* rx data available */
241 case 0x06: /* lsr change */
242 /* we can forget it. the LSR will read from bulk-in */
243 dev_dbg(&port
->dev
, "IIR: LSR Change: %02x\n", data
[0]);
248 static void f81232_read_int_callback(struct urb
*urb
)
250 struct usb_serial_port
*port
= urb
->context
;
251 unsigned char *data
= urb
->transfer_buffer
;
252 unsigned int actual_length
= urb
->actual_length
;
253 int status
= urb
->status
;
263 /* this urb is terminated, clean up */
264 dev_dbg(&port
->dev
, "%s - urb shutting down with status: %d\n",
268 dev_dbg(&port
->dev
, "%s - nonzero urb status received: %d\n",
273 usb_serial_debug_data(&port
->dev
, __func__
,
274 urb
->actual_length
, urb
->transfer_buffer
);
276 f81232_update_line_status(port
, data
, actual_length
);
279 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
281 dev_err(&urb
->dev
->dev
,
282 "%s - usb_submit_urb failed with result %d\n",
286 static void f81232_process_read_urb(struct urb
*urb
)
288 struct usb_serial_port
*port
= urb
->context
;
289 unsigned char *data
= urb
->transfer_buffer
;
295 * When opening the port we get a 1-byte packet with the current LSR,
298 if ((urb
->actual_length
< 2) || (urb
->actual_length
% 2))
301 /* bulk-in data: [LSR(1Byte)+DATA(1Byte)][LSR(1Byte)+DATA(1Byte)]... */
303 for (i
= 0; i
< urb
->actual_length
; i
+= 2) {
304 tty_flag
= TTY_NORMAL
;
307 if (lsr
& UART_LSR_BRK_ERROR_BITS
) {
308 if (lsr
& UART_LSR_BI
) {
309 tty_flag
= TTY_BREAK
;
311 usb_serial_handle_break(port
);
312 } else if (lsr
& UART_LSR_PE
) {
313 tty_flag
= TTY_PARITY
;
314 port
->icount
.parity
++;
315 } else if (lsr
& UART_LSR_FE
) {
316 tty_flag
= TTY_FRAME
;
317 port
->icount
.frame
++;
320 if (lsr
& UART_LSR_OE
) {
321 port
->icount
.overrun
++;
322 tty_insert_flip_char(&port
->port
, 0,
327 if (port
->port
.console
&& port
->sysrq
) {
328 if (usb_serial_handle_sysrq_char(port
, data
[i
+ 1]))
332 tty_insert_flip_char(&port
->port
, data
[i
+ 1], tty_flag
);
335 tty_flip_buffer_push(&port
->port
);
338 static void f81232_break_ctl(struct tty_struct
*tty
, int break_state
)
340 /* FIXME - Stubbed out for now */
343 * break_state = -1 to turn on break, and 0 to turn off break
344 * see drivers/char/tty_io.c to see it used.
345 * last_set_data_urb_value NEVER has the break bit set in it.
349 static void f81232_set_baudrate(struct usb_serial_port
*port
, speed_t baudrate
)
355 divisor
= calc_baud_divisor(baudrate
);
357 status
= f81232_get_register(port
, LINE_CONTROL_REGISTER
,
360 dev_err(&port
->dev
, "%s failed to get LCR: %d\n",
365 status
= f81232_set_register(port
, LINE_CONTROL_REGISTER
,
366 lcr
| UART_LCR_DLAB
); /* Enable DLAB */
368 dev_err(&port
->dev
, "%s failed to set DLAB: %d\n",
373 status
= f81232_set_register(port
, RECEIVE_BUFFER_REGISTER
,
374 divisor
& 0x00ff); /* low */
376 dev_err(&port
->dev
, "%s failed to set baudrate MSB: %d\n",
381 status
= f81232_set_register(port
, INTERRUPT_ENABLE_REGISTER
,
382 (divisor
& 0xff00) >> 8); /* high */
384 dev_err(&port
->dev
, "%s failed to set baudrate LSB: %d\n",
389 status
= f81232_set_register(port
, LINE_CONTROL_REGISTER
,
390 lcr
& ~UART_LCR_DLAB
);
392 dev_err(&port
->dev
, "%s failed to set DLAB: %d\n",
397 static int f81232_port_enable(struct usb_serial_port
*port
)
402 /* fifo on, trigger8, clear TX/RX*/
403 val
= UART_FCR_TRIGGER_8
| UART_FCR_ENABLE_FIFO
| UART_FCR_CLEAR_RCVR
|
406 status
= f81232_set_register(port
, FIFO_CONTROL_REGISTER
, val
);
408 dev_err(&port
->dev
, "%s failed to set FCR: %d\n",
413 /* MSR Interrupt only, LSR will read from Bulk-in odd byte */
414 status
= f81232_set_register(port
, INTERRUPT_ENABLE_REGISTER
,
417 dev_err(&port
->dev
, "%s failed to set IER: %d\n",
425 static int f81232_port_disable(struct usb_serial_port
*port
)
429 status
= f81232_set_register(port
, INTERRUPT_ENABLE_REGISTER
, 0);
431 dev_err(&port
->dev
, "%s failed to set IER: %d\n",
439 static void f81232_set_termios(struct tty_struct
*tty
,
440 struct usb_serial_port
*port
, struct ktermios
*old_termios
)
446 /* Don't change anything if nothing has changed */
447 if (old_termios
&& !tty_termios_hw_change(&tty
->termios
, old_termios
))
450 if (C_BAUD(tty
) == B0
)
451 f81232_set_mctrl(port
, 0, TIOCM_DTR
| TIOCM_RTS
);
452 else if (old_termios
&& (old_termios
->c_cflag
& CBAUD
) == B0
)
453 f81232_set_mctrl(port
, TIOCM_DTR
| TIOCM_RTS
, 0);
455 baudrate
= tty_get_baud_rate(tty
);
457 if (baudrate
> F81232_MAX_BAUDRATE
) {
458 baudrate
= F81232_MAX_BAUDRATE
;
459 tty_encode_baud_rate(tty
, baudrate
, baudrate
);
461 f81232_set_baudrate(port
, baudrate
);
465 new_lcr
|= UART_LCR_PARITY
;
468 new_lcr
|= UART_LCR_EPAR
;
471 new_lcr
|= UART_LCR_SPAR
;
475 new_lcr
|= UART_LCR_STOP
;
477 switch (C_CSIZE(tty
)) {
479 new_lcr
|= UART_LCR_WLEN5
;
482 new_lcr
|= UART_LCR_WLEN6
;
485 new_lcr
|= UART_LCR_WLEN7
;
489 new_lcr
|= UART_LCR_WLEN8
;
493 status
= f81232_set_register(port
, LINE_CONTROL_REGISTER
, new_lcr
);
495 dev_err(&port
->dev
, "%s failed to set LCR: %d\n",
500 static int f81232_tiocmget(struct tty_struct
*tty
)
503 struct usb_serial_port
*port
= tty
->driver_data
;
504 struct f81232_private
*port_priv
= usb_get_serial_port_data(port
);
507 /* force get current MSR changed state */
508 f81232_read_msr(port
);
510 mutex_lock(&port_priv
->lock
);
511 mcr
= port_priv
->modem_control
;
512 msr
= port_priv
->modem_status
;
513 mutex_unlock(&port_priv
->lock
);
515 r
= (mcr
& UART_MCR_DTR
? TIOCM_DTR
: 0) |
516 (mcr
& UART_MCR_RTS
? TIOCM_RTS
: 0) |
517 (msr
& UART_MSR_CTS
? TIOCM_CTS
: 0) |
518 (msr
& UART_MSR_DCD
? TIOCM_CAR
: 0) |
519 (msr
& UART_MSR_RI
? TIOCM_RI
: 0) |
520 (msr
& UART_MSR_DSR
? TIOCM_DSR
: 0);
525 static int f81232_tiocmset(struct tty_struct
*tty
,
526 unsigned int set
, unsigned int clear
)
528 struct usb_serial_port
*port
= tty
->driver_data
;
530 return f81232_set_mctrl(port
, set
, clear
);
533 static int f81232_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
537 result
= f81232_port_enable(port
);
543 f81232_set_termios(tty
, port
, NULL
);
545 result
= usb_submit_urb(port
->interrupt_in_urb
, GFP_KERNEL
);
547 dev_err(&port
->dev
, "%s - failed submitting interrupt urb,"
548 " error %d\n", __func__
, result
);
552 result
= usb_serial_generic_open(tty
, port
);
554 usb_kill_urb(port
->interrupt_in_urb
);
561 static void f81232_close(struct usb_serial_port
*port
)
563 f81232_port_disable(port
);
564 usb_serial_generic_close(port
);
565 usb_kill_urb(port
->interrupt_in_urb
);
568 static void f81232_dtr_rts(struct usb_serial_port
*port
, int on
)
571 f81232_set_mctrl(port
, TIOCM_DTR
| TIOCM_RTS
, 0);
573 f81232_set_mctrl(port
, 0, TIOCM_DTR
| TIOCM_RTS
);
576 static int f81232_carrier_raised(struct usb_serial_port
*port
)
579 struct f81232_private
*priv
= usb_get_serial_port_data(port
);
581 mutex_lock(&priv
->lock
);
582 msr
= priv
->modem_status
;
583 mutex_unlock(&priv
->lock
);
585 if (msr
& UART_MSR_DCD
)
590 static int f81232_get_serial_info(struct usb_serial_port
*port
,
593 struct serial_struct ser
;
595 memset(&ser
, 0, sizeof(ser
));
597 ser
.type
= PORT_16550A
;
598 ser
.line
= port
->minor
;
599 ser
.port
= port
->port_number
;
600 ser
.baud_base
= F81232_MAX_BAUDRATE
;
602 if (copy_to_user((void __user
*)arg
, &ser
, sizeof(ser
)))
608 static int f81232_ioctl(struct tty_struct
*tty
,
609 unsigned int cmd
, unsigned long arg
)
611 struct usb_serial_port
*port
= tty
->driver_data
;
615 return f81232_get_serial_info(port
, arg
);
622 static void f81232_interrupt_work(struct work_struct
*work
)
624 struct f81232_private
*priv
=
625 container_of(work
, struct f81232_private
, interrupt_work
);
627 f81232_read_msr(priv
->port
);
630 static int f81232_port_probe(struct usb_serial_port
*port
)
632 struct f81232_private
*priv
;
634 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
638 mutex_init(&priv
->lock
);
639 INIT_WORK(&priv
->interrupt_work
, f81232_interrupt_work
);
641 usb_set_serial_port_data(port
, priv
);
643 port
->port
.drain_delay
= 256;
649 static int f81232_port_remove(struct usb_serial_port
*port
)
651 struct f81232_private
*priv
;
653 priv
= usb_get_serial_port_data(port
);
659 static struct usb_serial_driver f81232_device
= {
661 .owner
= THIS_MODULE
,
664 .id_table
= id_table
,
667 .bulk_out_size
= 256,
669 .close
= f81232_close
,
670 .dtr_rts
= f81232_dtr_rts
,
671 .carrier_raised
= f81232_carrier_raised
,
672 .ioctl
= f81232_ioctl
,
673 .break_ctl
= f81232_break_ctl
,
674 .set_termios
= f81232_set_termios
,
675 .tiocmget
= f81232_tiocmget
,
676 .tiocmset
= f81232_tiocmset
,
677 .tiocmiwait
= usb_serial_generic_tiocmiwait
,
678 .process_read_urb
= f81232_process_read_urb
,
679 .read_int_callback
= f81232_read_int_callback
,
680 .port_probe
= f81232_port_probe
,
681 .port_remove
= f81232_port_remove
,
684 static struct usb_serial_driver
* const serial_drivers
[] = {
689 module_usb_serial_driver(serial_drivers
, id_table
);
691 MODULE_DESCRIPTION("Fintek F81232 USB to serial adaptor driver");
692 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");
693 MODULE_AUTHOR("Peter Hong <peter_hong@fintek.com.tw>");
694 MODULE_LICENSE("GPL v2");