Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / lib / usb_dev_board_drivers / ftdi_sio.c @ 11

History | View | Annotate | Download (37.9 KB)

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
 * (25/Jul/2002) Bill Ryder inserted Dmitri's TIOCMIWAIT patch
21
 *      Not tested by me but it doesn't break anything I use.
22
 * 
23
 * (04/Jan/2002) Kuba Ober
24
 *        Implemented 38400 baudrate kludge, where it can be substituted with other
25
 *          values. That's the only way to set custom baudrates.
26
 *        Implemented TIOCSSERIAL, TIOCGSERIAL ioctl's so that setserial is happy.
27
 *        FIXME: both baudrate things should eventually go to usbserial.c as other
28
 *          devices may need that functionality too. Actually, it can probably be
29
 *          merged in serial.c somehow - too many drivers repeat this code over
30
 *          and over.
31
 *        Fixed baudrate forgetfulness - open() used to reset baudrate to 9600 every time.
32
 *        Divisors for baudrates are calculated by a macro.
33
 *        Small code cleanups. Ugly whitespace changes for Plato's sake only ;-].
34
 *
35
 * (04/Nov/2001) Bill Ryder
36
 *        Fixed bug in read_bulk_callback where incorrect urb buffer was used.
37
 *        Cleaned up write offset calculation
38
 *        Added write_room since default values can be incorrect for sio
39
 *        Changed write_bulk_callback to use same queue_task as other drivers
40
 *        (the previous version caused panics)
41
 *        Removed port iteration code since the device only has one I/O port and it
42
 *          was wrong anyway.
43
 * 
44
 * (31/May/2001) gkh
45
 *        Switched from using spinlock to a semaphore, which fixes lots of problems.
46
 *
47
 * (23/May/2001)   Bill Ryder
48
 *        Added runtime debug patch (thanx Tyson D Sawyer).
49
 *        Cleaned up comments for 8U232
50
 *        Added parity, framing and overrun error handling
51
 *        Added receive break handling.
52
 * 
53
 * (04/08/2001) gb
54
 *        Identify version on module load.
55
 *       
56
 * (18/March/2001) Bill Ryder
57
 *        (Not released)
58
 *        Added send break handling. (requires kernel patch too)
59
 *        Fixed 8U232AM hardware RTS/CTS etc status reporting.
60
 *        Added flipbuf fix copied from generic device
61
 * 
62
 * (12/3/2000) Bill Ryder
63
 *        Added support for 8U232AM device.
64
 *        Moved PID and VIDs into header file only.
65
 *        Turned on low-latency for the tty (device will do high baudrates)
66
 *        Added shutdown routine to close files when device removed.
67
 *        More debug and error message cleanups.
68
 *
69
 * (11/13/2000) Bill Ryder
70
 *        Added spinlock protected open code and close code.
71
 *        Multiple opens work (sort of - see webpage mentioned above).
72
 *        Cleaned up comments. Removed multiple PID/VID definitions.
73
 *        Factorised cts/dtr code
74
 *        Made use of __FUNCTION__ in dbg's
75
 *      
76
 * (11/01/2000) Adam J. Richter
77
 *        usb_device_id table support
78
 * 
79
 * (10/05/2000) gkh
80
 *        Fixed bug with urb->dev not being set properly, now that the usb
81
 *        core needs it.
82
 * 
83
 * (09/11/2000) gkh
84
 *        Removed DEBUG #ifdefs with call to usb_serial_debug_data
85
 *
86
 * (07/19/2000) gkh
87
 *        Added module_init and module_exit functions to handle the fact that this
88
 *        driver is a loadable module now.
89
 *
90
 * (04/04/2000) Bill Ryder 
91
 *        Fixed bugs in TCGET/TCSET ioctls (by removing them - they are
92
 *        handled elsewhere in the tty io driver chain).
93
 *
94
 * (03/30/2000) Bill Ryder 
95
 *        Implemented lots of ioctls
96
 *        Fixed a race condition in write
97
 *        Changed some dbg's to errs
98
 *
99
 * (03/26/2000) gkh
100
 *        Split driver up into device specific pieces.
101
 *
102
 */
103

    
104
/* Bill Ryder - bryder@sgi.com - wrote the FTDI_SIO implementation */
105
/* Thanx to FTDI for so kindly providing details of the protocol required */
106
/*   to talk to the device */
107
/* Thanx to gkh and the rest of the usb dev group for all code I have assimilated :-) */
108

    
109
#include <linux/config.h>
110
#include <linux/kernel.h>
111
#include <linux/sched.h>
112
#include <linux/signal.h>
113
#include <linux/errno.h>
114
#include <linux/poll.h>
115
#include <linux/init.h>
116
#include <linux/slab.h>
117
#include <linux/fcntl.h>
118
#include <linux/tty.h>
119
#include <linux/tty_driver.h>
120
#include <linux/tty_flip.h>
121
#include <linux/module.h>
122
#include <linux/spinlock.h>
123
#include <linux/usb.h>
124
#include <linux/serial.h>
125
#ifdef CONFIG_USB_SERIAL_DEBUG
126
        static int debug = 1;
127
#else
128
        static int debug;
129
#endif
130

    
131
#include "usb-serial.h"
132
#include "ftdi_sio.h"
133

    
134
/*
135
 * Version Information
136
 */
137
#define DRIVER_VERSION "v1.2.1"
138
#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Bill Ryder <bryder@sgi.com>, Kuba Ober <kuba@mareimbrium.org>"
139
#define DRIVER_DESC "USB FTDI Serial Converters Driver"
140

    
141
static __devinitdata struct usb_device_id id_table_sio [] = {
142
        { USB_DEVICE(FTDI_VID, FTDI_SIO_PID) },
143
        { }                                                /* Terminating entry */
144
};
145

    
146
/*
147
 * The 8U232AM has the same API as the sio except for:
148
 * - it can support MUCH higher baudrates; up to:
149
 *   o 921600 for RS232 and 2000000 for RS422/485 at 48MHz
150
 *   o 230400 at 12MHz
151
 *   so .. 8U232AM's baudrate setting codes are different
152
 * - it has a two byte status code.
153
 * - it returns characters every 16ms (the FTDI does it every 40ms)
154
 */
