belkin_sa.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Belkin USB Serial Adapter Driver
  4. *
  5. * Copyright (C) 2000 William Greathouse ([email protected])
  6. * Copyright (C) 2000-2001 Greg Kroah-Hartman ([email protected])
  7. * Copyright (C) 2010 Johan Hovold ([email protected])
  8. *
  9. * This program is largely derived from work by the linux-usb group
  10. * and associated source files. Please see the usb/serial files for
  11. * individual credits and copyrights.
  12. *
  13. * See Documentation/usb/usb-serial.rst for more information on using this
  14. * driver
  15. *
  16. * TODO:
  17. * -- Add true modem control line query capability. Currently we track the
  18. * states reported by the interrupt and the states we request.
  19. * -- Add support for flush commands
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/errno.h>
  23. #include <linux/slab.h>
  24. #include <linux/tty.h>
  25. #include <linux/tty_driver.h>
  26. #include <linux/tty_flip.h>
  27. #include <linux/module.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/usb.h>
  31. #include <linux/usb/serial.h>
  32. #include "belkin_sa.h"
  33. #define DRIVER_AUTHOR "William Greathouse <[email protected]>"
  34. #define DRIVER_DESC "USB Belkin Serial converter driver"
  35. /* function prototypes for a Belkin USB Serial Adapter F5U103 */
  36. static int belkin_sa_port_probe(struct usb_serial_port *port);
  37. static void belkin_sa_port_remove(struct usb_serial_port *port);
  38. static int belkin_sa_open(struct tty_struct *tty,
  39. struct usb_serial_port *port);
  40. static void belkin_sa_close(struct usb_serial_port *port);
  41. static void belkin_sa_read_int_callback(struct urb *urb);
  42. static void belkin_sa_process_read_urb(struct urb *urb);
  43. static void belkin_sa_set_termios(struct tty_struct *tty,
  44. struct usb_serial_port *port,
  45. const struct ktermios *old_termios);
  46. static void belkin_sa_break_ctl(struct tty_struct *tty, int break_state);
  47. static int belkin_sa_tiocmget(struct tty_struct *tty);
  48. static int belkin_sa_tiocmset(struct tty_struct *tty,
  49. unsigned int set, unsigned int clear);
  50. static const struct usb_device_id id_table[] = {
  51. { USB_DEVICE(BELKIN_SA_VID, BELKIN_SA_PID) },
  52. { USB_DEVICE(BELKIN_OLD_VID, BELKIN_OLD_PID) },
  53. { USB_DEVICE(PERACOM_VID, PERACOM_PID) },
  54. { USB_DEVICE(GOHUBS_VID, GOHUBS_PID) },
  55. { USB_DEVICE(GOHUBS_VID, HANDYLINK_PID) },
  56. { USB_DEVICE(BELKIN_DOCKSTATION_VID, BELKIN_DOCKSTATION_PID) },
  57. { } /* Terminating entry */
  58. };
  59. MODULE_DEVICE_TABLE(usb, id_table);
  60. /* All of the device info needed for the serial converters */
  61. static struct usb_serial_driver belkin_device = {
  62. .driver = {
  63. .owner = THIS_MODULE,
  64. .name = "belkin",
  65. },
  66. .description = "Belkin / Peracom / GoHubs USB Serial Adapter",
  67. .id_table = id_table,
  68. .num_ports = 1,
  69. .open = belkin_sa_open,
  70. .close = belkin_sa_close,
  71. .read_int_callback = belkin_sa_read_int_callback,
  72. .process_read_urb = belkin_sa_process_read_urb,
  73. .set_termios = belkin_sa_set_termios,
  74. .break_ctl = belkin_sa_break_ctl,
  75. .tiocmget = belkin_sa_tiocmget,
  76. .tiocmset = belkin_sa_tiocmset,
  77. .port_probe = belkin_sa_port_probe,
  78. .port_remove = belkin_sa_port_remove,
  79. };
  80. static struct usb_serial_driver * const serial_drivers[] = {
  81. &belkin_device, NULL
  82. };
  83. struct belkin_sa_private {
  84. spinlock_t lock;
  85. unsigned long control_state;
  86. unsigned char last_lsr;
  87. unsigned char last_msr;
  88. int bad_flow_control;
  89. };
  90. /*
  91. * ***************************************************************************
  92. * Belkin USB Serial Adapter F5U103 specific driver functions
  93. * ***************************************************************************
  94. */
  95. #define WDR_TIMEOUT 5000 /* default urb timeout */
  96. /* assumes that struct usb_serial *serial is available */
  97. #define BSA_USB_CMD(c, v) usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), \
  98. (c), BELKIN_SA_SET_REQUEST_TYPE, \
  99. (v), 0, NULL, 0, WDR_TIMEOUT)
  100. static int belkin_sa_port_probe(struct usb_serial_port *port)
  101. {
  102. struct usb_device *dev = port->serial->dev;
  103. struct belkin_sa_private *priv;
  104. priv = kmalloc(sizeof(struct belkin_sa_private), GFP_KERNEL);
  105. if (!priv)
  106. return -ENOMEM;
  107. spin_lock_init(&priv->lock);
  108. priv->control_state = 0;
  109. priv->last_lsr = 0;
  110. priv->last_msr = 0;
  111. /* see comments at top of file */
  112. priv->bad_flow_control =
  113. (le16_to_cpu(dev->descriptor.bcdDevice) <= 0x0206) ? 1 : 0;
  114. dev_info(&dev->dev, "bcdDevice: %04x, bfc: %d\n",
  115. le16_to_cpu(dev->descriptor.bcdDevice),
  116. priv->bad_flow_control);
  117. usb_set_serial_port_data(port, priv);
  118. return 0;
  119. }
  120. static void belkin_sa_port_remove(struct usb_serial_port *port)
  121. {
  122. struct belkin_sa_private *priv;
  123. priv = usb_get_serial_port_data(port);
  124. kfree(priv);
  125. }
  126. static int belkin_sa_open(struct tty_struct *tty,
  127. struct usb_serial_port *port)
  128. {
  129. int retval;
  130. retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  131. if (retval) {
  132. dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
  133. return retval;
  134. }
  135. retval = usb_serial_generic_open(tty, port);
  136. if (retval)
  137. usb_kill_urb(port->interrupt_in_urb);
  138. return retval;
  139. }
  140. static void belkin_sa_close(struct usb_serial_port *port)
  141. {
  142. usb_serial_generic_close(port);
  143. usb_kill_urb(port->interrupt_in_urb);
  144. }
  145. static void belkin_sa_read_int_callback(struct urb *urb)
  146. {
  147. struct usb_serial_port *port = urb->context;
  148. struct belkin_sa_private *priv;
  149. unsigned char *data = urb->transfer_buffer;
  150. int retval;
  151. int status = urb->status;
  152. unsigned long flags;
  153. switch (status) {
  154. case 0:
  155. /* success */
  156. break;
  157. case -ECONNRESET:
  158. case -ENOENT:
  159. case -ESHUTDOWN:
  160. /* this urb is terminated, clean up */
  161. dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
  162. __func__, status);
  163. return;
  164. default:
  165. dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
  166. __func__, status);
  167. goto exit;
  168. }
  169. usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
  170. /* Handle known interrupt data */
  171. /* ignore data[0] and data[1] */
  172. priv = usb_get_serial_port_data(port);
  173. spin_lock_irqsave(&priv->lock, flags);
  174. priv->last_msr = data[BELKIN_SA_MSR_INDEX];
  175. /* Record Control Line states */
  176. if (priv->last_msr & BELKIN_SA_MSR_DSR)
  177. priv->control_state |= TIOCM_DSR;
  178. else
  179. priv->control_state &= ~TIOCM_DSR;
  180. if (priv->last_msr & BELKIN_SA_MSR_CTS)
  181. priv->control_state |= TIOCM_CTS;
  182. else
  183. priv->control_state &= ~TIOCM_CTS;
  184. if (priv->last_msr & BELKIN_SA_MSR_RI)
  185. priv->control_state |= TIOCM_RI;
  186. else
  187. priv->control_state &= ~TIOCM_RI;
  188. if (priv->last_msr & BELKIN_SA_MSR_CD)
  189. priv->control_state |= TIOCM_CD;
  190. else
  191. priv->control_state &= ~TIOCM_CD;
  192. priv->last_lsr = data[BELKIN_SA_LSR_INDEX];
  193. spin_unlock_irqrestore(&priv->lock, flags);
  194. exit:
  195. retval = usb_submit_urb(urb, GFP_ATOMIC);
  196. if (retval)
  197. dev_err(&port->dev, "%s - usb_submit_urb failed with "
  198. "result %d\n", __func__, retval);
  199. }
  200. static void belkin_sa_process_read_urb(struct urb *urb)
  201. {
  202. struct usb_serial_port *port = urb->context;
  203. struct belkin_sa_private *priv = usb_get_serial_port_data(port);
  204. unsigned char *data = urb->transfer_buffer;
  205. unsigned long flags;
  206. unsigned char status;
  207. char tty_flag;
  208. /* Update line status */
  209. tty_flag = TTY_NORMAL;
  210. spin_lock_irqsave(&priv->lock, flags);
  211. status = priv->last_lsr;
  212. priv->last_lsr &= ~BELKIN_SA_LSR_ERR;
  213. spin_unlock_irqrestore(&priv->lock, flags);
  214. if (!urb->actual_length)
  215. return;
  216. if (status & BELKIN_SA_LSR_ERR) {
  217. /* Break takes precedence over parity, which takes precedence
  218. * over framing errors. */
  219. if (status & BELKIN_SA_LSR_BI)
  220. tty_flag = TTY_BREAK;
  221. else if (status & BELKIN_SA_LSR_PE)
  222. tty_flag = TTY_PARITY;
  223. else if (status & BELKIN_SA_LSR_FE)
  224. tty_flag = TTY_FRAME;
  225. dev_dbg(&port->dev, "tty_flag = %d\n", tty_flag);
  226. /* Overrun is special, not associated with a char. */
  227. if (status & BELKIN_SA_LSR_OE)
  228. tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
  229. }
  230. tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
  231. urb->actual_length);
  232. tty_flip_buffer_push(&port->port);
  233. }
  234. static void belkin_sa_set_termios(struct tty_struct *tty,
  235. struct usb_serial_port *port,
  236. const struct ktermios *old_termios)
  237. {
  238. struct usb_serial *serial = port->serial;
  239. struct belkin_sa_private *priv = usb_get_serial_port_data(port);
  240. unsigned int iflag;
  241. unsigned int cflag;
  242. unsigned int old_iflag = 0;
  243. unsigned int old_cflag = 0;
  244. __u16 urb_value = 0; /* Will hold the new flags */
  245. unsigned long flags;
  246. unsigned long control_state;
  247. int bad_flow_control;
  248. speed_t baud;
  249. struct ktermios *termios = &tty->termios;
  250. iflag = termios->c_iflag;
  251. cflag = termios->c_cflag;
  252. termios->c_cflag &= ~CMSPAR;
  253. /* get a local copy of the current port settings */
  254. spin_lock_irqsave(&priv->lock, flags);
  255. control_state = priv->control_state;
  256. bad_flow_control = priv->bad_flow_control;
  257. spin_unlock_irqrestore(&priv->lock, flags);
  258. old_iflag = old_termios->c_iflag;
  259. old_cflag = old_termios->c_cflag;
  260. /* Set the baud rate */
  261. if ((cflag & CBAUD) != (old_cflag & CBAUD)) {
  262. /* reassert DTR and (maybe) RTS on transition from B0 */
  263. if ((old_cflag & CBAUD) == B0) {
  264. control_state |= (TIOCM_DTR|TIOCM_RTS);
  265. if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, 1) < 0)
  266. dev_err(&port->dev, "Set DTR error\n");
  267. /* don't set RTS if using hardware flow control */
  268. if (!(old_cflag & CRTSCTS))
  269. if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST
  270. , 1) < 0)
  271. dev_err(&port->dev, "Set RTS error\n");
  272. }
  273. }
  274. baud = tty_get_baud_rate(tty);
  275. if (baud) {
  276. urb_value = BELKIN_SA_BAUD(baud);
  277. /* Clip to maximum speed */
  278. if (urb_value == 0)
  279. urb_value = 1;
  280. /* Turn it back into a resulting real baud rate */
  281. baud = BELKIN_SA_BAUD(urb_value);
  282. /* Report the actual baud rate back to the caller */
  283. tty_encode_baud_rate(tty, baud, baud);
  284. if (BSA_USB_CMD(BELKIN_SA_SET_BAUDRATE_REQUEST, urb_value) < 0)
  285. dev_err(&port->dev, "Set baudrate error\n");
  286. } else {
  287. /* Disable flow control */
  288. if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST,
  289. BELKIN_SA_FLOW_NONE) < 0)
  290. dev_err(&port->dev, "Disable flowcontrol error\n");
  291. /* Drop RTS and DTR */
  292. control_state &= ~(TIOCM_DTR | TIOCM_RTS);
  293. if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, 0) < 0)
  294. dev_err(&port->dev, "DTR LOW error\n");
  295. if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, 0) < 0)
  296. dev_err(&port->dev, "RTS LOW error\n");
  297. }
  298. /* set the parity */
  299. if ((cflag ^ old_cflag) & (PARENB | PARODD)) {
  300. if (cflag & PARENB)
  301. urb_value = (cflag & PARODD) ? BELKIN_SA_PARITY_ODD
  302. : BELKIN_SA_PARITY_EVEN;
  303. else
  304. urb_value = BELKIN_SA_PARITY_NONE;
  305. if (BSA_USB_CMD(BELKIN_SA_SET_PARITY_REQUEST, urb_value) < 0)
  306. dev_err(&port->dev, "Set parity error\n");
  307. }
  308. /* set the number of data bits */
  309. if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
  310. urb_value = BELKIN_SA_DATA_BITS(tty_get_char_size(cflag));
  311. if (BSA_USB_CMD(BELKIN_SA_SET_DATA_BITS_REQUEST, urb_value) < 0)
  312. dev_err(&port->dev, "Set data bits error\n");
  313. }
  314. /* set the number of stop bits */
  315. if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
  316. urb_value = (cflag & CSTOPB) ? BELKIN_SA_STOP_BITS(2)
  317. : BELKIN_SA_STOP_BITS(1);
  318. if (BSA_USB_CMD(BELKIN_SA_SET_STOP_BITS_REQUEST,
  319. urb_value) < 0)
  320. dev_err(&port->dev, "Set stop bits error\n");
  321. }
  322. /* Set flow control */
  323. if (((iflag ^ old_iflag) & (IXOFF | IXON)) ||
  324. ((cflag ^ old_cflag) & CRTSCTS)) {
  325. urb_value = 0;
  326. if ((iflag & IXOFF) || (iflag & IXON))
  327. urb_value |= (BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
  328. else
  329. urb_value &= ~(BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
  330. if (cflag & CRTSCTS)
  331. urb_value |= (BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
  332. else
  333. urb_value &= ~(BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
  334. if (bad_flow_control)
  335. urb_value &= ~(BELKIN_SA_FLOW_IRTS);
  336. if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST, urb_value) < 0)
  337. dev_err(&port->dev, "Set flow control error\n");
  338. }
  339. /* save off the modified port settings */
  340. spin_lock_irqsave(&priv->lock, flags);
  341. priv->control_state = control_state;
  342. spin_unlock_irqrestore(&priv->lock, flags);
  343. }
  344. static void belkin_sa_break_ctl(struct tty_struct *tty, int break_state)
  345. {
  346. struct usb_serial_port *port = tty->driver_data;
  347. struct usb_serial *serial = port->serial;
  348. if (BSA_USB_CMD(BELKIN_SA_SET_BREAK_REQUEST, break_state ? 1 : 0) < 0)
  349. dev_err(&port->dev, "Set break_ctl %d\n", break_state);
  350. }
  351. static int belkin_sa_tiocmget(struct tty_struct *tty)
  352. {
  353. struct usb_serial_port *port = tty->driver_data;
  354. struct belkin_sa_private *priv = usb_get_serial_port_data(port);
  355. unsigned long control_state;
  356. unsigned long flags;
  357. spin_lock_irqsave(&priv->lock, flags);
  358. control_state = priv->control_state;
  359. spin_unlock_irqrestore(&priv->lock, flags);
  360. return control_state;
  361. }
  362. static int belkin_sa_tiocmset(struct tty_struct *tty,
  363. unsigned int set, unsigned int clear)
  364. {
  365. struct usb_serial_port *port = tty->driver_data;
  366. struct usb_serial *serial = port->serial;
  367. struct belkin_sa_private *priv = usb_get_serial_port_data(port);
  368. unsigned long control_state;
  369. unsigned long flags;
  370. int retval;
  371. int rts = 0;
  372. int dtr = 0;
  373. spin_lock_irqsave(&priv->lock, flags);
  374. control_state = priv->control_state;
  375. if (set & TIOCM_RTS) {
  376. control_state |= TIOCM_RTS;
  377. rts = 1;
  378. }
  379. if (set & TIOCM_DTR) {
  380. control_state |= TIOCM_DTR;
  381. dtr = 1;
  382. }
  383. if (clear & TIOCM_RTS) {
  384. control_state &= ~TIOCM_RTS;
  385. rts = 0;
  386. }
  387. if (clear & TIOCM_DTR) {
  388. control_state &= ~TIOCM_DTR;
  389. dtr = 0;
  390. }
  391. priv->control_state = control_state;
  392. spin_unlock_irqrestore(&priv->lock, flags);
  393. retval = BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, rts);
  394. if (retval < 0) {
  395. dev_err(&port->dev, "Set RTS error %d\n", retval);
  396. goto exit;
  397. }
  398. retval = BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, dtr);
  399. if (retval < 0) {
  400. dev_err(&port->dev, "Set DTR error %d\n", retval);
  401. goto exit;
  402. }
  403. exit:
  404. return retval;
  405. }
  406. module_usb_serial_driver(serial_drivers, id_table);
  407. MODULE_AUTHOR(DRIVER_AUTHOR);
  408. MODULE_DESCRIPTION(DRIVER_DESC);
  409. MODULE_LICENSE("GPL");