mct_u232.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. /*
  2. * MCT (Magic Control Technology Corp.) USB RS232 Converter Driver
  3. *
  4. * Copyright (C) 2000 Wolfgang Grandegger (wolfgang@ces.ch)
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is largely derived from the Belkin USB Serial Adapter Driver
  12. * (see belkin_sa.[ch]). All of the information about the device was acquired
  13. * by using SniffUSB on Windows98. For technical details see mct_u232.h.
  14. *
  15. * William G. Greathouse and Greg Kroah-Hartman provided great help on how to
  16. * do the reverse engineering and how to write a USB serial device driver.
  17. *
  18. * TO BE DONE, TO BE CHECKED:
  19. * DTR/RTS signal handling may be incomplete or incorrect. I have mainly
  20. * implemented what I have seen with SniffUSB or found in belkin_sa.c.
  21. * For further TODOs check also belkin_sa.c.
  22. *
  23. * TEST STATUS:
  24. * Basic tests have been performed with minicom/zmodem transfers and
  25. * modem dialing under Linux 2.4.0-test10 (for me it works fine).
  26. *
  27. * 04-Nov-2003 Bill Marr <marr at flex dot com>
  28. * - Mimic Windows driver by sending 2 USB 'device request' messages
  29. * following normal 'baud rate change' message. This allows data to be
  30. * transmitted to RS-232 devices which don't assert the 'CTS' signal.
  31. *
  32. * 10-Nov-2001 Wolfgang Grandegger
  33. * - Fixed an endianess problem with the baudrate selection for PowerPC.
  34. *
  35. * 06-Dec-2001 Martin Hamilton <martinh@gnu.org>
  36. * - Added support for the Belkin F5U109 DB9 adaptor
  37. *
  38. * 30-May-2001 Greg Kroah-Hartman
  39. * - switched from using spinlock to a semaphore, which fixes lots of
  40. * problems.
  41. *
  42. * 04-May-2001 Stelian Pop
  43. * - Set the maximum bulk output size for Sitecom U232-P25 model to 16 bytes
  44. * instead of the device reported 32 (using 32 bytes causes many data
  45. * loss, Windows driver uses 16 too).
  46. *
  47. * 02-May-2001 Stelian Pop
  48. * - Fixed the baud calculation for Sitecom U232-P25 model
  49. *
  50. * 08-Apr-2001 gb
  51. * - Identify version on module load.
  52. *
  53. * 06-Jan-2001 Cornel Ciocirlan
  54. * - Added support for Sitecom U232-P25 model (Product Id 0x0230)
  55. * - Added support for D-Link DU-H3SP USB BAY (Product Id 0x0200)
  56. *
  57. * 29-Nov-2000 Greg Kroah-Hartman
  58. * - Added device id table to fit with 2.4.0-test11 structure.
  59. * - took out DEAL_WITH_TWO_INT_IN_ENDPOINTS #define as it's not needed
  60. * (lots of things will change if/when the usb-serial core changes to
  61. * handle these issues.
  62. *
  63. * 27-Nov-2000 Wolfgang Grandegge
  64. * A version for kernel 2.4.0-test10 released to the Linux community
  65. * (via linux-usb-devel).
  66. */
  67. #include <linux/kernel.h>
  68. #include <linux/errno.h>
  69. #include <linux/init.h>
  70. #include <linux/slab.h>
  71. #include <linux/tty.h>
  72. #include <linux/tty_driver.h>
  73. #include <linux/tty_flip.h>
  74. #include <linux/module.h>
  75. #include <linux/spinlock.h>
  76. #include <linux/uaccess.h>
  77. #include <linux/usb.h>
  78. #include <linux/usb/serial.h>
  79. #include "mct_u232.h"
  80. /*
  81. * Version Information
  82. */
  83. #define DRIVER_VERSION "z2.1" /* Linux in-kernel version */
  84. #define DRIVER_AUTHOR "Wolfgang Grandegger <wolfgang@ces.ch>"
  85. #define DRIVER_DESC "Magic Control Technology USB-RS232 converter driver"
  86. static int debug;
  87. /*
  88. * Function prototypes
  89. */
  90. static int mct_u232_startup(struct usb_serial *serial);
  91. static void mct_u232_release(struct usb_serial *serial);
  92. static int mct_u232_open(struct tty_struct *tty,
  93. struct usb_serial_port *port, struct file *filp);
  94. static void mct_u232_close(struct usb_serial_port *port);
  95. static void mct_u232_dtr_rts(struct usb_serial_port *port, int on);
  96. static void mct_u232_read_int_callback(struct urb *urb);
  97. static void mct_u232_set_termios(struct tty_struct *tty,
  98. struct usb_serial_port *port, struct ktermios *old);
  99. static void mct_u232_break_ctl(struct tty_struct *tty, int break_state);
  100. static int mct_u232_tiocmget(struct tty_struct *tty, struct file *file);
  101. static int mct_u232_tiocmset(struct tty_struct *tty, struct file *file,
  102. unsigned int set, unsigned int clear);
  103. static void mct_u232_throttle(struct tty_struct *tty);
  104. static void mct_u232_unthrottle(struct tty_struct *tty);
  105. /*
  106. * All of the device info needed for the MCT USB-RS232 converter.
  107. */
  108. static struct usb_device_id id_table_combined [] = {
  109. { USB_DEVICE(MCT_U232_VID, MCT_U232_PID) },
  110. { USB_DEVICE(MCT_U232_VID, MCT_U232_SITECOM_PID) },
  111. { USB_DEVICE(MCT_U232_VID, MCT_U232_DU_H3SP_PID) },
  112. { USB_DEVICE(MCT_U232_BELKIN_F5U109_VID, MCT_U232_BELKIN_F5U109_PID) },
  113. { } /* Terminating entry */
  114. };
  115. MODULE_DEVICE_TABLE(usb, id_table_combined);
  116. static struct usb_driver mct_u232_driver = {
  117. .name = "mct_u232",
  118. .probe = usb_serial_probe,
  119. .disconnect = usb_serial_disconnect,
  120. .id_table = id_table_combined,
  121. .no_dynamic_id = 1,
  122. };
  123. static struct usb_serial_driver mct_u232_device = {
  124. .driver = {
  125. .owner = THIS_MODULE,
  126. .name = "mct_u232",
  127. },
  128. .description = "MCT U232",
  129. .usb_driver = &mct_u232_driver,
  130. .id_table = id_table_combined,
  131. .num_ports = 1,
  132. .open = mct_u232_open,
  133. .close = mct_u232_close,
  134. .dtr_rts = mct_u232_dtr_rts,
  135. .throttle = mct_u232_throttle,
  136. .unthrottle = mct_u232_unthrottle,
  137. .read_int_callback = mct_u232_read_int_callback,
  138. .set_termios = mct_u232_set_termios,
  139. .break_ctl = mct_u232_break_ctl,
  140. .tiocmget = mct_u232_tiocmget,
  141. .tiocmset = mct_u232_tiocmset,
  142. .attach = mct_u232_startup,
  143. .release = mct_u232_release,
  144. };
  145. struct mct_u232_private {
  146. spinlock_t lock;
  147. unsigned int control_state; /* Modem Line Setting (TIOCM) */
  148. unsigned char last_lcr; /* Line Control Register */
  149. unsigned char last_lsr; /* Line Status Register */
  150. unsigned char last_msr; /* Modem Status Register */
  151. unsigned int rx_flags; /* Throttling flags */
  152. };
  153. #define THROTTLED 0x01
  154. /*
  155. * Handle vendor specific USB requests
  156. */
  157. #define WDR_TIMEOUT 5000 /* default urb timeout */
  158. /*
  159. * Later day 2.6.0-test kernels have new baud rates like B230400 which
  160. * we do not know how to support. We ignore them for the moment.
  161. */
  162. static int mct_u232_calculate_baud_rate(struct usb_serial *serial,
  163. speed_t value, speed_t *result)
  164. {
  165. *result = value;
  166. if (le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_SITECOM_PID
  167. || le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_BELKIN_F5U109_PID) {
  168. switch (value) {
  169. case 300:
  170. return 0x01;
  171. case 600:
  172. return 0x02; /* this one not tested */
  173. case 1200:
  174. return 0x03;
  175. case 2400:
  176. return 0x04;
  177. case 4800:
  178. return 0x06;
  179. case 9600:
  180. return 0x08;
  181. case 19200:
  182. return 0x09;
  183. case 38400:
  184. return 0x0a;
  185. case 57600:
  186. return 0x0b;
  187. case 115200:
  188. return 0x0c;
  189. default:
  190. *result = 9600;
  191. return 0x08;
  192. }
  193. } else {
  194. /* FIXME: Can we use any divider - should we do
  195. divider = 115200/value;
  196. real baud = 115200/divider */
  197. switch (value) {
  198. case 300: break;
  199. case 600: break;
  200. case 1200: break;
  201. case 2400: break;
  202. case 4800: break;
  203. case 9600: break;
  204. case 19200: break;
  205. case 38400: break;
  206. case 57600: break;
  207. case 115200: break;
  208. default:
  209. value = 9600;
  210. *result = 9600;
  211. }
  212. return 115200/value;
  213. }
  214. }
  215. static int mct_u232_set_baud_rate(struct tty_struct *tty,
  216. struct usb_serial *serial, struct usb_serial_port *port, speed_t value)
  217. {
  218. __le32 divisor;
  219. int rc;
  220. unsigned char zero_byte = 0;
  221. unsigned char cts_enable_byte = 0;
  222. speed_t speed;
  223. divisor = cpu_to_le32(mct_u232_calculate_baud_rate(serial, value,
  224. &speed));
  225. rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  226. MCT_U232_SET_BAUD_RATE_REQUEST,
  227. MCT_U232_SET_REQUEST_TYPE,
  228. 0, 0, &divisor, MCT_U232_SET_BAUD_RATE_SIZE,
  229. WDR_TIMEOUT);
  230. if (rc < 0) /*FIXME: What value speed results */
  231. dev_err(&port->dev, "Set BAUD RATE %d failed (error = %d)\n",
  232. value, rc);
  233. else
  234. tty_encode_baud_rate(tty, speed, speed);
  235. dbg("set_baud_rate: value: 0x%x, divisor: 0x%x", value, divisor);
  236. /* Mimic the MCT-supplied Windows driver (version 1.21P.0104), which
  237. always sends two extra USB 'device request' messages after the
  238. 'baud rate change' message. The actual functionality of the
  239. request codes in these messages is not fully understood but these
  240. particular codes are never seen in any operation besides a baud
  241. rate change. Both of these messages send a single byte of data.
  242. In the first message, the value of this byte is always zero.
  243. The second message has been determined experimentally to control
  244. whether data will be transmitted to a device which is not asserting
  245. the 'CTS' signal. If the second message's data byte is zero, data
  246. will be transmitted even if 'CTS' is not asserted (i.e. no hardware
  247. flow control). if the second message's data byte is nonzero (a
  248. value of 1 is used by this driver), data will not be transmitted to
  249. a device which is not asserting 'CTS'.
  250. */
  251. rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  252. MCT_U232_SET_UNKNOWN1_REQUEST,
  253. MCT_U232_SET_REQUEST_TYPE,
  254. 0, 0, &zero_byte, MCT_U232_SET_UNKNOWN1_SIZE,
  255. WDR_TIMEOUT);
  256. if (rc < 0)
  257. dev_err(&port->dev, "Sending USB device request code %d "
  258. "failed (error = %d)\n", MCT_U232_SET_UNKNOWN1_REQUEST,
  259. rc);
  260. if (port && C_CRTSCTS(tty))
  261. cts_enable_byte = 1;
  262. dbg("set_baud_rate: send second control message, data = %02X",
  263. cts_enable_byte);
  264. rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  265. MCT_U232_SET_CTS_REQUEST,
  266. MCT_U232_SET_REQUEST_TYPE,
  267. 0, 0, &cts_enable_byte, MCT_U232_SET_CTS_SIZE,
  268. WDR_TIMEOUT);
  269. if (rc < 0)
  270. dev_err(&port->dev, "Sending USB device request code %d "
  271. "failed (error = %d)\n", MCT_U232_SET_CTS_REQUEST, rc);
  272. return rc;
  273. } /* mct_u232_set_baud_rate */
  274. static int mct_u232_set_line_ctrl(struct usb_serial *serial, unsigned char lcr)
  275. {
  276. int rc;
  277. rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  278. MCT_U232_SET_LINE_CTRL_REQUEST,
  279. MCT_U232_SET_REQUEST_TYPE,
  280. 0, 0, &lcr, MCT_U232_SET_LINE_CTRL_SIZE,
  281. WDR_TIMEOUT);
  282. if (rc < 0)
  283. dev_err(&serial->dev->dev,
  284. "Set LINE CTRL 0x%x failed (error = %d)\n", lcr, rc);
  285. dbg("set_line_ctrl: 0x%x", lcr);
  286. return rc;
  287. } /* mct_u232_set_line_ctrl */
  288. static int mct_u232_set_modem_ctrl(struct usb_serial *serial,
  289. unsigned int control_state)
  290. {
  291. int rc;
  292. unsigned char mcr = MCT_U232_MCR_NONE;
  293. if (control_state & TIOCM_DTR)
  294. mcr |= MCT_U232_MCR_DTR;
  295. if (control_state & TIOCM_RTS)
  296. mcr |= MCT_U232_MCR_RTS;
  297. rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  298. MCT_U232_SET_MODEM_CTRL_REQUEST,
  299. MCT_U232_SET_REQUEST_TYPE,
  300. 0, 0, &mcr, MCT_U232_SET_MODEM_CTRL_SIZE,
  301. WDR_TIMEOUT);
  302. if (rc < 0)
  303. dev_err(&serial->dev->dev,
  304. "Set MODEM CTRL 0x%x failed (error = %d)\n", mcr, rc);
  305. dbg("set_modem_ctrl: state=0x%x ==> mcr=0x%x", control_state, mcr);
  306. return rc;
  307. } /* mct_u232_set_modem_ctrl */
  308. static int mct_u232_get_modem_stat(struct usb_serial *serial,
  309. unsigned char *msr)
  310. {
  311. int rc;
  312. rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  313. MCT_U232_GET_MODEM_STAT_REQUEST,
  314. MCT_U232_GET_REQUEST_TYPE,
  315. 0, 0, msr, MCT_U232_GET_MODEM_STAT_SIZE,
  316. WDR_TIMEOUT);
  317. if (rc < 0) {
  318. dev_err(&serial->dev->dev,
  319. "Get MODEM STATus failed (error = %d)\n", rc);
  320. *msr = 0;
  321. }
  322. dbg("get_modem_stat: 0x%x", *msr);
  323. return rc;
  324. } /* mct_u232_get_modem_stat */
  325. static void mct_u232_msr_to_state(unsigned int *control_state,
  326. unsigned char msr)
  327. {
  328. /* Translate Control Line states */
  329. if (msr & MCT_U232_MSR_DSR)
  330. *control_state |= TIOCM_DSR;
  331. else
  332. *control_state &= ~TIOCM_DSR;
  333. if (msr & MCT_U232_MSR_CTS)
  334. *control_state |= TIOCM_CTS;
  335. else
  336. *control_state &= ~TIOCM_CTS;
  337. if (msr & MCT_U232_MSR_RI)
  338. *control_state |= TIOCM_RI;
  339. else
  340. *control_state &= ~TIOCM_RI;
  341. if (msr & MCT_U232_MSR_CD)
  342. *control_state |= TIOCM_CD;
  343. else
  344. *control_state &= ~TIOCM_CD;
  345. dbg("msr_to_state: msr=0x%x ==> state=0x%x", msr, *control_state);
  346. } /* mct_u232_msr_to_state */
  347. /*
  348. * Driver's tty interface functions
  349. */
  350. static int mct_u232_startup(struct usb_serial *serial)
  351. {
  352. struct mct_u232_private *priv;
  353. struct usb_serial_port *port, *rport;
  354. priv = kzalloc(sizeof(struct mct_u232_private), GFP_KERNEL);
  355. if (!priv)
  356. return -ENOMEM;
  357. spin_lock_init(&priv->lock);
  358. usb_set_serial_port_data(serial->port[0], priv);
  359. init_waitqueue_head(&serial->port[0]->write_wait);
  360. /* Puh, that's dirty */
  361. port = serial->port[0];
  362. rport = serial->port[1];
  363. /* No unlinking, it wasn't submitted yet. */
  364. usb_free_urb(port->read_urb);
  365. port->read_urb = rport->interrupt_in_urb;
  366. rport->interrupt_in_urb = NULL;
  367. port->read_urb->context = port;
  368. return 0;
  369. } /* mct_u232_startup */
  370. static void mct_u232_release(struct usb_serial *serial)
  371. {
  372. struct mct_u232_private *priv;
  373. int i;
  374. dbg("%s", __func__);
  375. for (i = 0; i < serial->num_ports; ++i) {
  376. /* My special items, the standard routines free my urbs */
  377. priv = usb_get_serial_port_data(serial->port[i]);
  378. kfree(priv);
  379. }
  380. } /* mct_u232_release */
  381. static int mct_u232_open(struct tty_struct *tty,
  382. struct usb_serial_port *port, struct file *filp)
  383. {
  384. struct usb_serial *serial = port->serial;
  385. struct mct_u232_private *priv = usb_get_serial_port_data(port);
  386. int retval = 0;
  387. unsigned int control_state;
  388. unsigned long flags;
  389. unsigned char last_lcr;
  390. unsigned char last_msr;
  391. dbg("%s port %d", __func__, port->number);
  392. /* Compensate for a hardware bug: although the Sitecom U232-P25
  393. * device reports a maximum output packet size of 32 bytes,
  394. * it seems to be able to accept only 16 bytes (and that's what
  395. * SniffUSB says too...)
  396. */
  397. if (le16_to_cpu(serial->dev->descriptor.idProduct)
  398. == MCT_U232_SITECOM_PID)
  399. port->bulk_out_size = 16;
  400. /* Do a defined restart: the normal serial device seems to
  401. * always turn on DTR and RTS here, so do the same. I'm not
  402. * sure if this is really necessary. But it should not harm
  403. * either.
  404. */
  405. spin_lock_irqsave(&priv->lock, flags);
  406. if (tty && (tty->termios->c_cflag & CBAUD))
  407. priv->control_state = TIOCM_DTR | TIOCM_RTS;
  408. else
  409. priv->control_state = 0;
  410. priv->last_lcr = (MCT_U232_DATA_BITS_8 |
  411. MCT_U232_PARITY_NONE |
  412. MCT_U232_STOP_BITS_1);
  413. control_state = priv->control_state;
  414. last_lcr = priv->last_lcr;
  415. spin_unlock_irqrestore(&priv->lock, flags);
  416. mct_u232_set_modem_ctrl(serial, control_state);
  417. mct_u232_set_line_ctrl(serial, last_lcr);
  418. /* Read modem status and update control state */
  419. mct_u232_get_modem_stat(serial, &last_msr);
  420. spin_lock_irqsave(&priv->lock, flags);
  421. priv->last_msr = last_msr;
  422. mct_u232_msr_to_state(&priv->control_state, priv->last_msr);
  423. spin_unlock_irqrestore(&priv->lock, flags);
  424. port->read_urb->dev = port->serial->dev;
  425. retval = usb_submit_urb(port->read_urb, GFP_KERNEL);
  426. if (retval) {
  427. dev_err(&port->dev,
  428. "usb_submit_urb(read bulk) failed pipe 0x%x err %d\n",
  429. port->read_urb->pipe, retval);
  430. goto error;
  431. }
  432. port->interrupt_in_urb->dev = port->serial->dev;
  433. retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  434. if (retval) {
  435. usb_kill_urb(port->read_urb);
  436. dev_err(&port->dev,
  437. "usb_submit_urb(read int) failed pipe 0x%x err %d",
  438. port->interrupt_in_urb->pipe, retval);
  439. goto error;
  440. }
  441. return 0;
  442. error:
  443. return retval;
  444. } /* mct_u232_open */
  445. static void mct_u232_dtr_rts(struct usb_serial_port *port, int on)
  446. {
  447. unsigned int control_state;
  448. struct mct_u232_private *priv = usb_get_serial_port_data(port);
  449. mutex_lock(&port->serial->disc_mutex);
  450. if (!port->serial->disconnected) {
  451. /* drop DTR and RTS */
  452. spin_lock_irq(&priv->lock);
  453. if (on)
  454. priv->control_state |= TIOCM_DTR | TIOCM_RTS;
  455. else
  456. priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
  457. control_state = priv->control_state;
  458. spin_unlock_irq(&priv->lock);
  459. mct_u232_set_modem_ctrl(port->serial, control_state);
  460. }
  461. mutex_unlock(&port->serial->disc_mutex);
  462. }
  463. static void mct_u232_close(struct usb_serial_port *port)
  464. {
  465. dbg("%s port %d", __func__, port->number);
  466. if (port->serial->dev) {
  467. /* shutdown our urbs */
  468. usb_kill_urb(port->write_urb);
  469. usb_kill_urb(port->read_urb);
  470. usb_kill_urb(port->interrupt_in_urb);
  471. }
  472. } /* mct_u232_close */
  473. static void mct_u232_read_int_callback(struct urb *urb)
  474. {
  475. struct usb_serial_port *port = urb->context;
  476. struct mct_u232_private *priv = usb_get_serial_port_data(port);
  477. struct usb_serial *serial = port->serial;
  478. struct tty_struct *tty;
  479. unsigned char *data = urb->transfer_buffer;
  480. int retval;
  481. int status = urb->status;
  482. unsigned long flags;
  483. switch (status) {
  484. case 0:
  485. /* success */
  486. break;
  487. case -ECONNRESET:
  488. case -ENOENT:
  489. case -ESHUTDOWN:
  490. /* this urb is terminated, clean up */
  491. dbg("%s - urb shutting down with status: %d",
  492. __func__, status);
  493. return;
  494. default:
  495. dbg("%s - nonzero urb status received: %d",
  496. __func__, status);
  497. goto exit;
  498. }
  499. if (!serial) {
  500. dbg("%s - bad serial pointer, exiting", __func__);
  501. return;
  502. }
  503. dbg("%s - port %d", __func__, port->number);
  504. usb_serial_debug_data(debug, &port->dev, __func__,
  505. urb->actual_length, data);
  506. /*
  507. * Work-a-round: handle the 'usual' bulk-in pipe here
  508. */
  509. if (urb->transfer_buffer_length > 2) {
  510. tty = tty_port_tty_get(&port->port);
  511. if (urb->actual_length) {
  512. tty_insert_flip_string(tty, data, urb->actual_length);
  513. tty_flip_buffer_push(tty);
  514. tty_kref_put(tty);
  515. }
  516. goto exit;
  517. }
  518. /*
  519. * The interrupt-in pipe signals exceptional conditions (modem line
  520. * signal changes and errors). data[0] holds MSR, data[1] holds LSR.
  521. */
  522. spin_lock_irqsave(&priv->lock, flags);
  523. priv->last_msr = data[MCT_U232_MSR_INDEX];
  524. /* Record Control Line states */
  525. mct_u232_msr_to_state(&priv->control_state, priv->last_msr);
  526. #if 0
  527. /* Not yet handled. See belkin_sa.c for further information */
  528. /* Now to report any errors */
  529. priv->last_lsr = data[MCT_U232_LSR_INDEX];
  530. /*
  531. * fill in the flip buffer here, but I do not know the relation
  532. * to the current/next receive buffer or characters. I need
  533. * to look in to this before committing any code.
  534. */
  535. if (priv->last_lsr & MCT_U232_LSR_ERR) {
  536. tty = tty_port_tty_get(&port->port);
  537. /* Overrun Error */
  538. if (priv->last_lsr & MCT_U232_LSR_OE) {
  539. }
  540. /* Parity Error */
  541. if (priv->last_lsr & MCT_U232_LSR_PE) {
  542. }
  543. /* Framing Error */
  544. if (priv->last_lsr & MCT_U232_LSR_FE) {
  545. }
  546. /* Break Indicator */
  547. if (priv->last_lsr & MCT_U232_LSR_BI) {
  548. }
  549. tty_kref_put(tty);
  550. }
  551. #endif
  552. spin_unlock_irqrestore(&priv->lock, flags);
  553. exit:
  554. retval = usb_submit_urb(urb, GFP_ATOMIC);
  555. if (retval)
  556. dev_err(&port->dev,
  557. "%s - usb_submit_urb failed with result %d\n",
  558. __func__, retval);
  559. } /* mct_u232_read_int_callback */
  560. static void mct_u232_set_termios(struct tty_struct *tty,
  561. struct usb_serial_port *port,
  562. struct ktermios *old_termios)
  563. {
  564. struct usb_serial *serial = port->serial;
  565. struct mct_u232_private *priv = usb_get_serial_port_data(port);
  566. struct ktermios *termios = tty->termios;
  567. unsigned int cflag = termios->c_cflag;
  568. unsigned int old_cflag = old_termios->c_cflag;
  569. unsigned long flags;
  570. unsigned int control_state;
  571. unsigned char last_lcr;
  572. /* get a local copy of the current port settings */
  573. spin_lock_irqsave(&priv->lock, flags);
  574. control_state = priv->control_state;
  575. spin_unlock_irqrestore(&priv->lock, flags);
  576. last_lcr = 0;
  577. /*
  578. * Update baud rate.
  579. * Do not attempt to cache old rates and skip settings,
  580. * disconnects screw such tricks up completely.
  581. * Premature optimization is the root of all evil.
  582. */
  583. /* reassert DTR and RTS on transition from B0 */
  584. if ((old_cflag & CBAUD) == B0) {
  585. dbg("%s: baud was B0", __func__);
  586. control_state |= TIOCM_DTR | TIOCM_RTS;
  587. mct_u232_set_modem_ctrl(serial, control_state);
  588. }
  589. mct_u232_set_baud_rate(tty, serial, port, tty_get_baud_rate(tty));
  590. if ((cflag & CBAUD) == B0) {
  591. dbg("%s: baud is B0", __func__);
  592. /* Drop RTS and DTR */
  593. control_state &= ~(TIOCM_DTR | TIOCM_RTS);
  594. mct_u232_set_modem_ctrl(serial, control_state);
  595. }
  596. /*
  597. * Update line control register (LCR)
  598. */
  599. /* set the parity */
  600. if (cflag & PARENB)
  601. last_lcr |= (cflag & PARODD) ?
  602. MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN;
  603. else
  604. last_lcr |= MCT_U232_PARITY_NONE;
  605. /* set the number of data bits */
  606. switch (cflag & CSIZE) {
  607. case CS5:
  608. last_lcr |= MCT_U232_DATA_BITS_5; break;
  609. case CS6:
  610. last_lcr |= MCT_U232_DATA_BITS_6; break;
  611. case CS7:
  612. last_lcr |= MCT_U232_DATA_BITS_7; break;
  613. case CS8:
  614. last_lcr |= MCT_U232_DATA_BITS_8; break;
  615. default:
  616. dev_err(&port->dev,
  617. "CSIZE was not CS5-CS8, using default of 8\n");
  618. last_lcr |= MCT_U232_DATA_BITS_8;
  619. break;
  620. }
  621. termios->c_cflag &= ~CMSPAR;
  622. /* set the number of stop bits */
  623. last_lcr |= (cflag & CSTOPB) ?
  624. MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;
  625. mct_u232_set_line_ctrl(serial, last_lcr);
  626. /* save off the modified port settings */
  627. spin_lock_irqsave(&priv->lock, flags);
  628. priv->control_state = control_state;
  629. priv->last_lcr = last_lcr;
  630. spin_unlock_irqrestore(&priv->lock, flags);
  631. } /* mct_u232_set_termios */
  632. static void mct_u232_break_ctl(struct tty_struct *tty, int break_state)
  633. {
  634. struct usb_serial_port *port = tty->driver_data;
  635. struct usb_serial *serial = port->serial;
  636. struct mct_u232_private *priv = usb_get_serial_port_data(port);
  637. unsigned char lcr;
  638. unsigned long flags;
  639. dbg("%sstate=%d", __func__, break_state);
  640. spin_lock_irqsave(&priv->lock, flags);
  641. lcr = priv->last_lcr;
  642. if (break_state)
  643. lcr |= MCT_U232_SET_BREAK;
  644. spin_unlock_irqrestore(&priv->lock, flags);
  645. mct_u232_set_line_ctrl(serial, lcr);
  646. } /* mct_u232_break_ctl */
  647. static int mct_u232_tiocmget(struct tty_struct *tty, struct file *file)
  648. {
  649. struct usb_serial_port *port = tty->driver_data;
  650. struct mct_u232_private *priv = usb_get_serial_port_data(port);
  651. unsigned int control_state;
  652. unsigned long flags;
  653. dbg("%s", __func__);
  654. spin_lock_irqsave(&priv->lock, flags);
  655. control_state = priv->control_state;
  656. spin_unlock_irqrestore(&priv->lock, flags);
  657. return control_state;
  658. }
  659. static int mct_u232_tiocmset(struct tty_struct *tty, struct file *file,
  660. unsigned int set, unsigned int clear)
  661. {
  662. struct usb_serial_port *port = tty->driver_data;
  663. struct usb_serial *serial = port->serial;
  664. struct mct_u232_private *priv = usb_get_serial_port_data(port);
  665. unsigned int control_state;
  666. unsigned long flags;
  667. dbg("%s", __func__);
  668. spin_lock_irqsave(&priv->lock, flags);
  669. control_state = priv->control_state;
  670. if (set & TIOCM_RTS)
  671. control_state |= TIOCM_RTS;
  672. if (set & TIOCM_DTR)
  673. control_state |= TIOCM_DTR;
  674. if (clear & TIOCM_RTS)
  675. control_state &= ~TIOCM_RTS;
  676. if (clear & TIOCM_DTR)
  677. control_state &= ~TIOCM_DTR;
  678. priv->control_state = control_state;
  679. spin_unlock_irqrestore(&priv->lock, flags);
  680. return mct_u232_set_modem_ctrl(serial, control_state);
  681. }
  682. static void mct_u232_throttle(struct tty_struct *tty)
  683. {
  684. struct usb_serial_port *port = tty->driver_data;
  685. struct mct_u232_private *priv = usb_get_serial_port_data(port);
  686. unsigned long flags;
  687. unsigned int control_state;
  688. dbg("%s - port %d", __func__, port->number);
  689. spin_lock_irqsave(&priv->lock, flags);
  690. priv->rx_flags |= THROTTLED;
  691. if (C_CRTSCTS(tty)) {
  692. priv->control_state &= ~TIOCM_RTS;
  693. control_state = priv->control_state;
  694. spin_unlock_irqrestore(&priv->lock, flags);
  695. (void) mct_u232_set_modem_ctrl(port->serial, control_state);
  696. } else {
  697. spin_unlock_irqrestore(&priv->lock, flags);
  698. }
  699. }
  700. static void mct_u232_unthrottle(struct tty_struct *tty)
  701. {
  702. struct usb_serial_port *port = tty->driver_data;
  703. struct mct_u232_private *priv = usb_get_serial_port_data(port);
  704. unsigned long flags;
  705. unsigned int control_state;
  706. dbg("%s - port %d", __func__, port->number);
  707. spin_lock_irqsave(&priv->lock, flags);
  708. if ((priv->rx_flags & THROTTLED) && C_CRTSCTS(tty)) {
  709. priv->rx_flags &= ~THROTTLED;
  710. priv->control_state |= TIOCM_RTS;
  711. control_state = priv->control_state;
  712. spin_unlock_irqrestore(&priv->lock, flags);
  713. (void) mct_u232_set_modem_ctrl(port->serial, control_state);
  714. } else {
  715. spin_unlock_irqrestore(&priv->lock, flags);
  716. }
  717. }
  718. static int __init mct_u232_init(void)
  719. {
  720. int retval;
  721. retval = usb_serial_register(&mct_u232_device);
  722. if (retval)
  723. goto failed_usb_serial_register;
  724. retval = usb_register(&mct_u232_driver);
  725. if (retval)
  726. goto failed_usb_register;
  727. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  728. DRIVER_DESC "\n");
  729. return 0;
  730. failed_usb_register:
  731. usb_serial_deregister(&mct_u232_device);
  732. failed_usb_serial_register:
  733. return retval;
  734. }
  735. static void __exit mct_u232_exit(void)
  736. {
  737. usb_deregister(&mct_u232_driver);
  738. usb_serial_deregister(&mct_u232_device);
  739. }
  740. module_init(mct_u232_init);
  741. module_exit(mct_u232_exit);
  742. MODULE_AUTHOR(DRIVER_AUTHOR);
  743. MODULE_DESCRIPTION(DRIVER_DESC);
  744. MODULE_LICENSE("GPL");
  745. module_param(debug, bool, S_IRUGO | S_IWUSR);
  746. MODULE_PARM_DESC(debug, "Debug enabled or not");