ark3116.c 21 KB

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