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/mutex.h>
38 #include <linux/serial.h>
39 #include <linux/ioctl.h>
40 #include <linux/firmware.h>
41 #include <asm/uaccess.h>
42 #include <linux/usb.h>
43 #include <linux/usb/serial.h>
46 #include "io_usbvend.h"
52 #define DRIVER_VERSION "v0.7mode043006"
53 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
54 #define DRIVER_DESC "Edgeport USB Serial Driver"
56 #define EPROM_PAGE_SIZE 64
59 struct edgeport_uart_buf_desc {
60 __u32 count; // Number of bytes currently in buffer
63 /* different hardware types */
64 #define HARDWARE_TYPE_930 0
65 #define HARDWARE_TYPE_TIUMP 1
67 // IOCTL_PRIVATE_TI_GET_MODE Definitions
68 #define TI_MODE_CONFIGURING 0 // Device has not entered start device
69 #define TI_MODE_BOOT 1 // Staying in boot mode
70 #define TI_MODE_DOWNLOAD 2 // Made it to download mode
71 #define TI_MODE_TRANSITIONING 3 // Currently in boot mode but transitioning to download mode
74 #define EDGE_READ_URB_RUNNING 0
75 #define EDGE_READ_URB_STOPPING 1
76 #define EDGE_READ_URB_STOPPED 2
78 #define EDGE_LOW_LATENCY 1
79 #define EDGE_CLOSING_WAIT 4000 /* in .01 sec */
81 #define EDGE_OUT_BUF_SIZE 1024
84 /* Product information read from the Edgeport */
87 int TiMode; // Current TI Mode
88 __u8 hardware_type; // Type of hardware
89 } __attribute__((packed));
93 unsigned int buf_size;
99 struct edgeport_port {
106 __u32 ump_read_timeout; /* Number of miliseconds the UMP will
107 wait without data before completing
112 struct edgeport_uart_buf_desc tx;
113 struct async_icount icount;
114 wait_queue_head_t delta_msr_wait; /* for handling sleeping while
115 waiting for msr change to
117 struct edgeport_serial *edge_serial;
118 struct usb_serial_port *port;
119 __u8 bUartMode; /* Port type, 0: RS232, etc. */
121 int ep_read_urb_state;
122 int ep_write_urb_in_use;
123 struct edge_buf *ep_out_buf;
126 struct edgeport_serial {
127 struct product_info product_info;
128 u8 TI_I2C_Type; // Type of I2C in UMP
129 u8 TiReadI2C; // Set to TRUE if we have read the I2c in Boot Mode
130 struct mutex es_lock;
132 struct usb_serial *serial;
136 /* Devices that this driver supports */
137 static struct usb_device_id edgeport_1port_id_table [] = {
138 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
139 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
140 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
141 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
142 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
143 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
144 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
145 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
146 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
147 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
148 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
149 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
150 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
151 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
152 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
153 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
157 static struct usb_device_id edgeport_2port_id_table [] = {
158 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
159 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
160 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
161 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
162 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
163 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
164 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
165 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
166 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
167 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
168 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
169 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
170 /* The 4, 8 and 16 port devices show up as multiple 2 port devices */
171 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
172 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
173 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
174 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
175 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
179 /* Devices that this driver supports */
180 static struct usb_device_id id_table_combined [] = {
181 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
182 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
183 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
184 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
185 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
186 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
187 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
188 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
189 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
190 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
191 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
192 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
193 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
194 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
195 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
196 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
197 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
198 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
199 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
200 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
201 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
202 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
203 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
204 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
205 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
206 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
207 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
208 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
209 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
210 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
211 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
212 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
213 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
217 MODULE_DEVICE_TABLE (usb, id_table_combined);
219 static struct usb_driver io_driver = {
221 .probe = usb_serial_probe,
222 .disconnect = usb_serial_disconnect,
223 .id_table = id_table_combined,
228 static unsigned char OperationalMajorVersion;
229 static unsigned char OperationalMinorVersion;
230 static unsigned short OperationalBuildNumber;
234 static int TIStayInBootMode = 0;
235 static int low_latency = EDGE_LOW_LATENCY;
236 static int closing_wait = EDGE_CLOSING_WAIT;
237 static int ignore_cpu_rev = 0;
238 static int default_uart_mode = 0; /* RS232 */
241 static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length);
243 static void stop_read(struct edgeport_port *edge_port);
244 static int restart_read(struct edgeport_port *edge_port);
246 static void edge_set_termios (struct usb_serial_port *port, struct ktermios *old_termios);
247 static void edge_send(struct usb_serial_port *port);
249 /* sysfs attributes */
250 static int edge_create_sysfs_attrs(struct usb_serial_port *port);
251 static int edge_remove_sysfs_attrs(struct usb_serial_port *port);
253 /* circular buffer */
254 static struct edge_buf *edge_buf_alloc(unsigned int size);
255 static void edge_buf_free(struct edge_buf *eb);
256 static void edge_buf_clear(struct edge_buf *eb);
257 static unsigned int edge_buf_data_avail(struct edge_buf *eb);
258 static unsigned int edge_buf_space_avail(struct edge_buf *eb);
259 static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
261 static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
265 static int TIReadVendorRequestSync (struct usb_device *dev,
274 status = usb_control_msg (dev,
275 usb_rcvctrlpipe(dev, 0),
287 if (status != size) {
288 dbg ("%s - wanted to write %d, but only wrote %d",
289 __func__, size, status);
295 static int TISendVendorRequestSync (struct usb_device *dev,
304 status = usb_control_msg (dev,
305 usb_sndctrlpipe(dev, 0),
317 if (status != size) {
318 dbg ("%s - wanted to write %d, but only wrote %d",
319 __func__, size, status);
325 static int TIWriteCommandSync (struct usb_device *dev, __u8 command,
326 __u8 moduleid, __u16 value, u8 *data,
329 return TISendVendorRequestSync (dev,
333 data, // TransferBuffer
334 size); // TransferBufferLength
338 /* clear tx/rx buffers and fifo in TI UMP */
339 static int TIPurgeDataSync (struct usb_serial_port *port, __u16 mask)
341 int port_number = port->number - port->serial->minor;
343 dbg ("%s - port %d, mask %x", __func__, port_number, mask);
345 return TIWriteCommandSync (port->serial->dev,
347 (__u8)(UMPM_UART1_PORT + port_number),
354 * TIReadDownloadMemory - Read edgeport memory from TI chip
355 * @dev: usb device pointer
356 * @start_address: Device CPU address at which to read
357 * @length: Length of above data
358 * @address_type: Can read both XDATA and I2C
359 * @buffer: pointer to input data buffer
361 static int TIReadDownloadMemory(struct usb_device *dev, int start_address,
362 int length, __u8 address_type, __u8 *buffer)
366 __be16 be_start_address;
368 dbg ("%s - @ %x for %d", __func__, start_address, length);
370 /* Read in blocks of 64 bytes
371 * (TI firmware can't handle more than 64 byte reads)
377 read_length = (__u8)length;
379 if (read_length > 1) {
380 dbg ("%s - @ %x for %d", __func__,
381 start_address, read_length);
383 be_start_address = cpu_to_be16 (start_address);
384 status = TIReadVendorRequestSync (dev,
385 UMPC_MEMORY_READ, // Request
386 (__u16)address_type, // wValue (Address type)
387 (__force __u16)be_start_address, // wIndex (Address to read)
388 buffer, // TransferBuffer
389 read_length); // TransferBufferLength
392 dbg ("%s - ERROR %x", __func__, status);
396 if (read_length > 1) {
397 usb_serial_debug_data(debug, &dev->dev, __func__,
398 read_length, buffer);
401 /* Update pointers/length */
402 start_address += read_length;
403 buffer += read_length;
404 length -= read_length;
410 static int TIReadRam (struct usb_device *dev, int start_address, int length, __u8 *buffer)
412 return TIReadDownloadMemory (dev,
415 DTK_ADDR_SPACE_XDATA,
419 /* Read edgeport memory to a given block */
420 static int TIReadBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 * buffer)
425 for (i=0; i< length; i++) {
426 status = TIReadVendorRequestSync (serial->serial->dev,
427 UMPC_MEMORY_READ, // Request
428 serial->TI_I2C_Type, // wValue (Address type)
429 (__u16)(start_address+i), // wIndex
430 &buffer[i], // TransferBuffer
431 0x01); // TransferBufferLength
433 dbg ("%s - ERROR %x", __func__, status);
438 dbg ("%s - start_address = %x, length = %d", __func__, start_address, length);
439 usb_serial_debug_data(debug, &serial->serial->dev->dev, __func__, length, buffer);
441 serial->TiReadI2C = 1;
446 /* Write given block to TI EPROM memory */
447 static int TIWriteBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
453 /* Must do a read before write */
454 if (!serial->TiReadI2C) {
455 status = TIReadBootMemory(serial, 0, 1, &temp);
460 for (i=0; i < length; ++i) {
461 status = TISendVendorRequestSync (serial->serial->dev,
462 UMPC_MEMORY_WRITE, // Request
464 (__u16)(i+start_address), // wIndex
465 NULL, // TransferBuffer
466 0); // TransferBufferLength
471 dbg ("%s - start_sddr = %x, length = %d", __func__, start_address, length);
472 usb_serial_debug_data(debug, &serial->serial->dev->dev, __func__, length, buffer);
478 /* Write edgeport I2C memory to TI chip */
479 static int TIWriteDownloadI2C (struct edgeport_serial *serial, int start_address, int length, __u8 address_type, __u8 *buffer)
483 __be16 be_start_address;
485 /* We can only send a maximum of 1 aligned byte page at a time */
487 /* calulate the number of bytes left in the first page */
488 write_length = EPROM_PAGE_SIZE - (start_address & (EPROM_PAGE_SIZE - 1));
490 if (write_length > length)
491 write_length = length;
493 dbg ("%s - BytesInFirstPage Addr = %x, length = %d", __func__, start_address, write_length);
494 usb_serial_debug_data(debug, &serial->serial->dev->dev, __func__, write_length, buffer);
496 /* Write first page */
497 be_start_address = cpu_to_be16 (start_address);
498 status = TISendVendorRequestSync (serial->serial->dev,
499 UMPC_MEMORY_WRITE, // Request
500 (__u16)address_type, // wValue
501 (__force __u16)be_start_address, // wIndex
502 buffer, // TransferBuffer
505 dbg ("%s - ERROR %d", __func__, status);
509 length -= write_length;
510 start_address += write_length;
511 buffer += write_length;
513 /* We should be aligned now -- can write max page size bytes at a time */
515 if (length > EPROM_PAGE_SIZE)
516 write_length = EPROM_PAGE_SIZE;
518 write_length = length;
520 dbg ("%s - Page Write Addr = %x, length = %d", __func__, start_address, write_length);
521 usb_serial_debug_data(debug, &serial->serial->dev->dev, __func__, write_length, buffer);
523 /* Write next page */
524 be_start_address = cpu_to_be16 (start_address);
525 status = TISendVendorRequestSync (serial->serial->dev,
526 UMPC_MEMORY_WRITE, // Request
527 (__u16)address_type, // wValue
528 (__force __u16)be_start_address, // wIndex
529 buffer, // TransferBuffer
530 write_length); // TransferBufferLength
532 dev_err (&serial->serial->dev->dev, "%s - ERROR %d\n", __func__, status);
536 length -= write_length;
537 start_address += write_length;
538 buffer += write_length;
543 /* Examine the UMP DMA registers and LSR
545 * Check the MSBit of the X and Y DMA byte count registers.
546 * A zero in this bit indicates that the TX DMA buffers are empty
547 * then check the TX Empty bit in the UART.
549 static int TIIsTxActive (struct edgeport_port *port)
552 struct out_endpoint_desc_block *oedb;
556 oedb = kmalloc (sizeof (* oedb), GFP_KERNEL);
558 dev_err (&port->port->dev, "%s - out of memory\n", __func__);
562 lsr = kmalloc (1, GFP_KERNEL); /* Sigh, that's right, just one byte,
563 as not all platforms can do DMA
569 /* Read the DMA Count Registers */
570 status = TIReadRam (port->port->serial->dev,
576 goto exit_is_tx_active;
578 dbg ("%s - XByteCount 0x%X", __func__, oedb->XByteCount);
581 status = TIReadRam (port->port->serial->dev,
582 port->uart_base + UMPMEM_OFFS_UART_LSR,
587 goto exit_is_tx_active;
588 dbg ("%s - LSR = 0x%X", __func__, *lsr);
590 /* If either buffer has data or we are transmitting then return TRUE */
591 if ((oedb->XByteCount & 0x80 ) != 0 )
594 if ((*lsr & UMP_UART_LSR_TX_MASK ) == 0 )
597 /* We return Not Active if we get any kind of error */
599 dbg ("%s - return %d", __func__, bytes_left );
606 static void TIChasePort(struct edgeport_port *port, unsigned long timeout, int flush)
609 struct tty_struct *tty = port->port->tty;
614 timeout = (HZ*EDGE_CLOSING_WAIT)/100;
616 /* wait for data to drain from the buffer */
617 spin_lock_irqsave(&port->ep_lock, flags);
618 init_waitqueue_entry(&wait, current);
619 add_wait_queue(&tty->write_wait, &wait);
621 set_current_state(TASK_INTERRUPTIBLE);
622 if (edge_buf_data_avail(port->ep_out_buf) == 0
623 || timeout == 0 || signal_pending(current)
624 || !usb_get_intfdata(port->port->serial->interface)) /* disconnect */
626 spin_unlock_irqrestore(&port->ep_lock, flags);
627 timeout = schedule_timeout(timeout);
628 spin_lock_irqsave(&port->ep_lock, flags);
630 set_current_state(TASK_RUNNING);
631 remove_wait_queue(&tty->write_wait, &wait);
633 edge_buf_clear(port->ep_out_buf);
634 spin_unlock_irqrestore(&port->ep_lock, flags);
636 /* wait for data to drain from the device */
638 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
639 && usb_get_intfdata(port->port->serial->interface)) { /* not disconnected */
640 if (!TIIsTxActive(port))
646 if (!usb_get_intfdata(port->port->serial->interface))
649 /* wait one more character time, based on baud rate */
650 /* (TIIsTxActive doesn't seem to wait for the last byte) */
651 if ((baud_rate=port->baud_rate) == 0)
653 msleep(max(1, DIV_ROUND_UP(10000, baud_rate)));
656 static int TIChooseConfiguration (struct usb_device *dev)
658 // There may be multiple configurations on this device, in which case
659 // we would need to read and parse all of them to find out which one
660 // we want. However, we just support one config at this point,
661 // configuration # 1, which is Config Descriptor 0.
663 dbg ("%s - Number of Interfaces = %d", __func__, dev->config->desc.bNumInterfaces);
664 dbg ("%s - MAX Power = %d", __func__, dev->config->desc.bMaxPower*2);
666 if (dev->config->desc.bNumInterfaces != 1) {
667 dev_err (&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __func__);
674 static int TIReadRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
678 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
679 status = TIReadDownloadMemory (serial->serial->dev,
685 status = TIReadBootMemory (serial,
694 static int TIWriteRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
696 if (serial->product_info.TiMode == TI_MODE_BOOT)
697 return TIWriteBootMemory (serial,
702 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
703 return TIWriteDownloadI2C (serial,
714 /* Read a descriptor header from I2C based on type */
715 static int TIGetDescriptorAddress (struct edgeport_serial *serial, int desc_type, struct ti_i2c_desc *rom_desc)
720 /* Search for requested descriptor in I2C */
723 status = TIReadRom (serial,
725 sizeof(struct ti_i2c_desc),
730 if (rom_desc->Type == desc_type)
731 return start_address;
733 start_address = start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size;
735 } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
740 /* Validate descriptor checksum */
741 static int ValidChecksum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
746 for (i=0; i < rom_desc->Size; i++) {
747 cs = (__u8)(cs + buffer[i]);
749 if (cs != rom_desc->CheckSum) {
750 dbg ("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
756 /* Make sure that the I2C image is good */
757 static int TiValidateI2cImage (struct edgeport_serial *serial)
759 struct device *dev = &serial->serial->dev->dev;
761 struct ti_i2c_desc *rom_desc;
762 int start_address = 2;
766 rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
768 dev_err (dev, "%s - out of memory\n", __func__);
771 buffer = kmalloc (TI_MAX_I2C_SIZE, GFP_KERNEL);
773 dev_err (dev, "%s - out of memory when allocating buffer\n", __func__);
778 // Read the first byte (Signature0) must be 0x52 or 0x10
779 status = TIReadRom (serial, 0, 1, buffer);
781 goto ExitTiValidateI2cImage;
783 if (*buffer != UMP5152 && *buffer != UMP3410) {
784 dev_err (dev, "%s - invalid buffer signature\n", __func__);
786 goto ExitTiValidateI2cImage;
791 status = TIReadRom (serial,
793 sizeof(struct ti_i2c_desc),
798 if ((start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size) > TI_MAX_I2C_SIZE) {
800 dbg ("%s - structure too big, erroring out.", __func__);
804 dbg ("%s Type = 0x%x", __func__, rom_desc->Type);
806 // Skip type 2 record
807 ttype = rom_desc->Type & 0x0f;
808 if ( ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
809 && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO ) {
810 // Read the descriptor data
811 status = TIReadRom(serial,
812 start_address+sizeof(struct ti_i2c_desc),
818 status = ValidChecksum(rom_desc, buffer);
822 start_address = start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size;
824 } while ((rom_desc->Type != I2C_DESC_TYPE_ION) && (start_address < TI_MAX_I2C_SIZE));
826 if ((rom_desc->Type != I2C_DESC_TYPE_ION) || (start_address > TI_MAX_I2C_SIZE))
829 ExitTiValidateI2cImage:
835 static int TIReadManufDescriptor (struct edgeport_serial *serial, __u8 *buffer)
839 struct ti_i2c_desc *rom_desc;
840 struct edge_ti_manuf_descriptor *desc;
842 rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
844 dev_err (&serial->serial->dev->dev, "%s - out of memory\n", __func__);
847 start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_ION, rom_desc);
849 if (!start_address) {
850 dbg ("%s - Edge Descriptor not found in I2C", __func__);
855 // Read the descriptor data
856 status = TIReadRom (serial,
857 start_address+sizeof(struct ti_i2c_desc),
863 status = ValidChecksum(rom_desc, buffer);
865 desc = (struct edge_ti_manuf_descriptor *)buffer;
866 dbg ( "%s - IonConfig 0x%x", __func__, desc->IonConfig );
867 dbg ( "%s - Version %d", __func__, desc->Version );
868 dbg ( "%s - Cpu/Board 0x%x", __func__, desc->CpuRev_BoardRev );
869 dbg ( "%s - NumPorts %d", __func__, desc->NumPorts );
870 dbg ( "%s - NumVirtualPorts %d", __func__, desc->NumVirtualPorts );
871 dbg ( "%s - TotalPorts %d", __func__, desc->TotalPorts );
878 /* Build firmware header used for firmware update */
879 static int BuildI2CFirmwareHeader (__u8 *header, struct device *dev)
886 struct ti_i2c_desc *i2c_header;
887 struct ti_i2c_image_header *img_header;
888 struct ti_i2c_firmware_rec *firmware_rec;
889 const struct firmware *fw;
890 const char *fw_name = "edgeport/down3.bin";
892 // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
893 // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver
894 // will download the latest firmware (padded to 15.5k) into the UMP ram.
895 // And finally when the device comes back up in download mode the driver will cause
896 // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
897 // the record type from 0xf2 to 0x02.
899 // Allocate a 15.5k buffer + 2 bytes for version number (Firmware Record)
900 buffer_size = (((1024 * 16) - 512 )+ sizeof(struct ti_i2c_firmware_rec));
902 buffer = kmalloc (buffer_size, GFP_KERNEL);
904 dev_err (dev, "%s - out of memory\n", __func__);
908 // Set entire image of 0xffs
909 memset (buffer, 0xff, buffer_size);
911 err = request_firmware(&fw, fw_name, dev);
913 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
919 /* Save Download Version Number */
920 OperationalMajorVersion = fw->data[0];
921 OperationalMinorVersion = fw->data[1];
922 OperationalBuildNumber = fw->data[2] | (fw->data[3] << 8);
924 // Copy version number into firmware record
925 firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
927 firmware_rec->Ver_Major = OperationalMajorVersion;
928 firmware_rec->Ver_Minor = OperationalMinorVersion;
930 // Pointer to fw_down memory image
931 img_header = (struct ti_i2c_image_header *)&fw->data[4];
933 memcpy (buffer + sizeof(struct ti_i2c_firmware_rec),
934 &fw->data[4 + sizeof(struct ti_i2c_image_header)],
935 le16_to_cpu(img_header->Length));
937 release_firmware(fw);
939 for (i=0; i < buffer_size; i++) {
940 cs = (__u8)(cs + buffer[i]);
946 i2c_header = (struct ti_i2c_desc *)header;
947 firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data;
949 i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK;
950 i2c_header->Size = (__u16)buffer_size;
951 i2c_header->CheckSum = cs;
952 firmware_rec->Ver_Major = OperationalMajorVersion;
953 firmware_rec->Ver_Minor = OperationalMinorVersion;
958 /* Try to figure out what type of I2c we have */
959 static int TIGetI2cTypeInBootMode (struct edgeport_serial *serial)
964 // Try to read type 2
965 status = TIReadVendorRequestSync (serial->serial->dev,
966 UMPC_MEMORY_READ, // Request
967 DTK_ADDR_SPACE_I2C_TYPE_II, // wValue (Address type)
969 &data, // TransferBuffer
970 0x01); // TransferBufferLength
972 dbg ("%s - read 2 status error = %d", __func__, status);
974 dbg ("%s - read 2 data = 0x%x", __func__, data);
975 if ((!status) && (data == UMP5152 || data == UMP3410)) {
976 dbg ("%s - ROM_TYPE_II", __func__);
977 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
981 // Try to read type 3
982 status = TIReadVendorRequestSync (serial->serial->dev,
983 UMPC_MEMORY_READ, // Request
984 DTK_ADDR_SPACE_I2C_TYPE_III, // wValue (Address type)
986 &data, // TransferBuffer
987 0x01); // TransferBufferLength
989 dbg ("%s - read 3 status error = %d", __func__, status);
991 dbg ("%s - read 2 data = 0x%x", __func__, data);
992 if ((!status) && (data == UMP5152 || data == UMP3410)) {
993 dbg ("%s - ROM_TYPE_III", __func__);
994 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
998 dbg ("%s - Unknown", __func__);
999 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1003 static int TISendBulkTransferSync (struct usb_serial *serial, void *buffer, int length, int *num_sent)
1007 status = usb_bulk_msg (serial->dev,
1008 usb_sndbulkpipe(serial->dev,
1009 serial->port[0]->bulk_out_endpointAddress),
1017 /* Download given firmware image to the device (IN BOOT MODE) */
1018 static int TIDownloadCodeImage (struct edgeport_serial *serial, __u8 *image, int image_length)
1025 // Transfer firmware image
1026 for (pos = 0; pos < image_length; ) {
1027 // Read the next buffer from file
1028 transfer = image_length - pos;
1029 if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
1030 transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
1033 status = TISendBulkTransferSync (serial->serial, &image[pos], transfer, &done);
1036 // Advance buffer pointer
1044 static int TIConfigureBootDevice (struct usb_device *dev)
1050 * DownloadTIFirmware - Download run-time operating firmware to the TI5052
1052 * This routine downloads the main operating code into the TI5052, using the
1053 * boot code already burned into E2PROM or ROM.
1055 static int TIDownloadFirmware (struct edgeport_serial *serial)
1057 struct device *dev = &serial->serial->dev->dev;
1060 struct edge_ti_manuf_descriptor *ti_manuf_desc;
1061 struct usb_interface_descriptor *interface;
1062 int download_cur_ver;
1063 int download_new_ver;
1065 /* This routine is entered by both the BOOT mode and the Download mode
1066 * We can determine which code is running by the reading the config
1067 * descriptor and if we have only one bulk pipe it is in boot mode
1069 serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
1071 /* Default to type 2 i2c */
1072 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1074 status = TIChooseConfiguration (serial->serial->dev);
1078 interface = &serial->serial->interface->cur_altsetting->desc;
1080 dev_err (dev, "%s - no interface set, error!\n", __func__);
1084 // Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
1085 // if we have more than one endpoint we are definitely in download mode
1086 if (interface->bNumEndpoints > 1)
1087 serial->product_info.TiMode = TI_MODE_DOWNLOAD;
1089 // Otherwise we will remain in configuring mode
1090 serial->product_info.TiMode = TI_MODE_CONFIGURING;
1092 /********************************************************************/
1094 /********************************************************************/
1095 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
1096 struct ti_i2c_desc *rom_desc;
1098 dbg("%s - RUNNING IN DOWNLOAD MODE", __func__);
1100 status = TiValidateI2cImage (serial);
1102 dbg("%s - DOWNLOAD MODE -- BAD I2C", __func__);
1106 /* Validate Hardware version number
1107 * Read Manufacturing Descriptor from TI Based Edgeport
1109 ti_manuf_desc = kmalloc (sizeof (*ti_manuf_desc), GFP_KERNEL);
1110 if (!ti_manuf_desc) {
1111 dev_err (dev, "%s - out of memory.\n", __func__);
1114 status = TIReadManufDescriptor (serial, (__u8 *)ti_manuf_desc);
1116 kfree (ti_manuf_desc);
1120 // Check version number of ION descriptor
1121 if (!ignore_cpu_rev && TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev) < 2) {
1122 dbg ( "%s - Wrong CPU Rev %d (Must be 2)", __func__,
1123 TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev));
1124 kfree (ti_manuf_desc);
1128 rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
1130 dev_err (dev, "%s - out of memory.\n", __func__);
1131 kfree (ti_manuf_desc);
1135 // Search for type 2 record (firmware record)
1136 if ((start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc)) != 0) {
1137 struct ti_i2c_firmware_rec *firmware_version;
1140 dbg ("%s - Found Type FIRMWARE (Type 2) record", __func__);
1142 firmware_version = kmalloc (sizeof (*firmware_version), GFP_KERNEL);
1143 if (!firmware_version) {
1144 dev_err (dev, "%s - out of memory.\n", __func__);
1146 kfree (ti_manuf_desc);
1150 // Validate version number
1151 // Read the descriptor data
1152 status = TIReadRom (serial,
1153 start_address+sizeof(struct ti_i2c_desc),
1154 sizeof(struct ti_i2c_firmware_rec),
1155 (__u8 *)firmware_version);
1157 kfree (firmware_version);
1159 kfree (ti_manuf_desc);
1163 // Check version number of download with current version in I2c
1164 download_cur_ver = (firmware_version->Ver_Major << 8) +
1165 (firmware_version->Ver_Minor);
1166 download_new_ver = (OperationalMajorVersion << 8) +
1167 (OperationalMinorVersion);
1169 dbg ("%s - >>>Firmware Versions Device %d.%d Driver %d.%d",
1171 firmware_version->Ver_Major,
1172 firmware_version->Ver_Minor,
1173 OperationalMajorVersion,
1174 OperationalMinorVersion);
1176 // Check if we have an old version in the I2C and update if necessary
1177 if (download_cur_ver != download_new_ver) {
1178 dbg ("%s - Update I2C Download from %d.%d to %d.%d",
1180 firmware_version->Ver_Major,
1181 firmware_version->Ver_Minor,
1182 OperationalMajorVersion,
1183 OperationalMinorVersion);
1185 // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
1186 // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver
1187 // will download the latest firmware (padded to 15.5k) into the UMP ram.
1188 // And finally when the device comes back up in download mode the driver will cause
1189 // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
1190 // the record type from 0xf2 to 0x02.
1192 record = I2C_DESC_TYPE_FIRMWARE_BLANK;
1194 // Change the I2C Firmware record type to 0xf2 to trigger an update
1195 status = TIWriteRom (serial,
1200 kfree (firmware_version);
1202 kfree (ti_manuf_desc);
1206 // verify the write -- must do this in order for write to
1207 // complete before we do the hardware reset
1208 status = TIReadRom (serial,
1214 kfree (firmware_version);
1216 kfree (ti_manuf_desc);
1220 if (record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
1221 dev_err (dev, "%s - error resetting device\n", __func__);
1222 kfree (firmware_version);
1224 kfree (ti_manuf_desc);
1228 dbg ("%s - HARDWARE RESET", __func__);
1230 // Reset UMP -- Back to BOOT MODE
1231 status = TISendVendorRequestSync (serial->serial->dev,
1232 UMPC_HARDWARE_RESET, // Request
1235 NULL, // TransferBuffer
1236 0); // TransferBufferLength
1238 dbg ( "%s - HARDWARE RESET return %d", __func__, status);
1240 /* return an error on purpose. */
1241 kfree (firmware_version);
1243 kfree (ti_manuf_desc);
1246 kfree (firmware_version);
1248 // Search for type 0xF2 record (firmware blank record)
1249 else if ((start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
1250 #define HEADER_SIZE (sizeof(struct ti_i2c_desc) + sizeof(struct ti_i2c_firmware_rec))
1254 header = kmalloc (HEADER_SIZE, GFP_KERNEL);
1256 dev_err (dev, "%s - out of memory.\n", __func__);
1258 kfree (ti_manuf_desc);
1262 vheader = kmalloc (HEADER_SIZE, GFP_KERNEL);
1264 dev_err (dev, "%s - out of memory.\n", __func__);
1267 kfree (ti_manuf_desc);
1271 dbg ("%s - Found Type BLANK FIRMWARE (Type F2) record", __func__);
1273 // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
1274 // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver
1275 // will download the latest firmware (padded to 15.5k) into the UMP ram.
1276 // And finally when the device comes back up in download mode the driver will cause
1277 // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
1278 // the record type from 0xf2 to 0x02.
1279 status = BuildI2CFirmwareHeader(header, dev);
1284 kfree (ti_manuf_desc);
1288 // Update I2C with type 0xf2 record with correct size and checksum
1289 status = TIWriteRom (serial,
1297 kfree (ti_manuf_desc);
1301 // verify the write -- must do this in order for write to
1302 // complete before we do the hardware reset
1303 status = TIReadRom (serial,
1309 dbg ("%s - can't read header back", __func__);
1313 kfree (ti_manuf_desc);
1316 if (memcmp(vheader, header, HEADER_SIZE)) {
1317 dbg ("%s - write download record failed", __func__);
1321 kfree (ti_manuf_desc);
1328 dbg ("%s - Start firmware update", __func__);
1330 // Tell firmware to copy download image into I2C
1331 status = TISendVendorRequestSync (serial->serial->dev,
1332 UMPC_COPY_DNLD_TO_I2C, // Request
1335 NULL, // TransferBuffer
1336 0); // TransferBufferLength
1338 dbg ("%s - Update complete 0x%x", __func__, status);
1340 dev_err (dev, "%s - UMPC_COPY_DNLD_TO_I2C failed\n", __func__);
1342 kfree (ti_manuf_desc);
1347 // The device is running the download code
1349 kfree (ti_manuf_desc);
1353 /********************************************************************/
1355 /********************************************************************/
1356 dbg("%s - RUNNING IN BOOT MODE", __func__);
1358 // Configure the TI device so we can use the BULK pipes for download
1359 status = TIConfigureBootDevice (serial->serial->dev);
1363 if (le16_to_cpu(serial->serial->dev->descriptor.idVendor) != USB_VENDOR_ID_ION) {
1364 dbg ("%s - VID = 0x%x", __func__,
1365 le16_to_cpu(serial->serial->dev->descriptor.idVendor));
1366 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1367 goto StayInBootMode;
1370 // We have an ION device (I2c Must be programmed)
1371 // Determine I2C image type
1372 if (TIGetI2cTypeInBootMode(serial)) {
1373 goto StayInBootMode;
1376 // Registry variable set?
1377 if (TIStayInBootMode) {
1378 dbg ("%s - TIStayInBootMode", __func__);
1379 goto StayInBootMode;
1382 // Check for ION Vendor ID and that the I2C is valid
1383 if (!TiValidateI2cImage(serial)) {
1384 struct ti_i2c_image_header *header;
1390 const struct firmware *fw;
1391 const char *fw_name = "edgeport/down3.bin";
1393 /* Validate Hardware version number
1394 * Read Manufacturing Descriptor from TI Based Edgeport
1396 ti_manuf_desc = kmalloc (sizeof (*ti_manuf_desc), GFP_KERNEL);
1397 if (!ti_manuf_desc) {
1398 dev_err (dev, "%s - out of memory.\n", __func__);
1401 status = TIReadManufDescriptor (serial, (__u8 *)ti_manuf_desc);
1403 kfree (ti_manuf_desc);
1404 goto StayInBootMode;
1407 // Check for version 2
1408 if (!ignore_cpu_rev && TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev) < 2) {
1409 dbg ("%s - Wrong CPU Rev %d (Must be 2)", __func__,
1410 TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev));
1411 kfree (ti_manuf_desc);
1412 goto StayInBootMode;
1415 kfree (ti_manuf_desc);
1417 // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
1418 // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver
1419 // will download the latest firmware (padded to 15.5k) into the UMP ram.
1420 // And finally when the device comes back up in download mode the driver will cause
1421 // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
1422 // the record type from 0xf2 to 0x02.
1425 * Do we really have to copy the whole firmware image,
1426 * or could we do this in place!
1429 // Allocate a 15.5k buffer + 3 byte header
1430 buffer_size = (((1024 * 16) - 512) + sizeof(struct ti_i2c_image_header));
1431 buffer = kmalloc (buffer_size, GFP_KERNEL);
1433 dev_err (dev, "%s - out of memory\n", __func__);
1437 // Initialize the buffer to 0xff (pad the buffer)
1438 memset (buffer, 0xff, buffer_size);
1440 err = request_firmware(&fw, fw_name, dev);
1442 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
1447 memcpy(buffer, &fw->data[4], fw->size - 4);
1448 release_firmware(fw);
1450 for(i = sizeof(struct ti_i2c_image_header); i < buffer_size; i++) {
1451 cs = (__u8)(cs + buffer[i]);
1454 header = (struct ti_i2c_image_header *)buffer;
1456 // update length and checksum after padding
1457 header->Length = cpu_to_le16((__u16)(buffer_size - sizeof(struct ti_i2c_image_header)));
1458 header->CheckSum = cs;
1460 // Download the operational code
1461 dbg ("%s - Downloading operational code image (TI UMP)", __func__);
1462 status = TIDownloadCodeImage (serial, buffer, buffer_size);
1467 dbg ("%s - Error downloading operational code image", __func__);
1471 // Device will reboot
1472 serial->product_info.TiMode = TI_MODE_TRANSITIONING;
1474 dbg ("%s - Download successful -- Device rebooting...", __func__);
1476 /* return an error on purpose */
1481 // Eprom is invalid or blank stay in boot mode
1482 dbg("%s - STAYING IN BOOT MODE", __func__);
1483 serial->product_info.TiMode = TI_MODE_BOOT;
1489 static int TISetDtr (struct edgeport_port *port)
1491 int port_number = port->port->number - port->port->serial->minor;
1493 dbg ("%s", __func__);
1494 port->shadow_mcr |= MCR_DTR;
1496 return TIWriteCommandSync (port->port->serial->dev,
1498 (__u8)(UMPM_UART1_PORT + port_number),
1504 static int TIClearDtr (struct edgeport_port *port)
1506 int port_number = port->port->number - port->port->serial->minor;
1508 dbg ("%s", __func__);
1509 port->shadow_mcr &= ~MCR_DTR;
1511 return TIWriteCommandSync (port->port->serial->dev,
1513 (__u8)(UMPM_UART1_PORT + port_number),
1519 static int TISetRts (struct edgeport_port *port)
1521 int port_number = port->port->number - port->port->serial->minor;
1523 dbg ("%s", __func__);
1524 port->shadow_mcr |= MCR_RTS;
1526 return TIWriteCommandSync (port->port->serial->dev,
1528 (__u8)(UMPM_UART1_PORT + port_number),
1534 static int TIClearRts (struct edgeport_port *port)
1536 int port_number = port->port->number - port->port->serial->minor;
1538 dbg ("%s", __func__);
1539 port->shadow_mcr &= ~MCR_RTS;
1541 return TIWriteCommandSync (port->port->serial->dev,
1543 (__u8)(UMPM_UART1_PORT + port_number),
1549 static int TISetLoopBack (struct edgeport_port *port)
1551 int port_number = port->port->number - port->port->serial->minor;
1553 dbg ("%s", __func__);
1555 return TIWriteCommandSync (port->port->serial->dev,
1556 UMPC_SET_CLR_LOOPBACK,
1557 (__u8)(UMPM_UART1_PORT + port_number),
1563 static int TIClearLoopBack (struct edgeport_port *port)
1565 int port_number = port->port->number - port->port->serial->minor;
1567 dbg ("%s", __func__);
1569 return TIWriteCommandSync (port->port->serial->dev,
1570 UMPC_SET_CLR_LOOPBACK,
1571 (__u8)(UMPM_UART1_PORT + port_number),
1577 static int TISetBreak (struct edgeport_port *port)
1579 int port_number = port->port->number - port->port->serial->minor;
1581 dbg ("%s", __func__);
1583 return TIWriteCommandSync (port->port->serial->dev,
1585 (__u8)(UMPM_UART1_PORT + port_number),
1591 static int TIClearBreak (struct edgeport_port *port)
1593 int port_number = port->port->number - port->port->serial->minor;
1595 dbg ("%s", __func__);
1597 return TIWriteCommandSync (port->port->serial->dev,
1599 (__u8)(UMPM_UART1_PORT + port_number),
1605 static int TIRestoreMCR (struct edgeport_port *port, __u8 mcr)
1609 dbg ("%s - %x", __func__, mcr);
1612 status = TISetDtr (port);
1614 status = TIClearDtr (port);
1620 status = TISetRts (port);
1622 status = TIClearRts (port);
1627 if (mcr & MCR_LOOPBACK)
1628 status = TISetLoopBack (port);
1630 status = TIClearLoopBack (port);
1637 /* Convert TI LSR to standard UART flags */
1638 static __u8 MapLineStatus (__u8 ti_lsr)
1642 #define MAP_FLAG(flagUmp, flagUart) \
1643 if (ti_lsr & flagUmp) \
1646 MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR) /* overrun */
1647 MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */
1648 MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */
1649 MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */
1650 MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* receive data available */
1651 MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* transmit holding register empty */
1658 static void handle_new_msr (struct edgeport_port *edge_port, __u8 msr)
1660 struct async_icount *icount;
1661 struct tty_struct *tty;
1663 dbg ("%s - %02x", __func__, msr);
1665 if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR | EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
1666 icount = &edge_port->icount;
1668 /* update input line counters */
1669 if (msr & EDGEPORT_MSR_DELTA_CTS)
1671 if (msr & EDGEPORT_MSR_DELTA_DSR)
1673 if (msr & EDGEPORT_MSR_DELTA_CD)
1675 if (msr & EDGEPORT_MSR_DELTA_RI)
1677 wake_up_interruptible (&edge_port->delta_msr_wait);
1680 /* Save the new modem status */
1681 edge_port->shadow_msr = msr & 0xf0;
1683 tty = edge_port->port->tty;
1684 /* handle CTS flow control */
1685 if (tty && C_CRTSCTS(tty)) {
1686 if (msr & EDGEPORT_MSR_CTS) {
1687 tty->hw_stopped = 0;
1690 tty->hw_stopped = 1;
1697 static void handle_new_lsr (struct edgeport_port *edge_port, int lsr_data, __u8 lsr, __u8 data)
1699 struct async_icount *icount;
1700 __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
1702 dbg ("%s - %02x", __func__, new_lsr);
1704 edge_port->shadow_lsr = lsr;
1706 if (new_lsr & LSR_BREAK) {
1708 * Parity and Framing errors only count if they
1709 * occur exclusive of a break being received.
1711 new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
1714 /* Place LSR data byte into Rx buffer */
1715 if (lsr_data && edge_port->port->tty)
1716 edge_tty_recv(&edge_port->port->dev, edge_port->port->tty, &data, 1);
1718 /* update input line counters */
1719 icount = &edge_port->icount;
1720 if (new_lsr & LSR_BREAK)
1722 if (new_lsr & LSR_OVER_ERR)
1724 if (new_lsr & LSR_PAR_ERR)
1726 if (new_lsr & LSR_FRM_ERR)
1731 static void edge_interrupt_callback (struct urb *urb)
1733 struct edgeport_serial *edge_serial = urb->context;
1734 struct usb_serial_port *port;
1735 struct edgeport_port *edge_port;
1736 unsigned char *data = urb->transfer_buffer;
1737 int length = urb->actual_length;
1743 int status = urb->status;
1745 dbg("%s", __func__);
1754 /* this urb is terminated, clean up */
1755 dbg("%s - urb shutting down with status: %d",
1759 dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
1760 "%d\n", __func__, status);
1765 dbg ("%s - no data in urb", __func__);
1769 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __func__, length, data);
1772 dbg ("%s - expecting packet of size 2, got %d", __func__, length);
1776 port_number = TIUMP_GET_PORT_FROM_CODE (data[0]);
1777 function = TIUMP_GET_FUNC_FROM_CODE (data[0]);
1778 dbg ("%s - port_number %d, function %d, info 0x%x",
1779 __func__, port_number, function, data[1]);
1780 port = edge_serial->serial->port[port_number];
1781 edge_port = usb_get_serial_port_data(port);
1783 dbg ("%s - edge_port not found", __func__);
1787 case TIUMP_INTERRUPT_CODE_LSR:
1788 lsr = MapLineStatus(data[1]);
1789 if (lsr & UMP_UART_LSR_DATA_MASK) {
1790 /* Save the LSR event for bulk read completion routine */
1791 dbg ("%s - LSR Event Port %u LSR Status = %02x",
1792 __func__, port_number, lsr);
1793 edge_port->lsr_event = 1;
1794 edge_port->lsr_mask = lsr;
1796 dbg ("%s - ===== Port %d LSR Status = %02x ======",
1797 __func__, port_number, lsr);
1798 handle_new_lsr (edge_port, 0, lsr, 0);
1802 case TIUMP_INTERRUPT_CODE_MSR: // MSR
1803 /* Copy MSR from UMP */
1805 dbg ("%s - ===== Port %u MSR Status = %02x ======\n",
1806 __func__, port_number, msr);
1807 handle_new_msr (edge_port, msr);
1811 dev_err (&urb->dev->dev, "%s - Unknown Interrupt code from UMP %x\n",
1818 retval = usb_submit_urb (urb, GFP_ATOMIC);
1820 dev_err (&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
1824 static void edge_bulk_in_callback (struct urb *urb)
1826 struct edgeport_port *edge_port = urb->context;
1827 unsigned char *data = urb->transfer_buffer;
1828 struct tty_struct *tty;
1831 int status = urb->status;
1833 dbg("%s", __func__);
1842 /* this urb is terminated, clean up */
1843 dbg("%s - urb shutting down with status: %d",
1847 dev_err (&urb->dev->dev,"%s - nonzero read bulk status received: %d\n",
1851 if (status == -EPIPE)
1855 dev_err(&urb->dev->dev,"%s - stopping read!\n", __func__);
1859 port_number = edge_port->port->number - edge_port->port->serial->minor;
1861 if (edge_port->lsr_event) {
1862 edge_port->lsr_event = 0;
1863 dbg ("%s ===== Port %u LSR Status = %02x, Data = %02x ======",
1864 __func__, port_number, edge_port->lsr_mask, *data);
1865 handle_new_lsr (edge_port, 1, edge_port->lsr_mask, *data);
1866 /* Adjust buffer length/pointer */
1867 --urb->actual_length;
1871 tty = edge_port->port->tty;
1872 if (tty && urb->actual_length) {
1873 usb_serial_debug_data(debug, &edge_port->port->dev, __func__, urb->actual_length, data);
1875 if (edge_port->close_pending) {
1876 dbg ("%s - close is pending, dropping data on the floor.", __func__);
1878 edge_tty_recv(&edge_port->port->dev, tty, data, urb->actual_length);
1880 edge_port->icount.rx += urb->actual_length;
1884 /* continue read unless stopped */
1885 spin_lock(&edge_port->ep_lock);
1886 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING) {
1887 urb->dev = edge_port->port->serial->dev;
1888 retval = usb_submit_urb(urb, GFP_ATOMIC);
1889 } else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING) {
1890 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
1892 spin_unlock(&edge_port->ep_lock);
1894 dev_err (&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
1898 static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length)
1903 cnt = tty_buffer_request_room(tty, length);
1905 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1906 __func__, length - cnt);
1910 tty_insert_flip_string(tty, data, cnt);
1913 } while (length > 0);
1915 tty_flip_buffer_push(tty);
1918 static void edge_bulk_out_callback (struct urb *urb)
1920 struct usb_serial_port *port = urb->context;
1921 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1922 int status = urb->status;
1924 dbg ("%s - port %d", __func__, port->number);
1926 edge_port->ep_write_urb_in_use = 0;
1935 /* this urb is terminated, clean up */
1936 dbg("%s - urb shutting down with status: %d",
1940 dev_err(&urb->dev->dev, "%s - nonzero write bulk status "
1941 "received: %d\n", __func__, status);
1944 /* send any buffered data */
1948 static int edge_open (struct usb_serial_port *port, struct file * filp)
1950 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1951 struct edgeport_serial *edge_serial;
1952 struct usb_device *dev;
1957 u8 transaction_timeout;
1959 dbg("%s - port %d", __func__, port->number);
1961 if (edge_port == NULL)
1964 port->tty->low_latency = low_latency;
1966 port_number = port->number - port->serial->minor;
1967 switch (port_number) {
1969 edge_port->uart_base = UMPMEM_BASE_UART1;
1970 edge_port->dma_address = UMPD_OEDB1_ADDRESS;
1973 edge_port->uart_base = UMPMEM_BASE_UART2;
1974 edge_port->dma_address = UMPD_OEDB2_ADDRESS;
1977 dev_err (&port->dev, "Unknown port number!!!\n");
1981 dbg ("%s - port_number = %d, uart_base = %04x, dma_address = %04x",
1982 __func__, port_number, edge_port->uart_base, edge_port->dma_address);
1984 dev = port->serial->dev;
1986 memset (&(edge_port->icount), 0x00, sizeof(edge_port->icount));
1987 init_waitqueue_head (&edge_port->delta_msr_wait);
1989 /* turn off loopback */
1990 status = TIClearLoopBack (edge_port);
1992 dev_err(&port->dev,"%s - cannot send clear loopback command, %d\n",
1997 /* set up the port settings */
1998 edge_set_termios (port, port->tty->termios);
2000 /* open up the port */
2002 /* milliseconds to timeout for DMA transfer */
2003 transaction_timeout = 2;
2005 edge_port->ump_read_timeout = max (20, ((transaction_timeout * 3) / 2) );
2007 // milliseconds to timeout for DMA transfer
2008 open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
2009 UMP_PIPE_TRANS_TIMEOUT_ENA |
2010 (transaction_timeout << 2));
2012 dbg ("%s - Sending UMPC_OPEN_PORT", __func__);
2014 /* Tell TI to open and start the port */
2015 status = TIWriteCommandSync (dev,
2017 (u8)(UMPM_UART1_PORT + port_number),
2022 dev_err(&port->dev,"%s - cannot send open command, %d\n", __func__, status);
2026 /* Start the DMA? */
2027 status = TIWriteCommandSync (dev,
2029 (u8)(UMPM_UART1_PORT + port_number),
2034 dev_err(&port->dev,"%s - cannot send start DMA command, %d\n", __func__, status);
2038 /* Clear TX and RX buffers in UMP */
2039 status = TIPurgeDataSync (port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
2041 dev_err(&port->dev,"%s - cannot send clear buffers command, %d\n", __func__, status);
2045 /* Read Initial MSR */
2046 status = TIReadVendorRequestSync (dev,
2047 UMPC_READ_MSR, // Request
2049 (__u16)(UMPM_UART1_PORT + port_number), // wIndex (Address)
2050 &edge_port->shadow_msr, // TransferBuffer
2051 1); // TransferBufferLength
2053 dev_err(&port->dev,"%s - cannot send read MSR command, %d\n", __func__, status);
2057 dbg ("ShadowMSR 0x%X", edge_port->shadow_msr);
2059 /* Set Initial MCR */
2060 edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
2061 dbg ("ShadowMCR 0x%X", edge_port->shadow_mcr);
2063 edge_serial = edge_port->edge_serial;
2064 if (mutex_lock_interruptible(&edge_serial->es_lock))
2065 return -ERESTARTSYS;
2066 if (edge_serial->num_ports_open == 0) {
2067 /* we are the first port to be opened, let's post the interrupt urb */
2068 urb = edge_serial->serial->port[0]->interrupt_in_urb;
2070 dev_err (&port->dev, "%s - no interrupt urb present, exiting\n", __func__);
2072 goto release_es_lock;
2074 urb->complete = edge_interrupt_callback;
2075 urb->context = edge_serial;
2077 status = usb_submit_urb (urb, GFP_KERNEL);
2079 dev_err (&port->dev, "%s - usb_submit_urb failed with value %d\n", __func__, status);
2080 goto release_es_lock;
2085 * reset the data toggle on the bulk endpoints to work around bug in
2086 * host controllers where things get out of sync some times
2088 usb_clear_halt (dev, port->write_urb->pipe);
2089 usb_clear_halt (dev, port->read_urb->pipe);
2091 /* start up our bulk read urb */
2092 urb = port->read_urb;
2094 dev_err (&port->dev, "%s - no read urb present, exiting\n", __func__);
2096 goto unlink_int_urb;
2098 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2099 urb->complete = edge_bulk_in_callback;
2100 urb->context = edge_port;
2102 status = usb_submit_urb (urb, GFP_KERNEL);
2104 dev_err (&port->dev, "%s - read bulk usb_submit_urb failed with value %d\n", __func__, status);
2105 goto unlink_int_urb;
2108 ++edge_serial->num_ports_open;
2110 dbg("%s - exited", __func__);
2112 goto release_es_lock;
2115 if (edge_port->edge_serial->num_ports_open == 0)
2116 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2118 mutex_unlock(&edge_serial->es_lock);
2122 static void edge_close (struct usb_serial_port *port, struct file *filp)
2124 struct edgeport_serial *edge_serial;
2125 struct edgeport_port *edge_port;
2129 dbg("%s - port %d", __func__, port->number);
2131 edge_serial = usb_get_serial_data(port->serial);
2132 edge_port = usb_get_serial_port_data(port);
2133 if ((edge_serial == NULL) || (edge_port == NULL))
2136 /* The bulkreadcompletion routine will check
2137 * this flag and dump add read data */
2138 edge_port->close_pending = 1;
2140 /* chase the port close and flush */
2141 TIChasePort (edge_port, (HZ*closing_wait)/100, 1);
2143 usb_kill_urb(port->read_urb);
2144 usb_kill_urb(port->write_urb);
2145 edge_port->ep_write_urb_in_use = 0;
2147 /* assuming we can still talk to the device,
2148 * send a close port command to it */
2149 dbg("%s - send umpc_close_port", __func__);
2150 port_number = port->number - port->serial->minor;
2151 status = TIWriteCommandSync (port->serial->dev,
2153 (__u8)(UMPM_UART1_PORT + port_number),
2157 mutex_lock(&edge_serial->es_lock);
2158 --edge_port->edge_serial->num_ports_open;
2159 if (edge_port->edge_serial->num_ports_open <= 0) {
2160 /* last port is now closed, let's shut down our interrupt urb */
2161 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2162 edge_port->edge_serial->num_ports_open = 0;
2164 mutex_unlock(&edge_serial->es_lock);
2165 edge_port->close_pending = 0;
2167 dbg("%s - exited", __func__);
2170 static int edge_write (struct usb_serial_port *port, const unsigned char *data, int count)
2172 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2173 unsigned long flags;
2175 dbg("%s - port %d", __func__, port->number);
2178 dbg("%s - write request of 0 bytes", __func__);
2182 if (edge_port == NULL)
2184 if (edge_port->close_pending == 1)
2187 spin_lock_irqsave(&edge_port->ep_lock, flags);
2188 count = edge_buf_put(edge_port->ep_out_buf, data, count);
2189 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2196 static void edge_send(struct usb_serial_port *port)
2199 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2200 struct tty_struct *tty = port->tty;
2201 unsigned long flags;
2204 dbg("%s - port %d", __func__, port->number);
2206 spin_lock_irqsave(&edge_port->ep_lock, flags);
2208 if (edge_port->ep_write_urb_in_use) {
2209 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2213 count = edge_buf_get(edge_port->ep_out_buf,
2214 port->write_urb->transfer_buffer,
2215 port->bulk_out_size);
2218 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2222 edge_port->ep_write_urb_in_use = 1;
2224 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2226 usb_serial_debug_data(debug, &port->dev, __func__, count, port->write_urb->transfer_buffer);
2228 /* set up our urb */
2229 usb_fill_bulk_urb (port->write_urb, port->serial->dev,
2230 usb_sndbulkpipe (port->serial->dev,
2231 port->bulk_out_endpointAddress),
2232 port->write_urb->transfer_buffer, count,
2233 edge_bulk_out_callback,
2236 /* send the data out the bulk port */
2237 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2239 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __func__, result);
2240 edge_port->ep_write_urb_in_use = 0;
2241 // TODO: reschedule edge_send
2243 edge_port->icount.tx += count;
2246 /* wakeup any process waiting for writes to complete */
2247 /* there is now more room in the buffer for new writes */
2249 /* let the tty driver wakeup if it has a special write_wakeup function */
2254 static int edge_write_room (struct usb_serial_port *port)
2256 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2258 unsigned long flags;
2260 dbg("%s - port %d", __func__, port->number);
2262 if (edge_port == NULL)
2264 if (edge_port->close_pending == 1)
2267 spin_lock_irqsave(&edge_port->ep_lock, flags);
2268 room = edge_buf_space_avail(edge_port->ep_out_buf);
2269 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2271 dbg("%s - returns %d", __func__, room);
2275 static int edge_chars_in_buffer (struct usb_serial_port *port)
2277 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2279 unsigned long flags;
2281 dbg("%s - port %d", __func__, port->number);
2283 if (edge_port == NULL)
2285 if (edge_port->close_pending == 1)
2288 spin_lock_irqsave(&edge_port->ep_lock, flags);
2289 chars = edge_buf_data_avail(edge_port->ep_out_buf);
2290 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2292 dbg ("%s - returns %d", __func__, chars);
2296 static void edge_throttle (struct usb_serial_port *port)
2298 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2299 struct tty_struct *tty;
2302 dbg("%s - port %d", __func__, port->number);
2304 if (edge_port == NULL)
2309 dbg ("%s - no tty available", __func__);
2313 /* if we are implementing XON/XOFF, send the stop character */
2315 unsigned char stop_char = STOP_CHAR(tty);
2316 status = edge_write (port, &stop_char, 1);
2318 dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status);
2322 /* if we are implementing RTS/CTS, stop reads */
2323 /* and the Edgeport will clear the RTS line */
2325 stop_read(edge_port);
2329 static void edge_unthrottle (struct usb_serial_port *port)
2331 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2332 struct tty_struct *tty;
2335 dbg("%s - port %d", __func__, port->number);
2337 if (edge_port == NULL)
2342 dbg ("%s - no tty available", __func__);
2346 /* if we are implementing XON/XOFF, send the start character */
2348 unsigned char start_char = START_CHAR(tty);
2349 status = edge_write (port, &start_char, 1);
2351 dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status);
2355 /* if we are implementing RTS/CTS, restart reads */
2356 /* are the Edgeport will assert the RTS line */
2357 if (C_CRTSCTS(tty)) {
2358 status = restart_read(edge_port);
2360 dev_err(&port->dev, "%s - read bulk usb_submit_urb failed with value %d\n", __func__, status);
2365 static void stop_read(struct edgeport_port *edge_port)
2367 unsigned long flags;
2369 spin_lock_irqsave(&edge_port->ep_lock, flags);
2371 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
2372 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
2373 edge_port->shadow_mcr &= ~MCR_RTS;
2375 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2378 static int restart_read(struct edgeport_port *edge_port)
2382 unsigned long flags;
2384 spin_lock_irqsave(&edge_port->ep_lock, flags);
2386 if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
2387 urb = edge_port->port->read_urb;
2388 urb->complete = edge_bulk_in_callback;
2389 urb->context = edge_port;
2390 urb->dev = edge_port->port->serial->dev;
2391 status = usb_submit_urb(urb, GFP_ATOMIC);
2393 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2394 edge_port->shadow_mcr |= MCR_RTS;
2396 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2401 static void change_port_settings (struct edgeport_port *edge_port, struct ktermios *old_termios)
2403 struct ump_uart_config *config;
2404 struct tty_struct *tty;
2408 int port_number = edge_port->port->number - edge_port->port->serial->minor;
2410 dbg("%s - port %d", __func__, edge_port->port->number);
2412 tty = edge_port->port->tty;
2414 config = kmalloc (sizeof (*config), GFP_KERNEL);
2416 dev_err (&edge_port->port->dev, "%s - out of memory\n", __func__);
2420 cflag = tty->termios->c_cflag;
2424 /* These flags must be set */
2425 config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
2426 config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
2427 config->bUartMode = (__u8)(edge_port->bUartMode);
2429 switch (cflag & CSIZE) {
2431 config->bDataBits = UMP_UART_CHAR5BITS;
2432 dbg ("%s - data bits = 5", __func__);
2435 config->bDataBits = UMP_UART_CHAR6BITS;
2436 dbg ("%s - data bits = 6", __func__);
2439 config->bDataBits = UMP_UART_CHAR7BITS;
2440 dbg ("%s - data bits = 7", __func__);
2444 config->bDataBits = UMP_UART_CHAR8BITS;
2445 dbg ("%s - data bits = 8", __func__);
2449 if (cflag & PARENB) {
2450 if (cflag & PARODD) {
2451 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2452 config->bParity = UMP_UART_ODDPARITY;
2453 dbg("%s - parity = odd", __func__);
2455 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2456 config->bParity = UMP_UART_EVENPARITY;
2457 dbg("%s - parity = even", __func__);
2460 config->bParity = UMP_UART_NOPARITY;
2461 dbg("%s - parity = none", __func__);
2464 if (cflag & CSTOPB) {
2465 config->bStopBits = UMP_UART_STOPBIT2;
2466 dbg("%s - stop bits = 2", __func__);
2468 config->bStopBits = UMP_UART_STOPBIT1;
2469 dbg("%s - stop bits = 1", __func__);
2472 /* figure out the flow control settings */
2473 if (cflag & CRTSCTS) {
2474 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
2475 config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
2476 dbg("%s - RTS/CTS is enabled", __func__);
2478 dbg("%s - RTS/CTS is disabled", __func__);
2479 tty->hw_stopped = 0;
2480 restart_read(edge_port);
2483 /* if we are implementing XON/XOFF, set the start and stop character in the device */
2484 if (I_IXOFF(tty) || I_IXON(tty)) {
2485 config->cXon = START_CHAR(tty);
2486 config->cXoff = STOP_CHAR(tty);
2488 /* if we are implementing INBOUND XON/XOFF */
2490 config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
2491 dbg ("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2492 __func__, config->cXon, config->cXoff);
2494 dbg ("%s - INBOUND XON/XOFF is disabled", __func__);
2497 /* if we are implementing OUTBOUND XON/XOFF */
2499 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
2500 dbg ("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2501 __func__, config->cXon, config->cXoff);
2503 dbg ("%s - OUTBOUND XON/XOFF is disabled", __func__);
2507 tty->termios->c_cflag &= ~CMSPAR;
2509 /* Round the baud rate */
2510 baud = tty_get_baud_rate(tty);
2512 /* pick a default, any default... */
2515 tty_encode_baud_rate(tty, baud, baud);
2517 edge_port->baud_rate = baud;
2518 config->wBaudRate = (__u16)((461550L + baud/2) / baud);
2520 /* FIXME: Recompute actual baud from divisor here */
2522 dbg ("%s - baud rate = %d, wBaudRate = %d", __func__, baud, config->wBaudRate);
2524 dbg ("wBaudRate: %d", (int)(461550L / config->wBaudRate));
2525 dbg ("wFlags: 0x%x", config->wFlags);
2526 dbg ("bDataBits: %d", config->bDataBits);
2527 dbg ("bParity: %d", config->bParity);
2528 dbg ("bStopBits: %d", config->bStopBits);
2529 dbg ("cXon: %d", config->cXon);
2530 dbg ("cXoff: %d", config->cXoff);
2531 dbg ("bUartMode: %d", config->bUartMode);
2533 /* move the word values into big endian mode */
2534 cpu_to_be16s (&config->wFlags);
2535 cpu_to_be16s (&config->wBaudRate);
2537 status = TIWriteCommandSync (edge_port->port->serial->dev,
2539 (__u8)(UMPM_UART1_PORT + port_number),
2544 dbg ("%s - error %d when trying to write config to device",
2553 static void edge_set_termios (struct usb_serial_port *port, struct ktermios *old_termios)
2555 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2556 struct tty_struct *tty = port->tty;
2559 cflag = tty->termios->c_cflag;
2561 dbg("%s - clfag %08x iflag %08x", __func__,
2562 tty->termios->c_cflag, tty->termios->c_iflag);
2563 dbg("%s - old clfag %08x old iflag %08x", __func__,
2564 old_termios->c_cflag, old_termios->c_iflag);
2566 dbg("%s - port %d", __func__, port->number);
2568 if (edge_port == NULL)
2571 /* change the port settings to the new ones specified */
2572 change_port_settings (edge_port, old_termios);
2577 static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear)
2579 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2581 unsigned long flags;
2583 dbg("%s - port %d", __func__, port->number);
2585 spin_lock_irqsave(&edge_port->ep_lock, flags);
2586 mcr = edge_port->shadow_mcr;
2587 if (set & TIOCM_RTS)
2589 if (set & TIOCM_DTR)
2591 if (set & TIOCM_LOOP)
2592 mcr |= MCR_LOOPBACK;
2594 if (clear & TIOCM_RTS)
2596 if (clear & TIOCM_DTR)
2598 if (clear & TIOCM_LOOP)
2599 mcr &= ~MCR_LOOPBACK;
2601 edge_port->shadow_mcr = mcr;
2602 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2604 TIRestoreMCR (edge_port, mcr);
2609 static int edge_tiocmget(struct usb_serial_port *port, struct file *file)
2611 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2612 unsigned int result = 0;
2615 unsigned long flags;
2617 dbg("%s - port %d", __func__, port->number);
2619 spin_lock_irqsave(&edge_port->ep_lock, flags);
2621 msr = edge_port->shadow_msr;
2622 mcr = edge_port->shadow_mcr;
2623 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
2624 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
2625 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
2626 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
2627 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
2628 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
2631 dbg("%s -- %x", __func__, result);
2632 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2637 static int get_serial_info (struct edgeport_port *edge_port, struct serial_struct __user *retinfo)
2639 struct serial_struct tmp;
2644 memset(&tmp, 0, sizeof(tmp));
2646 tmp.type = PORT_16550A;
2647 tmp.line = edge_port->port->serial->minor;
2648 tmp.port = edge_port->port->number;
2650 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2651 tmp.xmit_fifo_size = edge_port->port->bulk_out_size;
2652 tmp.baud_base = 9600;
2653 tmp.close_delay = 5*HZ;
2654 tmp.closing_wait = closing_wait;
2655 // tmp.custom_divisor = state->custom_divisor;
2656 // tmp.hub6 = state->hub6;
2657 // tmp.io_type = state->io_type;
2660 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2665 static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
2667 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2668 struct async_icount cnow;
2669 struct async_icount cprev;
2671 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
2675 dbg("%s - (%d) TIOCINQ", __func__, port->number);
2676 // return get_number_bytes_avail(edge_port, (unsigned int *) arg);
2680 dbg("%s - (%d) TIOCSERGETLSR", __func__, port->number);
2681 // return get_lsr_info(edge_port, (unsigned int *) arg);
2685 dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
2686 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
2690 dbg("%s - (%d) TIOCSSERIAL", __func__, port->number);
2694 dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
2695 cprev = edge_port->icount;
2697 interruptible_sleep_on(&edge_port->delta_msr_wait);
2698 /* see if a signal did it */
2699 if (signal_pending(current))
2700 return -ERESTARTSYS;
2701 cnow = edge_port->icount;
2702 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2703 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2704 return -EIO; /* no change => error */
2705 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2706 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2707 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2708 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
2717 dbg ("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
2718 port->number, edge_port->icount.rx, edge_port->icount.tx);
2719 if (copy_to_user((void __user *)arg, &edge_port->icount, sizeof(edge_port->icount)))
2724 return -ENOIOCTLCMD;
2727 static void edge_break (struct usb_serial_port *port, int break_state)
2729 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2732 dbg ("%s - state = %d", __func__, break_state);
2734 /* chase the port close */
2735 TIChasePort (edge_port, 0, 0);
2737 if (break_state == -1) {
2738 status = TISetBreak (edge_port);
2740 status = TIClearBreak (edge_port);
2743 dbg ("%s - error %d sending break set/clear command.",
2748 static int edge_startup (struct usb_serial *serial)
2750 struct edgeport_serial *edge_serial;
2751 struct edgeport_port *edge_port;
2752 struct usb_device *dev;
2758 /* create our private serial structure */
2759 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
2760 if (edge_serial == NULL) {
2761 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
2764 mutex_init(&edge_serial->es_lock);
2765 edge_serial->serial = serial;
2766 usb_set_serial_data(serial, edge_serial);
2768 status = TIDownloadFirmware (edge_serial);
2770 kfree (edge_serial);
2774 /* set up our port private structures */
2775 for (i = 0; i < serial->num_ports; ++i) {
2776 edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
2777 if (edge_port == NULL) {
2778 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
2781 spin_lock_init(&edge_port->ep_lock);
2782 edge_port->ep_out_buf = edge_buf_alloc(EDGE_OUT_BUF_SIZE);
2783 if (edge_port->ep_out_buf == NULL) {
2784 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
2788 edge_port->port = serial->port[i];
2789 edge_port->edge_serial = edge_serial;
2790 usb_set_serial_port_data(serial->port[i], edge_port);
2791 edge_port->bUartMode = default_uart_mode;
2797 for (--i; i>=0; --i) {
2798 edge_port = usb_get_serial_port_data(serial->port[i]);
2799 edge_buf_free(edge_port->ep_out_buf);
2801 usb_set_serial_port_data(serial->port[i], NULL);
2803 kfree (edge_serial);
2804 usb_set_serial_data(serial, NULL);
2808 static void edge_shutdown (struct usb_serial *serial)
2811 struct edgeport_port *edge_port;
2813 dbg ("%s", __func__);
2815 for (i = 0; i < serial->num_ports; ++i) {
2816 edge_port = usb_get_serial_port_data(serial->port[i]);
2817 edge_remove_sysfs_attrs(edge_port->port);
2818 edge_buf_free(edge_port->ep_out_buf);
2820 usb_set_serial_port_data(serial->port[i], NULL);
2822 kfree(usb_get_serial_data(serial));
2823 usb_set_serial_data(serial, NULL);
2827 /* Sysfs Attributes */
2829 static ssize_t show_uart_mode(struct device *dev,
2830 struct device_attribute *attr, char *buf)
2832 struct usb_serial_port *port = to_usb_serial_port(dev);
2833 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2835 return sprintf(buf, "%d\n", edge_port->bUartMode);
2838 static ssize_t store_uart_mode(struct device *dev,
2839 struct device_attribute *attr, const char *valbuf, size_t count)
2841 struct usb_serial_port *port = to_usb_serial_port(dev);
2842 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2843 unsigned int v = simple_strtoul(valbuf, NULL, 0);
2845 dbg("%s: setting uart_mode = %d", __func__, v);
2848 edge_port->bUartMode = v;
2850 dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
2855 static DEVICE_ATTR(uart_mode, S_IWUSR | S_IRUGO, show_uart_mode, store_uart_mode);
2857 static int edge_create_sysfs_attrs(struct usb_serial_port *port)
2859 return device_create_file(&port->dev, &dev_attr_uart_mode);
2862 static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
2864 device_remove_file(&port->dev, &dev_attr_uart_mode);
2869 /* Circular Buffer */
2874 * Allocate a circular buffer and all associated memory.
2877 static struct edge_buf *edge_buf_alloc(unsigned int size)
2879 struct edge_buf *eb;
2885 eb = kmalloc(sizeof(struct edge_buf), GFP_KERNEL);
2889 eb->buf_buf = kmalloc(size, GFP_KERNEL);
2890 if (eb->buf_buf == NULL) {
2895 eb->buf_size = size;
2896 eb->buf_get = eb->buf_put = eb->buf_buf;
2905 * Free the buffer and all associated memory.
2908 static void edge_buf_free(struct edge_buf *eb)
2920 * Clear out all data in the circular buffer.
2923 static void edge_buf_clear(struct edge_buf *eb)
2926 eb->buf_get = eb->buf_put;
2927 /* equivalent to a get of all data available */
2932 * edge_buf_data_avail
2934 * Return the number of bytes of data available in the circular
2938 static unsigned int edge_buf_data_avail(struct edge_buf *eb)
2941 return ((eb->buf_size + eb->buf_put - eb->buf_get) % eb->buf_size);
2948 * edge_buf_space_avail
2950 * Return the number of bytes of space available in the circular
2954 static unsigned int edge_buf_space_avail(struct edge_buf *eb)
2957 return ((eb->buf_size + eb->buf_get - eb->buf_put - 1) % eb->buf_size);
2966 * Copy data data from a user buffer and put it into the circular buffer.
2967 * Restrict to the amount of space available.
2969 * Return the number of bytes copied.
2972 static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
2981 len = edge_buf_space_avail(eb);
2988 len = eb->buf_buf + eb->buf_size - eb->buf_put;
2990 memcpy(eb->buf_put, buf, len);
2991 memcpy(eb->buf_buf, buf+len, count - len);
2992 eb->buf_put = eb->buf_buf + count - len;
2994 memcpy(eb->buf_put, buf, count);
2996 eb->buf_put += count;
2997 else /* count == len */
2998 eb->buf_put = eb->buf_buf;
3008 * Get data from the circular buffer and copy to the given buffer.
3009 * Restrict to the amount of data available.
3011 * Return the number of bytes copied.
3014 static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
3023 len = edge_buf_data_avail(eb);
3030 len = eb->buf_buf + eb->buf_size - eb->buf_get;
3032 memcpy(buf, eb->buf_get, len);
3033 memcpy(buf+len, eb->buf_buf, count - len);
3034 eb->buf_get = eb->buf_buf + count - len;
3036 memcpy(buf, eb->buf_get, count);
3038 eb->buf_get += count;
3039 else /* count == len */
3040 eb->buf_get = eb->buf_buf;
3047 static struct usb_serial_driver edgeport_1port_device = {
3049 .owner = THIS_MODULE,
3050 .name = "edgeport_ti_1",
3052 .description = "Edgeport TI 1 port adapter",
3053 .usb_driver = &io_driver,
3054 .id_table = edgeport_1port_id_table,
3057 .close = edge_close,
3058 .throttle = edge_throttle,
3059 .unthrottle = edge_unthrottle,
3060 .attach = edge_startup,
3061 .shutdown = edge_shutdown,
3062 .port_probe = edge_create_sysfs_attrs,
3063 .ioctl = edge_ioctl,
3064 .set_termios = edge_set_termios,
3065 .tiocmget = edge_tiocmget,
3066 .tiocmset = edge_tiocmset,
3067 .write = edge_write,
3068 .write_room = edge_write_room,
3069 .chars_in_buffer = edge_chars_in_buffer,
3070 .break_ctl = edge_break,
3071 .read_int_callback = edge_interrupt_callback,
3072 .read_bulk_callback = edge_bulk_in_callback,
3073 .write_bulk_callback = edge_bulk_out_callback,
3076 static struct usb_serial_driver edgeport_2port_device = {
3078 .owner = THIS_MODULE,
3079 .name = "edgeport_ti_2",
3081 .description = "Edgeport TI 2 port adapter",
3082 .usb_driver = &io_driver,
3083 .id_table = edgeport_2port_id_table,
3086 .close = edge_close,
3087 .throttle = edge_throttle,
3088 .unthrottle = edge_unthrottle,
3089 .attach = edge_startup,
3090 .shutdown = edge_shutdown,
3091 .port_probe = edge_create_sysfs_attrs,
3092 .ioctl = edge_ioctl,
3093 .set_termios = edge_set_termios,
3094 .tiocmget = edge_tiocmget,
3095 .tiocmset = edge_tiocmset,
3096 .write = edge_write,
3097 .write_room = edge_write_room,
3098 .chars_in_buffer = edge_chars_in_buffer,
3099 .break_ctl = edge_break,
3100 .read_int_callback = edge_interrupt_callback,
3101 .read_bulk_callback = edge_bulk_in_callback,
3102 .write_bulk_callback = edge_bulk_out_callback,
3106 static int __init edgeport_init(void)
3109 retval = usb_serial_register(&edgeport_1port_device);
3111 goto failed_1port_device_register;
3112 retval = usb_serial_register(&edgeport_2port_device);
3114 goto failed_2port_device_register;
3115 retval = usb_register(&io_driver);
3117 goto failed_usb_register;
3118 info(DRIVER_DESC " " DRIVER_VERSION);
3120 failed_usb_register:
3121 usb_serial_deregister(&edgeport_2port_device);
3122 failed_2port_device_register:
3123 usb_serial_deregister(&edgeport_1port_device);
3124 failed_1port_device_register:
3128 static void __exit edgeport_exit (void)
3130 usb_deregister (&io_driver);
3131 usb_serial_deregister (&edgeport_1port_device);
3132 usb_serial_deregister (&edgeport_2port_device);
3135 module_init(edgeport_init);
3136 module_exit(edgeport_exit);
3138 /* Module information */
3139 MODULE_AUTHOR(DRIVER_AUTHOR);
3140 MODULE_DESCRIPTION(DRIVER_DESC);
3141 MODULE_LICENSE("GPL");
3142 MODULE_FIRMWARE("edgeport/down3.bin");
3144 module_param(debug, bool, S_IRUGO | S_IWUSR);
3145 MODULE_PARM_DESC(debug, "Debug enabled or not");
3147 module_param(low_latency, bool, S_IRUGO | S_IWUSR);
3148 MODULE_PARM_DESC(low_latency, "Low latency enabled or not");
3150 module_param(closing_wait, int, S_IRUGO | S_IWUSR);
3151 MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
3153 module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
3154 MODULE_PARM_DESC(ignore_cpu_rev, "Ignore the cpu revision when connecting to a device");
3156 module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
3157 MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");