kl5kusb105.c 29 KB

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