15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * Belkin USB Serial Adapter Driver
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Copyright (C) 2000 William Greathouse (wgreathouse@smva.com)
61da177e4SLinus Torvalds * Copyright (C) 2000-2001 Greg Kroah-Hartman (greg@kroah.com)
72afd8287SJohan Hovold * Copyright (C) 2010 Johan Hovold (jhovold@gmail.com)
81da177e4SLinus Torvalds *
91da177e4SLinus Torvalds * This program is largely derived from work by the linux-usb group
101da177e4SLinus Torvalds * and associated source files. Please see the usb/serial files for
111da177e4SLinus Torvalds * individual credits and copyrights.
121da177e4SLinus Torvalds *
13ecefae6dSMauro Carvalho Chehab * See Documentation/usb/usb-serial.rst for more information on using this
14b69c1499SAlan Cox * driver
151da177e4SLinus Torvalds *
161da177e4SLinus Torvalds * TODO:
17cd8c5053SRahul Bedarkar * -- Add true modem control line query capability. Currently we track the
181da177e4SLinus Torvalds * states reported by the interrupt and the states we request.
191da177e4SLinus Torvalds * -- Add support for flush commands
201da177e4SLinus Torvalds */
211da177e4SLinus Torvalds
221da177e4SLinus Torvalds #include <linux/kernel.h>
231da177e4SLinus Torvalds #include <linux/errno.h>
241da177e4SLinus Torvalds #include <linux/slab.h>
251da177e4SLinus Torvalds #include <linux/tty.h>
261da177e4SLinus Torvalds #include <linux/tty_driver.h>
271da177e4SLinus Torvalds #include <linux/tty_flip.h>
281da177e4SLinus Torvalds #include <linux/module.h>
291da177e4SLinus Torvalds #include <linux/spinlock.h>
30b69c1499SAlan Cox #include <linux/uaccess.h>
311da177e4SLinus Torvalds #include <linux/usb.h>
32a969888cSGreg Kroah-Hartman #include <linux/usb/serial.h>
331da177e4SLinus Torvalds #include "belkin_sa.h"
341da177e4SLinus Torvalds
351da177e4SLinus Torvalds #define DRIVER_AUTHOR "William Greathouse <wgreathouse@smva.com>"
361da177e4SLinus Torvalds #define DRIVER_DESC "USB Belkin Serial converter driver"
371da177e4SLinus Torvalds
381da177e4SLinus Torvalds /* function prototypes for a Belkin USB Serial Adapter F5U103 */
39fa919751SJohan Hovold static int belkin_sa_port_probe(struct usb_serial_port *port);
40c5d1448fSUwe Kleine-König static void belkin_sa_port_remove(struct usb_serial_port *port);
41b69c1499SAlan Cox static int belkin_sa_open(struct tty_struct *tty,
42a509a7e4SAlan Cox struct usb_serial_port *port);
43335f8514SAlan Cox static void belkin_sa_close(struct usb_serial_port *port);
447d12e780SDavid Howells static void belkin_sa_read_int_callback(struct urb *urb);
452afd8287SJohan Hovold static void belkin_sa_process_read_urb(struct urb *urb);
46b69c1499SAlan Cox static void belkin_sa_set_termios(struct tty_struct *tty,
47f6d47fe5SIlpo Järvinen struct usb_serial_port *port,
48f6d47fe5SIlpo Järvinen const struct ktermios *old_termios);
49*6ff58ae1SJohan Hovold static int belkin_sa_break_ctl(struct tty_struct *tty, int break_state);
5060b33c13SAlan Cox static int belkin_sa_tiocmget(struct tty_struct *tty);
5120b9d177SAlan Cox static int belkin_sa_tiocmset(struct tty_struct *tty,
52b69c1499SAlan Cox unsigned int set, unsigned int clear);
531da177e4SLinus Torvalds
541da177e4SLinus Torvalds
5568e24113SGreg Kroah-Hartman static const struct usb_device_id id_table[] = {
561da177e4SLinus Torvalds { USB_DEVICE(BELKIN_SA_VID, BELKIN_SA_PID) },
571da177e4SLinus Torvalds { USB_DEVICE(BELKIN_OLD_VID, BELKIN_OLD_PID) },
581da177e4SLinus Torvalds { USB_DEVICE(PERACOM_VID, PERACOM_PID) },
591da177e4SLinus Torvalds { USB_DEVICE(GOHUBS_VID, GOHUBS_PID) },
601da177e4SLinus Torvalds { USB_DEVICE(GOHUBS_VID, HANDYLINK_PID) },
611da177e4SLinus Torvalds { USB_DEVICE(BELKIN_DOCKSTATION_VID, BELKIN_DOCKSTATION_PID) },
621da177e4SLinus Torvalds { } /* Terminating entry */
631da177e4SLinus Torvalds };
6468e24113SGreg Kroah-Hartman MODULE_DEVICE_TABLE(usb, id_table);
651da177e4SLinus Torvalds
661da177e4SLinus Torvalds /* All of the device info needed for the serial converters */
67ea65370dSGreg Kroah-Hartman static struct usb_serial_driver belkin_device = {
6818fcac35SGreg Kroah-Hartman .driver = {
69269bda1cSGreg Kroah-Hartman .name = "belkin",
7018fcac35SGreg Kroah-Hartman },
71269bda1cSGreg Kroah-Hartman .description = "Belkin / Peracom / GoHubs USB Serial Adapter",
7268e24113SGreg Kroah-Hartman .id_table = id_table,
731da177e4SLinus Torvalds .num_ports = 1,
741da177e4SLinus Torvalds .open = belkin_sa_open,
751da177e4SLinus Torvalds .close = belkin_sa_close,
76b69c1499SAlan Cox .read_int_callback = belkin_sa_read_int_callback,
772afd8287SJohan Hovold .process_read_urb = belkin_sa_process_read_urb,
781da177e4SLinus Torvalds .set_termios = belkin_sa_set_termios,
791da177e4SLinus Torvalds .break_ctl = belkin_sa_break_ctl,
801da177e4SLinus Torvalds .tiocmget = belkin_sa_tiocmget,
811da177e4SLinus Torvalds .tiocmset = belkin_sa_tiocmset,
82fa919751SJohan Hovold .port_probe = belkin_sa_port_probe,
83fa919751SJohan Hovold .port_remove = belkin_sa_port_remove,
841da177e4SLinus Torvalds };
851da177e4SLinus Torvalds
8608a4f6bcSAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
8708a4f6bcSAlan Stern &belkin_device, NULL
8808a4f6bcSAlan Stern };
8908a4f6bcSAlan Stern
901da177e4SLinus Torvalds struct belkin_sa_private {
911da177e4SLinus Torvalds spinlock_t lock;
921da177e4SLinus Torvalds unsigned long control_state;
931da177e4SLinus Torvalds unsigned char last_lsr;
941da177e4SLinus Torvalds unsigned char last_msr;
951da177e4SLinus Torvalds int bad_flow_control;
961da177e4SLinus Torvalds };
971da177e4SLinus Torvalds
981da177e4SLinus Torvalds
991da177e4SLinus Torvalds /*
1001da177e4SLinus Torvalds * ***************************************************************************
1011da177e4SLinus Torvalds * Belkin USB Serial Adapter F5U103 specific driver functions
1021da177e4SLinus Torvalds * ***************************************************************************
1031da177e4SLinus Torvalds */
1041da177e4SLinus Torvalds
1051da177e4SLinus Torvalds #define WDR_TIMEOUT 5000 /* default urb timeout */
1061da177e4SLinus Torvalds
1071da177e4SLinus Torvalds /* assumes that struct usb_serial *serial is available */
1081da177e4SLinus Torvalds #define BSA_USB_CMD(c, v) usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), \
1091da177e4SLinus Torvalds (c), BELKIN_SA_SET_REQUEST_TYPE, \
1101da177e4SLinus Torvalds (v), 0, NULL, 0, WDR_TIMEOUT)
1111da177e4SLinus Torvalds
belkin_sa_port_probe(struct usb_serial_port * port)112fa919751SJohan Hovold static int belkin_sa_port_probe(struct usb_serial_port *port)
1131da177e4SLinus Torvalds {
114fa919751SJohan Hovold struct usb_device *dev = port->serial->dev;
1151da177e4SLinus Torvalds struct belkin_sa_private *priv;
1161da177e4SLinus Torvalds
1171da177e4SLinus Torvalds priv = kmalloc(sizeof(struct belkin_sa_private), GFP_KERNEL);
1181da177e4SLinus Torvalds if (!priv)
119fa919751SJohan Hovold return -ENOMEM;
120fa919751SJohan Hovold
1211da177e4SLinus Torvalds spin_lock_init(&priv->lock);
1221da177e4SLinus Torvalds priv->control_state = 0;
1231da177e4SLinus Torvalds priv->last_lsr = 0;
1241da177e4SLinus Torvalds priv->last_msr = 0;
1251da177e4SLinus Torvalds /* see comments at top of file */
126b69c1499SAlan Cox priv->bad_flow_control =
127b69c1499SAlan Cox (le16_to_cpu(dev->descriptor.bcdDevice) <= 0x0206) ? 1 : 0;
128c197a8dbSGreg Kroah-Hartman dev_info(&dev->dev, "bcdDevice: %04x, bfc: %d\n",
129b69c1499SAlan Cox le16_to_cpu(dev->descriptor.bcdDevice),
130b69c1499SAlan Cox priv->bad_flow_control);
1311da177e4SLinus Torvalds
132fa919751SJohan Hovold usb_set_serial_port_data(port, priv);
1331da177e4SLinus Torvalds
134b69c1499SAlan Cox return 0;
1351da177e4SLinus Torvalds }
1361da177e4SLinus Torvalds
belkin_sa_port_remove(struct usb_serial_port * port)137c5d1448fSUwe Kleine-König static void belkin_sa_port_remove(struct usb_serial_port *port)
1381da177e4SLinus Torvalds {
139fa919751SJohan Hovold struct belkin_sa_private *priv;
1401da177e4SLinus Torvalds
141fa919751SJohan Hovold priv = usb_get_serial_port_data(port);
142fa919751SJohan Hovold kfree(priv);
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds
belkin_sa_open(struct tty_struct * tty,struct usb_serial_port * port)145b69c1499SAlan Cox static int belkin_sa_open(struct tty_struct *tty,
146a509a7e4SAlan Cox struct usb_serial_port *port)
1471da177e4SLinus Torvalds {
148f2f8b7feSJohan Hovold int retval;
1491da177e4SLinus Torvalds
1501da177e4SLinus Torvalds retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
1511da177e4SLinus Torvalds if (retval) {
152194343d9SGreg Kroah-Hartman dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
153f2f8b7feSJohan Hovold return retval;
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds
156f2f8b7feSJohan Hovold retval = usb_serial_generic_open(tty, port);
157f2f8b7feSJohan Hovold if (retval)
158f2f8b7feSJohan Hovold usb_kill_urb(port->interrupt_in_urb);
159f2f8b7feSJohan Hovold
1601da177e4SLinus Torvalds return retval;
161726ef428SJohan Hovold }
1621da177e4SLinus Torvalds
belkin_sa_close(struct usb_serial_port * port)163335f8514SAlan Cox static void belkin_sa_close(struct usb_serial_port *port)
1641da177e4SLinus Torvalds {
165f26788daSJohan Hovold usb_serial_generic_close(port);
1661da177e4SLinus Torvalds usb_kill_urb(port->interrupt_in_urb);
167726ef428SJohan Hovold }
1681da177e4SLinus Torvalds
belkin_sa_read_int_callback(struct urb * urb)1697d12e780SDavid Howells static void belkin_sa_read_int_callback(struct urb *urb)
1701da177e4SLinus Torvalds {
171cdc97792SMing Lei struct usb_serial_port *port = urb->context;
1721da177e4SLinus Torvalds struct belkin_sa_private *priv;
1731da177e4SLinus Torvalds unsigned char *data = urb->transfer_buffer;
1741da177e4SLinus Torvalds int retval;
175f26aad25SGreg Kroah-Hartman int status = urb->status;
1761da177e4SLinus Torvalds unsigned long flags;
1771da177e4SLinus Torvalds
178f26aad25SGreg Kroah-Hartman switch (status) {
1791da177e4SLinus Torvalds case 0:
1801da177e4SLinus Torvalds /* success */
1811da177e4SLinus Torvalds break;
1821da177e4SLinus Torvalds case -ECONNRESET:
1831da177e4SLinus Torvalds case -ENOENT:
1841da177e4SLinus Torvalds case -ESHUTDOWN:
1851da177e4SLinus Torvalds /* this urb is terminated, clean up */
186c89aa638SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
187441b62c1SHarvey Harrison __func__, status);
1881da177e4SLinus Torvalds return;
1891da177e4SLinus Torvalds default:
190c89aa638SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
191441b62c1SHarvey Harrison __func__, status);
1921da177e4SLinus Torvalds goto exit;
1931da177e4SLinus Torvalds }
1941da177e4SLinus Torvalds
19559d33f2fSGreg Kroah-Hartman usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
1961da177e4SLinus Torvalds
1971da177e4SLinus Torvalds /* Handle known interrupt data */
1981da177e4SLinus Torvalds /* ignore data[0] and data[1] */
1991da177e4SLinus Torvalds
2001da177e4SLinus Torvalds priv = usb_get_serial_port_data(port);
2011da177e4SLinus Torvalds spin_lock_irqsave(&priv->lock, flags);
2021da177e4SLinus Torvalds priv->last_msr = data[BELKIN_SA_MSR_INDEX];
2031da177e4SLinus Torvalds
2041da177e4SLinus Torvalds /* Record Control Line states */
2051da177e4SLinus Torvalds if (priv->last_msr & BELKIN_SA_MSR_DSR)
2061da177e4SLinus Torvalds priv->control_state |= TIOCM_DSR;
2071da177e4SLinus Torvalds else
2081da177e4SLinus Torvalds priv->control_state &= ~TIOCM_DSR;
2091da177e4SLinus Torvalds
2101da177e4SLinus Torvalds if (priv->last_msr & BELKIN_SA_MSR_CTS)
2111da177e4SLinus Torvalds priv->control_state |= TIOCM_CTS;
2121da177e4SLinus Torvalds else
2131da177e4SLinus Torvalds priv->control_state &= ~TIOCM_CTS;
2141da177e4SLinus Torvalds
2151da177e4SLinus Torvalds if (priv->last_msr & BELKIN_SA_MSR_RI)
2161da177e4SLinus Torvalds priv->control_state |= TIOCM_RI;
2171da177e4SLinus Torvalds else
2181da177e4SLinus Torvalds priv->control_state &= ~TIOCM_RI;
2191da177e4SLinus Torvalds
2201da177e4SLinus Torvalds if (priv->last_msr & BELKIN_SA_MSR_CD)
2211da177e4SLinus Torvalds priv->control_state |= TIOCM_CD;
2221da177e4SLinus Torvalds else
2231da177e4SLinus Torvalds priv->control_state &= ~TIOCM_CD;
2241da177e4SLinus Torvalds
2251da177e4SLinus Torvalds priv->last_lsr = data[BELKIN_SA_LSR_INDEX];
2261da177e4SLinus Torvalds spin_unlock_irqrestore(&priv->lock, flags);
2271da177e4SLinus Torvalds exit:
2281da177e4SLinus Torvalds retval = usb_submit_urb(urb, GFP_ATOMIC);
2291da177e4SLinus Torvalds if (retval)
230194343d9SGreg Kroah-Hartman dev_err(&port->dev, "%s - usb_submit_urb failed with "
231194343d9SGreg Kroah-Hartman "result %d\n", __func__, retval);
2321da177e4SLinus Torvalds }
2331da177e4SLinus Torvalds
belkin_sa_process_read_urb(struct urb * urb)2342afd8287SJohan Hovold static void belkin_sa_process_read_urb(struct urb *urb)
2352afd8287SJohan Hovold {
2362afd8287SJohan Hovold struct usb_serial_port *port = urb->context;
2372afd8287SJohan Hovold struct belkin_sa_private *priv = usb_get_serial_port_data(port);
2382afd8287SJohan Hovold unsigned char *data = urb->transfer_buffer;
2392afd8287SJohan Hovold unsigned long flags;
2402afd8287SJohan Hovold unsigned char status;
2412afd8287SJohan Hovold char tty_flag;
2422afd8287SJohan Hovold
2432afd8287SJohan Hovold /* Update line status */
2442afd8287SJohan Hovold tty_flag = TTY_NORMAL;
2452afd8287SJohan Hovold
2462afd8287SJohan Hovold spin_lock_irqsave(&priv->lock, flags);
2472afd8287SJohan Hovold status = priv->last_lsr;
2482afd8287SJohan Hovold priv->last_lsr &= ~BELKIN_SA_LSR_ERR;
2492afd8287SJohan Hovold spin_unlock_irqrestore(&priv->lock, flags);
2502afd8287SJohan Hovold
2512afd8287SJohan Hovold if (!urb->actual_length)
2522afd8287SJohan Hovold return;
2532afd8287SJohan Hovold
2542afd8287SJohan Hovold if (status & BELKIN_SA_LSR_ERR) {
2552afd8287SJohan Hovold /* Break takes precedence over parity, which takes precedence
2562afd8287SJohan Hovold * over framing errors. */
2572afd8287SJohan Hovold if (status & BELKIN_SA_LSR_BI)
2582afd8287SJohan Hovold tty_flag = TTY_BREAK;
2592afd8287SJohan Hovold else if (status & BELKIN_SA_LSR_PE)
2602afd8287SJohan Hovold tty_flag = TTY_PARITY;
2612afd8287SJohan Hovold else if (status & BELKIN_SA_LSR_FE)
2622afd8287SJohan Hovold tty_flag = TTY_FRAME;
2632afd8287SJohan Hovold dev_dbg(&port->dev, "tty_flag = %d\n", tty_flag);
2642afd8287SJohan Hovold
2652afd8287SJohan Hovold /* Overrun is special, not associated with a char. */
2662afd8287SJohan Hovold if (status & BELKIN_SA_LSR_OE)
26792a19f9cSJiri Slaby tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
2682afd8287SJohan Hovold }
2692afd8287SJohan Hovold
2702f693357SJiri Slaby tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
2712afd8287SJohan Hovold urb->actual_length);
2722e124b4aSJiri Slaby tty_flip_buffer_push(&port->port);
2732afd8287SJohan Hovold }
2742afd8287SJohan Hovold
belkin_sa_set_termios(struct tty_struct * tty,struct usb_serial_port * port,const struct ktermios * old_termios)27595da310eSAlan Cox static void belkin_sa_set_termios(struct tty_struct *tty,
276f6d47fe5SIlpo Järvinen struct usb_serial_port *port,
277f6d47fe5SIlpo Järvinen const struct ktermios *old_termios)
2781da177e4SLinus Torvalds {
2791da177e4SLinus Torvalds struct usb_serial *serial = port->serial;
2801da177e4SLinus Torvalds struct belkin_sa_private *priv = usb_get_serial_port_data(port);
2811da177e4SLinus Torvalds unsigned int iflag;
2821da177e4SLinus Torvalds unsigned int cflag;
2831da177e4SLinus Torvalds unsigned int old_iflag = 0;
2841da177e4SLinus Torvalds unsigned int old_cflag = 0;
2851da177e4SLinus Torvalds __u16 urb_value = 0; /* Will hold the new flags */
2861da177e4SLinus Torvalds unsigned long flags;
2871da177e4SLinus Torvalds unsigned long control_state;
2881da177e4SLinus Torvalds int bad_flow_control;
2899a8baec7SAlan Cox speed_t baud;
290adc8d746SAlan Cox struct ktermios *termios = &tty->termios;
2911da177e4SLinus Torvalds
2923ec466b4SAlan Cox iflag = termios->c_iflag;
2933ec466b4SAlan Cox cflag = termios->c_cflag;
2941da177e4SLinus Torvalds
2953ec466b4SAlan Cox termios->c_cflag &= ~CMSPAR;
2961da177e4SLinus Torvalds
2971da177e4SLinus Torvalds /* get a local copy of the current port settings */
2981da177e4SLinus Torvalds spin_lock_irqsave(&priv->lock, flags);
2991da177e4SLinus Torvalds control_state = priv->control_state;
3001da177e4SLinus Torvalds bad_flow_control = priv->bad_flow_control;
3011da177e4SLinus Torvalds spin_unlock_irqrestore(&priv->lock, flags);
3021da177e4SLinus Torvalds
3031da177e4SLinus Torvalds old_iflag = old_termios->c_iflag;
3041da177e4SLinus Torvalds old_cflag = old_termios->c_cflag;
3051da177e4SLinus Torvalds
3061da177e4SLinus Torvalds /* Set the baud rate */
3071da177e4SLinus Torvalds if ((cflag & CBAUD) != (old_cflag & CBAUD)) {
3081da177e4SLinus Torvalds /* reassert DTR and (maybe) RTS on transition from B0 */
3091da177e4SLinus Torvalds if ((old_cflag & CBAUD) == B0) {
3101da177e4SLinus Torvalds control_state |= (TIOCM_DTR|TIOCM_RTS);
3111da177e4SLinus Torvalds if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, 1) < 0)
312194343d9SGreg Kroah-Hartman dev_err(&port->dev, "Set DTR error\n");
3131da177e4SLinus Torvalds /* don't set RTS if using hardware flow control */
3141da177e4SLinus Torvalds if (!(old_cflag & CRTSCTS))
315b69c1499SAlan Cox if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST
316b69c1499SAlan Cox , 1) < 0)
317194343d9SGreg Kroah-Hartman dev_err(&port->dev, "Set RTS error\n");
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds }
3209a8baec7SAlan Cox
32195da310eSAlan Cox baud = tty_get_baud_rate(tty);
3223ec466b4SAlan Cox if (baud) {
3239a8baec7SAlan Cox urb_value = BELKIN_SA_BAUD(baud);
3249a8baec7SAlan Cox /* Clip to maximum speed */
3259a8baec7SAlan Cox if (urb_value == 0)
3269a8baec7SAlan Cox urb_value = 1;
3279a8baec7SAlan Cox /* Turn it back into a resulting real baud rate */
3289a8baec7SAlan Cox baud = BELKIN_SA_BAUD(urb_value);
3299a8baec7SAlan Cox
3303ec466b4SAlan Cox /* Report the actual baud rate back to the caller */
33195da310eSAlan Cox tty_encode_baud_rate(tty, baud, baud);
3321da177e4SLinus Torvalds if (BSA_USB_CMD(BELKIN_SA_SET_BAUDRATE_REQUEST, urb_value) < 0)
333194343d9SGreg Kroah-Hartman dev_err(&port->dev, "Set baudrate error\n");
3341da177e4SLinus Torvalds } else {
3351da177e4SLinus Torvalds /* Disable flow control */
336b69c1499SAlan Cox if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST,
337b69c1499SAlan Cox BELKIN_SA_FLOW_NONE) < 0)
338194343d9SGreg Kroah-Hartman dev_err(&port->dev, "Disable flowcontrol error\n");
3391da177e4SLinus Torvalds /* Drop RTS and DTR */
3401da177e4SLinus Torvalds control_state &= ~(TIOCM_DTR | TIOCM_RTS);
3411da177e4SLinus Torvalds if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, 0) < 0)
342194343d9SGreg Kroah-Hartman dev_err(&port->dev, "DTR LOW error\n");
3431da177e4SLinus Torvalds if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, 0) < 0)
344194343d9SGreg Kroah-Hartman dev_err(&port->dev, "RTS LOW error\n");
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds
3471da177e4SLinus Torvalds /* set the parity */
348b69c1499SAlan Cox if ((cflag ^ old_cflag) & (PARENB | PARODD)) {
3491da177e4SLinus Torvalds if (cflag & PARENB)
350b69c1499SAlan Cox urb_value = (cflag & PARODD) ? BELKIN_SA_PARITY_ODD
351b69c1499SAlan Cox : BELKIN_SA_PARITY_EVEN;
3521da177e4SLinus Torvalds else
3531da177e4SLinus Torvalds urb_value = BELKIN_SA_PARITY_NONE;
3541da177e4SLinus Torvalds if (BSA_USB_CMD(BELKIN_SA_SET_PARITY_REQUEST, urb_value) < 0)
355194343d9SGreg Kroah-Hartman dev_err(&port->dev, "Set parity error\n");
3561da177e4SLinus Torvalds }
3571da177e4SLinus Torvalds
3581da177e4SLinus Torvalds /* set the number of data bits */
3591da177e4SLinus Torvalds if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
3603ec2ff37SJiri Slaby urb_value = BELKIN_SA_DATA_BITS(tty_get_char_size(cflag));
3611da177e4SLinus Torvalds if (BSA_USB_CMD(BELKIN_SA_SET_DATA_BITS_REQUEST, urb_value) < 0)
362194343d9SGreg Kroah-Hartman dev_err(&port->dev, "Set data bits error\n");
3631da177e4SLinus Torvalds }
3641da177e4SLinus Torvalds
3651da177e4SLinus Torvalds /* set the number of stop bits */
3661da177e4SLinus Torvalds if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
367b69c1499SAlan Cox urb_value = (cflag & CSTOPB) ? BELKIN_SA_STOP_BITS(2)
368b69c1499SAlan Cox : BELKIN_SA_STOP_BITS(1);
369b69c1499SAlan Cox if (BSA_USB_CMD(BELKIN_SA_SET_STOP_BITS_REQUEST,
370b69c1499SAlan Cox urb_value) < 0)
371194343d9SGreg Kroah-Hartman dev_err(&port->dev, "Set stop bits error\n");
3721da177e4SLinus Torvalds }
3731da177e4SLinus Torvalds
3741da177e4SLinus Torvalds /* Set flow control */
375b69c1499SAlan Cox if (((iflag ^ old_iflag) & (IXOFF | IXON)) ||
376b69c1499SAlan Cox ((cflag ^ old_cflag) & CRTSCTS)) {
3771da177e4SLinus Torvalds urb_value = 0;
3781da177e4SLinus Torvalds if ((iflag & IXOFF) || (iflag & IXON))
3791da177e4SLinus Torvalds urb_value |= (BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
3801da177e4SLinus Torvalds else
3811da177e4SLinus Torvalds urb_value &= ~(BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
3821da177e4SLinus Torvalds
3831da177e4SLinus Torvalds if (cflag & CRTSCTS)
3841da177e4SLinus Torvalds urb_value |= (BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
3851da177e4SLinus Torvalds else
3861da177e4SLinus Torvalds urb_value &= ~(BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
3871da177e4SLinus Torvalds
3881da177e4SLinus Torvalds if (bad_flow_control)
3891da177e4SLinus Torvalds urb_value &= ~(BELKIN_SA_FLOW_IRTS);
3901da177e4SLinus Torvalds
3911da177e4SLinus Torvalds if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST, urb_value) < 0)
392194343d9SGreg Kroah-Hartman dev_err(&port->dev, "Set flow control error\n");
3931da177e4SLinus Torvalds }
3941da177e4SLinus Torvalds
3951da177e4SLinus Torvalds /* save off the modified port settings */
3961da177e4SLinus Torvalds spin_lock_irqsave(&priv->lock, flags);
3971da177e4SLinus Torvalds priv->control_state = control_state;
3981da177e4SLinus Torvalds spin_unlock_irqrestore(&priv->lock, flags);
399726ef428SJohan Hovold }
4001da177e4SLinus Torvalds
belkin_sa_break_ctl(struct tty_struct * tty,int break_state)401*6ff58ae1SJohan Hovold static int belkin_sa_break_ctl(struct tty_struct *tty, int break_state)
4021da177e4SLinus Torvalds {
40395da310eSAlan Cox struct usb_serial_port *port = tty->driver_data;
4041da177e4SLinus Torvalds struct usb_serial *serial = port->serial;
405*6ff58ae1SJohan Hovold int ret;
4061da177e4SLinus Torvalds
407*6ff58ae1SJohan Hovold ret = BSA_USB_CMD(BELKIN_SA_SET_BREAK_REQUEST, break_state ? 1 : 0);
408*6ff58ae1SJohan Hovold if (ret < 0) {
409194343d9SGreg Kroah-Hartman dev_err(&port->dev, "Set break_ctl %d\n", break_state);
410*6ff58ae1SJohan Hovold return ret;
411*6ff58ae1SJohan Hovold }
412*6ff58ae1SJohan Hovold
413*6ff58ae1SJohan Hovold return 0;
4141da177e4SLinus Torvalds }
4151da177e4SLinus Torvalds
belkin_sa_tiocmget(struct tty_struct * tty)41660b33c13SAlan Cox static int belkin_sa_tiocmget(struct tty_struct *tty)
4171da177e4SLinus Torvalds {
41895da310eSAlan Cox struct usb_serial_port *port = tty->driver_data;
4191da177e4SLinus Torvalds struct belkin_sa_private *priv = usb_get_serial_port_data(port);
4201da177e4SLinus Torvalds unsigned long control_state;
4211da177e4SLinus Torvalds unsigned long flags;
4221da177e4SLinus Torvalds
4231da177e4SLinus Torvalds spin_lock_irqsave(&priv->lock, flags);
4241da177e4SLinus Torvalds control_state = priv->control_state;
4251da177e4SLinus Torvalds spin_unlock_irqrestore(&priv->lock, flags);
4261da177e4SLinus Torvalds
4271da177e4SLinus Torvalds return control_state;
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds
belkin_sa_tiocmset(struct tty_struct * tty,unsigned int set,unsigned int clear)43020b9d177SAlan Cox static int belkin_sa_tiocmset(struct tty_struct *tty,
4311da177e4SLinus Torvalds unsigned int set, unsigned int clear)
4321da177e4SLinus Torvalds {
43395da310eSAlan Cox struct usb_serial_port *port = tty->driver_data;
4341da177e4SLinus Torvalds struct usb_serial *serial = port->serial;
4351da177e4SLinus Torvalds struct belkin_sa_private *priv = usb_get_serial_port_data(port);
4361da177e4SLinus Torvalds unsigned long control_state;
4371da177e4SLinus Torvalds unsigned long flags;
4381da177e4SLinus Torvalds int retval;
4391da177e4SLinus Torvalds int rts = 0;
4401da177e4SLinus Torvalds int dtr = 0;
4411da177e4SLinus Torvalds
4421da177e4SLinus Torvalds spin_lock_irqsave(&priv->lock, flags);
4431da177e4SLinus Torvalds control_state = priv->control_state;
4441da177e4SLinus Torvalds
4451da177e4SLinus Torvalds if (set & TIOCM_RTS) {
4461da177e4SLinus Torvalds control_state |= TIOCM_RTS;
4471da177e4SLinus Torvalds rts = 1;
4481da177e4SLinus Torvalds }
4491da177e4SLinus Torvalds if (set & TIOCM_DTR) {
4501da177e4SLinus Torvalds control_state |= TIOCM_DTR;
4511da177e4SLinus Torvalds dtr = 1;
4521da177e4SLinus Torvalds }
4531da177e4SLinus Torvalds if (clear & TIOCM_RTS) {
4541da177e4SLinus Torvalds control_state &= ~TIOCM_RTS;
4551da177e4SLinus Torvalds rts = 0;
4561da177e4SLinus Torvalds }
4571da177e4SLinus Torvalds if (clear & TIOCM_DTR) {
4581da177e4SLinus Torvalds control_state &= ~TIOCM_DTR;
4591da177e4SLinus Torvalds dtr = 0;
4601da177e4SLinus Torvalds }
4611da177e4SLinus Torvalds
4621da177e4SLinus Torvalds priv->control_state = control_state;
4631da177e4SLinus Torvalds spin_unlock_irqrestore(&priv->lock, flags);
4641da177e4SLinus Torvalds
4651da177e4SLinus Torvalds retval = BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, rts);
4661da177e4SLinus Torvalds if (retval < 0) {
467194343d9SGreg Kroah-Hartman dev_err(&port->dev, "Set RTS error %d\n", retval);
4681da177e4SLinus Torvalds goto exit;
4691da177e4SLinus Torvalds }
4701da177e4SLinus Torvalds
4711da177e4SLinus Torvalds retval = BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, dtr);
4721da177e4SLinus Torvalds if (retval < 0) {
473194343d9SGreg Kroah-Hartman dev_err(&port->dev, "Set DTR error %d\n", retval);
4741da177e4SLinus Torvalds goto exit;
4751da177e4SLinus Torvalds }
4761da177e4SLinus Torvalds exit:
4771da177e4SLinus Torvalds return retval;
4781da177e4SLinus Torvalds }
4791da177e4SLinus Torvalds
48068e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table);
4811da177e4SLinus Torvalds
4821da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR);
4831da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
4841da177e4SLinus Torvalds MODULE_LICENSE("GPL");
485