kl5kusb105.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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 <linux/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 void 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,
  55. const struct ktermios *old_termios);
  56. static int klsi_105_tiocmget(struct tty_struct *tty);
  57. static void klsi_105_process_read_urb(struct urb *urb);
  58. static int klsi_105_prepare_write_buffer(struct usb_serial_port *port,
  59. void *dest, size_t size);
  60. /*
  61. * All of the device info needed for the KLSI converters.
  62. */
  63. static const struct usb_device_id id_table[] = {
  64. { USB_DEVICE(PALMCONNECT_VID, PALMCONNECT_PID) },
  65. { } /* Terminating entry */
  66. };
  67. MODULE_DEVICE_TABLE(usb, id_table);
  68. static struct usb_serial_driver kl5kusb105d_device = {
  69. .driver = {
  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_send(port->serial->dev,
  111. 0,
  112. KL5KUSB105A_SIO_SET_DATA,
  113. USB_TYPE_VENDOR | USB_DIR_OUT |
  114. USB_RECIP_INTERFACE,
  115. 0, /* value */
  116. 0, /* index */
  117. settings,
  118. sizeof(struct klsi_105_port_settings),
  119. KLSI_TIMEOUT,
  120. GFP_KERNEL);
  121. if (rc)
  122. dev_err(&port->dev,
  123. "Change port settings failed (error = %d)\n", rc);
  124. dev_dbg(&port->dev,
  125. "pktlen %u, baudrate 0x%02x, databits %u, u1 %u, u2 %u\n",
  126. settings->pktlen, settings->baudrate, settings->databits,
  127. settings->unknown1, settings->unknown2);
  128. return rc;
  129. }
  130. /*
  131. * Read line control via vendor command and return result through
  132. * the state pointer.
  133. */
  134. static int klsi_105_get_line_state(struct usb_serial_port *port,
  135. unsigned long *state)
  136. {
  137. u16 status;
  138. int rc;
  139. rc = usb_control_msg_recv(port->serial->dev, 0,
  140. KL5KUSB105A_SIO_POLL,
  141. USB_TYPE_VENDOR | USB_DIR_IN,
  142. 0, /* value */
  143. 0, /* index */
  144. &status, sizeof(status),
  145. 10000,
  146. GFP_KERNEL);
  147. if (rc) {
  148. dev_err(&port->dev, "reading line status failed: %d\n", rc);
  149. return rc;
  150. }
  151. le16_to_cpus(&status);
  152. dev_dbg(&port->dev, "read status %04x\n", status);
  153. *state = ((status & KL5KUSB105A_DSR) ? TIOCM_DSR : 0) |
  154. ((status & KL5KUSB105A_CTS) ? TIOCM_CTS : 0);
  155. return 0;
  156. }
  157. /*
  158. * Driver's tty interface functions
  159. */
  160. static int klsi_105_port_probe(struct usb_serial_port *port)
  161. {
  162. struct klsi_105_private *priv;
  163. priv = kmalloc_obj(*priv);
  164. if (!priv)
  165. return -ENOMEM;
  166. /* set initial values for control structures */
  167. priv->cfg.pktlen = 5;
  168. priv->cfg.baudrate = kl5kusb105a_sio_b9600;
  169. priv->cfg.databits = kl5kusb105a_dtb_8;
  170. priv->cfg.unknown1 = 0;
  171. priv->cfg.unknown2 = 1;
  172. priv->line_state = 0;
  173. spin_lock_init(&priv->lock);
  174. usb_set_serial_port_data(port, priv);
  175. return 0;
  176. }
  177. static void klsi_105_port_remove(struct usb_serial_port *port)
  178. {
  179. struct klsi_105_private *priv;
  180. priv = usb_get_serial_port_data(port);
  181. kfree(priv);
  182. }
  183. static int klsi_105_open(struct tty_struct *tty, struct usb_serial_port *port)
  184. {
  185. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  186. int retval = 0;
  187. int rc;
  188. unsigned long line_state;
  189. struct klsi_105_port_settings cfg;
  190. unsigned long flags;
  191. /* Do a defined restart:
  192. * Set up sane default baud rate and send the 'READ_ON'
  193. * vendor command.
  194. * FIXME: set modem line control (how?)
  195. * Then read the modem line control and store values in
  196. * priv->line_state.
  197. */
  198. cfg.pktlen = 5;
  199. cfg.baudrate = kl5kusb105a_sio_b9600;
  200. cfg.databits = kl5kusb105a_dtb_8;
  201. cfg.unknown1 = 0;
  202. cfg.unknown2 = 1;
  203. klsi_105_chg_port_settings(port, &cfg);
  204. spin_lock_irqsave(&priv->lock, flags);
  205. priv->cfg.pktlen = cfg.pktlen;
  206. priv->cfg.baudrate = cfg.baudrate;
  207. priv->cfg.databits = cfg.databits;
  208. priv->cfg.unknown1 = cfg.unknown1;
  209. priv->cfg.unknown2 = cfg.unknown2;
  210. spin_unlock_irqrestore(&priv->lock, flags);
  211. /* READ_ON and urb submission */
  212. rc = usb_serial_generic_open(tty, port);
  213. if (rc)
  214. return rc;
  215. rc = usb_control_msg(port->serial->dev,
  216. usb_sndctrlpipe(port->serial->dev, 0),
  217. KL5KUSB105A_SIO_CONFIGURE,
  218. USB_TYPE_VENDOR|USB_DIR_OUT|USB_RECIP_INTERFACE,
  219. KL5KUSB105A_SIO_CONFIGURE_READ_ON,
  220. 0, /* index */
  221. NULL,
  222. 0,
  223. KLSI_TIMEOUT);
  224. if (rc < 0) {
  225. dev_err(&port->dev, "Enabling read failed (error = %d)\n", rc);
  226. retval = rc;
  227. goto err_generic_close;
  228. } else
  229. dev_dbg(&port->dev, "%s - enabled reading\n", __func__);
  230. rc = klsi_105_get_line_state(port, &line_state);
  231. if (rc < 0) {
  232. retval = rc;
  233. goto err_disable_read;
  234. }
  235. spin_lock_irqsave(&priv->lock, flags);
  236. priv->line_state = line_state;
  237. spin_unlock_irqrestore(&priv->lock, flags);
  238. dev_dbg(&port->dev, "%s - read line state 0x%lx\n", __func__,
  239. line_state);
  240. return 0;
  241. err_disable_read:
  242. usb_control_msg(port->serial->dev,
  243. usb_sndctrlpipe(port->serial->dev, 0),
  244. KL5KUSB105A_SIO_CONFIGURE,
  245. USB_TYPE_VENDOR | USB_DIR_OUT,
  246. KL5KUSB105A_SIO_CONFIGURE_READ_OFF,
  247. 0, /* index */
  248. NULL, 0,
  249. KLSI_TIMEOUT);
  250. err_generic_close:
  251. usb_serial_generic_close(port);
  252. return retval;
  253. }
  254. static void klsi_105_close(struct usb_serial_port *port)
  255. {
  256. int rc;
  257. /* send READ_OFF */
  258. rc = usb_control_msg(port->serial->dev,
  259. usb_sndctrlpipe(port->serial->dev, 0),
  260. KL5KUSB105A_SIO_CONFIGURE,
  261. USB_TYPE_VENDOR | USB_DIR_OUT,
  262. KL5KUSB105A_SIO_CONFIGURE_READ_OFF,
  263. 0, /* index */
  264. NULL, 0,
  265. KLSI_TIMEOUT);
  266. if (rc < 0)
  267. dev_err(&port->dev, "failed to disable read: %d\n", rc);
  268. /* shutdown our bulk reads and writes */
  269. usb_serial_generic_close(port);
  270. }
  271. /* We need to write a complete 64-byte data block and encode the
  272. * number actually sent in the first double-byte, LSB-order. That
  273. * leaves at most 62 bytes of payload.
  274. */
  275. #define KLSI_HDR_LEN 2
  276. static int klsi_105_prepare_write_buffer(struct usb_serial_port *port,
  277. void *dest, size_t size)
  278. {
  279. unsigned char *buf = dest;
  280. int count;
  281. count = kfifo_out_locked(&port->write_fifo, buf + KLSI_HDR_LEN, size,
  282. &port->lock);
  283. put_unaligned_le16(count, buf);
  284. return count + KLSI_HDR_LEN;
  285. }
  286. /* The data received is preceded by a length double-byte in LSB-first order.
  287. */
  288. static void klsi_105_process_read_urb(struct urb *urb)
  289. {
  290. struct usb_serial_port *port = urb->context;
  291. unsigned char *data = urb->transfer_buffer;
  292. unsigned len;
  293. /* empty urbs seem to happen, we ignore them */
  294. if (!urb->actual_length)
  295. return;
  296. if (urb->actual_length <= KLSI_HDR_LEN) {
  297. dev_dbg(&port->dev, "%s - malformed packet\n", __func__);
  298. return;
  299. }
  300. len = get_unaligned_le16(data);
  301. if (len > urb->actual_length - KLSI_HDR_LEN) {
  302. dev_dbg(&port->dev, "%s - packet length mismatch\n", __func__);
  303. len = urb->actual_length - KLSI_HDR_LEN;
  304. }
  305. tty_insert_flip_string(&port->port, data + KLSI_HDR_LEN, len);
  306. tty_flip_buffer_push(&port->port);
  307. }
  308. static void klsi_105_set_termios(struct tty_struct *tty,
  309. struct usb_serial_port *port,
  310. const struct ktermios *old_termios)
  311. {
  312. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  313. struct device *dev = &port->dev;
  314. unsigned int iflag = tty->termios.c_iflag;
  315. unsigned int old_iflag = old_termios->c_iflag;
  316. unsigned int cflag = tty->termios.c_cflag;
  317. unsigned int old_cflag = old_termios->c_cflag;
  318. struct klsi_105_port_settings *cfg;
  319. unsigned long flags;
  320. speed_t baud;
  321. cfg = kmalloc_obj(*cfg);
  322. if (!cfg)
  323. return;
  324. /* lock while we are modifying the settings */
  325. spin_lock_irqsave(&priv->lock, flags);
  326. /*
  327. * Update baud rate
  328. */
  329. baud = tty_get_baud_rate(tty);
  330. switch (baud) {
  331. case 0: /* handled below */
  332. break;
  333. case 1200:
  334. priv->cfg.baudrate = kl5kusb105a_sio_b1200;
  335. break;
  336. case 2400:
  337. priv->cfg.baudrate = kl5kusb105a_sio_b2400;
  338. break;
  339. case 4800:
  340. priv->cfg.baudrate = kl5kusb105a_sio_b4800;
  341. break;
  342. case 9600:
  343. priv->cfg.baudrate = kl5kusb105a_sio_b9600;
  344. break;
  345. case 19200:
  346. priv->cfg.baudrate = kl5kusb105a_sio_b19200;
  347. break;
  348. case 38400:
  349. priv->cfg.baudrate = kl5kusb105a_sio_b38400;
  350. break;
  351. case 57600:
  352. priv->cfg.baudrate = kl5kusb105a_sio_b57600;
  353. break;
  354. case 115200:
  355. priv->cfg.baudrate = kl5kusb105a_sio_b115200;
  356. break;
  357. default:
  358. dev_dbg(dev, "unsupported baudrate, using 9600\n");
  359. priv->cfg.baudrate = kl5kusb105a_sio_b9600;
  360. baud = 9600;
  361. break;
  362. }
  363. /*
  364. * FIXME: implement B0 handling
  365. *
  366. * Maybe this should be simulated by sending read disable and read
  367. * enable messages?
  368. */
  369. tty_encode_baud_rate(tty, baud, baud);
  370. if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
  371. /* set the number of data bits */
  372. switch (cflag & CSIZE) {
  373. case CS5:
  374. dev_dbg(dev, "%s - 5 bits/byte not supported\n", __func__);
  375. spin_unlock_irqrestore(&priv->lock, flags);
  376. goto err;
  377. case CS6:
  378. dev_dbg(dev, "%s - 6 bits/byte not supported\n", __func__);
  379. spin_unlock_irqrestore(&priv->lock, flags);
  380. goto err;
  381. case CS7:
  382. priv->cfg.databits = kl5kusb105a_dtb_7;
  383. break;
  384. case CS8:
  385. priv->cfg.databits = kl5kusb105a_dtb_8;
  386. break;
  387. default:
  388. dev_err(dev, "CSIZE was not CS5-CS8, using default of 8\n");
  389. priv->cfg.databits = kl5kusb105a_dtb_8;
  390. break;
  391. }
  392. }
  393. /*
  394. * Update line control register (LCR)
  395. */
  396. if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD))
  397. || (cflag & CSTOPB) != (old_cflag & CSTOPB)) {
  398. /* Not currently supported */
  399. tty->termios.c_cflag &= ~(PARENB|PARODD|CSTOPB);
  400. }
  401. /*
  402. * Set flow control: well, I do not really now how to handle DTR/RTS.
  403. * Just do what we have seen with SniffUSB on Win98.
  404. */
  405. if ((iflag & IXOFF) != (old_iflag & IXOFF)
  406. || (iflag & IXON) != (old_iflag & IXON)
  407. || (cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
  408. /* Not currently supported */
  409. tty->termios.c_cflag &= ~CRTSCTS;
  410. }
  411. memcpy(cfg, &priv->cfg, sizeof(*cfg));
  412. spin_unlock_irqrestore(&priv->lock, flags);
  413. /* now commit changes to device */
  414. klsi_105_chg_port_settings(port, cfg);
  415. err:
  416. kfree(cfg);
  417. }
  418. static int klsi_105_tiocmget(struct tty_struct *tty)
  419. {
  420. struct usb_serial_port *port = tty->driver_data;
  421. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  422. unsigned long flags;
  423. int rc;
  424. unsigned long line_state;
  425. rc = klsi_105_get_line_state(port, &line_state);
  426. if (rc < 0) {
  427. dev_err(&port->dev,
  428. "Reading line control failed (error = %d)\n", rc);
  429. /* better return value? EAGAIN? */
  430. return rc;
  431. }
  432. spin_lock_irqsave(&priv->lock, flags);
  433. priv->line_state = line_state;
  434. spin_unlock_irqrestore(&priv->lock, flags);
  435. dev_dbg(&port->dev, "%s - read line state 0x%lx\n", __func__, line_state);
  436. return (int)line_state;
  437. }
  438. module_usb_serial_driver(serial_drivers, id_table);
  439. MODULE_AUTHOR(DRIVER_AUTHOR);
  440. MODULE_DESCRIPTION(DRIVER_DESC);
  441. MODULE_LICENSE("GPL");