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