belkin_sa.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 int 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, struct ktermios * old);
  45. static void belkin_sa_break_ctl(struct tty_struct *tty, int break_state);
  46. static int belkin_sa_tiocmget(struct tty_struct *tty);
  47. static int belkin_sa_tiocmset(struct tty_struct *tty,
  48. unsigned int set, unsigned int clear);
  49. static const struct usb_device_id id_table[] = {
  50. { USB_DEVICE(BELKIN_SA_VID, BELKIN_SA_PID) },
  51. { USB_DEVICE(BELKIN_OLD_VID, BELKIN_OLD_PID) },
  52. { USB_DEVICE(PERACOM_VID, PERACOM_PID) },
  53. { USB_DEVICE(GOHUBS_VID, GOHUBS_PID) },
  54. { USB_DEVICE(GOHUBS_VID, HANDYLINK_PID) },
  55. { USB_DEVICE(BELKIN_DOCKSTATION_VID, BELKIN_DOCKSTATION_PID) },
  56. { } /* Terminating entry */
  57. };
  58. MODULE_DEVICE_TABLE(usb, id_table);
  59. /* All of the device info needed for the serial converters */
  60. static struct usb_serial_driver belkin_device = {
  61. .driver = {
  62. .owner = THIS_MODULE,
  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(sizeof(struct belkin_sa_private), GFP_KERNEL);
  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 int 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. return 0;
  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, 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. switch (cflag & CSIZE) {
  310. case CS5:
  311. urb_value = BELKIN_SA_DATA_BITS(5);
  312. break;
  313. case CS6:
  314. urb_value = BELKIN_SA_DATA_BITS(6);
  315. break;
  316. case CS7:
  317. urb_value = BELKIN_SA_DATA_BITS(7);
  318. break;
  319. case CS8:
  320. urb_value = BELKIN_SA_DATA_BITS(8);
  321. break;
  322. default:
  323. dev_dbg(&port->dev,
  324. "CSIZE was not CS5-CS8, using default of 8\n");
  325. urb_value = BELKIN_SA_DATA_BITS(8);
  326. break;
  327. }
  328. if (BSA_USB_CMD(BELKIN_SA_SET_DATA_BITS_REQUEST, urb_value) < 0)
  329. dev_err(&port->dev, "Set data bits error\n");
  330. }
  331. /* set the number of stop bits */
  332. if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
  333. urb_value = (cflag & CSTOPB) ? BELKIN_SA_STOP_BITS(2)
  334. : BELKIN_SA_STOP_BITS(1);
  335. if (BSA_USB_CMD(BELKIN_SA_SET_STOP_BITS_REQUEST,
  336. urb_value) < 0)
  337. dev_err(&port->dev, "Set stop bits error\n");
  338. }
  339. /* Set flow control */
  340. if (((iflag ^ old_iflag) & (IXOFF | IXON)) ||
  341. ((cflag ^ old_cflag) & CRTSCTS)) {
  342. urb_value = 0;
  343. if ((iflag & IXOFF) || (iflag & IXON))
  344. urb_value |= (BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
  345. else
  346. urb_value &= ~(BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
  347. if (cflag & CRTSCTS)
  348. urb_value |= (BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
  349. else
  350. urb_value &= ~(BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
  351. if (bad_flow_control)
  352. urb_value &= ~(BELKIN_SA_FLOW_IRTS);
  353. if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST, urb_value) < 0)
  354. dev_err(&port->dev, "Set flow control error\n");
  355. }
  356. /* save off the modified port settings */
  357. spin_lock_irqsave(&priv->lock, flags);
  358. priv->control_state = control_state;
  359. spin_unlock_irqrestore(&priv->lock, flags);
  360. }
  361. static void belkin_sa_break_ctl(struct tty_struct *tty, int break_state)
  362. {
  363. struct usb_serial_port *port = tty->driver_data;
  364. struct usb_serial *serial = port->serial;
  365. if (BSA_USB_CMD(BELKIN_SA_SET_BREAK_REQUEST, break_state ? 1 : 0) < 0)
  366. dev_err(&port->dev, "Set break_ctl %d\n", break_state);
  367. }
  368. static int belkin_sa_tiocmget(struct tty_struct *tty)
  369. {
  370. struct usb_serial_port *port = tty->driver_data;
  371. struct belkin_sa_private *priv = usb_get_serial_port_data(port);
  372. unsigned long control_state;
  373. unsigned long flags;
  374. spin_lock_irqsave(&priv->lock, flags);
  375. control_state = priv->control_state;
  376. spin_unlock_irqrestore(&priv->lock, flags);
  377. return control_state;
  378. }
  379. static int belkin_sa_tiocmset(struct tty_struct *tty,
  380. unsigned int set, unsigned int clear)
  381. {
  382. struct usb_serial_port *port = tty->driver_data;
  383. struct usb_serial *serial = port->serial;
  384. struct belkin_sa_private *priv = usb_get_serial_port_data(port);
  385. unsigned long control_state;
  386. unsigned long flags;
  387. int retval;
  388. int rts = 0;
  389. int dtr = 0;
  390. spin_lock_irqsave(&priv->lock, flags);
  391. control_state = priv->control_state;
  392. if (set & TIOCM_RTS) {
  393. control_state |= TIOCM_RTS;
  394. rts = 1;
  395. }
  396. if (set & TIOCM_DTR) {
  397. control_state |= TIOCM_DTR;
  398. dtr = 1;
  399. }
  400. if (clear & TIOCM_RTS) {
  401. control_state &= ~TIOCM_RTS;
  402. rts = 0;
  403. }
  404. if (clear & TIOCM_DTR) {
  405. control_state &= ~TIOCM_DTR;
  406. dtr = 0;
  407. }
  408. priv->control_state = control_state;
  409. spin_unlock_irqrestore(&priv->lock, flags);
  410. retval = BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, rts);
  411. if (retval < 0) {
  412. dev_err(&port->dev, "Set RTS error %d\n", retval);
  413. goto exit;
  414. }
  415. retval = BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, dtr);
  416. if (retval < 0) {
  417. dev_err(&port->dev, "Set DTR error %d\n", retval);
  418. goto exit;
  419. }
  420. exit:
  421. return retval;
  422. }
  423. module_usb_serial_driver(serial_drivers, id_table);
  424. MODULE_AUTHOR(DRIVER_AUTHOR);
  425. MODULE_DESCRIPTION(DRIVER_DESC);
  426. MODULE_LICENSE("GPL");