mct_u232.c 26 KB

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