kl5kusb105.c 29 KB

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