kl5kusb105.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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. /* send READ_OFF */
  402. rc = usb_control_msg (port->serial->dev,
  403. usb_sndctrlpipe(port->serial->dev, 0),
  404. KL5KUSB105A_SIO_CONFIGURE,
  405. USB_TYPE_VENDOR | USB_DIR_OUT,
  406. KL5KUSB105A_SIO_CONFIGURE_READ_OFF,
  407. 0, /* index */
  408. NULL, 0,
  409. KLSI_TIMEOUT);
  410. if (rc < 0)
  411. err("Disabling read failed (error = %d)", rc);
  412. /* shutdown our bulk reads and writes */
  413. usb_kill_urb(port->write_urb);
  414. usb_kill_urb(port->read_urb);
  415. /* unlink our write pool */
  416. /* FIXME */
  417. /* wgg - do I need this? I think so. */
  418. usb_kill_urb(port->interrupt_in_urb);
  419. info("kl5kusb105 port stats: %ld bytes in, %ld bytes out", priv->bytes_in, priv->bytes_out);
  420. } /* klsi_105_close */
  421. /* We need to write a complete 64-byte data block and encode the
  422. * number actually sent in the first double-byte, LSB-order. That
  423. * leaves at most 62 bytes of payload.
  424. */
  425. #define KLSI_105_DATA_OFFSET 2 /* in the bulk urb data block */
  426. static int klsi_105_write (struct usb_serial_port *port,
  427. const unsigned char *buf, int count)
  428. {
  429. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  430. int result, size;
  431. int bytes_sent=0;
  432. dbg("%s - port %d", __FUNCTION__, port->number);
  433. while (count > 0) {
  434. /* try to find a free urb (write 0 bytes if none) */
  435. struct urb *urb = NULL;
  436. unsigned long flags;
  437. int i;
  438. /* since the pool is per-port we might not need the spin lock !? */
  439. spin_lock_irqsave (&priv->lock, flags);
  440. for (i=0; i<NUM_URBS; i++) {
  441. if (priv->write_urb_pool[i]->status != -EINPROGRESS) {
  442. urb = priv->write_urb_pool[i];
  443. dbg("%s - using pool URB %d", __FUNCTION__, i);
  444. break;
  445. }
  446. }
  447. spin_unlock_irqrestore (&priv->lock, flags);
  448. if (urb==NULL) {
  449. dbg("%s - no more free urbs", __FUNCTION__);
  450. goto exit;
  451. }
  452. if (urb->transfer_buffer == NULL) {
  453. urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC);
  454. if (urb->transfer_buffer == NULL) {
  455. err("%s - no more kernel memory...", __FUNCTION__);
  456. goto exit;
  457. }
  458. }
  459. size = min (count, port->bulk_out_size - KLSI_105_DATA_OFFSET);
  460. size = min (size, URB_TRANSFER_BUFFER_SIZE - KLSI_105_DATA_OFFSET);
  461. memcpy (urb->transfer_buffer + KLSI_105_DATA_OFFSET, buf, size);
  462. /* write payload size into transfer buffer */
  463. ((__u8 *)urb->transfer_buffer)[0] = (__u8) (size & 0xFF);
  464. ((__u8 *)urb->transfer_buffer)[1] = (__u8) ((size & 0xFF00)>>8);
  465. /* set up our urb */
  466. usb_fill_bulk_urb(urb, port->serial->dev,
  467. usb_sndbulkpipe(port->serial->dev,
  468. port->bulk_out_endpointAddress),
  469. urb->transfer_buffer,
  470. URB_TRANSFER_BUFFER_SIZE,
  471. klsi_105_write_bulk_callback,
  472. port);
  473. /* send the data out the bulk port */
  474. result = usb_submit_urb(urb, GFP_ATOMIC);
  475. if (result) {
  476. err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
  477. goto exit;
  478. }
  479. buf += size;
  480. bytes_sent += size;
  481. count -= size;
  482. }
  483. exit:
  484. /* lockless, but it's for debug info only... */
  485. priv->bytes_out+=bytes_sent;
  486. return bytes_sent; /* that's how much we wrote */
  487. } /* klsi_105_write */
  488. static void klsi_105_write_bulk_callback ( struct urb *urb)
  489. {
  490. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  491. dbg("%s - port %d", __FUNCTION__, port->number);
  492. if (urb->status) {
  493. dbg("%s - nonzero write bulk status received: %d", __FUNCTION__,
  494. urb->status);
  495. return;
  496. }
  497. usb_serial_port_softint(port);
  498. } /* klsi_105_write_bulk_completion_callback */
  499. /* return number of characters currently in the writing process */
  500. static int klsi_105_chars_in_buffer (struct usb_serial_port *port)
  501. {
  502. int chars = 0;
  503. int i;
  504. unsigned long flags;
  505. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  506. spin_lock_irqsave (&priv->lock, flags);
  507. for (i = 0; i < NUM_URBS; ++i) {
  508. if (priv->write_urb_pool[i]->status == -EINPROGRESS) {
  509. chars += URB_TRANSFER_BUFFER_SIZE;
  510. }
  511. }
  512. spin_unlock_irqrestore (&priv->lock, flags);
  513. dbg("%s - returns %d", __FUNCTION__, chars);
  514. return (chars);
  515. }
  516. static int klsi_105_write_room (struct usb_serial_port *port)
  517. {
  518. unsigned long flags;
  519. int i;
  520. int room = 0;
  521. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  522. spin_lock_irqsave (&priv->lock, flags);
  523. for (i = 0; i < NUM_URBS; ++i) {
  524. if (priv->write_urb_pool[i]->status != -EINPROGRESS) {
  525. room += URB_TRANSFER_BUFFER_SIZE;
  526. }
  527. }
  528. spin_unlock_irqrestore (&priv->lock, flags);
  529. dbg("%s - returns %d", __FUNCTION__, room);
  530. return (room);
  531. }
  532. static void klsi_105_read_bulk_callback (struct urb *urb)
  533. {
  534. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  535. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  536. struct tty_struct *tty;
  537. unsigned char *data = urb->transfer_buffer;
  538. int rc;
  539. dbg("%s - port %d", __FUNCTION__, port->number);
  540. /* The urb might have been killed. */
  541. if (urb->status) {
  542. dbg("%s - nonzero read bulk status received: %d", __FUNCTION__,
  543. urb->status);
  544. return;
  545. }
  546. /* The data received is again preceded by a length double-byte in LSB-
  547. * first order (see klsi_105_write() )
  548. */
  549. if (urb->actual_length == 0) {
  550. /* empty urbs seem to happen, we ignore them */
  551. /* dbg("%s - emtpy URB", __FUNCTION__); */
  552. ;
  553. } else if (urb->actual_length <= 2) {
  554. dbg("%s - size %d URB not understood", __FUNCTION__,
  555. urb->actual_length);
  556. usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
  557. urb->actual_length, data);
  558. } else {
  559. int bytes_sent = ((__u8 *) data)[0] +
  560. ((unsigned int) ((__u8 *) data)[1] << 8);
  561. tty = port->tty;
  562. /* we should immediately resubmit the URB, before attempting
  563. * to pass the data on to the tty layer. But that needs locking
  564. * against re-entry an then mixed-up data because of
  565. * intermixed tty_flip_buffer_push()s
  566. * FIXME
  567. */
  568. usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
  569. urb->actual_length, data);
  570. if (bytes_sent + 2 > urb->actual_length) {
  571. dbg("%s - trying to read more data than available"
  572. " (%d vs. %d)", __FUNCTION__,
  573. bytes_sent+2, urb->actual_length);
  574. /* cap at implied limit */
  575. bytes_sent = urb->actual_length - 2;
  576. }
  577. tty_buffer_request_room(tty, bytes_sent);
  578. tty_insert_flip_string(tty, data + 2, bytes_sent);
  579. tty_flip_buffer_push(tty);
  580. /* again lockless, but debug info only */
  581. priv->bytes_in += bytes_sent;
  582. }
  583. /* Continue trying to always read */
  584. usb_fill_bulk_urb(port->read_urb, port->serial->dev,
  585. usb_rcvbulkpipe(port->serial->dev,
  586. port->bulk_in_endpointAddress),
  587. port->read_urb->transfer_buffer,
  588. port->read_urb->transfer_buffer_length,
  589. klsi_105_read_bulk_callback,
  590. port);
  591. rc = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  592. if (rc)
  593. err("%s - failed resubmitting read urb, error %d", __FUNCTION__, rc);
  594. } /* klsi_105_read_bulk_callback */
  595. static void klsi_105_set_termios (struct usb_serial_port *port,
  596. struct ktermios *old_termios)
  597. {
  598. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  599. unsigned int iflag = port->tty->termios->c_iflag;
  600. unsigned int old_iflag = old_termios->c_iflag;
  601. unsigned int cflag = port->tty->termios->c_cflag;
  602. unsigned int old_cflag = old_termios->c_cflag;
  603. struct klsi_105_port_settings cfg;
  604. unsigned long flags;
  605. /* lock while we are modifying the settings */
  606. spin_lock_irqsave (&priv->lock, flags);
  607. /*
  608. * Update baud rate
  609. */
  610. if( (cflag & CBAUD) != (old_cflag & CBAUD) ) {
  611. /* reassert DTR and (maybe) RTS on transition from B0 */
  612. if( (old_cflag & CBAUD) == B0 ) {
  613. dbg("%s: baud was B0", __FUNCTION__);
  614. #if 0
  615. priv->control_state |= TIOCM_DTR;
  616. /* don't set RTS if using hardware flow control */
  617. if (!(old_cflag & CRTSCTS)) {
  618. priv->control_state |= TIOCM_RTS;
  619. }
  620. mct_u232_set_modem_ctrl(serial, priv->control_state);
  621. #endif
  622. }
  623. switch(cflag & CBAUD) {
  624. case B0: /* handled below */
  625. break;
  626. case B1200: priv->cfg.baudrate = kl5kusb105a_sio_b1200;
  627. break;
  628. case B2400: priv->cfg.baudrate = kl5kusb105a_sio_b2400;
  629. break;
  630. case B4800: priv->cfg.baudrate = kl5kusb105a_sio_b4800;
  631. break;
  632. case B9600: priv->cfg.baudrate = kl5kusb105a_sio_b9600;
  633. break;
  634. case B19200: priv->cfg.baudrate = kl5kusb105a_sio_b19200;
  635. break;
  636. case B38400: priv->cfg.baudrate = kl5kusb105a_sio_b38400;
  637. break;
  638. case B57600: priv->cfg.baudrate = kl5kusb105a_sio_b57600;
  639. break;
  640. case B115200: priv->cfg.baudrate = kl5kusb105a_sio_b115200;
  641. break;
  642. default:
  643. err("KLSI USB->Serial converter:"
  644. " unsupported baudrate request, using default"
  645. " of 9600");
  646. priv->cfg.baudrate = kl5kusb105a_sio_b9600;
  647. break;
  648. }
  649. if ((cflag & CBAUD) == B0 ) {
  650. dbg("%s: baud is B0", __FUNCTION__);
  651. /* Drop RTS and DTR */
  652. /* maybe this should be simulated by sending read
  653. * disable and read enable messages?
  654. */
  655. ;
  656. #if 0
  657. priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
  658. mct_u232_set_modem_ctrl(serial, priv->control_state);
  659. #endif
  660. }
  661. }
  662. if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
  663. /* set the number of data bits */
  664. switch (cflag & CSIZE) {
  665. case CS5:
  666. dbg("%s - 5 bits/byte not supported", __FUNCTION__);
  667. spin_unlock_irqrestore (&priv->lock, flags);
  668. return ;
  669. case CS6:
  670. dbg("%s - 6 bits/byte not supported", __FUNCTION__);
  671. spin_unlock_irqrestore (&priv->lock, flags);
  672. return ;
  673. case CS7:
  674. priv->cfg.databits = kl5kusb105a_dtb_7;
  675. break;
  676. case CS8:
  677. priv->cfg.databits = kl5kusb105a_dtb_8;
  678. break;
  679. default:
  680. err("CSIZE was not CS5-CS8, using default of 8");
  681. priv->cfg.databits = kl5kusb105a_dtb_8;
  682. break;
  683. }
  684. }
  685. /*
  686. * Update line control register (LCR)
  687. */
  688. if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD))
  689. || (cflag & CSTOPB) != (old_cflag & CSTOPB) ) {
  690. #if 0
  691. priv->last_lcr = 0;
  692. /* set the parity */
  693. if (cflag & PARENB)
  694. priv->last_lcr |= (cflag & PARODD) ?
  695. MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN;
  696. else
  697. priv->last_lcr |= MCT_U232_PARITY_NONE;
  698. /* set the number of stop bits */
  699. priv->last_lcr |= (cflag & CSTOPB) ?
  700. MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;
  701. mct_u232_set_line_ctrl(serial, priv->last_lcr);
  702. #endif
  703. ;
  704. }
  705. /*
  706. * Set flow control: well, I do not really now how to handle DTR/RTS.
  707. * Just do what we have seen with SniffUSB on Win98.
  708. */
  709. if( (iflag & IXOFF) != (old_iflag & IXOFF)
  710. || (iflag & IXON) != (old_iflag & IXON)
  711. || (cflag & CRTSCTS) != (old_cflag & CRTSCTS) ) {
  712. /* Drop DTR/RTS if no flow control otherwise assert */
  713. #if 0
  714. if ((iflag & IXOFF) || (iflag & IXON) || (cflag & CRTSCTS) )
  715. priv->control_state |= TIOCM_DTR | TIOCM_RTS;
  716. else
  717. priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
  718. mct_u232_set_modem_ctrl(serial, priv->control_state);
  719. #endif
  720. ;
  721. }
  722. memcpy (&cfg, &priv->cfg, sizeof(cfg));
  723. spin_unlock_irqrestore (&priv->lock, flags);
  724. /* now commit changes to device */
  725. klsi_105_chg_port_settings(port, &cfg);
  726. } /* klsi_105_set_termios */
  727. #if 0
  728. static void mct_u232_break_ctl( struct usb_serial_port *port, int break_state )
  729. {
  730. struct usb_serial *serial = port->serial;
  731. struct mct_u232_private *priv = (struct mct_u232_private *)port->private;
  732. unsigned char lcr = priv->last_lcr;
  733. dbg("%sstate=%d", __FUNCTION__, break_state);
  734. if (break_state)
  735. lcr |= MCT_U232_SET_BREAK;
  736. mct_u232_set_line_ctrl(serial, lcr);
  737. } /* mct_u232_break_ctl */
  738. #endif
  739. static int klsi_105_tiocmget (struct usb_serial_port *port, struct file *file)
  740. {
  741. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  742. unsigned long flags;
  743. int rc;
  744. unsigned long line_state;
  745. dbg("%s - request, just guessing", __FUNCTION__);
  746. rc = klsi_105_get_line_state(port, &line_state);
  747. if (rc < 0) {
  748. err("Reading line control failed (error = %d)", rc);
  749. /* better return value? EAGAIN? */
  750. return rc;
  751. }
  752. spin_lock_irqsave (&priv->lock, flags);
  753. priv->line_state = line_state;
  754. spin_unlock_irqrestore (&priv->lock, flags);
  755. dbg("%s - read line state 0x%lx", __FUNCTION__, line_state);
  756. return (int)line_state;
  757. }
  758. static int klsi_105_tiocmset (struct usb_serial_port *port, struct file *file,
  759. unsigned int set, unsigned int clear)
  760. {
  761. int retval = -EINVAL;
  762. dbg("%s", __FUNCTION__);
  763. /* if this ever gets implemented, it should be done something like this:
  764. struct usb_serial *serial = port->serial;
  765. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  766. unsigned long flags;
  767. int control;
  768. spin_lock_irqsave (&priv->lock, flags);
  769. if (set & TIOCM_RTS)
  770. priv->control_state |= TIOCM_RTS;
  771. if (set & TIOCM_DTR)
  772. priv->control_state |= TIOCM_DTR;
  773. if (clear & TIOCM_RTS)
  774. priv->control_state &= ~TIOCM_RTS;
  775. if (clear & TIOCM_DTR)
  776. priv->control_state &= ~TIOCM_DTR;
  777. control = priv->control_state;
  778. spin_unlock_irqrestore (&priv->lock, flags);
  779. retval = mct_u232_set_modem_ctrl(serial, control);
  780. */
  781. return retval;
  782. }
  783. static void klsi_105_throttle (struct usb_serial_port *port)
  784. {
  785. dbg("%s - port %d", __FUNCTION__, port->number);
  786. usb_kill_urb(port->read_urb);
  787. }
  788. static void klsi_105_unthrottle (struct usb_serial_port *port)
  789. {
  790. int result;
  791. dbg("%s - port %d", __FUNCTION__, port->number);
  792. port->read_urb->dev = port->serial->dev;
  793. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  794. if (result)
  795. err("%s - failed submitting read urb, error %d", __FUNCTION__,
  796. result);
  797. }
  798. static int __init klsi_105_init (void)
  799. {
  800. int retval;
  801. retval = usb_serial_register(&kl5kusb105d_device);
  802. if (retval)
  803. goto failed_usb_serial_register;
  804. retval = usb_register(&kl5kusb105d_driver);
  805. if (retval)
  806. goto failed_usb_register;
  807. info(DRIVER_DESC " " DRIVER_VERSION);
  808. return 0;
  809. failed_usb_register:
  810. usb_serial_deregister(&kl5kusb105d_device);
  811. failed_usb_serial_register:
  812. return retval;
  813. }
  814. static void __exit klsi_105_exit (void)
  815. {
  816. usb_deregister (&kl5kusb105d_driver);
  817. usb_serial_deregister (&kl5kusb105d_device);
  818. }
  819. module_init (klsi_105_init);
  820. module_exit (klsi_105_exit);
  821. MODULE_AUTHOR( DRIVER_AUTHOR );
  822. MODULE_DESCRIPTION( DRIVER_DESC );
  823. MODULE_LICENSE("GPL");
  824. module_param(debug, bool, S_IRUGO | S_IWUSR);
  825. MODULE_PARM_DESC(debug, "enable extensive debugging messages");
  826. /* vim: set sts=8 ts=8 sw=8: */