kl5kusb105.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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. int status = urb->status;
  492. dbg("%s - port %d", __FUNCTION__, port->number);
  493. if (status) {
  494. dbg("%s - nonzero write bulk status received: %d", __FUNCTION__,
  495. status);
  496. return;
  497. }
  498. usb_serial_port_softint(port);
  499. } /* klsi_105_write_bulk_completion_callback */
  500. /* return number of characters currently in the writing process */
  501. static int klsi_105_chars_in_buffer (struct usb_serial_port *port)
  502. {
  503. int chars = 0;
  504. int i;
  505. unsigned long flags;
  506. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  507. spin_lock_irqsave (&priv->lock, flags);
  508. for (i = 0; i < NUM_URBS; ++i) {
  509. if (priv->write_urb_pool[i]->status == -EINPROGRESS) {
  510. chars += URB_TRANSFER_BUFFER_SIZE;
  511. }
  512. }
  513. spin_unlock_irqrestore (&priv->lock, flags);
  514. dbg("%s - returns %d", __FUNCTION__, chars);
  515. return (chars);
  516. }
  517. static int klsi_105_write_room (struct usb_serial_port *port)
  518. {
  519. unsigned long flags;
  520. int i;
  521. int room = 0;
  522. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  523. spin_lock_irqsave (&priv->lock, flags);
  524. for (i = 0; i < NUM_URBS; ++i) {
  525. if (priv->write_urb_pool[i]->status != -EINPROGRESS) {
  526. room += URB_TRANSFER_BUFFER_SIZE;
  527. }
  528. }
  529. spin_unlock_irqrestore (&priv->lock, flags);
  530. dbg("%s - returns %d", __FUNCTION__, room);
  531. return (room);
  532. }
  533. static void klsi_105_read_bulk_callback (struct urb *urb)
  534. {
  535. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  536. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  537. struct tty_struct *tty;
  538. unsigned char *data = urb->transfer_buffer;
  539. int rc;
  540. int status = urb->status;
  541. dbg("%s - port %d", __FUNCTION__, port->number);
  542. /* The urb might have been killed. */
  543. if (status) {
  544. dbg("%s - nonzero read bulk status received: %d", __FUNCTION__,
  545. status);
  546. return;
  547. }
  548. /* The data received is again preceded by a length double-byte in LSB-
  549. * first order (see klsi_105_write() )
  550. */
  551. if (urb->actual_length == 0) {
  552. /* empty urbs seem to happen, we ignore them */
  553. /* dbg("%s - emtpy URB", __FUNCTION__); */
  554. ;
  555. } else if (urb->actual_length <= 2) {
  556. dbg("%s - size %d URB not understood", __FUNCTION__,
  557. urb->actual_length);
  558. usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
  559. urb->actual_length, data);
  560. } else {
  561. int bytes_sent = ((__u8 *) data)[0] +
  562. ((unsigned int) ((__u8 *) data)[1] << 8);
  563. tty = port->tty;
  564. /* we should immediately resubmit the URB, before attempting
  565. * to pass the data on to the tty layer. But that needs locking
  566. * against re-entry an then mixed-up data because of
  567. * intermixed tty_flip_buffer_push()s
  568. * FIXME
  569. */
  570. usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
  571. urb->actual_length, data);
  572. if (bytes_sent + 2 > urb->actual_length) {
  573. dbg("%s - trying to read more data than available"
  574. " (%d vs. %d)", __FUNCTION__,
  575. bytes_sent+2, urb->actual_length);
  576. /* cap at implied limit */
  577. bytes_sent = urb->actual_length - 2;
  578. }
  579. tty_buffer_request_room(tty, bytes_sent);
  580. tty_insert_flip_string(tty, data + 2, bytes_sent);
  581. tty_flip_buffer_push(tty);
  582. /* again lockless, but debug info only */
  583. priv->bytes_in += bytes_sent;
  584. }
  585. /* Continue trying to always read */
  586. usb_fill_bulk_urb(port->read_urb, port->serial->dev,
  587. usb_rcvbulkpipe(port->serial->dev,
  588. port->bulk_in_endpointAddress),
  589. port->read_urb->transfer_buffer,
  590. port->read_urb->transfer_buffer_length,
  591. klsi_105_read_bulk_callback,
  592. port);
  593. rc = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  594. if (rc)
  595. err("%s - failed resubmitting read urb, error %d", __FUNCTION__, rc);
  596. } /* klsi_105_read_bulk_callback */
  597. static void klsi_105_set_termios (struct usb_serial_port *port,
  598. struct ktermios *old_termios)
  599. {
  600. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  601. unsigned int iflag = port->tty->termios->c_iflag;
  602. unsigned int old_iflag = old_termios->c_iflag;
  603. unsigned int cflag = port->tty->termios->c_cflag;
  604. unsigned int old_cflag = old_termios->c_cflag;
  605. struct klsi_105_port_settings cfg;
  606. unsigned long flags;
  607. /* lock while we are modifying the settings */
  608. spin_lock_irqsave (&priv->lock, flags);
  609. /*
  610. * Update baud rate
  611. */
  612. if( (cflag & CBAUD) != (old_cflag & CBAUD) ) {
  613. /* reassert DTR and (maybe) RTS on transition from B0 */
  614. if( (old_cflag & CBAUD) == B0 ) {
  615. dbg("%s: baud was B0", __FUNCTION__);
  616. #if 0
  617. priv->control_state |= TIOCM_DTR;
  618. /* don't set RTS if using hardware flow control */
  619. if (!(old_cflag & CRTSCTS)) {
  620. priv->control_state |= TIOCM_RTS;
  621. }
  622. mct_u232_set_modem_ctrl(serial, priv->control_state);
  623. #endif
  624. }
  625. switch(tty_get_baud_rate(port->tty)) {
  626. case 0: /* handled below */
  627. break;
  628. case 1200:
  629. priv->cfg.baudrate = kl5kusb105a_sio_b1200;
  630. break;
  631. case 2400:
  632. priv->cfg.baudrate = kl5kusb105a_sio_b2400;
  633. break;
  634. case 4800:
  635. priv->cfg.baudrate = kl5kusb105a_sio_b4800;
  636. break;
  637. case 9600:
  638. priv->cfg.baudrate = kl5kusb105a_sio_b9600;
  639. break;
  640. case 19200:
  641. priv->cfg.baudrate = kl5kusb105a_sio_b19200;
  642. break;
  643. case 38400:
  644. priv->cfg.baudrate = kl5kusb105a_sio_b38400;
  645. break;
  646. case 57600:
  647. priv->cfg.baudrate = kl5kusb105a_sio_b57600;
  648. break;
  649. case 115200:
  650. priv->cfg.baudrate = kl5kusb105a_sio_b115200;
  651. break;
  652. default:
  653. err("KLSI USB->Serial converter:"
  654. " unsupported baudrate request, using default"
  655. " of 9600");
  656. priv->cfg.baudrate = kl5kusb105a_sio_b9600;
  657. break;
  658. }
  659. if ((cflag & CBAUD) == B0 ) {
  660. dbg("%s: baud is B0", __FUNCTION__);
  661. /* Drop RTS and DTR */
  662. /* maybe this should be simulated by sending read
  663. * disable and read enable messages?
  664. */
  665. ;
  666. #if 0
  667. priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
  668. mct_u232_set_modem_ctrl(serial, priv->control_state);
  669. #endif
  670. }
  671. }
  672. if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
  673. /* set the number of data bits */
  674. switch (cflag & CSIZE) {
  675. case CS5:
  676. dbg("%s - 5 bits/byte not supported", __FUNCTION__);
  677. spin_unlock_irqrestore (&priv->lock, flags);
  678. return ;
  679. case CS6:
  680. dbg("%s - 6 bits/byte not supported", __FUNCTION__);
  681. spin_unlock_irqrestore (&priv->lock, flags);
  682. return ;
  683. case CS7:
  684. priv->cfg.databits = kl5kusb105a_dtb_7;
  685. break;
  686. case CS8:
  687. priv->cfg.databits = kl5kusb105a_dtb_8;
  688. break;
  689. default:
  690. err("CSIZE was not CS5-CS8, using default of 8");
  691. priv->cfg.databits = kl5kusb105a_dtb_8;
  692. break;
  693. }
  694. }
  695. /*
  696. * Update line control register (LCR)
  697. */
  698. if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD))
  699. || (cflag & CSTOPB) != (old_cflag & CSTOPB) ) {
  700. #if 0
  701. priv->last_lcr = 0;
  702. /* set the parity */
  703. if (cflag & PARENB)
  704. priv->last_lcr |= (cflag & PARODD) ?
  705. MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN;
  706. else
  707. priv->last_lcr |= MCT_U232_PARITY_NONE;
  708. /* set the number of stop bits */
  709. priv->last_lcr |= (cflag & CSTOPB) ?
  710. MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;
  711. mct_u232_set_line_ctrl(serial, priv->last_lcr);
  712. #endif
  713. ;
  714. }
  715. /*
  716. * Set flow control: well, I do not really now how to handle DTR/RTS.
  717. * Just do what we have seen with SniffUSB on Win98.
  718. */
  719. if( (iflag & IXOFF) != (old_iflag & IXOFF)
  720. || (iflag & IXON) != (old_iflag & IXON)
  721. || (cflag & CRTSCTS) != (old_cflag & CRTSCTS) ) {
  722. /* Drop DTR/RTS if no flow control otherwise assert */
  723. #if 0
  724. if ((iflag & IXOFF) || (iflag & IXON) || (cflag & CRTSCTS) )
  725. priv->control_state |= TIOCM_DTR | TIOCM_RTS;
  726. else
  727. priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
  728. mct_u232_set_modem_ctrl(serial, priv->control_state);
  729. #endif
  730. ;
  731. }
  732. memcpy (&cfg, &priv->cfg, sizeof(cfg));
  733. spin_unlock_irqrestore (&priv->lock, flags);
  734. /* now commit changes to device */
  735. klsi_105_chg_port_settings(port, &cfg);
  736. } /* klsi_105_set_termios */
  737. #if 0
  738. static void mct_u232_break_ctl( struct usb_serial_port *port, int break_state )
  739. {
  740. struct usb_serial *serial = port->serial;
  741. struct mct_u232_private *priv = (struct mct_u232_private *)port->private;
  742. unsigned char lcr = priv->last_lcr;
  743. dbg("%sstate=%d", __FUNCTION__, break_state);
  744. if (break_state)
  745. lcr |= MCT_U232_SET_BREAK;
  746. mct_u232_set_line_ctrl(serial, lcr);
  747. } /* mct_u232_break_ctl */
  748. #endif
  749. static int klsi_105_tiocmget (struct usb_serial_port *port, struct file *file)
  750. {
  751. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  752. unsigned long flags;
  753. int rc;
  754. unsigned long line_state;
  755. dbg("%s - request, just guessing", __FUNCTION__);
  756. rc = klsi_105_get_line_state(port, &line_state);
  757. if (rc < 0) {
  758. err("Reading line control failed (error = %d)", rc);
  759. /* better return value? EAGAIN? */
  760. return rc;
  761. }
  762. spin_lock_irqsave (&priv->lock, flags);
  763. priv->line_state = line_state;
  764. spin_unlock_irqrestore (&priv->lock, flags);
  765. dbg("%s - read line state 0x%lx", __FUNCTION__, line_state);
  766. return (int)line_state;
  767. }
  768. static int klsi_105_tiocmset (struct usb_serial_port *port, struct file *file,
  769. unsigned int set, unsigned int clear)
  770. {
  771. int retval = -EINVAL;
  772. dbg("%s", __FUNCTION__);
  773. /* if this ever gets implemented, it should be done something like this:
  774. struct usb_serial *serial = port->serial;
  775. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  776. unsigned long flags;
  777. int control;
  778. spin_lock_irqsave (&priv->lock, flags);
  779. if (set & TIOCM_RTS)
  780. priv->control_state |= TIOCM_RTS;
  781. if (set & TIOCM_DTR)
  782. priv->control_state |= TIOCM_DTR;
  783. if (clear & TIOCM_RTS)
  784. priv->control_state &= ~TIOCM_RTS;
  785. if (clear & TIOCM_DTR)
  786. priv->control_state &= ~TIOCM_DTR;
  787. control = priv->control_state;
  788. spin_unlock_irqrestore (&priv->lock, flags);
  789. retval = mct_u232_set_modem_ctrl(serial, control);
  790. */
  791. return retval;
  792. }
  793. static void klsi_105_throttle (struct usb_serial_port *port)
  794. {
  795. dbg("%s - port %d", __FUNCTION__, port->number);
  796. usb_kill_urb(port->read_urb);
  797. }
  798. static void klsi_105_unthrottle (struct usb_serial_port *port)
  799. {
  800. int result;
  801. dbg("%s - port %d", __FUNCTION__, port->number);
  802. port->read_urb->dev = port->serial->dev;
  803. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  804. if (result)
  805. err("%s - failed submitting read urb, error %d", __FUNCTION__,
  806. result);
  807. }
  808. static int __init klsi_105_init (void)
  809. {
  810. int retval;
  811. retval = usb_serial_register(&kl5kusb105d_device);
  812. if (retval)
  813. goto failed_usb_serial_register;
  814. retval = usb_register(&kl5kusb105d_driver);
  815. if (retval)
  816. goto failed_usb_register;
  817. info(DRIVER_DESC " " DRIVER_VERSION);
  818. return 0;
  819. failed_usb_register:
  820. usb_serial_deregister(&kl5kusb105d_device);
  821. failed_usb_serial_register:
  822. return retval;
  823. }
  824. static void __exit klsi_105_exit (void)
  825. {
  826. usb_deregister (&kl5kusb105d_driver);
  827. usb_serial_deregister (&kl5kusb105d_device);
  828. }
  829. module_init (klsi_105_init);
  830. module_exit (klsi_105_exit);
  831. MODULE_AUTHOR( DRIVER_AUTHOR );
  832. MODULE_DESCRIPTION( DRIVER_DESC );
  833. MODULE_LICENSE("GPL");
  834. module_param(debug, bool, S_IRUGO | S_IWUSR);
  835. MODULE_PARM_DESC(debug, "enable extensive debugging messages");
  836. /* vim: set sts=8 ts=8 sw=8: */