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 #define CONTROL_DTR 0x01
35 #define CONTROL_RTS 0x02
37 #define UART_STATE 0x08
38 #define UART_STATE_TRANSIENT_MASK 0x74
41 #define UART_BREAK_ERROR 0x04
42 #define UART_RING 0x08
43 #define UART_FRAME_ERROR 0x10
44 #define UART_PARITY_ERROR 0x20
45 #define UART_OVERRUN_ERROR 0x40
48 struct f81232_private {
54 static void f81232_update_line_status(struct usb_serial_port *port,
56 unsigned int actual_length)
59 * FIXME: Update port->icount, and call
61 * wake_up_interruptible(&port->port.delta_msr_wait);
67 static void f81232_read_int_callback(struct urb *urb)
69 struct usb_serial_port *port = urb->context;
70 unsigned char *data = urb->transfer_buffer;
71 unsigned int actual_length = urb->actual_length;
72 int status = urb->status;
82 /* this urb is terminated, clean up */
83 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
87 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
92 usb_serial_debug_data(&port->dev, __func__,
93 urb->actual_length, urb->transfer_buffer);
95 f81232_update_line_status(port, data, actual_length);
98 retval = usb_submit_urb(urb, GFP_ATOMIC);
100 dev_err(&urb->dev->dev,
101 "%s - usb_submit_urb failed with result %d\n",
105 static void f81232_process_read_urb(struct urb *urb)
107 struct usb_serial_port *port = urb->context;
108 unsigned char *data = urb->transfer_buffer;
114 * When opening the port we get a 1-byte packet with the current LSR,
117 if ((urb->actual_length < 2) || (urb->actual_length % 2))
120 /* bulk-in data: [LSR(1Byte)+DATA(1Byte)][LSR(1Byte)+DATA(1Byte)]... */
122 for (i = 0; i < urb->actual_length; i += 2) {
123 tty_flag = TTY_NORMAL;
126 if (lsr & UART_LSR_BRK_ERROR_BITS) {
127 if (lsr & UART_LSR_BI) {
128 tty_flag = TTY_BREAK;
130 usb_serial_handle_break(port);
131 } else if (lsr & UART_LSR_PE) {
132 tty_flag = TTY_PARITY;
133 port->icount.parity++;
134 } else if (lsr & UART_LSR_FE) {
135 tty_flag = TTY_FRAME;
136 port->icount.frame++;
139 if (lsr & UART_LSR_OE) {
140 port->icount.overrun++;
141 tty_insert_flip_char(&port->port, 0,
146 if (port->port.console && port->sysrq) {
147 if (usb_serial_handle_sysrq_char(port, data[i + 1]))
151 tty_insert_flip_char(&port->port, data[i + 1], tty_flag);
154 tty_flip_buffer_push(&port->port);
157 static int set_control_lines(struct usb_device *dev, u8 value)
159 /* FIXME - Stubbed out for now */
163 static void f81232_break_ctl(struct tty_struct *tty, int break_state)
165 /* FIXME - Stubbed out for now */
168 * break_state = -1 to turn on break, and 0 to turn off break
169 * see drivers/char/tty_io.c to see it used.
170 * last_set_data_urb_value NEVER has the break bit set in it.
174 static void f81232_set_termios(struct tty_struct *tty,
175 struct usb_serial_port *port, struct ktermios *old_termios)
177 /* FIXME - Stubbed out for now */
179 /* Don't change anything if nothing has changed */
180 if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
183 /* Do the real work here... */
185 tty_termios_copy_hw(&tty->termios, old_termios);
188 static int f81232_tiocmget(struct tty_struct *tty)
190 /* FIXME - Stubbed out for now */
194 static int f81232_tiocmset(struct tty_struct *tty,
195 unsigned int set, unsigned int clear)
197 /* FIXME - Stubbed out for now */
201 static int f81232_open(struct tty_struct *tty, struct usb_serial_port *port)
207 f81232_set_termios(tty, port, NULL);
209 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
211 dev_err(&port->dev, "%s - failed submitting interrupt urb,"
212 " error %d\n", __func__, result);
216 result = usb_serial_generic_open(tty, port);
218 usb_kill_urb(port->interrupt_in_urb);
225 static void f81232_close(struct usb_serial_port *port)
227 usb_serial_generic_close(port);
228 usb_kill_urb(port->interrupt_in_urb);
231 static void f81232_dtr_rts(struct usb_serial_port *port, int on)
233 struct f81232_private *priv = usb_get_serial_port_data(port);
236 mutex_lock(&priv->lock);
237 /* Change DTR and RTS */
239 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
241 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
242 control = priv->line_control;
243 mutex_unlock(&priv->lock);
244 set_control_lines(port->serial->dev, control);
247 static int f81232_carrier_raised(struct usb_serial_port *port)
249 struct f81232_private *priv = usb_get_serial_port_data(port);
250 if (priv->modem_status & UART_DCD)
255 static int f81232_ioctl(struct tty_struct *tty,
256 unsigned int cmd, unsigned long arg)
258 struct serial_struct ser;
259 struct usb_serial_port *port = tty->driver_data;
263 memset(&ser, 0, sizeof ser);
264 ser.type = PORT_16654;
265 ser.line = port->minor;
266 ser.port = port->port_number;
267 ser.baud_base = 460800;
269 if (copy_to_user((void __user *)arg, &ser, sizeof ser))
279 static int f81232_port_probe(struct usb_serial_port *port)
281 struct f81232_private *priv;
283 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
287 mutex_init(&priv->lock);
289 usb_set_serial_port_data(port, priv);
291 port->port.drain_delay = 256;
296 static int f81232_port_remove(struct usb_serial_port *port)
298 struct f81232_private *priv;
300 priv = usb_get_serial_port_data(port);
306 static struct usb_serial_driver f81232_device = {
308 .owner = THIS_MODULE,
311 .id_table = id_table,
314 .bulk_out_size = 256,
316 .close = f81232_close,
317 .dtr_rts = f81232_dtr_rts,
318 .carrier_raised = f81232_carrier_raised,
319 .ioctl = f81232_ioctl,
320 .break_ctl = f81232_break_ctl,
321 .set_termios = f81232_set_termios,
322 .tiocmget = f81232_tiocmget,
323 .tiocmset = f81232_tiocmset,
324 .tiocmiwait = usb_serial_generic_tiocmiwait,
325 .process_read_urb = f81232_process_read_urb,
326 .read_int_callback = f81232_read_int_callback,
327 .port_probe = f81232_port_probe,
328 .port_remove = f81232_port_remove,
331 static struct usb_serial_driver * const serial_drivers[] = {
336 module_usb_serial_driver(serial_drivers, id_table);
338 MODULE_DESCRIPTION("Fintek F81232 USB to serial adaptor driver");
339 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org");
340 MODULE_LICENSE("GPL v2");