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