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/kernel.h>
28 #include <linux/jiffies.h>
29 #include <linux/errno.h>
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/tty.h>
33 #include <linux/tty_driver.h>
34 #include <linux/tty_flip.h>
35 #include <linux/module.h>
36 #include <linux/spinlock.h>
37 #include <linux/serial.h>
38 #include <linux/ioctl.h>
39 #include <asm/uaccess.h>
40 #include <asm/semaphore.h>
41 #include <linux/usb.h>
42 #include <linux/usb/serial.h>
45 #include "io_usbvend.h"
51 #define DRIVER_VERSION "v0.7"
52 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
53 #define DRIVER_DESC "Edgeport USB Serial Driver"
56 /* firmware image code */
57 #define IMAGE_VERSION_NAME PagableOperationalCodeImageVersion
58 #define IMAGE_ARRAY_NAME PagableOperationalCodeImage
59 #define IMAGE_SIZE PagableOperationalCodeSize
60 #include "io_fw_down3.h" /* Define array OperationalCodeImage[] */
62 #define EPROM_PAGE_SIZE 64
65 struct edgeport_uart_buf_desc {
66 __u32 count; // Number of bytes currently in buffer
69 /* different hardware types */
70 #define HARDWARE_TYPE_930 0
71 #define HARDWARE_TYPE_TIUMP 1
73 // IOCTL_PRIVATE_TI_GET_MODE Definitions
74 #define TI_MODE_CONFIGURING 0 // Device has not entered start device
75 #define TI_MODE_BOOT 1 // Staying in boot mode
76 #define TI_MODE_DOWNLOAD 2 // Made it to download mode
77 #define TI_MODE_TRANSITIONING 3 // Currently in boot mode but transitioning to download mode
80 #define EDGE_READ_URB_RUNNING 0
81 #define EDGE_READ_URB_STOPPING 1
82 #define EDGE_READ_URB_STOPPED 2
84 #define EDGE_LOW_LATENCY 1
85 #define EDGE_CLOSING_WAIT 4000 /* in .01 sec */
87 #define EDGE_OUT_BUF_SIZE 1024
90 /* Product information read from the Edgeport */
93 int TiMode; // Current TI Mode
94 __u8 hardware_type; // Type of hardware
95 } __attribute__((packed));
99 unsigned int buf_size;
105 struct edgeport_port {
112 __u32 ump_read_timeout; /* Number of miliseconds the UMP will
113 wait without data before completing
118 struct edgeport_uart_buf_desc tx;
119 struct async_icount icount;
120 wait_queue_head_t delta_msr_wait; /* for handling sleeping while
121 waiting for msr change to
123 struct edgeport_serial *edge_serial;
124 struct usb_serial_port *port;
125 __u8 bUartMode; /* Port type, 0: RS232, etc. */
127 int ep_read_urb_state;
128 int ep_write_urb_in_use;
129 struct edge_buf *ep_out_buf;
132 struct edgeport_serial {
133 struct product_info product_info;
134 u8 TI_I2C_Type; // Type of I2C in UMP
135 u8 TiReadI2C; // Set to TRUE if we have read the I2c in Boot Mode
136 struct semaphore es_sem;
138 struct usb_serial *serial;
142 /* Devices that this driver supports */
143 static struct usb_device_id edgeport_1port_id_table [] = {
144 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
145 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
146 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
147 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
148 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
149 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
150 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
151 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
152 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
153 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
154 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
155 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
156 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
157 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
158 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
159 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
163 static struct usb_device_id edgeport_2port_id_table [] = {
164 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
165 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
166 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
167 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
168 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
169 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
170 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
171 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
172 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
173 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
174 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
175 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
176 // The 4-port shows up as two 2-port devices
177 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
181 /* Devices that this driver supports */
182 static struct usb_device_id id_table_combined [] = {
183 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
184 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
185 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
186 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
187 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
188 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
189 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
190 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
191 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
192 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
193 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
194 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
195 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
196 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
197 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
198 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
199 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
200 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
201 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
202 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
203 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
204 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
205 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
206 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
207 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
208 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
209 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
210 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
211 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
215 MODULE_DEVICE_TABLE (usb, id_table_combined);
217 static struct usb_driver io_driver = {
219 .probe = usb_serial_probe,
220 .disconnect = usb_serial_disconnect,
221 .id_table = id_table_combined,
226 static struct EDGE_FIRMWARE_VERSION_INFO OperationalCodeImageVersion;
230 static int TIStayInBootMode = 0;
231 static int low_latency = EDGE_LOW_LATENCY;
232 static int closing_wait = EDGE_CLOSING_WAIT;
233 static int ignore_cpu_rev = 0;
236 static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length);
238 static void stop_read(struct edgeport_port *edge_port);
239 static int restart_read(struct edgeport_port *edge_port);
241 static void edge_set_termios (struct usb_serial_port *port, struct ktermios *old_termios);
242 static void edge_send(struct usb_serial_port *port);
244 /* circular buffer */
245 static struct edge_buf *edge_buf_alloc(unsigned int size);
246 static void edge_buf_free(struct edge_buf *eb);
247 static void edge_buf_clear(struct edge_buf *eb);
248 static unsigned int edge_buf_data_avail(struct edge_buf *eb);
249 static unsigned int edge_buf_space_avail(struct edge_buf *eb);
250 static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
252 static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
256 static int TIReadVendorRequestSync (struct usb_device *dev,
265 status = usb_control_msg (dev,
266 usb_rcvctrlpipe(dev, 0),
278 if (status != size) {
279 dbg ("%s - wanted to write %d, but only wrote %d",
280 __FUNCTION__, size, status);
286 static int TISendVendorRequestSync (struct usb_device *dev,
295 status = usb_control_msg (dev,
296 usb_sndctrlpipe(dev, 0),
308 if (status != size) {
309 dbg ("%s - wanted to write %d, but only wrote %d",
310 __FUNCTION__, size, status);
316 static int TIWriteCommandSync (struct usb_device *dev, __u8 command,
317 __u8 moduleid, __u16 value, u8 *data,
320 return TISendVendorRequestSync (dev,
324 data, // TransferBuffer
325 size); // TransferBufferLength
329 /* clear tx/rx buffers and fifo in TI UMP */
330 static int TIPurgeDataSync (struct usb_serial_port *port, __u16 mask)
332 int port_number = port->number - port->serial->minor;
334 dbg ("%s - port %d, mask %x", __FUNCTION__, port_number, mask);
336 return TIWriteCommandSync (port->serial->dev,
338 (__u8)(UMPM_UART1_PORT + port_number),
345 * TIReadDownloadMemory - Read edgeport memory from TI chip
346 * @dev: usb device pointer
347 * @start_address: Device CPU address at which to read
348 * @length: Length of above data
349 * @address_type: Can read both XDATA and I2C
350 * @buffer: pointer to input data buffer
352 static int TIReadDownloadMemory(struct usb_device *dev, int start_address,
353 int length, __u8 address_type, __u8 *buffer)
357 __be16 be_start_address;
359 dbg ("%s - @ %x for %d", __FUNCTION__, start_address, length);
361 /* Read in blocks of 64 bytes
362 * (TI firmware can't handle more than 64 byte reads)
368 read_length = (__u8)length;
370 if (read_length > 1) {
371 dbg ("%s - @ %x for %d", __FUNCTION__,
372 start_address, read_length);
374 be_start_address = cpu_to_be16 (start_address);
375 status = TIReadVendorRequestSync (dev,
376 UMPC_MEMORY_READ, // Request
377 (__u16)address_type, // wValue (Address type)
378 (__force __u16)be_start_address, // wIndex (Address to read)
379 buffer, // TransferBuffer
380 read_length); // TransferBufferLength
383 dbg ("%s - ERROR %x", __FUNCTION__, status);
387 if (read_length > 1) {
388 usb_serial_debug_data(debug, &dev->dev, __FUNCTION__,
389 read_length, buffer);
392 /* Update pointers/length */
393 start_address += read_length;
394 buffer += read_length;
395 length -= read_length;
401 static int TIReadRam (struct usb_device *dev, int start_address, int length, __u8 *buffer)
403 return TIReadDownloadMemory (dev,
406 DTK_ADDR_SPACE_XDATA,
410 /* Read edgeport memory to a given block */
411 static int TIReadBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 * buffer)
416 for (i=0; i< length; i++) {
417 status = TIReadVendorRequestSync (serial->serial->dev,
418 UMPC_MEMORY_READ, // Request
419 serial->TI_I2C_Type, // wValue (Address type)
420 (__u16)(start_address+i), // wIndex
421 &buffer[i], // TransferBuffer
422 0x01); // TransferBufferLength
424 dbg ("%s - ERROR %x", __FUNCTION__, status);
429 dbg ("%s - start_address = %x, length = %d", __FUNCTION__, start_address, length);
430 usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, length, buffer);
432 serial->TiReadI2C = 1;
437 /* Write given block to TI EPROM memory */
438 static int TIWriteBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
444 /* Must do a read before write */
445 if (!serial->TiReadI2C) {
446 status = TIReadBootMemory(serial, 0, 1, &temp);
451 for (i=0; i < length; ++i) {
452 status = TISendVendorRequestSync (serial->serial->dev,
453 UMPC_MEMORY_WRITE, // Request
455 (__u16)(i+start_address), // wIndex
456 NULL, // TransferBuffer
457 0); // TransferBufferLength
462 dbg ("%s - start_sddr = %x, length = %d", __FUNCTION__, start_address, length);
463 usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, length, buffer);
469 /* Write edgeport I2C memory to TI chip */
470 static int TIWriteDownloadI2C (struct edgeport_serial *serial, int start_address, int length, __u8 address_type, __u8 *buffer)
474 __be16 be_start_address;
476 /* We can only send a maximum of 1 aligned byte page at a time */
478 /* calulate the number of bytes left in the first page */
479 write_length = EPROM_PAGE_SIZE - (start_address & (EPROM_PAGE_SIZE - 1));
481 if (write_length > length)
482 write_length = length;
484 dbg ("%s - BytesInFirstPage Addr = %x, length = %d", __FUNCTION__, start_address, write_length);
485 usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, write_length, buffer);
487 /* Write first page */
488 be_start_address = cpu_to_be16 (start_address);
489 status = TISendVendorRequestSync (serial->serial->dev,
490 UMPC_MEMORY_WRITE, // Request
491 (__u16)address_type, // wValue
492 (__force __u16)be_start_address, // wIndex
493 buffer, // TransferBuffer
496 dbg ("%s - ERROR %d", __FUNCTION__, status);
500 length -= write_length;
501 start_address += write_length;
502 buffer += write_length;
504 /* We should be aligned now -- can write max page size bytes at a time */
506 if (length > EPROM_PAGE_SIZE)
507 write_length = EPROM_PAGE_SIZE;
509 write_length = length;
511 dbg ("%s - Page Write Addr = %x, length = %d", __FUNCTION__, start_address, write_length);
512 usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, write_length, buffer);
514 /* Write next page */
515 be_start_address = cpu_to_be16 (start_address);
516 status = TISendVendorRequestSync (serial->serial->dev,
517 UMPC_MEMORY_WRITE, // Request
518 (__u16)address_type, // wValue
519 (__force __u16)be_start_address, // wIndex
520 buffer, // TransferBuffer
521 write_length); // TransferBufferLength
523 dev_err (&serial->serial->dev->dev, "%s - ERROR %d\n", __FUNCTION__, status);
527 length -= write_length;
528 start_address += write_length;
529 buffer += write_length;
534 /* Examine the UMP DMA registers and LSR
536 * Check the MSBit of the X and Y DMA byte count registers.
537 * A zero in this bit indicates that the TX DMA buffers are empty
538 * then check the TX Empty bit in the UART.
540 static int TIIsTxActive (struct edgeport_port *port)
543 struct out_endpoint_desc_block *oedb;
547 oedb = kmalloc (sizeof (* oedb), GFP_KERNEL);
549 dev_err (&port->port->dev, "%s - out of memory\n", __FUNCTION__);
553 lsr = kmalloc (1, GFP_KERNEL); /* Sigh, that's right, just one byte,
554 as not all platforms can do DMA
560 /* Read the DMA Count Registers */
561 status = TIReadRam (port->port->serial->dev,
567 goto exit_is_tx_active;
569 dbg ("%s - XByteCount 0x%X", __FUNCTION__, oedb->XByteCount);
572 status = TIReadRam (port->port->serial->dev,
573 port->uart_base + UMPMEM_OFFS_UART_LSR,
578 goto exit_is_tx_active;
579 dbg ("%s - LSR = 0x%X", __FUNCTION__, *lsr);
581 /* If either buffer has data or we are transmitting then return TRUE */
582 if ((oedb->XByteCount & 0x80 ) != 0 )
585 if ((*lsr & UMP_UART_LSR_TX_MASK ) == 0 )
588 /* We return Not Active if we get any kind of error */
590 dbg ("%s - return %d", __FUNCTION__, bytes_left );
597 static void TIChasePort(struct edgeport_port *port, unsigned long timeout, int flush)
600 struct tty_struct *tty = port->port->tty;
605 timeout = (HZ*EDGE_CLOSING_WAIT)/100;
607 /* wait for data to drain from the buffer */
608 spin_lock_irqsave(&port->ep_lock, flags);
609 init_waitqueue_entry(&wait, current);
610 add_wait_queue(&tty->write_wait, &wait);
612 set_current_state(TASK_INTERRUPTIBLE);
613 if (edge_buf_data_avail(port->ep_out_buf) == 0
614 || timeout == 0 || signal_pending(current)
615 || !usb_get_intfdata(port->port->serial->interface)) /* disconnect */
617 spin_unlock_irqrestore(&port->ep_lock, flags);
618 timeout = schedule_timeout(timeout);
619 spin_lock_irqsave(&port->ep_lock, flags);
621 set_current_state(TASK_RUNNING);
622 remove_wait_queue(&tty->write_wait, &wait);
624 edge_buf_clear(port->ep_out_buf);
625 spin_unlock_irqrestore(&port->ep_lock, flags);
627 /* wait for data to drain from the device */
629 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
630 && usb_get_intfdata(port->port->serial->interface)) { /* not disconnected */
631 if (!TIIsTxActive(port))
637 if (!usb_get_intfdata(port->port->serial->interface))
640 /* wait one more character time, based on baud rate */
641 /* (TIIsTxActive doesn't seem to wait for the last byte) */
642 if ((baud_rate=port->baud_rate) == 0)
644 msleep(max(1,(10000+baud_rate-1)/baud_rate));
647 static int TIChooseConfiguration (struct usb_device *dev)
649 // There may be multiple configurations on this device, in which case
650 // we would need to read and parse all of them to find out which one
651 // we want. However, we just support one config at this point,
652 // configuration # 1, which is Config Descriptor 0.
654 dbg ("%s - Number of Interfaces = %d", __FUNCTION__, dev->config->desc.bNumInterfaces);
655 dbg ("%s - MAX Power = %d", __FUNCTION__, dev->config->desc.bMaxPower*2);
657 if (dev->config->desc.bNumInterfaces != 1) {
658 dev_err (&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __FUNCTION__);
665 static int TIReadRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
669 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
670 status = TIReadDownloadMemory (serial->serial->dev,
676 status = TIReadBootMemory (serial,
685 static int TIWriteRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
687 if (serial->product_info.TiMode == TI_MODE_BOOT)
688 return TIWriteBootMemory (serial,
693 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
694 return TIWriteDownloadI2C (serial,
705 /* Read a descriptor header from I2C based on type */
706 static int TIGetDescriptorAddress (struct edgeport_serial *serial, int desc_type, struct ti_i2c_desc *rom_desc)
711 /* Search for requested descriptor in I2C */
714 status = TIReadRom (serial,
716 sizeof(struct ti_i2c_desc),
721 if (rom_desc->Type == desc_type)
722 return start_address;
724 start_address = start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size;
726 } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
731 /* Validate descriptor checksum */
732 static int ValidChecksum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
737 for (i=0; i < rom_desc->Size; i++) {
738 cs = (__u8)(cs + buffer[i]);
740 if (cs != rom_desc->CheckSum) {
741 dbg ("%s - Mismatch %x - %x", __FUNCTION__, rom_desc->CheckSum, cs);
747 /* Make sure that the I2C image is good */
748 static int TiValidateI2cImage (struct edgeport_serial *serial)
750 struct device *dev = &serial->serial->dev->dev;
752 struct ti_i2c_desc *rom_desc;
753 int start_address = 2;
757 rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
759 dev_err (dev, "%s - out of memory\n", __FUNCTION__);
762 buffer = kmalloc (TI_MAX_I2C_SIZE, GFP_KERNEL);
764 dev_err (dev, "%s - out of memory when allocating buffer\n", __FUNCTION__);
769 // Read the first byte (Signature0) must be 0x52 or 0x10
770 status = TIReadRom (serial, 0, 1, buffer);
772 goto ExitTiValidateI2cImage;
774 if (*buffer != UMP5152 && *buffer != UMP3410) {
775 dev_err (dev, "%s - invalid buffer signature\n", __FUNCTION__);
777 goto ExitTiValidateI2cImage;
782 status = TIReadRom (serial,
784 sizeof(struct ti_i2c_desc),
789 if ((start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size) > TI_MAX_I2C_SIZE) {
791 dbg ("%s - structure too big, erroring out.", __FUNCTION__);
795 dbg ("%s Type = 0x%x", __FUNCTION__, rom_desc->Type);
797 // Skip type 2 record
798 ttype = rom_desc->Type & 0x0f;
799 if ( ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
800 && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO ) {
801 // Read the descriptor data
802 status = TIReadRom(serial,
803 start_address+sizeof(struct ti_i2c_desc),
809 status = ValidChecksum(rom_desc, buffer);
813 start_address = start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size;
815 } while ((rom_desc->Type != I2C_DESC_TYPE_ION) && (start_address < TI_MAX_I2C_SIZE));
817 if ((rom_desc->Type != I2C_DESC_TYPE_ION) || (start_address > TI_MAX_I2C_SIZE))
820 ExitTiValidateI2cImage:
826 static int TIReadManufDescriptor (struct edgeport_serial *serial, __u8 *buffer)
830 struct ti_i2c_desc *rom_desc;
831 struct edge_ti_manuf_descriptor *desc;
833 rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
835 dev_err (&serial->serial->dev->dev, "%s - out of memory\n", __FUNCTION__);
838 start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_ION, rom_desc);
840 if (!start_address) {
841 dbg ("%s - Edge Descriptor not found in I2C", __FUNCTION__);
846 // Read the descriptor data
847 status = TIReadRom (serial,
848 start_address+sizeof(struct ti_i2c_desc),
854 status = ValidChecksum(rom_desc, buffer);
856 desc = (struct edge_ti_manuf_descriptor *)buffer;
857 dbg ( "%s - IonConfig 0x%x", __FUNCTION__, desc->IonConfig );
858 dbg ( "%s - Version %d", __FUNCTION__, desc->Version );
859 dbg ( "%s - Cpu/Board 0x%x", __FUNCTION__, desc->CpuRev_BoardRev );
860 dbg ( "%s - NumPorts %d", __FUNCTION__, desc->NumPorts );
861 dbg ( "%s - NumVirtualPorts %d", __FUNCTION__, desc->NumVirtualPorts );
862 dbg ( "%s - TotalPorts %d", __FUNCTION__, desc->TotalPorts );
869 /* Build firmware header used for firmware update */
870 static int BuildI2CFirmwareHeader (__u8 *header, struct device *dev)
876 struct ti_i2c_desc *i2c_header;
877 struct ti_i2c_image_header *img_header;
878 struct ti_i2c_firmware_rec *firmware_rec;
880 // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
881 // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver
882 // will download the latest firmware (padded to 15.5k) into the UMP ram.
883 // And finally when the device comes back up in download mode the driver will cause
884 // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
885 // the record type from 0xf2 to 0x02.
887 // Allocate a 15.5k buffer + 2 bytes for version number (Firmware Record)
888 buffer_size = (((1024 * 16) - 512 )+ sizeof(struct ti_i2c_firmware_rec));
890 buffer = kmalloc (buffer_size, GFP_KERNEL);
892 dev_err (dev, "%s - out of memory\n", __FUNCTION__);
896 // Set entire image of 0xffs
897 memset (buffer, 0xff, buffer_size);
899 // Copy version number into firmware record
900 firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
902 firmware_rec->Ver_Major = OperationalCodeImageVersion.MajorVersion;
903 firmware_rec->Ver_Minor = OperationalCodeImageVersion.MinorVersion;
905 // Pointer to fw_down memory image
906 img_header = (struct ti_i2c_image_header *)&PagableOperationalCodeImage[0];
908 memcpy (buffer + sizeof(struct ti_i2c_firmware_rec),
909 &PagableOperationalCodeImage[sizeof(struct ti_i2c_image_header)],
910 le16_to_cpu(img_header->Length));
912 for (i=0; i < buffer_size; i++) {
913 cs = (__u8)(cs + buffer[i]);
919 i2c_header = (struct ti_i2c_desc *)header;
920 firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data;
922 i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK;
923 i2c_header->Size = (__u16)buffer_size;
924 i2c_header->CheckSum = cs;
925 firmware_rec->Ver_Major = OperationalCodeImageVersion.MajorVersion;
926 firmware_rec->Ver_Minor = OperationalCodeImageVersion.MinorVersion;
931 /* Try to figure out what type of I2c we have */
932 static int TIGetI2cTypeInBootMode (struct edgeport_serial *serial)
937 // Try to read type 2
938 status = TIReadVendorRequestSync (serial->serial->dev,
939 UMPC_MEMORY_READ, // Request
940 DTK_ADDR_SPACE_I2C_TYPE_II, // wValue (Address type)
942 &data, // TransferBuffer
943 0x01); // TransferBufferLength
945 dbg ("%s - read 2 status error = %d", __FUNCTION__, status);
947 dbg ("%s - read 2 data = 0x%x", __FUNCTION__, data);
948 if ((!status) && (data == UMP5152 || data == UMP3410)) {
949 dbg ("%s - ROM_TYPE_II", __FUNCTION__);
950 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
954 // Try to read type 3
955 status = TIReadVendorRequestSync (serial->serial->dev,
956 UMPC_MEMORY_READ, // Request
957 DTK_ADDR_SPACE_I2C_TYPE_III, // wValue (Address type)
959 &data, // TransferBuffer
960 0x01); // TransferBufferLength
962 dbg ("%s - read 3 status error = %d", __FUNCTION__, status);
964 dbg ("%s - read 2 data = 0x%x", __FUNCTION__, data);
965 if ((!status) && (data == UMP5152 || data == UMP3410)) {
966 dbg ("%s - ROM_TYPE_III", __FUNCTION__);
967 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
971 dbg ("%s - Unknown", __FUNCTION__);
972 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
976 static int TISendBulkTransferSync (struct usb_serial *serial, void *buffer, int length, int *num_sent)
980 status = usb_bulk_msg (serial->dev,
981 usb_sndbulkpipe(serial->dev,
982 serial->port[0]->bulk_out_endpointAddress),
990 /* Download given firmware image to the device (IN BOOT MODE) */
991 static int TIDownloadCodeImage (struct edgeport_serial *serial, __u8 *image, int image_length)
998 // Transfer firmware image
999 for (pos = 0; pos < image_length; ) {
1000 // Read the next buffer from file
1001 transfer = image_length - pos;
1002 if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
1003 transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
1006 status = TISendBulkTransferSync (serial->serial, &image[pos], transfer, &done);
1009 // Advance buffer pointer
1017 static int TIConfigureBootDevice (struct usb_device *dev)
1023 * DownloadTIFirmware - Download run-time operating firmware to the TI5052
1025 * This routine downloads the main operating code into the TI5052, using the
1026 * boot code already burned into E2PROM or ROM.
1028 static int TIDownloadFirmware (struct edgeport_serial *serial)
1030 struct device *dev = &serial->serial->dev->dev;
1033 struct edge_ti_manuf_descriptor *ti_manuf_desc;
1034 struct usb_interface_descriptor *interface;
1035 int download_cur_ver;
1036 int download_new_ver;
1038 /* This routine is entered by both the BOOT mode and the Download mode
1039 * We can determine which code is running by the reading the config
1040 * descriptor and if we have only one bulk pipe it is in boot mode
1042 serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
1044 /* Default to type 2 i2c */
1045 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1047 status = TIChooseConfiguration (serial->serial->dev);
1051 interface = &serial->serial->interface->cur_altsetting->desc;
1053 dev_err (dev, "%s - no interface set, error!\n", __FUNCTION__);
1057 // Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
1058 // if we have more than one endpoint we are definitely in download mode
1059 if (interface->bNumEndpoints > 1)
1060 serial->product_info.TiMode = TI_MODE_DOWNLOAD;
1062 // Otherwise we will remain in configuring mode
1063 serial->product_info.TiMode = TI_MODE_CONFIGURING;
1065 // Save Download Version Number
1066 OperationalCodeImageVersion.MajorVersion = PagableOperationalCodeImageVersion.MajorVersion;
1067 OperationalCodeImageVersion.MinorVersion = PagableOperationalCodeImageVersion.MinorVersion;
1068 OperationalCodeImageVersion.BuildNumber = PagableOperationalCodeImageVersion.BuildNumber;
1070 /********************************************************************/
1072 /********************************************************************/
1073 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
1074 struct ti_i2c_desc *rom_desc;
1076 dbg ("%s - <<<<<<<<<<<<<<<RUNNING IN DOWNLOAD MODE>>>>>>>>>>", __FUNCTION__);
1078 status = TiValidateI2cImage (serial);
1080 dbg ("%s - <<<<<<<<<<<<<<<DOWNLOAD MODE -- BAD I2C >>>>>>>>>>",
1085 /* Validate Hardware version number
1086 * Read Manufacturing Descriptor from TI Based Edgeport
1088 ti_manuf_desc = kmalloc (sizeof (*ti_manuf_desc), GFP_KERNEL);
1089 if (!ti_manuf_desc) {
1090 dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
1093 status = TIReadManufDescriptor (serial, (__u8 *)ti_manuf_desc);
1095 kfree (ti_manuf_desc);
1099 // Check version number of ION descriptor
1100 if (!ignore_cpu_rev && TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev) < 2) {
1101 dbg ( "%s - Wrong CPU Rev %d (Must be 2)", __FUNCTION__,
1102 TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev));
1103 kfree (ti_manuf_desc);
1107 rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
1109 dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
1110 kfree (ti_manuf_desc);
1114 // Search for type 2 record (firmware record)
1115 if ((start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc)) != 0) {
1116 struct ti_i2c_firmware_rec *firmware_version;
1119 dbg ("%s - Found Type FIRMWARE (Type 2) record", __FUNCTION__);
1121 firmware_version = kmalloc (sizeof (*firmware_version), GFP_KERNEL);
1122 if (!firmware_version) {
1123 dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
1125 kfree (ti_manuf_desc);
1129 // Validate version number
1130 // Read the descriptor data
1131 status = TIReadRom (serial,
1132 start_address+sizeof(struct ti_i2c_desc),
1133 sizeof(struct ti_i2c_firmware_rec),
1134 (__u8 *)firmware_version);
1136 kfree (firmware_version);
1138 kfree (ti_manuf_desc);
1142 // Check version number of download with current version in I2c
1143 download_cur_ver = (firmware_version->Ver_Major << 8) +
1144 (firmware_version->Ver_Minor);
1145 download_new_ver = (OperationalCodeImageVersion.MajorVersion << 8) +
1146 (OperationalCodeImageVersion.MinorVersion);
1148 dbg ("%s - >>>Firmware Versions Device %d.%d Driver %d.%d",
1150 firmware_version->Ver_Major,
1151 firmware_version->Ver_Minor,
1152 OperationalCodeImageVersion.MajorVersion,
1153 OperationalCodeImageVersion.MinorVersion);
1155 // Check if we have an old version in the I2C and update if necessary
1156 if (download_cur_ver != download_new_ver) {
1157 dbg ("%s - Update I2C Download from %d.%d to %d.%d",
1159 firmware_version->Ver_Major,
1160 firmware_version->Ver_Minor,
1161 OperationalCodeImageVersion.MajorVersion,
1162 OperationalCodeImageVersion.MinorVersion);
1164 // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
1165 // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver
1166 // will download the latest firmware (padded to 15.5k) into the UMP ram.
1167 // And finally when the device comes back up in download mode the driver will cause
1168 // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
1169 // the record type from 0xf2 to 0x02.
1171 record = I2C_DESC_TYPE_FIRMWARE_BLANK;
1173 // Change the I2C Firmware record type to 0xf2 to trigger an update
1174 status = TIWriteRom (serial,
1179 kfree (firmware_version);
1181 kfree (ti_manuf_desc);
1185 // verify the write -- must do this in order for write to
1186 // complete before we do the hardware reset
1187 status = TIReadRom (serial,
1193 kfree (firmware_version);
1195 kfree (ti_manuf_desc);
1199 if (record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
1200 dev_err (dev, "%s - error resetting device\n", __FUNCTION__);
1201 kfree (firmware_version);
1203 kfree (ti_manuf_desc);
1207 dbg ("%s - HARDWARE RESET", __FUNCTION__);
1209 // Reset UMP -- Back to BOOT MODE
1210 status = TISendVendorRequestSync (serial->serial->dev,
1211 UMPC_HARDWARE_RESET, // Request
1214 NULL, // TransferBuffer
1215 0); // TransferBufferLength
1217 dbg ( "%s - HARDWARE RESET return %d", __FUNCTION__, status);
1219 /* return an error on purpose. */
1220 kfree (firmware_version);
1222 kfree (ti_manuf_desc);
1225 kfree (firmware_version);
1227 // Search for type 0xF2 record (firmware blank record)
1228 else if ((start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
1229 #define HEADER_SIZE (sizeof(struct ti_i2c_desc) + sizeof(struct ti_i2c_firmware_rec))
1233 header = kmalloc (HEADER_SIZE, GFP_KERNEL);
1235 dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
1237 kfree (ti_manuf_desc);
1241 vheader = kmalloc (HEADER_SIZE, GFP_KERNEL);
1243 dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
1246 kfree (ti_manuf_desc);
1250 dbg ("%s - Found Type BLANK FIRMWARE (Type F2) record", __FUNCTION__);
1252 // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
1253 // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver
1254 // will download the latest firmware (padded to 15.5k) into the UMP ram.
1255 // And finally when the device comes back up in download mode the driver will cause
1256 // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
1257 // the record type from 0xf2 to 0x02.
1258 status = BuildI2CFirmwareHeader(header, dev);
1263 kfree (ti_manuf_desc);
1267 // Update I2C with type 0xf2 record with correct size and checksum
1268 status = TIWriteRom (serial,
1276 kfree (ti_manuf_desc);
1280 // verify the write -- must do this in order for write to
1281 // complete before we do the hardware reset
1282 status = TIReadRom (serial,
1288 dbg ("%s - can't read header back", __FUNCTION__);
1292 kfree (ti_manuf_desc);
1295 if (memcmp(vheader, header, HEADER_SIZE)) {
1296 dbg ("%s - write download record failed", __FUNCTION__);
1300 kfree (ti_manuf_desc);
1307 dbg ("%s - Start firmware update", __FUNCTION__);
1309 // Tell firmware to copy download image into I2C
1310 status = TISendVendorRequestSync (serial->serial->dev,
1311 UMPC_COPY_DNLD_TO_I2C, // Request
1314 NULL, // TransferBuffer
1315 0); // TransferBufferLength
1317 dbg ("%s - Update complete 0x%x", __FUNCTION__, status);
1319 dev_err (dev, "%s - UMPC_COPY_DNLD_TO_I2C failed\n", __FUNCTION__);
1321 kfree (ti_manuf_desc);
1326 // The device is running the download code
1328 kfree (ti_manuf_desc);
1332 /********************************************************************/
1334 /********************************************************************/
1335 dbg ("%s - <<<<<<<<<<<<<<<RUNNING IN BOOT MODE>>>>>>>>>>>>>>>",
1338 // Configure the TI device so we can use the BULK pipes for download
1339 status = TIConfigureBootDevice (serial->serial->dev);
1343 if (le16_to_cpu(serial->serial->dev->descriptor.idVendor) != USB_VENDOR_ID_ION) {
1344 dbg ("%s - VID = 0x%x", __FUNCTION__,
1345 le16_to_cpu(serial->serial->dev->descriptor.idVendor));
1346 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1347 goto StayInBootMode;
1350 // We have an ION device (I2c Must be programmed)
1351 // Determine I2C image type
1352 if (TIGetI2cTypeInBootMode(serial)) {
1353 goto StayInBootMode;
1356 // Registry variable set?
1357 if (TIStayInBootMode) {
1358 dbg ("%s - TIStayInBootMode", __FUNCTION__);
1359 goto StayInBootMode;
1362 // Check for ION Vendor ID and that the I2C is valid
1363 if (!TiValidateI2cImage(serial)) {
1364 struct ti_i2c_image_header *header;
1370 /* Validate Hardware version number
1371 * Read Manufacturing Descriptor from TI Based Edgeport
1373 ti_manuf_desc = kmalloc (sizeof (*ti_manuf_desc), GFP_KERNEL);
1374 if (!ti_manuf_desc) {
1375 dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
1378 status = TIReadManufDescriptor (serial, (__u8 *)ti_manuf_desc);
1380 kfree (ti_manuf_desc);
1381 goto StayInBootMode;
1384 // Check for version 2
1385 if (!ignore_cpu_rev && TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev) < 2) {
1386 dbg ("%s - Wrong CPU Rev %d (Must be 2)", __FUNCTION__,
1387 TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev));
1388 kfree (ti_manuf_desc);
1389 goto StayInBootMode;
1392 kfree (ti_manuf_desc);
1394 // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
1395 // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver
1396 // will download the latest firmware (padded to 15.5k) into the UMP ram.
1397 // And finally when the device comes back up in download mode the driver will cause
1398 // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
1399 // the record type from 0xf2 to 0x02.
1402 * Do we really have to copy the whole firmware image,
1403 * or could we do this in place!
1406 // Allocate a 15.5k buffer + 3 byte header
1407 buffer_size = (((1024 * 16) - 512) + sizeof(struct ti_i2c_image_header));
1408 buffer = kmalloc (buffer_size, GFP_KERNEL);
1410 dev_err (dev, "%s - out of memory\n", __FUNCTION__);
1414 // Initialize the buffer to 0xff (pad the buffer)
1415 memset (buffer, 0xff, buffer_size);
1417 memcpy (buffer, &PagableOperationalCodeImage[0], PagableOperationalCodeSize);
1419 for(i = sizeof(struct ti_i2c_image_header); i < buffer_size; i++) {
1420 cs = (__u8)(cs + buffer[i]);
1423 header = (struct ti_i2c_image_header *)buffer;
1425 // update length and checksum after padding
1426 header->Length = cpu_to_le16((__u16)(buffer_size - sizeof(struct ti_i2c_image_header)));
1427 header->CheckSum = cs;
1429 // Download the operational code
1430 dbg ("%s - Downloading operational code image (TI UMP)", __FUNCTION__);
1431 status = TIDownloadCodeImage (serial, buffer, buffer_size);
1436 dbg ("%s - Error downloading operational code image", __FUNCTION__);
1440 // Device will reboot
1441 serial->product_info.TiMode = TI_MODE_TRANSITIONING;
1443 dbg ("%s - Download successful -- Device rebooting...", __FUNCTION__);
1445 /* return an error on purpose */
1450 // Eprom is invalid or blank stay in boot mode
1451 dbg ("%s - <<<<<<<<<<<<<<<STAYING IN BOOT MODE>>>>>>>>>>>>", __FUNCTION__);
1452 serial->product_info.TiMode = TI_MODE_BOOT;
1458 static int TISetDtr (struct edgeport_port *port)
1460 int port_number = port->port->number - port->port->serial->minor;
1462 dbg ("%s", __FUNCTION__);
1463 port->shadow_mcr |= MCR_DTR;
1465 return TIWriteCommandSync (port->port->serial->dev,
1467 (__u8)(UMPM_UART1_PORT + port_number),
1473 static int TIClearDtr (struct edgeport_port *port)
1475 int port_number = port->port->number - port->port->serial->minor;
1477 dbg ("%s", __FUNCTION__);
1478 port->shadow_mcr &= ~MCR_DTR;
1480 return TIWriteCommandSync (port->port->serial->dev,
1482 (__u8)(UMPM_UART1_PORT + port_number),
1488 static int TISetRts (struct edgeport_port *port)
1490 int port_number = port->port->number - port->port->serial->minor;
1492 dbg ("%s", __FUNCTION__);
1493 port->shadow_mcr |= MCR_RTS;
1495 return TIWriteCommandSync (port->port->serial->dev,
1497 (__u8)(UMPM_UART1_PORT + port_number),
1503 static int TIClearRts (struct edgeport_port *port)
1505 int port_number = port->port->number - port->port->serial->minor;
1507 dbg ("%s", __FUNCTION__);
1508 port->shadow_mcr &= ~MCR_RTS;
1510 return TIWriteCommandSync (port->port->serial->dev,
1512 (__u8)(UMPM_UART1_PORT + port_number),
1518 static int TISetLoopBack (struct edgeport_port *port)
1520 int port_number = port->port->number - port->port->serial->minor;
1522 dbg ("%s", __FUNCTION__);
1524 return TIWriteCommandSync (port->port->serial->dev,
1525 UMPC_SET_CLR_LOOPBACK,
1526 (__u8)(UMPM_UART1_PORT + port_number),
1532 static int TIClearLoopBack (struct edgeport_port *port)
1534 int port_number = port->port->number - port->port->serial->minor;
1536 dbg ("%s", __FUNCTION__);
1538 return TIWriteCommandSync (port->port->serial->dev,
1539 UMPC_SET_CLR_LOOPBACK,
1540 (__u8)(UMPM_UART1_PORT + port_number),
1546 static int TISetBreak (struct edgeport_port *port)
1548 int port_number = port->port->number - port->port->serial->minor;
1550 dbg ("%s", __FUNCTION__);
1552 return TIWriteCommandSync (port->port->serial->dev,
1554 (__u8)(UMPM_UART1_PORT + port_number),
1560 static int TIClearBreak (struct edgeport_port *port)
1562 int port_number = port->port->number - port->port->serial->minor;
1564 dbg ("%s", __FUNCTION__);
1566 return TIWriteCommandSync (port->port->serial->dev,
1568 (__u8)(UMPM_UART1_PORT + port_number),
1574 static int TIRestoreMCR (struct edgeport_port *port, __u8 mcr)
1578 dbg ("%s - %x", __FUNCTION__, mcr);
1581 status = TISetDtr (port);
1583 status = TIClearDtr (port);
1589 status = TISetRts (port);
1591 status = TIClearRts (port);
1596 if (mcr & MCR_LOOPBACK)
1597 status = TISetLoopBack (port);
1599 status = TIClearLoopBack (port);
1606 /* Convert TI LSR to standard UART flags */
1607 static __u8 MapLineStatus (__u8 ti_lsr)
1611 #define MAP_FLAG(flagUmp, flagUart) \
1612 if (ti_lsr & flagUmp) \
1615 MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR) /* overrun */
1616 MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */
1617 MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */
1618 MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */
1619 MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* receive data available */
1620 MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* transmit holding register empty */
1627 static void handle_new_msr (struct edgeport_port *edge_port, __u8 msr)
1629 struct async_icount *icount;
1630 struct tty_struct *tty;
1632 dbg ("%s - %02x", __FUNCTION__, msr);
1634 if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR | EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
1635 icount = &edge_port->icount;
1637 /* update input line counters */
1638 if (msr & EDGEPORT_MSR_DELTA_CTS)
1640 if (msr & EDGEPORT_MSR_DELTA_DSR)
1642 if (msr & EDGEPORT_MSR_DELTA_CD)
1644 if (msr & EDGEPORT_MSR_DELTA_RI)
1646 wake_up_interruptible (&edge_port->delta_msr_wait);
1649 /* Save the new modem status */
1650 edge_port->shadow_msr = msr & 0xf0;
1652 tty = edge_port->port->tty;
1653 /* handle CTS flow control */
1654 if (tty && C_CRTSCTS(tty)) {
1655 if (msr & EDGEPORT_MSR_CTS) {
1656 tty->hw_stopped = 0;
1659 tty->hw_stopped = 1;
1666 static void handle_new_lsr (struct edgeport_port *edge_port, int lsr_data, __u8 lsr, __u8 data)
1668 struct async_icount *icount;
1669 __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
1671 dbg ("%s - %02x", __FUNCTION__, new_lsr);
1673 edge_port->shadow_lsr = lsr;
1675 if (new_lsr & LSR_BREAK) {
1677 * Parity and Framing errors only count if they
1678 * occur exclusive of a break being received.
1680 new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
1683 /* Place LSR data byte into Rx buffer */
1684 if (lsr_data && edge_port->port->tty)
1685 edge_tty_recv(&edge_port->port->dev, edge_port->port->tty, &data, 1);
1687 /* update input line counters */
1688 icount = &edge_port->icount;
1689 if (new_lsr & LSR_BREAK)
1691 if (new_lsr & LSR_OVER_ERR)
1693 if (new_lsr & LSR_PAR_ERR)
1695 if (new_lsr & LSR_FRM_ERR)
1700 static void edge_interrupt_callback (struct urb *urb)
1702 struct edgeport_serial *edge_serial = (struct edgeport_serial *)urb->context;
1703 struct usb_serial_port *port;
1704 struct edgeport_port *edge_port;
1705 unsigned char *data = urb->transfer_buffer;
1706 int length = urb->actual_length;
1713 dbg("%s", __FUNCTION__);
1715 switch (urb->status) {
1722 /* this urb is terminated, clean up */
1723 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
1726 dev_err(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __FUNCTION__, urb->status);
1731 dbg ("%s - no data in urb", __FUNCTION__);
1735 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __FUNCTION__, length, data);
1738 dbg ("%s - expecting packet of size 2, got %d", __FUNCTION__, length);
1742 port_number = TIUMP_GET_PORT_FROM_CODE (data[0]);
1743 function = TIUMP_GET_FUNC_FROM_CODE (data[0]);
1744 dbg ("%s - port_number %d, function %d, info 0x%x",
1745 __FUNCTION__, port_number, function, data[1]);
1746 port = edge_serial->serial->port[port_number];
1747 edge_port = usb_get_serial_port_data(port);
1749 dbg ("%s - edge_port not found", __FUNCTION__);
1753 case TIUMP_INTERRUPT_CODE_LSR:
1754 lsr = MapLineStatus(data[1]);
1755 if (lsr & UMP_UART_LSR_DATA_MASK) {
1756 /* Save the LSR event for bulk read completion routine */
1757 dbg ("%s - LSR Event Port %u LSR Status = %02x",
1758 __FUNCTION__, port_number, lsr);
1759 edge_port->lsr_event = 1;
1760 edge_port->lsr_mask = lsr;
1762 dbg ("%s - ===== Port %d LSR Status = %02x ======",
1763 __FUNCTION__, port_number, lsr);
1764 handle_new_lsr (edge_port, 0, lsr, 0);
1768 case TIUMP_INTERRUPT_CODE_MSR: // MSR
1769 /* Copy MSR from UMP */
1771 dbg ("%s - ===== Port %u MSR Status = %02x ======\n",
1772 __FUNCTION__, port_number, msr);
1773 handle_new_msr (edge_port, msr);
1777 dev_err (&urb->dev->dev, "%s - Unknown Interrupt code from UMP %x\n",
1778 __FUNCTION__, data[1]);
1784 status = usb_submit_urb (urb, GFP_ATOMIC);
1786 dev_err (&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
1787 __FUNCTION__, status);
1790 static void edge_bulk_in_callback (struct urb *urb)
1792 struct edgeport_port *edge_port = (struct edgeport_port *)urb->context;
1793 unsigned char *data = urb->transfer_buffer;
1794 struct tty_struct *tty;
1798 dbg("%s", __FUNCTION__);
1800 switch (urb->status) {
1807 /* this urb is terminated, clean up */
1808 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
1811 dev_err (&urb->dev->dev,"%s - nonzero read bulk status received: %d\n",
1812 __FUNCTION__, urb->status );
1815 if (urb->status == -EPIPE)
1819 dev_err(&urb->dev->dev,"%s - stopping read!\n", __FUNCTION__);
1823 port_number = edge_port->port->number - edge_port->port->serial->minor;
1825 if (edge_port->lsr_event) {
1826 edge_port->lsr_event = 0;
1827 dbg ("%s ===== Port %u LSR Status = %02x, Data = %02x ======",
1828 __FUNCTION__, port_number, edge_port->lsr_mask, *data);
1829 handle_new_lsr (edge_port, 1, edge_port->lsr_mask, *data);
1830 /* Adjust buffer length/pointer */
1831 --urb->actual_length;
1835 tty = edge_port->port->tty;
1836 if (tty && urb->actual_length) {
1837 usb_serial_debug_data(debug, &edge_port->port->dev, __FUNCTION__, urb->actual_length, data);
1839 if (edge_port->close_pending) {
1840 dbg ("%s - close is pending, dropping data on the floor.", __FUNCTION__);
1842 edge_tty_recv(&edge_port->port->dev, tty, data, urb->actual_length);
1844 edge_port->icount.rx += urb->actual_length;
1848 /* continue read unless stopped */
1849 spin_lock(&edge_port->ep_lock);
1850 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING) {
1851 urb->dev = edge_port->port->serial->dev;
1852 status = usb_submit_urb(urb, GFP_ATOMIC);
1853 } else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING) {
1854 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
1856 spin_unlock(&edge_port->ep_lock);
1858 dev_err (&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
1859 __FUNCTION__, status);
1862 static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length)
1867 cnt = tty_buffer_request_room(tty, length);
1869 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1870 __FUNCTION__, length - cnt);
1874 tty_insert_flip_string(tty, data, cnt);
1877 } while (length > 0);
1879 tty_flip_buffer_push(tty);
1882 static void edge_bulk_out_callback (struct urb *urb)
1884 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1885 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1887 dbg ("%s - port %d", __FUNCTION__, port->number);
1889 edge_port->ep_write_urb_in_use = 0;
1891 switch (urb->status) {
1898 /* this urb is terminated, clean up */
1899 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
1902 dev_err (&urb->dev->dev,"%s - nonzero write bulk status received: %d\n",
1903 __FUNCTION__, urb->status);
1906 /* send any buffered data */
1910 static int edge_open (struct usb_serial_port *port, struct file * filp)
1912 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1913 struct edgeport_serial *edge_serial;
1914 struct usb_device *dev;
1919 u8 transaction_timeout;
1921 dbg("%s - port %d", __FUNCTION__, port->number);
1923 if (edge_port == NULL)
1927 port->tty->low_latency = low_latency;
1929 port_number = port->number - port->serial->minor;
1930 switch (port_number) {
1932 edge_port->uart_base = UMPMEM_BASE_UART1;
1933 edge_port->dma_address = UMPD_OEDB1_ADDRESS;
1936 edge_port->uart_base = UMPMEM_BASE_UART2;
1937 edge_port->dma_address = UMPD_OEDB2_ADDRESS;
1940 dev_err (&port->dev, "Unknown port number!!!\n");
1944 dbg ("%s - port_number = %d, uart_base = %04x, dma_address = %04x",
1945 __FUNCTION__, port_number, edge_port->uart_base, edge_port->dma_address);
1947 dev = port->serial->dev;
1949 memset (&(edge_port->icount), 0x00, sizeof(edge_port->icount));
1950 init_waitqueue_head (&edge_port->delta_msr_wait);
1952 /* turn off loopback */
1953 status = TIClearLoopBack (edge_port);
1955 dev_err(&port->dev,"%s - cannot send clear loopback command, %d\n",
1956 __FUNCTION__, status);
1960 /* set up the port settings */
1961 edge_set_termios (port, NULL);
1963 /* open up the port */
1965 /* milliseconds to timeout for DMA transfer */
1966 transaction_timeout = 2;
1968 edge_port->ump_read_timeout = max (20, ((transaction_timeout * 3) / 2) );
1970 // milliseconds to timeout for DMA transfer
1971 open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
1972 UMP_PIPE_TRANS_TIMEOUT_ENA |
1973 (transaction_timeout << 2));
1975 dbg ("%s - Sending UMPC_OPEN_PORT", __FUNCTION__);
1977 /* Tell TI to open and start the port */
1978 status = TIWriteCommandSync (dev,
1980 (u8)(UMPM_UART1_PORT + port_number),
1985 dev_err(&port->dev,"%s - cannot send open command, %d\n", __FUNCTION__, status);
1989 /* Start the DMA? */
1990 status = TIWriteCommandSync (dev,
1992 (u8)(UMPM_UART1_PORT + port_number),
1997 dev_err(&port->dev,"%s - cannot send start DMA command, %d\n", __FUNCTION__, status);
2001 /* Clear TX and RX buffers in UMP */
2002 status = TIPurgeDataSync (port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
2004 dev_err(&port->dev,"%s - cannot send clear buffers command, %d\n", __FUNCTION__, status);
2008 /* Read Initial MSR */
2009 status = TIReadVendorRequestSync (dev,
2010 UMPC_READ_MSR, // Request
2012 (__u16)(UMPM_UART1_PORT + port_number), // wIndex (Address)
2013 &edge_port->shadow_msr, // TransferBuffer
2014 1); // TransferBufferLength
2016 dev_err(&port->dev,"%s - cannot send read MSR command, %d\n", __FUNCTION__, status);
2020 dbg ("ShadowMSR 0x%X", edge_port->shadow_msr);
2022 /* Set Initial MCR */
2023 edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
2024 dbg ("ShadowMCR 0x%X", edge_port->shadow_mcr);
2026 edge_serial = edge_port->edge_serial;
2027 if (down_interruptible(&edge_serial->es_sem))
2028 return -ERESTARTSYS;
2029 if (edge_serial->num_ports_open == 0) {
2030 /* we are the first port to be opened, let's post the interrupt urb */
2031 urb = edge_serial->serial->port[0]->interrupt_in_urb;
2033 dev_err (&port->dev, "%s - no interrupt urb present, exiting\n", __FUNCTION__);
2037 urb->complete = edge_interrupt_callback;
2038 urb->context = edge_serial;
2040 status = usb_submit_urb (urb, GFP_KERNEL);
2042 dev_err (&port->dev, "%s - usb_submit_urb failed with value %d\n", __FUNCTION__, status);
2048 * reset the data toggle on the bulk endpoints to work around bug in
2049 * host controllers where things get out of sync some times
2051 usb_clear_halt (dev, port->write_urb->pipe);
2052 usb_clear_halt (dev, port->read_urb->pipe);
2054 /* start up our bulk read urb */
2055 urb = port->read_urb;
2057 dev_err (&port->dev, "%s - no read urb present, exiting\n", __FUNCTION__);
2059 goto unlink_int_urb;
2061 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2062 urb->complete = edge_bulk_in_callback;
2063 urb->context = edge_port;
2065 status = usb_submit_urb (urb, GFP_KERNEL);
2067 dev_err (&port->dev, "%s - read bulk usb_submit_urb failed with value %d\n", __FUNCTION__, status);
2068 goto unlink_int_urb;
2071 ++edge_serial->num_ports_open;
2073 dbg("%s - exited", __FUNCTION__);
2078 if (edge_port->edge_serial->num_ports_open == 0)
2079 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2081 up(&edge_serial->es_sem);
2085 static void edge_close (struct usb_serial_port *port, struct file *filp)
2087 struct edgeport_serial *edge_serial;
2088 struct edgeport_port *edge_port;
2092 dbg("%s - port %d", __FUNCTION__, port->number);
2094 edge_serial = usb_get_serial_data(port->serial);
2095 edge_port = usb_get_serial_port_data(port);
2096 if ((edge_serial == NULL) || (edge_port == NULL))
2099 /* The bulkreadcompletion routine will check
2100 * this flag and dump add read data */
2101 edge_port->close_pending = 1;
2103 /* chase the port close and flush */
2104 TIChasePort (edge_port, (HZ*closing_wait)/100, 1);
2106 usb_kill_urb(port->read_urb);
2107 usb_kill_urb(port->write_urb);
2108 edge_port->ep_write_urb_in_use = 0;
2110 /* assuming we can still talk to the device,
2111 * send a close port command to it */
2112 dbg("%s - send umpc_close_port", __FUNCTION__);
2113 port_number = port->number - port->serial->minor;
2114 status = TIWriteCommandSync (port->serial->dev,
2116 (__u8)(UMPM_UART1_PORT + port_number),
2120 down(&edge_serial->es_sem);
2121 --edge_port->edge_serial->num_ports_open;
2122 if (edge_port->edge_serial->num_ports_open <= 0) {
2123 /* last port is now closed, let's shut down our interrupt urb */
2124 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2125 edge_port->edge_serial->num_ports_open = 0;
2127 up(&edge_serial->es_sem);
2128 edge_port->close_pending = 0;
2130 dbg("%s - exited", __FUNCTION__);
2133 static int edge_write (struct usb_serial_port *port, const unsigned char *data, int count)
2135 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2136 unsigned long flags;
2138 dbg("%s - port %d", __FUNCTION__, port->number);
2141 dbg("%s - write request of 0 bytes", __FUNCTION__);
2145 if (edge_port == NULL)
2147 if (edge_port->close_pending == 1)
2150 spin_lock_irqsave(&edge_port->ep_lock, flags);
2151 count = edge_buf_put(edge_port->ep_out_buf, data, count);
2152 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2159 static void edge_send(struct usb_serial_port *port)
2162 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2163 struct tty_struct *tty = port->tty;
2164 unsigned long flags;
2167 dbg("%s - port %d", __FUNCTION__, port->number);
2169 spin_lock_irqsave(&edge_port->ep_lock, flags);
2171 if (edge_port->ep_write_urb_in_use) {
2172 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2176 count = edge_buf_get(edge_port->ep_out_buf,
2177 port->write_urb->transfer_buffer,
2178 port->bulk_out_size);
2181 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2185 edge_port->ep_write_urb_in_use = 1;
2187 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2189 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, port->write_urb->transfer_buffer);
2191 /* set up our urb */
2192 usb_fill_bulk_urb (port->write_urb, port->serial->dev,
2193 usb_sndbulkpipe (port->serial->dev,
2194 port->bulk_out_endpointAddress),
2195 port->write_urb->transfer_buffer, count,
2196 edge_bulk_out_callback,
2199 /* send the data out the bulk port */
2200 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2202 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
2203 edge_port->ep_write_urb_in_use = 0;
2204 // TODO: reschedule edge_send
2206 edge_port->icount.tx += count;
2209 /* wakeup any process waiting for writes to complete */
2210 /* there is now more room in the buffer for new writes */
2212 /* let the tty driver wakeup if it has a special write_wakeup function */
2217 static int edge_write_room (struct usb_serial_port *port)
2219 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2221 unsigned long flags;
2223 dbg("%s - port %d", __FUNCTION__, port->number);
2225 if (edge_port == NULL)
2227 if (edge_port->close_pending == 1)
2230 spin_lock_irqsave(&edge_port->ep_lock, flags);
2231 room = edge_buf_space_avail(edge_port->ep_out_buf);
2232 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2234 dbg("%s - returns %d", __FUNCTION__, room);
2238 static int edge_chars_in_buffer (struct usb_serial_port *port)
2240 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2242 unsigned long flags;
2244 dbg("%s - port %d", __FUNCTION__, port->number);
2246 if (edge_port == NULL)
2248 if (edge_port->close_pending == 1)
2251 spin_lock_irqsave(&edge_port->ep_lock, flags);
2252 chars = edge_buf_data_avail(edge_port->ep_out_buf);
2253 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2255 dbg ("%s - returns %d", __FUNCTION__, chars);
2259 static void edge_throttle (struct usb_serial_port *port)
2261 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2262 struct tty_struct *tty;
2265 dbg("%s - port %d", __FUNCTION__, port->number);
2267 if (edge_port == NULL)
2272 dbg ("%s - no tty available", __FUNCTION__);
2276 /* if we are implementing XON/XOFF, send the stop character */
2278 unsigned char stop_char = STOP_CHAR(tty);
2279 status = edge_write (port, &stop_char, 1);
2281 dev_err(&port->dev, "%s - failed to write stop character, %d\n", __FUNCTION__, status);
2285 /* if we are implementing RTS/CTS, stop reads */
2286 /* and the Edgeport will clear the RTS line */
2288 stop_read(edge_port);
2292 static void edge_unthrottle (struct usb_serial_port *port)
2294 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2295 struct tty_struct *tty;
2298 dbg("%s - port %d", __FUNCTION__, port->number);
2300 if (edge_port == NULL)
2305 dbg ("%s - no tty available", __FUNCTION__);
2309 /* if we are implementing XON/XOFF, send the start character */
2311 unsigned char start_char = START_CHAR(tty);
2312 status = edge_write (port, &start_char, 1);
2314 dev_err(&port->dev, "%s - failed to write start character, %d\n", __FUNCTION__, status);
2318 /* if we are implementing RTS/CTS, restart reads */
2319 /* are the Edgeport will assert the RTS line */
2320 if (C_CRTSCTS(tty)) {
2321 status = restart_read(edge_port);
2323 dev_err(&port->dev, "%s - read bulk usb_submit_urb failed with value %d\n", __FUNCTION__, status);
2328 static void stop_read(struct edgeport_port *edge_port)
2330 unsigned long flags;
2332 spin_lock_irqsave(&edge_port->ep_lock, flags);
2334 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
2335 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
2336 edge_port->shadow_mcr &= ~MCR_RTS;
2338 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2341 static int restart_read(struct edgeport_port *edge_port)
2345 unsigned long flags;
2347 spin_lock_irqsave(&edge_port->ep_lock, flags);
2349 if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
2350 urb = edge_port->port->read_urb;
2351 urb->complete = edge_bulk_in_callback;
2352 urb->context = edge_port;
2353 urb->dev = edge_port->port->serial->dev;
2354 status = usb_submit_urb(urb, GFP_KERNEL);
2356 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2357 edge_port->shadow_mcr |= MCR_RTS;
2359 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2364 static void change_port_settings (struct edgeport_port *edge_port, struct ktermios *old_termios)
2366 struct ump_uart_config *config;
2367 struct tty_struct *tty;
2371 int port_number = edge_port->port->number - edge_port->port->serial->minor;
2373 dbg("%s - port %d", __FUNCTION__, edge_port->port->number);
2375 tty = edge_port->port->tty;
2378 dbg("%s - no tty structures", __FUNCTION__);
2382 config = kmalloc (sizeof (*config), GFP_KERNEL);
2384 dev_err (&edge_port->port->dev, "%s - out of memory\n", __FUNCTION__);
2388 cflag = tty->termios->c_cflag;
2392 /* These flags must be set */
2393 config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
2394 config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
2395 config->bUartMode = (__u8)(edge_port->bUartMode);
2397 switch (cflag & CSIZE) {
2399 config->bDataBits = UMP_UART_CHAR5BITS;
2400 dbg ("%s - data bits = 5", __FUNCTION__);
2403 config->bDataBits = UMP_UART_CHAR6BITS;
2404 dbg ("%s - data bits = 6", __FUNCTION__);
2407 config->bDataBits = UMP_UART_CHAR7BITS;
2408 dbg ("%s - data bits = 7", __FUNCTION__);
2412 config->bDataBits = UMP_UART_CHAR8BITS;
2413 dbg ("%s - data bits = 8", __FUNCTION__);
2417 if (cflag & PARENB) {
2418 if (cflag & PARODD) {
2419 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2420 config->bParity = UMP_UART_ODDPARITY;
2421 dbg("%s - parity = odd", __FUNCTION__);
2423 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2424 config->bParity = UMP_UART_EVENPARITY;
2425 dbg("%s - parity = even", __FUNCTION__);
2428 config->bParity = UMP_UART_NOPARITY;
2429 dbg("%s - parity = none", __FUNCTION__);
2432 if (cflag & CSTOPB) {
2433 config->bStopBits = UMP_UART_STOPBIT2;
2434 dbg("%s - stop bits = 2", __FUNCTION__);
2436 config->bStopBits = UMP_UART_STOPBIT1;
2437 dbg("%s - stop bits = 1", __FUNCTION__);
2440 /* figure out the flow control settings */
2441 if (cflag & CRTSCTS) {
2442 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
2443 config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
2444 dbg("%s - RTS/CTS is enabled", __FUNCTION__);
2446 dbg("%s - RTS/CTS is disabled", __FUNCTION__);
2447 tty->hw_stopped = 0;
2448 restart_read(edge_port);
2451 /* if we are implementing XON/XOFF, set the start and stop character in the device */
2452 if (I_IXOFF(tty) || I_IXON(tty)) {
2453 config->cXon = START_CHAR(tty);
2454 config->cXoff = STOP_CHAR(tty);
2456 /* if we are implementing INBOUND XON/XOFF */
2458 config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
2459 dbg ("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2460 __FUNCTION__, config->cXon, config->cXoff);
2462 dbg ("%s - INBOUND XON/XOFF is disabled", __FUNCTION__);
2465 /* if we are implementing OUTBOUND XON/XOFF */
2467 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
2468 dbg ("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2469 __FUNCTION__, config->cXon, config->cXoff);
2471 dbg ("%s - OUTBOUND XON/XOFF is disabled", __FUNCTION__);
2475 /* Round the baud rate */
2476 baud = tty_get_baud_rate(tty);
2478 /* pick a default, any default... */
2481 edge_port->baud_rate = baud;
2482 config->wBaudRate = (__u16)((461550L + baud/2) / baud);
2484 dbg ("%s - baud rate = %d, wBaudRate = %d", __FUNCTION__, baud, config->wBaudRate);
2486 dbg ("wBaudRate: %d", (int)(461550L / config->wBaudRate));
2487 dbg ("wFlags: 0x%x", config->wFlags);
2488 dbg ("bDataBits: %d", config->bDataBits);
2489 dbg ("bParity: %d", config->bParity);
2490 dbg ("bStopBits: %d", config->bStopBits);
2491 dbg ("cXon: %d", config->cXon);
2492 dbg ("cXoff: %d", config->cXoff);
2493 dbg ("bUartMode: %d", config->bUartMode);
2495 /* move the word values into big endian mode */
2496 cpu_to_be16s (&config->wFlags);
2497 cpu_to_be16s (&config->wBaudRate);
2499 status = TIWriteCommandSync (edge_port->port->serial->dev,
2501 (__u8)(UMPM_UART1_PORT + port_number),
2506 dbg ("%s - error %d when trying to write config to device",
2507 __FUNCTION__, status);
2515 static void edge_set_termios (struct usb_serial_port *port, struct ktermios *old_termios)
2517 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2518 struct tty_struct *tty = port->tty;
2521 if (!port->tty || !port->tty->termios) {
2522 dbg ("%s - no tty or termios", __FUNCTION__);
2526 cflag = tty->termios->c_cflag;
2527 /* check that they really want us to change something */
2529 if (cflag == old_termios->c_cflag &&
2530 tty->termios->c_iflag == old_termios->c_iflag) {
2531 dbg ("%s - nothing to change", __FUNCTION__);
2536 dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
2537 tty->termios->c_cflag, tty->termios->c_iflag);
2539 dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
2540 old_termios->c_cflag, old_termios->c_iflag);
2543 dbg("%s - port %d", __FUNCTION__, port->number);
2545 if (edge_port == NULL)
2548 /* change the port settings to the new ones specified */
2549 change_port_settings (edge_port, old_termios);
2554 static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear)
2556 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2559 dbg("%s - port %d", __FUNCTION__, port->number);
2561 mcr = edge_port->shadow_mcr;
2562 if (set & TIOCM_RTS)
2564 if (set & TIOCM_DTR)
2566 if (set & TIOCM_LOOP)
2567 mcr |= MCR_LOOPBACK;
2569 if (clear & TIOCM_RTS)
2571 if (clear & TIOCM_DTR)
2573 if (clear & TIOCM_LOOP)
2574 mcr &= ~MCR_LOOPBACK;
2576 edge_port->shadow_mcr = mcr;
2578 TIRestoreMCR (edge_port, mcr);
2583 static int edge_tiocmget(struct usb_serial_port *port, struct file *file)
2585 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2586 unsigned int result = 0;
2590 dbg("%s - port %d", __FUNCTION__, port->number);
2592 msr = edge_port->shadow_msr;
2593 mcr = edge_port->shadow_mcr;
2594 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
2595 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
2596 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
2597 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
2598 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
2599 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
2602 dbg("%s -- %x", __FUNCTION__, result);
2607 static int get_serial_info (struct edgeport_port *edge_port, struct serial_struct __user *retinfo)
2609 struct serial_struct tmp;
2614 memset(&tmp, 0, sizeof(tmp));
2616 tmp.type = PORT_16550A;
2617 tmp.line = edge_port->port->serial->minor;
2618 tmp.port = edge_port->port->number;
2620 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2621 tmp.xmit_fifo_size = edge_port->port->bulk_out_size;
2622 tmp.baud_base = 9600;
2623 tmp.close_delay = 5*HZ;
2624 tmp.closing_wait = closing_wait;
2625 // tmp.custom_divisor = state->custom_divisor;
2626 // tmp.hub6 = state->hub6;
2627 // tmp.io_type = state->io_type;
2630 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2635 static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
2637 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2638 struct async_icount cnow;
2639 struct async_icount cprev;
2641 dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
2645 dbg("%s - (%d) TIOCINQ", __FUNCTION__, port->number);
2646 // return get_number_bytes_avail(edge_port, (unsigned int *) arg);
2650 dbg("%s - (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
2651 // return get_lsr_info(edge_port, (unsigned int *) arg);
2655 dbg("%s - (%d) TIOCGSERIAL", __FUNCTION__, port->number);
2656 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
2660 dbg("%s - (%d) TIOCSSERIAL", __FUNCTION__, port->number);
2664 dbg("%s - (%d) TIOCMIWAIT", __FUNCTION__, port->number);
2665 cprev = edge_port->icount;
2667 interruptible_sleep_on(&edge_port->delta_msr_wait);
2668 /* see if a signal did it */
2669 if (signal_pending(current))
2670 return -ERESTARTSYS;
2671 cnow = edge_port->icount;
2672 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2673 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2674 return -EIO; /* no change => error */
2675 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2676 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2677 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2678 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
2687 dbg ("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__,
2688 port->number, edge_port->icount.rx, edge_port->icount.tx);
2689 if (copy_to_user((void __user *)arg, &edge_port->icount, sizeof(edge_port->icount)))
2694 return -ENOIOCTLCMD;
2697 static void edge_break (struct usb_serial_port *port, int break_state)
2699 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2702 dbg ("%s - state = %d", __FUNCTION__, break_state);
2704 /* chase the port close */
2705 TIChasePort (edge_port, 0, 0);
2707 if (break_state == -1) {
2708 status = TISetBreak (edge_port);
2710 status = TIClearBreak (edge_port);
2713 dbg ("%s - error %d sending break set/clear command.",
2714 __FUNCTION__, status);
2718 static int edge_startup (struct usb_serial *serial)
2720 struct edgeport_serial *edge_serial;
2721 struct edgeport_port *edge_port;
2722 struct usb_device *dev;
2728 /* create our private serial structure */
2729 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
2730 if (edge_serial == NULL) {
2731 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
2734 sema_init(&edge_serial->es_sem, 1);
2735 edge_serial->serial = serial;
2736 usb_set_serial_data(serial, edge_serial);
2738 status = TIDownloadFirmware (edge_serial);
2740 kfree (edge_serial);
2744 /* set up our port private structures */
2745 for (i = 0; i < serial->num_ports; ++i) {
2746 edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
2747 if (edge_port == NULL) {
2748 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
2751 spin_lock_init(&edge_port->ep_lock);
2752 edge_port->ep_out_buf = edge_buf_alloc(EDGE_OUT_BUF_SIZE);
2753 if (edge_port->ep_out_buf == NULL) {
2754 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
2758 edge_port->port = serial->port[i];
2759 edge_port->edge_serial = edge_serial;
2760 usb_set_serial_port_data(serial->port[i], edge_port);
2761 edge_port->bUartMode = 0; /* Default is RS232 */
2767 for (--i; i>=0; --i) {
2768 edge_port = usb_get_serial_port_data(serial->port[i]);
2769 edge_buf_free(edge_port->ep_out_buf);
2771 usb_set_serial_port_data(serial->port[i], NULL);
2773 kfree (edge_serial);
2774 usb_set_serial_data(serial, NULL);
2778 static void edge_shutdown (struct usb_serial *serial)
2781 struct edgeport_port *edge_port;
2783 dbg ("%s", __FUNCTION__);
2785 for (i=0; i < serial->num_ports; ++i) {
2786 edge_port = usb_get_serial_port_data(serial->port[i]);
2788 edge_buf_free(edge_port->ep_out_buf);
2791 usb_set_serial_port_data(serial->port[i], NULL);
2793 kfree (usb_get_serial_data(serial));
2794 usb_set_serial_data(serial, NULL);
2798 /* Circular Buffer */
2803 * Allocate a circular buffer and all associated memory.
2806 static struct edge_buf *edge_buf_alloc(unsigned int size)
2808 struct edge_buf *eb;
2814 eb = kmalloc(sizeof(struct edge_buf), GFP_KERNEL);
2818 eb->buf_buf = kmalloc(size, GFP_KERNEL);
2819 if (eb->buf_buf == NULL) {
2824 eb->buf_size = size;
2825 eb->buf_get = eb->buf_put = eb->buf_buf;
2834 * Free the buffer and all associated memory.
2837 static void edge_buf_free(struct edge_buf *eb)
2849 * Clear out all data in the circular buffer.
2852 static void edge_buf_clear(struct edge_buf *eb)
2855 eb->buf_get = eb->buf_put;
2856 /* equivalent to a get of all data available */
2861 * edge_buf_data_avail
2863 * Return the number of bytes of data available in the circular
2867 static unsigned int edge_buf_data_avail(struct edge_buf *eb)
2870 return ((eb->buf_size + eb->buf_put - eb->buf_get) % eb->buf_size);
2877 * edge_buf_space_avail
2879 * Return the number of bytes of space available in the circular
2883 static unsigned int edge_buf_space_avail(struct edge_buf *eb)
2886 return ((eb->buf_size + eb->buf_get - eb->buf_put - 1) % eb->buf_size);
2895 * Copy data data from a user buffer and put it into the circular buffer.
2896 * Restrict to the amount of space available.
2898 * Return the number of bytes copied.
2901 static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
2910 len = edge_buf_space_avail(eb);
2917 len = eb->buf_buf + eb->buf_size - eb->buf_put;
2919 memcpy(eb->buf_put, buf, len);
2920 memcpy(eb->buf_buf, buf+len, count - len);
2921 eb->buf_put = eb->buf_buf + count - len;
2923 memcpy(eb->buf_put, buf, count);
2925 eb->buf_put += count;
2926 else /* count == len */
2927 eb->buf_put = eb->buf_buf;
2937 * Get data from the circular buffer and copy to the given buffer.
2938 * Restrict to the amount of data available.
2940 * Return the number of bytes copied.
2943 static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
2952 len = edge_buf_data_avail(eb);
2959 len = eb->buf_buf + eb->buf_size - eb->buf_get;
2961 memcpy(buf, eb->buf_get, len);
2962 memcpy(buf+len, eb->buf_buf, count - len);
2963 eb->buf_get = eb->buf_buf + count - len;
2965 memcpy(buf, eb->buf_get, count);
2967 eb->buf_get += count;
2968 else /* count == len */
2969 eb->buf_get = eb->buf_buf;
2976 static struct usb_serial_driver edgeport_1port_device = {
2978 .owner = THIS_MODULE,
2979 .name = "edgeport_ti_1",
2981 .description = "Edgeport TI 1 port adapter",
2982 .id_table = edgeport_1port_id_table,
2983 .num_interrupt_in = 1,
2988 .close = edge_close,
2989 .throttle = edge_throttle,
2990 .unthrottle = edge_unthrottle,
2991 .attach = edge_startup,
2992 .shutdown = edge_shutdown,
2993 .ioctl = edge_ioctl,
2994 .set_termios = edge_set_termios,
2995 .tiocmget = edge_tiocmget,
2996 .tiocmset = edge_tiocmset,
2997 .write = edge_write,
2998 .write_room = edge_write_room,
2999 .chars_in_buffer = edge_chars_in_buffer,
3000 .break_ctl = edge_break,
3001 .read_int_callback = edge_interrupt_callback,
3002 .read_bulk_callback = edge_bulk_in_callback,
3003 .write_bulk_callback = edge_bulk_out_callback,
3006 static struct usb_serial_driver edgeport_2port_device = {
3008 .owner = THIS_MODULE,
3009 .name = "edgeport_ti_2",
3011 .description = "Edgeport TI 2 port adapter",
3012 .id_table = edgeport_2port_id_table,
3013 .num_interrupt_in = 1,
3018 .close = edge_close,
3019 .throttle = edge_throttle,
3020 .unthrottle = edge_unthrottle,
3021 .attach = edge_startup,
3022 .shutdown = edge_shutdown,
3023 .ioctl = edge_ioctl,
3024 .set_termios = edge_set_termios,
3025 .tiocmget = edge_tiocmget,
3026 .tiocmset = edge_tiocmset,
3027 .write = edge_write,
3028 .write_room = edge_write_room,
3029 .chars_in_buffer = edge_chars_in_buffer,
3030 .break_ctl = edge_break,
3031 .read_int_callback = edge_interrupt_callback,
3032 .read_bulk_callback = edge_bulk_in_callback,
3033 .write_bulk_callback = edge_bulk_out_callback,
3037 static int __init edgeport_init(void)
3040 retval = usb_serial_register(&edgeport_1port_device);
3042 goto failed_1port_device_register;
3043 retval = usb_serial_register(&edgeport_2port_device);
3045 goto failed_2port_device_register;
3046 retval = usb_register(&io_driver);
3048 goto failed_usb_register;
3049 info(DRIVER_DESC " " DRIVER_VERSION);
3051 failed_usb_register:
3052 usb_serial_deregister(&edgeport_2port_device);
3053 failed_2port_device_register:
3054 usb_serial_deregister(&edgeport_1port_device);
3055 failed_1port_device_register:
3059 static void __exit edgeport_exit (void)
3061 usb_deregister (&io_driver);
3062 usb_serial_deregister (&edgeport_1port_device);
3063 usb_serial_deregister (&edgeport_2port_device);
3066 module_init(edgeport_init);
3067 module_exit(edgeport_exit);
3069 /* Module information */
3070 MODULE_AUTHOR(DRIVER_AUTHOR);
3071 MODULE_DESCRIPTION(DRIVER_DESC);
3072 MODULE_LICENSE("GPL");
3074 module_param(debug, bool, S_IRUGO | S_IWUSR);
3075 MODULE_PARM_DESC(debug, "Debug enabled or not");
3077 module_param(low_latency, bool, S_IRUGO | S_IWUSR);
3078 MODULE_PARM_DESC(low_latency, "Low latency enabled or not");
3080 module_param(closing_wait, int, S_IRUGO | S_IWUSR);
3081 MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
3083 module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
3084 MODULE_PARM_DESC(ignore_cpu_rev, "Ignore the cpu revision when connecting to a device");