kl5kusb105.c 27 KB

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