belkin_sa.c 14 KB

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