ark3116.c 21 KB

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