2 * Edgeport USB Serial Converter driver
4 * Copyright (C) 2000-2002 Inside Out Networks, All rights reserved.
5 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Supports the following devices:
13 * EP/1 EP/2 EP/4 EP/21 EP/22 EP/221 EP/42 EP/421 WATCHPORT
15 * For questions or problems with this driver, contact Inside Out
16 * Networks technical support, or Peter Berger <pberger@brimson.com>,
17 * or Al Borchers <alborchers@steinerpoint.com>.
21 * July 11, 2002 Removed 4 port device structure since all TI UMP
22 * chips have only 2 ports
23 * David Iacovelli (davidi@ionetworks.com)
27 #include <linux/kernel.h>
28 #include <linux/jiffies.h>
29 #include <linux/errno.h>
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/tty.h>
33 #include <linux/tty_driver.h>
34 #include <linux/tty_flip.h>
35 #include <linux/module.h>
36 #include <linux/spinlock.h>
37 #include <linux/mutex.h>
38 #include <linux/serial.h>
39 #include <linux/ioctl.h>
40 #include <linux/firmware.h>
41 #include <linux/uaccess.h>
42 #include <linux/usb.h>
43 #include <linux/usb/serial.h>
46 #include "io_usbvend.h"
52 #define DRIVER_VERSION "v0.7mode043006"
53 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
54 #define DRIVER_DESC "Edgeport USB Serial Driver"
56 #define EPROM_PAGE_SIZE 64
59 struct edgeport_uart_buf_desc {
60 __u32 count; /* Number of bytes currently in buffer */
63 /* different hardware types */
64 #define HARDWARE_TYPE_930 0
65 #define HARDWARE_TYPE_TIUMP 1
67 /* IOCTL_PRIVATE_TI_GET_MODE Definitions */
68 #define TI_MODE_CONFIGURING 0 /* Device has not entered start device */
69 #define TI_MODE_BOOT 1 /* Staying in boot mode */
70 #define TI_MODE_DOWNLOAD 2 /* Made it to download mode */
71 #define TI_MODE_TRANSITIONING 3 /* Currently in boot mode but
72 transitioning to download mode */
75 #define EDGE_READ_URB_RUNNING 0
76 #define EDGE_READ_URB_STOPPING 1
77 #define EDGE_READ_URB_STOPPED 2
79 #define EDGE_LOW_LATENCY 1
80 #define EDGE_CLOSING_WAIT 4000 /* in .01 sec */
82 #define EDGE_OUT_BUF_SIZE 1024
85 /* Product information read from the Edgeport */
87 int TiMode; /* Current TI Mode */
88 __u8 hardware_type; /* Type of hardware */
89 } __attribute__((packed));
93 unsigned int buf_size;
99 struct edgeport_port {
106 __u32 ump_read_timeout; /* Number of miliseconds the UMP will
107 wait without data before completing
112 struct edgeport_uart_buf_desc tx;
113 struct async_icount icount;
114 wait_queue_head_t delta_msr_wait; /* for handling sleeping while
115 waiting for msr change to
117 struct edgeport_serial *edge_serial;
118 struct usb_serial_port *port;
119 __u8 bUartMode; /* Port type, 0: RS232, etc. */
121 int ep_read_urb_state;
122 int ep_write_urb_in_use;
123 struct edge_buf *ep_out_buf;
126 struct edgeport_serial {
127 struct product_info product_info;
128 u8 TI_I2C_Type; /* Type of I2C in UMP */
129 u8 TiReadI2C; /* Set to TRUE if we have read the
131 struct mutex es_lock;
133 struct usb_serial *serial;
137 /* Devices that this driver supports */
138 static struct usb_device_id edgeport_1port_id_table [] = {
139 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
140 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
141 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
142 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
143 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
144 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
145 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
146 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
147 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
148 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
149 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
150 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
151 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
152 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
153 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
154 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
158 static struct usb_device_id edgeport_2port_id_table [] = {
159 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
160 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
161 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
162 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
163 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
164 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
165 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
166 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
167 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
168 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
169 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
170 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
171 /* The 4, 8 and 16 port devices show up as multiple 2 port devices */
172 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
173 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
174 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
175 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
176 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
180 /* Devices that this driver supports */
181 static struct usb_device_id id_table_combined [] = {
182 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
183 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
184 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
185 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
186 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
187 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
188 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
189 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
190 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
191 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
192 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
193 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
194 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
195 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
196 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
197 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
198 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
199 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
200 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
201 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
202 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
203 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
204 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
205 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
206 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
207 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
208 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
209 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
210 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
211 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
212 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
213 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
214 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
218 MODULE_DEVICE_TABLE(usb, id_table_combined);
220 static struct usb_driver io_driver = {
222 .probe = usb_serial_probe,
223 .disconnect = usb_serial_disconnect,
224 .id_table = id_table_combined,
229 static unsigned char OperationalMajorVersion;
230 static unsigned char OperationalMinorVersion;
231 static unsigned short OperationalBuildNumber;
235 static int low_latency = EDGE_LOW_LATENCY;
236 static int closing_wait = EDGE_CLOSING_WAIT;
237 static int ignore_cpu_rev;
238 static int default_uart_mode; /* RS232 */
240 static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
241 unsigned char *data, int length);
243 static void stop_read(struct edgeport_port *edge_port);
244 static int restart_read(struct edgeport_port *edge_port);
246 static void edge_set_termios(struct usb_serial_port *port,
247 struct ktermios *old_termios);
248 static void edge_send(struct usb_serial_port *port);
250 /* sysfs attributes */
251 static int edge_create_sysfs_attrs(struct usb_serial_port *port);
252 static int edge_remove_sysfs_attrs(struct usb_serial_port *port);
254 /* circular buffer */
255 static struct edge_buf *edge_buf_alloc(unsigned int size);
256 static void edge_buf_free(struct edge_buf *eb);
257 static void edge_buf_clear(struct edge_buf *eb);
258 static unsigned int edge_buf_data_avail(struct edge_buf *eb);
259 static unsigned int edge_buf_space_avail(struct edge_buf *eb);
260 static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
262 static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
266 static int ti_vread_sync(struct usb_device *dev, __u8 request,
267 __u16 value, __u16 index, u8 *data, int size)
271 status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
272 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
273 value, index, data, size, 1000);
276 if (status != size) {
277 dbg("%s - wanted to write %d, but only wrote %d",
278 __func__, size, status);
284 static int ti_vsend_sync(struct usb_device *dev, __u8 request,
285 __u16 value, __u16 index, u8 *data, int size)
289 status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
290 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
291 value, index, data, size, 1000);
294 if (status != size) {
295 dbg("%s - wanted to write %d, but only wrote %d",
296 __func__, size, status);
302 static int send_cmd(struct usb_device *dev, __u8 command,
303 __u8 moduleid, __u16 value, u8 *data,
306 return ti_vsend_sync(dev, command, value, moduleid, data, size);
309 /* clear tx/rx buffers and fifo in TI UMP */
310 static int purge_port(struct usb_serial_port *port, __u16 mask)
312 int port_number = port->number - port->serial->minor;
314 dbg("%s - port %d, mask %x", __func__, port_number, mask);
316 return send_cmd(port->serial->dev,
318 (__u8)(UMPM_UART1_PORT + port_number),
325 * read_download_mem - Read edgeport memory from TI chip
326 * @dev: usb device pointer
327 * @start_address: Device CPU address at which to read
328 * @length: Length of above data
329 * @address_type: Can read both XDATA and I2C
330 * @buffer: pointer to input data buffer
332 static int read_download_mem(struct usb_device *dev, int start_address,
333 int length, __u8 address_type, __u8 *buffer)
337 __be16 be_start_address;
339 dbg("%s - @ %x for %d", __func__, start_address, length);
341 /* Read in blocks of 64 bytes
342 * (TI firmware can't handle more than 64 byte reads)
348 read_length = (__u8)length;
350 if (read_length > 1) {
351 dbg("%s - @ %x for %d", __func__,
352 start_address, read_length);
354 be_start_address = cpu_to_be16(start_address);
355 status = ti_vread_sync(dev, UMPC_MEMORY_READ,
357 (__force __u16)be_start_address,
358 buffer, read_length);
361 dbg("%s - ERROR %x", __func__, status);
366 usb_serial_debug_data(debug, &dev->dev, __func__,
367 read_length, buffer);
369 /* Update pointers/length */
370 start_address += read_length;
371 buffer += read_length;
372 length -= read_length;
378 static int read_ram(struct usb_device *dev, int start_address,
379 int length, __u8 *buffer)
381 return read_download_mem(dev, start_address, length,
382 DTK_ADDR_SPACE_XDATA, buffer);
385 /* Read edgeport memory to a given block */
386 static int read_boot_mem(struct edgeport_serial *serial,
387 int start_address, int length, __u8 *buffer)
392 for (i = 0; i < length; i++) {
393 status = ti_vread_sync(serial->serial->dev,
394 UMPC_MEMORY_READ, serial->TI_I2C_Type,
395 (__u16)(start_address+i), &buffer[i], 0x01);
397 dbg("%s - ERROR %x", __func__, status);
402 dbg("%s - start_address = %x, length = %d",
403 __func__, start_address, length);
404 usb_serial_debug_data(debug, &serial->serial->dev->dev,
405 __func__, length, buffer);
407 serial->TiReadI2C = 1;
412 /* Write given block to TI EPROM memory */
413 static int write_boot_mem(struct edgeport_serial *serial,
414 int start_address, int length, __u8 *buffer)
420 /* Must do a read before write */
421 if (!serial->TiReadI2C) {
422 status = read_boot_mem(serial, 0, 1, &temp);
427 for (i = 0; i < length; ++i) {
428 status = ti_vsend_sync(serial->serial->dev,
429 UMPC_MEMORY_WRITE, buffer[i],
430 (__u16)(i + start_address), NULL, 0);
435 dbg("%s - start_sddr = %x, length = %d",
436 __func__, start_address, length);
437 usb_serial_debug_data(debug, &serial->serial->dev->dev,
438 __func__, length, buffer);
444 /* Write edgeport I2C memory to TI chip */
445 static int write_i2c_mem(struct edgeport_serial *serial,
446 int start_address, int length, __u8 address_type, __u8 *buffer)
450 __be16 be_start_address;
452 /* We can only send a maximum of 1 aligned byte page at a time */
454 /* calulate the number of bytes left in the first page */
455 write_length = EPROM_PAGE_SIZE -
456 (start_address & (EPROM_PAGE_SIZE - 1));
458 if (write_length > length)
459 write_length = length;
461 dbg("%s - BytesInFirstPage 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 first page */
467 be_start_address = cpu_to_be16(start_address);
468 status = ti_vsend_sync(serial->serial->dev,
469 UMPC_MEMORY_WRITE, (__u16)address_type,
470 (__force __u16)be_start_address,
471 buffer, write_length);
473 dbg("%s - ERROR %d", __func__, status);
477 length -= write_length;
478 start_address += write_length;
479 buffer += write_length;
481 /* We should be aligned now -- can write
482 max page size bytes at a time */
484 if (length > EPROM_PAGE_SIZE)
485 write_length = EPROM_PAGE_SIZE;
487 write_length = length;
489 dbg("%s - Page Write Addr = %x, length = %d",
490 __func__, start_address, write_length);
491 usb_serial_debug_data(debug, &serial->serial->dev->dev,
492 __func__, write_length, buffer);
494 /* Write next page */
495 be_start_address = cpu_to_be16(start_address);
496 status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
498 (__force __u16)be_start_address,
499 buffer, write_length);
501 dev_err(&serial->serial->dev->dev, "%s - ERROR %d\n",
506 length -= write_length;
507 start_address += write_length;
508 buffer += write_length;
513 /* Examine the UMP DMA registers and LSR
515 * Check the MSBit of the X and Y DMA byte count registers.
516 * A zero in this bit indicates that the TX DMA buffers are empty
517 * then check the TX Empty bit in the UART.
519 static int tx_active(struct edgeport_port *port)
522 struct out_endpoint_desc_block *oedb;
526 oedb = kmalloc(sizeof(*oedb), GFP_KERNEL);
528 dev_err(&port->port->dev, "%s - out of memory\n", __func__);
532 lsr = kmalloc(1, GFP_KERNEL); /* Sigh, that's right, just one byte,
533 as not all platforms can do DMA
539 /* Read the DMA Count Registers */
540 status = read_ram(port->port->serial->dev, port->dma_address,
541 sizeof(*oedb), (void *)oedb);
543 goto exit_is_tx_active;
545 dbg("%s - XByteCount 0x%X", __func__, oedb->XByteCount);
548 status = read_ram(port->port->serial->dev,
549 port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr);
552 goto exit_is_tx_active;
553 dbg("%s - LSR = 0x%X", __func__, *lsr);
555 /* If either buffer has data or we are transmitting then return TRUE */
556 if ((oedb->XByteCount & 0x80) != 0)
559 if ((*lsr & UMP_UART_LSR_TX_MASK) == 0)
562 /* We return Not Active if we get any kind of error */
564 dbg("%s - return %d", __func__, bytes_left);
571 static void chase_port(struct edgeport_port *port, unsigned long timeout,
575 struct tty_struct *tty = port->port->tty;
580 timeout = (HZ * EDGE_CLOSING_WAIT)/100;
582 /* wait for data to drain from the buffer */
583 spin_lock_irqsave(&port->ep_lock, flags);
584 init_waitqueue_entry(&wait, current);
585 add_wait_queue(&tty->write_wait, &wait);
587 set_current_state(TASK_INTERRUPTIBLE);
588 if (edge_buf_data_avail(port->ep_out_buf) == 0
589 || timeout == 0 || signal_pending(current)
590 || !usb_get_intfdata(port->port->serial->interface))
593 spin_unlock_irqrestore(&port->ep_lock, flags);
594 timeout = schedule_timeout(timeout);
595 spin_lock_irqsave(&port->ep_lock, flags);
597 set_current_state(TASK_RUNNING);
598 remove_wait_queue(&tty->write_wait, &wait);
600 edge_buf_clear(port->ep_out_buf);
601 spin_unlock_irqrestore(&port->ep_lock, flags);
603 /* wait for data to drain from the device */
605 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
606 && usb_get_intfdata(port->port->serial->interface)) {
607 /* not disconnected */
608 if (!tx_active(port))
614 if (!usb_get_intfdata(port->port->serial->interface))
617 /* wait one more character time, based on baud rate */
618 /* (tx_active doesn't seem to wait for the last byte) */
619 baud_rate = port->baud_rate;
622 msleep(max(1, DIV_ROUND_UP(10000, baud_rate)));
625 static int choose_config(struct usb_device *dev)
628 * There may be multiple configurations on this device, in which case
629 * we would need to read and parse all of them to find out which one
630 * we want. However, we just support one config at this point,
631 * configuration # 1, which is Config Descriptor 0.
634 dbg("%s - Number of Interfaces = %d",
635 __func__, dev->config->desc.bNumInterfaces);
636 dbg("%s - MAX Power = %d",
637 __func__, dev->config->desc.bMaxPower * 2);
639 if (dev->config->desc.bNumInterfaces != 1) {
640 dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n",
648 static int read_rom(struct edgeport_serial *serial,
649 int start_address, int length, __u8 *buffer)
653 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
654 status = read_download_mem(serial->serial->dev,
660 status = read_boot_mem(serial, start_address, length,
666 static int write_rom(struct edgeport_serial *serial, int start_address,
667 int length, __u8 *buffer)
669 if (serial->product_info.TiMode == TI_MODE_BOOT)
670 return write_boot_mem(serial, start_address, length,
673 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
674 return write_i2c_mem(serial, start_address, length,
675 serial->TI_I2C_Type, buffer);
681 /* Read a descriptor header from I2C based on type */
682 static int get_descriptor_addr(struct edgeport_serial *serial,
683 int desc_type, struct ti_i2c_desc *rom_desc)
688 /* Search for requested descriptor in I2C */
691 status = read_rom(serial,
693 sizeof(struct ti_i2c_desc),
698 if (rom_desc->Type == desc_type)
699 return start_address;
701 start_address = start_address + sizeof(struct ti_i2c_desc)
704 } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
709 /* Validate descriptor checksum */
710 static int valid_csum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
715 for (i = 0; i < rom_desc->Size; i++)
716 cs = (__u8)(cs + buffer[i]);
718 if (cs != rom_desc->CheckSum) {
719 dbg("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
725 /* Make sure that the I2C image is good */
726 static int check_i2c_image(struct edgeport_serial *serial)
728 struct device *dev = &serial->serial->dev->dev;
730 struct ti_i2c_desc *rom_desc;
731 int start_address = 2;
735 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
737 dev_err(dev, "%s - out of memory\n", __func__);
740 buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL);
742 dev_err(dev, "%s - out of memory when allocating buffer\n",
748 /* Read the first byte (Signature0) must be 0x52 or 0x10 */
749 status = read_rom(serial, 0, 1, buffer);
753 if (*buffer != UMP5152 && *buffer != UMP3410) {
754 dev_err(dev, "%s - invalid buffer signature\n", __func__);
760 /* Validate the I2C */
761 status = read_rom(serial,
763 sizeof(struct ti_i2c_desc),
768 if ((start_address + sizeof(struct ti_i2c_desc) +
769 rom_desc->Size) > TI_MAX_I2C_SIZE) {
771 dbg("%s - structure too big, erroring out.", __func__);
775 dbg("%s Type = 0x%x", __func__, rom_desc->Type);
777 /* Skip type 2 record */
778 ttype = rom_desc->Type & 0x0f;
779 if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
780 && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) {
781 /* Read the descriptor data */
782 status = read_rom(serial, start_address +
783 sizeof(struct ti_i2c_desc),
784 rom_desc->Size, buffer);
788 status = valid_csum(rom_desc, buffer);
792 start_address = start_address + sizeof(struct ti_i2c_desc) +
795 } while ((rom_desc->Type != I2C_DESC_TYPE_ION) &&
796 (start_address < TI_MAX_I2C_SIZE));
798 if ((rom_desc->Type != I2C_DESC_TYPE_ION) ||
799 (start_address > TI_MAX_I2C_SIZE))
808 static int get_manuf_info(struct edgeport_serial *serial, __u8 *buffer)
812 struct ti_i2c_desc *rom_desc;
813 struct edge_ti_manuf_descriptor *desc;
815 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
817 dev_err(&serial->serial->dev->dev, "%s - out of memory\n",
821 start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
824 if (!start_address) {
825 dbg("%s - Edge Descriptor not found in I2C", __func__);
830 /* Read the descriptor data */
831 status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc),
832 rom_desc->Size, buffer);
836 status = valid_csum(rom_desc, buffer);
838 desc = (struct edge_ti_manuf_descriptor *)buffer;
839 dbg("%s - IonConfig 0x%x", __func__, desc->IonConfig);
840 dbg("%s - Version %d", __func__, desc->Version);
841 dbg("%s - Cpu/Board 0x%x", __func__, desc->CpuRev_BoardRev);
842 dbg("%s - NumPorts %d", __func__, desc->NumPorts);
843 dbg("%s - NumVirtualPorts %d", __func__, desc->NumVirtualPorts);
844 dbg("%s - TotalPorts %d", __func__, desc->TotalPorts);
851 /* Build firmware header used for firmware update */
852 static int build_i2c_fw_hdr(__u8 *header, struct device *dev)
859 struct ti_i2c_desc *i2c_header;
860 struct ti_i2c_image_header *img_header;
861 struct ti_i2c_firmware_rec *firmware_rec;
862 const struct firmware *fw;
863 const char *fw_name = "edgeport/down3.bin";
865 /* In order to update the I2C firmware we must change the type 2 record
866 * to type 0xF2. This will force the UMP to come up in Boot Mode.
867 * Then while in boot mode, the driver will download the latest
868 * firmware (padded to 15.5k) into the UMP ram. And finally when the
869 * device comes back up in download mode the driver will cause the new
870 * firmware to be copied from the UMP Ram to I2C and the firmware will
871 * update the record type from 0xf2 to 0x02.
874 /* Allocate a 15.5k buffer + 2 bytes for version number
875 * (Firmware Record) */
876 buffer_size = (((1024 * 16) - 512 ) +
877 sizeof(struct ti_i2c_firmware_rec));
879 buffer = kmalloc(buffer_size, GFP_KERNEL);
881 dev_err(dev, "%s - out of memory\n", __func__);
885 // Set entire image of 0xffs
886 memset(buffer, 0xff, buffer_size);
888 err = request_firmware(&fw, fw_name, dev);
890 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
896 /* Save Download Version Number */
897 OperationalMajorVersion = fw->data[0];
898 OperationalMinorVersion = fw->data[1];
899 OperationalBuildNumber = fw->data[2] | (fw->data[3] << 8);
901 /* Copy version number into firmware record */
902 firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
904 firmware_rec->Ver_Major = OperationalMajorVersion;
905 firmware_rec->Ver_Minor = OperationalMinorVersion;
907 /* Pointer to fw_down memory image */
908 img_header = (struct ti_i2c_image_header *)&fw->data[4];
910 memcpy(buffer + sizeof(struct ti_i2c_firmware_rec),
911 &fw->data[4 + sizeof(struct ti_i2c_image_header)],
912 le16_to_cpu(img_header->Length));
914 release_firmware(fw);
916 for (i=0; i < buffer_size; i++) {
917 cs = (__u8)(cs + buffer[i]);
922 /* Build new header */
923 i2c_header = (struct ti_i2c_desc *)header;
924 firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data;
926 i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK;
927 i2c_header->Size = (__u16)buffer_size;
928 i2c_header->CheckSum = cs;
929 firmware_rec->Ver_Major = OperationalMajorVersion;
930 firmware_rec->Ver_Minor = OperationalMinorVersion;
935 /* Try to figure out what type of I2c we have */
936 static int i2c_type_bootmode(struct edgeport_serial *serial)
941 /* Try to read type 2 */
942 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
943 DTK_ADDR_SPACE_I2C_TYPE_II, 0, &data, 0x01);
945 dbg("%s - read 2 status error = %d", __func__, status);
947 dbg("%s - read 2 data = 0x%x", __func__, data);
948 if ((!status) && (data == UMP5152 || data == UMP3410)) {
949 dbg("%s - ROM_TYPE_II", __func__);
950 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
954 /* Try to read type 3 */
955 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
956 DTK_ADDR_SPACE_I2C_TYPE_III, 0, &data, 0x01);
958 dbg("%s - read 3 status error = %d", __func__, status);
960 dbg("%s - read 2 data = 0x%x", __func__, data);
961 if ((!status) && (data == UMP5152 || data == UMP3410)) {
962 dbg("%s - ROM_TYPE_III", __func__);
963 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
967 dbg("%s - Unknown", __func__);
968 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
972 static int bulk_xfer(struct usb_serial *serial, void *buffer,
973 int length, int *num_sent)
977 status = usb_bulk_msg(serial->dev,
978 usb_sndbulkpipe(serial->dev,
979 serial->port[0]->bulk_out_endpointAddress),
980 buffer, length, num_sent, 1000);
984 /* Download given firmware image to the device (IN BOOT MODE) */
985 static int download_code(struct edgeport_serial *serial, __u8 *image,
993 /* Transfer firmware image */
994 for (pos = 0; pos < image_length; ) {
995 /* Read the next buffer from file */
996 transfer = image_length - pos;
997 if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
998 transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
1001 status = bulk_xfer(serial->serial, &image[pos],
1005 /* Advance buffer pointer */
1013 static int config_boot_dev(struct usb_device *dev)
1018 static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc)
1020 return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev);
1024 * DownloadTIFirmware - Download run-time operating firmware to the TI5052
1026 * This routine downloads the main operating code into the TI5052, using the
1027 * boot code already burned into E2PROM or ROM.
1029 static int download_fw(struct edgeport_serial *serial)
1031 struct device *dev = &serial->serial->dev->dev;
1034 struct edge_ti_manuf_descriptor *ti_manuf_desc;
1035 struct usb_interface_descriptor *interface;
1036 int download_cur_ver;
1037 int download_new_ver;
1039 /* This routine is entered by both the BOOT mode and the Download mode
1040 * We can determine which code is running by the reading the config
1041 * descriptor and if we have only one bulk pipe it is in boot mode
1043 serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
1045 /* Default to type 2 i2c */
1046 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1048 status = choose_config(serial->serial->dev);
1052 interface = &serial->serial->interface->cur_altsetting->desc;
1054 dev_err(dev, "%s - no interface set, error!\n", __func__);
1059 * Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
1060 * if we have more than one endpoint we are definitely in download
1063 if (interface->bNumEndpoints > 1)
1064 serial->product_info.TiMode = TI_MODE_DOWNLOAD;
1066 /* Otherwise we will remain in configuring mode */
1067 serial->product_info.TiMode = TI_MODE_CONFIGURING;
1069 /********************************************************************/
1071 /********************************************************************/
1072 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
1073 struct ti_i2c_desc *rom_desc;
1075 dbg("%s - RUNNING IN DOWNLOAD MODE", __func__);
1077 status = check_i2c_image(serial);
1079 dbg("%s - DOWNLOAD MODE -- BAD I2C", __func__);
1083 /* Validate Hardware version number
1084 * Read Manufacturing Descriptor from TI Based Edgeport
1086 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
1087 if (!ti_manuf_desc) {
1088 dev_err(dev, "%s - out of memory.\n", __func__);
1091 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
1093 kfree(ti_manuf_desc);
1097 /* Check version number of ION descriptor */
1098 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1099 dbg("%s - Wrong CPU Rev %d (Must be 2)",
1100 __func__, ti_cpu_rev(ti_manuf_desc));
1101 kfree(ti_manuf_desc);
1105 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
1107 dev_err(dev, "%s - out of memory.\n", __func__);
1108 kfree(ti_manuf_desc);
1112 /* Search for type 2 record (firmware record) */
1113 start_address = get_descriptor_addr(serial,
1114 I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc);
1115 if (start_address != 0) {
1116 struct ti_i2c_firmware_rec *firmware_version;
1119 dbg("%s - Found Type FIRMWARE (Type 2) record",
1122 firmware_version = kmalloc(sizeof(*firmware_version),
1124 if (!firmware_version) {
1125 dev_err(dev, "%s - out of memory.\n", __func__);
1127 kfree(ti_manuf_desc);
1131 /* Validate version number
1132 * Read the descriptor data
1134 status = read_rom(serial, start_address +
1135 sizeof(struct ti_i2c_desc),
1136 sizeof(struct ti_i2c_firmware_rec),
1137 (__u8 *)firmware_version);
1139 kfree(firmware_version);
1141 kfree(ti_manuf_desc);
1145 /* Check version number of download with current
1147 download_cur_ver = (firmware_version->Ver_Major << 8) +
1148 (firmware_version->Ver_Minor);
1149 download_new_ver = (OperationalMajorVersion << 8) +
1150 (OperationalMinorVersion);
1152 dbg("%s - >> FW Versions Device %d.%d Driver %d.%d",
1154 firmware_version->Ver_Major,
1155 firmware_version->Ver_Minor,
1156 OperationalMajorVersion,
1157 OperationalMinorVersion);
1159 /* Check if we have an old version in the I2C and
1160 update if necessary */
1161 if (download_cur_ver != download_new_ver) {
1162 dbg("%s - Update I2C dld from %d.%d to %d.%d",
1164 firmware_version->Ver_Major,
1165 firmware_version->Ver_Minor,
1166 OperationalMajorVersion,
1167 OperationalMinorVersion);
1169 /* In order to update the I2C firmware we must
1170 * change the type 2 record to type 0xF2. This
1171 * will force the UMP to come up in Boot Mode.
1172 * Then while in boot mode, the driver will
1173 * download the latest firmware (padded to
1174 * 15.5k) into the UMP ram. Finally when the
1175 * device comes back up in download mode the
1176 * driver will cause the new firmware to be
1177 * copied from the UMP Ram to I2C and the
1178 * firmware will update the record type from
1181 record = I2C_DESC_TYPE_FIRMWARE_BLANK;
1183 /* Change the I2C Firmware record type to
1184 0xf2 to trigger an update */
1185 status = write_rom(serial, start_address,
1186 sizeof(record), &record);
1188 kfree(firmware_version);
1190 kfree(ti_manuf_desc);
1194 /* verify the write -- must do this in order
1195 * for write to complete before we do the
1198 status = read_rom(serial,
1203 kfree(firmware_version);
1205 kfree(ti_manuf_desc);
1209 if (record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
1211 "%s - error resetting device\n",
1213 kfree(firmware_version);
1215 kfree(ti_manuf_desc);
1219 dbg("%s - HARDWARE RESET", __func__);
1221 /* Reset UMP -- Back to BOOT MODE */
1222 status = ti_vsend_sync(serial->serial->dev,
1223 UMPC_HARDWARE_RESET,
1226 dbg("%s - HARDWARE RESET return %d",
1229 /* return an error on purpose. */
1230 kfree(firmware_version);
1232 kfree(ti_manuf_desc);
1235 kfree(firmware_version);
1237 /* Search for type 0xF2 record (firmware blank record) */
1238 else if ((start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
1239 #define HEADER_SIZE (sizeof(struct ti_i2c_desc) + \
1240 sizeof(struct ti_i2c_firmware_rec))
1244 header = kmalloc(HEADER_SIZE, GFP_KERNEL);
1246 dev_err(dev, "%s - out of memory.\n", __func__);
1248 kfree(ti_manuf_desc);
1252 vheader = kmalloc(HEADER_SIZE, GFP_KERNEL);
1254 dev_err(dev, "%s - out of memory.\n", __func__);
1257 kfree(ti_manuf_desc);
1261 dbg("%s - Found Type BLANK FIRMWARE (Type F2) record",
1265 * In order to update the I2C firmware we must change
1266 * the type 2 record to type 0xF2. This will force the
1267 * UMP to come up in Boot Mode. Then while in boot
1268 * mode, the driver will download the latest firmware
1269 * (padded to 15.5k) into the UMP ram. Finally when the
1270 * device comes back up in download mode the driver
1271 * will cause the new firmware to be copied from the
1272 * UMP Ram to I2C and the firmware will update the
1273 * record type from 0xf2 to 0x02.
1275 status = build_i2c_fw_hdr(header, dev);
1280 kfree(ti_manuf_desc);
1284 /* Update I2C with type 0xf2 record with correct
1285 size and checksum */
1286 status = write_rom(serial,
1294 kfree(ti_manuf_desc);
1298 /* verify the write -- must do this in order for
1299 write to complete before we do the hardware reset */
1300 status = read_rom(serial, start_address,
1301 HEADER_SIZE, vheader);
1304 dbg("%s - can't read header back", __func__);
1308 kfree(ti_manuf_desc);
1311 if (memcmp(vheader, header, HEADER_SIZE)) {
1312 dbg("%s - write download record failed",
1317 kfree(ti_manuf_desc);
1324 dbg("%s - Start firmware update", __func__);
1326 /* Tell firmware to copy download image into I2C */
1327 status = ti_vsend_sync(serial->serial->dev,
1328 UMPC_COPY_DNLD_TO_I2C, 0, 0, NULL, 0);
1330 dbg("%s - Update complete 0x%x", __func__, status);
1333 "%s - UMPC_COPY_DNLD_TO_I2C failed\n",
1336 kfree(ti_manuf_desc);
1341 // The device is running the download code
1343 kfree(ti_manuf_desc);
1347 /********************************************************************/
1349 /********************************************************************/
1350 dbg("%s - RUNNING IN BOOT MODE", __func__);
1352 /* Configure the TI device so we can use the BULK pipes for download */
1353 status = config_boot_dev(serial->serial->dev);
1357 if (le16_to_cpu(serial->serial->dev->descriptor.idVendor)
1358 != USB_VENDOR_ID_ION) {
1359 dbg("%s - VID = 0x%x", __func__,
1360 le16_to_cpu(serial->serial->dev->descriptor.idVendor));
1361 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1362 goto stayinbootmode;
1365 /* We have an ION device (I2c Must be programmed)
1366 Determine I2C image type */
1367 if (i2c_type_bootmode(serial))
1368 goto stayinbootmode;
1370 /* Check for ION Vendor ID and that the I2C is valid */
1371 if (!check_i2c_image(serial)) {
1372 struct ti_i2c_image_header *header;
1378 const struct firmware *fw;
1379 const char *fw_name = "edgeport/down3.bin";
1381 /* Validate Hardware version number
1382 * Read Manufacturing Descriptor from TI Based Edgeport
1384 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
1385 if (!ti_manuf_desc) {
1386 dev_err(dev, "%s - out of memory.\n", __func__);
1389 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
1391 kfree(ti_manuf_desc);
1392 goto stayinbootmode;
1395 /* Check for version 2 */
1396 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1397 dbg("%s - Wrong CPU Rev %d (Must be 2)",
1398 __func__, ti_cpu_rev(ti_manuf_desc));
1399 kfree(ti_manuf_desc);
1400 goto stayinbootmode;
1403 kfree(ti_manuf_desc);
1406 * In order to update the I2C firmware we must change the type
1407 * 2 record to type 0xF2. This will force the UMP to come up
1408 * in Boot Mode. Then while in boot mode, the driver will
1409 * download the latest firmware (padded to 15.5k) into the
1410 * UMP ram. Finally when the device comes back up in download
1411 * mode the driver will cause the new firmware to be copied
1412 * from the UMP Ram to I2C and the firmware will update the
1413 * record type from 0xf2 to 0x02.
1415 * Do we really have to copy the whole firmware image,
1416 * or could we do this in place!
1419 /* Allocate a 15.5k buffer + 3 byte header */
1420 buffer_size = (((1024 * 16) - 512) +
1421 sizeof(struct ti_i2c_image_header));
1422 buffer = kmalloc(buffer_size, GFP_KERNEL);
1424 dev_err(dev, "%s - out of memory\n", __func__);
1428 /* Initialize the buffer to 0xff (pad the buffer) */
1429 memset(buffer, 0xff, buffer_size);
1431 err = request_firmware(&fw, fw_name, dev);
1433 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
1438 memcpy(buffer, &fw->data[4], fw->size - 4);
1439 release_firmware(fw);
1441 for (i = sizeof(struct ti_i2c_image_header);
1442 i < buffer_size; i++) {
1443 cs = (__u8)(cs + buffer[i]);
1446 header = (struct ti_i2c_image_header *)buffer;
1448 /* update length and checksum after padding */
1449 header->Length = cpu_to_le16((__u16)(buffer_size -
1450 sizeof(struct ti_i2c_image_header)));
1451 header->CheckSum = cs;
1453 /* Download the operational code */
1454 dbg("%s - Downloading operational code image (TI UMP)",
1456 status = download_code(serial, buffer, buffer_size);
1461 dbg("%s - Error downloading operational code image",
1466 /* Device will reboot */
1467 serial->product_info.TiMode = TI_MODE_TRANSITIONING;
1469 dbg("%s - Download successful -- Device rebooting...",
1472 /* return an error on purpose */
1477 /* Eprom is invalid or blank stay in boot mode */
1478 dbg("%s - STAYING IN BOOT MODE", __func__);
1479 serial->product_info.TiMode = TI_MODE_BOOT;
1485 static int ti_do_config(struct edgeport_port *port, int feature, int on)
1487 int port_number = port->port->number - port->port->serial->minor;
1488 on = !!on; /* 1 or 0 not bitmask */
1489 return send_cmd(port->port->serial->dev,
1490 feature, (__u8)(UMPM_UART1_PORT + port_number),
1495 static int restore_mcr(struct edgeport_port *port, __u8 mcr)
1499 dbg("%s - %x", __func__, mcr);
1501 status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR);
1504 status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS);
1507 return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK);
1510 /* Convert TI LSR to standard UART flags */
1511 static __u8 map_line_status(__u8 ti_lsr)
1515 #define MAP_FLAG(flagUmp, flagUart) \
1516 if (ti_lsr & flagUmp) \
1519 MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR) /* overrun */
1520 MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */
1521 MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */
1522 MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */
1523 MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* rx data available */
1524 MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* tx hold reg empty */
1531 static void handle_new_msr(struct edgeport_port *edge_port, __u8 msr)
1533 struct async_icount *icount;
1534 struct tty_struct *tty;
1536 dbg("%s - %02x", __func__, msr);
1538 if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
1539 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
1540 icount = &edge_port->icount;
1542 /* update input line counters */
1543 if (msr & EDGEPORT_MSR_DELTA_CTS)
1545 if (msr & EDGEPORT_MSR_DELTA_DSR)
1547 if (msr & EDGEPORT_MSR_DELTA_CD)
1549 if (msr & EDGEPORT_MSR_DELTA_RI)
1551 wake_up_interruptible(&edge_port->delta_msr_wait);
1554 /* Save the new modem status */
1555 edge_port->shadow_msr = msr & 0xf0;
1557 tty = edge_port->port->tty;
1558 /* handle CTS flow control */
1559 if (tty && C_CRTSCTS(tty)) {
1560 if (msr & EDGEPORT_MSR_CTS) {
1561 tty->hw_stopped = 0;
1564 tty->hw_stopped = 1;
1571 static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data,
1572 __u8 lsr, __u8 data)
1574 struct async_icount *icount;
1575 __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR |
1576 LSR_FRM_ERR | LSR_BREAK));
1578 dbg("%s - %02x", __func__, new_lsr);
1580 edge_port->shadow_lsr = lsr;
1582 if (new_lsr & LSR_BREAK)
1584 * Parity and Framing errors only count if they
1585 * occur exclusive of a break being received.
1587 new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
1589 /* Place LSR data byte into Rx buffer */
1590 if (lsr_data && edge_port->port->tty)
1591 edge_tty_recv(&edge_port->port->dev, edge_port->port->tty,
1594 /* update input line counters */
1595 icount = &edge_port->icount;
1596 if (new_lsr & LSR_BREAK)
1598 if (new_lsr & LSR_OVER_ERR)
1600 if (new_lsr & LSR_PAR_ERR)
1602 if (new_lsr & LSR_FRM_ERR)
1607 static void edge_interrupt_callback(struct urb *urb)
1609 struct edgeport_serial *edge_serial = urb->context;
1610 struct usb_serial_port *port;
1611 struct edgeport_port *edge_port;
1612 unsigned char *data = urb->transfer_buffer;
1613 int length = urb->actual_length;
1619 int status = urb->status;
1621 dbg("%s", __func__);
1630 /* this urb is terminated, clean up */
1631 dbg("%s - urb shutting down with status: %d",
1635 dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
1636 "%d\n", __func__, status);
1641 dbg("%s - no data in urb", __func__);
1645 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev,
1646 __func__, length, data);
1649 dbg("%s - expecting packet of size 2, got %d",
1654 port_number = TIUMP_GET_PORT_FROM_CODE(data[0]);
1655 function = TIUMP_GET_FUNC_FROM_CODE(data[0]);
1656 dbg("%s - port_number %d, function %d, info 0x%x",
1657 __func__, port_number, function, data[1]);
1658 port = edge_serial->serial->port[port_number];
1659 edge_port = usb_get_serial_port_data(port);
1661 dbg("%s - edge_port not found", __func__);
1665 case TIUMP_INTERRUPT_CODE_LSR:
1666 lsr = map_line_status(data[1]);
1667 if (lsr & UMP_UART_LSR_DATA_MASK) {
1668 /* Save the LSR event for bulk read
1669 completion routine */
1670 dbg("%s - LSR Event Port %u LSR Status = %02x",
1671 __func__, port_number, lsr);
1672 edge_port->lsr_event = 1;
1673 edge_port->lsr_mask = lsr;
1675 dbg("%s - ===== Port %d LSR Status = %02x ======",
1676 __func__, port_number, lsr);
1677 handle_new_lsr(edge_port, 0, lsr, 0);
1681 case TIUMP_INTERRUPT_CODE_MSR: /* MSR */
1682 /* Copy MSR from UMP */
1684 dbg("%s - ===== Port %u MSR Status = %02x ======\n",
1685 __func__, port_number, msr);
1686 handle_new_msr(edge_port, msr);
1690 dev_err(&urb->dev->dev,
1691 "%s - Unknown Interrupt code from UMP %x\n",
1698 retval = usb_submit_urb(urb, GFP_ATOMIC);
1700 dev_err(&urb->dev->dev,
1701 "%s - usb_submit_urb failed with result %d\n",
1705 static void edge_bulk_in_callback(struct urb *urb)
1707 struct edgeport_port *edge_port = urb->context;
1708 unsigned char *data = urb->transfer_buffer;
1709 struct tty_struct *tty;
1712 int status = urb->status;
1714 dbg("%s", __func__);
1723 /* this urb is terminated, clean up */
1724 dbg("%s - urb shutting down with status: %d",
1728 dev_err(&urb->dev->dev,
1729 "%s - nonzero read bulk status received: %d\n",
1733 if (status == -EPIPE)
1737 dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__);
1741 port_number = edge_port->port->number - edge_port->port->serial->minor;
1743 if (edge_port->lsr_event) {
1744 edge_port->lsr_event = 0;
1745 dbg("%s ===== Port %u LSR Status = %02x, Data = %02x ======",
1746 __func__, port_number, edge_port->lsr_mask, *data);
1747 handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data);
1748 /* Adjust buffer length/pointer */
1749 --urb->actual_length;
1753 tty = edge_port->port->tty;
1754 if (tty && urb->actual_length) {
1755 usb_serial_debug_data(debug, &edge_port->port->dev,
1756 __func__, urb->actual_length, data);
1757 if (edge_port->close_pending)
1758 dbg("%s - close pending, dropping data on the floor",
1761 edge_tty_recv(&edge_port->port->dev, tty, data,
1762 urb->actual_length);
1763 edge_port->icount.rx += urb->actual_length;
1767 /* continue read unless stopped */
1768 spin_lock(&edge_port->ep_lock);
1769 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING) {
1770 urb->dev = edge_port->port->serial->dev;
1771 retval = usb_submit_urb(urb, GFP_ATOMIC);
1772 } else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING) {
1773 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
1775 spin_unlock(&edge_port->ep_lock);
1777 dev_err(&urb->dev->dev,
1778 "%s - usb_submit_urb failed with result %d\n",
1782 static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
1783 unsigned char *data, int length)
1787 tty_buffer_request_room(tty, 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;
1801 dbg("%s - port %d", __func__, port->number);
1803 edge_port->ep_write_urb_in_use = 0;
1812 /* this urb is terminated, clean up */
1813 dbg("%s - urb shutting down with status: %d",
1817 dev_err(&urb->dev->dev, "%s - nonzero write bulk status "
1818 "received: %d\n", __func__, status);
1821 /* send any buffered data */
1825 static int edge_open(struct usb_serial_port *port, struct file *filp)
1827 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1828 struct edgeport_serial *edge_serial;
1829 struct usb_device *dev;
1834 u8 transaction_timeout;
1836 dbg("%s - port %d", __func__, port->number);
1838 if (edge_port == NULL)
1841 port->tty->low_latency = low_latency;
1843 port_number = port->number - port->serial->minor;
1844 switch (port_number) {
1846 edge_port->uart_base = UMPMEM_BASE_UART1;
1847 edge_port->dma_address = UMPD_OEDB1_ADDRESS;
1850 edge_port->uart_base = UMPMEM_BASE_UART2;
1851 edge_port->dma_address = UMPD_OEDB2_ADDRESS;
1854 dev_err(&port->dev, "Unknown port number!!!\n");
1858 dbg("%s - port_number = %d, uart_base = %04x, dma_address = %04x",
1859 __func__, port_number, edge_port->uart_base,
1860 edge_port->dma_address);
1862 dev = port->serial->dev;
1864 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
1865 init_waitqueue_head(&edge_port->delta_msr_wait);
1867 /* turn off loopback */
1868 status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0);
1871 "%s - cannot send clear loopback command, %d\n",
1876 /* set up the port settings */
1877 edge_set_termios(port, port->tty->termios);
1879 /* open up the port */
1881 /* milliseconds to timeout for DMA transfer */
1882 transaction_timeout = 2;
1884 edge_port->ump_read_timeout =
1885 max(20, ((transaction_timeout * 3) / 2));
1887 /* milliseconds to timeout for DMA transfer */
1888 open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
1889 UMP_PIPE_TRANS_TIMEOUT_ENA |
1890 (transaction_timeout << 2));
1892 dbg("%s - Sending UMPC_OPEN_PORT", __func__);
1894 /* Tell TI to open and start the port */
1895 status = send_cmd(dev, UMPC_OPEN_PORT,
1896 (u8)(UMPM_UART1_PORT + port_number), open_settings, NULL, 0);
1898 dev_err(&port->dev, "%s - cannot send open command, %d\n",
1903 /* Start the DMA? */
1904 status = send_cmd(dev, UMPC_START_PORT,
1905 (u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
1907 dev_err(&port->dev, "%s - cannot send start DMA command, %d\n",
1912 /* Clear TX and RX buffers in UMP */
1913 status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
1916 "%s - cannot send clear buffers command, %d\n",
1921 /* Read Initial MSR */
1922 status = ti_vread_sync(dev, UMPC_READ_MSR, 0,
1923 (__u16)(UMPM_UART1_PORT + port_number),
1924 &edge_port->shadow_msr, 1);
1926 dev_err(&port->dev, "%s - cannot send read MSR command, %d\n",
1931 dbg("ShadowMSR 0x%X", edge_port->shadow_msr);
1933 /* Set Initial MCR */
1934 edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
1935 dbg("ShadowMCR 0x%X", edge_port->shadow_mcr);
1937 edge_serial = edge_port->edge_serial;
1938 if (mutex_lock_interruptible(&edge_serial->es_lock))
1939 return -ERESTARTSYS;
1940 if (edge_serial->num_ports_open == 0) {
1941 /* we are the first port to open, post the interrupt urb */
1942 urb = edge_serial->serial->port[0]->interrupt_in_urb;
1945 "%s - no interrupt urb present, exiting\n",
1948 goto release_es_lock;
1950 urb->complete = edge_interrupt_callback;
1951 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->complete = edge_bulk_in_callback;
1979 urb->context = edge_port;
1981 status = usb_submit_urb(urb, GFP_KERNEL);
1984 "%s - read bulk usb_submit_urb failed with value %d\n",
1986 goto unlink_int_urb;
1989 ++edge_serial->num_ports_open;
1991 dbg("%s - exited", __func__);
1993 goto release_es_lock;
1996 if (edge_port->edge_serial->num_ports_open == 0)
1997 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
1999 mutex_unlock(&edge_serial->es_lock);
2003 static void edge_close(struct usb_serial_port *port, struct file *filp)
2005 struct edgeport_serial *edge_serial;
2006 struct edgeport_port *edge_port;
2010 dbg("%s - port %d", __func__, port->number);
2012 edge_serial = usb_get_serial_data(port->serial);
2013 edge_port = usb_get_serial_port_data(port);
2014 if (edge_serial == NULL || edge_port == NULL)
2017 /* The bulkreadcompletion routine will check
2018 * this flag and dump add read data */
2019 edge_port->close_pending = 1;
2021 /* chase the port close and flush */
2022 chase_port(edge_port, (HZ * closing_wait) / 100, 1);
2024 usb_kill_urb(port->read_urb);
2025 usb_kill_urb(port->write_urb);
2026 edge_port->ep_write_urb_in_use = 0;
2028 /* assuming we can still talk to the device,
2029 * send a close port command to it */
2030 dbg("%s - send umpc_close_port", __func__);
2031 port_number = port->number - port->serial->minor;
2032 status = send_cmd(port->serial->dev,
2034 (__u8)(UMPM_UART1_PORT + port_number),
2038 mutex_lock(&edge_serial->es_lock);
2039 --edge_port->edge_serial->num_ports_open;
2040 if (edge_port->edge_serial->num_ports_open <= 0) {
2041 /* last port is now closed, let's shut down our interrupt urb */
2042 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2043 edge_port->edge_serial->num_ports_open = 0;
2045 mutex_unlock(&edge_serial->es_lock);
2046 edge_port->close_pending = 0;
2048 dbg("%s - exited", __func__);
2051 static int edge_write(struct usb_serial_port *port, const unsigned char *data,
2054 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2055 unsigned long flags;
2057 dbg("%s - port %d", __func__, port->number);
2060 dbg("%s - write request of 0 bytes", __func__);
2064 if (edge_port == NULL)
2066 if (edge_port->close_pending == 1)
2069 spin_lock_irqsave(&edge_port->ep_lock, flags);
2070 count = edge_buf_put(edge_port->ep_out_buf, data, count);
2071 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2078 static void edge_send(struct usb_serial_port *port)
2081 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2082 struct tty_struct *tty = port->tty;
2083 unsigned long flags;
2086 dbg("%s - port %d", __func__, port->number);
2088 spin_lock_irqsave(&edge_port->ep_lock, flags);
2090 if (edge_port->ep_write_urb_in_use) {
2091 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2095 count = edge_buf_get(edge_port->ep_out_buf,
2096 port->write_urb->transfer_buffer,
2097 port->bulk_out_size);
2100 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2104 edge_port->ep_write_urb_in_use = 1;
2106 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2108 usb_serial_debug_data(debug, &port->dev, __func__, count,
2109 port->write_urb->transfer_buffer);
2111 /* set up our urb */
2112 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
2113 usb_sndbulkpipe(port->serial->dev,
2114 port->bulk_out_endpointAddress),
2115 port->write_urb->transfer_buffer, count,
2116 edge_bulk_out_callback,
2119 /* send the data out the bulk port */
2120 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2123 "%s - failed submitting write urb, error %d\n",
2125 edge_port->ep_write_urb_in_use = 0;
2126 /* TODO: reschedule edge_send */
2128 edge_port->icount.tx += count;
2130 /* wakeup any process waiting for writes to complete */
2131 /* there is now more room in the buffer for new writes */
2136 static int edge_write_room(struct usb_serial_port *port)
2138 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2140 unsigned long flags;
2142 dbg("%s - port %d", __func__, port->number);
2144 if (edge_port == NULL)
2146 if (edge_port->close_pending == 1)
2149 spin_lock_irqsave(&edge_port->ep_lock, flags);
2150 room = edge_buf_space_avail(edge_port->ep_out_buf);
2151 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2153 dbg("%s - returns %d", __func__, room);
2157 static int edge_chars_in_buffer(struct usb_serial_port *port)
2159 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2161 unsigned long flags;
2163 dbg("%s - port %d", __func__, port->number);
2165 if (edge_port == NULL)
2167 if (edge_port->close_pending == 1)
2170 spin_lock_irqsave(&edge_port->ep_lock, flags);
2171 chars = edge_buf_data_avail(edge_port->ep_out_buf);
2172 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2174 dbg("%s - returns %d", __func__, chars);
2178 static void edge_throttle(struct usb_serial_port *port)
2180 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2181 struct tty_struct *tty = port->tty;
2184 dbg("%s - port %d", __func__, port->number);
2186 if (edge_port == NULL)
2189 /* if we are implementing XON/XOFF, send the stop character */
2191 unsigned char stop_char = STOP_CHAR(tty);
2192 status = edge_write(port, &stop_char, 1);
2195 "%s - failed to write stop character, %d\n",
2199 /* if we are implementing RTS/CTS, stop reads */
2200 /* and the Edgeport will clear the RTS line */
2202 stop_read(edge_port);
2206 static void edge_unthrottle(struct usb_serial_port *port)
2208 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2209 struct tty_struct *tty = port->tty;
2212 dbg("%s - port %d", __func__, port->number);
2214 if (edge_port == NULL)
2217 /* if we are implementing XON/XOFF, send the start character */
2219 unsigned char start_char = START_CHAR(tty);
2220 status = edge_write(port, &start_char, 1);
2223 "%s - failed to write start character, %d\n",
2226 /* if we are implementing RTS/CTS, restart reads */
2227 /* are the Edgeport will assert the RTS line */
2228 if (C_CRTSCTS(tty)) {
2229 status = restart_read(edge_port);
2232 "%s - read bulk usb_submit_urb failed: %d\n",
2238 static void stop_read(struct edgeport_port *edge_port)
2240 unsigned long flags;
2242 spin_lock_irqsave(&edge_port->ep_lock, flags);
2244 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
2245 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
2246 edge_port->shadow_mcr &= ~MCR_RTS;
2248 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2251 static int restart_read(struct edgeport_port *edge_port)
2255 unsigned long flags;
2257 spin_lock_irqsave(&edge_port->ep_lock, flags);
2259 if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
2260 urb = edge_port->port->read_urb;
2261 urb->complete = edge_bulk_in_callback;
2262 urb->context = edge_port;
2263 urb->dev = edge_port->port->serial->dev;
2264 status = usb_submit_urb(urb, GFP_ATOMIC);
2266 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2267 edge_port->shadow_mcr |= MCR_RTS;
2269 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2274 static void change_port_settings(struct edgeport_port *edge_port,
2275 struct ktermios *old_termios)
2277 struct ump_uart_config *config;
2278 struct tty_struct *tty;
2282 int port_number = edge_port->port->number -
2283 edge_port->port->serial->minor;
2285 dbg("%s - port %d", __func__, edge_port->port->number);
2287 tty = edge_port->port->tty;
2289 config = kmalloc(sizeof(*config), GFP_KERNEL);
2291 *tty->termios = *old_termios;
2292 dev_err(&edge_port->port->dev, "%s - out of memory\n",
2297 cflag = tty->termios->c_cflag;
2301 /* These flags must be set */
2302 config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
2303 config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
2304 config->bUartMode = (__u8)(edge_port->bUartMode);
2306 switch (cflag & CSIZE) {
2308 config->bDataBits = UMP_UART_CHAR5BITS;
2309 dbg("%s - data bits = 5", __func__);
2312 config->bDataBits = UMP_UART_CHAR6BITS;
2313 dbg("%s - data bits = 6", __func__);
2316 config->bDataBits = UMP_UART_CHAR7BITS;
2317 dbg("%s - data bits = 7", __func__);
2321 config->bDataBits = UMP_UART_CHAR8BITS;
2322 dbg("%s - data bits = 8", __func__);
2326 if (cflag & PARENB) {
2327 if (cflag & PARODD) {
2328 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2329 config->bParity = UMP_UART_ODDPARITY;
2330 dbg("%s - parity = odd", __func__);
2332 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2333 config->bParity = UMP_UART_EVENPARITY;
2334 dbg("%s - parity = even", __func__);
2337 config->bParity = UMP_UART_NOPARITY;
2338 dbg("%s - parity = none", __func__);
2341 if (cflag & CSTOPB) {
2342 config->bStopBits = UMP_UART_STOPBIT2;
2343 dbg("%s - stop bits = 2", __func__);
2345 config->bStopBits = UMP_UART_STOPBIT1;
2346 dbg("%s - stop bits = 1", __func__);
2349 /* figure out the flow control settings */
2350 if (cflag & CRTSCTS) {
2351 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
2352 config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
2353 dbg("%s - RTS/CTS is enabled", __func__);
2355 dbg("%s - RTS/CTS is disabled", __func__);
2356 tty->hw_stopped = 0;
2357 restart_read(edge_port);
2360 /* if we are implementing XON/XOFF, set the start and stop
2361 character in the device */
2362 config->cXon = START_CHAR(tty);
2363 config->cXoff = STOP_CHAR(tty);
2365 /* if we are implementing INBOUND XON/XOFF */
2367 config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
2368 dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2369 __func__, config->cXon, config->cXoff);
2371 dbg("%s - INBOUND XON/XOFF is disabled", __func__);
2373 /* if we are implementing OUTBOUND XON/XOFF */
2375 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
2376 dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2377 __func__, config->cXon, config->cXoff);
2379 dbg("%s - OUTBOUND XON/XOFF is disabled", __func__);
2381 tty->termios->c_cflag &= ~CMSPAR;
2383 /* Round the baud rate */
2384 baud = tty_get_baud_rate(tty);
2386 /* pick a default, any default... */
2389 tty_encode_baud_rate(tty, baud, baud);
2391 edge_port->baud_rate = baud;
2392 config->wBaudRate = (__u16)((461550L + baud/2) / baud);
2394 /* FIXME: Recompute actual baud from divisor here */
2396 dbg("%s - baud rate = %d, wBaudRate = %d", __func__, baud,
2399 dbg("wBaudRate: %d", (int)(461550L / config->wBaudRate));
2400 dbg("wFlags: 0x%x", config->wFlags);
2401 dbg("bDataBits: %d", config->bDataBits);
2402 dbg("bParity: %d", config->bParity);
2403 dbg("bStopBits: %d", config->bStopBits);
2404 dbg("cXon: %d", config->cXon);
2405 dbg("cXoff: %d", config->cXoff);
2406 dbg("bUartMode: %d", config->bUartMode);
2408 /* move the word values into big endian mode */
2409 cpu_to_be16s(&config->wFlags);
2410 cpu_to_be16s(&config->wBaudRate);
2412 status = send_cmd(edge_port->port->serial->dev, UMPC_SET_CONFIG,
2413 (__u8)(UMPM_UART1_PORT + port_number),
2414 0, (__u8 *)config, sizeof(*config));
2416 dbg("%s - error %d when trying to write config to device",
2422 static void edge_set_termios(struct usb_serial_port *port,
2423 struct ktermios *old_termios)
2425 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2426 struct tty_struct *tty = port->tty;
2428 dbg("%s - clfag %08x iflag %08x", __func__,
2429 tty->termios->c_cflag, tty->termios->c_iflag);
2430 dbg("%s - old clfag %08x old iflag %08x", __func__,
2431 old_termios->c_cflag, old_termios->c_iflag);
2432 dbg("%s - port %d", __func__, port->number);
2434 if (edge_port == NULL)
2436 /* change the port settings to the new ones specified */
2437 change_port_settings(edge_port, old_termios);
2440 static int edge_tiocmset(struct usb_serial_port *port, struct file *file,
2441 unsigned int set, unsigned int clear)
2443 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2445 unsigned long flags;
2447 dbg("%s - port %d", __func__, port->number);
2449 spin_lock_irqsave(&edge_port->ep_lock, flags);
2450 mcr = edge_port->shadow_mcr;
2451 if (set & TIOCM_RTS)
2453 if (set & TIOCM_DTR)
2455 if (set & TIOCM_LOOP)
2456 mcr |= MCR_LOOPBACK;
2458 if (clear & TIOCM_RTS)
2460 if (clear & TIOCM_DTR)
2462 if (clear & TIOCM_LOOP)
2463 mcr &= ~MCR_LOOPBACK;
2465 edge_port->shadow_mcr = mcr;
2466 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2468 restore_mcr(edge_port, mcr);
2472 static int edge_tiocmget(struct usb_serial_port *port, struct file *file)
2474 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2475 unsigned int result = 0;
2478 unsigned long flags;
2480 dbg("%s - port %d", __func__, port->number);
2482 spin_lock_irqsave(&edge_port->ep_lock, flags);
2484 msr = edge_port->shadow_msr;
2485 mcr = edge_port->shadow_mcr;
2486 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
2487 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
2488 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
2489 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
2490 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
2491 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
2494 dbg("%s -- %x", __func__, result);
2495 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2500 static int get_serial_info(struct edgeport_port *edge_port,
2501 struct serial_struct __user *retinfo)
2503 struct serial_struct tmp;
2508 memset(&tmp, 0, sizeof(tmp));
2510 tmp.type = PORT_16550A;
2511 tmp.line = edge_port->port->serial->minor;
2512 tmp.port = edge_port->port->number;
2514 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2515 tmp.xmit_fifo_size = edge_port->port->bulk_out_size;
2516 tmp.baud_base = 9600;
2517 tmp.close_delay = 5*HZ;
2518 tmp.closing_wait = closing_wait;
2520 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2525 static int edge_ioctl(struct usb_serial_port *port, struct file *file,
2526 unsigned int cmd, unsigned long arg)
2528 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2529 struct async_icount cnow;
2530 struct async_icount cprev;
2532 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
2536 dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
2537 return get_serial_info(edge_port,
2538 (struct serial_struct __user *) arg);
2540 dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
2541 cprev = edge_port->icount;
2543 interruptible_sleep_on(&edge_port->delta_msr_wait);
2544 /* see if a signal did it */
2545 if (signal_pending(current))
2546 return -ERESTARTSYS;
2547 cnow = edge_port->icount;
2548 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2549 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2550 return -EIO; /* no change => error */
2551 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2552 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2553 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2554 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2562 dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
2563 port->number, edge_port->icount.rx, edge_port->icount.tx);
2564 if (copy_to_user((void __user *)arg, &edge_port->icount,
2565 sizeof(edge_port->icount)))
2569 return -ENOIOCTLCMD;
2572 static void edge_break(struct usb_serial_port *port, int on)
2574 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2576 int bv = 0; /* Off */
2578 dbg("%s - state = %d", __func__, on);
2580 /* chase the port close */
2581 chase_port(edge_port, 0, 0);
2585 status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv);
2587 dbg("%s - error %d sending break set/clear command.",
2591 static int edge_startup(struct usb_serial *serial)
2593 struct edgeport_serial *edge_serial;
2594 struct edgeport_port *edge_port;
2595 struct usb_device *dev;
2601 /* create our private serial structure */
2602 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
2603 if (edge_serial == NULL) {
2604 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
2607 mutex_init(&edge_serial->es_lock);
2608 edge_serial->serial = serial;
2609 usb_set_serial_data(serial, edge_serial);
2611 status = download_fw(edge_serial);
2617 /* set up our port private structures */
2618 for (i = 0; i < serial->num_ports; ++i) {
2619 edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
2620 if (edge_port == NULL) {
2621 dev_err(&serial->dev->dev, "%s - Out of memory\n",
2625 spin_lock_init(&edge_port->ep_lock);
2626 edge_port->ep_out_buf = edge_buf_alloc(EDGE_OUT_BUF_SIZE);
2627 if (edge_port->ep_out_buf == NULL) {
2628 dev_err(&serial->dev->dev, "%s - Out of memory\n",
2633 edge_port->port = serial->port[i];
2634 edge_port->edge_serial = edge_serial;
2635 usb_set_serial_port_data(serial->port[i], edge_port);
2636 edge_port->bUartMode = default_uart_mode;
2642 for (--i; i >= 0; --i) {
2643 edge_port = usb_get_serial_port_data(serial->port[i]);
2644 edge_buf_free(edge_port->ep_out_buf);
2646 usb_set_serial_port_data(serial->port[i], NULL);
2649 usb_set_serial_data(serial, NULL);
2653 static void edge_shutdown(struct usb_serial *serial)
2656 struct edgeport_port *edge_port;
2658 dbg("%s", __func__);
2660 for (i = 0; i < serial->num_ports; ++i) {
2661 edge_port = usb_get_serial_port_data(serial->port[i]);
2662 edge_remove_sysfs_attrs(edge_port->port);
2663 edge_buf_free(edge_port->ep_out_buf);
2665 usb_set_serial_port_data(serial->port[i], NULL);
2667 kfree(usb_get_serial_data(serial));
2668 usb_set_serial_data(serial, NULL);
2672 /* Sysfs Attributes */
2674 static ssize_t show_uart_mode(struct device *dev,
2675 struct device_attribute *attr, char *buf)
2677 struct usb_serial_port *port = to_usb_serial_port(dev);
2678 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2680 return sprintf(buf, "%d\n", edge_port->bUartMode);
2683 static ssize_t store_uart_mode(struct device *dev,
2684 struct device_attribute *attr, const char *valbuf, size_t count)
2686 struct usb_serial_port *port = to_usb_serial_port(dev);
2687 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2688 unsigned int v = simple_strtoul(valbuf, NULL, 0);
2690 dbg("%s: setting uart_mode = %d", __func__, v);
2693 edge_port->bUartMode = v;
2695 dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
2700 static DEVICE_ATTR(uart_mode, S_IWUSR | S_IRUGO, show_uart_mode,
2703 static int edge_create_sysfs_attrs(struct usb_serial_port *port)
2705 return device_create_file(&port->dev, &dev_attr_uart_mode);
2708 static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
2710 device_remove_file(&port->dev, &dev_attr_uart_mode);
2715 /* Circular Buffer */
2720 * Allocate a circular buffer and all associated memory.
2723 static struct edge_buf *edge_buf_alloc(unsigned int size)
2725 struct edge_buf *eb;
2731 eb = kmalloc(sizeof(struct edge_buf), GFP_KERNEL);
2735 eb->buf_buf = kmalloc(size, GFP_KERNEL);
2736 if (eb->buf_buf == NULL) {
2741 eb->buf_size = size;
2742 eb->buf_get = eb->buf_put = eb->buf_buf;
2751 * Free the buffer and all associated memory.
2754 static void edge_buf_free(struct edge_buf *eb)
2766 * Clear out all data in the circular buffer.
2769 static void edge_buf_clear(struct edge_buf *eb)
2772 eb->buf_get = eb->buf_put;
2773 /* equivalent to a get of all data available */
2778 * edge_buf_data_avail
2780 * Return the number of bytes of data available in the circular
2784 static unsigned int edge_buf_data_avail(struct edge_buf *eb)
2788 return ((eb->buf_size + eb->buf_put - eb->buf_get) % eb->buf_size);
2793 * edge_buf_space_avail
2795 * Return the number of bytes of space available in the circular
2799 static unsigned int edge_buf_space_avail(struct edge_buf *eb)
2803 return ((eb->buf_size + eb->buf_get - eb->buf_put - 1) % eb->buf_size);
2810 * Copy data data from a user buffer and put it into the circular buffer.
2811 * Restrict to the amount of space available.
2813 * Return the number of bytes copied.
2816 static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
2825 len = edge_buf_space_avail(eb);
2832 len = eb->buf_buf + eb->buf_size - eb->buf_put;
2834 memcpy(eb->buf_put, buf, len);
2835 memcpy(eb->buf_buf, buf+len, count - len);
2836 eb->buf_put = eb->buf_buf + count - len;
2838 memcpy(eb->buf_put, buf, count);
2840 eb->buf_put += count;
2841 else /* count == len */
2842 eb->buf_put = eb->buf_buf;
2852 * Get data from the circular buffer and copy to the given buffer.
2853 * Restrict to the amount of data available.
2855 * Return the number of bytes copied.
2858 static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
2867 len = edge_buf_data_avail(eb);
2874 len = eb->buf_buf + eb->buf_size - eb->buf_get;
2876 memcpy(buf, eb->buf_get, len);
2877 memcpy(buf+len, eb->buf_buf, count - len);
2878 eb->buf_get = eb->buf_buf + count - len;
2880 memcpy(buf, eb->buf_get, count);
2882 eb->buf_get += count;
2883 else /* count == len */
2884 eb->buf_get = eb->buf_buf;
2891 static struct usb_serial_driver edgeport_1port_device = {
2893 .owner = THIS_MODULE,
2894 .name = "edgeport_ti_1",
2896 .description = "Edgeport TI 1 port adapter",
2897 .usb_driver = &io_driver,
2898 .id_table = edgeport_1port_id_table,
2901 .close = edge_close,
2902 .throttle = edge_throttle,
2903 .unthrottle = edge_unthrottle,
2904 .attach = edge_startup,
2905 .shutdown = edge_shutdown,
2906 .port_probe = edge_create_sysfs_attrs,
2907 .ioctl = edge_ioctl,
2908 .set_termios = edge_set_termios,
2909 .tiocmget = edge_tiocmget,
2910 .tiocmset = edge_tiocmset,
2911 .write = edge_write,
2912 .write_room = edge_write_room,
2913 .chars_in_buffer = edge_chars_in_buffer,
2914 .break_ctl = edge_break,
2915 .read_int_callback = edge_interrupt_callback,
2916 .read_bulk_callback = edge_bulk_in_callback,
2917 .write_bulk_callback = edge_bulk_out_callback,
2920 static struct usb_serial_driver edgeport_2port_device = {
2922 .owner = THIS_MODULE,
2923 .name = "edgeport_ti_2",
2925 .description = "Edgeport TI 2 port adapter",
2926 .usb_driver = &io_driver,
2927 .id_table = edgeport_2port_id_table,
2930 .close = edge_close,
2931 .throttle = edge_throttle,
2932 .unthrottle = edge_unthrottle,
2933 .attach = edge_startup,
2934 .shutdown = edge_shutdown,
2935 .port_probe = edge_create_sysfs_attrs,
2936 .ioctl = edge_ioctl,
2937 .set_termios = edge_set_termios,
2938 .tiocmget = edge_tiocmget,
2939 .tiocmset = edge_tiocmset,
2940 .write = edge_write,
2941 .write_room = edge_write_room,
2942 .chars_in_buffer = edge_chars_in_buffer,
2943 .break_ctl = edge_break,
2944 .read_int_callback = edge_interrupt_callback,
2945 .read_bulk_callback = edge_bulk_in_callback,
2946 .write_bulk_callback = edge_bulk_out_callback,
2950 static int __init edgeport_init(void)
2953 retval = usb_serial_register(&edgeport_1port_device);
2955 goto failed_1port_device_register;
2956 retval = usb_serial_register(&edgeport_2port_device);
2958 goto failed_2port_device_register;
2959 retval = usb_register(&io_driver);
2961 goto failed_usb_register;
2962 info(DRIVER_DESC " " DRIVER_VERSION);
2964 failed_usb_register:
2965 usb_serial_deregister(&edgeport_2port_device);
2966 failed_2port_device_register:
2967 usb_serial_deregister(&edgeport_1port_device);
2968 failed_1port_device_register:
2972 static void __exit edgeport_exit(void)
2974 usb_deregister(&io_driver);
2975 usb_serial_deregister(&edgeport_1port_device);
2976 usb_serial_deregister(&edgeport_2port_device);
2979 module_init(edgeport_init);
2980 module_exit(edgeport_exit);
2982 /* Module information */
2983 MODULE_AUTHOR(DRIVER_AUTHOR);
2984 MODULE_DESCRIPTION(DRIVER_DESC);
2985 MODULE_LICENSE("GPL");
2986 MODULE_FIRMWARE("edgeport/down3.bin");
2988 module_param(debug, bool, S_IRUGO | S_IWUSR);
2989 MODULE_PARM_DESC(debug, "Debug enabled or not");
2991 module_param(low_latency, bool, S_IRUGO | S_IWUSR);
2992 MODULE_PARM_DESC(low_latency, "Low latency enabled or not");
2994 module_param(closing_wait, int, S_IRUGO | S_IWUSR);
2995 MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
2997 module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
2998 MODULE_PARM_DESC(ignore_cpu_rev,
2999 "Ignore the cpu revision when connecting to a device");
3001 module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
3002 MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");