kl5kusb105.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * KLSI KL5KUSB105 chip RS232 converter driver
  3. *
  4. * Copyright (C) 2010 Johan Hovold <jhovold@gmail.com>
  5. * Copyright (C) 2001 Utz-Uwe Haus <haus@uuhaus.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * All information about the device was acquired using SniffUSB ans snoopUSB
  13. * on Windows98.
  14. * It was written out of frustration with the PalmConnect USB Serial adapter
  15. * sold by Palm Inc.
  16. * Neither Palm, nor their contractor (MCCI) or their supplier (KLSI) provided
  17. * information that was not already available.
  18. *
  19. * It seems that KLSI bought some silicon-design information from ScanLogic,
  20. * whose SL11R processor is at the core of the KL5KUSB chipset from KLSI.
  21. * KLSI has firmware available for their devices; it is probable that the
  22. * firmware differs from that used by KLSI in their products. If you have an
  23. * original KLSI device and can provide some information on it, I would be
  24. * most interested in adding support for it here. If you have any information
  25. * on the protocol used (or find errors in my reverse-engineered stuff), please
  26. * let me know.
  27. *
  28. * The code was only tested with a PalmConnect USB adapter; if you
  29. * are adventurous, try it with any KLSI-based device and let me know how it
  30. * breaks so that I can fix it!
  31. */
  32. /* TODO:
  33. * check modem line signals
  34. * implement handshaking or decide that we do not support it
  35. */
  36. #include <linux/kernel.h>
  37. #include <linux/errno.h>
  38. #include <linux/slab.h>
  39. #include <linux/tty.h>
  40. #include <linux/tty_driver.h>
  41. #include <linux/tty_flip.h>
  42. #include <linux/module.h>
  43. #include <linux/uaccess.h>
  44. #include <asm/unaligned.h>
  45. #include <linux/usb.h>
  46. #include <linux/usb/serial.h>
  47. #include "kl5kusb105.h"
  48. #define DRIVER_AUTHOR "Utz-Uwe Haus <haus@uuhaus.de>, Johan Hovold <jhovold@gmail.com>"
  49. #define DRIVER_DESC "KLSI KL5KUSB105 chipset USB->Serial Converter driver"
  50. /*
  51. * Function prototypes
  52. */
  53. static int klsi_105_port_probe(struct usb_serial_port *port);
  54. static int klsi_105_port_remove(struct usb_serial_port *port);
  55. static int klsi_105_open(struct tty_struct *tty, struct usb_serial_port *port);
  56. static void klsi_105_close(struct usb_serial_port *port);
  57. static void klsi_105_set_termios(struct tty_struct *tty,
  58. struct usb_serial_port *port, struct ktermios *old);
  59. static int klsi_105_tiocmget(struct tty_struct *tty);
  60. static void klsi_105_process_read_urb(struct urb *urb);
  61. static int klsi_105_prepare_write_buffer(struct usb_serial_port *port,
  62. void *dest, size_t size);
  63. /*
  64. * All of the device info needed for the KLSI converters.
  65. */
  66. static const struct usb_device_id id_table[] = {
  67. { USB_DEVICE(PALMCONNECT_VID, PALMCONNECT_PID) },
  68. { USB_DEVICE(KLSI_VID, KLSI_KL5KUSB105D_PID) },
  69. { } /* Terminating entry */
  70. };
  71. MODULE_DEVICE_TABLE(usb, id_table);
  72. static struct usb_serial_driver kl5kusb105d_device = {
  73. .driver = {
  74. .owner = THIS_MODULE,
  75. .name = "kl5kusb105d",
  76. },
  77. .description = "KL5KUSB105D / PalmConnect",
  78. .id_table = id_table,
  79. .num_ports = 1,
  80. .bulk_out_size = 64,
  81. .open = klsi_105_open,
  82. .close = klsi_105_close,
  83. .set_termios = klsi_105_set_termios,
  84. .tiocmget = klsi_105_tiocmget,
  85. .port_probe = klsi_105_port_probe,
  86. .port_remove = klsi_105_port_remove,
  87. .throttle = usb_serial_generic_throttle,
  88. .unthrottle = usb_serial_generic_unthrottle,
  89. .process_read_urb = klsi_105_process_read_urb,
  90. .prepare_write_buffer = klsi_105_prepare_write_buffer,
  91. };
  92. static struct usb_serial_driver * const serial_drivers[] = {
  93. &kl5kusb105d_device, NULL
  94. };
  95. struct klsi_105_port_settings {
  96. u8 pktlen; /* always 5, it seems */
  97. u8 baudrate;
  98. u8 databits;
  99. u8 unknown1;
  100. u8 unknown2;
  101. };
  102. struct klsi_105_private {
  103. struct klsi_105_port_settings cfg;
  104. unsigned long line_state; /* modem line settings */
  105. spinlock_t lock;
  106. };
  107. /*
  108. * Handle vendor specific USB requests
  109. */
  110. #define KLSI_TIMEOUT 5000 /* default urb timeout */
  111. static int klsi_105_chg_port_settings(struct usb_serial_port *port,
  112. struct klsi_105_port_settings *settings)
  113. {
  114. int rc;
  115. rc = usb_control_msg(port->serial->dev,
  116. usb_sndctrlpipe(port->serial->dev, 0),
  117. KL5KUSB105A_SIO_SET_DATA,
  118. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_INTERFACE,
  119. 0, /* value */
  120. 0, /* index */
  121. settings,
  122. sizeof(struct klsi_105_port_settings),
  123. KLSI_TIMEOUT);
  124. if (rc < 0)
  125. dev_err(&port->dev,
  126. "Change port settings failed (error = %d)\n", rc);
  127. dev_dbg(&port->dev,
  128. "pktlen %u, baudrate 0x%02x, databits %u, u1 %u, u2 %u\n",
  129. settings->pktlen, settings->baudrate, settings->databits,
  130. settings->unknown1, settings->unknown2);
  131. return rc;
  132. }
  133. /* translate a 16-bit status value from the device to linux's TIO bits */
  134. static unsigned long klsi_105_status2linestate(const __u16 status)
  135. {
  136. unsigned long res = 0;
  137. res = ((status & KL5KUSB105A_DSR) ? TIOCM_DSR : 0)
  138. | ((status & KL5KUSB105A_CTS) ? TIOCM_CTS : 0)
  139. ;
  140. return res;
  141. }
  142. /*
  143. * Read line control via vendor command and return result through
  144. * *line_state_p
  145. */
  146. /* It seems that the status buffer has always only 2 bytes length */
  147. #define KLSI_STATUSBUF_LEN 2
  148. static int klsi_105_get_line_state(struct usb_serial_port *port,
  149. unsigned long *line_state_p)
  150. {
  151. int rc;
  152. u8 *status_buf;
  153. __u16 status;
  154. status_buf = kmalloc(KLSI_STATUSBUF_LEN, GFP_KERNEL);
  155. if (!status_buf)
  156. return -ENOMEM;
  157. status_buf[0] = 0xff;
  158. status_buf[1] = 0xff;
  159. rc = usb_control_msg(port->serial->dev,
  160. usb_rcvctrlpipe(port->serial->dev, 0),
  161. KL5KUSB105A_SIO_POLL,
  162. USB_TYPE_VENDOR | USB_DIR_IN,
  163. 0, /* value */
  164. 0, /* index */
  165. status_buf, KLSI_STATUSBUF_LEN,
  166. 10000
  167. );
  168. if (rc != KLSI_STATUSBUF_LEN) {
  169. dev_err(&port->dev, "reading line status failed: %d\n", rc);
  170. if (rc >= 0)
  171. rc = -EIO;
  172. } else {
  173. status = get_unaligned_le16(status_buf);
  174. dev_dbg(&port->dev, "read status %02x %02x\n",
  175. status_buf[0], status_buf[1]);
  176. *line_state_p = klsi_105_status2linestate(status);
  177. }
  178. kfree(status_buf);
  179. return rc;
  180. }
  181. /*
  182. * Driver's tty interface functions
  183. */
  184. static int klsi_105_port_probe(struct usb_serial_port *port)
  185. {
  186. struct klsi_105_private *priv;
  187. priv = kmalloc(sizeof(*priv), GFP_KERNEL);
  188. if (!priv)
  189. return -ENOMEM;
  190. /* set initial values for control structures */
  191. priv->cfg.pktlen = 5;
  192. priv->cfg.baudrate = kl5kusb105a_sio_b9600;
  193. priv->cfg.databits = kl5kusb105a_dtb_8;
  194. priv->cfg.unknown1 = 0;
  195. priv->cfg.unknown2 = 1;
  196. priv->line_state = 0;
  197. spin_lock_init(&priv->lock);
  198. usb_set_serial_port_data(port, priv);
  199. return 0;
  200. }
  201. static int klsi_105_port_remove(struct usb_serial_port *port)
  202. {
  203. struct klsi_105_private *priv;
  204. priv = usb_get_serial_port_data(port);
  205. kfree(priv);
  206. return 0;
  207. }
  208. static int klsi_105_open(struct tty_struct *tty, struct usb_serial_port *port)
  209. {
  210. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  211. int retval = 0;
  212. int rc;
  213. unsigned long line_state;
  214. struct klsi_105_port_settings *cfg;
  215. unsigned long flags;
  216. /* Do a defined restart:
  217. * Set up sane default baud rate and send the 'READ_ON'
  218. * vendor command.
  219. * FIXME: set modem line control (how?)
  220. * Then read the modem line control and store values in
  221. * priv->line_state.
  222. */
  223. cfg = kmalloc(sizeof(*cfg), GFP_KERNEL);
  224. if (!cfg)
  225. return -ENOMEM;
  226. cfg->pktlen = 5;
  227. cfg->baudrate = kl5kusb105a_sio_b9600;
  228. cfg->databits = kl5kusb105a_dtb_8;
  229. cfg->unknown1 = 0;
  230. cfg->unknown2 = 1;
  231. klsi_105_chg_port_settings(port, cfg);
  232. spin_lock_irqsave(&priv->lock, flags);
  233. priv->cfg.pktlen = cfg->pktlen;
  234. priv->cfg.baudrate = cfg->baudrate;
  235. priv->cfg.databits = cfg->databits;
  236. priv->cfg.unknown1 = cfg->unknown1;
  237. priv->cfg.unknown2 = cfg->unknown2;
  238. spin_unlock_irqrestore(&priv->lock, flags);
  239. /* READ_ON and urb submission */
  240. rc = usb_serial_generic_open(tty, port);
  241. if (rc) {
  242. retval = rc;
  243. goto err_free_cfg;
  244. }
  245. rc = usb_control_msg(port->serial->dev,
  246. usb_sndctrlpipe(port->serial->dev, 0),
  247. KL5KUSB105A_SIO_CONFIGURE,
  248. USB_TYPE_VENDOR|USB_DIR_OUT|USB_RECIP_INTERFACE,
  249. KL5KUSB105A_SIO_CONFIGURE_READ_ON,
  250. 0, /* index */
  251. NULL,
  252. 0,
  253. KLSI_TIMEOUT);
  254. if (rc < 0) {
  255. dev_err(&port->dev, "Enabling read failed (error = %d)\n", rc);
  256. retval = rc;
  257. goto err_generic_close;
  258. } else
  259. dev_dbg(&port->dev, "%s - enabled reading\n", __func__);
  260. rc = klsi_105_get_line_state(port, &line_state);
  261. if (rc < 0) {
  262. retval = rc;
  263. goto err_disable_read;
  264. }
  265. spin_lock_irqsave(&priv->lock, flags);
  266. priv->line_state = line_state;
  267. spin_unlock_irqrestore(&priv->lock, flags);
  268. dev_dbg(&port->dev, "%s - read line state 0x%lx\n", __func__,
  269. line_state);
  270. return 0;
  271. err_disable_read:
  272. usb_control_msg(port->serial->dev,
  273. usb_sndctrlpipe(port->serial->dev, 0),
  274. KL5KUSB105A_SIO_CONFIGURE,
  275. USB_TYPE_VENDOR | USB_DIR_OUT,
  276. KL5KUSB105A_SIO_CONFIGURE_READ_OFF,
  277. 0, /* index */
  278. NULL, 0,
  279. KLSI_TIMEOUT);
  280. err_generic_close:
  281. usb_serial_generic_close(port);
  282. err_free_cfg:
  283. kfree(cfg);
  284. return retval;
  285. }
  286. static void klsi_105_close(struct usb_serial_port *port)
  287. {
  288. int rc;
  289. /* send READ_OFF */
  290. rc = usb_control_msg(port->serial->dev,
  291. usb_sndctrlpipe(port->serial->dev, 0),
  292. KL5KUSB105A_SIO_CONFIGURE,
  293. USB_TYPE_VENDOR | USB_DIR_OUT,
  294. KL5KUSB105A_SIO_CONFIGURE_READ_OFF,
  295. 0, /* index */
  296. NULL, 0,
  297. KLSI_TIMEOUT);
  298. if (rc < 0)
  299. dev_err(&port->dev, "failed to disable read: %d\n", rc);
  300. /* shutdown our bulk reads and writes */
  301. usb_serial_generic_close(port);
  302. }
  303. /* We need to write a complete 64-byte data block and encode the
  304. * number actually sent in the first double-byte, LSB-order. That
  305. * leaves at most 62 bytes of payload.
  306. */
  307. #define KLSI_HDR_LEN 2
  308. static int klsi_105_prepare_write_buffer(struct usb_serial_port *port,
  309. void *dest, size_t size)
  310. {
  311. unsigned char *buf = dest;
  312. int count;
  313. count = kfifo_out_locked(&port->write_fifo, buf + KLSI_HDR_LEN, size,
  314. &port->lock);
  315. put_unaligned_le16(count, buf);
  316. return count + KLSI_HDR_LEN;
  317. }
  318. /* The data received is preceded by a length double-byte in LSB-first order.
  319. */
  320. static void klsi_105_process_read_urb(struct urb *urb)
  321. {
  322. struct usb_serial_port *port = urb->context;
  323. unsigned char *data = urb->transfer_buffer;
  324. unsigned len;
  325. /* empty urbs seem to happen, we ignore them */
  326. if (!urb->actual_length)
  327. return;
  328. if (urb->actual_length <= KLSI_HDR_LEN) {
  329. dev_dbg(&port->dev, "%s - malformed packet\n", __func__);
  330. return;
  331. }
  332. len = get_unaligned_le16(data);
  333. if (len > urb->actual_length - KLSI_HDR_LEN) {
  334. dev_dbg(&port->dev, "%s - packet length mismatch\n", __func__);
  335. len = urb->actual_length - KLSI_HDR_LEN;
  336. }
  337. tty_insert_flip_string(&port->port, data + KLSI_HDR_LEN, len);
  338. tty_flip_buffer_push(&port->port);
  339. }
  340. static void klsi_105_set_termios(struct tty_struct *tty,
  341. struct usb_serial_port *port,
  342. struct ktermios *old_termios)
  343. {
  344. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  345. struct device *dev = &port->dev;
  346. unsigned int iflag = tty->termios.c_iflag;
  347. unsigned int old_iflag = old_termios->c_iflag;
  348. unsigned int cflag = tty->termios.c_cflag;
  349. unsigned int old_cflag = old_termios->c_cflag;
  350. struct klsi_105_port_settings *cfg;
  351. unsigned long flags;
  352. speed_t baud;
  353. cfg = kmalloc(sizeof(*cfg), GFP_KERNEL);
  354. if (!cfg)
  355. return;
  356. /* lock while we are modifying the settings */
  357. spin_lock_irqsave(&priv->lock, flags);
  358. /*
  359. * Update baud rate
  360. */
  361. baud = tty_get_baud_rate(tty);
  362. switch (baud) {
  363. case 0: /* handled below */
  364. break;
  365. case 1200:
  366. priv->cfg.baudrate = kl5kusb105a_sio_b1200;
  367. break;
  368. case 2400:
  369. priv->cfg.baudrate = kl5kusb105a_sio_b2400;
  370. break;
  371. case 4800:
  372. priv->cfg.baudrate = kl5kusb105a_sio_b4800;
  373. break;
  374. case 9600:
  375. priv->cfg.baudrate = kl5kusb105a_sio_b9600;
  376. break;
  377. case 19200:
  378. priv->cfg.baudrate = kl5kusb105a_sio_b19200;
  379. break;
  380. case 38400:
  381. priv->cfg.baudrate = kl5kusb105a_sio_b38400;
  382. break;
  383. case 57600:
  384. priv->cfg.baudrate = kl5kusb105a_sio_b57600;
  385. break;
  386. case 115200:
  387. priv->cfg.baudrate = kl5kusb105a_sio_b115200;
  388. break;
  389. default:
  390. dev_dbg(dev, "unsupported baudrate, using 9600\n");
  391. priv->cfg.baudrate = kl5kusb105a_sio_b9600;
  392. baud = 9600;
  393. break;
  394. }
  395. /*
  396. * FIXME: implement B0 handling
  397. *
  398. * Maybe this should be simulated by sending read disable and read
  399. * enable messages?
  400. */
  401. tty_encode_baud_rate(tty, baud, baud);
  402. if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
  403. /* set the number of data bits */
  404. switch (cflag & CSIZE) {
  405. case CS5:
  406. dev_dbg(dev, "%s - 5 bits/byte not supported\n", __func__);
  407. spin_unlock_irqrestore(&priv->lock, flags);
  408. goto err;
  409. case CS6:
  410. dev_dbg(dev, "%s - 6 bits/byte not supported\n", __func__);
  411. spin_unlock_irqrestore(&priv->lock, flags);
  412. goto err;
  413. case CS7:
  414. priv->cfg.databits = kl5kusb105a_dtb_7;
  415. break;
  416. case CS8:
  417. priv->cfg.databits = kl5kusb105a_dtb_8;
  418. break;
  419. default:
  420. dev_err(dev, "CSIZE was not CS5-CS8, using default of 8\n");
  421. priv->cfg.databits = kl5kusb105a_dtb_8;
  422. break;
  423. }
  424. }
  425. /*
  426. * Update line control register (LCR)
  427. */
  428. if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD))
  429. || (cflag & CSTOPB) != (old_cflag & CSTOPB)) {
  430. /* Not currently supported */
  431. tty->termios.c_cflag &= ~(PARENB|PARODD|CSTOPB);
  432. }
  433. /*
  434. * Set flow control: well, I do not really now how to handle DTR/RTS.
  435. * Just do what we have seen with SniffUSB on Win98.
  436. */
  437. if ((iflag & IXOFF) != (old_iflag & IXOFF)
  438. || (iflag & IXON) != (old_iflag & IXON)
  439. || (cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
  440. /* Not currently supported */
  441. tty->termios.c_cflag &= ~CRTSCTS;
  442. }
  443. memcpy(cfg, &priv->cfg, sizeof(*cfg));
  444. spin_unlock_irqrestore(&priv->lock, flags);
  445. /* now commit changes to device */
  446. klsi_105_chg_port_settings(port, cfg);
  447. err:
  448. kfree(cfg);
  449. }
  450. static int klsi_105_tiocmget(struct tty_struct *tty)
  451. {
  452. struct usb_serial_port *port = tty->driver_data;
  453. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  454. unsigned long flags;
  455. int rc;
  456. unsigned long line_state;
  457. rc = klsi_105_get_line_state(port, &line_state);
  458. if (rc < 0) {
  459. dev_err(&port->dev,
  460. "Reading line control failed (error = %d)\n", rc);
  461. /* better return value? EAGAIN? */
  462. return rc;
  463. }
  464. spin_lock_irqsave(&priv->lock, flags);
  465. priv->line_state = line_state;
  466. spin_unlock_irqrestore(&priv->lock, flags);
  467. dev_dbg(&port->dev, "%s - read line state 0x%lx\n", __func__, line_state);
  468. return (int)line_state;
  469. }
  470. module_usb_serial_driver(serial_drivers, id_table);
  471. MODULE_AUTHOR(DRIVER_AUTHOR);
  472. MODULE_DESCRIPTION(DRIVER_DESC);
  473. MODULE_LICENSE("GPL");