kl5kusb105.c 13 KB

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