kl5kusb105.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. /* READ_ON and urb submission */
  235. rc = usb_serial_generic_open(tty, port);
  236. if (rc) {
  237. retval = rc;
  238. goto err_free_cfg;
  239. }
  240. rc = usb_control_msg(port->serial->dev,
  241. usb_sndctrlpipe(port->serial->dev, 0),
  242. KL5KUSB105A_SIO_CONFIGURE,
  243. USB_TYPE_VENDOR|USB_DIR_OUT|USB_RECIP_INTERFACE,
  244. KL5KUSB105A_SIO_CONFIGURE_READ_ON,
  245. 0, /* index */
  246. NULL,
  247. 0,
  248. KLSI_TIMEOUT);
  249. if (rc < 0) {
  250. dev_err(&port->dev, "Enabling read failed (error = %d)\n", rc);
  251. retval = rc;
  252. goto err_generic_close;
  253. } else
  254. dev_dbg(&port->dev, "%s - enabled reading\n", __func__);
  255. rc = klsi_105_get_line_state(port, &line_state);
  256. if (rc < 0) {
  257. retval = rc;
  258. goto err_disable_read;
  259. }
  260. spin_lock_irqsave(&priv->lock, flags);
  261. priv->line_state = line_state;
  262. spin_unlock_irqrestore(&priv->lock, flags);
  263. dev_dbg(&port->dev, "%s - read line state 0x%lx\n", __func__,
  264. line_state);
  265. return 0;
  266. err_disable_read:
  267. usb_control_msg(port->serial->dev,
  268. usb_sndctrlpipe(port->serial->dev, 0),
  269. KL5KUSB105A_SIO_CONFIGURE,
  270. USB_TYPE_VENDOR | USB_DIR_OUT,
  271. KL5KUSB105A_SIO_CONFIGURE_READ_OFF,
  272. 0, /* index */
  273. NULL, 0,
  274. KLSI_TIMEOUT);
  275. err_generic_close:
  276. usb_serial_generic_close(port);
  277. err_free_cfg:
  278. kfree(cfg);
  279. return retval;
  280. }
  281. static void klsi_105_close(struct usb_serial_port *port)
  282. {
  283. int rc;
  284. /* send READ_OFF */
  285. rc = usb_control_msg(port->serial->dev,
  286. usb_sndctrlpipe(port->serial->dev, 0),
  287. KL5KUSB105A_SIO_CONFIGURE,
  288. USB_TYPE_VENDOR | USB_DIR_OUT,
  289. KL5KUSB105A_SIO_CONFIGURE_READ_OFF,
  290. 0, /* index */
  291. NULL, 0,
  292. KLSI_TIMEOUT);
  293. if (rc < 0)
  294. dev_err(&port->dev, "failed to disable read: %d\n", rc);
  295. /* shutdown our bulk reads and writes */
  296. usb_serial_generic_close(port);
  297. }
  298. /* We need to write a complete 64-byte data block and encode the
  299. * number actually sent in the first double-byte, LSB-order. That
  300. * leaves at most 62 bytes of payload.
  301. */
  302. #define KLSI_HDR_LEN 2
  303. static int klsi_105_prepare_write_buffer(struct usb_serial_port *port,
  304. void *dest, size_t size)
  305. {
  306. unsigned char *buf = dest;
  307. int count;
  308. count = kfifo_out_locked(&port->write_fifo, buf + KLSI_HDR_LEN, size,
  309. &port->lock);
  310. put_unaligned_le16(count, buf);
  311. return count + KLSI_HDR_LEN;
  312. }
  313. /* The data received is preceded by a length double-byte in LSB-first order.
  314. */
  315. static void klsi_105_process_read_urb(struct urb *urb)
  316. {
  317. struct usb_serial_port *port = urb->context;
  318. unsigned char *data = urb->transfer_buffer;
  319. unsigned len;
  320. /* empty urbs seem to happen, we ignore them */
  321. if (!urb->actual_length)
  322. return;
  323. if (urb->actual_length <= KLSI_HDR_LEN) {
  324. dev_dbg(&port->dev, "%s - malformed packet\n", __func__);
  325. return;
  326. }
  327. len = get_unaligned_le16(data);
  328. if (len > urb->actual_length - KLSI_HDR_LEN) {
  329. dev_dbg(&port->dev, "%s - packet length mismatch\n", __func__);
  330. len = urb->actual_length - KLSI_HDR_LEN;
  331. }
  332. tty_insert_flip_string(&port->port, data + KLSI_HDR_LEN, len);
  333. tty_flip_buffer_push(&port->port);
  334. }
  335. static void klsi_105_set_termios(struct tty_struct *tty,
  336. struct usb_serial_port *port,
  337. struct ktermios *old_termios)
  338. {
  339. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  340. struct device *dev = &port->dev;
  341. unsigned int iflag = tty->termios.c_iflag;
  342. unsigned int old_iflag = old_termios->c_iflag;
  343. unsigned int cflag = tty->termios.c_cflag;
  344. unsigned int old_cflag = old_termios->c_cflag;
  345. struct klsi_105_port_settings *cfg;
  346. unsigned long flags;
  347. speed_t baud;
  348. cfg = kmalloc(sizeof(*cfg), GFP_KERNEL);
  349. if (!cfg)
  350. return;
  351. /* lock while we are modifying the settings */
  352. spin_lock_irqsave(&priv->lock, flags);
  353. /*
  354. * Update baud rate
  355. */
  356. baud = tty_get_baud_rate(tty);
  357. switch (baud) {
  358. case 0: /* handled below */
  359. break;
  360. case 1200:
  361. priv->cfg.baudrate = kl5kusb105a_sio_b1200;
  362. break;
  363. case 2400:
  364. priv->cfg.baudrate = kl5kusb105a_sio_b2400;
  365. break;
  366. case 4800:
  367. priv->cfg.baudrate = kl5kusb105a_sio_b4800;
  368. break;
  369. case 9600:
  370. priv->cfg.baudrate = kl5kusb105a_sio_b9600;
  371. break;
  372. case 19200:
  373. priv->cfg.baudrate = kl5kusb105a_sio_b19200;
  374. break;
  375. case 38400:
  376. priv->cfg.baudrate = kl5kusb105a_sio_b38400;
  377. break;
  378. case 57600:
  379. priv->cfg.baudrate = kl5kusb105a_sio_b57600;
  380. break;
  381. case 115200:
  382. priv->cfg.baudrate = kl5kusb105a_sio_b115200;
  383. break;
  384. default:
  385. dev_dbg(dev, "unsupported baudrate, using 9600\n");
  386. priv->cfg.baudrate = kl5kusb105a_sio_b9600;
  387. baud = 9600;
  388. break;
  389. }
  390. /*
  391. * FIXME: implement B0 handling
  392. *
  393. * Maybe this should be simulated by sending read disable and read
  394. * enable messages?
  395. */
  396. tty_encode_baud_rate(tty, baud, baud);
  397. if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
  398. /* set the number of data bits */
  399. switch (cflag & CSIZE) {
  400. case CS5:
  401. dev_dbg(dev, "%s - 5 bits/byte not supported\n", __func__);
  402. spin_unlock_irqrestore(&priv->lock, flags);
  403. goto err;
  404. case CS6:
  405. dev_dbg(dev, "%s - 6 bits/byte not supported\n", __func__);
  406. spin_unlock_irqrestore(&priv->lock, flags);
  407. goto err;
  408. case CS7:
  409. priv->cfg.databits = kl5kusb105a_dtb_7;
  410. break;
  411. case CS8:
  412. priv->cfg.databits = kl5kusb105a_dtb_8;
  413. break;
  414. default:
  415. dev_err(dev, "CSIZE was not CS5-CS8, using default of 8\n");
  416. priv->cfg.databits = kl5kusb105a_dtb_8;
  417. break;
  418. }
  419. }
  420. /*
  421. * Update line control register (LCR)
  422. */
  423. if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD))
  424. || (cflag & CSTOPB) != (old_cflag & CSTOPB)) {
  425. /* Not currently supported */
  426. tty->termios.c_cflag &= ~(PARENB|PARODD|CSTOPB);
  427. }
  428. /*
  429. * Set flow control: well, I do not really now how to handle DTR/RTS.
  430. * Just do what we have seen with SniffUSB on Win98.
  431. */
  432. if ((iflag & IXOFF) != (old_iflag & IXOFF)
  433. || (iflag & IXON) != (old_iflag & IXON)
  434. || (cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
  435. /* Not currently supported */
  436. tty->termios.c_cflag &= ~CRTSCTS;
  437. }
  438. memcpy(cfg, &priv->cfg, sizeof(*cfg));
  439. spin_unlock_irqrestore(&priv->lock, flags);
  440. /* now commit changes to device */
  441. klsi_105_chg_port_settings(port, cfg);
  442. err:
  443. kfree(cfg);
  444. }
  445. static int klsi_105_tiocmget(struct tty_struct *tty)
  446. {
  447. struct usb_serial_port *port = tty->driver_data;
  448. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  449. unsigned long flags;
  450. int rc;
  451. unsigned long line_state;
  452. rc = klsi_105_get_line_state(port, &line_state);
  453. if (rc < 0) {
  454. dev_err(&port->dev,
  455. "Reading line control failed (error = %d)\n", rc);
  456. /* better return value? EAGAIN? */
  457. return rc;
  458. }
  459. spin_lock_irqsave(&priv->lock, flags);
  460. priv->line_state = line_state;
  461. spin_unlock_irqrestore(&priv->lock, flags);
  462. dev_dbg(&port->dev, "%s - read line state 0x%lx\n", __func__, line_state);
  463. return (int)line_state;
  464. }
  465. module_usb_serial_driver(serial_drivers, id_table);
  466. MODULE_AUTHOR(DRIVER_AUTHOR);
  467. MODULE_DESCRIPTION(DRIVER_DESC);
  468. MODULE_LICENSE("GPL");