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>.
20 #include <linux/kernel.h>
21 #include <linux/jiffies.h>
22 #include <linux/errno.h>
23 #include <linux/slab.h>
24 #include <linux/tty.h>
25 #include <linux/tty_driver.h>
26 #include <linux/tty_flip.h>
27 #include <linux/module.h>
28 #include <linux/spinlock.h>
29 #include <linux/mutex.h>
30 #include <linux/serial.h>
31 #include <linux/swab.h>
32 #include <linux/kfifo.h>
33 #include <linux/ioctl.h>
34 #include <linux/firmware.h>
35 #include <linux/uaccess.h>
36 #include <linux/usb.h>
37 #include <linux/usb/serial.h>
40 #include "io_usbvend.h"
43 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
44 #define DRIVER_DESC "Edgeport USB Serial Driver"
46 #define EPROM_PAGE_SIZE 64
49 /* different hardware types */
50 #define HARDWARE_TYPE_930 0
51 #define HARDWARE_TYPE_TIUMP 1
53 /* IOCTL_PRIVATE_TI_GET_MODE Definitions */
54 #define TI_MODE_CONFIGURING 0 /* Device has not entered start device */
55 #define TI_MODE_BOOT 1 /* Staying in boot mode */
56 #define TI_MODE_DOWNLOAD 2 /* Made it to download mode */
57 #define TI_MODE_TRANSITIONING 3 /* Currently in boot mode but
58 transitioning to download mode */
61 #define EDGE_READ_URB_RUNNING 0
62 #define EDGE_READ_URB_STOPPING 1
63 #define EDGE_READ_URB_STOPPED 2
65 #define EDGE_CLOSING_WAIT 4000 /* in .01 sec */
68 /* Product information read from the Edgeport */
70 int TiMode; /* Current TI Mode */
71 __u8 hardware_type; /* Type of hardware */
72 } __attribute__((packed));
75 * Edgeport firmware header
77 * "build_number" has been set to 0 in all three of the images I have
78 * seen, and Digi Tech Support suggests that it is safe to ignore it.
80 * "length" is the number of bytes of actual data following the header.
82 * "checksum" is the low order byte resulting from adding the values of
85 struct edgeport_fw_hdr {
93 struct edgeport_port {
100 __u32 ump_read_timeout; /* Number of milliseconds the UMP will
101 wait without data before completing
107 struct edgeport_serial *edge_serial;
108 struct usb_serial_port *port;
109 __u8 bUartMode; /* Port type, 0: RS232, etc. */
111 int ep_read_urb_state;
112 int ep_write_urb_in_use;
115 struct edgeport_serial {
116 struct product_info product_info;
117 u8 TI_I2C_Type; /* Type of I2C in UMP */
118 u8 TiReadI2C; /* Set to TRUE if we have read the
120 struct mutex es_lock;
122 struct usb_serial *serial;
123 struct delayed_work heartbeat_work;
129 /* Devices that this driver supports */
130 static const struct usb_device_id edgeport_1port_id_table[] = {
131 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
132 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
133 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
134 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
135 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
136 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
137 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
138 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
139 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
140 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
141 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
142 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
143 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
144 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
145 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
146 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
150 static const struct usb_device_id edgeport_2port_id_table[] = {
151 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
152 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
153 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
154 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
155 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
156 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
157 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
158 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
159 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
160 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
161 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
162 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
163 /* The 4, 8 and 16 port devices show up as multiple 2 port devices */
164 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
165 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
166 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
167 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
168 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
172 /* Devices that this driver supports */
173 static const struct usb_device_id id_table_combined[] = {
174 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
175 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
176 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
177 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
178 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
179 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
180 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
181 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
182 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
183 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
184 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
185 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
186 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
187 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
188 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
189 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
190 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
191 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
192 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
193 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
194 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
195 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
196 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
197 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
198 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
199 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
200 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
201 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
202 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
203 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
204 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
205 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
206 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
210 MODULE_DEVICE_TABLE(usb, id_table_combined);
212 static int closing_wait = EDGE_CLOSING_WAIT;
213 static bool ignore_cpu_rev;
214 static int default_uart_mode; /* RS232 */
216 static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
219 static void stop_read(struct edgeport_port *edge_port);
220 static int restart_read(struct edgeport_port *edge_port);
222 static void edge_set_termios(struct tty_struct *tty,
223 struct usb_serial_port *port, struct ktermios *old_termios);
224 static void edge_send(struct usb_serial_port *port, struct tty_struct *tty);
226 /* sysfs attributes */
227 static int edge_create_sysfs_attrs(struct usb_serial_port *port);
228 static int edge_remove_sysfs_attrs(struct usb_serial_port *port);
231 * Some release of Edgeport firmware "down3.bin" after version 4.80
232 * introduced code to automatically disconnect idle devices on some
233 * Edgeport models after periods of inactivity, typically ~60 seconds.
234 * This occurs without regard to whether ports on the device are open
235 * or not. Digi International Tech Support suggested:
237 * 1. Adding driver "heartbeat" code to reset the firmware timer by
238 * requesting a descriptor record every 15 seconds, which should be
239 * effective with newer firmware versions that require it, and benign
240 * with older versions that do not. In practice 40 seconds seems often
242 * 2. The heartbeat code is currently required only on Edgeport/416 models.
244 #define FW_HEARTBEAT_VERSION_CUTOFF ((4 << 8) + 80)
245 #define FW_HEARTBEAT_SECS 40
247 /* Timeouts in msecs: firmware downloads take longer */
248 #define TI_VSEND_TIMEOUT_DEFAULT 1000
249 #define TI_VSEND_TIMEOUT_FW_DOWNLOAD 10000
251 static int ti_vread_sync(struct usb_device *dev, __u8 request,
252 __u16 value, __u16 index, u8 *data, int size)
256 status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
257 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
258 value, index, data, size, 1000);
261 if (status != size) {
262 dev_dbg(&dev->dev, "%s - wanted to write %d, but only wrote %d\n",
263 __func__, size, status);
269 static int ti_vsend_sync(struct usb_device *dev, u8 request, u16 value,
270 u16 index, u8 *data, int size, int timeout)
274 status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
275 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
276 value, index, data, size, timeout);
279 if (status != size) {
280 dev_dbg(&dev->dev, "%s - wanted to write %d, but only wrote %d\n",
281 __func__, size, status);
287 static int send_cmd(struct usb_device *dev, __u8 command,
288 __u8 moduleid, __u16 value, u8 *data,
291 return ti_vsend_sync(dev, command, value, moduleid, data, size,
292 TI_VSEND_TIMEOUT_DEFAULT);
295 /* clear tx/rx buffers and fifo in TI UMP */
296 static int purge_port(struct usb_serial_port *port, __u16 mask)
298 int port_number = port->port_number;
300 dev_dbg(&port->dev, "%s - port %d, mask %x\n", __func__, port_number, mask);
302 return send_cmd(port->serial->dev,
304 (__u8)(UMPM_UART1_PORT + port_number),
311 * read_download_mem - Read edgeport memory from TI chip
312 * @dev: usb device pointer
313 * @start_address: Device CPU address at which to read
314 * @length: Length of above data
315 * @address_type: Can read both XDATA and I2C
316 * @buffer: pointer to input data buffer
318 static int read_download_mem(struct usb_device *dev, int start_address,
319 int length, __u8 address_type, __u8 *buffer)
323 u16 be_start_address;
325 dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, length);
327 /* Read in blocks of 64 bytes
328 * (TI firmware can't handle more than 64 byte reads)
334 read_length = (__u8)length;
336 if (read_length > 1) {
337 dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, read_length);
340 * NOTE: Must use swab as wIndex is sent in little-endian
341 * byte order regardless of host byte order.
343 be_start_address = swab16((u16)start_address);
344 status = ti_vread_sync(dev, UMPC_MEMORY_READ,
347 buffer, read_length);
350 dev_dbg(&dev->dev, "%s - ERROR %x\n", __func__, status);
355 usb_serial_debug_data(&dev->dev, __func__, read_length, buffer);
357 /* Update pointers/length */
358 start_address += read_length;
359 buffer += read_length;
360 length -= read_length;
366 static int read_ram(struct usb_device *dev, int start_address,
367 int length, __u8 *buffer)
369 return read_download_mem(dev, start_address, length,
370 DTK_ADDR_SPACE_XDATA, buffer);
373 /* Read edgeport memory to a given block */
374 static int read_boot_mem(struct edgeport_serial *serial,
375 int start_address, int length, __u8 *buffer)
380 for (i = 0; i < length; i++) {
381 status = ti_vread_sync(serial->serial->dev,
382 UMPC_MEMORY_READ, serial->TI_I2C_Type,
383 (__u16)(start_address+i), &buffer[i], 0x01);
385 dev_dbg(&serial->serial->dev->dev, "%s - ERROR %x\n", __func__, status);
390 dev_dbg(&serial->serial->dev->dev, "%s - start_address = %x, length = %d\n",
391 __func__, start_address, length);
392 usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer);
394 serial->TiReadI2C = 1;
399 /* Write given block to TI EPROM memory */
400 static int write_boot_mem(struct edgeport_serial *serial,
401 int start_address, int length, __u8 *buffer)
407 /* Must do a read before write */
408 if (!serial->TiReadI2C) {
409 temp = kmalloc(1, GFP_KERNEL);
413 status = read_boot_mem(serial, 0, 1, temp);
419 for (i = 0; i < length; ++i) {
420 status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
421 buffer[i], (u16)(i + start_address), NULL,
422 0, TI_VSEND_TIMEOUT_DEFAULT);
427 dev_dbg(&serial->serial->dev->dev, "%s - start_sddr = %x, length = %d\n", __func__, start_address, length);
428 usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer);
434 /* Write edgeport I2C memory to TI chip */
435 static int write_i2c_mem(struct edgeport_serial *serial,
436 int start_address, int length, __u8 address_type, __u8 *buffer)
438 struct device *dev = &serial->serial->dev->dev;
441 u16 be_start_address;
443 /* We can only send a maximum of 1 aligned byte page at a time */
445 /* calculate the number of bytes left in the first page */
446 write_length = EPROM_PAGE_SIZE -
447 (start_address & (EPROM_PAGE_SIZE - 1));
449 if (write_length > length)
450 write_length = length;
452 dev_dbg(dev, "%s - BytesInFirstPage Addr = %x, length = %d\n",
453 __func__, start_address, write_length);
454 usb_serial_debug_data(dev, __func__, write_length, buffer);
459 * NOTE: Must use swab as wIndex is sent in little-endian byte order
460 * regardless of host byte order.
462 be_start_address = swab16((u16)start_address);
463 status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
464 (u16)address_type, be_start_address,
465 buffer, write_length, TI_VSEND_TIMEOUT_DEFAULT);
467 dev_dbg(dev, "%s - ERROR %d\n", __func__, status);
471 length -= write_length;
472 start_address += write_length;
473 buffer += write_length;
475 /* We should be aligned now -- can write
476 max page size bytes at a time */
478 if (length > EPROM_PAGE_SIZE)
479 write_length = EPROM_PAGE_SIZE;
481 write_length = length;
483 dev_dbg(dev, "%s - Page Write Addr = %x, length = %d\n",
484 __func__, start_address, write_length);
485 usb_serial_debug_data(dev, __func__, write_length, buffer);
490 * NOTE: Must use swab as wIndex is sent in little-endian byte
491 * order regardless of host byte order.
493 be_start_address = swab16((u16)start_address);
494 status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
495 (u16)address_type, be_start_address, buffer,
496 write_length, TI_VSEND_TIMEOUT_DEFAULT);
498 dev_err(dev, "%s - ERROR %d\n", __func__, status);
502 length -= write_length;
503 start_address += write_length;
504 buffer += write_length;
509 /* Examine the UMP DMA registers and LSR
511 * Check the MSBit of the X and Y DMA byte count registers.
512 * A zero in this bit indicates that the TX DMA buffers are empty
513 * then check the TX Empty bit in the UART.
515 static int tx_active(struct edgeport_port *port)
518 struct out_endpoint_desc_block *oedb;
522 oedb = kmalloc(sizeof(*oedb), GFP_KERNEL);
526 lsr = kmalloc(1, GFP_KERNEL); /* Sigh, that's right, just one byte,
527 as not all platforms can do DMA
533 /* Read the DMA Count Registers */
534 status = read_ram(port->port->serial->dev, port->dma_address,
535 sizeof(*oedb), (void *)oedb);
537 goto exit_is_tx_active;
539 dev_dbg(&port->port->dev, "%s - XByteCount 0x%X\n", __func__, oedb->XByteCount);
542 status = read_ram(port->port->serial->dev,
543 port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr);
546 goto exit_is_tx_active;
547 dev_dbg(&port->port->dev, "%s - LSR = 0x%X\n", __func__, *lsr);
549 /* If either buffer has data or we are transmitting then return TRUE */
550 if ((oedb->XByteCount & 0x80) != 0)
553 if ((*lsr & UMP_UART_LSR_TX_MASK) == 0)
556 /* We return Not Active if we get any kind of error */
558 dev_dbg(&port->port->dev, "%s - return %d\n", __func__, bytes_left);
565 static int choose_config(struct usb_device *dev)
568 * There may be multiple configurations on this device, in which case
569 * we would need to read and parse all of them to find out which one
570 * we want. However, we just support one config at this point,
571 * configuration # 1, which is Config Descriptor 0.
574 dev_dbg(&dev->dev, "%s - Number of Interfaces = %d\n",
575 __func__, dev->config->desc.bNumInterfaces);
576 dev_dbg(&dev->dev, "%s - MAX Power = %d\n",
577 __func__, dev->config->desc.bMaxPower * 2);
579 if (dev->config->desc.bNumInterfaces != 1) {
580 dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __func__);
587 static int read_rom(struct edgeport_serial *serial,
588 int start_address, int length, __u8 *buffer)
592 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
593 status = read_download_mem(serial->serial->dev,
599 status = read_boot_mem(serial, start_address, length,
605 static int write_rom(struct edgeport_serial *serial, int start_address,
606 int length, __u8 *buffer)
608 if (serial->product_info.TiMode == TI_MODE_BOOT)
609 return write_boot_mem(serial, start_address, length,
612 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
613 return write_i2c_mem(serial, start_address, length,
614 serial->TI_I2C_Type, buffer);
620 /* Read a descriptor header from I2C based on type */
621 static int get_descriptor_addr(struct edgeport_serial *serial,
622 int desc_type, struct ti_i2c_desc *rom_desc)
627 /* Search for requested descriptor in I2C */
630 status = read_rom(serial,
632 sizeof(struct ti_i2c_desc),
637 if (rom_desc->Type == desc_type)
638 return start_address;
640 start_address = start_address + sizeof(struct ti_i2c_desc) +
641 le16_to_cpu(rom_desc->Size);
643 } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
648 /* Validate descriptor checksum */
649 static int valid_csum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
654 for (i = 0; i < le16_to_cpu(rom_desc->Size); i++)
655 cs = (__u8)(cs + buffer[i]);
657 if (cs != rom_desc->CheckSum) {
658 pr_debug("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
664 /* Make sure that the I2C image is good */
665 static int check_i2c_image(struct edgeport_serial *serial)
667 struct device *dev = &serial->serial->dev->dev;
669 struct ti_i2c_desc *rom_desc;
670 int start_address = 2;
674 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
678 buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL);
684 /* Read the first byte (Signature0) must be 0x52 or 0x10 */
685 status = read_rom(serial, 0, 1, buffer);
689 if (*buffer != UMP5152 && *buffer != UMP3410) {
690 dev_err(dev, "%s - invalid buffer signature\n", __func__);
696 /* Validate the I2C */
697 status = read_rom(serial,
699 sizeof(struct ti_i2c_desc),
704 if ((start_address + sizeof(struct ti_i2c_desc) +
705 le16_to_cpu(rom_desc->Size)) > TI_MAX_I2C_SIZE) {
707 dev_dbg(dev, "%s - structure too big, erroring out.\n", __func__);
711 dev_dbg(dev, "%s Type = 0x%x\n", __func__, rom_desc->Type);
713 /* Skip type 2 record */
714 ttype = rom_desc->Type & 0x0f;
715 if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
716 && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) {
717 /* Read the descriptor data */
718 status = read_rom(serial, start_address +
719 sizeof(struct ti_i2c_desc),
720 le16_to_cpu(rom_desc->Size),
725 status = valid_csum(rom_desc, buffer);
729 start_address = start_address + sizeof(struct ti_i2c_desc) +
730 le16_to_cpu(rom_desc->Size);
732 } while ((rom_desc->Type != I2C_DESC_TYPE_ION) &&
733 (start_address < TI_MAX_I2C_SIZE));
735 if ((rom_desc->Type != I2C_DESC_TYPE_ION) ||
736 (start_address > TI_MAX_I2C_SIZE))
745 static int get_manuf_info(struct edgeport_serial *serial, __u8 *buffer)
749 struct ti_i2c_desc *rom_desc;
750 struct edge_ti_manuf_descriptor *desc;
751 struct device *dev = &serial->serial->dev->dev;
753 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
757 start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
760 if (!start_address) {
761 dev_dbg(dev, "%s - Edge Descriptor not found in I2C\n", __func__);
766 /* Read the descriptor data */
767 status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc),
768 le16_to_cpu(rom_desc->Size), buffer);
772 status = valid_csum(rom_desc, buffer);
774 desc = (struct edge_ti_manuf_descriptor *)buffer;
775 dev_dbg(dev, "%s - IonConfig 0x%x\n", __func__, desc->IonConfig);
776 dev_dbg(dev, "%s - Version %d\n", __func__, desc->Version);
777 dev_dbg(dev, "%s - Cpu/Board 0x%x\n", __func__, desc->CpuRev_BoardRev);
778 dev_dbg(dev, "%s - NumPorts %d\n", __func__, desc->NumPorts);
779 dev_dbg(dev, "%s - NumVirtualPorts %d\n", __func__, desc->NumVirtualPorts);
780 dev_dbg(dev, "%s - TotalPorts %d\n", __func__, desc->TotalPorts);
787 /* Build firmware header used for firmware update */
788 static int build_i2c_fw_hdr(u8 *header, struct device *dev,
789 const struct firmware *fw)
795 struct ti_i2c_desc *i2c_header;
796 struct ti_i2c_image_header *img_header;
797 struct ti_i2c_firmware_rec *firmware_rec;
798 struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data;
800 /* In order to update the I2C firmware we must change the type 2 record
801 * to type 0xF2. This will force the UMP to come up in Boot Mode.
802 * Then while in boot mode, the driver will download the latest
803 * firmware (padded to 15.5k) into the UMP ram. And finally when the
804 * device comes back up in download mode the driver will cause the new
805 * firmware to be copied from the UMP Ram to I2C and the firmware will
806 * update the record type from 0xf2 to 0x02.
809 /* Allocate a 15.5k buffer + 2 bytes for version number
810 * (Firmware Record) */
811 buffer_size = (((1024 * 16) - 512 ) +
812 sizeof(struct ti_i2c_firmware_rec));
814 buffer = kmalloc(buffer_size, GFP_KERNEL);
818 // Set entire image of 0xffs
819 memset(buffer, 0xff, buffer_size);
821 /* Copy version number into firmware record */
822 firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
824 firmware_rec->Ver_Major = fw_hdr->major_version;
825 firmware_rec->Ver_Minor = fw_hdr->minor_version;
827 /* Pointer to fw_down memory image */
828 img_header = (struct ti_i2c_image_header *)&fw->data[4];
830 memcpy(buffer + sizeof(struct ti_i2c_firmware_rec),
831 &fw->data[4 + sizeof(struct ti_i2c_image_header)],
832 le16_to_cpu(img_header->Length));
834 for (i=0; i < buffer_size; i++) {
835 cs = (__u8)(cs + buffer[i]);
840 /* Build new header */
841 i2c_header = (struct ti_i2c_desc *)header;
842 firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data;
844 i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK;
845 i2c_header->Size = cpu_to_le16(buffer_size);
846 i2c_header->CheckSum = cs;
847 firmware_rec->Ver_Major = fw_hdr->major_version;
848 firmware_rec->Ver_Minor = fw_hdr->minor_version;
853 /* Try to figure out what type of I2c we have */
854 static int i2c_type_bootmode(struct edgeport_serial *serial)
856 struct device *dev = &serial->serial->dev->dev;
860 data = kmalloc(1, GFP_KERNEL);
864 /* Try to read type 2 */
865 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
866 DTK_ADDR_SPACE_I2C_TYPE_II, 0, data, 0x01);
868 dev_dbg(dev, "%s - read 2 status error = %d\n", __func__, status);
870 dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data);
871 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
872 dev_dbg(dev, "%s - ROM_TYPE_II\n", __func__);
873 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
877 /* Try to read type 3 */
878 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
879 DTK_ADDR_SPACE_I2C_TYPE_III, 0, data, 0x01);
881 dev_dbg(dev, "%s - read 3 status error = %d\n", __func__, status);
883 dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data);
884 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
885 dev_dbg(dev, "%s - ROM_TYPE_III\n", __func__);
886 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
890 dev_dbg(dev, "%s - Unknown\n", __func__);
891 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
898 static int bulk_xfer(struct usb_serial *serial, void *buffer,
899 int length, int *num_sent)
903 status = usb_bulk_msg(serial->dev,
904 usb_sndbulkpipe(serial->dev,
905 serial->port[0]->bulk_out_endpointAddress),
906 buffer, length, num_sent, 1000);
910 /* Download given firmware image to the device (IN BOOT MODE) */
911 static int download_code(struct edgeport_serial *serial, __u8 *image,
919 /* Transfer firmware image */
920 for (pos = 0; pos < image_length; ) {
921 /* Read the next buffer from file */
922 transfer = image_length - pos;
923 if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
924 transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
927 status = bulk_xfer(serial->serial, &image[pos],
931 /* Advance buffer pointer */
939 static int config_boot_dev(struct usb_device *dev)
944 static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc)
946 return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev);
949 static int check_fw_sanity(struct edgeport_serial *serial,
950 const struct firmware *fw)
955 struct device *dev = &serial->serial->interface->dev;
956 struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data;
958 if (fw->size < sizeof(struct edgeport_fw_hdr)) {
959 dev_err(dev, "incomplete fw header\n");
963 length_total = le16_to_cpu(fw_hdr->length) +
964 sizeof(struct edgeport_fw_hdr);
966 if (fw->size != length_total) {
967 dev_err(dev, "bad fw size (expected: %u, got: %zu)\n",
968 length_total, fw->size);
972 for (pos = sizeof(struct edgeport_fw_hdr); pos < fw->size; ++pos)
973 checksum += fw->data[pos];
975 if (checksum != fw_hdr->checksum) {
976 dev_err(dev, "bad fw checksum (expected: 0x%x, got: 0x%x)\n",
977 fw_hdr->checksum, checksum);
985 * DownloadTIFirmware - Download run-time operating firmware to the TI5052
987 * This routine downloads the main operating code into the TI5052, using the
988 * boot code already burned into E2PROM or ROM.
990 static int download_fw(struct edgeport_serial *serial,
991 const struct firmware *fw)
993 struct device *dev = &serial->serial->dev->dev;
996 struct edge_ti_manuf_descriptor *ti_manuf_desc;
997 struct usb_interface_descriptor *interface;
998 int download_cur_ver;
999 int download_new_ver;
1000 struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data;
1002 if (check_fw_sanity(serial, fw))
1005 /* If on-board version is newer, "fw_version" will be updated below. */
1006 serial->fw_version = (fw_hdr->major_version << 8) +
1007 fw_hdr->minor_version;
1009 /* This routine is entered by both the BOOT mode and the Download mode
1010 * We can determine which code is running by the reading the config
1011 * descriptor and if we have only one bulk pipe it is in boot mode
1013 serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
1015 /* Default to type 2 i2c */
1016 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1018 status = choose_config(serial->serial->dev);
1022 interface = &serial->serial->interface->cur_altsetting->desc;
1024 dev_err(dev, "%s - no interface set, error!\n", __func__);
1029 * Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
1030 * if we have more than one endpoint we are definitely in download
1033 if (interface->bNumEndpoints > 1)
1034 serial->product_info.TiMode = TI_MODE_DOWNLOAD;
1036 /* Otherwise we will remain in configuring mode */
1037 serial->product_info.TiMode = TI_MODE_CONFIGURING;
1039 /********************************************************************/
1041 /********************************************************************/
1042 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
1043 struct ti_i2c_desc *rom_desc;
1045 dev_dbg(dev, "%s - RUNNING IN DOWNLOAD MODE\n", __func__);
1047 status = check_i2c_image(serial);
1049 dev_dbg(dev, "%s - DOWNLOAD MODE -- BAD I2C\n", __func__);
1053 /* Validate Hardware version number
1054 * Read Manufacturing Descriptor from TI Based Edgeport
1056 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
1060 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
1062 kfree(ti_manuf_desc);
1066 /* Check version number of ION descriptor */
1067 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1068 dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n",
1069 __func__, ti_cpu_rev(ti_manuf_desc));
1070 kfree(ti_manuf_desc);
1074 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
1076 kfree(ti_manuf_desc);
1080 /* Search for type 2 record (firmware record) */
1081 start_address = get_descriptor_addr(serial,
1082 I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc);
1083 if (start_address != 0) {
1084 struct ti_i2c_firmware_rec *firmware_version;
1087 dev_dbg(dev, "%s - Found Type FIRMWARE (Type 2) record\n", __func__);
1089 firmware_version = kmalloc(sizeof(*firmware_version),
1091 if (!firmware_version) {
1093 kfree(ti_manuf_desc);
1097 /* Validate version number
1098 * Read the descriptor data
1100 status = read_rom(serial, start_address +
1101 sizeof(struct ti_i2c_desc),
1102 sizeof(struct ti_i2c_firmware_rec),
1103 (__u8 *)firmware_version);
1105 kfree(firmware_version);
1107 kfree(ti_manuf_desc);
1111 /* Check version number of download with current
1113 download_cur_ver = (firmware_version->Ver_Major << 8) +
1114 (firmware_version->Ver_Minor);
1115 download_new_ver = (fw_hdr->major_version << 8) +
1116 (fw_hdr->minor_version);
1118 dev_dbg(dev, "%s - >> FW Versions Device %d.%d Driver %d.%d\n",
1119 __func__, firmware_version->Ver_Major,
1120 firmware_version->Ver_Minor,
1121 fw_hdr->major_version, fw_hdr->minor_version);
1123 /* Check if we have an old version in the I2C and
1124 update if necessary */
1125 if (download_cur_ver < download_new_ver) {
1126 dev_dbg(dev, "%s - Update I2C dld from %d.%d to %d.%d\n",
1128 firmware_version->Ver_Major,
1129 firmware_version->Ver_Minor,
1130 fw_hdr->major_version,
1131 fw_hdr->minor_version);
1133 record = kmalloc(1, GFP_KERNEL);
1135 kfree(firmware_version);
1137 kfree(ti_manuf_desc);
1140 /* In order to update the I2C firmware we must
1141 * change the type 2 record to type 0xF2. This
1142 * will force the UMP to come up in Boot Mode.
1143 * Then while in boot mode, the driver will
1144 * download the latest firmware (padded to
1145 * 15.5k) into the UMP ram. Finally when the
1146 * device comes back up in download mode the
1147 * driver will cause the new firmware to be
1148 * copied from the UMP Ram to I2C and the
1149 * firmware will update the record type from
1152 *record = I2C_DESC_TYPE_FIRMWARE_BLANK;
1154 /* Change the I2C Firmware record type to
1155 0xf2 to trigger an update */
1156 status = write_rom(serial, start_address,
1157 sizeof(*record), record);
1160 kfree(firmware_version);
1162 kfree(ti_manuf_desc);
1166 /* verify the write -- must do this in order
1167 * for write to complete before we do the
1170 status = read_rom(serial,
1176 kfree(firmware_version);
1178 kfree(ti_manuf_desc);
1182 if (*record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
1183 dev_err(dev, "%s - error resetting device\n", __func__);
1185 kfree(firmware_version);
1187 kfree(ti_manuf_desc);
1191 dev_dbg(dev, "%s - HARDWARE RESET\n", __func__);
1193 /* Reset UMP -- Back to BOOT MODE */
1194 status = ti_vsend_sync(serial->serial->dev,
1195 UMPC_HARDWARE_RESET,
1197 TI_VSEND_TIMEOUT_DEFAULT);
1199 dev_dbg(dev, "%s - HARDWARE RESET return %d\n", __func__, status);
1201 /* return an error on purpose. */
1203 kfree(firmware_version);
1205 kfree(ti_manuf_desc);
1208 /* Same or newer fw version is already loaded */
1209 serial->fw_version = download_cur_ver;
1211 kfree(firmware_version);
1213 /* Search for type 0xF2 record (firmware blank record) */
1214 else if ((start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
1215 #define HEADER_SIZE (sizeof(struct ti_i2c_desc) + \
1216 sizeof(struct ti_i2c_firmware_rec))
1220 header = kmalloc(HEADER_SIZE, GFP_KERNEL);
1223 kfree(ti_manuf_desc);
1227 vheader = kmalloc(HEADER_SIZE, GFP_KERNEL);
1231 kfree(ti_manuf_desc);
1235 dev_dbg(dev, "%s - Found Type BLANK FIRMWARE (Type F2) record\n", __func__);
1238 * In order to update the I2C firmware we must change
1239 * the type 2 record to type 0xF2. This will force the
1240 * UMP to come up in Boot Mode. Then while in boot
1241 * mode, the driver will download the latest firmware
1242 * (padded to 15.5k) into the UMP ram. Finally when the
1243 * device comes back up in download mode the driver
1244 * will cause the new firmware to be copied from the
1245 * UMP Ram to I2C and the firmware will update the
1246 * record type from 0xf2 to 0x02.
1248 status = build_i2c_fw_hdr(header, dev, fw);
1253 kfree(ti_manuf_desc);
1257 /* Update I2C with type 0xf2 record with correct
1258 size and checksum */
1259 status = write_rom(serial,
1267 kfree(ti_manuf_desc);
1271 /* verify the write -- must do this in order for
1272 write to complete before we do the hardware reset */
1273 status = read_rom(serial, start_address,
1274 HEADER_SIZE, vheader);
1277 dev_dbg(dev, "%s - can't read header back\n", __func__);
1281 kfree(ti_manuf_desc);
1284 if (memcmp(vheader, header, HEADER_SIZE)) {
1285 dev_dbg(dev, "%s - write download record failed\n", __func__);
1289 kfree(ti_manuf_desc);
1296 dev_dbg(dev, "%s - Start firmware update\n", __func__);
1298 /* Tell firmware to copy download image into I2C */
1299 status = ti_vsend_sync(serial->serial->dev,
1300 UMPC_COPY_DNLD_TO_I2C,
1302 TI_VSEND_TIMEOUT_FW_DOWNLOAD);
1304 dev_dbg(dev, "%s - Update complete 0x%x\n", __func__, status);
1307 "%s - UMPC_COPY_DNLD_TO_I2C failed\n",
1310 kfree(ti_manuf_desc);
1315 // The device is running the download code
1317 kfree(ti_manuf_desc);
1321 /********************************************************************/
1323 /********************************************************************/
1324 dev_dbg(dev, "%s - RUNNING IN BOOT MODE\n", __func__);
1326 /* Configure the TI device so we can use the BULK pipes for download */
1327 status = config_boot_dev(serial->serial->dev);
1331 if (le16_to_cpu(serial->serial->dev->descriptor.idVendor)
1332 != USB_VENDOR_ID_ION) {
1333 dev_dbg(dev, "%s - VID = 0x%x\n", __func__,
1334 le16_to_cpu(serial->serial->dev->descriptor.idVendor));
1335 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1336 goto stayinbootmode;
1339 /* We have an ION device (I2c Must be programmed)
1340 Determine I2C image type */
1341 if (i2c_type_bootmode(serial))
1342 goto stayinbootmode;
1344 /* Check for ION Vendor ID and that the I2C is valid */
1345 if (!check_i2c_image(serial)) {
1346 struct ti_i2c_image_header *header;
1352 /* Validate Hardware version number
1353 * Read Manufacturing Descriptor from TI Based Edgeport
1355 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
1359 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
1361 kfree(ti_manuf_desc);
1362 goto stayinbootmode;
1365 /* Check for version 2 */
1366 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1367 dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n",
1368 __func__, ti_cpu_rev(ti_manuf_desc));
1369 kfree(ti_manuf_desc);
1370 goto stayinbootmode;
1373 kfree(ti_manuf_desc);
1376 * In order to update the I2C firmware we must change the type
1377 * 2 record to type 0xF2. This will force the UMP to come up
1378 * in Boot Mode. Then while in boot mode, the driver will
1379 * download the latest firmware (padded to 15.5k) into the
1380 * UMP ram. Finally when the device comes back up in download
1381 * mode the driver will cause the new firmware to be copied
1382 * from the UMP Ram to I2C and the firmware will update the
1383 * record type from 0xf2 to 0x02.
1385 * Do we really have to copy the whole firmware image,
1386 * or could we do this in place!
1389 /* Allocate a 15.5k buffer + 3 byte header */
1390 buffer_size = (((1024 * 16) - 512) +
1391 sizeof(struct ti_i2c_image_header));
1392 buffer = kmalloc(buffer_size, GFP_KERNEL);
1396 /* Initialize the buffer to 0xff (pad the buffer) */
1397 memset(buffer, 0xff, buffer_size);
1398 memcpy(buffer, &fw->data[4], fw->size - 4);
1400 for (i = sizeof(struct ti_i2c_image_header);
1401 i < buffer_size; i++) {
1402 cs = (__u8)(cs + buffer[i]);
1405 header = (struct ti_i2c_image_header *)buffer;
1407 /* update length and checksum after padding */
1408 header->Length = cpu_to_le16((__u16)(buffer_size -
1409 sizeof(struct ti_i2c_image_header)));
1410 header->CheckSum = cs;
1412 /* Download the operational code */
1413 dev_dbg(dev, "%s - Downloading operational code image version %d.%d (TI UMP)\n",
1415 fw_hdr->major_version, fw_hdr->minor_version);
1416 status = download_code(serial, buffer, buffer_size);
1421 dev_dbg(dev, "%s - Error downloading operational code image\n", __func__);
1425 /* Device will reboot */
1426 serial->product_info.TiMode = TI_MODE_TRANSITIONING;
1428 dev_dbg(dev, "%s - Download successful -- Device rebooting...\n", __func__);
1430 /* return an error on purpose */
1435 /* Eprom is invalid or blank stay in boot mode */
1436 dev_dbg(dev, "%s - STAYING IN BOOT MODE\n", __func__);
1437 serial->product_info.TiMode = TI_MODE_BOOT;
1443 static int ti_do_config(struct edgeport_port *port, int feature, int on)
1445 int port_number = port->port->port_number;
1447 on = !!on; /* 1 or 0 not bitmask */
1448 return send_cmd(port->port->serial->dev,
1449 feature, (__u8)(UMPM_UART1_PORT + port_number),
1454 static int restore_mcr(struct edgeport_port *port, __u8 mcr)
1458 dev_dbg(&port->port->dev, "%s - %x\n", __func__, mcr);
1460 status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR);
1463 status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS);
1466 return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK);
1469 /* Convert TI LSR to standard UART flags */
1470 static __u8 map_line_status(__u8 ti_lsr)
1474 #define MAP_FLAG(flagUmp, flagUart) \
1475 if (ti_lsr & flagUmp) \
1478 MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR) /* overrun */
1479 MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */
1480 MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */
1481 MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */
1482 MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* rx data available */
1483 MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* tx hold reg empty */
1490 static void handle_new_msr(struct edgeport_port *edge_port, __u8 msr)
1492 struct async_icount *icount;
1493 struct tty_struct *tty;
1495 dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, msr);
1497 if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
1498 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
1499 icount = &edge_port->port->icount;
1501 /* update input line counters */
1502 if (msr & EDGEPORT_MSR_DELTA_CTS)
1504 if (msr & EDGEPORT_MSR_DELTA_DSR)
1506 if (msr & EDGEPORT_MSR_DELTA_CD)
1508 if (msr & EDGEPORT_MSR_DELTA_RI)
1510 wake_up_interruptible(&edge_port->port->port.delta_msr_wait);
1513 /* Save the new modem status */
1514 edge_port->shadow_msr = msr & 0xf0;
1516 tty = tty_port_tty_get(&edge_port->port->port);
1517 /* handle CTS flow control */
1518 if (tty && C_CRTSCTS(tty)) {
1519 if (msr & EDGEPORT_MSR_CTS)
1525 static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data,
1526 __u8 lsr, __u8 data)
1528 struct async_icount *icount;
1529 __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR |
1530 LSR_FRM_ERR | LSR_BREAK));
1532 dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, new_lsr);
1534 edge_port->shadow_lsr = lsr;
1536 if (new_lsr & LSR_BREAK)
1538 * Parity and Framing errors only count if they
1539 * occur exclusive of a break being received.
1541 new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
1543 /* Place LSR data byte into Rx buffer */
1545 edge_tty_recv(edge_port->port, &data, 1);
1547 /* update input line counters */
1548 icount = &edge_port->port->icount;
1549 if (new_lsr & LSR_BREAK)
1551 if (new_lsr & LSR_OVER_ERR)
1553 if (new_lsr & LSR_PAR_ERR)
1555 if (new_lsr & LSR_FRM_ERR)
1560 static void edge_interrupt_callback(struct urb *urb)
1562 struct edgeport_serial *edge_serial = urb->context;
1563 struct usb_serial_port *port;
1564 struct edgeport_port *edge_port;
1566 unsigned char *data = urb->transfer_buffer;
1567 int length = urb->actual_length;
1573 int status = urb->status;
1582 /* this urb is terminated, clean up */
1583 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
1587 dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
1588 "%d\n", __func__, status);
1593 dev_dbg(&urb->dev->dev, "%s - no data in urb\n", __func__);
1597 dev = &edge_serial->serial->dev->dev;
1598 usb_serial_debug_data(dev, __func__, length, data);
1601 dev_dbg(dev, "%s - expecting packet of size 2, got %d\n", __func__, length);
1605 port_number = TIUMP_GET_PORT_FROM_CODE(data[0]);
1606 function = TIUMP_GET_FUNC_FROM_CODE(data[0]);
1607 dev_dbg(dev, "%s - port_number %d, function %d, info 0x%x\n", __func__,
1608 port_number, function, data[1]);
1609 port = edge_serial->serial->port[port_number];
1610 edge_port = usb_get_serial_port_data(port);
1612 dev_dbg(dev, "%s - edge_port not found\n", __func__);
1616 case TIUMP_INTERRUPT_CODE_LSR:
1617 lsr = map_line_status(data[1]);
1618 if (lsr & UMP_UART_LSR_DATA_MASK) {
1619 /* Save the LSR event for bulk read
1620 completion routine */
1621 dev_dbg(dev, "%s - LSR Event Port %u LSR Status = %02x\n",
1622 __func__, port_number, lsr);
1623 edge_port->lsr_event = 1;
1624 edge_port->lsr_mask = lsr;
1626 dev_dbg(dev, "%s - ===== Port %d LSR Status = %02x ======\n",
1627 __func__, port_number, lsr);
1628 handle_new_lsr(edge_port, 0, lsr, 0);
1632 case TIUMP_INTERRUPT_CODE_MSR: /* MSR */
1633 /* Copy MSR from UMP */
1635 dev_dbg(dev, "%s - ===== Port %u MSR Status = %02x ======\n",
1636 __func__, port_number, msr);
1637 handle_new_msr(edge_port, msr);
1641 dev_err(&urb->dev->dev,
1642 "%s - Unknown Interrupt code from UMP %x\n",
1649 retval = usb_submit_urb(urb, GFP_ATOMIC);
1651 dev_err(&urb->dev->dev,
1652 "%s - usb_submit_urb failed with result %d\n",
1656 static void edge_bulk_in_callback(struct urb *urb)
1658 struct edgeport_port *edge_port = urb->context;
1659 struct device *dev = &edge_port->port->dev;
1660 unsigned char *data = urb->transfer_buffer;
1663 int status = urb->status;
1672 /* this urb is terminated, clean up */
1673 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
1676 dev_err(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n", __func__, status);
1679 if (status == -EPIPE)
1683 dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__);
1687 port_number = edge_port->port->port_number;
1689 if (edge_port->lsr_event) {
1690 edge_port->lsr_event = 0;
1691 dev_dbg(dev, "%s ===== Port %u LSR Status = %02x, Data = %02x ======\n",
1692 __func__, port_number, edge_port->lsr_mask, *data);
1693 handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data);
1694 /* Adjust buffer length/pointer */
1695 --urb->actual_length;
1699 if (urb->actual_length) {
1700 usb_serial_debug_data(dev, __func__, urb->actual_length, data);
1701 if (edge_port->close_pending)
1702 dev_dbg(dev, "%s - close pending, dropping data on the floor\n",
1705 edge_tty_recv(edge_port->port, data,
1706 urb->actual_length);
1707 edge_port->port->icount.rx += urb->actual_length;
1711 /* continue read unless stopped */
1712 spin_lock(&edge_port->ep_lock);
1713 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
1714 retval = usb_submit_urb(urb, GFP_ATOMIC);
1715 else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING)
1716 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
1718 spin_unlock(&edge_port->ep_lock);
1720 dev_err(dev, "%s - usb_submit_urb failed with result %d\n", __func__, retval);
1723 static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
1728 queued = tty_insert_flip_string(&port->port, data, length);
1729 if (queued < length)
1730 dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
1731 __func__, length - queued);
1732 tty_flip_buffer_push(&port->port);
1735 static void edge_bulk_out_callback(struct urb *urb)
1737 struct usb_serial_port *port = urb->context;
1738 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1739 int status = urb->status;
1740 struct tty_struct *tty;
1742 edge_port->ep_write_urb_in_use = 0;
1751 /* this urb is terminated, clean up */
1752 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
1756 dev_err_console(port, "%s - nonzero write bulk status "
1757 "received: %d\n", __func__, status);
1760 /* send any buffered data */
1761 tty = tty_port_tty_get(&port->port);
1762 edge_send(port, tty);
1766 static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
1768 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1769 struct edgeport_serial *edge_serial;
1770 struct usb_device *dev;
1775 u8 transaction_timeout;
1777 if (edge_port == NULL)
1780 port_number = port->port_number;
1782 dev = port->serial->dev;
1784 /* turn off loopback */
1785 status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0);
1788 "%s - cannot send clear loopback command, %d\n",
1793 /* set up the port settings */
1795 edge_set_termios(tty, port, &tty->termios);
1797 /* open up the port */
1799 /* milliseconds to timeout for DMA transfer */
1800 transaction_timeout = 2;
1802 edge_port->ump_read_timeout =
1803 max(20, ((transaction_timeout * 3) / 2));
1805 /* milliseconds to timeout for DMA transfer */
1806 open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
1807 UMP_PIPE_TRANS_TIMEOUT_ENA |
1808 (transaction_timeout << 2));
1810 dev_dbg(&port->dev, "%s - Sending UMPC_OPEN_PORT\n", __func__);
1812 /* Tell TI to open and start the port */
1813 status = send_cmd(dev, UMPC_OPEN_PORT,
1814 (u8)(UMPM_UART1_PORT + port_number), open_settings, NULL, 0);
1816 dev_err(&port->dev, "%s - cannot send open command, %d\n",
1821 /* Start the DMA? */
1822 status = send_cmd(dev, UMPC_START_PORT,
1823 (u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
1825 dev_err(&port->dev, "%s - cannot send start DMA command, %d\n",
1830 /* Clear TX and RX buffers in UMP */
1831 status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
1834 "%s - cannot send clear buffers command, %d\n",
1839 /* Read Initial MSR */
1840 status = ti_vread_sync(dev, UMPC_READ_MSR, 0,
1841 (__u16)(UMPM_UART1_PORT + port_number),
1842 &edge_port->shadow_msr, 1);
1844 dev_err(&port->dev, "%s - cannot send read MSR command, %d\n",
1849 dev_dbg(&port->dev, "ShadowMSR 0x%X\n", edge_port->shadow_msr);
1851 /* Set Initial MCR */
1852 edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
1853 dev_dbg(&port->dev, "ShadowMCR 0x%X\n", edge_port->shadow_mcr);
1855 edge_serial = edge_port->edge_serial;
1856 if (mutex_lock_interruptible(&edge_serial->es_lock))
1857 return -ERESTARTSYS;
1858 if (edge_serial->num_ports_open == 0) {
1859 /* we are the first port to open, post the interrupt urb */
1860 urb = edge_serial->serial->port[0]->interrupt_in_urb;
1863 "%s - no interrupt urb present, exiting\n",
1866 goto release_es_lock;
1868 urb->context = edge_serial;
1869 status = usb_submit_urb(urb, GFP_KERNEL);
1872 "%s - usb_submit_urb failed with value %d\n",
1874 goto release_es_lock;
1879 * reset the data toggle on the bulk endpoints to work around bug in
1880 * host controllers where things get out of sync some times
1882 usb_clear_halt(dev, port->write_urb->pipe);
1883 usb_clear_halt(dev, port->read_urb->pipe);
1885 /* start up our bulk read urb */
1886 urb = port->read_urb;
1888 dev_err(&port->dev, "%s - no read urb present, exiting\n",
1891 goto unlink_int_urb;
1893 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
1894 urb->context = edge_port;
1895 status = usb_submit_urb(urb, GFP_KERNEL);
1898 "%s - read bulk usb_submit_urb failed with value %d\n",
1900 goto unlink_int_urb;
1903 ++edge_serial->num_ports_open;
1905 goto release_es_lock;
1908 if (edge_port->edge_serial->num_ports_open == 0)
1909 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
1911 mutex_unlock(&edge_serial->es_lock);
1915 static void edge_close(struct usb_serial_port *port)
1917 struct edgeport_serial *edge_serial;
1918 struct edgeport_port *edge_port;
1919 struct usb_serial *serial = port->serial;
1920 unsigned long flags;
1923 edge_serial = usb_get_serial_data(port->serial);
1924 edge_port = usb_get_serial_port_data(port);
1925 if (edge_serial == NULL || edge_port == NULL)
1928 /* The bulkreadcompletion routine will check
1929 * this flag and dump add read data */
1930 edge_port->close_pending = 1;
1932 usb_kill_urb(port->read_urb);
1933 usb_kill_urb(port->write_urb);
1934 edge_port->ep_write_urb_in_use = 0;
1935 spin_lock_irqsave(&edge_port->ep_lock, flags);
1936 kfifo_reset_out(&port->write_fifo);
1937 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1939 dev_dbg(&port->dev, "%s - send umpc_close_port\n", __func__);
1940 port_number = port->port_number;
1941 send_cmd(serial->dev, UMPC_CLOSE_PORT,
1942 (__u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
1944 mutex_lock(&edge_serial->es_lock);
1945 --edge_port->edge_serial->num_ports_open;
1946 if (edge_port->edge_serial->num_ports_open <= 0) {
1947 /* last port is now closed, let's shut down our interrupt urb */
1948 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
1949 edge_port->edge_serial->num_ports_open = 0;
1951 mutex_unlock(&edge_serial->es_lock);
1952 edge_port->close_pending = 0;
1955 static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
1956 const unsigned char *data, int count)
1958 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1961 dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
1965 if (edge_port == NULL)
1967 if (edge_port->close_pending == 1)
1970 count = kfifo_in_locked(&port->write_fifo, data, count,
1971 &edge_port->ep_lock);
1972 edge_send(port, tty);
1977 static void edge_send(struct usb_serial_port *port, struct tty_struct *tty)
1980 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1981 unsigned long flags;
1983 spin_lock_irqsave(&edge_port->ep_lock, flags);
1985 if (edge_port->ep_write_urb_in_use) {
1986 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1990 count = kfifo_out(&port->write_fifo,
1991 port->write_urb->transfer_buffer,
1992 port->bulk_out_size);
1995 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1999 edge_port->ep_write_urb_in_use = 1;
2001 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2003 usb_serial_debug_data(&port->dev, __func__, count, port->write_urb->transfer_buffer);
2005 /* set up our urb */
2006 port->write_urb->transfer_buffer_length = count;
2008 /* send the data out the bulk port */
2009 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2011 dev_err_console(port,
2012 "%s - failed submitting write urb, error %d\n",
2014 edge_port->ep_write_urb_in_use = 0;
2015 /* TODO: reschedule edge_send */
2017 edge_port->port->icount.tx += count;
2019 /* wakeup any process waiting for writes to complete */
2020 /* there is now more room in the buffer for new writes */
2025 static int edge_write_room(struct tty_struct *tty)
2027 struct usb_serial_port *port = tty->driver_data;
2028 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2030 unsigned long flags;
2032 if (edge_port == NULL)
2034 if (edge_port->close_pending == 1)
2037 spin_lock_irqsave(&edge_port->ep_lock, flags);
2038 room = kfifo_avail(&port->write_fifo);
2039 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2041 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
2045 static int edge_chars_in_buffer(struct tty_struct *tty)
2047 struct usb_serial_port *port = tty->driver_data;
2048 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2050 unsigned long flags;
2051 if (edge_port == NULL)
2054 spin_lock_irqsave(&edge_port->ep_lock, flags);
2055 chars = kfifo_len(&port->write_fifo);
2056 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2058 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
2062 static bool edge_tx_empty(struct usb_serial_port *port)
2064 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2067 ret = tx_active(edge_port);
2074 static void edge_throttle(struct tty_struct *tty)
2076 struct usb_serial_port *port = tty->driver_data;
2077 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2080 if (edge_port == NULL)
2083 /* if we are implementing XON/XOFF, send the stop character */
2085 unsigned char stop_char = STOP_CHAR(tty);
2086 status = edge_write(tty, port, &stop_char, 1);
2088 dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status);
2092 /* if we are implementing RTS/CTS, stop reads */
2093 /* and the Edgeport will clear the RTS line */
2095 stop_read(edge_port);
2099 static void edge_unthrottle(struct tty_struct *tty)
2101 struct usb_serial_port *port = tty->driver_data;
2102 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2105 if (edge_port == NULL)
2108 /* if we are implementing XON/XOFF, send the start character */
2110 unsigned char start_char = START_CHAR(tty);
2111 status = edge_write(tty, port, &start_char, 1);
2113 dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status);
2116 /* if we are implementing RTS/CTS, restart reads */
2117 /* are the Edgeport will assert the RTS line */
2118 if (C_CRTSCTS(tty)) {
2119 status = restart_read(edge_port);
2122 "%s - read bulk usb_submit_urb failed: %d\n",
2128 static void stop_read(struct edgeport_port *edge_port)
2130 unsigned long flags;
2132 spin_lock_irqsave(&edge_port->ep_lock, flags);
2134 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
2135 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
2136 edge_port->shadow_mcr &= ~MCR_RTS;
2138 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2141 static int restart_read(struct edgeport_port *edge_port)
2145 unsigned long flags;
2147 spin_lock_irqsave(&edge_port->ep_lock, flags);
2149 if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
2150 urb = edge_port->port->read_urb;
2151 status = usb_submit_urb(urb, GFP_ATOMIC);
2153 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2154 edge_port->shadow_mcr |= MCR_RTS;
2156 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2161 static void change_port_settings(struct tty_struct *tty,
2162 struct edgeport_port *edge_port, struct ktermios *old_termios)
2164 struct device *dev = &edge_port->port->dev;
2165 struct ump_uart_config *config;
2169 int port_number = edge_port->port->port_number;
2171 config = kmalloc (sizeof (*config), GFP_KERNEL);
2173 tty->termios = *old_termios;
2177 cflag = tty->termios.c_cflag;
2181 /* These flags must be set */
2182 config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
2183 config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
2184 config->bUartMode = (__u8)(edge_port->bUartMode);
2186 switch (cflag & CSIZE) {
2188 config->bDataBits = UMP_UART_CHAR5BITS;
2189 dev_dbg(dev, "%s - data bits = 5\n", __func__);
2192 config->bDataBits = UMP_UART_CHAR6BITS;
2193 dev_dbg(dev, "%s - data bits = 6\n", __func__);
2196 config->bDataBits = UMP_UART_CHAR7BITS;
2197 dev_dbg(dev, "%s - data bits = 7\n", __func__);
2201 config->bDataBits = UMP_UART_CHAR8BITS;
2202 dev_dbg(dev, "%s - data bits = 8\n", __func__);
2206 if (cflag & PARENB) {
2207 if (cflag & PARODD) {
2208 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2209 config->bParity = UMP_UART_ODDPARITY;
2210 dev_dbg(dev, "%s - parity = odd\n", __func__);
2212 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2213 config->bParity = UMP_UART_EVENPARITY;
2214 dev_dbg(dev, "%s - parity = even\n", __func__);
2217 config->bParity = UMP_UART_NOPARITY;
2218 dev_dbg(dev, "%s - parity = none\n", __func__);
2221 if (cflag & CSTOPB) {
2222 config->bStopBits = UMP_UART_STOPBIT2;
2223 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
2225 config->bStopBits = UMP_UART_STOPBIT1;
2226 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
2229 /* figure out the flow control settings */
2230 if (cflag & CRTSCTS) {
2231 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
2232 config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
2233 dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
2235 dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
2236 restart_read(edge_port);
2239 /* if we are implementing XON/XOFF, set the start and stop
2240 character in the device */
2241 config->cXon = START_CHAR(tty);
2242 config->cXoff = STOP_CHAR(tty);
2244 /* if we are implementing INBOUND XON/XOFF */
2246 config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
2247 dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2248 __func__, config->cXon, config->cXoff);
2250 dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
2252 /* if we are implementing OUTBOUND XON/XOFF */
2254 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
2255 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2256 __func__, config->cXon, config->cXoff);
2258 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
2260 tty->termios.c_cflag &= ~CMSPAR;
2262 /* Round the baud rate */
2263 baud = tty_get_baud_rate(tty);
2265 /* pick a default, any default... */
2268 tty_encode_baud_rate(tty, baud, baud);
2270 edge_port->baud_rate = baud;
2271 config->wBaudRate = (__u16)((461550L + baud/2) / baud);
2273 /* FIXME: Recompute actual baud from divisor here */
2275 dev_dbg(dev, "%s - baud rate = %d, wBaudRate = %d\n", __func__, baud, config->wBaudRate);
2277 dev_dbg(dev, "wBaudRate: %d\n", (int)(461550L / config->wBaudRate));
2278 dev_dbg(dev, "wFlags: 0x%x\n", config->wFlags);
2279 dev_dbg(dev, "bDataBits: %d\n", config->bDataBits);
2280 dev_dbg(dev, "bParity: %d\n", config->bParity);
2281 dev_dbg(dev, "bStopBits: %d\n", config->bStopBits);
2282 dev_dbg(dev, "cXon: %d\n", config->cXon);
2283 dev_dbg(dev, "cXoff: %d\n", config->cXoff);
2284 dev_dbg(dev, "bUartMode: %d\n", config->bUartMode);
2286 /* move the word values into big endian mode */
2287 cpu_to_be16s(&config->wFlags);
2288 cpu_to_be16s(&config->wBaudRate);
2290 status = send_cmd(edge_port->port->serial->dev, UMPC_SET_CONFIG,
2291 (__u8)(UMPM_UART1_PORT + port_number),
2292 0, (__u8 *)config, sizeof(*config));
2294 dev_dbg(dev, "%s - error %d when trying to write config to device\n",
2299 static void edge_set_termios(struct tty_struct *tty,
2300 struct usb_serial_port *port, struct ktermios *old_termios)
2302 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2305 cflag = tty->termios.c_cflag;
2307 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
2308 tty->termios.c_cflag, tty->termios.c_iflag);
2309 dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
2310 old_termios->c_cflag, old_termios->c_iflag);
2312 if (edge_port == NULL)
2314 /* change the port settings to the new ones specified */
2315 change_port_settings(tty, edge_port, old_termios);
2318 static int edge_tiocmset(struct tty_struct *tty,
2319 unsigned int set, unsigned int clear)
2321 struct usb_serial_port *port = tty->driver_data;
2322 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2324 unsigned long flags;
2326 spin_lock_irqsave(&edge_port->ep_lock, flags);
2327 mcr = edge_port->shadow_mcr;
2328 if (set & TIOCM_RTS)
2330 if (set & TIOCM_DTR)
2332 if (set & TIOCM_LOOP)
2333 mcr |= MCR_LOOPBACK;
2335 if (clear & TIOCM_RTS)
2337 if (clear & TIOCM_DTR)
2339 if (clear & TIOCM_LOOP)
2340 mcr &= ~MCR_LOOPBACK;
2342 edge_port->shadow_mcr = mcr;
2343 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2345 restore_mcr(edge_port, mcr);
2349 static int edge_tiocmget(struct tty_struct *tty)
2351 struct usb_serial_port *port = tty->driver_data;
2352 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2353 unsigned int result = 0;
2356 unsigned long flags;
2358 spin_lock_irqsave(&edge_port->ep_lock, flags);
2360 msr = edge_port->shadow_msr;
2361 mcr = edge_port->shadow_mcr;
2362 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
2363 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
2364 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
2365 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
2366 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
2367 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
2370 dev_dbg(&port->dev, "%s -- %x\n", __func__, result);
2371 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2376 static int get_serial_info(struct edgeport_port *edge_port,
2377 struct serial_struct __user *retinfo)
2379 struct serial_struct tmp;
2385 cwait = edge_port->port->port.closing_wait;
2386 if (cwait != ASYNC_CLOSING_WAIT_NONE)
2387 cwait = jiffies_to_msecs(cwait) / 10;
2389 memset(&tmp, 0, sizeof(tmp));
2391 tmp.type = PORT_16550A;
2392 tmp.line = edge_port->port->minor;
2393 tmp.port = edge_port->port->port_number;
2395 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2396 tmp.xmit_fifo_size = edge_port->port->bulk_out_size;
2397 tmp.baud_base = 9600;
2398 tmp.close_delay = 5*HZ;
2399 tmp.closing_wait = cwait;
2401 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2406 static int edge_ioctl(struct tty_struct *tty,
2407 unsigned int cmd, unsigned long arg)
2409 struct usb_serial_port *port = tty->driver_data;
2410 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2414 dev_dbg(&port->dev, "%s - TIOCGSERIAL\n", __func__);
2415 return get_serial_info(edge_port,
2416 (struct serial_struct __user *) arg);
2418 return -ENOIOCTLCMD;
2421 static void edge_break(struct tty_struct *tty, int break_state)
2423 struct usb_serial_port *port = tty->driver_data;
2424 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2426 int bv = 0; /* Off */
2428 if (break_state == -1)
2430 status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv);
2432 dev_dbg(&port->dev, "%s - error %d sending break set/clear command.\n",
2436 static void edge_heartbeat_schedule(struct edgeport_serial *edge_serial)
2438 if (!edge_serial->use_heartbeat)
2441 schedule_delayed_work(&edge_serial->heartbeat_work,
2442 FW_HEARTBEAT_SECS * HZ);
2445 static void edge_heartbeat_work(struct work_struct *work)
2447 struct edgeport_serial *serial;
2448 struct ti_i2c_desc *rom_desc;
2450 serial = container_of(work, struct edgeport_serial,
2451 heartbeat_work.work);
2453 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
2455 /* Descriptor address request is enough to reset the firmware timer */
2456 if (!rom_desc || !get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
2458 dev_err(&serial->serial->interface->dev,
2459 "%s - Incomplete heartbeat\n", __func__);
2463 edge_heartbeat_schedule(serial);
2466 static int edge_startup(struct usb_serial *serial)
2468 struct edgeport_serial *edge_serial;
2470 const struct firmware *fw;
2471 const char *fw_name = "edgeport/down3.bin";
2472 struct device *dev = &serial->interface->dev;
2475 /* create our private serial structure */
2476 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
2480 mutex_init(&edge_serial->es_lock);
2481 edge_serial->serial = serial;
2482 usb_set_serial_data(serial, edge_serial);
2484 status = request_firmware(&fw, fw_name, dev);
2486 dev_err(dev, "Failed to load image \"%s\" err %d\n",
2492 status = download_fw(edge_serial, fw);
2493 release_firmware(fw);
2499 product_id = le16_to_cpu(
2500 edge_serial->serial->dev->descriptor.idProduct);
2502 /* Currently only the EP/416 models require heartbeat support */
2503 if (edge_serial->fw_version > FW_HEARTBEAT_VERSION_CUTOFF) {
2504 if (product_id == ION_DEVICE_ID_TI_EDGEPORT_416 ||
2505 product_id == ION_DEVICE_ID_TI_EDGEPORT_416B) {
2506 edge_serial->use_heartbeat = true;
2510 INIT_DELAYED_WORK(&edge_serial->heartbeat_work, edge_heartbeat_work);
2511 edge_heartbeat_schedule(edge_serial);
2516 static void edge_disconnect(struct usb_serial *serial)
2520 static void edge_release(struct usb_serial *serial)
2522 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
2524 cancel_delayed_work_sync(&edge_serial->heartbeat_work);
2528 static int edge_port_probe(struct usb_serial_port *port)
2530 struct edgeport_port *edge_port;
2533 edge_port = kzalloc(sizeof(*edge_port), GFP_KERNEL);
2537 spin_lock_init(&edge_port->ep_lock);
2538 edge_port->port = port;
2539 edge_port->edge_serial = usb_get_serial_data(port->serial);
2540 edge_port->bUartMode = default_uart_mode;
2542 switch (port->port_number) {
2544 edge_port->uart_base = UMPMEM_BASE_UART1;
2545 edge_port->dma_address = UMPD_OEDB1_ADDRESS;
2548 edge_port->uart_base = UMPMEM_BASE_UART2;
2549 edge_port->dma_address = UMPD_OEDB2_ADDRESS;
2552 dev_err(&port->dev, "unknown port number\n");
2558 "%s - port_number = %d, uart_base = %04x, dma_address = %04x\n",
2559 __func__, port->port_number, edge_port->uart_base,
2560 edge_port->dma_address);
2562 usb_set_serial_port_data(port, edge_port);
2564 ret = edge_create_sysfs_attrs(port);
2568 port->port.closing_wait = msecs_to_jiffies(closing_wait * 10);
2569 port->port.drain_delay = 1;
2578 static int edge_port_remove(struct usb_serial_port *port)
2580 struct edgeport_port *edge_port;
2582 edge_port = usb_get_serial_port_data(port);
2583 edge_remove_sysfs_attrs(port);
2589 /* Sysfs Attributes */
2591 static ssize_t uart_mode_show(struct device *dev,
2592 struct device_attribute *attr, char *buf)
2594 struct usb_serial_port *port = to_usb_serial_port(dev);
2595 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2597 return sprintf(buf, "%d\n", edge_port->bUartMode);
2600 static ssize_t uart_mode_store(struct device *dev,
2601 struct device_attribute *attr, const char *valbuf, size_t count)
2603 struct usb_serial_port *port = to_usb_serial_port(dev);
2604 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2605 unsigned int v = simple_strtoul(valbuf, NULL, 0);
2607 dev_dbg(dev, "%s: setting uart_mode = %d\n", __func__, v);
2610 edge_port->bUartMode = v;
2612 dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
2616 static DEVICE_ATTR_RW(uart_mode);
2618 static int edge_create_sysfs_attrs(struct usb_serial_port *port)
2620 return device_create_file(&port->dev, &dev_attr_uart_mode);
2623 static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
2625 device_remove_file(&port->dev, &dev_attr_uart_mode);
2630 static int edge_suspend(struct usb_serial *serial, pm_message_t message)
2632 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
2634 cancel_delayed_work_sync(&edge_serial->heartbeat_work);
2639 static int edge_resume(struct usb_serial *serial)
2641 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
2643 edge_heartbeat_schedule(edge_serial);
2649 static struct usb_serial_driver edgeport_1port_device = {
2651 .owner = THIS_MODULE,
2652 .name = "edgeport_ti_1",
2654 .description = "Edgeport TI 1 port adapter",
2655 .id_table = edgeport_1port_id_table,
2658 .close = edge_close,
2659 .throttle = edge_throttle,
2660 .unthrottle = edge_unthrottle,
2661 .attach = edge_startup,
2662 .disconnect = edge_disconnect,
2663 .release = edge_release,
2664 .port_probe = edge_port_probe,
2665 .port_remove = edge_port_remove,
2666 .ioctl = edge_ioctl,
2667 .set_termios = edge_set_termios,
2668 .tiocmget = edge_tiocmget,
2669 .tiocmset = edge_tiocmset,
2670 .tiocmiwait = usb_serial_generic_tiocmiwait,
2671 .get_icount = usb_serial_generic_get_icount,
2672 .write = edge_write,
2673 .write_room = edge_write_room,
2674 .chars_in_buffer = edge_chars_in_buffer,
2675 .tx_empty = edge_tx_empty,
2676 .break_ctl = edge_break,
2677 .read_int_callback = edge_interrupt_callback,
2678 .read_bulk_callback = edge_bulk_in_callback,
2679 .write_bulk_callback = edge_bulk_out_callback,
2681 .suspend = edge_suspend,
2682 .resume = edge_resume,
2686 static struct usb_serial_driver edgeport_2port_device = {
2688 .owner = THIS_MODULE,
2689 .name = "edgeport_ti_2",
2691 .description = "Edgeport TI 2 port adapter",
2692 .id_table = edgeport_2port_id_table,
2695 .close = edge_close,
2696 .throttle = edge_throttle,
2697 .unthrottle = edge_unthrottle,
2698 .attach = edge_startup,
2699 .disconnect = edge_disconnect,
2700 .release = edge_release,
2701 .port_probe = edge_port_probe,
2702 .port_remove = edge_port_remove,
2703 .ioctl = edge_ioctl,
2704 .set_termios = edge_set_termios,
2705 .tiocmget = edge_tiocmget,
2706 .tiocmset = edge_tiocmset,
2707 .tiocmiwait = usb_serial_generic_tiocmiwait,
2708 .get_icount = usb_serial_generic_get_icount,
2709 .write = edge_write,
2710 .write_room = edge_write_room,
2711 .chars_in_buffer = edge_chars_in_buffer,
2712 .tx_empty = edge_tx_empty,
2713 .break_ctl = edge_break,
2714 .read_int_callback = edge_interrupt_callback,
2715 .read_bulk_callback = edge_bulk_in_callback,
2716 .write_bulk_callback = edge_bulk_out_callback,
2718 .suspend = edge_suspend,
2719 .resume = edge_resume,
2723 static struct usb_serial_driver * const serial_drivers[] = {
2724 &edgeport_1port_device, &edgeport_2port_device, NULL
2727 module_usb_serial_driver(serial_drivers, id_table_combined);
2729 MODULE_AUTHOR(DRIVER_AUTHOR);
2730 MODULE_DESCRIPTION(DRIVER_DESC);
2731 MODULE_LICENSE("GPL");
2732 MODULE_FIRMWARE("edgeport/down3.bin");
2734 module_param(closing_wait, int, S_IRUGO | S_IWUSR);
2735 MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
2737 module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
2738 MODULE_PARM_DESC(ignore_cpu_rev,
2739 "Ignore the cpu revision when connecting to a device");
2741 module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
2742 MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");