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