ark3116.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2009 by Bart Hartgers ([email protected])
  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 <[email protected]>"
  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 void 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. }
  158. static void ark3116_set_termios(struct tty_struct *tty,
  159. struct usb_serial_port *port,
  160. const struct ktermios *old_termios)
  161. {
  162. struct usb_serial *serial = port->serial;
  163. struct ark3116_private *priv = usb_get_serial_port_data(port);
  164. struct ktermios *termios = &tty->termios;
  165. unsigned int cflag = termios->c_cflag;
  166. int bps = tty_get_baud_rate(tty);
  167. int quot;
  168. __u8 lcr, hcr, eval;
  169. /* set data bit count */
  170. lcr = UART_LCR_WLEN(tty_get_char_size(cflag));
  171. if (cflag & CSTOPB)
  172. lcr |= UART_LCR_STOP;
  173. if (cflag & PARENB)
  174. lcr |= UART_LCR_PARITY;
  175. if (!(cflag & PARODD))
  176. lcr |= UART_LCR_EPAR;
  177. if (cflag & CMSPAR)
  178. lcr |= UART_LCR_SPAR;
  179. /* handshake control */
  180. hcr = (cflag & CRTSCTS) ? 0x03 : 0x00;
  181. /* calc baudrate */
  182. dev_dbg(&port->dev, "%s - setting bps to %d\n", __func__, bps);
  183. eval = 0;
  184. switch (bps) {
  185. case 0:
  186. quot = calc_divisor(9600);
  187. break;
  188. default:
  189. if ((bps < 75) || (bps > 3000000))
  190. bps = 9600;
  191. quot = calc_divisor(bps);
  192. break;
  193. case 460800:
  194. eval = 1;
  195. quot = calc_divisor(bps);
  196. break;
  197. case 921600:
  198. eval = 2;
  199. quot = calc_divisor(bps);
  200. break;
  201. }
  202. /* Update state: synchronize */
  203. mutex_lock(&priv->hw_lock);
  204. /* keep old LCR_SBC bit */
  205. lcr |= (priv->lcr & UART_LCR_SBC);
  206. dev_dbg(&port->dev, "%s - setting hcr:0x%02x,lcr:0x%02x,quot:%d\n",
  207. __func__, hcr, lcr, quot);
  208. /* handshake control */
  209. if (priv->hcr != hcr) {
  210. priv->hcr = hcr;
  211. ark3116_write_reg(serial, 0x8, hcr);
  212. }
  213. /* baudrate */
  214. if (priv->quot != quot) {
  215. priv->quot = quot;
  216. priv->lcr = lcr; /* need to write lcr anyway */
  217. /* disable DMA since transmit/receive is
  218. * shadowed by UART_DLL
  219. */
  220. ark3116_write_reg(serial, UART_FCR, 0);
  221. ark3116_write_reg(serial, UART_LCR,
  222. lcr|UART_LCR_DLAB);
  223. ark3116_write_reg(serial, UART_DLL, quot & 0xff);
  224. ark3116_write_reg(serial, UART_DLM, (quot>>8) & 0xff);
  225. /* restore lcr */
  226. ark3116_write_reg(serial, UART_LCR, lcr);
  227. /* magic baudrate thingy: not sure what it does,
  228. * but windows does this as well.
  229. */
  230. ark3116_write_reg(serial, 0xe, eval);
  231. /* enable DMA */
  232. ark3116_write_reg(serial, UART_FCR, UART_FCR_DMA_SELECT);
  233. } else if (priv->lcr != lcr) {
  234. priv->lcr = lcr;
  235. ark3116_write_reg(serial, UART_LCR, lcr);
  236. }
  237. mutex_unlock(&priv->hw_lock);
  238. /* check for software flow control */
  239. if (I_IXOFF(tty) || I_IXON(tty)) {
  240. dev_warn(&port->dev,
  241. "software flow control not implemented\n");
  242. }
  243. /* Don't rewrite B0 */
  244. if (tty_termios_baud_rate(termios))
  245. tty_termios_encode_baud_rate(termios, bps, bps);
  246. }
  247. static void ark3116_close(struct usb_serial_port *port)
  248. {
  249. struct usb_serial *serial = port->serial;
  250. /* disable DMA */
  251. ark3116_write_reg(serial, UART_FCR, 0);
  252. /* deactivate interrupts */
  253. ark3116_write_reg(serial, UART_IER, 0);
  254. usb_serial_generic_close(port);
  255. usb_kill_urb(port->interrupt_in_urb);
  256. }
  257. static int ark3116_open(struct tty_struct *tty, struct usb_serial_port *port)
  258. {
  259. struct ark3116_private *priv = usb_get_serial_port_data(port);
  260. struct usb_serial *serial = port->serial;
  261. unsigned char *buf;
  262. int result;
  263. buf = kmalloc(1, GFP_KERNEL);
  264. if (buf == NULL)
  265. return -ENOMEM;
  266. result = usb_serial_generic_open(tty, port);
  267. if (result) {
  268. dev_dbg(&port->dev,
  269. "%s - usb_serial_generic_open failed: %d\n",
  270. __func__, result);
  271. goto err_free;
  272. }
  273. /* remove any data still left: also clears error state */
  274. ark3116_read_reg(serial, UART_RX, buf);
  275. /* read modem status */
  276. result = ark3116_read_reg(serial, UART_MSR, buf);
  277. if (result)
  278. goto err_close;
  279. priv->msr = *buf;
  280. /* read line status */
  281. result = ark3116_read_reg(serial, UART_LSR, buf);
  282. if (result)
  283. goto err_close;
  284. priv->lsr = *buf;
  285. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  286. if (result) {
  287. dev_err(&port->dev, "submit irq_in urb failed %d\n",
  288. result);
  289. goto err_close;
  290. }
  291. /* activate interrupts */
  292. ark3116_write_reg(port->serial, UART_IER, UART_IER_MSI|UART_IER_RLSI);
  293. /* enable DMA */
  294. ark3116_write_reg(port->serial, UART_FCR, UART_FCR_DMA_SELECT);
  295. /* setup termios */
  296. if (tty)
  297. ark3116_set_termios(tty, port, NULL);
  298. kfree(buf);
  299. return 0;
  300. err_close:
  301. usb_serial_generic_close(port);
  302. err_free:
  303. kfree(buf);
  304. return result;
  305. }
  306. static int ark3116_tiocmget(struct tty_struct *tty)
  307. {
  308. struct usb_serial_port *port = tty->driver_data;
  309. struct ark3116_private *priv = usb_get_serial_port_data(port);
  310. __u32 status;
  311. __u32 ctrl;
  312. unsigned long flags;
  313. mutex_lock(&priv->hw_lock);
  314. ctrl = priv->mcr;
  315. mutex_unlock(&priv->hw_lock);
  316. spin_lock_irqsave(&priv->status_lock, flags);
  317. status = priv->msr;
  318. spin_unlock_irqrestore(&priv->status_lock, flags);
  319. return (status & UART_MSR_DSR ? TIOCM_DSR : 0) |
  320. (status & UART_MSR_CTS ? TIOCM_CTS : 0) |
  321. (status & UART_MSR_RI ? TIOCM_RI : 0) |
  322. (status & UART_MSR_DCD ? TIOCM_CD : 0) |
  323. (ctrl & UART_MCR_DTR ? TIOCM_DTR : 0) |
  324. (ctrl & UART_MCR_RTS ? TIOCM_RTS : 0) |
  325. (ctrl & UART_MCR_OUT1 ? TIOCM_OUT1 : 0) |
  326. (ctrl & UART_MCR_OUT2 ? TIOCM_OUT2 : 0);
  327. }
  328. static int ark3116_tiocmset(struct tty_struct *tty,
  329. unsigned set, unsigned clr)
  330. {
  331. struct usb_serial_port *port = tty->driver_data;
  332. struct ark3116_private *priv = usb_get_serial_port_data(port);
  333. /* we need to take the mutex here, to make sure that the value
  334. * in priv->mcr is actually the one that is in the hardware
  335. */
  336. mutex_lock(&priv->hw_lock);
  337. if (set & TIOCM_RTS)
  338. priv->mcr |= UART_MCR_RTS;
  339. if (set & TIOCM_DTR)
  340. priv->mcr |= UART_MCR_DTR;
  341. if (set & TIOCM_OUT1)
  342. priv->mcr |= UART_MCR_OUT1;
  343. if (set & TIOCM_OUT2)
  344. priv->mcr |= UART_MCR_OUT2;
  345. if (clr & TIOCM_RTS)
  346. priv->mcr &= ~UART_MCR_RTS;
  347. if (clr & TIOCM_DTR)
  348. priv->mcr &= ~UART_MCR_DTR;
  349. if (clr & TIOCM_OUT1)
  350. priv->mcr &= ~UART_MCR_OUT1;
  351. if (clr & TIOCM_OUT2)
  352. priv->mcr &= ~UART_MCR_OUT2;
  353. ark3116_write_reg(port->serial, UART_MCR, priv->mcr);
  354. mutex_unlock(&priv->hw_lock);
  355. return 0;
  356. }
  357. static void ark3116_break_ctl(struct tty_struct *tty, int break_state)
  358. {
  359. struct usb_serial_port *port = tty->driver_data;
  360. struct ark3116_private *priv = usb_get_serial_port_data(port);
  361. /* LCR is also used for other things: protect access */
  362. mutex_lock(&priv->hw_lock);
  363. if (break_state)
  364. priv->lcr |= UART_LCR_SBC;
  365. else
  366. priv->lcr &= ~UART_LCR_SBC;
  367. ark3116_write_reg(port->serial, UART_LCR, priv->lcr);
  368. mutex_unlock(&priv->hw_lock);
  369. }
  370. static void ark3116_update_msr(struct usb_serial_port *port, __u8 msr)
  371. {
  372. struct ark3116_private *priv = usb_get_serial_port_data(port);
  373. unsigned long flags;
  374. spin_lock_irqsave(&priv->status_lock, flags);
  375. priv->msr = msr;
  376. spin_unlock_irqrestore(&priv->status_lock, flags);
  377. if (msr & UART_MSR_ANY_DELTA) {
  378. /* update input line counters */
  379. if (msr & UART_MSR_DCTS)
  380. port->icount.cts++;
  381. if (msr & UART_MSR_DDSR)
  382. port->icount.dsr++;
  383. if (msr & UART_MSR_DDCD)
  384. port->icount.dcd++;
  385. if (msr & UART_MSR_TERI)
  386. port->icount.rng++;
  387. wake_up_interruptible(&port->port.delta_msr_wait);
  388. }
  389. }
  390. static void ark3116_update_lsr(struct usb_serial_port *port, __u8 lsr)
  391. {
  392. struct ark3116_private *priv = usb_get_serial_port_data(port);
  393. unsigned long flags;
  394. spin_lock_irqsave(&priv->status_lock, flags);
  395. /* combine bits */
  396. priv->lsr |= lsr;
  397. spin_unlock_irqrestore(&priv->status_lock, flags);
  398. if (lsr&UART_LSR_BRK_ERROR_BITS) {
  399. if (lsr & UART_LSR_BI)
  400. port->icount.brk++;
  401. if (lsr & UART_LSR_FE)
  402. port->icount.frame++;
  403. if (lsr & UART_LSR_PE)
  404. port->icount.parity++;
  405. if (lsr & UART_LSR_OE)
  406. port->icount.overrun++;
  407. }
  408. }
  409. static void ark3116_read_int_callback(struct urb *urb)
  410. {
  411. struct usb_serial_port *port = urb->context;
  412. int status = urb->status;
  413. const __u8 *data = urb->transfer_buffer;
  414. int result;
  415. switch (status) {
  416. case -ECONNRESET:
  417. case -ENOENT:
  418. case -ESHUTDOWN:
  419. /* this urb is terminated, clean up */
  420. dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
  421. __func__, status);
  422. return;
  423. default:
  424. dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
  425. __func__, status);
  426. break;
  427. case 0: /* success */
  428. /* discovered this by trail and error... */
  429. if ((urb->actual_length == 4) && (data[0] == 0xe8)) {
  430. const __u8 id = data[1]&UART_IIR_ID;
  431. dev_dbg(&port->dev, "%s: iir=%02x\n", __func__, data[1]);
  432. if (id == UART_IIR_MSI) {
  433. dev_dbg(&port->dev, "%s: msr=%02x\n",
  434. __func__, data[3]);
  435. ark3116_update_msr(port, data[3]);
  436. break;
  437. } else if (id == UART_IIR_RLSI) {
  438. dev_dbg(&port->dev, "%s: lsr=%02x\n",
  439. __func__, data[2]);
  440. ark3116_update_lsr(port, data[2]);
  441. break;
  442. }
  443. }
  444. /*
  445. * Not sure what this data meant...
  446. */
  447. usb_serial_debug_data(&port->dev, __func__,
  448. urb->actual_length,
  449. urb->transfer_buffer);
  450. break;
  451. }
  452. result = usb_submit_urb(urb, GFP_ATOMIC);
  453. if (result)
  454. dev_err(&port->dev, "failed to resubmit interrupt urb: %d\n",
  455. result);
  456. }
  457. /* Data comes in via the bulk (data) URB, errors/interrupts via the int URB.
  458. * This means that we cannot be sure which data byte has an associated error
  459. * condition, so we report an error for all data in the next bulk read.
  460. *
  461. * Actually, there might even be a window between the bulk data leaving the
  462. * ark and reading/resetting the lsr in the read_bulk_callback where an
  463. * interrupt for the next data block could come in.
  464. * Without somekind of ordering on the ark, we would have to report the
  465. * error for the next block of data as well...
  466. * For now, let's pretend this can't happen.
  467. */
  468. static void ark3116_process_read_urb(struct urb *urb)
  469. {
  470. struct usb_serial_port *port = urb->context;
  471. struct ark3116_private *priv = usb_get_serial_port_data(port);
  472. unsigned char *data = urb->transfer_buffer;
  473. char tty_flag = TTY_NORMAL;
  474. unsigned long flags;
  475. __u32 lsr;
  476. /* update line status */
  477. spin_lock_irqsave(&priv->status_lock, flags);
  478. lsr = priv->lsr;
  479. priv->lsr &= ~UART_LSR_BRK_ERROR_BITS;
  480. spin_unlock_irqrestore(&priv->status_lock, flags);
  481. if (!urb->actual_length)
  482. return;
  483. if (lsr & UART_LSR_BRK_ERROR_BITS) {
  484. if (lsr & UART_LSR_BI)
  485. tty_flag = TTY_BREAK;
  486. else if (lsr & UART_LSR_PE)
  487. tty_flag = TTY_PARITY;
  488. else if (lsr & UART_LSR_FE)
  489. tty_flag = TTY_FRAME;
  490. /* overrun is special, not associated with a char */
  491. if (lsr & UART_LSR_OE)
  492. tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
  493. }
  494. tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
  495. urb->actual_length);
  496. tty_flip_buffer_push(&port->port);
  497. }
  498. static struct usb_serial_driver ark3116_device = {
  499. .driver = {
  500. .owner = THIS_MODULE,
  501. .name = "ark3116",
  502. },
  503. .id_table = id_table,
  504. .num_ports = 1,
  505. .num_bulk_in = 1,
  506. .num_bulk_out = 1,
  507. .num_interrupt_in = 1,
  508. .port_probe = ark3116_port_probe,
  509. .port_remove = ark3116_port_remove,
  510. .set_termios = ark3116_set_termios,
  511. .tiocmget = ark3116_tiocmget,
  512. .tiocmset = ark3116_tiocmset,
  513. .tiocmiwait = usb_serial_generic_tiocmiwait,
  514. .get_icount = usb_serial_generic_get_icount,
  515. .open = ark3116_open,
  516. .close = ark3116_close,
  517. .break_ctl = ark3116_break_ctl,
  518. .read_int_callback = ark3116_read_int_callback,
  519. .process_read_urb = ark3116_process_read_urb,
  520. };
  521. static struct usb_serial_driver * const serial_drivers[] = {
  522. &ark3116_device, NULL
  523. };
  524. module_usb_serial_driver(serial_drivers, id_table);
  525. MODULE_LICENSE("GPL");
  526. MODULE_AUTHOR(DRIVER_AUTHOR);
  527. MODULE_DESCRIPTION(DRIVER_DESC);
  528. /*
  529. * The following describes what I learned from studying the old
  530. * ark3116.c driver, disassembling the windows driver, and some lucky
  531. * guesses. Since I do not have any datasheet or other
  532. * documentation, inaccuracies are almost guaranteed.
  533. *
  534. * Some specs for the ARK3116 can be found here:
  535. * http://web.archive.org/web/20060318000438/
  536. * www.arkmicro.com/en/products/view.php?id=10
  537. * On that page, 2 GPIO pins are mentioned: I assume these are the
  538. * OUT1 and OUT2 pins of the UART, so I added support for those
  539. * through the MCR. Since the pins are not available on my hardware,
  540. * I could not verify this.
  541. * Also, it states there is "on-chip hardware flow control". I have
  542. * discovered how to enable that. Unfortunately, I do not know how to
  543. * enable XON/XOFF (software) flow control, which would need support
  544. * from the chip as well to work. Because of the wording on the web
  545. * page there is a real possibility the chip simply does not support
  546. * software flow control.
  547. *
  548. * I got my ark3116 as part of a mobile phone adapter cable. On the
  549. * PCB, the following numbered contacts are present:
  550. *
  551. * 1:- +5V
  552. * 2:o DTR
  553. * 3:i RX
  554. * 4:i DCD
  555. * 5:o RTS
  556. * 6:o TX
  557. * 7:i RI
  558. * 8:i DSR
  559. * 10:- 0V
  560. * 11:i CTS
  561. *
  562. * On my chip, all signals seem to be 3.3V, but 5V tolerant. But that
  563. * may be different for the one you have ;-).
  564. *
  565. * The windows driver limits the registers to 0-F, so I assume there
  566. * are actually 16 present on the device.
  567. *
  568. * On an UART interrupt, 4 bytes of data come in on the interrupt
  569. * endpoint. The bytes are 0xe8 IIR LSR MSR.
  570. *
  571. * The baudrate seems to be generated from the 12MHz crystal, using
  572. * 4-times subsampling. So quot=12e6/(4*baud). Also see description
  573. * of register E.
  574. *
  575. * Registers 0-7:
  576. * These seem to be the same as for a regular 16450. The FCR is set
  577. * to UART_FCR_DMA_SELECT (0x8), I guess to enable transfers between
  578. * the UART and the USB bridge/DMA engine.
  579. *
  580. * Register 8:
  581. * By trial and error, I found out that bit 0 enables hardware CTS,
  582. * stopping TX when CTS is +5V. Bit 1 does the same for RTS, making
  583. * RTS +5V when the 3116 cannot transfer the data to the USB bus
  584. * (verified by disabling the reading URB). Note that as far as I can
  585. * tell, the windows driver does NOT use this, so there might be some
  586. * hardware bug or something.
  587. *
  588. * According to a patch provided here
  589. * https://lore.kernel.org/lkml/[email protected]
  590. * the ARK3116 can also be used as an IrDA dongle. Since I do not have
  591. * such a thing, I could not investigate that aspect. However, I can
  592. * speculate ;-).
  593. *
  594. * - IrDA encodes data differently than RS232. Most likely, one of
  595. * the bits in registers 9..E enables the IR ENDEC (encoder/decoder).
  596. * - Depending on the IR transceiver, the input and output need to be
  597. * inverted, so there are probably bits for that as well.
  598. * - IrDA is half-duplex, so there should be a bit for selecting that.
  599. *
  600. * This still leaves at least two registers unaccounted for. Perhaps
  601. * The chip can do XON/XOFF or CRC in HW?
  602. *
  603. * Register 9:
  604. * Set to 0x00 for IrDA, when the baudrate is initialised.
  605. *
  606. * Register A:
  607. * Set to 0x01 for IrDA, at init.
  608. *
  609. * Register B:
  610. * Set to 0x01 for IrDA, 0x00 for RS232, at init.
  611. *
  612. * Register C:
  613. * Set to 00 for IrDA, at init.
  614. *
  615. * Register D:
  616. * Set to 0x41 for IrDA, at init.
  617. *
  618. * Register E:
  619. * Somekind of baudrate override. The windows driver seems to set
  620. * this to 0x00 for normal baudrates, 0x01 for 460800, 0x02 for 921600.
  621. * Since 460800 and 921600 cannot be obtained by dividing 3MHz by an integer,
  622. * it could be somekind of subdivisor thingy.
  623. * However,it does not seem to do anything: selecting 921600 (divisor 3,
  624. * reg E=2), still gets 1 MHz. I also checked if registers 9, C or F would
  625. * work, but they don't.
  626. *
  627. * Register F: unknown
  628. */