summaryrefslogblamecommitdiffstats
path: root/drivers/usb/serial/keyspan.c
blob: 53be824eb1bf2070e0e666ea67313de07de035c8 (plain) (tree)
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
































































































                                                                               











                             
                             

















































































































































































































































































                                                                                                      

                                                                                        













                                                                                       













                                                                            
                                                     
























































                                                                                      
                                                      








                                                                       
                                              

 
                                                     




                                  
                                                       












                                                                                      
                                                      




























































                                                                                                   
                                                       





                                 
                                                 














































                                                                                              
                                                     



                                 
                                                       












                                                                                      
                                                      



























































                                                                                   
                                                       




                                 
                                                       






















                                                                                        
                                                      





























































                                                                                      
                                                     



                                 
                                                     



















































                                                                                      
                                                       



                                 
                                                     








































































                                                                                             
                                                      

















































                                                                               
                                                       



































































































































                                                                                                      
                                               
                                  
































































































































































                                                                                                           
                                                                     





















                                                                                   





                                                  














































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                                         
                                                                            



                                                                                     






                                                 
                                                                                  



                                                                                                    












































































                                                                           
/*
  Keyspan USB to Serial Converter driver
 
  (C) Copyright (C) 2000-2001	Hugh Blemings <hugh@blemings.org>
  (C) Copyright (C) 2002	Greg Kroah-Hartman <greg@kroah.com>
   
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  See http://misc.nu/hugh/keyspan.html for more information.
  
  Code in this driver inspired by and in a number of places taken
  from Brian Warner's original Keyspan-PDA driver.

  This driver has been put together with the support of Innosys, Inc.
  and Keyspan, Inc the manufacturers of the Keyspan USB-serial products.
  Thanks Guys :)
  
  Thanks to Paulus for miscellaneous tidy ups, some largish chunks
  of much nicer and/or completely new code and (perhaps most uniquely)
  having the patience to sit down and explain why and where he'd changed
  stuff. 
  
  Tip 'o the hat to IBM (and previously Linuxcare :) for supporting 
  staff in their work on open source projects.

  Change History

    2003sep04	LPM (Keyspan) add support for new single port product USA19HS.
				Improve setup message handling for all devices.

    Wed Feb 19 22:00:00 PST 2003 (Jeffrey S. Laing <keyspan@jsl.com>)
      Merged the current (1/31/03) Keyspan code with the current (2.4.21-pre4)
      Linux source tree.  The Linux tree lacked support for the 49WLC and
      others.  The Keyspan patches didn't work with the current kernel.

    2003jan30	LPM	add support for the 49WLC and MPR

    Wed Apr 25 12:00:00 PST 2002 (Keyspan)
      Started with Hugh Blemings' code dated Jan 17, 2002.  All adapters
      now supported (including QI and QW).  Modified port open, port
      close, and send setup() logic to fix various data and endpoint
      synchronization bugs and device LED status bugs.  Changed keyspan_
      write_room() to accurately return transmit buffer availability.
      Changed forwardingLength from 1 to 16 for all adapters.

    Fri Oct 12 16:45:00 EST 2001
      Preliminary USA-19QI and USA-28 support (both test OK for me, YMMV)

    Wed Apr 25 12:00:00 PST 2002 (Keyspan)
      Started with Hugh Blemings' code dated Jan 17, 2002.  All adapters
      now supported (including QI and QW).  Modified port open, port
      close, and send setup() logic to fix various data and endpoint
      synchronization bugs and device LED status bugs.  Changed keyspan_
      write_room() to accurately return transmit buffer availability.
      Changed forwardingLength from 1 to 16 for all adapters.

    Fri Oct 12 16:45:00 EST 2001
      Preliminary USA-19QI and USA-28 support (both test OK for me, YMMV)

    Mon Oct  8 14:29:00 EST 2001 hugh
      Fixed bug that prevented mulitport devices operating correctly
      if they weren't the first unit attached.

    Sat Oct  6 12:31:21 EST 2001 hugh
      Added support for USA-28XA and -28XB, misc cleanups, break support
      for usa26 based models thanks to David Gibson.

    Thu May 31 11:56:42 PDT 2001 gkh
      switched from using spinlock to a semaphore
   
    (04/08/2001) gb
	Identify version on module load.
   
    (11/01/2000) Adam J. Richter
	usb_device_id table support.
   
    Tue Oct 10 23:15:33 EST 2000 Hugh
      Merged Paul's changes with my USA-49W mods.  Work in progress
      still...
  
    Wed Jul 19 14:00:42 EST 2000 gkh
      Added module_init and module_exit functions to handle the fact that
      this driver is a loadable module now.
 
    Tue Jul 18 16:14:52 EST 2000 Hugh
      Basic character input/output for USA-19 now mostly works,
      fixed at 9600 baud for the moment.

    Sat Jul  8 11:11:48 EST 2000 Hugh
      First public release - nothing works except the firmware upload.
      Tested on PPC and x86 architectures, seems to behave...
*/


#include <linux/kernel.h>
#include <linux/jiffies.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <asm/uaccess.h>
#include <linux/usb.h>
#include <linux/usb/serial.h>
#include "keyspan.h"

static int debug;

/*
 * Version Information
 */
#define DRIVER_VERSION "v1.1.4"
#define DRIVER_AUTHOR "Hugh Blemings <hugh@misc.nu"
#define DRIVER_DESC "Keyspan USB to Serial Converter Driver"

#define INSTAT_BUFLEN	32
#define GLOCONT_BUFLEN	64

	/* Per device and per port private data */
struct keyspan_serial_private {
	const struct keyspan_device_details	*device_details;

	struct urb	*instat_urb;
	char		instat_buf[INSTAT_BUFLEN];

	/* XXX this one probably will need a lock */
	struct urb	*glocont_urb;
	char		glocont_buf[GLOCONT_BUFLEN];
};

struct keyspan_port_private {
	/* Keep track of which input & output endpoints to use */
	int		in_flip;
	int		out_flip;

	/* Keep duplicate of device details in each port
	   structure as well - simplifies some of the
	   callback functions etc. */
	const struct keyspan_device_details	*device_details;

	/* Input endpoints and buffer for this port */
	struct urb	*in_urbs[2];
	char		in_buffer[2][64];
	/* Output endpoints and buffer for this port */
	struct urb	*out_urbs[2];
	char		out_buffer[2][64];

	/* Input ack endpoint */
	struct urb	*inack_urb;
	char		inack_buffer[1];

	/* Output control endpoint */
	struct urb	*outcont_urb;
	char		outcont_buffer[64];

	/* Settings for the port */
	int		baud;
	int		old_baud;
	unsigned int	cflag;
	unsigned int	old_cflag;
	enum		{flow_none, flow_cts, flow_xon} flow_control;
	int		rts_state;	/* Handshaking pins (outputs) */
	int		dtr_state;
	int		cts_state;	/* Handshaking pins (inputs) */
	int		dsr_state;
	int		dcd_state;
	int		ri_state;
	int		break_on;

	unsigned long	tx_start_time[2];
	int		resend_cont;	/* need to resend control packet */
};

	
/* Include Keyspan message headers.  All current Keyspan Adapters
   make use of one of four message formats which are referred
   to as USA-26, USA-28 and USA-49, USA-90 by Keyspan and within this driver. */
#include "keyspan_usa26msg.h"
#include "keyspan_usa28msg.h"
#include "keyspan_usa49msg.h"
#include "keyspan_usa90msg.h"
	

/* Functions used by new usb-serial code. */
static int __init keyspan_init (void)
{
	int retval;
	retval = usb_serial_register(&keyspan_pre_device);
	if (retval)
		goto failed_pre_device_register;
	retval = usb_serial_register(&keyspan_1port_device);
	if (retval)
		goto failed_1port_device_register;
	retval = usb_serial_register(&keyspan_2port_device);
	if (retval)
		goto failed_2port_device_register;
	retval = usb_serial_register(&keyspan_4port_device);
	if (retval)
		goto failed_4port_device_register;
	retval = usb_register(&keyspan_driver);
	if (retval) 
		goto failed_usb_register;

	info(DRIVER_VERSION ":" DRIVER_DESC);

	return 0;
failed_usb_register:
	usb_serial_deregister(&keyspan_4port_device);
failed_4port_device_register:
	usb_serial_deregister(&keyspan_2port_device);
failed_2port_device_register:
	usb_serial_deregister(&keyspan_1port_device);
failed_1port_device_register:
	usb_serial_deregister(&keyspan_pre_device);
failed_pre_device_register:
	return retval;
}

static void __exit keyspan_exit (void)
{
	usb_deregister (&keyspan_driver);
	usb_serial_deregister (&keyspan_pre_device);
	usb_serial_deregister (&keyspan_1port_device);
	usb_serial_deregister (&keyspan_2port_device);
	usb_serial_deregister (&keyspan_4port_device);
}

module_init(keyspan_init);
module_exit(keyspan_exit);

static void keyspan_rx_throttle (struct usb_serial_port *port)
{
	dbg("%s - port %d", __FUNCTION__, port->number);
}


