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