belkin_sa.c 15 KB

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