xref: /linux/drivers/usb/serial/f81232.c (revision 8f668fbbd571bbd5187a9a0eae150b768fc388ac)
1 /*
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 
28 static const struct usb_device_id id_table[] = {
29 	{ USB_DEVICE(0x1934, 0x0706) },
30 	{ }					/* Terminating entry */
31 };
32 MODULE_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 
48 struct f81232_private {
49 	spinlock_t lock;
50 	u8 line_control;
51 	u8 line_status;
52 };
53 
54 static void f81232_update_line_status(struct usb_serial_port *port,
55 				      unsigned char *data,
56 				      unsigned int actual_length)
57 {
58 	/*
59 	 * FIXME: Update port->icount, and call
60 	 *
61 	 *		wake_up_interruptible(&port->port.delta_msr_wait);
62 	 *
63 	 *	  on MSR changes.
64 	 */
65 }
66 
67 static void f81232_read_int_callback(struct urb *urb)
68 {
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;
73 	int retval;
74 
75 	switch (status) {
76 	case 0:
77 		/* success */
78 		break;
79 	case -ECONNRESET:
80 	case -ENOENT:
81 	case -ESHUTDOWN:
82 		/* this urb is terminated, clean up */
83 		dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
84 			__func__, status);
85 		return;
86 	default:
87 		dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
88 			__func__, status);
89 		goto exit;
90 	}
91 
92 	usb_serial_debug_data(&port->dev, __func__,
93 			      urb->actual_length, urb->transfer_buffer);
94 
95 	f81232_update_line_status(port, data, actual_length);
96 
97 exit:
98 	retval = usb_submit_urb(urb, GFP_ATOMIC);
99 	if (retval)
100 		dev_err(&urb->dev->dev,
101 			"%s - usb_submit_urb failed with result %d\n",
102 			__func__, retval);
103 }
104 
105 static void f81232_process_read_urb(struct urb *urb)
106 {
107 	struct usb_serial_port *port = urb->context;
108 	struct f81232_private *priv = usb_get_serial_port_data(port);
109 	unsigned char *data = urb->transfer_buffer;
110 	char tty_flag = TTY_NORMAL;
111 	unsigned long flags;
112 	u8 line_status;
113 	int i;
114 
115 	/* update line status */
116 	spin_lock_irqsave(&priv->lock, flags);
117 	line_status = priv->line_status;
118 	priv->line_status &= ~UART_STATE_TRANSIENT_MASK;
119 	spin_unlock_irqrestore(&priv->lock, flags);
120 
121 	if (!urb->actual_length)
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;
132 	dev_dbg(&port->dev, "%s - tty_flag = %d\n", __func__, tty_flag);
133 
134 	/* overrun is special, not associated with a char */
135 	if (line_status & UART_OVERRUN_ERROR)
136 		tty_insert_flip_char(&port->port, 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(&port->port, data[i],
142 						tty_flag);
143 	} else {
144 		tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
145 							urb->actual_length);
146 	}
147 
148 	tty_flip_buffer_push(&port->port);
149 }
150 
151 static int set_control_lines(struct usb_device *dev, u8 value)
152 {
153 	/* FIXME - Stubbed out for now */
154 	return 0;
155 }
156 
157 static 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 
168 static 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 */
174 	if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
175 		return;
176 
177 	/* Do the real work here... */
178 	if (old_termios)
179 		tty_termios_copy_hw(&tty->termios, old_termios);
180 }
181 
182 static int f81232_tiocmget(struct tty_struct *tty)
183 {
184 	/* FIXME - Stubbed out for now */
185 	return 0;
186 }
187 
188 static int f81232_tiocmset(struct tty_struct *tty,
189 			unsigned int set, unsigned int clear)
190 {
191 	/* FIXME - Stubbed out for now */
192 	return 0;
193 }
194 
195 static int f81232_open(struct tty_struct *tty, struct usb_serial_port *port)
196 {
197 	int result;
198 
199 	/* Setup termios */
200 	if (tty)
201 		f81232_set_termios(tty, port, NULL);
202 
203 	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 	return 0;
217 }
218 
219 static void f81232_close(struct usb_serial_port *port)
220 {
221 	usb_serial_generic_close(port);
222 	usb_kill_urb(port->interrupt_in_urb);
223 }
224 
225 static void f81232_dtr_rts(struct usb_serial_port *port, int on)
226 {
227 	struct f81232_private *priv = usb_get_serial_port_data(port);
228 	unsigned long flags;
229 	u8 control;
230 
231 	spin_lock_irqsave(&priv->lock, flags);
232 	/* Change DTR and RTS */
233 	if (on)
234 		priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
235 	else
236 		priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
237 	control = priv->line_control;
238 	spin_unlock_irqrestore(&priv->lock, flags);
239 	set_control_lines(port->serial->dev, control);
240 }
241 
242 static int f81232_carrier_raised(struct usb_serial_port *port)
243 {
244 	struct f81232_private *priv = usb_get_serial_port_data(port);
245 	if (priv->line_status & UART_DCD)
246 		return 1;
247 	return 0;
248 }
249 
250 static int f81232_ioctl(struct tty_struct *tty,
251 			unsigned int cmd, unsigned long arg)
252 {
253 	struct serial_struct ser;
254 	struct usb_serial_port *port = tty->driver_data;
255 
256 	switch (cmd) {
257 	case TIOCGSERIAL:
258 		memset(&ser, 0, sizeof ser);
259 		ser.type = PORT_16654;
260 		ser.line = port->minor;
261 		ser.port = port->port_number;
262 		ser.baud_base = 460800;
263 
264 		if (copy_to_user((void __user *)arg, &ser, sizeof ser))
265 			return -EFAULT;
266 
267 		return 0;
268 	default:
269 		break;
270 	}
271 	return -ENOIOCTLCMD;
272 }
273 
274 static int f81232_port_probe(struct usb_serial_port *port)
275 {
276 	struct f81232_private *priv;
277 
278 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
279 	if (!priv)
280 		return -ENOMEM;
281 
282 	spin_lock_init(&priv->lock);
283 
284 	usb_set_serial_port_data(port, priv);
285 
286 	port->port.drain_delay = 256;
287 
288 	return 0;
289 }
290 
291 static int f81232_port_remove(struct usb_serial_port *port)
292 {
293 	struct f81232_private *priv;
294 
295 	priv = usb_get_serial_port_data(port);
296 	kfree(priv);
297 
298 	return 0;
299 }
300 
301 static struct usb_serial_driver f81232_device = {
302 	.driver = {
303 		.owner =	THIS_MODULE,
304 		.name =		"f81232",
305 	},
306 	.id_table =		id_table,
307 	.num_ports =		1,
308 	.bulk_in_size =		256,
309 	.bulk_out_size =	256,
310 	.open =			f81232_open,
311 	.close =		f81232_close,
312 	.dtr_rts = 		f81232_dtr_rts,
313 	.carrier_raised =	f81232_carrier_raised,
314 	.ioctl =		f81232_ioctl,
315 	.break_ctl =		f81232_break_ctl,
316 	.set_termios =		f81232_set_termios,
317 	.tiocmget =		f81232_tiocmget,
318 	.tiocmset =		f81232_tiocmset,
319 	.tiocmiwait =		usb_serial_generic_tiocmiwait,
320 	.process_read_urb =	f81232_process_read_urb,
321 	.read_int_callback =	f81232_read_int_callback,
322 	.port_probe =		f81232_port_probe,
323 	.port_remove =		f81232_port_remove,
324 };
325 
326 static struct usb_serial_driver * const serial_drivers[] = {
327 	&f81232_device,
328 	NULL,
329 };
330 
331 module_usb_serial_driver(serial_drivers, id_table);
332 
333 MODULE_DESCRIPTION("Fintek F81232 USB to serial adaptor driver");
334 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org");
335 MODULE_LICENSE("GPL v2");
336