mct_u232.c 22 KB

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