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