static void keyspan_rx_unthrottle (struct usb_serial_port *port)
{
	dbg("%s - port %d", __FUNCTION__, port->number);
}


static void keyspan_break_ctl (struct usb_serial_port *port, int break_state)
{
	struct keyspan_port_private 	*p_priv;

 	dbg("%s", __FUNCTION__);

	p_priv = usb_get_serial_port_data(port);

	if (break_state == -1)
		p_priv->break_on = 1;
	else
		p_priv->break_on = 0;

	keyspan_send_setup(port, 0);
}


static void keyspan_set_termios (struct usb_serial_port *port, 
				     struct termios *old_termios)
{
	int				baud_rate, device_port;
	struct keyspan_port_private 	*p_priv;
	const struct keyspan_device_details	*d_details;
	unsigned int 			cflag;

	dbg("%s", __FUNCTION__); 

	p_priv = usb_get_serial_port_data(port);
	d_details = p_priv->device_details;
	cflag = port->tty->termios->c_cflag;
	device_port = port->number - port->serial->minor;

	/* Baud rate calculation takes baud rate as an integer
	   so other rates can be generated if desired. */
	baud_rate = tty_get_baud_rate(port->tty);
	/* If no match or invalid, don't change */		
	if (baud_rate >= 0
	    && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
				NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
		/* FIXME - more to do here to ensure rate changes cleanly */
		p_priv->baud = baud_rate;
	}

	/* set CTS/RTS handshake etc. */
	p_priv->cflag = cflag;
	p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;

	keyspan_send_setup(port, 0);
}

static int keyspan_tiocmget(struct usb_serial_port *port, struct file *file)
{
	unsigned int			value;
	struct keyspan_port_private 	*p_priv;

	p_priv = usb_get_serial_port_data(port);
	
	value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
		((p_priv->dtr_state) ? TIOCM_DTR : 0) |
		((p_priv->cts_state) ? TIOCM_CTS : 0) |
		((p_priv->dsr_state) ? TIOCM_DSR : 0) |
		((p_priv->dcd_state) ? TIOCM_CAR : 0) |
		((p_priv->ri_state) ? TIOCM_RNG : 0); 

	return value;
}

static int keyspan_tiocmset(struct usb_serial_port *port, struct file *file,
			    unsigned int set, unsigned int clear)
{
	struct keyspan_port_private 	*p_priv;

	p_priv = usb_get_serial_port_data(port);
	
	if (set & TIOCM_RTS)
		p_priv->rts_state = 1;
	if (set & TIOCM_DTR)
		p_priv->dtr_state = 1;

	if (clear & TIOCM_RTS)
		p_priv->rts_state = 0;
	if (clear & TIOCM_DTR)
		p_priv->dtr_state = 0;
	keyspan_send_setup(port, 0);
	return 0;
}

static int keyspan_ioctl(struct usb_serial_port *port, struct file *file,
			     unsigned int cmd, unsigned long arg)
{
	return -ENOIOCTLCMD;
}

	/* Write function is similar for the four protocols used
	   with only a minor change for usa90 (usa19hs) required */
static int keyspan_write(struct usb_serial_port *port, 
			 const unsigned char *buf, int count)
{
	struct keyspan_port_private 	*p_priv;
	const struct keyspan_device_details	*d_details;
	int				flip;
	int 				left, todo;
	struct urb			*this_urb;
 	int 				err, maxDataLen, dataOffset;

	p_priv = usb_get_serial_port_data(port);
	d_details = p_priv->device_details;

	if (d_details->msg_format == msg_usa90) {
   		maxDataLen = 64;
		dataOffset = 0;
	} else {
		maxDataLen = 63;
		dataOffset = 1;
	}
	
	dbg("%s - for port %d (%d chars), flip=%d",
	    __FUNCTION__, port->number, count, p_priv->out_flip);

	for (left = count; left > 0; left -= todo) {
		todo = left;
		if (todo > maxDataLen)
			todo = maxDataLen;

		flip = p_priv->out_flip;
	
		/* Check we have a valid urb/endpoint before we use it... */
		if ((this_urb = p_priv->out_urbs[flip]) == NULL) {
			/* no bulk out, so return 0 bytes written */
			dbg("%s - no output urb :(", __FUNCTION__);
			return count;
		}

		dbg("%s - endpoint %d flip %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe), flip);

		if (this_urb->status == -EINPROGRESS) {
			if (time_before(jiffies, p_priv->tx_start_time[flip] + 10 * HZ))
				break;
			usb_unlink_urb(this_urb);
			break;
		}

		/* First byte in buffer is "last flag" (except for usa19hx) - unused so
		   for now so set to zero */
		((char *)this_urb->transfer_buffer)[0] = 0;

		memcpy (this_urb->transfer_buffer + dataOffset, buf, todo);
		buf += todo;

		/* send the data out the bulk port */
		this_urb->transfer_buffer_length = todo + dataOffset;

		this_urb->dev = port->serial->dev;
		if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
			dbg("usb_submit_urb(write bulk) failed (%d)", err);
		}
		p_priv->tx_start_time[flip] = jiffies;

		/* Flip for next time if usa26 or usa28 interface
		   (not used on usa49) */
		p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
	}

	return count - left;
}

static void	usa26_indat_callback(struct urb *urb)
{
	int			i, err;
	int			endpoint;
	struct usb_serial_port	*port;
	struct tty_struct	*tty;
	unsigned char 		*data = urb->transfer_buffer;

	dbg ("%s", __FUNCTION__); 

	endpoint = usb_pipeendpoint(urb->pipe);

	if (urb->status) {
		dbg("%s - nonzero status: %x on endpoint %d.",
		    __FUNCTION__, urb->status, endpoint);
		return;
	}

	port = (struct usb_serial_port *) urb->context;
	tty = port->tty;
	if (urb->actual_length) {
		/* 0x80 bit is error flag */
		if ((data[0] & 0x80) == 0) {
			/* no errors on individual bytes, only possible overrun err*/
			if (data[0] & RXERROR_OVERRUN)
					err = TTY_OVERRUN;
			else err = 0;
			for (i = 1; i < urb->actual_length ; ++i) {
				tty_insert_flip_char(tty, data[i], err);
			}
		} else {
			/* some bytes had errors, every byte has status */
			dbg("%s - RX error!!!!", __FUNCTION__);
			for (i = 0; i + 1 < urb->actual_length; i += 2) {
				int stat = data[i], flag = 0;
				if (stat & RXERROR_OVERRUN)
					flag |= TTY_OVERRUN;
				if (stat & RXERROR_FRAMING)
					flag |= TTY_FRAME;
				if (stat & RXERROR_PARITY)
					flag |= TTY_PARITY;
				/* XXX should handle break (0x10) */
				tty_insert_flip_char(tty, data[i+1], flag);
			}
		}
		tty_flip_buffer_push(tty);
	}
				
		/* Resubmit urb so we continue receiving */
	urb->dev = port->serial->dev;
	if (port->open_count)
		if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
			dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
		}
	return;
}

 	/* Outdat handling is common for all devices */
static void	usa2x_outdat_callback(struct urb *urb)
{
	struct usb_serial_port *port;
	struct keyspan_port_private *p_priv;

	port = (struct usb_serial_port *) urb->context;
	p_priv = usb_get_serial_port_data(port);
	dbg ("%s - urb %d", __FUNCTION__, urb == p_priv->out_urbs[1]); 

	if (port->open_count)
		usb_serial_port_softint(port);
}

static void	usa26_inack_callback(struct urb *urb)
{
	dbg ("%s", __FUNCTION__); 
	
}

static void	usa26_outcont_callback(struct urb *urb)
{
	struct usb_serial_port *port;
	struct keyspan_port_private *p_priv;

	port = (struct usb_serial_port *) urb->context;
	p_priv = usb_get_serial_port_data(port);

	if (p_priv->resend_cont) {
		dbg ("%s - sending setup", __FUNCTION__); 
		keyspan_usa26_send_setup(port->serial, port, p_priv->resend_cont - 1);
	}
}

static void	usa26_instat_callback(struct urb *urb)
{
	unsigned char 				*data = urb->transfer_buffer;
	struct keyspan_usa26_portStatusMessage	*msg;
	struct usb_serial			*serial;
	struct usb_serial_port			*port;
	struct keyspan_port_private	 	*p_priv;
	int old_dcd_state, err;

	serial = (struct usb_serial *) urb->context;

	if (urb->status) {
		dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
		return;
	}
	if (urb->actual_length != 9) {
		dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
		goto exit;
	}

	msg = (struct keyspan_usa26_portStatusMessage *)data;

#if 0
	dbg("%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
	    __FUNCTION__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr, msg->ri, msg->_txOff,
	    msg->_txXoff, msg->rxEnabled, msg->controlResponse);
#endif

	/* Now do something useful with the data */


	/* Check port number from message and retrieve private data */	
	if (msg->port >= serial->num_ports) {
		dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
		goto exit;
	}
	port = serial->port[msg->port];
	p_priv = usb_get_serial_port_data(port);
	
	/* Update handshaking pin state information */
	old_dcd_state = p_priv->dcd_state;
	p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
	p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
	p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
	p_priv->ri_state = ((msg->ri) ? 1 : 0);

	if (port->tty && !C_CLOCAL(port->tty)
	    && old_dcd_state != p_priv->dcd_state) {
		if (old_dcd_state)
			tty_hangup(port->tty);
		/*  else */
		/*	wake_up_interruptible(&p_priv->open_wait); */
	}
	
	/* Resubmit urb so we continue receiving */
	urb->dev = serial->dev;
	if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
		dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
	}
