ark3116.c 21 KB

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