155

    
156

    
157
static __devinitdata struct usb_device_id id_table_8U232AM [] = {
158
        { USB_DEVICE(FTDI_VID, FTDI_8U232AM_PID) },
159
        { USB_DEVICE(FTDI_NF_RIC_VID, FTDI_NF_RIC_PID) },
160
        { }                                                /* Terminating entry */
161
};
162

    
163

    
164
static __devinitdata struct usb_device_id id_table_combined [] = {
165
        { USB_DEVICE(FTDI_VID, FTDI_SIO_PID) },
166
        { USB_DEVICE(FTDI_VID, FTDI_8U232AM_PID) },
167
        { USB_DEVICE(FTDI_NF_RIC_VID, FTDI_NF_RIC_PID) },
168
        { }                                                /* Terminating entry */
169
};
170

    
171
MODULE_DEVICE_TABLE (usb, id_table_combined);
172

    
173
struct ftdi_private {
174
        ftdi_chip_type_t chip_type;
175
                                /* type of the device, either SIO or FT8U232AM */
176
        int baud_base;                /* baud base clock for divisor setting */
177
        int custom_divisor;        /* custom_divisor kludge, this is for baud_base (different from what goes to the chip!) */
178
        __u16 last_set_data_urb_value ;
179
                                /* the last data state set - needed for doing a break */
180
        int write_offset;       /* This is the offset in the usb data block to write the serial data - 
181
                                 * it is different between devices
182
                                 */
183
        int flags;                /* some ASYNC_xxxx flags are supported */
184
        wait_queue_head_t delta_msr_wait; /* Used for TIOCMIWAIT */
185
         char prev_status, diff_status;        /* Used for TIOCMIWAIT */
186
};
187

    
188
/* Used for TIOCMIWAIT */
189
#define FTDI_STATUS_B0_MASK        (FTDI_RS0_CTS | FTDI_RS0_DSR | FTDI_RS0_RI | FTDI_RS0_RLSD)
190
#define FTDI_STATUS_B1_MASK        (FTDI_RS_BI)
191
/* End TIOCMIWAIT */
192

    
193
#define FTDI_IMPL_ASYNC_FLAGS = ( ASYNC_SPD_HI | ASYNC_SPD_VHI \
194
 ASYNC_SPD_CUST | ASYNC_SPD_SHI | ASYNC_SPD_WARP )
