ark3116.c 20 KB

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