xref: /openbmc/linux/drivers/usb/serial/belkin_sa.c (revision 60b33c13)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Belkin USB Serial Adapter Driver
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 2000		William Greathouse (wgreathouse@smva.com)
51da177e4SLinus Torvalds  *  Copyright (C) 2000-2001 	Greg Kroah-Hartman (greg@kroah.com)
62afd8287SJohan Hovold  *  Copyright (C) 2010		Johan Hovold (jhovold@gmail.com)
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *  This program is largely derived from work by the linux-usb group
91da177e4SLinus Torvalds  *  and associated source files.  Please see the usb/serial files for
101da177e4SLinus Torvalds  *  individual credits and copyrights.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * 	This program is free software; you can redistribute it and/or modify
131da177e4SLinus Torvalds  * 	it under the terms of the GNU General Public License as published by
141da177e4SLinus Torvalds  * 	the Free Software Foundation; either version 2 of the License, or
151da177e4SLinus Torvalds  * 	(at your option) any later version.
161da177e4SLinus Torvalds  *
17b69c1499SAlan Cox  * See Documentation/usb/usb-serial.txt for more information on using this
18b69c1499SAlan Cox  * driver
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  * TODO:
211da177e4SLinus Torvalds  * -- Add true modem contol line query capability.  Currently we track the
221da177e4SLinus Torvalds  *    states reported by the interrupt and the states we request.
231da177e4SLinus Torvalds  * -- Add error reporting back to application for UART error conditions.
241da177e4SLinus Torvalds  *    Just point me at how to implement this and I'll do it. I've put the
251da177e4SLinus Torvalds  *    framework in, but haven't analyzed the "tty_flip" interface yet.
261da177e4SLinus Torvalds  * -- Add support for flush commands
271da177e4SLinus Torvalds  * -- Add everything that is missing :)
281da177e4SLinus Torvalds  *
291da177e4SLinus Torvalds  * 27-Nov-2001 gkh
301da177e4SLinus Torvalds  * 	compressed all the differnent device entries into 1.
311da177e4SLinus Torvalds  *
321da177e4SLinus Torvalds  * 30-May-2001 gkh
33b69c1499SAlan Cox  *	switched from using spinlock to a semaphore, which fixes lots of
34b69c1499SAlan Cox  *	problems.
351da177e4SLinus Torvalds  *
361da177e4SLinus Torvalds  * 08-Apr-2001 gb
371da177e4SLinus Torvalds  *	- Identify version on module load.
381da177e4SLinus Torvalds  *
391da177e4SLinus Torvalds  * 12-Mar-2001 gkh
401da177e4SLinus Torvalds  *	- Added support for the GoHubs GO-COM232 device which is the same as the
411da177e4SLinus Torvalds  *	  Peracom device.
421da177e4SLinus Torvalds  *
431da177e4SLinus Torvalds  * 06-Nov-2000 gkh
441da177e4SLinus Torvalds  *	- Added support for the old Belkin and Peracom devices.
451da177e4SLinus Torvalds  *	- Made the port able to be opened multiple times.
461da177e4SLinus Torvalds  *	- Added some defaults incase the line settings are things these devices
471da177e4SLinus Torvalds  *	  can't support.
481da177e4SLinus Torvalds  *
491da177e4SLinus Torvalds  * 18-Oct-2000 William Greathouse
501da177e4SLinus Torvalds  *    Released into the wild (linux-usb-devel)
511da177e4SLinus Torvalds  *
521da177e4SLinus Torvalds  * 17-Oct-2000 William Greathouse
531da177e4SLinus Torvalds  *    Add code to recognize firmware version and set hardware flow control
541da177e4SLinus Torvalds  *    appropriately.  Belkin states that firmware prior to 3.05 does not
551da177e4SLinus Torvalds  *    operate correctly in hardware handshake mode.  I have verified this
561da177e4SLinus Torvalds  *    on firmware 2.05 -- for both RTS and DTR input flow control, the control
571da177e4SLinus Torvalds  *    line is not reset.  The test performed by the Belkin Win* driver is
581da177e4SLinus Torvalds  *    to enable hardware flow control for firmware 2.06 or greater and
591da177e4SLinus Torvalds  *    for 1.00 or prior.  I am only enabling for 2.06 or greater.
601da177e4SLinus Torvalds  *
611da177e4SLinus Torvalds  * 12-Oct-2000 William Greathouse
621da177e4SLinus Torvalds  *    First cut at supporting Belkin USB Serial Adapter F5U103
631da177e4SLinus Torvalds  *    I did not have a copy of the original work to support this
641da177e4SLinus Torvalds  *    adapter, so pardon any stupid mistakes.  All of the information
651da177e4SLinus Torvalds  *    I am using to write this driver was acquired by using a modified
661da177e4SLinus Torvalds  *    UsbSnoop on Windows2000 and from examining the other USB drivers.
671da177e4SLinus Torvalds  */
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #include <linux/kernel.h>
701da177e4SLinus Torvalds #include <linux/errno.h>
711da177e4SLinus Torvalds #include <linux/init.h>
721da177e4SLinus Torvalds #include <linux/slab.h>
731da177e4SLinus Torvalds #include <linux/tty.h>
741da177e4SLinus Torvalds #include <linux/tty_driver.h>
751da177e4SLinus Torvalds #include <linux/tty_flip.h>
761da177e4SLinus Torvalds #include <linux/module.h>
771da177e4SLinus Torvalds #include <linux/spinlock.h>
78b69c1499SAlan Cox #include <linux/uaccess.h>
791da177e4SLinus Torvalds #include <linux/usb.h>
80a969888cSGreg Kroah-Hartman #include <linux/usb/serial.h>
811da177e4SLinus Torvalds #include "belkin_sa.h"
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds static int debug;
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds /*
861da177e4SLinus Torvalds  * Version Information
871da177e4SLinus Torvalds  */
882afd8287SJohan Hovold #define DRIVER_VERSION "v1.3"
891da177e4SLinus Torvalds #define DRIVER_AUTHOR "William Greathouse <wgreathouse@smva.com>"
901da177e4SLinus Torvalds #define DRIVER_DESC "USB Belkin Serial converter driver"
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds /* function prototypes for a Belkin USB Serial Adapter F5U103 */
931da177e4SLinus Torvalds static int  belkin_sa_startup(struct usb_serial *serial);
94f9c99bb8SAlan Stern static void belkin_sa_release(struct usb_serial *serial);
95b69c1499SAlan Cox static int  belkin_sa_open(struct tty_struct *tty,
96a509a7e4SAlan Cox 			struct usb_serial_port *port);
97335f8514SAlan Cox static void belkin_sa_close(struct usb_serial_port *port);
987d12e780SDavid Howells static void belkin_sa_read_int_callback(struct urb *urb);
992afd8287SJohan Hovold static void belkin_sa_process_read_urb(struct urb *urb);
100b69c1499SAlan Cox static void belkin_sa_set_termios(struct tty_struct *tty,
101b69c1499SAlan Cox 			struct usb_serial_port *port, struct ktermios * old);
10295da310eSAlan Cox static void belkin_sa_break_ctl(struct tty_struct *tty, int break_state);
10360b33c13SAlan Cox static int  belkin_sa_tiocmget(struct tty_struct *tty);
104b69c1499SAlan Cox static int  belkin_sa_tiocmset(struct tty_struct *tty, struct file *file,
105b69c1499SAlan Cox 					unsigned int set, unsigned int clear);
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 
1087d40d7e8SNémeth Márton static const struct usb_device_id id_table_combined[] = {
1091da177e4SLinus Torvalds 	{ USB_DEVICE(BELKIN_SA_VID, BELKIN_SA_PID) },
1101da177e4SLinus Torvalds 	{ USB_DEVICE(BELKIN_OLD_VID, BELKIN_OLD_PID) },
1111da177e4SLinus Torvalds 	{ USB_DEVICE(PERACOM_VID, PERACOM_PID) },
1121da177e4SLinus Torvalds 	{ USB_DEVICE(GOHUBS_VID, GOHUBS_PID) },
1131da177e4SLinus Torvalds 	{ USB_DEVICE(GOHUBS_VID, HANDYLINK_PID) },
1141da177e4SLinus Torvalds 	{ USB_DEVICE(BELKIN_DOCKSTATION_VID, BELKIN_DOCKSTATION_PID) },
1151da177e4SLinus Torvalds 	{ }	/* Terminating entry */
1161da177e4SLinus Torvalds };
1171da177e4SLinus Torvalds MODULE_DEVICE_TABLE(usb, id_table_combined);
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds static struct usb_driver belkin_driver = {
1201da177e4SLinus Torvalds 	.name =		"belkin",
1211da177e4SLinus Torvalds 	.probe =	usb_serial_probe,
1221da177e4SLinus Torvalds 	.disconnect =	usb_serial_disconnect,
1231da177e4SLinus Torvalds 	.id_table =	id_table_combined,
124ba9dc657SGreg Kroah-Hartman 	.no_dynamic_id =	1,
1251da177e4SLinus Torvalds };
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds /* All of the device info needed for the serial converters */
128ea65370dSGreg Kroah-Hartman static struct usb_serial_driver belkin_device = {
12918fcac35SGreg Kroah-Hartman 	.driver = {
1301da177e4SLinus Torvalds 		.owner =	THIS_MODULE,
131269bda1cSGreg Kroah-Hartman 		.name =		"belkin",
13218fcac35SGreg Kroah-Hartman 	},
133269bda1cSGreg Kroah-Hartman 	.description =		"Belkin / Peracom / GoHubs USB Serial Adapter",
134d9b1b787SJohannes Hölzl 	.usb_driver =		&belkin_driver,
1351da177e4SLinus Torvalds 	.id_table =		id_table_combined,
1361da177e4SLinus Torvalds 	.num_ports =		1,
1371da177e4SLinus Torvalds 	.open =			belkin_sa_open,
1381da177e4SLinus Torvalds 	.close =		belkin_sa_close,
139b69c1499SAlan Cox 	.read_int_callback =	belkin_sa_read_int_callback,
1402afd8287SJohan Hovold 	.process_read_urb =	belkin_sa_process_read_urb,
1411da177e4SLinus Torvalds 	.set_termios =		belkin_sa_set_termios,
1421da177e4SLinus Torvalds 	.break_ctl =		belkin_sa_break_ctl,
1431da177e4SLinus Torvalds 	.tiocmget =		belkin_sa_tiocmget,
1441da177e4SLinus Torvalds 	.tiocmset =		belkin_sa_tiocmset,
1451da177e4SLinus Torvalds 	.attach =		belkin_sa_startup,
146f9c99bb8SAlan Stern 	.release =		belkin_sa_release,
1471da177e4SLinus Torvalds };
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds struct belkin_sa_private {
1501da177e4SLinus Torvalds 	spinlock_t		lock;
1511da177e4SLinus Torvalds 	unsigned long		control_state;
1521da177e4SLinus Torvalds 	unsigned char		last_lsr;
1531da177e4SLinus Torvalds 	unsigned char		last_msr;
1541da177e4SLinus Torvalds 	int			bad_flow_control;
1551da177e4SLinus Torvalds };
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds /*
1591da177e4SLinus Torvalds  * ***************************************************************************
1601da177e4SLinus Torvalds  * Belkin USB Serial Adapter F5U103 specific driver functions
1611da177e4SLinus Torvalds  * ***************************************************************************
1621da177e4SLinus Torvalds  */
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds #define WDR_TIMEOUT 5000 /* default urb timeout */
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds /* assumes that struct usb_serial *serial is available */
1671da177e4SLinus Torvalds #define BSA_USB_CMD(c, v) usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), \
1681da177e4SLinus Torvalds 					    (c), BELKIN_SA_SET_REQUEST_TYPE, \
1691da177e4SLinus Torvalds 					    (v), 0, NULL, 0, WDR_TIMEOUT)
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds /* do some startup allocations not currently performed by usb_serial_probe() */
1721da177e4SLinus Torvalds static int belkin_sa_startup(struct usb_serial *serial)
1731da177e4SLinus Torvalds {
1741da177e4SLinus Torvalds 	struct usb_device *dev = serial->dev;
1751da177e4SLinus Torvalds 	struct belkin_sa_private *priv;
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	/* allocate the private data structure */
1781da177e4SLinus Torvalds 	priv = kmalloc(sizeof(struct belkin_sa_private), GFP_KERNEL);
1791da177e4SLinus Torvalds 	if (!priv)
180b69c1499SAlan Cox 		return -1; /* error */
1811da177e4SLinus Torvalds 	/* set initial values for control structures */
1821da177e4SLinus Torvalds 	spin_lock_init(&priv->lock);
1831da177e4SLinus Torvalds 	priv->control_state = 0;
1841da177e4SLinus Torvalds 	priv->last_lsr = 0;
1851da177e4SLinus Torvalds 	priv->last_msr = 0;
1861da177e4SLinus Torvalds 	/* see comments at top of file */
187b69c1499SAlan Cox 	priv->bad_flow_control =
188b69c1499SAlan Cox 		(le16_to_cpu(dev->descriptor.bcdDevice) <= 0x0206) ? 1 : 0;
189c197a8dbSGreg Kroah-Hartman 	dev_info(&dev->dev, "bcdDevice: %04x, bfc: %d\n",
190b69c1499SAlan Cox 					le16_to_cpu(dev->descriptor.bcdDevice),
191b69c1499SAlan Cox 					priv->bad_flow_control);
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	init_waitqueue_head(&serial->port[0]->write_wait);
1941da177e4SLinus Torvalds 	usb_set_serial_port_data(serial->port[0], priv);
1951da177e4SLinus Torvalds 
196b69c1499SAlan Cox 	return 0;
1971da177e4SLinus Torvalds }
1981da177e4SLinus Torvalds 
199f9c99bb8SAlan Stern static void belkin_sa_release(struct usb_serial *serial)
2001da177e4SLinus Torvalds {
2011da177e4SLinus Torvalds 	int i;
2021da177e4SLinus Torvalds 
203441b62c1SHarvey Harrison 	dbg("%s", __func__);
2041da177e4SLinus Torvalds 
205726ef428SJohan Hovold 	for (i = 0; i < serial->num_ports; ++i)
206726ef428SJohan Hovold 		kfree(usb_get_serial_port_data(serial->port[i]));
2071da177e4SLinus Torvalds }
2081da177e4SLinus Torvalds 
209b69c1499SAlan Cox static int belkin_sa_open(struct tty_struct *tty,
210a509a7e4SAlan Cox 					struct usb_serial_port *port)
2111da177e4SLinus Torvalds {
212f2f8b7feSJohan Hovold 	int retval;
2131da177e4SLinus Torvalds 
214441b62c1SHarvey Harrison 	dbg("%s port %d", __func__, port->number);
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
2171da177e4SLinus Torvalds 	if (retval) {
218194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
219f2f8b7feSJohan Hovold 		return retval;
2201da177e4SLinus Torvalds 	}
2211da177e4SLinus Torvalds 
222f2f8b7feSJohan Hovold 	retval = usb_serial_generic_open(tty, port);
223f2f8b7feSJohan Hovold 	if (retval)
224f2f8b7feSJohan Hovold 		usb_kill_urb(port->interrupt_in_urb);
225f2f8b7feSJohan Hovold 
2261da177e4SLinus Torvalds 	return retval;
227726ef428SJohan Hovold }
2281da177e4SLinus Torvalds 
229335f8514SAlan Cox static void belkin_sa_close(struct usb_serial_port *port)
2301da177e4SLinus Torvalds {
231441b62c1SHarvey Harrison 	dbg("%s port %d", __func__, port->number);
2321da177e4SLinus Torvalds 
233f26788daSJohan Hovold 	usb_serial_generic_close(port);
2341da177e4SLinus Torvalds 	usb_kill_urb(port->interrupt_in_urb);
235726ef428SJohan Hovold }
2361da177e4SLinus Torvalds 
2377d12e780SDavid Howells static void belkin_sa_read_int_callback(struct urb *urb)
2381da177e4SLinus Torvalds {
239cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
2401da177e4SLinus Torvalds 	struct belkin_sa_private *priv;
2411da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
2421da177e4SLinus Torvalds 	int retval;
243f26aad25SGreg Kroah-Hartman 	int status = urb->status;
2441da177e4SLinus Torvalds 	unsigned long flags;
2451da177e4SLinus Torvalds 
246f26aad25SGreg Kroah-Hartman 	switch (status) {
2471da177e4SLinus Torvalds 	case 0:
2481da177e4SLinus Torvalds 		/* success */
2491da177e4SLinus Torvalds 		break;
2501da177e4SLinus Torvalds 	case -ECONNRESET:
2511da177e4SLinus Torvalds 	case -ENOENT:
2521da177e4SLinus Torvalds 	case -ESHUTDOWN:
2531da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
254f26aad25SGreg Kroah-Hartman 		dbg("%s - urb shutting down with status: %d",
255441b62c1SHarvey Harrison 		    __func__, status);
2561da177e4SLinus Torvalds 		return;
2571da177e4SLinus Torvalds 	default:
258f26aad25SGreg Kroah-Hartman 		dbg("%s - nonzero urb status received: %d",
259441b62c1SHarvey Harrison 		    __func__, status);
2601da177e4SLinus Torvalds 		goto exit;
2611da177e4SLinus Torvalds 	}
2621da177e4SLinus Torvalds 
263b69c1499SAlan Cox 	usb_serial_debug_data(debug, &port->dev, __func__,
264b69c1499SAlan Cox 					urb->actual_length, data);
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	/* Handle known interrupt data */
2671da177e4SLinus Torvalds 	/* ignore data[0] and data[1] */
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	priv = usb_get_serial_port_data(port);
2701da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
2711da177e4SLinus Torvalds 	priv->last_msr = data[BELKIN_SA_MSR_INDEX];
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	/* Record Control Line states */
2741da177e4SLinus Torvalds 	if (priv->last_msr & BELKIN_SA_MSR_DSR)
2751da177e4SLinus Torvalds 		priv->control_state |= TIOCM_DSR;
2761da177e4SLinus Torvalds 	else
2771da177e4SLinus Torvalds 		priv->control_state &= ~TIOCM_DSR;
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	if (priv->last_msr & BELKIN_SA_MSR_CTS)
2801da177e4SLinus Torvalds 		priv->control_state |= TIOCM_CTS;
2811da177e4SLinus Torvalds 	else
2821da177e4SLinus Torvalds 		priv->control_state &= ~TIOCM_CTS;
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	if (priv->last_msr & BELKIN_SA_MSR_RI)
2851da177e4SLinus Torvalds 		priv->control_state |= TIOCM_RI;
2861da177e4SLinus Torvalds 	else
2871da177e4SLinus Torvalds 		priv->control_state &= ~TIOCM_RI;
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds 	if (priv->last_msr & BELKIN_SA_MSR_CD)
2901da177e4SLinus Torvalds 		priv->control_state |= TIOCM_CD;
2911da177e4SLinus Torvalds 	else
2921da177e4SLinus Torvalds 		priv->control_state &= ~TIOCM_CD;
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	priv->last_lsr = data[BELKIN_SA_LSR_INDEX];
2951da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
2961da177e4SLinus Torvalds exit:
2971da177e4SLinus Torvalds 	retval = usb_submit_urb(urb, GFP_ATOMIC);
2981da177e4SLinus Torvalds 	if (retval)
299194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "%s - usb_submit_urb failed with "
300194343d9SGreg Kroah-Hartman 			"result %d\n", __func__, retval);
3011da177e4SLinus Torvalds }
3021da177e4SLinus Torvalds 
3032afd8287SJohan Hovold static void belkin_sa_process_read_urb(struct urb *urb)
3042afd8287SJohan Hovold {
3052afd8287SJohan Hovold 	struct usb_serial_port *port = urb->context;
3062afd8287SJohan Hovold 	struct belkin_sa_private *priv = usb_get_serial_port_data(port);
3072afd8287SJohan Hovold 	struct tty_struct *tty;
3082afd8287SJohan Hovold 	unsigned char *data = urb->transfer_buffer;
3092afd8287SJohan Hovold 	unsigned long flags;
3102afd8287SJohan Hovold 	unsigned char status;
3112afd8287SJohan Hovold 	char tty_flag;
3122afd8287SJohan Hovold 
3132afd8287SJohan Hovold 	/* Update line status */
3142afd8287SJohan Hovold 	tty_flag = TTY_NORMAL;
3152afd8287SJohan Hovold 
3162afd8287SJohan Hovold 	spin_lock_irqsave(&priv->lock, flags);
3172afd8287SJohan Hovold 	status = priv->last_lsr;
3182afd8287SJohan Hovold 	priv->last_lsr &= ~BELKIN_SA_LSR_ERR;
3192afd8287SJohan Hovold 	spin_unlock_irqrestore(&priv->lock, flags);
3202afd8287SJohan Hovold 
3212afd8287SJohan Hovold 	if (!urb->actual_length)
3222afd8287SJohan Hovold 		return;
3232afd8287SJohan Hovold 
3242afd8287SJohan Hovold 	tty = tty_port_tty_get(&port->port);
3252afd8287SJohan Hovold 	if (!tty)
3262afd8287SJohan Hovold 		return;
3272afd8287SJohan Hovold 
3282afd8287SJohan Hovold 	if (status & BELKIN_SA_LSR_ERR) {
3292afd8287SJohan Hovold 		/* Break takes precedence over parity, which takes precedence
3302afd8287SJohan Hovold 		 * over framing errors. */
3312afd8287SJohan Hovold 		if (status & BELKIN_SA_LSR_BI)
3322afd8287SJohan Hovold 			tty_flag = TTY_BREAK;
3332afd8287SJohan Hovold 		else if (status & BELKIN_SA_LSR_PE)
3342afd8287SJohan Hovold 			tty_flag = TTY_PARITY;
3352afd8287SJohan Hovold 		else if (status & BELKIN_SA_LSR_FE)
3362afd8287SJohan Hovold 			tty_flag = TTY_FRAME;
3372afd8287SJohan Hovold 		dev_dbg(&port->dev, "tty_flag = %d\n", tty_flag);
3382afd8287SJohan Hovold 
3392afd8287SJohan Hovold 		/* Overrun is special, not associated with a char. */
3402afd8287SJohan Hovold 		if (status & BELKIN_SA_LSR_OE)
3412afd8287SJohan Hovold 			tty_insert_flip_char(tty, 0, TTY_OVERRUN);
3422afd8287SJohan Hovold 	}
3432afd8287SJohan Hovold 
3442afd8287SJohan Hovold 	tty_insert_flip_string_fixed_flag(tty, data, tty_flag,
3452afd8287SJohan Hovold 							urb->actual_length);
3462afd8287SJohan Hovold 	tty_flip_buffer_push(tty);
3472afd8287SJohan Hovold 	tty_kref_put(tty);
3482afd8287SJohan Hovold }
3492afd8287SJohan Hovold 
35095da310eSAlan Cox static void belkin_sa_set_termios(struct tty_struct *tty,
35195da310eSAlan Cox 		struct usb_serial_port *port, struct ktermios *old_termios)
3521da177e4SLinus Torvalds {
3531da177e4SLinus Torvalds 	struct usb_serial *serial = port->serial;
3541da177e4SLinus Torvalds 	struct belkin_sa_private *priv = usb_get_serial_port_data(port);
3551da177e4SLinus Torvalds 	unsigned int iflag;
3561da177e4SLinus Torvalds 	unsigned int cflag;
3571da177e4SLinus Torvalds 	unsigned int old_iflag = 0;
3581da177e4SLinus Torvalds 	unsigned int old_cflag = 0;
3591da177e4SLinus Torvalds 	__u16 urb_value = 0; /* Will hold the new flags */
3601da177e4SLinus Torvalds 	unsigned long flags;
3611da177e4SLinus Torvalds 	unsigned long control_state;
3621da177e4SLinus Torvalds 	int bad_flow_control;
3639a8baec7SAlan Cox 	speed_t baud;
36495da310eSAlan Cox 	struct ktermios *termios = tty->termios;
3651da177e4SLinus Torvalds 
3663ec466b4SAlan Cox 	iflag = termios->c_iflag;
3673ec466b4SAlan Cox 	cflag = termios->c_cflag;
3681da177e4SLinus Torvalds 
3693ec466b4SAlan Cox 	termios->c_cflag &= ~CMSPAR;
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 	/* get a local copy of the current port settings */
3721da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
3731da177e4SLinus Torvalds 	control_state = priv->control_state;
3741da177e4SLinus Torvalds 	bad_flow_control = priv->bad_flow_control;
3751da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 	old_iflag = old_termios->c_iflag;
3781da177e4SLinus Torvalds 	old_cflag = old_termios->c_cflag;
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	/* Set the baud rate */
3811da177e4SLinus Torvalds 	if ((cflag & CBAUD) != (old_cflag & CBAUD)) {
3821da177e4SLinus Torvalds 		/* reassert DTR and (maybe) RTS on transition from B0 */
3831da177e4SLinus Torvalds 		if ((old_cflag & CBAUD) == B0) {
3841da177e4SLinus Torvalds 			control_state |= (TIOCM_DTR|TIOCM_RTS);
3851da177e4SLinus Torvalds 			if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, 1) < 0)
386194343d9SGreg Kroah-Hartman 				dev_err(&port->dev, "Set DTR error\n");
3871da177e4SLinus Torvalds 			/* don't set RTS if using hardware flow control */
3881da177e4SLinus Torvalds 			if (!(old_cflag & CRTSCTS))
389b69c1499SAlan Cox 				if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST
390b69c1499SAlan Cox 								, 1) < 0)
391194343d9SGreg Kroah-Hartman 					dev_err(&port->dev, "Set RTS error\n");
3921da177e4SLinus Torvalds 		}
3931da177e4SLinus Torvalds 	}
3949a8baec7SAlan Cox 
39595da310eSAlan Cox 	baud = tty_get_baud_rate(tty);
3963ec466b4SAlan Cox 	if (baud) {
3979a8baec7SAlan Cox 		urb_value = BELKIN_SA_BAUD(baud);
3989a8baec7SAlan Cox 		/* Clip to maximum speed */
3999a8baec7SAlan Cox 		if (urb_value == 0)
4009a8baec7SAlan Cox 			urb_value = 1;
4019a8baec7SAlan Cox 		/* Turn it back into a resulting real baud rate */
4029a8baec7SAlan Cox 		baud = BELKIN_SA_BAUD(urb_value);
4039a8baec7SAlan Cox 
4043ec466b4SAlan Cox 		/* Report the actual baud rate back to the caller */
40595da310eSAlan Cox 		tty_encode_baud_rate(tty, baud, baud);
4061da177e4SLinus Torvalds 		if (BSA_USB_CMD(BELKIN_SA_SET_BAUDRATE_REQUEST, urb_value) < 0)
407194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "Set baudrate error\n");
4081da177e4SLinus Torvalds 	} else {
4091da177e4SLinus Torvalds 		/* Disable flow control */
410b69c1499SAlan Cox 		if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST,
411b69c1499SAlan Cox 						BELKIN_SA_FLOW_NONE) < 0)
412194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "Disable flowcontrol error\n");
4131da177e4SLinus Torvalds 		/* Drop RTS and DTR */
4141da177e4SLinus Torvalds 		control_state &= ~(TIOCM_DTR | TIOCM_RTS);
4151da177e4SLinus Torvalds 		if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, 0) < 0)
416194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "DTR LOW error\n");
4171da177e4SLinus Torvalds 		if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, 0) < 0)
418194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "RTS LOW error\n");
4191da177e4SLinus Torvalds 	}
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	/* set the parity */
422b69c1499SAlan Cox 	if ((cflag ^ old_cflag) & (PARENB | PARODD)) {
4231da177e4SLinus Torvalds 		if (cflag & PARENB)
424b69c1499SAlan Cox 			urb_value = (cflag & PARODD) ?  BELKIN_SA_PARITY_ODD
425b69c1499SAlan Cox 						: BELKIN_SA_PARITY_EVEN;
4261da177e4SLinus Torvalds 		else
4271da177e4SLinus Torvalds 			urb_value = BELKIN_SA_PARITY_NONE;
4281da177e4SLinus Torvalds 		if (BSA_USB_CMD(BELKIN_SA_SET_PARITY_REQUEST, urb_value) < 0)
429194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "Set parity error\n");
4301da177e4SLinus Torvalds 	}
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	/* set the number of data bits */
4331da177e4SLinus Torvalds 	if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
4341da177e4SLinus Torvalds 		switch (cflag & CSIZE) {
435b69c1499SAlan Cox 		case CS5:
436b69c1499SAlan Cox 			urb_value = BELKIN_SA_DATA_BITS(5);
437b69c1499SAlan Cox 			break;
438b69c1499SAlan Cox 		case CS6:
439b69c1499SAlan Cox 			urb_value = BELKIN_SA_DATA_BITS(6);
440b69c1499SAlan Cox 			break;
441b69c1499SAlan Cox 		case CS7:
442b69c1499SAlan Cox 			urb_value = BELKIN_SA_DATA_BITS(7);
443b69c1499SAlan Cox 			break;
444b69c1499SAlan Cox 		case CS8:
445b69c1499SAlan Cox 			urb_value = BELKIN_SA_DATA_BITS(8);
446b69c1499SAlan Cox 			break;
4479a8baec7SAlan Cox 		default: dbg("CSIZE was not CS5-CS8, using default of 8");
4481da177e4SLinus Torvalds 			urb_value = BELKIN_SA_DATA_BITS(8);
4491da177e4SLinus Torvalds 			break;
4501da177e4SLinus Torvalds 		}
4511da177e4SLinus Torvalds 		if (BSA_USB_CMD(BELKIN_SA_SET_DATA_BITS_REQUEST, urb_value) < 0)
452194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "Set data bits error\n");
4531da177e4SLinus Torvalds 	}
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 	/* set the number of stop bits */
4561da177e4SLinus Torvalds 	if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
457b69c1499SAlan Cox 		urb_value = (cflag & CSTOPB) ? BELKIN_SA_STOP_BITS(2)
458b69c1499SAlan Cox 						: BELKIN_SA_STOP_BITS(1);
459b69c1499SAlan Cox 		if (BSA_USB_CMD(BELKIN_SA_SET_STOP_BITS_REQUEST,
460b69c1499SAlan Cox 							urb_value) < 0)
461194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "Set stop bits error\n");
4621da177e4SLinus Torvalds 	}
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	/* Set flow control */
465b69c1499SAlan Cox 	if (((iflag ^ old_iflag) & (IXOFF | IXON)) ||
466b69c1499SAlan Cox 		((cflag ^ old_cflag) & CRTSCTS)) {
4671da177e4SLinus Torvalds 		urb_value = 0;
4681da177e4SLinus Torvalds 		if ((iflag & IXOFF) || (iflag & IXON))
4691da177e4SLinus Torvalds 			urb_value |= (BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
4701da177e4SLinus Torvalds 		else
4711da177e4SLinus Torvalds 			urb_value &= ~(BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 		if (cflag & CRTSCTS)
4741da177e4SLinus Torvalds 			urb_value |=  (BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
4751da177e4SLinus Torvalds 		else
4761da177e4SLinus Torvalds 			urb_value &= ~(BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 		if (bad_flow_control)
4791da177e4SLinus Torvalds 			urb_value &= ~(BELKIN_SA_FLOW_IRTS);
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 		if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST, urb_value) < 0)
482194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "Set flow control error\n");
4831da177e4SLinus Torvalds 	}
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	/* save off the modified port settings */
4861da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
4871da177e4SLinus Torvalds 	priv->control_state = control_state;
4881da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
489726ef428SJohan Hovold }
4901da177e4SLinus Torvalds 
49195da310eSAlan Cox static void belkin_sa_break_ctl(struct tty_struct *tty, int break_state)
4921da177e4SLinus Torvalds {
49395da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
4941da177e4SLinus Torvalds 	struct usb_serial *serial = port->serial;
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	if (BSA_USB_CMD(BELKIN_SA_SET_BREAK_REQUEST, break_state ? 1 : 0) < 0)
497194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "Set break_ctl %d\n", break_state);
4981da177e4SLinus Torvalds }
4991da177e4SLinus Torvalds 
50060b33c13SAlan Cox static int belkin_sa_tiocmget(struct tty_struct *tty)
5011da177e4SLinus Torvalds {
50295da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
5031da177e4SLinus Torvalds 	struct belkin_sa_private *priv = usb_get_serial_port_data(port);
5041da177e4SLinus Torvalds 	unsigned long control_state;
5051da177e4SLinus Torvalds 	unsigned long flags;
5061da177e4SLinus Torvalds 
507441b62c1SHarvey Harrison 	dbg("%s", __func__);
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
5101da177e4SLinus Torvalds 	control_state = priv->control_state;
5111da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 	return control_state;
5141da177e4SLinus Torvalds }
5151da177e4SLinus Torvalds 
51695da310eSAlan Cox static int belkin_sa_tiocmset(struct tty_struct *tty, struct file *file,
5171da177e4SLinus Torvalds 			       unsigned int set, unsigned int clear)
5181da177e4SLinus Torvalds {
51995da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
5201da177e4SLinus Torvalds 	struct usb_serial *serial = port->serial;
5211da177e4SLinus Torvalds 	struct belkin_sa_private *priv = usb_get_serial_port_data(port);
5221da177e4SLinus Torvalds 	unsigned long control_state;
5231da177e4SLinus Torvalds 	unsigned long flags;
5241da177e4SLinus Torvalds 	int retval;
5251da177e4SLinus Torvalds 	int rts = 0;
5261da177e4SLinus Torvalds 	int dtr = 0;
5271da177e4SLinus Torvalds 
528441b62c1SHarvey Harrison 	dbg("%s", __func__);
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
5311da177e4SLinus Torvalds 	control_state = priv->control_state;
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 	if (set & TIOCM_RTS) {
5341da177e4SLinus Torvalds 		control_state |= TIOCM_RTS;
5351da177e4SLinus Torvalds 		rts = 1;
5361da177e4SLinus Torvalds 	}
5371da177e4SLinus Torvalds 	if (set & TIOCM_DTR) {
5381da177e4SLinus Torvalds 		control_state |= TIOCM_DTR;
5391da177e4SLinus Torvalds 		dtr = 1;
5401da177e4SLinus Torvalds 	}
5411da177e4SLinus Torvalds 	if (clear & TIOCM_RTS) {
5421da177e4SLinus Torvalds 		control_state &= ~TIOCM_RTS;
5431da177e4SLinus Torvalds 		rts = 0;
5441da177e4SLinus Torvalds 	}
5451da177e4SLinus Torvalds 	if (clear & TIOCM_DTR) {
5461da177e4SLinus Torvalds 		control_state &= ~TIOCM_DTR;
5471da177e4SLinus Torvalds 		dtr = 0;
5481da177e4SLinus Torvalds 	}
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds 	priv->control_state = control_state;
5511da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	retval = BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, rts);
5541da177e4SLinus Torvalds 	if (retval < 0) {
555194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "Set RTS error %d\n", retval);
5561da177e4SLinus Torvalds 		goto exit;
5571da177e4SLinus Torvalds 	}
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	retval = BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, dtr);
5601da177e4SLinus Torvalds 	if (retval < 0) {
561194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "Set DTR error %d\n", retval);
5621da177e4SLinus Torvalds 		goto exit;
5631da177e4SLinus Torvalds 	}
5641da177e4SLinus Torvalds exit:
5651da177e4SLinus Torvalds 	return retval;
5661da177e4SLinus Torvalds }
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds static int __init belkin_sa_init(void)
5701da177e4SLinus Torvalds {
5711da177e4SLinus Torvalds 	int retval;
5721da177e4SLinus Torvalds 	retval = usb_serial_register(&belkin_device);
5731da177e4SLinus Torvalds 	if (retval)
5741da177e4SLinus Torvalds 		goto failed_usb_serial_register;
5751da177e4SLinus Torvalds 	retval = usb_register(&belkin_driver);
5761da177e4SLinus Torvalds 	if (retval)
5771da177e4SLinus Torvalds 		goto failed_usb_register;
578c197a8dbSGreg Kroah-Hartman 	printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
579c197a8dbSGreg Kroah-Hartman 	       DRIVER_DESC "\n");
5801da177e4SLinus Torvalds 	return 0;
5811da177e4SLinus Torvalds failed_usb_register:
5821da177e4SLinus Torvalds 	usb_serial_deregister(&belkin_device);
5831da177e4SLinus Torvalds failed_usb_serial_register:
5841da177e4SLinus Torvalds 	return retval;
5851da177e4SLinus Torvalds }
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds static void __exit belkin_sa_exit (void)
5881da177e4SLinus Torvalds {
5891da177e4SLinus Torvalds 	usb_deregister(&belkin_driver);
5901da177e4SLinus Torvalds 	usb_serial_deregister(&belkin_device);
5911da177e4SLinus Torvalds }
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds module_init(belkin_sa_init);
5951da177e4SLinus Torvalds module_exit(belkin_sa_exit);
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR);
5981da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
5991da177e4SLinus Torvalds MODULE_VERSION(DRIVER_VERSION);
6001da177e4SLinus Torvalds MODULE_LICENSE("GPL");
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds module_param(debug, bool, S_IRUGO | S_IWUSR);
6031da177e4SLinus Torvalds MODULE_PARM_DESC(debug, "Debug enabled or not");
604