kl5kusb105.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. /*
  2. * KLSI KL5KUSB105 chip RS232 converter driver
  3. *
  4. * Copyright (C) 2001 Utz-Uwe Haus <haus@uuhaus.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * All information about the device was acquired using SniffUSB ans snoopUSB
  12. * on Windows98.
  13. * It was written out of frustration with the PalmConnect USB Serial adapter
  14. * sold by Palm Inc.
  15. * Neither Palm, nor their contractor (MCCI) or their supplier (KLSI) provided
  16. * information that was not already available.
  17. *
  18. * It seems that KLSI bought some silicon-design information from ScanLogic,
  19. * whose SL11R processor is at the core of the KL5KUSB chipset from KLSI.
  20. * KLSI has firmware available for their devices; it is probable that the
  21. * firmware differs from that used by KLSI in their products. If you have an
  22. * original KLSI device and can provide some information on it, I would be
  23. * most interested in adding support for it here. If you have any information
  24. * on the protocol used (or find errors in my reverse-engineered stuff), please
  25. * let me know.
  26. *
  27. * The code was only tested with a PalmConnect USB adapter; if you
  28. * are adventurous, try it with any KLSI-based device and let me know how it
  29. * breaks so that I can fix it!
  30. */
  31. /* TODO:
  32. * check modem line signals
  33. * implement handshaking or decide that we do not support it
  34. */
  35. /* History:
  36. * 0.3a - implemented pools of write URBs
  37. * 0.3 - alpha version for public testing
  38. * 0.2 - TIOCMGET works, so autopilot(1) can be used!
  39. * 0.1 - can be used to to pilot-xfer -p /dev/ttyUSB0 -l
  40. *
  41. * The driver skeleton is mainly based on mct_u232.c and various other
  42. * pieces of code shamelessly copied from the drivers/usb/serial/ directory.
  43. */
  44. #include <linux/kernel.h>
  45. #include <linux/errno.h>
  46. #include <linux/init.h>
  47. #include <linux/slab.h>
  48. #include <linux/tty.h>
  49. #include <linux/tty_driver.h>
  50. #include <linux/tty_flip.h>
  51. #include <linux/module.h>
  52. #include <asm/uaccess.h>
  53. #include <asm/unaligned.h>
  54. #include <linux/usb.h>
  55. #include <linux/usb/serial.h>
  56. #include "kl5kusb105.h"
  57. static int debug;
  58. /*
  59. * Version Information
  60. */
  61. #define DRIVER_VERSION "v0.3a"
  62. #define DRIVER_AUTHOR "Utz-Uwe Haus <haus@uuhaus.de>"
  63. #define DRIVER_DESC "KLSI KL5KUSB105 chipset USB->Serial Converter driver"
  64. /*
  65. * Function prototypes
  66. */
  67. static int klsi_105_startup (struct usb_serial *serial);
  68. static void klsi_105_shutdown (struct usb_serial *serial);
  69. static int klsi_105_open (struct tty_struct *tty,
  70. struct usb_serial_port *port,
  71. struct file *filp);
  72. static void klsi_105_close (struct tty_struct *tty,
  73. struct usb_serial_port *port,
  74. struct file *filp);
  75. static int klsi_105_write (struct tty_struct *tty,
  76. struct usb_serial_port *port,
  77. const unsigned char *buf,
  78. int count);
  79. static void klsi_105_write_bulk_callback (struct urb *urb);
  80. static int klsi_105_chars_in_buffer (struct tty_struct *tty);
  81. static int klsi_105_write_room (struct tty_struct *tty);
  82. static void klsi_105_read_bulk_callback (struct urb *urb);
  83. static void klsi_105_set_termios (struct tty_struct *tty,
  84. struct usb_serial_port *port,
  85. struct ktermios *old);
  86. static void klsi_105_throttle (struct tty_struct *tty);
  87. static void klsi_105_unthrottle (struct tty_struct *tty);
  88. /*
  89. static void klsi_105_break_ctl (struct tty_struct *tty,
  90. int break_state );
  91. */
  92. static int klsi_105_tiocmget (struct tty_struct *tty,
  93. struct file *file);
  94. static int klsi_105_tiocmset (struct tty_struct *tty,
  95. struct file *file, unsigned int set,
  96. unsigned int clear);
  97. /*
  98. * All of the device info needed for the KLSI converters.
  99. */
  100. static struct usb_device_id id_table [] = {
  101. { USB_DEVICE(PALMCONNECT_VID, PALMCONNECT_PID) },
  102. { USB_DEVICE(KLSI_VID, KLSI_KL5KUSB105D_PID) },
  103. { } /* Terminating entry */
  104. };
  105. MODULE_DEVICE_TABLE (usb, id_table);
  106. static struct usb_driver kl5kusb105d_driver = {
  107. .name = "kl5kusb105d",
  108. .probe = usb_serial_probe,
  109. .disconnect = usb_serial_disconnect,
  110. .id_table = id_table,
  111. .no_dynamic_id = 1,
  112. };
  113. static struct usb_serial_driver kl5kusb105d_device = {
  114. .driver = {
  115. .owner = THIS_MODULE,
  116. .name = "kl5kusb105d",
  117. },
  118. .description = "KL5KUSB105D / PalmConnect",
  119. .usb_driver = &kl5kusb105d_driver,
  120. .id_table = id_table,
  121. .num_ports = 1,
  122. .open = klsi_105_open,
  123. .close = klsi_105_close,
  124. .write = klsi_105_write,
  125. .write_bulk_callback = klsi_105_write_bulk_callback,
  126. .chars_in_buffer = klsi_105_chars_in_buffer,
  127. .write_room = klsi_105_write_room,
  128. .read_bulk_callback =klsi_105_read_bulk_callback,
  129. .set_termios = klsi_105_set_termios,
  130. /*.break_ctl = klsi_105_break_ctl,*/
  131. .tiocmget = klsi_105_tiocmget,
  132. .tiocmset = klsi_105_tiocmset,
  133. .attach = klsi_105_startup,
  134. .shutdown = klsi_105_shutdown,
  135. .throttle = klsi_105_throttle,
  136. .unthrottle = klsi_105_unthrottle,
  137. };
  138. struct klsi_105_port_settings {
  139. __u8 pktlen; /* always 5, it seems */
  140. __u8 baudrate;
  141. __u8 databits;
  142. __u8 unknown1;
  143. __u8 unknown2;
  144. } __attribute__ ((packed));
  145. /* we implement a pool of NUM_URBS urbs per usb_serial */
  146. #define NUM_URBS 1
  147. #define URB_TRANSFER_BUFFER_SIZE 64
  148. struct klsi_105_private {
  149. struct klsi_105_port_settings cfg;
  150. struct ktermios termios;
  151. unsigned long line_state; /* modem line settings */
  152. /* write pool */
  153. struct urb * write_urb_pool[NUM_URBS];
  154. spinlock_t lock;
  155. unsigned long bytes_in;
  156. unsigned long bytes_out;
  157. };
  158. /*
  159. * Handle vendor specific USB requests
  160. */
  161. #define KLSI_TIMEOUT 5000 /* default urb timeout */
  162. static int klsi_105_chg_port_settings(struct usb_serial_port *port,
  163. struct klsi_105_port_settings *settings)
  164. {
  165. int rc;
  166. rc = usb_control_msg(port->serial->dev,
  167. usb_sndctrlpipe(port->serial->dev, 0),
  168. KL5KUSB105A_SIO_SET_DATA,
  169. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_INTERFACE,
  170. 0, /* value */
  171. 0, /* index */
  172. settings,
  173. sizeof(struct klsi_105_port_settings),
  174. KLSI_TIMEOUT);
  175. if (rc < 0)
  176. err("Change port settings failed (error = %d)", rc);
  177. info("%s - %d byte block, baudrate %x, databits %d, u1 %d, u2 %d",
  178. __func__,
  179. settings->pktlen,
  180. settings->baudrate, settings->databits,
  181. settings->unknown1, settings->unknown2);
  182. return rc;
  183. } /* klsi_105_chg_port_settings */
  184. /* translate a 16-bit status value from the device to linux's TIO bits */
  185. static unsigned long klsi_105_status2linestate(const __u16 status)
  186. {
  187. unsigned long res = 0;
  188. res = ((status & KL5KUSB105A_DSR) ? TIOCM_DSR : 0)
  189. | ((status & KL5KUSB105A_CTS) ? TIOCM_CTS : 0)
  190. ;
  191. return res;
  192. }
  193. /*
  194. * Read line control via vendor command and return result through
  195. * *line_state_p
  196. */
  197. /* It seems that the status buffer has always only 2 bytes length */
  198. #define KLSI_STATUSBUF_LEN 2
  199. static int klsi_105_get_line_state(struct usb_serial_port *port,
  200. unsigned long *line_state_p)
  201. {
  202. int rc;
  203. __u8 status_buf[KLSI_STATUSBUF_LEN] = { -1,-1};
  204. __u16 status;
  205. info("%s - sending SIO Poll request", __func__);
  206. rc = usb_control_msg(port->serial->dev,
  207. usb_rcvctrlpipe(port->serial->dev, 0),
  208. KL5KUSB105A_SIO_POLL,
  209. USB_TYPE_VENDOR | USB_DIR_IN,
  210. 0, /* value */
  211. 0, /* index */
  212. status_buf, KLSI_STATUSBUF_LEN,
  213. 10000
  214. );
  215. if (rc < 0)
  216. err("Reading line status failed (error = %d)", rc);
  217. else {
  218. status = get_unaligned_le16(status_buf);
  219. info("%s - read status %x %x", __func__,
  220. status_buf[0], status_buf[1]);
  221. *line_state_p = klsi_105_status2linestate(status);
  222. }
  223. return rc;
  224. }
  225. /*
  226. * Driver's tty interface functions
  227. */
  228. static int klsi_105_startup (struct usb_serial *serial)
  229. {
  230. struct klsi_105_private *priv;
  231. int i, j;
  232. /* check if we support the product id (see keyspan.c)
  233. * FIXME
  234. */
  235. /* allocate the private data structure */
  236. for (i=0; i<serial->num_ports; i++) {
  237. priv = kmalloc(sizeof(struct klsi_105_private),
  238. GFP_KERNEL);
  239. if (!priv) {
  240. dbg("%skmalloc for klsi_105_private failed.", __func__);
  241. i--;
  242. goto err_cleanup;
  243. }
  244. /* set initial values for control structures */
  245. priv->cfg.pktlen = 5;
  246. priv->cfg.baudrate = kl5kusb105a_sio_b9600;
  247. priv->cfg.databits = kl5kusb105a_dtb_8;
  248. priv->cfg.unknown1 = 0;
  249. priv->cfg.unknown2 = 1;
  250. priv->line_state = 0;
  251. priv->bytes_in = 0;
  252. priv->bytes_out = 0;
  253. usb_set_serial_port_data(serial->port[i], priv);
  254. spin_lock_init (&priv->lock);
  255. for (j=0; j<NUM_URBS; j++) {
  256. struct urb* urb = usb_alloc_urb(0, GFP_KERNEL);
  257. priv->write_urb_pool[j] = urb;
  258. if (urb == NULL) {
  259. err("No more urbs???");
  260. goto err_cleanup;
  261. }
  262. urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE,
  263. GFP_KERNEL);
  264. if (!urb->transfer_buffer) {
  265. err("%s - out of memory for urb buffers.", __func__);
  266. goto err_cleanup;
  267. }
  268. }
  269. /* priv->termios is left uninitalized until port opening */
  270. init_waitqueue_head(&serial->port[i]->write_wait);
  271. }
  272. return 0;
  273. err_cleanup:
  274. for (; i >= 0; i--) {
  275. priv = usb_get_serial_port_data(serial->port[i]);
  276. for (j=0; j < NUM_URBS; j++) {
  277. if (priv->write_urb_pool[j]) {
  278. kfree(priv->write_urb_pool[j]->transfer_buffer);
  279. usb_free_urb(priv->write_urb_pool[j]);
  280. }
  281. }
  282. usb_set_serial_port_data(serial->port[i], NULL);
  283. }
  284. return -ENOMEM;
  285. } /* klsi_105_startup */
  286. static void klsi_105_shutdown (struct usb_serial *serial)
  287. {
  288. int i;
  289. dbg("%s", __func__);
  290. /* stop reads and writes on all ports */
  291. for (i=0; i < serial->num_ports; ++i) {
  292. struct klsi_105_private *priv = usb_get_serial_port_data(serial->port[i]);
  293. unsigned long flags;
  294. if (priv) {
  295. /* kill our write urb pool */
  296. int j;
  297. struct urb **write_urbs = priv->write_urb_pool;
  298. spin_lock_irqsave(&priv->lock,flags);
  299. for (j = 0; j < NUM_URBS; j++) {
  300. if (write_urbs[j]) {
  301. /* FIXME - uncomment the following
  302. * usb_kill_urb call when the host
  303. * controllers get fixed to set
  304. * urb->dev = NULL after the urb is
  305. * finished. Otherwise this call
  306. * oopses. */
  307. /* usb_kill_urb(write_urbs[j]); */
  308. kfree(write_urbs[j]->transfer_buffer);
  309. usb_free_urb (write_urbs[j]);
  310. }
  311. }
  312. spin_unlock_irqrestore (&priv->lock, flags);
  313. kfree(priv);
  314. usb_set_serial_port_data(serial->port[i], NULL);
  315. }
  316. }
  317. } /* klsi_105_shutdown */
  318. static int klsi_105_open(struct tty_struct *tty,
  319. struct usb_serial_port *port, struct file *filp)
  320. {
  321. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  322. int retval = 0;
  323. int rc;
  324. int i;
  325. unsigned long line_state;
  326. struct klsi_105_port_settings cfg;
  327. unsigned long flags;
  328. dbg("%s port %d", __func__, port->number);
  329. /* force low_latency on so that our tty_push actually forces
  330. * the data through
  331. * tty->low_latency = 1; */
  332. /* Do a defined restart:
  333. * Set up sane default baud rate and send the 'READ_ON'
  334. * vendor command.
  335. * FIXME: set modem line control (how?)
  336. * Then read the modem line control and store values in
  337. * priv->line_state.
  338. */
  339. cfg.pktlen = 5;
  340. cfg.baudrate = kl5kusb105a_sio_b9600;
  341. cfg.databits = kl5kusb105a_dtb_8;
  342. cfg.unknown1 = 0;
  343. cfg.unknown2 = 1;
  344. klsi_105_chg_port_settings(port, &cfg);
  345. /* set up termios structure */
  346. spin_lock_irqsave (&priv->lock, flags);
  347. priv->termios.c_iflag = tty->termios->c_iflag;
  348. priv->termios.c_oflag = tty->termios->c_oflag;
  349. priv->termios.c_cflag = tty->termios->c_cflag;
  350. priv->termios.c_lflag = tty->termios->c_lflag;
  351. for (i=0; i<NCCS; i++)
  352. priv->termios.c_cc[i] = tty->termios->c_cc[i];
  353. priv->cfg.pktlen = cfg.pktlen;
  354. priv->cfg.baudrate = cfg.baudrate;
  355. priv->cfg.databits = cfg.databits;
  356. priv->cfg.unknown1 = cfg.unknown1;
  357. priv->cfg.unknown2 = cfg.unknown2;
  358. spin_unlock_irqrestore (&priv->lock, flags);
  359. /* READ_ON and urb submission */
  360. usb_fill_bulk_urb(port->read_urb, port->serial->dev,
  361. usb_rcvbulkpipe(port->serial->dev,
  362. port->bulk_in_endpointAddress),
  363. port->read_urb->transfer_buffer,
  364. port->read_urb->transfer_buffer_length,
  365. klsi_105_read_bulk_callback,
  366. port);
  367. rc = usb_submit_urb(port->read_urb, GFP_KERNEL);
  368. if (rc) {
  369. err("%s - failed submitting read urb, error %d", __func__, rc);
  370. retval = rc;
  371. goto exit;
  372. }
  373. rc = usb_control_msg(port->serial->dev,
  374. usb_sndctrlpipe(port->serial->dev,0),
  375. KL5KUSB105A_SIO_CONFIGURE,
  376. USB_TYPE_VENDOR|USB_DIR_OUT|USB_RECIP_INTERFACE,
  377. KL5KUSB105A_SIO_CONFIGURE_READ_ON,
  378. 0, /* index */
  379. NULL,
  380. 0,
  381. KLSI_TIMEOUT);
  382. if (rc < 0) {
  383. err("Enabling read failed (error = %d)", rc);
  384. retval = rc;
  385. } else
  386. dbg("%s - enabled reading", __func__);
  387. rc = klsi_105_get_line_state(port, &line_state);
  388. if (rc >= 0) {
  389. spin_lock_irqsave (&priv->lock, flags);
  390. priv->line_state = line_state;
  391. spin_unlock_irqrestore (&priv->lock, flags);
  392. dbg("%s - read line state 0x%lx", __func__, line_state);
  393. retval = 0;
  394. } else
  395. retval = rc;
  396. exit:
  397. return retval;
  398. } /* klsi_105_open */
  399. static void klsi_105_close(struct tty_struct *tty,
  400. struct usb_serial_port *port, struct file *filp)
  401. {
  402. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  403. int rc;
  404. dbg("%s port %d", __func__, port->number);
  405. mutex_lock(&port->serial->disc_mutex);
  406. if (!port->serial->disconnected) {
  407. /* send READ_OFF */
  408. rc = usb_control_msg (port->serial->dev,
  409. usb_sndctrlpipe(port->serial->dev, 0),
  410. KL5KUSB105A_SIO_CONFIGURE,
  411. USB_TYPE_VENDOR | USB_DIR_OUT,
  412. KL5KUSB105A_SIO_CONFIGURE_READ_OFF,
  413. 0, /* index */
  414. NULL, 0,
  415. KLSI_TIMEOUT);
  416. if (rc < 0)
  417. err("Disabling read failed (error = %d)", rc);
  418. }
  419. mutex_unlock(&port->serial->disc_mutex);
  420. /* shutdown our bulk reads and writes */
  421. usb_kill_urb(port->write_urb);
  422. usb_kill_urb(port->read_urb);
  423. /* unlink our write pool */
  424. /* FIXME */
  425. /* wgg - do I need this? I think so. */
  426. usb_kill_urb(port->interrupt_in_urb);
  427. info("kl5kusb105 port stats: %ld bytes in, %ld bytes out", priv->bytes_in, priv->bytes_out);
  428. } /* klsi_105_close */
  429. /* We need to write a complete 64-byte data block and encode the
  430. * number actually sent in the first double-byte, LSB-order. That
  431. * leaves at most 62 bytes of payload.
  432. */
  433. #define KLSI_105_DATA_OFFSET 2 /* in the bulk urb data block */
  434. static int klsi_105_write(struct tty_struct *tty,
  435. struct usb_serial_port *port, const unsigned char *buf, int count)
  436. {
  437. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  438. int result, size;
  439. int bytes_sent=0;
  440. dbg("%s - port %d", __func__, port->number);
  441. while (count > 0) {
  442. /* try to find a free urb (write 0 bytes if none) */
  443. struct urb *urb = NULL;
  444. unsigned long flags;
  445. int i;
  446. /* since the pool is per-port we might not need the spin lock !? */
  447. spin_lock_irqsave (&priv->lock, flags);
  448. for (i=0; i<NUM_URBS; i++) {
  449. if (priv->write_urb_pool[i]->status != -EINPROGRESS) {
  450. urb = priv->write_urb_pool[i];
  451. dbg("%s - using pool URB %d", __func__, i);
  452. break;
  453. }
  454. }
  455. spin_unlock_irqrestore (&priv->lock, flags);
  456. if (urb==NULL) {
  457. dbg("%s - no more free urbs", __func__);
  458. goto exit;
  459. }
  460. if (urb->transfer_buffer == NULL) {
  461. urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC);
  462. if (urb->transfer_buffer == NULL) {
  463. err("%s - no more kernel memory...", __func__);
  464. goto exit;
  465. }
  466. }
  467. size = min (count, port->bulk_out_size - KLSI_105_DATA_OFFSET);
  468. size = min (size, URB_TRANSFER_BUFFER_SIZE - KLSI_105_DATA_OFFSET);
  469. memcpy (urb->transfer_buffer + KLSI_105_DATA_OFFSET, buf, size);
  470. /* write payload size into transfer buffer */
  471. ((__u8 *)urb->transfer_buffer)[0] = (__u8) (size & 0xFF);
  472. ((__u8 *)urb->transfer_buffer)[1] = (__u8) ((size & 0xFF00)>>8);
  473. /* set up our urb */
  474. usb_fill_bulk_urb(urb, port->serial->dev,
  475. usb_sndbulkpipe(port->serial->dev,
  476. port->bulk_out_endpointAddress),
  477. urb->transfer_buffer,
  478. URB_TRANSFER_BUFFER_SIZE,
  479. klsi_105_write_bulk_callback,
  480. port);
  481. /* send the data out the bulk port */
  482. result = usb_submit_urb(urb, GFP_ATOMIC);
  483. if (result) {
  484. err("%s - failed submitting write urb, error %d", __func__, result);
  485. goto exit;
  486. }
  487. buf += size;
  488. bytes_sent += size;
  489. count -= size;
  490. }
  491. exit:
  492. /* lockless, but it's for debug info only... */
  493. priv->bytes_out+=bytes_sent;
  494. return bytes_sent; /* that's how much we wrote */
  495. } /* klsi_105_write */
  496. static void klsi_105_write_bulk_callback ( struct urb *urb)
  497. {
  498. struct usb_serial_port *port = urb->context;
  499. int status = urb->status;
  500. dbg("%s - port %d", __func__, port->number);
  501. if (status) {
  502. dbg("%s - nonzero write bulk status received: %d", __func__,
  503. status);
  504. return;
  505. }
  506. usb_serial_port_softint(port);
  507. } /* klsi_105_write_bulk_completion_callback */
  508. /* return number of characters currently in the writing process */
  509. static int klsi_105_chars_in_buffer (struct tty_struct *tty)
  510. {
  511. struct usb_serial_port *port = tty->driver_data;
  512. int chars = 0;
  513. int i;
  514. unsigned long flags;
  515. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  516. spin_lock_irqsave (&priv->lock, flags);
  517. for (i = 0; i < NUM_URBS; ++i) {
  518. if (priv->write_urb_pool[i]->status == -EINPROGRESS) {
  519. chars += URB_TRANSFER_BUFFER_SIZE;
  520. }
  521. }
  522. spin_unlock_irqrestore (&priv->lock, flags);
  523. dbg("%s - returns %d", __func__, chars);
  524. return (chars);
  525. }
  526. static int klsi_105_write_room (struct tty_struct *tty)
  527. {
  528. struct usb_serial_port *port = tty->driver_data;
  529. unsigned long flags;
  530. int i;
  531. int room = 0;
  532. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  533. spin_lock_irqsave (&priv->lock, flags);
  534. for (i = 0; i < NUM_URBS; ++i) {
  535. if (priv->write_urb_pool[i]->status != -EINPROGRESS) {
  536. room += URB_TRANSFER_BUFFER_SIZE;
  537. }
  538. }
  539. spin_unlock_irqrestore (&priv->lock, flags);
  540. dbg("%s - returns %d", __func__, room);
  541. return (room);
  542. }
  543. static void klsi_105_read_bulk_callback (struct urb *urb)
  544. {
  545. struct usb_serial_port *port = urb->context;
  546. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  547. struct tty_struct *tty;
  548. unsigned char *data = urb->transfer_buffer;
  549. int rc;
  550. int status = urb->status;
  551. dbg("%s - port %d", __func__, port->number);
  552. /* The urb might have been killed. */
  553. if (status) {
  554. dbg("%s - nonzero read bulk status received: %d", __func__,
  555. status);
  556. return;
  557. }
  558. /* The data received is again preceded by a length double-byte in LSB-
  559. * first order (see klsi_105_write() )
  560. */
  561. if (urb->actual_length == 0) {
  562. /* empty urbs seem to happen, we ignore them */
  563. /* dbg("%s - emtpy URB", __func__); */
  564. ;
  565. } else if (urb->actual_length <= 2) {
  566. dbg("%s - size %d URB not understood", __func__,
  567. urb->actual_length);
  568. usb_serial_debug_data(debug, &port->dev, __func__,
  569. urb->actual_length, data);
  570. } else {
  571. int bytes_sent = ((__u8 *) data)[0] +
  572. ((unsigned int) ((__u8 *) data)[1] << 8);
  573. tty = port->port.tty;
  574. /* we should immediately resubmit the URB, before attempting
  575. * to pass the data on to the tty layer. But that needs locking
  576. * against re-entry an then mixed-up data because of
  577. * intermixed tty_flip_buffer_push()s
  578. * FIXME
  579. */
  580. usb_serial_debug_data(debug, &port->dev, __func__,
  581. urb->actual_length, data);
  582. if (bytes_sent + 2 > urb->actual_length) {
  583. dbg("%s - trying to read more data than available"
  584. " (%d vs. %d)", __func__,
  585. bytes_sent+2, urb->actual_length);
  586. /* cap at implied limit */
  587. bytes_sent = urb->actual_length - 2;
  588. }
  589. tty_buffer_request_room(tty, bytes_sent);
  590. tty_insert_flip_string(tty, data + 2, bytes_sent);
  591. tty_flip_buffer_push(tty);
  592. /* again lockless, but debug info only */
  593. priv->bytes_in += bytes_sent;
  594. }
  595. /* Continue trying to always read */
  596. usb_fill_bulk_urb(port->read_urb, port->serial->dev,
  597. usb_rcvbulkpipe(port->serial->dev,
  598. port->bulk_in_endpointAddress),
  599. port->read_urb->transfer_buffer,
  600. port->read_urb->transfer_buffer_length,
  601. klsi_105_read_bulk_callback,
  602. port);
  603. rc = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  604. if (rc)
  605. err("%s - failed resubmitting read urb, error %d", __func__, rc);
  606. } /* klsi_105_read_bulk_callback */
  607. static void klsi_105_set_termios (struct tty_struct *tty,
  608. struct usb_serial_port *port,
  609. struct ktermios *old_termios)
  610. {
  611. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  612. unsigned int iflag = tty->termios->c_iflag;
  613. unsigned int old_iflag = old_termios->c_iflag;
  614. unsigned int cflag = tty->termios->c_cflag;
  615. unsigned int old_cflag = old_termios->c_cflag;
  616. struct klsi_105_port_settings cfg;
  617. unsigned long flags;
  618. speed_t baud;
  619. /* lock while we are modifying the settings */
  620. spin_lock_irqsave (&priv->lock, flags);
  621. /*
  622. * Update baud rate
  623. */
  624. baud = tty_get_baud_rate(tty);
  625. if( (cflag & CBAUD) != (old_cflag & CBAUD) ) {
  626. /* reassert DTR and (maybe) RTS on transition from B0 */
  627. if( (old_cflag & CBAUD) == B0 ) {
  628. dbg("%s: baud was B0", __func__);
  629. #if 0
  630. priv->control_state |= TIOCM_DTR;
  631. /* don't set RTS if using hardware flow control */
  632. if (!(old_cflag & CRTSCTS)) {
  633. priv->control_state |= TIOCM_RTS;
  634. }
  635. mct_u232_set_modem_ctrl(serial, priv->control_state);
  636. #endif
  637. }
  638. }
  639. switch(baud) {
  640. case 0: /* handled below */
  641. break;
  642. case 1200:
  643. priv->cfg.baudrate = kl5kusb105a_sio_b1200;
  644. break;
  645. case 2400:
  646. priv->cfg.baudrate = kl5kusb105a_sio_b2400;
  647. break;
  648. case 4800:
  649. priv->cfg.baudrate = kl5kusb105a_sio_b4800;
  650. break;
  651. case 9600:
  652. priv->cfg.baudrate = kl5kusb105a_sio_b9600;
  653. break;
  654. case 19200:
  655. priv->cfg.baudrate = kl5kusb105a_sio_b19200;
  656. break;
  657. case 38400:
  658. priv->cfg.baudrate = kl5kusb105a_sio_b38400;
  659. break;
  660. case 57600:
  661. priv->cfg.baudrate = kl5kusb105a_sio_b57600;
  662. break;
  663. case 115200:
  664. priv->cfg.baudrate = kl5kusb105a_sio_b115200;
  665. break;
  666. default:
  667. dbg("KLSI USB->Serial converter:"
  668. " unsupported baudrate request, using default"
  669. " of 9600");
  670. priv->cfg.baudrate = kl5kusb105a_sio_b9600;
  671. baud = 9600;
  672. break;
  673. }
  674. if ((cflag & CBAUD) == B0 ) {
  675. dbg("%s: baud is B0", __func__);
  676. /* Drop RTS and DTR */
  677. /* maybe this should be simulated by sending read
  678. * disable and read enable messages?
  679. */
  680. ;
  681. #if 0
  682. priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
  683. mct_u232_set_modem_ctrl(serial, priv->control_state);
  684. #endif
  685. }
  686. tty_encode_baud_rate(tty, baud, baud);
  687. if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
  688. /* set the number of data bits */
  689. switch (cflag & CSIZE) {
  690. case CS5:
  691. dbg("%s - 5 bits/byte not supported", __func__);
  692. spin_unlock_irqrestore (&priv->lock, flags);
  693. return ;
  694. case CS6:
  695. dbg("%s - 6 bits/byte not supported", __func__);
  696. spin_unlock_irqrestore (&priv->lock, flags);
  697. return ;
  698. case CS7:
  699. priv->cfg.databits = kl5kusb105a_dtb_7;
  700. break;
  701. case CS8:
  702. priv->cfg.databits = kl5kusb105a_dtb_8;
  703. break;
  704. default:
  705. err("CSIZE was not CS5-CS8, using default of 8");
  706. priv->cfg.databits = kl5kusb105a_dtb_8;
  707. break;
  708. }
  709. }
  710. /*
  711. * Update line control register (LCR)
  712. */
  713. if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD))
  714. || (cflag & CSTOPB) != (old_cflag & CSTOPB) ) {
  715. /* Not currently supported */
  716. tty->termios->c_cflag &= ~(PARENB|PARODD|CSTOPB);
  717. #if 0
  718. priv->last_lcr = 0;
  719. /* set the parity */
  720. if (cflag & PARENB)
  721. priv->last_lcr |= (cflag & PARODD) ?
  722. MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN;
  723. else
  724. priv->last_lcr |= MCT_U232_PARITY_NONE;
  725. /* set the number of stop bits */
  726. priv->last_lcr |= (cflag & CSTOPB) ?
  727. MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;
  728. mct_u232_set_line_ctrl(serial, priv->last_lcr);
  729. #endif
  730. ;
  731. }
  732. /*
  733. * Set flow control: well, I do not really now how to handle DTR/RTS.
  734. * Just do what we have seen with SniffUSB on Win98.
  735. */
  736. if( (iflag & IXOFF) != (old_iflag & IXOFF)
  737. || (iflag & IXON) != (old_iflag & IXON)
  738. || (cflag & CRTSCTS) != (old_cflag & CRTSCTS) ) {
  739. /* Not currently supported */
  740. tty->termios->c_cflag &= ~CRTSCTS;
  741. /* Drop DTR/RTS if no flow control otherwise assert */
  742. #if 0
  743. if ((iflag & IXOFF) || (iflag & IXON) || (cflag & CRTSCTS) )
  744. priv->control_state |= TIOCM_DTR | TIOCM_RTS;
  745. else
  746. priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
  747. mct_u232_set_modem_ctrl(serial, priv->control_state);
  748. #endif
  749. ;
  750. }
  751. memcpy (&cfg, &priv->cfg, sizeof(cfg));
  752. spin_unlock_irqrestore (&priv->lock, flags);
  753. /* now commit changes to device */
  754. klsi_105_chg_port_settings(port, &cfg);
  755. } /* klsi_105_set_termios */
  756. #if 0
  757. static void mct_u232_break_ctl( struct tty_struct *tty, int break_state )
  758. {
  759. struct usb_serial_port *port = tty->driver_data;
  760. struct usb_serial *serial = port->serial;
  761. struct mct_u232_private *priv = (struct mct_u232_private *)port->private;
  762. unsigned char lcr = priv->last_lcr;
  763. dbg("%sstate=%d", __func__, break_state);
  764. if (break_state)
  765. lcr |= MCT_U232_SET_BREAK;
  766. mct_u232_set_line_ctrl(serial, lcr);
  767. } /* mct_u232_break_ctl */
  768. #endif
  769. static int klsi_105_tiocmget (struct tty_struct *tty, struct file *file)
  770. {
  771. struct usb_serial_port *port = tty->driver_data;
  772. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  773. unsigned long flags;
  774. int rc;
  775. unsigned long line_state;
  776. dbg("%s - request, just guessing", __func__);
  777. rc = klsi_105_get_line_state(port, &line_state);
  778. if (rc < 0) {
  779. err("Reading line control failed (error = %d)", rc);
  780. /* better return value? EAGAIN? */
  781. return rc;
  782. }
  783. spin_lock_irqsave (&priv->lock, flags);
  784. priv->line_state = line_state;
  785. spin_unlock_irqrestore (&priv->lock, flags);
  786. dbg("%s - read line state 0x%lx", __func__, line_state);
  787. return (int)line_state;
  788. }
  789. static int klsi_105_tiocmset (struct tty_struct *tty, struct file *file,
  790. unsigned int set, unsigned int clear)
  791. {
  792. int retval = -EINVAL;
  793. dbg("%s", __func__);
  794. /* if this ever gets implemented, it should be done something like this:
  795. struct usb_serial *serial = port->serial;
  796. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  797. unsigned long flags;
  798. int control;
  799. spin_lock_irqsave (&priv->lock, flags);
  800. if (set & TIOCM_RTS)
  801. priv->control_state |= TIOCM_RTS;
  802. if (set & TIOCM_DTR)
  803. priv->control_state |= TIOCM_DTR;
  804. if (clear & TIOCM_RTS)
  805. priv->control_state &= ~TIOCM_RTS;
  806. if (clear & TIOCM_DTR)
  807. priv->control_state &= ~TIOCM_DTR;
  808. control = priv->control_state;
  809. spin_unlock_irqrestore (&priv->lock, flags);
  810. retval = mct_u232_set_modem_ctrl(serial, control);
  811. */
  812. return retval;
  813. }
  814. static void klsi_105_throttle (struct tty_struct *tty)
  815. {
  816. struct usb_serial_port *port = tty->driver_data;
  817. dbg("%s - port %d", __func__, port->number);
  818. usb_kill_urb(port->read_urb);
  819. }
  820. static void klsi_105_unthrottle (struct tty_struct *tty)
  821. {
  822. struct usb_serial_port *port = tty->driver_data;
  823. int result;
  824. dbg("%s - port %d", __func__, port->number);
  825. port->read_urb->dev = port->serial->dev;
  826. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  827. if (result)
  828. err("%s - failed submitting read urb, error %d", __func__,
  829. result);
  830. }
  831. static int __init klsi_105_init (void)
  832. {
  833. int retval;
  834. retval = usb_serial_register(&kl5kusb105d_device);
  835. if (retval)
  836. goto failed_usb_serial_register;
  837. retval = usb_register(&kl5kusb105d_driver);
  838. if (retval)
  839. goto failed_usb_register;
  840. info(DRIVER_DESC " " DRIVER_VERSION);
  841. return 0;
  842. failed_usb_register:
  843. usb_serial_deregister(&kl5kusb105d_device);
  844. failed_usb_serial_register:
  845. return retval;
  846. }
  847. static void __exit klsi_105_exit (void)
  848. {
  849. usb_deregister (&kl5kusb105d_driver);
  850. usb_serial_deregister (&kl5kusb105d_device);
  851. }
  852. module_init (klsi_105_init);
  853. module_exit (klsi_105_exit);
  854. MODULE_AUTHOR( DRIVER_AUTHOR );
  855. MODULE_DESCRIPTION( DRIVER_DESC );
  856. MODULE_LICENSE("GPL");
  857. module_param(debug, bool, S_IRUGO | S_IWUSR);
  858. MODULE_PARM_DESC(debug, "enable extensive debugging messages");
  859. /* vim: set sts=8 ts=8 sw=8: */