exit: ;
}

static void	usa26_glocont_callback(struct urb *urb)
{
	dbg ("%s", __FUNCTION__);
	
}


static void usa28_indat_callback(struct urb *urb)
{
	int                     i, err;
	struct usb_serial_port  *port;
	struct tty_struct       *tty;
	unsigned char           *data;
	struct keyspan_port_private             *p_priv;

	dbg ("%s", __FUNCTION__);

	port = (struct usb_serial_port *) urb->context;
	p_priv = usb_get_serial_port_data(port);
	data = urb->transfer_buffer;

	if (urb != p_priv->in_urbs[p_priv->in_flip])
		return;

	do {
		if (urb->status) {
			dbg("%s - nonzero status: %x on endpoint %d.",
			    __FUNCTION__, urb->status, usb_pipeendpoint(urb->pipe));
			return;
		}

		port = (struct usb_serial_port *) urb->context;
		p_priv = usb_get_serial_port_data(port);
		data = urb->transfer_buffer;

		tty = port->tty;
		if (urb->actual_length) {
			for (i = 0; i < urb->actual_length ; ++i) {
				tty_insert_flip_char(tty, data[i], 0);
			}
			tty_flip_buffer_push(tty);
		}

		/* Resubmit urb so we continue receiving */
		urb->dev = port->serial->dev;
		if (port->open_count)
			if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
				dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
			}
		p_priv->in_flip ^= 1;

		urb = p_priv->in_urbs[p_priv->in_flip];
	} while (urb->status != -EINPROGRESS);
}

static void	usa28_inack_callback(struct urb *urb)
{
	dbg ("%s", __FUNCTION__);
}

static void	usa28_outcont_callback(struct urb *urb)
{
	struct usb_serial_port *port;
	struct keyspan_port_private *p_priv;

	port = (struct usb_serial_port *) urb->context;
	p_priv = usb_get_serial_port_data(port);

	if (p_priv->resend_cont) {
		dbg ("%s - sending setup", __FUNCTION__);
		keyspan_usa28_send_setup(port->serial, port, p_priv->resend_cont - 1);
	}
}

static void	usa28_instat_callback(struct urb *urb)
{
	int					err;
	unsigned char 				*data = urb->transfer_buffer;
	struct keyspan_usa28_portStatusMessage	*msg;
	struct usb_serial			*serial;
	struct usb_serial_port			*port;
	struct keyspan_port_private	 	*p_priv;
	int old_dcd_state;

	serial = (struct usb_serial *) urb->context;

	if (urb->status) {
		dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
		return;
	}

	if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
		dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
		goto exit;
	}

	/*dbg("%s %x %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__
	    data[0], data[1], data[2], data[3], data[4], data[5],
	    data[6], data[7], data[8], data[9], data[10], data[11]);*/
	
		/* Now do something useful with the data */
	msg = (struct keyspan_usa28_portStatusMessage *)data;


		/* Check port number from message and retrieve private data */	
	if (msg->port >= serial->num_ports) {
		dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
		goto exit;
	}
	port = serial->port[msg->port];
	p_priv = usb_get_serial_port_data(port);
	
	/* Update handshaking pin state information */
	old_dcd_state = p_priv->dcd_state;
	p_priv->cts_state = ((msg->cts) ? 1 : 0);
	p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
	p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
	p_priv->ri_state = ((msg->ri) ? 1 : 0);

	if (port->tty && !C_CLOCAL(port->tty)
	    && old_dcd_state != p_priv->dcd_state) {
		if (old_dcd_state)
			tty_hangup(port->tty);
		/*  else */
		/*	wake_up_interruptible(&p_priv->open_wait); */
	}

		/* Resubmit urb so we continue receiving */
	urb->dev = serial->dev;
	if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
		dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
	}
exit: ;
}

static void	usa28_glocont_callback(struct urb *urb)
{
	dbg ("%s", __FUNCTION__);
}


static void	usa49_glocont_callback(struct urb *urb)
{
	struct usb_serial *serial;
	struct usb_serial_port *port;
	struct keyspan_port_private *p_priv;
	int i;

	dbg ("%s", __FUNCTION__);

	serial = (struct usb_serial *) urb->context;
	for (i = 0; i < serial->num_ports; ++i) {
		port = serial->port[i];
		p_priv = usb_get_serial_port_data(port);

		if (p_priv->resend_cont) {
			dbg ("%s - sending setup", __FUNCTION__); 
			keyspan_usa49_send_setup(serial, port, p_priv->resend_cont - 1);
			break;
		}
	}
}

	/* This is actually called glostat in the Keyspan
	   doco */
static void	usa49_instat_callback(struct urb *urb)
{
	int					err;
	unsigned char 				*data = urb->transfer_buffer;
	struct keyspan_usa49_portStatusMessage	*msg;
	struct usb_serial			*serial;
	struct usb_serial_port			*port;
	struct keyspan_port_private	 	*p_priv;
	int old_dcd_state;

	dbg ("%s", __FUNCTION__);

	serial = (struct usb_serial *) urb->context;

	if (urb->status) {
		dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
		return;
	}

	if (urb->actual_length != sizeof(struct keyspan_usa49_portStatusMessage)) {
		dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
		goto exit;
	}

	/*dbg(" %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__, 
	    data[0], data[1], data[2], data[3], data[4], data[5],
	    data[6], data[7], data[8], data[9], data[10]);*/
	
		/* Now do something useful with the data */
	msg = (struct keyspan_usa49_portStatusMessage *)data;

		/* Check port number from message and retrieve private data */	
	if (msg->portNumber >= serial->num_ports) {
		dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->portNumber);
		goto exit;
	}
	port = serial->port[msg->portNumber];
	p_priv = usb_get_serial_port_data(port);
	
	/* Update handshaking pin state information */
	old_dcd_state = p_priv->dcd_state;
	p_priv->cts_state = ((msg->cts) ? 1 : 0);
	p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
	p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
	p_priv->ri_state = ((msg->ri) ? 1 : 0);

	if (port->tty && !C_CLOCAL(port->tty)
	    && old_dcd_state != p_priv->dcd_state) {
		if (old_dcd_state)
			tty_hangup(port->tty);
		/*  else */
		/*	wake_up_interruptible(&p_priv->open_wait); */
	}

		/* Resubmit urb so we continue receiving */
	urb->dev = serial->dev;

	if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
		dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
	}
exit:	;
}

static void	usa49_inack_callback(struct urb *urb)
{
	dbg ("%s", __FUNCTION__);
}

static void	usa49_indat_callback(struct urb *urb)
{
	int			i, err;
	int			endpoint;
	struct usb_serial_port	*port;
	struct tty_struct	*tty;
	unsigned char 		*data = urb->transfer_buffer;

	dbg ("%s", __FUNCTION__);

	endpoint = usb_pipeendpoint(urb->pipe);

	if (urb->status) {
		dbg("%s - nonzero status: %x on endpoint %d.", __FUNCTION__,
		    urb->status, endpoint);
		return;
	}

	port = (struct usb_serial_port *) urb->context;
	tty = port->tty;
	if (urb->actual_length) {
		/* 0x80 bit is error flag */
		if ((data[0] & 0x80) == 0) {
			/* no error on any byte */
			for (i = 1; i < urb->actual_length ; ++i) {
				tty_insert_flip_char(tty, data[i], 0);
			}
		} else {
			/* some bytes had errors, every byte has status */
			for (i = 0; i + 1 < urb->actual_length; i += 2) {
				int stat = data[i], flag = 0;
				if (stat & RXERROR_OVERRUN)
					flag |= TTY_OVERRUN;
				if (stat & RXERROR_FRAMING)
					flag |= TTY_FRAME;
				if (stat & RXERROR_PARITY)
					flag |= TTY_PARITY;
				/* XXX should handle break (0x10) */
				tty_insert_flip_char(tty, data[i+1], flag);
			}
		}
		tty_flip_buffer_push(tty);
	}
				
		/* Resubmit urb so we continue receiving */
	urb->dev = port->serial->dev;
	if (port->open_count)
		if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
			dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
		}
}

/* not used, usa-49 doesn't have per-port control endpoints */
static void	usa49_outcont_callback(struct urb *urb)
{
	dbg ("%s", __FUNCTION__);
}

