ark3116.c 25 KB

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