blob: 6e4eb57d0177406c5eb8f9d30c83f0551dd159f7 [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>
15#include <linux/init.h>
16#include <linux/slab.h>
17#include <linux/tty.h>
18#include <linux/tty_driver.h>
19#include <linux/tty_flip.h>
20#include <linux/serial.h>
21#include <linux/module.h>
22#include <linux/moduleparam.h>
23#include <linux/spinlock.h>
24#include <linux/uaccess.h>
25#include <linux/usb.h>
26#include <linux/usb/serial.h>
27
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -080028static const struct usb_device_id id_table[] = {
29 { USB_DEVICE(0x1934, 0x0706) },
30 { } /* Terminating entry */
31};
32MODULE_DEVICE_TABLE(usb, id_table);
33
34#define CONTROL_DTR 0x01
35#define CONTROL_RTS 0x02
36
37#define UART_STATE 0x08
38#define UART_STATE_TRANSIENT_MASK 0x74
39#define UART_DCD 0x01
40#define UART_DSR 0x02
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
46#define UART_CTS 0x80
47
48struct f81232_private {
49 spinlock_t lock;
50 wait_queue_head_t delta_msr_wait;
51 u8 line_control;
52 u8 line_status;
53};
54
55static void f81232_update_line_status(struct usb_serial_port *port,
56 unsigned char *data,
57 unsigned int actual_length)
58{
59}
60
61static void f81232_read_int_callback(struct urb *urb)
62{
63 struct usb_serial_port *port = urb->context;
64 unsigned char *data = urb->transfer_buffer;
65 unsigned int actual_length = urb->actual_length;
66 int status = urb->status;
67 int retval;
68
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -080069 switch (status) {
70 case 0:
71 /* success */
72 break;
73 case -ECONNRESET:
74 case -ENOENT:
75 case -ESHUTDOWN:
76 /* this urb is terminated, clean up */
Greg Kroah-Hartmana94e9b92012-05-15 16:27:17 -070077 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
78 __func__, status);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -080079 return;
80 default:
Greg Kroah-Hartmana94e9b92012-05-15 16:27:17 -070081 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
82 __func__, status);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -080083 goto exit;
84 }
85
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +010086 usb_serial_debug_data(&port->dev, __func__,
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -080087 urb->actual_length, urb->transfer_buffer);
88
89 f81232_update_line_status(port, data, actual_length);
90
91exit:
92 retval = usb_submit_urb(urb, GFP_ATOMIC);
93 if (retval)
94 dev_err(&urb->dev->dev,
95 "%s - usb_submit_urb failed with result %d\n",
96 __func__, retval);
97}
98
99static void f81232_process_read_urb(struct urb *urb)
100{
101 struct usb_serial_port *port = urb->context;
102 struct f81232_private *priv = usb_get_serial_port_data(port);
103 struct tty_struct *tty;
104 unsigned char *data = urb->transfer_buffer;
105 char tty_flag = TTY_NORMAL;
106 unsigned long flags;
107 u8 line_status;
108 int i;
109
110 /* update line status */
111 spin_lock_irqsave(&priv->lock, flags);
112 line_status = priv->line_status;
113 priv->line_status &= ~UART_STATE_TRANSIENT_MASK;
114 spin_unlock_irqrestore(&priv->lock, flags);
115 wake_up_interruptible(&priv->delta_msr_wait);
116
117 if (!urb->actual_length)
118 return;
119
120 tty = tty_port_tty_get(&port->port);
121 if (!tty)
122 return;
123
124 /* break takes precedence over parity, */
125 /* which takes precedence over framing errors */
126 if (line_status & UART_BREAK_ERROR)
127 tty_flag = TTY_BREAK;
128 else if (line_status & UART_PARITY_ERROR)
129 tty_flag = TTY_PARITY;
130 else if (line_status & UART_FRAME_ERROR)
131 tty_flag = TTY_FRAME;
Greg Kroah-Hartmana94e9b92012-05-15 16:27:17 -0700132 dev_dbg(&port->dev, "%s - tty_flag = %d\n", __func__, tty_flag);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800133
134 /* overrun is special, not associated with a char */
135 if (line_status & UART_OVERRUN_ERROR)
136 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
137
138 if (port->port.console && port->sysrq) {
139 for (i = 0; i < urb->actual_length; ++i)
140 if (!usb_serial_handle_sysrq_char(port, data[i]))
141 tty_insert_flip_char(tty, data[i], tty_flag);
142 } else {
143 tty_insert_flip_string_fixed_flag(tty, data, tty_flag,
144 urb->actual_length);
145 }
146
147 tty_flip_buffer_push(tty);
148 tty_kref_put(tty);
149}
150
151static int set_control_lines(struct usb_device *dev, u8 value)
152{
153 /* FIXME - Stubbed out for now */
154 return 0;
155}
156
157static void f81232_break_ctl(struct tty_struct *tty, int break_state)
158{
159 /* FIXME - Stubbed out for now */
160
161 /*
162 * break_state = -1 to turn on break, and 0 to turn off break
163 * see drivers/char/tty_io.c to see it used.
164 * last_set_data_urb_value NEVER has the break bit set in it.
165 */
166}
167
168static void f81232_set_termios(struct tty_struct *tty,
169 struct usb_serial_port *port, struct ktermios *old_termios)
170{
171 /* FIXME - Stubbed out for now */
172
173 /* Don't change anything if nothing has changed */
Alan Coxadc8d742012-07-14 15:31:47 +0100174 if (!tty_termios_hw_change(&tty->termios, old_termios))
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800175 return;
176
177 /* Do the real work here... */
Alan Coxc97ce272012-07-14 15:32:10 +0100178 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{
196 struct ktermios tmp_termios;
197 int result;
198
199 /* Setup termios */
200 if (tty)
201 f81232_set_termios(tty, port, &tmp_termios);
202
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800203 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
204 if (result) {
205 dev_err(&port->dev, "%s - failed submitting interrupt urb,"
206 " error %d\n", __func__, result);
207 return result;
208 }
209
210 result = usb_serial_generic_open(tty, port);
211 if (result) {
212 usb_kill_urb(port->interrupt_in_urb);
213 return result;
214 }
215
216 port->port.drain_delay = 256;
217 return 0;
218}
219
220static void f81232_close(struct usb_serial_port *port)
221{
222 usb_serial_generic_close(port);
223 usb_kill_urb(port->interrupt_in_urb);
224}
225
226static void f81232_dtr_rts(struct usb_serial_port *port, int on)
227{
228 struct f81232_private *priv = usb_get_serial_port_data(port);
229 unsigned long flags;
230 u8 control;
231
232 spin_lock_irqsave(&priv->lock, flags);
233 /* Change DTR and RTS */
234 if (on)
235 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
236 else
237 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
238 control = priv->line_control;
239 spin_unlock_irqrestore(&priv->lock, flags);
240 set_control_lines(port->serial->dev, control);
241}
242
243static int f81232_carrier_raised(struct usb_serial_port *port)
244{
245 struct f81232_private *priv = usb_get_serial_port_data(port);
246 if (priv->line_status & UART_DCD)
247 return 1;
248 return 0;
249}
250
251static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
252{
253 struct f81232_private *priv = usb_get_serial_port_data(port);
254 unsigned long flags;
255 unsigned int prevstatus;
256 unsigned int status;
257 unsigned int changed;
258
259 spin_lock_irqsave(&priv->lock, flags);
260 prevstatus = priv->line_status;
261 spin_unlock_irqrestore(&priv->lock, flags);
262
263 while (1) {
264 interruptible_sleep_on(&priv->delta_msr_wait);
265 /* see if a signal did it */
266 if (signal_pending(current))
267 return -ERESTARTSYS;
268
269 spin_lock_irqsave(&priv->lock, flags);
270 status = priv->line_status;
271 spin_unlock_irqrestore(&priv->lock, flags);
272
273 changed = prevstatus ^ status;
274
275 if (((arg & TIOCM_RNG) && (changed & UART_RING)) ||
276 ((arg & TIOCM_DSR) && (changed & UART_DSR)) ||
277 ((arg & TIOCM_CD) && (changed & UART_DCD)) ||
278 ((arg & TIOCM_CTS) && (changed & UART_CTS))) {
279 return 0;
280 }
281 prevstatus = status;
282 }
283 /* NOTREACHED */
284 return 0;
285}
286
287static int f81232_ioctl(struct tty_struct *tty,
288 unsigned int cmd, unsigned long arg)
289{
290 struct serial_struct ser;
291 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartmana94e9b92012-05-15 16:27:17 -0700292
293 dev_dbg(&port->dev, "%s (%d) cmd = 0x%04x\n", __func__,
294 port->number, cmd);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800295
296 switch (cmd) {
297 case TIOCGSERIAL:
298 memset(&ser, 0, sizeof ser);
299 ser.type = PORT_16654;
300 ser.line = port->serial->minor;
301 ser.port = port->number;
302 ser.baud_base = 460800;
303
304 if (copy_to_user((void __user *)arg, &ser, sizeof ser))
305 return -EFAULT;
306
307 return 0;
308
309 case TIOCMIWAIT:
Greg Kroah-Hartmana94e9b92012-05-15 16:27:17 -0700310 dev_dbg(&port->dev, "%s (%d) TIOCMIWAIT\n", __func__,
311 port->number);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800312 return wait_modem_info(port, arg);
313 default:
Greg Kroah-Hartmana94e9b92012-05-15 16:27:17 -0700314 dev_dbg(&port->dev, "%s not supported = 0x%04x\n",
315 __func__, cmd);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800316 break;
317 }
318 return -ENOIOCTLCMD;
319}
320
Johan Hovold3124d1d2012-10-17 13:34:56 +0200321static int f81232_port_probe(struct usb_serial_port *port)
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800322{
323 struct f81232_private *priv;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800324
Johan Hovold3124d1d2012-10-17 13:34:56 +0200325 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
326 if (!priv)
327 return -ENOMEM;
328
329 spin_lock_init(&priv->lock);
330 init_waitqueue_head(&priv->delta_msr_wait);
331
332 usb_set_serial_port_data(port, priv);
333
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800334 return 0;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800335}
336
Johan Hovold3124d1d2012-10-17 13:34:56 +0200337static int f81232_port_remove(struct usb_serial_port *port)
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800338{
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800339 struct f81232_private *priv;
340
Johan Hovold3124d1d2012-10-17 13:34:56 +0200341 priv = usb_get_serial_port_data(port);
342 kfree(priv);
343
344 return 0;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800345}
346
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800347static struct usb_serial_driver f81232_device = {
348 .driver = {
349 .owner = THIS_MODULE,
350 .name = "f81232",
351 },
352 .id_table = id_table,
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800353 .num_ports = 1,
354 .bulk_in_size = 256,
355 .bulk_out_size = 256,
356 .open = f81232_open,
357 .close = f81232_close,
358 .dtr_rts = f81232_dtr_rts,
359 .carrier_raised = f81232_carrier_raised,
360 .ioctl = f81232_ioctl,
361 .break_ctl = f81232_break_ctl,
362 .set_termios = f81232_set_termios,
363 .tiocmget = f81232_tiocmget,
364 .tiocmset = f81232_tiocmset,
365 .process_read_urb = f81232_process_read_urb,
366 .read_int_callback = f81232_read_int_callback,
Johan Hovold3124d1d2012-10-17 13:34:56 +0200367 .port_probe = f81232_port_probe,
368 .port_remove = f81232_port_remove,
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800369};
370
371static struct usb_serial_driver * const serial_drivers[] = {
372 &f81232_device,
373 NULL,
374};
375
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700376module_usb_serial_driver(serial_drivers, id_table);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800377
378MODULE_DESCRIPTION("Fintek F81232 USB to serial adaptor driver");
379MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org");
380MODULE_LICENSE("GPL v2");