static void	usa90_indat_callback(struct urb *urb)
{
	int			i, err;
	int			endpoint;
	struct usb_serial_port	*port;
	struct keyspan_port_private	 	*p_priv;
	struct tty_struct	*tty;
	unsigned char 		*data = urb->transfer_buffer;

	dbg ("%s", __FUNCTION__); 

	endpoint = usb_pipeendpoint(urb->pipe);


	if (urb->status) {
		dbg("%s - nonzero status: %x on endpoint %d.",
		    __FUNCTION__, urb->status, endpoint);
		return;
	}

	port = (struct usb_serial_port *) urb->context;
	p_priv = usb_get_serial_port_data(port);

	tty = port->tty;
	if (urb->actual_length) {
	
		/* if current mode is DMA, looks like usa28 format
	   		otherwise looks like usa26 data format */

		if (p_priv->baud > 57600) {
			for (i = 0; i < urb->actual_length ; ++i) 
				tty_insert_flip_char(tty, data[i], 0);
		}
		else {
			
			/* 0x80 bit is error flag */
			if ((data[0] & 0x80) == 0) {
				/* no errors on individual bytes, only possible overrun err*/
				if (data[0] & RXERROR_OVERRUN)
						err = TTY_OVERRUN;
				else err = 0;
				for (i = 1; i < urb->actual_length ; ++i) 
					tty_insert_flip_char(tty, data[i], err);
			
			} 
			else {
			/* some bytes had errors, every byte has status */
				dbg("%s - RX error!!!!", __FUNCTION__);
				for (i = 0; i + 1 < urb->actual_length; i += 2) {
					int stat = data[i], flag = 0;
					if (stat & RXERROR_OVERRUN)
						flag |= TTY_OVERRUN;
					if (stat & RXERROR_FRAMING)
						flag |= TTY_FRAME;
					if (stat & RXERROR_PARITY)
						flag |= TTY_PARITY;
					/* XXX should handle break (0x10) */
					tty_insert_flip_char(tty, data[i+1], flag);
				}
			}
		}
		tty_flip_buffer_push(tty);
	}
				
	/* Resubmit urb so we continue receiving */
	urb->dev = port->serial->dev;
	if (port->open_count)
		if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
			dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
		}
	return;
}


static void	usa90_instat_callback(struct urb *urb)
{
	unsigned char 				*data = urb->transfer_buffer;
	struct keyspan_usa90_portStatusMessage	*msg;
	struct usb_serial			*serial;
	struct usb_serial_port			*port;
	struct keyspan_port_private	 	*p_priv;
	int old_dcd_state, err;

	serial = (struct usb_serial *) urb->context;

	if (urb->status) {
		dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
		return;
	}
	if (urb->actual_length < 14) {
		dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
		goto exit;
	}

	msg = (struct keyspan_usa90_portStatusMessage *)data;

	/* Now do something useful with the data */

	port = serial->port[0];
	p_priv = usb_get_serial_port_data(port);
	
	/* Update handshaking pin state information */
	old_dcd_state = p_priv->dcd_state;
	p_priv->cts_state = ((msg->cts) ? 1 : 0);
	p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
	p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
	p_priv->ri_state = ((msg->ri) ? 1 : 0);

	if (port->tty && !C_CLOCAL(port->tty)
	    && old_dcd_state != p_priv->dcd_state) {
		if (old_dcd_state)
			tty_hangup(port->tty);
		/*  else */
		/*	wake_up_interruptible(&p_priv->open_wait); */
	}
	
	/* Resubmit urb so we continue receiving */
	urb->dev = serial->dev;
	if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
		dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
	}
exit:
	;
}

static void	usa90_outcont_callback(struct urb *urb)
{
	struct usb_serial_port *port;
	struct keyspan_port_private *p_priv;

	port = (struct usb_serial_port *) urb->context;
	p_priv = usb_get_serial_port_data(port);

	if (p_priv->resend_cont) {
		dbg ("%s - sending setup", __FUNCTION__); 
		keyspan_usa90_send_setup(port->serial, port, p_priv->resend_cont - 1);
	}
}

static int keyspan_write_room (struct usb_serial_port *port)
{
	struct keyspan_port_private	*p_priv;
	const struct keyspan_device_details	*d_details;
	int				flip;
	int				data_len;
	struct urb			*this_urb;

	dbg("%s", __FUNCTION__);
	p_priv = usb_get_serial_port_data(port);
	d_details = p_priv->device_details;

	if (d_details->msg_format == msg_usa90)
   		data_len = 64;
	else
		data_len = 63;

	flip = p_priv->out_flip;

	/* Check both endpoints to see if any are available. */
	if ((this_urb = p_priv->out_urbs[flip]) != NULL) {
		if (this_urb->status != -EINPROGRESS)
			return (data_len);
		flip = (flip + 1) & d_details->outdat_endp_flip;        
		if ((this_urb = p_priv->out_urbs[flip]) != NULL) 
			if (this_urb->status != -EINPROGRESS)
				return (data_len);
	}
	return (0);
}


static int keyspan_chars_in_buffer (struct usb_serial_port *port)
{
	return (0);
}


static int keyspan_open (struct usb_serial_port *port, struct file *filp)
{
	struct keyspan_port_private 	*p_priv;
	struct keyspan_serial_private 	*s_priv;
	struct usb_serial 		*serial = port->serial;
	const struct keyspan_device_details	*d_details;
	int				i, err;
	int				baud_rate, device_port;
	struct urb			*urb;
	unsigned int			cflag;

	s_priv = usb_get_serial_data(serial);
	p_priv = usb_get_serial_port_data(port);
	d_details = p_priv->device_details;
	
	dbg("%s - port%d.", __FUNCTION__, port->number); 

	/* Set some sane defaults */
	p_priv->rts_state = 1;
	p_priv->dtr_state = 1;
	p_priv->baud = 9600;

	/* force baud and lcr to be set on open */
	p_priv->old_baud = 0;
	p_priv->old_cflag = 0;

	p_priv->out_flip = 0;
	p_priv->in_flip = 0;

	/* Reset low level data toggle and start reading from endpoints */
	for (i = 0; i < 2; i++) {
		if ((urb = p_priv->in_urbs[i]) == NULL)
			continue;
		urb->dev = serial->dev;

		/* make sure endpoint data toggle is synchronized with the device */
		
		usb_clear_halt(urb->dev, urb->pipe);

		if ((err = usb_submit_urb(urb, GFP_KERNEL)) != 0) {
			dbg("%s - submit urb %d failed (%d)", __FUNCTION__, i, err);
		}
	}

	/* Reset low level data toggle on out endpoints */
	for (i = 0; i < 2; i++) {
		if ((urb = p_priv->out_urbs[i]) == NULL)
			continue;
		urb->dev = serial->dev;
		/* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), 0); */
	}

	/* get the terminal config for the setup message now so we don't 
	 * need to send 2 of them */

	cflag = port->tty->termios->c_cflag;
	device_port = port->number - port->serial->minor;

	/* Baud rate calculation takes baud rate as an integer
	   so other rates can be generated if desired. */
	baud_rate = tty_get_baud_rate(port->tty);
	/* If no match or invalid, leave as default */		
	if (baud_rate >= 0
	    && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
				NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
		p_priv->baud = baud_rate;
	}

	/* set CTS/RTS handshake etc. */
	p_priv->cflag = cflag;
	p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;

	keyspan_send_setup(port, 1);
	//mdelay(100);
	//keyspan_set_termios(port, NULL);

	return (0);
}

static inline void stop_urb(struct urb *urb)
{
	if (urb && urb->status == -EINPROGRESS)
		usb_kill_urb(urb);
}

static void keyspan_close(struct usb_serial_port *port, struct file *filp)
{
	int			i;
	struct usb_serial	*serial = port->serial;
	struct keyspan_serial_private 	*s_priv;
	struct keyspan_port_private 	*p_priv;

	dbg("%s", __FUNCTION__);
	s_priv = usb_get_serial_data(serial);
	p_priv = usb_get_serial_port_data(port);
	
	p_priv->rts_state = 0;
	p_priv->dtr_state = 0;
	
	if (serial->dev) {
		keyspan_send_setup(port, 2);
		/* pilot-xfer seems to work best with this delay */
		mdelay(100);
		// keyspan_set_termios(port, NULL);
	}

	/*while (p_priv->outcont_urb->status == -EINPROGRESS) {
		dbg("%s - urb in progress", __FUNCTION__);
	}*/

	p_priv->out_flip = 0;
	p_priv->in_flip = 0;

	if (serial->dev) {
		/* Stop reading/writing urbs */
		stop_urb(p_priv->inack_urb);
		/* stop_urb(p_priv->outcont_urb); */
		for (i = 0; i < 2; i++) {
			stop_urb(p_priv->in_urbs[i]);
			stop_urb(p_priv->out_urbs[i]);
		}
	}
	port->tty = NULL;
}


	/* download the firmware to a pre-renumeration device */
