ark3116.c 26 KB

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