kl5kusb105.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  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. dev_info(&port->serial->dev->dev,
  167. "%d byte block, baudrate %x, databits %d, u1 %d, u2 %d\n",
  168. settings->pktlen, settings->baudrate, settings->databits,
  169. settings->unknown1, settings->unknown2);
  170. return rc;
  171. } /* klsi_105_chg_port_settings */
  172. /* translate a 16-bit status value from the device to linux's TIO bits */
  173. static unsigned long klsi_105_status2linestate(const __u16 status)
  174. {
  175. unsigned long res = 0;
  176. res = ((status & KL5KUSB105A_DSR) ? TIOCM_DSR : 0)
  177. | ((status & KL5KUSB105A_CTS) ? TIOCM_CTS : 0)
  178. ;
  179. return res;
  180. }
  181. /*
  182. * Read line control via vendor command and return result through
  183. * *line_state_p
  184. */
  185. /* It seems that the status buffer has always only 2 bytes length */
  186. #define KLSI_STATUSBUF_LEN 2
  187. static int klsi_105_get_line_state(struct usb_serial_port *port,
  188. unsigned long *line_state_p)
  189. {
  190. int rc;
  191. __u8 status_buf[KLSI_STATUSBUF_LEN] = { -1, -1};
  192. __u16 status;
  193. dev_info(&port->serial->dev->dev, "sending SIO Poll request\n");
  194. rc = usb_control_msg(port->serial->dev,
  195. usb_rcvctrlpipe(port->serial->dev, 0),
  196. KL5KUSB105A_SIO_POLL,
  197. USB_TYPE_VENDOR | USB_DIR_IN,
  198. 0, /* value */
  199. 0, /* index */
  200. status_buf, KLSI_STATUSBUF_LEN,
  201. 10000
  202. );
  203. if (rc < 0)
  204. err("Reading line status failed (error = %d)", rc);
  205. else {
  206. status = get_unaligned_le16(status_buf);
  207. dev_info(&port->serial->dev->dev, "read status %x %x",
  208. status_buf[0], status_buf[1]);
  209. *line_state_p = klsi_105_status2linestate(status);
  210. }
  211. return rc;
  212. }
  213. /*
  214. * Driver's tty interface functions
  215. */
  216. static int klsi_105_startup(struct usb_serial *serial)
  217. {
  218. struct klsi_105_private *priv;
  219. int i, j;
  220. /* check if we support the product id (see keyspan.c)
  221. * FIXME
  222. */
  223. /* allocate the private data structure */
  224. for (i = 0; i < serial->num_ports; i++) {
  225. priv = kmalloc(sizeof(struct klsi_105_private),
  226. GFP_KERNEL);
  227. if (!priv) {
  228. dbg("%skmalloc for klsi_105_private failed.", __func__);
  229. i--;
  230. goto err_cleanup;
  231. }
  232. /* set initial values for control structures */
  233. priv->cfg.pktlen = 5;
  234. priv->cfg.baudrate = kl5kusb105a_sio_b9600;
  235. priv->cfg.databits = kl5kusb105a_dtb_8;
  236. priv->cfg.unknown1 = 0;
  237. priv->cfg.unknown2 = 1;
  238. priv->line_state = 0;
  239. priv->bytes_in = 0;
  240. priv->bytes_out = 0;
  241. usb_set_serial_port_data(serial->port[i], priv);
  242. spin_lock_init(&priv->lock);
  243. for (j = 0; j < NUM_URBS; j++) {
  244. struct urb *urb = usb_alloc_urb(0, GFP_KERNEL);
  245. priv->write_urb_pool[j] = urb;
  246. if (urb == NULL) {
  247. err("No more urbs???");
  248. goto err_cleanup;
  249. }
  250. urb->transfer_buffer =
  251. kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
  252. if (!urb->transfer_buffer) {
  253. err("%s - out of memory for urb buffers.",
  254. __func__);
  255. goto err_cleanup;
  256. }
  257. }
  258. /* priv->termios is left uninitalized until port opening */
  259. init_waitqueue_head(&serial->port[i]->write_wait);
  260. }
  261. return 0;
  262. err_cleanup:
  263. for (; i >= 0; i--) {
  264. priv = usb_get_serial_port_data(serial->port[i]);
  265. for (j = 0; j < NUM_URBS; j++) {
  266. if (priv->write_urb_pool[j]) {
  267. kfree(priv->write_urb_pool[j]->transfer_buffer);
  268. usb_free_urb(priv->write_urb_pool[j]);
  269. }
  270. }
  271. usb_set_serial_port_data(serial->port[i], NULL);
  272. }
  273. return -ENOMEM;
  274. } /* klsi_105_startup */
  275. static void klsi_105_shutdown(struct usb_serial *serial)
  276. {
  277. int i;
  278. dbg("%s", __func__);
  279. /* stop reads and writes on all ports */
  280. for (i = 0; i < serial->num_ports; ++i) {
  281. struct klsi_105_private *priv =
  282. usb_get_serial_port_data(serial->port[i]);
  283. unsigned long flags;
  284. if (priv) {
  285. /* kill our write urb pool */
  286. int j;
  287. struct urb **write_urbs = priv->write_urb_pool;
  288. spin_lock_irqsave(&priv->lock, flags);
  289. for (j = 0; j < NUM_URBS; j++) {
  290. if (write_urbs[j]) {
  291. /* FIXME - uncomment the following
  292. * usb_kill_urb call when the host
  293. * controllers get fixed to set
  294. * urb->dev = NULL after the urb is
  295. * finished. Otherwise this call
  296. * oopses. */
  297. /* usb_kill_urb(write_urbs[j]); */
  298. kfree(write_urbs[j]->transfer_buffer);
  299. usb_free_urb(write_urbs[j]);
  300. }
  301. }
  302. spin_unlock_irqrestore(&priv->lock, flags);
  303. kfree(priv);
  304. usb_set_serial_port_data(serial->port[i], NULL);
  305. }
  306. }
  307. } /* klsi_105_shutdown */
  308. static int klsi_105_open(struct tty_struct *tty,
  309. struct usb_serial_port *port, struct file *filp)
  310. {
  311. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  312. int retval = 0;
  313. int rc;
  314. int i;
  315. unsigned long line_state;
  316. struct klsi_105_port_settings cfg;
  317. unsigned long flags;
  318. dbg("%s port %d", __func__, port->number);
  319. /* force low_latency on so that our tty_push actually forces
  320. * the data through
  321. * tty->low_latency = 1; */
  322. /* Do a defined restart:
  323. * Set up sane default baud rate and send the 'READ_ON'
  324. * vendor command.
  325. * FIXME: set modem line control (how?)
  326. * Then read the modem line control and store values in
  327. * priv->line_state.
  328. */
  329. cfg.pktlen = 5;
  330. cfg.baudrate = kl5kusb105a_sio_b9600;
  331. cfg.databits = kl5kusb105a_dtb_8;
  332. cfg.unknown1 = 0;
  333. cfg.unknown2 = 1;
  334. klsi_105_chg_port_settings(port, &cfg);
  335. /* set up termios structure */
  336. spin_lock_irqsave(&priv->lock, flags);
  337. priv->termios.c_iflag = tty->termios->c_iflag;
  338. priv->termios.c_oflag = tty->termios->c_oflag;
  339. priv->termios.c_cflag = tty->termios->c_cflag;
  340. priv->termios.c_lflag = tty->termios->c_lflag;
  341. for (i = 0; i < NCCS; i++)
  342. priv->termios.c_cc[i] = tty->termios->c_cc[i];
  343. priv->cfg.pktlen = cfg.pktlen;
  344. priv->cfg.baudrate = cfg.baudrate;
  345. priv->cfg.databits = cfg.databits;
  346. priv->cfg.unknown1 = cfg.unknown1;
  347. priv->cfg.unknown2 = cfg.unknown2;
  348. spin_unlock_irqrestore(&priv->lock, flags);
  349. /* READ_ON and urb submission */
  350. usb_fill_bulk_urb(port->read_urb, port->serial->dev,
  351. usb_rcvbulkpipe(port->serial->dev,
  352. port->bulk_in_endpointAddress),
  353. port->read_urb->transfer_buffer,
  354. port->read_urb->transfer_buffer_length,
  355. klsi_105_read_bulk_callback,
  356. port);
  357. rc = usb_submit_urb(port->read_urb, GFP_KERNEL);
  358. if (rc) {
  359. err("%s - failed submitting read urb, error %d", __func__, rc);
  360. retval = rc;
  361. goto exit;
  362. }
  363. rc = usb_control_msg(port->serial->dev,
  364. usb_sndctrlpipe(port->serial->dev, 0),
  365. KL5KUSB105A_SIO_CONFIGURE,
  366. USB_TYPE_VENDOR|USB_DIR_OUT|USB_RECIP_INTERFACE,
  367. KL5KUSB105A_SIO_CONFIGURE_READ_ON,
  368. 0, /* index */
  369. NULL,
  370. 0,
  371. KLSI_TIMEOUT);
  372. if (rc < 0) {
  373. err("Enabling read failed (error = %d)", rc);
  374. retval = rc;
  375. } else
  376. dbg("%s - enabled reading", __func__);
  377. rc = klsi_105_get_line_state(port, &line_state);
  378. if (rc >= 0) {
  379. spin_lock_irqsave(&priv->lock, flags);
  380. priv->line_state = line_state;
  381. spin_unlock_irqrestore(&priv->lock, flags);
  382. dbg("%s - read line state 0x%lx", __func__, line_state);
  383. retval = 0;
  384. } else
  385. retval = rc;
  386. exit:
  387. return retval;
  388. } /* klsi_105_open */
  389. static void klsi_105_close(struct tty_struct *tty,
  390. struct usb_serial_port *port, struct file *filp)
  391. {
  392. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  393. int rc;
  394. dbg("%s port %d", __func__, port->number);
  395. mutex_lock(&port->serial->disc_mutex);
  396. if (!port->serial->disconnected) {
  397. /* send READ_OFF */
  398. rc = usb_control_msg(port->serial->dev,
  399. usb_sndctrlpipe(port->serial->dev, 0),
  400. KL5KUSB105A_SIO_CONFIGURE,
  401. USB_TYPE_VENDOR | USB_DIR_OUT,
  402. KL5KUSB105A_SIO_CONFIGURE_READ_OFF,
  403. 0, /* index */
  404. NULL, 0,
  405. KLSI_TIMEOUT);
  406. if (rc < 0)
  407. err("Disabling read failed (error = %d)", rc);
  408. }
  409. mutex_unlock(&port->serial->disc_mutex);
  410. /* shutdown our bulk reads and writes */
  411. usb_kill_urb(port->write_urb);
  412. usb_kill_urb(port->read_urb);
  413. /* unlink our write pool */
  414. /* FIXME */
  415. /* wgg - do I need this? I think so. */
  416. usb_kill_urb(port->interrupt_in_urb);
  417. dev_info(&port->serial->dev->dev,
  418. "port stats: %ld bytes in, %ld bytes out\n",
  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 = tty_port_tty_get(&port->port);
  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. tty_kref_put(tty);
  587. /* again lockless, but debug info only */
  588. priv->bytes_in += bytes_sent;
  589. }
  590. /* Continue trying to always read */
  591. usb_fill_bulk_urb(port->read_urb, port->serial->dev,
  592. usb_rcvbulkpipe(port->serial->dev,
  593. port->bulk_in_endpointAddress),
  594. port->read_urb->transfer_buffer,
  595. port->read_urb->transfer_buffer_length,
  596. klsi_105_read_bulk_callback,
  597. port);
  598. rc = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  599. if (rc)
  600. err("%s - failed resubmitting read urb, error %d",
  601. __func__, rc);
  602. } /* klsi_105_read_bulk_callback */
  603. static void klsi_105_set_termios(struct tty_struct *tty,
  604. struct usb_serial_port *port,
  605. struct ktermios *old_termios)
  606. {
  607. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  608. unsigned int iflag = tty->termios->c_iflag;
  609. unsigned int old_iflag = old_termios->c_iflag;
  610. unsigned int cflag = tty->termios->c_cflag;
  611. unsigned int old_cflag = old_termios->c_cflag;
  612. struct klsi_105_port_settings cfg;
  613. unsigned long flags;
  614. speed_t baud;
  615. /* lock while we are modifying the settings */
  616. spin_lock_irqsave(&priv->lock, flags);
  617. /*
  618. * Update baud rate
  619. */
  620. baud = tty_get_baud_rate(tty);
  621. if ((cflag & CBAUD) != (old_cflag & CBAUD)) {
  622. /* reassert DTR and (maybe) RTS on transition from B0 */
  623. if ((old_cflag & CBAUD) == B0) {
  624. dbg("%s: baud was B0", __func__);
  625. #if 0
  626. priv->control_state |= TIOCM_DTR;
  627. /* don't set RTS if using hardware flow control */
  628. if (!(old_cflag & CRTSCTS))
  629. priv->control_state |= TIOCM_RTS;
  630. mct_u232_set_modem_ctrl(serial, priv->control_state);
  631. #endif
  632. }
  633. }
  634. switch (baud) {
  635. case 0: /* handled below */
  636. break;
  637. case 1200:
  638. priv->cfg.baudrate = kl5kusb105a_sio_b1200;
  639. break;
  640. case 2400:
  641. priv->cfg.baudrate = kl5kusb105a_sio_b2400;
  642. break;
  643. case 4800:
  644. priv->cfg.baudrate = kl5kusb105a_sio_b4800;
  645. break;
  646. case 9600:
  647. priv->cfg.baudrate = kl5kusb105a_sio_b9600;
  648. break;
  649. case 19200:
  650. priv->cfg.baudrate = kl5kusb105a_sio_b19200;
  651. break;
  652. case 38400:
  653. priv->cfg.baudrate = kl5kusb105a_sio_b38400;
  654. break;
  655. case 57600:
  656. priv->cfg.baudrate = kl5kusb105a_sio_b57600;
  657. break;
  658. case 115200:
  659. priv->cfg.baudrate = kl5kusb105a_sio_b115200;
  660. break;
  661. default:
  662. dbg("KLSI USB->Serial converter:"
  663. " unsupported baudrate request, using default of 9600");
  664. priv->cfg.baudrate = kl5kusb105a_sio_b9600;
  665. baud = 9600;
  666. break;
  667. }
  668. if ((cflag & CBAUD) == B0) {
  669. dbg("%s: baud is B0", __func__);
  670. /* Drop RTS and DTR */
  671. /* maybe this should be simulated by sending read
  672. * disable and read enable messages?
  673. */
  674. ;
  675. #if 0
  676. priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
  677. mct_u232_set_modem_ctrl(serial, priv->control_state);
  678. #endif
  679. }
  680. tty_encode_baud_rate(tty, baud, baud);
  681. if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
  682. /* set the number of data bits */
  683. switch (cflag & CSIZE) {
  684. case CS5:
  685. dbg("%s - 5 bits/byte not supported", __func__);
  686. spin_unlock_irqrestore(&priv->lock, flags);
  687. return ;
  688. case CS6:
  689. dbg("%s - 6 bits/byte not supported", __func__);
  690. spin_unlock_irqrestore(&priv->lock, flags);
  691. return ;
  692. case CS7:
  693. priv->cfg.databits = kl5kusb105a_dtb_7;
  694. break;
  695. case CS8:
  696. priv->cfg.databits = kl5kusb105a_dtb_8;
  697. break;
  698. default:
  699. err("CSIZE was not CS5-CS8, using default of 8");
  700. priv->cfg.databits = kl5kusb105a_dtb_8;
  701. break;
  702. }
  703. }
  704. /*
  705. * Update line control register (LCR)
  706. */
  707. if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD))
  708. || (cflag & CSTOPB) != (old_cflag & CSTOPB)) {
  709. /* Not currently supported */
  710. tty->termios->c_cflag &= ~(PARENB|PARODD|CSTOPB);
  711. #if 0
  712. priv->last_lcr = 0;
  713. /* set the parity */
  714. if (cflag & PARENB)
  715. priv->last_lcr |= (cflag & PARODD) ?
  716. MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN;
  717. else
  718. priv->last_lcr |= MCT_U232_PARITY_NONE;
  719. /* set the number of stop bits */
  720. priv->last_lcr |= (cflag & CSTOPB) ?
  721. MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;
  722. mct_u232_set_line_ctrl(serial, priv->last_lcr);
  723. #endif
  724. ;
  725. }
  726. /*
  727. * Set flow control: well, I do not really now how to handle DTR/RTS.
  728. * Just do what we have seen with SniffUSB on Win98.
  729. */
  730. if ((iflag & IXOFF) != (old_iflag & IXOFF)
  731. || (iflag & IXON) != (old_iflag & IXON)
  732. || (cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
  733. /* Not currently supported */
  734. tty->termios->c_cflag &= ~CRTSCTS;
  735. /* Drop DTR/RTS if no flow control otherwise assert */
  736. #if 0
  737. if ((iflag & IXOFF) || (iflag & IXON) || (cflag & CRTSCTS))
  738. priv->control_state |= TIOCM_DTR | TIOCM_RTS;
  739. else
  740. priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
  741. mct_u232_set_modem_ctrl(serial, priv->control_state);
  742. #endif
  743. ;
  744. }
  745. memcpy(&cfg, &priv->cfg, sizeof(cfg));
  746. spin_unlock_irqrestore(&priv->lock, flags);
  747. /* now commit changes to device */
  748. klsi_105_chg_port_settings(port, &cfg);
  749. } /* klsi_105_set_termios */
  750. #if 0
  751. static void mct_u232_break_ctl(struct tty_struct *tty, int break_state)
  752. {
  753. struct usb_serial_port *port = tty->driver_data;
  754. struct usb_serial *serial = port->serial;
  755. struct mct_u232_private *priv =
  756. (struct mct_u232_private *)port->private;
  757. unsigned char lcr = priv->last_lcr;
  758. dbg("%sstate=%d", __func__, break_state);
  759. if (break_state)
  760. lcr |= MCT_U232_SET_BREAK;
  761. mct_u232_set_line_ctrl(serial, lcr);
  762. } /* mct_u232_break_ctl */
  763. #endif
  764. static int klsi_105_tiocmget(struct tty_struct *tty, struct file *file)
  765. {
  766. struct usb_serial_port *port = tty->driver_data;
  767. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  768. unsigned long flags;
  769. int rc;
  770. unsigned long line_state;
  771. dbg("%s - request, just guessing", __func__);
  772. rc = klsi_105_get_line_state(port, &line_state);
  773. if (rc < 0) {
  774. err("Reading line control failed (error = %d)", rc);
  775. /* better return value? EAGAIN? */
  776. return rc;
  777. }
  778. spin_lock_irqsave(&priv->lock, flags);
  779. priv->line_state = line_state;
  780. spin_unlock_irqrestore(&priv->lock, flags);
  781. dbg("%s - read line state 0x%lx", __func__, line_state);
  782. return (int)line_state;
  783. }
  784. static int klsi_105_tiocmset(struct tty_struct *tty, struct file *file,
  785. unsigned int set, unsigned int clear)
  786. {
  787. int retval = -EINVAL;
  788. dbg("%s", __func__);
  789. /* if this ever gets implemented, it should be done something like this:
  790. struct usb_serial *serial = port->serial;
  791. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  792. unsigned long flags;
  793. int control;
  794. spin_lock_irqsave (&priv->lock, flags);
  795. if (set & TIOCM_RTS)
  796. priv->control_state |= TIOCM_RTS;
  797. if (set & TIOCM_DTR)
  798. priv->control_state |= TIOCM_DTR;
  799. if (clear & TIOCM_RTS)
  800. priv->control_state &= ~TIOCM_RTS;
  801. if (clear & TIOCM_DTR)
  802. priv->control_state &= ~TIOCM_DTR;
  803. control = priv->control_state;
  804. spin_unlock_irqrestore (&priv->lock, flags);
  805. retval = mct_u232_set_modem_ctrl(serial, control);
  806. */
  807. return retval;
  808. }
  809. static void klsi_105_throttle(struct tty_struct *tty)
  810. {
  811. struct usb_serial_port *port = tty->driver_data;
  812. dbg("%s - port %d", __func__, port->number);
  813. usb_kill_urb(port->read_urb);
  814. }
  815. static void klsi_105_unthrottle(struct tty_struct *tty)
  816. {
  817. struct usb_serial_port *port = tty->driver_data;
  818. int result;
  819. dbg("%s - port %d", __func__, port->number);
  820. port->read_urb->dev = port->serial->dev;
  821. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  822. if (result)
  823. err("%s - failed submitting read urb, error %d", __func__,
  824. result);
  825. }
  826. static int __init klsi_105_init(void)
  827. {
  828. int retval;
  829. retval = usb_serial_register(&kl5kusb105d_device);
  830. if (retval)
  831. goto failed_usb_serial_register;
  832. retval = usb_register(&kl5kusb105d_driver);
  833. if (retval)
  834. goto failed_usb_register;
  835. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  836. DRIVER_DESC "\n");
  837. return 0;
  838. failed_usb_register:
  839. usb_serial_deregister(&kl5kusb105d_device);
  840. failed_usb_serial_register:
  841. return retval;
  842. }
  843. static void __exit klsi_105_exit(void)
  844. {
  845. usb_deregister(&kl5kusb105d_driver);
  846. usb_serial_deregister(&kl5kusb105d_device);
  847. }
  848. module_init(klsi_105_init);
  849. module_exit(klsi_105_exit);
  850. MODULE_AUTHOR(DRIVER_AUTHOR);
  851. MODULE_DESCRIPTION(DRIVER_DESC);
  852. MODULE_LICENSE("GPL");
  853. module_param(debug, bool, S_IRUGO | S_IWUSR);
  854. MODULE_PARM_DESC(debug, "enable extensive debugging messages");
  855. /* vim: set sts=8 ts=8 sw=8: */