kl5kusb105.c 14 KB

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