]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/usb/serial/io_ti.c
USB: serial: remove unnecessary reinitialisations of urb->dev
[mirror_ubuntu-bionic-kernel.git] / drivers / usb / serial / io_ti.c
CommitLineData
1da177e4
LT
1/*
2 * Edgeport USB Serial Converter driver
3 *
4 * Copyright (C) 2000-2002 Inside Out Networks, All rights reserved.
5 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
6 *
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.
11 *
12 * Supports the following devices:
13 * EP/1 EP/2 EP/4 EP/21 EP/22 EP/221 EP/42 EP/421 WATCHPORT
14 *
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>.
1da177e4
LT
18 */
19
1da177e4
LT
20#include <linux/kernel.h>
21#include <linux/jiffies.h>
22#include <linux/errno.h>
23#include <linux/init.h>
24#include <linux/slab.h>
25#include <linux/tty.h>
26#include <linux/tty_driver.h>
27#include <linux/tty_flip.h>
28#include <linux/module.h>
29#include <linux/spinlock.h>
241ca64f 30#include <linux/mutex.h>
1da177e4 31#include <linux/serial.h>
d733cec1 32#include <linux/kfifo.h>
1da177e4 33#include <linux/ioctl.h>
d12b219a 34#include <linux/firmware.h>
2742fd88 35#include <linux/uaccess.h>
1da177e4 36#include <linux/usb.h>
a969888c 37#include <linux/usb/serial.h>
1da177e4 38
1da177e4
LT
39#include "io_16654.h"
40#include "io_usbvend.h"
41#include "io_ti.h"
42
43/*
44 * Version Information
45 */
fc4cbd75 46#define DRIVER_VERSION "v0.7mode043006"
1da177e4
LT
47#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
48#define DRIVER_DESC "Edgeport USB Serial Driver"
49
1da177e4
LT
50#define EPROM_PAGE_SIZE 64
51
52
1da177e4
LT
53/* different hardware types */
54#define HARDWARE_TYPE_930 0
55#define HARDWARE_TYPE_TIUMP 1
56
2742fd88
AC
57/* IOCTL_PRIVATE_TI_GET_MODE Definitions */
58#define TI_MODE_CONFIGURING 0 /* Device has not entered start device */
59#define TI_MODE_BOOT 1 /* Staying in boot mode */
60#define TI_MODE_DOWNLOAD 2 /* Made it to download mode */
61#define TI_MODE_TRANSITIONING 3 /* Currently in boot mode but
62 transitioning to download mode */
1da177e4
LT
63
64/* read urb state */
65#define EDGE_READ_URB_RUNNING 0
66#define EDGE_READ_URB_STOPPING 1
67#define EDGE_READ_URB_STOPPED 2
68
1da177e4
LT
69#define EDGE_CLOSING_WAIT 4000 /* in .01 sec */
70
71#define EDGE_OUT_BUF_SIZE 1024
72
73
74/* Product information read from the Edgeport */
2742fd88
AC
75struct product_info {
76 int TiMode; /* Current TI Mode */
77 __u8 hardware_type; /* Type of hardware */
1da177e4
LT
78} __attribute__((packed));
79
1da177e4
LT
80struct edgeport_port {
81 __u16 uart_base;
82 __u16 dma_address;
83 __u8 shadow_msr;
84 __u8 shadow_mcr;
85 __u8 shadow_lsr;
86 __u8 lsr_mask;
19af5cdb 87 __u32 ump_read_timeout; /* Number of milliseconds the UMP will
1da177e4
LT
88 wait without data before completing
89 a read short */
90 int baud_rate;
91 int close_pending;
92 int lsr_event;
1da177e4
LT
93 struct async_icount icount;
94 wait_queue_head_t delta_msr_wait; /* for handling sleeping while
95 waiting for msr change to
96 happen */
97 struct edgeport_serial *edge_serial;
98 struct usb_serial_port *port;
2742fd88 99 __u8 bUartMode; /* Port type, 0: RS232, etc. */
1da177e4
LT
100 spinlock_t ep_lock;
101 int ep_read_urb_state;
102 int ep_write_urb_in_use;
d733cec1 103 struct kfifo write_fifo;
1da177e4
LT
104};
105
106struct edgeport_serial {
107 struct product_info product_info;
2742fd88
AC
108 u8 TI_I2C_Type; /* Type of I2C in UMP */
109 u8 TiReadI2C; /* Set to TRUE if we have read the
110 I2c in Boot Mode */
241ca64f 111 struct mutex es_lock;
1da177e4
LT
112 int num_ports_open;
113 struct usb_serial *serial;
114};
115
116
117/* Devices that this driver supports */
7d40d7e8 118static const struct usb_device_id edgeport_1port_id_table[] = {
1da177e4
LT
119 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
120 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
121 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
122 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
123 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
124 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
125 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
126 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
127 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
128 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
129 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
130 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
131 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
132 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
133 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
134 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
135 { }
136};
137
7d40d7e8 138static const struct usb_device_id edgeport_2port_id_table[] = {
1da177e4
LT
139 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
140 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
141 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
142 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
143 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
144 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
145 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
146 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
147 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
148 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
149 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
150 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
fc4cbd75 151 /* The 4, 8 and 16 port devices show up as multiple 2 port devices */
1da177e4 152 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
fc4cbd75
MP
153 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
154 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
155 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
156 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
1da177e4
LT
157 { }
158};
159
160/* Devices that this driver supports */
7d40d7e8 161static const struct usb_device_id id_table_combined[] = {
1da177e4
LT
162 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
163 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
164 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
165 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
166 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
167 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
168 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
169 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
170 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
171 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
172 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
173 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
174 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
175 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
176 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
177 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
178 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
179 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
180 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
181 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
182 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
183 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
184 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
185 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
186 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
187 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
188 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
189 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
190 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
fc4cbd75
MP
191 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
192 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
193 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
194 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
1da177e4
LT
195 { }
196};
197
2742fd88 198MODULE_DEVICE_TABLE(usb, id_table_combined);
1da177e4
LT
199
200static struct usb_driver io_driver = {
1da177e4
LT
201 .name = "io_ti",
202 .probe = usb_serial_probe,
203 .disconnect = usb_serial_disconnect,
204 .id_table = id_table_combined,
ba9dc657 205 .no_dynamic_id = 1,
1da177e4
LT
206};
207
208
d12b219a
JS
209static unsigned char OperationalMajorVersion;
210static unsigned char OperationalMinorVersion;
211static unsigned short OperationalBuildNumber;
1da177e4
LT
212
213static int debug;
214
1da177e4 215static int closing_wait = EDGE_CLOSING_WAIT;
2742fd88
AC
216static int ignore_cpu_rev;
217static int default_uart_mode; /* RS232 */
1da177e4 218
2742fd88
AC
219static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
220 unsigned char *data, int length);
1da177e4
LT
221
222static void stop_read(struct edgeport_port *edge_port);
223static int restart_read(struct edgeport_port *edge_port);
224
95da310e
AC
225static void edge_set_termios(struct tty_struct *tty,
226 struct usb_serial_port *port, struct ktermios *old_termios);
227static void edge_send(struct tty_struct *tty);
1da177e4 228
fc4cbd75
MP
229/* sysfs attributes */
230static int edge_create_sysfs_attrs(struct usb_serial_port *port);
231static int edge_remove_sysfs_attrs(struct usb_serial_port *port);
232
1da177e4 233
2742fd88
AC
234static int ti_vread_sync(struct usb_device *dev, __u8 request,
235 __u16 value, __u16 index, u8 *data, int size)
1da177e4
LT
236{
237 int status;
238
2742fd88
AC
239 status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
240 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
241 value, index, data, size, 1000);
1da177e4
LT
242 if (status < 0)
243 return status;
244 if (status != size) {
2742fd88
AC
245 dbg("%s - wanted to write %d, but only wrote %d",
246 __func__, size, status);
1da177e4
LT
247 return -ECOMM;
248 }
249 return 0;
250}
251
2742fd88
AC
252static int ti_vsend_sync(struct usb_device *dev, __u8 request,
253 __u16 value, __u16 index, u8 *data, int size)
1da177e4
LT
254{
255 int status;
256
2742fd88
AC
257 status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
258 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
259 value, index, data, size, 1000);
1da177e4
LT
260 if (status < 0)
261 return status;
262 if (status != size) {
2742fd88 263 dbg("%s - wanted to write %d, but only wrote %d",
441b62c1 264 __func__, size, status);
1da177e4
LT
265 return -ECOMM;
266 }
267 return 0;
268}
269
2742fd88 270static int send_cmd(struct usb_device *dev, __u8 command,
1da177e4
LT
271 __u8 moduleid, __u16 value, u8 *data,
272 int size)
273{
2742fd88 274 return ti_vsend_sync(dev, command, value, moduleid, data, size);
1da177e4
LT
275}
276
277/* clear tx/rx buffers and fifo in TI UMP */
2742fd88 278static int purge_port(struct usb_serial_port *port, __u16 mask)
1da177e4
LT
279{
280 int port_number = port->number - port->serial->minor;
281
2742fd88 282 dbg("%s - port %d, mask %x", __func__, port_number, mask);
1da177e4 283
2742fd88 284 return send_cmd(port->serial->dev,
1da177e4
LT
285 UMPC_PURGE_PORT,
286 (__u8)(UMPM_UART1_PORT + port_number),
287 mask,
288 NULL,
289 0);
290}
291
292/**
2742fd88 293 * read_download_mem - Read edgeport memory from TI chip
1da177e4
LT
294 * @dev: usb device pointer
295 * @start_address: Device CPU address at which to read
296 * @length: Length of above data
297 * @address_type: Can read both XDATA and I2C
298 * @buffer: pointer to input data buffer
299 */
2742fd88 300static int read_download_mem(struct usb_device *dev, int start_address,
1da177e4
LT
301 int length, __u8 address_type, __u8 *buffer)
302{
303 int status = 0;
304 __u8 read_length;
305 __be16 be_start_address;
2742fd88
AC
306
307 dbg("%s - @ %x for %d", __func__, start_address, length);
1da177e4
LT
308
309 /* Read in blocks of 64 bytes
310 * (TI firmware can't handle more than 64 byte reads)
311 */
312 while (length) {
313 if (length > 64)
2742fd88 314 read_length = 64;
1da177e4
LT
315 else
316 read_length = (__u8)length;
317
318 if (read_length > 1) {
2742fd88 319 dbg("%s - @ %x for %d", __func__,
1da177e4
LT
320 start_address, read_length);
321 }
2742fd88
AC
322 be_start_address = cpu_to_be16(start_address);
323 status = ti_vread_sync(dev, UMPC_MEMORY_READ,
324 (__u16)address_type,
325 (__force __u16)be_start_address,
326 buffer, read_length);
1da177e4
LT
327
328 if (status) {
2742fd88 329 dbg("%s - ERROR %x", __func__, status);
1da177e4
LT
330 return status;
331 }
332
2742fd88 333 if (read_length > 1)
441b62c1 334 usb_serial_debug_data(debug, &dev->dev, __func__,
1da177e4 335 read_length, buffer);
1da177e4
LT
336
337 /* Update pointers/length */
338 start_address += read_length;
339 buffer += read_length;
340 length -= read_length;
341 }
2742fd88 342
1da177e4
LT
343 return status;
344}
345
2742fd88
AC
346static int read_ram(struct usb_device *dev, int start_address,
347 int length, __u8 *buffer)
1da177e4 348{
2742fd88
AC
349 return read_download_mem(dev, start_address, length,
350 DTK_ADDR_SPACE_XDATA, buffer);
1da177e4
LT
351}
352
353/* Read edgeport memory to a given block */
2742fd88
AC
354static int read_boot_mem(struct edgeport_serial *serial,
355 int start_address, int length, __u8 *buffer)
1da177e4
LT
356{
357 int status = 0;
358 int i;
359
2742fd88
AC
360 for (i = 0; i < length; i++) {
361 status = ti_vread_sync(serial->serial->dev,
362 UMPC_MEMORY_READ, serial->TI_I2C_Type,
363 (__u16)(start_address+i), &buffer[i], 0x01);
1da177e4 364 if (status) {
2742fd88 365 dbg("%s - ERROR %x", __func__, status);
1da177e4
LT
366 return status;
367 }
368 }
369
2742fd88
AC
370 dbg("%s - start_address = %x, length = %d",
371 __func__, start_address, length);
372 usb_serial_debug_data(debug, &serial->serial->dev->dev,
373 __func__, length, buffer);
1da177e4
LT
374
375 serial->TiReadI2C = 1;
376
377 return status;
378}
379
380/* Write given block to TI EPROM memory */
2742fd88
AC
381static int write_boot_mem(struct edgeport_serial *serial,
382 int start_address, int length, __u8 *buffer)
1da177e4
LT
383{
384 int status = 0;
385 int i;
e9305d2f 386 u8 *temp;
1da177e4
LT
387
388 /* Must do a read before write */
389 if (!serial->TiReadI2C) {
e9305d2f
JH
390 temp = kmalloc(1, GFP_KERNEL);
391 if (!temp) {
392 dev_err(&serial->serial->dev->dev,
393 "%s - out of memory\n", __func__);
394 return -ENOMEM;
395 }
396 status = read_boot_mem(serial, 0, 1, temp);
397 kfree(temp);
1da177e4
LT
398 if (status)
399 return status;
400 }
401
2742fd88
AC
402 for (i = 0; i < length; ++i) {
403 status = ti_vsend_sync(serial->serial->dev,
404 UMPC_MEMORY_WRITE, buffer[i],
405 (__u16)(i + start_address), NULL, 0);
1da177e4
LT
406 if (status)
407 return status;
408 }
409
2742fd88
AC
410 dbg("%s - start_sddr = %x, length = %d",
411 __func__, start_address, length);
412 usb_serial_debug_data(debug, &serial->serial->dev->dev,
413 __func__, length, buffer);
1da177e4
LT
414
415 return status;
416}
417
418
419/* Write edgeport I2C memory to TI chip */
2742fd88
AC
420static int write_i2c_mem(struct edgeport_serial *serial,
421 int start_address, int length, __u8 address_type, __u8 *buffer)
1da177e4
LT
422{
423 int status = 0;
424 int write_length;
425 __be16 be_start_address;
426
427 /* We can only send a maximum of 1 aligned byte page at a time */
2742fd88 428
25985edc 429 /* calculate the number of bytes left in the first page */
2742fd88
AC
430 write_length = EPROM_PAGE_SIZE -
431 (start_address & (EPROM_PAGE_SIZE - 1));
1da177e4
LT
432
433 if (write_length > length)
434 write_length = length;
435
2742fd88
AC
436 dbg("%s - BytesInFirstPage Addr = %x, length = %d",
437 __func__, start_address, write_length);
438 usb_serial_debug_data(debug, &serial->serial->dev->dev,
439 __func__, write_length, buffer);
1da177e4
LT
440
441 /* Write first page */
2742fd88
AC
442 be_start_address = cpu_to_be16(start_address);
443 status = ti_vsend_sync(serial->serial->dev,
444 UMPC_MEMORY_WRITE, (__u16)address_type,
445 (__force __u16)be_start_address,
446 buffer, write_length);
1da177e4 447 if (status) {
2742fd88 448 dbg("%s - ERROR %d", __func__, status);
1da177e4
LT
449 return status;
450 }
451
452 length -= write_length;
453 start_address += write_length;
454 buffer += write_length;
455
2742fd88
AC
456 /* We should be aligned now -- can write
457 max page size bytes at a time */
1da177e4
LT
458 while (length) {
459 if (length > EPROM_PAGE_SIZE)
460 write_length = EPROM_PAGE_SIZE;
461 else
462 write_length = length;
463
2742fd88
AC
464 dbg("%s - Page Write Addr = %x, length = %d",
465 __func__, start_address, write_length);
466 usb_serial_debug_data(debug, &serial->serial->dev->dev,
467 __func__, write_length, buffer);
1da177e4
LT
468
469 /* Write next page */
2742fd88
AC
470 be_start_address = cpu_to_be16(start_address);
471 status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
472 (__u16)address_type,
473 (__force __u16)be_start_address,
474 buffer, write_length);
1da177e4 475 if (status) {
2742fd88
AC
476 dev_err(&serial->serial->dev->dev, "%s - ERROR %d\n",
477 __func__, status);
1da177e4
LT
478 return status;
479 }
2742fd88 480
1da177e4
LT
481 length -= write_length;
482 start_address += write_length;
483 buffer += write_length;
484 }
485 return status;
486}
487
488/* Examine the UMP DMA registers and LSR
2742fd88 489 *
1da177e4
LT
490 * Check the MSBit of the X and Y DMA byte count registers.
491 * A zero in this bit indicates that the TX DMA buffers are empty
492 * then check the TX Empty bit in the UART.
493 */
2742fd88 494static int tx_active(struct edgeport_port *port)
1da177e4
LT
495{
496 int status;
497 struct out_endpoint_desc_block *oedb;
498 __u8 *lsr;
499 int bytes_left = 0;
500
2742fd88 501 oedb = kmalloc(sizeof(*oedb), GFP_KERNEL);
1da177e4 502 if (!oedb) {
2742fd88 503 dev_err(&port->port->dev, "%s - out of memory\n", __func__);
1da177e4
LT
504 return -ENOMEM;
505 }
506
2742fd88 507 lsr = kmalloc(1, GFP_KERNEL); /* Sigh, that's right, just one byte,
1da177e4
LT
508 as not all platforms can do DMA
509 from stack */
510 if (!lsr) {
511 kfree(oedb);
512 return -ENOMEM;
513 }
514 /* Read the DMA Count Registers */
2742fd88
AC
515 status = read_ram(port->port->serial->dev, port->dma_address,
516 sizeof(*oedb), (void *)oedb);
1da177e4
LT
517 if (status)
518 goto exit_is_tx_active;
519
2742fd88 520 dbg("%s - XByteCount 0x%X", __func__, oedb->XByteCount);
1da177e4
LT
521
522 /* and the LSR */
2742fd88
AC
523 status = read_ram(port->port->serial->dev,
524 port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr);
1da177e4
LT
525
526 if (status)
527 goto exit_is_tx_active;
2742fd88
AC
528 dbg("%s - LSR = 0x%X", __func__, *lsr);
529
1da177e4 530 /* If either buffer has data or we are transmitting then return TRUE */
2742fd88 531 if ((oedb->XByteCount & 0x80) != 0)
1da177e4
LT
532 bytes_left += 64;
533
2742fd88 534 if ((*lsr & UMP_UART_LSR_TX_MASK) == 0)
1da177e4
LT
535 bytes_left += 1;
536
537 /* We return Not Active if we get any kind of error */
538exit_is_tx_active:
2742fd88 539 dbg("%s - return %d", __func__, bytes_left);
1da177e4
LT
540
541 kfree(lsr);
542 kfree(oedb);
543 return bytes_left;
544}
545
2742fd88
AC
546static void chase_port(struct edgeport_port *port, unsigned long timeout,
547 int flush)
1da177e4
LT
548{
549 int baud_rate;
4a90f09b 550 struct tty_struct *tty = tty_port_tty_get(&port->port->port);
1da177e4
LT
551 wait_queue_t wait;
552 unsigned long flags;
553
554 if (!timeout)
2742fd88 555 timeout = (HZ * EDGE_CLOSING_WAIT)/100;
1da177e4
LT
556
557 /* wait for data to drain from the buffer */
558 spin_lock_irqsave(&port->ep_lock, flags);
559 init_waitqueue_entry(&wait, current);
560 add_wait_queue(&tty->write_wait, &wait);
561 for (;;) {
562 set_current_state(TASK_INTERRUPTIBLE);
d733cec1 563 if (kfifo_len(&port->write_fifo) == 0
1da177e4 564 || timeout == 0 || signal_pending(current)
2742fd88
AC
565 || !usb_get_intfdata(port->port->serial->interface))
566 /* disconnect */
1da177e4
LT
567 break;
568 spin_unlock_irqrestore(&port->ep_lock, flags);
569 timeout = schedule_timeout(timeout);
570 spin_lock_irqsave(&port->ep_lock, flags);
571 }
572 set_current_state(TASK_RUNNING);
573 remove_wait_queue(&tty->write_wait, &wait);
574 if (flush)
d733cec1 575 kfifo_reset_out(&port->write_fifo);
1da177e4 576 spin_unlock_irqrestore(&port->ep_lock, flags);
4a90f09b 577 tty_kref_put(tty);
1da177e4
LT
578
579 /* wait for data to drain from the device */
580 timeout += jiffies;
581 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
2742fd88
AC
582 && usb_get_intfdata(port->port->serial->interface)) {
583 /* not disconnected */
584 if (!tx_active(port))
1da177e4
LT
585 break;
586 msleep(10);
587 }
588
589 /* disconnected */
590 if (!usb_get_intfdata(port->port->serial->interface))
591 return;
592
593 /* wait one more character time, based on baud rate */
2742fd88
AC
594 /* (tx_active doesn't seem to wait for the last byte) */
595 baud_rate = port->baud_rate;
596 if (baud_rate == 0)
1da177e4 597 baud_rate = 50;
dfa5ec79 598 msleep(max(1, DIV_ROUND_UP(10000, baud_rate)));
1da177e4
LT
599}
600
2742fd88 601static int choose_config(struct usb_device *dev)
1da177e4 602{
2742fd88
AC
603 /*
604 * There may be multiple configurations on this device, in which case
605 * we would need to read and parse all of them to find out which one
606 * we want. However, we just support one config at this point,
607 * configuration # 1, which is Config Descriptor 0.
608 */
1da177e4 609
2742fd88
AC
610 dbg("%s - Number of Interfaces = %d",
611 __func__, dev->config->desc.bNumInterfaces);
612 dbg("%s - MAX Power = %d",
613 __func__, dev->config->desc.bMaxPower * 2);
1da177e4
LT
614
615 if (dev->config->desc.bNumInterfaces != 1) {
2742fd88
AC
616 dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n",
617 __func__);
1da177e4
LT
618 return -ENODEV;
619 }
620
621 return 0;
622}
623
2742fd88
AC
624static int read_rom(struct edgeport_serial *serial,
625 int start_address, int length, __u8 *buffer)
1da177e4
LT
626{
627 int status;
628
629 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
2742fd88 630 status = read_download_mem(serial->serial->dev,
1da177e4
LT
631 start_address,
632 length,
633 serial->TI_I2C_Type,
634 buffer);
635 } else {
2742fd88
AC
636 status = read_boot_mem(serial, start_address, length,
637 buffer);
1da177e4 638 }
1da177e4
LT
639 return status;
640}
641
2742fd88
AC
642static int write_rom(struct edgeport_serial *serial, int start_address,
643 int length, __u8 *buffer)
1da177e4
LT
644{
645 if (serial->product_info.TiMode == TI_MODE_BOOT)
2742fd88
AC
646 return write_boot_mem(serial, start_address, length,
647 buffer);
1da177e4
LT
648
649 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
2742fd88
AC
650 return write_i2c_mem(serial, start_address, length,
651 serial->TI_I2C_Type, buffer);
1da177e4
LT
652 return -EINVAL;
653}
654
655
656
657/* Read a descriptor header from I2C based on type */
2742fd88
AC
658static int get_descriptor_addr(struct edgeport_serial *serial,
659 int desc_type, struct ti_i2c_desc *rom_desc)
1da177e4
LT
660{
661 int start_address;
662 int status;
663
664 /* Search for requested descriptor in I2C */
665 start_address = 2;
666 do {
2742fd88 667 status = read_rom(serial,
1da177e4
LT
668 start_address,
669 sizeof(struct ti_i2c_desc),
2742fd88 670 (__u8 *)rom_desc);
1da177e4
LT
671 if (status)
672 return 0;
673
674 if (rom_desc->Type == desc_type)
675 return start_address;
676
2742fd88
AC
677 start_address = start_address + sizeof(struct ti_i2c_desc)
678 + rom_desc->Size;
1da177e4
LT
679
680 } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
2742fd88 681
1da177e4
LT
682 return 0;
683}
684
685/* Validate descriptor checksum */
2742fd88 686static int valid_csum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
1da177e4
LT
687{
688 __u16 i;
689 __u8 cs = 0;
690
2742fd88 691 for (i = 0; i < rom_desc->Size; i++)
1da177e4 692 cs = (__u8)(cs + buffer[i]);
2742fd88 693
1da177e4 694 if (cs != rom_desc->CheckSum) {
2742fd88 695 dbg("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
1da177e4
LT
696 return -EINVAL;
697 }
698 return 0;
699}
700
701/* Make sure that the I2C image is good */
2742fd88 702static int check_i2c_image(struct edgeport_serial *serial)
1da177e4
LT
703{
704 struct device *dev = &serial->serial->dev->dev;
705 int status = 0;
706 struct ti_i2c_desc *rom_desc;
707 int start_address = 2;
708 __u8 *buffer;
709 __u16 ttype;
710
2742fd88 711 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
1da177e4 712 if (!rom_desc) {
2742fd88 713 dev_err(dev, "%s - out of memory\n", __func__);
1da177e4
LT
714 return -ENOMEM;
715 }
2742fd88 716 buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL);
1da177e4 717 if (!buffer) {
2742fd88
AC
718 dev_err(dev, "%s - out of memory when allocating buffer\n",
719 __func__);
720 kfree(rom_desc);
1da177e4
LT
721 return -ENOMEM;
722 }
723
2742fd88
AC
724 /* Read the first byte (Signature0) must be 0x52 or 0x10 */
725 status = read_rom(serial, 0, 1, buffer);
1da177e4 726 if (status)
2742fd88 727 goto out;
1da177e4
LT
728
729 if (*buffer != UMP5152 && *buffer != UMP3410) {
2742fd88 730 dev_err(dev, "%s - invalid buffer signature\n", __func__);
1da177e4 731 status = -ENODEV;
2742fd88 732 goto out;
1da177e4
LT
733 }
734
735 do {
2742fd88
AC
736 /* Validate the I2C */
737 status = read_rom(serial,
1da177e4
LT
738 start_address,
739 sizeof(struct ti_i2c_desc),
740 (__u8 *)rom_desc);
741 if (status)
742 break;
743
2742fd88
AC
744 if ((start_address + sizeof(struct ti_i2c_desc) +
745 rom_desc->Size) > TI_MAX_I2C_SIZE) {
1da177e4 746 status = -ENODEV;
2742fd88 747 dbg("%s - structure too big, erroring out.", __func__);
1da177e4
LT
748 break;
749 }
750
2742fd88 751 dbg("%s Type = 0x%x", __func__, rom_desc->Type);
1da177e4 752
2742fd88 753 /* Skip type 2 record */
1da177e4 754 ttype = rom_desc->Type & 0x0f;
2742fd88
AC
755 if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
756 && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) {
757 /* Read the descriptor data */
758 status = read_rom(serial, start_address +
759 sizeof(struct ti_i2c_desc),
760 rom_desc->Size, buffer);
1da177e4
LT
761 if (status)
762 break;
763
2742fd88 764 status = valid_csum(rom_desc, buffer);
1da177e4
LT
765 if (status)
766 break;
767 }
2742fd88
AC
768 start_address = start_address + sizeof(struct ti_i2c_desc) +
769 rom_desc->Size;
1da177e4 770
2742fd88
AC
771 } while ((rom_desc->Type != I2C_DESC_TYPE_ION) &&
772 (start_address < TI_MAX_I2C_SIZE));
1da177e4 773
2742fd88
AC
774 if ((rom_desc->Type != I2C_DESC_TYPE_ION) ||
775 (start_address > TI_MAX_I2C_SIZE))
1da177e4
LT
776 status = -ENODEV;
777
2742fd88
AC
778out:
779 kfree(buffer);
780 kfree(rom_desc);
1da177e4
LT
781 return status;
782}
783
2742fd88 784static int get_manuf_info(struct edgeport_serial *serial, __u8 *buffer)
1da177e4
LT
785{
786 int status;
787 int start_address;
788 struct ti_i2c_desc *rom_desc;
789 struct edge_ti_manuf_descriptor *desc;
790
2742fd88 791 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
1da177e4 792 if (!rom_desc) {
2742fd88
AC
793 dev_err(&serial->serial->dev->dev, "%s - out of memory\n",
794 __func__);
1da177e4
LT
795 return -ENOMEM;
796 }
2742fd88
AC
797 start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
798 rom_desc);
1da177e4
LT
799
800 if (!start_address) {
2742fd88 801 dbg("%s - Edge Descriptor not found in I2C", __func__);
1da177e4
LT
802 status = -ENODEV;
803 goto exit;
804 }
805
2742fd88
AC
806 /* Read the descriptor data */
807 status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc),
808 rom_desc->Size, buffer);
1da177e4
LT
809 if (status)
810 goto exit;
2742fd88
AC
811
812 status = valid_csum(rom_desc, buffer);
813
1da177e4 814 desc = (struct edge_ti_manuf_descriptor *)buffer;
2742fd88
AC
815 dbg("%s - IonConfig 0x%x", __func__, desc->IonConfig);
816 dbg("%s - Version %d", __func__, desc->Version);
817 dbg("%s - Cpu/Board 0x%x", __func__, desc->CpuRev_BoardRev);
818 dbg("%s - NumPorts %d", __func__, desc->NumPorts);
819 dbg("%s - NumVirtualPorts %d", __func__, desc->NumVirtualPorts);
820 dbg("%s - TotalPorts %d", __func__, desc->TotalPorts);
1da177e4
LT
821
822exit:
2742fd88 823 kfree(rom_desc);
1da177e4
LT
824 return status;
825}
826
827/* Build firmware header used for firmware update */
2742fd88 828static int build_i2c_fw_hdr(__u8 *header, struct device *dev)
1da177e4
LT
829{
830 __u8 *buffer;
831 int buffer_size;
832 int i;
d12b219a 833 int err;
1da177e4
LT
834 __u8 cs = 0;
835 struct ti_i2c_desc *i2c_header;
836 struct ti_i2c_image_header *img_header;
837 struct ti_i2c_firmware_rec *firmware_rec;
d12b219a
JS
838 const struct firmware *fw;
839 const char *fw_name = "edgeport/down3.bin";
1da177e4 840
2742fd88
AC
841 /* In order to update the I2C firmware we must change the type 2 record
842 * to type 0xF2. This will force the UMP to come up in Boot Mode.
843 * Then while in boot mode, the driver will download the latest
844 * firmware (padded to 15.5k) into the UMP ram. And finally when the
845 * device comes back up in download mode the driver will cause the new
846 * firmware to be copied from the UMP Ram to I2C and the firmware will
847 * update the record type from 0xf2 to 0x02.
848 */
849
850 /* Allocate a 15.5k buffer + 2 bytes for version number
851 * (Firmware Record) */
852 buffer_size = (((1024 * 16) - 512 ) +
853 sizeof(struct ti_i2c_firmware_rec));
854
855 buffer = kmalloc(buffer_size, GFP_KERNEL);
1da177e4 856 if (!buffer) {
2742fd88 857 dev_err(dev, "%s - out of memory\n", __func__);
1da177e4
LT
858 return -ENOMEM;
859 }
2742fd88 860
1da177e4 861 // Set entire image of 0xffs
2742fd88 862 memset(buffer, 0xff, buffer_size);
1da177e4 863
d12b219a
JS
864 err = request_firmware(&fw, fw_name, dev);
865 if (err) {
866 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
867 fw_name, err);
868 kfree(buffer);
869 return err;
870 }
871
872 /* Save Download Version Number */
873 OperationalMajorVersion = fw->data[0];
874 OperationalMinorVersion = fw->data[1];
875 OperationalBuildNumber = fw->data[2] | (fw->data[3] << 8);
876
2742fd88 877 /* Copy version number into firmware record */
1da177e4
LT
878 firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
879
d12b219a
JS
880 firmware_rec->Ver_Major = OperationalMajorVersion;
881 firmware_rec->Ver_Minor = OperationalMinorVersion;
1da177e4 882
2742fd88 883 /* Pointer to fw_down memory image */
d12b219a 884 img_header = (struct ti_i2c_image_header *)&fw->data[4];
1da177e4 885
2742fd88 886 memcpy(buffer + sizeof(struct ti_i2c_firmware_rec),
d12b219a 887 &fw->data[4 + sizeof(struct ti_i2c_image_header)],
1da177e4
LT
888 le16_to_cpu(img_header->Length));
889
d12b219a
JS
890 release_firmware(fw);
891
1da177e4
LT
892 for (i=0; i < buffer_size; i++) {
893 cs = (__u8)(cs + buffer[i]);
894 }
895
2742fd88 896 kfree(buffer);
1da177e4 897
2742fd88 898 /* Build new header */
1da177e4
LT
899 i2c_header = (struct ti_i2c_desc *)header;
900 firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data;
2742fd88 901
1da177e4
LT
902 i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK;
903 i2c_header->Size = (__u16)buffer_size;
904 i2c_header->CheckSum = cs;
d12b219a
JS
905 firmware_rec->Ver_Major = OperationalMajorVersion;
906 firmware_rec->Ver_Minor = OperationalMinorVersion;
1da177e4
LT
907
908 return 0;
909}
910
911/* Try to figure out what type of I2c we have */
2742fd88 912static int i2c_type_bootmode(struct edgeport_serial *serial)
1da177e4
LT
913{
914 int status;
e9305d2f
JH
915 u8 *data;
916
917 data = kmalloc(1, GFP_KERNEL);
918 if (!data) {
919 dev_err(&serial->serial->dev->dev,
920 "%s - out of memory\n", __func__);
921 return -ENOMEM;
922 }
2742fd88
AC
923
924 /* Try to read type 2 */
925 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
e9305d2f 926 DTK_ADDR_SPACE_I2C_TYPE_II, 0, data, 0x01);
1da177e4 927 if (status)
2742fd88 928 dbg("%s - read 2 status error = %d", __func__, status);
1da177e4 929 else
e9305d2f
JH
930 dbg("%s - read 2 data = 0x%x", __func__, *data);
931 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
2742fd88 932 dbg("%s - ROM_TYPE_II", __func__);
1da177e4 933 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
e9305d2f 934 goto out;
1da177e4
LT
935 }
936
2742fd88
AC
937 /* Try to read type 3 */
938 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
e9305d2f 939 DTK_ADDR_SPACE_I2C_TYPE_III, 0, data, 0x01);
1da177e4 940 if (status)
2742fd88 941 dbg("%s - read 3 status error = %d", __func__, status);
1da177e4 942 else
e9305d2f
JH
943 dbg("%s - read 2 data = 0x%x", __func__, *data);
944 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
2742fd88 945 dbg("%s - ROM_TYPE_III", __func__);
1da177e4 946 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
e9305d2f 947 goto out;
1da177e4
LT
948 }
949
2742fd88 950 dbg("%s - Unknown", __func__);
1da177e4 951 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
e9305d2f
JH
952 status = -ENODEV;
953out:
954 kfree(data);
955 return status;
1da177e4
LT
956}
957
2742fd88
AC
958static int bulk_xfer(struct usb_serial *serial, void *buffer,
959 int length, int *num_sent)
1da177e4
LT
960{
961 int status;
962
2742fd88
AC
963 status = usb_bulk_msg(serial->dev,
964 usb_sndbulkpipe(serial->dev,
965 serial->port[0]->bulk_out_endpointAddress),
966 buffer, length, num_sent, 1000);
1da177e4
LT
967 return status;
968}
969
970/* Download given firmware image to the device (IN BOOT MODE) */
2742fd88
AC
971static int download_code(struct edgeport_serial *serial, __u8 *image,
972 int image_length)
1da177e4
LT
973{
974 int status = 0;
975 int pos;
976 int transfer;
977 int done;
978
2742fd88 979 /* Transfer firmware image */
1da177e4 980 for (pos = 0; pos < image_length; ) {
2742fd88 981 /* Read the next buffer from file */
1da177e4
LT
982 transfer = image_length - pos;
983 if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
984 transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
985
2742fd88
AC
986 /* Transfer data */
987 status = bulk_xfer(serial->serial, &image[pos],
988 transfer, &done);
1da177e4
LT
989 if (status)
990 break;
2742fd88 991 /* Advance buffer pointer */
1da177e4
LT
992 pos += done;
993 }
994
995 return status;
996}
997
2742fd88
AC
998/* FIXME!!! */
999static int config_boot_dev(struct usb_device *dev)
1da177e4
LT
1000{
1001 return 0;
1002}
1003
2742fd88
AC
1004static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc)
1005{
1006 return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev);
1007}
1008
1da177e4
LT
1009/**
1010 * DownloadTIFirmware - Download run-time operating firmware to the TI5052
2742fd88 1011 *
1da177e4
LT
1012 * This routine downloads the main operating code into the TI5052, using the
1013 * boot code already burned into E2PROM or ROM.
1014 */
2742fd88 1015static int download_fw(struct edgeport_serial *serial)
1da177e4
LT
1016{
1017 struct device *dev = &serial->serial->dev->dev;
1018 int status = 0;
1019 int start_address;
1020 struct edge_ti_manuf_descriptor *ti_manuf_desc;
1021 struct usb_interface_descriptor *interface;
1022 int download_cur_ver;
1023 int download_new_ver;
1024
1025 /* This routine is entered by both the BOOT mode and the Download mode
1026 * We can determine which code is running by the reading the config
1027 * descriptor and if we have only one bulk pipe it is in boot mode
1028 */
1029 serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
1030
1031 /* Default to type 2 i2c */
1032 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1033
2742fd88 1034 status = choose_config(serial->serial->dev);
1da177e4
LT
1035 if (status)
1036 return status;
1037
1038 interface = &serial->serial->interface->cur_altsetting->desc;
1039 if (!interface) {
2742fd88 1040 dev_err(dev, "%s - no interface set, error!\n", __func__);
1da177e4
LT
1041 return -ENODEV;
1042 }
1043
2742fd88
AC
1044 /*
1045 * Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
1046 * if we have more than one endpoint we are definitely in download
1047 * mode
1048 */
1da177e4
LT
1049 if (interface->bNumEndpoints > 1)
1050 serial->product_info.TiMode = TI_MODE_DOWNLOAD;
1051 else
2742fd88 1052 /* Otherwise we will remain in configuring mode */
1da177e4
LT
1053 serial->product_info.TiMode = TI_MODE_CONFIGURING;
1054
1da177e4
LT
1055 /********************************************************************/
1056 /* Download Mode */
1057 /********************************************************************/
1058 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
1059 struct ti_i2c_desc *rom_desc;
1060
9544e833 1061 dbg("%s - RUNNING IN DOWNLOAD MODE", __func__);
1da177e4 1062
2742fd88 1063 status = check_i2c_image(serial);
1da177e4 1064 if (status) {
9544e833 1065 dbg("%s - DOWNLOAD MODE -- BAD I2C", __func__);
1da177e4
LT
1066 return status;
1067 }
2742fd88 1068
1da177e4
LT
1069 /* Validate Hardware version number
1070 * Read Manufacturing Descriptor from TI Based Edgeport
1071 */
2742fd88 1072 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
1da177e4 1073 if (!ti_manuf_desc) {
2742fd88 1074 dev_err(dev, "%s - out of memory.\n", __func__);
1da177e4
LT
1075 return -ENOMEM;
1076 }
2742fd88 1077 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
1da177e4 1078 if (status) {
2742fd88 1079 kfree(ti_manuf_desc);
1da177e4
LT
1080 return status;
1081 }
1082
2742fd88
AC
1083 /* Check version number of ION descriptor */
1084 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1085 dbg("%s - Wrong CPU Rev %d (Must be 2)",
1086 __func__, ti_cpu_rev(ti_manuf_desc));
1087 kfree(ti_manuf_desc);
1088 return -EINVAL;
1089 }
1da177e4 1090
2742fd88 1091 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
1da177e4 1092 if (!rom_desc) {
2742fd88
AC
1093 dev_err(dev, "%s - out of memory.\n", __func__);
1094 kfree(ti_manuf_desc);
1da177e4
LT
1095 return -ENOMEM;
1096 }
1097
2742fd88
AC
1098 /* Search for type 2 record (firmware record) */
1099 start_address = get_descriptor_addr(serial,
1100 I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc);
1101 if (start_address != 0) {
1da177e4 1102 struct ti_i2c_firmware_rec *firmware_version;
e9305d2f 1103 u8 *record;
1da177e4 1104
2742fd88
AC
1105 dbg("%s - Found Type FIRMWARE (Type 2) record",
1106 __func__);
1da177e4 1107
2742fd88
AC
1108 firmware_version = kmalloc(sizeof(*firmware_version),
1109 GFP_KERNEL);
1da177e4 1110 if (!firmware_version) {
2742fd88
AC
1111 dev_err(dev, "%s - out of memory.\n", __func__);
1112 kfree(rom_desc);
1113 kfree(ti_manuf_desc);
1da177e4
LT
1114 return -ENOMEM;
1115 }
1116
2742fd88
AC
1117 /* Validate version number
1118 * Read the descriptor data
1119 */
1120 status = read_rom(serial, start_address +
1121 sizeof(struct ti_i2c_desc),
1da177e4
LT
1122 sizeof(struct ti_i2c_firmware_rec),
1123 (__u8 *)firmware_version);
1124 if (status) {
2742fd88
AC
1125 kfree(firmware_version);
1126 kfree(rom_desc);
1127 kfree(ti_manuf_desc);
1da177e4
LT
1128 return status;
1129 }
1130
2742fd88
AC
1131 /* Check version number of download with current
1132 version in I2c */
1133 download_cur_ver = (firmware_version->Ver_Major << 8) +
1da177e4 1134 (firmware_version->Ver_Minor);
d12b219a
JS
1135 download_new_ver = (OperationalMajorVersion << 8) +
1136 (OperationalMinorVersion);
1da177e4 1137
2742fd88
AC
1138 dbg("%s - >> FW Versions Device %d.%d Driver %d.%d",
1139 __func__,
1140 firmware_version->Ver_Major,
1141 firmware_version->Ver_Minor,
1142 OperationalMajorVersion,
1143 OperationalMinorVersion);
1da177e4 1144
2742fd88
AC
1145 /* Check if we have an old version in the I2C and
1146 update if necessary */
0827a9ff 1147 if (download_cur_ver < download_new_ver) {
2742fd88
AC
1148 dbg("%s - Update I2C dld from %d.%d to %d.%d",
1149 __func__,
1150 firmware_version->Ver_Major,
1151 firmware_version->Ver_Minor,
1152 OperationalMajorVersion,
1153 OperationalMinorVersion);
1154
e9305d2f
JH
1155 record = kmalloc(1, GFP_KERNEL);
1156 if (!record) {
1157 dev_err(dev, "%s - out of memory.\n",
1158 __func__);
1159 kfree(firmware_version);
1160 kfree(rom_desc);
1161 kfree(ti_manuf_desc);
1162 return -ENOMEM;
1163 }
2742fd88
AC
1164 /* In order to update the I2C firmware we must
1165 * change the type 2 record to type 0xF2. This
1166 * will force the UMP to come up in Boot Mode.
1167 * Then while in boot mode, the driver will
1168 * download the latest firmware (padded to
1169 * 15.5k) into the UMP ram. Finally when the
1170 * device comes back up in download mode the
1171 * driver will cause the new firmware to be
1172 * copied from the UMP Ram to I2C and the
1173 * firmware will update the record type from
1174 * 0xf2 to 0x02.
1175 */
e9305d2f 1176 *record = I2C_DESC_TYPE_FIRMWARE_BLANK;
1da177e4 1177
2742fd88
AC
1178 /* Change the I2C Firmware record type to
1179 0xf2 to trigger an update */
1180 status = write_rom(serial, start_address,
e9305d2f 1181 sizeof(*record), record);
1da177e4 1182 if (status) {
e9305d2f 1183 kfree(record);
2742fd88
AC
1184 kfree(firmware_version);
1185 kfree(rom_desc);
1186 kfree(ti_manuf_desc);
1da177e4
LT
1187 return status;
1188 }
1189
2742fd88
AC
1190 /* verify the write -- must do this in order
1191 * for write to complete before we do the
1192 * hardware reset
1193 */
1194 status = read_rom(serial,
1da177e4 1195 start_address,
e9305d2f
JH
1196 sizeof(*record),
1197 record);
1da177e4 1198 if (status) {
e9305d2f 1199 kfree(record);
2742fd88
AC
1200 kfree(firmware_version);
1201 kfree(rom_desc);
1202 kfree(ti_manuf_desc);
1da177e4
LT
1203 return status;
1204 }
1205
e9305d2f 1206 if (*record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
2742fd88
AC
1207 dev_err(dev,
1208 "%s - error resetting device\n",
1209 __func__);
e9305d2f 1210 kfree(record);
2742fd88
AC
1211 kfree(firmware_version);
1212 kfree(rom_desc);
1213 kfree(ti_manuf_desc);
1da177e4
LT
1214 return -ENODEV;
1215 }
1216
2742fd88 1217 dbg("%s - HARDWARE RESET", __func__);
1da177e4 1218
2742fd88
AC
1219 /* Reset UMP -- Back to BOOT MODE */
1220 status = ti_vsend_sync(serial->serial->dev,
1221 UMPC_HARDWARE_RESET,
1222 0, 0, NULL, 0);
1da177e4 1223
2742fd88
AC
1224 dbg("%s - HARDWARE RESET return %d",
1225 __func__, status);
1da177e4
LT
1226
1227 /* return an error on purpose. */
e9305d2f 1228 kfree(record);
2742fd88
AC
1229 kfree(firmware_version);
1230 kfree(rom_desc);
1231 kfree(ti_manuf_desc);
1da177e4
LT
1232 return -ENODEV;
1233 }
2742fd88 1234 kfree(firmware_version);
1da177e4 1235 }
2742fd88
AC
1236 /* Search for type 0xF2 record (firmware blank record) */
1237 else if ((start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
1238#define HEADER_SIZE (sizeof(struct ti_i2c_desc) + \
1239 sizeof(struct ti_i2c_firmware_rec))
1da177e4
LT
1240 __u8 *header;
1241 __u8 *vheader;
1242
2742fd88 1243 header = kmalloc(HEADER_SIZE, GFP_KERNEL);
1da177e4 1244 if (!header) {
2742fd88
AC
1245 dev_err(dev, "%s - out of memory.\n", __func__);
1246 kfree(rom_desc);
1247 kfree(ti_manuf_desc);
1da177e4
LT
1248 return -ENOMEM;
1249 }
2742fd88
AC
1250
1251 vheader = kmalloc(HEADER_SIZE, GFP_KERNEL);
1da177e4 1252 if (!vheader) {
2742fd88
AC
1253 dev_err(dev, "%s - out of memory.\n", __func__);
1254 kfree(header);
1255 kfree(rom_desc);
1256 kfree(ti_manuf_desc);
1da177e4
LT
1257 return -ENOMEM;
1258 }
2742fd88
AC
1259
1260 dbg("%s - Found Type BLANK FIRMWARE (Type F2) record",
1261 __func__);
1262
1263 /*
1264 * In order to update the I2C firmware we must change
1265 * the type 2 record to type 0xF2. This will force the
1266 * UMP to come up in Boot Mode. Then while in boot
1267 * mode, the driver will download the latest firmware
1268 * (padded to 15.5k) into the UMP ram. Finally when the
1269 * device comes back up in download mode the driver
1270 * will cause the new firmware to be copied from the
1271 * UMP Ram to I2C and the firmware will update the
1272 * record type from 0xf2 to 0x02.
1273 */
1274 status = build_i2c_fw_hdr(header, dev);
1da177e4 1275 if (status) {
2742fd88
AC
1276 kfree(vheader);
1277 kfree(header);
1278 kfree(rom_desc);
1279 kfree(ti_manuf_desc);
fd6e5bbb 1280 return -EINVAL;
1da177e4
LT
1281 }
1282
2742fd88
AC
1283 /* Update I2C with type 0xf2 record with correct
1284 size and checksum */
1285 status = write_rom(serial,
1da177e4
LT
1286 start_address,
1287 HEADER_SIZE,
1288 header);
1289 if (status) {
2742fd88
AC
1290 kfree(vheader);
1291 kfree(header);
1292 kfree(rom_desc);
1293 kfree(ti_manuf_desc);
9800eb33 1294 return -EINVAL;
1da177e4
LT
1295 }
1296
2742fd88
AC
1297 /* verify the write -- must do this in order for
1298 write to complete before we do the hardware reset */
1299 status = read_rom(serial, start_address,
1300 HEADER_SIZE, vheader);
1da177e4
LT
1301
1302 if (status) {
2742fd88
AC
1303 dbg("%s - can't read header back", __func__);
1304 kfree(vheader);
1305 kfree(header);
1306 kfree(rom_desc);
1307 kfree(ti_manuf_desc);
1da177e4
LT
1308 return status;
1309 }
1310 if (memcmp(vheader, header, HEADER_SIZE)) {
2742fd88
AC
1311 dbg("%s - write download record failed",
1312 __func__);
1313 kfree(vheader);
1314 kfree(header);
1315 kfree(rom_desc);
1316 kfree(ti_manuf_desc);
1e29709e 1317 return -EINVAL;
1da177e4
LT
1318 }
1319
2742fd88
AC
1320 kfree(vheader);
1321 kfree(header);
1da177e4 1322
2742fd88 1323 dbg("%s - Start firmware update", __func__);
1da177e4 1324
2742fd88
AC
1325 /* Tell firmware to copy download image into I2C */
1326 status = ti_vsend_sync(serial->serial->dev,
1327 UMPC_COPY_DNLD_TO_I2C, 0, 0, NULL, 0);
1da177e4 1328
2742fd88 1329 dbg("%s - Update complete 0x%x", __func__, status);
1da177e4 1330 if (status) {
2742fd88
AC
1331 dev_err(dev,
1332 "%s - UMPC_COPY_DNLD_TO_I2C failed\n",
1333 __func__);
1334 kfree(rom_desc);
1335 kfree(ti_manuf_desc);
1da177e4
LT
1336 return status;
1337 }
1338 }
1339
1340 // The device is running the download code
2742fd88
AC
1341 kfree(rom_desc);
1342 kfree(ti_manuf_desc);
1da177e4
LT
1343 return 0;
1344 }
1345
1346 /********************************************************************/
1347 /* Boot Mode */
1348 /********************************************************************/
9544e833 1349 dbg("%s - RUNNING IN BOOT MODE", __func__);
1da177e4 1350
2742fd88
AC
1351 /* Configure the TI device so we can use the BULK pipes for download */
1352 status = config_boot_dev(serial->serial->dev);
1da177e4
LT
1353 if (status)
1354 return status;
1355
2742fd88
AC
1356 if (le16_to_cpu(serial->serial->dev->descriptor.idVendor)
1357 != USB_VENDOR_ID_ION) {
1358 dbg("%s - VID = 0x%x", __func__,
1da177e4
LT
1359 le16_to_cpu(serial->serial->dev->descriptor.idVendor));
1360 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
2742fd88 1361 goto stayinbootmode;
1da177e4
LT
1362 }
1363
2742fd88
AC
1364 /* We have an ION device (I2c Must be programmed)
1365 Determine I2C image type */
1366 if (i2c_type_bootmode(serial))
1367 goto stayinbootmode;
1da177e4 1368
2742fd88
AC
1369 /* Check for ION Vendor ID and that the I2C is valid */
1370 if (!check_i2c_image(serial)) {
1da177e4
LT
1371 struct ti_i2c_image_header *header;
1372 int i;
1373 __u8 cs = 0;
1374 __u8 *buffer;
1375 int buffer_size;
d12b219a
JS
1376 int err;
1377 const struct firmware *fw;
1378 const char *fw_name = "edgeport/down3.bin";
1da177e4
LT
1379
1380 /* Validate Hardware version number
1381 * Read Manufacturing Descriptor from TI Based Edgeport
1382 */
2742fd88 1383 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
1da177e4 1384 if (!ti_manuf_desc) {
2742fd88 1385 dev_err(dev, "%s - out of memory.\n", __func__);
1da177e4
LT
1386 return -ENOMEM;
1387 }
2742fd88 1388 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
1da177e4 1389 if (status) {
2742fd88
AC
1390 kfree(ti_manuf_desc);
1391 goto stayinbootmode;
1da177e4
LT
1392 }
1393
2742fd88
AC
1394 /* Check for version 2 */
1395 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1396 dbg("%s - Wrong CPU Rev %d (Must be 2)",
1397 __func__, ti_cpu_rev(ti_manuf_desc));
1398 kfree(ti_manuf_desc);
1399 goto stayinbootmode;
1da177e4
LT
1400 }
1401
2742fd88 1402 kfree(ti_manuf_desc);
1da177e4 1403
1da177e4 1404 /*
2742fd88
AC
1405 * In order to update the I2C firmware we must change the type
1406 * 2 record to type 0xF2. This will force the UMP to come up
1407 * in Boot Mode. Then while in boot mode, the driver will
1408 * download the latest firmware (padded to 15.5k) into the
1409 * UMP ram. Finally when the device comes back up in download
1410 * mode the driver will cause the new firmware to be copied
1411 * from the UMP Ram to I2C and the firmware will update the
1412 * record type from 0xf2 to 0x02.
1413 *
1da177e4
LT
1414 * Do we really have to copy the whole firmware image,
1415 * or could we do this in place!
1416 */
1417
2742fd88
AC
1418 /* Allocate a 15.5k buffer + 3 byte header */
1419 buffer_size = (((1024 * 16) - 512) +
1420 sizeof(struct ti_i2c_image_header));
1421 buffer = kmalloc(buffer_size, GFP_KERNEL);
1da177e4 1422 if (!buffer) {
2742fd88 1423 dev_err(dev, "%s - out of memory\n", __func__);
1da177e4
LT
1424 return -ENOMEM;
1425 }
2742fd88
AC
1426
1427 /* Initialize the buffer to 0xff (pad the buffer) */
1428 memset(buffer, 0xff, buffer_size);
1da177e4 1429
d12b219a
JS
1430 err = request_firmware(&fw, fw_name, dev);
1431 if (err) {
1432 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
1433 fw_name, err);
1434 kfree(buffer);
1435 return err;
1436 }
1437 memcpy(buffer, &fw->data[4], fw->size - 4);
1438 release_firmware(fw);
1da177e4 1439
2742fd88
AC
1440 for (i = sizeof(struct ti_i2c_image_header);
1441 i < buffer_size; i++) {
1da177e4
LT
1442 cs = (__u8)(cs + buffer[i]);
1443 }
2742fd88 1444
1da177e4 1445 header = (struct ti_i2c_image_header *)buffer;
2742fd88
AC
1446
1447 /* update length and checksum after padding */
1448 header->Length = cpu_to_le16((__u16)(buffer_size -
1449 sizeof(struct ti_i2c_image_header)));
1da177e4
LT
1450 header->CheckSum = cs;
1451
2742fd88
AC
1452 /* Download the operational code */
1453 dbg("%s - Downloading operational code image (TI UMP)",
1454 __func__);
1455 status = download_code(serial, buffer, buffer_size);
1da177e4 1456
2742fd88 1457 kfree(buffer);
1da177e4
LT
1458
1459 if (status) {
2742fd88
AC
1460 dbg("%s - Error downloading operational code image",
1461 __func__);
1da177e4
LT
1462 return status;
1463 }
1464
2742fd88 1465 /* Device will reboot */
1da177e4
LT
1466 serial->product_info.TiMode = TI_MODE_TRANSITIONING;
1467
2742fd88
AC
1468 dbg("%s - Download successful -- Device rebooting...",
1469 __func__);
1da177e4
LT
1470
1471 /* return an error on purpose */
1472 return -ENODEV;
1473 }
1474
2742fd88
AC
1475stayinbootmode:
1476 /* Eprom is invalid or blank stay in boot mode */
9544e833 1477 dbg("%s - STAYING IN BOOT MODE", __func__);
1da177e4
LT
1478 serial->product_info.TiMode = TI_MODE_BOOT;
1479
1480 return 0;
1481}
1482
1483
2742fd88 1484static int ti_do_config(struct edgeport_port *port, int feature, int on)
1da177e4
LT
1485{
1486 int port_number = port->port->number - port->port->serial->minor;
2742fd88
AC
1487 on = !!on; /* 1 or 0 not bitmask */
1488 return send_cmd(port->port->serial->dev,
1489 feature, (__u8)(UMPM_UART1_PORT + port_number),
1490 on, NULL, 0);
1da177e4
LT
1491}
1492
1da177e4 1493
2742fd88 1494static int restore_mcr(struct edgeport_port *port, __u8 mcr)
1da177e4
LT
1495{
1496 int status = 0;
1497
2742fd88 1498 dbg("%s - %x", __func__, mcr);
1da177e4 1499
2742fd88 1500 status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR);
1da177e4
LT
1501 if (status)
1502 return status;
2742fd88 1503 status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS);
1da177e4
LT
1504 if (status)
1505 return status;
2742fd88 1506 return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK);
1da177e4
LT
1507}
1508
1da177e4 1509/* Convert TI LSR to standard UART flags */
2742fd88 1510static __u8 map_line_status(__u8 ti_lsr)
1da177e4
LT
1511{
1512 __u8 lsr = 0;
1513
1514#define MAP_FLAG(flagUmp, flagUart) \
1515 if (ti_lsr & flagUmp) \
1516 lsr |= flagUart;
1517
1518 MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR) /* overrun */
1519 MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */
1520 MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */
1521 MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */
2742fd88
AC
1522 MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* rx data available */
1523 MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* tx hold reg empty */
1da177e4
LT
1524
1525#undef MAP_FLAG
1526
1527 return lsr;
1528}
1529
2742fd88 1530static void handle_new_msr(struct edgeport_port *edge_port, __u8 msr)
1da177e4
LT
1531{
1532 struct async_icount *icount;
1533 struct tty_struct *tty;
1534
2742fd88 1535 dbg("%s - %02x", __func__, msr);
1da177e4 1536
2742fd88
AC
1537 if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
1538 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
1da177e4
LT
1539 icount = &edge_port->icount;
1540
1541 /* update input line counters */
1542 if (msr & EDGEPORT_MSR_DELTA_CTS)
1543 icount->cts++;
1544 if (msr & EDGEPORT_MSR_DELTA_DSR)
1545 icount->dsr++;
1546 if (msr & EDGEPORT_MSR_DELTA_CD)
1547 icount->dcd++;
1548 if (msr & EDGEPORT_MSR_DELTA_RI)
1549 icount->rng++;
2742fd88 1550 wake_up_interruptible(&edge_port->delta_msr_wait);
1da177e4
LT
1551 }
1552
1553 /* Save the new modem status */
1554 edge_port->shadow_msr = msr & 0xf0;
1555
4a90f09b 1556 tty = tty_port_tty_get(&edge_port->port->port);
1da177e4
LT
1557 /* handle CTS flow control */
1558 if (tty && C_CRTSCTS(tty)) {
1559 if (msr & EDGEPORT_MSR_CTS) {
1560 tty->hw_stopped = 0;
1561 tty_wakeup(tty);
1562 } else {
1563 tty->hw_stopped = 1;
1564 }
1565 }
4a90f09b 1566 tty_kref_put(tty);
1da177e4
LT
1567}
1568
2742fd88
AC
1569static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data,
1570 __u8 lsr, __u8 data)
1da177e4
LT
1571{
1572 struct async_icount *icount;
2742fd88
AC
1573 __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR |
1574 LSR_FRM_ERR | LSR_BREAK));
4a90f09b 1575 struct tty_struct *tty;
1da177e4 1576
2742fd88 1577 dbg("%s - %02x", __func__, new_lsr);
1da177e4
LT
1578
1579 edge_port->shadow_lsr = lsr;
1580
2742fd88 1581 if (new_lsr & LSR_BREAK)
1da177e4
LT
1582 /*
1583 * Parity and Framing errors only count if they
1584 * occur exclusive of a break being received.
1585 */
1586 new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
1da177e4
LT
1587
1588 /* Place LSR data byte into Rx buffer */
4a90f09b
AC
1589 if (lsr_data) {
1590 tty = tty_port_tty_get(&edge_port->port->port);
1591 if (tty) {
1592 edge_tty_recv(&edge_port->port->dev, tty, &data, 1);
1593 tty_kref_put(tty);
1594 }
1595 }
1da177e4
LT
1596
1597 /* update input line counters */
1598 icount = &edge_port->icount;
1599 if (new_lsr & LSR_BREAK)
1600 icount->brk++;
1601 if (new_lsr & LSR_OVER_ERR)
1602 icount->overrun++;
1603 if (new_lsr & LSR_PAR_ERR)
1604 icount->parity++;
1605 if (new_lsr & LSR_FRM_ERR)
1606 icount->frame++;
1607}
1608
1609
2742fd88 1610static void edge_interrupt_callback(struct urb *urb)
1da177e4 1611{
cdc97792 1612 struct edgeport_serial *edge_serial = urb->context;
1da177e4
LT
1613 struct usb_serial_port *port;
1614 struct edgeport_port *edge_port;
1615 unsigned char *data = urb->transfer_buffer;
1616 int length = urb->actual_length;
1617 int port_number;
1618 int function;
ee337c21 1619 int retval;
1da177e4
LT
1620 __u8 lsr;
1621 __u8 msr;
ee337c21 1622 int status = urb->status;
1da177e4 1623
441b62c1 1624 dbg("%s", __func__);
1da177e4 1625
ee337c21 1626 switch (status) {
1da177e4
LT
1627 case 0:
1628 /* success */
1629 break;
1630 case -ECONNRESET:
1631 case -ENOENT:
1632 case -ESHUTDOWN:
1633 /* this urb is terminated, clean up */
ee337c21 1634 dbg("%s - urb shutting down with status: %d",
441b62c1 1635 __func__, status);
1da177e4
LT
1636 return;
1637 default:
ee337c21 1638 dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
441b62c1 1639 "%d\n", __func__, status);
1da177e4
LT
1640 goto exit;
1641 }
1642
1643 if (!length) {
2742fd88 1644 dbg("%s - no data in urb", __func__);
1da177e4
LT
1645 goto exit;
1646 }
2742fd88
AC
1647
1648 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev,
1649 __func__, length, data);
1650
1da177e4 1651 if (length != 2) {
2742fd88
AC
1652 dbg("%s - expecting packet of size 2, got %d",
1653 __func__, length);
1da177e4
LT
1654 goto exit;
1655 }
1656
2742fd88
AC
1657 port_number = TIUMP_GET_PORT_FROM_CODE(data[0]);
1658 function = TIUMP_GET_FUNC_FROM_CODE(data[0]);
1659 dbg("%s - port_number %d, function %d, info 0x%x",
441b62c1 1660 __func__, port_number, function, data[1]);
1da177e4
LT
1661 port = edge_serial->serial->port[port_number];
1662 edge_port = usb_get_serial_port_data(port);
1663 if (!edge_port) {
2742fd88 1664 dbg("%s - edge_port not found", __func__);
1da177e4
LT
1665 return;
1666 }
1667 switch (function) {
1668 case TIUMP_INTERRUPT_CODE_LSR:
2742fd88 1669 lsr = map_line_status(data[1]);
1da177e4 1670 if (lsr & UMP_UART_LSR_DATA_MASK) {
2742fd88
AC
1671 /* Save the LSR event for bulk read
1672 completion routine */
1673 dbg("%s - LSR Event Port %u LSR Status = %02x",
441b62c1 1674 __func__, port_number, lsr);
1da177e4
LT
1675 edge_port->lsr_event = 1;
1676 edge_port->lsr_mask = lsr;
1677 } else {
2742fd88 1678 dbg("%s - ===== Port %d LSR Status = %02x ======",
441b62c1 1679 __func__, port_number, lsr);
2742fd88 1680 handle_new_lsr(edge_port, 0, lsr, 0);
1da177e4
LT
1681 }
1682 break;
1683
2742fd88 1684 case TIUMP_INTERRUPT_CODE_MSR: /* MSR */
1da177e4
LT
1685 /* Copy MSR from UMP */
1686 msr = data[1];
759f3634 1687 dbg("%s - ===== Port %u MSR Status = %02x ======",
441b62c1 1688 __func__, port_number, msr);
2742fd88 1689 handle_new_msr(edge_port, msr);
1da177e4
LT
1690 break;
1691
1692 default:
2742fd88
AC
1693 dev_err(&urb->dev->dev,
1694 "%s - Unknown Interrupt code from UMP %x\n",
1695 __func__, data[1]);
1da177e4 1696 break;
2742fd88 1697
1da177e4
LT
1698 }
1699
1700exit:
2742fd88 1701 retval = usb_submit_urb(urb, GFP_ATOMIC);
ee337c21 1702 if (retval)
2742fd88
AC
1703 dev_err(&urb->dev->dev,
1704 "%s - usb_submit_urb failed with result %d\n",
441b62c1 1705 __func__, retval);
1da177e4
LT
1706}
1707
2742fd88 1708static void edge_bulk_in_callback(struct urb *urb)
1da177e4 1709{
cdc97792 1710 struct edgeport_port *edge_port = urb->context;
1da177e4
LT
1711 unsigned char *data = urb->transfer_buffer;
1712 struct tty_struct *tty;
ee337c21 1713 int retval = 0;
1da177e4 1714 int port_number;
ee337c21 1715 int status = urb->status;
1da177e4 1716
441b62c1 1717 dbg("%s", __func__);
1da177e4 1718
ee337c21 1719 switch (status) {
1da177e4
LT
1720 case 0:
1721 /* success */
1722 break;
1723 case -ECONNRESET:
1724 case -ENOENT:
1725 case -ESHUTDOWN:
1726 /* this urb is terminated, clean up */
ee337c21 1727 dbg("%s - urb shutting down with status: %d",
441b62c1 1728 __func__, status);
1da177e4
LT
1729 return;
1730 default:
2742fd88
AC
1731 dev_err(&urb->dev->dev,
1732 "%s - nonzero read bulk status received: %d\n",
1733 __func__, status);
1da177e4
LT
1734 }
1735
ee337c21 1736 if (status == -EPIPE)
1da177e4
LT
1737 goto exit;
1738
ee337c21 1739 if (status) {
2742fd88 1740 dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__);
1da177e4
LT
1741 return;
1742 }
1743
1744 port_number = edge_port->port->number - edge_port->port->serial->minor;
1745
1746 if (edge_port->lsr_event) {
1747 edge_port->lsr_event = 0;
2742fd88 1748 dbg("%s ===== Port %u LSR Status = %02x, Data = %02x ======",
441b62c1 1749 __func__, port_number, edge_port->lsr_mask, *data);
2742fd88 1750 handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data);
1da177e4
LT
1751 /* Adjust buffer length/pointer */
1752 --urb->actual_length;
1753 ++data;
1754 }
1755
4a90f09b 1756 tty = tty_port_tty_get(&edge_port->port->port);
1da177e4 1757 if (tty && urb->actual_length) {
2742fd88
AC
1758 usb_serial_debug_data(debug, &edge_port->port->dev,
1759 __func__, urb->actual_length, data);
1760 if (edge_port->close_pending)
1761 dbg("%s - close pending, dropping data on the floor",
1762 __func__);
1763 else
1764 edge_tty_recv(&edge_port->port->dev, tty, data,
1765 urb->actual_length);
1da177e4
LT
1766 edge_port->icount.rx += urb->actual_length;
1767 }
4a90f09b 1768 tty_kref_put(tty);
1da177e4
LT
1769
1770exit:
1771 /* continue read unless stopped */
1772 spin_lock(&edge_port->ep_lock);
5833041f 1773 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
ee337c21 1774 retval = usb_submit_urb(urb, GFP_ATOMIC);
5833041f 1775 else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING)
1da177e4 1776 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
5833041f 1777
1da177e4 1778 spin_unlock(&edge_port->ep_lock);
ee337c21 1779 if (retval)
2742fd88
AC
1780 dev_err(&urb->dev->dev,
1781 "%s - usb_submit_urb failed with result %d\n",
441b62c1 1782 __func__, retval);
1da177e4
LT
1783}
1784
2742fd88
AC
1785static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
1786 unsigned char *data, int length)
1da177e4 1787{
2742fd88 1788 int queued;
1da177e4 1789
2742fd88
AC
1790 queued = tty_insert_flip_string(tty, data, length);
1791 if (queued < length)
1792 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1793 __func__, length - queued);
1da177e4
LT
1794 tty_flip_buffer_push(tty);
1795}
1796
2742fd88 1797static void edge_bulk_out_callback(struct urb *urb)
1da177e4 1798{
cdc97792 1799 struct usb_serial_port *port = urb->context;
1da177e4 1800 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
ee337c21 1801 int status = urb->status;
4a90f09b 1802 struct tty_struct *tty;
1da177e4 1803
2742fd88 1804 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
1805
1806 edge_port->ep_write_urb_in_use = 0;
1807
ee337c21 1808 switch (status) {
1da177e4
LT
1809 case 0:
1810 /* success */
1811 break;
1812 case -ECONNRESET:
1813 case -ENOENT:
1814 case -ESHUTDOWN:
1815 /* this urb is terminated, clean up */
ee337c21 1816 dbg("%s - urb shutting down with status: %d",
441b62c1 1817 __func__, status);
1da177e4
LT
1818 return;
1819 default:
ee337c21 1820 dev_err(&urb->dev->dev, "%s - nonzero write bulk status "
441b62c1 1821 "received: %d\n", __func__, status);
1da177e4
LT
1822 }
1823
1824 /* send any buffered data */
4a90f09b
AC
1825 tty = tty_port_tty_get(&port->port);
1826 edge_send(tty);
1827 tty_kref_put(tty);
1da177e4
LT
1828}
1829
a509a7e4 1830static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
1da177e4
LT
1831{
1832 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1833 struct edgeport_serial *edge_serial;
1834 struct usb_device *dev;
1835 struct urb *urb;
1836 int port_number;
1837 int status;
1838 u16 open_settings;
1839 u8 transaction_timeout;
1840
441b62c1 1841 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
1842
1843 if (edge_port == NULL)
1844 return -ENODEV;
1845
1da177e4
LT
1846 port_number = port->number - port->serial->minor;
1847 switch (port_number) {
2742fd88
AC
1848 case 0:
1849 edge_port->uart_base = UMPMEM_BASE_UART1;
1850 edge_port->dma_address = UMPD_OEDB1_ADDRESS;
1851 break;
1852 case 1:
1853 edge_port->uart_base = UMPMEM_BASE_UART2;
1854 edge_port->dma_address = UMPD_OEDB2_ADDRESS;
1855 break;
1856 default:
1857 dev_err(&port->dev, "Unknown port number!!!\n");
1858 return -ENODEV;
1da177e4
LT
1859 }
1860
2742fd88
AC
1861 dbg("%s - port_number = %d, uart_base = %04x, dma_address = %04x",
1862 __func__, port_number, edge_port->uart_base,
1863 edge_port->dma_address);
1da177e4
LT
1864
1865 dev = port->serial->dev;
1866
2742fd88
AC
1867 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
1868 init_waitqueue_head(&edge_port->delta_msr_wait);
1da177e4
LT
1869
1870 /* turn off loopback */
2742fd88 1871 status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0);
1da177e4 1872 if (status) {
2742fd88
AC
1873 dev_err(&port->dev,
1874 "%s - cannot send clear loopback command, %d\n",
441b62c1 1875 __func__, status);
1da177e4
LT
1876 return status;
1877 }
2742fd88 1878
1da177e4 1879 /* set up the port settings */
95da310e 1880 if (tty)
4a90f09b 1881 edge_set_termios(tty, port, tty->termios);
1da177e4
LT
1882
1883 /* open up the port */
1884
1885 /* milliseconds to timeout for DMA transfer */
1886 transaction_timeout = 2;
1887
2742fd88
AC
1888 edge_port->ump_read_timeout =
1889 max(20, ((transaction_timeout * 3) / 2));
1da177e4 1890
2742fd88
AC
1891 /* milliseconds to timeout for DMA transfer */
1892 open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
1893 UMP_PIPE_TRANS_TIMEOUT_ENA |
1da177e4
LT
1894 (transaction_timeout << 2));
1895
2742fd88 1896 dbg("%s - Sending UMPC_OPEN_PORT", __func__);
1da177e4
LT
1897
1898 /* Tell TI to open and start the port */
2742fd88
AC
1899 status = send_cmd(dev, UMPC_OPEN_PORT,
1900 (u8)(UMPM_UART1_PORT + port_number), open_settings, NULL, 0);
1da177e4 1901 if (status) {
2742fd88
AC
1902 dev_err(&port->dev, "%s - cannot send open command, %d\n",
1903 __func__, status);
1da177e4
LT
1904 return status;
1905 }
1906
1907 /* Start the DMA? */
2742fd88
AC
1908 status = send_cmd(dev, UMPC_START_PORT,
1909 (u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
1da177e4 1910 if (status) {
2742fd88
AC
1911 dev_err(&port->dev, "%s - cannot send start DMA command, %d\n",
1912 __func__, status);
1da177e4
LT
1913 return status;
1914 }
1915
1916 /* Clear TX and RX buffers in UMP */
2742fd88 1917 status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
1da177e4 1918 if (status) {
2742fd88
AC
1919 dev_err(&port->dev,
1920 "%s - cannot send clear buffers command, %d\n",
1921 __func__, status);
1da177e4
LT
1922 return status;
1923 }
1924
1925 /* Read Initial MSR */
2742fd88
AC
1926 status = ti_vread_sync(dev, UMPC_READ_MSR, 0,
1927 (__u16)(UMPM_UART1_PORT + port_number),
1928 &edge_port->shadow_msr, 1);
1da177e4 1929 if (status) {
2742fd88
AC
1930 dev_err(&port->dev, "%s - cannot send read MSR command, %d\n",
1931 __func__, status);
1da177e4
LT
1932 return status;
1933 }
1934
2742fd88
AC
1935 dbg("ShadowMSR 0x%X", edge_port->shadow_msr);
1936
1da177e4
LT
1937 /* Set Initial MCR */
1938 edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
2742fd88 1939 dbg("ShadowMCR 0x%X", edge_port->shadow_mcr);
1da177e4
LT
1940
1941 edge_serial = edge_port->edge_serial;
241ca64f 1942 if (mutex_lock_interruptible(&edge_serial->es_lock))
1da177e4
LT
1943 return -ERESTARTSYS;
1944 if (edge_serial->num_ports_open == 0) {
2742fd88 1945 /* we are the first port to open, post the interrupt urb */
1da177e4
LT
1946 urb = edge_serial->serial->port[0]->interrupt_in_urb;
1947 if (!urb) {
2742fd88
AC
1948 dev_err(&port->dev,
1949 "%s - no interrupt urb present, exiting\n",
1950 __func__);
1da177e4 1951 status = -EINVAL;
241ca64f 1952 goto release_es_lock;
1da177e4
LT
1953 }
1954 urb->complete = edge_interrupt_callback;
1955 urb->context = edge_serial;
2742fd88 1956 status = usb_submit_urb(urb, GFP_KERNEL);
1da177e4 1957 if (status) {
2742fd88
AC
1958 dev_err(&port->dev,
1959 "%s - usb_submit_urb failed with value %d\n",
1960 __func__, status);
241ca64f 1961 goto release_es_lock;
1da177e4
LT
1962 }
1963 }
1964
1965 /*
1966 * reset the data toggle on the bulk endpoints to work around bug in
1967 * host controllers where things get out of sync some times
1968 */
2742fd88
AC
1969 usb_clear_halt(dev, port->write_urb->pipe);
1970 usb_clear_halt(dev, port->read_urb->pipe);
1da177e4
LT
1971
1972 /* start up our bulk read urb */
1973 urb = port->read_urb;
1974 if (!urb) {
2742fd88
AC
1975 dev_err(&port->dev, "%s - no read urb present, exiting\n",
1976 __func__);
1da177e4
LT
1977 status = -EINVAL;
1978 goto unlink_int_urb;
1979 }
1980 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
1981 urb->complete = edge_bulk_in_callback;
1982 urb->context = edge_port;
2742fd88 1983 status = usb_submit_urb(urb, GFP_KERNEL);
1da177e4 1984 if (status) {
2742fd88
AC
1985 dev_err(&port->dev,
1986 "%s - read bulk usb_submit_urb failed with value %d\n",
1987 __func__, status);
1da177e4
LT
1988 goto unlink_int_urb;
1989 }
1990
1991 ++edge_serial->num_ports_open;
1992
441b62c1 1993 dbg("%s - exited", __func__);
1da177e4 1994
241ca64f 1995 goto release_es_lock;
1da177e4
LT
1996
1997unlink_int_urb:
1998 if (edge_port->edge_serial->num_ports_open == 0)
1999 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
241ca64f
MK
2000release_es_lock:
2001 mutex_unlock(&edge_serial->es_lock);
1da177e4
LT
2002 return status;
2003}
2004
335f8514 2005static void edge_close(struct usb_serial_port *port)
1da177e4
LT
2006{
2007 struct edgeport_serial *edge_serial;
2008 struct edgeport_port *edge_port;
2009 int port_number;
2010 int status;
2011
441b62c1 2012 dbg("%s - port %d", __func__, port->number);
2742fd88 2013
1da177e4
LT
2014 edge_serial = usb_get_serial_data(port->serial);
2015 edge_port = usb_get_serial_port_data(port);
2742fd88 2016 if (edge_serial == NULL || edge_port == NULL)
1da177e4 2017 return;
2742fd88
AC
2018
2019 /* The bulkreadcompletion routine will check
1da177e4
LT
2020 * this flag and dump add read data */
2021 edge_port->close_pending = 1;
2022
2023 /* chase the port close and flush */
2742fd88 2024 chase_port(edge_port, (HZ * closing_wait) / 100, 1);
1da177e4
LT
2025
2026 usb_kill_urb(port->read_urb);
2027 usb_kill_urb(port->write_urb);
2028 edge_port->ep_write_urb_in_use = 0;
2029
2030 /* assuming we can still talk to the device,
2031 * send a close port command to it */
441b62c1 2032 dbg("%s - send umpc_close_port", __func__);
1da177e4 2033 port_number = port->number - port->serial->minor;
2742fd88 2034 status = send_cmd(port->serial->dev,
1da177e4
LT
2035 UMPC_CLOSE_PORT,
2036 (__u8)(UMPM_UART1_PORT + port_number),
2037 0,
2038 NULL,
2039 0);
241ca64f 2040 mutex_lock(&edge_serial->es_lock);
1da177e4
LT
2041 --edge_port->edge_serial->num_ports_open;
2042 if (edge_port->edge_serial->num_ports_open <= 0) {
2043 /* last port is now closed, let's shut down our interrupt urb */
2044 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2045 edge_port->edge_serial->num_ports_open = 0;
2046 }
241ca64f 2047 mutex_unlock(&edge_serial->es_lock);
1da177e4
LT
2048 edge_port->close_pending = 0;
2049
441b62c1 2050 dbg("%s - exited", __func__);
1da177e4
LT
2051}
2052
95da310e
AC
2053static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
2054 const unsigned char *data, int count)
1da177e4
LT
2055{
2056 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1da177e4 2057
441b62c1 2058 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
2059
2060 if (count == 0) {
441b62c1 2061 dbg("%s - write request of 0 bytes", __func__);
1da177e4
LT
2062 return 0;
2063 }
2064
2065 if (edge_port == NULL)
2066 return -ENODEV;
2067 if (edge_port->close_pending == 1)
2068 return -ENODEV;
2069
d733cec1
JH
2070 count = kfifo_in_locked(&edge_port->write_fifo, data, count,
2071 &edge_port->ep_lock);
95da310e 2072 edge_send(tty);
1da177e4
LT
2073
2074 return count;
2075}
2076
95da310e 2077static void edge_send(struct tty_struct *tty)
1da177e4 2078{
95da310e 2079 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
2080 int count, result;
2081 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1da177e4
LT
2082 unsigned long flags;
2083
2084
441b62c1 2085 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
2086
2087 spin_lock_irqsave(&edge_port->ep_lock, flags);
2088
2089 if (edge_port->ep_write_urb_in_use) {
2090 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2091 return;
2092 }
2093
d733cec1 2094 count = kfifo_out(&edge_port->write_fifo,
1da177e4
LT
2095 port->write_urb->transfer_buffer,
2096 port->bulk_out_size);
2097
2098 if (count == 0) {
2099 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2100 return;
2101 }
2102
2103 edge_port->ep_write_urb_in_use = 1;
2104
2105 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2106
2742fd88
AC
2107 usb_serial_debug_data(debug, &port->dev, __func__, count,
2108 port->write_urb->transfer_buffer);
1da177e4
LT
2109
2110 /* set up our urb */
2742fd88
AC
2111 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
2112 usb_sndbulkpipe(port->serial->dev,
1da177e4
LT
2113 port->bulk_out_endpointAddress),
2114 port->write_urb->transfer_buffer, count,
2115 edge_bulk_out_callback,
2116 port);
2117
2118 /* send the data out the bulk port */
2119 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2120 if (result) {
2742fd88
AC
2121 dev_err(&port->dev,
2122 "%s - failed submitting write urb, error %d\n",
2123 __func__, result);
1da177e4 2124 edge_port->ep_write_urb_in_use = 0;
2742fd88
AC
2125 /* TODO: reschedule edge_send */
2126 } else
1da177e4 2127 edge_port->icount.tx += count;
1da177e4
LT
2128
2129 /* wakeup any process waiting for writes to complete */
2130 /* there is now more room in the buffer for new writes */
2742fd88 2131 if (tty)
1da177e4 2132 tty_wakeup(tty);
1da177e4
LT
2133}
2134
95da310e 2135static int edge_write_room(struct tty_struct *tty)
1da177e4 2136{
95da310e 2137 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
2138 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2139 int room = 0;
2140 unsigned long flags;
2141
441b62c1 2142 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
2143
2144 if (edge_port == NULL)
2742fd88 2145 return 0;
1da177e4 2146 if (edge_port->close_pending == 1)
2742fd88 2147 return 0;
1da177e4
LT
2148
2149 spin_lock_irqsave(&edge_port->ep_lock, flags);
d733cec1 2150 room = kfifo_avail(&edge_port->write_fifo);
1da177e4
LT
2151 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2152
441b62c1 2153 dbg("%s - returns %d", __func__, room);
1da177e4
LT
2154 return room;
2155}
2156
95da310e 2157static int edge_chars_in_buffer(struct tty_struct *tty)
1da177e4 2158{
95da310e 2159 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
2160 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2161 int chars = 0;
2162 unsigned long flags;
2163
441b62c1 2164 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
2165
2166 if (edge_port == NULL)
2742fd88 2167 return 0;
1da177e4 2168 if (edge_port->close_pending == 1)
2742fd88 2169 return 0;
1da177e4
LT
2170
2171 spin_lock_irqsave(&edge_port->ep_lock, flags);
d733cec1 2172 chars = kfifo_len(&edge_port->write_fifo);
1da177e4
LT
2173 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2174
2742fd88 2175 dbg("%s - returns %d", __func__, chars);
1da177e4
LT
2176 return chars;
2177}
2178
95da310e 2179static void edge_throttle(struct tty_struct *tty)
1da177e4 2180{
95da310e 2181 struct usb_serial_port *port = tty->driver_data;
1da177e4 2182 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1da177e4
LT
2183 int status;
2184
441b62c1 2185 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
2186
2187 if (edge_port == NULL)
2188 return;
2189
1da177e4
LT
2190 /* if we are implementing XON/XOFF, send the stop character */
2191 if (I_IXOFF(tty)) {
2192 unsigned char stop_char = STOP_CHAR(tty);
95da310e
AC
2193 status = edge_write(tty, port, &stop_char, 1);
2194 if (status <= 0) {
2195 dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status);
2196 }
1da177e4
LT
2197 }
2198
2199 /* if we are implementing RTS/CTS, stop reads */
2200 /* and the Edgeport will clear the RTS line */
2201 if (C_CRTSCTS(tty))
2202 stop_read(edge_port);
2203
2204}
2205
95da310e 2206static void edge_unthrottle(struct tty_struct *tty)
1da177e4 2207{
95da310e 2208 struct usb_serial_port *port = tty->driver_data;
1da177e4 2209 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1da177e4
LT
2210 int status;
2211
441b62c1 2212 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
2213
2214 if (edge_port == NULL)
2215 return;
2216
1da177e4
LT
2217 /* if we are implementing XON/XOFF, send the start character */
2218 if (I_IXOFF(tty)) {
2219 unsigned char start_char = START_CHAR(tty);
95da310e
AC
2220 status = edge_write(tty, port, &start_char, 1);
2221 if (status <= 0) {
2222 dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status);
2223 }
1da177e4 2224 }
1da177e4
LT
2225 /* if we are implementing RTS/CTS, restart reads */
2226 /* are the Edgeport will assert the RTS line */
2227 if (C_CRTSCTS(tty)) {
2228 status = restart_read(edge_port);
2229 if (status)
2742fd88
AC
2230 dev_err(&port->dev,
2231 "%s - read bulk usb_submit_urb failed: %d\n",
2232 __func__, status);
1da177e4
LT
2233 }
2234
2235}
2236
2237static void stop_read(struct edgeport_port *edge_port)
2238{
2239 unsigned long flags;
2240
2241 spin_lock_irqsave(&edge_port->ep_lock, flags);
2242
2243 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
2244 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
2245 edge_port->shadow_mcr &= ~MCR_RTS;
2246
2247 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2248}
2249
2250static int restart_read(struct edgeport_port *edge_port)
2251{
2252 struct urb *urb;
2253 int status = 0;
2254 unsigned long flags;
2255
2256 spin_lock_irqsave(&edge_port->ep_lock, flags);
2257
2258 if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
2259 urb = edge_port->port->read_urb;
2260 urb->complete = edge_bulk_in_callback;
2261 urb->context = edge_port;
efdff608 2262 status = usb_submit_urb(urb, GFP_ATOMIC);
1da177e4
LT
2263 }
2264 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2265 edge_port->shadow_mcr |= MCR_RTS;
2266
2267 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2268
2269 return status;
2270}
2271
95da310e
AC
2272static void change_port_settings(struct tty_struct *tty,
2273 struct edgeport_port *edge_port, struct ktermios *old_termios)
1da177e4
LT
2274{
2275 struct ump_uart_config *config;
1da177e4
LT
2276 int baud;
2277 unsigned cflag;
2278 int status;
2742fd88
AC
2279 int port_number = edge_port->port->number -
2280 edge_port->port->serial->minor;
1da177e4 2281
441b62c1 2282 dbg("%s - port %d", __func__, edge_port->port->number);
1da177e4 2283
95da310e 2284 config = kmalloc (sizeof (*config), GFP_KERNEL);
1da177e4 2285 if (!config) {
2742fd88
AC
2286 *tty->termios = *old_termios;
2287 dev_err(&edge_port->port->dev, "%s - out of memory\n",
2288 __func__);
1da177e4
LT
2289 return;
2290 }
2291
2292 cflag = tty->termios->c_cflag;
2293
2294 config->wFlags = 0;
2295
2296 /* These flags must be set */
2297 config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
2298 config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
2299 config->bUartMode = (__u8)(edge_port->bUartMode);
2300
2301 switch (cflag & CSIZE) {
2742fd88
AC
2302 case CS5:
2303 config->bDataBits = UMP_UART_CHAR5BITS;
2304 dbg("%s - data bits = 5", __func__);
2305 break;
2306 case CS6:
2307 config->bDataBits = UMP_UART_CHAR6BITS;
2308 dbg("%s - data bits = 6", __func__);
2309 break;
2310 case CS7:
2311 config->bDataBits = UMP_UART_CHAR7BITS;
2312 dbg("%s - data bits = 7", __func__);
2313 break;
2314 default:
2315 case CS8:
2316 config->bDataBits = UMP_UART_CHAR8BITS;
2317 dbg("%s - data bits = 8", __func__);
1da177e4
LT
2318 break;
2319 }
2320
2321 if (cflag & PARENB) {
2322 if (cflag & PARODD) {
2323 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2324 config->bParity = UMP_UART_ODDPARITY;
441b62c1 2325 dbg("%s - parity = odd", __func__);
1da177e4
LT
2326 } else {
2327 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2328 config->bParity = UMP_UART_EVENPARITY;
441b62c1 2329 dbg("%s - parity = even", __func__);
1da177e4
LT
2330 }
2331 } else {
2742fd88 2332 config->bParity = UMP_UART_NOPARITY;
441b62c1 2333 dbg("%s - parity = none", __func__);
1da177e4
LT
2334 }
2335
2336 if (cflag & CSTOPB) {
2337 config->bStopBits = UMP_UART_STOPBIT2;
441b62c1 2338 dbg("%s - stop bits = 2", __func__);
1da177e4
LT
2339 } else {
2340 config->bStopBits = UMP_UART_STOPBIT1;
441b62c1 2341 dbg("%s - stop bits = 1", __func__);
1da177e4
LT
2342 }
2343
2344 /* figure out the flow control settings */
2345 if (cflag & CRTSCTS) {
2346 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
2347 config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
441b62c1 2348 dbg("%s - RTS/CTS is enabled", __func__);
1da177e4 2349 } else {
441b62c1 2350 dbg("%s - RTS/CTS is disabled", __func__);
1da177e4
LT
2351 tty->hw_stopped = 0;
2352 restart_read(edge_port);
2353 }
2354
2742fd88
AC
2355 /* if we are implementing XON/XOFF, set the start and stop
2356 character in the device */
2357 config->cXon = START_CHAR(tty);
2358 config->cXoff = STOP_CHAR(tty);
1da177e4 2359
2742fd88
AC
2360 /* if we are implementing INBOUND XON/XOFF */
2361 if (I_IXOFF(tty)) {
2362 config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
2363 dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2364 __func__, config->cXon, config->cXoff);
2365 } else
2366 dbg("%s - INBOUND XON/XOFF is disabled", __func__);
1da177e4 2367
2742fd88
AC
2368 /* if we are implementing OUTBOUND XON/XOFF */
2369 if (I_IXON(tty)) {
2370 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
2371 dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2372 __func__, config->cXon, config->cXoff);
2373 } else
2374 dbg("%s - OUTBOUND XON/XOFF is disabled", __func__);
1da177e4 2375
d5f5bcd4
AC
2376 tty->termios->c_cflag &= ~CMSPAR;
2377
1da177e4
LT
2378 /* Round the baud rate */
2379 baud = tty_get_baud_rate(tty);
2380 if (!baud) {
2381 /* pick a default, any default... */
2382 baud = 9600;
d5f5bcd4
AC
2383 } else
2384 tty_encode_baud_rate(tty, baud, baud);
2385
1da177e4
LT
2386 edge_port->baud_rate = baud;
2387 config->wBaudRate = (__u16)((461550L + baud/2) / baud);
2388
d5f5bcd4
AC
2389 /* FIXME: Recompute actual baud from divisor here */
2390
2742fd88
AC
2391 dbg("%s - baud rate = %d, wBaudRate = %d", __func__, baud,
2392 config->wBaudRate);
1da177e4 2393
2742fd88
AC
2394 dbg("wBaudRate: %d", (int)(461550L / config->wBaudRate));
2395 dbg("wFlags: 0x%x", config->wFlags);
2396 dbg("bDataBits: %d", config->bDataBits);
2397 dbg("bParity: %d", config->bParity);
2398 dbg("bStopBits: %d", config->bStopBits);
2399 dbg("cXon: %d", config->cXon);
2400 dbg("cXoff: %d", config->cXoff);
2401 dbg("bUartMode: %d", config->bUartMode);
1da177e4
LT
2402
2403 /* move the word values into big endian mode */
2742fd88
AC
2404 cpu_to_be16s(&config->wFlags);
2405 cpu_to_be16s(&config->wBaudRate);
1da177e4 2406
2742fd88 2407 status = send_cmd(edge_port->port->serial->dev, UMPC_SET_CONFIG,
1da177e4 2408 (__u8)(UMPM_UART1_PORT + port_number),
2742fd88
AC
2409 0, (__u8 *)config, sizeof(*config));
2410 if (status)
2411 dbg("%s - error %d when trying to write config to device",
441b62c1 2412 __func__, status);
2742fd88 2413 kfree(config);
1da177e4
LT
2414}
2415
2e0ddd62 2416static void edge_set_termios(struct tty_struct *tty,
95da310e 2417 struct usb_serial_port *port, struct ktermios *old_termios)
1da177e4
LT
2418{
2419 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
95da310e
AC
2420 unsigned int cflag;
2421
2422 cflag = tty->termios->c_cflag;
1da177e4 2423
441b62c1 2424 dbg("%s - clfag %08x iflag %08x", __func__,
1da177e4 2425 tty->termios->c_cflag, tty->termios->c_iflag);
441b62c1 2426 dbg("%s - old clfag %08x old iflag %08x", __func__,
d5f5bcd4 2427 old_termios->c_cflag, old_termios->c_iflag);
441b62c1 2428 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
2429
2430 if (edge_port == NULL)
2431 return;
1da177e4 2432 /* change the port settings to the new ones specified */
95da310e 2433 change_port_settings(tty, edge_port, old_termios);
1da177e4
LT
2434}
2435
20b9d177 2436static int edge_tiocmset(struct tty_struct *tty,
2742fd88 2437 unsigned int set, unsigned int clear)
1da177e4 2438{
95da310e 2439 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
2440 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2441 unsigned int mcr;
3d71fe0b 2442 unsigned long flags;
1da177e4 2443
441b62c1 2444 dbg("%s - port %d", __func__, port->number);
1da177e4 2445
3d71fe0b 2446 spin_lock_irqsave(&edge_port->ep_lock, flags);
1da177e4
LT
2447 mcr = edge_port->shadow_mcr;
2448 if (set & TIOCM_RTS)
2449 mcr |= MCR_RTS;
2450 if (set & TIOCM_DTR)
2451 mcr |= MCR_DTR;
2452 if (set & TIOCM_LOOP)
2453 mcr |= MCR_LOOPBACK;
2454
2455 if (clear & TIOCM_RTS)
2456 mcr &= ~MCR_RTS;
2457 if (clear & TIOCM_DTR)
2458 mcr &= ~MCR_DTR;
2459 if (clear & TIOCM_LOOP)
2460 mcr &= ~MCR_LOOPBACK;
2461
2462 edge_port->shadow_mcr = mcr;
3d71fe0b 2463 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1da177e4 2464
2742fd88 2465 restore_mcr(edge_port, mcr);
1da177e4
LT
2466 return 0;
2467}
2468
60b33c13 2469static int edge_tiocmget(struct tty_struct *tty)
1da177e4 2470{
95da310e 2471 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
2472 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2473 unsigned int result = 0;
2474 unsigned int msr;
2475 unsigned int mcr;
3d71fe0b 2476 unsigned long flags;
1da177e4 2477
441b62c1 2478 dbg("%s - port %d", __func__, port->number);
1da177e4 2479
3d71fe0b
AC
2480 spin_lock_irqsave(&edge_port->ep_lock, flags);
2481
1da177e4
LT
2482 msr = edge_port->shadow_msr;
2483 mcr = edge_port->shadow_mcr;
2484 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
2485 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
2486 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
2487 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
2488 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
2489 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
2490
2491
441b62c1 2492 dbg("%s -- %x", __func__, result);
3d71fe0b 2493 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1da177e4
LT
2494
2495 return result;
2496}
2497
0bca1b91
AC
2498static int edge_get_icount(struct tty_struct *tty,
2499 struct serial_icounter_struct *icount)
2500{
2501 struct usb_serial_port *port = tty->driver_data;
2502 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2503 struct async_icount *ic = &edge_port->icount;
2504
2505 icount->cts = ic->cts;
2506 icount->dsr = ic->dsr;
2507 icount->rng = ic->rng;
2508 icount->dcd = ic->dcd;
2509 icount->tx = ic->tx;
2510 icount->rx = ic->rx;
2511 icount->frame = ic->frame;
2512 icount->parity = ic->parity;
2513 icount->overrun = ic->overrun;
2514 icount->brk = ic->brk;
2515 icount->buf_overrun = ic->buf_overrun;
2516 return 0;
2517}
2518
2742fd88
AC
2519static int get_serial_info(struct edgeport_port *edge_port,
2520 struct serial_struct __user *retinfo)
1da177e4
LT
2521{
2522 struct serial_struct tmp;
2523
2524 if (!retinfo)
2525 return -EFAULT;
2526
2527 memset(&tmp, 0, sizeof(tmp));
2528
2529 tmp.type = PORT_16550A;
2530 tmp.line = edge_port->port->serial->minor;
2531 tmp.port = edge_port->port->number;
2532 tmp.irq = 0;
2533 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2534 tmp.xmit_fifo_size = edge_port->port->bulk_out_size;
2535 tmp.baud_base = 9600;
2536 tmp.close_delay = 5*HZ;
2537 tmp.closing_wait = closing_wait;
1da177e4
LT
2538
2539 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2540 return -EFAULT;
2541 return 0;
2542}
2543
00a0d0d6 2544static int edge_ioctl(struct tty_struct *tty,
2742fd88 2545 unsigned int cmd, unsigned long arg)
1da177e4 2546{
95da310e 2547 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
2548 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2549 struct async_icount cnow;
2550 struct async_icount cprev;
2551
441b62c1 2552 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
1da177e4
LT
2553
2554 switch (cmd) {
2742fd88
AC
2555 case TIOCGSERIAL:
2556 dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
2557 return get_serial_info(edge_port,
2558 (struct serial_struct __user *) arg);
2559 case TIOCMIWAIT:
2560 dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
2561 cprev = edge_port->icount;
2562 while (1) {
2563 interruptible_sleep_on(&edge_port->delta_msr_wait);
2564 /* see if a signal did it */
2565 if (signal_pending(current))
2566 return -ERESTARTSYS;
2567 cnow = edge_port->icount;
2568 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2569 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2570 return -EIO; /* no change => error */
2571 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2572 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2573 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2574 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2575 return 0;
1da177e4 2576 }
2742fd88
AC
2577 cprev = cnow;
2578 }
2579 /* not reached */
2580 break;
1da177e4 2581 }
1da177e4
LT
2582 return -ENOIOCTLCMD;
2583}
2584
95da310e 2585static void edge_break(struct tty_struct *tty, int break_state)
1da177e4 2586{
95da310e 2587 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
2588 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2589 int status;
2742fd88 2590 int bv = 0; /* Off */
1da177e4 2591
95da310e 2592 dbg("%s - state = %d", __func__, break_state);
1da177e4
LT
2593
2594 /* chase the port close */
2742fd88 2595 chase_port(edge_port, 0, 0);
1da177e4 2596
95da310e 2597 if (break_state == -1)
2742fd88
AC
2598 bv = 1; /* On */
2599 status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv);
2600 if (status)
2601 dbg("%s - error %d sending break set/clear command.",
441b62c1 2602 __func__, status);
1da177e4
LT
2603}
2604
2742fd88 2605static int edge_startup(struct usb_serial *serial)
1da177e4
LT
2606{
2607 struct edgeport_serial *edge_serial;
2608 struct edgeport_port *edge_port;
2609 struct usb_device *dev;
2610 int status;
2611 int i;
2612
2613 dev = serial->dev;
2614
2615 /* create our private serial structure */
80b6ca48 2616 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
1da177e4 2617 if (edge_serial == NULL) {
441b62c1 2618 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
1da177e4
LT
2619 return -ENOMEM;
2620 }
241ca64f 2621 mutex_init(&edge_serial->es_lock);
1da177e4
LT
2622 edge_serial->serial = serial;
2623 usb_set_serial_data(serial, edge_serial);
2624
2742fd88 2625 status = download_fw(edge_serial);
1da177e4 2626 if (status) {
2742fd88 2627 kfree(edge_serial);
1da177e4
LT
2628 return status;
2629 }
2630
2631 /* set up our port private structures */
2632 for (i = 0; i < serial->num_ports; ++i) {
80b6ca48 2633 edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
1da177e4 2634 if (edge_port == NULL) {
2742fd88
AC
2635 dev_err(&serial->dev->dev, "%s - Out of memory\n",
2636 __func__);
1da177e4
LT
2637 goto cleanup;
2638 }
1da177e4 2639 spin_lock_init(&edge_port->ep_lock);
d733cec1
JH
2640 if (kfifo_alloc(&edge_port->write_fifo, EDGE_OUT_BUF_SIZE,
2641 GFP_KERNEL)) {
2742fd88
AC
2642 dev_err(&serial->dev->dev, "%s - Out of memory\n",
2643 __func__);
1da177e4
LT
2644 kfree(edge_port);
2645 goto cleanup;
2646 }
2647 edge_port->port = serial->port[i];
2648 edge_port->edge_serial = edge_serial;
2649 usb_set_serial_port_data(serial->port[i], edge_port);
fc4cbd75 2650 edge_port->bUartMode = default_uart_mode;
1da177e4 2651 }
2742fd88 2652
1da177e4
LT
2653 return 0;
2654
2655cleanup:
2742fd88 2656 for (--i; i >= 0; --i) {
1da177e4 2657 edge_port = usb_get_serial_port_data(serial->port[i]);
d733cec1 2658 kfifo_free(&edge_port->write_fifo);
1da177e4
LT
2659 kfree(edge_port);
2660 usb_set_serial_port_data(serial->port[i], NULL);
2661 }
2742fd88 2662 kfree(edge_serial);
1da177e4
LT
2663 usb_set_serial_data(serial, NULL);
2664 return -ENOMEM;
2665}
2666
f9c99bb8 2667static void edge_disconnect(struct usb_serial *serial)
1da177e4
LT
2668{
2669 int i;
2670 struct edgeport_port *edge_port;
2671
2742fd88 2672 dbg("%s", __func__);
1da177e4 2673
0d46c007 2674 for (i = 0; i < serial->num_ports; ++i) {
1da177e4 2675 edge_port = usb_get_serial_port_data(serial->port[i]);
fc4cbd75 2676 edge_remove_sysfs_attrs(edge_port->port);
f9c99bb8
AS
2677 }
2678}
2679
2680static void edge_release(struct usb_serial *serial)
2681{
2682 int i;
2683 struct edgeport_port *edge_port;
2684
2685 dbg("%s", __func__);
2686
2687 for (i = 0; i < serial->num_ports; ++i) {
2688 edge_port = usb_get_serial_port_data(serial->port[i]);
d733cec1 2689 kfifo_free(&edge_port->write_fifo);
0d46c007 2690 kfree(edge_port);
1da177e4 2691 }
0d46c007 2692 kfree(usb_get_serial_data(serial));
1da177e4
LT
2693}
2694
2695
fc4cbd75
MP
2696/* Sysfs Attributes */
2697
2698static ssize_t show_uart_mode(struct device *dev,
2699 struct device_attribute *attr, char *buf)
2700{
2701 struct usb_serial_port *port = to_usb_serial_port(dev);
2702 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2703
2704 return sprintf(buf, "%d\n", edge_port->bUartMode);
2705}
2706
2707static ssize_t store_uart_mode(struct device *dev,
2708 struct device_attribute *attr, const char *valbuf, size_t count)
2709{
2710 struct usb_serial_port *port = to_usb_serial_port(dev);
2711 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2712 unsigned int v = simple_strtoul(valbuf, NULL, 0);
2713
441b62c1 2714 dbg("%s: setting uart_mode = %d", __func__, v);
fc4cbd75
MP
2715
2716 if (v < 256)
2717 edge_port->bUartMode = v;
2718 else
441b62c1 2719 dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
fc4cbd75
MP
2720
2721 return count;
2722}
2723
2742fd88
AC
2724static DEVICE_ATTR(uart_mode, S_IWUSR | S_IRUGO, show_uart_mode,
2725 store_uart_mode);
fc4cbd75
MP
2726
2727static int edge_create_sysfs_attrs(struct usb_serial_port *port)
2728{
2729 return device_create_file(&port->dev, &dev_attr_uart_mode);
2730}
2731
2732static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
2733{
2734 device_remove_file(&port->dev, &dev_attr_uart_mode);
2735 return 0;
2736}
2737
2738
ea65370d 2739static struct usb_serial_driver edgeport_1port_device = {
18fcac35 2740 .driver = {
269bda1c
GKH
2741 .owner = THIS_MODULE,
2742 .name = "edgeport_ti_1",
18fcac35 2743 },
269bda1c 2744 .description = "Edgeport TI 1 port adapter",
d9b1b787 2745 .usb_driver = &io_driver,
1da177e4 2746 .id_table = edgeport_1port_id_table,
1da177e4
LT
2747 .num_ports = 1,
2748 .open = edge_open,
2749 .close = edge_close,
2750 .throttle = edge_throttle,
2751 .unthrottle = edge_unthrottle,
2752 .attach = edge_startup,
f9c99bb8
AS
2753 .disconnect = edge_disconnect,
2754 .release = edge_release,
fc4cbd75 2755 .port_probe = edge_create_sysfs_attrs,
1da177e4
LT
2756 .ioctl = edge_ioctl,
2757 .set_termios = edge_set_termios,
2758 .tiocmget = edge_tiocmget,
2759 .tiocmset = edge_tiocmset,
0bca1b91 2760 .get_icount = edge_get_icount,
1da177e4
LT
2761 .write = edge_write,
2762 .write_room = edge_write_room,
2763 .chars_in_buffer = edge_chars_in_buffer,
2764 .break_ctl = edge_break,
2765 .read_int_callback = edge_interrupt_callback,
2766 .read_bulk_callback = edge_bulk_in_callback,
2767 .write_bulk_callback = edge_bulk_out_callback,
2768};
2769
ea65370d 2770static struct usb_serial_driver edgeport_2port_device = {
18fcac35 2771 .driver = {
269bda1c
GKH
2772 .owner = THIS_MODULE,
2773 .name = "edgeport_ti_2",
18fcac35 2774 },
269bda1c 2775 .description = "Edgeport TI 2 port adapter",
d9b1b787 2776 .usb_driver = &io_driver,
1da177e4 2777 .id_table = edgeport_2port_id_table,
1da177e4
LT
2778 .num_ports = 2,
2779 .open = edge_open,
2780 .close = edge_close,
2781 .throttle = edge_throttle,
2782 .unthrottle = edge_unthrottle,
2783 .attach = edge_startup,
f9c99bb8
AS
2784 .disconnect = edge_disconnect,
2785 .release = edge_release,
fc4cbd75 2786 .port_probe = edge_create_sysfs_attrs,
1da177e4
LT
2787 .ioctl = edge_ioctl,
2788 .set_termios = edge_set_termios,
2789 .tiocmget = edge_tiocmget,
2790 .tiocmset = edge_tiocmset,
2791 .write = edge_write,
2792 .write_room = edge_write_room,
2793 .chars_in_buffer = edge_chars_in_buffer,
2794 .break_ctl = edge_break,
2795 .read_int_callback = edge_interrupt_callback,
2796 .read_bulk_callback = edge_bulk_in_callback,
2797 .write_bulk_callback = edge_bulk_out_callback,
2798};
2799
2800
2801static int __init edgeport_init(void)
2802{
2803 int retval;
2804 retval = usb_serial_register(&edgeport_1port_device);
2805 if (retval)
2806 goto failed_1port_device_register;
2807 retval = usb_serial_register(&edgeport_2port_device);
2808 if (retval)
2809 goto failed_2port_device_register;
2810 retval = usb_register(&io_driver);
2742fd88 2811 if (retval)
1da177e4 2812 goto failed_usb_register;
c197a8db
GKH
2813 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
2814 DRIVER_DESC "\n");
1da177e4
LT
2815 return 0;
2816failed_usb_register:
2817 usb_serial_deregister(&edgeport_2port_device);
2818failed_2port_device_register:
2819 usb_serial_deregister(&edgeport_1port_device);
2820failed_1port_device_register:
2821 return retval;
2822}
2823
2742fd88 2824static void __exit edgeport_exit(void)
1da177e4 2825{
2742fd88
AC
2826 usb_deregister(&io_driver);
2827 usb_serial_deregister(&edgeport_1port_device);
2828 usb_serial_deregister(&edgeport_2port_device);
1da177e4
LT
2829}
2830
2831module_init(edgeport_init);
2832module_exit(edgeport_exit);
2833
2834/* Module information */
2835MODULE_AUTHOR(DRIVER_AUTHOR);
2836MODULE_DESCRIPTION(DRIVER_DESC);
2837MODULE_LICENSE("GPL");
d12b219a 2838MODULE_FIRMWARE("edgeport/down3.bin");
1da177e4
LT
2839
2840module_param(debug, bool, S_IRUGO | S_IWUSR);
2841MODULE_PARM_DESC(debug, "Debug enabled or not");
2842
1da177e4
LT
2843module_param(closing_wait, int, S_IRUGO | S_IWUSR);
2844MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
2845
2846module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
2742fd88
AC
2847MODULE_PARM_DESC(ignore_cpu_rev,
2848 "Ignore the cpu revision when connecting to a device");
1da177e4 2849
fc4cbd75
MP
2850module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
2851MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");