8b2e4c78abcd55eca44e160df649c6e97202d5b5
[linux-2.6.git] / drivers / usb / serial / io_ti.c
1 /*
2  * Edgeport USB Serial Converter driver
3  *
4  * Copyright (C) 2000-2002 Inside Out Networks, All rights reserved.
5  * Copyright (C) 2001-2002 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  *      EP/1 EP/2 EP/4 EP/21 EP/22 EP/221 EP/42 EP/421 WATCHPORT
14  *
15  * For questions or problems with this driver, contact Inside Out
16  * Networks technical support, or Peter Berger <pberger@brimson.com>,
17  * or Al Borchers <alborchers@steinerpoint.com>.
18  *
19  * Version history:
20  *
21  *      July 11, 2002   Removed 4 port device structure since all TI UMP 
22  *                      chips have only 2 ports 
23  *                      David Iacovelli (davidi@ionetworks.com)
24  *
25  */
26
27 #include <linux/config.h>
28 #include <linux/kernel.h>
29 #include <linux/jiffies.h>
30 #include <linux/errno.h>
31 #include <linux/init.h>
32 #include <linux/slab.h>
33 #include <linux/tty.h>
34 #include <linux/tty_driver.h>
35 #include <linux/tty_flip.h>
36 #include <linux/module.h>
37 #include <linux/spinlock.h>
38 #include <linux/serial.h>
39 #include <linux/ioctl.h>
40 #include <asm/uaccess.h>
41 #include <asm/semaphore.h>
42 #include <linux/usb.h>
43
44 #include "usb-serial.h"
45 #include "io_16654.h"
46 #include "io_usbvend.h"
47 #include "io_ti.h"
48
49 /*
50  * Version Information
51  */
52 #define DRIVER_VERSION "v0.7"
53 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
54 #define DRIVER_DESC "Edgeport USB Serial Driver"
55
56
57 /* firmware image code */
58 #define IMAGE_VERSION_NAME      PagableOperationalCodeImageVersion
59 #define IMAGE_ARRAY_NAME        PagableOperationalCodeImage
60 #define IMAGE_SIZE              PagableOperationalCodeSize
61 #include "io_fw_down3.h"        /* Define array OperationalCodeImage[] */
62
63 #define EPROM_PAGE_SIZE         64
64
65
66 struct edgeport_uart_buf_desc {
67         __u32 count;            // Number of bytes currently in buffer
68 };
69
70 /* different hardware types */
71 #define HARDWARE_TYPE_930       0
72 #define HARDWARE_TYPE_TIUMP     1
73
74 // IOCTL_PRIVATE_TI_GET_MODE Definitions
75 #define TI_MODE_CONFIGURING     0   // Device has not entered start device 
76 #define TI_MODE_BOOT            1   // Staying in boot mode
77 #define TI_MODE_DOWNLOAD        2   // Made it to download mode
78 #define TI_MODE_TRANSITIONING   3   // Currently in boot mode but transitioning to download mode
79
80 /* read urb state */
81 #define EDGE_READ_URB_RUNNING   0
82 #define EDGE_READ_URB_STOPPING  1
83 #define EDGE_READ_URB_STOPPED   2
84
85 #define EDGE_LOW_LATENCY        1
86 #define EDGE_CLOSING_WAIT       4000    /* in .01 sec */
87
88 #define EDGE_OUT_BUF_SIZE       1024
89
90
91 /* Product information read from the Edgeport */
92 struct product_info
93 {
94         int     TiMode;                 // Current TI Mode
95         __u8    hardware_type;          // Type of hardware
96 } __attribute__((packed));
97
98 /* circular buffer */
99 struct edge_buf {
100         unsigned int    buf_size;
101         char            *buf_buf;
102         char            *buf_get;
103         char            *buf_put;
104 };
105
106 struct edgeport_port {
107         __u16 uart_base;
108         __u16 dma_address;
109         __u8 shadow_msr;
110         __u8 shadow_mcr;
111         __u8 shadow_lsr;
112         __u8 lsr_mask;
113         __u32 ump_read_timeout;         /* Number of miliseconds the UMP will
114                                            wait without data before completing
115                                            a read short */
116         int baud_rate;
117         int close_pending;
118         int lsr_event;
119         struct edgeport_uart_buf_desc tx;
120         struct async_icount     icount;
121         wait_queue_head_t       delta_msr_wait; /* for handling sleeping while
122                                                    waiting for msr change to
123                                                    happen */
124         struct edgeport_serial  *edge_serial;
125         struct usb_serial_port  *port;
126         __u8 bUartMode;         /* Port type, 0: RS232, etc. */ 
127         spinlock_t ep_lock;
128         int ep_read_urb_state;
129         int ep_write_urb_in_use;
130         struct edge_buf *ep_out_buf;
131 };
132
133 struct edgeport_serial {
134         struct product_info product_info;
135         u8 TI_I2C_Type;                 // Type of I2C in UMP
136         u8 TiReadI2C;                   // Set to TRUE if we have read the I2c in Boot Mode
137         struct semaphore es_sem;
138         int num_ports_open;
139         struct usb_serial *serial;
140 };
141
142
143 /* Devices that this driver supports */
144 static struct usb_device_id edgeport_1port_id_table [] = {
145         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
146         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
147         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
148         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
149         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
150         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
151         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
152         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
153         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
154         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
155         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
156         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
157         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
158         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
159         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
160         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
161         { }
162 };
163
164 static struct usb_device_id edgeport_2port_id_table [] = {
165         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
166         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
167         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
168         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
169         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
170         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
171         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
172         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
173         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
174         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
175         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
176         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
177 // The 4-port shows up as two 2-port devices
178         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
179         { }
180 };
181
182 /* Devices that this driver supports */
183 static struct usb_device_id id_table_combined [] = {
184         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
185         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
186         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
187         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
188         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
189         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
190         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
191         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
192         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
193         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
194         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
195         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
196         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
197         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
198         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
199         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
200         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
201         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
202         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
203         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
204         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
205         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
206         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
207         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
208         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
209         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
210         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
211         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
212         { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
213         { }
214 };
215
216 MODULE_DEVICE_TABLE (usb, id_table_combined);
217
218 static struct usb_driver io_driver = {
219         .owner =        THIS_MODULE,
220         .name =         "io_ti",
221         .probe =        usb_serial_probe,
222         .disconnect =   usb_serial_disconnect,
223         .id_table =     id_table_combined,
224         .no_dynamic_id =        1,
225 };
226
227
228 static struct EDGE_FIRMWARE_VERSION_INFO OperationalCodeImageVersion;
229
230 static int debug;
231
232 static int TIStayInBootMode = 0;
233 static int low_latency = EDGE_LOW_LATENCY;
234 static int closing_wait = EDGE_CLOSING_WAIT;
235 static int ignore_cpu_rev = 0;
236
237
238 static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length);
239
240 static void stop_read(struct edgeport_port *edge_port);
241 static int restart_read(struct edgeport_port *edge_port);
242
243 static void edge_set_termios (struct usb_serial_port *port, struct termios *old_termios);
244 static void edge_send(struct usb_serial_port *port);
245
246 /* circular buffer */
247 static struct edge_buf *edge_buf_alloc(unsigned int size);
248 static void edge_buf_free(struct edge_buf *eb);
249 static void edge_buf_clear(struct edge_buf *eb);
250 static unsigned int edge_buf_data_avail(struct edge_buf *eb);
251 static unsigned int edge_buf_space_avail(struct edge_buf *eb);
252 static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
253         unsigned int count);
254 static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
255         unsigned int count);
256
257
258 static int TIReadVendorRequestSync (struct usb_device *dev,
259                                 __u8            request,
260                                 __u16           value,
261                                 __u16           index,
262                                 u8              *data,
263                                 int             size)
264 {
265         int status;
266
267         status = usb_control_msg (dev,
268                                 usb_rcvctrlpipe(dev, 0),
269                                 request,
270                                 (USB_TYPE_VENDOR | 
271                                  USB_RECIP_DEVICE | 
272                                  USB_DIR_IN),
273                                 value,
274                                 index,
275                                 data,
276                                 size,
277                                 1000);
278         if (status < 0)
279                 return status;
280         if (status != size) {
281                 dbg ("%s - wanted to write %d, but only wrote %d",
282                      __FUNCTION__, size, status);
283                 return -ECOMM;
284         }
285         return 0;
286 }
287
288 static int TISendVendorRequestSync (struct usb_device *dev,
289                                 __u8            request,
290                                 __u16           value,
291                                 __u16           index,
292                                 u8              *data,
293                                 int             size)
294 {
295         int status;
296
297         status = usb_control_msg (dev,
298                                 usb_sndctrlpipe(dev, 0),
299                                 request,
300                                 (USB_TYPE_VENDOR | 
301                                  USB_RECIP_DEVICE | 
302                                  USB_DIR_OUT),
303                                 value,
304                                 index,
305                                 data,
306                                 size,
307                                 1000);
308         if (status < 0)
309                 return status;
310         if (status != size) {
311                 dbg ("%s - wanted to write %d, but only wrote %d",
312                      __FUNCTION__, size, status);
313                 return -ECOMM;
314         }
315         return 0;
316 }
317
318 static int TIWriteCommandSync (struct usb_device *dev, __u8 command,
319                                 __u8 moduleid, __u16 value, u8 *data,
320                                 int size)
321 {
322         return TISendVendorRequestSync (dev,
323                                           command,                      // Request
324                                           value,                        // wValue 
325                                           moduleid,                     // wIndex
326                                           data,                         // TransferBuffer
327                                           size);                        // TransferBufferLength
328
329 }
330
331 /* clear tx/rx buffers and fifo in TI UMP */
332 static int TIPurgeDataSync (struct usb_serial_port *port, __u16 mask)
333 {
334         int port_number = port->number - port->serial->minor;
335
336         dbg ("%s - port %d, mask %x", __FUNCTION__, port_number, mask);
337
338         return TIWriteCommandSync (port->serial->dev,
339                                         UMPC_PURGE_PORT,
340                                         (__u8)(UMPM_UART1_PORT + port_number),
341                                         mask,
342                                         NULL,
343                                         0);
344 }
345
346 /**
347  * TIReadDownloadMemory - Read edgeport memory from TI chip
348  * @dev: usb device pointer
349  * @start_address: Device CPU address at which to read
350  * @length: Length of above data
351  * @address_type: Can read both XDATA and I2C
352  * @buffer: pointer to input data buffer
353  */
354 static int TIReadDownloadMemory(struct usb_device *dev, int start_address,
355                                 int length, __u8 address_type, __u8 *buffer)
356 {
357         int status = 0;
358         __u8 read_length;
359         __be16 be_start_address;
360         
361         dbg ("%s - @ %x for %d", __FUNCTION__, start_address, length);
362
363         /* Read in blocks of 64 bytes
364          * (TI firmware can't handle more than 64 byte reads)
365          */
366         while (length) {
367                 if (length > 64)
368                         read_length= 64;
369                 else
370                         read_length = (__u8)length;
371
372                 if (read_length > 1) {
373                         dbg ("%s - @ %x for %d", __FUNCTION__, 
374                              start_address, read_length);
375                 }
376                 be_start_address = cpu_to_be16 (start_address);
377                 status = TIReadVendorRequestSync (dev,
378                                                   UMPC_MEMORY_READ,     // Request
379                                                   (__u16)address_type,  // wValue (Address type)
380                                                   (__force __u16)be_start_address,      // wIndex (Address to read)
381                                                   buffer,               // TransferBuffer
382                                                   read_length); // TransferBufferLength
383
384                 if (status) {
385                         dbg ("%s - ERROR %x", __FUNCTION__, status);
386                         return status;
387                 }
388
389                 if (read_length > 1) {
390                         usb_serial_debug_data(debug, &dev->dev, __FUNCTION__,
391                                               read_length, buffer);
392                 }
393
394                 /* Update pointers/length */
395                 start_address += read_length;
396                 buffer += read_length;
397                 length -= read_length;
398         }
399         
400         return status;
401 }
402
403 static int TIReadRam (struct usb_device *dev, int start_address, int length, __u8 *buffer)
404 {
405         return TIReadDownloadMemory (dev,
406                                      start_address,
407                                      length,
408                                      DTK_ADDR_SPACE_XDATA,
409                                      buffer);
410 }
411
412 /* Read edgeport memory to a given block */
413 static int TIReadBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 * buffer)
414 {
415         int status = 0;
416         int i;
417
418         for (i=0; i< length; i++) {
419                 status = TIReadVendorRequestSync (serial->serial->dev,
420                                         UMPC_MEMORY_READ,               // Request
421                                         serial->TI_I2C_Type,            // wValue (Address type)
422                                         (__u16)(start_address+i),       // wIndex
423                                         &buffer[i],                     // TransferBuffer
424                                         0x01);                          // TransferBufferLength
425                 if (status) {
426                         dbg ("%s - ERROR %x", __FUNCTION__, status);
427                         return status;
428                 }
429         }
430
431         dbg ("%s - start_address = %x, length = %d", __FUNCTION__, start_address, length);
432         usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, length, buffer);
433
434         serial->TiReadI2C = 1;
435
436         return status;
437 }
438
439 /* Write given block to TI EPROM memory */
440 static int TIWriteBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
441 {
442         int status = 0;
443         int i;
444         __u8 temp;
445
446         /* Must do a read before write */
447         if (!serial->TiReadI2C) {
448                 status = TIReadBootMemory(serial, 0, 1, &temp);
449                 if (status)
450                         return status;
451         }
452
453         for (i=0; i < length; ++i) {
454                 status = TISendVendorRequestSync (serial->serial->dev,
455                                                 UMPC_MEMORY_WRITE,              // Request
456                                                 buffer[i],                      // wValue
457                                                 (__u16)(i+start_address),       // wIndex
458                                                 NULL,                           // TransferBuffer
459                                                 0);                             // TransferBufferLength
460                 if (status)
461                         return status;
462         }
463
464         dbg ("%s - start_sddr = %x, length = %d", __FUNCTION__, start_address, length);
465         usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, length, buffer);
466
467         return status;
468 }
469
470
471 /* Write edgeport I2C memory to TI chip */
472 static int TIWriteDownloadI2C (struct edgeport_serial *serial, int start_address, int length, __u8 address_type, __u8 *buffer)
473 {
474         int status = 0;
475         int write_length;
476         __be16 be_start_address;
477
478         /* We can only send a maximum of 1 aligned byte page at a time */
479         
480         /* calulate the number of bytes left in the first page */
481         write_length = EPROM_PAGE_SIZE - (start_address & (EPROM_PAGE_SIZE - 1));
482
483         if (write_length > length)
484                 write_length = length;
485
486         dbg ("%s - BytesInFirstPage Addr = %x, length = %d", __FUNCTION__, start_address, write_length);
487         usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, write_length, buffer);
488
489         /* Write first page */
490         be_start_address = cpu_to_be16 (start_address);
491         status = TISendVendorRequestSync (serial->serial->dev,
492                                         UMPC_MEMORY_WRITE,      // Request
493                                         (__u16)address_type,    // wValue
494                                         (__force __u16)be_start_address,        // wIndex
495                                         buffer,                 // TransferBuffer
496                                         write_length);
497         if (status) {
498                 dbg ("%s - ERROR %d", __FUNCTION__, status);
499                 return status;
500         }
501
502         length          -= write_length;
503         start_address   += write_length;
504         buffer          += write_length;
505
506         /* We should be aligned now -- can write max page size bytes at a time */
507         while (length) {
508                 if (length > EPROM_PAGE_SIZE)
509                         write_length = EPROM_PAGE_SIZE;
510                 else
511                         write_length = length;
512
513                 dbg ("%s - Page Write Addr = %x, length = %d", __FUNCTION__, start_address, write_length);
514                 usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, write_length, buffer);
515
516                 /* Write next page */
517                 be_start_address = cpu_to_be16 (start_address);
518                 status = TISendVendorRequestSync (serial->serial->dev,
519                                                 UMPC_MEMORY_WRITE,      // Request
520                                                 (__u16)address_type,    // wValue
521                                                 (__force __u16)be_start_address,        // wIndex
522                                                 buffer,                 // TransferBuffer
523                                                 write_length);          // TransferBufferLength
524                 if (status) {
525                         dev_err (&serial->serial->dev->dev, "%s - ERROR %d\n", __FUNCTION__, status);
526                         return status;
527                 }
528                 
529                 length          -= write_length;
530                 start_address   += write_length;
531                 buffer          += write_length;
532         }
533         return status;
534 }
535
536 /* Examine the UMP DMA registers and LSR
537  * 
538  * Check the MSBit of the X and Y DMA byte count registers.
539  * A zero in this bit indicates that the TX DMA buffers are empty
540  * then check the TX Empty bit in the UART.
541  */
542 static int TIIsTxActive (struct edgeport_port *port)
543 {
544         int status;
545         struct out_endpoint_desc_block *oedb;
546         __u8 *lsr;
547         int bytes_left = 0;
548
549         oedb = kmalloc (sizeof (* oedb), GFP_KERNEL);
550         if (!oedb) {
551                 dev_err (&port->port->dev, "%s - out of memory\n", __FUNCTION__);
552                 return -ENOMEM;
553         }
554
555         lsr = kmalloc (1, GFP_KERNEL);  /* Sigh, that's right, just one byte,
556                                            as not all platforms can do DMA
557                                            from stack */
558         if (!lsr) {
559                 kfree(oedb);
560                 return -ENOMEM;
561         }
562         /* Read the DMA Count Registers */
563         status = TIReadRam (port->port->serial->dev,
564                             port->dma_address,
565                             sizeof( *oedb),
566                             (void *)oedb);
567
568         if (status)
569                 goto exit_is_tx_active;
570
571         dbg ("%s - XByteCount    0x%X", __FUNCTION__, oedb->XByteCount);
572
573         /* and the LSR */
574         status = TIReadRam (port->port->serial->dev, 
575                             port->uart_base + UMPMEM_OFFS_UART_LSR,
576                             1,
577                             lsr);
578
579         if (status)
580                 goto exit_is_tx_active;
581         dbg ("%s - LSR = 0x%X", __FUNCTION__, *lsr);
582         
583         /* If either buffer has data or we are transmitting then return TRUE */
584         if ((oedb->XByteCount & 0x80 ) != 0 )
585                 bytes_left += 64;
586
587         if ((*lsr & UMP_UART_LSR_TX_MASK ) == 0 )
588                 bytes_left += 1;
589
590         /* We return Not Active if we get any kind of error */
591 exit_is_tx_active:
592         dbg ("%s - return %d", __FUNCTION__, bytes_left );
593
594         kfree(lsr);
595         kfree(oedb);
596         return bytes_left;
597 }
598
599 static void TIChasePort(struct edgeport_port *port, unsigned long timeout, int flush)
600 {
601         int baud_rate;
602         struct tty_struct *tty = port->port->tty;
603         wait_queue_t wait;
604         unsigned long flags;
605
606         if (!timeout)
607                 timeout = (HZ*EDGE_CLOSING_WAIT)/100;
608
609         /* wait for data to drain from the buffer */
610         spin_lock_irqsave(&port->ep_lock, flags);
611         init_waitqueue_entry(&wait, current);
612         add_wait_queue(&tty->write_wait, &wait);
613         for (;;) {
614                 set_current_state(TASK_INTERRUPTIBLE);
615                 if (edge_buf_data_avail(port->ep_out_buf) == 0
616                 || timeout == 0 || signal_pending(current)
617                 || !usb_get_intfdata(port->port->serial->interface))  /* disconnect */
618                         break;
619                 spin_unlock_irqrestore(&port->ep_lock, flags);
620                 timeout = schedule_timeout(timeout);
621                 spin_lock_irqsave(&port->ep_lock, flags);
622         }
623         set_current_state(TASK_RUNNING);
624         remove_wait_queue(&tty->write_wait, &wait);
625         if (flush)
626                 edge_buf_clear(port->ep_out_buf);
627         spin_unlock_irqrestore(&port->ep_lock, flags);
628
629         /* wait for data to drain from the device */
630         timeout += jiffies;
631         while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
632         && usb_get_intfdata(port->port->serial->interface)) {  /* not disconnected */
633                 if (!TIIsTxActive(port))
634                         break;
635                 msleep(10);
636         }
637
638         /* disconnected */
639         if (!usb_get_intfdata(port->port->serial->interface))
640                 return;
641
642         /* wait one more character time, based on baud rate */
643         /* (TIIsTxActive doesn't seem to wait for the last byte) */
644         if ((baud_rate=port->baud_rate) == 0)
645                 baud_rate = 50;
646         msleep(max(1,(10000+baud_rate-1)/baud_rate));
647 }
648
649 static int TIChooseConfiguration (struct usb_device *dev)
650 {
651         // There may be multiple configurations on this device, in which case
652         // we would need to read and parse all of them to find out which one
653         // we want. However, we just support one config at this point,
654         // configuration # 1, which is Config Descriptor 0.
655
656         dbg ("%s - Number of Interfaces = %d", __FUNCTION__, dev->config->desc.bNumInterfaces);
657         dbg ("%s - MAX Power            = %d", __FUNCTION__, dev->config->desc.bMaxPower*2);
658
659         if (dev->config->desc.bNumInterfaces != 1) {
660                 dev_err (&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __FUNCTION__);
661                 return -ENODEV;
662         }
663
664         return 0;
665 }
666
667 static int TIReadRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
668 {
669         int status;
670
671         if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
672                 status = TIReadDownloadMemory (serial->serial->dev,
673                                                start_address,
674                                                length,
675                                                serial->TI_I2C_Type,
676                                                buffer);
677         } else {
678                 status = TIReadBootMemory (serial,
679                                            start_address,
680                                            length,
681                                            buffer);
682         }
683
684         return status;
685 }
686
687 static int TIWriteRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
688 {
689         if (serial->product_info.TiMode == TI_MODE_BOOT)
690                 return TIWriteBootMemory (serial,
691                                           start_address,
692                                           length,
693                                           buffer);
694
695         if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
696                 return TIWriteDownloadI2C (serial,
697                                            start_address,
698                                            length,
699                                            serial->TI_I2C_Type,
700                                            buffer);
701
702         return -EINVAL;
703 }
704
705
706
707 /* Read a descriptor header from I2C based on type */
708 static int TIGetDescriptorAddress (struct edgeport_serial *serial, int desc_type, struct ti_i2c_desc *rom_desc)
709 {
710         int start_address;
711         int status;
712
713         /* Search for requested descriptor in I2C */
714         start_address = 2;
715         do {
716                 status = TIReadRom (serial,
717                                    start_address,
718                                    sizeof(struct ti_i2c_desc),
719                                    (__u8 *)rom_desc );
720                 if (status)
721                         return 0;
722
723                 if (rom_desc->Type == desc_type)
724                         return start_address;
725
726                 start_address = start_address + sizeof(struct ti_i2c_desc) +  rom_desc->Size;
727
728         } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
729         
730         return 0;
731 }
732
733 /* Validate descriptor checksum */
734 static int ValidChecksum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
735 {
736         __u16 i;
737         __u8 cs = 0;
738
739         for (i=0; i < rom_desc->Size; i++) {
740                 cs = (__u8)(cs + buffer[i]);
741         }
742         if (cs != rom_desc->CheckSum) {
743                 dbg ("%s - Mismatch %x - %x", __FUNCTION__, rom_desc->CheckSum, cs);
744                 return -EINVAL;
745         }
746         return 0;
747 }
748
749 /* Make sure that the I2C image is good */
750 static int TiValidateI2cImage (struct edgeport_serial *serial)
751 {
752         struct device *dev = &serial->serial->dev->dev;
753         int status = 0;
754         struct ti_i2c_desc *rom_desc;
755         int start_address = 2;
756         __u8 *buffer;
757         __u16 ttype;
758
759         rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
760         if (!rom_desc) {
761                 dev_err (dev, "%s - out of memory\n", __FUNCTION__);
762                 return -ENOMEM;
763         }
764         buffer = kmalloc (TI_MAX_I2C_SIZE, GFP_KERNEL);
765         if (!buffer) {
766                 dev_err (dev, "%s - out of memory when allocating buffer\n", __FUNCTION__);
767                 kfree (rom_desc);
768                 return -ENOMEM;
769         }
770
771         // Read the first byte (Signature0) must be 0x52 or 0x10
772         status = TIReadRom (serial, 0, 1, buffer);
773         if (status)
774                 goto ExitTiValidateI2cImage; 
775
776         if (*buffer != UMP5152 && *buffer != UMP3410) {
777                 dev_err (dev, "%s - invalid buffer signature\n", __FUNCTION__);
778                 status = -ENODEV;
779                 goto ExitTiValidateI2cImage;
780         }
781
782         do {
783                 // Validate the I2C
784                 status = TIReadRom (serial,
785                                 start_address,
786                                 sizeof(struct ti_i2c_desc),
787                                 (__u8 *)rom_desc);
788                 if (status)
789                         break;
790
791                 if ((start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size) > TI_MAX_I2C_SIZE) {
792                         status = -ENODEV;
793                         dbg ("%s - structure too big, erroring out.", __FUNCTION__);
794                         break;
795                 }
796
797                 dbg ("%s Type = 0x%x", __FUNCTION__, rom_desc->Type);
798
799                 // Skip type 2 record
800                 ttype = rom_desc->Type & 0x0f;
801                 if ( ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
802                         && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO ) {
803                         // Read the descriptor data
804                         status = TIReadRom(serial,
805                                                 start_address+sizeof(struct ti_i2c_desc),
806                                                 rom_desc->Size,
807                                                 buffer);
808                         if (status)
809                                 break;
810
811                         status = ValidChecksum(rom_desc, buffer);
812                         if (status)
813                                 break;
814                 }
815                 start_address = start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size;
816
817         } while ((rom_desc->Type != I2C_DESC_TYPE_ION) && (start_address < TI_MAX_I2C_SIZE));
818
819         if ((rom_desc->Type != I2C_DESC_TYPE_ION) || (start_address > TI_MAX_I2C_SIZE))
820                 status = -ENODEV;
821
822 ExitTiValidateI2cImage: 
823         kfree (buffer);
824         kfree (rom_desc);
825         return status;
826 }
827
828 static int TIReadManufDescriptor (struct edgeport_serial *serial, __u8 *buffer)
829 {
830         int status;
831         int start_address;
832         struct ti_i2c_desc *rom_desc;
833         struct edge_ti_manuf_descriptor *desc;
834
835         rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
836         if (!rom_desc) {
837                 dev_err (&serial->serial->dev->dev, "%s - out of memory\n", __FUNCTION__);
838                 return -ENOMEM;
839         }
840         start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_ION, rom_desc);
841
842         if (!start_address) {
843                 dbg ("%s - Edge Descriptor not found in I2C", __FUNCTION__);
844                 status = -ENODEV;
845                 goto exit;
846         }
847
848         // Read the descriptor data
849         status = TIReadRom (serial,
850                                 start_address+sizeof(struct ti_i2c_desc),
851                                 rom_desc->Size,
852                                 buffer);
853         if (status)
854                 goto exit;
855         
856         status = ValidChecksum(rom_desc, buffer);
857         
858         desc = (struct edge_ti_manuf_descriptor *)buffer;
859         dbg ( "%s - IonConfig      0x%x", __FUNCTION__, desc->IonConfig         );
860         dbg ( "%s - Version          %d", __FUNCTION__, desc->Version           );
861         dbg ( "%s - Cpu/Board      0x%x", __FUNCTION__, desc->CpuRev_BoardRev   );
862         dbg ( "%s - NumPorts         %d", __FUNCTION__, desc->NumPorts          );      
863         dbg ( "%s - NumVirtualPorts  %d", __FUNCTION__, desc->NumVirtualPorts   );      
864         dbg ( "%s - TotalPorts       %d", __FUNCTION__, desc->TotalPorts        );      
865
866 exit:
867         kfree (rom_desc);
868         return status;
869 }
870
871 /* Build firmware header used for firmware update */
872 static int BuildI2CFirmwareHeader (__u8 *header, struct device *dev)
873 {
874         __u8 *buffer;
875         int buffer_size;
876         int i;
877         __u8 cs = 0;
878         struct ti_i2c_desc *i2c_header;
879         struct ti_i2c_image_header *img_header;
880         struct ti_i2c_firmware_rec *firmware_rec;
881
882         // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
883         // This will force the UMP to come up in Boot Mode.  Then while in boot mode, the driver 
884         // will download the latest firmware (padded to 15.5k) into the UMP ram. 
885         // And finally when the device comes back up in download mode the driver will cause 
886         // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
887         // the record type from 0xf2 to 0x02.
888         
889         // Allocate a 15.5k buffer + 2 bytes for version number (Firmware Record)
890         buffer_size = (((1024 * 16) - 512 )+ sizeof(struct ti_i2c_firmware_rec));
891
892         buffer = kmalloc (buffer_size, GFP_KERNEL);
893         if (!buffer) {
894                 dev_err (dev, "%s - out of memory\n", __FUNCTION__);
895                 return -ENOMEM;
896         }
897         
898         // Set entire image of 0xffs
899         memset (buffer, 0xff, buffer_size);
900
901         // Copy version number into firmware record
902         firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
903
904         firmware_rec->Ver_Major = OperationalCodeImageVersion.MajorVersion;
905         firmware_rec->Ver_Minor = OperationalCodeImageVersion.MinorVersion;
906
907         // Pointer to fw_down memory image
908         img_header = (struct ti_i2c_image_header *)&PagableOperationalCodeImage[0];
909
910         memcpy (buffer + sizeof(struct ti_i2c_firmware_rec),
911                 &PagableOperationalCodeImage[sizeof(struct ti_i2c_image_header)],
912                 le16_to_cpu(img_header->Length));
913
914         for (i=0; i < buffer_size; i++) {
915                 cs = (__u8)(cs + buffer[i]);
916         }
917
918         kfree (buffer);
919
920         // Build new header
921         i2c_header =  (struct ti_i2c_desc *)header;
922         firmware_rec =  (struct ti_i2c_firmware_rec*)i2c_header->Data;
923         
924         i2c_header->Type        = I2C_DESC_TYPE_FIRMWARE_BLANK;
925         i2c_header->Size        = (__u16)buffer_size;
926         i2c_header->CheckSum    = cs;
927         firmware_rec->Ver_Major = OperationalCodeImageVersion.MajorVersion;
928         firmware_rec->Ver_Minor = OperationalCodeImageVersion.MinorVersion;
929
930         return 0;
931 }
932
933 /* Try to figure out what type of I2c we have */
934 static int TIGetI2cTypeInBootMode (struct edgeport_serial *serial)
935 {
936         int status;
937         __u8 data;
938                 
939         // Try to read type 2
940         status = TIReadVendorRequestSync (serial->serial->dev,
941                                         UMPC_MEMORY_READ,               // Request
942                                         DTK_ADDR_SPACE_I2C_TYPE_II,     // wValue (Address type)
943                                         0,                              // wIndex
944                                         &data,                          // TransferBuffer
945                                         0x01);                          // TransferBufferLength
946         if (status)
947                 dbg ("%s - read 2 status error = %d", __FUNCTION__, status);
948         else
949                 dbg ("%s - read 2 data = 0x%x", __FUNCTION__, data);
950         if ((!status) && (data == UMP5152 || data == UMP3410)) {
951                 dbg ("%s - ROM_TYPE_II", __FUNCTION__);
952                 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
953                 return 0;
954         }
955
956         // Try to read type 3
957         status = TIReadVendorRequestSync (serial->serial->dev,
958                                         UMPC_MEMORY_READ,               // Request
959                                         DTK_ADDR_SPACE_I2C_TYPE_III,    // wValue (Address type)
960                                         0,                              // wIndex
961                                         &data,                          // TransferBuffer
962                                         0x01);                          // TransferBufferLength
963         if (status)
964                 dbg ("%s - read 3 status error = %d", __FUNCTION__, status);
965         else
966                 dbg ("%s - read 2 data = 0x%x", __FUNCTION__, data);
967         if ((!status) && (data == UMP5152 || data == UMP3410)) {
968                 dbg ("%s - ROM_TYPE_III", __FUNCTION__);
969                 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
970                 return 0;
971         }
972
973         dbg ("%s - Unknown", __FUNCTION__);
974         serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
975         return -ENODEV;
976 }
977
978 static int TISendBulkTransferSync (struct usb_serial *serial, void *buffer, int length, int *num_sent)
979 {
980         int status;
981
982         status = usb_bulk_msg (serial->dev,
983                                 usb_sndbulkpipe(serial->dev,
984                                                 serial->port[0]->bulk_out_endpointAddress),
985                                 buffer,
986                                 length,
987                                 num_sent,
988                                 1000);
989         return status;
990 }
991
992 /* Download given firmware image to the device (IN BOOT MODE) */
993 static int TIDownloadCodeImage (struct edgeport_serial *serial, __u8 *image, int image_length)
994 {
995         int status = 0;
996         int pos;
997         int transfer;
998         int done;
999
1000         // Transfer firmware image
1001         for (pos = 0; pos < image_length; ) {
1002                 // Read the next buffer from file
1003                 transfer = image_length - pos;
1004                 if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
1005                         transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
1006
1007                 // Transfer data
1008                 status = TISendBulkTransferSync (serial->serial, &image[pos], transfer, &done);
1009                 if (status)
1010                         break;
1011                 // Advance buffer pointer
1012                 pos += done;
1013         }
1014
1015         return status;
1016 }
1017
1018 // FIXME!!!
1019 static int TIConfigureBootDevice (struct usb_device *dev)
1020 {
1021         return 0;
1022 }
1023
1024 /**
1025  * DownloadTIFirmware - Download run-time operating firmware to the TI5052
1026  * 
1027  * This routine downloads the main operating code into the TI5052, using the
1028  * boot code already burned into E2PROM or ROM.
1029  */
1030 static int TIDownloadFirmware (struct edgeport_serial *serial)
1031 {
1032         struct device *dev = &serial->serial->dev->dev;
1033         int status = 0;
1034         int start_address;
1035         struct edge_ti_manuf_descriptor *ti_manuf_desc;
1036         struct usb_interface_descriptor *interface;
1037         int download_cur_ver;
1038         int download_new_ver;
1039
1040         /* This routine is entered by both the BOOT mode and the Download mode
1041          * We can determine which code is running by the reading the config
1042          * descriptor and if we have only one bulk pipe it is in boot mode
1043          */
1044         serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
1045
1046         /* Default to type 2 i2c */
1047         serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1048
1049         status = TIChooseConfiguration (serial->serial->dev);
1050         if (status)
1051                 return status;
1052
1053         interface = &serial->serial->interface->cur_altsetting->desc;
1054         if (!interface) {
1055                 dev_err (dev, "%s - no interface set, error!\n", __FUNCTION__);
1056                 return -ENODEV;
1057         }
1058
1059         // Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
1060         // if we have more than one endpoint we are definitely in download mode
1061         if (interface->bNumEndpoints > 1)
1062                 serial->product_info.TiMode = TI_MODE_DOWNLOAD;
1063         else
1064                 // Otherwise we will remain in configuring mode
1065                 serial->product_info.TiMode = TI_MODE_CONFIGURING;
1066
1067         // Save Download Version Number
1068         OperationalCodeImageVersion.MajorVersion = PagableOperationalCodeImageVersion.MajorVersion;
1069         OperationalCodeImageVersion.MinorVersion = PagableOperationalCodeImageVersion.MinorVersion;
1070         OperationalCodeImageVersion.BuildNumber  = PagableOperationalCodeImageVersion.BuildNumber;
1071
1072         /********************************************************************/
1073         /* Download Mode */
1074         /********************************************************************/
1075         if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
1076                 struct ti_i2c_desc *rom_desc;
1077
1078                 dbg ("%s - <<<<<<<<<<<<<<<RUNNING IN DOWNLOAD MODE>>>>>>>>>>", __FUNCTION__);
1079
1080                 status = TiValidateI2cImage (serial);
1081                 if (status) {
1082                         dbg ("%s - <<<<<<<<<<<<<<<DOWNLOAD MODE -- BAD I2C >>>>>>>>>>",
1083                              __FUNCTION__);
1084                         return status;
1085                 }
1086                 
1087                 /* Validate Hardware version number
1088                  * Read Manufacturing Descriptor from TI Based Edgeport
1089                  */
1090                 ti_manuf_desc = kmalloc (sizeof (*ti_manuf_desc), GFP_KERNEL);
1091                 if (!ti_manuf_desc) {
1092                         dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
1093                         return -ENOMEM;
1094                 }
1095                 status = TIReadManufDescriptor (serial, (__u8 *)ti_manuf_desc);
1096                 if (status) {
1097                         kfree (ti_manuf_desc);
1098                         return status;
1099                 }
1100
1101                 // Check version number of ION descriptor
1102                 if (!ignore_cpu_rev && TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev) < 2) {
1103                         dbg ( "%s - Wrong CPU Rev %d (Must be 2)", __FUNCTION__, 
1104                              TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev));
1105                         kfree (ti_manuf_desc);
1106                         return -EINVAL;
1107                 }
1108
1109                 rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
1110                 if (!rom_desc) {
1111                         dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
1112                         kfree (ti_manuf_desc);
1113                         return -ENOMEM;
1114                 }
1115
1116                 // Search for type 2 record (firmware record)
1117                 if ((start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc)) != 0) {
1118                         struct ti_i2c_firmware_rec *firmware_version;
1119                         __u8 record;
1120
1121                         dbg ("%s - Found Type FIRMWARE (Type 2) record", __FUNCTION__);
1122
1123                         firmware_version = kmalloc (sizeof (*firmware_version), GFP_KERNEL);
1124                         if (!firmware_version) {
1125                                 dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
1126                                 kfree (rom_desc);
1127                                 kfree (ti_manuf_desc);
1128                                 return -ENOMEM;
1129                         }
1130
1131                         // Validate version number                              
1132                         // Read the descriptor data
1133                         status = TIReadRom (serial,
1134                                         start_address+sizeof(struct ti_i2c_desc),
1135                                         sizeof(struct ti_i2c_firmware_rec),
1136                                         (__u8 *)firmware_version);
1137                         if (status) {
1138                                 kfree (firmware_version);
1139                                 kfree (rom_desc);
1140                                 kfree (ti_manuf_desc);
1141                                 return status;
1142                         }
1143
1144                         // Check version number of download with current version in I2c
1145                         download_cur_ver = (firmware_version->Ver_Major << 8) + 
1146                                            (firmware_version->Ver_Minor);
1147                         download_new_ver = (OperationalCodeImageVersion.MajorVersion << 8) +
1148                                            (OperationalCodeImageVersion.MinorVersion);
1149
1150                         dbg ("%s - >>>Firmware Versions Device %d.%d  Driver %d.%d",
1151                              __FUNCTION__,
1152                              firmware_version->Ver_Major,
1153                              firmware_version->Ver_Minor,
1154                              OperationalCodeImageVersion.MajorVersion,
1155                              OperationalCodeImageVersion.MinorVersion);
1156
1157                         // Check if we have an old version in the I2C and update if necessary
1158                         if (download_cur_ver != download_new_ver) {
1159                                 dbg ("%s - Update I2C Download from %d.%d to %d.%d",
1160                                      __FUNCTION__,
1161                                      firmware_version->Ver_Major,
1162                                      firmware_version->Ver_Minor,
1163                                      OperationalCodeImageVersion.MajorVersion,
1164                                      OperationalCodeImageVersion.MinorVersion);
1165
1166                                 // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
1167                                 // This will force the UMP to come up in Boot Mode.  Then while in boot mode, the driver 
1168                                 // will download the latest firmware (padded to 15.5k) into the UMP ram. 
1169                                 // And finally when the device comes back up in download mode the driver will cause 
1170                                 // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
1171                                 // the record type from 0xf2 to 0x02.
1172
1173                                 record = I2C_DESC_TYPE_FIRMWARE_BLANK;
1174
1175                                 // Change the I2C Firmware record type to 0xf2 to trigger an update
1176                                 status = TIWriteRom (serial,
1177                                                         start_address,
1178                                                         sizeof(record),
1179                                                         &record);
1180                                 if (status) {
1181                                         kfree (firmware_version);
1182                                         kfree (rom_desc);
1183                                         kfree (ti_manuf_desc);
1184                                         return status;
1185                                 }
1186
1187                                 // verify the write -- must do this in order for write to 
1188                                 // complete before we do the hardware reset
1189                                 status = TIReadRom (serial,
1190                                                         start_address,
1191                                                         sizeof(record),
1192                                                         &record);
1193
1194                                 if (status) {
1195                                         kfree (firmware_version);
1196                                         kfree (rom_desc);
1197                                         kfree (ti_manuf_desc);
1198                                         return status;
1199                                 }
1200
1201                                 if (record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
1202                                         dev_err (dev, "%s - error resetting device\n", __FUNCTION__);
1203                                         kfree (firmware_version);
1204                                         kfree (rom_desc);
1205                                         kfree (ti_manuf_desc);
1206                                         return -ENODEV;
1207                                 }
1208
1209                                 dbg ("%s - HARDWARE RESET", __FUNCTION__);
1210
1211                                 // Reset UMP -- Back to BOOT MODE
1212                                 status = TISendVendorRequestSync (serial->serial->dev,
1213                                                                 UMPC_HARDWARE_RESET,    // Request
1214                                                                 0,                      // wValue
1215                                                                 0,                      // wIndex
1216                                                                 NULL,                   // TransferBuffer
1217                                                                 0);                     // TransferBufferLength
1218
1219                                 dbg ( "%s - HARDWARE RESET return %d", __FUNCTION__, status);
1220
1221                                 /* return an error on purpose. */
1222                                 kfree (firmware_version);
1223                                 kfree (rom_desc);
1224                                 kfree (ti_manuf_desc);
1225                                 return -ENODEV;
1226                         }
1227                         kfree (firmware_version);
1228                 }
1229                 // Search for type 0xF2 record (firmware blank record)
1230                 else if ((start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
1231                         #define HEADER_SIZE     (sizeof(struct ti_i2c_desc) + sizeof(struct ti_i2c_firmware_rec))
1232                         __u8 *header;
1233                         __u8 *vheader;
1234
1235                         header  = kmalloc (HEADER_SIZE, GFP_KERNEL);
1236                         if (!header) {
1237                                 dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
1238                                 kfree (rom_desc);
1239                                 kfree (ti_manuf_desc);
1240                                 return -ENOMEM;
1241                         }
1242                                 
1243                         vheader = kmalloc (HEADER_SIZE, GFP_KERNEL);
1244                         if (!vheader) {
1245                                 dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
1246                                 kfree (header);
1247                                 kfree (rom_desc);
1248                                 kfree (ti_manuf_desc);
1249                                 return -ENOMEM;
1250                         }
1251                         
1252                         dbg ("%s - Found Type BLANK FIRMWARE (Type F2) record", __FUNCTION__);
1253
1254                         // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
1255                         // This will force the UMP to come up in Boot Mode.  Then while in boot mode, the driver 
1256                         // will download the latest firmware (padded to 15.5k) into the UMP ram. 
1257                         // And finally when the device comes back up in download mode the driver will cause 
1258                         // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
1259                         // the record type from 0xf2 to 0x02.
1260                         status = BuildI2CFirmwareHeader(header, dev);
1261                         if (status) {
1262                                 kfree (vheader);
1263                                 kfree (header);
1264                                 kfree (rom_desc);
1265                                 kfree (ti_manuf_desc);
1266                                 return status;
1267                         }
1268
1269                         // Update I2C with type 0xf2 record with correct size and checksum
1270                         status = TIWriteRom (serial,
1271                                                 start_address,
1272                                                 HEADER_SIZE,
1273                                                 header);
1274                         if (status) {
1275                                 kfree (vheader);
1276                                 kfree (header);
1277                                 kfree (rom_desc);
1278                                 kfree (ti_manuf_desc);
1279                                 return status;
1280                         }
1281
1282                         // verify the write -- must do this in order for write to 
1283                         // complete before we do the hardware reset
1284                         status = TIReadRom (serial,
1285                                                 start_address,
1286                                                 HEADER_SIZE,
1287                                                 vheader);
1288
1289                         if (status) {
1290                                 dbg ("%s - can't read header back", __FUNCTION__);
1291                                 kfree (vheader);
1292                                 kfree (header);
1293                                 kfree (rom_desc);
1294                                 kfree (ti_manuf_desc);
1295                                 return status;
1296                         }
1297                         if (memcmp(vheader, header, HEADER_SIZE)) {
1298                                 dbg ("%s - write download record failed", __FUNCTION__);
1299                                 kfree (vheader);
1300                                 kfree (header);
1301                                 kfree (rom_desc);
1302                                 kfree (ti_manuf_desc);
1303                                 return status;
1304                         }
1305
1306                         kfree (vheader);
1307                         kfree (header);
1308
1309                         dbg ("%s - Start firmware update", __FUNCTION__);
1310
1311                         // Tell firmware to copy download image into I2C 
1312                         status = TISendVendorRequestSync (serial->serial->dev,
1313                                                 UMPC_COPY_DNLD_TO_I2C,  // Request
1314                                                 0,                      // wValue 
1315                                                 0,                      // wIndex
1316                                                 NULL,                   // TransferBuffer
1317                                                 0);                     // TransferBufferLength
1318
1319                         dbg ("%s - Update complete 0x%x", __FUNCTION__, status);
1320                         if (status) {
1321                                 dev_err (dev, "%s - UMPC_COPY_DNLD_TO_I2C failed\n", __FUNCTION__);
1322                                 kfree (rom_desc);
1323                                 kfree (ti_manuf_desc);
1324                                 return status;
1325                         }
1326                 }
1327
1328                 // The device is running the download code
1329                 kfree (rom_desc);
1330                 kfree (ti_manuf_desc);
1331                 return 0;
1332         }
1333
1334         /********************************************************************/
1335         /* Boot Mode */
1336         /********************************************************************/
1337         dbg ("%s - <<<<<<<<<<<<<<<RUNNING IN BOOT MODE>>>>>>>>>>>>>>>",
1338              __FUNCTION__);
1339
1340         // Configure the TI device so we can use the BULK pipes for download
1341         status = TIConfigureBootDevice (serial->serial->dev);
1342         if (status)
1343                 return status;
1344
1345         if (le16_to_cpu(serial->serial->dev->descriptor.idVendor) != USB_VENDOR_ID_ION) {
1346                 dbg ("%s - VID = 0x%x", __FUNCTION__,
1347                      le16_to_cpu(serial->serial->dev->descriptor.idVendor));
1348                 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1349                 goto StayInBootMode;
1350         }
1351
1352         // We have an ION device (I2c Must be programmed)
1353         // Determine I2C image type
1354         if (TIGetI2cTypeInBootMode(serial)) {
1355                 goto StayInBootMode;
1356         }
1357
1358         // Registry variable set?
1359         if (TIStayInBootMode) {
1360                 dbg ("%s - TIStayInBootMode", __FUNCTION__);
1361                 goto StayInBootMode;
1362         }
1363
1364         // Check for ION Vendor ID and that the I2C is valid
1365         if (!TiValidateI2cImage(serial)) {
1366                 struct ti_i2c_image_header *header;
1367                 int i;
1368                 __u8 cs = 0;
1369                 __u8 *buffer;
1370                 int buffer_size;
1371
1372                 /* Validate Hardware version number
1373                  * Read Manufacturing Descriptor from TI Based Edgeport
1374                  */
1375                 ti_manuf_desc = kmalloc (sizeof (*ti_manuf_desc), GFP_KERNEL);
1376                 if (!ti_manuf_desc) {
1377                         dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
1378                         return -ENOMEM;
1379                 }
1380                 status = TIReadManufDescriptor (serial, (__u8 *)ti_manuf_desc);
1381                 if (status) {
1382                         kfree (ti_manuf_desc);
1383                         goto StayInBootMode;
1384                 }
1385
1386                 // Check for version 2
1387                 if (!ignore_cpu_rev && TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev) < 2) {
1388                         dbg ("%s - Wrong CPU Rev %d (Must be 2)", __FUNCTION__,
1389                              TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev));
1390                         kfree (ti_manuf_desc);
1391                         goto StayInBootMode;
1392                 }
1393
1394                 kfree (ti_manuf_desc);
1395
1396                 // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
1397                 // This will force the UMP to come up in Boot Mode.  Then while in boot mode, the driver 
1398                 // will download the latest firmware (padded to 15.5k) into the UMP ram. 
1399                 // And finally when the device comes back up in download mode the driver will cause 
1400                 // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
1401                 // the record type from 0xf2 to 0x02.
1402                 
1403                 /*
1404                  * Do we really have to copy the whole firmware image,
1405                  * or could we do this in place!
1406                  */
1407
1408                 // Allocate a 15.5k buffer + 3 byte header
1409                 buffer_size = (((1024 * 16) - 512) + sizeof(struct ti_i2c_image_header));
1410                 buffer = kmalloc (buffer_size, GFP_KERNEL);
1411                 if (!buffer) {
1412                         dev_err (dev, "%s - out of memory\n", __FUNCTION__);
1413                         return -ENOMEM;
1414                 }
1415                 
1416                 // Initialize the buffer to 0xff (pad the buffer)
1417                 memset (buffer, 0xff, buffer_size);
1418
1419                 memcpy (buffer, &PagableOperationalCodeImage[0], PagableOperationalCodeSize);
1420
1421                 for(i = sizeof(struct ti_i2c_image_header); i < buffer_size; i++) {
1422                         cs = (__u8)(cs + buffer[i]);
1423                 }
1424                 
1425                 header = (struct ti_i2c_image_header *)buffer;
1426                 
1427                 // update length and checksum after padding
1428                 header->Length   = cpu_to_le16((__u16)(buffer_size - sizeof(struct ti_i2c_image_header)));
1429                 header->CheckSum = cs;
1430
1431                 // Download the operational code 
1432                 dbg ("%s - Downloading operational code image (TI UMP)", __FUNCTION__);
1433                 status = TIDownloadCodeImage (serial, buffer, buffer_size);
1434
1435                 kfree (buffer);
1436
1437                 if (status) {
1438                         dbg ("%s - Error downloading operational code image", __FUNCTION__);
1439                         return status;
1440                 }
1441
1442                 // Device will reboot
1443                 serial->product_info.TiMode = TI_MODE_TRANSITIONING;
1444
1445                 dbg ("%s - Download successful -- Device rebooting...", __FUNCTION__);
1446
1447                 /* return an error on purpose */
1448                 return -ENODEV;
1449         }
1450
1451 StayInBootMode:
1452         // Eprom is invalid or blank stay in boot mode
1453         dbg ("%s - <<<<<<<<<<<<<<<STAYING IN BOOT MODE>>>>>>>>>>>>", __FUNCTION__);
1454         serial->product_info.TiMode = TI_MODE_BOOT;
1455
1456         return 0;
1457 }
1458
1459
1460 static int TISetDtr (struct edgeport_port *port)
1461 {
1462         int port_number = port->port->number - port->port->serial->minor;
1463
1464         dbg ("%s", __FUNCTION__);
1465         port->shadow_mcr |= MCR_DTR;
1466
1467         return TIWriteCommandSync (port->port->serial->dev,
1468                                 UMPC_SET_CLR_DTR,
1469                                 (__u8)(UMPM_UART1_PORT + port_number),
1470                                 1,      /* set */
1471                                 NULL,
1472                                 0);
1473 }
1474
1475 static int TIClearDtr (struct edgeport_port *port)
1476 {
1477         int port_number = port->port->number - port->port->serial->minor;
1478
1479         dbg ("%s", __FUNCTION__);
1480         port->shadow_mcr &= ~MCR_DTR;
1481
1482         return TIWriteCommandSync (port->port->serial->dev,
1483                                 UMPC_SET_CLR_DTR,
1484                                 (__u8)(UMPM_UART1_PORT + port_number),
1485                                 0,      /* clear */
1486                                 NULL,
1487                                 0);
1488 }
1489
1490 static int TISetRts (struct edgeport_port *port)
1491 {
1492         int port_number = port->port->number - port->port->serial->minor;
1493
1494         dbg ("%s", __FUNCTION__);
1495         port->shadow_mcr |= MCR_RTS;
1496
1497         return TIWriteCommandSync (port->port->serial->dev,
1498                                 UMPC_SET_CLR_RTS,
1499                                 (__u8)(UMPM_UART1_PORT + port_number),
1500                                 1,      /* set */
1501                                 NULL,
1502                                 0);
1503 }
1504
1505 static int TIClearRts (struct edgeport_port *port)
1506 {
1507         int port_number = port->port->number - port->port->serial->minor;
1508
1509         dbg ("%s", __FUNCTION__);
1510         port->shadow_mcr &= ~MCR_RTS;
1511
1512         return TIWriteCommandSync (port->port->serial->dev,
1513                                 UMPC_SET_CLR_RTS,
1514                                 (__u8)(UMPM_UART1_PORT + port_number),
1515                                 0,      /* clear */
1516                                 NULL,
1517                                 0);
1518 }
1519
1520 static int TISetLoopBack (struct edgeport_port *port)
1521 {
1522         int port_number = port->port->number - port->port->serial->minor;
1523
1524         dbg ("%s", __FUNCTION__);
1525
1526         return TIWriteCommandSync (port->port->serial->dev,
1527                                 UMPC_SET_CLR_LOOPBACK,
1528                                 (__u8)(UMPM_UART1_PORT + port_number),
1529                                 1,      /* set */
1530                                 NULL,
1531                                 0);
1532 }
1533
1534 static int TIClearLoopBack (struct edgeport_port *port)
1535 {
1536         int port_number = port->port->number - port->port->serial->minor;
1537
1538         dbg ("%s", __FUNCTION__);
1539
1540         return TIWriteCommandSync (port->port->serial->dev,
1541                                 UMPC_SET_CLR_LOOPBACK,
1542                                 (__u8)(UMPM_UART1_PORT + port_number),
1543                                 0,      /* clear */
1544                                 NULL,
1545                                 0);
1546 }
1547
1548 static int TISetBreak (struct edgeport_port *port)
1549 {
1550         int port_number = port->port->number - port->port->serial->minor;
1551
1552         dbg ("%s", __FUNCTION__);
1553
1554         return TIWriteCommandSync (port->port->serial->dev,
1555                                 UMPC_SET_CLR_BREAK,
1556                                 (__u8)(UMPM_UART1_PORT + port_number),
1557                                 1,      /* set */
1558                                 NULL,
1559                                 0);
1560 }
1561
1562 static int TIClearBreak (struct edgeport_port *port)
1563 {
1564         int port_number = port->port->number - port->port->serial->minor;
1565
1566         dbg ("%s", __FUNCTION__);
1567
1568         return TIWriteCommandSync (port->port->serial->dev,
1569                                 UMPC_SET_CLR_BREAK,
1570                                 (__u8)(UMPM_UART1_PORT + port_number),
1571                                 0,      /* clear */
1572                                 NULL,
1573                                 0);
1574 }
1575
1576 static int TIRestoreMCR (struct edgeport_port *port, __u8 mcr)
1577 {
1578         int status = 0;
1579
1580         dbg ("%s - %x", __FUNCTION__, mcr);
1581
1582         if (mcr & MCR_DTR)
1583                 status = TISetDtr (port);
1584         else
1585                 status = TIClearDtr (port);
1586
1587         if (status)
1588                 return status;
1589
1590         if (mcr & MCR_RTS)
1591                 status = TISetRts (port);
1592         else
1593                 status = TIClearRts (port);
1594
1595         if (status)
1596                 return status;
1597
1598         if (mcr & MCR_LOOPBACK)
1599                 status = TISetLoopBack (port);
1600         else
1601                 status = TIClearLoopBack (port);
1602
1603         return status;
1604 }
1605
1606
1607
1608 /* Convert TI LSR to standard UART flags */
1609 static __u8 MapLineStatus (__u8 ti_lsr)
1610 {
1611         __u8 lsr = 0;
1612
1613 #define MAP_FLAG(flagUmp, flagUart)    \
1614         if (ti_lsr & flagUmp) \
1615                 lsr |= flagUart;
1616
1617         MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR)    /* overrun */
1618         MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR)     /* parity error */
1619         MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR)     /* framing error */
1620         MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK)       /* break detected */
1621         MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL)    /* receive data available */
1622         MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY)    /* transmit holding register empty */
1623
1624 #undef MAP_FLAG
1625
1626         return lsr;
1627 }
1628
1629 static void handle_new_msr (struct edgeport_port *edge_port, __u8 msr)
1630 {
1631         struct async_icount *icount;
1632         struct tty_struct *tty;
1633
1634         dbg ("%s - %02x", __FUNCTION__, msr);
1635
1636         if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR | EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
1637                 icount = &edge_port->icount;
1638
1639                 /* update input line counters */
1640                 if (msr & EDGEPORT_MSR_DELTA_CTS)
1641                         icount->cts++;
1642                 if (msr & EDGEPORT_MSR_DELTA_DSR)
1643                         icount->dsr++;
1644                 if (msr & EDGEPORT_MSR_DELTA_CD)
1645                         icount->dcd++;
1646                 if (msr & EDGEPORT_MSR_DELTA_RI)
1647                         icount->rng++;
1648                 wake_up_interruptible (&edge_port->delta_msr_wait);
1649         }
1650
1651         /* Save the new modem status */
1652         edge_port->shadow_msr = msr & 0xf0;
1653
1654         tty = edge_port->port->tty;
1655         /* handle CTS flow control */
1656         if (tty && C_CRTSCTS(tty)) {
1657                 if (msr & EDGEPORT_MSR_CTS) {
1658                         tty->hw_stopped = 0;
1659                         tty_wakeup(tty);
1660                 } else {
1661                         tty->hw_stopped = 1;
1662                 }
1663         }
1664
1665         return;
1666 }
1667
1668 static void handle_new_lsr (struct edgeport_port *edge_port, int lsr_data, __u8 lsr, __u8 data)
1669 {
1670         struct async_icount *icount;
1671         __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
1672
1673         dbg ("%s - %02x", __FUNCTION__, new_lsr);
1674
1675         edge_port->shadow_lsr = lsr;
1676
1677         if (new_lsr & LSR_BREAK) {
1678                 /*
1679                  * Parity and Framing errors only count if they
1680                  * occur exclusive of a break being received.
1681                  */
1682                 new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
1683         }
1684
1685         /* Place LSR data byte into Rx buffer */
1686         if (lsr_data && edge_port->port->tty)
1687                 edge_tty_recv(&edge_port->port->dev, edge_port->port->tty, &data, 1);
1688
1689         /* update input line counters */
1690         icount = &edge_port->icount;
1691         if (new_lsr & LSR_BREAK)
1692                 icount->brk++;
1693         if (new_lsr & LSR_OVER_ERR)
1694                 icount->overrun++;
1695         if (new_lsr & LSR_PAR_ERR)
1696                 icount->parity++;
1697         if (new_lsr & LSR_FRM_ERR)
1698                 icount->frame++;
1699 }
1700
1701
1702 static void edge_interrupt_callback (struct urb *urb, struct pt_regs *regs)
1703 {
1704         struct edgeport_serial *edge_serial = (struct edgeport_serial *)urb->context;
1705         struct usb_serial_port *port;
1706         struct edgeport_port *edge_port;
1707         unsigned char *data = urb->transfer_buffer;
1708         int length = urb->actual_length;
1709         int port_number;
1710         int function;
1711         int status;
1712         __u8 lsr;
1713         __u8 msr;
1714
1715         dbg("%s", __FUNCTION__);
1716
1717         switch (urb->status) {
1718         case 0:
1719                 /* success */
1720                 break;
1721         case -ECONNRESET:
1722         case -ENOENT:
1723         case -ESHUTDOWN:
1724                 /* this urb is terminated, clean up */
1725                 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
1726                 return;
1727         default:
1728                 dev_err(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __FUNCTION__, urb->status);
1729                 goto exit;
1730         }
1731
1732         if (!length) {
1733                 dbg ("%s - no data in urb", __FUNCTION__);
1734                 goto exit;
1735         }
1736                 
1737         usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __FUNCTION__, length, data);
1738                 
1739         if (length != 2) {
1740                 dbg ("%s - expecting packet of size 2, got %d", __FUNCTION__, length);
1741                 goto exit;
1742         }
1743
1744         port_number = TIUMP_GET_PORT_FROM_CODE (data[0]);
1745         function    = TIUMP_GET_FUNC_FROM_CODE (data[0]);
1746         dbg ("%s - port_number %d, function %d, info 0x%x",
1747              __FUNCTION__, port_number, function, data[1]);
1748         port = edge_serial->serial->port[port_number];
1749         edge_port = usb_get_serial_port_data(port);
1750         if (!edge_port) {
1751                 dbg ("%s - edge_port not found", __FUNCTION__);
1752                 return;
1753         }
1754         switch (function) {
1755         case TIUMP_INTERRUPT_CODE_LSR:
1756                 lsr = MapLineStatus(data[1]);
1757                 if (lsr & UMP_UART_LSR_DATA_MASK) {
1758                         /* Save the LSR event for bulk read completion routine */
1759                         dbg ("%s - LSR Event Port %u LSR Status = %02x",
1760                              __FUNCTION__, port_number, lsr);
1761                         edge_port->lsr_event = 1;
1762                         edge_port->lsr_mask = lsr;
1763                 } else {
1764                         dbg ("%s - ===== Port %d LSR Status = %02x ======",
1765                              __FUNCTION__, port_number, lsr);
1766                         handle_new_lsr (edge_port, 0, lsr, 0);
1767                 }
1768                 break;
1769
1770         case TIUMP_INTERRUPT_CODE_MSR:  // MSR
1771                 /* Copy MSR from UMP */
1772                 msr = data[1];
1773                 dbg ("%s - ===== Port %u MSR Status = %02x ======\n",
1774                      __FUNCTION__, port_number, msr);
1775                 handle_new_msr (edge_port, msr);
1776                 break;
1777
1778         default:
1779                 dev_err (&urb->dev->dev, "%s - Unknown Interrupt code from UMP %x\n",
1780                          __FUNCTION__, data[1]);
1781                 break;
1782                 
1783         }
1784
1785 exit:
1786         status = usb_submit_urb (urb, GFP_ATOMIC);
1787         if (status)
1788                 dev_err (&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
1789                          __FUNCTION__, status);
1790 }
1791
1792 static void edge_bulk_in_callback (struct urb *urb, struct pt_regs *regs)
1793 {
1794         struct edgeport_port *edge_port = (struct edgeport_port *)urb->context;
1795         unsigned char *data = urb->transfer_buffer;
1796         struct tty_struct *tty;
1797         int status = 0;
1798         int port_number;
1799
1800         dbg("%s", __FUNCTION__);
1801
1802         switch (urb->status) {
1803         case 0:
1804                 /* success */
1805                 break;
1806         case -ECONNRESET:
1807         case -ENOENT:
1808         case -ESHUTDOWN:
1809                 /* this urb is terminated, clean up */
1810                 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
1811                 return;
1812         default:
1813                 dev_err (&urb->dev->dev,"%s - nonzero read bulk status received: %d\n",
1814                      __FUNCTION__, urb->status );
1815         }
1816
1817         if (urb->status == -EPIPE)
1818                 goto exit;
1819
1820         if (urb->status) {
1821                 dev_err(&urb->dev->dev,"%s - stopping read!\n", __FUNCTION__);
1822                 return;
1823         }
1824
1825         port_number = edge_port->port->number - edge_port->port->serial->minor;
1826
1827         if (edge_port->lsr_event) {
1828                 edge_port->lsr_event = 0;
1829                 dbg ("%s ===== Port %u LSR Status = %02x, Data = %02x ======",
1830                      __FUNCTION__, port_number, edge_port->lsr_mask, *data);
1831                 handle_new_lsr (edge_port, 1, edge_port->lsr_mask, *data);
1832                 /* Adjust buffer length/pointer */
1833                 --urb->actual_length;
1834                 ++data;
1835         }
1836
1837         tty = edge_port->port->tty;
1838         if (tty && urb->actual_length) {
1839                 usb_serial_debug_data(debug, &edge_port->port->dev, __FUNCTION__, urb->actual_length, data);
1840
1841                 if (edge_port->close_pending) {
1842                         dbg ("%s - close is pending, dropping data on the floor.", __FUNCTION__);
1843                 } else {
1844                         edge_tty_recv(&edge_port->port->dev, tty, data, urb->actual_length);
1845                 }
1846                 edge_port->icount.rx += urb->actual_length;
1847         }
1848
1849 exit:
1850         /* continue read unless stopped */
1851         spin_lock(&edge_port->ep_lock);
1852         if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING) {
1853                 urb->dev = edge_port->port->serial->dev;
1854                 status = usb_submit_urb(urb, GFP_ATOMIC);
1855         } else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING) {
1856                 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
1857         }
1858         spin_unlock(&edge_port->ep_lock);
1859         if (status)
1860                 dev_err (&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
1861                          __FUNCTION__, status);
1862 }
1863
1864 static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length)
1865 {
1866         int cnt;
1867
1868         do {
1869                 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
1870                         tty_flip_buffer_push(tty);
1871                         if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
1872                                 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1873                                         __FUNCTION__, length);
1874                                 return;
1875                         }
1876                 }
1877                 cnt = min(length, TTY_FLIPBUF_SIZE - tty->flip.count);
1878                 memcpy(tty->flip.char_buf_ptr, data, cnt);
1879                 memset(tty->flip.flag_buf_ptr, 0, cnt);
1880                 tty->flip.char_buf_ptr += cnt;
1881                 tty->flip.flag_buf_ptr += cnt;
1882                 tty->flip.count += cnt;
1883                 data += cnt;
1884                 length -= cnt;
1885         } while (length > 0);
1886
1887         tty_flip_buffer_push(tty);
1888 }
1889
1890 static void edge_bulk_out_callback (struct urb *urb, struct pt_regs *regs)
1891 {
1892         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1893         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1894
1895         dbg ("%s - port %d", __FUNCTION__, port->number);
1896
1897         edge_port->ep_write_urb_in_use = 0;
1898
1899         switch (urb->status) {
1900         case 0:
1901                 /* success */
1902                 break;
1903         case -ECONNRESET:
1904         case -ENOENT:
1905         case -ESHUTDOWN:
1906                 /* this urb is terminated, clean up */
1907                 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
1908                 return;
1909         default:
1910                 dev_err (&urb->dev->dev,"%s - nonzero write bulk status received: %d\n",
1911                      __FUNCTION__, urb->status);
1912         }
1913
1914         /* send any buffered data */
1915         edge_send(port);
1916 }
1917
1918 static int edge_open (struct usb_serial_port *port, struct file * filp)
1919 {
1920         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1921         struct edgeport_serial *edge_serial;
1922         struct usb_device *dev;
1923         struct urb *urb;
1924         int port_number;
1925         int status;
1926         u16 open_settings;
1927         u8 transaction_timeout;
1928
1929         dbg("%s - port %d", __FUNCTION__, port->number);
1930
1931         if (edge_port == NULL)
1932                 return -ENODEV;
1933
1934         if (port->tty)
1935                 port->tty->low_latency = low_latency;
1936
1937         port_number = port->number - port->serial->minor;
1938         switch (port_number) {
1939                 case 0:
1940                         edge_port->uart_base = UMPMEM_BASE_UART1;
1941                         edge_port->dma_address = UMPD_OEDB1_ADDRESS;
1942                         break;
1943                 case 1:
1944                         edge_port->uart_base = UMPMEM_BASE_UART2;
1945                         edge_port->dma_address = UMPD_OEDB2_ADDRESS;
1946                         break;
1947                 default:
1948                         dev_err (&port->dev, "Unknown port number!!!\n");
1949                         return -ENODEV;
1950         }
1951
1952         dbg ("%s - port_number = %d, uart_base = %04x, dma_address = %04x",
1953              __FUNCTION__, port_number, edge_port->uart_base, edge_port->dma_address);
1954
1955         dev = port->serial->dev;
1956
1957         memset (&(edge_port->icount), 0x00, sizeof(edge_port->icount));
1958         init_waitqueue_head (&edge_port->delta_msr_wait);
1959
1960         /* turn off loopback */
1961         status = TIClearLoopBack (edge_port);
1962         if (status) {
1963                 dev_err(&port->dev,"%s - cannot send clear loopback command, %d\n",
1964                         __FUNCTION__, status);
1965                 return status;
1966         }
1967         
1968         /* set up the port settings */
1969         edge_set_termios (port, NULL);
1970
1971         /* open up the port */
1972
1973         /* milliseconds to timeout for DMA transfer */
1974         transaction_timeout = 2;
1975
1976         edge_port->ump_read_timeout = max (20, ((transaction_timeout * 3) / 2) );
1977
1978         // milliseconds to timeout for DMA transfer
1979         open_settings = (u8)(UMP_DMA_MODE_CONTINOUS | 
1980                              UMP_PIPE_TRANS_TIMEOUT_ENA | 
1981                              (transaction_timeout << 2));
1982
1983         dbg ("%s - Sending UMPC_OPEN_PORT", __FUNCTION__);
1984
1985         /* Tell TI to open and start the port */
1986         status = TIWriteCommandSync (dev,
1987                                         UMPC_OPEN_PORT,
1988                                         (u8)(UMPM_UART1_PORT + port_number),
1989                                         open_settings,
1990                                         NULL,
1991                                         0);
1992         if (status) {
1993                 dev_err(&port->dev,"%s - cannot send open command, %d\n", __FUNCTION__, status);
1994                 return status;
1995         }
1996
1997         /* Start the DMA? */
1998         status = TIWriteCommandSync (dev,
1999                                         UMPC_START_PORT,
2000                                         (u8)(UMPM_UART1_PORT + port_number),
2001                                         0,
2002                                         NULL,
2003                                         0);
2004         if (status) {
2005                 dev_err(&port->dev,"%s - cannot send start DMA command, %d\n", __FUNCTION__, status);
2006                 return status;
2007         }
2008
2009         /* Clear TX and RX buffers in UMP */
2010         status = TIPurgeDataSync (port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
2011         if (status) {
2012                 dev_err(&port->dev,"%s - cannot send clear buffers command, %d\n", __FUNCTION__, status);
2013                 return status;
2014         }
2015
2016         /* Read Initial MSR */
2017         status = TIReadVendorRequestSync (dev,
2018                                         UMPC_READ_MSR,  // Request
2019                                         0,              // wValue
2020                                         (__u16)(UMPM_UART1_PORT + port_number), // wIndex (Address)
2021                                         &edge_port->shadow_msr,                 // TransferBuffer
2022                                         1);                                     // TransferBufferLength
2023         if (status) {
2024                 dev_err(&port->dev,"%s - cannot send read MSR command, %d\n", __FUNCTION__, status);
2025                 return status;
2026         }
2027
2028         dbg ("ShadowMSR 0x%X", edge_port->shadow_msr);
2029  
2030         /* Set Initial MCR */
2031         edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
2032         dbg ("ShadowMCR 0x%X", edge_port->shadow_mcr);
2033
2034         edge_serial = edge_port->edge_serial;
2035         if (down_interruptible(&edge_serial->es_sem))
2036                 return -ERESTARTSYS;
2037         if (edge_serial->num_ports_open == 0) {
2038                 /* we are the first port to be opened, let's post the interrupt urb */
2039                 urb = edge_serial->serial->port[0]->interrupt_in_urb;
2040                 if (!urb) {
2041                         dev_err (&port->dev, "%s - no interrupt urb present, exiting\n", __FUNCTION__);
2042                         status = -EINVAL;
2043                         goto up_es_sem;
2044                 }
2045                 urb->complete = edge_interrupt_callback;
2046                 urb->context = edge_serial;
2047                 urb->dev = dev;
2048                 status = usb_submit_urb (urb, GFP_KERNEL);
2049                 if (status) {
2050                         dev_err (&port->dev, "%s - usb_submit_urb failed with value %d\n", __FUNCTION__, status);
2051                         goto up_es_sem;
2052                 }
2053         }
2054
2055         /*
2056          * reset the data toggle on the bulk endpoints to work around bug in
2057          * host controllers where things get out of sync some times
2058          */
2059         usb_clear_halt (dev, port->write_urb->pipe);
2060         usb_clear_halt (dev, port->read_urb->pipe);
2061
2062         /* start up our bulk read urb */
2063         urb = port->read_urb;
2064         if (!urb) {
2065                 dev_err (&port->dev, "%s - no read urb present, exiting\n", __FUNCTION__);
2066                 status = -EINVAL;
2067                 goto unlink_int_urb;
2068         }
2069         edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2070         urb->complete = edge_bulk_in_callback;
2071         urb->context = edge_port;
2072         urb->dev = dev;
2073         status = usb_submit_urb (urb, GFP_KERNEL);
2074         if (status) {
2075                 dev_err (&port->dev, "%s - read bulk usb_submit_urb failed with value %d\n", __FUNCTION__, status);
2076                 goto unlink_int_urb;
2077         }
2078
2079         ++edge_serial->num_ports_open;
2080
2081         dbg("%s - exited", __FUNCTION__);
2082
2083         goto up_es_sem;
2084
2085 unlink_int_urb:
2086         if (edge_port->edge_serial->num_ports_open == 0)
2087                 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2088 up_es_sem:
2089         up(&edge_serial->es_sem);
2090         return status;
2091 }
2092
2093 static void edge_close (struct usb_serial_port *port, struct file *filp)
2094 {
2095         struct edgeport_serial *edge_serial;
2096         struct edgeport_port *edge_port;
2097         int port_number;
2098         int status;
2099
2100         dbg("%s - port %d", __FUNCTION__, port->number);
2101                          
2102         edge_serial = usb_get_serial_data(port->serial);
2103         edge_port = usb_get_serial_port_data(port);
2104         if ((edge_serial == NULL) || (edge_port == NULL))
2105                 return;
2106         
2107         /* The bulkreadcompletion routine will check 
2108          * this flag and dump add read data */
2109         edge_port->close_pending = 1;
2110
2111         /* chase the port close and flush */
2112         TIChasePort (edge_port, (HZ*closing_wait)/100, 1);
2113
2114         usb_kill_urb(port->read_urb);
2115         usb_kill_urb(port->write_urb);
2116         edge_port->ep_write_urb_in_use = 0;
2117
2118         /* assuming we can still talk to the device,
2119          * send a close port command to it */
2120         dbg("%s - send umpc_close_port", __FUNCTION__);
2121         port_number = port->number - port->serial->minor;
2122         status = TIWriteCommandSync (port->serial->dev,
2123                                      UMPC_CLOSE_PORT,
2124                                      (__u8)(UMPM_UART1_PORT + port_number),
2125                                      0,
2126                                      NULL,
2127                                      0);
2128         down(&edge_serial->es_sem);
2129         --edge_port->edge_serial->num_ports_open;
2130         if (edge_port->edge_serial->num_ports_open <= 0) {
2131                 /* last port is now closed, let's shut down our interrupt urb */
2132                 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2133                 edge_port->edge_serial->num_ports_open = 0;
2134         }
2135         up(&edge_serial->es_sem);
2136         edge_port->close_pending = 0;
2137
2138         dbg("%s - exited", __FUNCTION__);
2139 }
2140
2141 static int edge_write (struct usb_serial_port *port, const unsigned char *data, int count)
2142 {
2143         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2144         unsigned long flags;
2145
2146         dbg("%s - port %d", __FUNCTION__, port->number);
2147
2148         if (count == 0) {
2149                 dbg("%s - write request of 0 bytes", __FUNCTION__);
2150                 return 0;
2151         }
2152
2153         if (edge_port == NULL)
2154                 return -ENODEV;
2155         if (edge_port->close_pending == 1)
2156                 return -ENODEV;
2157
2158         spin_lock_irqsave(&edge_port->ep_lock, flags);
2159         count = edge_buf_put(edge_port->ep_out_buf, data, count);
2160         spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2161
2162         edge_send(port);
2163
2164         return count;
2165 }
2166
2167 static void edge_send(struct usb_serial_port *port)
2168 {
2169         int count, result;
2170         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2171         struct tty_struct *tty = port->tty;
2172         unsigned long flags;
2173
2174
2175         dbg("%s - port %d", __FUNCTION__, port->number);
2176
2177         spin_lock_irqsave(&edge_port->ep_lock, flags);
2178
2179         if (edge_port->ep_write_urb_in_use) {
2180                 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2181                 return;
2182         }
2183
2184         count = edge_buf_get(edge_port->ep_out_buf,
2185                                 port->write_urb->transfer_buffer,
2186                                 port->bulk_out_size);
2187
2188         if (count == 0) {
2189                 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2190                 return;
2191         }
2192
2193         edge_port->ep_write_urb_in_use = 1;
2194
2195         spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2196
2197         usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, port->write_urb->transfer_buffer);
2198
2199         /* set up our urb */
2200         usb_fill_bulk_urb (port->write_urb, port->serial->dev,
2201                            usb_sndbulkpipe (port->serial->dev,
2202                                             port->bulk_out_endpointAddress),
2203                            port->write_urb->transfer_buffer, count,
2204                            edge_bulk_out_callback,
2205                            port);
2206
2207         /* send the data out the bulk port */
2208         result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2209         if (result) {
2210                 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
2211                 edge_port->ep_write_urb_in_use = 0;
2212                 // TODO: reschedule edge_send
2213         } else {
2214                 edge_port->icount.tx += count;
2215         }
2216
2217         /* wakeup any process waiting for writes to complete */
2218         /* there is now more room in the buffer for new writes */
2219         if (tty) {
2220                 /* let the tty driver wakeup if it has a special write_wakeup function */
2221                 tty_wakeup(tty);
2222         }
2223 }
2224
2225 static int edge_write_room (struct usb_serial_port *port)
2226 {
2227         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2228         int room = 0;
2229         unsigned long flags;
2230
2231         dbg("%s - port %d", __FUNCTION__, port->number);
2232
2233         if (edge_port == NULL)
2234                 return -ENODEV;
2235         if (edge_port->close_pending == 1)
2236                 return -ENODEV;
2237
2238         spin_lock_irqsave(&edge_port->ep_lock, flags);
2239         room = edge_buf_space_avail(edge_port->ep_out_buf);
2240         spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2241
2242         dbg("%s - returns %d", __FUNCTION__, room);
2243         return room;
2244 }
2245
2246 static int edge_chars_in_buffer (struct usb_serial_port *port)
2247 {
2248         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2249         int chars = 0;
2250         unsigned long flags;
2251
2252         dbg("%s - port %d", __FUNCTION__, port->number);
2253
2254         if (edge_port == NULL)
2255                 return -ENODEV;
2256         if (edge_port->close_pending == 1)
2257                 return -ENODEV;
2258
2259         spin_lock_irqsave(&edge_port->ep_lock, flags);
2260         chars = edge_buf_data_avail(edge_port->ep_out_buf);
2261         spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2262
2263         dbg ("%s - returns %d", __FUNCTION__, chars);
2264         return chars;
2265 }
2266
2267 static void edge_throttle (struct usb_serial_port *port)
2268 {
2269         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2270         struct tty_struct *tty;
2271         int status;
2272
2273         dbg("%s - port %d", __FUNCTION__, port->number);
2274
2275         if (edge_port == NULL)
2276                 return;
2277
2278         tty = port->tty;
2279         if (!tty) {
2280                 dbg ("%s - no tty available", __FUNCTION__);
2281                 return;
2282         }
2283
2284         /* if we are implementing XON/XOFF, send the stop character */
2285         if (I_IXOFF(tty)) {
2286                 unsigned char stop_char = STOP_CHAR(tty);
2287                 status = edge_write (port, &stop_char, 1);
2288                 if (status <= 0) {
2289                         dev_err(&port->dev, "%s - failed to write stop character, %d\n", __FUNCTION__, status);
2290                 }
2291         }
2292
2293         /* if we are implementing RTS/CTS, stop reads */
2294         /* and the Edgeport will clear the RTS line */
2295         if (C_CRTSCTS(tty))
2296                 stop_read(edge_port);
2297
2298 }
2299
2300 static void edge_unthrottle (struct usb_serial_port *port)
2301 {
2302         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2303         struct tty_struct *tty;
2304         int status;
2305
2306         dbg("%s - port %d", __FUNCTION__, port->number);
2307
2308         if (edge_port == NULL)
2309                 return;
2310
2311         tty = port->tty;
2312         if (!tty) {
2313                 dbg ("%s - no tty available", __FUNCTION__);
2314                 return;
2315         }
2316
2317         /* if we are implementing XON/XOFF, send the start character */
2318         if (I_IXOFF(tty)) {
2319                 unsigned char start_char = START_CHAR(tty);
2320                 status = edge_write (port, &start_char, 1);
2321                 if (status <= 0) {
2322                         dev_err(&port->dev, "%s - failed to write start character, %d\n", __FUNCTION__, status);
2323                 }
2324         }
2325
2326         /* if we are implementing RTS/CTS, restart reads */
2327         /* are the Edgeport will assert the RTS line */
2328         if (C_CRTSCTS(tty)) {
2329                 status = restart_read(edge_port);
2330                 if (status)
2331                         dev_err(&port->dev, "%s - read bulk usb_submit_urb failed with value %d\n", __FUNCTION__, status);
2332         }
2333
2334 }
2335
2336 static void stop_read(struct edgeport_port *edge_port)
2337 {
2338         unsigned long flags;
2339
2340         spin_lock_irqsave(&edge_port->ep_lock, flags);
2341
2342         if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
2343                 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
2344         edge_port->shadow_mcr &= ~MCR_RTS;
2345
2346         spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2347 }
2348
2349 static int restart_read(struct edgeport_port *edge_port)
2350 {
2351         struct urb *urb;
2352         int status = 0;
2353         unsigned long flags;
2354
2355         spin_lock_irqsave(&edge_port->ep_lock, flags);
2356
2357         if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
2358                 urb = edge_port->port->read_urb;
2359                 urb->complete = edge_bulk_in_callback;
2360                 urb->context = edge_port;
2361                 urb->dev = edge_port->port->serial->dev;
2362                 status = usb_submit_urb(urb, GFP_KERNEL);
2363         }
2364         edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2365         edge_port->shadow_mcr |= MCR_RTS;
2366
2367         spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2368
2369         return status;
2370 }
2371
2372 static void change_port_settings (struct edgeport_port *edge_port, struct termios *old_termios)
2373 {
2374         struct ump_uart_config *config;
2375         struct tty_struct *tty;
2376         int baud;
2377         unsigned cflag;
2378         int status;
2379         int port_number = edge_port->port->number - edge_port->port->serial->minor;
2380
2381         dbg("%s - port %d", __FUNCTION__, edge_port->port->number);
2382
2383         tty = edge_port->port->tty;
2384         if ((!tty) ||
2385             (!tty->termios)) {
2386                 dbg("%s - no tty structures", __FUNCTION__);
2387                 return;
2388         }
2389
2390         config = kmalloc (sizeof (*config), GFP_KERNEL);
2391         if (!config) {
2392                 dev_err (&edge_port->port->dev, "%s - out of memory\n", __FUNCTION__);
2393                 return;
2394         }
2395
2396         cflag = tty->termios->c_cflag;
2397
2398         config->wFlags = 0;
2399
2400         /* These flags must be set */
2401         config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
2402         config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
2403         config->bUartMode = (__u8)(edge_port->bUartMode);
2404
2405         switch (cflag & CSIZE) {
2406                 case CS5:
2407                             config->bDataBits = UMP_UART_CHAR5BITS;
2408                             dbg ("%s - data bits = 5", __FUNCTION__);
2409                             break;
2410                 case CS6:
2411                             config->bDataBits = UMP_UART_CHAR6BITS;
2412                             dbg ("%s - data bits = 6", __FUNCTION__);
2413                             break;
2414                 case CS7:
2415                             config->bDataBits = UMP_UART_CHAR7BITS;
2416                             dbg ("%s - data bits = 7", __FUNCTION__);
2417                             break;
2418                 default:
2419                 case CS8:
2420                             config->bDataBits = UMP_UART_CHAR8BITS;
2421                             dbg ("%s - data bits = 8", __FUNCTION__);
2422                             break;
2423         }
2424
2425         if (cflag & PARENB) {
2426                 if (cflag & PARODD) {
2427                         config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2428                         config->bParity = UMP_UART_ODDPARITY;
2429                         dbg("%s - parity = odd", __FUNCTION__);
2430                 } else {
2431                         config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2432                         config->bParity = UMP_UART_EVENPARITY;
2433                         dbg("%s - parity = even", __FUNCTION__);
2434                 }
2435         } else {
2436                 config->bParity = UMP_UART_NOPARITY;    
2437                 dbg("%s - parity = none", __FUNCTION__);
2438         }
2439
2440         if (cflag & CSTOPB) {
2441                 config->bStopBits = UMP_UART_STOPBIT2;
2442                 dbg("%s - stop bits = 2", __FUNCTION__);
2443         } else {
2444                 config->bStopBits = UMP_UART_STOPBIT1;
2445                 dbg("%s - stop bits = 1", __FUNCTION__);
2446         }
2447
2448         /* figure out the flow control settings */
2449         if (cflag & CRTSCTS) {
2450                 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
2451                 config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
2452                 dbg("%s - RTS/CTS is enabled", __FUNCTION__);
2453         } else {
2454                 dbg("%s - RTS/CTS is disabled", __FUNCTION__);
2455                 tty->hw_stopped = 0;
2456                 restart_read(edge_port);
2457         }
2458
2459         /* if we are implementing XON/XOFF, set the start and stop character in the device */
2460         if (I_IXOFF(tty) || I_IXON(tty)) {
2461                 config->cXon  = START_CHAR(tty);
2462                 config->cXoff = STOP_CHAR(tty);
2463
2464                 /* if we are implementing INBOUND XON/XOFF */
2465                 if (I_IXOFF(tty)) {
2466                         config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
2467                         dbg ("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2468                              __FUNCTION__, config->cXon, config->cXoff);
2469                 } else {
2470                         dbg ("%s - INBOUND XON/XOFF is disabled", __FUNCTION__);
2471                 }
2472
2473                 /* if we are implementing OUTBOUND XON/XOFF */
2474                 if (I_IXON(tty)) {
2475                         config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
2476                         dbg ("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2477                              __FUNCTION__, config->cXon, config->cXoff);
2478                 } else {
2479                         dbg ("%s - OUTBOUND XON/XOFF is disabled", __FUNCTION__);
2480                 }
2481         }
2482
2483         /* Round the baud rate */
2484         baud = tty_get_baud_rate(tty);
2485         if (!baud) {
2486                 /* pick a default, any default... */
2487                 baud = 9600;
2488         }
2489         edge_port->baud_rate = baud;
2490         config->wBaudRate = (__u16)((461550L + baud/2) / baud);
2491
2492         dbg ("%s - baud rate = %d, wBaudRate = %d", __FUNCTION__, baud, config->wBaudRate);
2493
2494         dbg ("wBaudRate:   %d", (int)(461550L / config->wBaudRate));
2495         dbg ("wFlags:    0x%x", config->wFlags);
2496         dbg ("bDataBits:   %d", config->bDataBits);
2497         dbg ("bParity:     %d", config->bParity);
2498         dbg ("bStopBits:   %d", config->bStopBits);
2499         dbg ("cXon:        %d", config->cXon);
2500         dbg ("cXoff:       %d", config->cXoff);
2501         dbg ("bUartMode:   %d", config->bUartMode);
2502
2503         /* move the word values into big endian mode */
2504         cpu_to_be16s (&config->wFlags);
2505         cpu_to_be16s (&config->wBaudRate);
2506
2507         status = TIWriteCommandSync (edge_port->port->serial->dev,
2508                                 UMPC_SET_CONFIG,
2509                                 (__u8)(UMPM_UART1_PORT + port_number),
2510                                 0,
2511                                 (__u8 *)config,
2512                                 sizeof(*config));
2513         if (status) {
2514                 dbg ("%s - error %d when trying to write config to device",
2515                      __FUNCTION__, status);
2516         }
2517
2518         kfree (config);
2519         
2520         return;
2521 }
2522
2523 static void edge_set_termios (struct usb_serial_port *port, struct termios *old_termios)
2524 {
2525         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2526         struct tty_struct *tty = port->tty;
2527         unsigned int cflag;
2528
2529         if (!port->tty || !port->tty->termios) {
2530                 dbg ("%s - no tty or termios", __FUNCTION__);
2531                 return;
2532         }
2533
2534         cflag = tty->termios->c_cflag;
2535         /* check that they really want us to change something */
2536         if (old_termios) {
2537                 if (cflag == old_termios->c_cflag &&
2538                     tty->termios->c_iflag == old_termios->c_iflag) {
2539                         dbg ("%s - nothing to change", __FUNCTION__);
2540                         return;
2541                 }
2542         }
2543
2544         dbg("%s - clfag %08x iflag %08x", __FUNCTION__, 
2545             tty->termios->c_cflag, tty->termios->c_iflag);
2546         if (old_termios) {
2547                 dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
2548                     old_termios->c_cflag, old_termios->c_iflag);
2549         }
2550
2551         dbg("%s - port %d", __FUNCTION__, port->number);
2552
2553         if (edge_port == NULL)
2554                 return;
2555
2556         /* change the port settings to the new ones specified */
2557         change_port_settings (edge_port, old_termios);
2558
2559         return;
2560 }
2561
2562 static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear)
2563 {
2564         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2565         unsigned int mcr;
2566
2567         dbg("%s - port %d", __FUNCTION__, port->number);
2568
2569         mcr = edge_port->shadow_mcr;
2570         if (set & TIOCM_RTS)
2571                 mcr |= MCR_RTS;
2572         if (set & TIOCM_DTR)
2573                 mcr |= MCR_DTR;
2574         if (set & TIOCM_LOOP)
2575                 mcr |= MCR_LOOPBACK;
2576
2577         if (clear & TIOCM_RTS)
2578                 mcr &= ~MCR_RTS;
2579         if (clear & TIOCM_DTR)
2580                 mcr &= ~MCR_DTR;
2581         if (clear & TIOCM_LOOP)
2582                 mcr &= ~MCR_LOOPBACK;
2583
2584         edge_port->shadow_mcr = mcr;
2585
2586         TIRestoreMCR (edge_port, mcr);
2587
2588         return 0;
2589 }
2590
2591 static int edge_tiocmget(struct usb_serial_port *port, struct file *file)
2592 {
2593         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2594         unsigned int result = 0;
2595         unsigned int msr;
2596         unsigned int mcr;
2597
2598         dbg("%s - port %d", __FUNCTION__, port->number);
2599
2600         msr = edge_port->shadow_msr;
2601         mcr = edge_port->shadow_mcr;
2602         result = ((mcr & MCR_DTR)       ? TIOCM_DTR: 0)   /* 0x002 */
2603                   | ((mcr & MCR_RTS)    ? TIOCM_RTS: 0)   /* 0x004 */
2604                   | ((msr & EDGEPORT_MSR_CTS)   ? TIOCM_CTS: 0)   /* 0x020 */
2605                   | ((msr & EDGEPORT_MSR_CD)    ? TIOCM_CAR: 0)   /* 0x040 */
2606                   | ((msr & EDGEPORT_MSR_RI)    ? TIOCM_RI:  0)   /* 0x080 */
2607                   | ((msr & EDGEPORT_MSR_DSR)   ? TIOCM_DSR: 0);  /* 0x100 */
2608
2609
2610         dbg("%s -- %x", __FUNCTION__, result);
2611
2612         return result;
2613 }
2614
2615 static int get_serial_info (struct edgeport_port *edge_port, struct serial_struct __user *retinfo)
2616 {
2617         struct serial_struct tmp;
2618
2619         if (!retinfo)
2620                 return -EFAULT;
2621
2622         memset(&tmp, 0, sizeof(tmp));
2623
2624         tmp.type                = PORT_16550A;
2625         tmp.line                = edge_port->port->serial->minor;
2626         tmp.port                = edge_port->port->number;
2627         tmp.irq                 = 0;
2628         tmp.flags               = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2629         tmp.xmit_fifo_size      = edge_port->port->bulk_out_size;
2630         tmp.baud_base           = 9600;
2631         tmp.close_delay         = 5*HZ;
2632         tmp.closing_wait        = closing_wait;
2633 //      tmp.custom_divisor      = state->custom_divisor;
2634 //      tmp.hub6                = state->hub6;
2635 //      tmp.io_type             = state->io_type;
2636
2637
2638         if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2639                 return -EFAULT;
2640         return 0;
2641 }
2642
2643 static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
2644 {
2645         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2646         struct async_icount cnow;
2647         struct async_icount cprev;
2648
2649         dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
2650
2651         switch (cmd) {
2652                 case TIOCINQ:
2653                         dbg("%s - (%d) TIOCINQ", __FUNCTION__, port->number);
2654 //                      return get_number_bytes_avail(edge_port, (unsigned int *) arg);
2655                         break;
2656
2657                 case TIOCSERGETLSR:
2658                         dbg("%s - (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
2659 //                      return get_lsr_info(edge_port, (unsigned int *) arg);
2660                         break;
2661
2662                 case TIOCGSERIAL:
2663                         dbg("%s - (%d) TIOCGSERIAL", __FUNCTION__, port->number);
2664                         return get_serial_info(edge_port, (struct serial_struct __user *) arg);
2665                         break;
2666
2667                 case TIOCSSERIAL:
2668                         dbg("%s - (%d) TIOCSSERIAL", __FUNCTION__, port->number);
2669                         break;
2670
2671                 case TIOCMIWAIT:
2672                         dbg("%s - (%d) TIOCMIWAIT", __FUNCTION__, port->number);
2673                         cprev = edge_port->icount;
2674                         while (1) {
2675                                 interruptible_sleep_on(&edge_port->delta_msr_wait);
2676                                 /* see if a signal did it */
2677                                 if (signal_pending(current))
2678                                         return -ERESTARTSYS;
2679                                 cnow = edge_port->icount;
2680                                 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2681                                     cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2682                                         return -EIO; /* no change => error */
2683                                 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2684                                     ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2685                                     ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
2686                                     ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
2687                                         return 0;
2688                                 }
2689                                 cprev = cnow;
2690                         }
2691                         /* not reached */
2692                         break;
2693
2694                 case TIOCGICOUNT:
2695                         dbg ("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__,
2696                              port->number, edge_port->icount.rx, edge_port->icount.tx);
2697                         if (copy_to_user((void __user *)arg, &edge_port->icount, sizeof(edge_port->icount)))
2698                                 return -EFAULT;
2699                         return 0;
2700         }
2701
2702         return -ENOIOCTLCMD;
2703 }
2704
2705 static void edge_break (struct usb_serial_port *port, int break_state)
2706 {
2707         struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2708         int status;
2709
2710         dbg ("%s - state = %d", __FUNCTION__, break_state);
2711
2712         /* chase the port close */
2713         TIChasePort (edge_port, 0, 0);
2714
2715         if (break_state == -1) {
2716                 status = TISetBreak (edge_port);
2717         } else {
2718                 status = TIClearBreak (edge_port);
2719         }
2720         if (status) {
2721                 dbg ("%s - error %d sending break set/clear command.",
2722                      __FUNCTION__, status);
2723         }
2724 }
2725
2726 static int edge_startup (struct usb_serial *serial)
2727 {
2728         struct edgeport_serial *edge_serial;
2729         struct edgeport_port *edge_port;
2730         struct usb_device *dev;
2731         int status;
2732         int i;
2733
2734         dev = serial->dev;
2735
2736         /* create our private serial structure */
2737         edge_serial = kmalloc (sizeof(struct edgeport_serial), GFP_KERNEL);
2738         if (edge_serial == NULL) {
2739                 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
2740                 return -ENOMEM;
2741         }
2742         memset (edge_serial, 0, sizeof(struct edgeport_serial));
2743         sema_init(&edge_serial->es_sem, 1);
2744         edge_serial->serial = serial;
2745         usb_set_serial_data(serial, edge_serial);
2746
2747         status = TIDownloadFirmware (edge_serial);
2748         if (status) {
2749                 kfree (edge_serial);
2750                 return status;
2751         }
2752
2753         /* set up our port private structures */
2754         for (i = 0; i < serial->num_ports; ++i) {
2755                 edge_port = kmalloc (sizeof(struct edgeport_port), GFP_KERNEL);
2756                 if (edge_port == NULL) {
2757                         dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
2758                         goto cleanup;
2759                 }
2760                 memset (edge_port, 0, sizeof(struct edgeport_port));
2761                 spin_lock_init(&edge_port->ep_lock);
2762                 edge_port->ep_out_buf = edge_buf_alloc(EDGE_OUT_BUF_SIZE);
2763                 if (edge_port->ep_out_buf == NULL) {
2764                         dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
2765                         kfree(edge_port);
2766                         goto cleanup;
2767                 }
2768                 edge_port->port = serial->port[i];
2769                 edge_port->edge_serial = edge_serial;
2770                 usb_set_serial_port_data(serial->port[i], edge_port);
2771                 edge_port->bUartMode = 0;       /* Default is RS232 */
2772         }
2773         
2774         return 0;
2775
2776 cleanup:
2777         for (--i; i>=0; --i) {
2778                 edge_port = usb_get_serial_port_data(serial->port[i]);
2779                 edge_buf_free(edge_port->ep_out_buf);
2780                 kfree(edge_port);
2781                 usb_set_serial_port_data(serial->port[i], NULL);
2782         }
2783         kfree (edge_serial);
2784         usb_set_serial_data(serial, NULL);
2785         return -ENOMEM;
2786 }
2787
2788 static void edge_shutdown (struct usb_serial *serial)
2789 {
2790         int i;
2791         struct edgeport_port *edge_port;
2792
2793         dbg ("%s", __FUNCTION__);
2794
2795         for (i=0; i < serial->num_ports; ++i) {
2796                 edge_port = usb_get_serial_port_data(serial->port[i]);
2797                 if (edge_port) {
2798                         edge_buf_free(edge_port->ep_out_buf);
2799                         kfree(edge_port);
2800                 }
2801                 usb_set_serial_port_data(serial->port[i], NULL);
2802         }
2803         kfree (usb_get_serial_data(serial));
2804         usb_set_serial_data(serial, NULL);
2805 }
2806
2807
2808 /* Circular Buffer */
2809
2810 /*
2811  * edge_buf_alloc
2812  *
2813  * Allocate a circular buffer and all associated memory.
2814  */
2815
2816 static struct edge_buf *edge_buf_alloc(unsigned int size)
2817 {
2818         struct edge_buf *eb;
2819
2820
2821         if (size == 0)
2822                 return NULL;
2823
2824         eb = (struct edge_buf *)kmalloc(sizeof(struct edge_buf), GFP_KERNEL);
2825         if (eb == NULL)
2826                 return NULL;
2827
2828         eb->buf_buf = kmalloc(size, GFP_KERNEL);
2829         if (eb->buf_buf == NULL) {
2830                 kfree(eb);
2831                 return NULL;
2832         }
2833
2834         eb->buf_size = size;
2835         eb->buf_get = eb->buf_put = eb->buf_buf;
2836
2837         return eb;
2838 }
2839
2840
2841 /*
2842  * edge_buf_free
2843  *
2844  * Free the buffer and all associated memory.
2845  */
2846
2847 void edge_buf_free(struct edge_buf *eb)
2848 {
2849         if (eb) {
2850                 kfree(eb->buf_buf);
2851                 kfree(eb);
2852         }
2853 }
2854
2855
2856 /*
2857  * edge_buf_clear
2858  *
2859  * Clear out all data in the circular buffer.
2860  */
2861
2862 static void edge_buf_clear(struct edge_buf *eb)
2863 {
2864         if (eb != NULL)
2865                 eb->buf_get = eb->buf_put;
2866                 /* equivalent to a get of all data available */
2867 }
2868
2869
2870 /*
2871  * edge_buf_data_avail
2872  *
2873  * Return the number of bytes of data available in the circular
2874  * buffer.
2875  */
2876
2877 static unsigned int edge_buf_data_avail(struct edge_buf *eb)
2878 {
2879         if (eb != NULL)
2880                 return ((eb->buf_size + eb->buf_put - eb->buf_get) % eb->buf_size);
2881         else
2882                 return 0;
2883 }
2884
2885
2886 /*
2887  * edge_buf_space_avail
2888  *
2889  * Return the number of bytes of space available in the circular
2890  * buffer.
2891  */
2892
2893 static unsigned int edge_buf_space_avail(struct edge_buf *eb)
2894 {
2895         if (eb != NULL)
2896                 return ((eb->buf_size + eb->buf_get - eb->buf_put - 1) % eb->buf_size);
2897         else
2898                 return 0;
2899 }
2900
2901
2902 /*
2903  * edge_buf_put
2904  *
2905  * Copy data data from a user buffer and put it into the circular buffer.
2906  * Restrict to the amount of space available.
2907  *
2908  * Return the number of bytes copied.
2909  */
2910
2911 static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
2912         unsigned int count)
2913 {
2914         unsigned int len;
2915
2916
2917         if (eb == NULL)
2918                 return 0;
2919
2920         len  = edge_buf_space_avail(eb);
2921         if (count > len)
2922                 count = len;
2923
2924         if (count == 0)
2925                 return 0;
2926
2927         len = eb->buf_buf + eb->buf_size - eb->buf_put;
2928         if (count > len) {
2929                 memcpy(eb->buf_put, buf, len);
2930                 memcpy(eb->buf_buf, buf+len, count - len);
2931                 eb->buf_put = eb->buf_buf + count - len;
2932         } else {
2933                 memcpy(eb->buf_put, buf, count);
2934                 if (count < len)
2935                         eb->buf_put += count;
2936                 else /* count == len */
2937                         eb->buf_put = eb->buf_buf;
2938         }
2939
2940         return count;
2941 }
2942
2943
2944 /*
2945  * edge_buf_get
2946  *
2947  * Get data from the circular buffer and copy to the given buffer.
2948  * Restrict to the amount of data available.
2949  *
2950  * Return the number of bytes copied.
2951  */
2952
2953 static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
2954         unsigned int count)
2955 {
2956         unsigned int len;
2957
2958
2959         if (eb == NULL)
2960                 return 0;
2961
2962         len = edge_buf_data_avail(eb);
2963         if (count > len)
2964                 count = len;
2965
2966         if (count == 0)
2967                 return 0;
2968
2969         len = eb->buf_buf + eb->buf_size - eb->buf_get;
2970         if (count > len) {
2971                 memcpy(buf, eb->buf_get, len);
2972                 memcpy(buf+len, eb->buf_buf, count - len);
2973                 eb->buf_get = eb->buf_buf + count - len;
2974         } else {
2975                 memcpy(buf, eb->buf_get, count);
2976                 if (count < len)
2977                         eb->buf_get += count;
2978                 else /* count == len */
2979                         eb->buf_get = eb->buf_buf;
2980         }
2981
2982         return count;
2983 }
2984
2985
2986 static struct usb_serial_driver edgeport_1port_device = {
2987         .driver = {
2988                 .owner          = THIS_MODULE,
2989                 .name           = "edgeport_ti_1",
2990         },
2991         .description            = "Edgeport TI 1 port adapter",
2992         .id_table               = edgeport_1port_id_table,
2993         .num_interrupt_in       = 1,
2994         .num_bulk_in            = 1,
2995         .num_bulk_out           = 1,
2996         .num_ports              = 1,
2997         .open                   = edge_open,
2998         .close                  = edge_close,
2999         .throttle               = edge_throttle,
3000         .unthrottle             = edge_unthrottle,
3001         .attach                 = edge_startup,
3002         .shutdown               = edge_shutdown,
3003         .ioctl                  = edge_ioctl,
3004         .set_termios            = edge_set_termios,
3005         .tiocmget               = edge_tiocmget,
3006         .tiocmset               = edge_tiocmset,
3007         .write                  = edge_write,
3008         .write_room             = edge_write_room,
3009         .chars_in_buffer        = edge_chars_in_buffer,
3010         .break_ctl              = edge_break,
3011         .read_int_callback      = edge_interrupt_callback,
3012         .read_bulk_callback     = edge_bulk_in_callback,
3013         .write_bulk_callback    = edge_bulk_out_callback,
3014 };
3015
3016 static struct usb_serial_driver edgeport_2port_device = {
3017         .driver = {
3018                 .owner          = THIS_MODULE,
3019                 .name           = "edgeport_ti_2",
3020         },
3021         .description            = "Edgeport TI 2 port adapter",
3022         .id_table               = edgeport_2port_id_table,
3023         .num_interrupt_in       = 1,
3024         .num_bulk_in            = 2,
3025         .num_bulk_out           = 2,
3026         .num_ports              = 2,
3027         .open                   = edge_open,
3028         .close                  = edge_close,
3029         .throttle               = edge_throttle,
3030         .unthrottle             = edge_unthrottle,
3031         .attach                 = edge_startup,
3032         .shutdown               = edge_shutdown,
3033         .ioctl                  = edge_ioctl,
3034         .set_termios            = edge_set_termios,
3035         .tiocmget               = edge_tiocmget,
3036         .tiocmset               = edge_tiocmset,
3037         .write                  = edge_write,
3038         .write_room             = edge_write_room,
3039         .chars_in_buffer        = edge_chars_in_buffer,
3040         .break_ctl              = edge_break,
3041         .read_int_callback      = edge_interrupt_callback,
3042         .read_bulk_callback     = edge_bulk_in_callback,
3043         .write_bulk_callback    = edge_bulk_out_callback,
3044 };
3045
3046
3047 static int __init edgeport_init(void)
3048 {
3049         int retval;
3050         retval = usb_serial_register(&edgeport_1port_device);
3051         if (retval)
3052                 goto failed_1port_device_register;
3053         retval = usb_serial_register(&edgeport_2port_device);
3054         if (retval)
3055                 goto failed_2port_device_register;
3056         retval = usb_register(&io_driver);
3057         if (retval) 
3058                 goto failed_usb_register;
3059         info(DRIVER_DESC " " DRIVER_VERSION);
3060         return 0;
3061 failed_usb_register:
3062         usb_serial_deregister(&edgeport_2port_device);
3063 failed_2port_device_register:
3064         usb_serial_deregister(&edgeport_1port_device);
3065 failed_1port_device_register:
3066         return retval;
3067 }
3068
3069 static void __exit edgeport_exit (void)
3070 {
3071         usb_deregister (&io_driver);
3072         usb_serial_deregister (&edgeport_1port_device);
3073         usb_serial_deregister (&edgeport_2port_device);
3074 }
3075
3076 module_init(edgeport_init);
3077 module_exit(edgeport_exit);
3078
3079 /* Module information */
3080 MODULE_AUTHOR(DRIVER_AUTHOR);
3081 MODULE_DESCRIPTION(DRIVER_DESC);
3082 MODULE_LICENSE("GPL");
3083
3084 module_param(debug, bool, S_IRUGO | S_IWUSR);
3085 MODULE_PARM_DESC(debug, "Debug enabled or not");
3086
3087 module_param(low_latency, bool, S_IRUGO | S_IWUSR);
3088 MODULE_PARM_DESC(low_latency, "Low latency enabled or not");
3089
3090 module_param(closing_wait, int, S_IRUGO | S_IWUSR);
3091 MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
3092
3093 module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
3094 MODULE_PARM_DESC(ignore_cpu_rev, "Ignore the cpu revision when connecting to a device");
3095