ark3116.c 23 KB

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