belkin_sa.c 14 KB

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