kl5kusb105.c 27 KB

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