0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #include <linux/kernel.h>
0023 #include <linux/errno.h>
0024 #include <linux/slab.h>
0025 #include <linux/tty.h>
0026 #include <linux/tty_driver.h>
0027 #include <linux/tty_flip.h>
0028 #include <linux/module.h>
0029 #include <linux/spinlock.h>
0030 #include <linux/uaccess.h>
0031 #include <linux/usb.h>
0032 #include <linux/usb/serial.h>
0033 #include "belkin_sa.h"
0034
0035 #define DRIVER_AUTHOR "William Greathouse <wgreathouse@smva.com>"
0036 #define DRIVER_DESC "USB Belkin Serial converter driver"
0037
0038
0039 static int belkin_sa_port_probe(struct usb_serial_port *port);
0040 static void belkin_sa_port_remove(struct usb_serial_port *port);
0041 static int belkin_sa_open(struct tty_struct *tty,
0042 struct usb_serial_port *port);
0043 static void belkin_sa_close(struct usb_serial_port *port);
0044 static void belkin_sa_read_int_callback(struct urb *urb);
0045 static void belkin_sa_process_read_urb(struct urb *urb);
0046 static void belkin_sa_set_termios(struct tty_struct *tty,
0047 struct usb_serial_port *port, struct ktermios * old);
0048 static void belkin_sa_break_ctl(struct tty_struct *tty, int break_state);
0049 static int belkin_sa_tiocmget(struct tty_struct *tty);
0050 static int belkin_sa_tiocmset(struct tty_struct *tty,
0051 unsigned int set, unsigned int clear);
0052
0053
0054 static const struct usb_device_id id_table[] = {
0055 { USB_DEVICE(BELKIN_SA_VID, BELKIN_SA_PID) },
0056 { USB_DEVICE(BELKIN_OLD_VID, BELKIN_OLD_PID) },
0057 { USB_DEVICE(PERACOM_VID, PERACOM_PID) },
0058 { USB_DEVICE(GOHUBS_VID, GOHUBS_PID) },
0059 { USB_DEVICE(GOHUBS_VID, HANDYLINK_PID) },
0060 { USB_DEVICE(BELKIN_DOCKSTATION_VID, BELKIN_DOCKSTATION_PID) },
0061 { }
0062 };
0063 MODULE_DEVICE_TABLE(usb, id_table);
0064
0065
0066 static struct usb_serial_driver belkin_device = {
0067 .driver = {
0068 .owner = THIS_MODULE,
0069 .name = "belkin",
0070 },
0071 .description = "Belkin / Peracom / GoHubs USB Serial Adapter",
0072 .id_table = id_table,
0073 .num_ports = 1,
0074 .open = belkin_sa_open,
0075 .close = belkin_sa_close,
0076 .read_int_callback = belkin_sa_read_int_callback,
0077 .process_read_urb = belkin_sa_process_read_urb,
0078 .set_termios = belkin_sa_set_termios,
0079 .break_ctl = belkin_sa_break_ctl,
0080 .tiocmget = belkin_sa_tiocmget,
0081 .tiocmset = belkin_sa_tiocmset,
0082 .port_probe = belkin_sa_port_probe,
0083 .port_remove = belkin_sa_port_remove,
0084 };
0085
0086 static struct usb_serial_driver * const serial_drivers[] = {
0087 &belkin_device, NULL
0088 };
0089
0090 struct belkin_sa_private {
0091 spinlock_t lock;
0092 unsigned long control_state;
0093 unsigned char last_lsr;
0094 unsigned char last_msr;
0095 int bad_flow_control;
0096 };
0097
0098
0099
0100
0101
0102
0103
0104
0105 #define WDR_TIMEOUT 5000
0106
0107
0108 #define BSA_USB_CMD(c, v) usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), \
0109 (c), BELKIN_SA_SET_REQUEST_TYPE, \
0110 (v), 0, NULL, 0, WDR_TIMEOUT)
0111
0112 static int belkin_sa_port_probe(struct usb_serial_port *port)
0113 {
0114 struct usb_device *dev = port->serial->dev;
0115 struct belkin_sa_private *priv;
0116
0117 priv = kmalloc(sizeof(struct belkin_sa_private), GFP_KERNEL);
0118 if (!priv)
0119 return -ENOMEM;
0120
0121 spin_lock_init(&priv->lock);
0122 priv->control_state = 0;
0123 priv->last_lsr = 0;
0124 priv->last_msr = 0;
0125
0126 priv->bad_flow_control =
0127 (le16_to_cpu(dev->descriptor.bcdDevice) <= 0x0206) ? 1 : 0;
0128 dev_info(&dev->dev, "bcdDevice: %04x, bfc: %d\n",
0129 le16_to_cpu(dev->descriptor.bcdDevice),
0130 priv->bad_flow_control);
0131
0132 usb_set_serial_port_data(port, priv);
0133
0134 return 0;
0135 }
0136
0137 static void belkin_sa_port_remove(struct usb_serial_port *port)
0138 {
0139 struct belkin_sa_private *priv;
0140
0141 priv = usb_get_serial_port_data(port);
0142 kfree(priv);
0143 }
0144
0145 static int belkin_sa_open(struct tty_struct *tty,
0146 struct usb_serial_port *port)
0147 {
0148 int retval;
0149
0150 retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
0151 if (retval) {
0152 dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
0153 return retval;
0154 }
0155
0156 retval = usb_serial_generic_open(tty, port);
0157 if (retval)
0158 usb_kill_urb(port->interrupt_in_urb);
0159
0160 return retval;
0161 }
0162
0163 static void belkin_sa_close(struct usb_serial_port *port)
0164 {
0165 usb_serial_generic_close(port);
0166 usb_kill_urb(port->interrupt_in_urb);
0167 }
0168
0169 static void belkin_sa_read_int_callback(struct urb *urb)
0170 {
0171 struct usb_serial_port *port = urb->context;
0172 struct belkin_sa_private *priv;
0173 unsigned char *data = urb->transfer_buffer;
0174 int retval;
0175 int status = urb->status;
0176 unsigned long flags;
0177
0178 switch (status) {
0179 case 0:
0180
0181 break;
0182 case -ECONNRESET:
0183 case -ENOENT:
0184 case -ESHUTDOWN:
0185
0186 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
0187 __func__, status);
0188 return;
0189 default:
0190 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
0191 __func__, status);
0192 goto exit;
0193 }
0194
0195 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
0196
0197
0198
0199
0200 priv = usb_get_serial_port_data(port);
0201 spin_lock_irqsave(&priv->lock, flags);
0202 priv->last_msr = data[BELKIN_SA_MSR_INDEX];
0203
0204
0205 if (priv->last_msr & BELKIN_SA_MSR_DSR)
0206 priv->control_state |= TIOCM_DSR;
0207 else
0208 priv->control_state &= ~TIOCM_DSR;
0209
0210 if (priv->last_msr & BELKIN_SA_MSR_CTS)
0211 priv->control_state |= TIOCM_CTS;
0212 else
0213 priv->control_state &= ~TIOCM_CTS;
0214
0215 if (priv->last_msr & BELKIN_SA_MSR_RI)
0216 priv->control_state |= TIOCM_RI;
0217 else
0218 priv->control_state &= ~TIOCM_RI;
0219
0220 if (priv->last_msr & BELKIN_SA_MSR_CD)
0221 priv->control_state |= TIOCM_CD;
0222 else
0223 priv->control_state &= ~TIOCM_CD;
0224
0225 priv->last_lsr = data[BELKIN_SA_LSR_INDEX];
0226 spin_unlock_irqrestore(&priv->lock, flags);
0227 exit:
0228 retval = usb_submit_urb(urb, GFP_ATOMIC);
0229 if (retval)
0230 dev_err(&port->dev, "%s - usb_submit_urb failed with "
0231 "result %d\n", __func__, retval);
0232 }
0233
0234 static void belkin_sa_process_read_urb(struct urb *urb)
0235 {
0236 struct usb_serial_port *port = urb->context;
0237 struct belkin_sa_private *priv = usb_get_serial_port_data(port);
0238 unsigned char *data = urb->transfer_buffer;
0239 unsigned long flags;
0240 unsigned char status;
0241 char tty_flag;
0242
0243
0244 tty_flag = TTY_NORMAL;
0245
0246 spin_lock_irqsave(&priv->lock, flags);
0247 status = priv->last_lsr;
0248 priv->last_lsr &= ~BELKIN_SA_LSR_ERR;
0249 spin_unlock_irqrestore(&priv->lock, flags);
0250
0251 if (!urb->actual_length)
0252 return;
0253
0254 if (status & BELKIN_SA_LSR_ERR) {
0255
0256
0257 if (status & BELKIN_SA_LSR_BI)
0258 tty_flag = TTY_BREAK;
0259 else if (status & BELKIN_SA_LSR_PE)
0260 tty_flag = TTY_PARITY;
0261 else if (status & BELKIN_SA_LSR_FE)
0262 tty_flag = TTY_FRAME;
0263 dev_dbg(&port->dev, "tty_flag = %d\n", tty_flag);
0264
0265
0266 if (status & BELKIN_SA_LSR_OE)
0267 tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
0268 }
0269
0270 tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
0271 urb->actual_length);
0272 tty_flip_buffer_push(&port->port);
0273 }
0274
0275 static void belkin_sa_set_termios(struct tty_struct *tty,
0276 struct usb_serial_port *port, struct ktermios *old_termios)
0277 {
0278 struct usb_serial *serial = port->serial;
0279 struct belkin_sa_private *priv = usb_get_serial_port_data(port);
0280 unsigned int iflag;
0281 unsigned int cflag;
0282 unsigned int old_iflag = 0;
0283 unsigned int old_cflag = 0;
0284 __u16 urb_value = 0;
0285 unsigned long flags;
0286 unsigned long control_state;
0287 int bad_flow_control;
0288 speed_t baud;
0289 struct ktermios *termios = &tty->termios;
0290
0291 iflag = termios->c_iflag;
0292 cflag = termios->c_cflag;
0293
0294 termios->c_cflag &= ~CMSPAR;
0295
0296
0297 spin_lock_irqsave(&priv->lock, flags);
0298 control_state = priv->control_state;
0299 bad_flow_control = priv->bad_flow_control;
0300 spin_unlock_irqrestore(&priv->lock, flags);
0301
0302 old_iflag = old_termios->c_iflag;
0303 old_cflag = old_termios->c_cflag;
0304
0305
0306 if ((cflag & CBAUD) != (old_cflag & CBAUD)) {
0307
0308 if ((old_cflag & CBAUD) == B0) {
0309 control_state |= (TIOCM_DTR|TIOCM_RTS);
0310 if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, 1) < 0)
0311 dev_err(&port->dev, "Set DTR error\n");
0312
0313 if (!(old_cflag & CRTSCTS))
0314 if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST
0315 , 1) < 0)
0316 dev_err(&port->dev, "Set RTS error\n");
0317 }
0318 }
0319
0320 baud = tty_get_baud_rate(tty);
0321 if (baud) {
0322 urb_value = BELKIN_SA_BAUD(baud);
0323
0324 if (urb_value == 0)
0325 urb_value = 1;
0326
0327 baud = BELKIN_SA_BAUD(urb_value);
0328
0329
0330 tty_encode_baud_rate(tty, baud, baud);
0331 if (BSA_USB_CMD(BELKIN_SA_SET_BAUDRATE_REQUEST, urb_value) < 0)
0332 dev_err(&port->dev, "Set baudrate error\n");
0333 } else {
0334
0335 if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST,
0336 BELKIN_SA_FLOW_NONE) < 0)
0337 dev_err(&port->dev, "Disable flowcontrol error\n");
0338
0339 control_state &= ~(TIOCM_DTR | TIOCM_RTS);
0340 if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, 0) < 0)
0341 dev_err(&port->dev, "DTR LOW error\n");
0342 if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, 0) < 0)
0343 dev_err(&port->dev, "RTS LOW error\n");
0344 }
0345
0346
0347 if ((cflag ^ old_cflag) & (PARENB | PARODD)) {
0348 if (cflag & PARENB)
0349 urb_value = (cflag & PARODD) ? BELKIN_SA_PARITY_ODD
0350 : BELKIN_SA_PARITY_EVEN;
0351 else
0352 urb_value = BELKIN_SA_PARITY_NONE;
0353 if (BSA_USB_CMD(BELKIN_SA_SET_PARITY_REQUEST, urb_value) < 0)
0354 dev_err(&port->dev, "Set parity error\n");
0355 }
0356
0357
0358 if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
0359 urb_value = BELKIN_SA_DATA_BITS(tty_get_char_size(cflag));
0360 if (BSA_USB_CMD(BELKIN_SA_SET_DATA_BITS_REQUEST, urb_value) < 0)
0361 dev_err(&port->dev, "Set data bits error\n");
0362 }
0363
0364
0365 if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
0366 urb_value = (cflag & CSTOPB) ? BELKIN_SA_STOP_BITS(2)
0367 : BELKIN_SA_STOP_BITS(1);
0368 if (BSA_USB_CMD(BELKIN_SA_SET_STOP_BITS_REQUEST,
0369 urb_value) < 0)
0370 dev_err(&port->dev, "Set stop bits error\n");
0371 }
0372
0373
0374 if (((iflag ^ old_iflag) & (IXOFF | IXON)) ||
0375 ((cflag ^ old_cflag) & CRTSCTS)) {
0376 urb_value = 0;
0377 if ((iflag & IXOFF) || (iflag & IXON))
0378 urb_value |= (BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
0379 else
0380 urb_value &= ~(BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
0381
0382 if (cflag & CRTSCTS)
0383 urb_value |= (BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
0384 else
0385 urb_value &= ~(BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
0386
0387 if (bad_flow_control)
0388 urb_value &= ~(BELKIN_SA_FLOW_IRTS);
0389
0390 if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST, urb_value) < 0)
0391 dev_err(&port->dev, "Set flow control error\n");
0392 }
0393
0394
0395 spin_lock_irqsave(&priv->lock, flags);
0396 priv->control_state = control_state;
0397 spin_unlock_irqrestore(&priv->lock, flags);
0398 }
0399
0400 static void belkin_sa_break_ctl(struct tty_struct *tty, int break_state)
0401 {
0402 struct usb_serial_port *port = tty->driver_data;
0403 struct usb_serial *serial = port->serial;
0404
0405 if (BSA_USB_CMD(BELKIN_SA_SET_BREAK_REQUEST, break_state ? 1 : 0) < 0)
0406 dev_err(&port->dev, "Set break_ctl %d\n", break_state);
0407 }
0408
0409 static int belkin_sa_tiocmget(struct tty_struct *tty)
0410 {
0411 struct usb_serial_port *port = tty->driver_data;
0412 struct belkin_sa_private *priv = usb_get_serial_port_data(port);
0413 unsigned long control_state;
0414 unsigned long flags;
0415
0416 spin_lock_irqsave(&priv->lock, flags);
0417 control_state = priv->control_state;
0418 spin_unlock_irqrestore(&priv->lock, flags);
0419
0420 return control_state;
0421 }
0422
0423 static int belkin_sa_tiocmset(struct tty_struct *tty,
0424 unsigned int set, unsigned int clear)
0425 {
0426 struct usb_serial_port *port = tty->driver_data;
0427 struct usb_serial *serial = port->serial;
0428 struct belkin_sa_private *priv = usb_get_serial_port_data(port);
0429 unsigned long control_state;
0430 unsigned long flags;
0431 int retval;
0432 int rts = 0;
0433 int dtr = 0;
0434
0435 spin_lock_irqsave(&priv->lock, flags);
0436 control_state = priv->control_state;
0437
0438 if (set & TIOCM_RTS) {
0439 control_state |= TIOCM_RTS;
0440 rts = 1;
0441 }
0442 if (set & TIOCM_DTR) {
0443 control_state |= TIOCM_DTR;
0444 dtr = 1;
0445 }
0446 if (clear & TIOCM_RTS) {
0447 control_state &= ~TIOCM_RTS;
0448 rts = 0;
0449 }
0450 if (clear & TIOCM_DTR) {
0451 control_state &= ~TIOCM_DTR;
0452 dtr = 0;
0453 }
0454
0455 priv->control_state = control_state;
0456 spin_unlock_irqrestore(&priv->lock, flags);
0457
0458 retval = BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, rts);
0459 if (retval < 0) {
0460 dev_err(&port->dev, "Set RTS error %d\n", retval);
0461 goto exit;
0462 }
0463
0464 retval = BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, dtr);
0465 if (retval < 0) {
0466 dev_err(&port->dev, "Set DTR error %d\n", retval);
0467 goto exit;
0468 }
0469 exit:
0470 return retval;
0471 }
0472
0473 module_usb_serial_driver(serial_drivers, id_table);
0474
0475 MODULE_AUTHOR(DRIVER_AUTHOR);
0476 MODULE_DESCRIPTION(DRIVER_DESC);
0477 MODULE_LICENSE("GPL");