static int keyspan_fake_startup (struct usb_serial *serial)
{
	int 				response;
	const struct ezusb_hex_record 	*record;
	char				*fw_name;

	dbg("Keyspan startup version %04x product %04x",
	    le16_to_cpu(serial->dev->descriptor.bcdDevice),
	    le16_to_cpu(serial->dev->descriptor.idProduct));
	
	if ((le16_to_cpu(serial->dev->descriptor.bcdDevice) & 0x8000) != 0x8000) {
		dbg("Firmware already loaded.  Quitting.");
		return(1);
	}

		/* Select firmware image on the basis of idProduct */
	switch (le16_to_cpu(serial->dev->descriptor.idProduct)) {
	case keyspan_usa28_pre_product_id:
		record = &keyspan_usa28_firmware[0];
		fw_name = "USA28";
		break;

	case keyspan_usa28x_pre_product_id:
		record = &keyspan_usa28x_firmware[0];
		fw_name = "USA28X";
		break;

	case keyspan_usa28xa_pre_product_id:
		record = &keyspan_usa28xa_firmware[0];
		fw_name = "USA28XA";
		break;

	case keyspan_usa28xb_pre_product_id:
		record = &keyspan_usa28xb_firmware[0];
		fw_name = "USA28XB";
		break;

	case keyspan_usa19_pre_product_id:
		record = &keyspan_usa19_firmware[0];
		fw_name = "USA19";
		break;
			     
	case keyspan_usa19qi_pre_product_id:
		record = &keyspan_usa19qi_firmware[0];
		fw_name = "USA19QI";
		break;
			     
	case keyspan_mpr_pre_product_id:
		record = &keyspan_mpr_firmware[0];
		fw_name = "MPR";
		break;

	case keyspan_usa19qw_pre_product_id:
		record = &keyspan_usa19qw_firmware[0];
		fw_name = "USA19QI";
		break;
			     
	case keyspan_usa18x_pre_product_id:
		record = &keyspan_usa18x_firmware[0];
		fw_name = "USA18X";
		break;
			     
	case keyspan_usa19w_pre_product_id:
		record = &keyspan_usa19w_firmware[0];
		fw_name = "USA19W";
		break;
		
	case keyspan_usa49w_pre_product_id:
		record = &keyspan_usa49w_firmware[0];
		fw_name = "USA49W";
		break;

	case keyspan_usa49wlc_pre_product_id:
		record = &keyspan_usa49wlc_firmware[0];
		fw_name = "USA49WLC";
		break;

	default:
		record = NULL;
		fw_name = "Unknown";
		break;
	}

	if (record == NULL) {
		dev_err(&serial->dev->dev, "Required keyspan firmware image (%s) unavailable.\n", fw_name);
		return(1);
	}

	dbg("Uploading Keyspan %s firmware.", fw_name);

		/* download the firmware image */
	response = ezusb_set_reset(serial, 1);

	while(record->address != 0xffff) {
		response = ezusb_writememory(serial, record->address,
					     (unsigned char *)record->data,
					     record->data_size, 0xa0);
		if (response < 0) {
			dev_err(&serial->dev->dev, "ezusb_writememory failed for Keyspan"
				"firmware (%d %04X %p %d)\n",
				response, 
				record->address, record->data, record->data_size);
			break;
		}
		record++;
	}
		/* bring device out of reset. Renumeration will occur in a
		   moment and the new device will bind to the real driver */
	response = ezusb_set_reset(serial, 0);

	/* we don't want this device to have a driver assigned to it. */
	return (1);
}

/* Helper functions used by keyspan_setup_urbs */
static struct urb *keyspan_setup_urb (struct usb_serial *serial, int endpoint,
				      int dir, void *ctx, char *buf, int len,
				      void (*callback)(struct urb *))
{
	struct urb *urb;

	if (endpoint == -1)
		return NULL;		/* endpoint not needed */

	dbg ("%s - alloc for endpoint %d.", __FUNCTION__, endpoint);
	urb = usb_alloc_urb(0, GFP_KERNEL);		/* No ISO */
	if (urb == NULL) {
		dbg ("%s - alloc for endpoint %d failed.", __FUNCTION__, endpoint);
		return NULL;
	}

		/* Fill URB using supplied data. */
	usb_fill_bulk_urb(urb, serial->dev,
		      usb_sndbulkpipe(serial->dev, endpoint) | dir,
		      buf, len, callback, ctx);

	return urb;
}

static struct callbacks {
	void	(*instat_callback)(struct urb *);
	void	(*glocont_callback)(struct urb *);
	void	(*indat_callback)(struct urb *);
	void	(*outdat_callback)(struct urb *);
	void	(*inack_callback)(struct urb *);
	void	(*outcont_callback)(struct urb *);
} keyspan_callbacks[] = {
	{
		/* msg_usa26 callbacks */
		.instat_callback =	usa26_instat_callback,
		.glocont_callback =	usa26_glocont_callback,
		.indat_callback =	usa26_indat_callback,
		.outdat_callback =	usa2x_outdat_callback,
		.inack_callback =	usa26_inack_callback,
		.outcont_callback =	usa26_outcont_callback,
	}, {
		/* msg_usa28 callbacks */
		.instat_callback =	usa28_instat_callback,
		.glocont_callback =	usa28_glocont_callback,
		.indat_callback =	usa28_indat_callback,
		.outdat_callback =	usa2x_outdat_callback,
		.inack_callback =	usa28_inack_callback,
		.outcont_callback =	usa28_outcont_callback,
	}, {
		/* msg_usa49 callbacks */
		.instat_callback =	usa49_instat_callback,
		.glocont_callback =	usa49_glocont_callback,
		.indat_callback =	usa49_indat_callback,
		.outdat_callback =	usa2x_outdat_callback,
		.inack_callback =	usa49_inack_callback,
		.outcont_callback =	usa49_outcont_callback,
	}, {
		/* msg_usa90 callbacks */
		.instat_callback =	usa90_instat_callback,
		.glocont_callback =	usa28_glocont_callback,		
		.indat_callback =	usa90_indat_callback,
		.outdat_callback =	usa2x_outdat_callback,
		.inack_callback =	usa28_inack_callback,
		.outcont_callback =	usa90_outcont_callback,
	}
};

	/* Generic setup urbs function that uses
	   data in device_details */
static void keyspan_setup_urbs(struct usb_serial *serial)
{
	int				i, j;
	struct keyspan_serial_private 	*s_priv;
	const struct keyspan_device_details	*d_details;
	struct usb_serial_port		*port;
	struct keyspan_port_private	*p_priv;
	struct callbacks		*cback;
	int				endp;

	dbg ("%s", __FUNCTION__);

	s_priv = usb_get_serial_data(serial);
	d_details = s_priv->device_details;

		/* Setup values for the various callback routines */
	cback = &keyspan_callbacks[d_details->msg_format];

		/* Allocate and set up urbs for each one that is in use, 
		   starting with instat endpoints */
	s_priv->instat_urb = keyspan_setup_urb
		(serial, d_details->instat_endpoint, USB_DIR_IN,
		 serial, s_priv->instat_buf, INSTAT_BUFLEN,
		 cback->instat_callback);

	s_priv->glocont_urb = keyspan_setup_urb
		(serial, d_details->glocont_endpoint, USB_DIR_OUT,
		 serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
		 cback->glocont_callback);

		/* Setup endpoints for each port specific thing */
	for (i = 0; i < d_details->num_ports; i ++) {
		port = serial->port[i];
		p_priv = usb_get_serial_port_data(port);

		/* Do indat endpoints first, once for each flip */
		endp = d_details->indat_endpoints[i];
		for (j = 0; j <= d_details->indat_endp_flip; ++j, ++endp) {
			p_priv->in_urbs[j] = keyspan_setup_urb
				(serial, endp, USB_DIR_IN, port,
				 p_priv->in_buffer[j], 64,
				 cback->indat_callback);
		}
		for (; j < 2; ++j)
			p_priv->in_urbs[j] = NULL;

		/* outdat endpoints also have flip */
		endp = d_details->outdat_endpoints[i];
		for (j = 0; j <= d_details->outdat_endp_flip; ++j, ++endp) {
			p_priv->out_urbs[j] = keyspan_setup_urb
				(serial, endp, USB_DIR_OUT, port,
				 p_priv->out_buffer[j], 64,
				 cback->outdat_callback);
		}
		for (; j < 2; ++j)
			p_priv->out_urbs[j] = NULL;

		/* inack endpoint */
		p_priv->inack_urb = keyspan_setup_urb
			(serial, d_details->inack_endpoints[i], USB_DIR_IN,
			 port, p_priv->inack_buffer, 1, cback->inack_callback);

		/* outcont endpoint */
		p_priv->outcont_urb = keyspan_setup_urb
			(serial, d_details->outcont_endpoints[i], USB_DIR_OUT,
			 port, p_priv->outcont_buffer, 64,
			 cback->outcont_callback);
	}	

}

/* usa19 function doesn't require prescaler */
static int keyspan_usa19_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
				   u8 *rate_low, u8 *prescaler, int portnum)
{
	u32 	b16,	/* baud rate times 16 (actual rate used internally) */
		div,	/* divisor */	
		cnt;	/* inverse of divisor (programmed into 8051) */
		
	dbg ("%s - %d.", __FUNCTION__, baud_rate);

		/* prevent divide by zero...  */
	if( (b16 = (baud_rate * 16L)) == 0) {
		return (KEYSPAN_INVALID_BAUD_RATE);
	}

		/* Any "standard" rate over 57k6 is marginal on the USA-19
		   as we run out of divisor resolution. */
	if (baud_rate > 57600) {
		return (KEYSPAN_INVALID_BAUD_RATE);
	}

		/* calculate the divisor and the counter (its inverse) */
	if( (div = (baudclk / b16)) == 0) {
		return (KEYSPAN_INVALID_BAUD_RATE);
	}
	else {
		cnt = 0 - div;
	}

	if(div > 0xffff) {
		return (KEYSPAN_INVALID_BAUD_RATE);
	}

		/* return the counter values if non-null */
	if (rate_low) {
		*rate_low = (u8) (cnt & 0xff);
	}
	if (rate_hi) {
		*rate_hi = (u8) ((cnt >> 8) & 0xff);
	}
	if (rate_low && rate_hi) {
		dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
	}
	
	return (KEYSPAN_BAUD_RATE_OK);
}

