ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / usb / serial / ftdi_sio.c
1 /*
2  * USB FTDI SIO driver
3  *
4  *      Copyright (C) 1999 - 2001
5  *          Greg Kroah-Hartman (greg@kroah.com)
6  *          Bill Ryder (bryder@sgi.com)
7  *      Copyright (C) 2002
8  *          Kuba Ober (kuba@mareimbrium.org)
9  *
10  *      This program is free software; you can redistribute it and/or modify
11  *      it under the terms of the GNU General Public License as published by
12  *      the Free Software Foundation; either version 2 of the License, or
13  *      (at your option) any later version.
14  *
15  * See Documentation/usb/usb-serial.txt for more information on using this driver
16  *
17  * See http://ftdi-usb-sio.sourceforge.net for upto date testing info
18  *      and extra documentation
19  *
20  * (26/Mar/2004) Jan Capek
21  *      Added PID's for ICD-U20/ICD-U40 - incircuit PIC debuggers from CCS Inc.
22  *
23  * (09/Feb/2004) Ian Abbott
24  *      Changed full name of USB-UIRT device to avoid "/" character.
25  *      Added FTDI's alternate PID (0x6006) for FT232/245 devices.
26  *      Added PID for "ELV USB Module UO100" from Stefan Frings.
27  * 
28  * (21/Oct/2003) Ian Abbott
29  *      Renamed some VID/PID macros for Matrix Orbital and Perle Systems
30  *      devices.  Removed Matrix Orbital and Perle Systems devices from the
31  *      8U232AM device table, but left them in the FT232BM table, as they are
32  *      known to use only FT232BM.
33  *
34  * (17/Oct/2003) Scott Allen
35  *      Added vid/pid for Perle Systems UltraPort USB serial converters
36  *
37  * (21/Sep/2003) Ian Abbott
38  *      Added VID/PID for Omnidirectional Control Technology US101 USB to
39  *      RS-232 adapter (also rebadged as Dick Smith Electronics XH6381).
40  *      VID/PID supplied by Donald Gordon.
41  *
42  * (19/Aug/2003) Ian Abbott
43  *      Freed urb's transfer buffer in write bulk callback.
44  *      Omitted some paranoid checks in write bulk callback that don't matter.
45  *      Scheduled work in write bulk callback regardless of port's open count.
46  *
47  * (05/Aug/2003) Ian Abbott
48  *      Added VID/PID for ID TECH IDT1221U USB to RS-232 adapter.
49  *      VID/PID provided by Steve Briggs.
50  *
51  * (23/Jul/2003) Ian Abbott
52  *      Added PIDs for CrystalFontz 547, 633, 631, 635, 640 and 640 from
53  *      Wayne Wylupski.
54  *
55  * (10/Jul/2003) David Glance
56  *      Added PID for DSS-20 SyncStation cradle for Sony-Ericsson P800.
57  *
58  * (27/Jun/2003) Ian Abbott
59  *      Reworked the urb handling logic.  We have no more pool, but dynamically
60  *      allocate the urb and the transfer buffer on the fly.  In testing this
61  *      does not incure any measurable overhead.  This also relies on the fact
62  *      that we have proper reference counting logic for urbs.  I nicked this
63  *      from Greg KH's Visor driver.
64  *      
65  * (23/Jun/2003) Ian Abbott
66  *      Reduced flip buffer pushes and corrected a data length test in
67  *      ftdi_read_bulk_callback.
68  *      Defererence pointers after any paranoid checks, not before.
69  *
70  * (21/Jun/2003) Erik Nygren
71  *      Added support for Home Electronics Tira-1 IR tranceiver using FT232BM chip.
72  *      See <http://www.home-electro.com/tira1.htm>.  Only operates properly 
73  *      at 100000 and RTS-CTS, so set custom divisor mode on startup.
74  *      Also force the Tira-1 and USB-UIRT to only use their custom baud rates.
75  *
76  * (18/Jun/2003) Ian Abbott
77  *      Added Device ID of the USB relais from Rudolf Gugler (backported from
78  *      Philipp Gühring's patch for 2.5.x kernel).
79  *      Moved read transfer buffer reallocation into startup function.
80  *      Free existing write urb and transfer buffer in startup function.
81  *      Only use urbs in write urb pool that were successfully allocated.
82  *      Moved some constant macros out of functions.
83  *      Minor whitespace and comment changes.
84  *
85  * (12/Jun/2003) David Norwood
86  *      Added support for USB-UIRT IR tranceiver using 8U232AM chip.
87  *      See <http://home.earthlink.net/~jrhees/USBUIRT/index.htm>.  Only
88  *      operates properly at 312500, so set custom divisor mode on startup.
89  *
90  * (12/Jun/2003) Ian Abbott
91  *      Added Sealevel SeaLINK+ 210x, 220x, 240x, 280x vid/pids from Tuan Hoang
92  *      - I've eliminated some that don't seem to exist!
93  *      Added Home Electronics Tira-1 IR transceiver pid from Chris Horn
94  *      Some whitespace/coding-style cleanups
95  *
96  * (11/Jun/2003) Ian Abbott
97  *      Fixed unsafe spinlock usage in ftdi_write
98  *
99  * (24/Feb/2003) Richard Shooter
100  *      Increase read buffer size to improve read speeds at higher baud rates
101  *      (specifically tested with up to 1Mb/sec at 1.5M baud)
102  *
103  * (23/Feb/2003) John Wilkins
104  *      Added Xon/xoff flow control (activating support in the ftdi device)
105  *      Added vid/pid for Videonetworks/Homechoice (UK ISP)
106  *
107  * (23/Feb/2003) Bill Ryder
108  *      Added matrix orb device vid/pids from Wayne Wylupski
109  *
110  * (19/Feb/2003) Ian Abbott
111  *      For TIOCSSERIAL, set alt_speed to 0 when ASYNC_SPD_MASK value has
112  *      changed to something other than ASYNC_SPD_HI, ASYNC_SPD_VHI,
113  *      ASYNC_SPD_SHI or ASYNC_SPD_WARP.  Also, unless ASYNC_SPD_CUST is in
114  *      force, don't bother changing baud rate when custom_divisor has changed.
115  *
116  * (18/Feb/2003) Ian Abbott
117  *      Fixed TIOCMGET handling to include state of DTR and RTS, the state
118  *      of which are now saved by set_dtr() and set_rts().
119  *      Fixed improper storage class for buf in set_dtr() and set_rts().
120  *      Added FT232BM chip type and support for its extra baud rates (compared
121  *      to FT8U232AM).
122  *      Took account of special case divisor values for highest baud rates of
123  *      FT8U232AM and FT232BM.
124  *      For TIOCSSERIAL, forced alt_speed to 0 when ASYNC_SPD_CUST kludge used,
125  *      as previous alt_speed setting is now stale.
126  *      Moved startup code common between the startup routines for the
127  *      different chip types into a common subroutine.
128  *
129  * (17/Feb/2003) Bill Ryder
130  *      Added write urb buffer pool on a per device basis
131  *      Added more checking for open file on callbacks (fixed OOPS)
132  *      Added CrystalFontz 632 and 634 PIDs 
133  *         (thanx to CrystalFontz for the sample devices - they flushed out
134  *           some driver bugs)
135  *      Minor debugging message changes
136  *      Added throttle, unthrottle and chars_in_buffer functions
137  *      Fixed FTDI_SIO (the original device) bug
138  *      Fixed some shutdown handling
139  *      
140  * 
141  * 
142  * 
143  * (07/Jun/2002) Kuba Ober
144  *      Changed FTDI_SIO_BASE_BAUD_TO_DIVISOR macro into ftdi_baud_to_divisor
145  *      function. It was getting too complex.
146  *      Fix the divisor calculation logic which was setting divisor of 0.125
147  *      instead of 0.5 for fractional parts of divisor equal to 5/8, 6/8, 7/8.
148  *      Also make it bump up the divisor to next integer in case of 7/8 - it's
149  *      a better approximation.
150  *
151  * (25/Jul/2002) Bill Ryder inserted Dmitri's TIOCMIWAIT patch
152  *      Not tested by me but it doesn't break anything I use.
153  * 
154  * (04/Jan/2002) Kuba Ober
155  *      Implemented 38400 baudrate kludge, where it can be substituted with other
156  *        values. That's the only way to set custom baudrates.
157  *      Implemented TIOCSSERIAL, TIOCGSERIAL ioctl's so that setserial is happy.
158  *      FIXME: both baudrate things should eventually go to usbserial.c as other
159  *        devices may need that functionality too. Actually, it can probably be
160  *        merged in serial.c somehow - too many drivers repeat this code over
161  *        and over.
162  *      Fixed baudrate forgetfulness - open() used to reset baudrate to 9600 every time.
163  *      Divisors for baudrates are calculated by a macro.
164  *      Small code cleanups. Ugly whitespace changes for Plato's sake only ;-].
165  *
166  * (04/Nov/2001) Bill Ryder
167  *      Fixed bug in read_bulk_callback where incorrect urb buffer was used.
168  *      Cleaned up write offset calculation
169  *      Added write_room since default values can be incorrect for sio
170  *      Changed write_bulk_callback to use same queue_task as other drivers
171  *        (the previous version caused panics)
172  *      Removed port iteration code since the device only has one I/O port and it
173  *        was wrong anyway.
174  * 
175  * (31/May/2001) gkh
176  *      Switched from using spinlock to a semaphore, which fixes lots of problems.
177  *
178  * (23/May/2001)   Bill Ryder
179  *      Added runtime debug patch (thanx Tyson D Sawyer).
180  *      Cleaned up comments for 8U232
181  *      Added parity, framing and overrun error handling
182  *      Added receive break handling.
183  * 
184  * (04/08/2001) gb
185  *      Identify version on module load.
186  *       
187  * (18/March/2001) Bill Ryder
188  *      (Not released)
189  *      Added send break handling. (requires kernel patch too)
190  *      Fixed 8U232AM hardware RTS/CTS etc status reporting.
191  *      Added flipbuf fix copied from generic device
192  * 
193  * (12/3/2000) Bill Ryder
194  *      Added support for 8U232AM device.
195  *      Moved PID and VIDs into header file only.
196  *      Turned on low-latency for the tty (device will do high baudrates)
197  *      Added shutdown routine to close files when device removed.
198  *      More debug and error message cleanups.
199  *
200  * (11/13/2000) Bill Ryder
201  *      Added spinlock protected open code and close code.
202  *      Multiple opens work (sort of - see webpage mentioned above).
203  *      Cleaned up comments. Removed multiple PID/VID definitions.
204  *      Factorised cts/dtr code
205  *      Made use of __FUNCTION__ in dbg's
206  *      
207  * (11/01/2000) Adam J. Richter
208  *      usb_device_id table support
209  * 
210  * (10/05/2000) gkh
211  *      Fixed bug with urb->dev not being set properly, now that the usb
212  *      core needs it.
213  * 
214  * (09/11/2000) gkh
215  *      Removed DEBUG #ifdefs with call to usb_serial_debug_data
216  *
217  * (07/19/2000) gkh
218  *      Added module_init and module_exit functions to handle the fact that this
219  *      driver is a loadable module now.
220  *
221  * (04/04/2000) Bill Ryder 
222  *      Fixed bugs in TCGET/TCSET ioctls (by removing them - they are
223  *        handled elsewhere in the tty io driver chain).
224  *
225  * (03/30/2000) Bill Ryder 
226  *      Implemented lots of ioctls
227  *      Fixed a race condition in write
228  *      Changed some dbg's to errs
229  *
230  * (03/26/2000) gkh
231  *      Split driver up into device specific pieces.
232  *
233  */
234
235 /* Bill Ryder - bryder@sgi.com - wrote the FTDI_SIO implementation */
236 /* Thanx to FTDI for so kindly providing details of the protocol required */
237 /*   to talk to the device */
238 /* Thanx to gkh and the rest of the usb dev group for all code I have assimilated :-) */
239
240 #include <linux/config.h>
241 #include <linux/kernel.h>
242 #include <linux/errno.h>
243 #include <linux/init.h>
244 #include <linux/slab.h>
245 #include <linux/tty.h>
246 #include <linux/tty_driver.h>
247 #include <linux/tty_flip.h>
248 #include <linux/module.h>
249 #include <linux/spinlock.h>
250 #include <asm/uaccess.h>
251 #include <linux/usb.h>
252 #include <linux/serial.h>
253 #ifdef CONFIG_USB_SERIAL_DEBUG
254         static int debug = 1;
255 #else
256         static int debug;
257 #endif
258
259 #include "usb-serial.h"
260 #include "ftdi_sio.h"
261
262 /*
263  * Version Information
264  */
265 #define DRIVER_VERSION "v1.4.0"
266 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Bill Ryder <bryder@sgi.com>, Kuba Ober <kuba@mareimbrium.org>"
267 #define DRIVER_DESC "USB FTDI Serial Converters Driver"
268
269 static struct usb_device_id id_table_sio [] = {
270         { USB_DEVICE(FTDI_VID, FTDI_SIO_PID) },
271         { }                                             /* Terminating entry */
272 };
273
274 /*
275  * The 8U232AM has the same API as the sio except for:
276  * - it can support MUCH higher baudrates; up to:
277  *   o 921600 for RS232 and 2000000 for RS422/485 at 48MHz
278  *   o 230400 at 12MHz
279  *   so .. 8U232AM's baudrate setting codes are different
280  * - it has a two byte status code.
281  * - it returns characters every 16ms (the FTDI does it every 40ms)
282  *
283  * the bcdDevice value is used to differentiate FT232BM and FT245BM from
284  * the earlier FT8U232AM and FT8U232BM.  For now, include all known VID/PID
285  * combinations in both tables.
286  * FIXME: perhaps bcdDevice can also identify 12MHz devices, but I don't know
287  * if those ever went into mass production. [Ian Abbott]
288  */
289
290
291 static struct usb_device_id id_table_8U232AM [] = {
292         { USB_DEVICE_VER(FTDI_VID, FTDI_IRTRANS_PID, 0, 0x3ff) },
293         { USB_DEVICE_VER(FTDI_VID, FTDI_8U232AM_PID, 0, 0x3ff) },
294         { USB_DEVICE_VER(FTDI_VID, FTDI_8U232AM_ALT_PID, 0, 0x3ff) },
295         { USB_DEVICE_VER(FTDI_VID, FTDI_RELAIS_PID, 0, 0x3ff) },
296         { USB_DEVICE_VER(FTDI_NF_RIC_VID, FTDI_NF_RIC_PID, 0, 0x3ff) },
297         { USB_DEVICE_VER(FTDI_VID, FTDI_XF_632_PID, 0, 0x3ff) },
298         { USB_DEVICE_VER(FTDI_VID, FTDI_XF_634_PID, 0, 0x3ff) },
299         { USB_DEVICE_VER(FTDI_VID, FTDI_XF_547_PID, 0, 0x3ff) },
300         { USB_DEVICE_VER(FTDI_VID, FTDI_XF_633_PID, 0, 0x3ff) },
301         { USB_DEVICE_VER(FTDI_VID, FTDI_XF_631_PID, 0, 0x3ff) },
302         { USB_DEVICE_VER(FTDI_VID, FTDI_XF_635_PID, 0, 0x3ff) },
303         { USB_DEVICE_VER(FTDI_VID, FTDI_XF_640_PID, 0, 0x3ff) },
304         { USB_DEVICE_VER(FTDI_VID, FTDI_XF_642_PID, 0, 0x3ff) },
305         { USB_DEVICE_VER(FTDI_VID, FTDI_VNHCPCUSB_D_PID, 0, 0x3ff) },
306         { USB_DEVICE_VER(FTDI_VID, FTDI_DSS20_PID, 0, 0x3ff) },
307         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2101_PID, 0, 0x3ff) },
308         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2102_PID, 0, 0x3ff) },
309         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2103_PID, 0, 0x3ff) },
310         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2104_PID, 0, 0x3ff) },
311         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2201_1_PID, 0, 0x3ff) },
312         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2201_2_PID, 0, 0x3ff) },
313         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2202_1_PID, 0, 0x3ff) },
314         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2202_2_PID, 0, 0x3ff) },
315         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2203_1_PID, 0, 0x3ff) },
316         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2203_2_PID, 0, 0x3ff) },
317         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2401_1_PID, 0, 0x3ff) },
318         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2401_2_PID, 0, 0x3ff) },
319         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2401_3_PID, 0, 0x3ff) },
320         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2401_4_PID, 0, 0x3ff) },
321         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2402_1_PID, 0, 0x3ff) },
322         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2402_2_PID, 0, 0x3ff) },
323         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2402_3_PID, 0, 0x3ff) },
324         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2402_4_PID, 0, 0x3ff) },
325         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2403_1_PID, 0, 0x3ff) },
326         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2403_2_PID, 0, 0x3ff) },
327         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2403_3_PID, 0, 0x3ff) },
328         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2403_4_PID, 0, 0x3ff) },
329         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2801_1_PID, 0, 0x3ff) },
330         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2801_2_PID, 0, 0x3ff) },
331         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2801_3_PID, 0, 0x3ff) },
332         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2801_4_PID, 0, 0x3ff) },
333         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2801_5_PID, 0, 0x3ff) },
334         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2801_6_PID, 0, 0x3ff) },
335         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2801_7_PID, 0, 0x3ff) },
336         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2801_8_PID, 0, 0x3ff) },
337         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2802_1_PID, 0, 0x3ff) },
338         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2802_2_PID, 0, 0x3ff) },
339         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2802_3_PID, 0, 0x3ff) },
340         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2802_4_PID, 0, 0x3ff) },
341         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2802_5_PID, 0, 0x3ff) },
342         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2802_6_PID, 0, 0x3ff) },
343         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2802_7_PID, 0, 0x3ff) },
344         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2802_8_PID, 0, 0x3ff) },
345         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2803_1_PID, 0, 0x3ff) },
346         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2803_2_PID, 0, 0x3ff) },
347         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2803_3_PID, 0, 0x3ff) },
348         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2803_4_PID, 0, 0x3ff) },
349         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2803_5_PID, 0, 0x3ff) },
350         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2803_6_PID, 0, 0x3ff) },
351         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2803_7_PID, 0, 0x3ff) },
352         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2803_8_PID, 0, 0x3ff) },
353         { USB_DEVICE_VER(IDTECH_VID, IDTECH_IDT1221U_PID, 0, 0x3ff) },
354         { USB_DEVICE_VER(OCT_VID, OCT_US101_PID, 0, 0x3ff) },
355         { USB_DEVICE_VER(FTDI_VID, PROTEGO_SPECIAL_1, 0, 0x3ff) },
356         { USB_DEVICE_VER(FTDI_VID, PROTEGO_R2X0, 0, 0x3ff) },
357         { USB_DEVICE_VER(FTDI_VID, PROTEGO_SPECIAL_3, 0, 0x3ff) },
358         { USB_DEVICE_VER(FTDI_VID, PROTEGO_SPECIAL_4, 0, 0x3ff) },
359         { USB_DEVICE_VER(FTDI_VID, FTDI_ELV_UO100_PID, 0, 0x3ff) },
360         { USB_DEVICE_VER(FTDI_VID, INSIDE_ACCESSO, 0, 0x3ff) },
361         { }                                             /* Terminating entry */
362 };
363
364
365 static struct usb_device_id id_table_FT232BM [] = {
366         { USB_DEVICE_VER(FTDI_VID, FTDI_IRTRANS_PID, 0x400, 0xffff) },
367         { USB_DEVICE_VER(FTDI_VID, FTDI_8U232AM_PID, 0x400, 0xffff) },
368         { USB_DEVICE_VER(FTDI_VID, FTDI_8U232AM_ALT_PID, 0x400, 0xffff) },
369         { USB_DEVICE_VER(FTDI_VID, FTDI_RELAIS_PID, 0x400, 0xffff) },
370         { USB_DEVICE_VER(FTDI_NF_RIC_VID, FTDI_NF_RIC_PID, 0x400, 0xffff) },
371         { USB_DEVICE_VER(FTDI_VID, FTDI_XF_632_PID, 0x400, 0xffff) },
372         { USB_DEVICE_VER(FTDI_VID, FTDI_XF_634_PID, 0x400, 0xffff) },
373         { USB_DEVICE_VER(FTDI_VID, FTDI_XF_547_PID, 0x400, 0xffff) },
374         { USB_DEVICE_VER(FTDI_VID, FTDI_XF_633_PID, 0x400, 0xffff) },
375         { USB_DEVICE_VER(FTDI_VID, FTDI_XF_631_PID, 0x400, 0xffff) },
376         { USB_DEVICE_VER(FTDI_VID, FTDI_XF_635_PID, 0x400, 0xffff) },
377         { USB_DEVICE_VER(FTDI_VID, FTDI_XF_640_PID, 0x400, 0xffff) },
378         { USB_DEVICE_VER(FTDI_VID, FTDI_XF_642_PID, 0x400, 0xffff) },
379         { USB_DEVICE_VER(FTDI_VID, FTDI_VNHCPCUSB_D_PID, 0x400, 0xffff) },
380         { USB_DEVICE_VER(FTDI_VID, FTDI_DSS20_PID, 0x400, 0xffff) },
381         { USB_DEVICE_VER(FTDI_VID, FTDI_MTXORB_0_PID, 0x400, 0xffff) },
382         { USB_DEVICE_VER(FTDI_VID, FTDI_MTXORB_1_PID, 0x400, 0xffff) },
383         { USB_DEVICE_VER(FTDI_VID, FTDI_MTXORB_2_PID, 0x400, 0xffff) },
384         { USB_DEVICE_VER(FTDI_VID, FTDI_MTXORB_3_PID, 0x400, 0xffff) },
385         { USB_DEVICE_VER(FTDI_VID, FTDI_MTXORB_4_PID, 0x400, 0xffff) },
386         { USB_DEVICE_VER(FTDI_VID, FTDI_MTXORB_5_PID, 0x400, 0xffff) },
387         { USB_DEVICE_VER(FTDI_VID, FTDI_MTXORB_6_PID, 0x400, 0xffff) },
388         { USB_DEVICE_VER(FTDI_VID, FTDI_PERLE_ULTRAPORT_PID, 0x400, 0xffff) },
389         { USB_DEVICE_VER(FTDI_VID, FTDI_PIEGROUP_PID, 0x400, 0xffff) },
390         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2101_PID, 0x400, 0xffff) },
391         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2102_PID, 0x400, 0xffff) },
392         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2103_PID, 0x400, 0xffff) },
393         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2104_PID, 0x400, 0xffff) },
394         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2201_1_PID, 0x400, 0xffff) },
395         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2201_2_PID, 0x400, 0xffff) },
396         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2202_1_PID, 0x400, 0xffff) },
397         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2202_2_PID, 0x400, 0xffff) },
398         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2203_1_PID, 0x400, 0xffff) },
399         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2203_2_PID, 0x400, 0xffff) },
400         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2401_1_PID, 0x400, 0xffff) },
401         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2401_2_PID, 0x400, 0xffff) },
402         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2401_3_PID, 0x400, 0xffff) },
403         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2401_4_PID, 0x400, 0xffff) },
404         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2402_1_PID, 0x400, 0xffff) },
405         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2402_2_PID, 0x400, 0xffff) },
406         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2402_3_PID, 0x400, 0xffff) },
407         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2402_4_PID, 0x400, 0xffff) },
408         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2403_1_PID, 0x400, 0xffff) },
409         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2403_2_PID, 0x400, 0xffff) },
410         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2403_3_PID, 0x400, 0xffff) },
411         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2403_4_PID, 0x400, 0xffff) },
412         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2801_1_PID, 0x400, 0xffff) },
413         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2801_2_PID, 0x400, 0xffff) },
414         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2801_3_PID, 0x400, 0xffff) },
415         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2801_4_PID, 0x400, 0xffff) },
416         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2801_5_PID, 0x400, 0xffff) },
417         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2801_6_PID, 0x400, 0xffff) },
418         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2801_7_PID, 0x400, 0xffff) },
419         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2801_8_PID, 0x400, 0xffff) },
420         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2802_1_PID, 0x400, 0xffff) },
421         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2802_2_PID, 0x400, 0xffff) },
422         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2802_3_PID, 0x400, 0xffff) },
423         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2802_4_PID, 0x400, 0xffff) },
424         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2802_5_PID, 0x400, 0xffff) },
425         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2802_6_PID, 0x400, 0xffff) },
426         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2802_7_PID, 0x400, 0xffff) },
427         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2802_8_PID, 0x400, 0xffff) },
428         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2803_1_PID, 0x400, 0xffff) },
429         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2803_2_PID, 0x400, 0xffff) },
430         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2803_3_PID, 0x400, 0xffff) },
431         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2803_4_PID, 0x400, 0xffff) },
432         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2803_5_PID, 0x400, 0xffff) },
433         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2803_6_PID, 0x400, 0xffff) },
434         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2803_7_PID, 0x400, 0xffff) },
435         { USB_DEVICE_VER(SEALEVEL_VID, SEALEVEL_2803_8_PID, 0x400, 0xffff) },
436         { USB_DEVICE_VER(IDTECH_VID, IDTECH_IDT1221U_PID, 0x400, 0xffff) },
437         { USB_DEVICE_VER(OCT_VID, OCT_US101_PID, 0x400, 0xffff) },
438         { USB_DEVICE_VER(FTDI_VID, PROTEGO_SPECIAL_1, 0x400, 0xffff) },
439         { USB_DEVICE_VER(FTDI_VID, PROTEGO_R2X0, 0x400, 0xffff) },
440         { USB_DEVICE_VER(FTDI_VID, PROTEGO_SPECIAL_3, 0x400, 0xffff) },
441         { USB_DEVICE_VER(FTDI_VID, PROTEGO_SPECIAL_4, 0x400, 0xffff) },
442         { USB_DEVICE_VER(FTDI_VID, FTDI_GUDEADS_E808_PID, 0x400, 0xffff) },
443         { USB_DEVICE_VER(FTDI_VID, FTDI_GUDEADS_E809_PID, 0x400, 0xffff) },
444         { USB_DEVICE_VER(FTDI_VID, FTDI_GUDEADS_E80A_PID, 0x400, 0xffff) },
445         { USB_DEVICE_VER(FTDI_VID, FTDI_GUDEADS_E80B_PID, 0x400, 0xffff) },
446         { USB_DEVICE_VER(FTDI_VID, FTDI_GUDEADS_E80C_PID, 0x400, 0xffff) },
447         { USB_DEVICE_VER(FTDI_VID, FTDI_GUDEADS_E80D_PID, 0x400, 0xffff) },
448         { USB_DEVICE_VER(FTDI_VID, FTDI_GUDEADS_E80E_PID, 0x400, 0xffff) },
449         { USB_DEVICE_VER(FTDI_VID, FTDI_GUDEADS_E80F_PID, 0x400, 0xffff) },
450         { USB_DEVICE_VER(FTDI_VID, FTDI_GUDEADS_E888_PID, 0x400, 0xffff) },
451         { USB_DEVICE_VER(FTDI_VID, FTDI_GUDEADS_E889_PID, 0x400, 0xffff) },
452         { USB_DEVICE_VER(FTDI_VID, FTDI_GUDEADS_E88A_PID, 0x400, 0xffff) },
453         { USB_DEVICE_VER(FTDI_VID, FTDI_GUDEADS_E88B_PID, 0x400, 0xffff) },
454         { USB_DEVICE_VER(FTDI_VID, FTDI_GUDEADS_E88C_PID, 0x400, 0xffff) },
455         { USB_DEVICE_VER(FTDI_VID, FTDI_GUDEADS_E88D_PID, 0x400, 0xffff) },
456         { USB_DEVICE_VER(FTDI_VID, FTDI_GUDEADS_E88E_PID, 0x400, 0xffff) },
457         { USB_DEVICE_VER(FTDI_VID, FTDI_GUDEADS_E88F_PID, 0x400, 0xffff) },
458         { USB_DEVICE_VER(FTDI_VID, FTDI_ELV_UO100_PID, 0x400, 0xffff) },
459         { USB_DEVICE_VER(FTDI_VID, LINX_SDMUSBQSS_PID, 0x400, 0xffff) },
460         { USB_DEVICE_VER(FTDI_VID, LINX_MASTERDEVEL2_PID, 0x400, 0xffff) },
461         { USB_DEVICE_VER(FTDI_VID, LINX_FUTURE_0_PID, 0x400, 0xffff) },
462         { USB_DEVICE_VER(FTDI_VID, LINX_FUTURE_1_PID, 0x400, 0xffff) },
463         { USB_DEVICE_VER(FTDI_VID, LINX_FUTURE_2_PID, 0x400, 0xffff) },
464         { USB_DEVICE(FTDI_VID, FTDI_CCSICDU20_0_PID) },
465         { USB_DEVICE(FTDI_VID, FTDI_CCSICDU40_1_PID) },
466         { }                                             /* Terminating entry */
467 };
468
469
470 static struct usb_device_id id_table_USB_UIRT [] = {
471         { USB_DEVICE(FTDI_VID, FTDI_USB_UIRT_PID) },
472         { }                                             /* Terminating entry */
473 };
474
475
476 static struct usb_device_id id_table_HE_TIRA1 [] = {
477         { USB_DEVICE_VER(FTDI_VID, FTDI_HE_TIRA1_PID, 0x400, 0xffff) },
478         { }                                             /* Terminating entry */
479 };
480
481
482 static struct usb_device_id id_table_combined [] = {
483         { USB_DEVICE(FTDI_VID, FTDI_IRTRANS_PID) },
484         { USB_DEVICE(FTDI_VID, FTDI_SIO_PID) },
485         { USB_DEVICE(FTDI_VID, FTDI_8U232AM_PID) },
486         { USB_DEVICE(FTDI_VID, FTDI_8U232AM_ALT_PID) },
487         { USB_DEVICE(FTDI_VID, FTDI_RELAIS_PID) },
488         { USB_DEVICE(FTDI_VID, FTDI_XF_632_PID) },
489         { USB_DEVICE(FTDI_VID, FTDI_XF_634_PID) },
490         { USB_DEVICE(FTDI_VID, FTDI_XF_547_PID) },
491         { USB_DEVICE(FTDI_VID, FTDI_XF_633_PID) },
492         { USB_DEVICE(FTDI_VID, FTDI_XF_631_PID) },
493         { USB_DEVICE(FTDI_VID, FTDI_XF_635_PID) },
494         { USB_DEVICE(FTDI_VID, FTDI_XF_640_PID) },
495         { USB_DEVICE(FTDI_VID, FTDI_XF_642_PID) },
496         { USB_DEVICE(FTDI_VID, FTDI_DSS20_PID) },
497         { USB_DEVICE(FTDI_NF_RIC_VID, FTDI_NF_RIC_PID) },
498         { USB_DEVICE(FTDI_VID, FTDI_VNHCPCUSB_D_PID) },
499         { USB_DEVICE_VER(FTDI_VID, FTDI_MTXORB_0_PID, 0x400, 0xffff) },
500         { USB_DEVICE_VER(FTDI_VID, FTDI_MTXORB_1_PID, 0x400, 0xffff) },
501         { USB_DEVICE_VER(FTDI_VID, FTDI_MTXORB_2_PID, 0x400, 0xffff) },
502         { USB_DEVICE_VER(FTDI_VID, FTDI_MTXORB_3_PID, 0x400, 0xffff) },
503         { USB_DEVICE_VER(FTDI_VID, FTDI_MTXORB_4_PID, 0x400, 0xffff) },
504         { USB_DEVICE_VER(FTDI_VID, FTDI_MTXORB_5_PID, 0x400, 0xffff) },
505         { USB_DEVICE_VER(FTDI_VID, FTDI_MTXORB_6_PID, 0x400, 0xffff) },
506         { USB_DEVICE_VER(FTDI_VID, FTDI_PERLE_ULTRAPORT_PID, 0x400, 0xffff) },
507         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2101_PID) },
508         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2102_PID) },
509         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2103_PID) },
510         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2104_PID) },
511         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2201_1_PID) },
512         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2201_2_PID) },
513         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2202_1_PID) },
514         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2202_2_PID) },
515         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2203_1_PID) },
516         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2203_2_PID) },
517         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2401_1_PID) },
518         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2401_2_PID) },
519         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2401_3_PID) },
520         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2401_4_PID) },
521         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2402_1_PID) },
522         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2402_2_PID) },
523         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2402_3_PID) },
524         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2402_4_PID) },
525         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2403_1_PID) },
526         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2403_2_PID) },
527         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2403_3_PID) },
528         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2403_4_PID) },
529         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2801_1_PID) },
530         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2801_2_PID) },
531         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2801_3_PID) },
532         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2801_4_PID) },
533         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2801_5_PID) },
534         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2801_6_PID) },
535         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2801_7_PID) },
536         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2801_8_PID) },
537         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2802_1_PID) },
538         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2802_2_PID) },
539         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2802_3_PID) },
540         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2802_4_PID) },
541         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2802_5_PID) },
542         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2802_6_PID) },
543         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2802_7_PID) },
544         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2802_8_PID) },
545         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2803_1_PID) },
546         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2803_2_PID) },
547         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2803_3_PID) },
548         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2803_4_PID) },
549         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2803_5_PID) },
550         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2803_6_PID) },
551         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2803_7_PID) },
552         { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2803_8_PID) },
553         { USB_DEVICE(IDTECH_VID, IDTECH_IDT1221U_PID) },
554         { USB_DEVICE(OCT_VID, OCT_US101_PID) },
555         { USB_DEVICE_VER(FTDI_VID, FTDI_HE_TIRA1_PID, 0x400, 0xffff) },
556         { USB_DEVICE(FTDI_VID, FTDI_USB_UIRT_PID) },
557         { USB_DEVICE(FTDI_VID, PROTEGO_SPECIAL_1) },
558         { USB_DEVICE(FTDI_VID, PROTEGO_R2X0) },
559         { USB_DEVICE(FTDI_VID, PROTEGO_SPECIAL_3) },
560         { USB_DEVICE(FTDI_VID, PROTEGO_SPECIAL_4) },
561         { USB_DEVICE(FTDI_VID, FTDI_ELV_UO100_PID) },
562         { USB_DEVICE_VER(FTDI_VID, LINX_SDMUSBQSS_PID, 0x400, 0xffff) },
563         { USB_DEVICE_VER(FTDI_VID, LINX_MASTERDEVEL2_PID, 0x400, 0xffff) },
564         { USB_DEVICE_VER(FTDI_VID, LINX_FUTURE_0_PID, 0x400, 0xffff) },
565         { USB_DEVICE_VER(FTDI_VID, LINX_FUTURE_1_PID, 0x400, 0xffff) },
566         { USB_DEVICE_VER(FTDI_VID, LINX_FUTURE_2_PID, 0x400, 0xffff) },
567         { USB_DEVICE(FTDI_VID, FTDI_CCSICDU20_0_PID) },
568         { USB_DEVICE(FTDI_VID, FTDI_CCSICDU40_1_PID) },
569         { USB_DEVICE(FTDI_VID, INSIDE_ACCESSO) },
570         { }                                             /* Terminating entry */
571 };
572
573 MODULE_DEVICE_TABLE (usb, id_table_combined);
574
575 static struct usb_driver ftdi_driver = {
576         .name =         "ftdi_sio",
577         .probe =        usb_serial_probe,
578         .disconnect =   usb_serial_disconnect,
579         .id_table =     id_table_combined,
580 };
581
582
583 /* Constants for read urb and write urb */
584 #define BUFSZ 512
585 #define PKTSZ 64
586
587 struct ftdi_private {
588         ftdi_chip_type_t chip_type;
589                                 /* type of the device, either SIO or FT8U232AM */
590         int baud_base;          /* baud base clock for divisor setting */
591         int custom_divisor;     /* custom_divisor kludge, this is for baud_base (different from what goes to the chip!) */
592         __u16 last_set_data_urb_value ;
593                                 /* the last data state set - needed for doing a break */
594         int write_offset;       /* This is the offset in the usb data block to write the serial data - 
595                                  * it is different between devices
596                                  */
597         int flags;              /* some ASYNC_xxxx flags are supported */
598         unsigned long last_dtr_rts;     /* saved modem control outputs */
599         wait_queue_head_t delta_msr_wait; /* Used for TIOCMIWAIT */
600         char prev_status, diff_status;        /* Used for TIOCMIWAIT */
601
602         int force_baud;         /* if non-zero, force the baud rate to this value */
603         int force_rtscts;       /* if non-zero, force RTS-CTS to always be enabled */
604 };
605
606 /* Used for TIOCMIWAIT */
607 #define FTDI_STATUS_B0_MASK     (FTDI_RS0_CTS | FTDI_RS0_DSR | FTDI_RS0_RI | FTDI_RS0_RLSD)
608 #define FTDI_STATUS_B1_MASK     (FTDI_RS_BI)
609 /* End TIOCMIWAIT */
610
611 #define FTDI_IMPL_ASYNC_FLAGS = ( ASYNC_SPD_HI | ASYNC_SPD_VHI \
612  ASYNC_SPD_CUST | ASYNC_SPD_SHI | ASYNC_SPD_WARP )
613
614 /* function prototypes for a FTDI serial converter */
615 static int  ftdi_SIO_startup            (struct usb_serial *serial);
616 static int  ftdi_8U232AM_startup        (struct usb_serial *serial);
617 static int  ftdi_FT232BM_startup        (struct usb_serial *serial);
618 static int  ftdi_USB_UIRT_startup       (struct usb_serial *serial);
619 static int  ftdi_HE_TIRA1_startup       (struct usb_serial *serial);
620 static void ftdi_shutdown               (struct usb_serial *serial);
621 static int  ftdi_open                   (struct usb_serial_port *port, struct file *filp);
622 static void ftdi_close                  (struct usb_serial_port *port, struct file *filp);
623 static int  ftdi_write                  (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count);
624 static int  ftdi_write_room             (struct usb_serial_port *port);
625 static int  ftdi_chars_in_buffer        (struct usb_serial_port *port);
626 static void ftdi_write_bulk_callback    (struct urb *urb, struct pt_regs *regs);
627 static void ftdi_read_bulk_callback     (struct urb *urb, struct pt_regs *regs);
628 static void ftdi_set_termios            (struct usb_serial_port *port, struct termios * old);
629 static int  ftdi_tiocmget               (struct usb_serial_port *port, struct file *file);
630 static int  ftdi_tiocmset               (struct usb_serial_port *port, struct file * file, unsigned int set, unsigned int clear);
631 static int  ftdi_ioctl                  (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
632 static void ftdi_break_ctl              (struct usb_serial_port *port, int break_state );
633 static void ftdi_throttle               (struct usb_serial_port *port);
634 static void ftdi_unthrottle             (struct usb_serial_port *port);
635
636 static unsigned short int ftdi_232am_baud_base_to_divisor (int baud, int base);
637 static unsigned short int ftdi_232am_baud_to_divisor (int baud);
638 static __u32 ftdi_232bm_baud_base_to_divisor (int baud, int base);
639 static __u32 ftdi_232bm_baud_to_divisor (int baud);
640
641 static struct usb_serial_device_type ftdi_SIO_device = {
642         .owner =                THIS_MODULE,
643         .name =                 "FTDI SIO",
644         .id_table =             id_table_sio,
645         .num_interrupt_in =     0,
646         .num_bulk_in =          1,
647         .num_bulk_out =         1,
648         .num_ports =            1,
649         .open =                 ftdi_open,
650         .close =                ftdi_close,
651         .throttle =             ftdi_throttle,
652         .unthrottle =           ftdi_unthrottle,
653         .write =                ftdi_write,
654         .write_room =           ftdi_write_room,
655         .chars_in_buffer =      ftdi_chars_in_buffer,
656         .read_bulk_callback =   ftdi_read_bulk_callback,
657         .write_bulk_callback =  ftdi_write_bulk_callback,
658         .tiocmget =             ftdi_tiocmget,
659         .tiocmset =             ftdi_tiocmset,
660         .ioctl =                ftdi_ioctl,
661         .set_termios =          ftdi_set_termios,
662         .break_ctl =            ftdi_break_ctl,
663         .attach =               ftdi_SIO_startup,
664         .shutdown =             ftdi_shutdown,
665 };
666
667 static struct usb_serial_device_type ftdi_8U232AM_device = {
668         .owner =                THIS_MODULE,
669         .name =                 "FTDI 8U232AM Compatible",
670         .id_table =             id_table_8U232AM,
671         .num_interrupt_in =     0,
672         .num_bulk_in =          1,
673         .num_bulk_out =         1,
674         .num_ports =            1,
675         .open =                 ftdi_open,
676         .close =                ftdi_close,
677         .throttle =             ftdi_throttle,
678         .unthrottle =           ftdi_unthrottle,
679         .write =                ftdi_write,
680         .write_room =           ftdi_write_room,
681         .chars_in_buffer =      ftdi_chars_in_buffer,
682         .read_bulk_callback =   ftdi_read_bulk_callback,
683         .write_bulk_callback =  ftdi_write_bulk_callback,
684         .tiocmget =             ftdi_tiocmget,
685         .tiocmset =             ftdi_tiocmset,
686         .ioctl =                ftdi_ioctl,
687         .set_termios =          ftdi_set_termios,
688         .break_ctl =            ftdi_break_ctl,
689         .attach =               ftdi_8U232AM_startup,
690         .shutdown =             ftdi_shutdown,
691 };
692
693 static struct usb_serial_device_type ftdi_FT232BM_device = {
694         .owner =                THIS_MODULE,
695         .name =                 "FTDI FT232BM Compatible",
696         .id_table =             id_table_FT232BM,
697         .num_interrupt_in =     0,
698         .num_bulk_in =          1,
699         .num_bulk_out =         1,
700         .num_ports =            1,
701         .open =                 ftdi_open,
702         .close =                ftdi_close,
703         .throttle =             ftdi_throttle,
704         .unthrottle =           ftdi_unthrottle,
705         .write =                ftdi_write,
706         .write_room =           ftdi_write_room,
707         .chars_in_buffer =      ftdi_chars_in_buffer,
708         .read_bulk_callback =   ftdi_read_bulk_callback,
709         .write_bulk_callback =  ftdi_write_bulk_callback,
710         .tiocmget =             ftdi_tiocmget,
711         .tiocmset =             ftdi_tiocmset,
712         .ioctl =                ftdi_ioctl,
713         .set_termios =          ftdi_set_termios,
714         .break_ctl =            ftdi_break_ctl,
715         .attach =               ftdi_FT232BM_startup,
716         .shutdown =             ftdi_shutdown,
717 };
718
719 static struct usb_serial_device_type ftdi_USB_UIRT_device = {
720         .owner =                THIS_MODULE,
721         .name =                 "USB-UIRT Infrared Tranceiver",
722         .id_table =             id_table_USB_UIRT,
723         .num_interrupt_in =     0,
724         .num_bulk_in =          1,
725         .num_bulk_out =         1,
726         .num_ports =            1,
727         .open =                 ftdi_open,
728         .close =                ftdi_close,
729         .throttle =             ftdi_throttle,
730         .unthrottle =           ftdi_unthrottle,
731         .write =                ftdi_write,
732         .write_room =           ftdi_write_room,
733         .chars_in_buffer =      ftdi_chars_in_buffer,
734         .read_bulk_callback =   ftdi_read_bulk_callback,
735         .write_bulk_callback =  ftdi_write_bulk_callback,
736         .tiocmget =             ftdi_tiocmget,
737         .tiocmset =             ftdi_tiocmset,
738         .ioctl =                ftdi_ioctl,
739         .set_termios =          ftdi_set_termios,
740         .break_ctl =            ftdi_break_ctl,
741         .attach =               ftdi_USB_UIRT_startup,
742         .shutdown =             ftdi_shutdown,
743 };
744
745 /* The TIRA1 is based on a  FT232BM which requires a fixed baud rate of 100000
746  * and which requires RTS-CTS to be enabled. */
747 static struct usb_serial_device_type ftdi_HE_TIRA1_device = {
748         .owner =                THIS_MODULE,
749         .name =                 "Home-Electronics TIRA-1 IR Transceiver",
750         .id_table =             id_table_HE_TIRA1,
751         .num_interrupt_in =     0,
752         .num_bulk_in =          1,
753         .num_bulk_out =         1,
754         .num_ports =            1,
755         .open =                 ftdi_open,
756         .close =                ftdi_close,
757         .throttle =             ftdi_throttle,
758         .unthrottle =           ftdi_unthrottle,
759         .write =                ftdi_write,
760         .write_room =           ftdi_write_room,
761         .chars_in_buffer =      ftdi_chars_in_buffer,
762         .read_bulk_callback =   ftdi_read_bulk_callback,
763         .write_bulk_callback =  ftdi_write_bulk_callback,
764         .tiocmget =             ftdi_tiocmget,
765         .tiocmset =             ftdi_tiocmset,
766         .ioctl =                ftdi_ioctl,
767         .set_termios =          ftdi_set_termios,
768         .break_ctl =            ftdi_break_ctl,
769         .attach =               ftdi_HE_TIRA1_startup,
770         .shutdown =             ftdi_shutdown,
771 };
772
773
774
775 #define WDR_TIMEOUT (HZ * 5 ) /* default urb timeout */
776
777 /* High and low are for DTR, RTS etc etc */
778 #define HIGH 1
779 #define LOW 0
780
781 /*
782  * ***************************************************************************
783  * Utlity functions
784  * ***************************************************************************
785  */
786
787 static unsigned short int ftdi_232am_baud_base_to_divisor(int baud, int base)
788 {
789         unsigned short int divisor;
790         int divisor3 = base / 2 / baud; // divisor shifted 3 bits to the left
791         if ((divisor3 & 0x7) == 7) divisor3 ++; // round x.7/8 up to x+1
792         divisor = divisor3 >> 3;
793         divisor3 &= 0x7;
794         if (divisor3 == 1) divisor |= 0xc000; else // 0.125
795         if (divisor3 >= 4) divisor |= 0x4000; else // 0.5
796         if (divisor3 != 0) divisor |= 0x8000;      // 0.25
797         if (divisor == 1) divisor = 0;  /* special case for maximum baud rate */
798         return divisor;
799 }
800
801 static unsigned short int ftdi_232am_baud_to_divisor(int baud)
802 {
803          return(ftdi_232am_baud_base_to_divisor(baud, 48000000));
804 }
805
806 static __u32 ftdi_232bm_baud_base_to_divisor(int baud, int base)
807 {
808         static const unsigned char divfrac[8] = { 0, 3, 2, 4, 1, 5, 6, 7 };
809         __u32 divisor;
810         int divisor3 = base / 2 / baud; // divisor shifted 3 bits to the left
811         divisor = divisor3 >> 3;
812         divisor |= (__u32)divfrac[divisor3 & 0x7] << 14;
813         /* Deal with special cases for highest baud rates. */
814         if (divisor == 1) divisor = 0; else     // 1.0
815         if (divisor == 0x4001) divisor = 1;     // 1.5
816         return divisor;
817 }
818
819 static __u32 ftdi_232bm_baud_to_divisor(int baud)
820 {
821          return(ftdi_232bm_baud_base_to_divisor(baud, 48000000));
822 }
823
824 static int set_rts(struct usb_serial_port *port, int high_or_low)
825 {
826         struct ftdi_private *priv = usb_get_serial_port_data(port);
827         char *buf;
828         unsigned ftdi_high_or_low;
829         int rv;
830         
831         buf = kmalloc(1, GFP_NOIO);
832         if (!buf)
833                 return -ENOMEM;
834         
835         if (high_or_low) {
836                 ftdi_high_or_low = FTDI_SIO_SET_RTS_HIGH;
837                 priv->last_dtr_rts |= TIOCM_RTS;
838         } else {
839                 ftdi_high_or_low = FTDI_SIO_SET_RTS_LOW;
840                 priv->last_dtr_rts &= ~TIOCM_RTS;
841         }
842         rv = usb_control_msg(port->serial->dev,
843                                usb_sndctrlpipe(port->serial->dev, 0),
844                                FTDI_SIO_SET_MODEM_CTRL_REQUEST, 
845                                FTDI_SIO_SET_MODEM_CTRL_REQUEST_TYPE,
846                                ftdi_high_or_low, 0, 
847                                buf, 0, WDR_TIMEOUT);
848
849         kfree(buf);
850         return rv;
851 }
852
853
854 static int set_dtr(struct usb_serial_port *port, int high_or_low)
855 {
856         struct ftdi_private *priv = usb_get_serial_port_data(port);
857         char *buf;
858         unsigned ftdi_high_or_low;
859         int rv;
860         
861         buf = kmalloc(1, GFP_NOIO);
862         if (!buf)
863                 return -ENOMEM;
864
865         if (high_or_low) {
866                 ftdi_high_or_low = FTDI_SIO_SET_DTR_HIGH;
867                 priv->last_dtr_rts |= TIOCM_DTR;
868         } else {
869                 ftdi_high_or_low = FTDI_SIO_SET_DTR_LOW;
870                 priv->last_dtr_rts &= ~TIOCM_DTR;
871         }
872         rv = usb_control_msg(port->serial->dev,
873                                usb_sndctrlpipe(port->serial->dev, 0),
874                                FTDI_SIO_SET_MODEM_CTRL_REQUEST, 
875                                FTDI_SIO_SET_MODEM_CTRL_REQUEST_TYPE,
876                                ftdi_high_or_low, 0, 
877                                buf, 0, WDR_TIMEOUT);
878
879         kfree(buf);
880         return rv;
881 }
882
883
884 static __u32 get_ftdi_divisor(struct usb_serial_port * port);
885
886
887 static int change_speed(struct usb_serial_port *port)
888 {
889         char *buf;
890         __u16 urb_value;
891         __u16 urb_index;
892         __u32 urb_index_value;
893         int rv;
894
895         buf = kmalloc(1, GFP_NOIO);
896         if (!buf)
897                 return -ENOMEM;
898
899         urb_index_value = get_ftdi_divisor(port);
900         urb_value = (__u16)urb_index_value;
901         urb_index = (__u16)(urb_index_value >> 16);
902         
903         rv = usb_control_msg(port->serial->dev,
904                             usb_sndctrlpipe(port->serial->dev, 0),
905                             FTDI_SIO_SET_BAUDRATE_REQUEST,
906                             FTDI_SIO_SET_BAUDRATE_REQUEST_TYPE,
907                             urb_value, urb_index,
908                             buf, 0, 100);
909
910         kfree(buf);
911         return rv;
912 }
913
914
915 static __u32 get_ftdi_divisor(struct usb_serial_port * port)
916 { /* get_ftdi_divisor */
917         struct ftdi_private *priv = usb_get_serial_port_data(port);
918         __u32 div_value = 0;
919         int div_okay = 1;
920         char *chip_name = "";
921         int baud;
922
923         /*
924          * The logic involved in setting the baudrate can be cleanly split in 3 steps.
925          * Obtaining the actual baud rate is a little tricky since unix traditionally
926          * somehow ignored the possibility to set non-standard baud rates.
927          * 1. Standard baud rates are set in tty->termios->c_cflag
928          * 2. If these are not enough, you can set any speed using alt_speed as follows:
929          *    - set tty->termios->c_cflag speed to B38400
930          *    - set your real speed in tty->alt_speed; it gets ignored when
931          *      alt_speed==0, (or)
932          *    - call TIOCSSERIAL ioctl with (struct serial_struct) set as follows:
933          *      flags & ASYNC_SPD_MASK == ASYNC_SPD_[HI, VHI, SHI, WARP], this just
934          *      sets alt_speed to (HI: 57600, VHI: 115200, SHI: 230400, WARP: 460800)
935          * ** Steps 1, 2 are done courtesy of tty_get_baud_rate
936          * 3. You can also set baud rate by setting custom divisor as follows
937          *    - set tty->termios->c_cflag speed to B38400
938          *    - call TIOCSSERIAL ioctl with (struct serial_struct) set as follows:
939          *      o flags & ASYNC_SPD_MASK == ASYNC_SPD_CUST
940          *      o custom_divisor set to baud_base / your_new_baudrate
941          * ** Step 3 is done courtesy of code borrowed from serial.c - I should really
942          *    spend some time and separate+move this common code to serial.c, it is
943          *    replicated in nearly every serial driver you see.
944          */
945
946         /* 1. Get the baud rate from the tty settings, this observes alt_speed hack */
947
948         baud = tty_get_baud_rate(port->tty);
949         dbg("%s - tty_get_baud_rate reports speed %d", __FUNCTION__, baud);
950
951         /* 2. Observe async-compatible custom_divisor hack, update baudrate if needed */
952
953         if (baud == 38400 &&
954             ((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST) &&
955              (priv->custom_divisor)) {
956                 baud = priv->baud_base / priv->custom_divisor;
957                 dbg("%s - custom divisor %d sets baud rate to %d", __FUNCTION__, priv->custom_divisor, baud);
958         }
959
960         /* 3. Convert baudrate to device-specific divisor */
961
962         if (!baud) baud = 9600; 
963         switch(priv->chip_type) {
964         case SIO: /* SIO chip */
965                 chip_name = "SIO";
966                 switch(baud) {
967                 case 300: div_value = ftdi_sio_b300; break;
968                 case 600: div_value = ftdi_sio_b600; break;
969                 case 1200: div_value = ftdi_sio_b1200; break;
970                 case 2400: div_value = ftdi_sio_b2400; break;
971                 case 4800: div_value = ftdi_sio_b4800; break;
972                 case 9600: div_value = ftdi_sio_b9600; break;
973                 case 19200: div_value = ftdi_sio_b19200; break;
974                 case 38400: div_value = ftdi_sio_b38400; break;
975                 case 57600: div_value = ftdi_sio_b57600;  break;
976                 case 115200: div_value = ftdi_sio_b115200; break;
977                 } /* baud */
978                 if (div_value == 0) {
979                         dbg("%s - Baudrate (%d) requested is not supported", __FUNCTION__,  baud);
980                         div_value = ftdi_sio_b9600;
981                         div_okay = 0;
982                 }
983                 break;
984         case FT8U232AM: /* 8U232AM chip */
985                 chip_name = "FT8U232AM";
986                 if (baud <= 3000000) {
987                         div_value = ftdi_232am_baud_to_divisor(baud);
988                 } else {
989                         dbg("%s - Baud rate too high!", __FUNCTION__);
990                         div_value = ftdi_232am_baud_to_divisor(9600);
991                         div_okay = 0;
992                 }
993                 break;
994         case FT232BM: /* FT232BM chip */
995                 chip_name = "FT232BM";
996                 if (baud <= 3000000) {
997                         div_value = ftdi_232bm_baud_to_divisor(baud);
998                 } else {
999                         dbg("%s - Baud rate too high!", __FUNCTION__);
1000                         div_value = ftdi_232bm_baud_to_divisor(9600);
1001                         div_okay = 0;
1002                 }
1003                 break;
1004         } /* priv->chip_type */
1005
1006         if (div_okay) {
1007                 dbg("%s - Baud rate set to %d (divisor 0x%lX) on chip %s",
1008                         __FUNCTION__, baud, (unsigned long)div_value, chip_name);
1009         }
1010
1011         return(div_value);
1012 }
1013
1014
1015 static int get_serial_info(struct usb_serial_port * port, struct serial_struct * retinfo)
1016 {
1017         struct ftdi_private *priv = usb_get_serial_port_data(port);
1018         struct serial_struct tmp;
1019
1020         if (!retinfo)
1021                 return -EFAULT;
1022         memset(&tmp, 0, sizeof(tmp));
1023         tmp.flags = priv->flags;
1024         tmp.baud_base = priv->baud_base;
1025         tmp.custom_divisor = priv->custom_divisor;
1026         if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1027                 return -EFAULT;
1028         return 0;
1029 } /* get_serial_info */
1030
1031
1032 static int set_serial_info(struct usb_serial_port * port, struct serial_struct * newinfo)
1033 { /* set_serial_info */
1034         struct ftdi_private *priv = usb_get_serial_port_data(port);
1035         struct serial_struct new_serial;
1036         struct ftdi_private old_priv;
1037
1038         if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
1039                 return -EFAULT;
1040         old_priv = * priv;
1041
1042         /* Do error checking and permission checking */
1043
1044         if (!capable(CAP_SYS_ADMIN)) {
1045                 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
1046                      (priv->flags & ~ASYNC_USR_MASK)))
1047                         return -EPERM;
1048                 priv->flags = ((priv->flags & ~ASYNC_USR_MASK) |
1049                                (new_serial.flags & ASYNC_USR_MASK));
1050                 priv->custom_divisor = new_serial.custom_divisor;
1051                 goto check_and_exit;
1052         }
1053
1054         if ((new_serial.baud_base != priv->baud_base) ||
1055             (new_serial.baud_base < 9600))
1056                 return -EINVAL;
1057
1058         /* Make the changes - these are privileged changes! */
1059
1060         priv->flags = ((priv->flags & ~ASYNC_FLAGS) |
1061                        (new_serial.flags & ASYNC_FLAGS));       
1062         priv->custom_divisor = new_serial.custom_divisor;
1063
1064         port->tty->low_latency = (priv->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1065
1066 check_and_exit:
1067         if ((old_priv.flags & ASYNC_SPD_MASK) !=
1068              (priv->flags & ASYNC_SPD_MASK)) {
1069                 if ((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1070                         port->tty->alt_speed = 57600;
1071                 else if ((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1072                         port->tty->alt_speed = 115200;
1073                 else if ((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1074                         port->tty->alt_speed = 230400;
1075                 else if ((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1076                         port->tty->alt_speed = 460800;
1077                 else
1078                         port->tty->alt_speed = 0;
1079         }
1080         if (((old_priv.flags & ASYNC_SPD_MASK) !=
1081              (priv->flags & ASYNC_SPD_MASK)) ||
1082             (((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST) &&
1083              (old_priv.custom_divisor != priv->custom_divisor))) {
1084                 change_speed(port);
1085         }
1086         
1087         return (0);
1088
1089 } /* set_serial_info */
1090
1091 /*
1092  * ***************************************************************************
1093  * FTDI driver specific functions
1094  * ***************************************************************************
1095  */
1096
1097 /* Common startup subroutine */
1098 /* Called from ftdi_SIO_startup, etc. */
1099 static int ftdi_common_startup (struct usb_serial *serial)
1100 {
1101         struct usb_serial_port *port = serial->port[0];
1102         struct ftdi_private *priv;
1103         
1104         dbg("%s",__FUNCTION__);
1105
1106         priv = kmalloc(sizeof(struct ftdi_private), GFP_KERNEL);
1107         if (!priv){
1108                 err("%s- kmalloc(%Zd) failed.", __FUNCTION__, sizeof(struct ftdi_private));
1109                 return -ENOMEM;
1110         }
1111         memset(priv, 0, sizeof(*priv));
1112
1113         init_waitqueue_head(&priv->delta_msr_wait);
1114         /* This will push the characters through immediately rather
1115            than queue a task to deliver them */
1116         priv->flags = ASYNC_LOW_LATENCY;
1117
1118         /* Increase the size of read buffers */
1119         if (port->bulk_in_buffer) {
1120                 kfree (port->bulk_in_buffer);
1121         }
1122         port->bulk_in_buffer = kmalloc (BUFSZ, GFP_KERNEL);
1123         if (!port->bulk_in_buffer) {
1124                 kfree (priv);
1125                 return -ENOMEM;
1126         }
1127         if (port->read_urb) {
1128                 port->read_urb->transfer_buffer = port->bulk_in_buffer;
1129                 port->read_urb->transfer_buffer_length = BUFSZ;
1130         }
1131
1132         /* Free port's existing write urb and transfer buffer. */
1133         if (port->write_urb) {
1134                 usb_free_urb (port->write_urb);
1135                 port->write_urb = NULL;
1136         }
1137         if (port->bulk_out_buffer) {
1138                 kfree (port->bulk_out_buffer);
1139                 port->bulk_out_buffer = NULL;
1140         }
1141
1142         usb_set_serial_port_data(serial->port[0], priv);
1143         
1144         return (0);
1145 }
1146
1147
1148 /* Startup for the SIO chip */
1149 /* Called from usbserial:serial_probe */
1150 static int ftdi_SIO_startup (struct usb_serial *serial)
1151 {
1152         struct ftdi_private *priv;
1153         int err;
1154
1155         dbg("%s",__FUNCTION__);
1156
1157         err = ftdi_common_startup(serial);
1158         if (err){
1159                 return (err);
1160         }
1161
1162         priv = usb_get_serial_port_data(serial->port[0]);
1163         priv->chip_type = SIO;
1164         priv->baud_base = 12000000 / 16;
1165         priv->write_offset = 1;
1166         
1167         return (0);
1168 }
1169
1170 /* Startup for the 8U232AM chip */
1171 /* Called from usbserial:serial_probe */
1172 static int ftdi_8U232AM_startup (struct usb_serial *serial)
1173 { /* ftdi_8U232AM_startup */
1174         struct ftdi_private *priv;
1175         int err;
1176
1177         dbg("%s",__FUNCTION__);
1178         err = ftdi_common_startup(serial);
1179         if (err){
1180                 return (err);
1181         }
1182
1183         priv = usb_get_serial_port_data(serial->port[0]);
1184         priv->chip_type = FT8U232AM;
1185         priv->baud_base = 48000000 / 2; /* Would be / 16, but FTDI supports 0.125, 0.25 and 0.5 divisor fractions! */
1186         
1187         return (0);
1188 } /* ftdi_8U232AM_startup */
1189
1190 /* Startup for the FT232BM chip */
1191 /* Called from usbserial:serial_probe */
1192 static int ftdi_FT232BM_startup (struct usb_serial *serial)
1193 { /* ftdi_FT232BM_startup */
1194         struct ftdi_private *priv;
1195         int err;
1196
1197         dbg("%s",__FUNCTION__);
1198         err = ftdi_common_startup(serial);
1199         if (err){
1200                 return (err);
1201         }
1202
1203         priv = usb_get_serial_port_data(serial->port[0]);
1204         priv->chip_type = FT232BM;
1205         priv->baud_base = 48000000 / 2; /* Would be / 16, but FT232BM supports multiple of 0.125 divisor fractions! */
1206         
1207         return (0);
1208 } /* ftdi_FT232BM_startup */
1209
1210 /* Startup for the USB-UIRT device, which requires hardwired baudrate (38400 gets mapped to 312500) */
1211 /* Called from usbserial:serial_probe */
1212 static int ftdi_USB_UIRT_startup (struct usb_serial *serial)
1213 { /* ftdi_USB_UIRT_startup */
1214         struct ftdi_private *priv;
1215         int err;
1216
1217         dbg("%s",__FUNCTION__);
1218         err = ftdi_8U232AM_startup(serial);
1219         if (err){
1220                 return (err);
1221         }
1222
1223         priv = usb_get_serial_port_data(serial->port[0]);
1224         priv->flags |= ASYNC_SPD_CUST;
1225         priv->custom_divisor = 77;
1226         priv->force_baud = B38400;
1227         
1228         return (0);
1229 } /* ftdi_USB_UIRT_startup */
1230
1231 /* Startup for the HE-TIRA1 device, which requires hardwired
1232  * baudrate (38400 gets mapped to 100000) */
1233 static int ftdi_HE_TIRA1_startup (struct usb_serial *serial)
1234 { /* ftdi_HE_TIRA1_startup */
1235         struct ftdi_private *priv;
1236         int err;
1237
1238         dbg("%s",__FUNCTION__);
1239         err = ftdi_FT232BM_startup(serial);
1240         if (err){
1241                 return (err);
1242         }
1243
1244         priv = usb_get_serial_port_data(serial->port[0]);
1245         priv->flags |= ASYNC_SPD_CUST;
1246         priv->custom_divisor = 240;
1247         priv->force_baud = B38400;
1248         priv->force_rtscts = 1;
1249         
1250         return (0);
1251 } /* ftdi_HE_TIRA1_startup */
1252
1253
1254 /* ftdi_shutdown is called from usbserial:usb_serial_disconnect 
1255  *   it is called when the usb device is disconnected
1256  *
1257  *   usbserial:usb_serial_disconnect
1258  *      calls __serial_close for each open of the port
1259  *      shutdown is called then (ie ftdi_shutdown)
1260  */
1261
1262
1263 static void ftdi_shutdown (struct usb_serial *serial)
1264 { /* ftdi_shutdown */
1265         
1266         struct usb_serial_port *port = serial->port[0];
1267         struct ftdi_private *priv = usb_get_serial_port_data(port);
1268
1269         dbg("%s", __FUNCTION__);
1270
1271         /* all open ports are closed at this point 
1272          *    (by usbserial.c:__serial_close, which calls ftdi_close)  
1273          */
1274
1275         if (priv) {
1276                 kfree(priv);
1277                 usb_set_serial_port_data(port, NULL);
1278         }
1279 } /* ftdi_shutdown */
1280
1281
1282 static int  ftdi_open (struct usb_serial_port *port, struct file *filp)
1283 { /* ftdi_open */
1284         struct termios tmp_termios;
1285         struct usb_serial *serial = port->serial;
1286         struct ftdi_private *priv = usb_get_serial_port_data(port);
1287         
1288         int result = 0;
1289         char buf[1]; /* Needed for the usb_control_msg I think */
1290
1291         dbg("%s", __FUNCTION__);
1292
1293
1294         port->tty->low_latency = (priv->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1295
1296         /* No error checking for this (will get errors later anyway) */
1297         /* See ftdi_sio.h for description of what is reset */
1298         usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1299                         FTDI_SIO_RESET_REQUEST, FTDI_SIO_RESET_REQUEST_TYPE, 
1300                         FTDI_SIO_RESET_SIO, 
1301                         0, buf, 0, WDR_TIMEOUT);
1302
1303         /* Termios defaults are set by usb_serial_init. We don't change
1304            port->tty->termios - this would loose speed settings, etc.
1305            This is same behaviour as serial.c/rs_open() - Kuba */
1306
1307         /* ftdi_set_termios  will send usb control messages */
1308         ftdi_set_termios(port, &tmp_termios);
1309
1310         /* FIXME: Flow control might be enabled, so it should be checked -
1311            we have no control of defaults! */
1312         /* Turn on RTS and DTR since we are not flow controlling by default */
1313         if (set_dtr(port, HIGH) < 0) {
1314                 err("%s Error from DTR HIGH urb", __FUNCTION__);
1315         }
1316         if (set_rts(port, HIGH) < 0){
1317                 err("%s Error from RTS HIGH urb", __FUNCTION__);
1318         }
1319
1320         /* Start reading from the device */
1321         usb_fill_bulk_urb(port->read_urb, serial->dev, 
1322                       usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
1323                       port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
1324                       ftdi_read_bulk_callback, port);
1325         result = usb_submit_urb(port->read_urb, GFP_KERNEL);
1326         if (result)
1327                 err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
1328
1329
1330         return result;
1331 } /* ftdi_open */
1332
1333
1334
1335 /* 
1336  * usbserial:__serial_close  only calls ftdi_close if the point is open
1337  *
1338  *   This only gets called when it is the last close
1339  *   
1340  *   
1341  */
1342
1343 static void ftdi_close (struct usb_serial_port *port, struct file *filp)
1344 { /* ftdi_close */
1345         struct usb_serial *serial;
1346         unsigned int c_cflag = port->tty->termios->c_cflag;
1347         char buf[1];
1348
1349         dbg("%s", __FUNCTION__);
1350
1351         serial = get_usb_serial ( port, __FUNCTION__);
1352         if (!serial)
1353                 return;
1354
1355         if (serial->dev) {
1356                 if (c_cflag & HUPCL){
1357                         /* Disable flow control */
1358                         if (usb_control_msg(serial->dev, 
1359                                             usb_sndctrlpipe(serial->dev, 0),
1360                                             FTDI_SIO_SET_FLOW_CTRL_REQUEST,
1361                                             FTDI_SIO_SET_FLOW_CTRL_REQUEST_TYPE,
1362                                             0, 0, buf, 0, WDR_TIMEOUT) < 0) {
1363                                 err("error from flowcontrol urb");
1364                         }           
1365
1366                         /* drop DTR */
1367                         if (set_dtr(port, LOW) < 0){
1368                                 err("Error from DTR LOW urb");
1369                         }
1370                         /* drop RTS */
1371                         if (set_rts(port, LOW) < 0) {
1372                                 err("Error from RTS LOW urb");
1373                         }
1374                 } /* Note change no line if hupcl is off */
1375                 
1376                 /* shutdown our bulk read */
1377                 if (port->read_urb) {
1378                         if (usb_unlink_urb (port->read_urb) < 0) {
1379                                 err("Error unlinking read urb");
1380                         }
1381                 }
1382
1383         } /* if (serial->dev) */
1384
1385
1386 } /* ftdi_close */
1387
1388
1389   
1390 /* The SIO requires the first byte to have:
1391  *  B0 1
1392  *  B1 0
1393  *  B2..7 length of message excluding byte 0
1394  *
1395  * The new devices do not require this byte
1396  */
1397 static int ftdi_write (struct usb_serial_port *port, int from_user,
1398                            const unsigned char *buf, int count)
1399 { /* ftdi_write */
1400         struct usb_serial *serial = get_usb_serial ( port, __FUNCTION__);
1401         struct ftdi_private *priv = usb_get_serial_port_data(port);
1402         struct urb *urb;
1403         unsigned char *buffer;
1404         int data_offset ;       /* will be 1 for the SIO and 0 otherwise */
1405         int status;
1406         int transfer_size;
1407
1408         dbg("%s port %d, %d bytes", __FUNCTION__, port->number, count);
1409
1410         if (count == 0) {
1411                 err("write request of 0 bytes");
1412                 return 0;
1413         }
1414         
1415         data_offset = priv->write_offset;
1416         dbg("data_offset set to %d",data_offset);
1417
1418         /* Determine total transfer size */
1419         transfer_size = count;
1420         if (data_offset > 0) {
1421                 /* Original sio needs control bytes too... */
1422                 transfer_size += (data_offset *
1423                                 ((count + (PKTSZ - 1 - data_offset)) /
1424                                  (PKTSZ - data_offset)));
1425         }
1426
1427         buffer = kmalloc (transfer_size, GFP_ATOMIC);
1428         if (!buffer) {
1429                 err("%s ran out of kernel memory for urb ...", __FUNCTION__);
1430                 return -ENOMEM;
1431         }
1432
1433         urb = usb_alloc_urb(0, GFP_ATOMIC);
1434         if (!urb) {
1435                 err("%s - no more free urbs", __FUNCTION__);
1436                 kfree (buffer);
1437                 return -ENOMEM;
1438         }
1439
1440         /* Copy data */
1441         if (data_offset > 0) {
1442                 /* Original sio requires control byte at start of each packet. */
1443                 int user_pktsz = PKTSZ - data_offset;
1444                 int todo = count;
1445                 unsigned char *first_byte = buffer;
1446                 const unsigned char *current_position = buf;
1447
1448                 while (todo > 0) {
1449                         if (user_pktsz > todo) {
1450                                 user_pktsz = todo;
1451                         }
1452                         /* Write the control byte at the front of the packet*/
1453                         *first_byte = 1 | ((user_pktsz) << 2); 
1454                         /* Copy data for packet */
1455                         if (from_user) {
1456                                 if (copy_from_user (first_byte + data_offset,
1457                                                     current_position, user_pktsz)){
1458                                         kfree (buffer);
1459                                         usb_free_urb (urb);
1460                                         return -EFAULT;
1461                                 }
1462                         } else {
1463                                 memcpy (first_byte + data_offset,
1464                                         current_position, user_pktsz);
1465                         }
1466                         first_byte += user_pktsz + data_offset;
1467                         current_position += user_pktsz;
1468                         todo -= user_pktsz;
1469                 }
1470         } else {
1471                 /* No control byte required. */
1472                 /* Copy in the data to send */
1473                 if (from_user) {
1474                         if (copy_from_user (buffer, buf, count)) {
1475                                 kfree (buffer);
1476                                 usb_free_urb (urb);
1477                                 return -EFAULT;
1478                         }
1479                 } else {
1480                         memcpy (buffer, buf, count);
1481                 }
1482         }
1483
1484         usb_serial_debug_data (__FILE__, __FUNCTION__, transfer_size, buffer);
1485
1486         /* fill the buffer and send it */
1487         usb_fill_bulk_urb(urb, serial->dev, 
1488                       usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
1489                       buffer, transfer_size,
1490                       ftdi_write_bulk_callback, port);
1491
1492         status = usb_submit_urb(urb, GFP_ATOMIC);
1493         if (status) {
1494                 err("%s - failed submitting write urb, error %d", __FUNCTION__, status);
1495                 count = status;
1496         }
1497
1498         /* we are done with this urb, so let the host driver
1499          * really free it when it is finished with it */
1500         usb_free_urb (urb);
1501
1502         dbg("%s write returning: %d", __FUNCTION__, count);
1503         return count;
1504 } /* ftdi_write */
1505
1506
1507 /* This function may get called when the device is closed */
1508
1509 static void ftdi_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
1510 {
1511         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1512
1513         /* free up the transfer buffer, as usb_free_urb() does not do this */
1514         kfree (urb->transfer_buffer);
1515
1516         if (port_paranoia_check (port, __FUNCTION__))
1517                 return;
1518         
1519         dbg("%s - port %d", __FUNCTION__, port->number);
1520         
1521         if (urb->status) {
1522                 dbg("nonzero write bulk status received: %d", urb->status);
1523                 return;
1524         }
1525
1526         schedule_work(&port->work);
1527 } /* ftdi_write_bulk_callback */
1528
1529
1530 static int ftdi_write_room( struct usb_serial_port *port )
1531 {
1532         dbg("%s - port %d", __FUNCTION__, port->number);
1533
1534         /*
1535          * We really can take anything the user throws at us
1536          * but let's pick a nice big number to tell the tty
1537          * layer that we have lots of free space
1538          */
1539         return 2048;
1540 } /* ftdi_write_room */
1541
1542
1543 static int ftdi_chars_in_buffer (struct usb_serial_port *port)
1544 { /* ftdi_chars_in_buffer */
1545         dbg("%s - port %d", __FUNCTION__, port->number);
1546
1547         /* 
1548          * We can't really account for how much data we
1549          * have sent out, but hasn't made it through to the
1550          * device, so just tell the tty layer that everything
1551          * is flushed.
1552          */
1553         return 0;
1554 } /* ftdi_chars_in_buffer */
1555
1556
1557
1558 static void ftdi_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
1559 { /* ftdi_read_bulk_callback */
1560         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1561         struct usb_serial *serial;
1562         struct tty_struct *tty;
1563         struct ftdi_private *priv;
1564         char error_flag;
1565         unsigned char *data = urb->transfer_buffer;
1566
1567         int i;
1568         int result;
1569         int need_flip;
1570         int packet_offset;
1571
1572         if (urb->number_of_packets > 0) {
1573                 err("%s transfer_buffer_length %d actual_length %d number of packets %d",__FUNCTION__,
1574                     urb->transfer_buffer_length, urb->actual_length, urb->number_of_packets );
1575                 err("%s transfer_flags %x ", __FUNCTION__,urb->transfer_flags );
1576         }
1577
1578         dbg("%s", __FUNCTION__);
1579
1580         if (port_paranoia_check (port, __FUNCTION__)) {
1581                 return;
1582         }
1583         if (port->open_count <= 0)
1584                 return;
1585
1586         serial = get_usb_serial(port,__FUNCTION__);
1587         if (!serial){
1588                 dbg("%s - bad serial pointer - exiting",__FUNCTION__);
1589                 return;
1590         }
1591         
1592         tty = port->tty;
1593         if (!tty) {
1594                 dbg("%s - bad tty pointer - exiting",__FUNCTION__);
1595                 return;
1596         }
1597
1598         priv = usb_get_serial_port_data(port);
1599
1600         if (urb->status) {
1601                 /* This will happen at close every time so it is a dbg not an err */
1602                 dbg("(this is ok on close) nonzero read bulk status received: %d", urb->status);
1603                 return;
1604         }
1605
1606         /* The first two bytes of every read packet are status */
1607         if (urb->actual_length > 2) {
1608                 usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
1609         } else {
1610                 dbg("Status only: %03oo %03oo",data[0],data[1]);
1611         }
1612
1613
1614         /* TO DO -- check for hung up line and handle appropriately: */
1615         /*   send hangup  */
1616         /* See acm.c - you do a tty_hangup  - eg tty_hangup(tty) */
1617         /* if CD is dropped and the line is not CLOCAL then we should hangup */
1618
1619         need_flip = 0;
1620         for (packet_offset=0; packet_offset < urb->actual_length; packet_offset += PKTSZ) {
1621                 /* Compare new line status to the old one, signal if different */
1622                 if (priv != NULL) {
1623                         char new_status = data[packet_offset+0] & FTDI_STATUS_B0_MASK;
1624                         if (new_status != priv->prev_status) {
1625                                 priv->diff_status |= new_status ^ priv->prev_status;
1626                                 wake_up_interruptible(&priv->delta_msr_wait);
1627                                 priv->prev_status = new_status;
1628                         }
1629                 }
1630
1631                 /* Handle errors and break */
1632                 error_flag = TTY_NORMAL;
1633                 /* Although the device uses a bitmask and hence can have multiple */
1634                 /* errors on a packet - the order here sets the priority the */
1635                 /* error is returned to the tty layer  */
1636
1637                 if ( data[packet_offset+1] & FTDI_RS_OE ) {
1638                         error_flag = TTY_OVERRUN;
1639                         dbg("OVERRRUN error");
1640                 }
1641                 if ( data[packet_offset+1] & FTDI_RS_BI ) {
1642                         error_flag = TTY_BREAK;
1643                         dbg("BREAK received");
1644                 }
1645                 if ( data[packet_offset+1] & FTDI_RS_PE ) {
1646                         error_flag = TTY_PARITY;
1647                         dbg("PARITY error");
1648                 }
1649                 if ( data[packet_offset+1] & FTDI_RS_FE ) {
1650                         error_flag = TTY_FRAME;
1651                         dbg("FRAMING error");
1652                 }
1653                 if (urb->actual_length > packet_offset + 2) {
1654                         for (i = 2; (i < PKTSZ) && ((i+packet_offset) < urb->actual_length); ++i) {
1655                                 /* have to make sure we don't overflow the buffer
1656                                   with tty_insert_flip_char's */
1657                                 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
1658                                         tty_flip_buffer_push(tty);
1659                                 }
1660                                 /* Note that the error flag is duplicated for 
1661                                    every character received since we don't know
1662                                    which character it applied to */
1663                                 tty_insert_flip_char(tty, data[packet_offset+i], error_flag);
1664                         }
1665                         need_flip = 1;
1666                 }
1667
1668 #ifdef NOT_CORRECT_BUT_KEEPING_IT_FOR_NOW
1669                 /* if a parity error is detected you get status packets forever
1670                    until a character is sent without a parity error.
1671                    This doesn't work well since the application receives a never
1672                    ending stream of bad data - even though new data hasn't been sent.
1673                    Therefore I (bill) have taken this out.
1674                    However - this might make sense for framing errors and so on 
1675                    so I am leaving the code in for now.
1676                 */
1677                 else {
1678                         if (error_flag != TTY_NORMAL){
1679                                 dbg("error_flag is not normal");
1680                                 /* In this case it is just status - if that is an error send a bad character */
1681                                 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
1682                                         tty_flip_buffer_push(tty);
1683                                 }
1684                                 tty_insert_flip_char(tty, 0xff, error_flag);
1685                                 need_flip = 1;
1686                         }
1687                 }
1688 #endif
1689         } /* "for(packet_offset=0..." */
1690
1691         /* Low latency */
1692         if (need_flip) {
1693                 tty_flip_buffer_push(tty);
1694         }
1695
1696         /* if the port is closed stop trying to read */
1697         if (port->open_count > 0){
1698                 /* Continue trying to always read  */
1699                 usb_fill_bulk_urb(port->read_urb, serial->dev, 
1700                               usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
1701                               port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
1702                               ftdi_read_bulk_callback, port);
1703
1704                 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1705                 if (result)
1706                         err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
1707         }
1708
1709         return;
1710 } /* ftdi_read_bulk_callback */
1711
1712
1713 static void ftdi_break_ctl( struct usb_serial_port *port, int break_state )
1714 {
1715         struct usb_serial *serial = port->serial;
1716         struct ftdi_private *priv = usb_get_serial_port_data(port);
1717         __u16 urb_value = 0; 
1718         char buf[1];
1719         
1720         /* break_state = -1 to turn on break, and 0 to turn off break */
1721         /* see drivers/char/tty_io.c to see it used */
1722         /* last_set_data_urb_value NEVER has the break bit set in it */
1723
1724         if (break_state) {
1725                 urb_value = priv->last_set_data_urb_value | FTDI_SIO_SET_BREAK;
1726         } else {
1727                 urb_value = priv->last_set_data_urb_value; 
1728         }
1729
1730         
1731         if (usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1732                             FTDI_SIO_SET_DATA_REQUEST, 
1733                             FTDI_SIO_SET_DATA_REQUEST_TYPE,
1734                             urb_value , 0,
1735                             buf, 0, WDR_TIMEOUT) < 0) {
1736                 err("%s FAILED to enable/disable break state (state was %d)", __FUNCTION__,break_state);
1737         }          
1738
1739         dbg("%s break state is %d - urb is %d", __FUNCTION__,break_state, urb_value);
1740         
1741 }
1742
1743
1744 /* old_termios contains the original termios settings and tty->termios contains
1745  * the new setting to be used
1746  * WARNING: set_termios calls this with old_termios in kernel space
1747  */
1748
1749 static void ftdi_set_termios (struct usb_serial_port *port, struct termios *old_termios)
1750 { /* ftdi_termios */
1751         struct usb_serial *serial = port->serial;
1752         unsigned int cflag = port->tty->termios->c_cflag;
1753         struct ftdi_private *priv = usb_get_serial_port_data(port);
1754         __u16 urb_value; /* will hold the new flags */
1755         char buf[1]; /* Perhaps I should dynamically alloc this? */
1756         
1757         // Added for xon/xoff support
1758         unsigned int iflag = port->tty->termios->c_iflag;
1759         unsigned char vstop;
1760         unsigned char vstart;
1761         
1762         dbg("%s", __FUNCTION__);
1763
1764         /* Force baud rate if this device requires it, unless it is set to B0. */
1765         if (priv->force_baud && ((port->tty->termios->c_cflag & CBAUD) != B0)) {
1766                 dbg("%s: forcing baud rate for this device", __FUNCTION__);
1767                 port->tty->termios->c_cflag &= ~CBAUD;
1768                 port->tty->termios->c_cflag |= priv->force_baud;
1769         }
1770
1771         /* Force RTS-CTS if this device requires it. */
1772         if (priv->force_rtscts) {
1773                 dbg("%s: forcing rtscts for this device", __FUNCTION__);
1774                 port->tty->termios->c_cflag |= CRTSCTS;
1775         }
1776
1777         cflag = port->tty->termios->c_cflag;
1778
1779         /* FIXME -For this cut I don't care if the line is really changing or 
1780            not  - so just do the change regardless  - should be able to 
1781            compare old_termios and tty->termios */
1782         /* NOTE These routines can get interrupted by 
1783            ftdi_sio_read_bulk_callback  - need to examine what this 
1784            means - don't see any problems yet */
1785         
1786         /* Set number of data bits, parity, stop bits */
1787         
1788         urb_value = 0;
1789         urb_value |= (cflag & CSTOPB ? FTDI_SIO_SET_DATA_STOP_BITS_2 :
1790                       FTDI_SIO_SET_DATA_STOP_BITS_1);
1791         urb_value |= (cflag & PARENB ? 
1792                       (cflag & PARODD ? FTDI_SIO_SET_DATA_PARITY_ODD : 
1793                        FTDI_SIO_SET_DATA_PARITY_EVEN) :
1794                       FTDI_SIO_SET_DATA_PARITY_NONE);
1795         if (cflag & CSIZE) {
1796                 switch (cflag & CSIZE) {
1797                 case CS5: urb_value |= 5; dbg("Setting CS5"); break;
1798                 case CS6: urb_value |= 6; dbg("Setting CS6"); break;
1799                 case CS7: urb_value |= 7; dbg("Setting CS7"); break;
1800                 case CS8: urb_value |= 8; dbg("Setting CS8"); break;
1801                 default:
1802                         err("CSIZE was set but not CS5-CS8");
1803                 }
1804         }
1805
1806         /* This is needed by the break command since it uses the same command - but is
1807          *  or'ed with this value  */
1808         priv->last_set_data_urb_value = urb_value;
1809         
1810         if (usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1811                             FTDI_SIO_SET_DATA_REQUEST, 
1812                             FTDI_SIO_SET_DATA_REQUEST_TYPE,
1813                             urb_value , 0,
1814                             buf, 0, 100) < 0) {
1815                 err("%s FAILED to set databits/stopbits/parity", __FUNCTION__);
1816         }          
1817
1818         /* Now do the baudrate */
1819         if ((cflag & CBAUD) == B0 ) {
1820                 /* Disable flow control */
1821                 if (usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1822                                     FTDI_SIO_SET_FLOW_CTRL_REQUEST, 
1823                                     FTDI_SIO_SET_FLOW_CTRL_REQUEST_TYPE,
1824                                     0, 0, 
1825                                     buf, 0, WDR_TIMEOUT) < 0) {
1826                         err("%s error from disable flowcontrol urb", __FUNCTION__);
1827                 }           
1828                 /* Drop RTS and DTR */
1829                 if (set_dtr(port, LOW) < 0){
1830                         err("%s Error from DTR LOW urb", __FUNCTION__);
1831                 }
1832                 if (set_rts(port, LOW) < 0){
1833                         err("%s Error from RTS LOW urb", __FUNCTION__);
1834                 }       
1835                 
1836         } else {
1837                 /* set the baudrate determined before */
1838                 if (change_speed(port)) {
1839                         err("%s urb failed to set baurdrate", __FUNCTION__);
1840                 }
1841         }
1842
1843         /* Set flow control */
1844         /* Note device also supports DTR/CD (ugh) and Xon/Xoff in hardware */
1845         if (cflag & CRTSCTS) {
1846                 dbg("%s Setting to CRTSCTS flow control", __FUNCTION__);
1847                 if (usb_control_msg(serial->dev, 
1848                                     usb_sndctrlpipe(serial->dev, 0),
1849                                     FTDI_SIO_SET_FLOW_CTRL_REQUEST, 
1850                                     FTDI_SIO_SET_FLOW_CTRL_REQUEST_TYPE,
1851                                     0 , FTDI_SIO_RTS_CTS_HS,
1852                                     buf, 0, WDR_TIMEOUT) < 0) {
1853                         err("urb failed to set to rts/cts flow control");
1854                 }               
1855                 
1856         } else { 
1857                 /*
1858                  * Xon/Xoff code
1859                  *
1860                  * Check the IXOFF status in the iflag component of the termios structure
1861                  * if IXOFF is not set, the pre-xon/xoff code is executed.
1862                 */
1863                 if (iflag & IXOFF) {
1864                         dbg("%s  request to enable xonxoff iflag=%04x",__FUNCTION__,iflag);
1865                         // Try to enable the XON/XOFF on the ftdi_sio
1866                         // Set the vstart and vstop -- could have been done up above where
1867                         // a lot of other dereferencing is done but that would be very
1868                         // inefficient as vstart and vstop are not always needed
1869                         vstart=port->tty->termios->c_cc[VSTART];
1870                         vstop=port->tty->termios->c_cc[VSTOP];
1871                         urb_value=(vstop << 8) | (vstart);
1872
1873                         if (usb_control_msg(serial->dev,
1874                                             usb_sndctrlpipe(serial->dev, 0),
1875                                             FTDI_SIO_SET_FLOW_CTRL_REQUEST,
1876                                             FTDI_SIO_SET_FLOW_CTRL_REQUEST_TYPE,
1877                                             urb_value , FTDI_SIO_XON_XOFF_HS,
1878                                             buf, 0, WDR_TIMEOUT) < 0) {
1879                                 err("urb failed to set to xon/xoff flow control");
1880                         }
1881                 } else {
1882                         /* else clause to only run if cfag ! CRTSCTS and iflag ! XOFF */
1883                         /* CHECKME Assuming XON/XOFF handled by tty stack - not by device */
1884                         dbg("%s Turning off hardware flow control", __FUNCTION__);
1885                         if (usb_control_msg(serial->dev, 
1886                                             usb_sndctrlpipe(serial->dev, 0),
1887                                             FTDI_SIO_SET_FLOW_CTRL_REQUEST, 
1888                                             FTDI_SIO_SET_FLOW_CTRL_REQUEST_TYPE,
1889                                             0, 0, 
1890                                             buf, 0, WDR_TIMEOUT) < 0) {
1891                                 err("urb failed to clear flow control");
1892                         }                               
1893                 }
1894                 
1895         }
1896         return;
1897 } /* ftdi_termios */
1898
1899
1900 static int ftdi_tiocmget (struct usb_serial_port *port, struct file *file)
1901 {
1902         struct usb_serial *serial = port->serial;
1903         struct ftdi_private *priv = usb_get_serial_port_data(port);
1904         unsigned char buf[2];
1905         int ret;
1906
1907         dbg("%s TIOCMGET", __FUNCTION__);
1908         switch (priv->chip_type) {
1909         case SIO:
1910                 /* Request the status from the device */
1911                 if ((ret = usb_control_msg(serial->dev, 
1912                                            usb_rcvctrlpipe(serial->dev, 0),
1913                                            FTDI_SIO_GET_MODEM_STATUS_REQUEST, 
1914                                            FTDI_SIO_GET_MODEM_STATUS_REQUEST_TYPE,
1915                                            0, 0, 
1916                                            buf, 1, WDR_TIMEOUT)) < 0 ) {
1917                         err("%s Could not get modem status of device - err: %d", __FUNCTION__,
1918                             ret);
1919                         return(ret);
1920                 }
1921                 break;
1922         case FT8U232AM:
1923         case FT232BM:
1924                 /* the 8U232AM returns a two byte value (the sio is a 1 byte value) - in the same
1925                    format as the data returned from the in point */
1926                 if ((ret = usb_control_msg(serial->dev, 
1927                                            usb_rcvctrlpipe(serial->dev, 0),
1928                                            FTDI_SIO_GET_MODEM_STATUS_REQUEST, 
1929                                            FTDI_SIO_GET_MODEM_STATUS_REQUEST_TYPE,
1930                                            0, 0, 
1931                                            buf, 2, WDR_TIMEOUT)) < 0 ) {
1932                         err("%s Could not get modem status of device - err: %d", __FUNCTION__,
1933                             ret);
1934                         return(ret);
1935                 }
1936                 break;
1937         default:
1938                 return -EFAULT;
1939                 break;
1940         }
1941         
1942         return  (buf[0] & FTDI_SIO_DSR_MASK ? TIOCM_DSR : 0) |
1943                 (buf[0] & FTDI_SIO_CTS_MASK ? TIOCM_CTS : 0) |
1944                 (buf[0]  & FTDI_SIO_RI_MASK  ? TIOCM_RI  : 0) |
1945                 (buf[0]  & FTDI_SIO_RLSD_MASK ? TIOCM_CD  : 0) |
1946                 priv->last_dtr_rts;                     
1947 }
1948
1949 static int ftdi_tiocmset(struct usb_serial_port *port, struct file * file, unsigned int set, unsigned int clear)
1950 {
1951         int ret;
1952         
1953         if (set & TIOCM_DTR){
1954                 if ((ret = set_dtr(port, HIGH)) < 0) {
1955                         err("Urb to set DTR failed");
1956                         return(ret);
1957                 }
1958         }
1959         if (set & TIOCM_RTS) {
1960                 if ((ret = set_rts(port, HIGH)) < 0){
1961                         err("Urb to set RTS failed");
1962                         return(ret);
1963                 }
1964         }
1965         
1966         if (clear & TIOCM_DTR){
1967                 if ((ret = set_dtr(port, LOW)) < 0){
1968                         err("Urb to unset DTR failed");
1969                         return(ret);
1970                 }
1971         }       
1972         if (clear & TIOCM_RTS) {
1973                 if ((ret = set_rts(port, LOW)) < 0){
1974                         err("Urb to unset RTS failed");
1975                         return(ret);
1976                 }
1977         }
1978         return(0);
1979 }
1980
1981
1982 static int ftdi_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
1983 {
1984         struct ftdi_private *priv = usb_get_serial_port_data(port);
1985
1986         int  ret, mask;
1987         
1988         dbg("%s cmd 0x%04x", __FUNCTION__, cmd);
1989
1990         /* Based on code from acm.c and others */
1991         switch (cmd) {
1992
1993         case TIOCMBIS: /* turns on (Sets) the lines as specified by the mask */
1994                 dbg("%s TIOCMBIS", __FUNCTION__);
1995                 if (get_user(mask, (unsigned long *) arg))
1996                         return -EFAULT;
1997                 if (mask & TIOCM_DTR){
1998                         if ((ret = set_dtr(port, HIGH)) < 0) {
1999                                 err("Urb to set DTR failed");
2000                                 return(ret);
2001                         }
2002                 }
2003                 if (mask & TIOCM_RTS) {
2004                         if ((ret = set_rts(port, HIGH)) < 0){
2005                                 err("Urb to set RTS failed");
2006                                 return(ret);
2007                         }
2008                 }
2009                 return(0);
2010                 break;
2011
2012         case TIOCMBIC: /* turns off (Clears) the lines as specified by the mask */
2013                 dbg("%s TIOCMBIC", __FUNCTION__);
2014                 if (get_user(mask, (unsigned long *) arg))
2015                         return -EFAULT;
2016                 if (mask & TIOCM_DTR){
2017                         if ((ret = set_dtr(port, LOW)) < 0){
2018                                 err("Urb to unset DTR failed");
2019                                 return(ret);
2020                         }
2021                 }       
2022                 if (mask & TIOCM_RTS) {
2023                         if ((ret = set_rts(port, LOW)) < 0){
2024                                 err("Urb to unset RTS failed");
2025                                 return(ret);
2026                         }
2027                 }
2028                 return(0);
2029                 break;
2030
2031                 /*
2032                  * I had originally implemented TCSET{A,S}{,F,W} and
2033                  * TCGET{A,S} here separately, however when testing I
2034                  * found that the higher layers actually do the termios
2035                  * conversions themselves and pass the call onto
2036                  * ftdi_sio_set_termios. 
2037                  *
2038                  */
2039
2040         case TIOCGSERIAL: /* gets serial port data */
2041                 return get_serial_info(port, (struct serial_struct *) arg);
2042
2043         case TIOCSSERIAL: /* sets serial port data */
2044                 return set_serial_info(port, (struct serial_struct *) arg);
2045
2046         /*
2047          * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
2048          * - mask passed in arg for lines of interest
2049          *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
2050          * Caller should use TIOCGICOUNT to see which one it was.
2051          *
2052          * This code is borrowed from linux/drivers/char/serial.c
2053          */
2054         case TIOCMIWAIT:
2055                 while (priv != NULL) {
2056                         interruptible_sleep_on(&priv->delta_msr_wait);
2057                         /* see if a signal did it */
2058                         if (signal_pending(current))
2059                                 return -ERESTARTSYS;
2060                         else {
2061                                 char diff = priv->diff_status;
2062
2063                                 if (diff == 0) {
2064                                         return -EIO; /* no change => error */
2065                                 }
2066
2067                                 /* Consume all events */
2068                                 priv->diff_status = 0;
2069
2070                                 /* Return 0 if caller wanted to know about these bits */
2071                                 if ( ((arg & TIOCM_RNG) && (diff & FTDI_RS0_RI)) ||
2072                                      ((arg & TIOCM_DSR) && (diff & FTDI_RS0_DSR)) ||
2073                                      ((arg & TIOCM_CD)  && (diff & FTDI_RS0_RLSD)) ||
2074                                      ((arg & TIOCM_CTS) && (diff & FTDI_RS0_CTS)) ) {
2075                                         return 0;
2076                                 }
2077                                 /*
2078                                  * Otherwise caller can't care less about what happened,
2079                                  * and so we continue to wait for more events.
2080                                  */
2081                         }
2082                 }
2083                 return(0);
2084                 break;
2085         default:
2086                 break;
2087                 
2088         }
2089
2090
2091         /* This is not necessarily an error - turns out the higher layers will do 
2092          *  some ioctls itself (see comment above)
2093          */
2094         dbg("%s arg not supported - it was 0x%04x - check /usr/include/asm/ioctls.h", __FUNCTION__, cmd);
2095
2096         return(-ENOIOCTLCMD);
2097 } /* ftdi_ioctl */
2098
2099
2100 static void ftdi_throttle (struct usb_serial_port *port)
2101 {
2102         dbg("%s - port %d", __FUNCTION__, port->number);
2103         usb_unlink_urb (port->read_urb);
2104 }
2105
2106
2107 static void ftdi_unthrottle (struct usb_serial_port *port)
2108 {
2109         int result;
2110         struct usb_serial *serial = port->serial;
2111
2112         dbg("%s - port %d", __FUNCTION__, port->number);
2113
2114         port->read_urb->dev = serial->dev;
2115
2116         usb_fill_bulk_urb(port->read_urb, serial->dev, 
2117                       usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
2118                       port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
2119                       ftdi_read_bulk_callback, port);
2120
2121         result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
2122         if (result)
2123                 err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
2124 }
2125
2126 static int __init ftdi_init (void)
2127 {
2128         int retval;
2129
2130         dbg("%s", __FUNCTION__);
2131         retval = usb_serial_register(&ftdi_SIO_device);
2132         if (retval)
2133                 goto failed_SIO_register;
2134         retval = usb_serial_register(&ftdi_8U232AM_device);
2135         if (retval)
2136                 goto failed_8U232AM_register;
2137         retval = usb_serial_register(&ftdi_FT232BM_device);
2138         if (retval)
2139                 goto failed_FT232BM_register;
2140         retval = usb_serial_register(&ftdi_USB_UIRT_device);
2141         if (retval)
2142                 goto failed_USB_UIRT_register;
2143         retval = usb_serial_register(&ftdi_HE_TIRA1_device);
2144         if (retval)
2145                 goto failed_HE_TIRA1_register;
2146         retval = usb_register(&ftdi_driver);
2147         if (retval) 
2148                 goto failed_usb_register;
2149
2150         info(DRIVER_VERSION ":" DRIVER_DESC);
2151         return 0;
2152 failed_usb_register:
2153         usb_serial_deregister(&ftdi_HE_TIRA1_device);
2154 failed_HE_TIRA1_register:
2155         usb_serial_deregister(&ftdi_USB_UIRT_device);
2156 failed_USB_UIRT_register:
2157         usb_serial_deregister(&ftdi_FT232BM_device);
2158 failed_FT232BM_register:
2159         usb_serial_deregister(&ftdi_8U232AM_device);
2160 failed_8U232AM_register:
2161         usb_serial_deregister(&ftdi_SIO_device);
2162 failed_SIO_register:
2163         return retval;
2164 }
2165
2166
2167 static void __exit ftdi_exit (void)
2168 {
2169
2170         dbg("%s", __FUNCTION__);
2171
2172         usb_deregister (&ftdi_driver);
2173         usb_serial_deregister (&ftdi_HE_TIRA1_device);
2174         usb_serial_deregister (&ftdi_USB_UIRT_device);
2175         usb_serial_deregister (&ftdi_FT232BM_device);
2176         usb_serial_deregister (&ftdi_8U232AM_device);
2177         usb_serial_deregister (&ftdi_SIO_device);
2178
2179 }
2180
2181
2182 module_init(ftdi_init);
2183 module_exit(ftdi_exit);
2184
2185 MODULE_AUTHOR( DRIVER_AUTHOR );
2186 MODULE_DESCRIPTION( DRIVER_DESC );
2187 MODULE_LICENSE("GPL");
2188
2189 MODULE_PARM(debug, "i");
2190 MODULE_PARM_DESC(debug, "Debug enabled or not");
2191