195

    
196
/* function prototypes for a FTDI serial converter */
197
static int  ftdi_SIO_startup                (struct usb_serial *serial);
198
static int  ftdi_8U232AM_startup        (struct usb_serial *serial);
199
static void ftdi_shutdown                (struct usb_serial *serial);
200
static int  ftdi_open                        (struct usb_serial_port *port, struct file *filp);
201
static void ftdi_close                        (struct usb_serial_port *port, struct file *filp);
202
static int  ftdi_write                        (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count);
203
static int  ftdi_write_room                (struct usb_serial_port *port);
204
static void ftdi_write_bulk_callback        (struct urb *urb);
205
static void ftdi_read_bulk_callback        (struct urb *urb);
206
static void ftdi_set_termios                (struct usb_serial_port *port, struct termios * old);
207
static int  ftdi_ioctl                        (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
208
static void ftdi_break_ctl                (struct usb_serial_port *port, int break_state );
209

    
210
static struct usb_serial_device_type ftdi_SIO_device = {
211
        name:                        "FTDI SIO",
212
        id_table:                id_table_sio,
213
        needs_interrupt_in:        MUST_HAVE_NOT,
214
        needs_bulk_in:                MUST_HAVE,
215
        needs_bulk_out:                MUST_HAVE,
216
        num_interrupt_in:        0,
217
        num_bulk_in:                1,
218
        num_bulk_out:                1,
219
        num_ports:                1,
220
        open:                        ftdi_open,
221
        close:                        ftdi_close,
222
        write:                        ftdi_write,
223
        write_room:                ftdi_write_room,
224
        read_bulk_callback:        ftdi_read_bulk_callback,
225
        write_bulk_callback:        ftdi_write_bulk_callback,
226
        ioctl:                        ftdi_ioctl,
227
        set_termios:                ftdi_set_termios,
228
        break_ctl:                ftdi_break_ctl,
229
        startup:                ftdi_SIO_startup,
230
        shutdown:               ftdi_shutdown,
231
};
232

    
233
static struct usb_serial_device_type ftdi_8U232AM_device = {
234
        name:                        "FTDI 8U232AM",
235
        id_table:                id_table_8U232AM,
236
        needs_interrupt_in:        DONT_CARE,
237
        needs_bulk_in:                MUST_HAVE,
238
        needs_bulk_out:                MUST_HAVE,
239
        num_interrupt_in:        0,
240
        num_bulk_in:                1,
241
        num_bulk_out:                1,
242
        num_ports:                1,
243
        open:                        ftdi_open,
244
        close:                        ftdi_close,
245
        write:                        ftdi_write,
246
        write_room:                ftdi_write_room,
247
        read_bulk_callback:        ftdi_read_bulk_callback,
248
        write_bulk_callback:        ftdi_write_bulk_callback,
249
        ioctl:                        ftdi_ioctl,
250
        set_termios:                ftdi_set_termios,
251
        break_ctl:                ftdi_break_ctl,
252
        startup:                ftdi_8U232AM_startup,
253
        shutdown:               ftdi_shutdown,
254
};
255

    
256
#define WDR_TIMEOUT (HZ * 5 ) /* default urb timeout */
257

    
258
#define HIGH 1
259
#define LOW 0
260

    
261
/*
262
 * ***************************************************************************
263
 * Utlity functions
264
 * ***************************************************************************
265
 */
266

    
267

    
268
static int set_rts(struct usb_device *dev,
269
                   unsigned int pipe,
270
                   int high_or_low)
271
{
272
        static char buf[1];
273
        unsigned ftdi_high_or_low = (high_or_low? FTDI_SIO_SET_RTS_HIGH : 
274
                                FTDI_SIO_SET_RTS_LOW);
275
        return(usb_control_msg(dev, pipe,
276
                               FTDI_SIO_SET_MODEM_CTRL_REQUEST, 
277
                               FTDI_SIO_SET_MODEM_CTRL_REQUEST_TYPE,
278
                               ftdi_high_or_low, 0, 
279
                               buf, 0, WDR_TIMEOUT));
280
}
281

    
282

    
283
static int set_dtr(struct usb_device *dev,
284
                   unsigned int pipe,
285
                   int high_or_low)
286
{
287
        static char buf[1];
288
        unsigned ftdi_high_or_low = (high_or_low? FTDI_SIO_SET_DTR_HIGH : 
289
                                FTDI_SIO_SET_DTR_LOW);
290
        return(usb_control_msg(dev, pipe,
291
                               FTDI_SIO_SET_MODEM_CTRL_REQUEST, 
292
                               FTDI_SIO_SET_MODEM_CTRL_REQUEST_TYPE,
293
                               ftdi_high_or_low, 0, 
294
                               buf, 0, WDR_TIMEOUT));
295
}
296

    
297

    
298
static __u16 get_ftdi_divisor(struct usb_serial_port * port);
299

    
300

    
301
static int change_speed(struct usb_serial_port *port)
302
{
303
        char buf[1];
304
        __u16 urb_value;
305

    
306
        urb_value = get_ftdi_divisor(port);
307
        
308
        return (usb_control_msg(port->serial->dev,
309
                          usb_sndctrlpipe(port->serial->dev, 0),
310
                          FTDI_SIO_SET_BAUDRATE_REQUEST,
311
                          FTDI_SIO_SET_BAUDRATE_REQUEST_TYPE,
312
                          urb_value, 0,
313
                          buf, 0, 100) < 0);
314
}
315

    
316

    
317
static __u16 get_ftdi_divisor(struct usb_serial_port * port)
318
{ /* get_ftdi_divisor */
319
        
320
        struct ftdi_private * priv = (struct ftdi_private *)port->private;
321
        __u16 urb_value = 0;
322
        int baud;
323

    
324
        /*
325
         * The logic involved in setting the baudrate can be cleanly split in 3 steps.
326
         * Obtaining the actual baud rate is a little tricky since unix traditionally
327
         * somehow ignored the possibility to set non-standard baud rates.
328
         * 1. Standard baud rates are set in tty->termios->c_cflag
329
         * 2. If these are not enough, you can set any speed using alt_speed as follows:
330
         *    - set tty->termios->c_cflag speed to B38400
331
         *    - set your real speed in tty->alt_speed; it gets ignored when
332
         *      alt_speed==0, (or)
333
         *    - call TIOCSSERIAL ioctl with (struct serial_struct) set as follows:
334
         *      flags & ASYNC_SPD_MASK == ASYNC_SPD_[HI, VHI, SHI, WARP], this just
335
         *      sets alt_speed to (HI: 57600, VHI: 115200, SHI: 230400, WARP: 460800)
336
         * ** Steps 1, 2 are done courtesy of tty_get_baud_rate
337
         * 3. You can also set baud rate by setting custom divisor as follows
338
         *    - set tty->termios->c_cflag speed to B38400
339
         *    - call TIOCSSERIAL ioctl with (struct serial_struct) set as follows:
340
         *      o flags & ASYNC_SPD_MASK == ASYNC_SPD_CUST
341
         *      o custom_divisor set to baud_base / your_new_baudrate
342
         * ** Step 3 is done courtesy of code borrowed from serial.c - I should really
343
         *    spend some time and separate+move this common code to serial.c, it is
344
         *    replicated in nearly every serial driver you see.
345
         */
346

    
347
        /* 1. Get the baud rate from the tty settings, this observes alt_speed hack */
348

    
349
        baud = tty_get_baud_rate(port->tty);
350
        dbg(__FUNCTION__ " tty_get_baud_rate reports speed %d", baud);
351

    
352
        /* 2. Observe async-compatible custom_divisor hack, update baudrate if needed */
353

    
354
        if (baud == 38400 &&
355
            ((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST) &&
356
             (priv->custom_divisor)) {
357
                baud = priv->baud_base / priv->custom_divisor;
358
                dbg(__FUNCTION__ " custom divisor %d sets baud rate to %d", priv->custom_divisor, baud);
359
        }
360

    
361
        /* 3. Convert baudrate to device-specific divisor */
362

    
363
        if (!baud) baud = 9600;        
364
        switch(priv->chip_type) {
365
        case SIO: /* SIO chip */
366
                switch(baud) {
367
                case 300: urb_value = ftdi_sio_b300; break;
368
                case 600: urb_value = ftdi_sio_b600; break;
369
                case 1200: urb_value = ftdi_sio_b1200; break;
370
                case 2400: urb_value = ftdi_sio_b2400; break;
371
                case 4800: urb_value = ftdi_sio_b4800; break;
372
                case 9600: urb_value = ftdi_sio_b9600; break;
373
                case 19200: urb_value = ftdi_sio_b19200; break;
374
                case 38400: urb_value = ftdi_sio_b38400; break;
375
                case 57600: urb_value = ftdi_sio_b57600;  break;
376
                case 115200: urb_value = ftdi_sio_b115200; break;
377
                } /* baud */
378
                if (urb_value == 0)
379
                        dbg(__FUNCTION__ " Baudrate (%d) requested is not supported", baud);
380
                break;
381
        case FT8U232AM: /* 8U232AM chip */
382
                if (baud <= 3000000) {
383
                        urb_value = FTDI_SIO_BAUD_TO_DIVISOR(baud);
384
                } else {
385
                        dbg(__FUNCTION__ " Baud rate too high!");
386
                }
387
                break;
388
        } /* priv->chip_type */
389

    
390
        if (urb_value == 0) {
391
                urb_value = ftdi_sio_b9600;
392
        } else {
393
                dbg(__FUNCTION__ " Baud rate set to %d (divisor %d) on chip %s", baud, urb_value, (priv->chip_type == SIO) ? "SIO" : "FT8U232AM" );
394
        }
395

    
396
        return(urb_value);
397
}
398

    
399

    
400
static int get_serial_info(struct usb_serial_port * port, struct serial_struct * retinfo)
401
{
402
        struct ftdi_private * priv = (struct ftdi_private*) port->private;
403
        struct serial_struct tmp;
404

    
405
        if (!retinfo)
406
                return -EFAULT;
407
        memset(&tmp, 0, sizeof(tmp));
408
        tmp.flags = priv->flags;
409
        tmp.baud_base = priv->baud_base;
410
        tmp.custom_divisor = priv->custom_divisor;
411
        if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
412
                return -EFAULT;
413
        return 0;
414
} /* get_serial_info */
415

    
416

    
417
static int set_serial_info(struct usb_serial_port * port, struct serial_struct * newinfo)
418
{ /* set_serial_info */
419
        struct ftdi_private * priv = (struct ftdi_private *) port->private;
420
        struct serial_struct new_serial;
421
        struct ftdi_private old_priv;
422

    
423
        if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
424
                return -EFAULT;
425
        old_priv = * priv;
426

    
427
        /* Do error checking and permission checking */
428

    
429
        if (!capable(CAP_SYS_ADMIN)) {
430
                if (((new_serial.flags & ~ASYNC_USR_MASK) !=
431
                     (priv->flags & ~ASYNC_USR_MASK)))
432
                        return -EPERM;
433
                priv->flags = ((priv->flags & ~ASYNC_USR_MASK) |
434
                               (new_serial.flags & ASYNC_USR_MASK));
435
                priv->custom_divisor = new_serial.custom_divisor;
436
                goto check_and_exit;
437
        }
438

    
439
        if ((new_serial.baud_base != priv->baud_base) ||
440
            (new_serial.baud_base < 9600))
441
                return -EINVAL;
442

    
443
        /* Make the changes - these are privileged changes! */
444

    
445
        priv->flags = ((priv->flags & ~ASYNC_FLAGS) |
446
                       (new_serial.flags & ASYNC_FLAGS));        
447
        priv->custom_divisor = new_serial.custom_divisor;
448

    
449
        port->tty->low_latency = (priv->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
450

    
451
check_and_exit:
452
        if (((old_priv.flags & ASYNC_SPD_MASK) !=
453
             (priv->flags & ASYNC_SPD_MASK)) ||
454
            (old_priv.custom_divisor != priv->custom_divisor)) {
455
                if ((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
456
                        port->tty->alt_speed = 57600;
457
                if ((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
458
                        port->tty->alt_speed = 115200;
459
                if ((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
460
                        port->tty->alt_speed = 230400;
461
                if ((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
462
                        port->tty->alt_speed = 460800;
463
                change_speed(port);
464
        }
465
        
466
        return (0);
467

    
468
} /* set_serial_info */
469

    
470
/*
471
 * ***************************************************************************
472
 * FTDI driver specific functions
473
 * ***************************************************************************
474
 */
475

    
476
/* Startup for the SIO chip */
477
static int ftdi_SIO_startup (struct usb_serial *serial)
478
{
479
        struct ftdi_private *priv;
480

    
481
        priv = serial->port->private = kmalloc(sizeof(struct ftdi_private), GFP_KERNEL);
482
        if (!priv){
483
                err(__FUNCTION__"- kmalloc(%Zd) failed.", sizeof(struct ftdi_private));
484
                return -ENOMEM;
485
        }
486

    
487
        priv->chip_type = SIO;
488
        priv->baud_base = 12000000 / 16;
489
        priv->custom_divisor = 0;
490
        priv->write_offset = 1;
491
         priv->prev_status = priv->diff_status = 0;
492
        /* This will push the characters through immediately rather
493
           than queue a task to deliver them */
494
        priv->flags = ASYNC_LOW_LATENCY;
495
        
496
        return (0);
497
}
498

    
499
/* Startup for the 8U232AM chip */
500
static int ftdi_8U232AM_startup (struct usb_serial *serial)
501
{
502
        struct ftdi_private *priv;
503

    
504
        priv = serial->port->private = kmalloc(sizeof(struct ftdi_private), GFP_KERNEL);
505
        if (!priv){
506
                err(__FUNCTION__"- kmalloc(%Zd) failed.", sizeof(struct ftdi_private));
507
                return -ENOMEM;
508
        }
509

    
510
        priv->chip_type = FT8U232AM;
511
        priv->baud_base = 48000000 / 2; /* Would be / 16, but FTDI supports 0.125, 0.25 and 0.5 divisor fractions! */
512
        priv->custom_divisor = 0;
513
        priv->write_offset = 0;
514
        init_waitqueue_head(&priv->delta_msr_wait);
515
        /* This will push the characters through immediately rather
516
           than queue a task to deliver them */
517
        priv->flags = ASYNC_LOW_LATENCY;
518
        
519
        return (0);
520
}
521

    
522

    
523
static void ftdi_shutdown (struct usb_serial *serial)
524
{
525
        dbg (__FUNCTION__);
526

    
527
        /* stop reads and writes on all ports */
528
        while (serial->port[0].open_count > 0) {
529
                ftdi_close (&serial->port[0], NULL);
530
        }
531
        if (serial->port[0].private){
532
                kfree(serial->port[0].private);
533
                serial->port[0].private = NULL;
534
        }
535
}
536

    
537

    
538
static int  ftdi_open (struct usb_serial_port *port, struct file *filp)
539
{ /* ftdi_open */
540
        struct termios tmp_termios;
541
        struct usb_serial *serial = port->serial;
542
        struct ftdi_private *priv = port->private;
543
        
544
        int result = 0;
545
        char buf[1]; /* Needed for the usb_control_msg I think */
546

    
547
        dbg(__FUNCTION__);
548

    
549
        down (&port->sem);
550
        
551
        MOD_INC_USE_COUNT;
552
        ++port->open_count;
553

    
554
        if (!port->active){
555
                port->active = 1;
556

    
557
                port->tty->low_latency = (priv->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
558

    
559
                /* No error checking for this (will get errors later anyway) */
560
                /* See ftdi_sio.h for description of what is reset */
561
                usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
562
                                FTDI_SIO_RESET_REQUEST, FTDI_SIO_RESET_REQUEST_TYPE, 
563
                                FTDI_SIO_RESET_SIO, 
564
                                0, buf, 0, WDR_TIMEOUT);
565

    
566
                /* Termios defaults are set by usb_serial_init. We don't change
567
                   port->tty->termios - this would loose speed settings, etc.
568
                   This is same behaviour as serial.c/rs_open() - Kuba */
569

    
570
                /* ftdi_set_termios  will send usb control messages */
571
                ftdi_set_termios(port, &tmp_termios);
572

    
573
                /* FIXME: Flow control might be enabled, so it should be checked -
574
                   we have no control of defaults! */
575
                /* Turn on RTS and DTR since we are not flow controlling by default */
576
                if (set_dtr(serial->dev, usb_sndctrlpipe(serial->dev, 0),HIGH) < 0) {
577
                        err(__FUNCTION__ " Error from DTR HIGH urb");
578
                }
579
                if (set_rts(serial->dev, usb_sndctrlpipe(serial->dev, 0),HIGH) < 0){
580
                        err(__FUNCTION__ " Error from RTS HIGH urb");
581
                }
582
        
583
                /* Start reading from the device */
584
                FILL_BULK_URB(port->read_urb, serial->dev, 
585
                              usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
586
                              port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
587
                              ftdi_read_bulk_callback, port);
588
                result = usb_submit_urb(port->read_urb);
589
                if (result)
590
                        err(__FUNCTION__ " - failed submitting read urb, error %d", result);
591
        }
592

    
593
        up (&port->sem);
594
        return result;
595
} /* ftdi_open */
596

    
597

    
598
static void ftdi_close (struct usb_serial_port *port, struct file *filp)
599
{ /* ftdi_close */
600
        struct usb_serial *serial = port->serial; /* Checked in usbserial.c */
601
        unsigned int c_cflag = port->tty->termios->c_cflag;
602
        char buf[1];
603

    
604
        dbg( __FUNCTION__);
605

    
606
        down (&port->sem);
607
        --port->open_count;
608

    
609
        if (port->open_count <= 0) {
610
                if (serial->dev) {
611
                        if (c_cflag & HUPCL){
612
                                /* Disable flow control */
613
                                if (usb_control_msg(serial->dev, 
614
                                                    usb_sndctrlpipe(serial->dev, 0),
615
                                                    FTDI_SIO_SET_FLOW_CTRL_REQUEST,
616
                                                    FTDI_SIO_SET_FLOW_CTRL_REQUEST_TYPE,
617
                                                    0, 0, buf, 0, WDR_TIMEOUT) < 0) {
618
                                        err("error from flowcontrol urb");
619
                                }            
620

    
621
                                /* drop DTR */
622
                                if (set_dtr(serial->dev, usb_sndctrlpipe(serial->dev, 0), LOW) < 0){
623
                                        err("Error from DTR LOW urb");
624
                                }
625
                                /* drop RTS */
626
                                if (set_rts(serial->dev, usb_sndctrlpipe(serial->dev, 0),LOW) < 0) {
627
                                        err("Error from RTS LOW urb");
628
                                }        
629
                        } /* Note change no line is hupcl is off */
630

    
631
                        /* shutdown our bulk reads and writes */
632
                        /* ***CHECK*** behaviour when there is nothing queued */
633
                        usb_unlink_urb (port->write_urb);
634
                        usb_unlink_urb (port->read_urb);
635
                }
636
                port->active = 0;
637
                port->open_count = 0;
638
        } else {  
639
                /* Send a HUP if necessary */
640
                if (!(port->tty->termios->c_cflag & CLOCAL)){
641
                        tty_hangup(port->tty);
642
                }
643
        }
644

    
645
        up (&port->sem);
646
        MOD_DEC_USE_COUNT;
647

    
648
} /* ftdi_close */
649

    
650

    
651
  
652
/* The ftdi_sio requires the first byte to have:
653
 *  B0 1
654
 *  B1 0
655
 *  B2..7 length of message excluding byte 0
656
 */
657
static int ftdi_write (struct usb_serial_port *port, int from_user,
658
                           const unsigned char *buf, int count)
659
{ /* ftdi_write */
660
        struct usb_serial *serial = port->serial;
661
        struct ftdi_private *priv = (struct ftdi_private *)port->private;
662
        unsigned char *first_byte = port->write_urb->transfer_buffer;
663
        int data_offset ;
664
        int result;
665
        
666
        dbg(__FUNCTION__ " port %d, %d bytes", port->number, count);
667

    
668
        if (count == 0) {
669
                err("write request of 0 bytes");
670
                return 0;
671
        }
672
        
673
        data_offset = priv->write_offset;
674
        dbg("data_offset set to %d",data_offset);
675

    
676
        if (port->write_urb->status == -EINPROGRESS) {
677
                dbg (__FUNCTION__ " - already writing");
678
                return (0);
679
        }                
680

    
681
        down(&port->sem);
682

    
683
        count += data_offset;
684
        count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
685

    
686
        /* Copy in the data to send */
687
        if (from_user) {
688
                if (copy_from_user((char *)port->write_urb->transfer_buffer + data_offset,
689
                                   buf, count - data_offset )){
690
                        up (&port->sem);
691
                        return -EFAULT;
692
                }
693
        } else {
694
                memcpy((char *)port->write_urb->transfer_buffer + data_offset,
695
                       buf, count - data_offset );
696
        }  
697

    
698
        first_byte = port->write_urb->transfer_buffer;
699
        if (data_offset > 0){
700
                /* Write the control byte at the front of the packet*/
701
                *first_byte = 1 | ((count-data_offset) << 2) ; 
702
        }
703

    
704
        dbg(__FUNCTION__ " Bytes: %d, First Byte: 0x%02x",count, first_byte[0]);
705
        usb_serial_debug_data (__FILE__, __FUNCTION__, count, first_byte);
706
                
707
        /* send the data out the bulk port */
708
        FILL_BULK_URB(port->write_urb, serial->dev, 
709
                      usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
710
                      port->write_urb->transfer_buffer, count,
711
                      ftdi_write_bulk_callback, port);
712
                
713
        result = usb_submit_urb(port->write_urb);
714
        if (result) {
715
                err(__FUNCTION__ " - failed submitting write urb, error %d", result);
716
                up (&port->sem);
717
                return 0;
718
        }
719
        up (&port->sem);
720

    
721
        dbg(__FUNCTION__ " write returning: %d", count - data_offset);
722
        return (count - data_offset);
723

    
724
} /* ftdi_write */
725

    
726

    
727
static void ftdi_write_bulk_callback (struct urb *urb)
728
{
729
        struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
730
        struct usb_serial *serial;
731

    
732
        dbg(__FUNCTION__);
733

    
734
        if (port_paranoia_check (port, "ftdi_write_bulk_callback")) {
735
                return;
736
        }
737
        
738
        serial = port->serial;
739
        if (serial_paranoia_check (serial, "ftdi_write_bulk_callback")) {
740
                return;
741
        }
742
        
743
        if (urb->status) {
744
                dbg("nonzero write bulk status received: %d", urb->status);
745
                return;
746
        }
747
        queue_task(&port->tqueue, &tq_immediate);
748
        mark_bh(IMMEDIATE_BH);
749

    
750
        return;
751
} /* ftdi_write_bulk_callback */
752

    
753

    
754
static int ftdi_write_room( struct usb_serial_port *port )
755
{
756
        struct ftdi_private *priv = (struct ftdi_private *)port->private;
757
        int room;
758

    
759
        if ( port->write_urb->status == -EINPROGRESS) {
760
                /* There is a race here with the _write routines but it won't hurt */
761
                room = 0;
762
        } else { 
763
                room = port->bulk_out_size - priv->write_offset;
764
        }
765
        return(room);
766
} /* ftdi_write_room */
767

    
768

    
769
static void ftdi_read_bulk_callback (struct urb *urb)
770
{ /* ftdi_read_bulk_callback */
771
        struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
772
        struct usb_serial *serial;
773
               struct tty_struct *tty = port->tty ;
774
        struct ftdi_private *priv = (struct ftdi_private *) port->private;
775
        char error_flag;
776
               unsigned char *data = urb->transfer_buffer;
777

    
778
        const int data_offset = 2;
779
        int i;
780
        int result;
781

    
782
        dbg(__FUNCTION__ " - port %d", port->number);
783

    
784
        if (port_paranoia_check (port, "ftdi_sio_read_bulk_callback")) {
785
                return;
786
        }
787

    
788
        serial = port->serial;
789
        if (serial_paranoia_check (serial, "ftdi_sio_read_bulk_callback")) {
790
                return;
791
        }
792

    
793
        if (urb->status) {
794
                /* This will happen at close every time so it is a dbg not an err */
795
                dbg("nonzero read bulk status received: %d", urb->status);
796
                return;
797
        }
798

    
799
        if (urb->actual_length > 2) {
800
                usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
801
        } else {
802
                dbg("Just status 0o%03o0o%03o",data[0],data[1]);
803
        }
804

    
805

    
806
        /* TO DO -- check for hung up line and handle appropriately: */
807
        /*   send hangup  */
808
        /* See acm.c - you do a tty_hangup  - eg tty_hangup(tty) */
809
        /* if CD is dropped and the line is not CLOCAL then we should hangup */
810

    
811
        /* Compare new line status to the old one, signal if different */
812
        if (priv != NULL) {
813
                char new_status = data[0] & FTDI_STATUS_B0_MASK;
814
                if (new_status != priv->prev_status) {
815
                        priv->diff_status |= new_status ^ priv->prev_status;
816
                        wake_up_interruptible(&priv->delta_msr_wait);
817
                        priv->prev_status = new_status;
818
                }
819
        }
820

    
821
        /* Handle errors and break */
822
        error_flag = TTY_NORMAL;
823
        /* Although the device uses a bitmask and hence can have multiple */
824
        /* errors on a packet - the order here sets the priority the */
825
        /* error is returned to the tty layer  */
826
        
827
        if ( data[1] & FTDI_RS_OE ) { 
828
                error_flag = TTY_OVERRUN;
829
                dbg("OVERRRUN error");
830
        }
831
        if ( data[1] & FTDI_RS_BI ) { 
832
                error_flag = TTY_BREAK;
833
                dbg("BREAK received");
834
        }
835
        if ( data[1] & FTDI_RS_PE ) { 
836
                error_flag = TTY_PARITY;
837
                dbg("PARITY error");
838
        }
839
        if ( data[1] & FTDI_RS_FE ) { 
840
                error_flag = TTY_FRAME;
841
                dbg("FRAMING error");
842
        }
843
        if (urb->actual_length > data_offset) {
844

    
845
                for (i = data_offset ; i < urb->actual_length ; ++i) {
846
                        /* have to make sure we don't overflow the buffer
847
                          with tty_insert_flip_char's */
848
                        if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
849
                                tty_flip_buffer_push(tty);
850
                        }
851
                        /* Note that the error flag is duplicated for 
852
                           every character received since we don't know
853
                           which character it applied to */
854
                        tty_insert_flip_char(tty, data[i], error_flag);
855
                }
856
                  tty_flip_buffer_push(tty);
857

    
858

    
859
        } 
860

    
861
#ifdef NOT_CORRECT_BUT_KEEPING_IT_FOR_NOW
862
        /* if a parity error is detected you get status packets forever
863
           until a character is sent without a parity error.
864
           This doesn't work well since the application receives a never
865
           ending stream of bad data - even though new data hasn't been sent.
866
           Therefore I (bill) have taken this out.
867
           However - this might make sense for framing errors and so on 
868
           so I am leaving the code in for now.
869
        */
870
      else {
871
                if (error_flag != TTY_NORMAL){
872
                        dbg("error_flag is not normal");
873
                                /* In this case it is just status - if that is an error send a bad character */
874
                                if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
875
                                        tty_flip_buffer_push(tty);
876
                                }
877
                                tty_insert_flip_char(tty, 0xff, error_flag);
878
                                tty_flip_buffer_push(tty);
879
                }
880
        }
881
#endif
882

    
883
        /* Continue trying to always read  */
884
        FILL_BULK_URB(port->read_urb, serial->dev, 
885
                      usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
886
                      port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
887
                      ftdi_read_bulk_callback, port);
888

    
889
        result = usb_submit_urb(port->read_urb);
890
        if (result)
891
                err(__FUNCTION__ " - failed resubmitting read urb, error %d", result);
892

    
893
        return;
894
} /* ftdi_read_bulk_callback */
895

    
896

    
897
static void ftdi_break_ctl( struct usb_serial_port *port, int break_state )
898
{
899
        struct usb_serial *serial = port->serial;
900
        struct ftdi_private *priv = (struct ftdi_private *)port->private;
901
        __u16 urb_value = 0; 
902
        char buf[1];
903
        
904
        /* break_state = -1 to turn on break, and 0 to turn off break */
905
        /* see drivers/char/tty_io.c to see it used */
906
        /* last_set_data_urb_value NEVER has the break bit set in it */
907

    
908
        if (break_state) {
909
                urb_value = priv->last_set_data_urb_value | FTDI_SIO_SET_BREAK;
910
        } else {
911
                urb_value = priv->last_set_data_urb_value; 
912
        }
913

    
914
        
915
        if (usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
916
                            FTDI_SIO_SET_DATA_REQUEST, 
917
                            FTDI_SIO_SET_DATA_REQUEST_TYPE,
918
                            urb_value , 0,
919
                            buf, 0, WDR_TIMEOUT) < 0) {
920
                err(__FUNCTION__ " FAILED to enable/disable break state (state was %d)",break_state);
921
        }           
922

    
923
        dbg(__FUNCTION__ " break state is %d - urb is %d",break_state, urb_value);
924
        
925
}
926

    
927

    
928
/* old_termios contains the original termios settings and tty->termios contains
929
 * the new setting to be used
930
 * WARNING: set_termios calls this with old_termios in kernel space
931
 */
932

    
933
static void ftdi_set_termios (struct usb_serial_port *port, struct termios *old_termios)
934
{ /* ftdi_termios */
935
        struct usb_serial *serial = port->serial;
936
        unsigned int cflag = port->tty->termios->c_cflag;
937
        struct ftdi_private *priv = (struct ftdi_private *)port->private;        
938
        __u16 urb_value; /* will hold the new flags */
939
        char buf[1]; /* Perhaps I should dynamically alloc this? */
940
        
941
        
942
        dbg(__FUNCTION__);
943

    
944

    
945
        /* FIXME -For this cut I don't care if the line is really changing or 
946
           not  - so just do the change regardless  - should be able to 
947
           compare old_termios and tty->termios */
948
        /* NOTE These routines can get interrupted by 
949
           ftdi_sio_read_bulk_callback  - need to examine what this 
950
           means - don't see any problems yet */
951
        
952
        /* Set number of data bits, parity, stop bits */
953
        
954
        urb_value = 0;
955
        urb_value |= (cflag & CSTOPB ? FTDI_SIO_SET_DATA_STOP_BITS_2 :
956
                      FTDI_SIO_SET_DATA_STOP_BITS_1);
957
        urb_value |= (cflag & PARENB ? 
958
                      (cflag & PARODD ? FTDI_SIO_SET_DATA_PARITY_ODD : 
959
                       FTDI_SIO_SET_DATA_PARITY_EVEN) :
960
                      FTDI_SIO_SET_DATA_PARITY_NONE);
961
        if (cflag & CSIZE) {
962
                switch (cflag & CSIZE) {
963
                case CS5: urb_value |= 5; dbg("Setting CS5"); break;
964
                case CS6: urb_value |= 6; dbg("Setting CS6"); break;
965
                case CS7: urb_value |= 7; dbg("Setting CS7"); break;
966
                case CS8: urb_value |= 8; dbg("Setting CS8"); break;
967
                default:
968
                        err("CSIZE was set but not CS5-CS8");
969
                }
970
        }
971

    
972
        /* This is needed by the break command since it uses the same command - but is
973
         *  or'ed with this value  */
974
        priv->last_set_data_urb_value = urb_value;
975
        
976
        if (usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
977
                            FTDI_SIO_SET_DATA_REQUEST, 
978
                            FTDI_SIO_SET_DATA_REQUEST_TYPE,
979
                            urb_value , 0,
980
                            buf, 0, 100) < 0) {
981
                err(__FUNCTION__ " FAILED to set databits/stopbits/parity");
982
        }           
983

    
984
        /* Now do the baudrate */
985
        if ((cflag & CBAUD) == B0 ) {
986
                /* Disable flow control */
987
                if (usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
988
                                    FTDI_SIO_SET_FLOW_CTRL_REQUEST, 
989
                                    FTDI_SIO_SET_FLOW_CTRL_REQUEST_TYPE,
990
                                    0, 0, 
991
                                    buf, 0, WDR_TIMEOUT) < 0) {
992
                        err(__FUNCTION__ " error from disable flowcontrol urb");
993
                }            
994
                /* Drop RTS and DTR */
995
                if (set_dtr(serial->dev, usb_sndctrlpipe(serial->dev, 0),LOW) < 0){
996
                        err(__FUNCTION__ " Error from DTR LOW urb");
997
                }
998
                if (set_rts(serial->dev, usb_sndctrlpipe(serial->dev, 0),LOW) < 0){
999
                        err(__FUNCTION__ " Error from RTS LOW urb");
1000
                }        
1001
                
1002
        } else {
1003
                /* set the baudrate determined before */
1004
                if (change_speed(port)) {
1005
                        err(__FUNCTION__ " urb failed to set baurdrate");
1006
                }
1007
        }
1008

    
1009
        /* Set flow control */
1010
        /* Note device also supports DTR/CD (ugh) and Xon/Xoff in hardware */
1011
        if (cflag & CRTSCTS) {
1012
                dbg(__FUNCTION__ " Setting to CRTSCTS flow control");
1013
                if (usb_control_msg(serial->dev, 
1014
                                    usb_sndctrlpipe(serial->dev, 0),
1015
                                    FTDI_SIO_SET_FLOW_CTRL_REQUEST, 
1016
                                    FTDI_SIO_SET_FLOW_CTRL_REQUEST_TYPE,
1017
                                    0 , FTDI_SIO_RTS_CTS_HS,
1018
                                    buf, 0, WDR_TIMEOUT) < 0) {
1019
                        err("urb failed to set to rts/cts flow control");
1020
                }                
1021
                
1022
        } else { 
1023
                /* CHECKME Assuming XON/XOFF handled by tty stack - not by device */
1024
                dbg(__FUNCTION__ " Turning off hardware flow control");
1025
                if (usb_control_msg(serial->dev, 
1026
                                    usb_sndctrlpipe(serial->dev, 0),
1027
                                    FTDI_SIO_SET_FLOW_CTRL_REQUEST, 
1028
                                    FTDI_SIO_SET_FLOW_CTRL_REQUEST_TYPE,
1029
                                    0, 0, 
1030
                                    buf, 0, WDR_TIMEOUT) < 0) {
1031
                        err("urb failed to clear flow control");
1032
                }                                
1033
                
1034
        }
1035
        return;
1036
} /* ftdi_termios */
1037

    
1038

    
1039
static int ftdi_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
1040
{
1041
        struct usb_serial *serial = port->serial;
1042
        struct ftdi_private *priv = (struct ftdi_private *)port->private;
1043

    
1044
        __u16 urb_value=0; /* Will hold the new flags */
1045
        char buf[2];
1046
        int  ret, mask;
1047
        
1048
        dbg(__FUNCTION__ " cmd 0x%04x", cmd);
1049

    
1050
        /* Based on code from acm.c and others */
1051
        switch (cmd) {
1052

    
1053
        case TIOCMGET:
1054
                dbg(__FUNCTION__ " TIOCMGET");
1055
                switch (priv->chip_type) {
1056
                case SIO:
1057
                        /* Request the status from the device */
1058
                        if ((ret = usb_control_msg(serial->dev, 
1059
                                                   usb_rcvctrlpipe(serial->dev, 0),
1060
                                                   FTDI_SIO_GET_MODEM_STATUS_REQUEST, 
1061
                                                   FTDI_SIO_GET_MODEM_STATUS_REQUEST_TYPE,
1062
                                                   0, 0, 
1063
                                                   buf, 1, WDR_TIMEOUT)) < 0 ) {
1064
                                err(__FUNCTION__ " Could not get modem status of device - err: %d",
1065
                                    ret);
1066
                                return(ret);
1067
                        }
1068
                        break;
1069
                case FT8U232AM:
1070
                        /* the 8U232AM returns a two byte value (the sio is a 1 byte value) - in the same
1071
                           format as the data returned from the in point */
1072
                        if ((ret = usb_control_msg(serial->dev, 
1073
                                                   usb_rcvctrlpipe(serial->dev, 0),
1074
                                                   FTDI_SIO_GET_MODEM_STATUS_REQUEST, 
1075
                                                   FTDI_SIO_GET_MODEM_STATUS_REQUEST_TYPE,
1076
                                                   0, 0, 
1077
                                                   buf, 2, WDR_TIMEOUT)) < 0 ) {
1078
                                err(__FUNCTION__ " Could not get modem status of device - err: %d",
1079
                                    ret);
1080
                                return(ret);
1081
                        }
1082
                        break;
1083
                default:
1084
                        return -EFAULT;
1085
                        break;
1086
                }
1087

    
1088
                return put_user((buf[0] & FTDI_SIO_DSR_MASK ? TIOCM_DSR : 0) |
1089
                                (buf[0] & FTDI_SIO_CTS_MASK ? TIOCM_CTS : 0) |
1090
                                (buf[0]  & FTDI_SIO_RI_MASK  ? TIOCM_RI  : 0) |
1091
                                (buf[0]  & FTDI_SIO_RLSD_MASK ? TIOCM_CD  : 0),
1092
                                (unsigned long *) arg);
1093
                break;
1094

    
1095
        case TIOCMSET: /* Turns on and off the lines as specified by the mask */
1096
                dbg(__FUNCTION__ " TIOCMSET");
1097
                if (get_user(mask, (unsigned long *) arg))
1098
                        return -EFAULT;
1099
                urb_value = ((mask & TIOCM_DTR) ? HIGH : LOW);
1100
                if (set_dtr(serial->dev, usb_sndctrlpipe(serial->dev, 0),urb_value) < 0){
1101
                        err("Error from DTR set urb (TIOCMSET)");
1102
                }
1103
                urb_value = ((mask & TIOCM_RTS) ? HIGH : LOW);
1104
                if (set_rts(serial->dev, usb_sndctrlpipe(serial->dev, 0),urb_value) < 0){
1105
                        err("Error from RTS set urb (TIOCMSET)");
1106
                }        
1107
                break;
1108
                                        
1109
        case TIOCMBIS: /* turns on (Sets) the lines as specified by the mask */
1110
                dbg(__FUNCTION__ " TIOCMBIS");
1111
                 if (get_user(mask, (unsigned long *) arg))
1112
                        return -EFAULT;
1113
                  if (mask & TIOCM_DTR){
1114
                        if ((ret = set_dtr(serial->dev, 
1115
                                           usb_sndctrlpipe(serial->dev, 0),
1116
                                           HIGH)) < 0) {
1117
                                err("Urb to set DTR failed");
1118
                                return(ret);
1119
                        }
1120
                }
1121
                if (mask & TIOCM_RTS) {
1122
                        if ((ret = set_rts(serial->dev, 
1123
                                           usb_sndctrlpipe(serial->dev, 0),
1124
                                           HIGH)) < 0){
1125
                                err("Urb to set RTS failed");
1126
                                return(ret);
1127
                        }
1128
                }
1129
                                        break;
1130

    
1131
        case TIOCMBIC: /* turns off (Clears) the lines as specified by the mask */
1132
                dbg(__FUNCTION__ " TIOCMBIC");
1133
                 if (get_user(mask, (unsigned long *) arg))
1134
                        return -EFAULT;
1135
                  if (mask & TIOCM_DTR){
1136
                        if ((ret = set_dtr(serial->dev, 
1137
                                           usb_sndctrlpipe(serial->dev, 0),
1138
                                           LOW)) < 0){
1139
                                err("Urb to unset DTR failed");
1140
                                return(ret);
1141
                        }
1142
                }        
1143
                if (mask & TIOCM_RTS) {
1144
                        if ((ret = set_rts(serial->dev, 
1145
                                           usb_sndctrlpipe(serial->dev, 0),
1146
                                           LOW)) < 0){
1147
                                err("Urb to unset RTS failed");
1148
                                return(ret);
1149
                        }
1150
                }
1151
                break;
1152

    
1153
                /*
1154
                 * I had originally implemented TCSET{A,S}{,F,W} and
1155
                 * TCGET{A,S} here separately, however when testing I
1156
                 * found that the higher layers actually do the termios
1157
                 * conversions themselves and pass the call onto
1158
                 * ftdi_sio_set_termios. 
1159
                 *
1160
                 */
1161

    
1162
        case TIOCGSERIAL: /* gets serial port data */
1163
                return get_serial_info(port, (struct serial_struct *) arg);
1164

    
1165
        case TIOCSSERIAL: /* sets serial port data */
1166
                return set_serial_info(port, (struct serial_struct *) arg);
1167

    
1168
        /*
1169
         * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1170
         * - mask passed in arg for lines of interest
1171
         *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1172
         * Caller should use TIOCGICOUNT to see which one it was.
1173
         *
1174
         * This code is borrowed from linux/drivers/char/serial.c
1175
         */
1176
        case TIOCMIWAIT:
1177
                while (priv != NULL) {
1178
                        interruptible_sleep_on(&priv->delta_msr_wait);
1179
                        /* see if a signal did it */
1180
                        if (signal_pending(current))
1181
                                return -ERESTARTSYS;
1182
                        else {
1183
                                char diff = priv->diff_status;
1184

    
1185
                                if (diff == 0) {
1186
                                        return -EIO; /* no change => error */
1187
                                }
1188

    
1189
                                /* Consume all events */
1190
                                priv->diff_status = 0;
1191

    
1192
                                /* Return 0 if caller wanted to know about these bits */
1193
                                if ( ((arg & TIOCM_RNG) && (diff & FTDI_RS0_RI)) ||
1194
                                     ((arg & TIOCM_DSR) && (diff & FTDI_RS0_DSR)) ||
1195
                                     ((arg & TIOCM_CD)  && (diff & FTDI_RS0_RLSD)) ||
1196
                                     ((arg & TIOCM_CTS) && (diff & FTDI_RS0_CTS)) ) {
1197
                                        return 0;
1198
                                }
1199
                                /*
1200
                                 * Otherwise caller can't care less about what happened,
1201
                                 * and so we continue to wait for more events.
1202
                                 */
1203
                        }
1204
                }
1205
                /* NOTREACHED */
1206

    
1207
        default:
1208
          /* This is not an error - turns out the higher layers will do 
1209
           *  some ioctls itself (see comment above)
1210
            */
1211
                dbg(__FUNCTION__ " arg not supported - it was 0x%04x",cmd);
1212
                return(-ENOIOCTLCMD);
1213
                break;
1214
        }
1215
        return 0;
1216
} /* ftdi_ioctl */
1217

    
1218

    
1219
static int __init ftdi_init (void)
1220
{
1221
        dbg(__FUNCTION__);
1222
        usb_serial_register (&ftdi_SIO_device);
1223
        usb_serial_register (&ftdi_8U232AM_device);
1224
        info(DRIVER_VERSION ":" DRIVER_DESC);
1225
        return 0;
1226
}
1227

    
1228

    
1229
static void __exit ftdi_exit (void)
1230
{
1231
        dbg(__FUNCTION__);
1232
        usb_serial_deregister (&ftdi_SIO_device);
1233
        usb_serial_deregister (&ftdi_8U232AM_device);
1234
}
1235

    
1236

    
1237
module_init(ftdi_init);
1238
module_exit(ftdi_exit);
1239

    
1240
MODULE_AUTHOR( DRIVER_AUTHOR );
1241
MODULE_DESCRIPTION( DRIVER_DESC );
1242
MODULE_LICENSE("GPL");
1243

    
1244
MODULE_PARM(debug, "i");
1245
MODULE_PARM_DESC(debug, "Debug enabled or not");
1246