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 EP/21 EP/22 EP/221 EP/42 EP/421 WATCHPORT
15 * For questions or problems with this driver, contact Inside Out
16 * Networks technical support, or Peter Berger <pberger@brimson.com>,
17 * or Al Borchers <alborchers@steinerpoint.com>.
21 * July 11, 2002 Removed 4 port device structure since all TI UMP
22 * chips have only 2 ports
23 * David Iacovelli (davidi@ionetworks.com)
27 #include <linux/config.h>
28 #include <linux/kernel.h>
29 #include <linux/jiffies.h>
30 #include <linux/errno.h>
31 #include <linux/init.h>
32 #include <linux/slab.h>
33 #include <linux/tty.h>
34 #include <linux/tty_driver.h>
35 #include <linux/tty_flip.h>
36 #include <linux/module.h>
37 #include <linux/spinlock.h>
38 #include <linux/serial.h>
39 #include <linux/ioctl.h>
40 #include <asm/uaccess.h>
41 #include <asm/semaphore.h>
42 #include <linux/usb.h>
44 #include "usb-serial.h"
46 #include "io_usbvend.h"
52 #define DRIVER_VERSION "v0.7"
53 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
54 #define DRIVER_DESC "Edgeport USB Serial Driver"
57 /* firmware image code */
58 #define IMAGE_VERSION_NAME PagableOperationalCodeImageVersion
59 #define IMAGE_ARRAY_NAME PagableOperationalCodeImage
60 #define IMAGE_SIZE PagableOperationalCodeSize
61 #include "io_fw_down3.h" /* Define array OperationalCodeImage[] */
63 #define EPROM_PAGE_SIZE 64
66 struct edgeport_uart_buf_desc {
67 __u32 count; // Number of bytes currently in buffer
70 /* different hardware types */
71 #define HARDWARE_TYPE_930 0
72 #define HARDWARE_TYPE_TIUMP 1
74 // IOCTL_PRIVATE_TI_GET_MODE Definitions
75 #define TI_MODE_CONFIGURING 0 // Device has not entered start device
76 #define TI_MODE_BOOT 1 // Staying in boot mode
77 #define TI_MODE_DOWNLOAD 2 // Made it to download mode
78 #define TI_MODE_TRANSITIONING 3 // Currently in boot mode but transitioning to download mode
81 #define EDGE_READ_URB_RUNNING 0
82 #define EDGE_READ_URB_STOPPING 1
83 #define EDGE_READ_URB_STOPPED 2
85 #define EDGE_LOW_LATENCY 1
86 #define EDGE_CLOSING_WAIT 4000 /* in .01 sec */
88 #define EDGE_OUT_BUF_SIZE 1024
91 /* Product information read from the Edgeport */
94 int TiMode; // Current TI Mode
95 __u8 hardware_type; // Type of hardware
96 } __attribute__((packed));
100 unsigned int buf_size;
106 struct edgeport_port {
113 __u32 ump_read_timeout; /* Number of miliseconds the UMP will
114 wait without data before completing
119 struct edgeport_uart_buf_desc tx;
120 struct async_icount icount;
121 wait_queue_head_t delta_msr_wait; /* for handling sleeping while
122 waiting for msr change to
124 struct edgeport_serial *edge_serial;
125 struct usb_serial_port *port;
126 __u8 bUartMode; /* Port type, 0: RS232, etc. */
128 int ep_read_urb_state;
129 int ep_write_urb_in_use;
130 struct edge_buf *ep_out_buf;
133 struct edgeport_serial {
134 struct product_info product_info;
135 u8 TI_I2C_Type; // Type of I2C in UMP
136 u8 TiReadI2C; // Set to TRUE if we have read the I2c in Boot Mode
137 struct semaphore es_sem;
139 struct usb_serial *serial;
143 /* Devices that this driver supports */
144 static struct usb_device_id edgeport_1port_id_table [] = {
145 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
146 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
147 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
148 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
149 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
150 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
151 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
152 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
153 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
154 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
155 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
156 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
157 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
158 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
159 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
160 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
164 static struct usb_device_id edgeport_2port_id_table [] = {
165 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
166 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
167 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
168 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
169 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
170 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
171 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
172 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
173 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
174 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
175 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
176 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
177 // The 4-port shows up as two 2-port devices
178 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
182 /* Devices that this driver supports */
183 static struct usb_device_id id_table_combined [] = {
184 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
185 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
186 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
187 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
188 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
189 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
190 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
191 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
192 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
193 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
194 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
195 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
196 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
197 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
198 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
199 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
200 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
201 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
202 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
203 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
204 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
205 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
206 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
207 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
208 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
209 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
210 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
211 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
212 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
216 MODULE_DEVICE_TABLE (usb, id_table_combined);
218 static struct usb_driver io_driver = {
219 .owner = THIS_MODULE,
221 .probe = usb_serial_probe,
222 .disconnect = usb_serial_disconnect,
223 .id_table = id_table_combined,
228 static struct EDGE_FIRMWARE_VERSION_INFO OperationalCodeImageVersion;
232 static int TIStayInBootMode = 0;
233 static int low_latency = EDGE_LOW_LATENCY;
234 static int closing_wait = EDGE_CLOSING_WAIT;
235 static int ignore_cpu_rev = 0;
238 static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length);
240 static void stop_read(struct edgeport_port *edge_port);
241 static int restart_read(struct edgeport_port *edge_port);
243 static void edge_set_termios (struct usb_serial_port *port, struct termios *old_termios);
244 static void edge_send(struct usb_serial_port *port);
246 /* circular buffer */
247 static struct edge_buf *edge_buf_alloc(unsigned int size);
248 static void edge_buf_free(struct edge_buf *eb);
249 static void edge_buf_clear(struct edge_buf *eb);
250 static unsigned int edge_buf_data_avail(struct edge_buf *eb);
251 static unsigned int edge_buf_space_avail(struct edge_buf *eb);
252 static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
254 static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
258 static int TIReadVendorRequestSync (struct usb_device *dev,
267 status = usb_control_msg (dev,
268 usb_rcvctrlpipe(dev, 0),
280 if (status != size) {
281 dbg ("%s - wanted to write %d, but only wrote %d",
282 __FUNCTION__, size, status);
288 static int TISendVendorRequestSync (struct usb_device *dev,
297 status = usb_control_msg (dev,
298 usb_sndctrlpipe(dev, 0),
310 if (status != size) {
311 dbg ("%s - wanted to write %d, but only wrote %d",
312 __FUNCTION__, size, status);
318 static int TIWriteCommandSync (struct usb_device *dev, __u8 command,
319 __u8 moduleid, __u16 value, u8 *data,
322 return TISendVendorRequestSync (dev,
326 data, // TransferBuffer
327 size); // TransferBufferLength
331 /* clear tx/rx buffers and fifo in TI UMP */
332 static int TIPurgeDataSync (struct usb_serial_port *port, __u16 mask)
334 int port_number = port->number - port->serial->minor;
336 dbg ("%s - port %d, mask %x", __FUNCTION__, port_number, mask);
338 return TIWriteCommandSync (port->serial->dev,
340 (__u8)(UMPM_UART1_PORT + port_number),
347 * TIReadDownloadMemory - Read edgeport memory from TI chip
348 * @dev: usb device pointer
349 * @start_address: Device CPU address at which to read
350 * @length: Length of above data
351 * @address_type: Can read both XDATA and I2C
352 * @buffer: pointer to input data buffer
354 static int TIReadDownloadMemory(struct usb_device *dev, int start_address,
355 int length, __u8 address_type, __u8 *buffer)
359 __be16 be_start_address;
361 dbg ("%s - @ %x for %d", __FUNCTION__, start_address, length);
363 /* Read in blocks of 64 bytes
364 * (TI firmware can't handle more than 64 byte reads)
370 read_length = (__u8)length;
372 if (read_length > 1) {
373 dbg ("%s - @ %x for %d", __FUNCTION__,
374 start_address, read_length);
376 be_start_address = cpu_to_be16 (start_address);
377 status = TIReadVendorRequestSync (dev,
378 UMPC_MEMORY_READ, // Request
379 (__u16)address_type, // wValue (Address type)
380 (__force __u16)be_start_address, // wIndex (Address to read)
381 buffer, // TransferBuffer
382 read_length); // TransferBufferLength
385 dbg ("%s - ERROR %x", __FUNCTION__, status);
389 if (read_length > 1) {
390 usb_serial_debug_data(debug, &dev->dev, __FUNCTION__,
391 read_length, buffer);
394 /* Update pointers/length */
395 start_address += read_length;
396 buffer += read_length;
397 length -= read_length;
403 static int TIReadRam (struct usb_device *dev, int start_address, int length, __u8 *buffer)
405 return TIReadDownloadMemory (dev,
408 DTK_ADDR_SPACE_XDATA,
412 /* Read edgeport memory to a given block */
413 static int TIReadBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 * buffer)
418 for (i=0; i< length; i++) {
419 status = TIReadVendorRequestSync (serial->serial->dev,
420 UMPC_MEMORY_READ, // Request
421 serial->TI_I2C_Type, // wValue (Address type)
422 (__u16)(start_address+i), // wIndex
423 &buffer[i], // TransferBuffer
424 0x01); // TransferBufferLength
426 dbg ("%s - ERROR %x", __FUNCTION__, status);
431 dbg ("%s - start_address = %x, length = %d", __FUNCTION__, start_address, length);
432 usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, length, buffer);
434 serial->TiReadI2C = 1;
439 /* Write given block to TI EPROM memory */
440 static int TIWriteBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
446 /* Must do a read before write */
447 if (!serial->TiReadI2C) {
448 status = TIReadBootMemory(serial, 0, 1, &temp);
453 for (i=0; i < length; ++i) {
454 status = TISendVendorRequestSync (serial->serial->dev,
455 UMPC_MEMORY_WRITE, // Request
457 (__u16)(i+start_address), // wIndex
458 NULL, // TransferBuffer
459 0); // TransferBufferLength
464 dbg ("%s - start_sddr = %x, length = %d", __FUNCTION__, start_address, length);
465 usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, length, buffer);
471 /* Write edgeport I2C memory to TI chip */
472 static int TIWriteDownloadI2C (struct edgeport_serial *serial, int start_address, int length, __u8 address_type, __u8 *buffer)
476 __be16 be_start_address;
478 /* We can only send a maximum of 1 aligned byte page at a time */
480 /* calulate the number of bytes left in the first page */
481 write_length = EPROM_PAGE_SIZE - (start_address & (EPROM_PAGE_SIZE - 1));
483 if (write_length > length)
484 write_length = length;
486 dbg ("%s - BytesInFirstPage Addr = %x, length = %d", __FUNCTION__, start_address, write_length);
487 usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, write_length, buffer);
489 /* Write first page */
490 be_start_address = cpu_to_be16 (start_address);
491 status = TISendVendorRequestSync (serial->serial->dev,
492 UMPC_MEMORY_WRITE, // Request
493 (__u16)address_type, // wValue
494 (__force __u16)be_start_address, // wIndex
495 buffer, // TransferBuffer
498 dbg ("%s - ERROR %d", __FUNCTION__, status);
502 length -= write_length;
503 start_address += write_length;
504 buffer += write_length;
506 /* We should be aligned now -- can write max page size bytes at a time */
508 if (length > EPROM_PAGE_SIZE)
509 write_length = EPROM_PAGE_SIZE;
511 write_length = length;
513 dbg ("%s - Page Write Addr = %x, length = %d", __FUNCTION__, start_address, write_length);
514 usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, write_length, buffer);
516 /* Write next page */
517 be_start_address = cpu_to_be16 (start_address);
518 status = TISendVendorRequestSync (serial->serial->dev,
519 UMPC_MEMORY_WRITE, // Request
520 (__u16)address_type, // wValue
521 (__force __u16)be_start_address, // wIndex
522 buffer, // TransferBuffer
523 write_length); // TransferBufferLength
525 dev_err (&serial->serial->dev->dev, "%s - ERROR %d\n", __FUNCTION__, status);
529 length -= write_length;
530 start_address += write_length;
531 buffer += write_length;
536 /* Examine the UMP DMA registers and LSR
538 * Check the MSBit of the X and Y DMA byte count registers.
539 * A zero in this bit indicates that the TX DMA buffers are empty
540 * then check the TX Empty bit in the UART.
542 static int TIIsTxActive (struct edgeport_port *port)
545 struct out_endpoint_desc_block *oedb;
549 oedb = kmalloc (sizeof (* oedb), GFP_KERNEL);
551 dev_err (&port->port->dev, "%s - out of memory\n", __FUNCTION__);
555 lsr = kmalloc (1, GFP_KERNEL); /* Sigh, that's right, just one byte,
556 as not all platforms can do DMA
562 /* Read the DMA Count Registers */
563 status = TIReadRam (port->port->serial->dev,
569 goto exit_is_tx_active;
571 dbg ("%s - XByteCount 0x%X", __FUNCTION__, oedb->XByteCount);
574 status = TIReadRam (port->port->serial->dev,
575 port->uart_base + UMPMEM_OFFS_UART_LSR,
580 goto exit_is_tx_active;
581 dbg ("%s - LSR = 0x%X", __FUNCTION__, *lsr);
583 /* If either buffer has data or we are transmitting then return TRUE */
584 if ((oedb->XByteCount & 0x80 ) != 0 )
587 if ((*lsr & UMP_UART_LSR_TX_MASK ) == 0 )
590 /* We return Not Active if we get any kind of error */
592 dbg ("%s - return %d", __FUNCTION__, bytes_left );
599 static void TIChasePort(struct edgeport_port *port, unsigned long timeout, int flush)
602 struct tty_struct *tty = port->port->tty;
607 timeout = (HZ*EDGE_CLOSING_WAIT)/100;
609 /* wait for data to drain from the buffer */
610 spin_lock_irqsave(&port->ep_lock, flags);
611 init_waitqueue_entry(&wait, current);
612 add_wait_queue(&tty->write_wait, &wait);
614 set_current_state(TASK_INTERRUPTIBLE);
615 if (edge_buf_data_avail(port->ep_out_buf) == 0
616 || timeout == 0 || signal_pending(current)
617 || !usb_get_intfdata(port->port->serial->interface)) /* disconnect */
619 spin_unlock_irqrestore(&port->ep_lock, flags);
620 timeout = schedule_timeout(timeout);
621 spin_lock_irqsave(&port->ep_lock, flags);
623 set_current_state(TASK_RUNNING);
624 remove_wait_queue(&tty->write_wait, &wait);
626 edge_buf_clear(port->ep_out_buf);
627 spin_unlock_irqrestore(&port->ep_lock, flags);
629 /* wait for data to drain from the device */
631 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
632 && usb_get_intfdata(port->port->serial->interface)) { /* not disconnected */
633 if (!TIIsTxActive(port))
639 if (!usb_get_intfdata(port->port->serial->interface))
642 /* wait one more character time, based on baud rate */
643 /* (TIIsTxActive doesn't seem to wait for the last byte) */
644 if ((baud_rate=port->baud_rate) == 0)
646 msleep(max(1,(10000+baud_rate-1)/baud_rate));
649 static int TIChooseConfiguration (struct usb_device *dev)
651 // There may be multiple configurations on this device, in which case
652 // we would need to read and parse all of them to find out which one
653 // we want. However, we just support one config at this point,
654 // configuration # 1, which is Config Descriptor 0.
656 dbg ("%s - Number of Interfaces = %d", __FUNCTION__, dev->config->desc.bNumInterfaces);
657 dbg ("%s - MAX Power = %d", __FUNCTION__, dev->config->desc.bMaxPower*2);
659 if (dev->config->desc.bNumInterfaces != 1) {
660 dev_err (&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __FUNCTION__);
667 static int TIReadRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
671 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
672 status = TIReadDownloadMemory (serial->serial->dev,
678 status = TIReadBootMemory (serial,
687 static int TIWriteRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
689 if (serial->product_info.TiMode == TI_MODE_BOOT)
690 return TIWriteBootMemory (serial,
695 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
696 return TIWriteDownloadI2C (serial,
707 /* Read a descriptor header from I2C based on type */
708 static int TIGetDescriptorAddress (struct edgeport_serial *serial, int desc_type, struct ti_i2c_desc *rom_desc)
713 /* Search for requested descriptor in I2C */
716 status = TIReadRom (serial,
718 sizeof(struct ti_i2c_desc),
723 if (rom_desc->Type == desc_type)
724 return start_address;
726 start_address = start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size;
728 } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
733 /* Validate descriptor checksum */
734 static int ValidChecksum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
739 for (i=0; i < rom_desc->Size; i++) {
740 cs = (__u8)(cs + buffer[i]);
742 if (cs != rom_desc->CheckSum) {
743 dbg ("%s - Mismatch %x - %x", __FUNCTION__, rom_desc->CheckSum, cs);
749 /* Make sure that the I2C image is good */
750 static int TiValidateI2cImage (struct edgeport_serial *serial)
752 struct device *dev = &serial->serial->dev->dev;
754 struct ti_i2c_desc *rom_desc;
755 int start_address = 2;
759 rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
761 dev_err (dev, "%s - out of memory\n", __FUNCTION__);
764 buffer = kmalloc (TI_MAX_I2C_SIZE, GFP_KERNEL);
766 dev_err (dev, "%s - out of memory when allocating buffer\n", __FUNCTION__);
771 // Read the first byte (Signature0) must be 0x52 or 0x10
772 status = TIReadRom (serial, 0, 1, buffer);
774 goto ExitTiValidateI2cImage;
776 if (*buffer != UMP5152 && *buffer != UMP3410) {
777 dev_err (dev, "%s - invalid buffer signature\n", __FUNCTION__);
779 goto ExitTiValidateI2cImage;
784 status = TIReadRom (serial,
786 sizeof(struct ti_i2c_desc),
791 if ((start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size) > TI_MAX_I2C_SIZE) {
793 dbg ("%s - structure too big, erroring out.", __FUNCTION__);
797 dbg ("%s Type = 0x%x", __FUNCTION__, rom_desc->Type);
799 // Skip type 2 record
800 ttype = rom_desc->Type & 0x0f;
801 if ( ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
802 && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO ) {
803 // Read the descriptor data
804 status = TIReadRom(serial,
805 start_address+sizeof(struct ti_i2c_desc),
811 status = ValidChecksum(rom_desc, buffer);
815 start_address = start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size;
817 } while ((rom_desc->Type != I2C_DESC_TYPE_ION) && (start_address < TI_MAX_I2C_SIZE));
819 if ((rom_desc->Type != I2C_DESC_TYPE_ION) || (start_address > TI_MAX_I2C_SIZE))
822 ExitTiValidateI2cImage:
828 static int TIReadManufDescriptor (struct edgeport_serial *serial, __u8 *buffer)
832 struct ti_i2c_desc *rom_desc;
833 struct edge_ti_manuf_descriptor *desc;
835 rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
837 dev_err (&serial->serial->dev->dev, "%s - out of memory\n", __FUNCTION__);
840 start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_ION, rom_desc);
842 if (!start_address) {
843 dbg ("%s - Edge Descriptor not found in I2C", __FUNCTION__);
848 // Read the descriptor data
849 status = TIReadRom (serial,
850 start_address+sizeof(struct ti_i2c_desc),
856 status = ValidChecksum(rom_desc, buffer);
858 desc = (struct edge_ti_manuf_descriptor *)buffer;
859 dbg ( "%s - IonConfig 0x%x", __FUNCTION__, desc->IonConfig );
860 dbg ( "%s - Version %d", __FUNCTION__, desc->Version );
861 dbg ( "%s - Cpu/Board 0x%x", __FUNCTION__, desc->CpuRev_BoardRev );
862 dbg ( "%s - NumPorts %d", __FUNCTION__, desc->NumPorts );
863 dbg ( "%s - NumVirtualPorts %d", __FUNCTION__, desc->NumVirtualPorts );
864 dbg ( "%s - TotalPorts %d", __FUNCTION__, desc->TotalPorts );
871 /* Build firmware header used for firmware update */
872 static int BuildI2CFirmwareHeader (__u8 *header, struct device *dev)
878 struct ti_i2c_desc *i2c_header;
879 struct ti_i2c_image_header *img_header;
880 struct ti_i2c_firmware_rec *firmware_rec;
882 // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
883 // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver
884 // will download the latest firmware (padded to 15.5k) into the UMP ram.
885 // And finally when the device comes back up in download mode the driver will cause
886 // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
887 // the record type from 0xf2 to 0x02.
889 // Allocate a 15.5k buffer + 2 bytes for version number (Firmware Record)
890 buffer_size = (((1024 * 16) - 512 )+ sizeof(struct ti_i2c_firmware_rec));
892 buffer = kmalloc (buffer_size, GFP_KERNEL);
894 dev_err (dev, "%s - out of memory\n", __FUNCTION__);
898 // Set entire image of 0xffs
899 memset (buffer, 0xff, buffer_size);
901 // Copy version number into firmware record
902 firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
904 firmware_rec->Ver_Major = OperationalCodeImageVersion.MajorVersion;
905 firmware_rec->Ver_Minor = OperationalCodeImageVersion.MinorVersion;
907 // Pointer to fw_down memory image
908 img_header = (struct ti_i2c_image_header *)&PagableOperationalCodeImage[0];
910 memcpy (buffer + sizeof(struct ti_i2c_firmware_rec),
911 &PagableOperationalCodeImage[sizeof(struct ti_i2c_image_header)],
912 le16_to_cpu(img_header->Length));
914 for (i=0; i < buffer_size; i++) {
915 cs = (__u8)(cs + buffer[i]);
921 i2c_header = (struct ti_i2c_desc *)header;
922 firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data;
924 i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK;
925 i2c_header->Size = (__u16)buffer_size;
926 i2c_header->CheckSum = cs;
927 firmware_rec->Ver_Major = OperationalCodeImageVersion.MajorVersion;
928 firmware_rec->Ver_Minor = OperationalCodeImageVersion.MinorVersion;
933 /* Try to figure out what type of I2c we have */
934 static int TIGetI2cTypeInBootMode (struct edgeport_serial *serial)
939 // Try to read type 2
940 status = TIReadVendorRequestSync (serial->serial->dev,
941 UMPC_MEMORY_READ, // Request
942 DTK_ADDR_SPACE_I2C_TYPE_II, // wValue (Address type)
944 &data, // TransferBuffer
945 0x01); // TransferBufferLength
947 dbg ("%s - read 2 status error = %d", __FUNCTION__, status);
949 dbg ("%s - read 2 data = 0x%x", __FUNCTION__, data);
950 if ((!status) && (data == UMP5152 || data == UMP3410)) {
951 dbg ("%s - ROM_TYPE_II", __FUNCTION__);
952 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
956 // Try to read type 3
957 status = TIReadVendorRequestSync (serial->serial->dev,
958 UMPC_MEMORY_READ, // Request
959 DTK_ADDR_SPACE_I2C_TYPE_III, // wValue (Address type)
961 &data, // TransferBuffer
962 0x01); // TransferBufferLength
964 dbg ("%s - read 3 status error = %d", __FUNCTION__, status);
966 dbg ("%s - read 2 data = 0x%x", __FUNCTION__, data);
967 if ((!status) && (data == UMP5152 || data == UMP3410)) {
968 dbg ("%s - ROM_TYPE_III", __FUNCTION__);
969 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
973 dbg ("%s - Unknown", __FUNCTION__);
974 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
978 static int TISendBulkTransferSync (struct usb_serial *serial, void *buffer, int length, int *num_sent)
982 status = usb_bulk_msg (serial->dev,
983 usb_sndbulkpipe(serial->dev,
984 serial->port[0]->bulk_out_endpointAddress),
992 /* Download given firmware image to the device (IN BOOT MODE) */
993 static int TIDownloadCodeImage (struct edgeport_serial *serial, __u8 *image, int image_length)
1000 // Transfer firmware image
1001 for (pos = 0; pos < image_length; ) {
1002 // Read the next buffer from file
1003 transfer = image_length - pos;
1004 if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
1005 transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
1008 status = TISendBulkTransferSync (serial->serial, &image[pos], transfer, &done);
1011 // Advance buffer pointer
1019 static int TIConfigureBootDevice (struct usb_device *dev)
1025 * DownloadTIFirmware - Download run-time operating firmware to the TI5052
1027 * This routine downloads the main operating code into the TI5052, using the
1028 * boot code already burned into E2PROM or ROM.
1030 static int TIDownloadFirmware (struct edgeport_serial *serial)
1032 struct device *dev = &serial->serial->dev->dev;
1035 struct edge_ti_manuf_descriptor *ti_manuf_desc;
1036 struct usb_interface_descriptor *interface;
1037 int download_cur_ver;
1038 int download_new_ver;
1040 /* This routine is entered by both the BOOT mode and the Download mode
1041 * We can determine which code is running by the reading the config
1042 * descriptor and if we have only one bulk pipe it is in boot mode
1044 serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
1046 /* Default to type 2 i2c */
1047 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1049 status = TIChooseConfiguration (serial->serial->dev);
1053 interface = &serial->serial->interface->cur_altsetting->desc;
1055 dev_err (dev, "%s - no interface set, error!\n", __FUNCTION__);
1059 // Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
1060 // if we have more than one endpoint we are definitely in download mode
1061 if (interface->bNumEndpoints > 1)
1062 serial->product_info.TiMode = TI_MODE_DOWNLOAD;
1064 // Otherwise we will remain in configuring mode
1065 serial->product_info.TiMode = TI_MODE_CONFIGURING;
1067 // Save Download Version Number
1068 OperationalCodeImageVersion.MajorVersion = PagableOperationalCodeImageVersion.MajorVersion;
1069 OperationalCodeImageVersion.MinorVersion = PagableOperationalCodeImageVersion.MinorVersion;
1070 OperationalCodeImageVersion.BuildNumber = PagableOperationalCodeImageVersion.BuildNumber;
1072 /********************************************************************/
1074 /********************************************************************/
1075 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
1076 struct ti_i2c_desc *rom_desc;
1078 dbg ("%s - <<<<<<<<<<<<<<<RUNNING IN DOWNLOAD MODE>>>>>>>>>>", __FUNCTION__);
1080 status = TiValidateI2cImage (serial);
1082 dbg ("%s - <<<<<<<<<<<<<<<DOWNLOAD MODE -- BAD I2C >>>>>>>>>>",
1087 /* Validate Hardware version number
1088 * Read Manufacturing Descriptor from TI Based Edgeport
1090 ti_manuf_desc = kmalloc (sizeof (*ti_manuf_desc), GFP_KERNEL);
1091 if (!ti_manuf_desc) {
1092 dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
1095 status = TIReadManufDescriptor (serial, (__u8 *)ti_manuf_desc);
1097 kfree (ti_manuf_desc);
1101 // Check version number of ION descriptor
1102 if (!ignore_cpu_rev && TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev) < 2) {
1103 dbg ( "%s - Wrong CPU Rev %d (Must be 2)", __FUNCTION__,
1104 TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev));
1105 kfree (ti_manuf_desc);
1109 rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
1111 dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
1112 kfree (ti_manuf_desc);
1116 // Search for type 2 record (firmware record)
1117 if ((start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc)) != 0) {
1118 struct ti_i2c_firmware_rec *firmware_version;
1121 dbg ("%s - Found Type FIRMWARE (Type 2) record", __FUNCTION__);
1123 firmware_version = kmalloc (sizeof (*firmware_version), GFP_KERNEL);
1124 if (!firmware_version) {
1125 dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
1127 kfree (ti_manuf_desc);
1131 // Validate version number
1132 // Read the descriptor data
1133 status = TIReadRom (serial,
1134 start_address+sizeof(struct ti_i2c_desc),
1135 sizeof(struct ti_i2c_firmware_rec),
1136 (__u8 *)firmware_version);
1138 kfree (firmware_version);
1140 kfree (ti_manuf_desc);
1144 // Check version number of download with current version in I2c
1145 download_cur_ver = (firmware_version->Ver_Major << 8) +
1146 (firmware_version->Ver_Minor);
1147 download_new_ver = (OperationalCodeImageVersion.MajorVersion << 8) +
1148 (OperationalCodeImageVersion.MinorVersion);
1150 dbg ("%s - >>>Firmware Versions Device %d.%d Driver %d.%d",
1152 firmware_version->Ver_Major,
1153 firmware_version->Ver_Minor,
1154 OperationalCodeImageVersion.MajorVersion,
1155 OperationalCodeImageVersion.MinorVersion);
1157 // Check if we have an old version in the I2C and update if necessary
1158 if (download_cur_ver != download_new_ver) {
1159 dbg ("%s - Update I2C Download from %d.%d to %d.%d",
1161 firmware_version->Ver_Major,
1162 firmware_version->Ver_Minor,
1163 OperationalCodeImageVersion.MajorVersion,
1164 OperationalCodeImageVersion.MinorVersion);
1166 // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
1167 // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver
1168 // will download the latest firmware (padded to 15.5k) into the UMP ram.
1169 // And finally when the device comes back up in download mode the driver will cause
1170 // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
1171 // the record type from 0xf2 to 0x02.
1173 record = I2C_DESC_TYPE_FIRMWARE_BLANK;
1175 // Change the I2C Firmware record type to 0xf2 to trigger an update
1176 status = TIWriteRom (serial,
1181 kfree (firmware_version);
1183 kfree (ti_manuf_desc);
1187 // verify the write -- must do this in order for write to
1188 // complete before we do the hardware reset
1189 status = TIReadRom (serial,
1195 kfree (firmware_version);
1197 kfree (ti_manuf_desc);
1201 if (record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
1202 dev_err (dev, "%s - error resetting device\n", __FUNCTION__);
1203 kfree (firmware_version);
1205 kfree (ti_manuf_desc);
1209 dbg ("%s - HARDWARE RESET", __FUNCTION__);
1211 // Reset UMP -- Back to BOOT MODE
1212 status = TISendVendorRequestSync (serial->serial->dev,
1213 UMPC_HARDWARE_RESET, // Request
1216 NULL, // TransferBuffer
1217 0); // TransferBufferLength
1219 dbg ( "%s - HARDWARE RESET return %d", __FUNCTION__, status);
1221 /* return an error on purpose. */
1222 kfree (firmware_version);
1224 kfree (ti_manuf_desc);
1227 kfree (firmware_version);
1229 // Search for type 0xF2 record (firmware blank record)
1230 else if ((start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
1231 #define HEADER_SIZE (sizeof(struct ti_i2c_desc) + sizeof(struct ti_i2c_firmware_rec))
1235 header = kmalloc (HEADER_SIZE, GFP_KERNEL);
1237 dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
1239 kfree (ti_manuf_desc);
1243 vheader = kmalloc (HEADER_SIZE, GFP_KERNEL);
1245 dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
1248 kfree (ti_manuf_desc);
1252 dbg ("%s - Found Type BLANK FIRMWARE (Type F2) record", __FUNCTION__);
1254 // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
1255 // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver
1256 // will download the latest firmware (padded to 15.5k) into the UMP ram.
1257 // And finally when the device comes back up in download mode the driver will cause
1258 // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
1259 // the record type from 0xf2 to 0x02.
1260 status = BuildI2CFirmwareHeader(header, dev);
1265 kfree (ti_manuf_desc);
1269 // Update I2C with type 0xf2 record with correct size and checksum
1270 status = TIWriteRom (serial,
1278 kfree (ti_manuf_desc);
1282 // verify the write -- must do this in order for write to
1283 // complete before we do the hardware reset
1284 status = TIReadRom (serial,
1290 dbg ("%s - can't read header back", __FUNCTION__);
1294 kfree (ti_manuf_desc);
1297 if (memcmp(vheader, header, HEADER_SIZE)) {
1298 dbg ("%s - write download record failed", __FUNCTION__);
1302 kfree (ti_manuf_desc);
1309 dbg ("%s - Start firmware update", __FUNCTION__);
1311 // Tell firmware to copy download image into I2C
1312 status = TISendVendorRequestSync (serial->serial->dev,
1313 UMPC_COPY_DNLD_TO_I2C, // Request
1316 NULL, // TransferBuffer
1317 0); // TransferBufferLength
1319 dbg ("%s - Update complete 0x%x", __FUNCTION__, status);
1321 dev_err (dev, "%s - UMPC_COPY_DNLD_TO_I2C failed\n", __FUNCTION__);
1323 kfree (ti_manuf_desc);
1328 // The device is running the download code
1330 kfree (ti_manuf_desc);
1334 /********************************************************************/
1336 /********************************************************************/
1337 dbg ("%s - <<<<<<<<<<<<<<<RUNNING IN BOOT MODE>>>>>>>>>>>>>>>",
1340 // Configure the TI device so we can use the BULK pipes for download
1341 status = TIConfigureBootDevice (serial->serial->dev);
1345 if (le16_to_cpu(serial->serial->dev->descriptor.idVendor) != USB_VENDOR_ID_ION) {
1346 dbg ("%s - VID = 0x%x", __FUNCTION__,
1347 le16_to_cpu(serial->serial->dev->descriptor.idVendor));
1348 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1349 goto StayInBootMode;
1352 // We have an ION device (I2c Must be programmed)
1353 // Determine I2C image type
1354 if (TIGetI2cTypeInBootMode(serial)) {
1355 goto StayInBootMode;
1358 // Registry variable set?
1359 if (TIStayInBootMode) {
1360 dbg ("%s - TIStayInBootMode", __FUNCTION__);
1361 goto StayInBootMode;
1364 // Check for ION Vendor ID and that the I2C is valid
1365 if (!TiValidateI2cImage(serial)) {
1366 struct ti_i2c_image_header *header;
1372 /* Validate Hardware version number
1373 * Read Manufacturing Descriptor from TI Based Edgeport
1375 ti_manuf_desc = kmalloc (sizeof (*ti_manuf_desc), GFP_KERNEL);
1376 if (!ti_manuf_desc) {
1377 dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
1380 status = TIReadManufDescriptor (serial, (__u8 *)ti_manuf_desc);
1382 kfree (ti_manuf_desc);
1383 goto StayInBootMode;
1386 // Check for version 2
1387 if (!ignore_cpu_rev && TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev) < 2) {
1388 dbg ("%s - Wrong CPU Rev %d (Must be 2)", __FUNCTION__,
1389 TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev));
1390 kfree (ti_manuf_desc);
1391 goto StayInBootMode;
1394 kfree (ti_manuf_desc);
1396 // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
1397 // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver
1398 // will download the latest firmware (padded to 15.5k) into the UMP ram.
1399 // And finally when the device comes back up in download mode the driver will cause
1400 // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
1401 // the record type from 0xf2 to 0x02.
1404 * Do we really have to copy the whole firmware image,
1405 * or could we do this in place!
1408 // Allocate a 15.5k buffer + 3 byte header
1409 buffer_size = (((1024 * 16) - 512) + sizeof(struct ti_i2c_image_header));
1410 buffer = kmalloc (buffer_size, GFP_KERNEL);
1412 dev_err (dev, "%s - out of memory\n", __FUNCTION__);
1416 // Initialize the buffer to 0xff (pad the buffer)
1417 memset (buffer, 0xff, buffer_size);
1419 memcpy (buffer, &PagableOperationalCodeImage[0], PagableOperationalCodeSize);
1421 for(i = sizeof(struct ti_i2c_image_header); i < buffer_size; i++) {
1422 cs = (__u8)(cs + buffer[i]);
1425 header = (struct ti_i2c_image_header *)buffer;
1427 // update length and checksum after padding
1428 header->Length = cpu_to_le16((__u16)(buffer_size - sizeof(struct ti_i2c_image_header)));
1429 header->CheckSum = cs;
1431 // Download the operational code
1432 dbg ("%s - Downloading operational code image (TI UMP)", __FUNCTION__);
1433 status = TIDownloadCodeImage (serial, buffer, buffer_size);
1438 dbg ("%s - Error downloading operational code image", __FUNCTION__);
1442 // Device will reboot
1443 serial->product_info.TiMode = TI_MODE_TRANSITIONING;
1445 dbg ("%s - Download successful -- Device rebooting...", __FUNCTION__);
1447 /* return an error on purpose */
1452 // Eprom is invalid or blank stay in boot mode
1453 dbg ("%s - <<<<<<<<<<<<<<<STAYING IN BOOT MODE>>>>>>>>>>>>", __FUNCTION__);
1454 serial->product_info.TiMode = TI_MODE_BOOT;
1460 static int TISetDtr (struct edgeport_port *port)
1462 int port_number = port->port->number - port->port->serial->minor;
1464 dbg ("%s", __FUNCTION__);
1465 port->shadow_mcr |= MCR_DTR;
1467 return TIWriteCommandSync (port->port->serial->dev,
1469 (__u8)(UMPM_UART1_PORT + port_number),
1475 static int TIClearDtr (struct edgeport_port *port)
1477 int port_number = port->port->number - port->port->serial->minor;
1479 dbg ("%s", __FUNCTION__);
1480 port->shadow_mcr &= ~MCR_DTR;
1482 return TIWriteCommandSync (port->port->serial->dev,
1484 (__u8)(UMPM_UART1_PORT + port_number),
1490 static int TISetRts (struct edgeport_port *port)
1492 int port_number = port->port->number - port->port->serial->minor;
1494 dbg ("%s", __FUNCTION__);
1495 port->shadow_mcr |= MCR_RTS;
1497 return TIWriteCommandSync (port->port->serial->dev,
1499 (__u8)(UMPM_UART1_PORT + port_number),
1505 static int TIClearRts (struct edgeport_port *port)
1507 int port_number = port->port->number - port->port->serial->minor;
1509 dbg ("%s", __FUNCTION__);
1510 port->shadow_mcr &= ~MCR_RTS;
1512 return TIWriteCommandSync (port->port->serial->dev,
1514 (__u8)(UMPM_UART1_PORT + port_number),
1520 static int TISetLoopBack (struct edgeport_port *port)
1522 int port_number = port->port->number - port->port->serial->minor;
1524 dbg ("%s", __FUNCTION__);
1526 return TIWriteCommandSync (port->port->serial->dev,
1527 UMPC_SET_CLR_LOOPBACK,
1528 (__u8)(UMPM_UART1_PORT + port_number),
1534 static int TIClearLoopBack (struct edgeport_port *port)
1536 int port_number = port->port->number - port->port->serial->minor;
1538 dbg ("%s", __FUNCTION__);
1540 return TIWriteCommandSync (port->port->serial->dev,
1541 UMPC_SET_CLR_LOOPBACK,
1542 (__u8)(UMPM_UART1_PORT + port_number),
1548 static int TISetBreak (struct edgeport_port *port)
1550 int port_number = port->port->number - port->port->serial->minor;
1552 dbg ("%s", __FUNCTION__);
1554 return TIWriteCommandSync (port->port->serial->dev,
1556 (__u8)(UMPM_UART1_PORT + port_number),
1562 static int TIClearBreak (struct edgeport_port *port)
1564 int port_number = port->port->number - port->port->serial->minor;
1566 dbg ("%s", __FUNCTION__);
1568 return TIWriteCommandSync (port->port->serial->dev,
1570 (__u8)(UMPM_UART1_PORT + port_number),
1576 static int TIRestoreMCR (struct edgeport_port *port, __u8 mcr)
1580 dbg ("%s - %x", __FUNCTION__, mcr);
1583 status = TISetDtr (port);
1585 status = TIClearDtr (port);
1591 status = TISetRts (port);
1593 status = TIClearRts (port);
1598 if (mcr & MCR_LOOPBACK)
1599 status = TISetLoopBack (port);
1601 status = TIClearLoopBack (port);
1608 /* Convert TI LSR to standard UART flags */
1609 static __u8 MapLineStatus (__u8 ti_lsr)
1613 #define MAP_FLAG(flagUmp, flagUart) \
1614 if (ti_lsr & flagUmp) \
1617 MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR) /* overrun */
1618 MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */
1619 MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */
1620 MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */
1621 MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* receive data available */
1622 MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* transmit holding register empty */
1629 static void handle_new_msr (struct edgeport_port *edge_port, __u8 msr)
1631 struct async_icount *icount;
1632 struct tty_struct *tty;
1634 dbg ("%s - %02x", __FUNCTION__, msr);
1636 if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR | EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
1637 icount = &edge_port->icount;
1639 /* update input line counters */
1640 if (msr & EDGEPORT_MSR_DELTA_CTS)
1642 if (msr & EDGEPORT_MSR_DELTA_DSR)
1644 if (msr & EDGEPORT_MSR_DELTA_CD)
1646 if (msr & EDGEPORT_MSR_DELTA_RI)
1648 wake_up_interruptible (&edge_port->delta_msr_wait);
1651 /* Save the new modem status */
1652 edge_port->shadow_msr = msr & 0xf0;
1654 tty = edge_port->port->tty;
1655 /* handle CTS flow control */
1656 if (tty && C_CRTSCTS(tty)) {
1657 if (msr & EDGEPORT_MSR_CTS) {
1658 tty->hw_stopped = 0;
1661 tty->hw_stopped = 1;
1668 static void handle_new_lsr (struct edgeport_port *edge_port, int lsr_data, __u8 lsr, __u8 data)
1670 struct async_icount *icount;
1671 __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
1673 dbg ("%s - %02x", __FUNCTION__, new_lsr);
1675 edge_port->shadow_lsr = lsr;
1677 if (new_lsr & LSR_BREAK) {
1679 * Parity and Framing errors only count if they
1680 * occur exclusive of a break being received.
1682 new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
1685 /* Place LSR data byte into Rx buffer */
1686 if (lsr_data && edge_port->port->tty)
1687 edge_tty_recv(&edge_port->port->dev, edge_port->port->tty, &data, 1);
1689 /* update input line counters */
1690 icount = &edge_port->icount;
1691 if (new_lsr & LSR_BREAK)
1693 if (new_lsr & LSR_OVER_ERR)
1695 if (new_lsr & LSR_PAR_ERR)
1697 if (new_lsr & LSR_FRM_ERR)
1702 static void edge_interrupt_callback (struct urb *urb, struct pt_regs *regs)
1704 struct edgeport_serial *edge_serial = (struct edgeport_serial *)urb->context;
1705 struct usb_serial_port *port;
1706 struct edgeport_port *edge_port;
1707 unsigned char *data = urb->transfer_buffer;
1708 int length = urb->actual_length;
1715 dbg("%s", __FUNCTION__);
1717 switch (urb->status) {
1724 /* this urb is terminated, clean up */
1725 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
1728 dev_err(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __FUNCTION__, urb->status);
1733 dbg ("%s - no data in urb", __FUNCTION__);
1737 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __FUNCTION__, length, data);
1740 dbg ("%s - expecting packet of size 2, got %d", __FUNCTION__, length);
1744 port_number = TIUMP_GET_PORT_FROM_CODE (data[0]);
1745 function = TIUMP_GET_FUNC_FROM_CODE (data[0]);
1746 dbg ("%s - port_number %d, function %d, info 0x%x",
1747 __FUNCTION__, port_number, function, data[1]);
1748 port = edge_serial->serial->port[port_number];
1749 edge_port = usb_get_serial_port_data(port);
1751 dbg ("%s - edge_port not found", __FUNCTION__);
1755 case TIUMP_INTERRUPT_CODE_LSR:
1756 lsr = MapLineStatus(data[1]);
1757 if (lsr & UMP_UART_LSR_DATA_MASK) {
1758 /* Save the LSR event for bulk read completion routine */
1759 dbg ("%s - LSR Event Port %u LSR Status = %02x",
1760 __FUNCTION__, port_number, lsr);
1761 edge_port->lsr_event = 1;
1762 edge_port->lsr_mask = lsr;
1764 dbg ("%s - ===== Port %d LSR Status = %02x ======",
1765 __FUNCTION__, port_number, lsr);
1766 handle_new_lsr (edge_port, 0, lsr, 0);
1770 case TIUMP_INTERRUPT_CODE_MSR: // MSR
1771 /* Copy MSR from UMP */
1773 dbg ("%s - ===== Port %u MSR Status = %02x ======\n",
1774 __FUNCTION__, port_number, msr);
1775 handle_new_msr (edge_port, msr);
1779 dev_err (&urb->dev->dev, "%s - Unknown Interrupt code from UMP %x\n",
1780 __FUNCTION__, data[1]);
1786 status = usb_submit_urb (urb, GFP_ATOMIC);
1788 dev_err (&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
1789 __FUNCTION__, status);
1792 static void edge_bulk_in_callback (struct urb *urb, struct pt_regs *regs)
1794 struct edgeport_port *edge_port = (struct edgeport_port *)urb->context;
1795 unsigned char *data = urb->transfer_buffer;
1796 struct tty_struct *tty;
1800 dbg("%s", __FUNCTION__);
1802 switch (urb->status) {
1809 /* this urb is terminated, clean up */
1810 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
1813 dev_err (&urb->dev->dev,"%s - nonzero read bulk status received: %d\n",
1814 __FUNCTION__, urb->status );
1817 if (urb->status == -EPIPE)
1821 dev_err(&urb->dev->dev,"%s - stopping read!\n", __FUNCTION__);
1825 port_number = edge_port->port->number - edge_port->port->serial->minor;
1827 if (edge_port->lsr_event) {
1828 edge_port->lsr_event = 0;
1829 dbg ("%s ===== Port %u LSR Status = %02x, Data = %02x ======",
1830 __FUNCTION__, port_number, edge_port->lsr_mask, *data);
1831 handle_new_lsr (edge_port, 1, edge_port->lsr_mask, *data);
1832 /* Adjust buffer length/pointer */
1833 --urb->actual_length;
1837 tty = edge_port->port->tty;
1838 if (tty && urb->actual_length) {
1839 usb_serial_debug_data(debug, &edge_port->port->dev, __FUNCTION__, urb->actual_length, data);
1841 if (edge_port->close_pending) {
1842 dbg ("%s - close is pending, dropping data on the floor.", __FUNCTION__);
1844 edge_tty_recv(&edge_port->port->dev, tty, data, urb->actual_length);
1846 edge_port->icount.rx += urb->actual_length;
1850 /* continue read unless stopped */
1851 spin_lock(&edge_port->ep_lock);
1852 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING) {
1853 urb->dev = edge_port->port->serial->dev;
1854 status = usb_submit_urb(urb, GFP_ATOMIC);
1855 } else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING) {
1856 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
1858 spin_unlock(&edge_port->ep_lock);
1860 dev_err (&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
1861 __FUNCTION__, status);
1864 static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length)
1869 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
1870 tty_flip_buffer_push(tty);
1871 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
1872 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1873 __FUNCTION__, length);
1877 cnt = min(length, TTY_FLIPBUF_SIZE - tty->flip.count);
1878 memcpy(tty->flip.char_buf_ptr, data, cnt);
1879 memset(tty->flip.flag_buf_ptr, 0, cnt);
1880 tty->flip.char_buf_ptr += cnt;
1881 tty->flip.flag_buf_ptr += cnt;
1882 tty->flip.count += cnt;
1885 } while (length > 0);
1887 tty_flip_buffer_push(tty);
1890 static void edge_bulk_out_callback (struct urb *urb, struct pt_regs *regs)
1892 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1893 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1895 dbg ("%s - port %d", __FUNCTION__, port->number);
1897 edge_port->ep_write_urb_in_use = 0;
1899 switch (urb->status) {
1906 /* this urb is terminated, clean up */
1907 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
1910 dev_err (&urb->dev->dev,"%s - nonzero write bulk status received: %d\n",
1911 __FUNCTION__, urb->status);
1914 /* send any buffered data */
1918 static int edge_open (struct usb_serial_port *port, struct file * filp)
1920 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1921 struct edgeport_serial *edge_serial;
1922 struct usb_device *dev;
1927 u8 transaction_timeout;
1929 dbg("%s - port %d", __FUNCTION__, port->number);
1931 if (edge_port == NULL)
1935 port->tty->low_latency = low_latency;
1937 port_number = port->number - port->serial->minor;
1938 switch (port_number) {
1940 edge_port->uart_base = UMPMEM_BASE_UART1;
1941 edge_port->dma_address = UMPD_OEDB1_ADDRESS;
1944 edge_port->uart_base = UMPMEM_BASE_UART2;
1945 edge_port->dma_address = UMPD_OEDB2_ADDRESS;
1948 dev_err (&port->dev, "Unknown port number!!!\n");
1952 dbg ("%s - port_number = %d, uart_base = %04x, dma_address = %04x",
1953 __FUNCTION__, port_number, edge_port->uart_base, edge_port->dma_address);
1955 dev = port->serial->dev;
1957 memset (&(edge_port->icount), 0x00, sizeof(edge_port->icount));
1958 init_waitqueue_head (&edge_port->delta_msr_wait);
1960 /* turn off loopback */
1961 status = TIClearLoopBack (edge_port);
1963 dev_err(&port->dev,"%s - cannot send clear loopback command, %d\n",
1964 __FUNCTION__, status);
1968 /* set up the port settings */
1969 edge_set_termios (port, NULL);
1971 /* open up the port */
1973 /* milliseconds to timeout for DMA transfer */
1974 transaction_timeout = 2;
1976 edge_port->ump_read_timeout = max (20, ((transaction_timeout * 3) / 2) );
1978 // milliseconds to timeout for DMA transfer
1979 open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
1980 UMP_PIPE_TRANS_TIMEOUT_ENA |
1981 (transaction_timeout << 2));
1983 dbg ("%s - Sending UMPC_OPEN_PORT", __FUNCTION__);
1985 /* Tell TI to open and start the port */
1986 status = TIWriteCommandSync (dev,
1988 (u8)(UMPM_UART1_PORT + port_number),
1993 dev_err(&port->dev,"%s - cannot send open command, %d\n", __FUNCTION__, status);
1997 /* Start the DMA? */
1998 status = TIWriteCommandSync (dev,
2000 (u8)(UMPM_UART1_PORT + port_number),
2005 dev_err(&port->dev,"%s - cannot send start DMA command, %d\n", __FUNCTION__, status);
2009 /* Clear TX and RX buffers in UMP */
2010 status = TIPurgeDataSync (port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
2012 dev_err(&port->dev,"%s - cannot send clear buffers command, %d\n", __FUNCTION__, status);
2016 /* Read Initial MSR */
2017 status = TIReadVendorRequestSync (dev,
2018 UMPC_READ_MSR, // Request
2020 (__u16)(UMPM_UART1_PORT + port_number), // wIndex (Address)
2021 &edge_port->shadow_msr, // TransferBuffer
2022 1); // TransferBufferLength
2024 dev_err(&port->dev,"%s - cannot send read MSR command, %d\n", __FUNCTION__, status);
2028 dbg ("ShadowMSR 0x%X", edge_port->shadow_msr);
2030 /* Set Initial MCR */
2031 edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
2032 dbg ("ShadowMCR 0x%X", edge_port->shadow_mcr);
2034 edge_serial = edge_port->edge_serial;
2035 if (down_interruptible(&edge_serial->es_sem))
2036 return -ERESTARTSYS;
2037 if (edge_serial->num_ports_open == 0) {
2038 /* we are the first port to be opened, let's post the interrupt urb */
2039 urb = edge_serial->serial->port[0]->interrupt_in_urb;
2041 dev_err (&port->dev, "%s - no interrupt urb present, exiting\n", __FUNCTION__);
2045 urb->complete = edge_interrupt_callback;
2046 urb->context = edge_serial;
2048 status = usb_submit_urb (urb, GFP_KERNEL);
2050 dev_err (&port->dev, "%s - usb_submit_urb failed with value %d\n", __FUNCTION__, status);
2056 * reset the data toggle on the bulk endpoints to work around bug in
2057 * host controllers where things get out of sync some times
2059 usb_clear_halt (dev, port->write_urb->pipe);
2060 usb_clear_halt (dev, port->read_urb->pipe);
2062 /* start up our bulk read urb */
2063 urb = port->read_urb;
2065 dev_err (&port->dev, "%s - no read urb present, exiting\n", __FUNCTION__);
2067 goto unlink_int_urb;
2069 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2070 urb->complete = edge_bulk_in_callback;
2071 urb->context = edge_port;
2073 status = usb_submit_urb (urb, GFP_KERNEL);
2075 dev_err (&port->dev, "%s - read bulk usb_submit_urb failed with value %d\n", __FUNCTION__, status);
2076 goto unlink_int_urb;
2079 ++edge_serial->num_ports_open;
2081 dbg("%s - exited", __FUNCTION__);
2086 if (edge_port->edge_serial->num_ports_open == 0)
2087 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2089 up(&edge_serial->es_sem);
2093 static void edge_close (struct usb_serial_port *port, struct file *filp)
2095 struct edgeport_serial *edge_serial;
2096 struct edgeport_port *edge_port;
2100 dbg("%s - port %d", __FUNCTION__, port->number);
2102 edge_serial = usb_get_serial_data(port->serial);
2103 edge_port = usb_get_serial_port_data(port);
2104 if ((edge_serial == NULL) || (edge_port == NULL))
2107 /* The bulkreadcompletion routine will check
2108 * this flag and dump add read data */
2109 edge_port->close_pending = 1;
2111 /* chase the port close and flush */
2112 TIChasePort (edge_port, (HZ*closing_wait)/100, 1);
2114 usb_kill_urb(port->read_urb);
2115 usb_kill_urb(port->write_urb);
2116 edge_port->ep_write_urb_in_use = 0;
2118 /* assuming we can still talk to the device,
2119 * send a close port command to it */
2120 dbg("%s - send umpc_close_port", __FUNCTION__);
2121 port_number = port->number - port->serial->minor;
2122 status = TIWriteCommandSync (port->serial->dev,
2124 (__u8)(UMPM_UART1_PORT + port_number),
2128 down(&edge_serial->es_sem);
2129 --edge_port->edge_serial->num_ports_open;
2130 if (edge_port->edge_serial->num_ports_open <= 0) {
2131 /* last port is now closed, let's shut down our interrupt urb */
2132 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2133 edge_port->edge_serial->num_ports_open = 0;
2135 up(&edge_serial->es_sem);
2136 edge_port->close_pending = 0;
2138 dbg("%s - exited", __FUNCTION__);
2141 static int edge_write (struct usb_serial_port *port, const unsigned char *data, int count)
2143 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2144 unsigned long flags;
2146 dbg("%s - port %d", __FUNCTION__, port->number);
2149 dbg("%s - write request of 0 bytes", __FUNCTION__);
2153 if (edge_port == NULL)
2155 if (edge_port->close_pending == 1)
2158 spin_lock_irqsave(&edge_port->ep_lock, flags);
2159 count = edge_buf_put(edge_port->ep_out_buf, data, count);
2160 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2167 static void edge_send(struct usb_serial_port *port)
2170 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2171 struct tty_struct *tty = port->tty;
2172 unsigned long flags;
2175 dbg("%s - port %d", __FUNCTION__, port->number);
2177 spin_lock_irqsave(&edge_port->ep_lock, flags);
2179 if (edge_port->ep_write_urb_in_use) {
2180 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2184 count = edge_buf_get(edge_port->ep_out_buf,
2185 port->write_urb->transfer_buffer,
2186 port->bulk_out_size);
2189 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2193 edge_port->ep_write_urb_in_use = 1;
2195 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2197 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, port->write_urb->transfer_buffer);
2199 /* set up our urb */
2200 usb_fill_bulk_urb (port->write_urb, port->serial->dev,
2201 usb_sndbulkpipe (port->serial->dev,
2202 port->bulk_out_endpointAddress),
2203 port->write_urb->transfer_buffer, count,
2204 edge_bulk_out_callback,
2207 /* send the data out the bulk port */
2208 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2210 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
2211 edge_port->ep_write_urb_in_use = 0;
2212 // TODO: reschedule edge_send
2214 edge_port->icount.tx += count;
2217 /* wakeup any process waiting for writes to complete */
2218 /* there is now more room in the buffer for new writes */
2220 /* let the tty driver wakeup if it has a special write_wakeup function */
2225 static int edge_write_room (struct usb_serial_port *port)
2227 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2229 unsigned long flags;
2231 dbg("%s - port %d", __FUNCTION__, port->number);
2233 if (edge_port == NULL)
2235 if (edge_port->close_pending == 1)
2238 spin_lock_irqsave(&edge_port->ep_lock, flags);
2239 room = edge_buf_space_avail(edge_port->ep_out_buf);
2240 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2242 dbg("%s - returns %d", __FUNCTION__, room);
2246 static int edge_chars_in_buffer (struct usb_serial_port *port)
2248 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2250 unsigned long flags;
2252 dbg("%s - port %d", __FUNCTION__, port->number);
2254 if (edge_port == NULL)
2256 if (edge_port->close_pending == 1)
2259 spin_lock_irqsave(&edge_port->ep_lock, flags);
2260 chars = edge_buf_data_avail(edge_port->ep_out_buf);
2261 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2263 dbg ("%s - returns %d", __FUNCTION__, chars);
2267 static void edge_throttle (struct usb_serial_port *port)
2269 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2270 struct tty_struct *tty;
2273 dbg("%s - port %d", __FUNCTION__, port->number);
2275 if (edge_port == NULL)
2280 dbg ("%s - no tty available", __FUNCTION__);
2284 /* if we are implementing XON/XOFF, send the stop character */
2286 unsigned char stop_char = STOP_CHAR(tty);
2287 status = edge_write (port, &stop_char, 1);
2289 dev_err(&port->dev, "%s - failed to write stop character, %d\n", __FUNCTION__, status);
2293 /* if we are implementing RTS/CTS, stop reads */
2294 /* and the Edgeport will clear the RTS line */
2296 stop_read(edge_port);
2300 static void edge_unthrottle (struct usb_serial_port *port)
2302 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2303 struct tty_struct *tty;
2306 dbg("%s - port %d", __FUNCTION__, port->number);
2308 if (edge_port == NULL)
2313 dbg ("%s - no tty available", __FUNCTION__);
2317 /* if we are implementing XON/XOFF, send the start character */
2319 unsigned char start_char = START_CHAR(tty);
2320 status = edge_write (port, &start_char, 1);
2322 dev_err(&port->dev, "%s - failed to write start character, %d\n", __FUNCTION__, status);
2326 /* if we are implementing RTS/CTS, restart reads */
2327 /* are the Edgeport will assert the RTS line */
2328 if (C_CRTSCTS(tty)) {
2329 status = restart_read(edge_port);
2331 dev_err(&port->dev, "%s - read bulk usb_submit_urb failed with value %d\n", __FUNCTION__, status);
2336 static void stop_read(struct edgeport_port *edge_port)
2338 unsigned long flags;
2340 spin_lock_irqsave(&edge_port->ep_lock, flags);
2342 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
2343 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
2344 edge_port->shadow_mcr &= ~MCR_RTS;
2346 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2349 static int restart_read(struct edgeport_port *edge_port)
2353 unsigned long flags;
2355 spin_lock_irqsave(&edge_port->ep_lock, flags);
2357 if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
2358 urb = edge_port->port->read_urb;
2359 urb->complete = edge_bulk_in_callback;
2360 urb->context = edge_port;
2361 urb->dev = edge_port->port->serial->dev;
2362 status = usb_submit_urb(urb, GFP_KERNEL);
2364 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2365 edge_port->shadow_mcr |= MCR_RTS;
2367 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2372 static void change_port_settings (struct edgeport_port *edge_port, struct termios *old_termios)
2374 struct ump_uart_config *config;
2375 struct tty_struct *tty;
2379 int port_number = edge_port->port->number - edge_port->port->serial->minor;
2381 dbg("%s - port %d", __FUNCTION__, edge_port->port->number);
2383 tty = edge_port->port->tty;
2386 dbg("%s - no tty structures", __FUNCTION__);
2390 config = kmalloc (sizeof (*config), GFP_KERNEL);
2392 dev_err (&edge_port->port->dev, "%s - out of memory\n", __FUNCTION__);
2396 cflag = tty->termios->c_cflag;
2400 /* These flags must be set */
2401 config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
2402 config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
2403 config->bUartMode = (__u8)(edge_port->bUartMode);
2405 switch (cflag & CSIZE) {
2407 config->bDataBits = UMP_UART_CHAR5BITS;
2408 dbg ("%s - data bits = 5", __FUNCTION__);
2411 config->bDataBits = UMP_UART_CHAR6BITS;
2412 dbg ("%s - data bits = 6", __FUNCTION__);
2415 config->bDataBits = UMP_UART_CHAR7BITS;
2416 dbg ("%s - data bits = 7", __FUNCTION__);
2420 config->bDataBits = UMP_UART_CHAR8BITS;
2421 dbg ("%s - data bits = 8", __FUNCTION__);
2425 if (cflag & PARENB) {
2426 if (cflag & PARODD) {
2427 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2428 config->bParity = UMP_UART_ODDPARITY;
2429 dbg("%s - parity = odd", __FUNCTION__);
2431 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2432 config->bParity = UMP_UART_EVENPARITY;
2433 dbg("%s - parity = even", __FUNCTION__);
2436 config->bParity = UMP_UART_NOPARITY;
2437 dbg("%s - parity = none", __FUNCTION__);
2440 if (cflag & CSTOPB) {
2441 config->bStopBits = UMP_UART_STOPBIT2;
2442 dbg("%s - stop bits = 2", __FUNCTION__);
2444 config->bStopBits = UMP_UART_STOPBIT1;
2445 dbg("%s - stop bits = 1", __FUNCTION__);
2448 /* figure out the flow control settings */
2449 if (cflag & CRTSCTS) {
2450 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
2451 config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
2452 dbg("%s - RTS/CTS is enabled", __FUNCTION__);
2454 dbg("%s - RTS/CTS is disabled", __FUNCTION__);
2455 tty->hw_stopped = 0;
2456 restart_read(edge_port);
2459 /* if we are implementing XON/XOFF, set the start and stop character in the device */
2460 if (I_IXOFF(tty) || I_IXON(tty)) {
2461 config->cXon = START_CHAR(tty);
2462 config->cXoff = STOP_CHAR(tty);
2464 /* if we are implementing INBOUND XON/XOFF */
2466 config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
2467 dbg ("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2468 __FUNCTION__, config->cXon, config->cXoff);
2470 dbg ("%s - INBOUND XON/XOFF is disabled", __FUNCTION__);
2473 /* if we are implementing OUTBOUND XON/XOFF */
2475 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
2476 dbg ("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2477 __FUNCTION__, config->cXon, config->cXoff);
2479 dbg ("%s - OUTBOUND XON/XOFF is disabled", __FUNCTION__);
2483 /* Round the baud rate */
2484 baud = tty_get_baud_rate(tty);
2486 /* pick a default, any default... */
2489 edge_port->baud_rate = baud;
2490 config->wBaudRate = (__u16)((461550L + baud/2) / baud);
2492 dbg ("%s - baud rate = %d, wBaudRate = %d", __FUNCTION__, baud, config->wBaudRate);
2494 dbg ("wBaudRate: %d", (int)(461550L / config->wBaudRate));
2495 dbg ("wFlags: 0x%x", config->wFlags);
2496 dbg ("bDataBits: %d", config->bDataBits);
2497 dbg ("bParity: %d", config->bParity);
2498 dbg ("bStopBits: %d", config->bStopBits);
2499 dbg ("cXon: %d", config->cXon);
2500 dbg ("cXoff: %d", config->cXoff);
2501 dbg ("bUartMode: %d", config->bUartMode);
2503 /* move the word values into big endian mode */
2504 cpu_to_be16s (&config->wFlags);
2505 cpu_to_be16s (&config->wBaudRate);
2507 status = TIWriteCommandSync (edge_port->port->serial->dev,
2509 (__u8)(UMPM_UART1_PORT + port_number),
2514 dbg ("%s - error %d when trying to write config to device",
2515 __FUNCTION__, status);
2523 static void edge_set_termios (struct usb_serial_port *port, struct termios *old_termios)
2525 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2526 struct tty_struct *tty = port->tty;
2529 if (!port->tty || !port->tty->termios) {
2530 dbg ("%s - no tty or termios", __FUNCTION__);
2534 cflag = tty->termios->c_cflag;
2535 /* check that they really want us to change something */
2537 if (cflag == old_termios->c_cflag &&
2538 tty->termios->c_iflag == old_termios->c_iflag) {
2539 dbg ("%s - nothing to change", __FUNCTION__);
2544 dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
2545 tty->termios->c_cflag, tty->termios->c_iflag);
2547 dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
2548 old_termios->c_cflag, old_termios->c_iflag);
2551 dbg("%s - port %d", __FUNCTION__, port->number);
2553 if (edge_port == NULL)
2556 /* change the port settings to the new ones specified */
2557 change_port_settings (edge_port, old_termios);
2562 static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear)
2564 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2567 dbg("%s - port %d", __FUNCTION__, port->number);
2569 mcr = edge_port->shadow_mcr;
2570 if (set & TIOCM_RTS)
2572 if (set & TIOCM_DTR)
2574 if (set & TIOCM_LOOP)
2575 mcr |= MCR_LOOPBACK;
2577 if (clear & TIOCM_RTS)
2579 if (clear & TIOCM_DTR)
2581 if (clear & TIOCM_LOOP)
2582 mcr &= ~MCR_LOOPBACK;
2584 edge_port->shadow_mcr = mcr;
2586 TIRestoreMCR (edge_port, mcr);
2591 static int edge_tiocmget(struct usb_serial_port *port, struct file *file)
2593 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2594 unsigned int result = 0;
2598 dbg("%s - port %d", __FUNCTION__, port->number);
2600 msr = edge_port->shadow_msr;
2601 mcr = edge_port->shadow_mcr;
2602 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
2603 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
2604 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
2605 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
2606 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
2607 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
2610 dbg("%s -- %x", __FUNCTION__, result);
2615 static int get_serial_info (struct edgeport_port *edge_port, struct serial_struct __user *retinfo)
2617 struct serial_struct tmp;
2622 memset(&tmp, 0, sizeof(tmp));
2624 tmp.type = PORT_16550A;
2625 tmp.line = edge_port->port->serial->minor;
2626 tmp.port = edge_port->port->number;
2628 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2629 tmp.xmit_fifo_size = edge_port->port->bulk_out_size;
2630 tmp.baud_base = 9600;
2631 tmp.close_delay = 5*HZ;
2632 tmp.closing_wait = closing_wait;
2633 // tmp.custom_divisor = state->custom_divisor;
2634 // tmp.hub6 = state->hub6;
2635 // tmp.io_type = state->io_type;
2638 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2643 static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
2645 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2646 struct async_icount cnow;
2647 struct async_icount cprev;
2649 dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
2653 dbg("%s - (%d) TIOCINQ", __FUNCTION__, port->number);
2654 // return get_number_bytes_avail(edge_port, (unsigned int *) arg);
2658 dbg("%s - (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
2659 // return get_lsr_info(edge_port, (unsigned int *) arg);
2663 dbg("%s - (%d) TIOCGSERIAL", __FUNCTION__, port->number);
2664 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
2668 dbg("%s - (%d) TIOCSSERIAL", __FUNCTION__, port->number);
2672 dbg("%s - (%d) TIOCMIWAIT", __FUNCTION__, port->number);
2673 cprev = edge_port->icount;
2675 interruptible_sleep_on(&edge_port->delta_msr_wait);
2676 /* see if a signal did it */
2677 if (signal_pending(current))
2678 return -ERESTARTSYS;
2679 cnow = edge_port->icount;
2680 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2681 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2682 return -EIO; /* no change => error */
2683 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2684 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2685 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2686 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
2695 dbg ("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__,
2696 port->number, edge_port->icount.rx, edge_port->icount.tx);
2697 if (copy_to_user((void __user *)arg, &edge_port->icount, sizeof(edge_port->icount)))
2702 return -ENOIOCTLCMD;
2705 static void edge_break (struct usb_serial_port *port, int break_state)
2707 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2710 dbg ("%s - state = %d", __FUNCTION__, break_state);
2712 /* chase the port close */
2713 TIChasePort (edge_port, 0, 0);
2715 if (break_state == -1) {
2716 status = TISetBreak (edge_port);
2718 status = TIClearBreak (edge_port);
2721 dbg ("%s - error %d sending break set/clear command.",
2722 __FUNCTION__, status);
2726 static int edge_startup (struct usb_serial *serial)
2728 struct edgeport_serial *edge_serial;
2729 struct edgeport_port *edge_port;
2730 struct usb_device *dev;
2736 /* create our private serial structure */
2737 edge_serial = kmalloc (sizeof(struct edgeport_serial), GFP_KERNEL);
2738 if (edge_serial == NULL) {
2739 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
2742 memset (edge_serial, 0, sizeof(struct edgeport_serial));
2743 sema_init(&edge_serial->es_sem, 1);
2744 edge_serial->serial = serial;
2745 usb_set_serial_data(serial, edge_serial);
2747 status = TIDownloadFirmware (edge_serial);
2749 kfree (edge_serial);
2753 /* set up our port private structures */
2754 for (i = 0; i < serial->num_ports; ++i) {
2755 edge_port = kmalloc (sizeof(struct edgeport_port), GFP_KERNEL);
2756 if (edge_port == NULL) {
2757 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
2760 memset (edge_port, 0, sizeof(struct edgeport_port));
2761 spin_lock_init(&edge_port->ep_lock);
2762 edge_port->ep_out_buf = edge_buf_alloc(EDGE_OUT_BUF_SIZE);
2763 if (edge_port->ep_out_buf == NULL) {
2764 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
2768 edge_port->port = serial->port[i];
2769 edge_port->edge_serial = edge_serial;
2770 usb_set_serial_port_data(serial->port[i], edge_port);
2771 edge_port->bUartMode = 0; /* Default is RS232 */
2777 for (--i; i>=0; --i) {
2778 edge_port = usb_get_serial_port_data(serial->port[i]);
2779 edge_buf_free(edge_port->ep_out_buf);
2781 usb_set_serial_port_data(serial->port[i], NULL);
2783 kfree (edge_serial);
2784 usb_set_serial_data(serial, NULL);
2788 static void edge_shutdown (struct usb_serial *serial)
2791 struct edgeport_port *edge_port;
2793 dbg ("%s", __FUNCTION__);
2795 for (i=0; i < serial->num_ports; ++i) {
2796 edge_port = usb_get_serial_port_data(serial->port[i]);
2798 edge_buf_free(edge_port->ep_out_buf);
2801 usb_set_serial_port_data(serial->port[i], NULL);
2803 kfree (usb_get_serial_data(serial));
2804 usb_set_serial_data(serial, NULL);
2808 /* Circular Buffer */
2813 * Allocate a circular buffer and all associated memory.
2816 static struct edge_buf *edge_buf_alloc(unsigned int size)
2818 struct edge_buf *eb;
2824 eb = (struct edge_buf *)kmalloc(sizeof(struct edge_buf), GFP_KERNEL);
2828 eb->buf_buf = kmalloc(size, GFP_KERNEL);
2829 if (eb->buf_buf == NULL) {
2834 eb->buf_size = size;
2835 eb->buf_get = eb->buf_put = eb->buf_buf;
2844 * Free the buffer and all associated memory.
2847 void edge_buf_free(struct edge_buf *eb)
2859 * Clear out all data in the circular buffer.
2862 static void edge_buf_clear(struct edge_buf *eb)
2865 eb->buf_get = eb->buf_put;
2866 /* equivalent to a get of all data available */
2871 * edge_buf_data_avail
2873 * Return the number of bytes of data available in the circular
2877 static unsigned int edge_buf_data_avail(struct edge_buf *eb)
2880 return ((eb->buf_size + eb->buf_put - eb->buf_get) % eb->buf_size);
2887 * edge_buf_space_avail
2889 * Return the number of bytes of space available in the circular
2893 static unsigned int edge_buf_space_avail(struct edge_buf *eb)
2896 return ((eb->buf_size + eb->buf_get - eb->buf_put - 1) % eb->buf_size);
2905 * Copy data data from a user buffer and put it into the circular buffer.
2906 * Restrict to the amount of space available.
2908 * Return the number of bytes copied.
2911 static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
2920 len = edge_buf_space_avail(eb);
2927 len = eb->buf_buf + eb->buf_size - eb->buf_put;
2929 memcpy(eb->buf_put, buf, len);
2930 memcpy(eb->buf_buf, buf+len, count - len);
2931 eb->buf_put = eb->buf_buf + count - len;
2933 memcpy(eb->buf_put, buf, count);
2935 eb->buf_put += count;
2936 else /* count == len */
2937 eb->buf_put = eb->buf_buf;
2947 * Get data from the circular buffer and copy to the given buffer.
2948 * Restrict to the amount of data available.
2950 * Return the number of bytes copied.
2953 static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
2962 len = edge_buf_data_avail(eb);
2969 len = eb->buf_buf + eb->buf_size - eb->buf_get;
2971 memcpy(buf, eb->buf_get, len);
2972 memcpy(buf+len, eb->buf_buf, count - len);
2973 eb->buf_get = eb->buf_buf + count - len;
2975 memcpy(buf, eb->buf_get, count);
2977 eb->buf_get += count;
2978 else /* count == len */
2979 eb->buf_get = eb->buf_buf;
2986 static struct usb_serial_driver edgeport_1port_device = {
2988 .owner = THIS_MODULE,
2989 .name = "edgeport_ti_1",
2991 .description = "Edgeport TI 1 port adapter",
2992 .id_table = edgeport_1port_id_table,
2993 .num_interrupt_in = 1,
2998 .close = edge_close,
2999 .throttle = edge_throttle,
3000 .unthrottle = edge_unthrottle,
3001 .attach = edge_startup,
3002 .shutdown = edge_shutdown,
3003 .ioctl = edge_ioctl,
3004 .set_termios = edge_set_termios,
3005 .tiocmget = edge_tiocmget,
3006 .tiocmset = edge_tiocmset,
3007 .write = edge_write,
3008 .write_room = edge_write_room,
3009 .chars_in_buffer = edge_chars_in_buffer,
3010 .break_ctl = edge_break,
3011 .read_int_callback = edge_interrupt_callback,
3012 .read_bulk_callback = edge_bulk_in_callback,
3013 .write_bulk_callback = edge_bulk_out_callback,
3016 static struct usb_serial_driver edgeport_2port_device = {
3018 .owner = THIS_MODULE,
3019 .name = "edgeport_ti_2",
3021 .description = "Edgeport TI 2 port adapter",
3022 .id_table = edgeport_2port_id_table,
3023 .num_interrupt_in = 1,
3028 .close = edge_close,
3029 .throttle = edge_throttle,
3030 .unthrottle = edge_unthrottle,
3031 .attach = edge_startup,
3032 .shutdown = edge_shutdown,
3033 .ioctl = edge_ioctl,
3034 .set_termios = edge_set_termios,
3035 .tiocmget = edge_tiocmget,
3036 .tiocmset = edge_tiocmset,
3037 .write = edge_write,
3038 .write_room = edge_write_room,
3039 .chars_in_buffer = edge_chars_in_buffer,
3040 .break_ctl = edge_break,
3041 .read_int_callback = edge_interrupt_callback,
3042 .read_bulk_callback = edge_bulk_in_callback,
3043 .write_bulk_callback = edge_bulk_out_callback,
3047 static int __init edgeport_init(void)
3050 retval = usb_serial_register(&edgeport_1port_device);
3052 goto failed_1port_device_register;
3053 retval = usb_serial_register(&edgeport_2port_device);
3055 goto failed_2port_device_register;
3056 retval = usb_register(&io_driver);
3058 goto failed_usb_register;
3059 info(DRIVER_DESC " " DRIVER_VERSION);
3061 failed_usb_register:
3062 usb_serial_deregister(&edgeport_2port_device);
3063 failed_2port_device_register:
3064 usb_serial_deregister(&edgeport_1port_device);
3065 failed_1port_device_register:
3069 static void __exit edgeport_exit (void)
3071 usb_deregister (&io_driver);
3072 usb_serial_deregister (&edgeport_1port_device);
3073 usb_serial_deregister (&edgeport_2port_device);
3076 module_init(edgeport_init);
3077 module_exit(edgeport_exit);
3079 /* Module information */
3080 MODULE_AUTHOR(DRIVER_AUTHOR);
3081 MODULE_DESCRIPTION(DRIVER_DESC);
3082 MODULE_LICENSE("GPL");
3084 module_param(debug, bool, S_IRUGO | S_IWUSR);
3085 MODULE_PARM_DESC(debug, "Debug enabled or not");
3087 module_param(low_latency, bool, S_IRUGO | S_IWUSR);
3088 MODULE_PARM_DESC(low_latency, "Low latency enabled or not");
3090 module_param(closing_wait, int, S_IRUGO | S_IWUSR);
3091 MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
3093 module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
3094 MODULE_PARM_DESC(ignore_cpu_rev, "Ignore the cpu revision when connecting to a device");