kl5kusb105.c 28 KB

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