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