/* usa19hs function doesn't require prescaler */
static int keyspan_usa19hs_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
				   u8 *rate_low, u8 *prescaler, int portnum)
{
	u32 	b16,	/* baud rate times 16 (actual rate used internally) */
			div;	/* divisor */	
		
	dbg ("%s - %d.", __FUNCTION__, baud_rate);

		/* prevent divide by zero...  */
	if( (b16 = (baud_rate * 16L)) == 0) 
		return (KEYSPAN_INVALID_BAUD_RATE);
	


		/* calculate the divisor */
	if( (div = (baudclk / b16)) == 0) 
		return (KEYSPAN_INVALID_BAUD_RATE);

	if(div > 0xffff) 
		return (KEYSPAN_INVALID_BAUD_RATE);

		/* return the counter values if non-null */
	if (rate_low) 
		*rate_low = (u8) (div & 0xff);
	
	if (rate_hi) 
		*rate_hi = (u8) ((div >> 8) & 0xff);
	
	if (rate_low && rate_hi) 
		dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
	
	return (KEYSPAN_BAUD_RATE_OK);
}

static int keyspan_usa19w_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
				    u8 *rate_low, u8 *prescaler, int portnum)
{
	u32 	b16,	/* baud rate times 16 (actual rate used internally) */
		clk,	/* clock with 13/8 prescaler */
		div,	/* divisor using 13/8 prescaler */	
		res,	/* resulting baud rate using 13/8 prescaler */
		diff,	/* error using 13/8 prescaler */
		smallest_diff;
	u8	best_prescaler;
	int	i;

	dbg ("%s - %d.", __FUNCTION__, baud_rate);

		/* prevent divide by zero */
	if( (b16 = baud_rate * 16L) == 0) {
		return (KEYSPAN_INVALID_BAUD_RATE);
	}

		/* Calculate prescaler by trying them all and looking
		   for best fit */
		
		/* start with largest possible difference */
	smallest_diff = 0xffffffff;

		/* 0 is an invalid prescaler, used as a flag */
	best_prescaler = 0;

	for(i = 8; i <= 0xff; ++i) {
		clk = (baudclk * 8) / (u32) i;
		
		if( (div = clk / b16) == 0) {
			continue;
		}

		res = clk / div;
		diff= (res > b16) ? (res-b16) : (b16-res);

		if(diff < smallest_diff) {
			best_prescaler = i;
			smallest_diff = diff;
		}
	}

	if(best_prescaler == 0) {
		return (KEYSPAN_INVALID_BAUD_RATE);
	}

	clk = (baudclk * 8) / (u32) best_prescaler;
	div = clk / b16;

		/* return the divisor and prescaler if non-null */
	if (rate_low) {
		*rate_low = (u8) (div & 0xff);
	}
	if (rate_hi) {
		*rate_hi = (u8) ((div >> 8) & 0xff);
	}
	if (prescaler) {
		*prescaler = best_prescaler;
		/*  dbg("%s - %d %d", __FUNCTION__, *prescaler, div); */
	}
	return (KEYSPAN_BAUD_RATE_OK);
}

	/* USA-28 supports different maximum baud rates on each port */
static int keyspan_usa28_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
				    u8 *rate_low, u8 *prescaler, int portnum)
{
	u32 	b16,	/* baud rate times 16 (actual rate used internally) */
		div,	/* divisor */	
		cnt;	/* inverse of divisor (programmed into 8051) */

	dbg ("%s - %d.", __FUNCTION__, baud_rate);

		/* prevent divide by zero */
	if ((b16 = baud_rate * 16L) == 0)
		return (KEYSPAN_INVALID_BAUD_RATE);

		/* calculate the divisor and the counter (its inverse) */
	if ((div = (KEYSPAN_USA28_BAUDCLK / b16)) == 0) {
		return (KEYSPAN_INVALID_BAUD_RATE);
	}
	else {
		cnt = 0 - div;
	}

		/* check for out of range, based on portnum, 
		   and return result */
	if(portnum == 0) {
		if(div > 0xffff)
			return (KEYSPAN_INVALID_BAUD_RATE);
	}
	else {
		if(portnum == 1) {
			if(div > 0xff) {
				return (KEYSPAN_INVALID_BAUD_RATE);
			}
		}
		else {
			return (KEYSPAN_INVALID_BAUD_RATE);
		}
	}

		/* return the counter values if not NULL
		   (port 1 will ignore retHi) */
	if (rate_low) {
		*rate_low = (u8) (cnt & 0xff);
	}
	if (rate_hi) {
		*rate_hi = (u8) ((cnt >> 8) & 0xff);
	}
	dbg ("%s - %d OK.", __FUNCTION__, baud_rate);
	return (KEYSPAN_BAUD_RATE_OK);
}

static int keyspan_usa26_send_setup(struct usb_serial *serial,
				    struct usb_serial_port *port,
				    int reset_port)
{
	struct keyspan_usa26_portControlMessage	msg;		
	struct keyspan_serial_private 		*s_priv;
	struct keyspan_port_private 		*p_priv;
	const struct keyspan_device_details	*d_details;
	int 					outcont_urb;
	struct urb				*this_urb;
	int 					device_port, err;

	dbg ("%s reset=%d", __FUNCTION__, reset_port); 

	s_priv = usb_get_serial_data(serial);
	p_priv = usb_get_serial_port_data(port);
	d_details = s_priv->device_details;
	device_port = port->number - port->serial->minor;

	outcont_urb = d_details->outcont_endpoints[port->number];
	this_urb = p_priv->outcont_urb;

	dbg("%s - endpoint %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe));

		/* Make sure we have an urb then send the message */
	if (this_urb == NULL) {
		dbg("%s - oops no urb.", __FUNCTION__);
		return -1;
	}

	/* Save reset port val for resend.
	Don't overwrite resend for close condition. */
	if (p_priv->resend_cont != 3)
		p_priv->resend_cont = reset_port + 1;
	if (this_urb->status == -EINPROGRESS) {
		/*  dbg ("%s - already writing", __FUNCTION__); */
		mdelay(5);
		return(-1);
	}

	memset(&msg, 0, sizeof (struct keyspan_usa26_portControlMessage));
	
		/* Only set baud rate if it's changed */	
	if (p_priv->old_baud != p_priv->baud) {
		p_priv->old_baud = p_priv->baud;
		msg.setClocking = 0xff;
		if (d_details->calculate_baud_rate
		    (p_priv->baud, d_details->baudclk, &msg.baudHi,
		     &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
			dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
			    p_priv->baud);
			msg.baudLo = 0;
			msg.baudHi = 125;	/* Values for 9600 baud */
			msg.prescaler = 10;
		}
		msg.setPrescaler = 0xff;
	}

	msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
	switch (p_priv->cflag & CSIZE) {
	case CS5:
		msg.lcr |= USA_DATABITS_5;
		break;
	case CS6:
		msg.lcr |= USA_DATABITS_6;
		break;
	case CS7:
		msg.lcr |= USA_DATABITS_7;
		break;
	case CS8:
		msg.lcr |= USA_DATABITS_8;
		break;
	}
	if (p_priv->cflag & PARENB) {
		/* note USA_PARITY_NONE == 0 */
		msg.lcr |= (p_priv->cflag & PARODD)?
			USA_PARITY_ODD: USA_PARITY_EVEN;
	}
	msg.setLcr = 0xff;

	msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
	msg.xonFlowControl = 0;
	msg.setFlowControl = 0xff;
	msg.forwardingLength = 16;
	msg.xonChar = 17;
	msg.xoffChar = 19;

	/* Opening port */
	if (reset_port == 1) {
		msg._txOn = 1;
		msg._txOff = 0;
		msg.txFlush = 0;
		msg.txBreak = 0;
		msg.rxOn = 1;
		msg.rxOff = 0;
		msg.rxFlush = 1;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0xff;
	}

	/* Closing port */
	else if (reset_port == 2) {
		msg._txOn = 0;
		msg._txOff = 1;
		msg.txFlush = 0;
		msg.txBreak = 0;
		msg.rxOn = 0;
		msg.rxOff = 1;
		msg.rxFlush = 1;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0;
	}

	/* Sending intermediate configs */
	else {
		msg._txOn = (! p_priv->break_on);
		msg._txOff = 0;
		msg.txFlush = 0;
		msg.txBreak = (p_priv->break_on);
		msg.rxOn = 0;
		msg.rxOff = 0;
		msg.rxFlush = 0;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0x0;
	}

		/* Do handshaking outputs */	
	msg.setTxTriState_setRts = 0xff;
	msg.txTriState_rts = p_priv->rts_state;

	msg.setHskoa_setDtr = 0xff;
	msg.hskoa_dtr = p_priv->dtr_state;
		
	p_priv->resend_cont = 0;
	memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
	
	/* send the data out the device on control endpoint */
	this_urb->transfer_buffer_length = sizeof(msg);

	this_urb->dev = serial->dev;
	if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
		dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
	}
#if 0
	else {
		dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__
		    outcont_urb, this_urb->transfer_buffer_length,
		    usb_pipeendpoint(this_urb->pipe));
	}
