belkin_sa.c 14 KB

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