xref: /linux/drivers/usb/serial/belkin_sa.c (revision c89aa63898f2238cd3c6537963a82b05146b9338)
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 support for flush commands
241da177e4SLinus Torvalds  */
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #include <linux/kernel.h>
271da177e4SLinus Torvalds #include <linux/errno.h>
281da177e4SLinus Torvalds #include <linux/init.h>
291da177e4SLinus Torvalds #include <linux/slab.h>
301da177e4SLinus Torvalds #include <linux/tty.h>
311da177e4SLinus Torvalds #include <linux/tty_driver.h>
321da177e4SLinus Torvalds #include <linux/tty_flip.h>
331da177e4SLinus Torvalds #include <linux/module.h>
341da177e4SLinus Torvalds #include <linux/spinlock.h>
35b69c1499SAlan Cox #include <linux/uaccess.h>
361da177e4SLinus Torvalds #include <linux/usb.h>
37a969888cSGreg Kroah-Hartman #include <linux/usb/serial.h>
381da177e4SLinus Torvalds #include "belkin_sa.h"
391da177e4SLinus Torvalds 
4090ab5ee9SRusty Russell static bool debug;
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds /*
431da177e4SLinus Torvalds  * Version Information
441da177e4SLinus Torvalds  */
452afd8287SJohan Hovold #define DRIVER_VERSION "v1.3"
461da177e4SLinus Torvalds #define DRIVER_AUTHOR "William Greathouse <wgreathouse@smva.com>"
471da177e4SLinus Torvalds #define DRIVER_DESC "USB Belkin Serial converter driver"
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds /* function prototypes for a Belkin USB Serial Adapter F5U103 */
501da177e4SLinus Torvalds static int  belkin_sa_startup(struct usb_serial *serial);
51f9c99bb8SAlan Stern static void belkin_sa_release(struct usb_serial *serial);
52b69c1499SAlan Cox static int  belkin_sa_open(struct tty_struct *tty,
53a509a7e4SAlan Cox 			struct usb_serial_port *port);
54335f8514SAlan Cox static void belkin_sa_close(struct usb_serial_port *port);
557d12e780SDavid Howells static void belkin_sa_read_int_callback(struct urb *urb);
562afd8287SJohan Hovold static void belkin_sa_process_read_urb(struct urb *urb);
57b69c1499SAlan Cox static void belkin_sa_set_termios(struct tty_struct *tty,
58b69c1499SAlan Cox 			struct usb_serial_port *port, struct ktermios * old);
5995da310eSAlan Cox static void belkin_sa_break_ctl(struct tty_struct *tty, int break_state);
6060b33c13SAlan Cox static int  belkin_sa_tiocmget(struct tty_struct *tty);
6120b9d177SAlan Cox static int  belkin_sa_tiocmset(struct tty_struct *tty,
62b69c1499SAlan Cox 					unsigned int set, unsigned int clear);
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds 
6568e24113SGreg Kroah-Hartman static const struct usb_device_id id_table[] = {
661da177e4SLinus Torvalds 	{ USB_DEVICE(BELKIN_SA_VID, BELKIN_SA_PID) },
671da177e4SLinus Torvalds 	{ USB_DEVICE(BELKIN_OLD_VID, BELKIN_OLD_PID) },
681da177e4SLinus Torvalds 	{ USB_DEVICE(PERACOM_VID, PERACOM_PID) },
691da177e4SLinus Torvalds 	{ USB_DEVICE(GOHUBS_VID, GOHUBS_PID) },
701da177e4SLinus Torvalds 	{ USB_DEVICE(GOHUBS_VID, HANDYLINK_PID) },
711da177e4SLinus Torvalds 	{ USB_DEVICE(BELKIN_DOCKSTATION_VID, BELKIN_DOCKSTATION_PID) },
721da177e4SLinus Torvalds 	{ }	/* Terminating entry */
731da177e4SLinus Torvalds };
7468e24113SGreg Kroah-Hartman MODULE_DEVICE_TABLE(usb, id_table);
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds /* All of the device info needed for the serial converters */
77ea65370dSGreg Kroah-Hartman static struct usb_serial_driver belkin_device = {
7818fcac35SGreg Kroah-Hartman 	.driver = {
791da177e4SLinus Torvalds 		.owner =	THIS_MODULE,
80269bda1cSGreg Kroah-Hartman 		.name =		"belkin",
8118fcac35SGreg Kroah-Hartman 	},
82269bda1cSGreg Kroah-Hartman 	.description =		"Belkin / Peracom / GoHubs USB Serial Adapter",
8368e24113SGreg Kroah-Hartman 	.id_table =		id_table,
841da177e4SLinus Torvalds 	.num_ports =		1,
851da177e4SLinus Torvalds 	.open =			belkin_sa_open,
861da177e4SLinus Torvalds 	.close =		belkin_sa_close,
87b69c1499SAlan Cox 	.read_int_callback =	belkin_sa_read_int_callback,
882afd8287SJohan Hovold 	.process_read_urb =	belkin_sa_process_read_urb,
891da177e4SLinus Torvalds 	.set_termios =		belkin_sa_set_termios,
901da177e4SLinus Torvalds 	.break_ctl =		belkin_sa_break_ctl,
911da177e4SLinus Torvalds 	.tiocmget =		belkin_sa_tiocmget,
921da177e4SLinus Torvalds 	.tiocmset =		belkin_sa_tiocmset,
931da177e4SLinus Torvalds 	.attach =		belkin_sa_startup,
94f9c99bb8SAlan Stern 	.release =		belkin_sa_release,
951da177e4SLinus Torvalds };
961da177e4SLinus Torvalds 
9708a4f6bcSAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
9808a4f6bcSAlan Stern 	&belkin_device, NULL
9908a4f6bcSAlan Stern };
10008a4f6bcSAlan Stern 
1011da177e4SLinus Torvalds struct belkin_sa_private {
1021da177e4SLinus Torvalds 	spinlock_t		lock;
1031da177e4SLinus Torvalds 	unsigned long		control_state;
1041da177e4SLinus Torvalds 	unsigned char		last_lsr;
1051da177e4SLinus Torvalds 	unsigned char		last_msr;
1061da177e4SLinus Torvalds 	int			bad_flow_control;
1071da177e4SLinus Torvalds };
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds /*
1111da177e4SLinus Torvalds  * ***************************************************************************
1121da177e4SLinus Torvalds  * Belkin USB Serial Adapter F5U103 specific driver functions
1131da177e4SLinus Torvalds  * ***************************************************************************
1141da177e4SLinus Torvalds  */
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds #define WDR_TIMEOUT 5000 /* default urb timeout */
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds /* assumes that struct usb_serial *serial is available */
1191da177e4SLinus Torvalds #define BSA_USB_CMD(c, v) usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), \
1201da177e4SLinus Torvalds 					    (c), BELKIN_SA_SET_REQUEST_TYPE, \
1211da177e4SLinus Torvalds 					    (v), 0, NULL, 0, WDR_TIMEOUT)
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds /* do some startup allocations not currently performed by usb_serial_probe() */
1241da177e4SLinus Torvalds static int belkin_sa_startup(struct usb_serial *serial)
1251da177e4SLinus Torvalds {
1261da177e4SLinus Torvalds 	struct usb_device *dev = serial->dev;
1271da177e4SLinus Torvalds 	struct belkin_sa_private *priv;
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	/* allocate the private data structure */
1301da177e4SLinus Torvalds 	priv = kmalloc(sizeof(struct belkin_sa_private), GFP_KERNEL);
1311da177e4SLinus Torvalds 	if (!priv)
132b69c1499SAlan Cox 		return -1; /* error */
1331da177e4SLinus Torvalds 	/* set initial values for control structures */
1341da177e4SLinus Torvalds 	spin_lock_init(&priv->lock);
1351da177e4SLinus Torvalds 	priv->control_state = 0;
1361da177e4SLinus Torvalds 	priv->last_lsr = 0;
1371da177e4SLinus Torvalds 	priv->last_msr = 0;
1381da177e4SLinus Torvalds 	/* see comments at top of file */
139b69c1499SAlan Cox 	priv->bad_flow_control =
140b69c1499SAlan Cox 		(le16_to_cpu(dev->descriptor.bcdDevice) <= 0x0206) ? 1 : 0;
141c197a8dbSGreg Kroah-Hartman 	dev_info(&dev->dev, "bcdDevice: %04x, bfc: %d\n",
142b69c1499SAlan Cox 					le16_to_cpu(dev->descriptor.bcdDevice),
143b69c1499SAlan Cox 					priv->bad_flow_control);
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	init_waitqueue_head(&serial->port[0]->write_wait);
1461da177e4SLinus Torvalds 	usb_set_serial_port_data(serial->port[0], priv);
1471da177e4SLinus Torvalds 
148b69c1499SAlan Cox 	return 0;
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds 
151f9c99bb8SAlan Stern static void belkin_sa_release(struct usb_serial *serial)
1521da177e4SLinus Torvalds {
1531da177e4SLinus Torvalds 	int i;
1541da177e4SLinus Torvalds 
155726ef428SJohan Hovold 	for (i = 0; i < serial->num_ports; ++i)
156726ef428SJohan Hovold 		kfree(usb_get_serial_port_data(serial->port[i]));
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
159b69c1499SAlan Cox static int belkin_sa_open(struct tty_struct *tty,
160a509a7e4SAlan Cox 					struct usb_serial_port *port)
1611da177e4SLinus Torvalds {
162f2f8b7feSJohan Hovold 	int retval;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
1651da177e4SLinus Torvalds 	if (retval) {
166194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
167f2f8b7feSJohan Hovold 		return retval;
1681da177e4SLinus Torvalds 	}
1691da177e4SLinus Torvalds 
170f2f8b7feSJohan Hovold 	retval = usb_serial_generic_open(tty, port);
171f2f8b7feSJohan Hovold 	if (retval)
172f2f8b7feSJohan Hovold 		usb_kill_urb(port->interrupt_in_urb);
173f2f8b7feSJohan Hovold 
1741da177e4SLinus Torvalds 	return retval;
175726ef428SJohan Hovold }
1761da177e4SLinus Torvalds 
177335f8514SAlan Cox static void belkin_sa_close(struct usb_serial_port *port)
1781da177e4SLinus Torvalds {
179f26788daSJohan Hovold 	usb_serial_generic_close(port);
1801da177e4SLinus Torvalds 	usb_kill_urb(port->interrupt_in_urb);
181726ef428SJohan Hovold }
1821da177e4SLinus Torvalds 
1837d12e780SDavid Howells static void belkin_sa_read_int_callback(struct urb *urb)
1841da177e4SLinus Torvalds {
185cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
1861da177e4SLinus Torvalds 	struct belkin_sa_private *priv;
1871da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
1881da177e4SLinus Torvalds 	int retval;
189f26aad25SGreg Kroah-Hartman 	int status = urb->status;
1901da177e4SLinus Torvalds 	unsigned long flags;
1911da177e4SLinus Torvalds 
192f26aad25SGreg Kroah-Hartman 	switch (status) {
1931da177e4SLinus Torvalds 	case 0:
1941da177e4SLinus Torvalds 		/* success */
1951da177e4SLinus Torvalds 		break;
1961da177e4SLinus Torvalds 	case -ECONNRESET:
1971da177e4SLinus Torvalds 	case -ENOENT:
1981da177e4SLinus Torvalds 	case -ESHUTDOWN:
1991da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
200*c89aa638SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
201441b62c1SHarvey Harrison 			__func__, status);
2021da177e4SLinus Torvalds 		return;
2031da177e4SLinus Torvalds 	default:
204*c89aa638SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
205441b62c1SHarvey Harrison 			__func__, status);
2061da177e4SLinus Torvalds 		goto exit;
2071da177e4SLinus Torvalds 	}
2081da177e4SLinus Torvalds 
209b69c1499SAlan Cox 	usb_serial_debug_data(debug, &port->dev, __func__,
210b69c1499SAlan Cox 					urb->actual_length, data);
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	/* Handle known interrupt data */
2131da177e4SLinus Torvalds 	/* ignore data[0] and data[1] */
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	priv = usb_get_serial_port_data(port);
2161da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
2171da177e4SLinus Torvalds 	priv->last_msr = data[BELKIN_SA_MSR_INDEX];
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	/* Record Control Line states */
2201da177e4SLinus Torvalds 	if (priv->last_msr & BELKIN_SA_MSR_DSR)
2211da177e4SLinus Torvalds 		priv->control_state |= TIOCM_DSR;
2221da177e4SLinus Torvalds 	else
2231da177e4SLinus Torvalds 		priv->control_state &= ~TIOCM_DSR;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	if (priv->last_msr & BELKIN_SA_MSR_CTS)
2261da177e4SLinus Torvalds 		priv->control_state |= TIOCM_CTS;
2271da177e4SLinus Torvalds 	else
2281da177e4SLinus Torvalds 		priv->control_state &= ~TIOCM_CTS;
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	if (priv->last_msr & BELKIN_SA_MSR_RI)
2311da177e4SLinus Torvalds 		priv->control_state |= TIOCM_RI;
2321da177e4SLinus Torvalds 	else
2331da177e4SLinus Torvalds 		priv->control_state &= ~TIOCM_RI;
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	if (priv->last_msr & BELKIN_SA_MSR_CD)
2361da177e4SLinus Torvalds 		priv->control_state |= TIOCM_CD;
2371da177e4SLinus Torvalds 	else
2381da177e4SLinus Torvalds 		priv->control_state &= ~TIOCM_CD;
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	priv->last_lsr = data[BELKIN_SA_LSR_INDEX];
2411da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
2421da177e4SLinus Torvalds exit:
2431da177e4SLinus Torvalds 	retval = usb_submit_urb(urb, GFP_ATOMIC);
2441da177e4SLinus Torvalds 	if (retval)
245194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "%s - usb_submit_urb failed with "
246194343d9SGreg Kroah-Hartman 			"result %d\n", __func__, retval);
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds 
2492afd8287SJohan Hovold static void belkin_sa_process_read_urb(struct urb *urb)
2502afd8287SJohan Hovold {
2512afd8287SJohan Hovold 	struct usb_serial_port *port = urb->context;
2522afd8287SJohan Hovold 	struct belkin_sa_private *priv = usb_get_serial_port_data(port);
2532afd8287SJohan Hovold 	struct tty_struct *tty;
2542afd8287SJohan Hovold 	unsigned char *data = urb->transfer_buffer;
2552afd8287SJohan Hovold 	unsigned long flags;
2562afd8287SJohan Hovold 	unsigned char status;
2572afd8287SJohan Hovold 	char tty_flag;
2582afd8287SJohan Hovold 
2592afd8287SJohan Hovold 	/* Update line status */
2602afd8287SJohan Hovold 	tty_flag = TTY_NORMAL;
2612afd8287SJohan Hovold 
2622afd8287SJohan Hovold 	spin_lock_irqsave(&priv->lock, flags);
2632afd8287SJohan Hovold 	status = priv->last_lsr;
2642afd8287SJohan Hovold 	priv->last_lsr &= ~BELKIN_SA_LSR_ERR;
2652afd8287SJohan Hovold 	spin_unlock_irqrestore(&priv->lock, flags);
2662afd8287SJohan Hovold 
2672afd8287SJohan Hovold 	if (!urb->actual_length)
2682afd8287SJohan Hovold 		return;
2692afd8287SJohan Hovold 
2702afd8287SJohan Hovold 	tty = tty_port_tty_get(&port->port);
2712afd8287SJohan Hovold 	if (!tty)
2722afd8287SJohan Hovold 		return;
2732afd8287SJohan Hovold 
2742afd8287SJohan Hovold 	if (status & BELKIN_SA_LSR_ERR) {
2752afd8287SJohan Hovold 		/* Break takes precedence over parity, which takes precedence
2762afd8287SJohan Hovold 		 * over framing errors. */
2772afd8287SJohan Hovold 		if (status & BELKIN_SA_LSR_BI)
2782afd8287SJohan Hovold 			tty_flag = TTY_BREAK;
2792afd8287SJohan Hovold 		else if (status & BELKIN_SA_LSR_PE)
2802afd8287SJohan Hovold 			tty_flag = TTY_PARITY;
2812afd8287SJohan Hovold 		else if (status & BELKIN_SA_LSR_FE)
2822afd8287SJohan Hovold 			tty_flag = TTY_FRAME;
2832afd8287SJohan Hovold 		dev_dbg(&port->dev, "tty_flag = %d\n", tty_flag);
2842afd8287SJohan Hovold 
2852afd8287SJohan Hovold 		/* Overrun is special, not associated with a char. */
2862afd8287SJohan Hovold 		if (status & BELKIN_SA_LSR_OE)
2872afd8287SJohan Hovold 			tty_insert_flip_char(tty, 0, TTY_OVERRUN);
2882afd8287SJohan Hovold 	}
2892afd8287SJohan Hovold 
2902afd8287SJohan Hovold 	tty_insert_flip_string_fixed_flag(tty, data, tty_flag,
2912afd8287SJohan Hovold 							urb->actual_length);
2922afd8287SJohan Hovold 	tty_flip_buffer_push(tty);
2932afd8287SJohan Hovold 	tty_kref_put(tty);
2942afd8287SJohan Hovold }
2952afd8287SJohan Hovold 
29695da310eSAlan Cox static void belkin_sa_set_termios(struct tty_struct *tty,
29795da310eSAlan Cox 		struct usb_serial_port *port, struct ktermios *old_termios)
2981da177e4SLinus Torvalds {
2991da177e4SLinus Torvalds 	struct usb_serial *serial = port->serial;
3001da177e4SLinus Torvalds 	struct belkin_sa_private *priv = usb_get_serial_port_data(port);
3011da177e4SLinus Torvalds 	unsigned int iflag;
3021da177e4SLinus Torvalds 	unsigned int cflag;
3031da177e4SLinus Torvalds 	unsigned int old_iflag = 0;
3041da177e4SLinus Torvalds 	unsigned int old_cflag = 0;
3051da177e4SLinus Torvalds 	__u16 urb_value = 0; /* Will hold the new flags */
3061da177e4SLinus Torvalds 	unsigned long flags;
3071da177e4SLinus Torvalds 	unsigned long control_state;
3081da177e4SLinus Torvalds 	int bad_flow_control;
3099a8baec7SAlan Cox 	speed_t baud;
31095da310eSAlan Cox 	struct ktermios *termios = tty->termios;
3111da177e4SLinus Torvalds 
3123ec466b4SAlan Cox 	iflag = termios->c_iflag;
3133ec466b4SAlan Cox 	cflag = termios->c_cflag;
3141da177e4SLinus Torvalds 
3153ec466b4SAlan Cox 	termios->c_cflag &= ~CMSPAR;
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	/* get a local copy of the current port settings */
3181da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
3191da177e4SLinus Torvalds 	control_state = priv->control_state;
3201da177e4SLinus Torvalds 	bad_flow_control = priv->bad_flow_control;
3211da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds 	old_iflag = old_termios->c_iflag;
3241da177e4SLinus Torvalds 	old_cflag = old_termios->c_cflag;
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	/* Set the baud rate */
3271da177e4SLinus Torvalds 	if ((cflag & CBAUD) != (old_cflag & CBAUD)) {
3281da177e4SLinus Torvalds 		/* reassert DTR and (maybe) RTS on transition from B0 */
3291da177e4SLinus Torvalds 		if ((old_cflag & CBAUD) == B0) {
3301da177e4SLinus Torvalds 			control_state |= (TIOCM_DTR|TIOCM_RTS);
3311da177e4SLinus Torvalds 			if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, 1) < 0)
332194343d9SGreg Kroah-Hartman 				dev_err(&port->dev, "Set DTR error\n");
3331da177e4SLinus Torvalds 			/* don't set RTS if using hardware flow control */
3341da177e4SLinus Torvalds 			if (!(old_cflag & CRTSCTS))
335b69c1499SAlan Cox 				if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST
336b69c1499SAlan Cox 								, 1) < 0)
337194343d9SGreg Kroah-Hartman 					dev_err(&port->dev, "Set RTS error\n");
3381da177e4SLinus Torvalds 		}
3391da177e4SLinus Torvalds 	}
3409a8baec7SAlan Cox 
34195da310eSAlan Cox 	baud = tty_get_baud_rate(tty);
3423ec466b4SAlan Cox 	if (baud) {
3439a8baec7SAlan Cox 		urb_value = BELKIN_SA_BAUD(baud);
3449a8baec7SAlan Cox 		/* Clip to maximum speed */
3459a8baec7SAlan Cox 		if (urb_value == 0)
3469a8baec7SAlan Cox 			urb_value = 1;
3479a8baec7SAlan Cox 		/* Turn it back into a resulting real baud rate */
3489a8baec7SAlan Cox 		baud = BELKIN_SA_BAUD(urb_value);
3499a8baec7SAlan Cox 
3503ec466b4SAlan Cox 		/* Report the actual baud rate back to the caller */
35195da310eSAlan Cox 		tty_encode_baud_rate(tty, baud, baud);
3521da177e4SLinus Torvalds 		if (BSA_USB_CMD(BELKIN_SA_SET_BAUDRATE_REQUEST, urb_value) < 0)
353194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "Set baudrate error\n");
3541da177e4SLinus Torvalds 	} else {
3551da177e4SLinus Torvalds 		/* Disable flow control */
356b69c1499SAlan Cox 		if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST,
357b69c1499SAlan Cox 						BELKIN_SA_FLOW_NONE) < 0)
358194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "Disable flowcontrol error\n");
3591da177e4SLinus Torvalds 		/* Drop RTS and DTR */
3601da177e4SLinus Torvalds 		control_state &= ~(TIOCM_DTR | TIOCM_RTS);
3611da177e4SLinus Torvalds 		if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, 0) < 0)
362194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "DTR LOW error\n");
3631da177e4SLinus Torvalds 		if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, 0) < 0)
364194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "RTS LOW error\n");
3651da177e4SLinus Torvalds 	}
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	/* set the parity */
368b69c1499SAlan Cox 	if ((cflag ^ old_cflag) & (PARENB | PARODD)) {
3691da177e4SLinus Torvalds 		if (cflag & PARENB)
370b69c1499SAlan Cox 			urb_value = (cflag & PARODD) ?  BELKIN_SA_PARITY_ODD
371b69c1499SAlan Cox 						: BELKIN_SA_PARITY_EVEN;
3721da177e4SLinus Torvalds 		else
3731da177e4SLinus Torvalds 			urb_value = BELKIN_SA_PARITY_NONE;
3741da177e4SLinus Torvalds 		if (BSA_USB_CMD(BELKIN_SA_SET_PARITY_REQUEST, urb_value) < 0)
375194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "Set parity error\n");
3761da177e4SLinus Torvalds 	}
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	/* set the number of data bits */
3791da177e4SLinus Torvalds 	if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
3801da177e4SLinus Torvalds 		switch (cflag & CSIZE) {
381b69c1499SAlan Cox 		case CS5:
382b69c1499SAlan Cox 			urb_value = BELKIN_SA_DATA_BITS(5);
383b69c1499SAlan Cox 			break;
384b69c1499SAlan Cox 		case CS6:
385b69c1499SAlan Cox 			urb_value = BELKIN_SA_DATA_BITS(6);
386b69c1499SAlan Cox 			break;
387b69c1499SAlan Cox 		case CS7:
388b69c1499SAlan Cox 			urb_value = BELKIN_SA_DATA_BITS(7);
389b69c1499SAlan Cox 			break;
390b69c1499SAlan Cox 		case CS8:
391b69c1499SAlan Cox 			urb_value = BELKIN_SA_DATA_BITS(8);
392b69c1499SAlan Cox 			break;
39370f3c758SBen Minerds 		default:
394*c89aa638SGreg Kroah-Hartman 			dev_dbg(&port->dev,
395*c89aa638SGreg Kroah-Hartman 				"CSIZE was not CS5-CS8, using default of 8\n");
3961da177e4SLinus Torvalds 			urb_value = BELKIN_SA_DATA_BITS(8);
3971da177e4SLinus Torvalds 			break;
3981da177e4SLinus Torvalds 		}
3991da177e4SLinus Torvalds 		if (BSA_USB_CMD(BELKIN_SA_SET_DATA_BITS_REQUEST, urb_value) < 0)
400194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "Set data bits error\n");
4011da177e4SLinus Torvalds 	}
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 	/* set the number of stop bits */
4041da177e4SLinus Torvalds 	if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
405b69c1499SAlan Cox 		urb_value = (cflag & CSTOPB) ? BELKIN_SA_STOP_BITS(2)
406b69c1499SAlan Cox 						: BELKIN_SA_STOP_BITS(1);
407b69c1499SAlan Cox 		if (BSA_USB_CMD(BELKIN_SA_SET_STOP_BITS_REQUEST,
408b69c1499SAlan Cox 							urb_value) < 0)
409194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "Set stop bits error\n");
4101da177e4SLinus Torvalds 	}
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	/* Set flow control */
413b69c1499SAlan Cox 	if (((iflag ^ old_iflag) & (IXOFF | IXON)) ||
414b69c1499SAlan Cox 		((cflag ^ old_cflag) & CRTSCTS)) {
4151da177e4SLinus Torvalds 		urb_value = 0;
4161da177e4SLinus Torvalds 		if ((iflag & IXOFF) || (iflag & IXON))
4171da177e4SLinus Torvalds 			urb_value |= (BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
4181da177e4SLinus Torvalds 		else
4191da177e4SLinus Torvalds 			urb_value &= ~(BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 		if (cflag & CRTSCTS)
4221da177e4SLinus Torvalds 			urb_value |=  (BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
4231da177e4SLinus Torvalds 		else
4241da177e4SLinus Torvalds 			urb_value &= ~(BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 		if (bad_flow_control)
4271da177e4SLinus Torvalds 			urb_value &= ~(BELKIN_SA_FLOW_IRTS);
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 		if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST, urb_value) < 0)
430194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "Set flow control error\n");
4311da177e4SLinus Torvalds 	}
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	/* save off the modified port settings */
4341da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
4351da177e4SLinus Torvalds 	priv->control_state = control_state;
4361da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
437726ef428SJohan Hovold }
4381da177e4SLinus Torvalds 
43995da310eSAlan Cox static void belkin_sa_break_ctl(struct tty_struct *tty, int break_state)
4401da177e4SLinus Torvalds {
44195da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
4421da177e4SLinus Torvalds 	struct usb_serial *serial = port->serial;
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 	if (BSA_USB_CMD(BELKIN_SA_SET_BREAK_REQUEST, break_state ? 1 : 0) < 0)
445194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "Set break_ctl %d\n", break_state);
4461da177e4SLinus Torvalds }
4471da177e4SLinus Torvalds 
44860b33c13SAlan Cox static int belkin_sa_tiocmget(struct tty_struct *tty)
4491da177e4SLinus Torvalds {
45095da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
4511da177e4SLinus Torvalds 	struct belkin_sa_private *priv = usb_get_serial_port_data(port);
4521da177e4SLinus Torvalds 	unsigned long control_state;
4531da177e4SLinus Torvalds 	unsigned long flags;
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
4561da177e4SLinus Torvalds 	control_state = priv->control_state;
4571da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 	return control_state;
4601da177e4SLinus Torvalds }
4611da177e4SLinus Torvalds 
46220b9d177SAlan Cox static int belkin_sa_tiocmset(struct tty_struct *tty,
4631da177e4SLinus Torvalds 			       unsigned int set, unsigned int clear)
4641da177e4SLinus Torvalds {
46595da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
4661da177e4SLinus Torvalds 	struct usb_serial *serial = port->serial;
4671da177e4SLinus Torvalds 	struct belkin_sa_private *priv = usb_get_serial_port_data(port);
4681da177e4SLinus Torvalds 	unsigned long control_state;
4691da177e4SLinus Torvalds 	unsigned long flags;
4701da177e4SLinus Torvalds 	int retval;
4711da177e4SLinus Torvalds 	int rts = 0;
4721da177e4SLinus Torvalds 	int dtr = 0;
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
4751da177e4SLinus Torvalds 	control_state = priv->control_state;
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	if (set & TIOCM_RTS) {
4781da177e4SLinus Torvalds 		control_state |= TIOCM_RTS;
4791da177e4SLinus Torvalds 		rts = 1;
4801da177e4SLinus Torvalds 	}
4811da177e4SLinus Torvalds 	if (set & TIOCM_DTR) {
4821da177e4SLinus Torvalds 		control_state |= TIOCM_DTR;
4831da177e4SLinus Torvalds 		dtr = 1;
4841da177e4SLinus Torvalds 	}
4851da177e4SLinus Torvalds 	if (clear & TIOCM_RTS) {
4861da177e4SLinus Torvalds 		control_state &= ~TIOCM_RTS;
4871da177e4SLinus Torvalds 		rts = 0;
4881da177e4SLinus Torvalds 	}
4891da177e4SLinus Torvalds 	if (clear & TIOCM_DTR) {
4901da177e4SLinus Torvalds 		control_state &= ~TIOCM_DTR;
4911da177e4SLinus Torvalds 		dtr = 0;
4921da177e4SLinus Torvalds 	}
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 	priv->control_state = control_state;
4951da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	retval = BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, rts);
4981da177e4SLinus Torvalds 	if (retval < 0) {
499194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "Set RTS error %d\n", retval);
5001da177e4SLinus Torvalds 		goto exit;
5011da177e4SLinus Torvalds 	}
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds 	retval = BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, dtr);
5041da177e4SLinus Torvalds 	if (retval < 0) {
505194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "Set DTR error %d\n", retval);
5061da177e4SLinus Torvalds 		goto exit;
5071da177e4SLinus Torvalds 	}
5081da177e4SLinus Torvalds exit:
5091da177e4SLinus Torvalds 	return retval;
5101da177e4SLinus Torvalds }
5111da177e4SLinus Torvalds 
51268e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table);
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR);
5151da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
5161da177e4SLinus Torvalds MODULE_VERSION(DRIVER_VERSION);
5171da177e4SLinus Torvalds MODULE_LICENSE("GPL");
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds module_param(debug, bool, S_IRUGO | S_IWUSR);
5201da177e4SLinus Torvalds MODULE_PARM_DESC(debug, "Debug enabled or not");
521