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