ark3116.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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 usb_serial_port *port,
  333. struct serial_struct __user *retinfo)
  334. {
  335. struct serial_struct tmp;
  336. memset(&tmp, 0, sizeof(tmp));
  337. tmp.type = PORT_16654;
  338. tmp.line = port->minor;
  339. tmp.port = port->port_number;
  340. tmp.baud_base = 460800;
  341. if (copy_to_user(retinfo, &tmp, sizeof(tmp)))
  342. return -EFAULT;
  343. return 0;
  344. }
  345. static int ark3116_ioctl(struct tty_struct *tty,
  346. unsigned int cmd, unsigned long arg)
  347. {
  348. struct usb_serial_port *port = tty->driver_data;
  349. void __user *user_arg = (void __user *)arg;
  350. switch (cmd) {
  351. case TIOCGSERIAL:
  352. return ark3116_get_serial_info(port, user_arg);
  353. default:
  354. break;
  355. }
  356. return -ENOIOCTLCMD;
  357. }
  358. static int ark3116_tiocmget(struct tty_struct *tty)
  359. {
  360. struct usb_serial_port *port = tty->driver_data;
  361. struct ark3116_private *priv = usb_get_serial_port_data(port);
  362. __u32 status;
  363. __u32 ctrl;
  364. unsigned long flags;
  365. mutex_lock(&priv->hw_lock);
  366. ctrl = priv->mcr;
  367. mutex_unlock(&priv->hw_lock);
  368. spin_lock_irqsave(&priv->status_lock, flags);
  369. status = priv->msr;
  370. spin_unlock_irqrestore(&priv->status_lock, flags);
  371. return (status & UART_MSR_DSR ? TIOCM_DSR : 0) |
  372. (status & UART_MSR_CTS ? TIOCM_CTS : 0) |
  373. (status & UART_MSR_RI ? TIOCM_RI : 0) |
  374. (status & UART_MSR_DCD ? TIOCM_CD : 0) |
  375. (ctrl & UART_MCR_DTR ? TIOCM_DTR : 0) |
  376. (ctrl & UART_MCR_RTS ? TIOCM_RTS : 0) |
  377. (ctrl & UART_MCR_OUT1 ? TIOCM_OUT1 : 0) |
  378. (ctrl & UART_MCR_OUT2 ? TIOCM_OUT2 : 0);
  379. }
  380. static int ark3116_tiocmset(struct tty_struct *tty,
  381. unsigned set, unsigned clr)
  382. {
  383. struct usb_serial_port *port = tty->driver_data;
  384. struct ark3116_private *priv = usb_get_serial_port_data(port);
  385. /* we need to take the mutex here, to make sure that the value
  386. * in priv->mcr is actually the one that is in the hardware
  387. */
  388. mutex_lock(&priv->hw_lock);
  389. if (set & TIOCM_RTS)
  390. priv->mcr |= UART_MCR_RTS;
  391. if (set & TIOCM_DTR)
  392. priv->mcr |= UART_MCR_DTR;
  393. if (set & TIOCM_OUT1)
  394. priv->mcr |= UART_MCR_OUT1;
  395. if (set & TIOCM_OUT2)
  396. priv->mcr |= UART_MCR_OUT2;
  397. if (clr & TIOCM_RTS)
  398. priv->mcr &= ~UART_MCR_RTS;
  399. if (clr & TIOCM_DTR)
  400. priv->mcr &= ~UART_MCR_DTR;
  401. if (clr & TIOCM_OUT1)
  402. priv->mcr &= ~UART_MCR_OUT1;
  403. if (clr & TIOCM_OUT2)
  404. priv->mcr &= ~UART_MCR_OUT2;
  405. ark3116_write_reg(port->serial, UART_MCR, priv->mcr);
  406. mutex_unlock(&priv->hw_lock);
  407. return 0;
  408. }
  409. static void ark3116_break_ctl(struct tty_struct *tty, int break_state)
  410. {
  411. struct usb_serial_port *port = tty->driver_data;
  412. struct ark3116_private *priv = usb_get_serial_port_data(port);
  413. /* LCR is also used for other things: protect access */
  414. mutex_lock(&priv->hw_lock);
  415. if (break_state)
  416. priv->lcr |= UART_LCR_SBC;
  417. else
  418. priv->lcr &= ~UART_LCR_SBC;
  419. ark3116_write_reg(port->serial, UART_LCR, priv->lcr);
  420. mutex_unlock(&priv->hw_lock);
  421. }
  422. static void ark3116_update_msr(struct usb_serial_port *port, __u8 msr)
  423. {
  424. struct ark3116_private *priv = usb_get_serial_port_data(port);
  425. unsigned long flags;
  426. spin_lock_irqsave(&priv->status_lock, flags);
  427. priv->msr = msr;
  428. spin_unlock_irqrestore(&priv->status_lock, flags);
  429. if (msr & UART_MSR_ANY_DELTA) {
  430. /* update input line counters */
  431. if (msr & UART_MSR_DCTS)
  432. port->icount.cts++;
  433. if (msr & UART_MSR_DDSR)
  434. port->icount.dsr++;
  435. if (msr & UART_MSR_DDCD)
  436. port->icount.dcd++;
  437. if (msr & UART_MSR_TERI)
  438. port->icount.rng++;
  439. wake_up_interruptible(&port->port.delta_msr_wait);
  440. }
  441. }
  442. static void ark3116_update_lsr(struct usb_serial_port *port, __u8 lsr)
  443. {
  444. struct ark3116_private *priv = usb_get_serial_port_data(port);
  445. unsigned long flags;
  446. spin_lock_irqsave(&priv->status_lock, flags);
  447. /* combine bits */
  448. priv->lsr |= lsr;
  449. spin_unlock_irqrestore(&priv->status_lock, flags);
  450. if (lsr&UART_LSR_BRK_ERROR_BITS) {
  451. if (lsr & UART_LSR_BI)
  452. port->icount.brk++;
  453. if (lsr & UART_LSR_FE)
  454. port->icount.frame++;
  455. if (lsr & UART_LSR_PE)
  456. port->icount.parity++;
  457. if (lsr & UART_LSR_OE)
  458. port->icount.overrun++;
  459. }
  460. }
  461. static void ark3116_read_int_callback(struct urb *urb)
  462. {
  463. struct usb_serial_port *port = urb->context;
  464. int status = urb->status;
  465. const __u8 *data = urb->transfer_buffer;
  466. int result;
  467. switch (status) {
  468. case -ECONNRESET:
  469. case -ENOENT:
  470. case -ESHUTDOWN:
  471. /* this urb is terminated, clean up */
  472. dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
  473. __func__, status);
  474. return;
  475. default:
  476. dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
  477. __func__, status);
  478. break;
  479. case 0: /* success */
  480. /* discovered this by trail and error... */
  481. if ((urb->actual_length == 4) && (data[0] == 0xe8)) {
  482. const __u8 id = data[1]&UART_IIR_ID;
  483. dev_dbg(&port->dev, "%s: iir=%02x\n", __func__, data[1]);
  484. if (id == UART_IIR_MSI) {
  485. dev_dbg(&port->dev, "%s: msr=%02x\n",
  486. __func__, data[3]);
  487. ark3116_update_msr(port, data[3]);
  488. break;
  489. } else if (id == UART_IIR_RLSI) {
  490. dev_dbg(&port->dev, "%s: lsr=%02x\n",
  491. __func__, data[2]);
  492. ark3116_update_lsr(port, data[2]);
  493. break;
  494. }
  495. }
  496. /*
  497. * Not sure what this data meant...
  498. */
  499. usb_serial_debug_data(&port->dev, __func__,
  500. urb->actual_length,
  501. urb->transfer_buffer);
  502. break;
  503. }
  504. result = usb_submit_urb(urb, GFP_ATOMIC);
  505. if (result)
  506. dev_err(&port->dev, "failed to resubmit interrupt urb: %d\n",
  507. result);
  508. }
  509. /* Data comes in via the bulk (data) URB, errors/interrupts via the int URB.
  510. * This means that we cannot be sure which data byte has an associated error
  511. * condition, so we report an error for all data in the next bulk read.
  512. *
  513. * Actually, there might even be a window between the bulk data leaving the
  514. * ark and reading/resetting the lsr in the read_bulk_callback where an
  515. * interrupt for the next data block could come in.
  516. * Without somekind of ordering on the ark, we would have to report the
  517. * error for the next block of data as well...
  518. * For now, let's pretend this can't happen.
  519. */
  520. static void ark3116_process_read_urb(struct urb *urb)
  521. {
  522. struct usb_serial_port *port = urb->context;
  523. struct ark3116_private *priv = usb_get_serial_port_data(port);
  524. unsigned char *data = urb->transfer_buffer;
  525. char tty_flag = TTY_NORMAL;
  526. unsigned long flags;
  527. __u32 lsr;
  528. /* update line status */
  529. spin_lock_irqsave(&priv->status_lock, flags);
  530. lsr = priv->lsr;
  531. priv->lsr &= ~UART_LSR_BRK_ERROR_BITS;
  532. spin_unlock_irqrestore(&priv->status_lock, flags);
  533. if (!urb->actual_length)
  534. return;
  535. if (lsr & UART_LSR_BRK_ERROR_BITS) {
  536. if (lsr & UART_LSR_BI)
  537. tty_flag = TTY_BREAK;
  538. else if (lsr & UART_LSR_PE)
  539. tty_flag = TTY_PARITY;
  540. else if (lsr & UART_LSR_FE)
  541. tty_flag = TTY_FRAME;
  542. /* overrun is special, not associated with a char */
  543. if (lsr & UART_LSR_OE)
  544. tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
  545. }
  546. tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
  547. urb->actual_length);
  548. tty_flip_buffer_push(&port->port);
  549. }
  550. static struct usb_serial_driver ark3116_device = {
  551. .driver = {
  552. .owner = THIS_MODULE,
  553. .name = "ark3116",
  554. },
  555. .id_table = id_table,
  556. .num_ports = 1,
  557. .num_bulk_in = 1,
  558. .num_bulk_out = 1,
  559. .num_interrupt_in = 1,
  560. .port_probe = ark3116_port_probe,
  561. .port_remove = ark3116_port_remove,
  562. .set_termios = ark3116_set_termios,
  563. .init_termios = ark3116_init_termios,
  564. .ioctl = ark3116_ioctl,
  565. .tiocmget = ark3116_tiocmget,
  566. .tiocmset = ark3116_tiocmset,
  567. .tiocmiwait = usb_serial_generic_tiocmiwait,
  568. .get_icount = usb_serial_generic_get_icount,
  569. .open = ark3116_open,
  570. .close = ark3116_close,
  571. .break_ctl = ark3116_break_ctl,
  572. .read_int_callback = ark3116_read_int_callback,
  573. .process_read_urb = ark3116_process_read_urb,
  574. };
  575. static struct usb_serial_driver * const serial_drivers[] = {
  576. &ark3116_device, NULL
  577. };
  578. module_usb_serial_driver(serial_drivers, id_table);
  579. MODULE_LICENSE("GPL");
  580. MODULE_AUTHOR(DRIVER_AUTHOR);
  581. MODULE_DESCRIPTION(DRIVER_DESC);
  582. /*
  583. * The following describes what I learned from studying the old
  584. * ark3116.c driver, disassembling the windows driver, and some lucky
  585. * guesses. Since I do not have any datasheet or other
  586. * documentation, inaccuracies are almost guaranteed.
  587. *
  588. * Some specs for the ARK3116 can be found here:
  589. * http://web.archive.org/web/20060318000438/
  590. * www.arkmicro.com/en/products/view.php?id=10
  591. * On that page, 2 GPIO pins are mentioned: I assume these are the
  592. * OUT1 and OUT2 pins of the UART, so I added support for those
  593. * through the MCR. Since the pins are not available on my hardware,
  594. * I could not verify this.
  595. * Also, it states there is "on-chip hardware flow control". I have
  596. * discovered how to enable that. Unfortunately, I do not know how to
  597. * enable XON/XOFF (software) flow control, which would need support
  598. * from the chip as well to work. Because of the wording on the web
  599. * page there is a real possibility the chip simply does not support
  600. * software flow control.
  601. *
  602. * I got my ark3116 as part of a mobile phone adapter cable. On the
  603. * PCB, the following numbered contacts are present:
  604. *
  605. * 1:- +5V
  606. * 2:o DTR
  607. * 3:i RX
  608. * 4:i DCD
  609. * 5:o RTS
  610. * 6:o TX
  611. * 7:i RI
  612. * 8:i DSR
  613. * 10:- 0V
  614. * 11:i CTS
  615. *
  616. * On my chip, all signals seem to be 3.3V, but 5V tolerant. But that
  617. * may be different for the one you have ;-).
  618. *
  619. * The windows driver limits the registers to 0-F, so I assume there
  620. * are actually 16 present on the device.
  621. *
  622. * On an UART interrupt, 4 bytes of data come in on the interrupt
  623. * endpoint. The bytes are 0xe8 IIR LSR MSR.
  624. *
  625. * The baudrate seems to be generated from the 12MHz crystal, using
  626. * 4-times subsampling. So quot=12e6/(4*baud). Also see description
  627. * of register E.
  628. *
  629. * Registers 0-7:
  630. * These seem to be the same as for a regular 16450. The FCR is set
  631. * to UART_FCR_DMA_SELECT (0x8), I guess to enable transfers between
  632. * the UART and the USB bridge/DMA engine.
  633. *
  634. * Register 8:
  635. * By trial and error, I found out that bit 0 enables hardware CTS,
  636. * stopping TX when CTS is +5V. Bit 1 does the same for RTS, making
  637. * RTS +5V when the 3116 cannot transfer the data to the USB bus
  638. * (verified by disabling the reading URB). Note that as far as I can
  639. * tell, the windows driver does NOT use this, so there might be some
  640. * hardware bug or something.
  641. *
  642. * According to a patch provided here
  643. * (http://lkml.org/lkml/2009/7/26/56), the ARK3116 can also be used
  644. * as an IrDA dongle. Since I do not have such a thing, I could not
  645. * investigate that aspect. However, I can speculate ;-).
  646. *
  647. * - IrDA encodes data differently than RS232. Most likely, one of
  648. * the bits in registers 9..E enables the IR ENDEC (encoder/decoder).
  649. * - Depending on the IR transceiver, the input and output need to be
  650. * inverted, so there are probably bits for that as well.
  651. * - IrDA is half-duplex, so there should be a bit for selecting that.
  652. *
  653. * This still leaves at least two registers unaccounted for. Perhaps
  654. * The chip can do XON/XOFF or CRC in HW?
  655. *
  656. * Register 9:
  657. * Set to 0x00 for IrDA, when the baudrate is initialised.
  658. *
  659. * Register A:
  660. * Set to 0x01 for IrDA, at init.
  661. *
  662. * Register B:
  663. * Set to 0x01 for IrDA, 0x00 for RS232, at init.
  664. *
  665. * Register C:
  666. * Set to 00 for IrDA, at init.
  667. *
  668. * Register D:
  669. * Set to 0x41 for IrDA, at init.
  670. *
  671. * Register E:
  672. * Somekind of baudrate override. The windows driver seems to set
  673. * this to 0x00 for normal baudrates, 0x01 for 460800, 0x02 for 921600.
  674. * Since 460800 and 921600 cannot be obtained by dividing 3MHz by an integer,
  675. * it could be somekind of subdivisor thingy.
  676. * However,it does not seem to do anything: selecting 921600 (divisor 3,
  677. * reg E=2), still gets 1 MHz. I also checked if registers 9, C or F would
  678. * work, but they don't.
  679. *
  680. * Register F: unknown
  681. */