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/swab.h>
40 #include <linux/kfifo.h>
41 #include <linux/ioctl.h>
42 #include <linux/firmware.h>
43 #include <linux/uaccess.h>
44 #include <linux/usb.h>
45 #include <linux/usb/serial.h>
48 #include "io_usbvend.h"
54 #define DRIVER_VERSION "v0.7mode043006"
55 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
56 #define DRIVER_DESC "Edgeport USB Serial Driver"
58 #define EPROM_PAGE_SIZE 64
61 /* different hardware types */
62 #define HARDWARE_TYPE_930 0
63 #define HARDWARE_TYPE_TIUMP 1
65 /* IOCTL_PRIVATE_TI_GET_MODE Definitions */
66 #define TI_MODE_CONFIGURING 0 /* Device has not entered start device */
67 #define TI_MODE_BOOT 1 /* Staying in boot mode */
68 #define TI_MODE_DOWNLOAD 2 /* Made it to download mode */
69 #define TI_MODE_TRANSITIONING 3 /* Currently in boot mode but
70 transitioning to download mode */
73 #define EDGE_READ_URB_RUNNING 0
74 #define EDGE_READ_URB_STOPPING 1
75 #define EDGE_READ_URB_STOPPED 2
77 #define EDGE_CLOSING_WAIT 4000 /* in .01 sec */
79 #define EDGE_OUT_BUF_SIZE 1024
82 /* Product information read from the Edgeport */
84 int TiMode; /* Current TI Mode */
85 __u8 hardware_type; /* Type of hardware */
86 } __attribute__((packed));
88 struct edgeport_port {
95 __u32 ump_read_timeout; /* Number of milliseconds the UMP will
96 wait without data before completing
101 struct async_icount icount;
102 struct edgeport_serial *edge_serial;
103 struct usb_serial_port *port;
104 __u8 bUartMode; /* Port type, 0: RS232, etc. */
106 int ep_read_urb_state;
107 int ep_write_urb_in_use;
108 struct kfifo write_fifo;
111 struct edgeport_serial {
112 struct product_info product_info;
113 u8 TI_I2C_Type; /* Type of I2C in UMP */
114 u8 TiReadI2C; /* Set to TRUE if we have read the
116 struct mutex es_lock;
118 struct usb_serial *serial;
122 /* Devices that this driver supports */
123 static const struct usb_device_id edgeport_1port_id_table[] = {
124 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
125 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
126 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
127 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
128 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
129 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
130 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
131 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
132 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
133 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
134 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
135 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
136 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
137 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
138 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
139 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
143 static const struct usb_device_id edgeport_2port_id_table[] = {
144 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
145 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
146 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
147 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
148 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
149 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
150 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
151 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
152 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
153 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
154 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
155 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
156 /* The 4, 8 and 16 port devices show up as multiple 2 port devices */
157 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
158 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
159 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
160 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
161 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
165 /* Devices that this driver supports */
166 static const struct usb_device_id id_table_combined[] = {
167 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
168 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
169 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
170 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
171 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
172 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
173 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
174 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
175 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
176 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
177 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
178 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
179 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
180 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
181 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
182 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
183 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
184 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
185 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
186 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
187 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
188 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
189 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
190 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
191 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
192 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
193 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
194 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
195 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
196 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
197 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
198 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
199 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
203 MODULE_DEVICE_TABLE(usb, id_table_combined);
205 static struct usb_driver io_driver = {
207 .probe = usb_serial_probe,
208 .disconnect = usb_serial_disconnect,
209 .id_table = id_table_combined,
214 static unsigned char OperationalMajorVersion;
215 static unsigned char OperationalMinorVersion;
216 static unsigned short OperationalBuildNumber;
220 static int closing_wait = EDGE_CLOSING_WAIT;
221 static int ignore_cpu_rev;
222 static int default_uart_mode; /* RS232 */
224 static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
225 unsigned char *data, int length);
227 static void stop_read(struct edgeport_port *edge_port);
228 static int restart_read(struct edgeport_port *edge_port);
230 static void edge_set_termios(struct tty_struct *tty,
231 struct usb_serial_port *port, struct ktermios *old_termios);
232 static void edge_send(struct tty_struct *tty);
234 /* sysfs attributes */
235 static int edge_create_sysfs_attrs(struct usb_serial_port *port);
236 static int edge_remove_sysfs_attrs(struct usb_serial_port *port);
239 static int ti_vread_sync(struct usb_device *dev, __u8 request,
240 __u16 value, __u16 index, u8 *data, int size)
244 status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
245 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
246 value, index, data, size, 1000);
249 if (status != size) {
250 dbg("%s - wanted to write %d, but only wrote %d",
251 __func__, size, status);
257 static int ti_vsend_sync(struct usb_device *dev, __u8 request,
258 __u16 value, __u16 index, u8 *data, int size)
262 status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
263 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
264 value, index, data, size, 1000);
267 if (status != size) {
268 dbg("%s - wanted to write %d, but only wrote %d",
269 __func__, size, status);
275 static int send_cmd(struct usb_device *dev, __u8 command,
276 __u8 moduleid, __u16 value, u8 *data,
279 return ti_vsend_sync(dev, command, value, moduleid, data, size);
282 /* clear tx/rx buffers and fifo in TI UMP */
283 static int purge_port(struct usb_serial_port *port, __u16 mask)
285 int port_number = port->number - port->serial->minor;
287 dbg("%s - port %d, mask %x", __func__, port_number, mask);
289 return send_cmd(port->serial->dev,
291 (__u8)(UMPM_UART1_PORT + port_number),
298 * read_download_mem - Read edgeport memory from TI chip
299 * @dev: usb device pointer
300 * @start_address: Device CPU address at which to read
301 * @length: Length of above data
302 * @address_type: Can read both XDATA and I2C
303 * @buffer: pointer to input data buffer
305 static int read_download_mem(struct usb_device *dev, int start_address,
306 int length, __u8 address_type, __u8 *buffer)
310 u16 be_start_address;
312 dbg("%s - @ %x for %d", __func__, start_address, length);
314 /* Read in blocks of 64 bytes
315 * (TI firmware can't handle more than 64 byte reads)
321 read_length = (__u8)length;
323 if (read_length > 1) {
324 dbg("%s - @ %x for %d", __func__,
325 start_address, read_length);
328 * NOTE: Must use swab as wIndex is sent in little-endian
329 * byte order regardless of host byte order.
331 be_start_address = swab16((u16)start_address);
332 status = ti_vread_sync(dev, UMPC_MEMORY_READ,
335 buffer, read_length);
338 dbg("%s - ERROR %x", __func__, status);
343 usb_serial_debug_data(debug, &dev->dev, __func__,
344 read_length, buffer);
346 /* Update pointers/length */
347 start_address += read_length;
348 buffer += read_length;
349 length -= read_length;
355 static int read_ram(struct usb_device *dev, int start_address,
356 int length, __u8 *buffer)
358 return read_download_mem(dev, start_address, length,
359 DTK_ADDR_SPACE_XDATA, buffer);
362 /* Read edgeport memory to a given block */
363 static int read_boot_mem(struct edgeport_serial *serial,
364 int start_address, int length, __u8 *buffer)
369 for (i = 0; i < length; i++) {
370 status = ti_vread_sync(serial->serial->dev,
371 UMPC_MEMORY_READ, serial->TI_I2C_Type,
372 (__u16)(start_address+i), &buffer[i], 0x01);
374 dbg("%s - ERROR %x", __func__, status);
379 dbg("%s - start_address = %x, length = %d",
380 __func__, start_address, length);
381 usb_serial_debug_data(debug, &serial->serial->dev->dev,
382 __func__, length, buffer);
384 serial->TiReadI2C = 1;
389 /* Write given block to TI EPROM memory */
390 static int write_boot_mem(struct edgeport_serial *serial,
391 int start_address, int length, __u8 *buffer)
397 /* Must do a read before write */
398 if (!serial->TiReadI2C) {
399 temp = kmalloc(1, GFP_KERNEL);
401 dev_err(&serial->serial->dev->dev,
402 "%s - out of memory\n", __func__);
405 status = read_boot_mem(serial, 0, 1, temp);
411 for (i = 0; i < length; ++i) {
412 status = ti_vsend_sync(serial->serial->dev,
413 UMPC_MEMORY_WRITE, buffer[i],
414 (__u16)(i + start_address), NULL, 0);
419 dbg("%s - start_sddr = %x, length = %d",
420 __func__, start_address, length);
421 usb_serial_debug_data(debug, &serial->serial->dev->dev,
422 __func__, length, buffer);
428 /* Write edgeport I2C memory to TI chip */
429 static int write_i2c_mem(struct edgeport_serial *serial,
430 int start_address, int length, __u8 address_type, __u8 *buffer)
434 u16 be_start_address;
436 /* We can only send a maximum of 1 aligned byte page at a time */
438 /* calculate the number of bytes left in the first page */
439 write_length = EPROM_PAGE_SIZE -
440 (start_address & (EPROM_PAGE_SIZE - 1));
442 if (write_length > length)
443 write_length = length;
445 dbg("%s - BytesInFirstPage Addr = %x, length = %d",
446 __func__, start_address, write_length);
447 usb_serial_debug_data(debug, &serial->serial->dev->dev,
448 __func__, write_length, buffer);
453 * NOTE: Must use swab as wIndex is sent in little-endian byte order
454 * regardless of host byte order.
456 be_start_address = swab16((u16)start_address);
457 status = ti_vsend_sync(serial->serial->dev,
458 UMPC_MEMORY_WRITE, (__u16)address_type,
460 buffer, write_length);
462 dbg("%s - ERROR %d", __func__, status);
466 length -= write_length;
467 start_address += write_length;
468 buffer += write_length;
470 /* We should be aligned now -- can write
471 max page size bytes at a time */
473 if (length > EPROM_PAGE_SIZE)
474 write_length = EPROM_PAGE_SIZE;
476 write_length = length;
478 dbg("%s - Page Write Addr = %x, length = %d",
479 __func__, start_address, write_length);
480 usb_serial_debug_data(debug, &serial->serial->dev->dev,
481 __func__, write_length, buffer);
486 * NOTE: Must use swab as wIndex is sent in little-endian byte
487 * order regardless of host byte order.
489 be_start_address = swab16((u16)start_address);
490 status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
493 buffer, write_length);
495 dev_err(&serial->serial->dev->dev, "%s - ERROR %d\n",
500 length -= write_length;
501 start_address += write_length;
502 buffer += write_length;
507 /* Examine the UMP DMA registers and LSR
509 * Check the MSBit of the X and Y DMA byte count registers.
510 * A zero in this bit indicates that the TX DMA buffers are empty
511 * then check the TX Empty bit in the UART.
513 static int tx_active(struct edgeport_port *port)
516 struct out_endpoint_desc_block *oedb;
520 oedb = kmalloc(sizeof(*oedb), GFP_KERNEL);
522 dev_err(&port->port->dev, "%s - out of memory\n", __func__);
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 dbg("%s - XByteCount 0x%X", __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 dbg("%s - LSR = 0x%X", __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 dbg("%s - return %d", __func__, bytes_left);
565 static void chase_port(struct edgeport_port *port, unsigned long timeout,
569 struct tty_struct *tty = tty_port_tty_get(&port->port->port);
577 timeout = (HZ * EDGE_CLOSING_WAIT)/100;
579 /* wait for data to drain from the buffer */
580 spin_lock_irqsave(&port->ep_lock, flags);
581 init_waitqueue_entry(&wait, current);
582 add_wait_queue(&tty->write_wait, &wait);
584 set_current_state(TASK_INTERRUPTIBLE);
585 if (kfifo_len(&port->write_fifo) == 0
586 || timeout == 0 || signal_pending(current)
587 || !usb_get_intfdata(port->port->serial->interface))
590 spin_unlock_irqrestore(&port->ep_lock, flags);
591 timeout = schedule_timeout(timeout);
592 spin_lock_irqsave(&port->ep_lock, flags);
594 set_current_state(TASK_RUNNING);
595 remove_wait_queue(&tty->write_wait, &wait);
597 kfifo_reset_out(&port->write_fifo);
598 spin_unlock_irqrestore(&port->ep_lock, flags);
601 /* wait for data to drain from the device */
603 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
604 && usb_get_intfdata(port->port->serial->interface)) {
605 /* not disconnected */
606 if (!tx_active(port))
612 if (!usb_get_intfdata(port->port->serial->interface))
615 /* wait one more character time, based on baud rate */
616 /* (tx_active doesn't seem to wait for the last byte) */
617 baud_rate = port->baud_rate;
620 msleep(max(1, DIV_ROUND_UP(10000, baud_rate)));
623 static int choose_config(struct usb_device *dev)
626 * There may be multiple configurations on this device, in which case
627 * we would need to read and parse all of them to find out which one
628 * we want. However, we just support one config at this point,
629 * configuration # 1, which is Config Descriptor 0.
632 dbg("%s - Number of Interfaces = %d",
633 __func__, dev->config->desc.bNumInterfaces);
634 dbg("%s - MAX Power = %d",
635 __func__, dev->config->desc.bMaxPower * 2);
637 if (dev->config->desc.bNumInterfaces != 1) {
638 dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n",
646 static int read_rom(struct edgeport_serial *serial,
647 int start_address, int length, __u8 *buffer)
651 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
652 status = read_download_mem(serial->serial->dev,
658 status = read_boot_mem(serial, start_address, length,
664 static int write_rom(struct edgeport_serial *serial, int start_address,
665 int length, __u8 *buffer)
667 if (serial->product_info.TiMode == TI_MODE_BOOT)
668 return write_boot_mem(serial, start_address, length,
671 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
672 return write_i2c_mem(serial, start_address, length,
673 serial->TI_I2C_Type, buffer);
679 /* Read a descriptor header from I2C based on type */
680 static int get_descriptor_addr(struct edgeport_serial *serial,
681 int desc_type, struct ti_i2c_desc *rom_desc)
686 /* Search for requested descriptor in I2C */
689 status = read_rom(serial,
691 sizeof(struct ti_i2c_desc),
696 if (rom_desc->Type == desc_type)
697 return start_address;
699 start_address = start_address + sizeof(struct ti_i2c_desc) +
700 le16_to_cpu(rom_desc->Size);
702 } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
707 /* Validate descriptor checksum */
708 static int valid_csum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
713 for (i = 0; i < le16_to_cpu(rom_desc->Size); i++)
714 cs = (__u8)(cs + buffer[i]);
716 if (cs != rom_desc->CheckSum) {
717 dbg("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
723 /* Make sure that the I2C image is good */
724 static int check_i2c_image(struct edgeport_serial *serial)
726 struct device *dev = &serial->serial->dev->dev;
728 struct ti_i2c_desc *rom_desc;
729 int start_address = 2;
733 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
735 dev_err(dev, "%s - out of memory\n", __func__);
738 buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL);
740 dev_err(dev, "%s - out of memory when allocating buffer\n",
746 /* Read the first byte (Signature0) must be 0x52 or 0x10 */
747 status = read_rom(serial, 0, 1, buffer);
751 if (*buffer != UMP5152 && *buffer != UMP3410) {
752 dev_err(dev, "%s - invalid buffer signature\n", __func__);
758 /* Validate the I2C */
759 status = read_rom(serial,
761 sizeof(struct ti_i2c_desc),
766 if ((start_address + sizeof(struct ti_i2c_desc) +
767 le16_to_cpu(rom_desc->Size)) > TI_MAX_I2C_SIZE) {
769 dbg("%s - structure too big, erroring out.", __func__);
773 dbg("%s Type = 0x%x", __func__, rom_desc->Type);
775 /* Skip type 2 record */
776 ttype = rom_desc->Type & 0x0f;
777 if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
778 && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) {
779 /* Read the descriptor data */
780 status = read_rom(serial, start_address +
781 sizeof(struct ti_i2c_desc),
782 le16_to_cpu(rom_desc->Size),
787 status = valid_csum(rom_desc, buffer);
791 start_address = start_address + sizeof(struct ti_i2c_desc) +
792 le16_to_cpu(rom_desc->Size);
794 } while ((rom_desc->Type != I2C_DESC_TYPE_ION) &&
795 (start_address < TI_MAX_I2C_SIZE));
797 if ((rom_desc->Type != I2C_DESC_TYPE_ION) ||
798 (start_address > TI_MAX_I2C_SIZE))
807 static int get_manuf_info(struct edgeport_serial *serial, __u8 *buffer)
811 struct ti_i2c_desc *rom_desc;
812 struct edge_ti_manuf_descriptor *desc;
814 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
816 dev_err(&serial->serial->dev->dev, "%s - out of memory\n",
820 start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
823 if (!start_address) {
824 dbg("%s - Edge Descriptor not found in I2C", __func__);
829 /* Read the descriptor data */
830 status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc),
831 le16_to_cpu(rom_desc->Size), buffer);
835 status = valid_csum(rom_desc, buffer);
837 desc = (struct edge_ti_manuf_descriptor *)buffer;
838 dbg("%s - IonConfig 0x%x", __func__, desc->IonConfig);
839 dbg("%s - Version %d", __func__, desc->Version);
840 dbg("%s - Cpu/Board 0x%x", __func__, desc->CpuRev_BoardRev);
841 dbg("%s - NumPorts %d", __func__, desc->NumPorts);
842 dbg("%s - NumVirtualPorts %d", __func__, desc->NumVirtualPorts);
843 dbg("%s - TotalPorts %d", __func__, desc->TotalPorts);
850 /* Build firmware header used for firmware update */
851 static int build_i2c_fw_hdr(__u8 *header, struct device *dev)
858 struct ti_i2c_desc *i2c_header;
859 struct ti_i2c_image_header *img_header;
860 struct ti_i2c_firmware_rec *firmware_rec;
861 const struct firmware *fw;
862 const char *fw_name = "edgeport/down3.bin";
864 /* In order to update the I2C firmware we must change the type 2 record
865 * to type 0xF2. This will force the UMP to come up in Boot Mode.
866 * Then while in boot mode, the driver will download the latest
867 * firmware (padded to 15.5k) into the UMP ram. And finally when the
868 * device comes back up in download mode the driver will cause the new
869 * firmware to be copied from the UMP Ram to I2C and the firmware will
870 * update the record type from 0xf2 to 0x02.
873 /* Allocate a 15.5k buffer + 2 bytes for version number
874 * (Firmware Record) */
875 buffer_size = (((1024 * 16) - 512 ) +
876 sizeof(struct ti_i2c_firmware_rec));
878 buffer = kmalloc(buffer_size, GFP_KERNEL);
880 dev_err(dev, "%s - out of memory\n", __func__);
884 // Set entire image of 0xffs
885 memset(buffer, 0xff, buffer_size);
887 err = request_firmware(&fw, fw_name, dev);
889 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
895 /* Save Download Version Number */
896 OperationalMajorVersion = fw->data[0];
897 OperationalMinorVersion = fw->data[1];
898 OperationalBuildNumber = fw->data[2] | (fw->data[3] << 8);
900 /* Copy version number into firmware record */
901 firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
903 firmware_rec->Ver_Major = OperationalMajorVersion;
904 firmware_rec->Ver_Minor = OperationalMinorVersion;
906 /* Pointer to fw_down memory image */
907 img_header = (struct ti_i2c_image_header *)&fw->data[4];
909 memcpy(buffer + sizeof(struct ti_i2c_firmware_rec),
910 &fw->data[4 + sizeof(struct ti_i2c_image_header)],
911 le16_to_cpu(img_header->Length));
913 release_firmware(fw);
915 for (i=0; i < buffer_size; i++) {
916 cs = (__u8)(cs + buffer[i]);
921 /* Build new header */
922 i2c_header = (struct ti_i2c_desc *)header;
923 firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data;
925 i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK;
926 i2c_header->Size = cpu_to_le16(buffer_size);
927 i2c_header->CheckSum = cs;
928 firmware_rec->Ver_Major = OperationalMajorVersion;
929 firmware_rec->Ver_Minor = OperationalMinorVersion;
934 /* Try to figure out what type of I2c we have */
935 static int i2c_type_bootmode(struct edgeport_serial *serial)
940 data = kmalloc(1, GFP_KERNEL);
942 dev_err(&serial->serial->dev->dev,
943 "%s - out of memory\n", __func__);
947 /* Try to read type 2 */
948 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
949 DTK_ADDR_SPACE_I2C_TYPE_II, 0, data, 0x01);
951 dbg("%s - read 2 status error = %d", __func__, status);
953 dbg("%s - read 2 data = 0x%x", __func__, *data);
954 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
955 dbg("%s - ROM_TYPE_II", __func__);
956 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
960 /* Try to read type 3 */
961 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
962 DTK_ADDR_SPACE_I2C_TYPE_III, 0, data, 0x01);
964 dbg("%s - read 3 status error = %d", __func__, status);
966 dbg("%s - read 2 data = 0x%x", __func__, *data);
967 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
968 dbg("%s - ROM_TYPE_III", __func__);
969 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
973 dbg("%s - Unknown", __func__);
974 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
981 static int bulk_xfer(struct usb_serial *serial, void *buffer,
982 int length, int *num_sent)
986 status = usb_bulk_msg(serial->dev,
987 usb_sndbulkpipe(serial->dev,
988 serial->port[0]->bulk_out_endpointAddress),
989 buffer, length, num_sent, 1000);
993 /* Download given firmware image to the device (IN BOOT MODE) */
994 static int download_code(struct edgeport_serial *serial, __u8 *image,
1002 /* Transfer firmware image */
1003 for (pos = 0; pos < image_length; ) {
1004 /* Read the next buffer from file */
1005 transfer = image_length - pos;
1006 if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
1007 transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
1010 status = bulk_xfer(serial->serial, &image[pos],
1014 /* Advance buffer pointer */
1022 static int config_boot_dev(struct usb_device *dev)
1027 static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc)
1029 return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev);
1033 * DownloadTIFirmware - Download run-time operating firmware to the TI5052
1035 * This routine downloads the main operating code into the TI5052, using the
1036 * boot code already burned into E2PROM or ROM.
1038 static int download_fw(struct edgeport_serial *serial)
1040 struct device *dev = &serial->serial->dev->dev;
1043 struct edge_ti_manuf_descriptor *ti_manuf_desc;
1044 struct usb_interface_descriptor *interface;
1045 int download_cur_ver;
1046 int download_new_ver;
1048 /* This routine is entered by both the BOOT mode and the Download mode
1049 * We can determine which code is running by the reading the config
1050 * descriptor and if we have only one bulk pipe it is in boot mode
1052 serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
1054 /* Default to type 2 i2c */
1055 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1057 status = choose_config(serial->serial->dev);
1061 interface = &serial->serial->interface->cur_altsetting->desc;
1063 dev_err(dev, "%s - no interface set, error!\n", __func__);
1068 * Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
1069 * if we have more than one endpoint we are definitely in download
1072 if (interface->bNumEndpoints > 1)
1073 serial->product_info.TiMode = TI_MODE_DOWNLOAD;
1075 /* Otherwise we will remain in configuring mode */
1076 serial->product_info.TiMode = TI_MODE_CONFIGURING;
1078 /********************************************************************/
1080 /********************************************************************/
1081 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
1082 struct ti_i2c_desc *rom_desc;
1084 dbg("%s - RUNNING IN DOWNLOAD MODE", __func__);
1086 status = check_i2c_image(serial);
1088 dbg("%s - DOWNLOAD MODE -- BAD I2C", __func__);
1092 /* Validate Hardware version number
1093 * Read Manufacturing Descriptor from TI Based Edgeport
1095 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
1096 if (!ti_manuf_desc) {
1097 dev_err(dev, "%s - out of memory.\n", __func__);
1100 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
1102 kfree(ti_manuf_desc);
1106 /* Check version number of ION descriptor */
1107 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1108 dbg("%s - Wrong CPU Rev %d (Must be 2)",
1109 __func__, ti_cpu_rev(ti_manuf_desc));
1110 kfree(ti_manuf_desc);
1114 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
1116 dev_err(dev, "%s - out of memory.\n", __func__);
1117 kfree(ti_manuf_desc);
1121 /* Search for type 2 record (firmware record) */
1122 start_address = get_descriptor_addr(serial,
1123 I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc);
1124 if (start_address != 0) {
1125 struct ti_i2c_firmware_rec *firmware_version;
1128 dbg("%s - Found Type FIRMWARE (Type 2) record",
1131 firmware_version = kmalloc(sizeof(*firmware_version),
1133 if (!firmware_version) {
1134 dev_err(dev, "%s - out of memory.\n", __func__);
1136 kfree(ti_manuf_desc);
1140 /* Validate version number
1141 * Read the descriptor data
1143 status = read_rom(serial, start_address +
1144 sizeof(struct ti_i2c_desc),
1145 sizeof(struct ti_i2c_firmware_rec),
1146 (__u8 *)firmware_version);
1148 kfree(firmware_version);
1150 kfree(ti_manuf_desc);
1154 /* Check version number of download with current
1156 download_cur_ver = (firmware_version->Ver_Major << 8) +
1157 (firmware_version->Ver_Minor);
1158 download_new_ver = (OperationalMajorVersion << 8) +
1159 (OperationalMinorVersion);
1161 dbg("%s - >> FW Versions Device %d.%d Driver %d.%d",
1163 firmware_version->Ver_Major,
1164 firmware_version->Ver_Minor,
1165 OperationalMajorVersion,
1166 OperationalMinorVersion);
1168 /* Check if we have an old version in the I2C and
1169 update if necessary */
1170 if (download_cur_ver < download_new_ver) {
1171 dbg("%s - Update I2C dld from %d.%d to %d.%d",
1173 firmware_version->Ver_Major,
1174 firmware_version->Ver_Minor,
1175 OperationalMajorVersion,
1176 OperationalMinorVersion);
1178 record = kmalloc(1, GFP_KERNEL);
1180 dev_err(dev, "%s - out of memory.\n",
1182 kfree(firmware_version);
1184 kfree(ti_manuf_desc);
1187 /* In order to update the I2C firmware we must
1188 * change the type 2 record to type 0xF2. This
1189 * will force the UMP to come up in Boot Mode.
1190 * Then while in boot mode, the driver will
1191 * download the latest firmware (padded to
1192 * 15.5k) into the UMP ram. Finally when the
1193 * device comes back up in download mode the
1194 * driver will cause the new firmware to be
1195 * copied from the UMP Ram to I2C and the
1196 * firmware will update the record type from
1199 *record = I2C_DESC_TYPE_FIRMWARE_BLANK;
1201 /* Change the I2C Firmware record type to
1202 0xf2 to trigger an update */
1203 status = write_rom(serial, start_address,
1204 sizeof(*record), record);
1207 kfree(firmware_version);
1209 kfree(ti_manuf_desc);
1213 /* verify the write -- must do this in order
1214 * for write to complete before we do the
1217 status = read_rom(serial,
1223 kfree(firmware_version);
1225 kfree(ti_manuf_desc);
1229 if (*record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
1231 "%s - error resetting device\n",
1234 kfree(firmware_version);
1236 kfree(ti_manuf_desc);
1240 dbg("%s - HARDWARE RESET", __func__);
1242 /* Reset UMP -- Back to BOOT MODE */
1243 status = ti_vsend_sync(serial->serial->dev,
1244 UMPC_HARDWARE_RESET,
1247 dbg("%s - HARDWARE RESET return %d",
1250 /* return an error on purpose. */
1252 kfree(firmware_version);
1254 kfree(ti_manuf_desc);
1257 kfree(firmware_version);
1259 /* Search for type 0xF2 record (firmware blank record) */
1260 else if ((start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
1261 #define HEADER_SIZE (sizeof(struct ti_i2c_desc) + \
1262 sizeof(struct ti_i2c_firmware_rec))
1266 header = kmalloc(HEADER_SIZE, GFP_KERNEL);
1268 dev_err(dev, "%s - out of memory.\n", __func__);
1270 kfree(ti_manuf_desc);
1274 vheader = kmalloc(HEADER_SIZE, GFP_KERNEL);
1276 dev_err(dev, "%s - out of memory.\n", __func__);
1279 kfree(ti_manuf_desc);
1283 dbg("%s - Found Type BLANK FIRMWARE (Type F2) record",
1287 * In order to update the I2C firmware we must change
1288 * the type 2 record to type 0xF2. This will force the
1289 * UMP to come up in Boot Mode. Then while in boot
1290 * mode, the driver will download the latest firmware
1291 * (padded to 15.5k) into the UMP ram. Finally when the
1292 * device comes back up in download mode the driver
1293 * will cause the new firmware to be copied from the
1294 * UMP Ram to I2C and the firmware will update the
1295 * record type from 0xf2 to 0x02.
1297 status = build_i2c_fw_hdr(header, dev);
1302 kfree(ti_manuf_desc);
1306 /* Update I2C with type 0xf2 record with correct
1307 size and checksum */
1308 status = write_rom(serial,
1316 kfree(ti_manuf_desc);
1320 /* verify the write -- must do this in order for
1321 write to complete before we do the hardware reset */
1322 status = read_rom(serial, start_address,
1323 HEADER_SIZE, vheader);
1326 dbg("%s - can't read header back", __func__);
1330 kfree(ti_manuf_desc);
1333 if (memcmp(vheader, header, HEADER_SIZE)) {
1334 dbg("%s - write download record failed",
1339 kfree(ti_manuf_desc);
1346 dbg("%s - Start firmware update", __func__);
1348 /* Tell firmware to copy download image into I2C */
1349 status = ti_vsend_sync(serial->serial->dev,
1350 UMPC_COPY_DNLD_TO_I2C, 0, 0, NULL, 0);
1352 dbg("%s - Update complete 0x%x", __func__, status);
1355 "%s - UMPC_COPY_DNLD_TO_I2C failed\n",
1358 kfree(ti_manuf_desc);
1363 // The device is running the download code
1365 kfree(ti_manuf_desc);
1369 /********************************************************************/
1371 /********************************************************************/
1372 dbg("%s - RUNNING IN BOOT MODE", __func__);
1374 /* Configure the TI device so we can use the BULK pipes for download */
1375 status = config_boot_dev(serial->serial->dev);
1379 if (le16_to_cpu(serial->serial->dev->descriptor.idVendor)
1380 != USB_VENDOR_ID_ION) {
1381 dbg("%s - VID = 0x%x", __func__,
1382 le16_to_cpu(serial->serial->dev->descriptor.idVendor));
1383 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1384 goto stayinbootmode;
1387 /* We have an ION device (I2c Must be programmed)
1388 Determine I2C image type */
1389 if (i2c_type_bootmode(serial))
1390 goto stayinbootmode;
1392 /* Check for ION Vendor ID and that the I2C is valid */
1393 if (!check_i2c_image(serial)) {
1394 struct ti_i2c_image_header *header;
1400 const struct firmware *fw;
1401 const char *fw_name = "edgeport/down3.bin";
1403 /* Validate Hardware version number
1404 * Read Manufacturing Descriptor from TI Based Edgeport
1406 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
1407 if (!ti_manuf_desc) {
1408 dev_err(dev, "%s - out of memory.\n", __func__);
1411 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
1413 kfree(ti_manuf_desc);
1414 goto stayinbootmode;
1417 /* Check for version 2 */
1418 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1419 dbg("%s - Wrong CPU Rev %d (Must be 2)",
1420 __func__, ti_cpu_rev(ti_manuf_desc));
1421 kfree(ti_manuf_desc);
1422 goto stayinbootmode;
1425 kfree(ti_manuf_desc);
1428 * In order to update the I2C firmware we must change the type
1429 * 2 record to type 0xF2. This will force the UMP to come up
1430 * in Boot Mode. Then while in boot mode, the driver will
1431 * download the latest firmware (padded to 15.5k) into the
1432 * UMP ram. Finally when the device comes back up in download
1433 * mode the driver will cause the new firmware to be copied
1434 * from the UMP Ram to I2C and the firmware will update the
1435 * record type from 0xf2 to 0x02.
1437 * Do we really have to copy the whole firmware image,
1438 * or could we do this in place!
1441 /* Allocate a 15.5k buffer + 3 byte header */
1442 buffer_size = (((1024 * 16) - 512) +
1443 sizeof(struct ti_i2c_image_header));
1444 buffer = kmalloc(buffer_size, GFP_KERNEL);
1446 dev_err(dev, "%s - out of memory\n", __func__);
1450 /* Initialize the buffer to 0xff (pad the buffer) */
1451 memset(buffer, 0xff, buffer_size);
1453 err = request_firmware(&fw, fw_name, dev);
1455 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
1460 memcpy(buffer, &fw->data[4], fw->size - 4);
1461 release_firmware(fw);
1463 for (i = sizeof(struct ti_i2c_image_header);
1464 i < buffer_size; i++) {
1465 cs = (__u8)(cs + buffer[i]);
1468 header = (struct ti_i2c_image_header *)buffer;
1470 /* update length and checksum after padding */
1471 header->Length = cpu_to_le16((__u16)(buffer_size -
1472 sizeof(struct ti_i2c_image_header)));
1473 header->CheckSum = cs;
1475 /* Download the operational code */
1476 dbg("%s - Downloading operational code image (TI UMP)",
1478 status = download_code(serial, buffer, buffer_size);
1483 dbg("%s - Error downloading operational code image",
1488 /* Device will reboot */
1489 serial->product_info.TiMode = TI_MODE_TRANSITIONING;
1491 dbg("%s - Download successful -- Device rebooting...",
1494 /* return an error on purpose */
1499 /* Eprom is invalid or blank stay in boot mode */
1500 dbg("%s - STAYING IN BOOT MODE", __func__);
1501 serial->product_info.TiMode = TI_MODE_BOOT;
1507 static int ti_do_config(struct edgeport_port *port, int feature, int on)
1509 int port_number = port->port->number - port->port->serial->minor;
1510 on = !!on; /* 1 or 0 not bitmask */
1511 return send_cmd(port->port->serial->dev,
1512 feature, (__u8)(UMPM_UART1_PORT + port_number),
1517 static int restore_mcr(struct edgeport_port *port, __u8 mcr)
1521 dbg("%s - %x", __func__, mcr);
1523 status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR);
1526 status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS);
1529 return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK);
1532 /* Convert TI LSR to standard UART flags */
1533 static __u8 map_line_status(__u8 ti_lsr)
1537 #define MAP_FLAG(flagUmp, flagUart) \
1538 if (ti_lsr & flagUmp) \
1541 MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR) /* overrun */
1542 MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */
1543 MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */
1544 MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */
1545 MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* rx data available */
1546 MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* tx hold reg empty */
1553 static void handle_new_msr(struct edgeport_port *edge_port, __u8 msr)
1555 struct async_icount *icount;
1556 struct tty_struct *tty;
1558 dbg("%s - %02x", __func__, msr);
1560 if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
1561 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
1562 icount = &edge_port->icount;
1564 /* update input line counters */
1565 if (msr & EDGEPORT_MSR_DELTA_CTS)
1567 if (msr & EDGEPORT_MSR_DELTA_DSR)
1569 if (msr & EDGEPORT_MSR_DELTA_CD)
1571 if (msr & EDGEPORT_MSR_DELTA_RI)
1573 wake_up_interruptible(&edge_port->port->delta_msr_wait);
1576 /* Save the new modem status */
1577 edge_port->shadow_msr = msr & 0xf0;
1579 tty = tty_port_tty_get(&edge_port->port->port);
1580 /* handle CTS flow control */
1581 if (tty && C_CRTSCTS(tty)) {
1582 if (msr & EDGEPORT_MSR_CTS) {
1583 tty->hw_stopped = 0;
1586 tty->hw_stopped = 1;
1592 static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data,
1593 __u8 lsr, __u8 data)
1595 struct async_icount *icount;
1596 __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR |
1597 LSR_FRM_ERR | LSR_BREAK));
1598 struct tty_struct *tty;
1600 dbg("%s - %02x", __func__, new_lsr);
1602 edge_port->shadow_lsr = lsr;
1604 if (new_lsr & LSR_BREAK)
1606 * Parity and Framing errors only count if they
1607 * occur exclusive of a break being received.
1609 new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
1611 /* Place LSR data byte into Rx buffer */
1613 tty = tty_port_tty_get(&edge_port->port->port);
1615 edge_tty_recv(&edge_port->port->dev, tty, &data, 1);
1620 /* update input line counters */
1621 icount = &edge_port->icount;
1622 if (new_lsr & LSR_BREAK)
1624 if (new_lsr & LSR_OVER_ERR)
1626 if (new_lsr & LSR_PAR_ERR)
1628 if (new_lsr & LSR_FRM_ERR)
1633 static void edge_interrupt_callback(struct urb *urb)
1635 struct edgeport_serial *edge_serial = urb->context;
1636 struct usb_serial_port *port;
1637 struct edgeport_port *edge_port;
1638 unsigned char *data = urb->transfer_buffer;
1639 int length = urb->actual_length;
1645 int status = urb->status;
1647 dbg("%s", __func__);
1656 /* this urb is terminated, clean up */
1657 dbg("%s - urb shutting down with status: %d",
1661 dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
1662 "%d\n", __func__, status);
1667 dbg("%s - no data in urb", __func__);
1671 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev,
1672 __func__, length, data);
1675 dbg("%s - expecting packet of size 2, got %d",
1680 port_number = TIUMP_GET_PORT_FROM_CODE(data[0]);
1681 function = TIUMP_GET_FUNC_FROM_CODE(data[0]);
1682 dbg("%s - port_number %d, function %d, info 0x%x",
1683 __func__, port_number, function, data[1]);
1684 port = edge_serial->serial->port[port_number];
1685 edge_port = usb_get_serial_port_data(port);
1687 dbg("%s - edge_port not found", __func__);
1691 case TIUMP_INTERRUPT_CODE_LSR:
1692 lsr = map_line_status(data[1]);
1693 if (lsr & UMP_UART_LSR_DATA_MASK) {
1694 /* Save the LSR event for bulk read
1695 completion routine */
1696 dbg("%s - LSR Event Port %u LSR Status = %02x",
1697 __func__, port_number, lsr);
1698 edge_port->lsr_event = 1;
1699 edge_port->lsr_mask = lsr;
1701 dbg("%s - ===== Port %d LSR Status = %02x ======",
1702 __func__, port_number, lsr);
1703 handle_new_lsr(edge_port, 0, lsr, 0);
1707 case TIUMP_INTERRUPT_CODE_MSR: /* MSR */
1708 /* Copy MSR from UMP */
1710 dbg("%s - ===== Port %u MSR Status = %02x ======",
1711 __func__, port_number, msr);
1712 handle_new_msr(edge_port, msr);
1716 dev_err(&urb->dev->dev,
1717 "%s - Unknown Interrupt code from UMP %x\n",
1724 retval = usb_submit_urb(urb, GFP_ATOMIC);
1726 dev_err(&urb->dev->dev,
1727 "%s - usb_submit_urb failed with result %d\n",
1731 static void edge_bulk_in_callback(struct urb *urb)
1733 struct edgeport_port *edge_port = urb->context;
1734 unsigned char *data = urb->transfer_buffer;
1735 struct tty_struct *tty;
1738 int status = urb->status;
1740 dbg("%s", __func__);
1749 /* this urb is terminated, clean up */
1750 dbg("%s - urb shutting down with status: %d",
1754 dev_err(&urb->dev->dev,
1755 "%s - nonzero read bulk status received: %d\n",
1759 if (status == -EPIPE)
1763 dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__);
1767 port_number = edge_port->port->number - edge_port->port->serial->minor;
1769 if (edge_port->lsr_event) {
1770 edge_port->lsr_event = 0;
1771 dbg("%s ===== Port %u LSR Status = %02x, Data = %02x ======",
1772 __func__, port_number, edge_port->lsr_mask, *data);
1773 handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data);
1774 /* Adjust buffer length/pointer */
1775 --urb->actual_length;
1779 tty = tty_port_tty_get(&edge_port->port->port);
1780 if (tty && urb->actual_length) {
1781 usb_serial_debug_data(debug, &edge_port->port->dev,
1782 __func__, urb->actual_length, data);
1783 if (edge_port->close_pending)
1784 dbg("%s - close pending, dropping data on the floor",
1787 edge_tty_recv(&edge_port->port->dev, tty, data,
1788 urb->actual_length);
1789 edge_port->icount.rx += urb->actual_length;
1794 /* continue read unless stopped */
1795 spin_lock(&edge_port->ep_lock);
1796 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING) {
1797 urb->dev = edge_port->port->serial->dev;
1798 retval = usb_submit_urb(urb, GFP_ATOMIC);
1799 } else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING) {
1800 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
1802 spin_unlock(&edge_port->ep_lock);
1804 dev_err(&urb->dev->dev,
1805 "%s - usb_submit_urb failed with result %d\n",
1809 static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
1810 unsigned char *data, int length)
1814 queued = tty_insert_flip_string(tty, data, length);
1815 if (queued < length)
1816 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1817 __func__, length - queued);
1818 tty_flip_buffer_push(tty);
1821 static void edge_bulk_out_callback(struct urb *urb)
1823 struct usb_serial_port *port = urb->context;
1824 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1825 int status = urb->status;
1826 struct tty_struct *tty;
1828 dbg("%s - port %d", __func__, port->number);
1830 edge_port->ep_write_urb_in_use = 0;
1839 /* this urb is terminated, clean up */
1840 dbg("%s - urb shutting down with status: %d",
1844 dev_err(&urb->dev->dev, "%s - nonzero write bulk status "
1845 "received: %d\n", __func__, status);
1848 /* send any buffered data */
1849 tty = tty_port_tty_get(&port->port);
1854 static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
1856 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1857 struct edgeport_serial *edge_serial;
1858 struct usb_device *dev;
1863 u8 transaction_timeout;
1865 dbg("%s - port %d", __func__, port->number);
1867 if (edge_port == NULL)
1870 port_number = port->number - port->serial->minor;
1871 switch (port_number) {
1873 edge_port->uart_base = UMPMEM_BASE_UART1;
1874 edge_port->dma_address = UMPD_OEDB1_ADDRESS;
1877 edge_port->uart_base = UMPMEM_BASE_UART2;
1878 edge_port->dma_address = UMPD_OEDB2_ADDRESS;
1881 dev_err(&port->dev, "Unknown port number!!!\n");
1885 dbg("%s - port_number = %d, uart_base = %04x, dma_address = %04x",
1886 __func__, port_number, edge_port->uart_base,
1887 edge_port->dma_address);
1889 dev = port->serial->dev;
1891 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
1893 /* turn off loopback */
1894 status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0);
1897 "%s - cannot send clear loopback command, %d\n",
1902 /* set up the port settings */
1904 edge_set_termios(tty, port, tty->termios);
1906 /* open up the port */
1908 /* milliseconds to timeout for DMA transfer */
1909 transaction_timeout = 2;
1911 edge_port->ump_read_timeout =
1912 max(20, ((transaction_timeout * 3) / 2));
1914 /* milliseconds to timeout for DMA transfer */
1915 open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
1916 UMP_PIPE_TRANS_TIMEOUT_ENA |
1917 (transaction_timeout << 2));
1919 dbg("%s - Sending UMPC_OPEN_PORT", __func__);
1921 /* Tell TI to open and start the port */
1922 status = send_cmd(dev, UMPC_OPEN_PORT,
1923 (u8)(UMPM_UART1_PORT + port_number), open_settings, NULL, 0);
1925 dev_err(&port->dev, "%s - cannot send open command, %d\n",
1930 /* Start the DMA? */
1931 status = send_cmd(dev, UMPC_START_PORT,
1932 (u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
1934 dev_err(&port->dev, "%s - cannot send start DMA command, %d\n",
1939 /* Clear TX and RX buffers in UMP */
1940 status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
1943 "%s - cannot send clear buffers command, %d\n",
1948 /* Read Initial MSR */
1949 status = ti_vread_sync(dev, UMPC_READ_MSR, 0,
1950 (__u16)(UMPM_UART1_PORT + port_number),
1951 &edge_port->shadow_msr, 1);
1953 dev_err(&port->dev, "%s - cannot send read MSR command, %d\n",
1958 dbg("ShadowMSR 0x%X", edge_port->shadow_msr);
1960 /* Set Initial MCR */
1961 edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
1962 dbg("ShadowMCR 0x%X", edge_port->shadow_mcr);
1964 edge_serial = edge_port->edge_serial;
1965 if (mutex_lock_interruptible(&edge_serial->es_lock))
1966 return -ERESTARTSYS;
1967 if (edge_serial->num_ports_open == 0) {
1968 /* we are the first port to open, post the interrupt urb */
1969 urb = edge_serial->serial->port[0]->interrupt_in_urb;
1972 "%s - no interrupt urb present, exiting\n",
1975 goto release_es_lock;
1977 urb->complete = edge_interrupt_callback;
1978 urb->context = edge_serial;
1980 status = usb_submit_urb(urb, GFP_KERNEL);
1983 "%s - usb_submit_urb failed with value %d\n",
1985 goto release_es_lock;
1990 * reset the data toggle on the bulk endpoints to work around bug in
1991 * host controllers where things get out of sync some times
1993 usb_clear_halt(dev, port->write_urb->pipe);
1994 usb_clear_halt(dev, port->read_urb->pipe);
1996 /* start up our bulk read urb */
1997 urb = port->read_urb;
1999 dev_err(&port->dev, "%s - no read urb present, exiting\n",
2002 goto unlink_int_urb;
2004 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2005 urb->complete = edge_bulk_in_callback;
2006 urb->context = edge_port;
2008 status = usb_submit_urb(urb, GFP_KERNEL);
2011 "%s - read bulk usb_submit_urb failed with value %d\n",
2013 goto unlink_int_urb;
2016 ++edge_serial->num_ports_open;
2018 dbg("%s - exited", __func__);
2020 goto release_es_lock;
2023 if (edge_port->edge_serial->num_ports_open == 0)
2024 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2026 mutex_unlock(&edge_serial->es_lock);
2030 static void edge_close(struct usb_serial_port *port)
2032 struct edgeport_serial *edge_serial;
2033 struct edgeport_port *edge_port;
2037 dbg("%s - port %d", __func__, port->number);
2039 edge_serial = usb_get_serial_data(port->serial);
2040 edge_port = usb_get_serial_port_data(port);
2041 if (edge_serial == NULL || edge_port == NULL)
2044 /* The bulkreadcompletion routine will check
2045 * this flag and dump add read data */
2046 edge_port->close_pending = 1;
2048 /* chase the port close and flush */
2049 chase_port(edge_port, (HZ * closing_wait) / 100, 1);
2051 usb_kill_urb(port->read_urb);
2052 usb_kill_urb(port->write_urb);
2053 edge_port->ep_write_urb_in_use = 0;
2055 /* assuming we can still talk to the device,
2056 * send a close port command to it */
2057 dbg("%s - send umpc_close_port", __func__);
2058 port_number = port->number - port->serial->minor;
2059 status = send_cmd(port->serial->dev,
2061 (__u8)(UMPM_UART1_PORT + port_number),
2065 mutex_lock(&edge_serial->es_lock);
2066 --edge_port->edge_serial->num_ports_open;
2067 if (edge_port->edge_serial->num_ports_open <= 0) {
2068 /* last port is now closed, let's shut down our interrupt urb */
2069 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2070 edge_port->edge_serial->num_ports_open = 0;
2072 mutex_unlock(&edge_serial->es_lock);
2073 edge_port->close_pending = 0;
2075 dbg("%s - exited", __func__);
2078 static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
2079 const unsigned char *data, int count)
2081 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2083 dbg("%s - port %d", __func__, port->number);
2086 dbg("%s - write request of 0 bytes", __func__);
2090 if (edge_port == NULL)
2092 if (edge_port->close_pending == 1)
2095 count = kfifo_in_locked(&edge_port->write_fifo, data, count,
2096 &edge_port->ep_lock);
2102 static void edge_send(struct tty_struct *tty)
2104 struct usb_serial_port *port = tty->driver_data;
2106 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2107 unsigned long flags;
2110 dbg("%s - port %d", __func__, port->number);
2112 spin_lock_irqsave(&edge_port->ep_lock, flags);
2114 if (edge_port->ep_write_urb_in_use) {
2115 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2119 count = kfifo_out(&edge_port->write_fifo,
2120 port->write_urb->transfer_buffer,
2121 port->bulk_out_size);
2124 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2128 edge_port->ep_write_urb_in_use = 1;
2130 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2132 usb_serial_debug_data(debug, &port->dev, __func__, count,
2133 port->write_urb->transfer_buffer);
2135 /* set up our urb */
2136 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
2137 usb_sndbulkpipe(port->serial->dev,
2138 port->bulk_out_endpointAddress),
2139 port->write_urb->transfer_buffer, count,
2140 edge_bulk_out_callback,
2143 /* send the data out the bulk port */
2144 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2147 "%s - failed submitting write urb, error %d\n",
2149 edge_port->ep_write_urb_in_use = 0;
2150 /* TODO: reschedule edge_send */
2152 edge_port->icount.tx += count;
2154 /* wakeup any process waiting for writes to complete */
2155 /* there is now more room in the buffer for new writes */
2160 static int edge_write_room(struct tty_struct *tty)
2162 struct usb_serial_port *port = tty->driver_data;
2163 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2165 unsigned long flags;
2167 dbg("%s - port %d", __func__, port->number);
2169 if (edge_port == NULL)
2171 if (edge_port->close_pending == 1)
2174 spin_lock_irqsave(&edge_port->ep_lock, flags);
2175 room = kfifo_avail(&edge_port->write_fifo);
2176 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2178 dbg("%s - returns %d", __func__, room);
2182 static int edge_chars_in_buffer(struct tty_struct *tty)
2184 struct usb_serial_port *port = tty->driver_data;
2185 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2187 unsigned long flags;
2189 dbg("%s - port %d", __func__, port->number);
2191 if (edge_port == NULL)
2193 if (edge_port->close_pending == 1)
2196 spin_lock_irqsave(&edge_port->ep_lock, flags);
2197 chars = kfifo_len(&edge_port->write_fifo);
2198 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2200 dbg("%s - returns %d", __func__, chars);
2204 static void edge_throttle(struct tty_struct *tty)
2206 struct usb_serial_port *port = tty->driver_data;
2207 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2210 dbg("%s - port %d", __func__, port->number);
2212 if (edge_port == NULL)
2215 /* if we are implementing XON/XOFF, send the stop character */
2217 unsigned char stop_char = STOP_CHAR(tty);
2218 status = edge_write(tty, port, &stop_char, 1);
2220 dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status);
2224 /* if we are implementing RTS/CTS, stop reads */
2225 /* and the Edgeport will clear the RTS line */
2227 stop_read(edge_port);
2231 static void edge_unthrottle(struct tty_struct *tty)
2233 struct usb_serial_port *port = tty->driver_data;
2234 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2237 dbg("%s - port %d", __func__, port->number);
2239 if (edge_port == NULL)
2242 /* if we are implementing XON/XOFF, send the start character */
2244 unsigned char start_char = START_CHAR(tty);
2245 status = edge_write(tty, port, &start_char, 1);
2247 dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status);
2250 /* if we are implementing RTS/CTS, restart reads */
2251 /* are the Edgeport will assert the RTS line */
2252 if (C_CRTSCTS(tty)) {
2253 status = restart_read(edge_port);
2256 "%s - read bulk usb_submit_urb failed: %d\n",
2262 static void stop_read(struct edgeport_port *edge_port)
2264 unsigned long flags;
2266 spin_lock_irqsave(&edge_port->ep_lock, flags);
2268 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
2269 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
2270 edge_port->shadow_mcr &= ~MCR_RTS;
2272 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2275 static int restart_read(struct edgeport_port *edge_port)
2279 unsigned long flags;
2281 spin_lock_irqsave(&edge_port->ep_lock, flags);
2283 if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
2284 urb = edge_port->port->read_urb;
2285 urb->complete = edge_bulk_in_callback;
2286 urb->context = edge_port;
2287 urb->dev = edge_port->port->serial->dev;
2288 status = usb_submit_urb(urb, GFP_ATOMIC);
2290 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2291 edge_port->shadow_mcr |= MCR_RTS;
2293 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2298 static void change_port_settings(struct tty_struct *tty,
2299 struct edgeport_port *edge_port, struct ktermios *old_termios)
2301 struct ump_uart_config *config;
2305 int port_number = edge_port->port->number -
2306 edge_port->port->serial->minor;
2308 dbg("%s - port %d", __func__, edge_port->port->number);
2310 config = kmalloc (sizeof (*config), GFP_KERNEL);
2312 *tty->termios = *old_termios;
2313 dev_err(&edge_port->port->dev, "%s - out of memory\n",
2318 cflag = tty->termios->c_cflag;
2322 /* These flags must be set */
2323 config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
2324 config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
2325 config->bUartMode = (__u8)(edge_port->bUartMode);
2327 switch (cflag & CSIZE) {
2329 config->bDataBits = UMP_UART_CHAR5BITS;
2330 dbg("%s - data bits = 5", __func__);
2333 config->bDataBits = UMP_UART_CHAR6BITS;
2334 dbg("%s - data bits = 6", __func__);
2337 config->bDataBits = UMP_UART_CHAR7BITS;
2338 dbg("%s - data bits = 7", __func__);
2342 config->bDataBits = UMP_UART_CHAR8BITS;
2343 dbg("%s - data bits = 8", __func__);
2347 if (cflag & PARENB) {
2348 if (cflag & PARODD) {
2349 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2350 config->bParity = UMP_UART_ODDPARITY;
2351 dbg("%s - parity = odd", __func__);
2353 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2354 config->bParity = UMP_UART_EVENPARITY;
2355 dbg("%s - parity = even", __func__);
2358 config->bParity = UMP_UART_NOPARITY;
2359 dbg("%s - parity = none", __func__);
2362 if (cflag & CSTOPB) {
2363 config->bStopBits = UMP_UART_STOPBIT2;
2364 dbg("%s - stop bits = 2", __func__);
2366 config->bStopBits = UMP_UART_STOPBIT1;
2367 dbg("%s - stop bits = 1", __func__);
2370 /* figure out the flow control settings */
2371 if (cflag & CRTSCTS) {
2372 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
2373 config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
2374 dbg("%s - RTS/CTS is enabled", __func__);
2376 dbg("%s - RTS/CTS is disabled", __func__);
2377 tty->hw_stopped = 0;
2378 restart_read(edge_port);
2381 /* if we are implementing XON/XOFF, set the start and stop
2382 character in the device */
2383 config->cXon = START_CHAR(tty);
2384 config->cXoff = STOP_CHAR(tty);
2386 /* if we are implementing INBOUND XON/XOFF */
2388 config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
2389 dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2390 __func__, config->cXon, config->cXoff);
2392 dbg("%s - INBOUND XON/XOFF is disabled", __func__);
2394 /* if we are implementing OUTBOUND XON/XOFF */
2396 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
2397 dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2398 __func__, config->cXon, config->cXoff);
2400 dbg("%s - OUTBOUND XON/XOFF is disabled", __func__);
2402 tty->termios->c_cflag &= ~CMSPAR;
2404 /* Round the baud rate */
2405 baud = tty_get_baud_rate(tty);
2407 /* pick a default, any default... */
2410 tty_encode_baud_rate(tty, baud, baud);
2412 edge_port->baud_rate = baud;
2413 config->wBaudRate = (__u16)((461550L + baud/2) / baud);
2415 /* FIXME: Recompute actual baud from divisor here */
2417 dbg("%s - baud rate = %d, wBaudRate = %d", __func__, baud,
2420 dbg("wBaudRate: %d", (int)(461550L / config->wBaudRate));
2421 dbg("wFlags: 0x%x", config->wFlags);
2422 dbg("bDataBits: %d", config->bDataBits);
2423 dbg("bParity: %d", config->bParity);
2424 dbg("bStopBits: %d", config->bStopBits);
2425 dbg("cXon: %d", config->cXon);
2426 dbg("cXoff: %d", config->cXoff);
2427 dbg("bUartMode: %d", config->bUartMode);
2429 /* move the word values into big endian mode */
2430 cpu_to_be16s(&config->wFlags);
2431 cpu_to_be16s(&config->wBaudRate);
2433 status = send_cmd(edge_port->port->serial->dev, UMPC_SET_CONFIG,
2434 (__u8)(UMPM_UART1_PORT + port_number),
2435 0, (__u8 *)config, sizeof(*config));
2437 dbg("%s - error %d when trying to write config to device",
2442 static void edge_set_termios(struct tty_struct *tty,
2443 struct usb_serial_port *port, struct ktermios *old_termios)
2445 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2448 cflag = tty->termios->c_cflag;
2450 dbg("%s - clfag %08x iflag %08x", __func__,
2451 tty->termios->c_cflag, tty->termios->c_iflag);
2452 dbg("%s - old clfag %08x old iflag %08x", __func__,
2453 old_termios->c_cflag, old_termios->c_iflag);
2454 dbg("%s - port %d", __func__, port->number);
2456 if (edge_port == NULL)
2458 /* change the port settings to the new ones specified */
2459 change_port_settings(tty, edge_port, old_termios);
2462 static int edge_tiocmset(struct tty_struct *tty,
2463 unsigned int set, unsigned int clear)
2465 struct usb_serial_port *port = tty->driver_data;
2466 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2468 unsigned long flags;
2470 dbg("%s - port %d", __func__, port->number);
2472 spin_lock_irqsave(&edge_port->ep_lock, flags);
2473 mcr = edge_port->shadow_mcr;
2474 if (set & TIOCM_RTS)
2476 if (set & TIOCM_DTR)
2478 if (set & TIOCM_LOOP)
2479 mcr |= MCR_LOOPBACK;
2481 if (clear & TIOCM_RTS)
2483 if (clear & TIOCM_DTR)
2485 if (clear & TIOCM_LOOP)
2486 mcr &= ~MCR_LOOPBACK;
2488 edge_port->shadow_mcr = mcr;
2489 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2491 restore_mcr(edge_port, mcr);
2495 static int edge_tiocmget(struct tty_struct *tty)
2497 struct usb_serial_port *port = tty->driver_data;
2498 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2499 unsigned int result = 0;
2502 unsigned long flags;
2504 dbg("%s - port %d", __func__, port->number);
2506 spin_lock_irqsave(&edge_port->ep_lock, flags);
2508 msr = edge_port->shadow_msr;
2509 mcr = edge_port->shadow_mcr;
2510 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
2511 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
2512 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
2513 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
2514 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
2515 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
2518 dbg("%s -- %x", __func__, result);
2519 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2524 static int edge_get_icount(struct tty_struct *tty,
2525 struct serial_icounter_struct *icount)
2527 struct usb_serial_port *port = tty->driver_data;
2528 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2529 struct async_icount *ic = &edge_port->icount;
2531 icount->cts = ic->cts;
2532 icount->dsr = ic->dsr;
2533 icount->rng = ic->rng;
2534 icount->dcd = ic->dcd;
2535 icount->tx = ic->tx;
2536 icount->rx = ic->rx;
2537 icount->frame = ic->frame;
2538 icount->parity = ic->parity;
2539 icount->overrun = ic->overrun;
2540 icount->brk = ic->brk;
2541 icount->buf_overrun = ic->buf_overrun;
2545 static int get_serial_info(struct edgeport_port *edge_port,
2546 struct serial_struct __user *retinfo)
2548 struct serial_struct tmp;
2553 memset(&tmp, 0, sizeof(tmp));
2555 tmp.type = PORT_16550A;
2556 tmp.line = edge_port->port->serial->minor;
2557 tmp.port = edge_port->port->number;
2559 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2560 tmp.xmit_fifo_size = edge_port->port->bulk_out_size;
2561 tmp.baud_base = 9600;
2562 tmp.close_delay = 5*HZ;
2563 tmp.closing_wait = closing_wait;
2565 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2570 static int edge_ioctl(struct tty_struct *tty,
2571 unsigned int cmd, unsigned long arg)
2573 struct usb_serial_port *port = tty->driver_data;
2574 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2575 struct async_icount cnow;
2576 struct async_icount cprev;
2578 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
2582 dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
2583 return get_serial_info(edge_port,
2584 (struct serial_struct __user *) arg);
2586 dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
2587 cprev = edge_port->icount;
2589 interruptible_sleep_on(&port->delta_msr_wait);
2590 /* see if a signal did it */
2591 if (signal_pending(current))
2592 return -ERESTARTSYS;
2594 if (port->serial->disconnected)
2597 cnow = edge_port->icount;
2598 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2599 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2600 return -EIO; /* no change => error */
2601 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2602 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2603 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2604 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2612 return -ENOIOCTLCMD;
2615 static void edge_break(struct tty_struct *tty, int break_state)
2617 struct usb_serial_port *port = tty->driver_data;
2618 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2620 int bv = 0; /* Off */
2622 dbg("%s - state = %d", __func__, break_state);
2624 /* chase the port close */
2625 chase_port(edge_port, 0, 0);
2627 if (break_state == -1)
2629 status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv);
2631 dbg("%s - error %d sending break set/clear command.",
2635 static int edge_startup(struct usb_serial *serial)
2637 struct edgeport_serial *edge_serial;
2638 struct edgeport_port *edge_port;
2639 struct usb_device *dev;
2645 /* create our private serial structure */
2646 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
2647 if (edge_serial == NULL) {
2648 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
2651 mutex_init(&edge_serial->es_lock);
2652 edge_serial->serial = serial;
2653 usb_set_serial_data(serial, edge_serial);
2655 status = download_fw(edge_serial);
2661 /* set up our port private structures */
2662 for (i = 0; i < serial->num_ports; ++i) {
2663 edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
2664 if (edge_port == NULL) {
2665 dev_err(&serial->dev->dev, "%s - Out of memory\n",
2669 spin_lock_init(&edge_port->ep_lock);
2670 if (kfifo_alloc(&edge_port->write_fifo, EDGE_OUT_BUF_SIZE,
2672 dev_err(&serial->dev->dev, "%s - Out of memory\n",
2677 edge_port->port = serial->port[i];
2678 edge_port->edge_serial = edge_serial;
2679 usb_set_serial_port_data(serial->port[i], edge_port);
2680 edge_port->bUartMode = default_uart_mode;
2686 for (--i; i >= 0; --i) {
2687 edge_port = usb_get_serial_port_data(serial->port[i]);
2688 kfifo_free(&edge_port->write_fifo);
2690 usb_set_serial_port_data(serial->port[i], NULL);
2693 usb_set_serial_data(serial, NULL);
2697 static void edge_disconnect(struct usb_serial *serial)
2699 dbg("%s", __func__);
2702 static void edge_release(struct usb_serial *serial)
2705 struct edgeport_port *edge_port;
2707 dbg("%s", __func__);
2709 for (i = 0; i < serial->num_ports; ++i) {
2710 edge_port = usb_get_serial_port_data(serial->port[i]);
2711 kfifo_free(&edge_port->write_fifo);
2714 kfree(usb_get_serial_data(serial));
2718 /* Sysfs Attributes */
2720 static ssize_t show_uart_mode(struct device *dev,
2721 struct device_attribute *attr, char *buf)
2723 struct usb_serial_port *port = to_usb_serial_port(dev);
2724 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2726 return sprintf(buf, "%d\n", edge_port->bUartMode);
2729 static ssize_t store_uart_mode(struct device *dev,
2730 struct device_attribute *attr, const char *valbuf, size_t count)
2732 struct usb_serial_port *port = to_usb_serial_port(dev);
2733 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2734 unsigned int v = simple_strtoul(valbuf, NULL, 0);
2736 dbg("%s: setting uart_mode = %d", __func__, v);
2739 edge_port->bUartMode = v;
2741 dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
2746 static DEVICE_ATTR(uart_mode, S_IWUSR | S_IRUGO, show_uart_mode,
2749 static int edge_create_sysfs_attrs(struct usb_serial_port *port)
2751 return device_create_file(&port->dev, &dev_attr_uart_mode);
2754 static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
2756 device_remove_file(&port->dev, &dev_attr_uart_mode);
2761 static struct usb_serial_driver edgeport_1port_device = {
2763 .owner = THIS_MODULE,
2764 .name = "edgeport_ti_1",
2766 .description = "Edgeport TI 1 port adapter",
2767 .usb_driver = &io_driver,
2768 .id_table = edgeport_1port_id_table,
2771 .close = edge_close,
2772 .throttle = edge_throttle,
2773 .unthrottle = edge_unthrottle,
2774 .attach = edge_startup,
2775 .disconnect = edge_disconnect,
2776 .release = edge_release,
2777 .port_probe = edge_create_sysfs_attrs,
2778 .port_remove = edge_remove_sysfs_attrs,
2779 .ioctl = edge_ioctl,
2780 .set_termios = edge_set_termios,
2781 .tiocmget = edge_tiocmget,
2782 .tiocmset = edge_tiocmset,
2783 .get_icount = edge_get_icount,
2784 .write = edge_write,
2785 .write_room = edge_write_room,
2786 .chars_in_buffer = edge_chars_in_buffer,
2787 .break_ctl = edge_break,
2788 .read_int_callback = edge_interrupt_callback,
2789 .read_bulk_callback = edge_bulk_in_callback,
2790 .write_bulk_callback = edge_bulk_out_callback,
2793 static struct usb_serial_driver edgeport_2port_device = {
2795 .owner = THIS_MODULE,
2796 .name = "edgeport_ti_2",
2798 .description = "Edgeport TI 2 port adapter",
2799 .usb_driver = &io_driver,
2800 .id_table = edgeport_2port_id_table,
2803 .close = edge_close,
2804 .throttle = edge_throttle,
2805 .unthrottle = edge_unthrottle,
2806 .attach = edge_startup,
2807 .disconnect = edge_disconnect,
2808 .release = edge_release,
2809 .port_probe = edge_create_sysfs_attrs,
2810 .port_remove = edge_remove_sysfs_attrs,
2811 .ioctl = edge_ioctl,
2812 .set_termios = edge_set_termios,
2813 .tiocmget = edge_tiocmget,
2814 .tiocmset = edge_tiocmset,
2815 .get_icount = edge_get_icount,
2816 .write = edge_write,
2817 .write_room = edge_write_room,
2818 .chars_in_buffer = edge_chars_in_buffer,
2819 .break_ctl = edge_break,
2820 .read_int_callback = edge_interrupt_callback,
2821 .read_bulk_callback = edge_bulk_in_callback,
2822 .write_bulk_callback = edge_bulk_out_callback,
2826 static int __init edgeport_init(void)
2829 retval = usb_serial_register(&edgeport_1port_device);
2831 goto failed_1port_device_register;
2832 retval = usb_serial_register(&edgeport_2port_device);
2834 goto failed_2port_device_register;
2835 retval = usb_register(&io_driver);
2837 goto failed_usb_register;
2838 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
2841 failed_usb_register:
2842 usb_serial_deregister(&edgeport_2port_device);
2843 failed_2port_device_register:
2844 usb_serial_deregister(&edgeport_1port_device);
2845 failed_1port_device_register:
2849 static void __exit edgeport_exit(void)
2851 usb_deregister(&io_driver);
2852 usb_serial_deregister(&edgeport_1port_device);
2853 usb_serial_deregister(&edgeport_2port_device);
2856 module_init(edgeport_init);
2857 module_exit(edgeport_exit);
2859 /* Module information */
2860 MODULE_AUTHOR(DRIVER_AUTHOR);
2861 MODULE_DESCRIPTION(DRIVER_DESC);
2862 MODULE_LICENSE("GPL");
2863 MODULE_FIRMWARE("edgeport/down3.bin");
2865 module_param(debug, bool, S_IRUGO | S_IWUSR);
2866 MODULE_PARM_DESC(debug, "Debug enabled or not");
2868 module_param(closing_wait, int, S_IRUGO | S_IWUSR);
2869 MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
2871 module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
2872 MODULE_PARM_DESC(ignore_cpu_rev,
2873 "Ignore the cpu revision when connecting to a device");
2875 module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
2876 MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");