belkin_sa.c 14 KB

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