ark3116.c 22 KB

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