#endif

	return (0);
}

static int keyspan_usa28_send_setup(struct usb_serial *serial,
				    struct usb_serial_port *port,
				    int reset_port)
{
	struct keyspan_usa28_portControlMessage	msg;		
	struct keyspan_serial_private	 	*s_priv;
	struct keyspan_port_private 		*p_priv;
	const struct keyspan_device_details	*d_details;
	struct urb				*this_urb;
	int 					device_port, err;

	dbg ("%s", __FUNCTION__);

	s_priv = usb_get_serial_data(serial);
	p_priv = usb_get_serial_port_data(port);
	d_details = s_priv->device_details;
	device_port = port->number - port->serial->minor;

	/* only do something if we have a bulk out endpoint */
	if ((this_urb = p_priv->outcont_urb) == NULL) {
		dbg("%s - oops no urb.", __FUNCTION__);
		return -1;
	}

	/* Save reset port val for resend.
	   Don't overwrite resend for close condition. */
	if (p_priv->resend_cont != 3)
		p_priv->resend_cont = reset_port + 1;
	if (this_urb->status == -EINPROGRESS) {
		dbg ("%s already writing", __FUNCTION__);
		mdelay(5);
		return(-1);
	}

	memset(&msg, 0, sizeof (struct keyspan_usa28_portControlMessage));

	msg.setBaudRate = 1;
	if (d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
		&msg.baudHi, &msg.baudLo, NULL, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
		dbg("%s - Invalid baud rate requested %d.", __FUNCTION__, p_priv->baud);
		msg.baudLo = 0xff;
		msg.baudHi = 0xb2;	/* Values for 9600 baud */
	}

	/* If parity is enabled, we must calculate it ourselves. */
	msg.parity = 0;		/* XXX for now */

	msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
	msg.xonFlowControl = 0;

	/* Do handshaking outputs, DTR is inverted relative to RTS */	
	msg.rts = p_priv->rts_state;
	msg.dtr = p_priv->dtr_state;

	msg.forwardingLength = 16;
	msg.forwardMs = 10;
	msg.breakThreshold = 45;
	msg.xonChar = 17;
	msg.xoffChar = 19;

	/*msg.returnStatus = 1;
	msg.resetDataToggle = 0xff;*/
	/* Opening port */
	if (reset_port == 1) {
		msg._txOn = 1;
		msg._txOff = 0;
		msg.txFlush = 0;
		msg.txForceXoff = 0;
		msg.txBreak = 0;
		msg.rxOn = 1;
		msg.rxOff = 0;
		msg.rxFlush = 1;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0xff;
	}
	/* Closing port */
	else if (reset_port == 2) {
		msg._txOn = 0;
		msg._txOff = 1;
		msg.txFlush = 0;
		msg.txForceXoff = 0;
		msg.txBreak = 0;
		msg.rxOn = 0;
		msg.rxOff = 1;
		msg.rxFlush = 1;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0;
	}
	/* Sending intermediate configs */
	else {
		msg._txOn = (! p_priv->break_on);
		msg._txOff = 0;
		msg.txFlush = 0;
		msg.txForceXoff = 0;
		msg.txBreak = (p_priv->break_on);
		msg.rxOn = 0;
		msg.rxOff = 0;
		msg.rxFlush = 0;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0x0;
	}

	p_priv->resend_cont = 0;
	memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));

	/* send the data out the device on control endpoint */
	this_urb->transfer_buffer_length = sizeof(msg);

	this_urb->dev = serial->dev;
	if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
		dbg("%s - usb_submit_urb(setup) failed", __FUNCTION__);
	}
#if 0
	else {
		dbg("%s - usb_submit_urb(setup) OK %d bytes", __FUNCTION__,
		    this_urb->transfer_buffer_length);
	}
#endif

	return (0);
}

static int keyspan_usa49_send_setup(struct usb_serial *serial,
				    struct usb_serial_port *port,
				    int reset_port)
{
	struct keyspan_usa49_portControlMessage	msg;		
	struct keyspan_serial_private 		*s_priv;
	struct keyspan_port_private 		*p_priv;
	const struct keyspan_device_details	*d_details;
	int 					glocont_urb;
	struct urb				*this_urb;
	int 					err, device_port;

	dbg ("%s", __FUNCTION__);

	s_priv = usb_get_serial_data(serial);
	p_priv = usb_get_serial_port_data(port);
	d_details = s_priv->device_details;

	glocont_urb = d_details->glocont_endpoint;
	this_urb = s_priv->glocont_urb;

		/* Work out which port within the device is being setup */
	device_port = port->number - port->serial->minor;

	dbg("%s - endpoint %d port %d (%d)",__FUNCTION__, usb_pipeendpoint(this_urb->pipe), port->number, device_port);

		/* Make sure we have an urb then send the message */
	if (this_urb == NULL) {
		dbg("%s - oops no urb for port %d.", __FUNCTION__, port->number);
		return -1;
	}

	/* Save reset port val for resend.
	   Don't overwrite resend for close condition. */
	if (p_priv->resend_cont != 3)
		p_priv->resend_cont = reset_port + 1;
	if (this_urb->status == -EINPROGRESS) {
		/*  dbg ("%s - already writing", __FUNCTION__); */
		mdelay(5);
		return(-1);
	}

	memset(&msg, 0, sizeof (struct keyspan_usa49_portControlMessage));

	/*msg.portNumber = port->number;*/
	msg.portNumber = device_port;
	
		/* Only set baud rate if it's changed */	
	if (p_priv->old_baud != p_priv->baud) {
		p_priv->old_baud = p_priv->baud;
		msg.setClocking = 0xff;
		if (d_details->calculate_baud_rate
		    (p_priv->baud, d_details->baudclk, &msg.baudHi,
		     &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
			dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
			    p_priv->baud);
			msg.baudLo = 0;
			msg.baudHi = 125;	/* Values for 9600 baud */
			msg.prescaler = 10;
		}
		//msg.setPrescaler = 0xff;
	}

	msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
	switch (p_priv->cflag & CSIZE) {
	case CS5:
		msg.lcr |= USA_DATABITS_5;
		break;
	case CS6:
		msg.lcr |= USA_DATABITS_6;
		break;
	case CS7:
		msg.lcr |= USA_DATABITS_7;
		break;
	case CS8:
		msg.lcr |= USA_DATABITS_8;
		break;
	}
	if (p_priv->cflag & PARENB) {
		/* note USA_PARITY_NONE == 0 */
		msg.lcr |= (p_priv->cflag & PARODD)?
			USA_PARITY_ODD: USA_PARITY_EVEN;
	}
	msg.setLcr = 0xff;

	msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
	msg.xonFlowControl = 0;
	msg.setFlowControl = 0xff;
	
	msg.forwardingLength = 16;
	msg.xonChar = 17;
	msg.xoffChar = 19;

	/* Opening port */ 
	if (reset_port == 1) {
		msg._txOn = 1;
		msg._txOff = 0;
		msg.txFlush = 0;
		msg.txBreak = 0;
		msg.rxOn = 1;
		msg.rxOff = 0;
		msg.rxFlush = 1;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0xff;
		msg.enablePort = 1;
		msg.disablePort = 0;
	}
	/* Closing port */
	else if (reset_port == 2) {
		msg._txOn = 0;
		msg._txOff = 1;
		msg.txFlush = 0;
		msg.txBreak = 0;
		msg.rxOn = 0;
		msg.rxOff = 1;
		msg.rxFlush = 1;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0;
		msg.enablePort = 0;
		msg.disablePort = 1;
	}
	/* Sending intermediate configs */
	else {
		msg._txOn = (! p_priv->break_on);
		msg._txOff = 0;
		msg.txFlush = 0;
		msg.txBreak = (p_priv->break_on);
		msg.rxOn = 0;
		msg.rxOff = 0;
		msg.rxFlush = 0;
		msg.rxForward = 0;
		msg.returnStatus = 0;
		msg.resetDataToggle = 0x0;
		msg.enablePort = 0;
		msg.disablePort = 0;
	}

		/* Do handshaking outputs */	
	msg.setRts = 0xff;
	msg.rts = p_priv->rts_state;

	msg.setDtr = 0xff;
	msg.dtr = p_priv->dtr_state;
		
	p_priv->resend_cont = 0;
	memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
	
	/* send the data out the device on control endpoint */
	this_urb->transfer_buffer_length = sizeof(msg);

	this_urb->dev = serial->dev;
	if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
		dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
	}
