USB: io_ti: remove unsused private counter
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / serial / io_ti.c
blob2e3d0acbc389a0d19bbabdb61869352c181a1c15
1 /*
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>.
19 * Version history:
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>
45 #include "io_16654.h"
46 #include "io_usbvend.h"
47 #include "io_ti.h"
50 * Version Information
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 /* different hardware types */
60 #define HARDWARE_TYPE_930 0
61 #define HARDWARE_TYPE_TIUMP 1
63 /* IOCTL_PRIVATE_TI_GET_MODE Definitions */
64 #define TI_MODE_CONFIGURING 0 /* Device has not entered start device */
65 #define TI_MODE_BOOT 1 /* Staying in boot mode */
66 #define TI_MODE_DOWNLOAD 2 /* Made it to download mode */
67 #define TI_MODE_TRANSITIONING 3 /* Currently in boot mode but
68 transitioning to download mode */
70 /* read urb state */
71 #define EDGE_READ_URB_RUNNING 0
72 #define EDGE_READ_URB_STOPPING 1
73 #define EDGE_READ_URB_STOPPED 2
75 #define EDGE_CLOSING_WAIT 4000 /* in .01 sec */
77 #define EDGE_OUT_BUF_SIZE 1024
80 /* Product information read from the Edgeport */
81 struct product_info {
82 int TiMode; /* Current TI Mode */
83 __u8 hardware_type; /* Type of hardware */
84 } __attribute__((packed));
86 /* circular buffer */
87 struct edge_buf {
88 unsigned int buf_size;
89 char *buf_buf;
90 char *buf_get;
91 char *buf_put;
94 struct edgeport_port {
95 __u16 uart_base;
96 __u16 dma_address;
97 __u8 shadow_msr;
98 __u8 shadow_mcr;
99 __u8 shadow_lsr;
100 __u8 lsr_mask;
101 __u32 ump_read_timeout; /* Number of milliseconds the UMP will
102 wait without data before completing
103 a read short */
104 int baud_rate;
105 int close_pending;
106 int lsr_event;
107 struct async_icount icount;
108 wait_queue_head_t delta_msr_wait; /* for handling sleeping while
109 waiting for msr change to
110 happen */
111 struct edgeport_serial *edge_serial;
112 struct usb_serial_port *port;
113 __u8 bUartMode; /* Port type, 0: RS232, etc. */
114 spinlock_t ep_lock;
115 int ep_read_urb_state;
116 int ep_write_urb_in_use;
117 struct edge_buf *ep_out_buf;
120 struct edgeport_serial {
121 struct product_info product_info;
122 u8 TI_I2C_Type; /* Type of I2C in UMP */
123 u8 TiReadI2C; /* Set to TRUE if we have read the
124 I2c in Boot Mode */
125 struct mutex es_lock;
126 int num_ports_open;
127 struct usb_serial *serial;
131 /* Devices that this driver supports */
132 static const struct usb_device_id edgeport_1port_id_table[] = {
133 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
134 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
135 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
136 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
137 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
138 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
139 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
140 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
141 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
142 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
143 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
144 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
145 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
146 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
147 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
148 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
152 static const struct usb_device_id edgeport_2port_id_table[] = {
153 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
154 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
155 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
156 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
157 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
158 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
159 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
160 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
161 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
162 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
163 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
164 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
165 /* The 4, 8 and 16 port devices show up as multiple 2 port devices */
166 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
167 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
168 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
169 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
170 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
174 /* Devices that this driver supports */
175 static const struct usb_device_id id_table_combined[] = {
176 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
177 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
178 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
179 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
180 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
181 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
182 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
183 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
184 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
185 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
186 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
187 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
188 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
189 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
190 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
191 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
192 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
193 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
194 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
195 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
196 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
197 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
198 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
199 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
200 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
201 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
202 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
203 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
204 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
205 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
206 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
207 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
208 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
212 MODULE_DEVICE_TABLE(usb, id_table_combined);
214 static struct usb_driver io_driver = {
215 .name = "io_ti",
216 .probe = usb_serial_probe,
217 .disconnect = usb_serial_disconnect,
218 .id_table = id_table_combined,
219 .no_dynamic_id = 1,
223 static unsigned char OperationalMajorVersion;
224 static unsigned char OperationalMinorVersion;
225 static unsigned short OperationalBuildNumber;
227 static int debug;
229 static int closing_wait = EDGE_CLOSING_WAIT;
230 static int ignore_cpu_rev;
231 static int default_uart_mode; /* RS232 */
233 static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
234 unsigned char *data, int length);
236 static void stop_read(struct edgeport_port *edge_port);
237 static int restart_read(struct edgeport_port *edge_port);
239 static void edge_set_termios(struct tty_struct *tty,
240 struct usb_serial_port *port, struct ktermios *old_termios);
241 static void edge_send(struct tty_struct *tty);
243 /* sysfs attributes */
244 static int edge_create_sysfs_attrs(struct usb_serial_port *port);
245 static int edge_remove_sysfs_attrs(struct usb_serial_port *port);
247 /* circular buffer */
248 static struct edge_buf *edge_buf_alloc(unsigned int size);
249 static void edge_buf_free(struct edge_buf *eb);
250 static void edge_buf_clear(struct edge_buf *eb);
251 static unsigned int edge_buf_data_avail(struct edge_buf *eb);
252 static unsigned int edge_buf_space_avail(struct edge_buf *eb);
253 static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
254 unsigned int count);
255 static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
256 unsigned int count);
259 static int ti_vread_sync(struct usb_device *dev, __u8 request,
260 __u16 value, __u16 index, u8 *data, int size)
262 int status;
264 status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
265 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
266 value, index, data, size, 1000);
267 if (status < 0)
268 return status;
269 if (status != size) {
270 dbg("%s - wanted to write %d, but only wrote %d",
271 __func__, size, status);
272 return -ECOMM;
274 return 0;
277 static int ti_vsend_sync(struct usb_device *dev, __u8 request,
278 __u16 value, __u16 index, u8 *data, int size)
280 int status;
282 status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
283 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
284 value, index, data, size, 1000);
285 if (status < 0)
286 return status;
287 if (status != size) {
288 dbg("%s - wanted to write %d, but only wrote %d",
289 __func__, size, status);
290 return -ECOMM;
292 return 0;
295 static int send_cmd(struct usb_device *dev, __u8 command,
296 __u8 moduleid, __u16 value, u8 *data,
297 int size)
299 return ti_vsend_sync(dev, command, value, moduleid, data, size);
302 /* clear tx/rx buffers and fifo in TI UMP */
303 static int purge_port(struct usb_serial_port *port, __u16 mask)
305 int port_number = port->number - port->serial->minor;
307 dbg("%s - port %d, mask %x", __func__, port_number, mask);
309 return send_cmd(port->serial->dev,
310 UMPC_PURGE_PORT,
311 (__u8)(UMPM_UART1_PORT + port_number),
312 mask,
313 NULL,
318 * read_download_mem - Read edgeport memory from TI chip
319 * @dev: usb device pointer
320 * @start_address: Device CPU address at which to read
321 * @length: Length of above data
322 * @address_type: Can read both XDATA and I2C
323 * @buffer: pointer to input data buffer
325 static int read_download_mem(struct usb_device *dev, int start_address,
326 int length, __u8 address_type, __u8 *buffer)
328 int status = 0;
329 __u8 read_length;
330 __be16 be_start_address;
332 dbg("%s - @ %x for %d", __func__, start_address, length);
334 /* Read in blocks of 64 bytes
335 * (TI firmware can't handle more than 64 byte reads)
337 while (length) {
338 if (length > 64)
339 read_length = 64;
340 else
341 read_length = (__u8)length;
343 if (read_length > 1) {
344 dbg("%s - @ %x for %d", __func__,
345 start_address, read_length);
347 be_start_address = cpu_to_be16(start_address);
348 status = ti_vread_sync(dev, UMPC_MEMORY_READ,
349 (__u16)address_type,
350 (__force __u16)be_start_address,
351 buffer, read_length);
353 if (status) {
354 dbg("%s - ERROR %x", __func__, status);
355 return status;
358 if (read_length > 1)
359 usb_serial_debug_data(debug, &dev->dev, __func__,
360 read_length, buffer);
362 /* Update pointers/length */
363 start_address += read_length;
364 buffer += read_length;
365 length -= read_length;
368 return status;
371 static int read_ram(struct usb_device *dev, int start_address,
372 int length, __u8 *buffer)
374 return read_download_mem(dev, start_address, length,
375 DTK_ADDR_SPACE_XDATA, buffer);
378 /* Read edgeport memory to a given block */
379 static int read_boot_mem(struct edgeport_serial *serial,
380 int start_address, int length, __u8 *buffer)
382 int status = 0;
383 int i;
385 for (i = 0; i < length; i++) {
386 status = ti_vread_sync(serial->serial->dev,
387 UMPC_MEMORY_READ, serial->TI_I2C_Type,
388 (__u16)(start_address+i), &buffer[i], 0x01);
389 if (status) {
390 dbg("%s - ERROR %x", __func__, status);
391 return status;
395 dbg("%s - start_address = %x, length = %d",
396 __func__, start_address, length);
397 usb_serial_debug_data(debug, &serial->serial->dev->dev,
398 __func__, length, buffer);
400 serial->TiReadI2C = 1;
402 return status;
405 /* Write given block to TI EPROM memory */
406 static int write_boot_mem(struct edgeport_serial *serial,
407 int start_address, int length, __u8 *buffer)
409 int status = 0;
410 int i;
411 u8 *temp;
413 /* Must do a read before write */
414 if (!serial->TiReadI2C) {
415 temp = kmalloc(1, GFP_KERNEL);
416 if (!temp) {
417 dev_err(&serial->serial->dev->dev,
418 "%s - out of memory\n", __func__);
419 return -ENOMEM;
421 status = read_boot_mem(serial, 0, 1, temp);
422 kfree(temp);
423 if (status)
424 return status;
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);
431 if (status)
432 return status;
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);
440 return status;
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)
448 int status = 0;
449 int write_length;
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);
472 if (status) {
473 dbg("%s - ERROR %d", __func__, status);
474 return 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 */
483 while (length) {
484 if (length > EPROM_PAGE_SIZE)
485 write_length = EPROM_PAGE_SIZE;
486 else
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,
497 (__u16)address_type,
498 (__force __u16)be_start_address,
499 buffer, write_length);
500 if (status) {
501 dev_err(&serial->serial->dev->dev, "%s - ERROR %d\n",
502 __func__, status);
503 return status;
506 length -= write_length;
507 start_address += write_length;
508 buffer += write_length;
510 return status;
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)
521 int status;
522 struct out_endpoint_desc_block *oedb;
523 __u8 *lsr;
524 int bytes_left = 0;
526 oedb = kmalloc(sizeof(*oedb), GFP_KERNEL);
527 if (!oedb) {
528 dev_err(&port->port->dev, "%s - out of memory\n", __func__);
529 return -ENOMEM;
532 lsr = kmalloc(1, GFP_KERNEL); /* Sigh, that's right, just one byte,
533 as not all platforms can do DMA
534 from stack */
535 if (!lsr) {
536 kfree(oedb);
537 return -ENOMEM;
539 /* Read the DMA Count Registers */
540 status = read_ram(port->port->serial->dev, port->dma_address,
541 sizeof(*oedb), (void *)oedb);
542 if (status)
543 goto exit_is_tx_active;
545 dbg("%s - XByteCount 0x%X", __func__, oedb->XByteCount);
547 /* and the LSR */
548 status = read_ram(port->port->serial->dev,
549 port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr);
551 if (status)
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)
557 bytes_left += 64;
559 if ((*lsr & UMP_UART_LSR_TX_MASK) == 0)
560 bytes_left += 1;
562 /* We return Not Active if we get any kind of error */
563 exit_is_tx_active:
564 dbg("%s - return %d", __func__, bytes_left);
566 kfree(lsr);
567 kfree(oedb);
568 return bytes_left;
571 static void chase_port(struct edgeport_port *port, unsigned long timeout,
572 int flush)
574 int baud_rate;
575 struct tty_struct *tty = tty_port_tty_get(&port->port->port);
576 wait_queue_t wait;
577 unsigned long flags;
579 if (!timeout)
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);
586 for (;;) {
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))
591 /* disconnect */
592 break;
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);
599 if (flush)
600 edge_buf_clear(port->ep_out_buf);
601 spin_unlock_irqrestore(&port->ep_lock, flags);
602 tty_kref_put(tty);
604 /* wait for data to drain from the device */
605 timeout += jiffies;
606 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
607 && usb_get_intfdata(port->port->serial->interface)) {
608 /* not disconnected */
609 if (!tx_active(port))
610 break;
611 msleep(10);
614 /* disconnected */
615 if (!usb_get_intfdata(port->port->serial->interface))
616 return;
618 /* wait one more character time, based on baud rate */
619 /* (tx_active doesn't seem to wait for the last byte) */
620 baud_rate = port->baud_rate;
621 if (baud_rate == 0)
622 baud_rate = 50;
623 msleep(max(1, DIV_ROUND_UP(10000, baud_rate)));
626 static int choose_config(struct usb_device *dev)
629 * There may be multiple configurations on this device, in which case
630 * we would need to read and parse all of them to find out which one
631 * we want. However, we just support one config at this point,
632 * configuration # 1, which is Config Descriptor 0.
635 dbg("%s - Number of Interfaces = %d",
636 __func__, dev->config->desc.bNumInterfaces);
637 dbg("%s - MAX Power = %d",
638 __func__, dev->config->desc.bMaxPower * 2);
640 if (dev->config->desc.bNumInterfaces != 1) {
641 dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n",
642 __func__);
643 return -ENODEV;
646 return 0;
649 static int read_rom(struct edgeport_serial *serial,
650 int start_address, int length, __u8 *buffer)
652 int status;
654 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
655 status = read_download_mem(serial->serial->dev,
656 start_address,
657 length,
658 serial->TI_I2C_Type,
659 buffer);
660 } else {
661 status = read_boot_mem(serial, start_address, length,
662 buffer);
664 return status;
667 static int write_rom(struct edgeport_serial *serial, int start_address,
668 int length, __u8 *buffer)
670 if (serial->product_info.TiMode == TI_MODE_BOOT)
671 return write_boot_mem(serial, start_address, length,
672 buffer);
674 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
675 return write_i2c_mem(serial, start_address, length,
676 serial->TI_I2C_Type, buffer);
677 return -EINVAL;
682 /* Read a descriptor header from I2C based on type */
683 static int get_descriptor_addr(struct edgeport_serial *serial,
684 int desc_type, struct ti_i2c_desc *rom_desc)
686 int start_address;
687 int status;
689 /* Search for requested descriptor in I2C */
690 start_address = 2;
691 do {
692 status = read_rom(serial,
693 start_address,
694 sizeof(struct ti_i2c_desc),
695 (__u8 *)rom_desc);
696 if (status)
697 return 0;
699 if (rom_desc->Type == desc_type)
700 return start_address;
702 start_address = start_address + sizeof(struct ti_i2c_desc)
703 + rom_desc->Size;
705 } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
707 return 0;
710 /* Validate descriptor checksum */
711 static int valid_csum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
713 __u16 i;
714 __u8 cs = 0;
716 for (i = 0; i < rom_desc->Size; i++)
717 cs = (__u8)(cs + buffer[i]);
719 if (cs != rom_desc->CheckSum) {
720 dbg("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
721 return -EINVAL;
723 return 0;
726 /* Make sure that the I2C image is good */
727 static int check_i2c_image(struct edgeport_serial *serial)
729 struct device *dev = &serial->serial->dev->dev;
730 int status = 0;
731 struct ti_i2c_desc *rom_desc;
732 int start_address = 2;
733 __u8 *buffer;
734 __u16 ttype;
736 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
737 if (!rom_desc) {
738 dev_err(dev, "%s - out of memory\n", __func__);
739 return -ENOMEM;
741 buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL);
742 if (!buffer) {
743 dev_err(dev, "%s - out of memory when allocating buffer\n",
744 __func__);
745 kfree(rom_desc);
746 return -ENOMEM;
749 /* Read the first byte (Signature0) must be 0x52 or 0x10 */
750 status = read_rom(serial, 0, 1, buffer);
751 if (status)
752 goto out;
754 if (*buffer != UMP5152 && *buffer != UMP3410) {
755 dev_err(dev, "%s - invalid buffer signature\n", __func__);
756 status = -ENODEV;
757 goto out;
760 do {
761 /* Validate the I2C */
762 status = read_rom(serial,
763 start_address,
764 sizeof(struct ti_i2c_desc),
765 (__u8 *)rom_desc);
766 if (status)
767 break;
769 if ((start_address + sizeof(struct ti_i2c_desc) +
770 rom_desc->Size) > TI_MAX_I2C_SIZE) {
771 status = -ENODEV;
772 dbg("%s - structure too big, erroring out.", __func__);
773 break;
776 dbg("%s Type = 0x%x", __func__, rom_desc->Type);
778 /* Skip type 2 record */
779 ttype = rom_desc->Type & 0x0f;
780 if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
781 && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) {
782 /* Read the descriptor data */
783 status = read_rom(serial, start_address +
784 sizeof(struct ti_i2c_desc),
785 rom_desc->Size, buffer);
786 if (status)
787 break;
789 status = valid_csum(rom_desc, buffer);
790 if (status)
791 break;
793 start_address = start_address + sizeof(struct ti_i2c_desc) +
794 rom_desc->Size;
796 } while ((rom_desc->Type != I2C_DESC_TYPE_ION) &&
797 (start_address < TI_MAX_I2C_SIZE));
799 if ((rom_desc->Type != I2C_DESC_TYPE_ION) ||
800 (start_address > TI_MAX_I2C_SIZE))
801 status = -ENODEV;
803 out:
804 kfree(buffer);
805 kfree(rom_desc);
806 return status;
809 static int get_manuf_info(struct edgeport_serial *serial, __u8 *buffer)
811 int status;
812 int start_address;
813 struct ti_i2c_desc *rom_desc;
814 struct edge_ti_manuf_descriptor *desc;
816 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
817 if (!rom_desc) {
818 dev_err(&serial->serial->dev->dev, "%s - out of memory\n",
819 __func__);
820 return -ENOMEM;
822 start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
823 rom_desc);
825 if (!start_address) {
826 dbg("%s - Edge Descriptor not found in I2C", __func__);
827 status = -ENODEV;
828 goto exit;
831 /* Read the descriptor data */
832 status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc),
833 rom_desc->Size, buffer);
834 if (status)
835 goto exit;
837 status = valid_csum(rom_desc, buffer);
839 desc = (struct edge_ti_manuf_descriptor *)buffer;
840 dbg("%s - IonConfig 0x%x", __func__, desc->IonConfig);
841 dbg("%s - Version %d", __func__, desc->Version);
842 dbg("%s - Cpu/Board 0x%x", __func__, desc->CpuRev_BoardRev);
843 dbg("%s - NumPorts %d", __func__, desc->NumPorts);
844 dbg("%s - NumVirtualPorts %d", __func__, desc->NumVirtualPorts);
845 dbg("%s - TotalPorts %d", __func__, desc->TotalPorts);
847 exit:
848 kfree(rom_desc);
849 return status;
852 /* Build firmware header used for firmware update */
853 static int build_i2c_fw_hdr(__u8 *header, struct device *dev)
855 __u8 *buffer;
856 int buffer_size;
857 int i;
858 int err;
859 __u8 cs = 0;
860 struct ti_i2c_desc *i2c_header;
861 struct ti_i2c_image_header *img_header;
862 struct ti_i2c_firmware_rec *firmware_rec;
863 const struct firmware *fw;
864 const char *fw_name = "edgeport/down3.bin";
866 /* In order to update the I2C firmware we must change the type 2 record
867 * to type 0xF2. This will force the UMP to come up in Boot Mode.
868 * Then while in boot mode, the driver will download the latest
869 * firmware (padded to 15.5k) into the UMP ram. And finally when the
870 * device comes back up in download mode the driver will cause the new
871 * firmware to be copied from the UMP Ram to I2C and the firmware will
872 * update the record type from 0xf2 to 0x02.
875 /* Allocate a 15.5k buffer + 2 bytes for version number
876 * (Firmware Record) */
877 buffer_size = (((1024 * 16) - 512 ) +
878 sizeof(struct ti_i2c_firmware_rec));
880 buffer = kmalloc(buffer_size, GFP_KERNEL);
881 if (!buffer) {
882 dev_err(dev, "%s - out of memory\n", __func__);
883 return -ENOMEM;
886 // Set entire image of 0xffs
887 memset(buffer, 0xff, buffer_size);
889 err = request_firmware(&fw, fw_name, dev);
890 if (err) {
891 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
892 fw_name, err);
893 kfree(buffer);
894 return err;
897 /* Save Download Version Number */
898 OperationalMajorVersion = fw->data[0];
899 OperationalMinorVersion = fw->data[1];
900 OperationalBuildNumber = fw->data[2] | (fw->data[3] << 8);
902 /* Copy version number into firmware record */
903 firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
905 firmware_rec->Ver_Major = OperationalMajorVersion;
906 firmware_rec->Ver_Minor = OperationalMinorVersion;
908 /* Pointer to fw_down memory image */
909 img_header = (struct ti_i2c_image_header *)&fw->data[4];
911 memcpy(buffer + sizeof(struct ti_i2c_firmware_rec),
912 &fw->data[4 + sizeof(struct ti_i2c_image_header)],
913 le16_to_cpu(img_header->Length));
915 release_firmware(fw);
917 for (i=0; i < buffer_size; i++) {
918 cs = (__u8)(cs + buffer[i]);
921 kfree(buffer);
923 /* Build new header */
924 i2c_header = (struct ti_i2c_desc *)header;
925 firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data;
927 i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK;
928 i2c_header->Size = (__u16)buffer_size;
929 i2c_header->CheckSum = cs;
930 firmware_rec->Ver_Major = OperationalMajorVersion;
931 firmware_rec->Ver_Minor = OperationalMinorVersion;
933 return 0;
936 /* Try to figure out what type of I2c we have */
937 static int i2c_type_bootmode(struct edgeport_serial *serial)
939 int status;
940 u8 *data;
942 data = kmalloc(1, GFP_KERNEL);
943 if (!data) {
944 dev_err(&serial->serial->dev->dev,
945 "%s - out of memory\n", __func__);
946 return -ENOMEM;
949 /* Try to read type 2 */
950 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
951 DTK_ADDR_SPACE_I2C_TYPE_II, 0, data, 0x01);
952 if (status)
953 dbg("%s - read 2 status error = %d", __func__, status);
954 else
955 dbg("%s - read 2 data = 0x%x", __func__, *data);
956 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
957 dbg("%s - ROM_TYPE_II", __func__);
958 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
959 goto out;
962 /* Try to read type 3 */
963 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
964 DTK_ADDR_SPACE_I2C_TYPE_III, 0, data, 0x01);
965 if (status)
966 dbg("%s - read 3 status error = %d", __func__, status);
967 else
968 dbg("%s - read 2 data = 0x%x", __func__, *data);
969 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
970 dbg("%s - ROM_TYPE_III", __func__);
971 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
972 goto out;
975 dbg("%s - Unknown", __func__);
976 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
977 status = -ENODEV;
978 out:
979 kfree(data);
980 return status;
983 static int bulk_xfer(struct usb_serial *serial, void *buffer,
984 int length, int *num_sent)
986 int status;
988 status = usb_bulk_msg(serial->dev,
989 usb_sndbulkpipe(serial->dev,
990 serial->port[0]->bulk_out_endpointAddress),
991 buffer, length, num_sent, 1000);
992 return status;
995 /* Download given firmware image to the device (IN BOOT MODE) */
996 static int download_code(struct edgeport_serial *serial, __u8 *image,
997 int image_length)
999 int status = 0;
1000 int pos;
1001 int transfer;
1002 int done;
1004 /* Transfer firmware image */
1005 for (pos = 0; pos < image_length; ) {
1006 /* Read the next buffer from file */
1007 transfer = image_length - pos;
1008 if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
1009 transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
1011 /* Transfer data */
1012 status = bulk_xfer(serial->serial, &image[pos],
1013 transfer, &done);
1014 if (status)
1015 break;
1016 /* Advance buffer pointer */
1017 pos += done;
1020 return status;
1023 /* FIXME!!! */
1024 static int config_boot_dev(struct usb_device *dev)
1026 return 0;
1029 static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc)
1031 return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev);
1035 * DownloadTIFirmware - Download run-time operating firmware to the TI5052
1037 * This routine downloads the main operating code into the TI5052, using the
1038 * boot code already burned into E2PROM or ROM.
1040 static int download_fw(struct edgeport_serial *serial)
1042 struct device *dev = &serial->serial->dev->dev;
1043 int status = 0;
1044 int start_address;
1045 struct edge_ti_manuf_descriptor *ti_manuf_desc;
1046 struct usb_interface_descriptor *interface;
1047 int download_cur_ver;
1048 int download_new_ver;
1050 /* This routine is entered by both the BOOT mode and the Download mode
1051 * We can determine which code is running by the reading the config
1052 * descriptor and if we have only one bulk pipe it is in boot mode
1054 serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
1056 /* Default to type 2 i2c */
1057 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1059 status = choose_config(serial->serial->dev);
1060 if (status)
1061 return status;
1063 interface = &serial->serial->interface->cur_altsetting->desc;
1064 if (!interface) {
1065 dev_err(dev, "%s - no interface set, error!\n", __func__);
1066 return -ENODEV;
1070 * Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
1071 * if we have more than one endpoint we are definitely in download
1072 * mode
1074 if (interface->bNumEndpoints > 1)
1075 serial->product_info.TiMode = TI_MODE_DOWNLOAD;
1076 else
1077 /* Otherwise we will remain in configuring mode */
1078 serial->product_info.TiMode = TI_MODE_CONFIGURING;
1080 /********************************************************************/
1081 /* Download Mode */
1082 /********************************************************************/
1083 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
1084 struct ti_i2c_desc *rom_desc;
1086 dbg("%s - RUNNING IN DOWNLOAD MODE", __func__);
1088 status = check_i2c_image(serial);
1089 if (status) {
1090 dbg("%s - DOWNLOAD MODE -- BAD I2C", __func__);
1091 return status;
1094 /* Validate Hardware version number
1095 * Read Manufacturing Descriptor from TI Based Edgeport
1097 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
1098 if (!ti_manuf_desc) {
1099 dev_err(dev, "%s - out of memory.\n", __func__);
1100 return -ENOMEM;
1102 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
1103 if (status) {
1104 kfree(ti_manuf_desc);
1105 return status;
1108 /* Check version number of ION descriptor */
1109 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1110 dbg("%s - Wrong CPU Rev %d (Must be 2)",
1111 __func__, ti_cpu_rev(ti_manuf_desc));
1112 kfree(ti_manuf_desc);
1113 return -EINVAL;
1116 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
1117 if (!rom_desc) {
1118 dev_err(dev, "%s - out of memory.\n", __func__);
1119 kfree(ti_manuf_desc);
1120 return -ENOMEM;
1123 /* Search for type 2 record (firmware record) */
1124 start_address = get_descriptor_addr(serial,
1125 I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc);
1126 if (start_address != 0) {
1127 struct ti_i2c_firmware_rec *firmware_version;
1128 u8 *record;
1130 dbg("%s - Found Type FIRMWARE (Type 2) record",
1131 __func__);
1133 firmware_version = kmalloc(sizeof(*firmware_version),
1134 GFP_KERNEL);
1135 if (!firmware_version) {
1136 dev_err(dev, "%s - out of memory.\n", __func__);
1137 kfree(rom_desc);
1138 kfree(ti_manuf_desc);
1139 return -ENOMEM;
1142 /* Validate version number
1143 * Read the descriptor data
1145 status = read_rom(serial, start_address +
1146 sizeof(struct ti_i2c_desc),
1147 sizeof(struct ti_i2c_firmware_rec),
1148 (__u8 *)firmware_version);
1149 if (status) {
1150 kfree(firmware_version);
1151 kfree(rom_desc);
1152 kfree(ti_manuf_desc);
1153 return status;
1156 /* Check version number of download with current
1157 version in I2c */
1158 download_cur_ver = (firmware_version->Ver_Major << 8) +
1159 (firmware_version->Ver_Minor);
1160 download_new_ver = (OperationalMajorVersion << 8) +
1161 (OperationalMinorVersion);
1163 dbg("%s - >> FW Versions Device %d.%d Driver %d.%d",
1164 __func__,
1165 firmware_version->Ver_Major,
1166 firmware_version->Ver_Minor,
1167 OperationalMajorVersion,
1168 OperationalMinorVersion);
1170 /* Check if we have an old version in the I2C and
1171 update if necessary */
1172 if (download_cur_ver != download_new_ver) {
1173 dbg("%s - Update I2C dld from %d.%d to %d.%d",
1174 __func__,
1175 firmware_version->Ver_Major,
1176 firmware_version->Ver_Minor,
1177 OperationalMajorVersion,
1178 OperationalMinorVersion);
1180 record = kmalloc(1, GFP_KERNEL);
1181 if (!record) {
1182 dev_err(dev, "%s - out of memory.\n",
1183 __func__);
1184 kfree(firmware_version);
1185 kfree(rom_desc);
1186 kfree(ti_manuf_desc);
1187 return -ENOMEM;
1189 /* In order to update the I2C firmware we must
1190 * change the type 2 record to type 0xF2. This
1191 * will force the UMP to come up in Boot Mode.
1192 * Then while in boot mode, the driver will
1193 * download the latest firmware (padded to
1194 * 15.5k) into the UMP ram. Finally when the
1195 * device comes back up in download mode the
1196 * driver will cause the new firmware to be
1197 * copied from the UMP Ram to I2C and the
1198 * firmware will update the record type from
1199 * 0xf2 to 0x02.
1201 *record = I2C_DESC_TYPE_FIRMWARE_BLANK;
1203 /* Change the I2C Firmware record type to
1204 0xf2 to trigger an update */
1205 status = write_rom(serial, start_address,
1206 sizeof(*record), record);
1207 if (status) {
1208 kfree(record);
1209 kfree(firmware_version);
1210 kfree(rom_desc);
1211 kfree(ti_manuf_desc);
1212 return status;
1215 /* verify the write -- must do this in order
1216 * for write to complete before we do the
1217 * hardware reset
1219 status = read_rom(serial,
1220 start_address,
1221 sizeof(*record),
1222 record);
1223 if (status) {
1224 kfree(record);
1225 kfree(firmware_version);
1226 kfree(rom_desc);
1227 kfree(ti_manuf_desc);
1228 return status;
1231 if (*record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
1232 dev_err(dev,
1233 "%s - error resetting device\n",
1234 __func__);
1235 kfree(record);
1236 kfree(firmware_version);
1237 kfree(rom_desc);
1238 kfree(ti_manuf_desc);
1239 return -ENODEV;
1242 dbg("%s - HARDWARE RESET", __func__);
1244 /* Reset UMP -- Back to BOOT MODE */
1245 status = ti_vsend_sync(serial->serial->dev,
1246 UMPC_HARDWARE_RESET,
1247 0, 0, NULL, 0);
1249 dbg("%s - HARDWARE RESET return %d",
1250 __func__, status);
1252 /* return an error on purpose. */
1253 kfree(record);
1254 kfree(firmware_version);
1255 kfree(rom_desc);
1256 kfree(ti_manuf_desc);
1257 return -ENODEV;
1259 kfree(firmware_version);
1261 /* Search for type 0xF2 record (firmware blank record) */
1262 else if ((start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
1263 #define HEADER_SIZE (sizeof(struct ti_i2c_desc) + \
1264 sizeof(struct ti_i2c_firmware_rec))
1265 __u8 *header;
1266 __u8 *vheader;
1268 header = kmalloc(HEADER_SIZE, GFP_KERNEL);
1269 if (!header) {
1270 dev_err(dev, "%s - out of memory.\n", __func__);
1271 kfree(rom_desc);
1272 kfree(ti_manuf_desc);
1273 return -ENOMEM;
1276 vheader = kmalloc(HEADER_SIZE, GFP_KERNEL);
1277 if (!vheader) {
1278 dev_err(dev, "%s - out of memory.\n", __func__);
1279 kfree(header);
1280 kfree(rom_desc);
1281 kfree(ti_manuf_desc);
1282 return -ENOMEM;
1285 dbg("%s - Found Type BLANK FIRMWARE (Type F2) record",
1286 __func__);
1289 * In order to update the I2C firmware we must change
1290 * the type 2 record to type 0xF2. This will force the
1291 * UMP to come up in Boot Mode. Then while in boot
1292 * mode, the driver will download the latest firmware
1293 * (padded to 15.5k) into the UMP ram. Finally when the
1294 * device comes back up in download mode the driver
1295 * will cause the new firmware to be copied from the
1296 * UMP Ram to I2C and the firmware will update the
1297 * record type from 0xf2 to 0x02.
1299 status = build_i2c_fw_hdr(header, dev);
1300 if (status) {
1301 kfree(vheader);
1302 kfree(header);
1303 kfree(rom_desc);
1304 kfree(ti_manuf_desc);
1305 return status;
1308 /* Update I2C with type 0xf2 record with correct
1309 size and checksum */
1310 status = write_rom(serial,
1311 start_address,
1312 HEADER_SIZE,
1313 header);
1314 if (status) {
1315 kfree(vheader);
1316 kfree(header);
1317 kfree(rom_desc);
1318 kfree(ti_manuf_desc);
1319 return status;
1322 /* verify the write -- must do this in order for
1323 write to complete before we do the hardware reset */
1324 status = read_rom(serial, start_address,
1325 HEADER_SIZE, vheader);
1327 if (status) {
1328 dbg("%s - can't read header back", __func__);
1329 kfree(vheader);
1330 kfree(header);
1331 kfree(rom_desc);
1332 kfree(ti_manuf_desc);
1333 return status;
1335 if (memcmp(vheader, header, HEADER_SIZE)) {
1336 dbg("%s - write download record failed",
1337 __func__);
1338 kfree(vheader);
1339 kfree(header);
1340 kfree(rom_desc);
1341 kfree(ti_manuf_desc);
1342 return status;
1345 kfree(vheader);
1346 kfree(header);
1348 dbg("%s - Start firmware update", __func__);
1350 /* Tell firmware to copy download image into I2C */
1351 status = ti_vsend_sync(serial->serial->dev,
1352 UMPC_COPY_DNLD_TO_I2C, 0, 0, NULL, 0);
1354 dbg("%s - Update complete 0x%x", __func__, status);
1355 if (status) {
1356 dev_err(dev,
1357 "%s - UMPC_COPY_DNLD_TO_I2C failed\n",
1358 __func__);
1359 kfree(rom_desc);
1360 kfree(ti_manuf_desc);
1361 return status;
1365 // The device is running the download code
1366 kfree(rom_desc);
1367 kfree(ti_manuf_desc);
1368 return 0;
1371 /********************************************************************/
1372 /* Boot Mode */
1373 /********************************************************************/
1374 dbg("%s - RUNNING IN BOOT MODE", __func__);
1376 /* Configure the TI device so we can use the BULK pipes for download */
1377 status = config_boot_dev(serial->serial->dev);
1378 if (status)
1379 return status;
1381 if (le16_to_cpu(serial->serial->dev->descriptor.idVendor)
1382 != USB_VENDOR_ID_ION) {
1383 dbg("%s - VID = 0x%x", __func__,
1384 le16_to_cpu(serial->serial->dev->descriptor.idVendor));
1385 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1386 goto stayinbootmode;
1389 /* We have an ION device (I2c Must be programmed)
1390 Determine I2C image type */
1391 if (i2c_type_bootmode(serial))
1392 goto stayinbootmode;
1394 /* Check for ION Vendor ID and that the I2C is valid */
1395 if (!check_i2c_image(serial)) {
1396 struct ti_i2c_image_header *header;
1397 int i;
1398 __u8 cs = 0;
1399 __u8 *buffer;
1400 int buffer_size;
1401 int err;
1402 const struct firmware *fw;
1403 const char *fw_name = "edgeport/down3.bin";
1405 /* Validate Hardware version number
1406 * Read Manufacturing Descriptor from TI Based Edgeport
1408 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
1409 if (!ti_manuf_desc) {
1410 dev_err(dev, "%s - out of memory.\n", __func__);
1411 return -ENOMEM;
1413 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
1414 if (status) {
1415 kfree(ti_manuf_desc);
1416 goto stayinbootmode;
1419 /* Check for version 2 */
1420 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1421 dbg("%s - Wrong CPU Rev %d (Must be 2)",
1422 __func__, ti_cpu_rev(ti_manuf_desc));
1423 kfree(ti_manuf_desc);
1424 goto stayinbootmode;
1427 kfree(ti_manuf_desc);
1430 * In order to update the I2C firmware we must change the type
1431 * 2 record to type 0xF2. This will force the UMP to come up
1432 * in Boot Mode. Then while in boot mode, the driver will
1433 * download the latest firmware (padded to 15.5k) into the
1434 * UMP ram. Finally when the device comes back up in download
1435 * mode the driver will cause the new firmware to be copied
1436 * from the UMP Ram to I2C and the firmware will update the
1437 * record type from 0xf2 to 0x02.
1439 * Do we really have to copy the whole firmware image,
1440 * or could we do this in place!
1443 /* Allocate a 15.5k buffer + 3 byte header */
1444 buffer_size = (((1024 * 16) - 512) +
1445 sizeof(struct ti_i2c_image_header));
1446 buffer = kmalloc(buffer_size, GFP_KERNEL);
1447 if (!buffer) {
1448 dev_err(dev, "%s - out of memory\n", __func__);
1449 return -ENOMEM;
1452 /* Initialize the buffer to 0xff (pad the buffer) */
1453 memset(buffer, 0xff, buffer_size);
1455 err = request_firmware(&fw, fw_name, dev);
1456 if (err) {
1457 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
1458 fw_name, err);
1459 kfree(buffer);
1460 return err;
1462 memcpy(buffer, &fw->data[4], fw->size - 4);
1463 release_firmware(fw);
1465 for (i = sizeof(struct ti_i2c_image_header);
1466 i < buffer_size; i++) {
1467 cs = (__u8)(cs + buffer[i]);
1470 header = (struct ti_i2c_image_header *)buffer;
1472 /* update length and checksum after padding */
1473 header->Length = cpu_to_le16((__u16)(buffer_size -
1474 sizeof(struct ti_i2c_image_header)));
1475 header->CheckSum = cs;
1477 /* Download the operational code */
1478 dbg("%s - Downloading operational code image (TI UMP)",
1479 __func__);
1480 status = download_code(serial, buffer, buffer_size);
1482 kfree(buffer);
1484 if (status) {
1485 dbg("%s - Error downloading operational code image",
1486 __func__);
1487 return status;
1490 /* Device will reboot */
1491 serial->product_info.TiMode = TI_MODE_TRANSITIONING;
1493 dbg("%s - Download successful -- Device rebooting...",
1494 __func__);
1496 /* return an error on purpose */
1497 return -ENODEV;
1500 stayinbootmode:
1501 /* Eprom is invalid or blank stay in boot mode */
1502 dbg("%s - STAYING IN BOOT MODE", __func__);
1503 serial->product_info.TiMode = TI_MODE_BOOT;
1505 return 0;
1509 static int ti_do_config(struct edgeport_port *port, int feature, int on)
1511 int port_number = port->port->number - port->port->serial->minor;
1512 on = !!on; /* 1 or 0 not bitmask */
1513 return send_cmd(port->port->serial->dev,
1514 feature, (__u8)(UMPM_UART1_PORT + port_number),
1515 on, NULL, 0);
1519 static int restore_mcr(struct edgeport_port *port, __u8 mcr)
1521 int status = 0;
1523 dbg("%s - %x", __func__, mcr);
1525 status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR);
1526 if (status)
1527 return status;
1528 status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS);
1529 if (status)
1530 return status;
1531 return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK);
1534 /* Convert TI LSR to standard UART flags */
1535 static __u8 map_line_status(__u8 ti_lsr)
1537 __u8 lsr = 0;
1539 #define MAP_FLAG(flagUmp, flagUart) \
1540 if (ti_lsr & flagUmp) \
1541 lsr |= flagUart;
1543 MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR) /* overrun */
1544 MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */
1545 MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */
1546 MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */
1547 MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* rx data available */
1548 MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* tx hold reg empty */
1550 #undef MAP_FLAG
1552 return lsr;
1555 static void handle_new_msr(struct edgeport_port *edge_port, __u8 msr)
1557 struct async_icount *icount;
1558 struct tty_struct *tty;
1560 dbg("%s - %02x", __func__, msr);
1562 if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
1563 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
1564 icount = &edge_port->icount;
1566 /* update input line counters */
1567 if (msr & EDGEPORT_MSR_DELTA_CTS)
1568 icount->cts++;
1569 if (msr & EDGEPORT_MSR_DELTA_DSR)
1570 icount->dsr++;
1571 if (msr & EDGEPORT_MSR_DELTA_CD)
1572 icount->dcd++;
1573 if (msr & EDGEPORT_MSR_DELTA_RI)
1574 icount->rng++;
1575 wake_up_interruptible(&edge_port->delta_msr_wait);
1578 /* Save the new modem status */
1579 edge_port->shadow_msr = msr & 0xf0;
1581 tty = tty_port_tty_get(&edge_port->port->port);
1582 /* handle CTS flow control */
1583 if (tty && C_CRTSCTS(tty)) {
1584 if (msr & EDGEPORT_MSR_CTS) {
1585 tty->hw_stopped = 0;
1586 tty_wakeup(tty);
1587 } else {
1588 tty->hw_stopped = 1;
1591 tty_kref_put(tty);
1593 return;
1596 static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data,
1597 __u8 lsr, __u8 data)
1599 struct async_icount *icount;
1600 __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR |
1601 LSR_FRM_ERR | LSR_BREAK));
1602 struct tty_struct *tty;
1604 dbg("%s - %02x", __func__, new_lsr);
1606 edge_port->shadow_lsr = lsr;
1608 if (new_lsr & LSR_BREAK)
1610 * Parity and Framing errors only count if they
1611 * occur exclusive of a break being received.
1613 new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
1615 /* Place LSR data byte into Rx buffer */
1616 if (lsr_data) {
1617 tty = tty_port_tty_get(&edge_port->port->port);
1618 if (tty) {
1619 edge_tty_recv(&edge_port->port->dev, tty, &data, 1);
1620 tty_kref_put(tty);
1624 /* update input line counters */
1625 icount = &edge_port->icount;
1626 if (new_lsr & LSR_BREAK)
1627 icount->brk++;
1628 if (new_lsr & LSR_OVER_ERR)
1629 icount->overrun++;
1630 if (new_lsr & LSR_PAR_ERR)
1631 icount->parity++;
1632 if (new_lsr & LSR_FRM_ERR)
1633 icount->frame++;
1637 static void edge_interrupt_callback(struct urb *urb)
1639 struct edgeport_serial *edge_serial = urb->context;
1640 struct usb_serial_port *port;
1641 struct edgeport_port *edge_port;
1642 unsigned char *data = urb->transfer_buffer;
1643 int length = urb->actual_length;
1644 int port_number;
1645 int function;
1646 int retval;
1647 __u8 lsr;
1648 __u8 msr;
1649 int status = urb->status;
1651 dbg("%s", __func__);
1653 switch (status) {
1654 case 0:
1655 /* success */
1656 break;
1657 case -ECONNRESET:
1658 case -ENOENT:
1659 case -ESHUTDOWN:
1660 /* this urb is terminated, clean up */
1661 dbg("%s - urb shutting down with status: %d",
1662 __func__, status);
1663 return;
1664 default:
1665 dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
1666 "%d\n", __func__, status);
1667 goto exit;
1670 if (!length) {
1671 dbg("%s - no data in urb", __func__);
1672 goto exit;
1675 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev,
1676 __func__, length, data);
1678 if (length != 2) {
1679 dbg("%s - expecting packet of size 2, got %d",
1680 __func__, length);
1681 goto exit;
1684 port_number = TIUMP_GET_PORT_FROM_CODE(data[0]);
1685 function = TIUMP_GET_FUNC_FROM_CODE(data[0]);
1686 dbg("%s - port_number %d, function %d, info 0x%x",
1687 __func__, port_number, function, data[1]);
1688 port = edge_serial->serial->port[port_number];
1689 edge_port = usb_get_serial_port_data(port);
1690 if (!edge_port) {
1691 dbg("%s - edge_port not found", __func__);
1692 return;
1694 switch (function) {
1695 case TIUMP_INTERRUPT_CODE_LSR:
1696 lsr = map_line_status(data[1]);
1697 if (lsr & UMP_UART_LSR_DATA_MASK) {
1698 /* Save the LSR event for bulk read
1699 completion routine */
1700 dbg("%s - LSR Event Port %u LSR Status = %02x",
1701 __func__, port_number, lsr);
1702 edge_port->lsr_event = 1;
1703 edge_port->lsr_mask = lsr;
1704 } else {
1705 dbg("%s - ===== Port %d LSR Status = %02x ======",
1706 __func__, port_number, lsr);
1707 handle_new_lsr(edge_port, 0, lsr, 0);
1709 break;
1711 case TIUMP_INTERRUPT_CODE_MSR: /* MSR */
1712 /* Copy MSR from UMP */
1713 msr = data[1];
1714 dbg("%s - ===== Port %u MSR Status = %02x ======",
1715 __func__, port_number, msr);
1716 handle_new_msr(edge_port, msr);
1717 break;
1719 default:
1720 dev_err(&urb->dev->dev,
1721 "%s - Unknown Interrupt code from UMP %x\n",
1722 __func__, data[1]);
1723 break;
1727 exit:
1728 retval = usb_submit_urb(urb, GFP_ATOMIC);
1729 if (retval)
1730 dev_err(&urb->dev->dev,
1731 "%s - usb_submit_urb failed with result %d\n",
1732 __func__, retval);
1735 static void edge_bulk_in_callback(struct urb *urb)
1737 struct edgeport_port *edge_port = urb->context;
1738 unsigned char *data = urb->transfer_buffer;
1739 struct tty_struct *tty;
1740 int retval = 0;
1741 int port_number;
1742 int status = urb->status;
1744 dbg("%s", __func__);
1746 switch (status) {
1747 case 0:
1748 /* success */
1749 break;
1750 case -ECONNRESET:
1751 case -ENOENT:
1752 case -ESHUTDOWN:
1753 /* this urb is terminated, clean up */
1754 dbg("%s - urb shutting down with status: %d",
1755 __func__, status);
1756 return;
1757 default:
1758 dev_err(&urb->dev->dev,
1759 "%s - nonzero read bulk status received: %d\n",
1760 __func__, status);
1763 if (status == -EPIPE)
1764 goto exit;
1766 if (status) {
1767 dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__);
1768 return;
1771 port_number = edge_port->port->number - edge_port->port->serial->minor;
1773 if (edge_port->lsr_event) {
1774 edge_port->lsr_event = 0;
1775 dbg("%s ===== Port %u LSR Status = %02x, Data = %02x ======",
1776 __func__, port_number, edge_port->lsr_mask, *data);
1777 handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data);
1778 /* Adjust buffer length/pointer */
1779 --urb->actual_length;
1780 ++data;
1783 tty = tty_port_tty_get(&edge_port->port->port);
1784 if (tty && urb->actual_length) {
1785 usb_serial_debug_data(debug, &edge_port->port->dev,
1786 __func__, urb->actual_length, data);
1787 if (edge_port->close_pending)
1788 dbg("%s - close pending, dropping data on the floor",
1789 __func__);
1790 else
1791 edge_tty_recv(&edge_port->port->dev, tty, data,
1792 urb->actual_length);
1793 edge_port->icount.rx += urb->actual_length;
1795 tty_kref_put(tty);
1797 exit:
1798 /* continue read unless stopped */
1799 spin_lock(&edge_port->ep_lock);
1800 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING) {
1801 urb->dev = edge_port->port->serial->dev;
1802 retval = usb_submit_urb(urb, GFP_ATOMIC);
1803 } else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING) {
1804 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
1806 spin_unlock(&edge_port->ep_lock);
1807 if (retval)
1808 dev_err(&urb->dev->dev,
1809 "%s - usb_submit_urb failed with result %d\n",
1810 __func__, retval);
1813 static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
1814 unsigned char *data, int length)
1816 int queued;
1818 queued = tty_insert_flip_string(tty, data, length);
1819 if (queued < length)
1820 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1821 __func__, length - queued);
1822 tty_flip_buffer_push(tty);
1825 static void edge_bulk_out_callback(struct urb *urb)
1827 struct usb_serial_port *port = urb->context;
1828 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1829 int status = urb->status;
1830 struct tty_struct *tty;
1832 dbg("%s - port %d", __func__, port->number);
1834 edge_port->ep_write_urb_in_use = 0;
1836 switch (status) {
1837 case 0:
1838 /* success */
1839 break;
1840 case -ECONNRESET:
1841 case -ENOENT:
1842 case -ESHUTDOWN:
1843 /* this urb is terminated, clean up */
1844 dbg("%s - urb shutting down with status: %d",
1845 __func__, status);
1846 return;
1847 default:
1848 dev_err(&urb->dev->dev, "%s - nonzero write bulk status "
1849 "received: %d\n", __func__, status);
1852 /* send any buffered data */
1853 tty = tty_port_tty_get(&port->port);
1854 edge_send(tty);
1855 tty_kref_put(tty);
1858 static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
1860 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1861 struct edgeport_serial *edge_serial;
1862 struct usb_device *dev;
1863 struct urb *urb;
1864 int port_number;
1865 int status;
1866 u16 open_settings;
1867 u8 transaction_timeout;
1869 dbg("%s - port %d", __func__, port->number);
1871 if (edge_port == NULL)
1872 return -ENODEV;
1874 port_number = port->number - port->serial->minor;
1875 switch (port_number) {
1876 case 0:
1877 edge_port->uart_base = UMPMEM_BASE_UART1;
1878 edge_port->dma_address = UMPD_OEDB1_ADDRESS;
1879 break;
1880 case 1:
1881 edge_port->uart_base = UMPMEM_BASE_UART2;
1882 edge_port->dma_address = UMPD_OEDB2_ADDRESS;
1883 break;
1884 default:
1885 dev_err(&port->dev, "Unknown port number!!!\n");
1886 return -ENODEV;
1889 dbg("%s - port_number = %d, uart_base = %04x, dma_address = %04x",
1890 __func__, port_number, edge_port->uart_base,
1891 edge_port->dma_address);
1893 dev = port->serial->dev;
1895 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
1896 init_waitqueue_head(&edge_port->delta_msr_wait);
1898 /* turn off loopback */
1899 status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0);
1900 if (status) {
1901 dev_err(&port->dev,
1902 "%s - cannot send clear loopback command, %d\n",
1903 __func__, status);
1904 return status;
1907 /* set up the port settings */
1908 if (tty)
1909 edge_set_termios(tty, port, tty->termios);
1911 /* open up the port */
1913 /* milliseconds to timeout for DMA transfer */
1914 transaction_timeout = 2;
1916 edge_port->ump_read_timeout =
1917 max(20, ((transaction_timeout * 3) / 2));
1919 /* milliseconds to timeout for DMA transfer */
1920 open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
1921 UMP_PIPE_TRANS_TIMEOUT_ENA |
1922 (transaction_timeout << 2));
1924 dbg("%s - Sending UMPC_OPEN_PORT", __func__);
1926 /* Tell TI to open and start the port */
1927 status = send_cmd(dev, UMPC_OPEN_PORT,
1928 (u8)(UMPM_UART1_PORT + port_number), open_settings, NULL, 0);
1929 if (status) {
1930 dev_err(&port->dev, "%s - cannot send open command, %d\n",
1931 __func__, status);
1932 return status;
1935 /* Start the DMA? */
1936 status = send_cmd(dev, UMPC_START_PORT,
1937 (u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
1938 if (status) {
1939 dev_err(&port->dev, "%s - cannot send start DMA command, %d\n",
1940 __func__, status);
1941 return status;
1944 /* Clear TX and RX buffers in UMP */
1945 status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
1946 if (status) {
1947 dev_err(&port->dev,
1948 "%s - cannot send clear buffers command, %d\n",
1949 __func__, status);
1950 return status;
1953 /* Read Initial MSR */
1954 status = ti_vread_sync(dev, UMPC_READ_MSR, 0,
1955 (__u16)(UMPM_UART1_PORT + port_number),
1956 &edge_port->shadow_msr, 1);
1957 if (status) {
1958 dev_err(&port->dev, "%s - cannot send read MSR command, %d\n",
1959 __func__, status);
1960 return status;
1963 dbg("ShadowMSR 0x%X", edge_port->shadow_msr);
1965 /* Set Initial MCR */
1966 edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
1967 dbg("ShadowMCR 0x%X", edge_port->shadow_mcr);
1969 edge_serial = edge_port->edge_serial;
1970 if (mutex_lock_interruptible(&edge_serial->es_lock))
1971 return -ERESTARTSYS;
1972 if (edge_serial->num_ports_open == 0) {
1973 /* we are the first port to open, post the interrupt urb */
1974 urb = edge_serial->serial->port[0]->interrupt_in_urb;
1975 if (!urb) {
1976 dev_err(&port->dev,
1977 "%s - no interrupt urb present, exiting\n",
1978 __func__);
1979 status = -EINVAL;
1980 goto release_es_lock;
1982 urb->complete = edge_interrupt_callback;
1983 urb->context = edge_serial;
1984 urb->dev = dev;
1985 status = usb_submit_urb(urb, GFP_KERNEL);
1986 if (status) {
1987 dev_err(&port->dev,
1988 "%s - usb_submit_urb failed with value %d\n",
1989 __func__, status);
1990 goto release_es_lock;
1995 * reset the data toggle on the bulk endpoints to work around bug in
1996 * host controllers where things get out of sync some times
1998 usb_clear_halt(dev, port->write_urb->pipe);
1999 usb_clear_halt(dev, port->read_urb->pipe);
2001 /* start up our bulk read urb */
2002 urb = port->read_urb;
2003 if (!urb) {
2004 dev_err(&port->dev, "%s - no read urb present, exiting\n",
2005 __func__);
2006 status = -EINVAL;
2007 goto unlink_int_urb;
2009 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2010 urb->complete = edge_bulk_in_callback;
2011 urb->context = edge_port;
2012 urb->dev = dev;
2013 status = usb_submit_urb(urb, GFP_KERNEL);
2014 if (status) {
2015 dev_err(&port->dev,
2016 "%s - read bulk usb_submit_urb failed with value %d\n",
2017 __func__, status);
2018 goto unlink_int_urb;
2021 ++edge_serial->num_ports_open;
2023 dbg("%s - exited", __func__);
2025 goto release_es_lock;
2027 unlink_int_urb:
2028 if (edge_port->edge_serial->num_ports_open == 0)
2029 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2030 release_es_lock:
2031 mutex_unlock(&edge_serial->es_lock);
2032 return status;
2035 static void edge_close(struct usb_serial_port *port)
2037 struct edgeport_serial *edge_serial;
2038 struct edgeport_port *edge_port;
2039 int port_number;
2040 int status;
2042 dbg("%s - port %d", __func__, port->number);
2044 edge_serial = usb_get_serial_data(port->serial);
2045 edge_port = usb_get_serial_port_data(port);
2046 if (edge_serial == NULL || edge_port == NULL)
2047 return;
2049 /* The bulkreadcompletion routine will check
2050 * this flag and dump add read data */
2051 edge_port->close_pending = 1;
2053 /* chase the port close and flush */
2054 chase_port(edge_port, (HZ * closing_wait) / 100, 1);
2056 usb_kill_urb(port->read_urb);
2057 usb_kill_urb(port->write_urb);
2058 edge_port->ep_write_urb_in_use = 0;
2060 /* assuming we can still talk to the device,
2061 * send a close port command to it */
2062 dbg("%s - send umpc_close_port", __func__);
2063 port_number = port->number - port->serial->minor;
2064 status = send_cmd(port->serial->dev,
2065 UMPC_CLOSE_PORT,
2066 (__u8)(UMPM_UART1_PORT + port_number),
2068 NULL,
2070 mutex_lock(&edge_serial->es_lock);
2071 --edge_port->edge_serial->num_ports_open;
2072 if (edge_port->edge_serial->num_ports_open <= 0) {
2073 /* last port is now closed, let's shut down our interrupt urb */
2074 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2075 edge_port->edge_serial->num_ports_open = 0;
2077 mutex_unlock(&edge_serial->es_lock);
2078 edge_port->close_pending = 0;
2080 dbg("%s - exited", __func__);
2083 static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
2084 const unsigned char *data, int count)
2086 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2087 unsigned long flags;
2089 dbg("%s - port %d", __func__, port->number);
2091 if (count == 0) {
2092 dbg("%s - write request of 0 bytes", __func__);
2093 return 0;
2096 if (edge_port == NULL)
2097 return -ENODEV;
2098 if (edge_port->close_pending == 1)
2099 return -ENODEV;
2101 spin_lock_irqsave(&edge_port->ep_lock, flags);
2102 count = edge_buf_put(edge_port->ep_out_buf, data, count);
2103 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2105 edge_send(tty);
2107 return count;
2110 static void edge_send(struct tty_struct *tty)
2112 struct usb_serial_port *port = tty->driver_data;
2113 int count, result;
2114 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2115 unsigned long flags;
2118 dbg("%s - port %d", __func__, port->number);
2120 spin_lock_irqsave(&edge_port->ep_lock, flags);
2122 if (edge_port->ep_write_urb_in_use) {
2123 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2124 return;
2127 count = edge_buf_get(edge_port->ep_out_buf,
2128 port->write_urb->transfer_buffer,
2129 port->bulk_out_size);
2131 if (count == 0) {
2132 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2133 return;
2136 edge_port->ep_write_urb_in_use = 1;
2138 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2140 usb_serial_debug_data(debug, &port->dev, __func__, count,
2141 port->write_urb->transfer_buffer);
2143 /* set up our urb */
2144 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
2145 usb_sndbulkpipe(port->serial->dev,
2146 port->bulk_out_endpointAddress),
2147 port->write_urb->transfer_buffer, count,
2148 edge_bulk_out_callback,
2149 port);
2151 /* send the data out the bulk port */
2152 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2153 if (result) {
2154 dev_err(&port->dev,
2155 "%s - failed submitting write urb, error %d\n",
2156 __func__, result);
2157 edge_port->ep_write_urb_in_use = 0;
2158 /* TODO: reschedule edge_send */
2159 } else
2160 edge_port->icount.tx += count;
2162 /* wakeup any process waiting for writes to complete */
2163 /* there is now more room in the buffer for new writes */
2164 if (tty)
2165 tty_wakeup(tty);
2168 static int edge_write_room(struct tty_struct *tty)
2170 struct usb_serial_port *port = tty->driver_data;
2171 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2172 int room = 0;
2173 unsigned long flags;
2175 dbg("%s - port %d", __func__, port->number);
2177 if (edge_port == NULL)
2178 return 0;
2179 if (edge_port->close_pending == 1)
2180 return 0;
2182 spin_lock_irqsave(&edge_port->ep_lock, flags);
2183 room = edge_buf_space_avail(edge_port->ep_out_buf);
2184 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2186 dbg("%s - returns %d", __func__, room);
2187 return room;
2190 static int edge_chars_in_buffer(struct tty_struct *tty)
2192 struct usb_serial_port *port = tty->driver_data;
2193 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2194 int chars = 0;
2195 unsigned long flags;
2197 dbg("%s - port %d", __func__, port->number);
2199 if (edge_port == NULL)
2200 return 0;
2201 if (edge_port->close_pending == 1)
2202 return 0;
2204 spin_lock_irqsave(&edge_port->ep_lock, flags);
2205 chars = edge_buf_data_avail(edge_port->ep_out_buf);
2206 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2208 dbg("%s - returns %d", __func__, chars);
2209 return chars;
2212 static void edge_throttle(struct tty_struct *tty)
2214 struct usb_serial_port *port = tty->driver_data;
2215 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2216 int status;
2218 dbg("%s - port %d", __func__, port->number);
2220 if (edge_port == NULL)
2221 return;
2223 /* if we are implementing XON/XOFF, send the stop character */
2224 if (I_IXOFF(tty)) {
2225 unsigned char stop_char = STOP_CHAR(tty);
2226 status = edge_write(tty, port, &stop_char, 1);
2227 if (status <= 0) {
2228 dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status);
2232 /* if we are implementing RTS/CTS, stop reads */
2233 /* and the Edgeport will clear the RTS line */
2234 if (C_CRTSCTS(tty))
2235 stop_read(edge_port);
2239 static void edge_unthrottle(struct tty_struct *tty)
2241 struct usb_serial_port *port = tty->driver_data;
2242 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2243 int status;
2245 dbg("%s - port %d", __func__, port->number);
2247 if (edge_port == NULL)
2248 return;
2250 /* if we are implementing XON/XOFF, send the start character */
2251 if (I_IXOFF(tty)) {
2252 unsigned char start_char = START_CHAR(tty);
2253 status = edge_write(tty, port, &start_char, 1);
2254 if (status <= 0) {
2255 dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status);
2258 /* if we are implementing RTS/CTS, restart reads */
2259 /* are the Edgeport will assert the RTS line */
2260 if (C_CRTSCTS(tty)) {
2261 status = restart_read(edge_port);
2262 if (status)
2263 dev_err(&port->dev,
2264 "%s - read bulk usb_submit_urb failed: %d\n",
2265 __func__, status);
2270 static void stop_read(struct edgeport_port *edge_port)
2272 unsigned long flags;
2274 spin_lock_irqsave(&edge_port->ep_lock, flags);
2276 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
2277 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
2278 edge_port->shadow_mcr &= ~MCR_RTS;
2280 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2283 static int restart_read(struct edgeport_port *edge_port)
2285 struct urb *urb;
2286 int status = 0;
2287 unsigned long flags;
2289 spin_lock_irqsave(&edge_port->ep_lock, flags);
2291 if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
2292 urb = edge_port->port->read_urb;
2293 urb->complete = edge_bulk_in_callback;
2294 urb->context = edge_port;
2295 urb->dev = edge_port->port->serial->dev;
2296 status = usb_submit_urb(urb, GFP_ATOMIC);
2298 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2299 edge_port->shadow_mcr |= MCR_RTS;
2301 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2303 return status;
2306 static void change_port_settings(struct tty_struct *tty,
2307 struct edgeport_port *edge_port, struct ktermios *old_termios)
2309 struct ump_uart_config *config;
2310 int baud;
2311 unsigned cflag;
2312 int status;
2313 int port_number = edge_port->port->number -
2314 edge_port->port->serial->minor;
2316 dbg("%s - port %d", __func__, edge_port->port->number);
2318 config = kmalloc (sizeof (*config), GFP_KERNEL);
2319 if (!config) {
2320 *tty->termios = *old_termios;
2321 dev_err(&edge_port->port->dev, "%s - out of memory\n",
2322 __func__);
2323 return;
2326 cflag = tty->termios->c_cflag;
2328 config->wFlags = 0;
2330 /* These flags must be set */
2331 config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
2332 config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
2333 config->bUartMode = (__u8)(edge_port->bUartMode);
2335 switch (cflag & CSIZE) {
2336 case CS5:
2337 config->bDataBits = UMP_UART_CHAR5BITS;
2338 dbg("%s - data bits = 5", __func__);
2339 break;
2340 case CS6:
2341 config->bDataBits = UMP_UART_CHAR6BITS;
2342 dbg("%s - data bits = 6", __func__);
2343 break;
2344 case CS7:
2345 config->bDataBits = UMP_UART_CHAR7BITS;
2346 dbg("%s - data bits = 7", __func__);
2347 break;
2348 default:
2349 case CS8:
2350 config->bDataBits = UMP_UART_CHAR8BITS;
2351 dbg("%s - data bits = 8", __func__);
2352 break;
2355 if (cflag & PARENB) {
2356 if (cflag & PARODD) {
2357 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2358 config->bParity = UMP_UART_ODDPARITY;
2359 dbg("%s - parity = odd", __func__);
2360 } else {
2361 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2362 config->bParity = UMP_UART_EVENPARITY;
2363 dbg("%s - parity = even", __func__);
2365 } else {
2366 config->bParity = UMP_UART_NOPARITY;
2367 dbg("%s - parity = none", __func__);
2370 if (cflag & CSTOPB) {
2371 config->bStopBits = UMP_UART_STOPBIT2;
2372 dbg("%s - stop bits = 2", __func__);
2373 } else {
2374 config->bStopBits = UMP_UART_STOPBIT1;
2375 dbg("%s - stop bits = 1", __func__);
2378 /* figure out the flow control settings */
2379 if (cflag & CRTSCTS) {
2380 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
2381 config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
2382 dbg("%s - RTS/CTS is enabled", __func__);
2383 } else {
2384 dbg("%s - RTS/CTS is disabled", __func__);
2385 tty->hw_stopped = 0;
2386 restart_read(edge_port);
2389 /* if we are implementing XON/XOFF, set the start and stop
2390 character in the device */
2391 config->cXon = START_CHAR(tty);
2392 config->cXoff = STOP_CHAR(tty);
2394 /* if we are implementing INBOUND XON/XOFF */
2395 if (I_IXOFF(tty)) {
2396 config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
2397 dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2398 __func__, config->cXon, config->cXoff);
2399 } else
2400 dbg("%s - INBOUND XON/XOFF is disabled", __func__);
2402 /* if we are implementing OUTBOUND XON/XOFF */
2403 if (I_IXON(tty)) {
2404 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
2405 dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2406 __func__, config->cXon, config->cXoff);
2407 } else
2408 dbg("%s - OUTBOUND XON/XOFF is disabled", __func__);
2410 tty->termios->c_cflag &= ~CMSPAR;
2412 /* Round the baud rate */
2413 baud = tty_get_baud_rate(tty);
2414 if (!baud) {
2415 /* pick a default, any default... */
2416 baud = 9600;
2417 } else
2418 tty_encode_baud_rate(tty, baud, baud);
2420 edge_port->baud_rate = baud;
2421 config->wBaudRate = (__u16)((461550L + baud/2) / baud);
2423 /* FIXME: Recompute actual baud from divisor here */
2425 dbg("%s - baud rate = %d, wBaudRate = %d", __func__, baud,
2426 config->wBaudRate);
2428 dbg("wBaudRate: %d", (int)(461550L / config->wBaudRate));
2429 dbg("wFlags: 0x%x", config->wFlags);
2430 dbg("bDataBits: %d", config->bDataBits);
2431 dbg("bParity: %d", config->bParity);
2432 dbg("bStopBits: %d", config->bStopBits);
2433 dbg("cXon: %d", config->cXon);
2434 dbg("cXoff: %d", config->cXoff);
2435 dbg("bUartMode: %d", config->bUartMode);
2437 /* move the word values into big endian mode */
2438 cpu_to_be16s(&config->wFlags);
2439 cpu_to_be16s(&config->wBaudRate);
2441 status = send_cmd(edge_port->port->serial->dev, UMPC_SET_CONFIG,
2442 (__u8)(UMPM_UART1_PORT + port_number),
2443 0, (__u8 *)config, sizeof(*config));
2444 if (status)
2445 dbg("%s - error %d when trying to write config to device",
2446 __func__, status);
2447 kfree(config);
2448 return;
2451 static void edge_set_termios(struct tty_struct *tty,
2452 struct usb_serial_port *port, struct ktermios *old_termios)
2454 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2455 unsigned int cflag;
2457 cflag = tty->termios->c_cflag;
2459 dbg("%s - clfag %08x iflag %08x", __func__,
2460 tty->termios->c_cflag, tty->termios->c_iflag);
2461 dbg("%s - old clfag %08x old iflag %08x", __func__,
2462 old_termios->c_cflag, old_termios->c_iflag);
2463 dbg("%s - port %d", __func__, port->number);
2465 if (edge_port == NULL)
2466 return;
2467 /* change the port settings to the new ones specified */
2468 change_port_settings(tty, edge_port, old_termios);
2469 return;
2472 static int edge_tiocmset(struct tty_struct *tty, struct file *file,
2473 unsigned int set, unsigned int clear)
2475 struct usb_serial_port *port = tty->driver_data;
2476 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2477 unsigned int mcr;
2478 unsigned long flags;
2480 dbg("%s - port %d", __func__, port->number);
2482 spin_lock_irqsave(&edge_port->ep_lock, flags);
2483 mcr = edge_port->shadow_mcr;
2484 if (set & TIOCM_RTS)
2485 mcr |= MCR_RTS;
2486 if (set & TIOCM_DTR)
2487 mcr |= MCR_DTR;
2488 if (set & TIOCM_LOOP)
2489 mcr |= MCR_LOOPBACK;
2491 if (clear & TIOCM_RTS)
2492 mcr &= ~MCR_RTS;
2493 if (clear & TIOCM_DTR)
2494 mcr &= ~MCR_DTR;
2495 if (clear & TIOCM_LOOP)
2496 mcr &= ~MCR_LOOPBACK;
2498 edge_port->shadow_mcr = mcr;
2499 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2501 restore_mcr(edge_port, mcr);
2502 return 0;
2505 static int edge_tiocmget(struct tty_struct *tty, struct file *file)
2507 struct usb_serial_port *port = tty->driver_data;
2508 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2509 unsigned int result = 0;
2510 unsigned int msr;
2511 unsigned int mcr;
2512 unsigned long flags;
2514 dbg("%s - port %d", __func__, port->number);
2516 spin_lock_irqsave(&edge_port->ep_lock, flags);
2518 msr = edge_port->shadow_msr;
2519 mcr = edge_port->shadow_mcr;
2520 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
2521 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
2522 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
2523 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
2524 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
2525 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
2528 dbg("%s -- %x", __func__, result);
2529 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2531 return result;
2534 static int get_serial_info(struct edgeport_port *edge_port,
2535 struct serial_struct __user *retinfo)
2537 struct serial_struct tmp;
2539 if (!retinfo)
2540 return -EFAULT;
2542 memset(&tmp, 0, sizeof(tmp));
2544 tmp.type = PORT_16550A;
2545 tmp.line = edge_port->port->serial->minor;
2546 tmp.port = edge_port->port->number;
2547 tmp.irq = 0;
2548 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2549 tmp.xmit_fifo_size = edge_port->port->bulk_out_size;
2550 tmp.baud_base = 9600;
2551 tmp.close_delay = 5*HZ;
2552 tmp.closing_wait = closing_wait;
2554 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2555 return -EFAULT;
2556 return 0;
2559 static int edge_ioctl(struct tty_struct *tty, struct file *file,
2560 unsigned int cmd, unsigned long arg)
2562 struct usb_serial_port *port = tty->driver_data;
2563 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2564 struct async_icount cnow;
2565 struct async_icount cprev;
2567 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
2569 switch (cmd) {
2570 case TIOCGSERIAL:
2571 dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
2572 return get_serial_info(edge_port,
2573 (struct serial_struct __user *) arg);
2574 case TIOCMIWAIT:
2575 dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
2576 cprev = edge_port->icount;
2577 while (1) {
2578 interruptible_sleep_on(&edge_port->delta_msr_wait);
2579 /* see if a signal did it */
2580 if (signal_pending(current))
2581 return -ERESTARTSYS;
2582 cnow = edge_port->icount;
2583 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2584 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2585 return -EIO; /* no change => error */
2586 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2587 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2588 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2589 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2590 return 0;
2592 cprev = cnow;
2594 /* not reached */
2595 break;
2596 case TIOCGICOUNT:
2597 dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
2598 port->number, edge_port->icount.rx, edge_port->icount.tx);
2599 if (copy_to_user((void __user *)arg, &edge_port->icount,
2600 sizeof(edge_port->icount)))
2601 return -EFAULT;
2602 return 0;
2604 return -ENOIOCTLCMD;
2607 static void edge_break(struct tty_struct *tty, int break_state)
2609 struct usb_serial_port *port = tty->driver_data;
2610 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2611 int status;
2612 int bv = 0; /* Off */
2614 dbg("%s - state = %d", __func__, break_state);
2616 /* chase the port close */
2617 chase_port(edge_port, 0, 0);
2619 if (break_state == -1)
2620 bv = 1; /* On */
2621 status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv);
2622 if (status)
2623 dbg("%s - error %d sending break set/clear command.",
2624 __func__, status);
2627 static int edge_startup(struct usb_serial *serial)
2629 struct edgeport_serial *edge_serial;
2630 struct edgeport_port *edge_port;
2631 struct usb_device *dev;
2632 int status;
2633 int i;
2635 dev = serial->dev;
2637 /* create our private serial structure */
2638 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
2639 if (edge_serial == NULL) {
2640 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
2641 return -ENOMEM;
2643 mutex_init(&edge_serial->es_lock);
2644 edge_serial->serial = serial;
2645 usb_set_serial_data(serial, edge_serial);
2647 status = download_fw(edge_serial);
2648 if (status) {
2649 kfree(edge_serial);
2650 return status;
2653 /* set up our port private structures */
2654 for (i = 0; i < serial->num_ports; ++i) {
2655 edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
2656 if (edge_port == NULL) {
2657 dev_err(&serial->dev->dev, "%s - Out of memory\n",
2658 __func__);
2659 goto cleanup;
2661 spin_lock_init(&edge_port->ep_lock);
2662 edge_port->ep_out_buf = edge_buf_alloc(EDGE_OUT_BUF_SIZE);
2663 if (edge_port->ep_out_buf == NULL) {
2664 dev_err(&serial->dev->dev, "%s - Out of memory\n",
2665 __func__);
2666 kfree(edge_port);
2667 goto cleanup;
2669 edge_port->port = serial->port[i];
2670 edge_port->edge_serial = edge_serial;
2671 usb_set_serial_port_data(serial->port[i], edge_port);
2672 edge_port->bUartMode = default_uart_mode;
2675 return 0;
2677 cleanup:
2678 for (--i; i >= 0; --i) {
2679 edge_port = usb_get_serial_port_data(serial->port[i]);
2680 edge_buf_free(edge_port->ep_out_buf);
2681 kfree(edge_port);
2682 usb_set_serial_port_data(serial->port[i], NULL);
2684 kfree(edge_serial);
2685 usb_set_serial_data(serial, NULL);
2686 return -ENOMEM;
2689 static void edge_disconnect(struct usb_serial *serial)
2691 int i;
2692 struct edgeport_port *edge_port;
2694 dbg("%s", __func__);
2696 for (i = 0; i < serial->num_ports; ++i) {
2697 edge_port = usb_get_serial_port_data(serial->port[i]);
2698 edge_remove_sysfs_attrs(edge_port->port);
2702 static void edge_release(struct usb_serial *serial)
2704 int i;
2705 struct edgeport_port *edge_port;
2707 dbg("%s", __func__);
2709 for (i = 0; i < serial->num_ports; ++i) {
2710 edge_port = usb_get_serial_port_data(serial->port[i]);
2711 edge_buf_free(edge_port->ep_out_buf);
2712 kfree(edge_port);
2714 kfree(usb_get_serial_data(serial));
2718 /* Sysfs Attributes */
2720 static ssize_t show_uart_mode(struct device *dev,
2721 struct device_attribute *attr, char *buf)
2723 struct usb_serial_port *port = to_usb_serial_port(dev);
2724 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2726 return sprintf(buf, "%d\n", edge_port->bUartMode);
2729 static ssize_t store_uart_mode(struct device *dev,
2730 struct device_attribute *attr, const char *valbuf, size_t count)
2732 struct usb_serial_port *port = to_usb_serial_port(dev);
2733 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2734 unsigned int v = simple_strtoul(valbuf, NULL, 0);
2736 dbg("%s: setting uart_mode = %d", __func__, v);
2738 if (v < 256)
2739 edge_port->bUartMode = v;
2740 else
2741 dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
2743 return count;
2746 static DEVICE_ATTR(uart_mode, S_IWUSR | S_IRUGO, show_uart_mode,
2747 store_uart_mode);
2749 static int edge_create_sysfs_attrs(struct usb_serial_port *port)
2751 return device_create_file(&port->dev, &dev_attr_uart_mode);
2754 static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
2756 device_remove_file(&port->dev, &dev_attr_uart_mode);
2757 return 0;
2761 /* Circular Buffer */
2764 * edge_buf_alloc
2766 * Allocate a circular buffer and all associated memory.
2769 static struct edge_buf *edge_buf_alloc(unsigned int size)
2771 struct edge_buf *eb;
2774 if (size == 0)
2775 return NULL;
2777 eb = kmalloc(sizeof(struct edge_buf), GFP_KERNEL);
2778 if (eb == NULL)
2779 return NULL;
2781 eb->buf_buf = kmalloc(size, GFP_KERNEL);
2782 if (eb->buf_buf == NULL) {
2783 kfree(eb);
2784 return NULL;
2787 eb->buf_size = size;
2788 eb->buf_get = eb->buf_put = eb->buf_buf;
2790 return eb;
2795 * edge_buf_free
2797 * Free the buffer and all associated memory.
2800 static void edge_buf_free(struct edge_buf *eb)
2802 if (eb) {
2803 kfree(eb->buf_buf);
2804 kfree(eb);
2810 * edge_buf_clear
2812 * Clear out all data in the circular buffer.
2815 static void edge_buf_clear(struct edge_buf *eb)
2817 if (eb != NULL)
2818 eb->buf_get = eb->buf_put;
2819 /* equivalent to a get of all data available */
2824 * edge_buf_data_avail
2826 * Return the number of bytes of data available in the circular
2827 * buffer.
2830 static unsigned int edge_buf_data_avail(struct edge_buf *eb)
2832 if (eb == NULL)
2833 return 0;
2834 return ((eb->buf_size + eb->buf_put - eb->buf_get) % eb->buf_size);
2839 * edge_buf_space_avail
2841 * Return the number of bytes of space available in the circular
2842 * buffer.
2845 static unsigned int edge_buf_space_avail(struct edge_buf *eb)
2847 if (eb == NULL)
2848 return 0;
2849 return ((eb->buf_size + eb->buf_get - eb->buf_put - 1) % eb->buf_size);
2854 * edge_buf_put
2856 * Copy data data from a user buffer and put it into the circular buffer.
2857 * Restrict to the amount of space available.
2859 * Return the number of bytes copied.
2862 static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
2863 unsigned int count)
2865 unsigned int len;
2868 if (eb == NULL)
2869 return 0;
2871 len = edge_buf_space_avail(eb);
2872 if (count > len)
2873 count = len;
2875 if (count == 0)
2876 return 0;
2878 len = eb->buf_buf + eb->buf_size - eb->buf_put;
2879 if (count > len) {
2880 memcpy(eb->buf_put, buf, len);
2881 memcpy(eb->buf_buf, buf+len, count - len);
2882 eb->buf_put = eb->buf_buf + count - len;
2883 } else {
2884 memcpy(eb->buf_put, buf, count);
2885 if (count < len)
2886 eb->buf_put += count;
2887 else /* count == len */
2888 eb->buf_put = eb->buf_buf;
2891 return count;
2896 * edge_buf_get
2898 * Get data from the circular buffer and copy to the given buffer.
2899 * Restrict to the amount of data available.
2901 * Return the number of bytes copied.
2904 static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
2905 unsigned int count)
2907 unsigned int len;
2910 if (eb == NULL)
2911 return 0;
2913 len = edge_buf_data_avail(eb);
2914 if (count > len)
2915 count = len;
2917 if (count == 0)
2918 return 0;
2920 len = eb->buf_buf + eb->buf_size - eb->buf_get;
2921 if (count > len) {
2922 memcpy(buf, eb->buf_get, len);
2923 memcpy(buf+len, eb->buf_buf, count - len);
2924 eb->buf_get = eb->buf_buf + count - len;
2925 } else {
2926 memcpy(buf, eb->buf_get, count);
2927 if (count < len)
2928 eb->buf_get += count;
2929 else /* count == len */
2930 eb->buf_get = eb->buf_buf;
2933 return count;
2937 static struct usb_serial_driver edgeport_1port_device = {
2938 .driver = {
2939 .owner = THIS_MODULE,
2940 .name = "edgeport_ti_1",
2942 .description = "Edgeport TI 1 port adapter",
2943 .usb_driver = &io_driver,
2944 .id_table = edgeport_1port_id_table,
2945 .num_ports = 1,
2946 .open = edge_open,
2947 .close = edge_close,
2948 .throttle = edge_throttle,
2949 .unthrottle = edge_unthrottle,
2950 .attach = edge_startup,
2951 .disconnect = edge_disconnect,
2952 .release = edge_release,
2953 .port_probe = edge_create_sysfs_attrs,
2954 .ioctl = edge_ioctl,
2955 .set_termios = edge_set_termios,
2956 .tiocmget = edge_tiocmget,
2957 .tiocmset = edge_tiocmset,
2958 .write = edge_write,
2959 .write_room = edge_write_room,
2960 .chars_in_buffer = edge_chars_in_buffer,
2961 .break_ctl = edge_break,
2962 .read_int_callback = edge_interrupt_callback,
2963 .read_bulk_callback = edge_bulk_in_callback,
2964 .write_bulk_callback = edge_bulk_out_callback,
2967 static struct usb_serial_driver edgeport_2port_device = {
2968 .driver = {
2969 .owner = THIS_MODULE,
2970 .name = "edgeport_ti_2",
2972 .description = "Edgeport TI 2 port adapter",
2973 .usb_driver = &io_driver,
2974 .id_table = edgeport_2port_id_table,
2975 .num_ports = 2,
2976 .open = edge_open,
2977 .close = edge_close,
2978 .throttle = edge_throttle,
2979 .unthrottle = edge_unthrottle,
2980 .attach = edge_startup,
2981 .disconnect = edge_disconnect,
2982 .release = edge_release,
2983 .port_probe = edge_create_sysfs_attrs,
2984 .ioctl = edge_ioctl,
2985 .set_termios = edge_set_termios,
2986 .tiocmget = edge_tiocmget,
2987 .tiocmset = edge_tiocmset,
2988 .write = edge_write,
2989 .write_room = edge_write_room,
2990 .chars_in_buffer = edge_chars_in_buffer,
2991 .break_ctl = edge_break,
2992 .read_int_callback = edge_interrupt_callback,
2993 .read_bulk_callback = edge_bulk_in_callback,
2994 .write_bulk_callback = edge_bulk_out_callback,
2998 static int __init edgeport_init(void)
3000 int retval;
3001 retval = usb_serial_register(&edgeport_1port_device);
3002 if (retval)
3003 goto failed_1port_device_register;
3004 retval = usb_serial_register(&edgeport_2port_device);
3005 if (retval)
3006 goto failed_2port_device_register;
3007 retval = usb_register(&io_driver);
3008 if (retval)
3009 goto failed_usb_register;
3010 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
3011 DRIVER_DESC "\n");
3012 return 0;
3013 failed_usb_register:
3014 usb_serial_deregister(&edgeport_2port_device);
3015 failed_2port_device_register:
3016 usb_serial_deregister(&edgeport_1port_device);
3017 failed_1port_device_register:
3018 return retval;
3021 static void __exit edgeport_exit(void)
3023 usb_deregister(&io_driver);
3024 usb_serial_deregister(&edgeport_1port_device);
3025 usb_serial_deregister(&edgeport_2port_device);
3028 module_init(edgeport_init);
3029 module_exit(edgeport_exit);
3031 /* Module information */
3032 MODULE_AUTHOR(DRIVER_AUTHOR);
3033 MODULE_DESCRIPTION(DRIVER_DESC);
3034 MODULE_LICENSE("GPL");
3035 MODULE_FIRMWARE("edgeport/down3.bin");
3037 module_param(debug, bool, S_IRUGO | S_IWUSR);
3038 MODULE_PARM_DESC(debug, "Debug enabled or not");
3040 module_param(closing_wait, int, S_IRUGO | S_IWUSR);
3041 MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
3043 module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
3044 MODULE_PARM_DESC(ignore_cpu_rev,
3045 "Ignore the cpu revision when connecting to a device");
3047 module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
3048 MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");