MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / usb / serial / io_ti.c
blobace42c870727d67d0c66574bba77d90f55e6306e
1 /*
2 * Edgeport USB Serial Converter driver
4 * Copyright (C) 2000-2002 Inside Out Networks, All rights reserved.
5 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
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.
12 * Supports the following devices:
13 * EP/1 EP/2 EP/4
15 * Version history:
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)
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"
43 static int debug;
46 * Version Information
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"
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[] */
59 #define EPROM_PAGE_SIZE 64
62 struct edgeport_uart_buf_desc {
63 __u32 count; // Number of bytes currently in buffer
66 /* different hardware types */
67 #define HARDWARE_TYPE_930 0
68 #define HARDWARE_TYPE_TIUMP 1
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
77 /* Product information read from the Edgeport */
78 struct product_info
80 int TiMode; // Current TI Mode
81 __u8 hardware_type; // Type of hardware
82 } __attribute__((packed));
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;
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;
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) },
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_21) },
128 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
129 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
130 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
131 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
135 /* Devices that this driver supports */
136 static struct usb_device_id id_table_combined [] = {
137 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
138 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
139 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
140 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
141 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
142 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
143 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
144 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
145 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
146 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
150 MODULE_DEVICE_TABLE (usb, id_table_combined);
152 static struct usb_driver io_driver = {
153 .owner = THIS_MODULE,
154 .name = "io_ti",
155 .probe = usb_serial_probe,
156 .disconnect = usb_serial_disconnect,
157 .id_table = id_table_combined,
161 static struct EDGE_FIRMWARE_VERSION_INFO OperationalCodeImageVersion;
163 static int TIStayInBootMode = 0;
164 static int ignore_cpu_rev = 0;
168 static void edge_set_termios (struct usb_serial_port *port, struct termios *old_termios);
170 static int TIReadVendorRequestSync (struct usb_device *dev,
171 __u8 request,
172 __u16 value,
173 __u16 index,
174 u8 *data,
175 int size)
177 int status;
179 status = usb_control_msg (dev,
180 usb_rcvctrlpipe(dev, 0),
181 request,
182 (USB_TYPE_VENDOR |
183 USB_RECIP_DEVICE |
184 USB_DIR_IN),
185 value,
186 index,
187 data,
188 size,
189 HZ);
190 if (status < 0)
191 return status;
192 if (status != size) {
193 dbg ("%s - wanted to write %d, but only wrote %d",
194 __FUNCTION__, size, status);
195 return -ECOMM;
197 return 0;
200 static int TISendVendorRequestSync (struct usb_device *dev,
201 __u8 request,
202 __u16 value,
203 __u16 index,
204 u8 *data,
205 int size)
207 int status;
209 status = usb_control_msg (dev,
210 usb_sndctrlpipe(dev, 0),
211 request,
212 (USB_TYPE_VENDOR |
213 USB_RECIP_DEVICE |
214 USB_DIR_OUT),
215 value,
216 index,
217 data,
218 size,
219 HZ);
221 if (status < 0)
222 return status;
223 if (status != size) {
224 dbg ("%s - wanted to write %d, but only wrote %d",
225 __FUNCTION__, size, status);
226 return -ECOMM;
228 return 0;
231 static int TIWriteCommandSync (struct usb_device *dev, __u8 command,
232 __u8 moduleid, __u16 value, u8 *data,
233 int size)
235 return TISendVendorRequestSync (dev,
236 command, // Request
237 value, // wValue
238 moduleid, // wIndex
239 data, // TransferBuffer
240 size); // TransferBufferLength
245 /* clear tx/rx buffers and fifo in TI UMP */
246 static int TIPurgeDataSync (struct usb_serial_port *port, __u16 mask)
248 int port_number = port->number - port->serial->minor;
250 dbg ("%s - port %d, mask %x", __FUNCTION__, port_number, mask);
252 return TIWriteCommandSync (port->serial->dev,
253 UMPC_PURGE_PORT,
254 (__u8)(UMPM_UART1_PORT + port_number),
255 mask,
256 NULL,
261 * TIReadDownloadMemory - Read edgeport memory from TI chip
262 * @dev: usb device pointer
263 * @start_address: Device CPU address at which to read
264 * @length: Length of above data
265 * @address_type: Can read both XDATA and I2C
266 * @buffer: pointer to input data buffer
268 static int TIReadDownloadMemory(struct usb_device *dev, int start_address,
269 int length, __u8 address_type, __u8 *buffer)
271 int status = 0;
272 __u8 read_length;
273 __be16 be_start_address;
275 dbg ("%s - @ %x for %d", __FUNCTION__, start_address, length);
277 /* Read in blocks of 64 bytes
278 * (TI firmware can't handle more than 64 byte reads)
280 while (length) {
281 if (length > 64)
282 read_length= 64;
283 else
284 read_length = (__u8)length;
286 if (read_length > 1) {
287 dbg ("%s - @ %x for %d", __FUNCTION__,
288 start_address, read_length);
290 be_start_address = cpu_to_be16 (start_address);
291 status = TIReadVendorRequestSync (dev,
292 UMPC_MEMORY_READ, // Request
293 (__u16)address_type, // wValue (Address type)
294 be_start_address, // wIndex (Address to read)
295 buffer, // TransferBuffer
296 read_length); // TransferBufferLength
298 if (status) {
299 dbg ("%s - ERROR %x", __FUNCTION__, status);
300 return status;
303 if (read_length > 1) {
304 usb_serial_debug_data(debug, &dev->dev, __FUNCTION__,
305 read_length, buffer);
308 /* Update pointers/length */
309 start_address += read_length;
310 buffer += read_length;
311 length -= read_length;
314 return status;
317 static int TIReadRam (struct usb_device *dev, int start_address, int length, __u8 *buffer)
319 return TIReadDownloadMemory (dev,
320 start_address,
321 length,
322 DTK_ADDR_SPACE_XDATA,
323 buffer);
326 /* Read edgeport memory to a given block */
327 static int TIReadBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 * buffer)
329 int status = 0;
330 int i;
332 for (i=0; i< length; i++) {
333 status = TIReadVendorRequestSync (serial->serial->dev,
334 UMPC_MEMORY_READ, // Request
335 serial->TI_I2C_Type, // wValue (Address type)
336 (__u16)(start_address+i), // wIndex
337 &buffer[i], // TransferBuffer
338 0x01); // TransferBufferLength
339 if (status) {
340 dbg ("%s - ERROR %x", __FUNCTION__, status);
341 return status;
345 dbg ("%s - start_address = %x, length = %d", __FUNCTION__, start_address, length);
346 usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, length, buffer);
348 serial->TiReadI2C = 1;
350 return status;
353 /* Write given block to TI EPROM memory */
354 static int TIWriteBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
356 int status = 0;
357 int i;
358 __u8 temp;
360 /* Must do a read before write */
361 if (!serial->TiReadI2C) {
362 status = TIReadBootMemory(serial, 0, 1, &temp);
363 if (status)
364 return status;
367 for (i=0; i < length; ++i) {
368 status = TISendVendorRequestSync (serial->serial->dev,
369 UMPC_MEMORY_WRITE, // Request
370 buffer[i], // wValue
371 (__u16)(i+start_address), // wIndex
372 NULL, // TransferBuffer
373 0); // TransferBufferLength
374 if (status)
375 return status;
378 dbg ("%s - start_sddr = %x, length = %d", __FUNCTION__, start_address, length);
379 usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, length, buffer);
381 return status;
385 /* Write edgeport I2C memory to TI chip */
386 static int TIWriteDownloadI2C (struct edgeport_serial *serial, int start_address, int length, __u8 address_type, __u8 *buffer)
388 int status = 0;
389 int write_length;
390 __be16 be_start_address;
392 /* We can only send a maximum of 1 aligned byte page at a time */
394 /* calulate the number of bytes left in the first page */
395 write_length = EPROM_PAGE_SIZE - (start_address & (EPROM_PAGE_SIZE - 1));
397 if (write_length > length)
398 write_length = length;
400 dbg ("%s - BytesInFirstPage Addr = %x, length = %d", __FUNCTION__, start_address, write_length);
401 usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, write_length, buffer);
403 /* Write first page */
404 be_start_address = cpu_to_be16 (start_address);
405 status = TISendVendorRequestSync (serial->serial->dev,
406 UMPC_MEMORY_WRITE, // Request
407 (__u16)address_type, // wValue
408 be_start_address, // wIndex
409 buffer, // TransferBuffer
410 write_length);
411 if (status) {
412 dbg ("%s - ERROR %d", __FUNCTION__, status);
413 return status;
416 length -= write_length;
417 start_address += write_length;
418 buffer += write_length;
420 /* We should be aligned now -- can write max page size bytes at a time */
421 while (length) {
422 if (length > EPROM_PAGE_SIZE)
423 write_length = EPROM_PAGE_SIZE;
424 else
425 write_length = length;
427 dbg ("%s - Page Write Addr = %x, length = %d", __FUNCTION__, start_address, write_length);
428 usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, write_length, buffer);
430 /* Write next page */
431 be_start_address = cpu_to_be16 (start_address);
432 status = TISendVendorRequestSync (serial->serial->dev,
433 UMPC_MEMORY_WRITE, // Request
434 (__u16)address_type, // wValue
435 be_start_address, // wIndex
436 buffer, // TransferBuffer
437 write_length); // TransferBufferLength
438 if (status) {
439 dbg ("%s - ERROR %d", __FUNCTION__, status);
440 return status;
443 length -= write_length;
444 start_address += write_length;
445 buffer += write_length;
447 return status;
450 /* Examine the UMP DMA registers and LSR
452 * Check the MSBit of the X and Y DMA byte count registers.
453 * A zero in this bit indicates that the TX DMA buffers are empty
454 * then check the TX Empty bit in the UART.
456 static int TIIsTxActive (struct edgeport_port *port)
458 int status;
459 struct out_endpoint_desc_block *oedb;
460 __u8 *lsr;
461 int bytes_left = 0;
463 oedb = kmalloc (sizeof (* oedb), GFP_KERNEL);
464 if (!oedb) {
465 dev_err (&port->port->dev, "%s - out of memory\n", __FUNCTION__);
466 return -ENOMEM;
469 lsr = kmalloc (1, GFP_KERNEL); /* Sigh, that's right, just one byte,
470 as not all platforms can do DMA
471 from stack */
472 if (!lsr) {
473 kfree(oedb);
474 return -ENOMEM;
476 /* Read the DMA Count Registers */
477 status = TIReadRam (port->port->serial->dev,
478 port->dma_address,
479 sizeof( *oedb),
480 (void *)oedb);
482 if (status)
483 goto exit_is_tx_active;
485 dbg ("%s - XByteCount 0x%X", __FUNCTION__, oedb->XByteCount);
487 /* and the LSR */
488 status = TIReadRam (port->port->serial->dev,
489 port->uart_base + UMPMEM_OFFS_UART_LSR,
491 lsr);
493 if (status)
494 goto exit_is_tx_active;
495 dbg ("%s - LSR = 0x%X", __FUNCTION__, *lsr);
497 /* If either buffer has data or we are transmitting then return TRUE */
498 if ((oedb->XByteCount & 0x80 ) != 0 )
499 bytes_left += 64;
501 if ((*lsr & UMP_UART_LSR_TX_MASK ) == 0 )
502 bytes_left += 1;
504 /* We return Not Active if we get any kind of error */
505 exit_is_tx_active:
506 dbg ("%s - return %d", __FUNCTION__, bytes_left );
508 kfree(lsr);
509 kfree(oedb);
510 return bytes_left;
513 static void TIChasePort(struct edgeport_port *port)
515 int loops;
516 int last_count;
517 int write_size;
519 restart_tx_loop:
520 // Base the LoopTime on the baud rate
521 if (port->baud_rate == 0)
522 port->baud_rate = 1200;
524 write_size = port->tx.count;
525 loops = max(100, (100*write_size)/(port->baud_rate/10));
526 dbg ("%s - write_size %d, baud %d loop = %d", __FUNCTION__,
527 write_size, port->baud_rate, loops);
529 while (1) {
530 // Save Last count
531 last_count = port->tx.count;
533 dbg ("%s - Tx Buffer Size = %d loops = %d", __FUNCTION__,
534 last_count, loops);
536 /* Is the Edgeport Buffer empty? */
537 if (port->tx.count == 0)
538 break;
540 /* Block the thread for 10ms */
541 msleep(10);
543 if (last_count == port->tx.count) {
544 /* No activity.. count down. */
545 --loops;
546 if (loops == 0) {
547 dbg ("%s - Wait for TxEmpty - TIMEOUT",
548 __FUNCTION__);
549 return;
551 } else {
552 /* Reset timeout value back to a minimum of 1 second */
553 dbg ("%s - Wait for TxEmpty Reset Count", __FUNCTION__);
554 goto restart_tx_loop;
558 dbg ("%s - Local Tx Buffer Empty -- Waiting for TI UMP to EMPTY X/Y and FIFO",
559 __FUNCTION__);
561 write_size = TIIsTxActive (port);
562 loops = max(50, (100*write_size)/(port->baud_rate/10));
563 dbg ("%s - write_size %d, baud %d loop = %d", __FUNCTION__,
564 write_size, port->baud_rate, loops);
566 while (1) {
567 /* This function takes 4 ms; */
568 if (!TIIsTxActive (port)) {
569 /* Delay a few char times */
570 msleep(50);
571 dbg ("%s - Empty", __FUNCTION__);
572 return;
575 --loops;
576 if (loops == 0) {
577 dbg ("%s - TIMEOUT", __FUNCTION__);
578 return;
583 static int TIChooseConfiguration (struct usb_device *dev)
585 // There may be multiple configurations on this device, in which case
586 // we would need to read and parse all of them to find out which one
587 // we want. However, we just support one config at this point,
588 // configuration # 1, which is Config Descriptor 0.
590 dbg ("%s - Number of Interfaces = %d", __FUNCTION__, dev->config->desc.bNumInterfaces);
591 dbg ("%s - MAX Power = %d", __FUNCTION__, dev->config->desc.bMaxPower*2);
593 if (dev->config->desc.bNumInterfaces != 1) {
594 dev_err (&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __FUNCTION__);
595 return -ENODEV;
598 return 0;
601 static int TIReadRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
603 int status;
605 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
606 status = TIReadDownloadMemory (serial->serial->dev,
607 start_address,
608 length,
609 serial->TI_I2C_Type,
610 buffer);
611 } else {
612 status = TIReadBootMemory (serial,
613 start_address,
614 length,
615 buffer);
618 return status;
621 static int TIWriteRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
623 if (serial->product_info.TiMode == TI_MODE_BOOT)
624 return TIWriteBootMemory (serial,
625 start_address,
626 length,
627 buffer);
629 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
630 return TIWriteDownloadI2C (serial,
631 start_address,
632 length,
633 serial->TI_I2C_Type,
634 buffer);
636 return -EINVAL;
641 /* Read a descriptor header from I2C based on type */
642 static int TIGetDescriptorAddress (struct edgeport_serial *serial, int desc_type, struct ti_i2c_desc *rom_desc)
644 int start_address;
645 int status;
647 /* Search for requested descriptor in I2C */
648 start_address = 2;
649 do {
650 status = TIReadRom (serial,
651 start_address,
652 sizeof(struct ti_i2c_desc),
653 (__u8 *)rom_desc );
654 if (status)
655 return 0;
657 if (rom_desc->Type == desc_type)
658 return start_address;
660 start_address = start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size;
662 } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
664 return 0;
667 /* Validate descriptor checksum */
668 static int ValidChecksum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
670 __u16 i;
671 __u8 cs = 0;
673 for (i=0; i < rom_desc->Size; i++) {
674 cs = (__u8)(cs + buffer[i]);
676 if (cs != rom_desc->CheckSum) {
677 dbg ("%s - Mismatch %x - %x", __FUNCTION__, rom_desc->CheckSum, cs);
678 return -EINVAL;
680 return 0;
683 /* Make sure that the I2C image is good */
684 static int TiValidateI2cImage (struct edgeport_serial *serial)
686 struct device *dev = &serial->serial->dev->dev;
687 int status = 0;
688 struct ti_i2c_desc *rom_desc;
689 int start_address = 2;
690 __u8 *buffer;
692 rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
693 if (!rom_desc) {
694 dev_err (dev, "%s - out of memory\n", __FUNCTION__);
695 return -ENOMEM;
697 buffer = kmalloc (TI_MAX_I2C_SIZE, GFP_KERNEL);
698 if (!buffer) {
699 dev_err (dev, "%s - out of memory when allocating buffer\n", __FUNCTION__);
700 kfree (rom_desc);
701 return -ENOMEM;
704 // Read the first byte (Signature0) must be 0x52
705 status = TIReadRom (serial, 0, 1, buffer);
706 if (status)
707 goto ExitTiValidateI2cImage;
709 if (*buffer != 0x52) {
710 dev_err (dev, "%s - invalid buffer signature\n", __FUNCTION__);
711 status = -ENODEV;
712 goto ExitTiValidateI2cImage;
715 do {
716 // Validate the I2C
717 status = TIReadRom (serial,
718 start_address,
719 sizeof(struct ti_i2c_desc),
720 (__u8 *)rom_desc);
721 if (status)
722 break;
724 if ((start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size) > TI_MAX_I2C_SIZE) {
725 status = -ENODEV;
726 dbg ("%s - structure too big, erroring out.", __FUNCTION__);
727 break;
730 dbg ("%s Type = 0x%x", __FUNCTION__, rom_desc->Type);
732 // Skip type 2 record
733 if ((rom_desc->Type & 0x0f) != I2C_DESC_TYPE_FIRMWARE_BASIC) {
734 // Read the descriptor data
735 status = TIReadRom(serial,
736 start_address+sizeof(struct ti_i2c_desc),
737 rom_desc->Size,
738 buffer);
739 if (status)
740 break;
742 status = ValidChecksum(rom_desc, buffer);
743 if (status)
744 break;
746 start_address = start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size;
748 } while ((rom_desc->Type != I2C_DESC_TYPE_ION) && (start_address < TI_MAX_I2C_SIZE));
750 if ((rom_desc->Type != I2C_DESC_TYPE_ION) || (start_address > TI_MAX_I2C_SIZE))
751 status = -ENODEV;
753 ExitTiValidateI2cImage:
754 kfree (buffer);
755 kfree (rom_desc);
756 return status;
759 static int TIReadManufDescriptor (struct edgeport_serial *serial, __u8 *buffer)
761 int status;
762 int start_address;
763 struct ti_i2c_desc *rom_desc;
764 struct edge_ti_manuf_descriptor *desc;
766 rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
767 if (!rom_desc) {
768 dev_err (&serial->serial->dev->dev, "%s - out of memory\n", __FUNCTION__);
769 return -ENOMEM;
771 start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_ION, rom_desc);
773 if (!start_address) {
774 dbg ("%s - Edge Descriptor not found in I2C", __FUNCTION__);
775 status = -ENODEV;
776 goto exit;
779 // Read the descriptor data
780 status = TIReadRom (serial,
781 start_address+sizeof(struct ti_i2c_desc),
782 rom_desc->Size,
783 buffer);
784 if (status)
785 goto exit;
787 status = ValidChecksum(rom_desc, buffer);
789 desc = (struct edge_ti_manuf_descriptor *)buffer;
790 dbg ( "%s - IonConfig 0x%x", __FUNCTION__, desc->IonConfig );
791 dbg ( "%s - Version %d", __FUNCTION__, desc->Version );
792 dbg ( "%s - Cpu/Board 0x%x", __FUNCTION__, desc->CpuRev_BoardRev );
793 dbg ( "%s - NumPorts %d", __FUNCTION__, desc->NumPorts );
794 dbg ( "%s - NumVirtualPorts %d", __FUNCTION__, desc->NumVirtualPorts );
795 dbg ( "%s - TotalPorts %d", __FUNCTION__, desc->TotalPorts );
797 exit:
798 kfree (rom_desc);
799 return status;
802 /* Build firmware header used for firmware update */
803 static int BuildI2CFirmwareHeader (__u8 *header, struct device *dev)
805 __u8 *buffer;
806 int buffer_size;
807 int i;
808 __u8 cs = 0;
809 struct ti_i2c_desc *i2c_header;
810 struct ti_i2c_image_header *img_header;
811 struct ti_i2c_firmware_rec *firmware_rec;
813 // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
814 // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver
815 // will download the latest firmware (padded to 15.5k) into the UMP ram.
816 // And finally when the device comes back up in download mode the driver will cause
817 // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
818 // the record type from 0xf2 to 0x02.
820 // Allocate a 15.5k buffer + 2 bytes for version number (Firmware Record)
821 buffer_size = (((1024 * 16) - 512 )+ sizeof(struct ti_i2c_firmware_rec));
823 buffer = kmalloc (buffer_size, GFP_KERNEL);
824 if (!buffer) {
825 dev_err (dev, "%s - out of memory\n", __FUNCTION__);
826 return -ENOMEM;
829 // Set entire image of 0xffs
830 memset (buffer, 0xff, buffer_size);
832 // Copy version number into firmware record
833 firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
835 firmware_rec->Ver_Major = OperationalCodeImageVersion.MajorVersion;
836 firmware_rec->Ver_Minor = OperationalCodeImageVersion.MinorVersion;
838 // Pointer to fw_down memory image
839 img_header = (struct ti_i2c_image_header *)&PagableOperationalCodeImage[0];
841 memcpy (buffer + sizeof(struct ti_i2c_firmware_rec),
842 &PagableOperationalCodeImage[sizeof(struct ti_i2c_image_header)],
843 img_header->Length);
845 for (i=0; i < buffer_size; i++) {
846 cs = (__u8)(cs + buffer[i]);
849 kfree (buffer);
851 // Build new header
852 i2c_header = (struct ti_i2c_desc *)header;
853 firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data;
855 i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK;
856 i2c_header->Size = (__u16)buffer_size;
857 i2c_header->CheckSum = cs;
858 firmware_rec->Ver_Major = OperationalCodeImageVersion.MajorVersion;
859 firmware_rec->Ver_Minor = OperationalCodeImageVersion.MinorVersion;
861 return 0;
864 /* Try to figure out what type of I2c we have */
865 static int TIGetI2cTypeInBootMode (struct edgeport_serial *serial)
867 int status;
868 __u8 data;
870 // Try to read type 2
871 status = TIReadVendorRequestSync (serial->serial->dev,
872 UMPC_MEMORY_READ, // Request
873 DTK_ADDR_SPACE_I2C_TYPE_II, // wValue (Address type)
874 0, // wIndex
875 &data, // TransferBuffer
876 0x01); // TransferBufferLength
877 if (status)
878 dbg ("%s - read 2 status error = %d", __FUNCTION__, status);
879 else
880 dbg ("%s - read 2 data = 0x%x", __FUNCTION__, data);
881 if ((!status) && data == 0x52) {
882 dbg ("%s - ROM_TYPE_II", __FUNCTION__);
883 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
884 return 0;
887 // Try to read type 3
888 status = TIReadVendorRequestSync (serial->serial->dev,
889 UMPC_MEMORY_READ, // Request
890 DTK_ADDR_SPACE_I2C_TYPE_III, // wValue (Address type)
891 0, // wIndex
892 &data, // TransferBuffer
893 0x01); // TransferBufferLength
894 if (status)
895 dbg ("%s - read 3 status error = %d", __FUNCTION__, status);
896 else
897 dbg ("%s - read 2 data = 0x%x", __FUNCTION__, data);
898 if ((!status) && data == 0x52) {
899 dbg ("%s - ROM_TYPE_III", __FUNCTION__);
900 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
901 return 0;
904 dbg ("%s - Unknown", __FUNCTION__);
905 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
906 return -ENODEV;
909 static int TISendBulkTransferSync (struct usb_serial *serial, void *buffer, int length, int *num_sent)
911 int status;
913 status = usb_bulk_msg (serial->dev,
914 usb_sndbulkpipe(serial->dev,
915 serial->port[0]->bulk_out_endpointAddress),
916 buffer,
917 length,
918 num_sent,
919 HZ);
920 return status;
923 /* Download given firmware image to the device (IN BOOT MODE) */
924 static int TIDownloadCodeImage (struct edgeport_serial *serial, __u8 *image, int image_length)
926 int status = 0;
927 int pos;
928 int transfer;
929 int done;
931 // Transfer firmware image
932 for (pos = 0; pos < image_length; ) {
933 // Read the next buffer from file
934 transfer = image_length - pos;
935 if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
936 transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
938 // Transfer data
939 status = TISendBulkTransferSync (serial->serial, &image[pos], transfer, &done);
940 if (status)
941 break;
942 // Advance buffer pointer
943 pos += done;
946 return status;
949 // FIXME!!!
950 static int TIConfigureBootDevice (struct usb_device *dev)
952 return 0;
956 * DownloadTIFirmware - Download run-time operating firmware to the TI5052
958 * This routine downloads the main operating code into the TI5052, using the
959 * boot code already burned into E2PROM or ROM.
961 static int TIDownloadFirmware (struct edgeport_serial *serial)
963 struct device *dev = &serial->serial->dev->dev;
964 int status = 0;
965 int start_address;
966 struct edge_ti_manuf_descriptor *ti_manuf_desc;
967 struct usb_interface_descriptor *interface;
968 int download_cur_ver;
969 int download_new_ver;
971 /* This routine is entered by both the BOOT mode and the Download mode
972 * We can determine which code is running by the reading the config
973 * descriptor and if we have only one bulk pipe it is in boot mode
975 serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
977 /* Default to type 2 i2c */
978 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
980 status = TIChooseConfiguration (serial->serial->dev);
981 if (status)
982 return status;
984 interface = &serial->serial->interface->cur_altsetting->desc;
985 if (!interface) {
986 dev_err (&serial->serial->dev->dev, "%s - no interface set, error!", __FUNCTION__);
987 return -ENODEV;
990 // Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
991 // if we have more than one endpoint we are definitely in download mode
992 if (interface->bNumEndpoints > 1)
993 serial->product_info.TiMode = TI_MODE_DOWNLOAD;
994 else
995 // Otherwise we will remain in configuring mode
996 serial->product_info.TiMode = TI_MODE_CONFIGURING;
998 // Save Download Version Number
999 OperationalCodeImageVersion.MajorVersion = PagableOperationalCodeImageVersion.MajorVersion;
1000 OperationalCodeImageVersion.MinorVersion = PagableOperationalCodeImageVersion.MinorVersion;
1001 OperationalCodeImageVersion.BuildNumber = PagableOperationalCodeImageVersion.BuildNumber;
1003 /********************************************************************/
1004 /* Download Mode */
1005 /********************************************************************/
1006 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
1007 struct ti_i2c_desc *rom_desc;
1009 dbg ("%s - <<<<<<<<<<<<<<<RUNNING IN DOWNLOAD MODE>>>>>>>>>>", __FUNCTION__);
1011 status = TiValidateI2cImage (serial);
1012 if (status) {
1013 dbg ("%s - <<<<<<<<<<<<<<<DOWNLOAD MODE -- BAD I2C >>>>>>>>>>",
1014 __FUNCTION__);
1015 return status;
1018 /* Validate Hardware version number
1019 * Read Manufacturing Descriptor from TI Based Edgeport
1021 ti_manuf_desc = kmalloc (sizeof (*ti_manuf_desc), GFP_KERNEL);
1022 if (!ti_manuf_desc) {
1023 dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
1024 return -ENOMEM;
1026 status = TIReadManufDescriptor (serial, (__u8 *)ti_manuf_desc);
1027 if (status) {
1028 kfree (ti_manuf_desc);
1029 return status;
1032 // Check version number of ION descriptor
1033 if (!ignore_cpu_rev && TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev) < 2) {
1034 dbg ( "%s - Wrong CPU Rev %d (Must be 2)", __FUNCTION__,
1035 TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev));
1036 kfree (ti_manuf_desc);
1037 return -EINVAL;
1040 rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
1041 if (!rom_desc) {
1042 dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
1043 kfree (ti_manuf_desc);
1044 return -ENOMEM;
1047 // Search for type 2 record (firmware record)
1048 if ((start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc)) != 0) {
1049 struct ti_i2c_firmware_rec *firmware_version;
1050 __u8 record;
1052 dbg ("%s - Found Type FIRMWARE (Type 2) record", __FUNCTION__);
1054 firmware_version = kmalloc (sizeof (*firmware_version), GFP_KERNEL);
1055 if (!firmware_version) {
1056 dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
1057 kfree (rom_desc);
1058 kfree (ti_manuf_desc);
1059 return -ENOMEM;
1062 // Validate version number
1063 // Read the descriptor data
1064 status = TIReadRom (serial,
1065 start_address+sizeof(struct ti_i2c_desc),
1066 sizeof(struct ti_i2c_firmware_rec),
1067 (__u8 *)firmware_version);
1068 if (status) {
1069 kfree (firmware_version);
1070 kfree (rom_desc);
1071 kfree (ti_manuf_desc);
1072 return status;
1075 // Check version number of download with current version in I2c
1076 download_cur_ver = (firmware_version->Ver_Major << 8) +
1077 (firmware_version->Ver_Minor);
1078 download_new_ver = (OperationalCodeImageVersion.MajorVersion << 8) +
1079 (OperationalCodeImageVersion.MinorVersion);
1081 dbg ("%s - >>>Firmware Versions Device %d.%d Driver %d.%d",
1082 __FUNCTION__,
1083 firmware_version->Ver_Major,
1084 firmware_version->Ver_Minor,
1085 OperationalCodeImageVersion.MajorVersion,
1086 OperationalCodeImageVersion.MinorVersion);
1088 // Check if we have an old version in the I2C and update if necessary
1089 if (download_cur_ver != download_new_ver) {
1090 dbg ("%s - Update I2C Download from %d.%d to %d.%d",
1091 __FUNCTION__,
1092 firmware_version->Ver_Major,
1093 firmware_version->Ver_Minor,
1094 OperationalCodeImageVersion.MajorVersion,
1095 OperationalCodeImageVersion.MinorVersion);
1097 // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
1098 // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver
1099 // will download the latest firmware (padded to 15.5k) into the UMP ram.
1100 // And finally when the device comes back up in download mode the driver will cause
1101 // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
1102 // the record type from 0xf2 to 0x02.
1104 record = I2C_DESC_TYPE_FIRMWARE_BLANK;
1106 // Change the I2C Firmware record type to 0xf2 to trigger an update
1107 status = TIWriteRom (serial,
1108 start_address,
1109 sizeof(record),
1110 &record);
1111 if (status) {
1112 kfree (firmware_version);
1113 kfree (rom_desc);
1114 kfree (ti_manuf_desc);
1115 return status;
1118 // verify the write -- must do this in order for write to
1119 // complete before we do the hardware reset
1120 status = TIReadRom (serial,
1121 start_address,
1122 sizeof(record),
1123 &record);
1125 if (status) {
1126 kfree (firmware_version);
1127 kfree (rom_desc);
1128 kfree (ti_manuf_desc);
1129 return status;
1132 if (record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
1133 dev_err (dev, "%s - error resetting device\n", __FUNCTION__);
1134 kfree (firmware_version);
1135 kfree (rom_desc);
1136 kfree (ti_manuf_desc);
1137 return -ENODEV;
1140 dbg ("%s - HARDWARE RESET", __FUNCTION__);
1142 // Reset UMP -- Back to BOOT MODE
1143 status = TISendVendorRequestSync (serial->serial->dev,
1144 UMPC_HARDWARE_RESET, // Request
1145 0, // wValue
1146 0, // wIndex
1147 NULL, // TransferBuffer
1148 0); // TransferBufferLength
1150 dbg ( "%s - HARDWARE RESET return %d", __FUNCTION__, status);
1152 /* return an error on purpose. */
1153 kfree (firmware_version);
1154 kfree (rom_desc);
1155 kfree (ti_manuf_desc);
1156 return -ENODEV;
1158 kfree (firmware_version);
1160 // Search for type 0xF2 record (firmware blank record)
1161 else if ((start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
1162 #define HEADER_SIZE (sizeof(struct ti_i2c_desc) + sizeof(struct ti_i2c_firmware_rec))
1163 __u8 *header;
1164 __u8 *vheader;
1166 header = kmalloc (HEADER_SIZE, GFP_KERNEL);
1167 if (!header) {
1168 dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
1169 kfree (rom_desc);
1170 kfree (ti_manuf_desc);
1171 return -ENOMEM;
1174 vheader = kmalloc (HEADER_SIZE, GFP_KERNEL);
1175 if (!vheader) {
1176 dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
1177 kfree (header);
1178 kfree (rom_desc);
1179 kfree (ti_manuf_desc);
1180 return -ENOMEM;
1183 dbg ("%s - Found Type BLANK FIRMWARE (Type F2) record", __FUNCTION__);
1185 // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
1186 // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver
1187 // will download the latest firmware (padded to 15.5k) into the UMP ram.
1188 // And finally when the device comes back up in download mode the driver will cause
1189 // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
1190 // the record type from 0xf2 to 0x02.
1191 status = BuildI2CFirmwareHeader(header, dev);
1192 if (status) {
1193 kfree (vheader);
1194 kfree (header);
1195 kfree (rom_desc);
1196 kfree (ti_manuf_desc);
1197 return status;
1200 // Update I2C with type 0xf2 record with correct size and checksum
1201 status = TIWriteRom (serial,
1202 start_address,
1203 HEADER_SIZE,
1204 header);
1205 if (status) {
1206 kfree (vheader);
1207 kfree (header);
1208 kfree (rom_desc);
1209 kfree (ti_manuf_desc);
1210 return status;
1213 // verify the write -- must do this in order for write to
1214 // complete before we do the hardware reset
1215 status = TIReadRom (serial,
1216 start_address,
1217 HEADER_SIZE,
1218 vheader);
1220 if (status) {
1221 dbg ("%s - can't read header back", __FUNCTION__);
1222 kfree (vheader);
1223 kfree (header);
1224 kfree (rom_desc);
1225 kfree (ti_manuf_desc);
1226 return status;
1228 if (memcmp(vheader, header, HEADER_SIZE)) {
1229 dbg ("%s - write download record failed", __FUNCTION__);
1230 kfree (vheader);
1231 kfree (header);
1232 kfree (rom_desc);
1233 kfree (ti_manuf_desc);
1234 return status;
1237 kfree (vheader);
1238 kfree (header);
1240 dbg ("%s - Start firmware update", __FUNCTION__);
1242 // Tell firmware to copy download image into I2C
1243 status = TISendVendorRequestSync (serial->serial->dev,
1244 UMPC_COPY_DNLD_TO_I2C, // Request
1245 0, // wValue
1246 0, // wIndex
1247 NULL, // TransferBuffer
1248 0); // TransferBufferLength
1250 dbg ("%s - Update complete 0x%x", __FUNCTION__, status);
1251 if (status) {
1252 dbg ("%s - UMPC_COPY_DNLD_TO_I2C failed", __FUNCTION__);
1253 kfree (rom_desc);
1254 kfree (ti_manuf_desc);
1255 return status;
1259 // The device is running the download code
1260 kfree (rom_desc);
1261 kfree (ti_manuf_desc);
1262 return 0;
1265 /********************************************************************/
1266 /* Boot Mode */
1267 /********************************************************************/
1268 dbg ("%s - <<<<<<<<<<<<<<<RUNNING IN BOOT MODE>>>>>>>>>>>>>>>",
1269 __FUNCTION__);
1271 // Configure the TI device so we can use the BULK pipes for download
1272 status = TIConfigureBootDevice (serial->serial->dev);
1273 if (status)
1274 return status;
1276 if (serial->serial->dev->descriptor.idVendor != USB_VENDOR_ID_ION) {
1277 dbg ("%s - VID = 0x%x", __FUNCTION__,
1278 serial->serial->dev->descriptor.idVendor);
1279 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1280 goto StayInBootMode;
1283 // We have an ION device (I2c Must be programmed)
1284 // Determine I2C image type
1285 if (TIGetI2cTypeInBootMode(serial)) {
1286 goto StayInBootMode;
1289 // Registry variable set?
1290 if (TIStayInBootMode) {
1291 dbg ("%s - TIStayInBootMode", __FUNCTION__);
1292 goto StayInBootMode;
1295 // Check for ION Vendor ID and that the I2C is valid
1296 if (!TiValidateI2cImage(serial)) {
1297 struct ti_i2c_image_header *header;
1298 int i;
1299 __u8 cs = 0;
1300 __u8 *buffer;
1301 int buffer_size;
1303 /* Validate Hardware version number
1304 * Read Manufacturing Descriptor from TI Based Edgeport
1306 ti_manuf_desc = kmalloc (sizeof (*ti_manuf_desc), GFP_KERNEL);
1307 if (!ti_manuf_desc) {
1308 dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
1309 return -ENOMEM;
1311 status = TIReadManufDescriptor (serial, (__u8 *)ti_manuf_desc);
1312 if (status) {
1313 kfree (ti_manuf_desc);
1314 goto StayInBootMode;
1317 // Check for version 2
1318 if (!ignore_cpu_rev && TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev) < 2) {
1319 dbg ("%s - Wrong CPU Rev %d (Must be 2)", __FUNCTION__,
1320 TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev));
1321 kfree (ti_manuf_desc);
1322 goto StayInBootMode;
1325 kfree (ti_manuf_desc);
1327 // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
1328 // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver
1329 // will download the latest firmware (padded to 15.5k) into the UMP ram.
1330 // And finally when the device comes back up in download mode the driver will cause
1331 // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
1332 // the record type from 0xf2 to 0x02.
1335 * Do we really have to copy the whole firmware image,
1336 * or could we do this in place!
1339 // Allocate a 15.5k buffer + 3 byte header
1340 buffer_size = (((1024 * 16) - 512) + sizeof(struct ti_i2c_image_header));
1341 buffer = kmalloc (buffer_size, GFP_KERNEL);
1342 if (!buffer) {
1343 dev_err (dev, "%s - out of memory\n", __FUNCTION__);
1344 return -ENOMEM;
1347 // Initialize the buffer to 0xff (pad the buffer)
1348 memset (buffer, 0xff, buffer_size);
1350 memcpy (buffer, &PagableOperationalCodeImage[0], PagableOperationalCodeSize);
1352 for(i = sizeof(struct ti_i2c_image_header); i < buffer_size; i++) {
1353 cs = (__u8)(cs + buffer[i]);
1356 header = (struct ti_i2c_image_header *)buffer;
1358 // update length and checksum after padding
1359 header->Length = (__u16)(buffer_size - sizeof(struct ti_i2c_image_header));
1360 header->CheckSum = cs;
1362 // Download the operational code
1363 dbg ("%s - Downloading operational code image (TI UMP)", __FUNCTION__);
1364 status = TIDownloadCodeImage (serial, buffer, buffer_size);
1366 kfree (buffer);
1368 if (status) {
1369 dbg ("%s - Error downloading operational code image", __FUNCTION__);
1370 return status;
1373 // Device will reboot
1374 serial->product_info.TiMode = TI_MODE_TRANSITIONING;
1376 dbg ("%s - Download successful -- Device rebooting...", __FUNCTION__);
1378 /* return an error on purpose */
1379 return -ENODEV;
1382 StayInBootMode:
1383 // Eprom is invalid or blank stay in boot mode
1384 dbg ("%s - <<<<<<<<<<<<<<<STAYING IN BOOT MODE>>>>>>>>>>>>", __FUNCTION__);
1385 serial->product_info.TiMode = TI_MODE_BOOT;
1387 return 0;
1391 static int TISetDtr (struct edgeport_port *port)
1393 int port_number = port->port->number - port->port->serial->minor;
1395 dbg ("%s", __FUNCTION__);
1396 port->shadow_mcr |= MCR_DTR;
1398 return TIWriteCommandSync (port->port->serial->dev,
1399 UMPC_SET_CLR_DTR,
1400 (__u8)(UMPM_UART1_PORT + port_number),
1401 1, /* set */
1402 NULL,
1406 static int TIClearDtr (struct edgeport_port *port)
1408 int port_number = port->port->number - port->port->serial->minor;
1410 dbg ("%s", __FUNCTION__);
1411 port->shadow_mcr &= ~MCR_DTR;
1413 return TIWriteCommandSync (port->port->serial->dev,
1414 UMPC_SET_CLR_DTR,
1415 (__u8)(UMPM_UART1_PORT + port_number),
1416 0, /* clear */
1417 NULL,
1421 static int TISetRts (struct edgeport_port *port)
1423 int port_number = port->port->number - port->port->serial->minor;
1425 dbg ("%s", __FUNCTION__);
1426 port->shadow_mcr |= MCR_RTS;
1428 return TIWriteCommandSync (port->port->serial->dev,
1429 UMPC_SET_CLR_RTS,
1430 (__u8)(UMPM_UART1_PORT + port_number),
1431 1, /* set */
1432 NULL,
1436 static int TIClearRts (struct edgeport_port *port)
1438 int port_number = port->port->number - port->port->serial->minor;
1440 dbg ("%s", __FUNCTION__);
1441 port->shadow_mcr &= ~MCR_RTS;
1443 return TIWriteCommandSync (port->port->serial->dev,
1444 UMPC_SET_CLR_RTS,
1445 (__u8)(UMPM_UART1_PORT + port_number),
1446 0, /* clear */
1447 NULL,
1451 static int TISetLoopBack (struct edgeport_port *port)
1453 int port_number = port->port->number - port->port->serial->minor;
1455 dbg ("%s", __FUNCTION__);
1457 return TIWriteCommandSync (port->port->serial->dev,
1458 UMPC_SET_CLR_LOOPBACK,
1459 (__u8)(UMPM_UART1_PORT + port_number),
1460 1, /* set */
1461 NULL,
1465 static int TIClearLoopBack (struct edgeport_port *port)
1467 int port_number = port->port->number - port->port->serial->minor;
1469 dbg ("%s", __FUNCTION__);
1471 return TIWriteCommandSync (port->port->serial->dev,
1472 UMPC_SET_CLR_LOOPBACK,
1473 (__u8)(UMPM_UART1_PORT + port_number),
1474 0, /* clear */
1475 NULL,
1479 static int TISetBreak (struct edgeport_port *port)
1481 int port_number = port->port->number - port->port->serial->minor;
1483 dbg ("%s", __FUNCTION__);
1485 return TIWriteCommandSync (port->port->serial->dev,
1486 UMPC_SET_CLR_BREAK,
1487 (__u8)(UMPM_UART1_PORT + port_number),
1488 1, /* set */
1489 NULL,
1493 static int TIClearBreak (struct edgeport_port *port)
1495 int port_number = port->port->number - port->port->serial->minor;
1497 dbg ("%s", __FUNCTION__);
1499 return TIWriteCommandSync (port->port->serial->dev,
1500 UMPC_SET_CLR_BREAK,
1501 (__u8)(UMPM_UART1_PORT + port_number),
1502 0, /* clear */
1503 NULL,
1507 static int TIRestoreMCR (struct edgeport_port *port, __u8 mcr)
1509 int status = 0;
1511 dbg ("%s - %x", __FUNCTION__, mcr);
1513 if (mcr & MCR_DTR)
1514 status = TISetDtr (port);
1515 else
1516 status = TIClearDtr (port);
1518 if (status)
1519 return status;
1521 if (mcr & MCR_RTS)
1522 status = TISetRts (port);
1523 else
1524 status = TIClearRts (port);
1526 if (status)
1527 return status;
1529 if (mcr & MCR_LOOPBACK)
1530 status = TISetLoopBack (port);
1531 else
1532 status = TIClearLoopBack (port);
1534 return status;
1539 /* Convert TI LSR to standard UART flags */
1540 static __u8 MapLineStatus (__u8 ti_lsr)
1542 __u8 lsr = 0;
1544 #define MAP_FLAG(flagUmp, flagUart) \
1545 if (ti_lsr & flagUmp) \
1546 lsr |= flagUart;
1548 MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR) /* overrun */
1549 MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */
1550 MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */
1551 MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */
1552 MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* receive data available */
1553 MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* transmit holding register empty */
1555 #undef MAP_FLAG
1557 return lsr;
1560 static void handle_new_msr (struct edgeport_port *edge_port, __u8 msr)
1562 struct async_icount *icount;
1564 dbg ("%s - %02x", __FUNCTION__, msr);
1566 if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR | EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
1567 icount = &edge_port->icount;
1569 /* update input line counters */
1570 if (msr & EDGEPORT_MSR_DELTA_CTS)
1571 icount->cts++;
1572 if (msr & EDGEPORT_MSR_DELTA_DSR)
1573 icount->dsr++;
1574 if (msr & EDGEPORT_MSR_DELTA_CD)
1575 icount->dcd++;
1576 if (msr & EDGEPORT_MSR_DELTA_RI)
1577 icount->rng++;
1578 wake_up_interruptible (&edge_port->delta_msr_wait);
1581 /* Save the new modem status */
1582 edge_port->shadow_msr = msr & 0xf0;
1584 return;
1587 static void handle_new_lsr (struct edgeport_port *edge_port, int lsr_data, __u8 lsr, __u8 data)
1589 struct async_icount *icount;
1590 __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
1592 dbg ("%s - %02x", __FUNCTION__, new_lsr);
1594 edge_port->shadow_lsr = lsr;
1596 if (new_lsr & LSR_BREAK) {
1598 * Parity and Framing errors only count if they
1599 * occur exclusive of a break being received.
1601 new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
1604 /* Place LSR data byte into Rx buffer */
1605 if (lsr_data && edge_port->port->tty) {
1606 tty_insert_flip_char(edge_port->port->tty, data, 0);
1607 tty_flip_buffer_push(edge_port->port->tty);
1610 /* update input line counters */
1611 icount = &edge_port->icount;
1612 if (new_lsr & LSR_BREAK)
1613 icount->brk++;
1614 if (new_lsr & LSR_OVER_ERR)
1615 icount->overrun++;
1616 if (new_lsr & LSR_PAR_ERR)
1617 icount->parity++;
1618 if (new_lsr & LSR_FRM_ERR)
1619 icount->frame++;
1623 static void edge_interrupt_callback (struct urb *urb, struct pt_regs *regs)
1625 struct edgeport_serial *edge_serial = (struct edgeport_serial *)urb->context;
1626 struct usb_serial_port *port;
1627 struct edgeport_port *edge_port;
1628 unsigned char *data = urb->transfer_buffer;
1629 int length = urb->actual_length;
1630 int port_number;
1631 int function;
1632 int status;
1633 __u8 lsr;
1634 __u8 msr;
1636 dbg("%s", __FUNCTION__);
1638 switch (urb->status) {
1639 case 0:
1640 /* success */
1641 break;
1642 case -ECONNRESET:
1643 case -ENOENT:
1644 case -ESHUTDOWN:
1645 /* this urb is terminated, clean up */
1646 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
1647 return;
1648 default:
1649 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
1650 goto exit;
1653 if (!length) {
1654 dbg ("%s - no data in urb", __FUNCTION__);
1655 goto exit;
1658 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __FUNCTION__, length, data);
1660 if (length != 2) {
1661 dbg ("%s - expecting packet of size 2, got %d", __FUNCTION__, length);
1662 goto exit;
1665 port_number = TIUMP_GET_PORT_FROM_CODE (data[0]);
1666 function = TIUMP_GET_FUNC_FROM_CODE (data[0]);
1667 dbg ("%s - port_number %d, function %d, info 0x%x",
1668 __FUNCTION__, port_number, function, data[1]);
1669 port = edge_serial->serial->port[port_number];
1670 edge_port = usb_get_serial_port_data(port);
1671 if (!edge_port) {
1672 dbg ("%s - edge_port not found", __FUNCTION__);
1673 return;
1675 switch (function) {
1676 case TIUMP_INTERRUPT_CODE_LSR:
1677 lsr = MapLineStatus(data[1]);
1678 if (lsr & UMP_UART_LSR_DATA_MASK) {
1679 /* Save the LSR event for bulk read completion routine */
1680 dbg ("%s - LSR Event Port %u LSR Status = %02x",
1681 __FUNCTION__, port_number, lsr);
1682 edge_port->lsr_event = 1;
1683 edge_port->lsr_mask = lsr;
1684 } else {
1685 dbg ("%s - ===== Port %d LSR Status = %02x ======",
1686 __FUNCTION__, port_number, lsr);
1687 handle_new_lsr (edge_port, 0, lsr, 0);
1689 break;
1691 case TIUMP_INTERRUPT_CODE_MSR: // MSR
1692 /* Copy MSR from UMP */
1693 msr = data[1];
1694 dbg ("%s - ===== Port %u MSR Status = %02x ======\n",
1695 __FUNCTION__, port_number, msr);
1696 handle_new_msr (edge_port, msr);
1697 break;
1699 default:
1700 dev_err (&urb->dev->dev, "%s - Unknown Interrupt code from UMP %x\n",
1701 __FUNCTION__, data[1]);
1702 break;
1706 exit:
1707 status = usb_submit_urb (urb, GFP_ATOMIC);
1708 if (status)
1709 dev_err (&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
1710 __FUNCTION__, status);
1713 static void edge_bulk_in_callback (struct urb *urb, struct pt_regs *regs)
1715 struct edgeport_port *edge_port = (struct edgeport_port *)urb->context;
1716 unsigned char *data = urb->transfer_buffer;
1717 struct tty_struct *tty;
1718 int status;
1719 int i;
1720 int port_number;
1722 dbg("%s", __FUNCTION__);
1724 if (urb->status) {
1725 dbg ("%s - nonzero read bulk status received: %d",
1726 __FUNCTION__, urb->status);
1728 if (urb->status == -EPIPE) {
1729 /* clear any problem that might have happened on this pipe */
1730 usb_clear_halt (edge_port->port->serial->dev, urb->pipe);
1731 goto exit;
1733 return;
1736 port_number = edge_port->port->number - edge_port->port->serial->minor;
1738 if (edge_port->lsr_event) {
1739 edge_port->lsr_event = 0;
1740 dbg ("%s ===== Port %u LSR Status = %02x, Data = %02x ======",
1741 __FUNCTION__, port_number, edge_port->lsr_mask, *data);
1742 handle_new_lsr (edge_port, 1, edge_port->lsr_mask, *data);
1743 /* Adjust buffer length/pointer */
1744 --urb->actual_length;
1745 ++data;
1748 tty = edge_port->port->tty;
1749 if (tty && urb->actual_length) {
1750 usb_serial_debug_data(debug, &edge_port->port->dev, __FUNCTION__, urb->actual_length, data);
1752 if (edge_port->close_pending) {
1753 dbg ("%s - close is pending, dropping data on the floor.", __FUNCTION__);
1754 } else {
1755 for (i = 0; i < urb->actual_length ; ++i) {
1756 /* if we insert more than TTY_FLIPBUF_SIZE characters,
1757 * we drop them. */
1758 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
1759 tty_flip_buffer_push(tty);
1761 /* this doesn't actually push the data through unless
1762 * tty->low_latency is set */
1763 tty_insert_flip_char(tty, data[i], 0);
1765 tty_flip_buffer_push(tty);
1767 edge_port->icount.rx += urb->actual_length;
1770 exit:
1771 /* continue always trying to read */
1772 status = usb_submit_urb (urb, GFP_ATOMIC);
1773 if (status)
1774 dev_err (&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
1775 __FUNCTION__, status);
1778 static void edge_bulk_out_callback (struct urb *urb, struct pt_regs *regs)
1780 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1781 struct tty_struct *tty;
1783 dbg ("%s - port %d", __FUNCTION__, port->number);
1785 if (urb->status) {
1786 dbg ("%s - nonzero write bulk status received: %d",
1787 __FUNCTION__, urb->status);
1789 if (urb->status == -EPIPE) {
1790 /* clear any problem that might have happened on this pipe */
1791 usb_clear_halt (port->serial->dev, urb->pipe);
1793 return;
1796 tty = port->tty;
1797 if (tty) {
1798 /* let the tty driver wakeup if it has a special write_wakeup function */
1799 tty_wakeup(tty);
1803 static int edge_open (struct usb_serial_port *port, struct file * filp)
1805 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1806 struct edgeport_serial *edge_serial;
1807 struct usb_device *dev;
1808 struct urb *urb;
1809 int port_number;
1810 int status;
1811 u16 open_settings;
1812 u8 transaction_timeout;
1814 dbg("%s - port %d", __FUNCTION__, port->number);
1816 if (edge_port == NULL)
1817 return -ENODEV;
1819 /* force low_latency on so that our tty_push actually forces the data through,
1820 otherwise it is scheduled, and with high data rates (like with OHCI) data
1821 can get lost. */
1822 if (port->tty)
1823 port->tty->low_latency = 1;
1825 port_number = port->number - port->serial->minor;
1826 switch (port_number) {
1827 case 0:
1828 edge_port->uart_base = UMPMEM_BASE_UART1;
1829 edge_port->dma_address = UMPD_OEDB1_ADDRESS;
1830 break;
1831 case 1:
1832 edge_port->uart_base = UMPMEM_BASE_UART2;
1833 edge_port->dma_address = UMPD_OEDB2_ADDRESS;
1834 break;
1835 default:
1836 dev_err (&port->dev, "Unknown port number!!!\n");
1837 return -ENODEV;
1840 dbg ("%s - port_number = %d, uart_base = %04x, dma_address = %04x",
1841 __FUNCTION__, port_number, edge_port->uart_base, edge_port->dma_address);
1843 dev = port->serial->dev;
1845 memset (&(edge_port->icount), 0x00, sizeof(edge_port->icount));
1846 init_waitqueue_head (&edge_port->delta_msr_wait);
1848 /* turn off loopback */
1849 status = TIClearLoopBack (edge_port);
1850 if (status)
1851 return status;
1853 /* set up the port settings */
1854 edge_set_termios (port, NULL);
1856 /* open up the port */
1858 /* milliseconds to timeout for DMA transfer */
1859 transaction_timeout = 2;
1861 edge_port->ump_read_timeout = max (20, ((transaction_timeout * 3) / 2) );
1863 // milliseconds to timeout for DMA transfer
1864 open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
1865 UMP_PIPE_TRANS_TIMEOUT_ENA |
1866 (transaction_timeout << 2));
1868 dbg ("%s - Sending UMPC_OPEN_PORT", __FUNCTION__);
1870 /* Tell TI to open and start the port */
1871 status = TIWriteCommandSync (dev,
1872 UMPC_OPEN_PORT,
1873 (u8)(UMPM_UART1_PORT + port_number),
1874 open_settings,
1875 NULL,
1877 if (status)
1878 return status;
1880 /* Start the DMA? */
1881 status = TIWriteCommandSync (dev,
1882 UMPC_START_PORT,
1883 (u8)(UMPM_UART1_PORT + port_number),
1885 NULL,
1887 if (status)
1888 return status;
1890 /* Clear TX and RX buffers in UMP */
1891 status = TIPurgeDataSync (port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
1892 if (status)
1893 return status;
1895 /* Read Initial MSR */
1896 status = TIReadVendorRequestSync (dev,
1897 UMPC_READ_MSR, // Request
1898 0, // wValue
1899 (__u16)(UMPM_UART1_PORT + port_number), // wIndex (Address)
1900 &edge_port->shadow_msr, // TransferBuffer
1901 1); // TransferBufferLength
1902 if (status)
1903 return status;
1905 dbg ("ShadowMSR 0x%X", edge_port->shadow_msr);
1907 edge_serial = edge_port->edge_serial;
1908 if (edge_serial->num_ports_open == 0) {
1909 /* we are the first port to be opened, let's post the interrupt urb */
1910 urb = edge_serial->serial->port[0]->interrupt_in_urb;
1911 if (!urb) {
1912 dev_err (&port->dev, "%s - no interrupt urb present, exiting\n", __FUNCTION__);
1913 return -EINVAL;
1915 urb->complete = edge_interrupt_callback;
1916 urb->context = edge_serial;
1917 urb->dev = dev;
1918 status = usb_submit_urb (urb, GFP_KERNEL);
1919 if (status) {
1920 dev_err (&port->dev, "%s - usb_submit_urb failed with value %d\n", __FUNCTION__, status);
1921 return status;
1926 * reset the data toggle on the bulk endpoints to work around bug in
1927 * host controllers where things get out of sync some times
1929 usb_clear_halt (dev, port->write_urb->pipe);
1930 usb_clear_halt (dev, port->read_urb->pipe);
1932 /* start up our bulk read urb */
1933 urb = port->read_urb;
1934 if (!urb) {
1935 dev_err (&port->dev, "%s - no read urb present, exiting\n", __FUNCTION__);
1936 return -EINVAL;
1938 urb->complete = edge_bulk_in_callback;
1939 urb->context = edge_port;
1940 urb->dev = dev;
1941 status = usb_submit_urb (urb, GFP_KERNEL);
1942 if (status) {
1943 dev_err (&port->dev, "%s - read bulk usb_submit_urb failed with value %d\n", __FUNCTION__, status);
1944 return status;
1947 ++edge_serial->num_ports_open;
1949 dbg("%s - exited", __FUNCTION__);
1951 return 0;
1954 static void edge_close (struct usb_serial_port *port, struct file * filp)
1956 struct edgeport_serial *edge_serial;
1957 struct edgeport_port *edge_port;
1958 int port_number;
1959 int status;
1961 dbg("%s - port %d", __FUNCTION__, port->number);
1963 edge_serial = usb_get_serial_data(port->serial);
1964 edge_port = usb_get_serial_port_data(port);
1965 if ((edge_serial == NULL) || (edge_port == NULL))
1966 return;
1968 /* The bulkreadcompletion routine will check
1969 * this flag and dump add read data */
1970 edge_port->close_pending = 1;
1972 /* chase the port close */
1973 TIChasePort (edge_port);
1975 usb_unlink_urb (port->read_urb);
1977 /* assuming we can still talk to the device,
1978 * send a close port command to it */
1979 dbg("%s - send umpc_close_port", __FUNCTION__);
1980 port_number = port->number - port->serial->minor;
1981 status = TIWriteCommandSync (port->serial->dev,
1982 UMPC_CLOSE_PORT,
1983 (__u8)(UMPM_UART1_PORT + port_number),
1985 NULL,
1987 --edge_port->edge_serial->num_ports_open;
1988 if (edge_port->edge_serial->num_ports_open <= 0) {
1989 /* last port is now closed, let's shut down our interrupt urb */
1990 usb_unlink_urb (port->serial->port[0]->interrupt_in_urb);
1991 edge_port->edge_serial->num_ports_open = 0;
1993 edge_port->close_pending = 0;
1995 dbg("%s - exited", __FUNCTION__);
1998 static int edge_write (struct usb_serial_port *port, int from_user, const unsigned char *data, int count)
2000 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2001 int result;
2003 dbg("%s - port %d", __FUNCTION__, port->number);
2005 if (count == 0) {
2006 dbg("%s - write request of 0 bytes", __FUNCTION__);
2007 return 0;
2010 if (edge_port == NULL)
2011 return -ENODEV;
2012 if (edge_port->close_pending == 1)
2013 return -ENODEV;
2015 if (port->write_urb->status == -EINPROGRESS) {
2016 dbg ("%s - already writing", __FUNCTION__);
2017 return 0;
2020 count = min (count, port->bulk_out_size);
2022 if (from_user) {
2023 if (copy_from_user(port->write_urb->transfer_buffer, data, count))
2024 return -EFAULT;
2025 } else {
2026 memcpy (port->write_urb->transfer_buffer, data, count);
2029 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, port->write_urb->transfer_buffer);
2031 /* set up our urb */
2032 usb_fill_bulk_urb (port->write_urb, port->serial->dev,
2033 usb_sndbulkpipe (port->serial->dev,
2034 port->bulk_out_endpointAddress),
2035 port->write_urb->transfer_buffer, count,
2036 edge_bulk_out_callback,
2037 port);
2039 /* send the data out the bulk port */
2040 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2041 if (result)
2042 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
2043 else
2044 result = count;
2046 if (result > 0)
2047 edge_port->icount.tx += count;
2049 return result;
2052 static int edge_write_room (struct usb_serial_port *port)
2054 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2055 int room = 0;
2057 dbg("%s", __FUNCTION__);
2059 if (edge_port == NULL)
2060 return -ENODEV;
2061 if (edge_port->close_pending == 1)
2062 return -ENODEV;
2064 dbg("%s - port %d", __FUNCTION__, port->number);
2066 if (port->write_urb->status != -EINPROGRESS)
2067 room = port->bulk_out_size;
2069 dbg("%s - returns %d", __FUNCTION__, room);
2070 return room;
2073 static int edge_chars_in_buffer (struct usb_serial_port *port)
2075 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2076 int chars = 0;
2078 dbg("%s", __FUNCTION__);
2080 if (edge_port == NULL)
2081 return -ENODEV;
2082 if (edge_port->close_pending == 1)
2083 return -ENODEV;
2085 dbg("%s - port %d", __FUNCTION__, port->number);
2087 if (port->write_urb->status == -EINPROGRESS)
2088 chars = port->write_urb->transfer_buffer_length;
2090 dbg ("%s - returns %d", __FUNCTION__, chars);
2091 return chars;
2094 static void edge_throttle (struct usb_serial_port *port)
2096 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2097 struct tty_struct *tty;
2098 int status;
2100 dbg("%s - port %d", __FUNCTION__, port->number);
2102 if (edge_port == NULL)
2103 return;
2105 tty = port->tty;
2106 if (!tty) {
2107 dbg ("%s - no tty available", __FUNCTION__);
2108 return;
2110 /* if we are implementing XON/XOFF, send the stop character */
2111 if (I_IXOFF(tty)) {
2112 unsigned char stop_char = STOP_CHAR(tty);
2113 status = edge_write (port, 0, &stop_char, 1);
2114 if (status <= 0) {
2115 return;
2119 /* if we are implementing RTS/CTS, toggle that line */
2120 if (tty->termios->c_cflag & CRTSCTS) {
2121 status = TIClearRts (edge_port);
2124 usb_unlink_urb (port->read_urb);
2127 static void edge_unthrottle (struct usb_serial_port *port)
2129 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2130 struct tty_struct *tty;
2131 int status;
2133 dbg("%s - port %d", __FUNCTION__, port->number);
2135 if (edge_port == NULL)
2136 return;
2138 tty = port->tty;
2139 if (!tty) {
2140 dbg ("%s - no tty available", __FUNCTION__);
2141 return;
2144 /* if we are implementing XON/XOFF, send the start character */
2145 if (I_IXOFF(tty)) {
2146 unsigned char start_char = START_CHAR(tty);
2147 status = edge_write (port, 0, &start_char, 1);
2148 if (status <= 0) {
2149 return;
2153 /* if we are implementing RTS/CTS, toggle that line */
2154 if (tty->termios->c_cflag & CRTSCTS) {
2155 status = TISetRts (edge_port);
2158 port->read_urb->dev = port->serial->dev;
2159 status = usb_submit_urb (port->read_urb, GFP_ATOMIC);
2160 if (status) {
2161 dev_err (&port->dev, "%s - usb_submit_urb failed with value %d\n", __FUNCTION__, status);
2166 static void change_port_settings (struct edgeport_port *edge_port, struct termios *old_termios)
2168 struct ump_uart_config *config;
2169 struct tty_struct *tty;
2170 int baud;
2171 int round;
2172 unsigned cflag;
2173 int status;
2174 int port_number = edge_port->port->number - edge_port->port->serial->minor;
2176 dbg("%s - port %d", __FUNCTION__, edge_port->port->number);
2178 tty = edge_port->port->tty;
2179 if ((!tty) ||
2180 (!tty->termios)) {
2181 dbg("%s - no tty structures", __FUNCTION__);
2182 return;
2185 config = kmalloc (sizeof (*config), GFP_KERNEL);
2186 if (!config) {
2187 dev_err (&edge_port->port->dev, "%s - out of memory\n", __FUNCTION__);
2188 return;
2191 cflag = tty->termios->c_cflag;
2193 config->wFlags = 0;
2195 /* These flags must be set */
2196 config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
2197 config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
2198 config->bUartMode = 0;
2200 switch (cflag & CSIZE) {
2201 case CS5:
2202 config->bDataBits = UMP_UART_CHAR5BITS;
2203 dbg ("%s - data bits = 5", __FUNCTION__);
2204 break;
2205 case CS6:
2206 config->bDataBits = UMP_UART_CHAR6BITS;
2207 dbg ("%s - data bits = 6", __FUNCTION__);
2208 break;
2209 case CS7:
2210 config->bDataBits = UMP_UART_CHAR7BITS;
2211 dbg ("%s - data bits = 7", __FUNCTION__);
2212 break;
2213 default:
2214 case CS8:
2215 config->bDataBits = UMP_UART_CHAR8BITS;
2216 dbg ("%s - data bits = 8", __FUNCTION__);
2217 break;
2220 if (cflag & PARENB) {
2221 if (cflag & PARODD) {
2222 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2223 config->bParity = UMP_UART_ODDPARITY;
2224 dbg("%s - parity = odd", __FUNCTION__);
2225 } else {
2226 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2227 config->bParity = UMP_UART_EVENPARITY;
2228 dbg("%s - parity = even", __FUNCTION__);
2230 } else {
2231 config->bParity = UMP_UART_NOPARITY;
2232 dbg("%s - parity = none", __FUNCTION__);
2235 if (cflag & CSTOPB) {
2236 config->bStopBits = UMP_UART_STOPBIT2;
2237 dbg("%s - stop bits = 2", __FUNCTION__);
2238 } else {
2239 config->bStopBits = UMP_UART_STOPBIT1;
2240 dbg("%s - stop bits = 1", __FUNCTION__);
2243 /* figure out the flow control settings */
2244 if (cflag & CRTSCTS) {
2245 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
2246 config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
2247 dbg("%s - RTS/CTS is enabled", __FUNCTION__);
2248 } else {
2249 dbg("%s - RTS/CTS is disabled", __FUNCTION__);
2252 /* if we are implementing XON/XOFF, set the start and stop character in the device */
2253 if (I_IXOFF(tty) || I_IXON(tty)) {
2254 config->cXon = START_CHAR(tty);
2255 config->cXoff = STOP_CHAR(tty);
2257 /* if we are implementing INBOUND XON/XOFF */
2258 if (I_IXOFF(tty)) {
2259 config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
2260 dbg ("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2261 __FUNCTION__, config->cXon, config->cXoff);
2262 } else {
2263 dbg ("%s - INBOUND XON/XOFF is disabled", __FUNCTION__);
2266 /* if we are implementing OUTBOUND XON/XOFF */
2267 if (I_IXON(tty)) {
2268 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
2269 dbg ("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2270 __FUNCTION__, config->cXon, config->cXoff);
2271 } else {
2272 dbg ("%s - OUTBOUND XON/XOFF is disabled", __FUNCTION__);
2276 /* Round the baud rate */
2277 baud = tty_get_baud_rate(tty);
2278 if (!baud) {
2279 /* pick a default, any default... */
2280 baud = 9600;
2282 config->wBaudRate = (__u16)(461550L / baud);
2283 round = 4615500L / baud;
2284 if ((round - (config->wBaudRate * 10)) >= 5)
2285 config->wBaudRate++;
2287 dbg ("%s - baud rate = %d, wBaudRate = %d", __FUNCTION__, baud, config->wBaudRate);
2289 dbg ("wBaudRate: %d", (int)(461550L / config->wBaudRate));
2290 dbg ("wFlags: 0x%x", config->wFlags);
2291 dbg ("bDataBits: %d", config->bDataBits);
2292 dbg ("bParity: %d", config->bParity);
2293 dbg ("bStopBits: %d", config->bStopBits);
2294 dbg ("cXon: %d", config->cXon);
2295 dbg ("cXoff: %d", config->cXoff);
2296 dbg ("bUartMode: %d", config->bUartMode);
2298 /* move the word values into big endian mode */
2299 cpu_to_be16s (&config->wFlags);
2300 cpu_to_be16s (&config->wBaudRate);
2302 status = TIWriteCommandSync (edge_port->port->serial->dev,
2303 UMPC_SET_CONFIG,
2304 (__u8)(UMPM_UART1_PORT + port_number),
2306 (__u8 *)config,
2307 sizeof(*config));
2308 if (status) {
2309 dbg ("%s - error %d when trying to write config to device",
2310 __FUNCTION__, status);
2313 kfree (config);
2315 return;
2318 static void edge_set_termios (struct usb_serial_port *port, struct termios *old_termios)
2320 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2321 struct tty_struct *tty = port->tty;
2322 unsigned int cflag;
2324 if (!port->tty || !port->tty->termios) {
2325 dbg ("%s - no tty or termios", __FUNCTION__);
2326 return;
2329 cflag = tty->termios->c_cflag;
2330 /* check that they really want us to change something */
2331 if (old_termios) {
2332 if ((cflag == old_termios->c_cflag) &&
2333 (RELEVANT_IFLAG(tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
2334 dbg ("%s - nothing to change", __FUNCTION__);
2335 return;
2339 dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
2340 tty->termios->c_cflag,
2341 RELEVANT_IFLAG(tty->termios->c_iflag));
2342 if (old_termios) {
2343 dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
2344 old_termios->c_cflag,
2345 RELEVANT_IFLAG(old_termios->c_iflag));
2348 dbg("%s - port %d", __FUNCTION__, port->number);
2350 if (edge_port == NULL)
2351 return;
2353 /* change the port settings to the new ones specified */
2354 change_port_settings (edge_port, old_termios);
2356 return;
2359 static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear)
2361 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2362 unsigned int mcr;
2364 dbg("%s - port %d", __FUNCTION__, port->number);
2366 mcr = edge_port->shadow_mcr;
2367 if (set & TIOCM_RTS)
2368 mcr |= MCR_RTS;
2369 if (set & TIOCM_DTR)
2370 mcr |= MCR_DTR;
2371 if (set & TIOCM_LOOP)
2372 mcr |= MCR_LOOPBACK;
2374 if (clear & TIOCM_RTS)
2375 mcr &= ~MCR_RTS;
2376 if (clear & TIOCM_DTR)
2377 mcr &= ~MCR_DTR;
2378 if (clear & TIOCM_LOOP)
2379 mcr &= ~MCR_LOOPBACK;
2381 edge_port->shadow_mcr = mcr;
2383 TIRestoreMCR (edge_port, mcr);
2385 return 0;
2388 static int edge_tiocmget(struct usb_serial_port *port, struct file *file)
2390 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2391 unsigned int result = 0;
2392 unsigned int msr;
2393 unsigned int mcr;
2395 dbg("%s - port %d", __FUNCTION__, port->number);
2397 msr = edge_port->shadow_msr;
2398 mcr = edge_port->shadow_mcr;
2399 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
2400 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
2401 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
2402 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
2403 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
2404 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
2407 dbg("%s -- %x", __FUNCTION__, result);
2409 return result;
2412 static int get_serial_info (struct edgeport_port *edge_port, struct serial_struct __user *retinfo)
2414 struct serial_struct tmp;
2416 if (!retinfo)
2417 return -EFAULT;
2419 memset(&tmp, 0, sizeof(tmp));
2421 tmp.type = PORT_16550A;
2422 tmp.line = edge_port->port->serial->minor;
2423 tmp.port = edge_port->port->number;
2424 tmp.irq = 0;
2425 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2426 tmp.xmit_fifo_size = edge_port->port->bulk_out_size;
2427 tmp.baud_base = 9600;
2428 tmp.close_delay = 5*HZ;
2429 tmp.closing_wait = 30*HZ;
2430 // tmp.custom_divisor = state->custom_divisor;
2431 // tmp.hub6 = state->hub6;
2432 // tmp.io_type = state->io_type;
2435 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2436 return -EFAULT;
2437 return 0;
2440 static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
2442 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2443 struct async_icount cnow;
2444 struct async_icount cprev;
2446 dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
2448 switch (cmd) {
2449 case TIOCINQ:
2450 dbg("%s - (%d) TIOCINQ", __FUNCTION__, port->number);
2451 // return get_number_bytes_avail(edge_port, (unsigned int *) arg);
2452 break;
2454 case TIOCSERGETLSR:
2455 dbg("%s - (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
2456 // return get_lsr_info(edge_port, (unsigned int *) arg);
2457 break;
2459 case TIOCGSERIAL:
2460 dbg("%s - (%d) TIOCGSERIAL", __FUNCTION__, port->number);
2461 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
2462 break;
2464 case TIOCSSERIAL:
2465 dbg("%s - (%d) TIOCSSERIAL", __FUNCTION__, port->number);
2466 break;
2468 case TIOCMIWAIT:
2469 dbg("%s - (%d) TIOCMIWAIT", __FUNCTION__, port->number);
2470 cprev = edge_port->icount;
2471 while (1) {
2472 interruptible_sleep_on(&edge_port->delta_msr_wait);
2473 /* see if a signal did it */
2474 if (signal_pending(current))
2475 return -ERESTARTSYS;
2476 cnow = edge_port->icount;
2477 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2478 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2479 return -EIO; /* no change => error */
2480 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2481 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2482 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2483 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
2484 return 0;
2486 cprev = cnow;
2488 /* not reached */
2489 break;
2491 case TIOCGICOUNT:
2492 dbg ("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__,
2493 port->number, edge_port->icount.rx, edge_port->icount.tx);
2494 if (copy_to_user((void __user *)arg, &edge_port->icount, sizeof(edge_port->icount)))
2495 return -EFAULT;
2496 return 0;
2499 return -ENOIOCTLCMD;
2502 static void edge_break (struct usb_serial_port *port, int break_state)
2504 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2505 int status;
2507 dbg ("%s - state = %d", __FUNCTION__, break_state);
2509 /* chase the port close */
2510 TIChasePort (edge_port);
2512 if (break_state == -1) {
2513 status = TISetBreak (edge_port);
2514 } else {
2515 status = TIClearBreak (edge_port);
2517 if (status) {
2518 dbg ("%s - error %d sending break set/clear command.",
2519 __FUNCTION__, status);
2523 static int edge_startup (struct usb_serial *serial)
2525 struct edgeport_serial *edge_serial;
2526 struct edgeport_port *edge_port;
2527 struct usb_device *dev;
2528 int status;
2529 int i;
2531 dev = serial->dev;
2533 /* create our private serial structure */
2534 edge_serial = kmalloc (sizeof(struct edgeport_serial), GFP_KERNEL);
2535 if (edge_serial == NULL) {
2536 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
2537 return -ENOMEM;
2539 memset (edge_serial, 0, sizeof(struct edgeport_serial));
2540 edge_serial->serial = serial;
2541 usb_set_serial_data(serial, edge_serial);
2543 status = TIDownloadFirmware (edge_serial);
2544 if (status) {
2545 kfree (edge_serial);
2546 return status;
2549 /* set up our port private structures */
2550 for (i = 0; i < serial->num_ports; ++i) {
2551 edge_port = kmalloc (sizeof(struct edgeport_port), GFP_KERNEL);
2552 if (edge_port == NULL) {
2553 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
2554 return -ENOMEM;
2556 memset (edge_port, 0, sizeof(struct edgeport_port));
2557 edge_port->port = serial->port[i];
2558 edge_port->edge_serial = edge_serial;
2559 usb_set_serial_port_data(serial->port[i], edge_port);
2562 return 0;
2565 static void edge_shutdown (struct usb_serial *serial)
2567 int i;
2569 dbg ("%s", __FUNCTION__);
2571 for (i=0; i < serial->num_ports; ++i) {
2572 kfree (usb_get_serial_port_data(serial->port[i]));
2573 usb_set_serial_port_data(serial->port[i], NULL);
2575 kfree (usb_get_serial_data(serial));
2576 usb_set_serial_data(serial, NULL);
2580 static struct usb_serial_device_type edgeport_1port_device = {
2581 .owner = THIS_MODULE,
2582 .name = "Edgeport TI 1 port adapter",
2583 .short_name = "edgeport_ti_1",
2584 .id_table = edgeport_1port_id_table,
2585 .num_interrupt_in = 1,
2586 .num_bulk_in = 1,
2587 .num_bulk_out = 1,
2588 .num_ports = 1,
2589 .open = edge_open,
2590 .close = edge_close,
2591 .throttle = edge_throttle,
2592 .unthrottle = edge_unthrottle,
2593 .attach = edge_startup,
2594 .shutdown = edge_shutdown,
2595 .ioctl = edge_ioctl,
2596 .set_termios = edge_set_termios,
2597 .tiocmget = edge_tiocmget,
2598 .tiocmset = edge_tiocmset,
2599 .write = edge_write,
2600 .write_room = edge_write_room,
2601 .chars_in_buffer = edge_chars_in_buffer,
2602 .break_ctl = edge_break,
2605 static struct usb_serial_device_type edgeport_2port_device = {
2606 .owner = THIS_MODULE,
2607 .name = "Edgeport TI 2 port adapter",
2608 .short_name = "edgeport_ti_2",
2609 .id_table = edgeport_2port_id_table,
2610 .num_interrupt_in = 1,
2611 .num_bulk_in = 2,
2612 .num_bulk_out = 2,
2613 .num_ports = 2,
2614 .open = edge_open,
2615 .close = edge_close,
2616 .throttle = edge_throttle,
2617 .unthrottle = edge_unthrottle,
2618 .attach = edge_startup,
2619 .shutdown = edge_shutdown,
2620 .ioctl = edge_ioctl,
2621 .set_termios = edge_set_termios,
2622 .tiocmget = edge_tiocmget,
2623 .tiocmset = edge_tiocmset,
2624 .write = edge_write,
2625 .write_room = edge_write_room,
2626 .chars_in_buffer = edge_chars_in_buffer,
2627 .break_ctl = edge_break,
2631 static int __init edgeport_init(void)
2633 int retval;
2634 retval = usb_serial_register(&edgeport_1port_device);
2635 if (retval)
2636 goto failed_1port_device_register;
2637 retval = usb_serial_register(&edgeport_2port_device);
2638 if (retval)
2639 goto failed_2port_device_register;
2640 retval = usb_register(&io_driver);
2641 if (retval)
2642 goto failed_usb_register;
2643 info(DRIVER_DESC " " DRIVER_VERSION);
2644 return 0;
2645 failed_usb_register:
2646 usb_serial_deregister(&edgeport_2port_device);
2647 failed_2port_device_register:
2648 usb_serial_deregister(&edgeport_1port_device);
2649 failed_1port_device_register:
2650 return retval;
2653 static void __exit edgeport_exit (void)
2655 usb_deregister (&io_driver);
2656 usb_serial_deregister (&edgeport_1port_device);
2657 usb_serial_deregister (&edgeport_2port_device);
2660 module_init(edgeport_init);
2661 module_exit(edgeport_exit);
2663 /* Module information */
2664 MODULE_AUTHOR(DRIVER_AUTHOR);
2665 MODULE_DESCRIPTION(DRIVER_DESC);
2666 MODULE_LICENSE("GPL");
2668 module_param(debug, bool, S_IRUGO | S_IWUSR);
2669 MODULE_PARM_DESC(debug, "Debug enabled or not");
2671 module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
2672 MODULE_PARM_DESC(ignore_cpu_rev, "Ignore the cpu revision when connecting to a device");