ark3116.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2009 by Bart Hartgers (bart.hartgers+ark3116@gmail.com)
  4. * Original version:
  5. * Copyright (C) 2006
  6. * Simon Schulz (ark3116_driver <at> auctionant.de)
  7. *
  8. * ark3116
  9. * - implements a driver for the arkmicro ark3116 chipset (vendor=0x6547,
  10. * productid=0x0232) (used in a datacable called KQ-U8A)
  11. *
  12. * Supports full modem status lines, break, hardware flow control. Does not
  13. * support software flow control, since I do not know how to enable it in hw.
  14. *
  15. * This driver is a essentially new implementation. I initially dug
  16. * into the old ark3116.c driver and suddenly realized the ark3116 is
  17. * a 16450 with a USB interface glued to it. See comments at the
  18. * bottom of this file.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/ioctl.h>
  22. #include <linux/tty.h>
  23. #include <linux/slab.h>
  24. #include <linux/tty_flip.h>
  25. #include <linux/module.h>
  26. #include <linux/usb.h>
  27. #include <linux/usb/serial.h>
  28. #include <linux/serial.h>
  29. #include <linux/serial_reg.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/mutex.h>
  32. #include <linux/spinlock.h>
  33. #define DRIVER_AUTHOR "Bart Hartgers <bart.hartgers+ark3116@gmail.com>"
  34. #define DRIVER_DESC "USB ARK3116 serial/IrDA driver"
  35. #define DRIVER_DEV_DESC "ARK3116 RS232/IrDA"
  36. #define DRIVER_NAME "ark3116"
  37. /* usb timeout of 1 second */
  38. #define ARK_TIMEOUT 1000
  39. static const struct usb_device_id id_table[] = {
  40. { USB_DEVICE(0x6547, 0x0232) },
  41. { USB_DEVICE(0x18ec, 0x3118) }, /* USB to IrDA adapter */
  42. { },
  43. };
  44. MODULE_DEVICE_TABLE(usb, id_table);
  45. static int is_irda(struct usb_serial *serial)
  46. {
  47. struct usb_device *dev = serial->dev;
  48. if (le16_to_cpu(dev->descriptor.idVendor) == 0x18ec &&
  49. le16_to_cpu(dev->descriptor.idProduct) == 0x3118)
  50. return 1;
  51. return 0;
  52. }
  53. struct ark3116_private {
  54. int irda; /* 1 for irda device */
  55. /* protects hw register updates */
  56. struct mutex hw_lock;
  57. int quot; /* baudrate divisor */
  58. __u32 lcr; /* line control register value */
  59. __u32 hcr; /* handshake control register (0x8)
  60. * value */
  61. __u32 mcr; /* modem control register value */
  62. /* protects the status values below */
  63. spinlock_t status_lock;
  64. __u32 msr; /* modem status register value */
  65. __u32 lsr; /* line status register value */
  66. };
  67. static int ark3116_write_reg(struct usb_serial *serial,
  68. unsigned reg, __u8 val)
  69. {
  70. int result;
  71. /* 0xfe 0x40 are magic values taken from original driver */
  72. result = usb_control_msg(serial->dev,
  73. usb_sndctrlpipe(serial->dev, 0),
  74. 0xfe, 0x40, val, reg,
  75. NULL, 0, ARK_TIMEOUT);
  76. if (result)
  77. return result;
  78. return 0;
  79. }
  80. static int ark3116_read_reg(struct usb_serial *serial,
  81. unsigned reg, unsigned char *buf)
  82. {
  83. int result;
  84. /* 0xfe 0xc0 are magic values taken from original driver */
  85. result = usb_control_msg(serial->dev,
  86. usb_rcvctrlpipe(serial->dev, 0),
  87. 0xfe, 0xc0, 0, reg,
  88. buf, 1, ARK_TIMEOUT);
  89. if (result < 1) {
  90. dev_err(&serial->interface->dev,
  91. "failed to read register %u: %d\n",
  92. reg, result);
  93. if (result >= 0)
  94. result = -EIO;
  95. return result;
  96. }
  97. return 0;
  98. }
  99. static inline int calc_divisor(int bps)
  100. {
  101. /* Original ark3116 made some exceptions in rounding here
  102. * because windows did the same. Assume that is not really
  103. * necessary.
  104. * Crystal is 12MHz, probably because of USB, but we divide by 4?
  105. */
  106. return (12000000 + 2*bps) / (4*bps);
  107. }
  108. static int ark3116_port_probe(struct usb_serial_port *port)
  109. {
  110. struct usb_serial *serial = port->serial;
  111. struct ark3116_private *priv;
  112. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  113. if (!priv)
  114. return -ENOMEM;
  115. mutex_init(&priv->hw_lock);
  116. spin_lock_init(&priv->status_lock);
  117. priv->irda = is_irda(serial);
  118. usb_set_serial_port_data(port, priv);
  119. /* setup the hardware */
  120. ark3116_write_reg(serial, UART_IER, 0);
  121. /* disable DMA */
  122. ark3116_write_reg(serial, UART_FCR, 0);
  123. /* handshake control */
  124. priv->hcr = 0;
  125. ark3116_write_reg(serial, 0x8 , 0);
  126. /* modem control */
  127. priv->mcr = 0;
  128. ark3116_write_reg(serial, UART_MCR, 0);
  129. if (!(priv->irda)) {
  130. ark3116_write_reg(serial, 0xb , 0);
  131. } else {
  132. ark3116_write_reg(serial, 0xb , 1);
  133. ark3116_write_reg(serial, 0xc , 0);
  134. ark3116_write_reg(serial, 0xd , 0x41);
  135. ark3116_write_reg(serial, 0xa , 1);
  136. }
  137. /* setup baudrate */
  138. ark3116_write_reg(serial, UART_LCR, UART_LCR_DLAB);
  139. /* setup for 9600 8N1 */
  140. priv->quot = calc_divisor(9600);
  141. ark3116_write_reg(serial, UART_DLL, priv->quot & 0xff);
  142. ark3116_write_reg(serial, UART_DLM, (priv->quot>>8) & 0xff);
  143. priv->lcr = UART_LCR_WLEN8;
  144. ark3116_write_reg(serial, UART_LCR, UART_LCR_WLEN8);
  145. ark3116_write_reg(serial, 0xe, 0);
  146. if (priv->irda)
  147. ark3116_write_reg(serial, 0x9, 0);
  148. dev_info(&port->dev, "using %s mode\n", priv->irda ? "IrDA" : "RS232");
  149. return 0;
  150. }
  151. static int ark3116_port_remove(struct usb_serial_port *port)
  152. {
  153. struct ark3116_private *priv = usb_get_serial_port_data(port);
  154. /* device is closed, so URBs and DMA should be down */
  155. mutex_destroy(&priv->hw_lock);
  156. kfree(priv);
  157. return 0;
  158. }
  159. static void ark3116_init_termios(struct tty_struct *tty)
  160. {
  161. struct ktermios *termios = &tty->termios;
  162. *termios = tty_std_termios;
  163. termios->c_cflag = B9600 | CS8
  164. | CREAD | HUPCL | CLOCAL;
  165. termios->c_ispeed = 9600;
  166. termios->c_ospeed = 9600;
  167. }
  168. static void ark3116_set_termios(struct tty_struct *tty,
  169. struct usb_serial_port *port,
  170. struct ktermios *old_termios)
  171. {
  172. struct usb_serial *serial = port->serial;
  173. struct ark3116_private *priv = usb_get_serial_port_data(port);
  174. struct ktermios *termios = &tty->termios;
  175. unsigned int cflag = termios->c_cflag;
  176. int bps = tty_get_baud_rate(tty);
  177. int quot;
  178. __u8 lcr, hcr, eval;
  179. /* set data bit count */
  180. switch (cflag & CSIZE) {
  181. case CS5:
  182. lcr = UART_LCR_WLEN5;
  183. break;
  184. case CS6:
  185. lcr = UART_LCR_WLEN6;
  186. break;
  187. case CS7:
  188. lcr = UART_LCR_WLEN7;
  189. break;
  190. default:
  191. case CS8:
  192. lcr = UART_LCR_WLEN8;
  193. break;
  194. }
  195. if (cflag & CSTOPB)
  196. lcr |= UART_LCR_STOP;
  197. if (cflag & PARENB)
  198. lcr |= UART_LCR_PARITY;
  199. if (!(cflag & PARODD))
  200. lcr |= UART_LCR_EPAR;
  201. #ifdef CMSPAR
  202. if (cflag & CMSPAR)
  203. lcr |= UART_LCR_SPAR;
  204. #endif
  205. /* handshake control */
  206. hcr = (cflag & CRTSCTS) ? 0x03 : 0x00;
  207. /* calc baudrate */
  208. dev_dbg(&port->dev, "%s - setting bps to %d\n", __func__, bps);
  209. eval = 0;
  210. switch (bps) {
  211. case 0:
  212. quot = calc_divisor(9600);
  213. break;
  214. default:
  215. if ((bps < 75) || (bps > 3000000))
  216. bps = 9600;
  217. quot = calc_divisor(bps);
  218. break;
  219. case 460800:
  220. eval = 1;
  221. quot = calc_divisor(bps);
  222. break;
  223. case 921600:
  224. eval = 2;
  225. quot = calc_divisor(bps);
  226. break;
  227. }
  228. /* Update state: synchronize */
  229. mutex_lock(&priv->hw_lock);
  230. /* keep old LCR_SBC bit */
  231. lcr |= (priv->lcr & UART_LCR_SBC);
  232. dev_dbg(&port->dev, "%s - setting hcr:0x%02x,lcr:0x%02x,quot:%d\n",
  233. __func__, hcr, lcr, quot);
  234. /* handshake control */
  235. if (priv->hcr != hcr) {
  236. priv->hcr = hcr;
  237. ark3116_write_reg(serial, 0x8, hcr);
  238. }
  239. /* baudrate */
  240. if (priv->quot != quot) {
  241. priv->quot = quot;
  242. priv->lcr = lcr; /* need to write lcr anyway */
  243. /* disable DMA since transmit/receive is
  244. * shadowed by UART_DLL
  245. */
  246. ark3116_write_reg(serial, UART_FCR, 0);
  247. ark3116_write_reg(serial, UART_LCR,
  248. lcr|UART_LCR_DLAB);
  249. ark3116_write_reg(serial, UART_DLL, quot & 0xff);
  250. ark3116_write_reg(serial, UART_DLM, (quot>>8) & 0xff);
  251. /* restore lcr */
  252. ark3116_write_reg(serial, UART_LCR, lcr);
  253. /* magic baudrate thingy: not sure what it does,
  254. * but windows does this as well.
  255. */
  256. ark3116_write_reg(serial, 0xe, eval);
  257. /* enable DMA */
  258. ark3116_write_reg(serial, UART_FCR, UART_FCR_DMA_SELECT);
  259. } else if (priv->lcr != lcr) {
  260. priv->lcr = lcr;
  261. ark3116_write_reg(serial, UART_LCR, lcr);
  262. }
  263. mutex_unlock(&priv->hw_lock);
  264. /* check for software flow control */
  265. if (I_IXOFF(tty) || I_IXON(tty)) {
  266. dev_warn(&port->dev,
  267. "software flow control not implemented\n");
  268. }
  269. /* Don't rewrite B0 */
  270. if (tty_termios_baud_rate(termios))
  271. tty_termios_encode_baud_rate(termios, bps, bps);
  272. }
  273. static void ark3116_close(struct usb_serial_port *port)
  274. {
  275. struct usb_serial *serial = port->serial;
  276. /* disable DMA */
  277. ark3116_write_reg(serial, UART_FCR, 0);
  278. /* deactivate interrupts */
  279. ark3116_write_reg(serial, UART_IER, 0);
  280. usb_serial_generic_close(port);
  281. usb_kill_urb(port->interrupt_in_urb);
  282. }
  283. static int ark3116_open(struct tty_struct *tty, struct usb_serial_port *port)
  284. {
  285. struct ark3116_private *priv = usb_get_serial_port_data(port);
  286. struct usb_serial *serial = port->serial;
  287. unsigned char *buf;
  288. int result;
  289. buf = kmalloc(1, GFP_KERNEL);
  290. if (buf == NULL)
  291. return -ENOMEM;
  292. result = usb_serial_generic_open(tty, port);
  293. if (result) {
  294. dev_dbg(&port->dev,
  295. "%s - usb_serial_generic_open failed: %d\n",
  296. __func__, result);
  297. goto err_free;
  298. }
  299. /* remove any data still left: also clears error state */
  300. ark3116_read_reg(serial, UART_RX, buf);
  301. /* read modem status */
  302. result = ark3116_read_reg(serial, UART_MSR, buf);
  303. if (result)
  304. goto err_close;
  305. priv->msr = *buf;
  306. /* read line status */
  307. result = ark3116_read_reg(serial, UART_LSR, buf);
  308. if (result)
  309. goto err_close;
  310. priv->lsr = *buf;
  311. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  312. if (result) {
  313. dev_err(&port->dev, "submit irq_in urb failed %d\n",
  314. result);
  315. goto err_close;
  316. }
  317. /* activate interrupts */
  318. ark3116_write_reg(port->serial, UART_IER, UART_IER_MSI|UART_IER_RLSI);
  319. /* enable DMA */
  320. ark3116_write_reg(port->serial, UART_FCR, UART_FCR_DMA_SELECT);
  321. /* setup termios */
  322. if (tty)
  323. ark3116_set_termios(tty, port, NULL);
  324. kfree(buf);
  325. return 0;
  326. err_close:
  327. usb_serial_generic_close(port);
  328. err_free:
  329. kfree(buf);
  330. return result;
  331. }
  332. static int ark3116_get_serial_info(struct tty_struct *tty,
  333. struct serial_struct *ss)
  334. {
  335. struct usb_serial_port *port = tty->driver_data;
  336. ss->type = PORT_16654;
  337. ss->line = port->minor;
  338. ss->port = port->port_number;
  339. ss->baud_base = 460800;
  340. return 0;
  341. }
  342. static int ark3116_tiocmget(struct tty_struct *tty)
  343. {
  344. struct usb_serial_port *port = tty->driver_data;
  345. struct ark3116_private *priv = usb_get_serial_port_data(port);
  346. __u32 status;
  347. __u32 ctrl;
  348. unsigned long flags;
  349. mutex_lock(&priv->hw_lock);
  350. ctrl = priv->mcr;
  351. mutex_unlock(&priv->hw_lock);
  352. spin_lock_irqsave(&priv->status_lock, flags);
  353. status = priv->msr;
  354. spin_unlock_irqrestore(&priv->status_lock, flags);
  355. return (status & UART_MSR_DSR ? TIOCM_DSR : 0) |
  356. (status & UART_MSR_CTS ? TIOCM_CTS : 0) |
  357. (status & UART_MSR_RI ? TIOCM_RI : 0) |
  358. (status & UART_MSR_DCD ? TIOCM_CD : 0) |
  359. (ctrl & UART_MCR_DTR ? TIOCM_DTR : 0) |
  360. (ctrl & UART_MCR_RTS ? TIOCM_RTS : 0) |
  361. (ctrl & UART_MCR_OUT1 ? TIOCM_OUT1 : 0) |
  362. (ctrl & UART_MCR_OUT2 ? TIOCM_OUT2 : 0);
  363. }
  364. static int ark3116_tiocmset(struct tty_struct *tty,
  365. unsigned set, unsigned clr)
  366. {
  367. struct usb_serial_port *port = tty->driver_data;
  368. struct ark3116_private *priv = usb_get_serial_port_data(port);
  369. /* we need to take the mutex here, to make sure that the value
  370. * in priv->mcr is actually the one that is in the hardware
  371. */
  372. mutex_lock(&priv->hw_lock);
  373. if (set & TIOCM_RTS)
  374. priv->mcr |= UART_MCR_RTS;
  375. if (set & TIOCM_DTR)
  376. priv->mcr |= UART_MCR_DTR;
  377. if (set & TIOCM_OUT1)
  378. priv->mcr |= UART_MCR_OUT1;
  379. if (set & TIOCM_OUT2)
  380. priv->mcr |= UART_MCR_OUT2;
  381. if (clr & TIOCM_RTS)
  382. priv->mcr &= ~UART_MCR_RTS;
  383. if (clr & TIOCM_DTR)
  384. priv->mcr &= ~UART_MCR_DTR;
  385. if (clr & TIOCM_OUT1)
  386. priv->mcr &= ~UART_MCR_OUT1;
  387. if (clr & TIOCM_OUT2)
  388. priv->mcr &= ~UART_MCR_OUT2;
  389. ark3116_write_reg(port->serial, UART_MCR, priv->mcr);
  390. mutex_unlock(&priv->hw_lock);
  391. return 0;
  392. }
  393. static void ark3116_break_ctl(struct tty_struct *tty, int break_state)
  394. {
  395. struct usb_serial_port *port = tty->driver_data;
  396. struct ark3116_private *priv = usb_get_serial_port_data(port);
  397. /* LCR is also used for other things: protect access */
  398. mutex_lock(&priv->hw_lock);
  399. if (break_state)
  400. priv->lcr |= UART_LCR_SBC;
  401. else
  402. priv->lcr &= ~UART_LCR_SBC;
  403. ark3116_write_reg(port->serial, UART_LCR, priv->lcr);
  404. mutex_unlock(&priv->hw_lock);
  405. }
  406. static void ark3116_update_msr(struct usb_serial_port *port, __u8 msr)
  407. {
  408. struct ark3116_private *priv = usb_get_serial_port_data(port);
  409. unsigned long flags;
  410. spin_lock_irqsave(&priv->status_lock, flags);
  411. priv->msr = msr;
  412. spin_unlock_irqrestore(&priv->status_lock, flags);
  413. if (msr & UART_MSR_ANY_DELTA) {
  414. /* update input line counters */
  415. if (msr & UART_MSR_DCTS)
  416. port->icount.cts++;
  417. if (msr & UART_MSR_DDSR)
  418. port->icount.dsr++;
  419. if (msr & UART_MSR_DDCD)
  420. port->icount.dcd++;
  421. if (msr & UART_MSR_TERI)
  422. port->icount.rng++;
  423. wake_up_interruptible(&port->port.delta_msr_wait);
  424. }
  425. }
  426. static void ark3116_update_lsr(struct usb_serial_port *port, __u8 lsr)
  427. {
  428. struct ark3116_private *priv = usb_get_serial_port_data(port);
  429. unsigned long flags;
  430. spin_lock_irqsave(&priv->status_lock, flags);
  431. /* combine bits */
  432. priv->lsr |= lsr;
  433. spin_unlock_irqrestore(&priv->status_lock, flags);
  434. if (lsr&UART_LSR_BRK_ERROR_BITS) {
  435. if (lsr & UART_LSR_BI)
  436. port->icount.brk++;
  437. if (lsr & UART_LSR_FE)
  438. port->icount.frame++;
  439. if (lsr & UART_LSR_PE)
  440. port->icount.parity++;
  441. if (lsr & UART_LSR_OE)
  442. port->icount.overrun++;
  443. }
  444. }
  445. static void ark3116_read_int_callback(struct urb *urb)
  446. {
  447. struct usb_serial_port *port = urb->context;
  448. int status = urb->status;
  449. const __u8 *data = urb->transfer_buffer;
  450. int result;
  451. switch (status) {
  452. case -ECONNRESET:
  453. case -ENOENT:
  454. case -ESHUTDOWN:
  455. /* this urb is terminated, clean up */
  456. dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
  457. __func__, status);
  458. return;
  459. default:
  460. dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
  461. __func__, status);
  462. break;
  463. case 0: /* success */
  464. /* discovered this by trail and error... */
  465. if ((urb->actual_length == 4) && (data[0] == 0xe8)) {
  466. const __u8 id = data[1]&UART_IIR_ID;
  467. dev_dbg(&port->dev, "%s: iir=%02x\n", __func__, data[1]);
  468. if (id == UART_IIR_MSI) {
  469. dev_dbg(&port->dev, "%s: msr=%02x\n",
  470. __func__, data[3]);
  471. ark3116_update_msr(port, data[3]);
  472. break;
  473. } else if (id == UART_IIR_RLSI) {
  474. dev_dbg(&port->dev, "%s: lsr=%02x\n",
  475. __func__, data[2]);
  476. ark3116_update_lsr(port, data[2]);
  477. break;
  478. }
  479. }
  480. /*
  481. * Not sure what this data meant...
  482. */
  483. usb_serial_debug_data(&port->dev, __func__,
  484. urb->actual_length,
  485. urb->transfer_buffer);
  486. break;
  487. }
  488. result = usb_submit_urb(urb, GFP_ATOMIC);
  489. if (result)
  490. dev_err(&port->dev, "failed to resubmit interrupt urb: %d\n",
  491. result);
  492. }
  493. /* Data comes in via the bulk (data) URB, errors/interrupts via the int URB.
  494. * This means that we cannot be sure which data byte has an associated error
  495. * condition, so we report an error for all data in the next bulk read.
  496. *
  497. * Actually, there might even be a window between the bulk data leaving the
  498. * ark and reading/resetting the lsr in the read_bulk_callback where an
  499. * interrupt for the next data block could come in.
  500. * Without somekind of ordering on the ark, we would have to report the
  501. * error for the next block of data as well...
  502. * For now, let's pretend this can't happen.
  503. */
  504. static void ark3116_process_read_urb(struct urb *urb)
  505. {
  506. struct usb_serial_port *port = urb->context;
  507. struct ark3116_private *priv = usb_get_serial_port_data(port);
  508. unsigned char *data = urb->transfer_buffer;
  509. char tty_flag = TTY_NORMAL;
  510. unsigned long flags;
  511. __u32 lsr;
  512. /* update line status */
  513. spin_lock_irqsave(&priv->status_lock, flags);
  514. lsr = priv->lsr;
  515. priv->lsr &= ~UART_LSR_BRK_ERROR_BITS;
  516. spin_unlock_irqrestore(&priv->status_lock, flags);
  517. if (!urb->actual_length)
  518. return;
  519. if (lsr & UART_LSR_BRK_ERROR_BITS) {
  520. if (lsr & UART_LSR_BI)
  521. tty_flag = TTY_BREAK;
  522. else if (lsr & UART_LSR_PE)
  523. tty_flag = TTY_PARITY;
  524. else if (lsr & UART_LSR_FE)
  525. tty_flag = TTY_FRAME;
  526. /* overrun is special, not associated with a char */
  527. if (lsr & UART_LSR_OE)
  528. tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
  529. }
  530. tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
  531. urb->actual_length);
  532. tty_flip_buffer_push(&port->port);
  533. }
  534. static struct usb_serial_driver ark3116_device = {
  535. .driver = {
  536. .owner = THIS_MODULE,
  537. .name = "ark3116",
  538. },
  539. .id_table = id_table,
  540. .num_ports = 1,
  541. .num_bulk_in = 1,
  542. .num_bulk_out = 1,
  543. .num_interrupt_in = 1,
  544. .port_probe = ark3116_port_probe,
  545. .port_remove = ark3116_port_remove,
  546. .set_termios = ark3116_set_termios,
  547. .init_termios = ark3116_init_termios,
  548. .get_serial = ark3116_get_serial_info,
  549. .tiocmget = ark3116_tiocmget,
  550. .tiocmset = ark3116_tiocmset,
  551. .tiocmiwait = usb_serial_generic_tiocmiwait,
  552. .get_icount = usb_serial_generic_get_icount,
  553. .open = ark3116_open,
  554. .close = ark3116_close,
  555. .break_ctl = ark3116_break_ctl,
  556. .read_int_callback = ark3116_read_int_callback,
  557. .process_read_urb = ark3116_process_read_urb,
  558. };
  559. static struct usb_serial_driver * const serial_drivers[] = {
  560. &ark3116_device, NULL
  561. };
  562. module_usb_serial_driver(serial_drivers, id_table);
  563. MODULE_LICENSE("GPL");
  564. MODULE_AUTHOR(DRIVER_AUTHOR);
  565. MODULE_DESCRIPTION(DRIVER_DESC);
  566. /*
  567. * The following describes what I learned from studying the old
  568. * ark3116.c driver, disassembling the windows driver, and some lucky
  569. * guesses. Since I do not have any datasheet or other
  570. * documentation, inaccuracies are almost guaranteed.
  571. *
  572. * Some specs for the ARK3116 can be found here:
  573. * http://web.archive.org/web/20060318000438/
  574. * www.arkmicro.com/en/products/view.php?id=10
  575. * On that page, 2 GPIO pins are mentioned: I assume these are the
  576. * OUT1 and OUT2 pins of the UART, so I added support for those
  577. * through the MCR. Since the pins are not available on my hardware,
  578. * I could not verify this.
  579. * Also, it states there is "on-chip hardware flow control". I have
  580. * discovered how to enable that. Unfortunately, I do not know how to
  581. * enable XON/XOFF (software) flow control, which would need support
  582. * from the chip as well to work. Because of the wording on the web
  583. * page there is a real possibility the chip simply does not support
  584. * software flow control.
  585. *
  586. * I got my ark3116 as part of a mobile phone adapter cable. On the
  587. * PCB, the following numbered contacts are present:
  588. *
  589. * 1:- +5V
  590. * 2:o DTR
  591. * 3:i RX
  592. * 4:i DCD
  593. * 5:o RTS
  594. * 6:o TX
  595. * 7:i RI
  596. * 8:i DSR
  597. * 10:- 0V
  598. * 11:i CTS
  599. *
  600. * On my chip, all signals seem to be 3.3V, but 5V tolerant. But that
  601. * may be different for the one you have ;-).
  602. *
  603. * The windows driver limits the registers to 0-F, so I assume there
  604. * are actually 16 present on the device.
  605. *
  606. * On an UART interrupt, 4 bytes of data come in on the interrupt
  607. * endpoint. The bytes are 0xe8 IIR LSR MSR.
  608. *
  609. * The baudrate seems to be generated from the 12MHz crystal, using
  610. * 4-times subsampling. So quot=12e6/(4*baud). Also see description
  611. * of register E.
  612. *
  613. * Registers 0-7:
  614. * These seem to be the same as for a regular 16450. The FCR is set
  615. * to UART_FCR_DMA_SELECT (0x8), I guess to enable transfers between
  616. * the UART and the USB bridge/DMA engine.
  617. *
  618. * Register 8:
  619. * By trial and error, I found out that bit 0 enables hardware CTS,
  620. * stopping TX when CTS is +5V. Bit 1 does the same for RTS, making
  621. * RTS +5V when the 3116 cannot transfer the data to the USB bus
  622. * (verified by disabling the reading URB). Note that as far as I can
  623. * tell, the windows driver does NOT use this, so there might be some
  624. * hardware bug or something.
  625. *
  626. * According to a patch provided here
  627. * (http://lkml.org/lkml/2009/7/26/56), the ARK3116 can also be used
  628. * as an IrDA dongle. Since I do not have such a thing, I could not
  629. * investigate that aspect. However, I can speculate ;-).
  630. *
  631. * - IrDA encodes data differently than RS232. Most likely, one of
  632. * the bits in registers 9..E enables the IR ENDEC (encoder/decoder).
  633. * - Depending on the IR transceiver, the input and output need to be
  634. * inverted, so there are probably bits for that as well.
  635. * - IrDA is half-duplex, so there should be a bit for selecting that.
  636. *
  637. * This still leaves at least two registers unaccounted for. Perhaps
  638. * The chip can do XON/XOFF or CRC in HW?
  639. *
  640. * Register 9:
  641. * Set to 0x00 for IrDA, when the baudrate is initialised.
  642. *
  643. * Register A:
  644. * Set to 0x01 for IrDA, at init.
  645. *
  646. * Register B:
  647. * Set to 0x01 for IrDA, 0x00 for RS232, at init.
  648. *
  649. * Register C:
  650. * Set to 00 for IrDA, at init.
  651. *
  652. * Register D:
  653. * Set to 0x41 for IrDA, at init.
  654. *
  655. * Register E:
  656. * Somekind of baudrate override. The windows driver seems to set
  657. * this to 0x00 for normal baudrates, 0x01 for 460800, 0x02 for 921600.
  658. * Since 460800 and 921600 cannot be obtained by dividing 3MHz by an integer,
  659. * it could be somekind of subdivisor thingy.
  660. * However,it does not seem to do anything: selecting 921600 (divisor 3,
  661. * reg E=2), still gets 1 MHz. I also checked if registers 9, C or F would
  662. * work, but they don't.
  663. *
  664. * Register F: unknown
  665. */