#if 0
	else {
		dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__,
		    outcont_urb, this_urb->transfer_buffer_length,
		    usb_pipeendpoint(this_urb->pipe));
	}
#endif

	return (0);
}

static int keyspan_usa90_send_setup(struct usb_serial *serial,
				    struct usb_serial_port *port,
				    int reset_port)
{
	struct keyspan_usa90_portControlMessage	msg;		
	struct keyspan_serial_private 		*s_priv;
	struct keyspan_port_private 		*p_priv;
	const struct keyspan_device_details	*d_details;
	struct urb				*this_urb;
	int 					err;
	u8						prescaler;

	dbg ("%s", __FUNCTION__);

	s_priv = usb_get_serial_data(serial);
	p_priv = usb_get_serial_port_data(port);
	d_details = s_priv->device_details;

	/* only do something if we have a bulk out endpoint */
	if ((this_urb = p_priv->outcont_urb) == NULL) {
		dbg("%s - oops no urb.", __FUNCTION__);
		return -1;
	}

	/* Save reset port val for resend.
	   Don't overwrite resend for open/close condition. */
	if ((reset_port + 1) > p_priv->resend_cont)
		p_priv->resend_cont = reset_port + 1;
	if (this_urb->status == -EINPROGRESS) {
		dbg ("%s already writing", __FUNCTION__);
		mdelay(5);
		return(-1);
	}

	memset(&msg, 0, sizeof (struct keyspan_usa90_portControlMessage));

	/* Only set baud rate if it's changed */	
	if (p_priv->old_baud != p_priv->baud) {
		p_priv->old_baud = p_priv->baud;
		msg.setClocking = 0x01;
		if (d_details->calculate_baud_rate
		    (p_priv->baud, d_details->baudclk, &msg.baudHi,
		     &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE ) {
			dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
			    p_priv->baud);
			p_priv->baud = 9600;
			d_details->calculate_baud_rate (p_priv->baud, d_details->baudclk, 
				&msg.baudHi, &msg.baudLo, &prescaler, 0);
		}
		msg.setRxMode = 1;
		msg.setTxMode = 1;
	}

	/* modes must always be correctly specified */
	if (p_priv->baud > 57600)
	{
		msg.rxMode = RXMODE_DMA;
		msg.txMode = TXMODE_DMA;
	}
	else
	{
		msg.rxMode = RXMODE_BYHAND;
		msg.txMode = TXMODE_BYHAND;
	}

	msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
	switch (p_priv->cflag & CSIZE) {
	case CS5:
		msg.lcr |= USA_DATABITS_5;
		break;
	case CS6:
		msg.lcr |= USA_DATABITS_6;
		break;
	case CS7:
		msg.lcr |= USA_DATABITS_7;
		break;
	case CS8:
		msg.lcr |= USA_DATABITS_8;
		break;
	}
	if (p_priv->cflag & PARENB) {
		/* note USA_PARITY_NONE == 0 */
		msg.lcr |= (p_priv->cflag & PARODD)?
			USA_PARITY_ODD: USA_PARITY_EVEN;
	}
	if (p_priv->old_cflag != p_priv->cflag) {
		p_priv->old_cflag = p_priv->cflag;
		msg.setLcr = 0x01;
	}

	if (p_priv->flow_control == flow_cts)
		msg.txFlowControl = TXFLOW_CTS;
	msg.setTxFlowControl = 0x01;
	msg.setRxFlowControl = 0x01;
	
	msg.rxForwardingLength = 16;
	msg.rxForwardingTimeout = 16;	
	msg.txAckSetting = 0;
	msg.xonChar = 17;
	msg.xoffChar = 19;

	/* Opening port */ 
	if (reset_port == 1) {
		msg.portEnabled = 1;
		msg.rxFlush = 1;
		msg.txBreak = (p_priv->break_on);
	}
	/* Closing port */
	else if (reset_port == 2) {
		msg.portEnabled = 0;
	}
	/* Sending intermediate configs */
	else {
		if (port->open_count)
			msg.portEnabled = 1;
		msg.txBreak = (p_priv->break_on);
	}

	/* Do handshaking outputs */	
	msg.setRts = 0x01;
	msg.rts = p_priv->rts_state;

	msg.setDtr = 0x01;
	msg.dtr = p_priv->dtr_state;
		
	p_priv->resend_cont = 0;
	memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
	
	/* send the data out the device on control endpoint */
	this_urb->transfer_buffer_length = sizeof(msg);

	this_urb->dev = serial->dev;
	if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
		dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
	}
	return (0);
}

static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
{
	struct usb_serial *serial = port->serial;
	struct keyspan_serial_private *s_priv;
	const struct keyspan_device_details *d_details;

	dbg ("%s", __FUNCTION__);

	s_priv = usb_get_serial_data(serial);
	d_details = s_priv->device_details;

	switch (d_details->msg_format) {
	case msg_usa26:
		keyspan_usa26_send_setup(serial, port, reset_port);
		break;
	case msg_usa28:
		keyspan_usa28_send_setup(serial, port, reset_port);
		break;
	case msg_usa49:
		keyspan_usa49_send_setup(serial, port, reset_port);
		break;
	case msg_usa90:
		keyspan_usa90_send_setup(serial, port, reset_port);
		break;
	}
}


/* Gets called by the "real" driver (ie once firmware is loaded
   and renumeration has taken place. */
static int keyspan_startup (struct usb_serial *serial)
{
	int				i, err;
	struct usb_serial_port		*port;
	struct keyspan_serial_private 	*s_priv;
	struct keyspan_port_private	*p_priv;
	const struct keyspan_device_details	*d_details;

	dbg("%s", __FUNCTION__);

	for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
		if (d_details->product_id == le16_to_cpu(serial->dev->descriptor.idProduct))
			break;
	if (d_details == NULL) {
		dev_err(&serial->dev->dev, "%s - unknown product id %x\n", __FUNCTION__, le16_to_cpu(serial->dev->descriptor.idProduct));
		return 1;
	}

	/* Setup private data for serial driver */
	s_priv = kzalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
	if (!s_priv) {
		dbg("%s - kmalloc for keyspan_serial_private failed.", __FUNCTION__);
		return -ENOMEM;
	}

	s_priv->device_details = d_details;
	usb_set_serial_data(serial, s_priv);

	/* Now setup per port private data */
	for (i = 0; i < serial->num_ports; i++) {
		port = serial->port[i];
		p_priv = kzalloc(sizeof(struct keyspan_port_private), GFP_KERNEL);
		if (!p_priv) {
			dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __FUNCTION__, i);
			return (1);
		}
		p_priv->device_details = d_details;
		usb_set_serial_port_data(port, p_priv);
	}

	keyspan_setup_urbs(serial);

	s_priv->instat_urb->dev = serial->dev;
	if ((err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL)) != 0) {
		dbg("%s - submit instat urb failed %d", __FUNCTION__, err);
	}
			
	return (0);
}

static void keyspan_shutdown (struct usb_serial *serial)
{
	int				i, j;
	struct usb_serial_port		*port;
	struct keyspan_serial_private 	*s_priv;
	struct keyspan_port_private	*p_priv;

	dbg("%s", __FUNCTION__);

	s_priv = usb_get_serial_data(serial);

	/* Stop reading/writing urbs */
	stop_urb(s_priv->instat_urb);
	stop_urb(s_priv->glocont_urb);
	for (i = 0; i < serial->num_ports; ++i) {
		port = serial->port[i];
		p_priv = usb_get_serial_port_data(port);
		stop_urb(p_priv->inack_urb);
		stop_urb(p_priv->outcont_urb);
		for (j = 0; j < 2; j++) {
			stop_urb(p_priv->in_urbs[j]);
			stop_urb(p_priv->out_urbs[j]);
		}
	}

	/* Now free them */
	if (s_priv->instat_urb)
		usb_free_urb(s_priv->instat_urb);
	if (s_priv->glocont_urb)
		usb_free_urb(s_priv->glocont_urb);
	for (i = 0; i < serial->num_ports; ++i) {
		port = serial->port[i];
		p_priv = usb_get_serial_port_data(port);
		if (p_priv->inack_urb)
			usb_free_urb(p_priv->inack_urb);
		if (p_priv->outcont_urb)
			usb_free_urb(p_priv->outcont_urb);
		for (j = 0; j < 2; j++) {
			if (p_priv->in_urbs[j])
				usb_free_urb(p_priv->in_urbs[j]);
			if (p_priv->out_urbs[j])
				usb_free_urb(p_priv->out_urbs[j]);
		}
	}

	/*  dbg("Freeing serial->private."); */
	kfree(s_priv);

	/*  dbg("Freeing port->private."); */
	/* Now free per port private data */
	for (i = 0; i < serial->num_ports; i++) {
		port = serial->port[i];
		kfree(usb_get_serial_port_data(port));
	}
}

MODULE_AUTHOR( DRIVER_AUTHOR );
MODULE_DESCRIPTION( DRIVER_DESC );
MODULE_LICENSE("GPL");

module_param(debug, bool, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(debug, "Debug enabled or not");