kl5kusb105.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:29k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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