ark3116.c 23 KB

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