kl5kusb105.c 27 KB

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