xref: /linux/drivers/usb/serial/belkin_sa.c (revision 7b4456767fd478ff1dbee55444d5e3e5a7194089)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Belkin USB Serial Adapter Driver
4  *
5  *  Copyright (C) 2000		William Greathouse (wgreathouse@smva.com)
6  *  Copyright (C) 2000-2001	Greg Kroah-Hartman (greg@kroah.com)
7  *  Copyright (C) 2010		Johan Hovold (jhovold@gmail.com)
8  *
9  *  This program is largely derived from work by the linux-usb group
10  *  and associated source files.  Please see the usb/serial files for
11  *  individual credits and copyrights.
12  *
13  * See Documentation/usb/usb-serial.rst for more information on using this
14  * driver
15  *
16  * TODO:
17  * -- Add true modem control line query capability.  Currently we track the
18  *    states reported by the interrupt and the states we request.
19  * -- Add support for flush commands
20  */
21 
22 #include <linux/kernel.h>
23 #include <linux/errno.h>
24 #include <linux/slab.h>
25 #include <linux/tty.h>
26 #include <linux/tty_flip.h>
27 #include <linux/module.h>
28 #include <linux/spinlock.h>
29 #include <linux/usb.h>
30 #include <linux/usb/serial.h>
31 #include "belkin_sa.h"
32 
33 #define DRIVER_AUTHOR "William Greathouse <wgreathouse@smva.com>"
34 #define DRIVER_DESC "USB Belkin Serial converter driver"
35 
36 /* function prototypes for a Belkin USB Serial Adapter F5U103 */
37 static int belkin_sa_port_probe(struct usb_serial_port *port);
38 static void belkin_sa_port_remove(struct usb_serial_port *port);
39 static int  belkin_sa_open(struct tty_struct *tty,
40 			struct usb_serial_port *port);
41 static void belkin_sa_close(struct usb_serial_port *port);
42 static void belkin_sa_read_int_callback(struct urb *urb);
43 static void belkin_sa_process_read_urb(struct urb *urb);
44 static void belkin_sa_set_termios(struct tty_struct *tty,
45 				  struct usb_serial_port *port,
46 				  const struct ktermios *old_termios);
47 static int belkin_sa_break_ctl(struct tty_struct *tty, int break_state);
48 static int  belkin_sa_tiocmget(struct tty_struct *tty);
49 static int  belkin_sa_tiocmset(struct tty_struct *tty,
50 					unsigned int set, unsigned int clear);
51 
52 
53 static const struct usb_device_id id_table[] = {
54 	{ USB_DEVICE(BELKIN_SA_VID, BELKIN_SA_PID) },
55 	{ USB_DEVICE(BELKIN_OLD_VID, BELKIN_OLD_PID) },
56 	{ USB_DEVICE(PERACOM_VID, PERACOM_PID) },
57 	{ USB_DEVICE(GOHUBS_VID, GOHUBS_PID) },
58 	{ USB_DEVICE(GOHUBS_VID, HANDYLINK_PID) },
59 	{ USB_DEVICE(BELKIN_DOCKSTATION_VID, BELKIN_DOCKSTATION_PID) },
60 	{ }	/* Terminating entry */
61 };
62 MODULE_DEVICE_TABLE(usb, id_table);
63 
64 /* All of the device info needed for the serial converters */
65 static struct usb_serial_driver belkin_device = {
66 	.driver = {
67 		.name =		"belkin",
68 	},
69 	.description =		"Belkin / Peracom / GoHubs USB Serial Adapter",
70 	.id_table =		id_table,
71 	.num_ports =		1,
72 	.open =			belkin_sa_open,
73 	.close =		belkin_sa_close,
74 	.read_int_callback =	belkin_sa_read_int_callback,
75 	.process_read_urb =	belkin_sa_process_read_urb,
76 	.set_termios =		belkin_sa_set_termios,
77 	.break_ctl =		belkin_sa_break_ctl,
78 	.tiocmget =		belkin_sa_tiocmget,
79 	.tiocmset =		belkin_sa_tiocmset,
80 	.port_probe =		belkin_sa_port_probe,
81 	.port_remove =		belkin_sa_port_remove,
82 };
83 
84 static struct usb_serial_driver * const serial_drivers[] = {
85 	&belkin_device, NULL
86 };
87 
88 struct belkin_sa_private {
89 	spinlock_t		lock;
90 	unsigned long		control_state;
91 	unsigned char		last_lsr;
92 	unsigned char		last_msr;
93 	int			bad_flow_control;
94 };
95 
96 
97 /*
98  * ***************************************************************************
99  * Belkin USB Serial Adapter F5U103 specific driver functions
100  * ***************************************************************************
101  */
102 
103 #define WDR_TIMEOUT 5000 /* default urb timeout */
104 
105 /* assumes that struct usb_serial *serial is available */
106 #define BSA_USB_CMD(c, v) usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), \
107 					    (c), BELKIN_SA_SET_REQUEST_TYPE, \
108 					    (v), 0, NULL, 0, WDR_TIMEOUT)
109 
110 static int belkin_sa_port_probe(struct usb_serial_port *port)
111 {
112 	struct usb_device *dev = port->serial->dev;
113 	struct belkin_sa_private *priv;
114 
115 	priv = kmalloc_obj(struct belkin_sa_private);
116 	if (!priv)
117 		return -ENOMEM;
118 
119 	spin_lock_init(&priv->lock);
120 	priv->control_state = 0;
121 	priv->last_lsr = 0;
122 	priv->last_msr = 0;
123 	/* see comments at top of file */
124 	priv->bad_flow_control =
125 		(le16_to_cpu(dev->descriptor.bcdDevice) <= 0x0206) ? 1 : 0;
126 	dev_info(&dev->dev, "bcdDevice: %04x, bfc: %d\n",
127 					le16_to_cpu(dev->descriptor.bcdDevice),
128 					priv->bad_flow_control);
129 
130 	usb_set_serial_port_data(port, priv);
131 
132 	return 0;
133 }
134 
135 static void belkin_sa_port_remove(struct usb_serial_port *port)
136 {
137 	struct belkin_sa_private *priv;
138 
139 	priv = usb_get_serial_port_data(port);
140 	kfree(priv);
141 }
142 
143 static int belkin_sa_open(struct tty_struct *tty,
144 					struct usb_serial_port *port)
145 {
146 	int retval;
147 
148 	retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
149 	if (retval) {
150 		dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
151 		return retval;
152 	}
153 
154 	retval = usb_serial_generic_open(tty, port);
155 	if (retval)
156 		usb_kill_urb(port->interrupt_in_urb);
157 
158 	return retval;
159 }
160 
161 static void belkin_sa_close(struct usb_serial_port *port)
162 {
163 	usb_serial_generic_close(port);
164 	usb_kill_urb(port->interrupt_in_urb);
165 }
166 
167 static void belkin_sa_read_int_callback(struct urb *urb)
168 {
169 	struct usb_serial_port *port = urb->context;
170 	struct belkin_sa_private *priv;
171 	unsigned char *data = urb->transfer_buffer;
172 	int retval;
173 	int status = urb->status;
174 	unsigned long flags;
175 
176 	switch (status) {
177 	case 0:
178 		/* success */
179 		break;
180 	case -ECONNRESET:
181 	case -ENOENT:
182 	case -ESHUTDOWN:
183 		/* this urb is terminated, clean up */
184 		dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
185 			__func__, status);
186 		return;
187 	default:
188 		dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
189 			__func__, status);
190 		goto exit;
191 	}
192 
193 	usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
194 
195 	if (urb->actual_length < BELKIN_SA_MSR_INDEX + 1)
196 		goto exit;
197 
198 	/* Handle known interrupt data */
199 	/* ignore data[0] and data[1] */
200 
201 	priv = usb_get_serial_port_data(port);
202 	spin_lock_irqsave(&priv->lock, flags);
203 	priv->last_msr = data[BELKIN_SA_MSR_INDEX];
204 
205 	/* Record Control Line states */
206 	if (priv->last_msr & BELKIN_SA_MSR_DSR)
207 		priv->control_state |= TIOCM_DSR;
208 	else
209 		priv->control_state &= ~TIOCM_DSR;
210 
211 	if (priv->last_msr & BELKIN_SA_MSR_CTS)
212 		priv->control_state |= TIOCM_CTS;
213 	else
214 		priv->control_state &= ~TIOCM_CTS;
215 
216 	if (priv->last_msr & BELKIN_SA_MSR_RI)
217 		priv->control_state |= TIOCM_RI;
218 	else
219 		priv->control_state &= ~TIOCM_RI;
220 
221 	if (priv->last_msr & BELKIN_SA_MSR_CD)
222 		priv->control_state |= TIOCM_CD;
223 	else
224 		priv->control_state &= ~TIOCM_CD;
225 
226 	priv->last_lsr = data[BELKIN_SA_LSR_INDEX];
227 	spin_unlock_irqrestore(&priv->lock, flags);
228 exit:
229 	retval = usb_submit_urb(urb, GFP_ATOMIC);
230 	if (retval)
231 		dev_err(&port->dev, "%s - usb_submit_urb failed with "
232 			"result %d\n", __func__, retval);
233 }
234 
235 static void belkin_sa_process_read_urb(struct urb *urb)
236 {
237 	struct usb_serial_port *port = urb->context;
238 	struct belkin_sa_private *priv = usb_get_serial_port_data(port);
239 	unsigned char *data = urb->transfer_buffer;
240 	unsigned long flags;
241 	unsigned char status;
242 	char tty_flag;
243 
244 	/* Update line status */
245 	tty_flag = TTY_NORMAL;
246 
247 	spin_lock_irqsave(&priv->lock, flags);
248 	status = priv->last_lsr;
249 	priv->last_lsr &= ~BELKIN_SA_LSR_ERR;
250 	spin_unlock_irqrestore(&priv->lock, flags);
251 
252 	if (!urb->actual_length)
253 		return;
254 
255 	if (status & BELKIN_SA_LSR_ERR) {
256 		/* Break takes precedence over parity, which takes precedence
257 		 * over framing errors. */
258 		if (status & BELKIN_SA_LSR_BI)
259 			tty_flag = TTY_BREAK;
260 		else if (status & BELKIN_SA_LSR_PE)
261 			tty_flag = TTY_PARITY;
262 		else if (status & BELKIN_SA_LSR_FE)
263 			tty_flag = TTY_FRAME;
264 		dev_dbg(&port->dev, "tty_flag = %d\n", tty_flag);
265 
266 		/* Overrun is special, not associated with a char. */
267 		if (status & BELKIN_SA_LSR_OE)
268 			tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
269 	}
270 
271 	tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
272 							urb->actual_length);
273 	tty_flip_buffer_push(&port->port);
274 }
275 
276 static void belkin_sa_set_termios(struct tty_struct *tty,
277 				  struct usb_serial_port *port,
278 				  const struct ktermios *old_termios)
279 {
280 	struct usb_serial *serial = port->serial;
281 	struct belkin_sa_private *priv = usb_get_serial_port_data(port);
282 	unsigned int iflag;
283 	unsigned int cflag;
284 	unsigned int old_iflag = 0;
285 	unsigned int old_cflag = 0;
286 	__u16 urb_value = 0; /* Will hold the new flags */
287 	unsigned long flags;
288 	unsigned long control_state;
289 	int bad_flow_control;
290 	speed_t baud;
291 	struct ktermios *termios = &tty->termios;
292 
293 	iflag = termios->c_iflag;
294 	cflag = termios->c_cflag;
295 
296 	termios->c_cflag &= ~CMSPAR;
297 
298 	/* get a local copy of the current port settings */
299 	spin_lock_irqsave(&priv->lock, flags);
300 	control_state = priv->control_state;
301 	bad_flow_control = priv->bad_flow_control;
302 	spin_unlock_irqrestore(&priv->lock, flags);
303 
304 	old_iflag = old_termios->c_iflag;
305 	old_cflag = old_termios->c_cflag;
306 
307 	/* Set the baud rate */
308 	if ((cflag & CBAUD) != (old_cflag & CBAUD)) {
309 		/* reassert DTR and (maybe) RTS on transition from B0 */
310 		if ((old_cflag & CBAUD) == B0) {
311 			control_state |= (TIOCM_DTR|TIOCM_RTS);
312 			if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, 1) < 0)
313 				dev_err(&port->dev, "Set DTR error\n");
314 			/* don't set RTS if using hardware flow control */
315 			if (!(old_cflag & CRTSCTS))
316 				if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST
317 								, 1) < 0)
318 					dev_err(&port->dev, "Set RTS error\n");
319 		}
320 	}
321 
322 	baud = tty_get_baud_rate(tty);
323 	if (baud) {
324 		urb_value = BELKIN_SA_BAUD(baud);
325 		/* Clip to maximum speed */
326 		if (urb_value == 0)
327 			urb_value = 1;
328 		/* Turn it back into a resulting real baud rate */
329 		baud = BELKIN_SA_BAUD(urb_value);
330 
331 		/* Report the actual baud rate back to the caller */
332 		tty_encode_baud_rate(tty, baud, baud);
333 		if (BSA_USB_CMD(BELKIN_SA_SET_BAUDRATE_REQUEST, urb_value) < 0)
334 			dev_err(&port->dev, "Set baudrate error\n");
335 	} else {
336 		/* Disable flow control */
337 		if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST,
338 						BELKIN_SA_FLOW_NONE) < 0)
339 			dev_err(&port->dev, "Disable flowcontrol error\n");
340 		/* Drop RTS and DTR */
341 		control_state &= ~(TIOCM_DTR | TIOCM_RTS);
342 		if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, 0) < 0)
343 			dev_err(&port->dev, "DTR LOW error\n");
344 		if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, 0) < 0)
345 			dev_err(&port->dev, "RTS LOW error\n");
346 	}
347 
348 	/* set the parity */
349 	if ((cflag ^ old_cflag) & (PARENB | PARODD)) {
350 		if (cflag & PARENB)
351 			urb_value = (cflag & PARODD) ?  BELKIN_SA_PARITY_ODD
352 						: BELKIN_SA_PARITY_EVEN;
353 		else
354 			urb_value = BELKIN_SA_PARITY_NONE;
355 		if (BSA_USB_CMD(BELKIN_SA_SET_PARITY_REQUEST, urb_value) < 0)
356 			dev_err(&port->dev, "Set parity error\n");
357 	}
358 
359 	/* set the number of data bits */
360 	if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
361 		urb_value = BELKIN_SA_DATA_BITS(tty_get_char_size(cflag));
362 		if (BSA_USB_CMD(BELKIN_SA_SET_DATA_BITS_REQUEST, urb_value) < 0)
363 			dev_err(&port->dev, "Set data bits error\n");
364 	}
365 
366 	/* set the number of stop bits */
367 	if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
368 		urb_value = (cflag & CSTOPB) ? BELKIN_SA_STOP_BITS(2)
369 						: BELKIN_SA_STOP_BITS(1);
370 		if (BSA_USB_CMD(BELKIN_SA_SET_STOP_BITS_REQUEST,
371 							urb_value) < 0)
372 			dev_err(&port->dev, "Set stop bits error\n");
373 	}
374 
375 	/* Set flow control */
376 	if (((iflag ^ old_iflag) & (IXOFF | IXON)) ||
377 		((cflag ^ old_cflag) & CRTSCTS)) {
378 		urb_value = 0;
379 		if ((iflag & IXOFF) || (iflag & IXON))
380 			urb_value |= (BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
381 		else
382 			urb_value &= ~(BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
383 
384 		if (cflag & CRTSCTS)
385 			urb_value |=  (BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
386 		else
387 			urb_value &= ~(BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
388 
389 		if (bad_flow_control)
390 			urb_value &= ~(BELKIN_SA_FLOW_IRTS);
391 
392 		if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST, urb_value) < 0)
393 			dev_err(&port->dev, "Set flow control error\n");
394 	}
395 
396 	/* save off the modified port settings */
397 	spin_lock_irqsave(&priv->lock, flags);
398 	priv->control_state = control_state;
399 	spin_unlock_irqrestore(&priv->lock, flags);
400 }
401 
402 static int belkin_sa_break_ctl(struct tty_struct *tty, int break_state)
403 {
404 	struct usb_serial_port *port = tty->driver_data;
405 	struct usb_serial *serial = port->serial;
406 	int ret;
407 
408 	ret = BSA_USB_CMD(BELKIN_SA_SET_BREAK_REQUEST, break_state ? 1 : 0);
409 	if (ret < 0) {
410 		dev_err(&port->dev, "Set break_ctl %d\n", break_state);
411 		return ret;
412 	}
413 
414 	return 0;
415 }
416 
417 static int belkin_sa_tiocmget(struct tty_struct *tty)
418 {
419 	struct usb_serial_port *port = tty->driver_data;
420 	struct belkin_sa_private *priv = usb_get_serial_port_data(port);
421 	unsigned long control_state;
422 	unsigned long flags;
423 
424 	spin_lock_irqsave(&priv->lock, flags);
425 	control_state = priv->control_state;
426 	spin_unlock_irqrestore(&priv->lock, flags);
427 
428 	return control_state;
429 }
430 
431 static int belkin_sa_tiocmset(struct tty_struct *tty,
432 			       unsigned int set, unsigned int clear)
433 {
434 	struct usb_serial_port *port = tty->driver_data;
435 	struct usb_serial *serial = port->serial;
436 	struct belkin_sa_private *priv = usb_get_serial_port_data(port);
437 	unsigned long control_state;
438 	unsigned long flags;
439 	int retval = 0;
440 
441 	spin_lock_irqsave(&priv->lock, flags);
442 	control_state = priv->control_state;
443 
444 	if (set & TIOCM_RTS)
445 		control_state |= TIOCM_RTS;
446 	if (set & TIOCM_DTR)
447 		control_state |= TIOCM_DTR;
448 	if (clear & TIOCM_RTS)
449 		control_state &= ~TIOCM_RTS;
450 	if (clear & TIOCM_DTR)
451 		control_state &= ~TIOCM_DTR;
452 
453 	priv->control_state = control_state;
454 	spin_unlock_irqrestore(&priv->lock, flags);
455 
456 	if ((set | clear) & TIOCM_RTS) {
457 		retval = BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST,
458 					!!(control_state & TIOCM_RTS));
459 		if (retval < 0) {
460 			dev_err(&port->dev, "Set RTS error %d\n", retval);
461 			goto exit;
462 		}
463 	}
464 
465 	if ((set | clear) & TIOCM_DTR) {
466 		retval = BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST,
467 					!!(control_state & TIOCM_DTR));
468 		if (retval < 0) {
469 			dev_err(&port->dev, "Set DTR error %d\n", retval);
470 			goto exit;
471 		}
472 	}
473 exit:
474 	return retval;
475 }
476 
477 module_usb_serial_driver(serial_drivers, id_table);
478 
479 MODULE_AUTHOR(DRIVER_AUTHOR);
480 MODULE_DESCRIPTION(DRIVER_DESC);
481 MODULE_LICENSE("GPL");
482