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