kl5kusb105.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. /*.break_ctl = klsi_105_break_ctl,*/
  85. .tiocmget = klsi_105_tiocmget,
  86. .port_probe = klsi_105_port_probe,
  87. .port_remove = klsi_105_port_remove,
  88. .throttle = usb_serial_generic_throttle,
  89. .unthrottle = usb_serial_generic_unthrottle,
  90. .process_read_urb = klsi_105_process_read_urb,
  91. .prepare_write_buffer = klsi_105_prepare_write_buffer,
  92. };
  93. static struct usb_serial_driver * const serial_drivers[] = {
  94. &kl5kusb105d_device, NULL
  95. };
  96. struct klsi_105_port_settings {
  97. __u8 pktlen; /* always 5, it seems */
  98. __u8 baudrate;
  99. __u8 databits;
  100. __u8 unknown1;
  101. __u8 unknown2;
  102. } __attribute__ ((packed));
  103. struct klsi_105_private {
  104. struct klsi_105_port_settings cfg;
  105. struct ktermios termios;
  106. unsigned long line_state; /* modem line settings */
  107. spinlock_t lock;
  108. };
  109. /*
  110. * Handle vendor specific USB requests
  111. */
  112. #define KLSI_TIMEOUT 5000 /* default urb timeout */
  113. static int klsi_105_chg_port_settings(struct usb_serial_port *port,
  114. struct klsi_105_port_settings *settings)
  115. {
  116. int rc;
  117. rc = usb_control_msg(port->serial->dev,
  118. usb_sndctrlpipe(port->serial->dev, 0),
  119. KL5KUSB105A_SIO_SET_DATA,
  120. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_INTERFACE,
  121. 0, /* value */
  122. 0, /* index */
  123. settings,
  124. sizeof(struct klsi_105_port_settings),
  125. KLSI_TIMEOUT);
  126. if (rc < 0)
  127. dev_err(&port->dev,
  128. "Change port settings failed (error = %d)\n", rc);
  129. dev_info(&port->serial->dev->dev,
  130. "%d byte block, baudrate %x, databits %d, u1 %d, u2 %d\n",
  131. settings->pktlen, settings->baudrate, settings->databits,
  132. settings->unknown1, settings->unknown2);
  133. return rc;
  134. }
  135. /* translate a 16-bit status value from the device to linux's TIO bits */
  136. static unsigned long klsi_105_status2linestate(const __u16 status)
  137. {
  138. unsigned long res = 0;
  139. res = ((status & KL5KUSB105A_DSR) ? TIOCM_DSR : 0)
  140. | ((status & KL5KUSB105A_CTS) ? TIOCM_CTS : 0)
  141. ;
  142. return res;
  143. }
  144. /*
  145. * Read line control via vendor command and return result through
  146. * *line_state_p
  147. */
  148. /* It seems that the status buffer has always only 2 bytes length */
  149. #define KLSI_STATUSBUF_LEN 2
  150. static int klsi_105_get_line_state(struct usb_serial_port *port,
  151. unsigned long *line_state_p)
  152. {
  153. int rc;
  154. u8 *status_buf;
  155. __u16 status;
  156. dev_info(&port->serial->dev->dev, "sending SIO Poll request\n");
  157. status_buf = kmalloc(KLSI_STATUSBUF_LEN, GFP_KERNEL);
  158. if (!status_buf)
  159. return -ENOMEM;
  160. status_buf[0] = 0xff;
  161. status_buf[1] = 0xff;
  162. rc = usb_control_msg(port->serial->dev,
  163. usb_rcvctrlpipe(port->serial->dev, 0),
  164. KL5KUSB105A_SIO_POLL,
  165. USB_TYPE_VENDOR | USB_DIR_IN,
  166. 0, /* value */
  167. 0, /* index */
  168. status_buf, KLSI_STATUSBUF_LEN,
  169. 10000
  170. );
  171. if (rc < 0)
  172. dev_err(&port->dev, "Reading line status failed (error = %d)\n",
  173. rc);
  174. else {
  175. status = get_unaligned_le16(status_buf);
  176. dev_info(&port->serial->dev->dev, "read status %x %x\n",
  177. status_buf[0], status_buf[1]);
  178. *line_state_p = klsi_105_status2linestate(status);
  179. }
  180. kfree(status_buf);
  181. return rc;
  182. }
  183. /*
  184. * Driver's tty interface functions
  185. */
  186. static int klsi_105_port_probe(struct usb_serial_port *port)
  187. {
  188. struct klsi_105_private *priv;
  189. priv = kmalloc(sizeof(*priv), GFP_KERNEL);
  190. if (!priv)
  191. return -ENOMEM;
  192. /* set initial values for control structures */
  193. priv->cfg.pktlen = 5;
  194. priv->cfg.baudrate = kl5kusb105a_sio_b9600;
  195. priv->cfg.databits = kl5kusb105a_dtb_8;
  196. priv->cfg.unknown1 = 0;
  197. priv->cfg.unknown2 = 1;
  198. priv->line_state = 0;
  199. spin_lock_init(&priv->lock);
  200. /* priv->termios is left uninitialized until port opening */
  201. usb_set_serial_port_data(port, priv);
  202. return 0;
  203. }
  204. static int klsi_105_port_remove(struct usb_serial_port *port)
  205. {
  206. struct klsi_105_private *priv;
  207. priv = usb_get_serial_port_data(port);
  208. kfree(priv);
  209. return 0;
  210. }
  211. static int klsi_105_open(struct tty_struct *tty, struct usb_serial_port *port)
  212. {
  213. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  214. int retval = 0;
  215. int rc;
  216. int i;
  217. unsigned long line_state;
  218. struct klsi_105_port_settings *cfg;
  219. unsigned long flags;
  220. /* Do a defined restart:
  221. * Set up sane default baud rate and send the 'READ_ON'
  222. * vendor command.
  223. * FIXME: set modem line control (how?)
  224. * Then read the modem line control and store values in
  225. * priv->line_state.
  226. */
  227. cfg = kmalloc(sizeof(*cfg), GFP_KERNEL);
  228. if (!cfg)
  229. return -ENOMEM;
  230. cfg->pktlen = 5;
  231. cfg->baudrate = kl5kusb105a_sio_b9600;
  232. cfg->databits = kl5kusb105a_dtb_8;
  233. cfg->unknown1 = 0;
  234. cfg->unknown2 = 1;
  235. klsi_105_chg_port_settings(port, cfg);
  236. /* set up termios structure */
  237. spin_lock_irqsave(&priv->lock, flags);
  238. priv->termios.c_iflag = tty->termios.c_iflag;
  239. priv->termios.c_oflag = tty->termios.c_oflag;
  240. priv->termios.c_cflag = tty->termios.c_cflag;
  241. priv->termios.c_lflag = tty->termios.c_lflag;
  242. for (i = 0; i < NCCS; i++)
  243. priv->termios.c_cc[i] = tty->termios.c_cc[i];
  244. priv->cfg.pktlen = cfg->pktlen;
  245. priv->cfg.baudrate = cfg->baudrate;
  246. priv->cfg.databits = cfg->databits;
  247. priv->cfg.unknown1 = cfg->unknown1;
  248. priv->cfg.unknown2 = cfg->unknown2;
  249. spin_unlock_irqrestore(&priv->lock, flags);
  250. /* READ_ON and urb submission */
  251. rc = usb_serial_generic_open(tty, port);
  252. if (rc) {
  253. retval = rc;
  254. goto exit;
  255. }
  256. rc = usb_control_msg(port->serial->dev,
  257. usb_sndctrlpipe(port->serial->dev, 0),
  258. KL5KUSB105A_SIO_CONFIGURE,
  259. USB_TYPE_VENDOR|USB_DIR_OUT|USB_RECIP_INTERFACE,
  260. KL5KUSB105A_SIO_CONFIGURE_READ_ON,
  261. 0, /* index */
  262. NULL,
  263. 0,
  264. KLSI_TIMEOUT);
  265. if (rc < 0) {
  266. dev_err(&port->dev, "Enabling read failed (error = %d)\n", rc);
  267. retval = rc;
  268. } else
  269. dev_dbg(&port->dev, "%s - enabled reading\n", __func__);
  270. rc = klsi_105_get_line_state(port, &line_state);
  271. if (rc >= 0) {
  272. spin_lock_irqsave(&priv->lock, flags);
  273. priv->line_state = line_state;
  274. spin_unlock_irqrestore(&priv->lock, flags);
  275. dev_dbg(&port->dev, "%s - read line state 0x%lx\n", __func__, line_state);
  276. retval = 0;
  277. } else
  278. retval = rc;
  279. exit:
  280. kfree(cfg);
  281. return retval;
  282. }
  283. static void klsi_105_close(struct usb_serial_port *port)
  284. {
  285. int rc;
  286. /* send READ_OFF */
  287. rc = usb_control_msg(port->serial->dev,
  288. usb_sndctrlpipe(port->serial->dev, 0),
  289. KL5KUSB105A_SIO_CONFIGURE,
  290. USB_TYPE_VENDOR | USB_DIR_OUT,
  291. KL5KUSB105A_SIO_CONFIGURE_READ_OFF,
  292. 0, /* index */
  293. NULL, 0,
  294. KLSI_TIMEOUT);
  295. if (rc < 0)
  296. dev_err(&port->dev, "failed to disable read: %d\n", rc);
  297. /* shutdown our bulk reads and writes */
  298. usb_serial_generic_close(port);
  299. }
  300. /* We need to write a complete 64-byte data block and encode the
  301. * number actually sent in the first double-byte, LSB-order. That
  302. * leaves at most 62 bytes of payload.
  303. */
  304. #define KLSI_HDR_LEN 2
  305. static int klsi_105_prepare_write_buffer(struct usb_serial_port *port,
  306. void *dest, size_t size)
  307. {
  308. unsigned char *buf = dest;
  309. int count;
  310. count = kfifo_out_locked(&port->write_fifo, buf + KLSI_HDR_LEN, size,
  311. &port->lock);
  312. put_unaligned_le16(count, buf);
  313. return count + KLSI_HDR_LEN;
  314. }
  315. /* The data received is preceded by a length double-byte in LSB-first order.
  316. */
  317. static void klsi_105_process_read_urb(struct urb *urb)
  318. {
  319. struct usb_serial_port *port = urb->context;
  320. unsigned char *data = urb->transfer_buffer;
  321. unsigned len;
  322. /* empty urbs seem to happen, we ignore them */
  323. if (!urb->actual_length)
  324. return;
  325. if (urb->actual_length <= KLSI_HDR_LEN) {
  326. dev_dbg(&port->dev, "%s - malformed packet\n", __func__);
  327. return;
  328. }
  329. len = get_unaligned_le16(data);
  330. if (len > urb->actual_length - KLSI_HDR_LEN) {
  331. dev_dbg(&port->dev, "%s - packet length mismatch\n", __func__);
  332. len = urb->actual_length - KLSI_HDR_LEN;
  333. }
  334. tty_insert_flip_string(&port->port, data + KLSI_HDR_LEN, len);
  335. tty_flip_buffer_push(&port->port);
  336. }
  337. static void klsi_105_set_termios(struct tty_struct *tty,
  338. struct usb_serial_port *port,
  339. struct ktermios *old_termios)
  340. {
  341. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  342. struct device *dev = &port->dev;
  343. unsigned int iflag = tty->termios.c_iflag;
  344. unsigned int old_iflag = old_termios->c_iflag;
  345. unsigned int cflag = tty->termios.c_cflag;
  346. unsigned int old_cflag = old_termios->c_cflag;
  347. struct klsi_105_port_settings *cfg;
  348. unsigned long flags;
  349. speed_t baud;
  350. cfg = kmalloc(sizeof(*cfg), GFP_KERNEL);
  351. if (!cfg)
  352. return;
  353. /* lock while we are modifying the settings */
  354. spin_lock_irqsave(&priv->lock, flags);
  355. /*
  356. * Update baud rate
  357. */
  358. baud = tty_get_baud_rate(tty);
  359. if ((cflag & CBAUD) != (old_cflag & CBAUD)) {
  360. /* reassert DTR and (maybe) RTS on transition from B0 */
  361. if ((old_cflag & CBAUD) == B0) {
  362. dev_dbg(dev, "%s: baud was B0\n", __func__);
  363. #if 0
  364. priv->control_state |= TIOCM_DTR;
  365. /* don't set RTS if using hardware flow control */
  366. if (!(old_cflag & CRTSCTS))
  367. priv->control_state |= TIOCM_RTS;
  368. mct_u232_set_modem_ctrl(serial, priv->control_state);
  369. #endif
  370. }
  371. }
  372. switch (baud) {
  373. case 0: /* handled below */
  374. break;
  375. case 1200:
  376. priv->cfg.baudrate = kl5kusb105a_sio_b1200;
  377. break;
  378. case 2400:
  379. priv->cfg.baudrate = kl5kusb105a_sio_b2400;
  380. break;
  381. case 4800:
  382. priv->cfg.baudrate = kl5kusb105a_sio_b4800;
  383. break;
  384. case 9600:
  385. priv->cfg.baudrate = kl5kusb105a_sio_b9600;
  386. break;
  387. case 19200:
  388. priv->cfg.baudrate = kl5kusb105a_sio_b19200;
  389. break;
  390. case 38400:
  391. priv->cfg.baudrate = kl5kusb105a_sio_b38400;
  392. break;
  393. case 57600:
  394. priv->cfg.baudrate = kl5kusb105a_sio_b57600;
  395. break;
  396. case 115200:
  397. priv->cfg.baudrate = kl5kusb105a_sio_b115200;
  398. break;
  399. default:
  400. dev_dbg(dev, "unsupported baudrate, using 9600\n");
  401. priv->cfg.baudrate = kl5kusb105a_sio_b9600;
  402. baud = 9600;
  403. break;
  404. }
  405. if ((cflag & CBAUD) == B0) {
  406. dev_dbg(dev, "%s: baud is B0\n", __func__);
  407. /* Drop RTS and DTR */
  408. /* maybe this should be simulated by sending read
  409. * disable and read enable messages?
  410. */
  411. ;
  412. #if 0
  413. priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
  414. mct_u232_set_modem_ctrl(serial, priv->control_state);
  415. #endif
  416. }
  417. tty_encode_baud_rate(tty, baud, baud);
  418. if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
  419. /* set the number of data bits */
  420. switch (cflag & CSIZE) {
  421. case CS5:
  422. dev_dbg(dev, "%s - 5 bits/byte not supported\n", __func__);
  423. spin_unlock_irqrestore(&priv->lock, flags);
  424. goto err;
  425. case CS6:
  426. dev_dbg(dev, "%s - 6 bits/byte not supported\n", __func__);
  427. spin_unlock_irqrestore(&priv->lock, flags);
  428. goto err;
  429. case CS7:
  430. priv->cfg.databits = kl5kusb105a_dtb_7;
  431. break;
  432. case CS8:
  433. priv->cfg.databits = kl5kusb105a_dtb_8;
  434. break;
  435. default:
  436. dev_err(dev, "CSIZE was not CS5-CS8, using default of 8\n");
  437. priv->cfg.databits = kl5kusb105a_dtb_8;
  438. break;
  439. }
  440. }
  441. /*
  442. * Update line control register (LCR)
  443. */
  444. if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD))
  445. || (cflag & CSTOPB) != (old_cflag & CSTOPB)) {
  446. /* Not currently supported */
  447. tty->termios.c_cflag &= ~(PARENB|PARODD|CSTOPB);
  448. #if 0
  449. priv->last_lcr = 0;
  450. /* set the parity */
  451. if (cflag & PARENB)
  452. priv->last_lcr |= (cflag & PARODD) ?
  453. MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN;
  454. else
  455. priv->last_lcr |= MCT_U232_PARITY_NONE;
  456. /* set the number of stop bits */
  457. priv->last_lcr |= (cflag & CSTOPB) ?
  458. MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;
  459. mct_u232_set_line_ctrl(serial, priv->last_lcr);
  460. #endif
  461. ;
  462. }
  463. /*
  464. * Set flow control: well, I do not really now how to handle DTR/RTS.
  465. * Just do what we have seen with SniffUSB on Win98.
  466. */
  467. if ((iflag & IXOFF) != (old_iflag & IXOFF)
  468. || (iflag & IXON) != (old_iflag & IXON)
  469. || (cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
  470. /* Not currently supported */
  471. tty->termios.c_cflag &= ~CRTSCTS;
  472. /* Drop DTR/RTS if no flow control otherwise assert */
  473. #if 0
  474. if ((iflag & IXOFF) || (iflag & IXON) || (cflag & CRTSCTS))
  475. priv->control_state |= TIOCM_DTR | TIOCM_RTS;
  476. else
  477. priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
  478. mct_u232_set_modem_ctrl(serial, priv->control_state);
  479. #endif
  480. ;
  481. }
  482. memcpy(cfg, &priv->cfg, sizeof(*cfg));
  483. spin_unlock_irqrestore(&priv->lock, flags);
  484. /* now commit changes to device */
  485. klsi_105_chg_port_settings(port, cfg);
  486. err:
  487. kfree(cfg);
  488. }
  489. #if 0
  490. static void mct_u232_break_ctl(struct tty_struct *tty, int break_state)
  491. {
  492. struct usb_serial_port *port = tty->driver_data;
  493. struct usb_serial *serial = port->serial;
  494. struct mct_u232_private *priv =
  495. (struct mct_u232_private *)port->private;
  496. unsigned char lcr = priv->last_lcr;
  497. dev_dbg(&port->dev, "%s - state=%d\n", __func__, break_state);
  498. /* LOCKING */
  499. if (break_state)
  500. lcr |= MCT_U232_SET_BREAK;
  501. mct_u232_set_line_ctrl(serial, lcr);
  502. }
  503. #endif
  504. static int klsi_105_tiocmget(struct tty_struct *tty)
  505. {
  506. struct usb_serial_port *port = tty->driver_data;
  507. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  508. unsigned long flags;
  509. int rc;
  510. unsigned long line_state;
  511. rc = klsi_105_get_line_state(port, &line_state);
  512. if (rc < 0) {
  513. dev_err(&port->dev,
  514. "Reading line control failed (error = %d)\n", rc);
  515. /* better return value? EAGAIN? */
  516. return rc;
  517. }
  518. spin_lock_irqsave(&priv->lock, flags);
  519. priv->line_state = line_state;
  520. spin_unlock_irqrestore(&priv->lock, flags);
  521. dev_dbg(&port->dev, "%s - read line state 0x%lx\n", __func__, line_state);
  522. return (int)line_state;
  523. }
  524. module_usb_serial_driver(serial_drivers, id_table);
  525. MODULE_AUTHOR(DRIVER_AUTHOR);
  526. MODULE_DESCRIPTION(DRIVER_DESC);
  527. MODULE_LICENSE("GPL");