v2.4.9.9 -> v2.4.9.10
[opensuse:kernel.git] / drivers / usb / serial / io_edgeport.c
1 /*
2  * Edgeport USB Serial Converter driver
3  *
4  * Copyright(c) 2000 Inside Out Networks, All rights reserved.
5  * Copyright(c) 2001 Greg Kroah-Hartman <greg@kroah.com>
6  *
7  *      This program is free software; you can redistribute it and/or modify
8  *      it under the terms of the GNU General Public License as published by
9  *      the Free Software Foundation; either version 2 of the License, or
10  *      (at your option) any later version.
11  *
12  * Supports the following devices:
13  *      Edgeport/4
14  *      Edgeport/4t
15  *      Edgeport/2
16  *      Edgeport/4i
17  *      Edgeport/2i
18  *      Edgeport/421
19  *      Edgeport/21
20  *      Rapidport/4
21  *      Edgeport/8
22  *      Edgeport/2D8
23  *      Edgeport/4D8
24  *      Edgeport/8i
25  *
26  * Version history:
27  * 
28  * 2.1 2001_07_09 greg kroah-hartman
29  *      - added support for TIOCMBIS and TIOCMBIC.
30  *
31  *     (04/08/2001) gb
32  *      - Identify version on module load.
33  *
34  * 2.0 2001_03_05 greg kroah-hartman
35  *      - reworked entire driver to fit properly in with the other usb-serial
36  *        drivers.  Occasional oopses still happen, but it's a good start.
37  *
38  * 1.2.3 (02/23/2001) greg kroah-hartman
39  *      - changed device table to work properly for 2.4.x final format.
40  *      - fixed problem with dropping data at high data rates.
41  *
42  * 1.2.2 (11/27/2000) greg kroah-hartman
43  *      - cleaned up more NTisms.
44  *      - Added device table for 2.4.0-test11
45  *
46  * 1.2.1 (11/08/2000) greg kroah-hartman
47  *      - Started to clean up NTisms.
48  *      - Fixed problem with dev field of urb for kernels >= 2.4.0-test9
49  *
50  * 1.2 (10/17/2000) David Iacovelli
51  *      Remove all EPIC code and GPL source
52  *  Fix RELEVANT_IFLAG macro to include flow control 
53  *  changes port configuration changes.
54  *  Fix redefinition of SERIAL_MAGIC
55  *  Change all timeout values to 5 seconds
56  *  Tried to fix the UHCI multiple urb submission, but failed miserably.
57  *  it seems to work fine with OHCI.
58  *  ( Greg take a look at the #if 0 at end of WriteCmdUsb() we must 
59  *    find a way to work arount this UHCI bug )
60  *
61  * 1.1 (10/11/2000) David Iacovelli
62  *  Fix XON/XOFF flow control to support both IXON and IXOFF
63  *
64  * 0.9.27 (06/30/2000) David Iacovelli
65  *  Added transmit queue and now allocate urb for command writes.
66  *
67  * 0.9.26 (06/29/2000) David Iacovelli
68  *  Add support for 80251 based edgeport
69  *
70  * 0.9.25 (06/27/2000) David Iacovelli
71  *  Do not close the port if it has multiple opens.
72  *
73  * 0.9.24 (05/26/2000) David Iacovelli
74  *  Add IOCTLs to support RXTX and JAVA POS 
75  *  and first cut at running BlackBox Demo
76  *
77  * 0.9.23 (05/24/2000) David Iacovelli
78  *  Add IOCTLs to support RXTX and JAVA POS
79  *
80  * 0.9.22 (05/23/2000) David Iacovelli
81  *  fixed bug in enumeration.  If epconfig turns on mapping by
82  *  path after a device is already plugged in, we now update
83  *  the mapping correctly
84  *
85  * 0.9.21 (05/16/2000) David Iacovelli
86  *  Added BlockUntilChaseResp() to also wait for txcredits
87  *  Updated the way we allocate and handle write URBs 
88  *      Add debug code to dump buffers
89  *
90  * 0.9.20 (05/01/2000) David Iacovelli
91  *      change driver to use usb/tts/
92  *
93  * 0.9.19 (05/01/2000) David Iacovelli
94  *  Update code to compile if DEBUG is off
95  *
96  * 0.9.18 (04/28/2000) David Iacovelli
97  *  cleanup and test tty_register with devfs
98  *
99  * 0.9.17 (04/27/2000) greg kroah-hartman
100  *      changed tty_register around to be like the way it
101  *      was before, but now it works properly with devfs.
102  *
103  * 0.9.16 (04/26/2000) david iacovelli
104  *  Fixed bug in GetProductInfo()
105  *
106  * 0.9.15 (04/25/2000) david iacovelli
107  *      Updated enumeration
108  *
109  * 0.9.14 (04/24/2000) david iacovelli
110  *  Removed all config/status IOCTLS and 
111  *  converted to using /proc/edgeport
112  *  still playing with devfs
113  *
114  * 0.9.13 (04/24/2000) david iacovelli
115  *  Removed configuration based on ttyUSB0
116  *  Added support for configuration using /prod/edgeport
117  *  first attempt at using devfs (not working yet!)
118  *  Added IOCTL to GetProductInfo()
119  *  Added support for custom baud rates
120  *      Add support for random port numbers
121  *
122  * 0.9.12 (04/18/2000) david iacovelli
123  *      added additional configuration IOCTLs
124  *  use ttyUSB0 for configuration
125  *
126  * 0.9.11 (04/17/2000) greg kroah-hartman
127  *      fixed module initialization race conditions.
128  *      made all urbs dynamically allocated.
129  *      made driver devfs compatible. now it only registers the tty device
130  *      when the device is actually plugged in.
131  *
132  * 0.9.10 (04/13/2000) greg kroah-hartman
133  *      added proc interface framework.
134  *
135  * 0.9.9 (04/13/2000) david iacovelli
136  *      added enumeration code and ioctls to configure the device
137  *
138  * 0.9.8 (04/12/2000) david iacovelli
139  *  Change interrupt read start when device is plugged in
140  *  and stop when device is removed
141  *      process interrupt reads when all ports are closed 
142  *  (keep value of rxBytesAvail consistent with the edgeport)
143  *  set the USB_BULK_QUEUE flag so that we can shove a bunch 
144  *  of urbs at once down the pipe 
145  *
146  * 0.9.7 (04/10/2000) david iacovelli
147  *      start to add enumeration code.
148  *  generate serial number for epic devices
149  *  add support for kdb
150  *
151  * 0.9.6 (03/30/2000) david iacovelli
152  *  add IOCTL to get string, manufacture, and boot descriptors
153  *
154  * 0.9.5 (03/14/2000) greg kroah-hartman
155  *      more error checking added to SerialOpen to try to fix UHCI open problem
156  *
157  * 0.9.4 (03/09/2000) greg kroah-hartman
158  *      added more error checking to handle oops when data is hanging
159  *      around and tty is abruptly closed.
160  *
161  * 0.9.3 (03/09/2000) david iacovelli
162  *      Add epic support for xon/xoff chars
163  *      play with performance
164  *
165  * 0.9.2 (03/08/2000) greg kroah-hartman
166  *      changed most "info" calls to "dbg"
167  *      implemented flow control properly in the termios call
168  *
169  * 0.9.1 (03/08/2000) david iacovelli
170  *      added EPIC support
171  *      enabled bootloader update
172  *
173  * 0.9 (03/08/2000) greg kroah-hartman
174  *      Release to IO networks.
175  *      Integrated changes that David made
176  *  made getting urbs for writing SMP safe
177  *
178  * 0.8 (03/07/2000) greg kroah-hartman
179  *      Release to IO networks.
180  *      Fixed problems that were seen in code by David.
181  *  Now both Edgeport/4 and Edgeport/2 works properly.
182  *  Changed most of the functions to use port instead of serial.
183  *
184  * 0.7 (02/27/2000) greg kroah-hartman
185  *      Milestone 3 release.
186  *      Release to IO Networks
187  *      ioctl for waiting on line change implemented.
188  *      ioctl for getting statistics implemented.
189  *      multiport support working.
190  *      lsr and msr registers are now handled properly.
191  *      change break now hooked up and working.
192  *      support for all known Edgeport devices.
193  *
194  * 0.6 (02/22/2000) greg kroah-hartman
195  *      Release to IO networks.
196  *      CHASE is implemented correctly when port is closed.
197  *      SerialOpen now blocks correctly until port is fully opened.
198  *
199  * 0.5 (02/20/2000) greg kroah-hartman
200  *      Release to IO networks.
201  *      Known problems:
202  *              modem status register changes are not sent on to the user
203  *              CHASE is not implemented when the port is closed.
204  *
205  * 0.4 (02/16/2000) greg kroah-hartman
206  *      Second cut at the CeBit demo.
207  *      Doesn't leak memory on every write to the port
208  *      Still small leaks on startup.
209  *      Added support for Edgeport/2 and Edgeport/8
210  *
211  * 0.3 (02/15/2000) greg kroah-hartman
212  *      CeBit demo release.
213  *      Force the line settings to 4800, 8, 1, e for the demo.
214  *      Warning! This version leaks memory like crazy!
215  *
216  * 0.2 (01/30/2000) greg kroah-hartman
217  *      Milestone 1 release.
218  *      Device is found by USB subsystem, enumerated, fimware is downloaded
219  *      and the descriptors are printed to the debug log, config is set, and
220  *      green light starts to blink. Open port works, and data can be sent
221  *      and received at the default settings of the UART. Loopback connector
222  *      and debug log confirms this.
223  * 
224  * 0.1 (01/23/2000) greg kroah-hartman
225  *      Initial release to help IO Networks try to set up their test system. 
226  *      Edgeport4 is recognized, firmware is downloaded, config is set so 
227  *      device blinks green light every 3 sec. Port is bound, but opening,
228  *      closing, and sending data do not work properly.
229  * 
230  */
231
232 #include <linux/config.h>
233 #include <linux/kernel.h>
234 #include <linux/sched.h>
235 #include <linux/signal.h>
236 #include <linux/errno.h>
237 #include <linux/poll.h>
238 #include <linux/init.h>
239 #include <linux/slab.h>
240 #include <linux/fcntl.h>
241 #include <linux/tty.h>
242 #include <linux/tty_driver.h>
243 #include <linux/tty_flip.h>
244 #include <linux/module.h>
245 #include <linux/spinlock.h>
246 #include <linux/serial.h>
247 #include <linux/ioctl.h>
248 #include <linux/proc_fs.h>
249 #include <linux/usb.h>
250
251 #ifdef CONFIG_USB_SERIAL_DEBUG
252         static int debug = 1;
253 #else
254         static int debug;
255 #endif
256
257 #include "usb-serial.h"
258
259 #include "io_edgeport.h"
260 #include "io_ionsp.h"           /* info for the iosp messages */
261 #include "io_16654.h"           /* 16654 UART defines */
262
263 /*
264  * Version Information
265  */
266 #define DRIVER_VERSION "v2.1"
267 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
268 #define DRIVER_DESC "Edgeport USB Serial Driver"
269
270 /* First, the latest boot code - for first generation edgeports */
271 #define IMAGE_ARRAY_NAME        BootCodeImage_GEN1
272 #define IMAGE_VERSION_NAME      BootCodeImageVersion_GEN1
273 #include "io_fw_boot.h"         /* the bootloader firmware to download to a device, if it needs it */
274
275 /* for second generation edgeports */
276 #define IMAGE_ARRAY_NAME        BootCodeImage_GEN2
277 #define IMAGE_VERSION_NAME      BootCodeImageVersion_GEN2
278 #include "io_fw_boot2.h"        /* the bootloader firmware to download to a device, if it needs it */
279
280 /* Then finally the main run-time operational code - for first generation edgeports */
281 #define IMAGE_ARRAY_NAME        OperationalCodeImage_GEN1
282 #define IMAGE_VERSION_NAME      OperationalCodeImageVersion_GEN1
283 #include "io_fw_down.h"         /* Define array OperationalCodeImage[] */
284
285 /* for second generation edgeports */
286 #define IMAGE_ARRAY_NAME        OperationalCodeImage_GEN2
287 #define IMAGE_VERSION_NAME      OperationalCodeImageVersion_GEN2
288 #include "io_fw_down2.h"        /* Define array OperationalCodeImage[] */
289
290
291 #define MAX_NAME_LEN            64
292
293
294 #define CHASE_TIMEOUT           (5*HZ)          /* 5 seconds */
295 #define OPEN_TIMEOUT            (5*HZ)          /* 5 seconds */
296 #define COMMAND_TIMEOUT         (5*HZ)          /* 5 seconds */
297
298 #ifndef SERIAL_MAGIC
299         #define SERIAL_MAGIC    0x6702
300 #endif
301 #define PORT_MAGIC              0x7301
302
303
304 /* receive port state */
305 enum RXSTATE {
306         EXPECT_HDR1 = 0,        /* Expect header byte 1 */
307         EXPECT_HDR2 = 1,        /* Expect header byte 2 */
308         EXPECT_DATA = 2,        /* Expect 'RxBytesRemaining' data */
309         EXPECT_HDR3 = 3,        /* Expect header byte 3 (for status hdrs only) */
310 };
311
312
313 /* the info for all of the devices that this driver supports */
314 int EdgeportDevices[] = EDGEPORT_DEVICE_IDS;
315 #define NUM_EDGEPORT_DEVICES (sizeof(EdgeportDevices) / sizeof(int))
316
317
318 /* Transmit Fifo 
319  * This Transmit queue is an extension of the edgeport Rx buffer. 
320  * The maximum amount of data buffered in both the edgeport 
321  * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
322  */
323 struct TxFifo {
324         unsigned int    head;   /* index to head pointer (write) */
325         unsigned int    tail;   /* index to tail pointer (read)  */
326         unsigned int    count;  /* Bytes in queue */
327         unsigned int    size;   /* Max size of queue (equal to Max number of TxCredits) */
328         unsigned char   *fifo;  /* allocated Buffer */
329 };
330
331 /* This structure holds all of the local port information */
332 struct edgeport_port {
333         __u16                   txCredits;              /* our current credits for this port */
334         __u16                   maxTxCredits;           /* the max size of the port */
335
336         struct TxFifo           txfifo;                 /* transmit fifo -- size will be maxTxCredits */
337         struct urb              *write_urb;             /* write URB for this port */
338         char                    write_in_progress;      /* TRUE while a write URB is outstanding */
339
340         __u8                    shadowLCR;              /* last LCR value received */
341         __u8                    shadowMCR;              /* last MCR value received */
342         __u8                    shadowMSR;              /* last MSR value received */
343         __u8                    shadowLSR;              /* last LSR value received */
344         __u8                    shadowXonChar;          /* last value set as XON char in Edgeport */
345         __u8                    shadowXoffChar;         /* last value set as XOFF char in Edgeport */
346         __u8                    validDataMask;
347         __u32                   baudRate;
348
349         char                    open;
350         char                    openPending;
351         char                    commandPending;
352         char                    closePending;
353         char                    chaseResponsePending;
354
355         wait_queue_head_t       wait_chase;             /* for handling sleeping while waiting for chase to finish */
356         wait_queue_head_t       wait_open;              /* for handling sleeping while waiting for open to finish */
357         wait_queue_head_t       wait_command;           /* for handling sleeping while waiting for command to finish */
358         wait_queue_head_t       delta_msr_wait;         /* for handling sleeping while waiting for msr change to happen */
359
360         struct async_icount     icount;
361         struct usb_serial_port  *port;                  /* loop back to the owner of this object */
362 };
363
364
365 /* This structure holds all of the individual device information */
366 struct edgeport_serial {
367         char                    name[MAX_NAME_LEN+1];           /* string name of this device */
368
369         EDGE_MANUF_DESCRIPTOR           manuf_descriptor;       /* the manufacturer descriptor */
370         EDGE_BOOT_DESCRIPTOR            boot_descriptor;        /* the boot firmware descriptor */
371         struct edgeport_product_info    product_info;           /* Product Info */
372
373         __u8                    interrupt_in_endpoint;          /* the interrupt endpoint handle */
374         unsigned char *         interrupt_in_buffer;            /* the buffer we use for the interrupt endpoint */
375         struct urb *            interrupt_read_urb;             /* our interrupt urb */
376
377         __u8                    bulk_in_endpoint;               /* the bulk in endpoint handle */
378         unsigned char *         bulk_in_buffer;                 /* the buffer we use for the bulk in endpoint */
379         struct urb *            read_urb;                       /* our bulk read urb */
380
381         __u8                    bulk_out_endpoint;              /* the bulk out endpoint handle */
382
383         __s16                   rxBytesAvail;                   /* the number of bytes that we need to read from this device */
384
385         enum RXSTATE            rxState;                        /* the current state of the bulk receive processor */
386         __u8                    rxHeader1;                      /* receive header byte 1 */
387         __u8                    rxHeader2;                      /* receive header byte 2 */
388         __u8                    rxHeader3;                      /* receive header byte 3 */
389         __u8                    rxPort;                         /* the port that we are currently receiving data for */
390         __u8                    rxStatusCode;                   /* the receive status code */
391         __u8                    rxStatusParam;                  /* the receive status paramater */
392         __s16                   rxBytesRemaining;               /* the number of port bytes left to read */
393         struct usb_serial       *serial;                        /* loop back to the owner of this object */
394 };
395
396 /* baud rate information */
397 typedef struct _DIVISOR_TABLE_ENTRY {
398         __u32   BaudRate;
399         __u16  Divisor;
400 } DIVISOR_TABLE_ENTRY, *PDIVISOR_TABLE_ENTRY;
401
402 //
403 // Define table of divisors for Rev A EdgePort/4 hardware
404 // These assume a 3.6864MHz crystal, the standard /16, and
405 // MCR.7 = 0.
406 //
407 static DIVISOR_TABLE_ENTRY  DivisorTable[] = {
408         {   75,         3072},  
409         {   110,        2095},          /* 2094.545455 => 230450   => .0217 % over */
410         {   134,        1713},          /* 1713.011152 => 230398.5 => .00065% under */
411         {   150,        1536},
412         {   300,        768},
413         {   600,        384},
414         {   1200,       192},
415         {   1800,       128},
416         {   2400,       96},
417         {   4800,       48},
418         {   7200,       32},
419         {   9600,       24},
420         {   14400,      16},
421         {   19200,      12},
422         {   38400,      6},
423         {   57600,      4},
424         {   115200,     2},
425         {   230400,     1},
426 };
427
428 /* local variables */
429 static int      CmdUrbs = 0;                                                    /* Number of outstanding Command Write Urbs */
430
431
432 /* local function prototypes */
433
434 /* function prototypes for all URB callbacks */
435 static void edge_interrupt_callback     (struct urb *urb);
436 static void edge_bulk_in_callback               (struct urb *urb);
437 static void edge_bulk_out_data_callback (struct urb *urb);
438 static void edge_bulk_out_cmd_callback  (struct urb *urb);
439
440 /* function prototypes for the usbserial callbacks */
441 static int  edge_open                   (struct usb_serial_port *port, struct file *filp);
442 static void edge_close                  (struct usb_serial_port *port, struct file *filp);
443 static int  edge_write                  (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count);
444 static int  edge_write_room             (struct usb_serial_port *port);
445 static int  edge_chars_in_buffer        (struct usb_serial_port *port);
446 static void edge_throttle               (struct usb_serial_port *port);
447 static void edge_unthrottle             (struct usb_serial_port *port);
448 static void edge_set_termios            (struct usb_serial_port *port, struct termios *old_termios);
449 static int  edge_ioctl                  (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg);
450 static void edge_break                  (struct usb_serial_port *port, int break_state);
451 static int  edge_startup                (struct usb_serial *serial);
452 static void edge_shutdown               (struct usb_serial *serial);
453
454
455 #include "io_tables.h"  /* all of the devices that this driver supports */
456
457
458 /* function prototypes for all of our local functions */
459 static int  process_rcvd_data           (struct edgeport_serial *edge_serial, unsigned char *buffer, __u16 bufferLength);
460 static void process_rcvd_status         (struct edgeport_serial *edge_serial, __u8 byte2, __u8 byte3);
461 static void handle_new_msr              (struct edgeport_port *edge_port, __u8 newMsr);
462 static void handle_new_lsr              (struct edgeport_port *edge_port, __u8 lsrData, __u8 lsr, __u8 data);
463 static int  send_iosp_ext_cmd           (struct edgeport_port *edge_port, __u8 command, __u8 param);
464 static int  calc_baud_rate_divisor      (int baud_rate, int *divisor);
465 static int  send_cmd_write_baud_rate    (struct edgeport_port *edge_port, int baudRate);
466 static void change_port_settings        (struct edgeport_port *edge_port, struct termios *old_termios);
467 static int  send_cmd_write_uart_register        (struct edgeport_port *edge_port, __u8 regNum, __u8 regValue);
468 static int  write_cmd_usb               (struct edgeport_port *edge_port, unsigned char *buffer, int writeLength);
469 static void send_more_port_data         (struct edgeport_serial *edge_serial, struct edgeport_port *edge_port);
470
471 static int  sram_write                  (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
472 static int  rom_read                    (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
473 static int  rom_write                   (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
474 static void get_manufacturing_desc      (struct edgeport_serial *edge_serial);
475 static void get_boot_desc               (struct edgeport_serial *edge_serial);
476 static void load_application_firmware   (struct edgeport_serial *edge_serial);
477
478
479 static void unicode_to_ascii            (char *string, short *unicode, int unicode_size);
480
481 static int  get_string_desc             (struct usb_device *dev, int Id, struct usb_string_descriptor **pRetDesc);
482
483
484
485
486 // ************************************************************************
487 // ************************************************************************
488 // ************************************************************************
489 // ************************************************************************
490
491 // These functions should be in firmware.c
492
493 /************************************************************************
494  *                                                                      *
495  *      update_edgeport_E2PROM()        Compare current versions of             *
496  *                              Boot ROM and Manufacture                *
497  *                              Descriptors with versions               *
498  *                              embedded in this driver                 *
499  *                                                                      *
500  ************************************************************************/
501 void update_edgeport_E2PROM (struct edgeport_serial *edge_serial)
502 {
503         __u32 BootCurVer;
504         __u32 BootNewVer;
505         __u8 BootMajorVersion;                  
506         __u8 BootMinorVersion;                  
507         __u16 BootBuildNumber;
508         __u8 *BootImage;      
509         __u32 BootSize;
510         PEDGE_FIRMWARE_IMAGE_RECORD record;
511         unsigned char *firmware;
512         int response;
513
514
515         switch (edge_serial->product_info.iDownloadFile) {
516                 case EDGE_DOWNLOAD_FILE_I930:
517                         BootMajorVersion        = BootCodeImageVersion_GEN1.MajorVersion;
518                         BootMinorVersion        = BootCodeImageVersion_GEN1.MinorVersion;
519                         BootBuildNumber         = BootCodeImageVersion_GEN1.BuildNumber;
520                         BootImage               = &BootCodeImage_GEN1[0];
521                         BootSize                = sizeof( BootCodeImage_GEN1 );
522                         break;
523
524                 case EDGE_DOWNLOAD_FILE_80251:
525                         BootMajorVersion        = BootCodeImageVersion_GEN2.MajorVersion;
526                         BootMinorVersion        = BootCodeImageVersion_GEN2.MinorVersion;
527                         BootBuildNumber         = BootCodeImageVersion_GEN2.BuildNumber;
528                         BootImage               = &BootCodeImage_GEN2[0];
529                         BootSize                = sizeof( BootCodeImage_GEN2 );
530                         break;
531
532                 default:
533                         return;
534         }
535
536         // Check Boot Image Version
537         BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
538                      (edge_serial->boot_descriptor.MinorVersion << 16) +
539                       edge_serial->boot_descriptor.BuildNumber;
540
541         BootNewVer = (BootMajorVersion << 24) +
542                      (BootMinorVersion << 16) +
543                       BootBuildNumber;
544
545         dbg("Current Boot Image version %d.%d.%d",
546             edge_serial->boot_descriptor.MajorVersion,
547             edge_serial->boot_descriptor.MinorVersion,
548             edge_serial->boot_descriptor.BuildNumber);
549
550
551         if (BootNewVer > BootCurVer) {
552                 dbg("**Update Boot Image from %d.%d.%d to %d.%d.%d",
553                     edge_serial->boot_descriptor.MajorVersion,
554                     edge_serial->boot_descriptor.MinorVersion,
555                     edge_serial->boot_descriptor.BuildNumber,
556                     BootMajorVersion,
557                     BootMinorVersion,
558                     BootBuildNumber);
559
560
561                 dbg("Downloading new Boot Image");
562
563                 firmware = BootImage;
564
565                 for (;;) {
566                         record = (PEDGE_FIRMWARE_IMAGE_RECORD)firmware;
567                         response = rom_write (edge_serial->serial, record->ExtAddr, record->Addr, record->Len, &record->Data[0]);
568                         if (response < 0) {
569                                 err("sram_write failed (%x, %x, %d)", record->ExtAddr, record->Addr, record->Len);
570                                 break;
571                         }
572                         firmware += sizeof (EDGE_FIRMWARE_IMAGE_RECORD) + record->Len;
573                         if (firmware >= &BootImage[BootSize]) {
574                                 break;
575                         }
576                 }
577         } else {
578                 dbg("Boot Image -- already up to date");
579         }
580 }
581
582
583 /************************************************************************
584  *                                                                      *
585  *  Get string descriptor from device                                   *
586  *                                                                      *
587  ************************************************************************/
588 static int get_string (struct usb_device *dev, int Id, char *string)
589 {
590         struct usb_string_descriptor StringDesc;
591         struct usb_string_descriptor *pStringDesc;
592
593         dbg(__FUNCTION__ " - USB String ID = %d", Id );
594
595         if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc, sizeof(StringDesc))) {
596                 return 0;
597         }
598
599         pStringDesc = kmalloc (StringDesc.bLength, GFP_KERNEL);
600
601         if (!pStringDesc) {
602                 return 0;
603         }
604
605         if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc, StringDesc.bLength )) {
606                 kfree(pStringDesc);
607                 return 0;
608         }
609
610         unicode_to_ascii(string,  pStringDesc->wData,     pStringDesc->bLength/2-1);
611
612         kfree(pStringDesc);
613         return strlen(string);
614 }
615
616
617 /************************************************************************
618  *
619  *  Get string descriptor from device
620  *
621  ************************************************************************/
622 static int get_string_desc (struct usb_device *dev, int Id, struct usb_string_descriptor **pRetDesc)
623 {
624         struct usb_string_descriptor StringDesc;
625         struct usb_string_descriptor *pStringDesc;
626
627         dbg(__FUNCTION__ " - USB String ID = %d", Id );
628
629         if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc, sizeof(StringDesc))) {
630                 return 0;
631         }
632
633         pStringDesc = kmalloc (StringDesc.bLength, GFP_KERNEL);
634
635         if (!pStringDesc) {
636                 return -1;
637         }
638
639         if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc, StringDesc.bLength )) {
640                 kfree(pStringDesc);
641                 return -1;
642         }
643
644         *pRetDesc = pStringDesc;
645         return 0;
646 }
647
648
649
650
651 /************************************************************************
652  *                                                                                                                                              *
653  *                                                                                                                                              *
654  ************************************************************************/
655 static void get_product_info(struct edgeport_serial *edge_serial)
656 {
657         struct edgeport_product_info *product_info = &edge_serial->product_info;
658
659         memset (product_info, 0, sizeof(struct edgeport_product_info));
660
661         product_info->ProductId         = (__u16)(edge_serial->serial->dev->descriptor.idProduct & ~ION_DEVICE_ID_GENERATION_2);
662         product_info->NumPorts          = edge_serial->manuf_descriptor.NumPorts;
663         product_info->ProdInfoVer       = 0;
664
665         product_info->RomSize           = edge_serial->manuf_descriptor.RomSize;
666         product_info->RamSize           = edge_serial->manuf_descriptor.RamSize;
667         product_info->CpuRev            = edge_serial->manuf_descriptor.CpuRev;
668         product_info->BoardRev          = edge_serial->manuf_descriptor.BoardRev;
669
670         product_info->BootMajorVersion  = edge_serial->boot_descriptor.MajorVersion;
671         product_info->BootMinorVersion  = edge_serial->boot_descriptor.MinorVersion;
672         product_info->BootBuildNumber   = edge_serial->boot_descriptor.BuildNumber;
673
674         memcpy(product_info->ManufactureDescDate, edge_serial->manuf_descriptor.DescDate, sizeof(edge_serial->manuf_descriptor.DescDate));
675
676         // check if this is 2nd generation hardware
677         if (edge_serial->serial->dev->descriptor.idProduct & ION_DEVICE_ID_GENERATION_2) {
678                 product_info->FirmwareMajorVersion      = OperationalCodeImageVersion_GEN2.MajorVersion;
679                 product_info->FirmwareMinorVersion      = OperationalCodeImageVersion_GEN2.MinorVersion;
680                 product_info->FirmwareBuildNumber       = OperationalCodeImageVersion_GEN2.BuildNumber;
681                 product_info->iDownloadFile             = EDGE_DOWNLOAD_FILE_80251;
682         } else {
683                 product_info->FirmwareMajorVersion      = OperationalCodeImageVersion_GEN1.MajorVersion;
684                 product_info->FirmwareMinorVersion      = OperationalCodeImageVersion_GEN1.MinorVersion;
685                 product_info->FirmwareBuildNumber       = OperationalCodeImageVersion_GEN1.BuildNumber;
686                 product_info->iDownloadFile             = EDGE_DOWNLOAD_FILE_I930;
687         }
688
689         // Determine Product type and set appropriate flags
690         switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
691                 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
692                 case ION_DEVICE_ID_EDGEPORT_4T:
693                 case ION_DEVICE_ID_EDGEPORT_4:
694                 case ION_DEVICE_ID_EDGEPORT_2:
695                 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
696                 case ION_DEVICE_ID_EDGEPORT_8:
697                 case ION_DEVICE_ID_EDGEPORT_421:
698                 case ION_DEVICE_ID_EDGEPORT_21:
699                 case ION_DEVICE_ID_EDGEPORT_2_DIN:
700                 case ION_DEVICE_ID_EDGEPORT_4_DIN:
701                 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
702                         product_info->IsRS232 = 1;
703                         break;
704
705                 case ION_DEVICE_ID_EDGEPORT_2I:                            // Edgeport/2 RS422/RS485
706                         product_info->IsRS422 = 1;
707                         product_info->IsRS485 = 1;
708                         break;
709
710                 case ION_DEVICE_ID_EDGEPORT_8I:                            // Edgeport/4 RS422
711                 case ION_DEVICE_ID_EDGEPORT_4I:                            // Edgeport/4 RS422
712                         product_info->IsRS422 = 1;
713                         break;
714         }
715
716         // Dump Product Info structure
717         dbg("**Product Information:");
718         dbg("  ProductId             %x", product_info->ProductId );
719         dbg("  NumPorts              %d", product_info->NumPorts );
720         dbg("  ProdInfoVer           %d", product_info->ProdInfoVer );
721         dbg("  IsServer              %d", product_info->IsServer);
722         dbg("  IsRS232               %d", product_info->IsRS232 );
723         dbg("  IsRS422               %d", product_info->IsRS422 );
724         dbg("  IsRS485               %d", product_info->IsRS485 );
725         dbg("  RomSize               %d", product_info->RomSize );
726         dbg("  RamSize               %d", product_info->RamSize );
727         dbg("  CpuRev                %x", product_info->CpuRev  );
728         dbg("  BoardRev              %x", product_info->BoardRev);
729         dbg("  BootMajorVersion      %d.%d.%d", product_info->BootMajorVersion,
730             product_info->BootMinorVersion,
731             product_info->BootBuildNumber);
732         dbg("  FirmwareMajorVersion  %d.%d.%d", product_info->FirmwareMajorVersion,
733             product_info->FirmwareMinorVersion,
734             product_info->FirmwareBuildNumber);
735         dbg("  ManufactureDescDate   %d/%d/%d", product_info->ManufactureDescDate[0],
736             product_info->ManufactureDescDate[1],
737             product_info->ManufactureDescDate[2]+1900);
738         dbg("  iDownloadFile         0x%x",     product_info->iDownloadFile);
739
740 }
741
742
743 /************************************************************************/
744 /************************************************************************/
745 /*            U S B  C A L L B A C K   F U N C T I O N S                */
746 /*            U S B  C A L L B A C K   F U N C T I O N S                */
747 /************************************************************************/
748 /************************************************************************/
749
750 /*****************************************************************************
751  * edge_interrupt_callback
752  *      this is the callback function for when we have received data on the 
753  *      interrupt endpoint.
754  *****************************************************************************/
755 static void edge_interrupt_callback (struct urb *urb)
756 {
757         struct edgeport_serial  *edge_serial = (struct edgeport_serial *)urb->context;
758         struct edgeport_port *edge_port;
759         struct usb_serial_port *port;
760         unsigned char *data = urb->transfer_buffer;
761         int length = urb->actual_length;
762         int bytes_avail;
763         int position;
764         int txCredits;
765         int portNumber;
766         int result;
767
768         dbg(__FUNCTION__);
769
770         if (serial_paranoia_check (edge_serial->serial, __FUNCTION__)) {
771                 return;
772         }
773
774         if (urb->status) {
775                 dbg(__FUNCTION__" - nonzero control read status received: %d", urb->status);
776                 return;
777         }
778
779         // process this interrupt-read even if there are no ports open
780         if (length) {
781                 usb_serial_debug_data (__FILE__, __FUNCTION__, length, data);
782
783                 if (length > 1) {
784                         bytes_avail = data[0] | (data[1] << 8);
785                         if (bytes_avail) {
786                                 edge_serial->rxBytesAvail += bytes_avail;
787                                 dbg(__FUNCTION__" - bytes_avail = %d, rxBytesAvail %d", bytes_avail, edge_serial->rxBytesAvail);
788
789                                 if ((edge_serial->rxBytesAvail > 0) &&
790                                     (edge_serial->read_urb->status != -EINPROGRESS)) {
791                                         dbg(" --- Posting a read");
792
793                                         /* we have pending bytes on the bulk in pipe, send a request */
794                                         edge_serial->read_urb->dev = edge_serial->serial->dev;
795                                         result = usb_submit_urb(edge_serial->read_urb);
796                                         if (result) {
797                                                 dbg(__FUNCTION__" - usb_submit_urb(read bulk) failed with result = %d", result);
798                                         }
799                                 }
800                         }
801                 }
802                 /* grab the txcredits for the ports if available */
803                 position = 2;
804                 portNumber = 0;
805                 while ((position < length) && (portNumber < edge_serial->serial->num_ports)) {
806                         txCredits = data[position] | (data[position+1] << 8);
807                         if (txCredits) {
808                                 port = &edge_serial->serial->port[portNumber];
809                                 if (port_paranoia_check (port, __FUNCTION__) == 0) {
810                                         edge_port = (struct edgeport_port *)port->private;
811                                         if (edge_port->open) {
812                                                 edge_port->txCredits += txCredits;
813                                                 dbg(__FUNCTION__" - txcredits for port%d = %d", portNumber, edge_port->txCredits);
814
815                                                 /* tell the tty driver that something has changed */
816                                                 wake_up_interruptible(&edge_port->port->tty->write_wait);
817
818                                                 // Since we have more credit, check if more data can be sent
819                                                 send_more_port_data(edge_serial, edge_port);
820                                         }
821                                 }
822                         }
823                         position += 2;
824                         ++portNumber;
825                 }
826         }
827 }
828
829
830 /*****************************************************************************
831  * edge_bulk_in_callback
832  *      this is the callback function for when we have received data on the 
833  *      bulk in endpoint.
834  *****************************************************************************/
835 static void edge_bulk_in_callback (struct urb *urb)
836 {
837         struct edgeport_serial  *edge_serial = (struct edgeport_serial *)urb->context;
838         unsigned char           *data = urb->transfer_buffer;
839         int                     status;
840         __u16                   raw_data_length;
841
842         dbg(__FUNCTION__);
843
844         if (serial_paranoia_check (edge_serial->serial, __FUNCTION__)) {
845                 return;
846         }
847
848         if (urb->status) {
849                 dbg(__FUNCTION__" - nonzero read bulk status received: %d", urb->status);
850                 return;
851         }
852
853         if (urb->actual_length) {
854                 raw_data_length = urb->actual_length;
855
856                 usb_serial_debug_data (__FILE__, __FUNCTION__, raw_data_length, data);
857
858                 /* decrement our rxBytes available by the number that we just got */
859                 edge_serial->rxBytesAvail -= raw_data_length;
860
861                 dbg(__FUNCTION__" - Received = %d, rxBytesAvail %d", raw_data_length, edge_serial->rxBytesAvail);
862
863                 process_rcvd_data (edge_serial, data, urb->actual_length);
864
865                 /* check to see if there's any more data for us to read */
866                 if ((edge_serial->rxBytesAvail > 0) &&
867                     (edge_serial->read_urb->status != -EINPROGRESS)) {
868                         dbg(" --- Posting a read");
869
870                         /* there is, so resubmit our urb */
871                         edge_serial->read_urb->dev = edge_serial->serial->dev;
872                         status = usb_submit_urb(edge_serial->read_urb);
873                         if (status) {
874                                 err(__FUNCTION__" - usb_submit_urb(read bulk) failed, status = %d", status);
875                         }
876                 }
877         }
878 }
879
880
881 /*****************************************************************************
882  * edge_bulk_out_data_callback
883  *      this is the callback function for when we have finished sending serial data
884  *      on the bulk out endpoint.
885  *****************************************************************************/
886 static void edge_bulk_out_data_callback (struct urb *urb)
887 {
888         struct edgeport_port *edge_port = (struct edgeport_port *)urb->context;
889         struct tty_struct *tty;
890
891         dbg(__FUNCTION__);
892
893         if (port_paranoia_check (edge_port->port, __FUNCTION__)) {
894                 return;
895         }
896
897         if (urb->status) {
898                 dbg(__FUNCTION__" - nonzero write bulk status received: %d", urb->status);
899         }
900
901         tty = edge_port->port->tty;
902
903         /* let the tty driver wakeup if it has a special write_wakeup function */
904         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup) {
905                 (tty->ldisc.write_wakeup)(tty);
906         }
907
908         /* tell the tty driver that something has changed */
909         wake_up_interruptible(&tty->write_wait);
910
911         // Release the Write URB
912         edge_port->write_in_progress = FALSE;
913
914         // Check if more data needs to be sent
915         send_more_port_data((struct edgeport_serial *)(edge_port->port->serial->private), edge_port);
916 }
917
918
919 /*****************************************************************************
920  * BulkOutCmdCallback
921  *      this is the callback function for when we have finished sending a command
922  *      on the bulk out endpoint.
923  *****************************************************************************/
924 static void edge_bulk_out_cmd_callback (struct urb *urb)
925 {
926         struct edgeport_port *edge_port = (struct edgeport_port *)urb->context;
927         struct tty_struct *tty;
928         int status = urb->status;
929
930         dbg(__FUNCTION__);
931
932         CmdUrbs--;
933         dbg(__FUNCTION__" - FREE URB %p (outstanding %d)", urb, CmdUrbs);
934
935
936         /* if this urb had a transfer buffer already (old transfer) free it */
937         if (urb->transfer_buffer != NULL) {
938                 kfree(urb->transfer_buffer);
939         }
940
941         // Free the command urb
942         usb_unlink_urb (urb);
943         usb_free_urb   (urb);
944
945         if (port_paranoia_check (edge_port->port, __FUNCTION__)) {
946                 return;
947         }
948
949         if (status) {
950                 dbg(__FUNCTION__" - nonzero write bulk status received: %d", status);
951                 return;
952         }
953
954         /* Get pointer to tty */
955         tty = edge_port->port->tty;
956
957         /* tell the tty driver that something has changed */
958         wake_up_interruptible(&tty->write_wait);
959
960
961         /* we have completed the command */
962         edge_port->commandPending = FALSE;
963         wake_up_interruptible(&edge_port->wait_command);
964 }
965
966
967 /*****************************************************************************
968  * Driver tty interface functions
969  *****************************************************************************/
970
971 /*****************************************************************************
972  * SerialOpen
973  *      this function is called by the tty driver when a port is opened
974  *      If successful, we return 0
975  *      Otherwise we return a negative error number.
976  *****************************************************************************/
977 static int edge_open (struct usb_serial_port *port, struct file * filp)
978 {
979         struct edgeport_port *edge_port = (struct edgeport_port *)port->private;
980         struct usb_serial *serial;
981         struct edgeport_serial *edge_serial;
982         int response;
983         int timeout;
984
985         if (port_paranoia_check (port, __FUNCTION__))
986                 return -ENODEV;
987         
988         dbg(__FUNCTION__ " - port %d", port->number);
989
990         ++port->open_count;
991         MOD_INC_USE_COUNT;
992         
993         if (!port->active) {
994                 port->active = 1;
995
996                 /* force low_latency on so that our tty_push actually forces the data through, 
997                    otherwise it is scheduled, and with high data rates (like with OHCI) data
998                    can get lost. */
999                 port->tty->low_latency = 1;
1000         
1001                 /* see if we've set up our endpoint info yet (can't set it up in edge_startup
1002                    as the structures were not set up at that time.) */
1003                 serial = port->serial;
1004                 edge_serial = (struct edgeport_serial *)serial->private;
1005                 if (edge_serial->interrupt_in_buffer == NULL) {
1006                         struct usb_serial_port *port0 = &serial->port[0];
1007                         
1008                         /* not set up yet, so do it now */
1009                         edge_serial->interrupt_in_buffer = port0->interrupt_in_buffer;
1010                         edge_serial->interrupt_in_endpoint = port0->interrupt_in_endpointAddress;
1011                         edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
1012                         edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
1013                         edge_serial->bulk_in_endpoint = port0->bulk_in_endpointAddress;
1014                         edge_serial->read_urb = port0->read_urb;
1015                         edge_serial->bulk_out_endpoint = port0->bulk_out_endpointAddress;
1016                 
1017                         /* set up our interrupt urb */
1018                         /* Like to use FILL_INT_URB, but we don't know wMaxPacketSize or bInterval, something to change for 2.5... */
1019                         edge_serial->interrupt_read_urb->complete = edge_interrupt_callback;
1020                         edge_serial->interrupt_read_urb->context = edge_serial;
1021                         /* FILL_INT_URB(edge_serial->interrupt_read_urb, serial->dev, 
1022                                      usb_rcvintpipe (serial->dev, edge_serial->interrupt_in_endpoint),
1023                                      edge_serial->interrupt_in_buffer, edge_serial->interrupt_in_endpoint.wMaxPacketSize,
1024                                      edge_interrupt_callback, edge_serial, edge_serial->interrupt_in_endpoint.bInterval);
1025                         */
1026                         
1027                         /* set up our bulk in urb */
1028                         /* Like to use FILL_BULK_URB, but we don't know wMaxPacketSize or bInterval, something to change for 2.5... */
1029                         edge_serial->read_urb->complete = edge_bulk_in_callback;
1030                         edge_serial->read_urb->context = edge_serial;
1031                         /* FILL_BULK_URB(edge_serial->read_urb, serial->dev, 
1032                                       usb_rcvbulkpipe (serial->dev, edge_serial->bulk_in_endpoint),
1033                                       edge_serial->bulk_in_buffer, edge_serial->bulk_in_endpoint->wMaxPacketSize,
1034                                       edge_bulk_in_callback, edge_serial);
1035                         */
1036                 
1037                         /* start interrupt read for this edgeport
1038                          * this interrupt will continue as long as the edgeport is connected */
1039                         response = usb_submit_urb (edge_serial->interrupt_read_urb);
1040                         if (response) {
1041                                 err(__FUNCTION__" - Error %d submitting control urb", response);
1042                         }
1043                 }
1044                 
1045                 /* initialize our wait queues */
1046                 init_waitqueue_head(&edge_port->wait_open);
1047                 init_waitqueue_head(&edge_port->wait_chase);
1048                 init_waitqueue_head(&edge_port->delta_msr_wait);
1049                 init_waitqueue_head(&edge_port->wait_command);
1050
1051                 /* initialize our icount structure */
1052                 memset (&(edge_port->icount), 0x00, sizeof(edge_port->icount));
1053
1054                 /* initialize our port settings */
1055                 edge_port->txCredits            = 0;                    /* Can't send any data yet */
1056                 edge_port->shadowMCR            = MCR_MASTER_IE;        /* Must always set this bit to enable ints! */
1057                 edge_port->chaseResponsePending = FALSE;
1058
1059                 /* send a open port command */
1060                 edge_port->openPending = TRUE;
1061                 edge_port->open        = FALSE;
1062                 response = send_iosp_ext_cmd (edge_port, IOSP_CMD_OPEN_PORT, 0);
1063
1064                 if (response < 0) {
1065                         err(__FUNCTION__" - error sending open port command");
1066                         edge_port->openPending = FALSE;
1067                         port->active = 0;
1068                         port->open_count = 0;
1069                         MOD_DEC_USE_COUNT;
1070                         return -ENODEV;
1071                 }
1072
1073                 /* now wait for the port to be completly opened */
1074                 timeout = OPEN_TIMEOUT;
1075                 while (timeout && edge_port->openPending == TRUE) {
1076                         timeout = interruptible_sleep_on_timeout (&edge_port->wait_open, timeout);
1077                 }
1078
1079                 if (edge_port->open == FALSE) {
1080                         /* open timed out */
1081                         dbg(__FUNCTION__" - open timedout");
1082                         edge_port->openPending = FALSE;
1083                         port->active = 0;
1084                         port->open_count = 0;
1085                         MOD_DEC_USE_COUNT;
1086                         return -ENODEV;
1087                 }
1088
1089                 /* create the txfifo */
1090                 edge_port->txfifo.head  = 0;
1091                 edge_port->txfifo.tail  = 0;
1092                 edge_port->txfifo.count = 0;
1093                 edge_port->txfifo.size  = edge_port->maxTxCredits;
1094                 edge_port->txfifo.fifo  = kmalloc (edge_port->maxTxCredits, GFP_KERNEL);
1095
1096                 if (!edge_port->txfifo.fifo) {
1097                         dbg(__FUNCTION__" - no memory");
1098                         edge_close (port, filp);
1099                         return -ENOMEM;
1100                 }
1101
1102                 /* Allocate a URB for the write */
1103                 edge_port->write_urb = usb_alloc_urb (0);
1104
1105                 if (!edge_port->write_urb) {
1106                         dbg(__FUNCTION__" - no memory");
1107                         edge_close (port, filp);
1108                         return -ENOMEM;
1109                 }
1110
1111                 dbg(__FUNCTION__"(%d) - Initialize TX fifo to %d bytes", port->number, edge_port->maxTxCredits);
1112         }
1113
1114         dbg(__FUNCTION__" exited");
1115
1116         return 0;
1117 }
1118
1119
1120 /************************************************************************
1121  *
1122  * block_until_chase_response
1123  *
1124  *      This function will block the close until one of the following:
1125  *              1. Response to our Chase comes from Edgeport
1126  *              2. A timout of 10 seconds without activity has expired
1127  *                 (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
1128  *
1129  ************************************************************************/
1130 static void block_until_chase_response(struct edgeport_port *edge_port)
1131 {
1132         __u16 lastCredits;
1133         int timeout = 1*HZ;
1134         int wait = 10;
1135
1136         while (1) {
1137                 // Save Last credits
1138                 lastCredits = edge_port->txCredits;
1139
1140                 // Did we get our Chase response
1141                 if (edge_port->chaseResponsePending == FALSE) {
1142                         dbg(__FUNCTION__" - Got Chase Response");
1143
1144                         // did we get all of our credit back?
1145                         if (edge_port->txCredits == edge_port->maxTxCredits ) {
1146                                 dbg(__FUNCTION__" - Got all credits");
1147                                 return;
1148                         }
1149                 }
1150
1151                 // Block the thread for a while
1152                 interruptible_sleep_on_timeout (&edge_port->wait_chase, timeout);
1153
1154                 if (lastCredits == edge_port->txCredits) {
1155                         // No activity.. count down.
1156                         wait--;
1157                         if (wait == 0) {
1158                                 edge_port->chaseResponsePending = FALSE;
1159                                 dbg(__FUNCTION__" - Chase TIMEOUT");
1160                                 return;
1161                         }
1162                 } else {
1163                         // Reset timout value back to 10 seconds
1164                         dbg(__FUNCTION__" - Last %d, Current %d", lastCredits, edge_port->txCredits);
1165                         wait = 10;
1166                 }
1167         }
1168 }
1169
1170
1171 /************************************************************************
1172  *
1173  * block_until_tx_empty
1174  *
1175  *      This function will block the close until one of the following:
1176  *              1. TX count are 0
1177  *              2. The edgeport has stopped
1178  *              3. A timout of 3 seconds without activity has expired
1179  *
1180  ************************************************************************/
1181 static void block_until_tx_empty (struct edgeport_port *edge_port)
1182 {
1183         struct TxFifo *fifo = &edge_port->txfifo;
1184         __u32 lastCount;
1185         int timeout = HZ/10;
1186         int wait = 30;
1187
1188         while (1) {
1189                 // Save Last count
1190                 lastCount = fifo->count;
1191
1192                 // Is the Edgeport Buffer empty?
1193                 if (lastCount == 0) {
1194                         dbg(__FUNCTION__" - TX Buffer Empty");
1195                         return;
1196                 }
1197
1198                 // Block the thread for a while
1199                 interruptible_sleep_on_timeout (&edge_port->wait_chase, timeout);
1200
1201                 dbg(__FUNCTION__ " wait");
1202
1203                 if (lastCount == fifo->count) {
1204                         // No activity.. count down.
1205                         wait--;
1206                         if (wait == 0) {
1207                                 dbg(__FUNCTION__" - TIMEOUT");
1208                                 return;
1209                         }
1210                 } else {
1211                         // Reset timout value back to seconds
1212                         wait = 30;
1213                 }
1214         }
1215 }
1216
1217
1218 /*****************************************************************************
1219  * edge_close
1220  *      this function is called by the tty driver when a port is closed
1221  *****************************************************************************/
1222 static void edge_close (struct usb_serial_port *port, struct file * filp)
1223 {
1224         struct usb_serial *serial;
1225         struct edgeport_serial *edge_serial;
1226         struct edgeport_port *edge_port;
1227         int status;
1228
1229         if (port_paranoia_check (port, __FUNCTION__))
1230                 return;
1231         
1232         dbg(__FUNCTION__ " - port %d", port->number);
1233                          
1234         serial = get_usb_serial (port, __FUNCTION__);
1235         if (!serial)
1236                 return;
1237         
1238         edge_serial = (struct edgeport_serial *)serial->private;
1239         edge_port = (struct edgeport_port *)port->private;
1240
1241         --port->open_count;
1242
1243         if (port->open_count <= 0) {
1244                 // block until tx is empty
1245                 block_until_tx_empty(edge_port);
1246         
1247                 edge_port->closePending = TRUE;
1248         
1249                 /* flush and chase */
1250                 edge_port->chaseResponsePending = TRUE;
1251         
1252                 dbg(__FUNCTION__" - Sending IOSP_CMD_CHASE_PORT");
1253                 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CHASE_PORT, 0);
1254                 if (status == 0) {
1255                         // block until chase finished
1256                         block_until_chase_response(edge_port);
1257                 } else {
1258                         edge_port->chaseResponsePending = FALSE;
1259                 }
1260         
1261                 /* close the port */
1262                 dbg(__FUNCTION__" - Sending IOSP_CMD_CLOSE_PORT");
1263                 send_iosp_ext_cmd (edge_port, IOSP_CMD_CLOSE_PORT, 0);
1264         
1265                 //port->close = TRUE;
1266                 edge_port->closePending = FALSE;
1267                 edge_port->open = FALSE;
1268                 edge_port->openPending = FALSE;
1269         
1270                 if (edge_port->write_urb) {
1271                         /* if this urb had a transfer buffer already (old transfer) free it */
1272                         if (edge_port->write_urb->transfer_buffer != NULL) {
1273                                 kfree(edge_port->write_urb->transfer_buffer);
1274                         }
1275         
1276                         usb_unlink_urb (edge_port->write_urb);
1277                         usb_free_urb   (edge_port->write_urb);
1278                 }
1279         
1280                 if (edge_port->txfifo.fifo) {
1281                         kfree(edge_port->txfifo.fifo);
1282                 }
1283         }
1284
1285         MOD_DEC_USE_COUNT;
1286         dbg(__FUNCTION__" exited");
1287 }   
1288
1289
1290
1291
1292 /*****************************************************************************
1293  * SerialWrite
1294  *      this function is called by the tty driver when data should be written to
1295  *      the port.
1296  *      If successful, we return the number of bytes written, otherwise we return
1297  *      a negative error number.
1298  *****************************************************************************/
1299 static int edge_write (struct usb_serial_port *port, int from_user, const unsigned char *data, int count)
1300 {
1301         struct edgeport_port *edge_port = (struct edgeport_port *)port->private;
1302         struct TxFifo *fifo;
1303         int copySize;
1304         int bytesleft;
1305         int firsthalf;
1306         int secondhalf;
1307
1308         dbg(__FUNCTION__ " - port %d", port->number);
1309
1310         // get a pointer to the Tx fifo
1311         fifo = &edge_port->txfifo;
1312
1313         // calculate number of bytes to put in fifo
1314         copySize = min ((unsigned int)count, (edge_port->txCredits - fifo->count));
1315
1316         dbg(__FUNCTION__"(%d) of %d byte(s) Fifo room  %d -- will copy %d bytes", 
1317             port->number, count, edge_port->txCredits - fifo->count, copySize);
1318
1319         /* catch writes of 0 bytes which the tty driver likes to give us, and when txCredits is empty */
1320         if (copySize == 0) {
1321                 dbg (__FUNCTION__" - copySize = Zero");
1322                 return 0;
1323         }
1324
1325         // queue the data       
1326         // since we can never overflow the buffer we do not have to check for full condition
1327
1328         // the copy is done is two parts -- first fill to the end of the buffer
1329         // then copy the reset from the start of the buffer 
1330
1331         bytesleft = fifo->size - fifo->head;
1332         firsthalf = min (bytesleft, copySize);
1333         dbg (__FUNCTION__" - copy %d bytes of %d into fifo ", firsthalf, bytesleft);
1334
1335         /* now copy our data */
1336         if (from_user) {
1337                 if (copy_from_user(&fifo->fifo[fifo->head], data, firsthalf))
1338                         return -EFAULT;
1339         } else {
1340                 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
1341         }  
1342
1343         // update the index and size
1344         fifo->head  += firsthalf;
1345         fifo->count += firsthalf;
1346
1347         // wrap the index
1348         if (fifo->head == fifo->size) {
1349                 fifo->head = 0;
1350         }
1351
1352         secondhalf = copySize-firsthalf;
1353
1354         if (secondhalf) {
1355                 dbg (__FUNCTION__" - copy rest of data %d", secondhalf);
1356                 if (from_user) {
1357                         if (copy_from_user(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf))
1358                                 return -EFAULT;
1359                 } else {
1360                         memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
1361                 }
1362                 // update the index and size
1363                 fifo->count += secondhalf;
1364                 fifo->head  += secondhalf;
1365                 // No need to check for wrap since we can not get to end of fifo in this part
1366         }
1367
1368         if (copySize) {
1369                 usb_serial_debug_data (__FILE__, __FUNCTION__, copySize, data);
1370         }
1371
1372         send_more_port_data((struct edgeport_serial *)port->serial->private, edge_port);
1373
1374         dbg(__FUNCTION__" wrote %d byte(s) TxCredits %d, Fifo %d", copySize, edge_port->txCredits, fifo->count);
1375
1376         return copySize;   
1377 }
1378
1379
1380 /************************************************************************
1381  *
1382  * send_more_port_data()
1383  *
1384  *      This routine attempts to write additional UART transmit data
1385  *      to a port over the USB bulk pipe. It is called (1) when new
1386  *      data has been written to a port's TxBuffer from higher layers
1387  *      (2) when the peripheral sends us additional TxCredits indicating
1388  *      that it can accept more Tx data for a given port; and (3) when
1389  *      a bulk write completes successfully and we want to see if we
1390  *      can transmit more.
1391  *
1392  ************************************************************************/
1393 static void send_more_port_data(struct edgeport_serial *edge_serial, struct edgeport_port *edge_port)
1394 {
1395         struct TxFifo   *fifo = &edge_port->txfifo;
1396         struct urb      *urb;
1397         unsigned char   *buffer;
1398         int             status;
1399         unsigned long   flags;
1400         int             count;
1401         int             bytesleft;
1402         int             firsthalf;
1403         int             secondhalf;
1404
1405         dbg(__FUNCTION__"(%d)", edge_port->port->number);
1406
1407         /* find our next free urb */    // ICK!!! FIXME!!!
1408         save_flags(flags); cli();
1409
1410         if (edge_port->write_in_progress ||
1411             !edge_port->open             ||
1412             (fifo->count == 0)) {
1413                 restore_flags(flags);
1414                 dbg(__FUNCTION__"(%d) EXIT - fifo %d, PendingWrite = %d", edge_port->port->number, fifo->count, edge_port->write_in_progress);
1415                 return;
1416         }
1417
1418         // since the amount of data in the fifo will always fit into the
1419         // edgeport buffer we do not need to check the write length
1420
1421         //      Do we have enough credits for this port to make it worthwhile
1422         //      to bother queueing a write. If it's too small, say a few bytes,
1423         //      it's better to wait for more credits so we can do a larger
1424         //      write.
1425         if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits)) {
1426                 restore_flags(flags);
1427                 dbg(__FUNCTION__"(%d) Not enough credit - fifo %d TxCredit %d", edge_port->port->number, fifo->count, edge_port->txCredits );
1428                 return;
1429         }
1430
1431         // lock this write
1432         edge_port->write_in_progress = TRUE;
1433         restore_flags(flags);
1434
1435         // get a pointer to the write_urb
1436         urb = edge_port->write_urb;
1437
1438         /* if this urb had a transfer buffer already (old transfer) free it */
1439         if (urb->transfer_buffer != NULL) {
1440                 kfree(urb->transfer_buffer);
1441                 urb->transfer_buffer = NULL;
1442         }
1443
1444         /* build the data header for the buffer and port that we are about to send out */
1445         count = fifo->count;
1446         buffer = kmalloc (count+2, GFP_KERNEL);
1447         if (buffer == NULL) {
1448                 err(__FUNCTION__" - no more kernel memory...");
1449                 edge_port->write_in_progress = FALSE;
1450                 return;
1451         }
1452         buffer[0] = IOSP_BUILD_DATA_HDR1 (edge_port->port->number, count);
1453         buffer[1] = IOSP_BUILD_DATA_HDR2 (edge_port->port->number, count);
1454
1455         /* now copy our data */
1456         bytesleft =  fifo->size - fifo->tail;
1457         firsthalf = min (bytesleft, count);
1458         memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1459         fifo->tail  += firsthalf;
1460         fifo->count -= firsthalf;
1461         if (fifo->tail == fifo->size) {
1462                 fifo->tail = 0;
1463         }
1464
1465         secondhalf = count-firsthalf;
1466         if (secondhalf) {
1467                 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail], secondhalf);
1468                 fifo->tail  += secondhalf;
1469                 fifo->count -= secondhalf;
1470         }
1471
1472         if (count) {
1473                 usb_serial_debug_data (__FILE__, __FUNCTION__, count, &buffer[2]);
1474         }
1475
1476         /* fill up the urb with all of our data and submit it */
1477         FILL_BULK_URB (urb, edge_serial->serial->dev, 
1478                        usb_sndbulkpipe(edge_serial->serial->dev, edge_serial->bulk_out_endpoint),
1479                        buffer, count+2, edge_bulk_out_data_callback, edge_port);
1480
1481         /* set the USB_BULK_QUEUE flag so that we can shove a bunch of urbs at once down the pipe */
1482         urb->transfer_flags |= USB_QUEUE_BULK;
1483
1484         urb->dev = edge_serial->serial->dev;
1485         status = usb_submit_urb(urb);
1486         if (status) {
1487                 /* something went wrong */
1488                 dbg(__FUNCTION__" - usb_submit_urb(write bulk) failed");
1489                 edge_port->write_in_progress = FALSE;
1490         } else {
1491                 /* decrement the number of credits we have by the number we just sent */
1492                 edge_port->txCredits -= count;
1493                 edge_port->icount.tx += count;
1494         }
1495         dbg(__FUNCTION__" wrote %d byte(s) TxCredit %d, Fifo %d", count, edge_port->txCredits, fifo->count);
1496 }
1497
1498
1499 /*****************************************************************************
1500  * edge_write_room
1501  *      this function is called by the tty driver when it wants to know how many
1502  *      bytes of data we can accept for a specific port.
1503  *      If successful, we return the amount of room that we have for this port
1504  *      (the txCredits), 
1505  *      Otherwise we return a negative error number.
1506  *****************************************************************************/
1507 static int edge_write_room (struct usb_serial_port *port)
1508 {
1509         struct edgeport_port *edge_port = (struct edgeport_port *)(port->private);
1510         int room;
1511
1512         dbg(__FUNCTION__);
1513
1514         if (edge_port->closePending == TRUE) {
1515                 return -ENODEV;
1516         }
1517
1518         dbg(__FUNCTION__" - port %d", port->number);
1519
1520         if (!edge_port->open) {
1521                 dbg (__FUNCTION__" - port not opened");
1522                 return -EINVAL;
1523         }
1524
1525         // total of both buffers is still txCredit
1526         room = edge_port->txCredits - edge_port->txfifo.count;
1527
1528         dbg(__FUNCTION__" - returns %d", room);
1529         return room;
1530 }
1531
1532
1533 /*****************************************************************************
1534  * edge_chars_in_buffer
1535  *      this function is called by the tty driver when it wants to know how many
1536  *      bytes of data we currently have outstanding in the port (data that has
1537  *      been written, but hasn't made it out the port yet)
1538  *      If successful, we return the number of bytes left to be written in the 
1539  *      system, 
1540  *      Otherwise we return a negative error number.
1541  *****************************************************************************/
1542 static int edge_chars_in_buffer (struct usb_serial_port *port)
1543 {
1544         struct edgeport_port *edge_port = (struct edgeport_port *)(port->private);
1545         int num_chars;
1546
1547         dbg(__FUNCTION__);
1548
1549         if (!edge_port->open) {
1550                 dbg (__FUNCTION__" - port not opened");
1551                 return -EINVAL;
1552         }
1553
1554         num_chars = edge_port->maxTxCredits - edge_port->txCredits + edge_port->txfifo.count;
1555         if (num_chars) {
1556                 dbg(__FUNCTION__"(port %d) - returns %d", port->number, num_chars);
1557         }
1558
1559         return num_chars;
1560 }
1561
1562
1563 /*****************************************************************************
1564  * SerialThrottle
1565  *      this function is called by the tty driver when it wants to stop the data
1566  *      being read from the port.
1567  *****************************************************************************/
1568 static void edge_throttle (struct usb_serial_port *port)
1569 {
1570         struct edgeport_port *edge_port = (struct edgeport_port *)(port->private);
1571         struct tty_struct *tty;
1572         int status;
1573
1574         dbg(__FUNCTION__" - port %d", port->number);
1575
1576         if (!edge_port->open) {
1577                 dbg (__FUNCTION__" - port not opened");
1578                 return;
1579         }
1580
1581         tty = port->tty;
1582
1583         /* if we are implementing XON/XOFF, send the stop character */
1584         if (I_IXOFF(tty)) {
1585                 unsigned char stop_char = STOP_CHAR(tty);
1586                 status = edge_write (port, 0, &stop_char, 1);
1587                 if (status <= 0) {
1588                         return;
1589                 }
1590         }
1591
1592         /* if we are implementing RTS/CTS, toggle that line */
1593         if (tty->termios->c_cflag & CRTSCTS) {
1594                 edge_port->shadowMCR &= ~MCR_RTS;
1595                 status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1596                 if (status != 0) {
1597                         return;
1598                 }
1599         }
1600
1601         return;
1602 }
1603
1604
1605 /*****************************************************************************
1606  * edge_unthrottle
1607  *      this function is called by the tty driver when it wants to resume the data
1608  *      being read from the port (called after SerialThrottle is called)
1609  *****************************************************************************/
1610 static void edge_unthrottle (struct usb_serial_port *port)
1611 {
1612         struct edgeport_port *edge_port = (struct edgeport_port *)(port->private);
1613         struct tty_struct *tty;
1614         int status;
1615
1616         dbg(__FUNCTION__" - port %d", port->number);
1617
1618         if (!edge_port->open) {
1619                 dbg (__FUNCTION__" - port not opened");
1620                 return;
1621         }
1622
1623         tty = port->tty;
1624
1625         /* if we are implementing XON/XOFF, send the start character */
1626         if (I_IXOFF(tty)) {
1627                 unsigned char start_char = START_CHAR(tty);
1628                 status = edge_write (port, 0, &start_char, 1);
1629                 if (status <= 0) {
1630                         return;
1631                 }
1632         }
1633
1634         /* if we are implementing RTS/CTS, toggle that line */
1635         if (tty->termios->c_cflag & CRTSCTS) {
1636                 edge_port->shadowMCR |= MCR_RTS;
1637                 status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1638                 if (status != 0) {
1639                         return;
1640                 }
1641         }
1642
1643         return;
1644 }
1645
1646
1647 /*****************************************************************************
1648  * SerialSetTermios
1649  *      this function is called by the tty driver when it wants to change the termios structure
1650  *****************************************************************************/
1651 static void edge_set_termios (struct usb_serial_port *port, struct termios *old_termios)
1652 {
1653         struct edgeport_port *edge_port = (struct edgeport_port *)(port->private);
1654         struct tty_struct *tty = port->tty;
1655         unsigned int cflag = tty->termios->c_cflag;
1656
1657         dbg(__FUNCTION__" - clfag %08x %08x iflag %08x %08x", 
1658             tty->termios->c_cflag,
1659             old_termios->c_cflag,
1660             RELEVANT_IFLAG(tty->termios->c_iflag),
1661             RELEVANT_IFLAG(old_termios->c_iflag)
1662            );
1663
1664         /* check that they really want us to change something */
1665         if (old_termios) {
1666                 if ((cflag == old_termios->c_cflag) &&
1667                     (RELEVANT_IFLAG(tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
1668                         dbg(__FUNCTION__" - nothing to change");
1669                         return;
1670                 }
1671         }
1672
1673         dbg(__FUNCTION__" - port %d", port->number);
1674
1675         if (!edge_port->open) {
1676                 dbg (__FUNCTION__" - port not opened");
1677                 return;
1678         }
1679
1680         /* change the port settings to the new ones specified */
1681         change_port_settings (edge_port, old_termios);
1682
1683         return;
1684 }
1685
1686
1687 /*****************************************************************************
1688  * get_lsr_info - get line status register info
1689  *
1690  * Purpose: Let user call ioctl() to get info when the UART physically
1691  *          is emptied.  On bus types like RS485, the transmitter must
1692  *          release the bus after transmitting. This must be done when
1693  *          the transmit shift register is empty, not be done when the
1694  *          transmit holding register is empty.  This functionality
1695  *          allows an RS485 driver to be written in user space. 
1696  *****************************************************************************/
1697 static int get_lsr_info(struct edgeport_port *edge_port, unsigned int *value)
1698 {
1699         unsigned int result = 0;
1700
1701         if (edge_port->maxTxCredits == edge_port->txCredits &&
1702             edge_port->txfifo.count == 0) {
1703                 dbg(__FUNCTION__" -- Empty");
1704                 result = TIOCSER_TEMT;
1705         }
1706
1707         if (copy_to_user(value, &result, sizeof(int)))
1708                 return -EFAULT;
1709         return 0;
1710 }
1711
1712 static int get_number_bytes_avail(struct edgeport_port *edge_port, unsigned int *value)
1713 {
1714         unsigned int result = 0;
1715         struct tty_struct *tty = edge_port->port->tty;
1716
1717         result = tty->read_cnt;
1718
1719         dbg(__FUNCTION__"(%d) = %d",  edge_port->port->number, result);
1720         if (copy_to_user(value, &result, sizeof(int)))
1721                 return -EFAULT;
1722         //return 0;
1723         return -ENOIOCTLCMD;
1724 }
1725
1726 static int set_modem_info(struct edgeport_port *edge_port, unsigned int cmd, unsigned int *value)
1727 {
1728         unsigned int mcr = edge_port->shadowMCR;
1729         unsigned int arg;
1730
1731         if (copy_from_user(&arg, value, sizeof(int)))
1732                 return -EFAULT;
1733
1734         switch (cmd) {
1735                 case TIOCMBIS:
1736                         if (arg & TIOCM_RTS)
1737                                 mcr |= MCR_RTS;
1738                         if (arg & TIOCM_DTR)
1739                                 mcr |= MCR_RTS;
1740                         if (arg & TIOCM_LOOP)
1741                                 mcr |= MCR_LOOPBACK;
1742                         break;
1743
1744                 case TIOCMBIC:
1745                         if (arg & TIOCM_RTS)
1746                                 mcr &= ~MCR_RTS;
1747                         if (arg & TIOCM_DTR)
1748                                 mcr &= ~MCR_RTS;
1749                         if (arg & TIOCM_LOOP)
1750                                 mcr &= ~MCR_LOOPBACK;
1751                         break;
1752
1753                 case TIOCMSET:
1754                         /* turn off the RTS and DTR and LOOPBACK 
1755                          * and then only turn on what was asked to */
1756                         mcr &=  ~(MCR_RTS | MCR_DTR | MCR_LOOPBACK);
1757                         mcr |= ((arg & TIOCM_RTS) ? MCR_RTS : 0);
1758                         mcr |= ((arg & TIOCM_DTR) ? MCR_DTR : 0);
1759                         mcr |= ((arg & TIOCM_LOOP) ? MCR_LOOPBACK : 0);
1760                         break;
1761         }
1762
1763         edge_port->shadowMCR = mcr;
1764
1765         send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1766
1767         return 0;
1768 }
1769
1770 static int get_modem_info(struct edgeport_port *edge_port, unsigned int *value)
1771 {
1772         unsigned int result = 0;
1773         unsigned int msr = edge_port->shadowMSR;
1774         unsigned int mcr = edge_port->shadowMCR;
1775
1776         result = ((mcr & MCR_DTR)       ? TIOCM_DTR: 0)   /* 0x002 */
1777                   | ((mcr & MCR_RTS)    ? TIOCM_RTS: 0)   /* 0x004 */
1778                   | ((msr & MSR_CTS)    ? TIOCM_CTS: 0)   /* 0x020 */
1779                   | ((msr & MSR_CD)     ? TIOCM_CAR: 0)   /* 0x040 */
1780                   | ((msr & MSR_RI)     ? TIOCM_RI:  0)   /* 0x080 */
1781                   | ((msr & MSR_DSR)    ? TIOCM_DSR: 0);  /* 0x100 */
1782
1783
1784         dbg(__FUNCTION__" -- %x", result);
1785
1786         if (copy_to_user(value, &result, sizeof(int)))
1787                 return -EFAULT;
1788         return 0;
1789 }
1790
1791
1792
1793 static int get_serial_info(struct edgeport_port *edge_port, struct serial_struct * retinfo)
1794 {
1795         struct serial_struct tmp;
1796
1797         if (!retinfo)
1798                 return -EFAULT;
1799
1800         memset(&tmp, 0, sizeof(tmp));
1801
1802         tmp.type                = PORT_16550A;
1803         tmp.line                = edge_port->port->serial->minor;
1804         tmp.port                = edge_port->port->number;
1805         tmp.irq                 = 0;
1806         tmp.flags               = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1807         tmp.xmit_fifo_size      = edge_port->maxTxCredits;
1808         tmp.baud_base           = 9600;
1809         tmp.close_delay         = 5*HZ;
1810         tmp.closing_wait        = 30*HZ;
1811 //      tmp.custom_divisor      = state->custom_divisor;
1812 //      tmp.hub6                = state->hub6;
1813 //      tmp.io_type             = state->io_type;
1814
1815
1816         if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1817                 return -EFAULT;
1818         return 0;
1819 }
1820
1821
1822
1823 /*****************************************************************************
1824  * SerialIoctl
1825  *      this function handles any ioctl calls to the driver
1826  *****************************************************************************/
1827 static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
1828 {
1829         struct edgeport_port *edge_port = (struct edgeport_port *)(port->private);
1830         struct async_icount cnow;
1831         struct async_icount cprev;
1832         struct serial_icounter_struct icount;
1833         unsigned long flags;
1834
1835
1836         dbg(__FUNCTION__" - port %d, cmd = 0x%x", port->number, cmd);
1837
1838         switch (cmd) {
1839                 // return number of bytes available
1840                 case TIOCINQ:
1841                         dbg(__FUNCTION__" (%d) TIOCINQ",  port->number);
1842                         return get_number_bytes_avail(edge_port, (unsigned int *) arg);
1843                         break;
1844
1845 //              case TCGETS:            
1846 //                      dbg(__FUNCTION__" (%d) TCGETS",  port->number);
1847 //                      break;
1848
1849 //              case TCSETS:            
1850 //                      dbg(__FUNCTION__" (%d) TCSETS",  port->number);
1851 //                      break;
1852
1853                 case TIOCSERGETLSR:
1854                         dbg(__FUNCTION__" (%d) TIOCSERGETLSR",  port->number);
1855                         return get_lsr_info(edge_port, (unsigned int *) arg);
1856                         return 0;
1857
1858                 case TIOCMBIS:
1859                 case TIOCMBIC:
1860                 case TIOCMSET:
1861                         dbg(__FUNCTION__" (%d) TIOCMSET/TIOCMBIC/TIOCMSET",  port->number);
1862                         return set_modem_info(edge_port, cmd, (unsigned int *) arg);
1863
1864                 case TIOCMGET:  
1865                         dbg(__FUNCTION__" (%d) TIOCMGET",  port->number);
1866                         return get_modem_info(edge_port, (unsigned int *) arg);
1867
1868                 case TIOCGSERIAL:
1869                         dbg(__FUNCTION__" (%d) TIOCGSERIAL",  port->number);
1870                         return get_serial_info(edge_port, (struct serial_struct *) arg);
1871
1872                 case TIOCSSERIAL:
1873                         dbg(__FUNCTION__" (%d) TIOCSSERIAL",  port->number);
1874                         break;
1875
1876                 case TIOCMIWAIT:
1877                         dbg(__FUNCTION__" (%d) TIOCMIWAIT",  port->number);
1878                         save_flags(flags); cli();
1879                         cprev = edge_port->icount;
1880                         restore_flags(flags);
1881                         while (1) {
1882                                 interruptible_sleep_on(&edge_port->delta_msr_wait);
1883                                 /* see if a signal did it */
1884                                 if (signal_pending(current))
1885                                         return -ERESTARTSYS;
1886                                 save_flags(flags); cli();
1887                                 cnow = edge_port->icount; /* atomic copy */
1888                                 restore_flags(flags);
1889                                 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1890                                     cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1891                                         return -EIO; /* no change => error */
1892                                 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1893                                     ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1894                                     ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
1895                                     ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1896                                         return 0;
1897                                 }
1898                                 cprev = cnow;
1899                         }
1900                         /* NOTREACHED */
1901                         break;
1902
1903                 case TIOCGICOUNT:
1904                         save_flags(flags); cli();
1905                         cnow = edge_port->icount;
1906                         restore_flags(flags);
1907                         icount.cts = cnow.cts;
1908                         icount.dsr = cnow.dsr;
1909                         icount.rng = cnow.rng;
1910                         icount.dcd = cnow.dcd;
1911                         icount.rx = cnow.rx;
1912                         icount.tx = cnow.tx;
1913                         icount.frame = cnow.frame;
1914                         icount.overrun = cnow.overrun;
1915                         icount.parity = cnow.parity;
1916                         icount.brk = cnow.brk;
1917                         icount.buf_overrun = cnow.buf_overrun;
1918
1919                         dbg(__FUNCTION__" (%d) TIOCGICOUNT RX=%d, TX=%d",  port->number, icount.rx, icount.tx );
1920                         if (copy_to_user((void *)arg, &icount, sizeof(icount)))
1921                                 return -EFAULT;
1922                         return 0;
1923         }
1924
1925         return -ENOIOCTLCMD;
1926 }
1927
1928
1929 /*****************************************************************************
1930  * SerialBreak
1931  *      this function sends a break to the port
1932  *****************************************************************************/
1933 static void edge_break (struct usb_serial_port *port, int break_state)
1934 {
1935         struct edgeport_port *edge_port = (struct edgeport_port *)(port->private);
1936         int status;
1937
1938         /* flush and chase */
1939         edge_port->chaseResponsePending = TRUE;
1940
1941         dbg(__FUNCTION__" - Sending IOSP_CMD_CHASE_PORT");
1942         status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CHASE_PORT, 0);
1943         if (status == 0) {
1944                 // block until chase finished
1945                 block_until_chase_response(edge_port);
1946         } else {
1947                 edge_port->chaseResponsePending = FALSE;
1948         }
1949
1950         if (break_state == -1) {
1951                 dbg(__FUNCTION__" - Sending IOSP_CMD_SET_BREAK");
1952                 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_BREAK, 0);
1953         } else {
1954                 dbg(__FUNCTION__" - Sending IOSP_CMD_CLEAR_BREAK");
1955                 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CLEAR_BREAK, 0);
1956         }
1957         if (status) {
1958                 dbg(__FUNCTION__" - error sending break set/clear command.");
1959         }
1960
1961         return;
1962 }
1963
1964
1965 /*****************************************************************************
1966  * process_rcvd_data
1967  *      this function handles the data received on the bulk in pipe.
1968  *****************************************************************************/
1969 static int process_rcvd_data (struct edgeport_serial *edge_serial, unsigned char * buffer, __u16 bufferLength)
1970 {
1971         struct usb_serial_port *port;
1972         struct edgeport_port *edge_port;
1973         struct tty_struct *tty;
1974         __u16 lastBufferLength;
1975         __u16 rxLen;
1976         int i;
1977
1978         dbg(__FUNCTION__);
1979
1980         lastBufferLength = bufferLength + 1;
1981
1982         while (bufferLength > 0) {
1983                 /* failsafe incase we get a message that we don't understand */
1984                 if (lastBufferLength == bufferLength) {
1985                         dbg(__FUNCTION__" - stuck in loop, exiting it.");
1986                         break;
1987                 }
1988                 lastBufferLength = bufferLength;
1989
1990                 switch (edge_serial->rxState) {
1991                         case EXPECT_HDR1:
1992                                 edge_serial->rxHeader1 = *buffer;
1993                                 ++buffer;
1994                                 --bufferLength;
1995
1996                                 if (bufferLength == 0) {
1997                                         edge_serial->rxState = EXPECT_HDR2;
1998                                         break;
1999                                 }
2000                                 /* otherwise, drop on through */
2001
2002                         case EXPECT_HDR2:
2003                                 edge_serial->rxHeader2 = *buffer;
2004                                 ++buffer;
2005                                 --bufferLength;
2006
2007                                 dbg(__FUNCTION__" - Hdr1=%02X Hdr2=%02X", edge_serial->rxHeader1, edge_serial->rxHeader2);
2008
2009                                 // Process depending on whether this header is
2010                                 // data or status
2011
2012                                 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
2013                                         // Decode this status header and goto EXPECT_HDR1 (if we
2014                                         // can process the status with only 2 bytes), or goto
2015                                         // EXPECT_HDR3 to get the third byte.
2016
2017                                         edge_serial->rxPort       = IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
2018                                         edge_serial->rxStatusCode = IOSP_GET_STATUS_CODE(edge_serial->rxHeader1);
2019
2020                                         if (!IOSP_STATUS_IS_2BYTE(edge_serial->rxStatusCode)) {
2021                                                 // This status needs additional bytes. Save what we have
2022                                                 // and then wait for more data.
2023                                                 edge_serial->rxStatusParam = edge_serial->rxHeader2;
2024
2025                                                 edge_serial->rxState = EXPECT_HDR3;
2026                                                 break;
2027                                         }
2028
2029                                         // We have all the header bytes, process the status now
2030                                         process_rcvd_status (edge_serial, edge_serial->rxHeader2, 0);
2031
2032                                         edge_serial->rxState = EXPECT_HDR1;
2033                                         break;
2034                                 } else {
2035                                         edge_serial->rxPort = IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
2036                                         edge_serial->rxBytesRemaining = IOSP_GET_HDR_DATA_LEN(edge_serial->rxHeader1, edge_serial->rxHeader2);
2037
2038                                         dbg(__FUNCTION__" - Data for Port %u Len %u", edge_serial->rxPort, edge_serial->rxBytesRemaining);
2039
2040                                         //ASSERT( DevExt->RxPort < DevExt->NumPorts );
2041                                         //ASSERT( DevExt->RxBytesRemaining < IOSP_MAX_DATA_LENGTH );
2042
2043                                         if (bufferLength == 0 ) {
2044                                                 edge_serial->rxState = EXPECT_DATA;
2045                                                 break;
2046                                         }
2047                                         // Else, drop through
2048                                 }
2049
2050                         case EXPECT_DATA:       // Expect data
2051
2052                                 if (bufferLength < edge_serial->rxBytesRemaining) {
2053                                         rxLen = bufferLength;
2054                                         edge_serial->rxState = EXPECT_DATA;     // Expect data to start next buffer
2055                                 } else {
2056                                         // BufLen >= RxBytesRemaining
2057                                         rxLen = edge_serial->rxBytesRemaining;
2058                                         edge_serial->rxState = EXPECT_HDR1;     // Start another header next time
2059                                 }
2060
2061                                 bufferLength -= rxLen;
2062                                 edge_serial->rxBytesRemaining -= rxLen;
2063
2064                                 /* spit this data back into the tty driver if this port is open */
2065                                 if (rxLen) {
2066                                         port = &edge_serial->serial->port[edge_serial->rxPort];
2067                                         if (port_paranoia_check (port, __FUNCTION__) == 0) {
2068                                                 edge_port = (struct edgeport_port *)port->private;
2069                                                 if (edge_port->open) {
2070                                                         tty = edge_port->port->tty;
2071                                                         if (tty) {
2072                                                                 dbg (__FUNCTION__" - Sending %d bytes to TTY for port %d", rxLen, edge_serial->rxPort);
2073                                                                 for (i = 0; i < rxLen ; ++i) {
2074                                                                         /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
2075                                                                         if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
2076                                                                                 tty_flip_buffer_push(tty);
2077                                                                         }
2078                                                                         /* this doesn't actually push the data through unless tty->low_latency is set */
2079                                                                         tty_insert_flip_char(tty, buffer[i], 0);
2080                                                                 }
2081                                                                 tty_flip_buffer_push(tty);
2082                                                         }
2083                                                         edge_port->icount.rx += rxLen;
2084                                                 }
2085                                         }
2086                                         buffer += rxLen;
2087                                 }
2088
2089                                 break;
2090
2091                         case EXPECT_HDR3:                       // Expect 3rd byte of status header
2092                                 edge_serial->rxHeader3 = *buffer;
2093                                 ++buffer;
2094                                 --bufferLength;
2095
2096                                 // We have all the header bytes, process the status now
2097                                 process_rcvd_status (edge_serial, edge_serial->rxStatusParam, edge_serial->rxHeader3);
2098                                 edge_serial->rxState = EXPECT_HDR1;
2099                                 break;
2100
2101                 }
2102         }
2103
2104         return 0;
2105 }
2106
2107
2108 /*****************************************************************************
2109  * process_rcvd_status
2110  *      this function handles the any status messages received on the bulk in pipe.
2111  *****************************************************************************/
2112 static void process_rcvd_status (struct edgeport_serial *edge_serial, __u8 byte2, __u8 byte3)
2113 {
2114         struct usb_serial_port *port;
2115         struct edgeport_port *edge_port;
2116         __u8 code = edge_serial->rxStatusCode;
2117
2118         /* switch the port pointer to the one being currently talked about */
2119         port = &edge_serial->serial->port[edge_serial->rxPort];
2120         if (port_paranoia_check (port, __FUNCTION__)) {
2121                 return;
2122         }
2123         edge_port = (struct edgeport_port *)port->private;
2124         if (edge_port == NULL) {
2125                 err (__FUNCTION__ " - edge_port == NULL for port %d", edge_serial->rxPort);
2126                 return;
2127         }
2128
2129         dbg(__FUNCTION__" - port %d", edge_serial->rxPort);
2130
2131         if (code == IOSP_EXT_STATUS) {
2132                 switch (byte2) {
2133                         case IOSP_EXT_STATUS_CHASE_RSP:
2134                                 // we want to do EXT status regardless of port open/closed 
2135                                 dbg(__FUNCTION__" - Port %u EXT CHASE_RSP Data = %02x", edge_serial->rxPort, byte3 );
2136                                 // Currently, the only EXT_STATUS is Chase, so process here instead of one more call
2137                                 // to one more subroutine. If/when more EXT_STATUS, there'll be more work to do.
2138                                 // Also, we currently clear flag and close the port regardless of content of above's Byte3.
2139                                 // We could choose to do something else when Byte3 says Timeout on Chase from Edgeport,
2140                                 // like wait longer in block_until_chase_response, but for now we don't. 
2141                                 edge_port->chaseResponsePending = FALSE;
2142                                 wake_up_interruptible (&edge_port->wait_chase);
2143                                 return;
2144
2145                         case IOSP_EXT_STATUS_RX_CHECK_RSP:
2146                                 dbg( __FUNCTION__" ========== Port %u CHECK_RSP Sequence = %02x =============\n", edge_serial->rxPort, byte3 );
2147                                 //Port->RxCheckRsp = TRUE;
2148                                 return;
2149                 }
2150         }
2151
2152         if (code == IOSP_STATUS_OPEN_RSP) {
2153                 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
2154                 edge_port->maxTxCredits = edge_port->txCredits;
2155                 dbg (__FUNCTION__" - Port %u Open Response Inital MSR = %02x TxBufferSize = %d", edge_serial->rxPort, byte2, edge_port->txCredits);
2156                 handle_new_msr (edge_port, byte2);
2157
2158                 /* send the current line settings to the port so we are in sync with any further termios calls */
2159                 change_port_settings (edge_port, edge_port->port->tty->termios);
2160
2161                 /* we have completed the open */
2162                 edge_port->openPending = FALSE;
2163                 edge_port->open = TRUE;
2164                 wake_up_interruptible(&edge_port->wait_open);
2165                 return;
2166         }
2167
2168         // If port is closed, silently discard all rcvd status. We can
2169         // have cases where buffered status is received AFTER the close
2170         // port command is sent to the Edgeport.
2171         if ((!edge_port->open ) || (edge_port->closePending)) {
2172                 return;
2173         }
2174
2175         switch (code) {
2176                 // Not currently sent by Edgeport
2177                 case IOSP_STATUS_LSR:
2178                         dbg(__FUNCTION__" - Port %u LSR Status = %02x", edge_serial->rxPort, byte2);
2179                         handle_new_lsr (edge_port, FALSE, byte2, 0);
2180                         break;
2181
2182                 case IOSP_STATUS_LSR_DATA:
2183                         dbg(__FUNCTION__" - Port %u LSR Status = %02x, Data = %02x", edge_serial->rxPort, byte2, byte3);
2184                         // byte2 is LSR Register
2185                         // byte3 is broken data byte
2186                         handle_new_lsr (edge_port, TRUE, byte2, byte3);
2187                         break;
2188                         //
2189                         //      case IOSP_EXT_4_STATUS:
2190                         //              dbg(__FUNCTION__" - Port %u LSR Status = %02x Data = %02x", edge_serial->rxPort, byte2, byte3);
2191                         //              break;
2192                         //
2193                 case IOSP_STATUS_MSR:
2194                         dbg(__FUNCTION__" - Port %u MSR Status = %02x", edge_serial->rxPort, byte2);
2195
2196                         // Process this new modem status and generate appropriate
2197                         // events, etc, based on the new status. This routine
2198                         // also saves the MSR in Port->ShadowMsr.
2199                         handle_new_msr(edge_port, byte2);
2200                         break;
2201
2202                 default:
2203                         dbg(__FUNCTION__" - Unrecognized IOSP status code %u\n", code);
2204                         break;
2205         }
2206
2207         return;
2208 }
2209
2210
2211 /*****************************************************************************
2212  * handle_new_msr
2213  *      this function handles any change to the msr register for a port.
2214  *****************************************************************************/
2215 static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
2216 {
2217         struct  async_icount *icount;
2218
2219         dbg(__FUNCTION__" %02x", newMsr);
2220
2221         if (newMsr & (MSR_DELTA_CTS | MSR_DELTA_DSR | MSR_DELTA_RI | MSR_DELTA_CD)) {
2222                 icount = &edge_port->icount;
2223
2224                 /* update input line counters */
2225                 if (newMsr & MSR_DELTA_CTS) {
2226                         icount->cts++;
2227                 }
2228                 if (newMsr & MSR_DELTA_DSR) {
2229                         icount->dsr++;
2230                 }
2231                 if (newMsr & MSR_DELTA_CD) {
2232                         icount->dcd++;
2233                 }
2234                 if (newMsr & MSR_DELTA_RI) {
2235                         icount->rng++;
2236                 }
2237                 wake_up_interruptible(&edge_port->delta_msr_wait);
2238         }
2239
2240         /* Save the new modem status */
2241         edge_port->shadowMSR = newMsr & 0xf0;
2242
2243         return;
2244 }
2245
2246
2247 /*****************************************************************************
2248  * handle_new_lsr
2249  *      this function handles any change to the lsr register for a port.
2250  *****************************************************************************/
2251 static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData, __u8 lsr, __u8 data)
2252 {
2253         __u8    newLsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2254         struct  async_icount *icount;
2255
2256         dbg(__FUNCTION__" - %02x", newLsr);
2257
2258         edge_port->shadowLSR = lsr;
2259
2260         if (newLsr & LSR_BREAK) {
2261                 //
2262                 // Parity and Framing errors only count if they
2263                 // occur exclusive of a break being
2264                 // received.
2265                 //
2266                 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2267         }
2268
2269         /* Place LSR data byte into Rx buffer */
2270         if (lsrData) {
2271                 tty_insert_flip_char(edge_port->port->tty, data, 0);
2272                 tty_flip_buffer_push(edge_port->port->tty);
2273         }
2274
2275         /* update input line counters */
2276         icount = &edge_port->icount;
2277         if (newLsr & LSR_BREAK) {
2278                 icount->brk++;
2279         }
2280         if (newLsr & LSR_OVER_ERR) {
2281                 icount->overrun++;
2282         }
2283         if (newLsr & LSR_PAR_ERR) {
2284                 icount->parity++;
2285         }
2286         if (newLsr & LSR_FRM_ERR) {
2287                 icount->frame++;
2288         }
2289
2290         return;
2291 }
2292
2293
2294 /****************************************************************************
2295  * sram_write
2296  *      writes a number of bytes to the Edgeport device's sram starting at the 
2297  *      given address.
2298  *      If successful returns the number of bytes written, otherwise it returns
2299  *      a negative error number of the problem.
2300  ****************************************************************************/
2301 static int sram_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
2302 {
2303         int result;
2304         __u16 current_length;
2305         unsigned char *transfer_buffer;
2306
2307 //      dbg (__FUNCTION__" - %x, %x, %d", extAddr, addr, length);
2308
2309         transfer_buffer =  kmalloc (64, GFP_KERNEL);
2310         if (!transfer_buffer) {
2311                 err(__FUNCTION__" - kmalloc(%d) failed.\n", 64);
2312                 return -ENOMEM;
2313         }
2314
2315         /* need to split these writes up into 64 byte chunks */
2316         result = 0;
2317         while (length > 0) {
2318                 if (length > 64) {
2319                         current_length = 64;
2320                 } else {
2321                         current_length = length;
2322                 }
2323 //              dbg (__FUNCTION__" - writing %x, %x, %d", extAddr, addr, current_length);
2324                 memcpy (transfer_buffer, data, current_length);
2325                 result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), USB_REQUEST_ION_WRITE_RAM, 
2326                                           0x40, addr, extAddr, transfer_buffer, current_length, 300);
2327                 if (result < 0)
2328                         break;
2329                 length -= current_length;
2330                 addr += current_length;
2331                 data += current_length;
2332         }       
2333
2334         kfree (transfer_buffer);
2335         return result;
2336 }
2337
2338
2339 /****************************************************************************
2340  * rom_write
2341  *      writes a number of bytes to the Edgeport device's ROM starting at the
2342  *      given address.
2343  *      If successful returns the number of bytes written, otherwise it returns
2344  *      a negative error number of the problem.
2345  ****************************************************************************/
2346 static int rom_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
2347 {
2348         int result;
2349         __u16 current_length;
2350         unsigned char *transfer_buffer;
2351
2352 //      dbg (__FUNCTION__" - %x, %x, %d", extAddr, addr, length);
2353
2354         transfer_buffer =  kmalloc (64, GFP_KERNEL);
2355         if (!transfer_buffer) {
2356                 err(__FUNCTION__" - kmalloc(%d) failed.\n", 64);
2357                 return -ENOMEM;
2358         }
2359
2360         /* need to split these writes up into 64 byte chunks */
2361         result = 0;
2362         while (length > 0) {
2363                 if (length > 64) {
2364                         current_length = 64;
2365                 } else {
2366                         current_length = length;
2367                 }
2368 //              dbg (__FUNCTION__" - writing %x, %x, %d", extAddr, addr, current_length);
2369                 memcpy (transfer_buffer, data, current_length);
2370                 result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), USB_REQUEST_ION_WRITE_ROM, 
2371                                           0x40, addr, extAddr, transfer_buffer, current_length, 300);
2372                 if (result < 0)
2373                         break;
2374                 length -= current_length;
2375                 addr += current_length;
2376                 data += current_length;
2377         }       
2378
2379         kfree (transfer_buffer);
2380         return result;
2381 }
2382
2383
2384 /****************************************************************************
2385  * rom_read
2386  *      reads a number of bytes from the Edgeport device starting at the given
2387  *      address.
2388  *      If successful returns the number of bytes read, otherwise it returns
2389  *      a negative error number of the problem.
2390  ****************************************************************************/
2391 static int rom_read (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
2392 {
2393         int result;
2394         __u16 current_length;
2395         unsigned char *transfer_buffer;
2396
2397         dbg (__FUNCTION__" - %x, %x, %d", extAddr, addr, length);
2398
2399         transfer_buffer =  kmalloc (64, GFP_KERNEL);
2400         if (!transfer_buffer) {
2401                 err(__FUNCTION__" - kmalloc(%d) failed.\n", 64);
2402                 return -ENOMEM;
2403         }
2404
2405         /* need to split these reads up into 64 byte chunks */
2406         result = 0;
2407         while (length > 0) {
2408                 if (length > 64) {
2409                         current_length = 64;
2410                 } else {
2411                         current_length = length;
2412                 }
2413 //              dbg (__FUNCTION__" - %x, %x, %d", extAddr, addr, current_length);
2414                 result = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), USB_REQUEST_ION_READ_ROM, 
2415                                           0xC0, addr, extAddr, transfer_buffer, current_length, 300);
2416                 if (result < 0)
2417                         break;
2418                 memcpy (data, transfer_buffer, current_length);
2419                 length -= current_length;
2420                 addr += current_length;
2421                 data += current_length;
2422         }       
2423
2424         kfree (transfer_buffer);
2425         return result;
2426 }
2427
2428
2429 /****************************************************************************
2430  * send_iosp_ext_cmd
2431  *      Is used to send a IOSP message to the Edgeport device
2432  ****************************************************************************/
2433 static int send_iosp_ext_cmd (struct edgeport_port *edge_port, __u8 command, __u8 param)
2434 {
2435         unsigned char   *buffer;
2436         unsigned char   *currentCommand;
2437         int             length = 0;
2438         int             status = 0;
2439
2440         dbg(__FUNCTION__" - %d, %d", command, param);
2441
2442         buffer =  kmalloc (10, GFP_KERNEL);
2443         if (!buffer) {
2444                 err(__FUNCTION__" - kmalloc(%d) failed.\n", 10);
2445                 return -ENOMEM;
2446         }
2447
2448         currentCommand = buffer;
2449
2450         MAKE_CMD_EXT_CMD( &currentCommand, &length, edge_port->port->number, command, param);
2451
2452         status = write_cmd_usb (edge_port, buffer, length);
2453         if (status) {
2454                 /* something bad happened, let's free up the memory */
2455                 kfree(buffer);
2456         }
2457
2458         return status;
2459 }
2460
2461
2462 /*****************************************************************************
2463  * write_cmd_usb
2464  *      this function writes the given buffer out to the bulk write endpoint.
2465  *****************************************************************************/
2466 static int write_cmd_usb (struct edgeport_port *edge_port, unsigned char *buffer, int length)
2467 {
2468         struct edgeport_serial *edge_serial = (struct edgeport_serial *)edge_port->port->serial->private;
2469         int status = 0;
2470         urb_t *urb;
2471         int timeout;
2472
2473         usb_serial_debug_data (__FILE__, __FUNCTION__, length, buffer);
2474
2475         /* Allocate our next urb */
2476         urb = usb_alloc_urb (0);
2477
2478         CmdUrbs++;
2479
2480         dbg(__FUNCTION__" - ALLOCATE URB %p (outstanding %d)", urb, CmdUrbs);
2481
2482         if (!urb) {
2483                 return -ENOMEM;
2484         }
2485
2486         FILL_BULK_URB (urb, edge_serial->serial->dev, 
2487                        usb_sndbulkpipe(edge_serial->serial->dev, edge_serial->bulk_out_endpoint),
2488                        buffer, length, edge_bulk_out_cmd_callback, edge_port);
2489
2490         /* set the USB_BULK_QUEUE flag so that we can shove a bunch of urbs at once down the pipe */
2491         urb->transfer_flags |= USB_QUEUE_BULK;
2492
2493         edge_port->commandPending = TRUE;
2494         urb->dev = edge_serial->serial->dev;
2495         status = usb_submit_urb(urb);
2496
2497         if (status) {
2498                 /* something went wrong */
2499                 dbg(__FUNCTION__" - usb_submit_urb(write bulk) failed");
2500
2501                 /* if this urb had a transfer buffer already (old transfer) free it */
2502                 if (urb->transfer_buffer != NULL) {
2503                         kfree(urb->transfer_buffer);
2504                 }
2505                 usb_unlink_urb (urb);
2506                 usb_free_urb   (urb);
2507                 return status;
2508         }
2509
2510         // wait for command to finish
2511         timeout = COMMAND_TIMEOUT;
2512 #if 0
2513         while (timeout && edge_port->commandPending == TRUE) {
2514                 timeout = interruptible_sleep_on_timeout (&edge_port->wait_command, timeout);
2515         }
2516
2517         if (edge_port->commandPending == TRUE) {
2518                 /* command timed out */
2519                 dbg(__FUNCTION__" - command timed out");
2520                 status = -EINVAL;
2521         }
2522 #endif
2523         return status;
2524 }
2525
2526
2527 /*****************************************************************************
2528  * send_cmd_write_baud_rate
2529  *      this function sends the proper command to change the baud rate of the
2530  *      specified port.
2531  *****************************************************************************/
2532 static int send_cmd_write_baud_rate (struct edgeport_port *edge_port, int baudRate)
2533 {
2534         unsigned char *cmdBuffer;
2535         unsigned char *currCmd;
2536         int cmdLen = 0;
2537         int divisor;
2538         int status;
2539         unsigned char number = edge_port->port->number;
2540
2541         dbg(__FUNCTION__" - port = %d, baud = %d", number, baudRate);
2542
2543         status = calc_baud_rate_divisor (baudRate, &divisor);
2544         if (status) {
2545                 err(__FUNCTION__" - bad baud rate");
2546                 return status;
2547         }
2548
2549         // Alloc memory for the string of commands.
2550         cmdBuffer =  kmalloc (0x100, GFP_KERNEL);
2551         if (!cmdBuffer) {
2552                 err(__FUNCTION__" - kmalloc(%d) failed.\n", 0x100);
2553                 return -ENOMEM;
2554         }
2555         currCmd = cmdBuffer;
2556
2557         // Enable access to divisor latch
2558         MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE );
2559
2560         // Write the divisor itself
2561         MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, DLL, LOW8 (divisor) );
2562         MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, DLM, HIGH8(divisor) );
2563
2564         // Restore original value to disable access to divisor latch
2565         MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, LCR, edge_port->shadowLCR);
2566
2567         status = write_cmd_usb(edge_port, cmdBuffer, cmdLen );
2568