kl5kusb105.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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. if (break_state)
  770. lcr |= MCT_U232_SET_BREAK;
  771. mct_u232_set_line_ctrl(serial, lcr);
  772. } /* mct_u232_break_ctl */
  773. #endif
  774. static int klsi_105_tiocmget(struct tty_struct *tty, struct file *file)
  775. {
  776. struct usb_serial_port *port = tty->driver_data;
  777. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  778. unsigned long flags;
  779. int rc;
  780. unsigned long line_state;
  781. dbg("%s - request, just guessing", __func__);
  782. rc = klsi_105_get_line_state(port, &line_state);
  783. if (rc < 0) {
  784. dev_err(&port->dev,
  785. "Reading line control failed (error = %d)\n", rc);
  786. /* better return value? EAGAIN? */
  787. return rc;
  788. }
  789. spin_lock_irqsave(&priv->lock, flags);
  790. priv->line_state = line_state;
  791. spin_unlock_irqrestore(&priv->lock, flags);
  792. dbg("%s - read line state 0x%lx", __func__, line_state);
  793. return (int)line_state;
  794. }
  795. static int klsi_105_tiocmset(struct tty_struct *tty, struct file *file,
  796. unsigned int set, unsigned int clear)
  797. {
  798. int retval = -EINVAL;
  799. dbg("%s", __func__);
  800. /* if this ever gets implemented, it should be done something like this:
  801. struct usb_serial *serial = port->serial;
  802. struct klsi_105_private *priv = usb_get_serial_port_data(port);
  803. unsigned long flags;
  804. int control;
  805. spin_lock_irqsave (&priv->lock, flags);
  806. if (set & TIOCM_RTS)
  807. priv->control_state |= TIOCM_RTS;
  808. if (set & TIOCM_DTR)
  809. priv->control_state |= TIOCM_DTR;
  810. if (clear & TIOCM_RTS)
  811. priv->control_state &= ~TIOCM_RTS;
  812. if (clear & TIOCM_DTR)
  813. priv->control_state &= ~TIOCM_DTR;
  814. control = priv->control_state;
  815. spin_unlock_irqrestore (&priv->lock, flags);
  816. retval = mct_u232_set_modem_ctrl(serial, control);
  817. */
  818. return retval;
  819. }
  820. static void klsi_105_throttle(struct tty_struct *tty)
  821. {
  822. struct usb_serial_port *port = tty->driver_data;
  823. dbg("%s - port %d", __func__, port->number);
  824. usb_kill_urb(port->read_urb);
  825. }
  826. static void klsi_105_unthrottle(struct tty_struct *tty)
  827. {
  828. struct usb_serial_port *port = tty->driver_data;
  829. int result;
  830. dbg("%s - port %d", __func__, port->number);
  831. port->read_urb->dev = port->serial->dev;
  832. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  833. if (result)
  834. dev_err(&port->dev,
  835. "%s - failed submitting read urb, error %d\n",
  836. __func__, result);
  837. }
  838. static int __init klsi_105_init(void)
  839. {
  840. int retval;
  841. retval = usb_serial_register(&kl5kusb105d_device);
  842. if (retval)
  843. goto failed_usb_serial_register;
  844. retval = usb_register(&kl5kusb105d_driver);
  845. if (retval)
  846. goto failed_usb_register;
  847. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  848. DRIVER_DESC "\n");
  849. return 0;
  850. failed_usb_register:
  851. usb_serial_deregister(&kl5kusb105d_device);
  852. failed_usb_serial_register:
  853. return retval;
  854. }
  855. static void __exit klsi_105_exit(void)
  856. {
  857. usb_deregister(&kl5kusb105d_driver);
  858. usb_serial_deregister(&kl5kusb105d_device);
  859. }
  860. module_init(klsi_105_init);
  861. module_exit(klsi_105_exit);
  862. MODULE_AUTHOR(DRIVER_AUTHOR);
  863. MODULE_DESCRIPTION(DRIVER_DESC);
  864. MODULE_LICENSE("GPL");
  865. module_param(debug, bool, S_IRUGO | S_IWUSR);
  866. MODULE_PARM_DESC(debug, "enable extensive debugging messages");
  867. /* vim: set sts=8 ts=8 sw=8: */