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/init.h>
24 #include <linux/slab.h>
25 #include <linux/tty.h>
26 #include <linux/tty_driver.h>
27 #include <linux/tty_flip.h>
28 #include <linux/module.h>
29 #include <linux/spinlock.h>
30 #include <linux/mutex.h>
31 #include <linux/serial.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"
46 #define DRIVER_VERSION "v0.7mode043006"
47 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
48 #define DRIVER_DESC "Edgeport USB Serial Driver"
50 #define EPROM_PAGE_SIZE 64
53 /* different hardware types */
54 #define HARDWARE_TYPE_930 0
55 #define HARDWARE_TYPE_TIUMP 1
57 /* IOCTL_PRIVATE_TI_GET_MODE Definitions */
58 #define TI_MODE_CONFIGURING 0 /* Device has not entered start device */
59 #define TI_MODE_BOOT 1 /* Staying in boot mode */
60 #define TI_MODE_DOWNLOAD 2 /* Made it to download mode */
61 #define TI_MODE_TRANSITIONING 3 /* Currently in boot mode but
62 transitioning to download mode */
65 #define EDGE_READ_URB_RUNNING 0
66 #define EDGE_READ_URB_STOPPING 1
67 #define EDGE_READ_URB_STOPPED 2
69 #define EDGE_CLOSING_WAIT 4000 /* in .01 sec */
71 #define EDGE_OUT_BUF_SIZE 1024
74 /* Product information read from the Edgeport */
76 int TiMode; /* Current TI Mode */
77 __u8 hardware_type; /* Type of hardware */
78 } __attribute__((packed));
80 struct edgeport_port {
87 __u32 ump_read_timeout; /* Number of milliseconds the UMP will
88 wait without data before completing
93 struct async_icount icount;
94 wait_queue_head_t delta_msr_wait; /* for handling sleeping while
95 waiting for msr change to
97 struct edgeport_serial *edge_serial;
98 struct usb_serial_port *port;
99 __u8 bUartMode; /* Port type, 0: RS232, etc. */
101 int ep_read_urb_state;
102 int ep_write_urb_in_use;
103 struct kfifo write_fifo;
106 struct edgeport_serial {
107 struct product_info product_info;
108 u8 TI_I2C_Type; /* Type of I2C in UMP */
109 u8 TiReadI2C; /* Set to TRUE if we have read the
111 struct mutex es_lock;
113 struct usb_serial *serial;
117 /* Devices that this driver supports */
118 static const struct usb_device_id edgeport_1port_id_table[] = {
119 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
120 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
121 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
122 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
123 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
124 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
125 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
126 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
127 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
128 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
129 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
130 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
131 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
132 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
133 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
134 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
138 static const struct usb_device_id edgeport_2port_id_table[] = {
139 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
140 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
141 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
142 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
143 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
144 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
145 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
146 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
147 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
148 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
149 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
150 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
151 /* The 4, 8 and 16 port devices show up as multiple 2 port devices */
152 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
153 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
154 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
155 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
156 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
160 /* Devices that this driver supports */
161 static const struct usb_device_id id_table_combined[] = {
162 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
163 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
164 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
165 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
166 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
167 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
168 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
169 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
170 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
171 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
172 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
173 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
174 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
175 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
176 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
177 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
178 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
179 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
180 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
181 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
182 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
183 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
184 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
185 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
186 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
187 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
188 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
189 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
190 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
191 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
192 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
193 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
194 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
198 MODULE_DEVICE_TABLE(usb, id_table_combined);
200 static struct usb_driver io_driver = {
202 .id_table = id_table_combined,
206 static unsigned char OperationalMajorVersion;
207 static unsigned char OperationalMinorVersion;
208 static unsigned short OperationalBuildNumber;
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 device *dev, struct tty_struct *tty,
217 unsigned char *data, int length);
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 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 static int ti_vread_sync(struct usb_device *dev, __u8 request,
232 __u16 value, __u16 index, u8 *data, int size)
236 status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
237 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
238 value, index, data, size, 1000);
241 if (status != size) {
242 dbg("%s - wanted to write %d, but only wrote %d",
243 __func__, size, status);
249 static int ti_vsend_sync(struct usb_device *dev, __u8 request,
250 __u16 value, __u16 index, u8 *data, int size)
254 status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
255 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
256 value, index, data, size, 1000);
259 if (status != size) {
260 dbg("%s - wanted to write %d, but only wrote %d",
261 __func__, size, status);
267 static int send_cmd(struct usb_device *dev, __u8 command,
268 __u8 moduleid, __u16 value, u8 *data,
271 return ti_vsend_sync(dev, command, value, moduleid, data, size);
274 /* clear tx/rx buffers and fifo in TI UMP */
275 static int purge_port(struct usb_serial_port *port, __u16 mask)
277 int port_number = port->number - port->serial->minor;
279 dbg("%s - port %d, mask %x", __func__, port_number, mask);
281 return send_cmd(port->serial->dev,
283 (__u8)(UMPM_UART1_PORT + port_number),
290 * read_download_mem - Read edgeport memory from TI chip
291 * @dev: usb device pointer
292 * @start_address: Device CPU address at which to read
293 * @length: Length of above data
294 * @address_type: Can read both XDATA and I2C
295 * @buffer: pointer to input data buffer
297 static int read_download_mem(struct usb_device *dev, int start_address,
298 int length, __u8 address_type, __u8 *buffer)
302 __be16 be_start_address;
304 dbg("%s - @ %x for %d", __func__, start_address, length);
306 /* Read in blocks of 64 bytes
307 * (TI firmware can't handle more than 64 byte reads)
313 read_length = (__u8)length;
315 if (read_length > 1) {
316 dbg("%s - @ %x for %d", __func__,
317 start_address, read_length);
319 be_start_address = cpu_to_be16(start_address);
320 status = ti_vread_sync(dev, UMPC_MEMORY_READ,
322 (__force __u16)be_start_address,
323 buffer, read_length);
326 dbg("%s - ERROR %x", __func__, status);
331 usb_serial_debug_data(debug, &dev->dev, __func__,
332 read_length, buffer);
334 /* Update pointers/length */
335 start_address += read_length;
336 buffer += read_length;
337 length -= read_length;
343 static int read_ram(struct usb_device *dev, int start_address,
344 int length, __u8 *buffer)
346 return read_download_mem(dev, start_address, length,
347 DTK_ADDR_SPACE_XDATA, buffer);
350 /* Read edgeport memory to a given block */
351 static int read_boot_mem(struct edgeport_serial *serial,
352 int start_address, int length, __u8 *buffer)
357 for (i = 0; i < length; i++) {
358 status = ti_vread_sync(serial->serial->dev,
359 UMPC_MEMORY_READ, serial->TI_I2C_Type,
360 (__u16)(start_address+i), &buffer[i], 0x01);
362 dbg("%s - ERROR %x", __func__, status);
367 dbg("%s - start_address = %x, length = %d",
368 __func__, start_address, length);
369 usb_serial_debug_data(debug, &serial->serial->dev->dev,
370 __func__, length, buffer);
372 serial->TiReadI2C = 1;
377 /* Write given block to TI EPROM memory */
378 static int write_boot_mem(struct edgeport_serial *serial,
379 int start_address, int length, __u8 *buffer)
385 /* Must do a read before write */
386 if (!serial->TiReadI2C) {
387 temp = kmalloc(1, GFP_KERNEL);
389 dev_err(&serial->serial->dev->dev,
390 "%s - out of memory\n", __func__);
393 status = read_boot_mem(serial, 0, 1, temp);
399 for (i = 0; i < length; ++i) {
400 status = ti_vsend_sync(serial->serial->dev,
401 UMPC_MEMORY_WRITE, buffer[i],
402 (__u16)(i + start_address), NULL, 0);
407 dbg("%s - start_sddr = %x, length = %d",
408 __func__, start_address, length);
409 usb_serial_debug_data(debug, &serial->serial->dev->dev,
410 __func__, length, buffer);
416 /* Write edgeport I2C memory to TI chip */
417 static int write_i2c_mem(struct edgeport_serial *serial,
418 int start_address, int length, __u8 address_type, __u8 *buffer)
422 __be16 be_start_address;
424 /* We can only send a maximum of 1 aligned byte page at a time */
426 /* calculate the number of bytes left in the first page */
427 write_length = EPROM_PAGE_SIZE -
428 (start_address & (EPROM_PAGE_SIZE - 1));
430 if (write_length > length)
431 write_length = length;
433 dbg("%s - BytesInFirstPage Addr = %x, length = %d",
434 __func__, start_address, write_length);
435 usb_serial_debug_data(debug, &serial->serial->dev->dev,
436 __func__, write_length, buffer);
438 /* Write first page */
439 be_start_address = cpu_to_be16(start_address);
440 status = ti_vsend_sync(serial->serial->dev,
441 UMPC_MEMORY_WRITE, (__u16)address_type,
442 (__force __u16)be_start_address,
443 buffer, write_length);
445 dbg("%s - ERROR %d", __func__, status);
449 length -= write_length;
450 start_address += write_length;
451 buffer += write_length;
453 /* We should be aligned now -- can write
454 max page size bytes at a time */
456 if (length > EPROM_PAGE_SIZE)
457 write_length = EPROM_PAGE_SIZE;
459 write_length = length;
461 dbg("%s - Page Write Addr = %x, length = %d",
462 __func__, start_address, write_length);
463 usb_serial_debug_data(debug, &serial->serial->dev->dev,
464 __func__, write_length, buffer);
466 /* Write next page */
467 be_start_address = cpu_to_be16(start_address);
468 status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
470 (__force __u16)be_start_address,
471 buffer, write_length);
473 dev_err(&serial->serial->dev->dev, "%s - ERROR %d\n",
478 length -= write_length;
479 start_address += write_length;
480 buffer += write_length;
485 /* Examine the UMP DMA registers and LSR
487 * Check the MSBit of the X and Y DMA byte count registers.
488 * A zero in this bit indicates that the TX DMA buffers are empty
489 * then check the TX Empty bit in the UART.
491 static int tx_active(struct edgeport_port *port)
494 struct out_endpoint_desc_block *oedb;
498 oedb = kmalloc(sizeof(*oedb), GFP_KERNEL);
500 dev_err(&port->port->dev, "%s - out of memory\n", __func__);
504 lsr = kmalloc(1, GFP_KERNEL); /* Sigh, that's right, just one byte,
505 as not all platforms can do DMA
511 /* Read the DMA Count Registers */
512 status = read_ram(port->port->serial->dev, port->dma_address,
513 sizeof(*oedb), (void *)oedb);
515 goto exit_is_tx_active;
517 dbg("%s - XByteCount 0x%X", __func__, oedb->XByteCount);
520 status = read_ram(port->port->serial->dev,
521 port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr);
524 goto exit_is_tx_active;
525 dbg("%s - LSR = 0x%X", __func__, *lsr);
527 /* If either buffer has data or we are transmitting then return TRUE */
528 if ((oedb->XByteCount & 0x80) != 0)
531 if ((*lsr & UMP_UART_LSR_TX_MASK) == 0)
534 /* We return Not Active if we get any kind of error */
536 dbg("%s - return %d", __func__, bytes_left);
543 static void chase_port(struct edgeport_port *port, unsigned long timeout,
547 struct tty_struct *tty = tty_port_tty_get(&port->port->port);
548 struct usb_serial *serial = port->port->serial;
553 timeout = (HZ * EDGE_CLOSING_WAIT)/100;
555 /* wait for data to drain from the buffer */
556 spin_lock_irqsave(&port->ep_lock, flags);
557 init_waitqueue_entry(&wait, current);
558 add_wait_queue(&tty->write_wait, &wait);
560 set_current_state(TASK_INTERRUPTIBLE);
561 if (kfifo_len(&port->write_fifo) == 0
562 || timeout == 0 || signal_pending(current)
563 || serial->disconnected)
566 spin_unlock_irqrestore(&port->ep_lock, flags);
567 timeout = schedule_timeout(timeout);
568 spin_lock_irqsave(&port->ep_lock, flags);
570 set_current_state(TASK_RUNNING);
571 remove_wait_queue(&tty->write_wait, &wait);
573 kfifo_reset_out(&port->write_fifo);
574 spin_unlock_irqrestore(&port->ep_lock, flags);
577 /* wait for data to drain from the device */
579 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
580 && !serial->disconnected) {
581 /* not disconnected */
582 if (!tx_active(port))
588 if (serial->disconnected)
591 /* wait one more character time, based on baud rate */
592 /* (tx_active doesn't seem to wait for the last byte) */
593 baud_rate = port->baud_rate;
596 msleep(max(1, DIV_ROUND_UP(10000, baud_rate)));
599 static int choose_config(struct usb_device *dev)
602 * There may be multiple configurations on this device, in which case
603 * we would need to read and parse all of them to find out which one
604 * we want. However, we just support one config at this point,
605 * configuration # 1, which is Config Descriptor 0.
608 dbg("%s - Number of Interfaces = %d",
609 __func__, dev->config->desc.bNumInterfaces);
610 dbg("%s - MAX Power = %d",
611 __func__, dev->config->desc.bMaxPower * 2);
613 if (dev->config->desc.bNumInterfaces != 1) {
614 dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n",
622 static int read_rom(struct edgeport_serial *serial,
623 int start_address, int length, __u8 *buffer)
627 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
628 status = read_download_mem(serial->serial->dev,
634 status = read_boot_mem(serial, start_address, length,
640 static int write_rom(struct edgeport_serial *serial, int start_address,
641 int length, __u8 *buffer)
643 if (serial->product_info.TiMode == TI_MODE_BOOT)
644 return write_boot_mem(serial, start_address, length,
647 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
648 return write_i2c_mem(serial, start_address, length,
649 serial->TI_I2C_Type, buffer);
655 /* Read a descriptor header from I2C based on type */
656 static int get_descriptor_addr(struct edgeport_serial *serial,
657 int desc_type, struct ti_i2c_desc *rom_desc)
662 /* Search for requested descriptor in I2C */
665 status = read_rom(serial,
667 sizeof(struct ti_i2c_desc),
672 if (rom_desc->Type == desc_type)
673 return start_address;
675 start_address = start_address + sizeof(struct ti_i2c_desc)
678 } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
683 /* Validate descriptor checksum */
684 static int valid_csum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
689 for (i = 0; i < rom_desc->Size; i++)
690 cs = (__u8)(cs + buffer[i]);
692 if (cs != rom_desc->CheckSum) {
693 dbg("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
699 /* Make sure that the I2C image is good */
700 static int check_i2c_image(struct edgeport_serial *serial)
702 struct device *dev = &serial->serial->dev->dev;
704 struct ti_i2c_desc *rom_desc;
705 int start_address = 2;
709 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
711 dev_err(dev, "%s - out of memory\n", __func__);
714 buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL);
716 dev_err(dev, "%s - out of memory when allocating buffer\n",
722 /* Read the first byte (Signature0) must be 0x52 or 0x10 */
723 status = read_rom(serial, 0, 1, buffer);
727 if (*buffer != UMP5152 && *buffer != UMP3410) {
728 dev_err(dev, "%s - invalid buffer signature\n", __func__);
734 /* Validate the I2C */
735 status = read_rom(serial,
737 sizeof(struct ti_i2c_desc),
742 if ((start_address + sizeof(struct ti_i2c_desc) +
743 rom_desc->Size) > TI_MAX_I2C_SIZE) {
745 dbg("%s - structure too big, erroring out.", __func__);
749 dbg("%s Type = 0x%x", __func__, rom_desc->Type);
751 /* Skip type 2 record */
752 ttype = rom_desc->Type & 0x0f;
753 if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
754 && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) {
755 /* Read the descriptor data */
756 status = read_rom(serial, start_address +
757 sizeof(struct ti_i2c_desc),
758 rom_desc->Size, buffer);
762 status = valid_csum(rom_desc, buffer);
766 start_address = start_address + sizeof(struct ti_i2c_desc) +
769 } while ((rom_desc->Type != I2C_DESC_TYPE_ION) &&
770 (start_address < TI_MAX_I2C_SIZE));
772 if ((rom_desc->Type != I2C_DESC_TYPE_ION) ||
773 (start_address > TI_MAX_I2C_SIZE))
782 static int get_manuf_info(struct edgeport_serial *serial, __u8 *buffer)
786 struct ti_i2c_desc *rom_desc;
787 struct edge_ti_manuf_descriptor *desc;
789 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
791 dev_err(&serial->serial->dev->dev, "%s - out of memory\n",
795 start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
798 if (!start_address) {
799 dbg("%s - Edge Descriptor not found in I2C", __func__);
804 /* Read the descriptor data */
805 status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc),
806 rom_desc->Size, buffer);
810 status = valid_csum(rom_desc, buffer);
812 desc = (struct edge_ti_manuf_descriptor *)buffer;
813 dbg("%s - IonConfig 0x%x", __func__, desc->IonConfig);
814 dbg("%s - Version %d", __func__, desc->Version);
815 dbg("%s - Cpu/Board 0x%x", __func__, desc->CpuRev_BoardRev);
816 dbg("%s - NumPorts %d", __func__, desc->NumPorts);
817 dbg("%s - NumVirtualPorts %d", __func__, desc->NumVirtualPorts);
818 dbg("%s - TotalPorts %d", __func__, desc->TotalPorts);
825 /* Build firmware header used for firmware update */
826 static int build_i2c_fw_hdr(__u8 *header, struct device *dev)
833 struct ti_i2c_desc *i2c_header;
834 struct ti_i2c_image_header *img_header;
835 struct ti_i2c_firmware_rec *firmware_rec;
836 const struct firmware *fw;
837 const char *fw_name = "edgeport/down3.bin";
839 /* In order to update the I2C firmware we must change the type 2 record
840 * to type 0xF2. This will force the UMP to come up in Boot Mode.
841 * Then while in boot mode, the driver will download the latest
842 * firmware (padded to 15.5k) into the UMP ram. And finally when the
843 * device comes back up in download mode the driver will cause the new
844 * firmware to be copied from the UMP Ram to I2C and the firmware will
845 * update the record type from 0xf2 to 0x02.
848 /* Allocate a 15.5k buffer + 2 bytes for version number
849 * (Firmware Record) */
850 buffer_size = (((1024 * 16) - 512 ) +
851 sizeof(struct ti_i2c_firmware_rec));
853 buffer = kmalloc(buffer_size, GFP_KERNEL);
855 dev_err(dev, "%s - out of memory\n", __func__);
859 // Set entire image of 0xffs
860 memset(buffer, 0xff, buffer_size);
862 err = request_firmware(&fw, fw_name, dev);
864 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
870 /* Save Download Version Number */
871 OperationalMajorVersion = fw->data[0];
872 OperationalMinorVersion = fw->data[1];
873 OperationalBuildNumber = fw->data[2] | (fw->data[3] << 8);
875 /* Copy version number into firmware record */
876 firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
878 firmware_rec->Ver_Major = OperationalMajorVersion;
879 firmware_rec->Ver_Minor = OperationalMinorVersion;
881 /* Pointer to fw_down memory image */
882 img_header = (struct ti_i2c_image_header *)&fw->data[4];
884 memcpy(buffer + sizeof(struct ti_i2c_firmware_rec),
885 &fw->data[4 + sizeof(struct ti_i2c_image_header)],
886 le16_to_cpu(img_header->Length));
888 release_firmware(fw);
890 for (i=0; i < buffer_size; i++) {
891 cs = (__u8)(cs + buffer[i]);
896 /* Build new header */
897 i2c_header = (struct ti_i2c_desc *)header;
898 firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data;
900 i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK;
901 i2c_header->Size = (__u16)buffer_size;
902 i2c_header->CheckSum = cs;
903 firmware_rec->Ver_Major = OperationalMajorVersion;
904 firmware_rec->Ver_Minor = OperationalMinorVersion;
909 /* Try to figure out what type of I2c we have */
910 static int i2c_type_bootmode(struct edgeport_serial *serial)
915 data = kmalloc(1, GFP_KERNEL);
917 dev_err(&serial->serial->dev->dev,
918 "%s - out of memory\n", __func__);
922 /* Try to read type 2 */
923 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
924 DTK_ADDR_SPACE_I2C_TYPE_II, 0, data, 0x01);
926 dbg("%s - read 2 status error = %d", __func__, status);
928 dbg("%s - read 2 data = 0x%x", __func__, *data);
929 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
930 dbg("%s - ROM_TYPE_II", __func__);
931 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
935 /* Try to read type 3 */
936 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
937 DTK_ADDR_SPACE_I2C_TYPE_III, 0, data, 0x01);
939 dbg("%s - read 3 status error = %d", __func__, status);
941 dbg("%s - read 2 data = 0x%x", __func__, *data);
942 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
943 dbg("%s - ROM_TYPE_III", __func__);
944 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
948 dbg("%s - Unknown", __func__);
949 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
956 static int bulk_xfer(struct usb_serial *serial, void *buffer,
957 int length, int *num_sent)
961 status = usb_bulk_msg(serial->dev,
962 usb_sndbulkpipe(serial->dev,
963 serial->port[0]->bulk_out_endpointAddress),
964 buffer, length, num_sent, 1000);
968 /* Download given firmware image to the device (IN BOOT MODE) */
969 static int download_code(struct edgeport_serial *serial, __u8 *image,
977 /* Transfer firmware image */
978 for (pos = 0; pos < image_length; ) {
979 /* Read the next buffer from file */
980 transfer = image_length - pos;
981 if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
982 transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
985 status = bulk_xfer(serial->serial, &image[pos],
989 /* Advance buffer pointer */
997 static int config_boot_dev(struct usb_device *dev)
1002 static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc)
1004 return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev);
1008 * DownloadTIFirmware - Download run-time operating firmware to the TI5052
1010 * This routine downloads the main operating code into the TI5052, using the
1011 * boot code already burned into E2PROM or ROM.
1013 static int download_fw(struct edgeport_serial *serial)
1015 struct device *dev = &serial->serial->dev->dev;
1018 struct edge_ti_manuf_descriptor *ti_manuf_desc;
1019 struct usb_interface_descriptor *interface;
1020 int download_cur_ver;
1021 int download_new_ver;
1023 /* This routine is entered by both the BOOT mode and the Download mode
1024 * We can determine which code is running by the reading the config
1025 * descriptor and if we have only one bulk pipe it is in boot mode
1027 serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
1029 /* Default to type 2 i2c */
1030 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1032 status = choose_config(serial->serial->dev);
1036 interface = &serial->serial->interface->cur_altsetting->desc;
1038 dev_err(dev, "%s - no interface set, error!\n", __func__);
1043 * Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
1044 * if we have more than one endpoint we are definitely in download
1047 if (interface->bNumEndpoints > 1)
1048 serial->product_info.TiMode = TI_MODE_DOWNLOAD;
1050 /* Otherwise we will remain in configuring mode */
1051 serial->product_info.TiMode = TI_MODE_CONFIGURING;
1053 /********************************************************************/
1055 /********************************************************************/
1056 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
1057 struct ti_i2c_desc *rom_desc;
1059 dbg("%s - RUNNING IN DOWNLOAD MODE", __func__);
1061 status = check_i2c_image(serial);
1063 dbg("%s - DOWNLOAD MODE -- BAD I2C", __func__);
1067 /* Validate Hardware version number
1068 * Read Manufacturing Descriptor from TI Based Edgeport
1070 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
1071 if (!ti_manuf_desc) {
1072 dev_err(dev, "%s - out of memory.\n", __func__);
1075 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
1077 kfree(ti_manuf_desc);
1081 /* Check version number of ION descriptor */
1082 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1083 dbg("%s - Wrong CPU Rev %d (Must be 2)",
1084 __func__, ti_cpu_rev(ti_manuf_desc));
1085 kfree(ti_manuf_desc);
1089 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
1091 dev_err(dev, "%s - out of memory.\n", __func__);
1092 kfree(ti_manuf_desc);
1096 /* Search for type 2 record (firmware record) */
1097 start_address = get_descriptor_addr(serial,
1098 I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc);
1099 if (start_address != 0) {
1100 struct ti_i2c_firmware_rec *firmware_version;
1103 dbg("%s - Found Type FIRMWARE (Type 2) record",
1106 firmware_version = kmalloc(sizeof(*firmware_version),
1108 if (!firmware_version) {
1109 dev_err(dev, "%s - out of memory.\n", __func__);
1111 kfree(ti_manuf_desc);
1115 /* Validate version number
1116 * Read the descriptor data
1118 status = read_rom(serial, start_address +
1119 sizeof(struct ti_i2c_desc),
1120 sizeof(struct ti_i2c_firmware_rec),
1121 (__u8 *)firmware_version);
1123 kfree(firmware_version);
1125 kfree(ti_manuf_desc);
1129 /* Check version number of download with current
1131 download_cur_ver = (firmware_version->Ver_Major << 8) +
1132 (firmware_version->Ver_Minor);
1133 download_new_ver = (OperationalMajorVersion << 8) +
1134 (OperationalMinorVersion);
1136 dbg("%s - >> FW Versions Device %d.%d Driver %d.%d",
1138 firmware_version->Ver_Major,
1139 firmware_version->Ver_Minor,
1140 OperationalMajorVersion,
1141 OperationalMinorVersion);
1143 /* Check if we have an old version in the I2C and
1144 update if necessary */
1145 if (download_cur_ver < download_new_ver) {
1146 dbg("%s - Update I2C dld from %d.%d to %d.%d",
1148 firmware_version->Ver_Major,
1149 firmware_version->Ver_Minor,
1150 OperationalMajorVersion,
1151 OperationalMinorVersion);
1153 record = kmalloc(1, GFP_KERNEL);
1155 dev_err(dev, "%s - out of memory.\n",
1157 kfree(firmware_version);
1159 kfree(ti_manuf_desc);
1162 /* In order to update the I2C firmware we must
1163 * change the type 2 record to type 0xF2. This
1164 * will force the UMP to come up in Boot Mode.
1165 * Then while in boot mode, the driver will
1166 * download the latest firmware (padded to
1167 * 15.5k) into the UMP ram. Finally when the
1168 * device comes back up in download mode the
1169 * driver will cause the new firmware to be
1170 * copied from the UMP Ram to I2C and the
1171 * firmware will update the record type from
1174 *record = I2C_DESC_TYPE_FIRMWARE_BLANK;
1176 /* Change the I2C Firmware record type to
1177 0xf2 to trigger an update */
1178 status = write_rom(serial, start_address,
1179 sizeof(*record), record);
1182 kfree(firmware_version);
1184 kfree(ti_manuf_desc);
1188 /* verify the write -- must do this in order
1189 * for write to complete before we do the
1192 status = read_rom(serial,
1198 kfree(firmware_version);
1200 kfree(ti_manuf_desc);
1204 if (*record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
1206 "%s - error resetting device\n",
1209 kfree(firmware_version);
1211 kfree(ti_manuf_desc);
1215 dbg("%s - HARDWARE RESET", __func__);
1217 /* Reset UMP -- Back to BOOT MODE */
1218 status = ti_vsend_sync(serial->serial->dev,
1219 UMPC_HARDWARE_RESET,
1222 dbg("%s - HARDWARE RESET return %d",
1225 /* return an error on purpose. */
1227 kfree(firmware_version);
1229 kfree(ti_manuf_desc);
1232 kfree(firmware_version);
1234 /* Search for type 0xF2 record (firmware blank record) */
1235 else if ((start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
1236 #define HEADER_SIZE (sizeof(struct ti_i2c_desc) + \
1237 sizeof(struct ti_i2c_firmware_rec))
1241 header = kmalloc(HEADER_SIZE, GFP_KERNEL);
1243 dev_err(dev, "%s - out of memory.\n", __func__);
1245 kfree(ti_manuf_desc);
1249 vheader = kmalloc(HEADER_SIZE, GFP_KERNEL);
1251 dev_err(dev, "%s - out of memory.\n", __func__);
1254 kfree(ti_manuf_desc);
1258 dbg("%s - Found Type BLANK FIRMWARE (Type F2) record",
1262 * In order to update the I2C firmware we must change
1263 * the type 2 record to type 0xF2. This will force the
1264 * UMP to come up in Boot Mode. Then while in boot
1265 * mode, the driver will download the latest firmware
1266 * (padded to 15.5k) into the UMP ram. Finally when the
1267 * device comes back up in download mode the driver
1268 * will cause the new firmware to be copied from the
1269 * UMP Ram to I2C and the firmware will update the
1270 * record type from 0xf2 to 0x02.
1272 status = build_i2c_fw_hdr(header, dev);
1277 kfree(ti_manuf_desc);
1281 /* Update I2C with type 0xf2 record with correct
1282 size and checksum */
1283 status = write_rom(serial,
1291 kfree(ti_manuf_desc);
1295 /* verify the write -- must do this in order for
1296 write to complete before we do the hardware reset */
1297 status = read_rom(serial, start_address,
1298 HEADER_SIZE, vheader);
1301 dbg("%s - can't read header back", __func__);
1305 kfree(ti_manuf_desc);
1308 if (memcmp(vheader, header, HEADER_SIZE)) {
1309 dbg("%s - write download record failed",
1314 kfree(ti_manuf_desc);
1321 dbg("%s - Start firmware update", __func__);
1323 /* Tell firmware to copy download image into I2C */
1324 status = ti_vsend_sync(serial->serial->dev,
1325 UMPC_COPY_DNLD_TO_I2C, 0, 0, NULL, 0);
1327 dbg("%s - Update complete 0x%x", __func__, status);
1330 "%s - UMPC_COPY_DNLD_TO_I2C failed\n",
1333 kfree(ti_manuf_desc);
1338 // The device is running the download code
1340 kfree(ti_manuf_desc);
1344 /********************************************************************/
1346 /********************************************************************/
1347 dbg("%s - RUNNING IN BOOT MODE", __func__);
1349 /* Configure the TI device so we can use the BULK pipes for download */
1350 status = config_boot_dev(serial->serial->dev);
1354 if (le16_to_cpu(serial->serial->dev->descriptor.idVendor)
1355 != USB_VENDOR_ID_ION) {
1356 dbg("%s - VID = 0x%x", __func__,
1357 le16_to_cpu(serial->serial->dev->descriptor.idVendor));
1358 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1359 goto stayinbootmode;
1362 /* We have an ION device (I2c Must be programmed)
1363 Determine I2C image type */
1364 if (i2c_type_bootmode(serial))
1365 goto stayinbootmode;
1367 /* Check for ION Vendor ID and that the I2C is valid */
1368 if (!check_i2c_image(serial)) {
1369 struct ti_i2c_image_header *header;
1375 const struct firmware *fw;
1376 const char *fw_name = "edgeport/down3.bin";
1378 /* Validate Hardware version number
1379 * Read Manufacturing Descriptor from TI Based Edgeport
1381 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
1382 if (!ti_manuf_desc) {
1383 dev_err(dev, "%s - out of memory.\n", __func__);
1386 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
1388 kfree(ti_manuf_desc);
1389 goto stayinbootmode;
1392 /* Check for version 2 */
1393 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1394 dbg("%s - Wrong CPU Rev %d (Must be 2)",
1395 __func__, ti_cpu_rev(ti_manuf_desc));
1396 kfree(ti_manuf_desc);
1397 goto stayinbootmode;
1400 kfree(ti_manuf_desc);
1403 * In order to update the I2C firmware we must change the type
1404 * 2 record to type 0xF2. This will force the UMP to come up
1405 * in Boot Mode. Then while in boot mode, the driver will
1406 * download the latest firmware (padded to 15.5k) into the
1407 * UMP ram. Finally when the device comes back up in download
1408 * mode the driver will cause the new firmware to be copied
1409 * from the UMP Ram to I2C and the firmware will update the
1410 * record type from 0xf2 to 0x02.
1412 * Do we really have to copy the whole firmware image,
1413 * or could we do this in place!
1416 /* Allocate a 15.5k buffer + 3 byte header */
1417 buffer_size = (((1024 * 16) - 512) +
1418 sizeof(struct ti_i2c_image_header));
1419 buffer = kmalloc(buffer_size, GFP_KERNEL);
1421 dev_err(dev, "%s - out of memory\n", __func__);
1425 /* Initialize the buffer to 0xff (pad the buffer) */
1426 memset(buffer, 0xff, buffer_size);
1428 err = request_firmware(&fw, fw_name, dev);
1430 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
1435 memcpy(buffer, &fw->data[4], fw->size - 4);
1436 release_firmware(fw);
1438 for (i = sizeof(struct ti_i2c_image_header);
1439 i < buffer_size; i++) {
1440 cs = (__u8)(cs + buffer[i]);
1443 header = (struct ti_i2c_image_header *)buffer;
1445 /* update length and checksum after padding */
1446 header->Length = cpu_to_le16((__u16)(buffer_size -
1447 sizeof(struct ti_i2c_image_header)));
1448 header->CheckSum = cs;
1450 /* Download the operational code */
1451 dbg("%s - Downloading operational code image (TI UMP)",
1453 status = download_code(serial, buffer, buffer_size);
1458 dbg("%s - Error downloading operational code image",
1463 /* Device will reboot */
1464 serial->product_info.TiMode = TI_MODE_TRANSITIONING;
1466 dbg("%s - Download successful -- Device rebooting...",
1469 /* return an error on purpose */
1474 /* Eprom is invalid or blank stay in boot mode */
1475 dbg("%s - STAYING IN BOOT MODE", __func__);
1476 serial->product_info.TiMode = TI_MODE_BOOT;
1482 static int ti_do_config(struct edgeport_port *port, int feature, int on)
1484 int port_number = port->port->number - port->port->serial->minor;
1485 on = !!on; /* 1 or 0 not bitmask */
1486 return send_cmd(port->port->serial->dev,
1487 feature, (__u8)(UMPM_UART1_PORT + port_number),
1492 static int restore_mcr(struct edgeport_port *port, __u8 mcr)
1496 dbg("%s - %x", __func__, mcr);
1498 status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR);
1501 status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS);
1504 return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK);
1507 /* Convert TI LSR to standard UART flags */
1508 static __u8 map_line_status(__u8 ti_lsr)
1512 #define MAP_FLAG(flagUmp, flagUart) \
1513 if (ti_lsr & flagUmp) \
1516 MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR) /* overrun */
1517 MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */
1518 MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */
1519 MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */
1520 MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* rx data available */
1521 MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* tx hold reg empty */
1528 static void handle_new_msr(struct edgeport_port *edge_port, __u8 msr)
1530 struct async_icount *icount;
1531 struct tty_struct *tty;
1533 dbg("%s - %02x", __func__, msr);
1535 if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
1536 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
1537 icount = &edge_port->icount;
1539 /* update input line counters */
1540 if (msr & EDGEPORT_MSR_DELTA_CTS)
1542 if (msr & EDGEPORT_MSR_DELTA_DSR)
1544 if (msr & EDGEPORT_MSR_DELTA_CD)
1546 if (msr & EDGEPORT_MSR_DELTA_RI)
1548 wake_up_interruptible(&edge_port->delta_msr_wait);
1551 /* Save the new modem status */
1552 edge_port->shadow_msr = msr & 0xf0;
1554 tty = tty_port_tty_get(&edge_port->port->port);
1555 /* handle CTS flow control */
1556 if (tty && C_CRTSCTS(tty)) {
1557 if (msr & EDGEPORT_MSR_CTS) {
1558 tty->hw_stopped = 0;
1561 tty->hw_stopped = 1;
1567 static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data,
1568 __u8 lsr, __u8 data)
1570 struct async_icount *icount;
1571 __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR |
1572 LSR_FRM_ERR | LSR_BREAK));
1573 struct tty_struct *tty;
1575 dbg("%s - %02x", __func__, new_lsr);
1577 edge_port->shadow_lsr = lsr;
1579 if (new_lsr & LSR_BREAK)
1581 * Parity and Framing errors only count if they
1582 * occur exclusive of a break being received.
1584 new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
1586 /* Place LSR data byte into Rx buffer */
1588 tty = tty_port_tty_get(&edge_port->port->port);
1590 edge_tty_recv(&edge_port->port->dev, tty, &data, 1);
1595 /* update input line counters */
1596 icount = &edge_port->icount;
1597 if (new_lsr & LSR_BREAK)
1599 if (new_lsr & LSR_OVER_ERR)
1601 if (new_lsr & LSR_PAR_ERR)
1603 if (new_lsr & LSR_FRM_ERR)
1608 static void edge_interrupt_callback(struct urb *urb)
1610 struct edgeport_serial *edge_serial = urb->context;
1611 struct usb_serial_port *port;
1612 struct edgeport_port *edge_port;
1613 unsigned char *data = urb->transfer_buffer;
1614 int length = urb->actual_length;
1620 int status = urb->status;
1622 dbg("%s", __func__);
1631 /* this urb is terminated, clean up */
1632 dbg("%s - urb shutting down with status: %d",
1636 dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
1637 "%d\n", __func__, status);
1642 dbg("%s - no data in urb", __func__);
1646 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev,
1647 __func__, length, data);
1650 dbg("%s - expecting packet of size 2, got %d",
1655 port_number = TIUMP_GET_PORT_FROM_CODE(data[0]);
1656 function = TIUMP_GET_FUNC_FROM_CODE(data[0]);
1657 dbg("%s - port_number %d, function %d, info 0x%x",
1658 __func__, port_number, function, data[1]);
1659 port = edge_serial->serial->port[port_number];
1660 edge_port = usb_get_serial_port_data(port);
1662 dbg("%s - edge_port not found", __func__);
1666 case TIUMP_INTERRUPT_CODE_LSR:
1667 lsr = map_line_status(data[1]);
1668 if (lsr & UMP_UART_LSR_DATA_MASK) {
1669 /* Save the LSR event for bulk read
1670 completion routine */
1671 dbg("%s - LSR Event Port %u LSR Status = %02x",
1672 __func__, port_number, lsr);
1673 edge_port->lsr_event = 1;
1674 edge_port->lsr_mask = lsr;
1676 dbg("%s - ===== Port %d LSR Status = %02x ======",
1677 __func__, port_number, lsr);
1678 handle_new_lsr(edge_port, 0, lsr, 0);
1682 case TIUMP_INTERRUPT_CODE_MSR: /* MSR */
1683 /* Copy MSR from UMP */
1685 dbg("%s - ===== Port %u MSR Status = %02x ======",
1686 __func__, port_number, msr);
1687 handle_new_msr(edge_port, msr);
1691 dev_err(&urb->dev->dev,
1692 "%s - Unknown Interrupt code from UMP %x\n",
1699 retval = usb_submit_urb(urb, GFP_ATOMIC);
1701 dev_err(&urb->dev->dev,
1702 "%s - usb_submit_urb failed with result %d\n",
1706 static void edge_bulk_in_callback(struct urb *urb)
1708 struct edgeport_port *edge_port = urb->context;
1709 unsigned char *data = urb->transfer_buffer;
1710 struct tty_struct *tty;
1713 int status = urb->status;
1715 dbg("%s", __func__);
1724 /* this urb is terminated, clean up */
1725 dbg("%s - urb shutting down with status: %d",
1729 dev_err(&urb->dev->dev,
1730 "%s - nonzero read bulk status received: %d\n",
1734 if (status == -EPIPE)
1738 dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__);
1742 port_number = edge_port->port->number - edge_port->port->serial->minor;
1744 if (edge_port->lsr_event) {
1745 edge_port->lsr_event = 0;
1746 dbg("%s ===== Port %u LSR Status = %02x, Data = %02x ======",
1747 __func__, port_number, edge_port->lsr_mask, *data);
1748 handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data);
1749 /* Adjust buffer length/pointer */
1750 --urb->actual_length;
1754 tty = tty_port_tty_get(&edge_port->port->port);
1755 if (tty && urb->actual_length) {
1756 usb_serial_debug_data(debug, &edge_port->port->dev,
1757 __func__, urb->actual_length, data);
1758 if (edge_port->close_pending)
1759 dbg("%s - close pending, dropping data on the floor",
1762 edge_tty_recv(&edge_port->port->dev, tty, data,
1763 urb->actual_length);
1764 edge_port->icount.rx += urb->actual_length;
1769 /* continue read unless stopped */
1770 spin_lock(&edge_port->ep_lock);
1771 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
1772 retval = usb_submit_urb(urb, GFP_ATOMIC);
1773 else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING)
1774 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
1776 spin_unlock(&edge_port->ep_lock);
1778 dev_err(&urb->dev->dev,
1779 "%s - usb_submit_urb failed with result %d\n",
1783 static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
1784 unsigned char *data, int length)
1788 queued = tty_insert_flip_string(tty, data, length);
1789 if (queued < length)
1790 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1791 __func__, length - queued);
1792 tty_flip_buffer_push(tty);
1795 static void edge_bulk_out_callback(struct urb *urb)
1797 struct usb_serial_port *port = urb->context;
1798 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1799 int status = urb->status;
1800 struct tty_struct *tty;
1802 dbg("%s - port %d", __func__, port->number);
1804 edge_port->ep_write_urb_in_use = 0;
1813 /* this urb is terminated, clean up */
1814 dbg("%s - urb shutting down with status: %d",
1818 dev_err_console(port, "%s - nonzero write bulk status "
1819 "received: %d\n", __func__, status);
1822 /* send any buffered data */
1823 tty = tty_port_tty_get(&port->port);
1828 static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
1830 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1831 struct edgeport_serial *edge_serial;
1832 struct usb_device *dev;
1837 u8 transaction_timeout;
1839 dbg("%s - port %d", __func__, port->number);
1841 if (edge_port == NULL)
1844 port_number = port->number - port->serial->minor;
1845 switch (port_number) {
1847 edge_port->uart_base = UMPMEM_BASE_UART1;
1848 edge_port->dma_address = UMPD_OEDB1_ADDRESS;
1851 edge_port->uart_base = UMPMEM_BASE_UART2;
1852 edge_port->dma_address = UMPD_OEDB2_ADDRESS;
1855 dev_err(&port->dev, "Unknown port number!!!\n");
1859 dbg("%s - port_number = %d, uart_base = %04x, dma_address = %04x",
1860 __func__, port_number, edge_port->uart_base,
1861 edge_port->dma_address);
1863 dev = port->serial->dev;
1865 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
1866 init_waitqueue_head(&edge_port->delta_msr_wait);
1868 /* turn off loopback */
1869 status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0);
1872 "%s - cannot send clear loopback command, %d\n",
1877 /* set up the port settings */
1879 edge_set_termios(tty, port, tty->termios);
1881 /* open up the port */
1883 /* milliseconds to timeout for DMA transfer */
1884 transaction_timeout = 2;
1886 edge_port->ump_read_timeout =
1887 max(20, ((transaction_timeout * 3) / 2));
1889 /* milliseconds to timeout for DMA transfer */
1890 open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
1891 UMP_PIPE_TRANS_TIMEOUT_ENA |
1892 (transaction_timeout << 2));
1894 dbg("%s - Sending UMPC_OPEN_PORT", __func__);
1896 /* Tell TI to open and start the port */
1897 status = send_cmd(dev, UMPC_OPEN_PORT,
1898 (u8)(UMPM_UART1_PORT + port_number), open_settings, NULL, 0);
1900 dev_err(&port->dev, "%s - cannot send open command, %d\n",
1905 /* Start the DMA? */
1906 status = send_cmd(dev, UMPC_START_PORT,
1907 (u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
1909 dev_err(&port->dev, "%s - cannot send start DMA command, %d\n",
1914 /* Clear TX and RX buffers in UMP */
1915 status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
1918 "%s - cannot send clear buffers command, %d\n",
1923 /* Read Initial MSR */
1924 status = ti_vread_sync(dev, UMPC_READ_MSR, 0,
1925 (__u16)(UMPM_UART1_PORT + port_number),
1926 &edge_port->shadow_msr, 1);
1928 dev_err(&port->dev, "%s - cannot send read MSR command, %d\n",
1933 dbg("ShadowMSR 0x%X", edge_port->shadow_msr);
1935 /* Set Initial MCR */
1936 edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
1937 dbg("ShadowMCR 0x%X", edge_port->shadow_mcr);
1939 edge_serial = edge_port->edge_serial;
1940 if (mutex_lock_interruptible(&edge_serial->es_lock))
1941 return -ERESTARTSYS;
1942 if (edge_serial->num_ports_open == 0) {
1943 /* we are the first port to open, post the interrupt urb */
1944 urb = edge_serial->serial->port[0]->interrupt_in_urb;
1947 "%s - no interrupt urb present, exiting\n",
1950 goto release_es_lock;
1952 urb->context = edge_serial;
1953 status = usb_submit_urb(urb, GFP_KERNEL);
1956 "%s - usb_submit_urb failed with value %d\n",
1958 goto release_es_lock;
1963 * reset the data toggle on the bulk endpoints to work around bug in
1964 * host controllers where things get out of sync some times
1966 usb_clear_halt(dev, port->write_urb->pipe);
1967 usb_clear_halt(dev, port->read_urb->pipe);
1969 /* start up our bulk read urb */
1970 urb = port->read_urb;
1972 dev_err(&port->dev, "%s - no read urb present, exiting\n",
1975 goto unlink_int_urb;
1977 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
1978 urb->context = edge_port;
1979 status = usb_submit_urb(urb, GFP_KERNEL);
1982 "%s - read bulk usb_submit_urb failed with value %d\n",
1984 goto unlink_int_urb;
1987 ++edge_serial->num_ports_open;
1989 dbg("%s - exited", __func__);
1991 goto release_es_lock;
1994 if (edge_port->edge_serial->num_ports_open == 0)
1995 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
1997 mutex_unlock(&edge_serial->es_lock);
2001 static void edge_close(struct usb_serial_port *port)
2003 struct edgeport_serial *edge_serial;
2004 struct edgeport_port *edge_port;
2005 struct usb_serial *serial = port->serial;
2008 dbg("%s - port %d", __func__, port->number);
2010 edge_serial = usb_get_serial_data(port->serial);
2011 edge_port = usb_get_serial_port_data(port);
2012 if (edge_serial == NULL || edge_port == NULL)
2015 /* The bulkreadcompletion routine will check
2016 * this flag and dump add read data */
2017 edge_port->close_pending = 1;
2019 /* chase the port close and flush */
2020 chase_port(edge_port, (HZ * closing_wait) / 100, 1);
2022 usb_kill_urb(port->read_urb);
2023 usb_kill_urb(port->write_urb);
2024 edge_port->ep_write_urb_in_use = 0;
2026 /* assuming we can still talk to the device,
2027 * send a close port command to it */
2028 dbg("%s - send umpc_close_port", __func__);
2029 port_number = port->number - port->serial->minor;
2031 mutex_lock(&serial->disc_mutex);
2032 if (!serial->disconnected) {
2033 send_cmd(serial->dev,
2035 (__u8)(UMPM_UART1_PORT + port_number),
2040 mutex_unlock(&serial->disc_mutex);
2042 mutex_lock(&edge_serial->es_lock);
2043 --edge_port->edge_serial->num_ports_open;
2044 if (edge_port->edge_serial->num_ports_open <= 0) {
2045 /* last port is now closed, let's shut down our interrupt urb */
2046 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2047 edge_port->edge_serial->num_ports_open = 0;
2049 mutex_unlock(&edge_serial->es_lock);
2050 edge_port->close_pending = 0;
2052 dbg("%s - exited", __func__);
2055 static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
2056 const unsigned char *data, int count)
2058 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2060 dbg("%s - port %d", __func__, port->number);
2063 dbg("%s - write request of 0 bytes", __func__);
2067 if (edge_port == NULL)
2069 if (edge_port->close_pending == 1)
2072 count = kfifo_in_locked(&edge_port->write_fifo, data, count,
2073 &edge_port->ep_lock);
2079 static void edge_send(struct tty_struct *tty)
2081 struct usb_serial_port *port = tty->driver_data;
2083 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2084 unsigned long flags;
2087 dbg("%s - port %d", __func__, port->number);
2089 spin_lock_irqsave(&edge_port->ep_lock, flags);
2091 if (edge_port->ep_write_urb_in_use) {
2092 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2096 count = kfifo_out(&edge_port->write_fifo,
2097 port->write_urb->transfer_buffer,
2098 port->bulk_out_size);
2101 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2105 edge_port->ep_write_urb_in_use = 1;
2107 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2109 usb_serial_debug_data(debug, &port->dev, __func__, count,
2110 port->write_urb->transfer_buffer);
2112 /* set up our urb */
2113 port->write_urb->transfer_buffer_length = count;
2115 /* send the data out the bulk port */
2116 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2118 dev_err_console(port,
2119 "%s - failed submitting write urb, error %d\n",
2121 edge_port->ep_write_urb_in_use = 0;
2122 /* TODO: reschedule edge_send */
2124 edge_port->icount.tx += count;
2126 /* wakeup any process waiting for writes to complete */
2127 /* there is now more room in the buffer for new writes */
2132 static int edge_write_room(struct tty_struct *tty)
2134 struct usb_serial_port *port = tty->driver_data;
2135 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2137 unsigned long flags;
2139 dbg("%s - port %d", __func__, port->number);
2141 if (edge_port == NULL)
2143 if (edge_port->close_pending == 1)
2146 spin_lock_irqsave(&edge_port->ep_lock, flags);
2147 room = kfifo_avail(&edge_port->write_fifo);
2148 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2150 dbg("%s - returns %d", __func__, room);
2154 static int edge_chars_in_buffer(struct tty_struct *tty)
2156 struct usb_serial_port *port = tty->driver_data;
2157 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2159 unsigned long flags;
2161 dbg("%s - port %d", __func__, port->number);
2163 if (edge_port == NULL)
2165 if (edge_port->close_pending == 1)
2168 spin_lock_irqsave(&edge_port->ep_lock, flags);
2169 chars = kfifo_len(&edge_port->write_fifo);
2170 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2172 dbg("%s - returns %d", __func__, chars);
2176 static void edge_throttle(struct tty_struct *tty)
2178 struct usb_serial_port *port = tty->driver_data;
2179 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2182 dbg("%s - port %d", __func__, port->number);
2184 if (edge_port == NULL)
2187 /* if we are implementing XON/XOFF, send the stop character */
2189 unsigned char stop_char = STOP_CHAR(tty);
2190 status = edge_write(tty, port, &stop_char, 1);
2192 dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status);
2196 /* if we are implementing RTS/CTS, stop reads */
2197 /* and the Edgeport will clear the RTS line */
2199 stop_read(edge_port);
2203 static void edge_unthrottle(struct tty_struct *tty)
2205 struct usb_serial_port *port = tty->driver_data;
2206 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2209 dbg("%s - port %d", __func__, port->number);
2211 if (edge_port == NULL)
2214 /* if we are implementing XON/XOFF, send the start character */
2216 unsigned char start_char = START_CHAR(tty);
2217 status = edge_write(tty, port, &start_char, 1);
2219 dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status);
2222 /* if we are implementing RTS/CTS, restart reads */
2223 /* are the Edgeport will assert the RTS line */
2224 if (C_CRTSCTS(tty)) {
2225 status = restart_read(edge_port);
2228 "%s - read bulk usb_submit_urb failed: %d\n",
2234 static void stop_read(struct edgeport_port *edge_port)
2236 unsigned long flags;
2238 spin_lock_irqsave(&edge_port->ep_lock, flags);
2240 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
2241 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
2242 edge_port->shadow_mcr &= ~MCR_RTS;
2244 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2247 static int restart_read(struct edgeport_port *edge_port)
2251 unsigned long flags;
2253 spin_lock_irqsave(&edge_port->ep_lock, flags);
2255 if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
2256 urb = edge_port->port->read_urb;
2257 status = usb_submit_urb(urb, GFP_ATOMIC);
2259 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2260 edge_port->shadow_mcr |= MCR_RTS;
2262 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2267 static void change_port_settings(struct tty_struct *tty,
2268 struct edgeport_port *edge_port, struct ktermios *old_termios)
2270 struct ump_uart_config *config;
2274 int port_number = edge_port->port->number -
2275 edge_port->port->serial->minor;
2277 dbg("%s - port %d", __func__, edge_port->port->number);
2279 config = kmalloc (sizeof (*config), GFP_KERNEL);
2281 *tty->termios = *old_termios;
2282 dev_err(&edge_port->port->dev, "%s - out of memory\n",
2287 cflag = tty->termios->c_cflag;
2291 /* These flags must be set */
2292 config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
2293 config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
2294 config->bUartMode = (__u8)(edge_port->bUartMode);
2296 switch (cflag & CSIZE) {
2298 config->bDataBits = UMP_UART_CHAR5BITS;
2299 dbg("%s - data bits = 5", __func__);
2302 config->bDataBits = UMP_UART_CHAR6BITS;
2303 dbg("%s - data bits = 6", __func__);
2306 config->bDataBits = UMP_UART_CHAR7BITS;
2307 dbg("%s - data bits = 7", __func__);
2311 config->bDataBits = UMP_UART_CHAR8BITS;
2312 dbg("%s - data bits = 8", __func__);
2316 if (cflag & PARENB) {
2317 if (cflag & PARODD) {
2318 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2319 config->bParity = UMP_UART_ODDPARITY;
2320 dbg("%s - parity = odd", __func__);
2322 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2323 config->bParity = UMP_UART_EVENPARITY;
2324 dbg("%s - parity = even", __func__);
2327 config->bParity = UMP_UART_NOPARITY;
2328 dbg("%s - parity = none", __func__);
2331 if (cflag & CSTOPB) {
2332 config->bStopBits = UMP_UART_STOPBIT2;
2333 dbg("%s - stop bits = 2", __func__);
2335 config->bStopBits = UMP_UART_STOPBIT1;
2336 dbg("%s - stop bits = 1", __func__);
2339 /* figure out the flow control settings */
2340 if (cflag & CRTSCTS) {
2341 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
2342 config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
2343 dbg("%s - RTS/CTS is enabled", __func__);
2345 dbg("%s - RTS/CTS is disabled", __func__);
2346 tty->hw_stopped = 0;
2347 restart_read(edge_port);
2350 /* if we are implementing XON/XOFF, set the start and stop
2351 character in the device */
2352 config->cXon = START_CHAR(tty);
2353 config->cXoff = STOP_CHAR(tty);
2355 /* if we are implementing INBOUND XON/XOFF */
2357 config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
2358 dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2359 __func__, config->cXon, config->cXoff);
2361 dbg("%s - INBOUND XON/XOFF is disabled", __func__);
2363 /* if we are implementing OUTBOUND XON/XOFF */
2365 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
2366 dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2367 __func__, config->cXon, config->cXoff);
2369 dbg("%s - OUTBOUND XON/XOFF is disabled", __func__);
2371 tty->termios->c_cflag &= ~CMSPAR;
2373 /* Round the baud rate */
2374 baud = tty_get_baud_rate(tty);
2376 /* pick a default, any default... */
2379 tty_encode_baud_rate(tty, baud, baud);
2381 edge_port->baud_rate = baud;
2382 config->wBaudRate = (__u16)((461550L + baud/2) / baud);
2384 /* FIXME: Recompute actual baud from divisor here */
2386 dbg("%s - baud rate = %d, wBaudRate = %d", __func__, baud,
2389 dbg("wBaudRate: %d", (int)(461550L / config->wBaudRate));
2390 dbg("wFlags: 0x%x", config->wFlags);
2391 dbg("bDataBits: %d", config->bDataBits);
2392 dbg("bParity: %d", config->bParity);
2393 dbg("bStopBits: %d", config->bStopBits);
2394 dbg("cXon: %d", config->cXon);
2395 dbg("cXoff: %d", config->cXoff);
2396 dbg("bUartMode: %d", config->bUartMode);
2398 /* move the word values into big endian mode */
2399 cpu_to_be16s(&config->wFlags);
2400 cpu_to_be16s(&config->wBaudRate);
2402 status = send_cmd(edge_port->port->serial->dev, UMPC_SET_CONFIG,
2403 (__u8)(UMPM_UART1_PORT + port_number),
2404 0, (__u8 *)config, sizeof(*config));
2406 dbg("%s - error %d when trying to write config to device",
2411 static void edge_set_termios(struct tty_struct *tty,
2412 struct usb_serial_port *port, struct ktermios *old_termios)
2414 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2417 cflag = tty->termios->c_cflag;
2419 dbg("%s - clfag %08x iflag %08x", __func__,
2420 tty->termios->c_cflag, tty->termios->c_iflag);
2421 dbg("%s - old clfag %08x old iflag %08x", __func__,
2422 old_termios->c_cflag, old_termios->c_iflag);
2423 dbg("%s - port %d", __func__, port->number);
2425 if (edge_port == NULL)
2427 /* change the port settings to the new ones specified */
2428 change_port_settings(tty, edge_port, old_termios);
2431 static int edge_tiocmset(struct tty_struct *tty,
2432 unsigned int set, unsigned int clear)
2434 struct usb_serial_port *port = tty->driver_data;
2435 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2437 unsigned long flags;
2439 dbg("%s - port %d", __func__, port->number);
2441 spin_lock_irqsave(&edge_port->ep_lock, flags);
2442 mcr = edge_port->shadow_mcr;
2443 if (set & TIOCM_RTS)
2445 if (set & TIOCM_DTR)
2447 if (set & TIOCM_LOOP)
2448 mcr |= MCR_LOOPBACK;
2450 if (clear & TIOCM_RTS)
2452 if (clear & TIOCM_DTR)
2454 if (clear & TIOCM_LOOP)
2455 mcr &= ~MCR_LOOPBACK;
2457 edge_port->shadow_mcr = mcr;
2458 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2460 restore_mcr(edge_port, mcr);
2464 static int edge_tiocmget(struct tty_struct *tty)
2466 struct usb_serial_port *port = tty->driver_data;
2467 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2468 unsigned int result = 0;
2471 unsigned long flags;
2473 dbg("%s - port %d", __func__, port->number);
2475 spin_lock_irqsave(&edge_port->ep_lock, flags);
2477 msr = edge_port->shadow_msr;
2478 mcr = edge_port->shadow_mcr;
2479 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
2480 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
2481 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
2482 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
2483 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
2484 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
2487 dbg("%s -- %x", __func__, result);
2488 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2493 static int edge_get_icount(struct tty_struct *tty,
2494 struct serial_icounter_struct *icount)
2496 struct usb_serial_port *port = tty->driver_data;
2497 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2498 struct async_icount *ic = &edge_port->icount;
2500 icount->cts = ic->cts;
2501 icount->dsr = ic->dsr;
2502 icount->rng = ic->rng;
2503 icount->dcd = ic->dcd;
2504 icount->tx = ic->tx;
2505 icount->rx = ic->rx;
2506 icount->frame = ic->frame;
2507 icount->parity = ic->parity;
2508 icount->overrun = ic->overrun;
2509 icount->brk = ic->brk;
2510 icount->buf_overrun = ic->buf_overrun;
2514 static int get_serial_info(struct edgeport_port *edge_port,
2515 struct serial_struct __user *retinfo)
2517 struct serial_struct tmp;
2522 memset(&tmp, 0, sizeof(tmp));
2524 tmp.type = PORT_16550A;
2525 tmp.line = edge_port->port->serial->minor;
2526 tmp.port = edge_port->port->number;
2528 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2529 tmp.xmit_fifo_size = edge_port->port->bulk_out_size;
2530 tmp.baud_base = 9600;
2531 tmp.close_delay = 5*HZ;
2532 tmp.closing_wait = closing_wait;
2534 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2539 static int edge_ioctl(struct tty_struct *tty,
2540 unsigned int cmd, unsigned long arg)
2542 struct usb_serial_port *port = tty->driver_data;
2543 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2544 struct async_icount cnow;
2545 struct async_icount cprev;
2547 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
2551 dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
2552 return get_serial_info(edge_port,
2553 (struct serial_struct __user *) arg);
2555 dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
2556 cprev = edge_port->icount;
2558 interruptible_sleep_on(&edge_port->delta_msr_wait);
2559 /* see if a signal did it */
2560 if (signal_pending(current))
2561 return -ERESTARTSYS;
2562 cnow = edge_port->icount;
2563 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2564 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2565 return -EIO; /* no change => error */
2566 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2567 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2568 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2569 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2577 return -ENOIOCTLCMD;
2580 static void edge_break(struct tty_struct *tty, int break_state)
2582 struct usb_serial_port *port = tty->driver_data;
2583 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2585 int bv = 0; /* Off */
2587 dbg("%s - state = %d", __func__, break_state);
2589 /* chase the port close */
2590 chase_port(edge_port, 0, 0);
2592 if (break_state == -1)
2594 status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv);
2596 dbg("%s - error %d sending break set/clear command.",
2600 static int edge_startup(struct usb_serial *serial)
2602 struct edgeport_serial *edge_serial;
2603 struct edgeport_port *edge_port;
2604 struct usb_device *dev;
2610 /* create our private serial structure */
2611 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
2612 if (edge_serial == NULL) {
2613 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
2616 mutex_init(&edge_serial->es_lock);
2617 edge_serial->serial = serial;
2618 usb_set_serial_data(serial, edge_serial);
2620 status = download_fw(edge_serial);
2626 /* set up our port private structures */
2627 for (i = 0; i < serial->num_ports; ++i) {
2628 edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
2629 if (edge_port == NULL) {
2630 dev_err(&serial->dev->dev, "%s - Out of memory\n",
2634 spin_lock_init(&edge_port->ep_lock);
2635 if (kfifo_alloc(&edge_port->write_fifo, EDGE_OUT_BUF_SIZE,
2637 dev_err(&serial->dev->dev, "%s - Out of memory\n",
2642 edge_port->port = serial->port[i];
2643 edge_port->edge_serial = edge_serial;
2644 usb_set_serial_port_data(serial->port[i], edge_port);
2645 edge_port->bUartMode = default_uart_mode;
2651 for (--i; i >= 0; --i) {
2652 edge_port = usb_get_serial_port_data(serial->port[i]);
2653 kfifo_free(&edge_port->write_fifo);
2655 usb_set_serial_port_data(serial->port[i], NULL);
2658 usb_set_serial_data(serial, NULL);
2662 static void edge_disconnect(struct usb_serial *serial)
2664 dbg("%s", __func__);
2667 static void edge_release(struct usb_serial *serial)
2670 struct edgeport_port *edge_port;
2672 dbg("%s", __func__);
2674 for (i = 0; i < serial->num_ports; ++i) {
2675 edge_port = usb_get_serial_port_data(serial->port[i]);
2676 kfifo_free(&edge_port->write_fifo);
2679 kfree(usb_get_serial_data(serial));
2683 /* Sysfs Attributes */
2685 static ssize_t show_uart_mode(struct device *dev,
2686 struct device_attribute *attr, char *buf)
2688 struct usb_serial_port *port = to_usb_serial_port(dev);
2689 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2691 return sprintf(buf, "%d\n", edge_port->bUartMode);
2694 static ssize_t store_uart_mode(struct device *dev,
2695 struct device_attribute *attr, const char *valbuf, size_t count)
2697 struct usb_serial_port *port = to_usb_serial_port(dev);
2698 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2699 unsigned int v = simple_strtoul(valbuf, NULL, 0);
2701 dbg("%s: setting uart_mode = %d", __func__, v);
2704 edge_port->bUartMode = v;
2706 dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
2711 static DEVICE_ATTR(uart_mode, S_IWUSR | S_IRUGO, show_uart_mode,
2714 static int edge_create_sysfs_attrs(struct usb_serial_port *port)
2716 return device_create_file(&port->dev, &dev_attr_uart_mode);
2719 static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
2721 device_remove_file(&port->dev, &dev_attr_uart_mode);
2726 static struct usb_serial_driver edgeport_1port_device = {
2728 .owner = THIS_MODULE,
2729 .name = "edgeport_ti_1",
2731 .description = "Edgeport TI 1 port adapter",
2732 .id_table = edgeport_1port_id_table,
2735 .close = edge_close,
2736 .throttle = edge_throttle,
2737 .unthrottle = edge_unthrottle,
2738 .attach = edge_startup,
2739 .disconnect = edge_disconnect,
2740 .release = edge_release,
2741 .port_probe = edge_create_sysfs_attrs,
2742 .port_remove = edge_remove_sysfs_attrs,
2743 .ioctl = edge_ioctl,
2744 .set_termios = edge_set_termios,
2745 .tiocmget = edge_tiocmget,
2746 .tiocmset = edge_tiocmset,
2747 .get_icount = edge_get_icount,
2748 .write = edge_write,
2749 .write_room = edge_write_room,
2750 .chars_in_buffer = edge_chars_in_buffer,
2751 .break_ctl = edge_break,
2752 .read_int_callback = edge_interrupt_callback,
2753 .read_bulk_callback = edge_bulk_in_callback,
2754 .write_bulk_callback = edge_bulk_out_callback,
2757 static struct usb_serial_driver edgeport_2port_device = {
2759 .owner = THIS_MODULE,
2760 .name = "edgeport_ti_2",
2762 .description = "Edgeport TI 2 port adapter",
2763 .id_table = edgeport_2port_id_table,
2766 .close = edge_close,
2767 .throttle = edge_throttle,
2768 .unthrottle = edge_unthrottle,
2769 .attach = edge_startup,
2770 .disconnect = edge_disconnect,
2771 .release = edge_release,
2772 .port_probe = edge_create_sysfs_attrs,
2773 .port_remove = edge_remove_sysfs_attrs,
2774 .ioctl = edge_ioctl,
2775 .set_termios = edge_set_termios,
2776 .tiocmget = edge_tiocmget,
2777 .tiocmset = edge_tiocmset,
2778 .write = edge_write,
2779 .write_room = edge_write_room,
2780 .chars_in_buffer = edge_chars_in_buffer,
2781 .break_ctl = edge_break,
2782 .read_int_callback = edge_interrupt_callback,
2783 .read_bulk_callback = edge_bulk_in_callback,
2784 .write_bulk_callback = edge_bulk_out_callback,
2787 static struct usb_serial_driver * const serial_drivers[] = {
2788 &edgeport_1port_device, &edgeport_2port_device, NULL
2791 module_usb_serial_driver(io_driver, serial_drivers);
2793 MODULE_AUTHOR(DRIVER_AUTHOR);
2794 MODULE_DESCRIPTION(DRIVER_DESC);
2795 MODULE_LICENSE("GPL");
2796 MODULE_FIRMWARE("edgeport/down3.bin");
2798 module_param(debug, bool, S_IRUGO | S_IWUSR);
2799 MODULE_PARM_DESC(debug, "Debug enabled or not");
2801 module_param(closing_wait, int, S_IRUGO | S_IWUSR);
2802 MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
2804 module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
2805 MODULE_PARM_DESC(ignore_cpu_rev,
2806 "Ignore the cpu revision when connecting to a device");
2808 module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
2809 MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");