belkin_sa.c 15 KB

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