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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * USB Keyspan PDA / Xircom / Entregra Converter driver
  3.  *
  4.  * Copyright (c) 1999 - 2001 Greg Kroah-Hartman <greg@kroah.com>
  5.  * Copyright (c) 1999, 2000 Brian Warner <warner@lothar.com>
  6.  * Copyright (c) 2000 Al Borchers <borchers@steinerpoint.com>
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * See Documentation/usb/usb-serial.txt for more information on using this driver
  14.  * 
  15.  * (09/07/2001) gkh
  16.  * cleaned up the Xircom support.  Added ids for Entregra device which is
  17.  * the same as the Xircom device.  Enabled the code to be compiled for
  18.  * either Xircom or Keyspan devices.
  19.  *
  20.  * (08/11/2001) Cristian M. Craciunescu
  21.  * support for Xircom PGSDB9
  22.  *
  23.  * (05/31/2001) gkh
  24.  * switched from using spinlock to a semaphore, which fixes lots of problems.
  25.  *
  26.  * (04/08/2001) gb
  27.  * Identify version on module load.
  28.  * 
  29.  * (11/01/2000) Adam J. Richter
  30.  * usb_device_id table support
  31.  * 
  32.  * (10/05/2000) gkh
  33.  * Fixed bug with urb->dev not being set properly, now that the usb
  34.  * core needs it.
  35.  * 
  36.  * (08/28/2000) gkh
  37.  * Added locks for SMP safeness.
  38.  * Fixed MOD_INC and MOD_DEC logic and the ability to open a port more 
  39.  * than once.
  40.  * 
  41.  * (07/20/2000) borchers
  42.  * - keyspan_pda_write no longer sleeps if it is called on interrupt time;
  43.  *   PPP and the line discipline with stty echo on can call write on
  44.  *   interrupt time and this would cause an oops if write slept
  45.  * - if keyspan_pda_write is in an interrupt, it will not call
  46.  *   usb_control_msg (which sleeps) to query the room in the device
  47.  *   buffer, it simply uses the current room value it has
  48.  * - if the urb is busy or if it is throttled keyspan_pda_write just
  49.  *   returns 0, rather than sleeping to wait for this to change; the
  50.  *   write_chan code in n_tty.c will sleep if needed before calling
  51.  *   keyspan_pda_write again
  52.  * - if the device needs to be unthrottled, write now queues up the
  53.  *   call to usb_control_msg (which sleeps) to unthrottle the device
  54.  * - the wakeups from keyspan_pda_write_bulk_callback are queued rather
  55.  *   than done directly from the callback to avoid the race in write_chan
  56.  * - keyspan_pda_chars_in_buffer also indicates its buffer is full if the
  57.  *   urb status is -EINPROGRESS, meaning it cannot write at the moment
  58.  *      
  59.  * (07/19/2000) gkh
  60.  * Added module_init and module_exit functions to handle the fact that this
  61.  * driver is a loadable module now.
  62.  *
  63.  * (03/26/2000) gkh
  64.  * Split driver up into device specific pieces.
  65.  * 
  66.  */
  67. #include <linux/config.h>
  68. #include <linux/kernel.h>
  69. #include <linux/errno.h>
  70. #include <linux/init.h>
  71. #include <linux/slab.h>
  72. #include <linux/tty.h>
  73. #include <linux/tty_driver.h>
  74. #include <linux/tty_flip.h>
  75. #include <linux/module.h>
  76. #include <linux/spinlock.h>
  77. #include <linux/tqueue.h>
  78. #include <asm/uaccess.h>
  79. #include <linux/usb.h>
  80. #ifdef CONFIG_USB_SERIAL_DEBUG
  81. static int debug = 1;
  82. #else
  83. static int debug;
  84. #endif
  85. struct ezusb_hex_record {
  86. __u16 address;
  87. __u8 data_size;
  88. __u8 data[16];
  89. };
  90. /* make a simple define to handle if we are compiling keyspan_pda or xircom support */
  91. #if defined(CONFIG_USB_SERIAL_KEYSPAN_PDA) || defined(CONFIG_USB_SERIAL_KEYSPAN_PDA_MODULE)
  92. #define KEYSPAN
  93. #else
  94. #undef KEYSPAN
  95. #endif
  96. #if defined(CONFIG_USB_SERIAL_XIRCOM) || defined(CONFIG_USB_SERIAL_XIRCOM_MODULE)
  97. #define XIRCOM
  98. #else
  99. #undef XIRCOM
  100. #endif
  101. #ifdef KEYSPAN
  102. #include "keyspan_pda_fw.h"
  103. #endif
  104. #ifdef XIRCOM
  105. #include "xircom_pgs_fw.h"
  106. #endif
  107. #include "usb-serial.h"
  108. /*
  109.  * Version Information
  110.  */
  111. #define DRIVER_VERSION "v1.1"
  112. #define DRIVER_AUTHOR "Brian Warner <warner@lothar.com>"
  113. #define DRIVER_DESC "USB Keyspan PDA Converter driver"
  114. struct keyspan_pda_private {
  115. int tx_room;
  116. int tx_throttled;
  117. struct tq_struct wakeup_task;
  118. struct tq_struct unthrottle_task;
  119. };
  120. #define KEYSPAN_VENDOR_ID 0x06cd
  121. #define KEYSPAN_PDA_FAKE_ID 0x0103
  122. #define KEYSPAN_PDA_ID 0x0104 /* no clue */
  123. /* For Xircom PGSDB9 and older Entregra version of the same device */
  124. #define XIRCOM_VENDOR_ID 0x085a
  125. #define XIRCOM_FAKE_ID 0x8027
  126. #define ENTREGRA_VENDOR_ID 0x1645
  127. #define ENTREGRA_FAKE_ID 0x8093
  128. static __devinitdata struct usb_device_id id_table_combined [] = {
  129. #ifdef KEYSPAN
  130. { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
  131. #endif
  132. #ifdef XIRCOM
  133. { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
  134. { USB_DEVICE(ENTREGRA_VENDOR_ID, ENTREGRA_FAKE_ID) },
  135. #endif
  136. { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
  137. { } /* Terminating entry */
  138. };
  139. MODULE_DEVICE_TABLE (usb, id_table_combined);
  140. static struct usb_device_id id_table_std [] = {
  141. { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
  142. { } /* Terminating entry */
  143. };
  144. #ifdef KEYSPAN
  145. static struct usb_device_id id_table_fake [] = {
  146. { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
  147. { } /* Terminating entry */
  148. };
  149. #endif
  150. #ifdef XIRCOM
  151. static struct usb_device_id id_table_fake_xircom [] = {
  152.         { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
  153.         { USB_DEVICE(ENTREGRA_VENDOR_ID, ENTREGRA_FAKE_ID) },
  154.         { }                                             
  155. };
  156. #endif
  157. static void keyspan_pda_wakeup_write( struct usb_serial_port *port )
  158. {
  159. struct tty_struct *tty = port->tty;
  160. /* wake up port processes */
  161. wake_up_interruptible( &port->write_wait );
  162. /* wake up line discipline */
  163. if( (tty->flags & (1 << TTY_DO_WRITE_WAKEUP))
  164. && tty->ldisc.write_wakeup )
  165. (tty->ldisc.write_wakeup)(tty);
  166. /* wake up other tty processes */
  167. wake_up_interruptible( &tty->write_wait );
  168. /* For 2.2.16 backport -- wake_up_interruptible( &tty->poll_wait ); */
  169. }
  170. static void keyspan_pda_request_unthrottle( struct usb_serial *serial )
  171. {
  172. int result;
  173. dbg(" request_unthrottle");
  174. /* ask the device to tell us when the tx buffer becomes
  175.    sufficiently empty */
  176. result = usb_control_msg(serial->dev, 
  177.  usb_sndctrlpipe(serial->dev, 0),
  178.  7, /* request_unthrottle */
  179.  USB_TYPE_VENDOR | USB_RECIP_INTERFACE
  180.  | USB_DIR_OUT,
  181.  16, /* value: threshold */
  182.  0, /* index */
  183.  NULL,
  184.  0,
  185.  2*HZ);
  186. if (result < 0)
  187. dbg("%s - error %d from usb_control_msg", 
  188.     __FUNCTION__, result);
  189. }
  190. static void keyspan_pda_rx_interrupt (struct urb *urb)
  191. {
  192. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  193. struct usb_serial *serial;
  194.         struct tty_struct *tty;
  195. unsigned char *data = urb->transfer_buffer;
  196. int i;
  197. struct keyspan_pda_private *priv;
  198. priv = (struct keyspan_pda_private *)(port->private);
  199. /* the urb might have been killed. */
  200. if (urb->status)
  201. return;
  202. if (port_paranoia_check (port, "keyspan_pda_rx_interrupt")) {
  203. return;
  204. }
  205. serial = port->serial;
  206. if (serial_paranoia_check (serial, "keyspan_pda_rx_interrupt")) {
  207. return;
  208. }
  209.   /* see if the message is data or a status interrupt */
  210. switch (data[0]) {
  211. case 0:
  212. /* rest of message is rx data */
  213. if (urb->actual_length) {
  214. tty = serial->port[0].tty;
  215. for (i = 1; i < urb->actual_length ; ++i) {
  216. tty_insert_flip_char(tty, data[i], 0);
  217. }
  218. tty_flip_buffer_push(tty);
  219. }
  220. break;
  221. case 1:
  222. /* status interrupt */
  223. dbg(" rx int, d1=%d, d2=%d", data[1], data[2]);
  224. switch (data[1]) {
  225. case 1: /* modemline change */
  226. break;
  227. case 2: /* tx unthrottle interrupt */
  228. tty = serial->port[0].tty;
  229. priv->tx_throttled = 0;
  230. /* queue up a wakeup at scheduler time */
  231. schedule_task(&priv->wakeup_task);
  232. break;
  233. default:
  234. break;
  235. }
  236. break;
  237. default:
  238. break;
  239. }
  240. /* INT urbs are automatically re-submitted */
  241. }
  242. static void keyspan_pda_rx_throttle (struct usb_serial_port *port)
  243. {
  244. /* stop receiving characters. We just turn off the URB request, and
  245.    let chars pile up in the device. If we're doing hardware
  246.    flowcontrol, the device will signal the other end when its buffer
  247.    fills up. If we're doing XON/XOFF, this would be a good time to
  248.    send an XOFF, although it might make sense to foist that off
  249.    upon the device too. */
  250. dbg("keyspan_pda_rx_throttle port %d", port->number);
  251. usb_unlink_urb(port->interrupt_in_urb);
  252. }
  253. static void keyspan_pda_rx_unthrottle (struct usb_serial_port *port)
  254. {
  255. /* just restart the receive interrupt URB */
  256. dbg("keyspan_pda_rx_unthrottle port %d", port->number);
  257. port->interrupt_in_urb->dev = port->serial->dev;
  258. if (usb_submit_urb(port->interrupt_in_urb))
  259. dbg(" usb_submit_urb(read urb) failed");
  260. return;
  261. }
  262. static int keyspan_pda_setbaud (struct usb_serial *serial, int baud)
  263. {
  264. int rc;
  265. int bindex;
  266. switch(baud) {
  267. case 110: bindex = 0; break;
  268. case 300: bindex = 1; break;
  269. case 1200: bindex = 2; break;
  270. case 2400: bindex = 3; break;
  271. case 4800: bindex = 4; break;
  272. case 9600: bindex = 5; break;
  273. case 19200: bindex = 6; break;
  274. case 38400: bindex = 7; break;
  275. case 57600: bindex = 8; break;
  276. case 115200: bindex = 9; break;
  277. default: return -EINVAL;
  278. }
  279. /* rather than figure out how to sleep while waiting for this
  280.    to complete, I just use the "legacy" API. */
  281. rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  282.      0, /* set baud */
  283.      USB_TYPE_VENDOR 
  284.      | USB_RECIP_INTERFACE
  285.      | USB_DIR_OUT, /* type */
  286.      bindex, /* value */
  287.      0, /* index */
  288.      NULL, /* &data */
  289.      0, /* size */
  290.      2*HZ); /* timeout */
  291. return(rc);
  292. }
  293. static void keyspan_pda_break_ctl (struct usb_serial_port *port, int break_state)
  294. {
  295. struct usb_serial *serial = port->serial;
  296. int value;
  297. int result;
  298. if (break_state == -1)
  299. value = 1; /* start break */
  300. else
  301. value = 0; /* clear break */
  302. result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  303. 4, /* set break */
  304. USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
  305. value, 0, NULL, 0, 2*HZ);
  306. if (result < 0)
  307. dbg("%s - error %d from usb_control_msg", 
  308.     __FUNCTION__, result);
  309. /* there is something funky about this.. the TCSBRK that 'cu' performs
  310.    ought to translate into a break_ctl(-1),break_ctl(0) pair HZ/4
  311.    seconds apart, but it feels like the break sent isn't as long as it
  312.    is on /dev/ttyS0 */
  313. }
  314. static void keyspan_pda_set_termios (struct usb_serial_port *port, 
  315.      struct termios *old_termios)
  316. {
  317. struct usb_serial *serial = port->serial;
  318. unsigned int cflag = port->tty->termios->c_cflag;
  319. /* cflag specifies lots of stuff: number of stop bits, parity, number
  320.    of data bits, baud. What can the device actually handle?:
  321.    CSTOPB (1 stop bit or 2)
  322.    PARENB (parity)
  323.    CSIZE (5bit .. 8bit)
  324.    There is minimal hw support for parity (a PSW bit seems to hold the
  325.    parity of whatever is in the accumulator). The UART either deals
  326.    with 10 bits (start, 8 data, stop) or 11 bits (start, 8 data,
  327.    1 special, stop). So, with firmware changes, we could do:
  328.    8N1: 10 bit
  329.    8N2: 11 bit, extra bit always (mark?)
  330.    8[EOMS]1: 11 bit, extra bit is parity
  331.    7[EOMS]1: 10 bit, b0/b7 is parity
  332.    7[EOMS]2: 11 bit, b0/b7 is parity, extra bit always (mark?)
  333.    HW flow control is dictated by the tty->termios->c_cflags & CRTSCTS
  334.    bit.
  335.    For now, just do baud. */
  336. switch (cflag & CBAUD) {
  337. /* we could support more values here, just need to calculate
  338.    the necessary divisors in the firmware. <asm/termbits.h>
  339.    has the Bnnn constants. */
  340. case B110: keyspan_pda_setbaud(serial, 110); break;
  341. case B300: keyspan_pda_setbaud(serial, 300); break;
  342. case B1200: keyspan_pda_setbaud(serial, 1200); break;
  343. case B2400: keyspan_pda_setbaud(serial, 2400); break;
  344. case B4800: keyspan_pda_setbaud(serial, 4800); break;
  345. case B9600: keyspan_pda_setbaud(serial, 9600); break;
  346. case B19200: keyspan_pda_setbaud(serial, 19200); break;
  347. case B38400: keyspan_pda_setbaud(serial, 38400); break;
  348. case B57600: keyspan_pda_setbaud(serial, 57600); break;
  349. case B115200: keyspan_pda_setbaud(serial, 115200); break;
  350. default: dbg("can't handle requested baud rate"); break;
  351. }
  352. }
  353. /* modem control pins: DTR and RTS are outputs and can be controlled.
  354.    DCD, RI, DSR, CTS are inputs and can be read. All outputs can also be
  355.    read. The byte passed is: DTR(b7) DCD RI DSR CTS RTS(b2) unused unused */
  356. static int keyspan_pda_get_modem_info(struct usb_serial *serial,
  357.       unsigned char *value)
  358. {
  359. int rc;
  360. unsigned char data;
  361. rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  362.      3, /* get pins */
  363.      USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_IN,
  364.      0, 0, &data, 1, 2*HZ);
  365. if (rc > 0)
  366. *value = data;
  367. return rc;
  368. }
  369. static int keyspan_pda_set_modem_info(struct usb_serial *serial,
  370.       unsigned char value)
  371. {
  372. int rc;
  373. rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  374.      3, /* set pins */
  375.      USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_OUT,
  376.      value, 0, NULL, 0, 2*HZ);
  377. return rc;
  378. }
  379. static int keyspan_pda_ioctl(struct usb_serial_port *port, struct file *file,
  380.      unsigned int cmd, unsigned long arg)
  381. {
  382. struct usb_serial *serial = port->serial;
  383. int rc;
  384. unsigned int value;
  385. unsigned char status, mask;
  386. switch (cmd) {
  387. case TIOCMGET: /* get modem pins state */
  388. rc = keyspan_pda_get_modem_info(serial, &status);
  389. if (rc < 0)
  390. return rc;
  391. value =
  392. ((status & (1<<7)) ? TIOCM_DTR : 0) |
  393. ((status & (1<<6)) ? TIOCM_CAR : 0) |
  394. ((status & (1<<5)) ? TIOCM_RNG : 0) |
  395. ((status & (1<<4)) ? TIOCM_DSR : 0) |
  396. ((status & (1<<3)) ? TIOCM_CTS : 0) |
  397. ((status & (1<<2)) ? TIOCM_RTS : 0);
  398. if (copy_to_user((unsigned int *)arg, &value, sizeof(int)))
  399. return -EFAULT;
  400. return 0;
  401. case TIOCMSET: /* set a state as returned by MGET */
  402. if (copy_from_user(&value, (unsigned int *)arg, sizeof(int)))
  403. return -EFAULT;
  404. status =
  405. ((value & TIOCM_DTR) ? (1<<7) : 0) |
  406. ((value & TIOCM_CAR) ? (1<<6) : 0) |
  407. ((value & TIOCM_RNG) ? (1<<5) : 0) |
  408. ((value & TIOCM_DSR) ? (1<<4) : 0) |
  409. ((value & TIOCM_CTS) ? (1<<3) : 0) |
  410. ((value & TIOCM_RTS) ? (1<<2) : 0);
  411. rc = keyspan_pda_set_modem_info(serial, status);
  412. if (rc < 0)
  413. return rc;
  414. return 0;
  415. case TIOCMBIS: /* set bits in bitmask <arg> */
  416. case TIOCMBIC: /* clear bits from bitmask <arg> */
  417. if (copy_from_user(&value, (unsigned int *)arg, sizeof(int)))
  418. return -EFAULT;
  419. rc = keyspan_pda_get_modem_info(serial, &status);
  420. if (rc < 0)
  421. return rc;
  422. mask =
  423. ((value & TIOCM_RTS) ? (1<<2) : 0) |
  424. ((value & TIOCM_DTR) ? (1<<7) : 0);
  425. if (cmd == TIOCMBIS)
  426. status |= mask;
  427. else
  428. status &= ~mask;
  429. rc = keyspan_pda_set_modem_info(serial, status);
  430. if (rc < 0)
  431. return rc;
  432. return 0;
  433. case TIOCMIWAIT:
  434. /* wait for any of the 4 modem inputs (DCD,RI,DSR,CTS)*/
  435. /* TODO */
  436. case TIOCGICOUNT:
  437. /* return count of modemline transitions */
  438. return 0; /* TODO */
  439. }
  440. return -ENOIOCTLCMD;
  441. }
  442. static int keyspan_pda_write(struct usb_serial_port *port, int from_user, 
  443.      const unsigned char *buf, int count)
  444. {
  445. struct usb_serial *serial = port->serial;
  446. int request_unthrottle = 0;
  447. int rc = 0;
  448. struct keyspan_pda_private *priv;
  449. priv = (struct keyspan_pda_private *)(port->private);
  450. /* guess how much room is left in the device's ring buffer, and if we
  451.    want to send more than that, check first, updating our notion of
  452.    what is left. If our write will result in no room left, ask the
  453.    device to give us an interrupt when the room available rises above
  454.    a threshold, and hold off all writers (eventually, those using
  455.    select() or poll() too) until we receive that unthrottle interrupt.
  456.    Block if we can't write anything at all, otherwise write as much as
  457.    we can. */
  458. dbg("keyspan_pda_write(%d)",count);
  459. if (count == 0) {
  460. dbg(" write request of 0 bytes");
  461. return (0);
  462. }
  463. /* we might block because of:
  464.    the TX urb is in-flight (wait until it completes)
  465.    the device is full (wait until it says there is room)
  466. */
  467. if (port->write_urb->status == -EINPROGRESS || priv->tx_throttled ) {
  468. return( 0 );
  469. }
  470. /* At this point the URB is in our control, nobody else can submit it
  471.    again (the only sudden transition was the one from EINPROGRESS to
  472.    finished).  Also, the tx process is not throttled. So we are
  473.    ready to write. */
  474. count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
  475. /* Check if we might overrun the Tx buffer.   If so, ask the
  476.    device how much room it really has.  This is done only on
  477.    scheduler time, since usb_control_msg() sleeps. */
  478. if (count > priv->tx_room && !in_interrupt()) {
  479. unsigned char room;
  480. rc = usb_control_msg(serial->dev, 
  481.      usb_rcvctrlpipe(serial->dev, 0),
  482.      6, /* write_room */
  483.      USB_TYPE_VENDOR | USB_RECIP_INTERFACE
  484.      | USB_DIR_IN,
  485.      0, /* value: 0 means "remaining room" */
  486.      0, /* index */
  487.      &room,
  488.      1,
  489.      2*HZ);
  490. if (rc < 0) {
  491. dbg(" roomquery failed");
  492. goto exit;
  493. }
  494. if (rc == 0) {
  495. dbg(" roomquery returned 0 bytes");
  496. rc = -EIO; /* device didn't return any data */
  497. goto exit;
  498. }
  499. dbg(" roomquery says %d", room);
  500. priv->tx_room = room;
  501. }
  502. if (count > priv->tx_room) {
  503. /* we're about to completely fill the Tx buffer, so
  504.    we'll be throttled afterwards. */
  505. count = priv->tx_room;
  506. request_unthrottle = 1;
  507. }
  508. if (count) {
  509. /* now transfer data */
  510. if (from_user) {
  511. if( copy_from_user(port->write_urb->transfer_buffer,
  512. buf, count) ) {
  513. rc = -EFAULT;
  514. goto exit;
  515. }
  516. }
  517. else {
  518. memcpy (port->write_urb->transfer_buffer, buf, count);
  519. }  
  520. /* send the data out the bulk port */
  521. port->write_urb->transfer_buffer_length = count;
  522. priv->tx_room -= count;
  523. port->write_urb->dev = port->serial->dev;
  524. rc = usb_submit_urb(port->write_urb);
  525. if (rc) {
  526. dbg(" usb_submit_urb(write bulk) failed");
  527. goto exit;
  528. }
  529. }
  530. else {
  531. /* There wasn't any room left, so we are throttled until
  532.    the buffer empties a bit */
  533. request_unthrottle = 1;
  534. }
  535. if (request_unthrottle) {
  536. priv->tx_throttled = 1; /* block writers */
  537. schedule_task(&priv->unthrottle_task);
  538. }
  539. rc = count;
  540. exit:
  541. return rc;
  542. }
  543. static void keyspan_pda_write_bulk_callback (struct urb *urb)
  544. {
  545. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  546. struct usb_serial *serial;
  547. struct keyspan_pda_private *priv;
  548. priv = (struct keyspan_pda_private *)(port->private);
  549. if (port_paranoia_check (port, "keyspan_pda_rx_interrupt")) {
  550. return;
  551. }
  552. serial = port->serial;
  553. if (serial_paranoia_check (serial, "keyspan_pda_rx_interrupt")) {
  554. return;
  555. }
  556. /* queue up a wakeup at scheduler time */
  557. schedule_task(&priv->wakeup_task);
  558. }
  559. static int keyspan_pda_write_room (struct usb_serial_port *port)
  560. {
  561. struct keyspan_pda_private *priv;
  562. priv = (struct keyspan_pda_private *)(port->private);
  563. /* used by n_tty.c for processing of tabs and such. Giving it our
  564.    conservative guess is probably good enough, but needs testing by
  565.    running a console through the device. */
  566. return (priv->tx_room);
  567. }
  568. static int keyspan_pda_chars_in_buffer (struct usb_serial_port *port)
  569. {
  570. struct keyspan_pda_private *priv;
  571. priv = (struct keyspan_pda_private *)(port->private);
  572. /* when throttled, return at least WAKEUP_CHARS to tell select() (via
  573.    n_tty.c:normal_poll() ) that we're not writeable. */
  574. if( port->write_urb->status == -EINPROGRESS || priv->tx_throttled )
  575. return 256;
  576. return 0;
  577. }
  578. static int keyspan_pda_open (struct usb_serial_port *port, struct file *filp)
  579. {
  580. struct usb_serial *serial = port->serial;
  581. unsigned char room;
  582. int rc = 0;
  583. struct keyspan_pda_private *priv;
  584. /* find out how much room is in the Tx ring */
  585. rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  586.      6, /* write_room */
  587.      USB_TYPE_VENDOR | USB_RECIP_INTERFACE
  588.      | USB_DIR_IN,
  589.      0, /* value */
  590.      0, /* index */
  591.      &room,
  592.      1,
  593.      2*HZ);
  594. if (rc < 0) {
  595. dbg("%s - roomquery failed", __FUNCTION__);
  596. goto error;
  597. }
  598. if (rc == 0) {
  599. dbg("%s - roomquery returned 0 bytes", __FUNCTION__);
  600. rc = -EIO;
  601. goto error;
  602. }
  603. priv = (struct keyspan_pda_private *)(port->private);
  604. priv->tx_room = room;
  605. priv->tx_throttled = room ? 0 : 1;
  606. /* the normal serial device seems to always turn on DTR and RTS here,
  607.    so do the same */
  608. if (port->tty->termios->c_cflag & CBAUD)
  609. keyspan_pda_set_modem_info(serial, (1<<7) | (1<<2) );
  610. else
  611. keyspan_pda_set_modem_info(serial, 0);
  612. /*Start reading from the device*/
  613. port->interrupt_in_urb->dev = serial->dev;
  614. rc = usb_submit_urb(port->interrupt_in_urb);
  615. if (rc) {
  616. dbg("%s - usb_submit_urb(read int) failed", __FUNCTION__);
  617. goto error;
  618. }
  619. error:
  620. return rc;
  621. }
  622. static void keyspan_pda_close(struct usb_serial_port *port, struct file *filp)
  623. {
  624. struct usb_serial *serial = port->serial;
  625. if (serial->dev) {
  626. /* the normal serial device seems to always shut off DTR and RTS now */
  627. if (port->tty->termios->c_cflag & HUPCL)
  628. keyspan_pda_set_modem_info(serial, 0);
  629. /* shutdown our bulk reads and writes */
  630. usb_unlink_urb (port->write_urb);
  631. usb_unlink_urb (port->interrupt_in_urb);
  632. }
  633. }
  634. /* download the firmware to a "fake" device (pre-renumeration) */
  635. static int keyspan_pda_fake_startup (struct usb_serial *serial)
  636. {
  637. int response;
  638. const struct ezusb_hex_record *record = NULL;
  639. /* download the firmware here ... */
  640. response = ezusb_set_reset(serial, 1);
  641. #ifdef KEYSPAN
  642. if (serial->dev->descriptor.idVendor == KEYSPAN_VENDOR_ID)
  643. record = &keyspan_pda_firmware[0];
  644. #endif
  645. #ifdef XIRCOM
  646. if ((serial->dev->descriptor.idVendor == XIRCOM_VENDOR_ID) ||
  647.     (serial->dev->descriptor.idVendor == ENTREGRA_VENDOR_ID))
  648. record = &xircom_pgs_firmware[0];
  649. #endif
  650. if (record == NULL) {
  651. err("%s: unknown vendor, aborting.", __FUNCTION__);
  652. return -ENODEV;
  653. }
  654. while(record->address != 0xffff) {
  655. response = ezusb_writememory(serial, record->address,
  656.      (unsigned char *)record->data,
  657.      record->data_size, 0xa0);
  658. if (response < 0) {
  659. err("ezusb_writememory failed for Keyspan PDA "
  660.     "firmware (%d %04X %p %d)",
  661.     response, 
  662.     record->address, record->data, record->data_size);
  663. break;
  664. }
  665. record++;
  666. }
  667. /* bring device out of reset. Renumeration will occur in a moment
  668.    and the new device will bind to the real driver */
  669. response = ezusb_set_reset(serial, 0);
  670. /* we want this device to fail to have a driver assigned to it. */
  671. return (1);
  672. }
  673. static int keyspan_pda_startup (struct usb_serial *serial)
  674. {
  675. struct keyspan_pda_private *priv;
  676. /* allocate the private data structures for all ports. Well, for all
  677.    one ports. */
  678. priv = serial->port[0].private
  679. = kmalloc(sizeof(struct keyspan_pda_private), GFP_KERNEL);
  680. if (!priv)
  681. return (1); /* error */
  682. init_waitqueue_head(&serial->port[0].write_wait);
  683. INIT_LIST_HEAD(&priv->wakeup_task.list);
  684. priv->wakeup_task.sync = 0;
  685. priv->wakeup_task.routine = (void *)keyspan_pda_wakeup_write;
  686. priv->wakeup_task.data = (void *)(&serial->port[0]);
  687. INIT_LIST_HEAD(&priv->unthrottle_task.list);
  688. priv->unthrottle_task.sync = 0;
  689. priv->unthrottle_task.routine = (void *)keyspan_pda_request_unthrottle;
  690. priv->unthrottle_task.data = (void *)(serial);
  691. return (0);
  692. }
  693. static void keyspan_pda_shutdown (struct usb_serial *serial)
  694. {
  695. dbg("%s", __FUNCTION__);
  696. kfree(serial->port[0].private);
  697. }
  698. #ifdef KEYSPAN
  699. static struct usb_serial_device_type keyspan_pda_fake_device = {
  700. .owner = THIS_MODULE,
  701. .name = "Keyspan PDA - (prerenumeration)",
  702. .id_table = id_table_fake,
  703. .num_interrupt_in = NUM_DONT_CARE,
  704. .num_bulk_in = NUM_DONT_CARE,
  705. .num_bulk_out = NUM_DONT_CARE,
  706. .num_ports = 1,
  707. .startup = keyspan_pda_fake_startup,
  708. };
  709. #endif
  710. #ifdef XIRCOM
  711. static struct usb_serial_device_type xircom_pgs_fake_device = {
  712. .owner = THIS_MODULE,
  713. .name = "Xircom / Entregra PGS - (prerenumeration)",
  714. .id_table = id_table_fake_xircom,
  715. .num_interrupt_in = NUM_DONT_CARE,
  716. .num_bulk_in = NUM_DONT_CARE,
  717. .num_bulk_out = NUM_DONT_CARE,
  718. .num_ports = 1,
  719. .startup = keyspan_pda_fake_startup,
  720. };
  721. #endif
  722. static struct usb_serial_device_type keyspan_pda_device = {
  723. .owner = THIS_MODULE,
  724. .name = "Keyspan PDA",
  725. .id_table = id_table_std,
  726. .num_interrupt_in = 1,
  727. .num_bulk_in = 0,
  728. .num_bulk_out = 1,
  729. .num_ports = 1,
  730. .open = keyspan_pda_open,
  731. .close = keyspan_pda_close,
  732. .write = keyspan_pda_write,
  733. .write_room = keyspan_pda_write_room,
  734. .write_bulk_callback =  keyspan_pda_write_bulk_callback,
  735. .read_int_callback = keyspan_pda_rx_interrupt,
  736. .chars_in_buffer = keyspan_pda_chars_in_buffer,
  737. .throttle = keyspan_pda_rx_throttle,
  738. .unthrottle = keyspan_pda_rx_unthrottle,
  739. .ioctl = keyspan_pda_ioctl,
  740. .set_termios = keyspan_pda_set_termios,
  741. .break_ctl = keyspan_pda_break_ctl,
  742. .startup = keyspan_pda_startup,
  743. .shutdown = keyspan_pda_shutdown,
  744. };
  745. static int __init keyspan_pda_init (void)
  746. {
  747. usb_serial_register (&keyspan_pda_device);
  748. #ifdef KEYSPAN
  749. usb_serial_register (&keyspan_pda_fake_device);
  750. #endif
  751. #ifdef XIRCOM
  752. usb_serial_register (&xircom_pgs_fake_device);
  753. #endif
  754. info(DRIVER_DESC " " DRIVER_VERSION);
  755. return 0;
  756. }
  757. static void __exit keyspan_pda_exit (void)
  758. {
  759. usb_serial_deregister (&keyspan_pda_device);
  760. #ifdef KEYSPAN
  761. usb_serial_deregister (&keyspan_pda_fake_device);
  762. #endif
  763. #ifdef XIRCOM
  764. usb_serial_deregister (&xircom_pgs_fake_device);
  765. #endif
  766. }
  767. module_init(keyspan_pda_init);
  768. module_exit(keyspan_pda_exit);
  769. MODULE_AUTHOR( DRIVER_AUTHOR );
  770. MODULE_DESCRIPTION( DRIVER_DESC );
  771. MODULE_LICENSE("GPL");
  772. MODULE_PARM(debug, "i");
  773. MODULE_PARM_DESC(debug, "Debug enabled or not");