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