blob: c5dc233db2d94bcbdfb9bde55770f6daf625979e [file] [log] [blame]
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -08001/*
2 * Fintek F81232 USB to serial adaptor driver
3 *
4 * Copyright (C) 2012 Greg Kroah-Hartman (gregkh@linuxfoundation.org)
5 * Copyright (C) 2012 Linux Foundation
6 *
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.
10 *
11 */
12
13#include <linux/kernel.h>
14#include <linux/errno.h>
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -080015#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/spinlock.h>
23#include <linux/uaccess.h>
24#include <linux/usb.h>
25#include <linux/usb/serial.h>
26
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -080027static const struct usb_device_id id_table[] = {
28 { USB_DEVICE(0x1934, 0x0706) },
29 { } /* Terminating entry */
30};
31MODULE_DEVICE_TABLE(usb, id_table);
32
33#define CONTROL_DTR 0x01
34#define CONTROL_RTS 0x02
35
36#define UART_STATE 0x08
37#define UART_STATE_TRANSIENT_MASK 0x74
38#define UART_DCD 0x01
39#define UART_DSR 0x02
40#define UART_BREAK_ERROR 0x04
41#define UART_RING 0x08
42#define UART_FRAME_ERROR 0x10
43#define UART_PARITY_ERROR 0x20
44#define UART_OVERRUN_ERROR 0x40
45#define UART_CTS 0x80
46
47struct f81232_private {
48 spinlock_t lock;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -080049 u8 line_control;
50 u8 line_status;
51};
52
53static void f81232_update_line_status(struct usb_serial_port *port,
54 unsigned char *data,
55 unsigned int actual_length)
56{
Johan Hovold49fabf22013-12-29 19:22:57 +010057 /*
Johan Hovoldc50db822013-12-29 19:22:58 +010058 * FIXME: Update port->icount, and call
Johan Hovold49fabf22013-12-29 19:22:57 +010059 *
60 * wake_up_interruptible(&port->port.delta_msr_wait);
61 *
62 * on MSR changes.
63 */
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -080064}
65
66static void f81232_read_int_callback(struct urb *urb)
67{
68 struct usb_serial_port *port = urb->context;
69 unsigned char *data = urb->transfer_buffer;
70 unsigned int actual_length = urb->actual_length;
71 int status = urb->status;
72 int retval;
73
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -080074 switch (status) {
75 case 0:
76 /* success */
77 break;
78 case -ECONNRESET:
79 case -ENOENT:
80 case -ESHUTDOWN:
81 /* this urb is terminated, clean up */
Greg Kroah-Hartmana94e9b92012-05-15 16:27:17 -070082 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
83 __func__, status);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -080084 return;
85 default:
Greg Kroah-Hartmana94e9b92012-05-15 16:27:17 -070086 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
87 __func__, status);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -080088 goto exit;
89 }
90
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +010091 usb_serial_debug_data(&port->dev, __func__,
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -080092 urb->actual_length, urb->transfer_buffer);
93
94 f81232_update_line_status(port, data, actual_length);
95
96exit:
97 retval = usb_submit_urb(urb, GFP_ATOMIC);
98 if (retval)
99 dev_err(&urb->dev->dev,
100 "%s - usb_submit_urb failed with result %d\n",
101 __func__, retval);
102}
103
104static void f81232_process_read_urb(struct urb *urb)
105{
106 struct usb_serial_port *port = urb->context;
107 struct f81232_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800108 unsigned char *data = urb->transfer_buffer;
109 char tty_flag = TTY_NORMAL;
110 unsigned long flags;
111 u8 line_status;
112 int i;
113
114 /* update line status */
115 spin_lock_irqsave(&priv->lock, flags);
116 line_status = priv->line_status;
117 priv->line_status &= ~UART_STATE_TRANSIENT_MASK;
118 spin_unlock_irqrestore(&priv->lock, flags);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800119
120 if (!urb->actual_length)
121 return;
122
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800123 /* break takes precedence over parity, */
124 /* which takes precedence over framing errors */
125 if (line_status & UART_BREAK_ERROR)
126 tty_flag = TTY_BREAK;
127 else if (line_status & UART_PARITY_ERROR)
128 tty_flag = TTY_PARITY;
129 else if (line_status & UART_FRAME_ERROR)
130 tty_flag = TTY_FRAME;
Greg Kroah-Hartmana94e9b92012-05-15 16:27:17 -0700131 dev_dbg(&port->dev, "%s - tty_flag = %d\n", __func__, tty_flag);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800132
133 /* overrun is special, not associated with a char */
134 if (line_status & UART_OVERRUN_ERROR)
Jiri Slaby92a19f92013-01-03 15:53:03 +0100135 tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800136
137 if (port->port.console && port->sysrq) {
138 for (i = 0; i < urb->actual_length; ++i)
139 if (!usb_serial_handle_sysrq_char(port, data[i]))
Jiri Slaby92a19f92013-01-03 15:53:03 +0100140 tty_insert_flip_char(&port->port, data[i],
141 tty_flag);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800142 } else {
Jiri Slaby2f693352013-01-03 15:53:02 +0100143 tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800144 urb->actual_length);
145 }
146
Jiri Slaby2e124b42013-01-03 15:53:06 +0100147 tty_flip_buffer_push(&port->port);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800148}
149
150static int set_control_lines(struct usb_device *dev, u8 value)
151{
152 /* FIXME - Stubbed out for now */
153 return 0;
154}
155
156static void f81232_break_ctl(struct tty_struct *tty, int break_state)
157{
158 /* FIXME - Stubbed out for now */
159
160 /*
161 * break_state = -1 to turn on break, and 0 to turn off break
162 * see drivers/char/tty_io.c to see it used.
163 * last_set_data_urb_value NEVER has the break bit set in it.
164 */
165}
166
167static void f81232_set_termios(struct tty_struct *tty,
168 struct usb_serial_port *port, struct ktermios *old_termios)
169{
170 /* FIXME - Stubbed out for now */
171
172 /* Don't change anything if nothing has changed */
Johan Hovold21886722013-06-10 18:29:37 +0200173 if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800174 return;
175
176 /* Do the real work here... */
Johan Hovold21886722013-06-10 18:29:37 +0200177 if (old_termios)
178 tty_termios_copy_hw(&tty->termios, old_termios);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800179}
180
181static int f81232_tiocmget(struct tty_struct *tty)
182{
183 /* FIXME - Stubbed out for now */
184 return 0;
185}
186
187static int f81232_tiocmset(struct tty_struct *tty,
188 unsigned int set, unsigned int clear)
189{
190 /* FIXME - Stubbed out for now */
191 return 0;
192}
193
194static int f81232_open(struct tty_struct *tty, struct usb_serial_port *port)
195{
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800196 int result;
197
198 /* Setup termios */
199 if (tty)
Johan Hovold21886722013-06-10 18:29:37 +0200200 f81232_set_termios(tty, port, NULL);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800201
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800202 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
203 if (result) {
204 dev_err(&port->dev, "%s - failed submitting interrupt urb,"
205 " error %d\n", __func__, result);
206 return result;
207 }
208
209 result = usb_serial_generic_open(tty, port);
210 if (result) {
211 usb_kill_urb(port->interrupt_in_urb);
212 return result;
213 }
214
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800215 return 0;
216}
217
218static void f81232_close(struct usb_serial_port *port)
219{
220 usb_serial_generic_close(port);
221 usb_kill_urb(port->interrupt_in_urb);
222}
223
224static void f81232_dtr_rts(struct usb_serial_port *port, int on)
225{
226 struct f81232_private *priv = usb_get_serial_port_data(port);
227 unsigned long flags;
228 u8 control;
229
230 spin_lock_irqsave(&priv->lock, flags);
231 /* Change DTR and RTS */
232 if (on)
233 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
234 else
235 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
236 control = priv->line_control;
237 spin_unlock_irqrestore(&priv->lock, flags);
238 set_control_lines(port->serial->dev, control);
239}
240
241static int f81232_carrier_raised(struct usb_serial_port *port)
242{
243 struct f81232_private *priv = usb_get_serial_port_data(port);
244 if (priv->line_status & UART_DCD)
245 return 1;
246 return 0;
247}
248
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800249static int f81232_ioctl(struct tty_struct *tty,
250 unsigned int cmd, unsigned long arg)
251{
252 struct serial_struct ser;
253 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartmana94e9b92012-05-15 16:27:17 -0700254
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800255 switch (cmd) {
256 case TIOCGSERIAL:
257 memset(&ser, 0, sizeof ser);
258 ser.type = PORT_16654;
Greg Kroah-Hartmane5b1e202013-06-07 11:04:28 -0700259 ser.line = port->minor;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -0700260 ser.port = port->port_number;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800261 ser.baud_base = 460800;
262
263 if (copy_to_user((void __user *)arg, &ser, sizeof ser))
264 return -EFAULT;
265
266 return 0;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800267 default:
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800268 break;
269 }
270 return -ENOIOCTLCMD;
271}
272
Johan Hovold3124d1d2012-10-17 13:34:56 +0200273static int f81232_port_probe(struct usb_serial_port *port)
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800274{
275 struct f81232_private *priv;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800276
Johan Hovold3124d1d2012-10-17 13:34:56 +0200277 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
278 if (!priv)
279 return -ENOMEM;
280
281 spin_lock_init(&priv->lock);
Johan Hovold3124d1d2012-10-17 13:34:56 +0200282
283 usb_set_serial_port_data(port, priv);
284
Johan Hovoldd7be6222013-06-26 16:47:23 +0200285 port->port.drain_delay = 256;
286
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800287 return 0;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800288}
289
Johan Hovold3124d1d2012-10-17 13:34:56 +0200290static int f81232_port_remove(struct usb_serial_port *port)
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800291{
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800292 struct f81232_private *priv;
293
Johan Hovold3124d1d2012-10-17 13:34:56 +0200294 priv = usb_get_serial_port_data(port);
295 kfree(priv);
296
297 return 0;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800298}
299
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800300static struct usb_serial_driver f81232_device = {
301 .driver = {
302 .owner = THIS_MODULE,
303 .name = "f81232",
304 },
305 .id_table = id_table,
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800306 .num_ports = 1,
307 .bulk_in_size = 256,
308 .bulk_out_size = 256,
309 .open = f81232_open,
310 .close = f81232_close,
311 .dtr_rts = f81232_dtr_rts,
312 .carrier_raised = f81232_carrier_raised,
313 .ioctl = f81232_ioctl,
314 .break_ctl = f81232_break_ctl,
315 .set_termios = f81232_set_termios,
316 .tiocmget = f81232_tiocmget,
317 .tiocmset = f81232_tiocmset,
Johan Hovoldc50db822013-12-29 19:22:58 +0100318 .tiocmiwait = usb_serial_generic_tiocmiwait,
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800319 .process_read_urb = f81232_process_read_urb,
320 .read_int_callback = f81232_read_int_callback,
Johan Hovold3124d1d2012-10-17 13:34:56 +0200321 .port_probe = f81232_port_probe,
322 .port_remove = f81232_port_remove,
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800323};
324
325static struct usb_serial_driver * const serial_drivers[] = {
326 &f81232_device,
327 NULL,
328};
329
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700330module_usb_serial_driver(serial_drivers, id_table);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800331
332MODULE_DESCRIPTION("Fintek F81232 USB to serial adaptor driver");
333MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org");
334MODULE_LICENSE("GPL v2");