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