belkin_sa.c 15 KB

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