xref: /linux/drivers/usb/serial/belkin_sa.c (revision ea65370d025f5005649e5cb37c4d025e92c6fc38)
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)
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  This program is largely derived from work by the linux-usb group
81da177e4SLinus Torvalds  *  and associated source files.  Please see the usb/serial files for
91da177e4SLinus Torvalds  *  individual credits and copyrights.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * 	This program is free software; you can redistribute it and/or modify
121da177e4SLinus Torvalds  * 	it under the terms of the GNU General Public License as published by
131da177e4SLinus Torvalds  * 	the Free Software Foundation; either version 2 of the License, or
141da177e4SLinus Torvalds  * 	(at your option) any later version.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  * See Documentation/usb/usb-serial.txt for more information on using this driver
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  * TODO:
191da177e4SLinus Torvalds  * -- Add true modem contol line query capability.  Currently we track the
201da177e4SLinus Torvalds  *    states reported by the interrupt and the states we request.
211da177e4SLinus Torvalds  * -- Add error reporting back to application for UART error conditions.
221da177e4SLinus Torvalds  *    Just point me at how to implement this and I'll do it. I've put the
231da177e4SLinus Torvalds  *    framework in, but haven't analyzed the "tty_flip" interface yet.
241da177e4SLinus Torvalds  * -- Add support for flush commands
251da177e4SLinus Torvalds  * -- Add everything that is missing :)
261da177e4SLinus Torvalds  *
271da177e4SLinus Torvalds  * 27-Nov-2001 gkh
281da177e4SLinus Torvalds  * 	compressed all the differnent device entries into 1.
291da177e4SLinus Torvalds  *
301da177e4SLinus Torvalds  * 30-May-2001 gkh
311da177e4SLinus Torvalds  *	switched from using spinlock to a semaphore, which fixes lots of problems.
321da177e4SLinus Torvalds  *
331da177e4SLinus Torvalds  * 08-Apr-2001 gb
341da177e4SLinus Torvalds  *	- Identify version on module load.
351da177e4SLinus Torvalds  *
361da177e4SLinus Torvalds  * 12-Mar-2001 gkh
371da177e4SLinus Torvalds  *	- Added support for the GoHubs GO-COM232 device which is the same as the
381da177e4SLinus Torvalds  *	  Peracom device.
391da177e4SLinus Torvalds  *
401da177e4SLinus Torvalds  * 06-Nov-2000 gkh
411da177e4SLinus Torvalds  *	- Added support for the old Belkin and Peracom devices.
421da177e4SLinus Torvalds  *	- Made the port able to be opened multiple times.
431da177e4SLinus Torvalds  *	- Added some defaults incase the line settings are things these devices
441da177e4SLinus Torvalds  *	  can't support.
451da177e4SLinus Torvalds  *
461da177e4SLinus Torvalds  * 18-Oct-2000 William Greathouse
471da177e4SLinus Torvalds  *    Released into the wild (linux-usb-devel)
481da177e4SLinus Torvalds  *
491da177e4SLinus Torvalds  * 17-Oct-2000 William Greathouse
501da177e4SLinus Torvalds  *    Add code to recognize firmware version and set hardware flow control
511da177e4SLinus Torvalds  *    appropriately.  Belkin states that firmware prior to 3.05 does not
521da177e4SLinus Torvalds  *    operate correctly in hardware handshake mode.  I have verified this
531da177e4SLinus Torvalds  *    on firmware 2.05 -- for both RTS and DTR input flow control, the control
541da177e4SLinus Torvalds  *    line is not reset.  The test performed by the Belkin Win* driver is
551da177e4SLinus Torvalds  *    to enable hardware flow control for firmware 2.06 or greater and
561da177e4SLinus Torvalds  *    for 1.00 or prior.  I am only enabling for 2.06 or greater.
571da177e4SLinus Torvalds  *
581da177e4SLinus Torvalds  * 12-Oct-2000 William Greathouse
591da177e4SLinus Torvalds  *    First cut at supporting Belkin USB Serial Adapter F5U103
601da177e4SLinus Torvalds  *    I did not have a copy of the original work to support this
611da177e4SLinus Torvalds  *    adapter, so pardon any stupid mistakes.  All of the information
621da177e4SLinus Torvalds  *    I am using to write this driver was acquired by using a modified
631da177e4SLinus Torvalds  *    UsbSnoop on Windows2000 and from examining the other USB drivers.
641da177e4SLinus Torvalds  */
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds #include <linux/config.h>
671da177e4SLinus Torvalds #include <linux/kernel.h>
681da177e4SLinus Torvalds #include <linux/errno.h>
691da177e4SLinus Torvalds #include <linux/init.h>
701da177e4SLinus Torvalds #include <linux/slab.h>
711da177e4SLinus Torvalds #include <linux/tty.h>
721da177e4SLinus Torvalds #include <linux/tty_driver.h>
731da177e4SLinus Torvalds #include <linux/tty_flip.h>
741da177e4SLinus Torvalds #include <linux/module.h>
751da177e4SLinus Torvalds #include <linux/spinlock.h>
761da177e4SLinus Torvalds #include <asm/uaccess.h>
771da177e4SLinus Torvalds #include <linux/usb.h>
781da177e4SLinus Torvalds #include "usb-serial.h"
791da177e4SLinus Torvalds #include "belkin_sa.h"
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds static int debug;
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds /*
841da177e4SLinus Torvalds  * Version Information
851da177e4SLinus Torvalds  */
861da177e4SLinus Torvalds #define DRIVER_VERSION "v1.2"
871da177e4SLinus Torvalds #define DRIVER_AUTHOR "William Greathouse <wgreathouse@smva.com>"
881da177e4SLinus Torvalds #define DRIVER_DESC "USB Belkin Serial converter driver"
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds /* function prototypes for a Belkin USB Serial Adapter F5U103 */
911da177e4SLinus Torvalds static int  belkin_sa_startup		(struct usb_serial *serial);
921da177e4SLinus Torvalds static void belkin_sa_shutdown		(struct usb_serial *serial);
931da177e4SLinus Torvalds static int  belkin_sa_open		(struct usb_serial_port *port, struct file *filp);
941da177e4SLinus Torvalds static void belkin_sa_close		(struct usb_serial_port *port, struct file *filp);
951da177e4SLinus Torvalds static void belkin_sa_read_int_callback (struct urb *urb, struct pt_regs *regs);
961da177e4SLinus Torvalds static void belkin_sa_set_termios	(struct usb_serial_port *port, struct termios * old);
971da177e4SLinus Torvalds static int  belkin_sa_ioctl		(struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
981da177e4SLinus Torvalds static void belkin_sa_break_ctl		(struct usb_serial_port *port, int break_state );
991da177e4SLinus Torvalds static int  belkin_sa_tiocmget		(struct usb_serial_port *port, struct file *file);
1001da177e4SLinus Torvalds static int  belkin_sa_tiocmset		(struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear);
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds static struct usb_device_id id_table_combined [] = {
1041da177e4SLinus Torvalds 	{ USB_DEVICE(BELKIN_SA_VID, BELKIN_SA_PID) },
1051da177e4SLinus Torvalds 	{ USB_DEVICE(BELKIN_OLD_VID, BELKIN_OLD_PID) },
1061da177e4SLinus Torvalds 	{ USB_DEVICE(PERACOM_VID, PERACOM_PID) },
1071da177e4SLinus Torvalds 	{ USB_DEVICE(GOHUBS_VID, GOHUBS_PID) },
1081da177e4SLinus Torvalds 	{ USB_DEVICE(GOHUBS_VID, HANDYLINK_PID) },
1091da177e4SLinus Torvalds 	{ USB_DEVICE(BELKIN_DOCKSTATION_VID, BELKIN_DOCKSTATION_PID) },
1101da177e4SLinus Torvalds 	{ }							/* Terminating entry */
1111da177e4SLinus Torvalds };
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds MODULE_DEVICE_TABLE (usb, id_table_combined);
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds static struct usb_driver belkin_driver = {
1161da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
1171da177e4SLinus Torvalds 	.name =		"belkin",
1181da177e4SLinus Torvalds 	.probe =	usb_serial_probe,
1191da177e4SLinus Torvalds 	.disconnect =	usb_serial_disconnect,
1201da177e4SLinus Torvalds 	.id_table =	id_table_combined,
1211da177e4SLinus Torvalds };
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds /* All of the device info needed for the serial converters */
124*ea65370dSGreg Kroah-Hartman static struct usb_serial_driver belkin_device = {
1251da177e4SLinus Torvalds 	.owner =		THIS_MODULE,
1261da177e4SLinus Torvalds 	.name =			"Belkin / Peracom / GoHubs USB Serial Adapter",
1271da177e4SLinus Torvalds 	.short_name =		"belkin",
1281da177e4SLinus Torvalds 	.id_table =		id_table_combined,
1291da177e4SLinus Torvalds 	.num_interrupt_in =	1,
1301da177e4SLinus Torvalds 	.num_bulk_in =		1,
1311da177e4SLinus Torvalds 	.num_bulk_out =		1,
1321da177e4SLinus Torvalds 	.num_ports =		1,
1331da177e4SLinus Torvalds 	.open =			belkin_sa_open,
1341da177e4SLinus Torvalds 	.close =		belkin_sa_close,
1351da177e4SLinus Torvalds 	.read_int_callback =	belkin_sa_read_int_callback,	/* How we get the status info */
1361da177e4SLinus Torvalds 	.ioctl =		belkin_sa_ioctl,
1371da177e4SLinus Torvalds 	.set_termios =		belkin_sa_set_termios,
1381da177e4SLinus Torvalds 	.break_ctl =		belkin_sa_break_ctl,
1391da177e4SLinus Torvalds 	.tiocmget =		belkin_sa_tiocmget,
1401da177e4SLinus Torvalds 	.tiocmset =		belkin_sa_tiocmset,
1411da177e4SLinus Torvalds 	.attach =		belkin_sa_startup,
1421da177e4SLinus Torvalds 	.shutdown =		belkin_sa_shutdown,
1431da177e4SLinus Torvalds };
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds struct belkin_sa_private {
1471da177e4SLinus Torvalds 	spinlock_t		lock;
1481da177e4SLinus Torvalds 	unsigned long		control_state;
1491da177e4SLinus Torvalds 	unsigned char		last_lsr;
1501da177e4SLinus Torvalds 	unsigned char		last_msr;
1511da177e4SLinus Torvalds 	int			bad_flow_control;
1521da177e4SLinus Torvalds };
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds /*
1561da177e4SLinus Torvalds  * ***************************************************************************
1571da177e4SLinus Torvalds  * Belkin USB Serial Adapter F5U103 specific driver functions
1581da177e4SLinus Torvalds  * ***************************************************************************
1591da177e4SLinus Torvalds  */
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds #define WDR_TIMEOUT 5000 /* default urb timeout */
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds /* assumes that struct usb_serial *serial is available */
1641da177e4SLinus Torvalds #define BSA_USB_CMD(c,v) usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), \
1651da177e4SLinus Torvalds 					    (c), BELKIN_SA_SET_REQUEST_TYPE, \
1661da177e4SLinus Torvalds 					    (v), 0, NULL, 0, WDR_TIMEOUT)
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds /* do some startup allocations not currently performed by usb_serial_probe() */
1691da177e4SLinus Torvalds static int belkin_sa_startup (struct usb_serial *serial)
1701da177e4SLinus Torvalds {
1711da177e4SLinus Torvalds 	struct usb_device *dev = serial->dev;
1721da177e4SLinus Torvalds 	struct belkin_sa_private *priv;
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	/* allocate the private data structure */
1751da177e4SLinus Torvalds 	priv = kmalloc(sizeof(struct belkin_sa_private), GFP_KERNEL);
1761da177e4SLinus Torvalds 	if (!priv)
1771da177e4SLinus Torvalds 		return (-1); /* error */
1781da177e4SLinus Torvalds 	/* set initial values for control structures */
1791da177e4SLinus Torvalds 	spin_lock_init(&priv->lock);
1801da177e4SLinus Torvalds 	priv->control_state = 0;
1811da177e4SLinus Torvalds 	priv->last_lsr = 0;
1821da177e4SLinus Torvalds 	priv->last_msr = 0;
1831da177e4SLinus Torvalds 	/* see comments at top of file */
1841da177e4SLinus Torvalds 	priv->bad_flow_control = (le16_to_cpu(dev->descriptor.bcdDevice) <= 0x0206) ? 1 : 0;
1851da177e4SLinus Torvalds 	info("bcdDevice: %04x, bfc: %d", le16_to_cpu(dev->descriptor.bcdDevice), priv->bad_flow_control);
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	init_waitqueue_head(&serial->port[0]->write_wait);
1881da177e4SLinus Torvalds 	usb_set_serial_port_data(serial->port[0], priv);
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 	return (0);
1911da177e4SLinus Torvalds }
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds static void belkin_sa_shutdown (struct usb_serial *serial)
1951da177e4SLinus Torvalds {
1961da177e4SLinus Torvalds 	struct belkin_sa_private *priv;
1971da177e4SLinus Torvalds 	int i;
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	dbg ("%s", __FUNCTION__);
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	/* stop reads and writes on all ports */
2021da177e4SLinus Torvalds 	for (i=0; i < serial->num_ports; ++i) {
2031da177e4SLinus Torvalds 		/* My special items, the standard routines free my urbs */
2041da177e4SLinus Torvalds 		priv = usb_get_serial_port_data(serial->port[i]);
2051da177e4SLinus Torvalds 		kfree(priv);
2061da177e4SLinus Torvalds 	}
2071da177e4SLinus Torvalds }
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds static int  belkin_sa_open (struct usb_serial_port *port, struct file *filp)
2111da177e4SLinus Torvalds {
2121da177e4SLinus Torvalds 	int retval = 0;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	dbg("%s port %d", __FUNCTION__, port->number);
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	/*Start reading from the device*/
2171da177e4SLinus Torvalds 	/* TODO: Look at possibility of submitting multiple URBs to device to
2181da177e4SLinus Torvalds 	 *       enhance buffering.  Win trace shows 16 initial read URBs.
2191da177e4SLinus Torvalds 	 */
2201da177e4SLinus Torvalds 	port->read_urb->dev = port->serial->dev;
2211da177e4SLinus Torvalds 	retval = usb_submit_urb(port->read_urb, GFP_KERNEL);
2221da177e4SLinus Torvalds 	if (retval) {
2231da177e4SLinus Torvalds 		err("usb_submit_urb(read bulk) failed");
2241da177e4SLinus Torvalds 		goto exit;
2251da177e4SLinus Torvalds 	}
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 	port->interrupt_in_urb->dev = port->serial->dev;
2281da177e4SLinus Torvalds 	retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
2291da177e4SLinus Torvalds 	if (retval) {
2301da177e4SLinus Torvalds 		usb_kill_urb(port->read_urb);
2311da177e4SLinus Torvalds 		err(" usb_submit_urb(read int) failed");
2321da177e4SLinus Torvalds 	}
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds exit:
2351da177e4SLinus Torvalds 	return retval;
2361da177e4SLinus Torvalds } /* belkin_sa_open */
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds static void belkin_sa_close (struct usb_serial_port *port, struct file *filp)
2401da177e4SLinus Torvalds {
2411da177e4SLinus Torvalds 	dbg("%s port %d", __FUNCTION__, port->number);
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	/* shutdown our bulk reads and writes */
2441da177e4SLinus Torvalds 	usb_kill_urb(port->write_urb);
2451da177e4SLinus Torvalds 	usb_kill_urb(port->read_urb);
2461da177e4SLinus Torvalds 	usb_kill_urb(port->interrupt_in_urb);
2471da177e4SLinus Torvalds } /* belkin_sa_close */
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds static void belkin_sa_read_int_callback (struct urb *urb, struct pt_regs *regs)
2511da177e4SLinus Torvalds {
2521da177e4SLinus Torvalds 	struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
2531da177e4SLinus Torvalds 	struct belkin_sa_private *priv;
2541da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
2551da177e4SLinus Torvalds 	int retval;
2561da177e4SLinus Torvalds 	unsigned long flags;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	switch (urb->status) {
2591da177e4SLinus Torvalds 	case 0:
2601da177e4SLinus Torvalds 		/* success */
2611da177e4SLinus Torvalds 		break;
2621da177e4SLinus Torvalds 	case -ECONNRESET:
2631da177e4SLinus Torvalds 	case -ENOENT:
2641da177e4SLinus Torvalds 	case -ESHUTDOWN:
2651da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
2661da177e4SLinus Torvalds 		dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
2671da177e4SLinus Torvalds 		return;
2681da177e4SLinus Torvalds 	default:
2691da177e4SLinus Torvalds 		dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
2701da177e4SLinus Torvalds 		goto exit;
2711da177e4SLinus Torvalds 	}
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	/* Handle known interrupt data */
2761da177e4SLinus Torvalds 	/* ignore data[0] and data[1] */
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	priv = usb_get_serial_port_data(port);
2791da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
2801da177e4SLinus Torvalds 	priv->last_msr = data[BELKIN_SA_MSR_INDEX];
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	/* Record Control Line states */
2831da177e4SLinus Torvalds 	if (priv->last_msr & BELKIN_SA_MSR_DSR)
2841da177e4SLinus Torvalds 		priv->control_state |= TIOCM_DSR;
2851da177e4SLinus Torvalds 	else
2861da177e4SLinus Torvalds 		priv->control_state &= ~TIOCM_DSR;
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	if (priv->last_msr & BELKIN_SA_MSR_CTS)
2891da177e4SLinus Torvalds 		priv->control_state |= TIOCM_CTS;
2901da177e4SLinus Torvalds 	else
2911da177e4SLinus Torvalds 		priv->control_state &= ~TIOCM_CTS;
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	if (priv->last_msr & BELKIN_SA_MSR_RI)
2941da177e4SLinus Torvalds 		priv->control_state |= TIOCM_RI;
2951da177e4SLinus Torvalds 	else
2961da177e4SLinus Torvalds 		priv->control_state &= ~TIOCM_RI;
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	if (priv->last_msr & BELKIN_SA_MSR_CD)
2991da177e4SLinus Torvalds 		priv->control_state |= TIOCM_CD;
3001da177e4SLinus Torvalds 	else
3011da177e4SLinus Torvalds 		priv->control_state &= ~TIOCM_CD;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	/* Now to report any errors */
3041da177e4SLinus Torvalds 	priv->last_lsr = data[BELKIN_SA_LSR_INDEX];
3051da177e4SLinus Torvalds #if 0
3061da177e4SLinus Torvalds 	/*
3071da177e4SLinus Torvalds 	 * fill in the flip buffer here, but I do not know the relation
3081da177e4SLinus Torvalds 	 * to the current/next receive buffer or characters.  I need
3091da177e4SLinus Torvalds 	 * to look in to this before committing any code.
3101da177e4SLinus Torvalds 	 */
3111da177e4SLinus Torvalds 	if (priv->last_lsr & BELKIN_SA_LSR_ERR) {
3121da177e4SLinus Torvalds 		tty = port->tty;
3131da177e4SLinus Torvalds 		/* Overrun Error */
3141da177e4SLinus Torvalds 		if (priv->last_lsr & BELKIN_SA_LSR_OE) {
3151da177e4SLinus Torvalds 		}
3161da177e4SLinus Torvalds 		/* Parity Error */
3171da177e4SLinus Torvalds 		if (priv->last_lsr & BELKIN_SA_LSR_PE) {
3181da177e4SLinus Torvalds 		}
3191da177e4SLinus Torvalds 		/* Framing Error */
3201da177e4SLinus Torvalds 		if (priv->last_lsr & BELKIN_SA_LSR_FE) {
3211da177e4SLinus Torvalds 		}
3221da177e4SLinus Torvalds 		/* Break Indicator */
3231da177e4SLinus Torvalds 		if (priv->last_lsr & BELKIN_SA_LSR_BI) {
3241da177e4SLinus Torvalds 		}
3251da177e4SLinus Torvalds 	}
3261da177e4SLinus Torvalds #endif
3271da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
3281da177e4SLinus Torvalds exit:
3291da177e4SLinus Torvalds 	retval = usb_submit_urb (urb, GFP_ATOMIC);
3301da177e4SLinus Torvalds 	if (retval)
3311da177e4SLinus Torvalds 		err ("%s - usb_submit_urb failed with result %d",
3321da177e4SLinus Torvalds 		     __FUNCTION__, retval);
3331da177e4SLinus Torvalds }
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds static void belkin_sa_set_termios (struct usb_serial_port *port, struct termios *old_termios)
3361da177e4SLinus Torvalds {
3371da177e4SLinus Torvalds 	struct usb_serial *serial = port->serial;
3381da177e4SLinus Torvalds 	struct belkin_sa_private *priv = usb_get_serial_port_data(port);
3391da177e4SLinus Torvalds 	unsigned int iflag;
3401da177e4SLinus Torvalds 	unsigned int cflag;
3411da177e4SLinus Torvalds 	unsigned int old_iflag = 0;
3421da177e4SLinus Torvalds 	unsigned int old_cflag = 0;
3431da177e4SLinus Torvalds 	__u16 urb_value = 0; /* Will hold the new flags */
3441da177e4SLinus Torvalds 	unsigned long flags;
3451da177e4SLinus Torvalds 	unsigned long control_state;
3461da177e4SLinus Torvalds 	int bad_flow_control;
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 	if ((!port->tty) || (!port->tty->termios)) {
3491da177e4SLinus Torvalds 		dbg ("%s - no tty or termios structure", __FUNCTION__);
3501da177e4SLinus Torvalds 		return;
3511da177e4SLinus Torvalds 	}
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	iflag = port->tty->termios->c_iflag;
3541da177e4SLinus Torvalds 	cflag = port->tty->termios->c_cflag;
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 	/* get a local copy of the current port settings */
3571da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
3581da177e4SLinus Torvalds 	control_state = priv->control_state;
3591da177e4SLinus Torvalds 	bad_flow_control = priv->bad_flow_control;
3601da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 	/* check that they really want us to change something */
3631da177e4SLinus Torvalds 	if (old_termios) {
3641da177e4SLinus Torvalds 		if ((cflag == old_termios->c_cflag) &&
3651da177e4SLinus Torvalds 		    (RELEVANT_IFLAG(port->tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
3661da177e4SLinus Torvalds 			dbg("%s - nothing to change...", __FUNCTION__);
3671da177e4SLinus Torvalds 			return;
3681da177e4SLinus Torvalds 		}
3691da177e4SLinus Torvalds 		old_iflag = old_termios->c_iflag;
3701da177e4SLinus Torvalds 		old_cflag = old_termios->c_cflag;
3711da177e4SLinus Torvalds 	}
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	/* Set the baud rate */
3741da177e4SLinus Torvalds 	if( (cflag&CBAUD) != (old_cflag&CBAUD) ) {
3751da177e4SLinus Torvalds 		/* reassert DTR and (maybe) RTS on transition from B0 */
3761da177e4SLinus Torvalds 		if( (old_cflag&CBAUD) == B0 ) {
3771da177e4SLinus Torvalds 			control_state |= (TIOCM_DTR|TIOCM_RTS);
3781da177e4SLinus Torvalds 			if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, 1) < 0)
3791da177e4SLinus Torvalds 				err("Set DTR error");
3801da177e4SLinus Torvalds 			/* don't set RTS if using hardware flow control */
3811da177e4SLinus Torvalds 			if (!(old_cflag&CRTSCTS) )
3821da177e4SLinus Torvalds 				if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, 1) < 0)
3831da177e4SLinus Torvalds 					err("Set RTS error");
3841da177e4SLinus Torvalds 		}
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 		switch(cflag & CBAUD) {
3871da177e4SLinus Torvalds 			case B0: /* handled below */ break;
3881da177e4SLinus Torvalds 			case B300: urb_value = BELKIN_SA_BAUD(300); break;
3891da177e4SLinus Torvalds 			case B600: urb_value = BELKIN_SA_BAUD(600); break;
3901da177e4SLinus Torvalds 			case B1200: urb_value = BELKIN_SA_BAUD(1200); break;
3911da177e4SLinus Torvalds 			case B2400: urb_value = BELKIN_SA_BAUD(2400); break;
3921da177e4SLinus Torvalds 			case B4800: urb_value = BELKIN_SA_BAUD(4800); break;
3931da177e4SLinus Torvalds 			case B9600: urb_value = BELKIN_SA_BAUD(9600); break;
3941da177e4SLinus Torvalds 			case B19200: urb_value = BELKIN_SA_BAUD(19200); break;
3951da177e4SLinus Torvalds 			case B38400: urb_value = BELKIN_SA_BAUD(38400); break;
3961da177e4SLinus Torvalds 			case B57600: urb_value = BELKIN_SA_BAUD(57600); break;
3971da177e4SLinus Torvalds 			case B115200: urb_value = BELKIN_SA_BAUD(115200); break;
3981da177e4SLinus Torvalds 			case B230400: urb_value = BELKIN_SA_BAUD(230400); break;
3991da177e4SLinus Torvalds 			default: err("BELKIN USB Serial Adapter: unsupported baudrate request, using default of 9600");
4001da177e4SLinus Torvalds 				urb_value = BELKIN_SA_BAUD(9600); break;
4011da177e4SLinus Torvalds 		}
4021da177e4SLinus Torvalds 		if ((cflag & CBAUD) != B0 ) {
4031da177e4SLinus Torvalds 			if (BSA_USB_CMD(BELKIN_SA_SET_BAUDRATE_REQUEST, urb_value) < 0)
4041da177e4SLinus Torvalds 				err("Set baudrate error");
4051da177e4SLinus Torvalds 		} else {
4061da177e4SLinus Torvalds 			/* Disable flow control */
4071da177e4SLinus Torvalds 			if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST, BELKIN_SA_FLOW_NONE) < 0)
4081da177e4SLinus Torvalds 				err("Disable flowcontrol error");
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 			/* Drop RTS and DTR */
4111da177e4SLinus Torvalds 			control_state &= ~(TIOCM_DTR | TIOCM_RTS);
4121da177e4SLinus Torvalds 			if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, 0) < 0)
4131da177e4SLinus Torvalds 				err("DTR LOW error");
4141da177e4SLinus Torvalds 			if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, 0) < 0)
4151da177e4SLinus Torvalds 				err("RTS LOW error");
4161da177e4SLinus Torvalds 		}
4171da177e4SLinus Torvalds 	}
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	/* set the parity */
4201da177e4SLinus Torvalds 	if( (cflag&(PARENB|PARODD)) != (old_cflag&(PARENB|PARODD)) ) {
4211da177e4SLinus Torvalds 		if (cflag & PARENB)
4221da177e4SLinus Torvalds 			urb_value = (cflag & PARODD) ?  BELKIN_SA_PARITY_ODD : BELKIN_SA_PARITY_EVEN;
4231da177e4SLinus Torvalds 		else
4241da177e4SLinus Torvalds 			urb_value = BELKIN_SA_PARITY_NONE;
4251da177e4SLinus Torvalds 		if (BSA_USB_CMD(BELKIN_SA_SET_PARITY_REQUEST, urb_value) < 0)
4261da177e4SLinus Torvalds 			err("Set parity error");
4271da177e4SLinus Torvalds 	}
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	/* set the number of data bits */
4301da177e4SLinus Torvalds 	if( (cflag&CSIZE) != (old_cflag&CSIZE) ) {
4311da177e4SLinus Torvalds 		switch (cflag & CSIZE) {
4321da177e4SLinus Torvalds 			case CS5: urb_value = BELKIN_SA_DATA_BITS(5); break;
4331da177e4SLinus Torvalds 			case CS6: urb_value = BELKIN_SA_DATA_BITS(6); break;
4341da177e4SLinus Torvalds 			case CS7: urb_value = BELKIN_SA_DATA_BITS(7); break;
4351da177e4SLinus Torvalds 			case CS8: urb_value = BELKIN_SA_DATA_BITS(8); break;
4361da177e4SLinus Torvalds 			default: err("CSIZE was not CS5-CS8, using default of 8");
4371da177e4SLinus Torvalds 				urb_value = BELKIN_SA_DATA_BITS(8);
4381da177e4SLinus Torvalds 				break;
4391da177e4SLinus Torvalds 		}
4401da177e4SLinus Torvalds 		if (BSA_USB_CMD(BELKIN_SA_SET_DATA_BITS_REQUEST, urb_value) < 0)
4411da177e4SLinus Torvalds 			err("Set data bits error");
4421da177e4SLinus Torvalds 	}
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 	/* set the number of stop bits */
4451da177e4SLinus Torvalds 	if( (cflag&CSTOPB) != (old_cflag&CSTOPB) ) {
4461da177e4SLinus Torvalds 		urb_value = (cflag & CSTOPB) ? BELKIN_SA_STOP_BITS(2) : BELKIN_SA_STOP_BITS(1);
4471da177e4SLinus Torvalds 		if (BSA_USB_CMD(BELKIN_SA_SET_STOP_BITS_REQUEST, urb_value) < 0)
4481da177e4SLinus Torvalds 			err("Set stop bits error");
4491da177e4SLinus Torvalds 	}
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	/* Set flow control */
4521da177e4SLinus Torvalds 	if( (iflag&IXOFF)   != (old_iflag&IXOFF)
4531da177e4SLinus Torvalds 	||	(iflag&IXON)    != (old_iflag&IXON)
4541da177e4SLinus Torvalds 	||  (cflag&CRTSCTS) != (old_cflag&CRTSCTS) ) {
4551da177e4SLinus Torvalds 		urb_value = 0;
4561da177e4SLinus Torvalds 		if ((iflag & IXOFF) || (iflag & IXON))
4571da177e4SLinus Torvalds 			urb_value |= (BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
4581da177e4SLinus Torvalds 		else
4591da177e4SLinus Torvalds 			urb_value &= ~(BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 		if (cflag & CRTSCTS)
4621da177e4SLinus Torvalds 			urb_value |=  (BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
4631da177e4SLinus Torvalds 		else
4641da177e4SLinus Torvalds 			urb_value &= ~(BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 		if (bad_flow_control)
4671da177e4SLinus Torvalds 			urb_value &= ~(BELKIN_SA_FLOW_IRTS);
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 		if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST, urb_value) < 0)
4701da177e4SLinus Torvalds 			err("Set flow control error");
4711da177e4SLinus Torvalds 	}
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	/* save off the modified port settings */
4741da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
4751da177e4SLinus Torvalds 	priv->control_state = control_state;
4761da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
4771da177e4SLinus Torvalds } /* belkin_sa_set_termios */
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds static void belkin_sa_break_ctl( struct usb_serial_port *port, int break_state )
4811da177e4SLinus Torvalds {
4821da177e4SLinus Torvalds 	struct usb_serial *serial = port->serial;
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 	if (BSA_USB_CMD(BELKIN_SA_SET_BREAK_REQUEST, break_state ? 1 : 0) < 0)
4851da177e4SLinus Torvalds 		err("Set break_ctl %d", break_state);
4861da177e4SLinus Torvalds }
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds static int belkin_sa_tiocmget (struct usb_serial_port *port, struct file *file)
4901da177e4SLinus Torvalds {
4911da177e4SLinus Torvalds 	struct belkin_sa_private *priv = usb_get_serial_port_data(port);
4921da177e4SLinus Torvalds 	unsigned long control_state;
4931da177e4SLinus Torvalds 	unsigned long flags;
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 	dbg("%s", __FUNCTION__);
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
4981da177e4SLinus Torvalds 	control_state = priv->control_state;
4991da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 	return control_state;
5021da177e4SLinus Torvalds }
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds static int belkin_sa_tiocmset (struct usb_serial_port *port, struct file *file,
5061da177e4SLinus Torvalds 			       unsigned int set, unsigned int clear)
5071da177e4SLinus Torvalds {
5081da177e4SLinus Torvalds 	struct usb_serial *serial = port->serial;
5091da177e4SLinus Torvalds 	struct belkin_sa_private *priv = usb_get_serial_port_data(port);
5101da177e4SLinus Torvalds 	unsigned long control_state;
5111da177e4SLinus Torvalds 	unsigned long flags;
5121da177e4SLinus Torvalds 	int retval;
5131da177e4SLinus Torvalds 	int rts = 0;
5141da177e4SLinus Torvalds 	int dtr = 0;
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	dbg("%s", __FUNCTION__);
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
5191da177e4SLinus Torvalds 	control_state = priv->control_state;
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	if (set & TIOCM_RTS) {
5221da177e4SLinus Torvalds 		control_state |= TIOCM_RTS;
5231da177e4SLinus Torvalds 		rts = 1;
5241da177e4SLinus Torvalds 	}
5251da177e4SLinus Torvalds 	if (set & TIOCM_DTR) {
5261da177e4SLinus Torvalds 		control_state |= TIOCM_DTR;
5271da177e4SLinus Torvalds 		dtr = 1;
5281da177e4SLinus Torvalds 	}
5291da177e4SLinus Torvalds 	if (clear & TIOCM_RTS) {
5301da177e4SLinus Torvalds 		control_state &= ~TIOCM_RTS;
5311da177e4SLinus Torvalds 		rts = 0;
5321da177e4SLinus Torvalds 	}
5331da177e4SLinus Torvalds 	if (clear & TIOCM_DTR) {
5341da177e4SLinus Torvalds 		control_state &= ~TIOCM_DTR;
5351da177e4SLinus Torvalds 		dtr = 0;
5361da177e4SLinus Torvalds 	}
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 	priv->control_state = control_state;
5391da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 	retval = BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, rts);
5421da177e4SLinus Torvalds 	if (retval < 0) {
5431da177e4SLinus Torvalds 		err("Set RTS error %d", retval);
5441da177e4SLinus Torvalds 		goto exit;
5451da177e4SLinus Torvalds 	}
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	retval = BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, dtr);
5481da177e4SLinus Torvalds 	if (retval < 0) {
5491da177e4SLinus Torvalds 		err("Set DTR error %d", retval);
5501da177e4SLinus Torvalds 		goto exit;
5511da177e4SLinus Torvalds 	}
5521da177e4SLinus Torvalds exit:
5531da177e4SLinus Torvalds 	return retval;
5541da177e4SLinus Torvalds }
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds static int belkin_sa_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
5581da177e4SLinus Torvalds {
5591da177e4SLinus Torvalds 	switch (cmd) {
5601da177e4SLinus Torvalds 	case TIOCMIWAIT:
5611da177e4SLinus Torvalds 		/* wait for any of the 4 modem inputs (DCD,RI,DSR,CTS)*/
5621da177e4SLinus Torvalds 		/* TODO */
5631da177e4SLinus Torvalds 		return( 0 );
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	case TIOCGICOUNT:
5661da177e4SLinus Torvalds 		/* return count of modemline transitions */
5671da177e4SLinus Torvalds 		/* TODO */
5681da177e4SLinus Torvalds 		return 0;
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	default:
5711da177e4SLinus Torvalds 		dbg("belkin_sa_ioctl arg not supported - 0x%04x",cmd);
5721da177e4SLinus Torvalds 		return(-ENOIOCTLCMD);
5731da177e4SLinus Torvalds 		break;
5741da177e4SLinus Torvalds 	}
5751da177e4SLinus Torvalds 	return 0;
5761da177e4SLinus Torvalds } /* belkin_sa_ioctl */
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds static int __init belkin_sa_init (void)
5801da177e4SLinus Torvalds {
5811da177e4SLinus Torvalds 	int retval;
5821da177e4SLinus Torvalds 	retval = usb_serial_register(&belkin_device);
5831da177e4SLinus Torvalds 	if (retval)
5841da177e4SLinus Torvalds 		goto failed_usb_serial_register;
5851da177e4SLinus Torvalds 	retval = usb_register(&belkin_driver);
5861da177e4SLinus Torvalds 	if (retval)
5871da177e4SLinus Torvalds 		goto failed_usb_register;
5881da177e4SLinus Torvalds 	info(DRIVER_DESC " " DRIVER_VERSION);
5891da177e4SLinus Torvalds 	return 0;
5901da177e4SLinus Torvalds failed_usb_register:
5911da177e4SLinus Torvalds 	usb_serial_deregister(&belkin_device);
5921da177e4SLinus Torvalds failed_usb_serial_register:
5931da177e4SLinus Torvalds 	return retval;
5941da177e4SLinus Torvalds }
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds static void __exit belkin_sa_exit (void)
5981da177e4SLinus Torvalds {
5991da177e4SLinus Torvalds 	usb_deregister (&belkin_driver);
6001da177e4SLinus Torvalds 	usb_serial_deregister (&belkin_device);
6011da177e4SLinus Torvalds }
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds module_init (belkin_sa_init);
6051da177e4SLinus Torvalds module_exit (belkin_sa_exit);
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds MODULE_AUTHOR( DRIVER_AUTHOR );
6081da177e4SLinus Torvalds MODULE_DESCRIPTION( DRIVER_DESC );
6091da177e4SLinus Torvalds MODULE_VERSION( DRIVER_VERSION );
6101da177e4SLinus Torvalds MODULE_LICENSE("GPL");
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds module_param(debug, bool, S_IRUGO | S_IWUSR);
6131da177e4SLinus Torvalds MODULE_PARM_DESC(debug, "Debug enabled or not");
614